[eluser]Phil Sturgeon[/eluser]
This is still PHP so you can still use global variables as you would in any other PHP app, but config items are a good way to go. Remember though, that $_GLOBAL would work even in libraries and helpers where there is no CI instance by default.
[quote author="Robert May" date="1232998445"]Ooh, excellent. Thanks very much.
On a similar line of thought, is there a way to load config data from a database? I want to load user preferences globally, but have yet to figure out how to do it![/quote]
You
could extend the config class but I'm not a fan of that. I made a settings class and model, but you could do it with just a model for a more simple version.
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Settings {
private $CI;
private $items = array();
function __construct() {
$this->CI =& get_instance();
$this->CI->load->module_model('settings', 'settings_m');
}
function item($slug = '') {
// Check for previously called settings and return it if found
if(in_array($slug, array_keys($this->items)))
{
return $this->items[$slug];
}
// Check the database for this setting
if ($setting = $this->CI->settings_m->getSetting($slug))
{
$value = $setting->value;
}
// Not in the database? Try a config value instead
else
{
$value = $this->CI->config->item($slug);
}
// Save the value in the items array and return it
return $this->items[$slug] = $value;
}
function set_item($slug, $value) {
return $this->CI->settings_m->updateSetting($slug, array('value'=>$value));
}
function formControl(&$setting) {
switch($setting->type) {
default:
case 'text':
$form_control = form_input(array('id'=>$setting->slug,
'name'=>$setting->slug,
'value'=>($setting->value)));
break;
case 'textarea':
$form_control = form_textarea(array('id'=>$setting->slug,
'name'=>$setting->slug,
'value'=>($setting->value)));
break;
case 'select':
$form_control = form_dropdown($setting->slug, $this->_formatOptions($setting->options), $setting->value);
break;
case 'checkbox':
case 'radio':
$func = $setting->type == 'checkbox' ? 'form_checkbox' : 'form_radio';
$form_control = '';
foreach($this->_formatOptions($setting->options) as $value => $label):
$form_control .= ' '.form_radio(array('id'=>$setting->slug,
'name'=>$setting->slug,
'checked'=>$setting->value == $value,
'value'=>$value))
. ' '.$label;
endforeach;
break;
case 'function':
$search_for_function = $setting->options;
// Try to find the function named in the DB
if (is_callable(array($this, $search_for_function)))
{
$form_control = $this->$search_for_function($setting);
} else {
$form_control = "Error: Function named ".$search_for_function." not found.";
}
break;
}
return $form_control;
}
function _formatOptions($options = array()) {
$select_array = array();
foreach(explode('|', $options) as $option):
list($value, $name)=explode('=', $option);
$select_array[$value] = $name;
endforeach;
return $select_array;
}
}
?>
Make a model for this too ofc to handle the insert/select. Then you can autoload it and use:
Code:
$this->setting->item('something');
I like to keep config and settings different as they are different things, but this will also default to config values if it can't be found in the db. Just for a bit of flexibility.