I have a file called settings.php that has all of the settings that the software uses. The problem is that I need users to be able to change the config without editing the file. I know I could do this with preg_replace but with dynamic settings like 'Board name' it would get tricky. I've tried a few things that didn't seem to work. I now you're not going to write the code for me, I just need a starting point.
Settings File
$settings = array (
'home_display'=>'home',
'db_host'=>'localhost',
'db_user'=>'root',
'db_password'=>'',
'db'=>'',
'login_enabled'=>true,
'signup_enabled'=>true,
'site_name'=>'Cheesecake Portal',
'b_url'=>'beta.cheesecakebb.org',
'b_email'=>'symbiote#cheesecakeb.org',
'board_enabled'=>true
);
The idea i have is to rewrite the file on the fly, even if i think this isnt the best way, it's a way.
if( file_exists($mySettingsFile) ){
include($mySettingsFile);
}
else{
$settings = array ( // the default settings array
'home_display'=>'home',
'db_host'=>'localhost',
'db_user'=>'root',
'db_password'=>'',
'db'=>'',
'login_enabled'=>true,
'signup_enabled'=>true,
'site_name'=>'Cheesecake Portal',
'b_url'=>'beta.cheesecakebb.org',
'b_email'=>'symbiote#cheesecakeb.org',
'board_enabled'=>true
);
}
// modifiy your settings here searching the key(s) you want to modify
// then put it in the config file
file_put_contents($mySettingsFile, '<?php '.var_export($settings).' ?>');
I hope this will illustrate my idea good enough to help you.
Related
I am trying to read/write my configuration in the DokuWiki.
The problem that occurs is when I am trying to call $this->getConf('url'); I always get the response from the conf/default.php file.
This is how my files look like:
admin.php
$url = $this->getConf('url');
conf/default.php
$conf['url'] = 'https://www.example.com';
conf/metadata.php
$meta['url'] = array('string', 'url' => 'https://correct-url.com');
And the value of $url always is:
https://www.example.com
I am not sure what I do wrong.
Thanks!
You may have some misunderstanding to DokuWiki's config system.
The config, which is editable by users, will be saved at /conf/local.php (not inside plugins!). The plugin can only provide a default value at default.php, while the metadata.php is to define how the value is shown on the frontend settings manager.
In your case, the correct URL will be shown, if the DokuWiki global config file (/conf/local.php for example) includes $conf["your_plugin_name"]["url"] = "https://correct-url.com";.
For more: https://www.dokuwiki.org/devel:configuration
I stored several values in new fields of my plugin in the settings.php
However now I'm trying to put those values that I made onscreen, and I couldn't find a way to do that in moodle. Is there a way to do this in Moodle?
Any help is greatly appreciated. Thanks!
If you named the setting in settings.php something like 'PLUGINNAME/SETTINGNAME' (e.g. in the enrol_manual core plugin has a setting called 'enrol_manual/expiredaction'), then you can retrieve a single setting via:
$value = get_config('PLUGINNAME', 'SETTINGNAME');
$value = get_config('enrol_maual', 'expiredaction'); // For example.
If you want all the settings for a given plugin, then you can call:
$values = get_config('PLUGINNAME');
$values = get_config('enrol_manual'); // For example.
If, however, you've followed the bad practice of several of the settings for older core plugins, and the setting is called something like 'MYPLUGIN_SETTINGNAME', then you can retrieve the setting by calling:
$value = get_config('core', 'PLUGINNAME_SETTINGNAME');
$value = get_config('core', 'forum_displaymode'); // For example.
OR
global $CFG;
$value = $CFG->PLUGINNAME_SETTINGNAME;
$value = $CFG->forum_displaymode; // For example.
Naming settings without the '/' is bad, as it means the settings are loaded into the main $CFG global, which is already pretty bloated. Organising them into plugins also means all the plugin settings can be loaded as a simple object.
I want to create some variables / options to customize my wordpress theme.
At the moment I already have 5 variables which I set inside the functions.php file:
$isBoxedLayout = true;
$postCounter = 0;
$postDate = false;
$socialButtons;
$showCatLink = false;
I access them throughout the other files like header.php, content.php etc.:
global $isBoxedLayout;
global $postCounter;
At the top of the files. I think it would be better to have like only one array where I can store it all!? But how do I do it right?
Also, does somebody know how I can access a variable, set in the functions.php, inside the template-tags.php file?
I already tried this:
include '../functions.php'
But I get the following error:
include(../functions.php): failed to open stream: No such file or directory
When I want to access the variable in a file like content.php it works well. Where is the difference?
Maybe not exactly the answer you are looking for, but I would take a look at the Options API. It saves your options into the database and is very easy to use.
// Functions.php
add_option( 'isBoxedLayout', true, '', 'yes' );
// template-tags.php
$isBoxedLayout = get_option( 'isBoxedLayout' );
I have a file called functions.php.
It contains a lot of data in over 3000 lines.
Somewhere in the middle of this file there is code:
/*****************************************
My Custom Code
*****************************************/
if ( function_exists('my_code') )
my_code(array(
'name' => 'First instance',
'description' => 'Hello, hello.',
));
if ( function_exists('my_code') )
my_code(array(
'name' => 'Second instance',
'description' => 'Haha :)',
));
I'm listing all my_code arrays and I'm getting:
First Instance
Second Instance
Now, what I want to achieve, is, when user clicks X next to "First instance" PHP opens functions.php file in the background, finds the exact function and deletes it without touching anything else.
So after deleting "First Instance" functions.php file should look like this:
/*****************************************
My Custom Code
*****************************************/
if ( function_exists('my_code') )
my_code(array(
'name' => 'Second instance',
'description' => 'Haha :)',
));
Any idea how to achieve this? I know how to open files, write, delete, but I'm not sure how to wipe out not only a single line but a few lines around? :)
If your code is always in the format you described you could read each 5 lines from your file and if it's the instance you want to keep, output them to a string. Then write the string back to the original file.
But again yes, code modifying code IS PAIN. Storing your instances in a data structure such as databases or a formatted file is much better.
I think the best way would be to open the file and loop through the lines. You'll need to match each line of your function, or you would need match the first line and track the number of open and close brackets { } to know when you've reached the end of it.
If a line doesn't match you write that out to a new file. If it does match you ignore it. Then finally you make an system call to do a syntax check on the new file (in case something went wrong with your line matching):
system( "php -l newfile.php", &$retval );
Then check the return value $retval to make sure it was ok (it will be exactly equal to 0). If it is okay then you overwrite functions.php with your new file.
if( $retval === 0 ) {
// the syntax is good
rename( "newfile.php", "functions.php");
}
You would need to set the appropriate paths for this to work.
Now all of that said, this is not a very good idea and I would advise you not to implement it. A better method would be to break your functions out into separate files. Then use an INI config file or a database to keep track of what you should load. Either of those have the ability to be edited. Even a text data file would be better than mucking with the actual code.
Once you know what you're supposed to load then at the beginning require or include the appropriate file.
Here's a simple example of doing it with a database:
$res = mysql_query("SELECT file_name FROM load_functions");
if( mysql_error() ) {
// do something because the query failed
}
else {
while( list($file_name) = mysql_fetch_row($res) ) {
if( file_exists($file_name) ) {
require_once( $file_name );
}
else {
// warn because a file requested didn't exist
}
}
}
Hope that helps
I'm having difficulty figuring out how to write a module with a form that uploads files, in Drupal 6. Can anyone explain this, or point me to a good example/documentation discussing it?
EDIT:
Here is entirely what I am trying to do:
User uploads a .csv
Module reads the first line of the file to get fields
User matches csv fields with db fields
Each csv line is saved as a node (preview it first)
So far, I can do 1, 2, and 4 successfully. But it's unclear exactly how the steps should interact with each other ($form_state['redirect']? how should that be used?), and what the best practices are. And for 3, should I save that as session data?
How do I pass the file data between the various steps?
I know that node_import exists, but it's never worked for me, and my bug requests go ignored.
2nd EDIT: I used this at the start and end of every page that needed to deal with the file:
$file = unserialize($_SESSION['file']);
//alter $file object
$_SESSION['file'] = serialize(file);
I'm not sure it it's best practices, but it's been working.
This is not too difficult, you can see some info here. An example of a form with only a file upload.
function myform_form($form_state) {
$form = array('#attributes' => array('enctype' => 'multipart/form-data'));
$form['file'] = array(
'#type' => 'file',
'#title' => t('Upload video'),
'#size' => 48,
'#description' => t('Pick a video file to upload.'),
);
return $form;
}
EDIT:
Now to save the file use the file_save_upload function:
function myform_form_submit($form, $form_state) {
$validators = array();
$file = file_save_upload('file', $validators, 'path');
file_set_status($file, FILE_STATUS_PERMANENT);
}
2nd EDIT:
There's a lot of questions and ways to do the things you described. I wont go to much into the actual code of how to handle a csv file. What I would suggest is that you use the file id to keep track of the file. That would enable you to make urls that take a fid and use that to load the file you want to work on.
To get from your form to the next step, you can use the #redirect form property to get your users to the next step. From there is really depends how you do things, what you'll need to do.