Posted By

mladoux on 07/16/12


Tagged

configuration codeigniter mBlog


Versions (?)

mConfig


 / Published in: PHP
 

URL: http://markladoux.com

mConfig Database Configuration Library - Formally Settings

updates - Removed CI object variable, simplified, and other general goodness!

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3.  * mConfig
  4.  *
  5.  * Library that handles database settings for mBlog
  6.  *
  7.  * @author Mark LaDoux <[email protected]>
  8.  * @copyright Copyright © 2012, Mark LaDoux
  9.  * @version 2.0
  10.  * @link http://markladoux.com/
  11.  */
  12.  
  13. class mConfig
  14. {
  15. /**
  16. * Configuration Table
  17. *
  18. * @access protected
  19. * @since 1.0
  20. * @static string
  21. */
  22. protected static $table;
  23.  
  24. /**
  25. * Class Constructor
  26. *
  27. * Prepares the class for use and loads dependancies
  28. *
  29. * @access public
  30. * @since 2.0
  31. * @return void
  32. */
  33. public function __construct()
  34. {
  35. // Load static settings
  36. $this->config->load('mConfig');
  37. self::$table = $this->config->item('mConfig_table');
  38.  
  39. // Load database
  40. $this->load->database();
  41. }
  42.  
  43. /**
  44. * Magic __get Function
  45. *
  46. * Allows us to use the CI Super global without having to use aliases
  47. * Shamelessly stolen from Ion Auth
  48. *
  49. * @access public
  50. * @since 2.0
  51. * @param string
  52. * @return object
  53. */
  54. public function __call($var)
  55. {
  56. return get_instance()->$var;
  57. }
  58.  
  59. /**
  60. * item
  61. *
  62. * Retrieve item from mConfig database
  63. *
  64. * @access public
  65. * @since 2.0
  66. * @param string $item configuration item to retrieve
  67. * @param bool $check Report existance of setting
  68. * @return mixed
  69. */
  70. public function item($item, $check = FALSE)
  71. {
  72. // prepare query
  73. $this->db->where('item', $item);
  74. $query = $this->db->get(self::$table);
  75.  
  76. // if we have results, and we are in check mode, return TRUE
  77. if($check === TRUE && $query->num_rows() > 0)
  78. {
  79. return TRUE;
  80. }
  81.  
  82. // check results
  83. if($query->num_rows() < 1)
  84. {
  85. // if we are in check mode, return FALSE
  86. if($check === TRUE)
  87. {
  88. return FALSE;
  89. }
  90.  
  91. // otherwise return NULL
  92. return NULL;
  93. }
  94.  
  95. // get data
  96. $row = $query->results();
  97. $value = $row->value;
  98.  
  99. // filter data
  100. if(strtolower($value) == '{{false}}') $value = FALSE;
  101. if(strtolower($value) == '{{true}}') $value = TRUE;
  102. if(strtolower($value) == '{{null}}') $value = NULL;
  103. if(strtolower($value) == '{{empty}}') $value = '';
  104.  
  105. // return data for processing
  106. return $value;
  107. }
  108.  
  109. /**
  110. * delete
  111. *
  112. * Delete a value
  113. *
  114. * @access public
  115. * @since 2.0
  116. * @param string $item
  117. * @return void
  118. */
  119. public function delete($item)
  120. {
  121. $this->db->where('item', $item);
  122. $this->db->delete(self::$table);
  123. }
  124.  
  125. /**
  126. * set
  127. *
  128. * Set a value
  129. *
  130. * @access public
  131. * @since 1.0
  132. * @param string $item item to set
  133. * @param string $value value of item
  134. * @return void
  135. */
  136. public function set($item, $value)
  137. {
  138. // prepare value
  139. if($value === TRUE) $value = '{{true}}';
  140. if($value === FALSE) $value = '{{false}}';
  141. if($value === NULL) $value = '{{null}}';
  142. if($value == '') $value = '';
  143.  
  144. // check if value exists
  145. $exists = $this->item($item, TRUE);
  146.  
  147. if($exists === TRUE)
  148. {
  149. $this->_update_item($item, $value);
  150. }
  151. else
  152. {
  153. $this->_create_item($item, $value);
  154. }
  155. }
  156.  
  157. /**
  158. * _update_item
  159. *
  160. * Update a value in the database
  161. *
  162. * @access protected
  163. * @since 2.0
  164. * @param string $item item to update
  165. * @param string $value new value to set
  166. * @return void
  167. */
  168. protected function _update_item($item, $value)
  169. {
  170. $data = array('value' => $value);
  171. $this->db->where('item', $item);
  172. $this->db->update(self::$table, $data);
  173. }
  174.  
  175. /**
  176. * _create_item
  177. *
  178. * Create a new item/value pair in the database
  179. *
  180. * @access protected
  181. * @since 2.0
  182. * @param string $item item to create
  183. * @param string $value value to set
  184. * @return void
  185. */
  186. protected function _create_item($item, $value)
  187. {
  188. // prepare array
  189. $data = array(
  190. 'item' => $item,
  191. 'value' => $value
  192. );
  193.  
  194. // insert data
  195. $this->db->insert(self::$table, $data);
  196. }
  197. }

Report this snippet  

You need to login to post a comment.