/ Published in: PHP
mConfig Database Configuration Library - Formally Settings
updates - Removed CI object variable, simplified, and other general goodness!
Expand |
Embed | Plain Text
/** * mConfig * * Library that handles database settings for mBlog * * @author Mark LaDoux <[email protected]> * @copyright Copyright © 2012, Mark LaDoux * @version 2.0 * @link http://markladoux.com/ */ class mConfig { /** * Configuration Table * * @access protected * @since 1.0 * @static string */ protected static $table; /** * Class Constructor * * Prepares the class for use and loads dependancies * * @access public * @since 2.0 * @return void */ public function __construct() { // Load static settings $this->config->load('mConfig'); self::$table = $this->config->item('mConfig_table'); // Load database $this->load->database(); } /** * Magic __get Function * * Allows us to use the CI Super global without having to use aliases * Shamelessly stolen from Ion Auth * * @access public * @since 2.0 * @param string * @return object */ public function __call($var) { return get_instance()->$var; } /** * item * * Retrieve item from mConfig database * * @access public * @since 2.0 * @param string $item configuration item to retrieve * @param bool $check Report existance of setting * @return mixed */ public function item($item, $check = FALSE) { // prepare query $this->db->where('item', $item); $query = $this->db->get(self::$table); // if we have results, and we are in check mode, return TRUE if($check === TRUE && $query->num_rows() > 0) { return TRUE; } // check results if($query->num_rows() < 1) { // if we are in check mode, return FALSE if($check === TRUE) { return FALSE; } // otherwise return NULL return NULL; } // get data $row = $query->results(); $value = $row->value; // filter data // return data for processing return $value; } /** * delete * * Delete a value * * @access public * @since 2.0 * @param string $item * @return void */ public function delete($item) { $this->db->where('item', $item); $this->db->delete(self::$table); } /** * set * * Set a value * * @access public * @since 1.0 * @param string $item item to set * @param string $value value of item * @return void */ public function set($item, $value) { // prepare value if($value === TRUE) $value = '{{true}}'; if($value === FALSE) $value = '{{false}}'; if($value === NULL) $value = '{{null}}'; if($value == '') $value = ''; // check if value exists $exists = $this->item($item, TRUE); if($exists === TRUE) { $this->_update_item($item, $value); } else { $this->_create_item($item, $value); } } /** * _update_item * * Update a value in the database * * @access protected * @since 2.0 * @param string $item item to update * @param string $value new value to set * @return void */ protected function _update_item($item, $value) { $this->db->where('item', $item); $this->db->update(self::$table, $data); } /** * _create_item * * Create a new item/value pair in the database * * @access protected * @since 2.0 * @param string $item item to create * @param string $value value to set * @return void */ protected function _create_item($item, $value) { // prepare array 'item' => $item, 'value' => $value ); // insert data $this->db->insert(self::$table, $data); } }
You need to login to post a comment.
