Your Ad Here

Posted By

mladoux on 02/01/12


Tagged

codeigniter


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

notturnale
letunglam
fireball70


Settings Class


 / Published in: PHP
 

URL: http://markladoux.com/

A generic library to pull application settings from a database for CodeIgniter. Designed to not modify or effect operation of the CI_Config class.

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. /**
  4.  * Setting Class
  5.  *
  6.  * class accesses application settings from a database.
  7.  */
  8.  
  9. class Setting
  10. {
  11. private $ci = null; // holds our CodeIgniter instance
  12. private $items = null; // holds an array of our configuration items
  13. private $table = null; // table to pull settings from
  14.  
  15. /**
  16.   * __cosntruct
  17.   *
  18.   * ensures that class is ready for proper function
  19.   *
  20.   * @access public
  21.   * @return void method does not return a result
  22.   */
  23.  
  24. function __construct()
  25. {
  26. // get CodeIgniter instance
  27. $this->ci =& get_instance();
  28.  
  29. /*
  30.   If settings_table is not set, attempt to load the settings config
  31.   automatically. This way you rename it to something else to avoid
  32.   potential conflicts with any configurations in your code base.
  33.   */
  34. if($this->setting->item('settings_table') === FALSE)
  35. {
  36. $this->ci->config->load('settings');
  37. }
  38.  
  39. // configure database options and get items.
  40. $this->table = $this->ci->config->item('settings_table');
  41. $this->_refresh_items();
  42. }
  43.  
  44. /**
  45.   * _refresh_items
  46.   *
  47.   * refreshes the item array
  48.   *
  49.   * @access private
  50.   * @return void method does not return a result
  51.   */
  52.  
  53. private function _refresh_items()
  54. {
  55. $query = $this->ci->db->get($this->table);
  56. $this->items = array();
  57. if( $query->num_rows() > 0 )
  58. {
  59. while($row = $query->fetch_assoc())
  60. {
  61. $item = $row['item'];
  62. $value = $row['value'];
  63.  
  64. // do some nice filtering
  65. if(strtolower($value) == '{{false}}') $value = false;
  66. if(strtolower($value) == '{{true}}') $value = true;
  67. if(strtolower($value) == '{{null}}') $value = null;
  68. if(strtolower($value) == '{{empty}}') $value = '';
  69.  
  70. $this->items[$item] = $value;
  71. }
  72. }
  73. }
  74.  
  75. /**
  76.   * set
  77.   *
  78.   * sets the value of an item in the database
  79.   *
  80.   * @access public
  81.   * @param string $item item to set
  82.   * @param string $value value of the item
  83.   * @return void method does not return a result
  84.   */
  85.  
  86. public function set($item, $value)
  87. {
  88. // do a little pre-filtering
  89. if($value === false) $value = '{{false}}';
  90. if($value === true) $value = '{{true}}';
  91. if($value === null) $value = '{{null}}';
  92. if($value == '') $value = '{{empty}}';
  93.  
  94. // quick test
  95. if(isset($this->items[$item]))
  96. {
  97. $this->ci->db->where('item', $item);
  98. $this->ci->db->update($this->table, array('value' => $value));
  99. }
  100. else
  101. {
  102. $this->ci->db->insert($this->table, array('item' => $item, 'value' => $value));
  103. }
  104.  
  105. $this->_refresh_items();
  106. }
  107.  
  108. /**
  109.   * get
  110.   *
  111.   * retrieves a setting from the database - defaults to value from CI_Config
  112.   *
  113.   * - Edited to alternatively attempt to get config items, and to
  114.   * take in account a few planned changes to the library that I was
  115.   * too lazy to implement at this time.
  116.   * ( I just got off work and I'm tired, so sue me. )
  117.   *
  118.   * @access public
  119.   * @param string $item item to retrieve
  120.   * @return mixed value of the item if is set, else false
  121.   */
  122.  
  123. public function get($item) {
  124.  
  125. // first check if item is in the database
  126. if( isset($this->items[$item]))
  127. {
  128. $value = $this->items[$item];
  129. }
  130.  
  131. /*
  132.   if not in the database, assume that programmer intended to retrieve
  133.   something from the configuration array which will return false if
  134.   it is not set.
  135.   */
  136.  
  137. else
  138. {
  139. $value = $this->ci->config->item($item);
  140. }
  141.  
  142. return $value;
  143. }
  144. }

Report this snippet  

You need to login to post a comment.