Posted By

mladoux on 02/01/12


Tagged

codeigniter


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

notturnale
letunglam
fireball70
Priestd09


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 CIConfig class. In fact, it operates totally separately from CIConfig. Now self-installing ( just configure your database )

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3.  * Setting Class
  4.  *
  5.  * Handles application settings
  6.  *
  7.  * @author Mark LaDoux <[email protected]>
  8.  * @version 20120707
  9.  * @copyright Copyright (c) 2012, Mark LaDoux
  10.  */
  11.  
  12. class Setting
  13. {
  14. /**
  15.   * CodeIgniter Object
  16.   *
  17.   * @access protected
  18.   * @since 20120707
  19.   * @var object
  20.   */
  21. protected $ci;
  22.  
  23. /**
  24.   * Settings Table
  25.   *
  26.   * @access protected
  27.   * @since 20120707
  28.   * @var string
  29.   */
  30. protected $table;
  31.  
  32. /**
  33.   * Class constructor
  34.   *
  35.   * @access public
  36.   * @since 20120707
  37.   * @return void
  38.   */
  39. public function __construct()
  40. {
  41. // load CodeIgniter instance
  42. $this->ci =& get_instance();
  43.  
  44. // Retrieve settings
  45. $this->ci->config->load('setting');
  46. $this->table = $this->ci->config->item('settings_table');
  47.  
  48. // ensure database is loaded
  49. $this->ci->load->database();
  50.  
  51. // check if settings table exists
  52. if(! $this->ci->db->table_exists($this->table))
  53. {
  54. $this->_install();
  55. }
  56. }
  57.  
  58. /**
  59.   * Installs the database
  60.   *
  61.   * @access protected
  62.   * @since 20120707
  63.   * @return void
  64.   */
  65. protected function _install()
  66. {
  67. // initialize dbforge
  68. $this->ci->load->dbforge();
  69. $fields = array(
  70. 'setting_id' => array(
  71. 'type' => 'INT',
  72. 'unsigned' => TRUE,
  73. 'auto_increment' => TRUE,
  74. ),
  75. 'item' => array(
  76. 'type' => 'VARCHAR',
  77. 'constraint' => '255'),
  78. 'value' => array('type' => 'TEXT'),
  79. );
  80.  
  81. $this->ci->dbforge->add_field($fields);
  82. $this->ci->dbforge->add_key('setting_id', TRUE);
  83. $this->ci->dbforge->add_key('item');
  84. $this->ci->dbforge->create_table($this->table, TRUE);
  85. }
  86.  
  87. /**
  88.   * Retrieve a setting
  89.   *
  90.   * @access public
  91.   * @since 20120707
  92.   * @param string $item item to retrieve the value of
  93.   * @param bool $check report unset items (optional)
  94.   * @return mixed
  95.   */
  96. public function item($item, $check = FALSE)
  97. {
  98. // run query
  99. $this->ci->db->where('item', $item);
  100. $query = $this->ci->db->get($this->table);
  101.  
  102. // get number of rows
  103. $rows = $query->num_rows();
  104.  
  105. if($check === TRUE)
  106. {
  107. if($rows > 0)
  108. {
  109. return TRUE;
  110. }
  111.  
  112. return FALSE;
  113. }
  114.  
  115. if($rows > 0)
  116. {
  117. $row = $query->result();
  118. $value = $row->value;
  119. }
  120. else
  121. {
  122. $value = NULL;
  123. }
  124.  
  125. // do some filtering
  126. if(strtolower($value) == '{{false}}') $value = FALSE;
  127. if(strtolower($value) == '{{true}}') $value = TRUE;
  128. if(strtolower($value) == '{{null}}') $value = NULL;
  129. if(strtolower($value) == '{{empty}}') $value = '';
  130.  
  131. // return the result
  132. return $value;
  133. }
  134.  
  135. /**
  136.   * Set a value to an item
  137.   *
  138.   * @access public
  139.   * @since 20120707
  140.   * @param string $item item to set
  141.   * @param mixed $value value of the item
  142.   * @return void
  143.   */
  144. public function set($item, $value)
  145. {
  146. // check if item is set
  147. $item_set = $this->item($item, TRUE);
  148.  
  149. // prep value
  150. if($value === FALSE ) $value = '{{false}}';
  151. if($value === TRUE ) $value = '{{true}}';
  152. if($value === NULL ) $value = '{{null}}';
  153. if($value == '' ) $value = '{{empty}}';
  154.  
  155. // perform appropriate action
  156. if($item_set === TRUE)
  157. {
  158. $this->_update_item($item, $value);
  159. }
  160. else
  161. {
  162. $this->_create_item($item, $value);
  163. }
  164. }
  165.  
  166. /**
  167.   * Updates a setting in the database
  168.   *
  169.   * @access protected
  170.   * @since 20120707
  171.   * @param string $item item to set
  172.   * @param string $value value to set
  173.   * @return void
  174.   */
  175. protected function _udpate_item($item, $value)
  176. {
  177. $data = array('item' => $item, 'value' => $value);
  178. $this->ci->db->where('item', $item);
  179. $this->ci->db->update($this->table, $data);
  180. }
  181.  
  182. /**
  183.   * Creates a new setting in the database
  184.   *
  185.   * @access protected
  186.   * @since 20120707
  187.   * @param string $item item to set
  188.   * @param string $value value to set
  189.   * @return void
  190.   */
  191. protected function _create_item($item, $value)
  192. {
  193. $data = array('item' => $item, 'value' => $value);
  194. $this->ci->db->insert($this->table, $data);
  195. }
  196. }

Report this snippet  

You need to login to post a comment.