Posted By

mladoux on 05/28/12


Tagged

configuration codeigniter mBlog


Versions (?)

mBlog Settings Library


 / Published in: PHP
 

URL: http://markladoux.com/

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.

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. /**
  4.  * Setting Class
  5.  *
  6.  * Handles application settings
  7.  *
  8.  * @author Mark LaDoux <[email protected]>
  9.  * @version 1.0.0
  10.  * @copyright Copyright (c) 2012, Mark LaDoux
  11.  * @todo Add grouping capabilities to more closely mimic the config class.
  12.  */
  13.  
  14. class Setting
  15. {
  16. /**
  17. * Settings Table Name
  18. *
  19. * @access protected
  20. * @since 1.0.0
  21. * @var string
  22. */
  23. protected $table = 'settings';
  24.  
  25. /**
  26. * Holds already loaded values in an array to save on queries
  27. *
  28. * @access protected
  29. * @static array
  30. */
  31. protected static $values = array();
  32.  
  33. /**
  34. * Class Constructor
  35. *
  36. * @access public
  37. * @since 1.0.0
  38. * @return void
  39. */
  40. public function __construct()
  41. {
  42. // ensure database is loaded
  43. $this->load->database();
  44.  
  45. // configure library
  46. if($this->config->load('settings', TRUE, TRUE))
  47. {
  48. if($this->config->item('table', 'settings') !== FALSE)
  49. $this->table = $this->config->item('table', 'settings');
  50. }
  51. }
  52.  
  53. /**
  54. * Retrieve CI instance on demand
  55. *
  56. * @access public
  57. * @param string $param
  58. * @return object
  59. */
  60. public function __get($param)
  61. {
  62. $object =& get_instance()->$param;
  63. return $object;
  64. }
  65.  
  66. /**
  67. * Retrieve a setting
  68. *
  69. * @access public
  70. * @since 1.0.0
  71. * @param string $item item to retrieve the value of
  72. * @param bool $check report unset items (optional)
  73. * @return mixed
  74. */
  75. public function item($item, $check = FALSE)
  76. {
  77. // check if item is already loaded
  78. if(isset(self::$values[$item]))
  79. {
  80. if($check === TRUE)
  81. {
  82. return TRUE;
  83. }
  84.  
  85. // retrieve value and return result
  86. $value = self::$values[$item];
  87. return $value;
  88. }
  89.  
  90. // run query
  91. $this->db->where('item', $item);
  92. $query = $this->db->get($this->table);
  93.  
  94. // get number of rows
  95. $rows = $query->num_rows();
  96.  
  97. if($check === TRUE)
  98. {
  99. if($rows > 0)
  100. {
  101. return TRUE;
  102. }
  103.  
  104. return FALSE;
  105. }
  106.  
  107. if($rows > 0)
  108. {
  109. $row = $query->row();
  110. $value = $row->value;
  111. }
  112. else
  113. {
  114. $value = NULL;
  115. }
  116.  
  117. // do some filtering
  118. if(strtolower($value) == '{{false}}') $value = FALSE;
  119. if(strtolower($value) == '{{true}}') $value = TRUE;
  120. if(strtolower($value) == '{{null}}') $value = NULL;
  121. if(strtolower($value) == '{{empty}}') $value = '';
  122.  
  123. // store result for subsequent calls
  124. self::$values[$item] = $value;
  125.  
  126. // return the result
  127. return $value;
  128. }
  129.  
  130. /**
  131. * Set a value to an item
  132. *
  133. * @access public
  134. * @since 1.0.0
  135. * @param string $item item to set
  136. * @param mixed $value value of the item
  137. * @return void
  138. */
  139. public function set($item, $value)
  140. {
  141. // check if item is set
  142. if(isset(self::$values[$item]))
  143. {
  144. $item_set = TRUE;
  145. }
  146. else
  147. {
  148. $item_set = $this->item($item, TRUE);
  149. }
  150.  
  151. // store item for subseqent calls before formatting for the database
  152. // so we don't have to unformat it for serving back up
  153. self::$values[$item] = $value;
  154.  
  155. // prep value
  156. if($value === FALSE ) $value = '{{false}}';
  157. if($value === TRUE ) $value = '{{true}}';
  158. if($value === NULL ) $value = '{{null}}';
  159. if($value == '' ) $value = '{{empty}}';
  160.  
  161. // perform appropriate action
  162. if($item_set === TRUE)
  163. {
  164. $this->_update_item($item, $value);
  165. }
  166. else
  167. {
  168. $this->_create_item($item, $value);
  169. }
  170. }
  171.  
  172. /**
  173. * Updates a setting in the database
  174. *
  175. * @access protected
  176. * @since 1.0.0
  177. * @param string $item item to set
  178. * @param string $value value to set
  179. * @return void
  180. */
  181. protected function _udpate_item($item, $value)
  182. {
  183. $data = array('item' => $item, 'value' => $value);
  184. $this->db->where('item', $item);
  185. $this->db->update($this->table, $data);
  186. }
  187.  
  188. /**
  189. * Creates a new setting in the database
  190. *
  191. * @access protected
  192. * @since 1.0.0
  193. * @param string $item item to set
  194. * @param string $value value to set
  195. * @return void
  196. */
  197. protected function _create_item($item, $value)
  198. {
  199. $data = array('item' => $item, 'value' => $value);
  200. $this->db->insert($this->table, $data);
  201. }
  202. }
  203.  
  204.  
  205. /* End of file Setting.php */
  206. /* Location: ./application/libraries/Setting.php */

Report this snippet  

You need to login to post a comment.