/ Published in: PHP
Get's settings from the database, and adds them to the CodeIgniter config array. Some overwriting may occur.
TODO: filter out things that should not be overwritten, and make it configurable.
Expand |
Embed | Plain Text
/** * Setting Class * * Handles application settings * * @author Mark LaDoux <[email protected]> * @version 1.0.0 * @copyright Copyright (c) 2012, Mark LaDoux * @todo Add grouping capabilities to more closely mimic the config class. */ class Setting { /** * Settings Table Name * * @access protected * @since 1.0.0 * @var string */ protected $table = 'settings'; /** * Holds already loaded values in an array to save on queries * * @access protected * @static array */ /** * Class Constructor * * @access public * @since 1.0.0 * @return void */ public function __construct() { // ensure database is loaded $this->load->database(); // configure library if($this->config->load('settings', TRUE, TRUE)) { if($this->config->item('table', 'settings') !== FALSE) $this->table = $this->config->item('table', 'settings'); } } /** * Retrieve CI instance on demand * * @access public * @param string $param * @return object */ public function __get($param) { $object =& get_instance()->$param; return $object; } /** * Retrieve a setting * * @access public * @since 1.0.0 * @param string $item item to retrieve the value of * @param bool $check report unset items (optional) * @return mixed */ public function item($item, $check = FALSE) { // check if item is already loaded { if($check === TRUE) { return TRUE; } // retrieve value and return result $value = self::$values[$item]; return $value; } // run query $this->db->where('item', $item); $query = $this->db->get($this->table); // get number of rows $rows = $query->num_rows(); if($check === TRUE) { if($rows > 0) { return TRUE; } return FALSE; } if($rows > 0) { $row = $query->row(); $value = $row->value; } else { $value = NULL; } // do some filtering // store result for subsequent calls self::$values[$item] = $value; // return the result return $value; } /** * Set a value to an item * * @access public * @since 1.0.0 * @param string $item item to set * @param mixed $value value of the item * @return void */ public function set($item, $value) { // check if item is set { $item_set = TRUE; } else { $item_set = $this->item($item, TRUE); } // store item for subseqent calls before formatting for the database // so we don't have to unformat it for serving back up self::$values[$item] = $value; // prep value if($value === FALSE ) $value = '{{false}}'; if($value === TRUE ) $value = '{{true}}'; if($value === NULL ) $value = '{{null}}'; if($value == '' ) $value = '{{empty}}'; // perform appropriate action if($item_set === TRUE) { $this->_update_item($item, $value); } else { $this->_create_item($item, $value); } } /** * Updates a setting in the database * * @access protected * @since 1.0.0 * @param string $item item to set * @param string $value value to set * @return void */ protected function _udpate_item($item, $value) { $this->db->where('item', $item); $this->db->update($this->table, $data); } /** * Creates a new setting in the database * * @access protected * @since 1.0.0 * @param string $item item to set * @param string $value value to set * @return void */ protected function _create_item($item, $value) { $this->db->insert($this->table, $data); } } /* End of file Setting.php */ /* Location: ./application/libraries/Setting.php */
You need to login to post a comment.
