/ Published in: PHP
A generic library to pull application settings from a database for CodeIgniter. Designed to not modify or effect operation of the CI_Config class.
Expand |
Embed | Plain Text
/** * Setting Class * * class accesses application settings from a database. */ class Setting { private $ci = null; // holds our CodeIgniter instance private $items = null; // holds an array of our configuration items private $table = null; // table to pull settings from /** * __cosntruct * * ensures that class is ready for proper function * * @access public * @return void method does not return a result */ function __construct() { // get CodeIgniter instance $this->ci =& get_instance(); /* If settings_table is not set, attempt to load the settings config automatically. This way you rename it to something else to avoid potential conflicts with any configurations in your code base. */ if($this->setting->item('settings_table') === FALSE) { $this->ci->config->load('settings'); } // configure database options and get items. $this->table = $this->ci->config->item('settings_table'); $this->_refresh_items(); } /** * _refresh_items * * refreshes the item array * * @access private * @return void method does not return a result */ private function _refresh_items() { $query = $this->ci->db->get($this->table); if( $query->num_rows() > 0 ) { while($row = $query->fetch_assoc()) { $item = $row['item']; $value = $row['value']; // do some nice filtering $this->items[$item] = $value; } } } /** * set * * sets the value of an item in the database * * @access public * @param string $item item to set * @param string $value value of the item * @return void method does not return a result */ public function set($item, $value) { // do a little pre-filtering if($value === false) $value = '{{false}}'; if($value === true) $value = '{{true}}'; if($value === null) $value = '{{null}}'; if($value == '') $value = '{{empty}}'; // quick test { $this->ci->db->where('item', $item); } else { } $this->_refresh_items(); } /** * get * * retrieves a setting from the database - defaults to value from CI_Config * * - Edited to alternatively attempt to get config items, and to * take in account a few planned changes to the library that I was * too lazy to implement at this time. * ( I just got off work and I'm tired, so sue me. ) * * @access public * @param string $item item to retrieve * @return mixed value of the item if is set, else false */ public function get($item) { // first check if item is in the database { $value = $this->items[$item]; } /* if not in the database, assume that programmer intended to retrieve something from the configuration array which will return false if it is not set. */ else { $value = $this->ci->config->item($item); } return $value; } }
You need to login to post a comment.
