PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/application/models/settings.php

http://github.com/ushahidi/Ushahidi_Web
PHP | 214 lines | 110 code | 29 blank | 75 comment | 9 complexity | 684cd6a7ba94f105bb4a5a9006e8245c MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Model for Settings
  4. *
  5. * PHP version 5
  6. * LICENSE: This source file is subject to LGPL license
  7. * that is available through the world-wide-web at the following URI:
  8. * http://www.gnu.org/copyleft/lesser.html
  9. * @author Ushahidi Team <team@ushahidi.com>
  10. * @package Ushahidi - http://source.ushahididev.com
  11. * @subpackage Models
  12. * @copyright Ushahidi - http://www.ushahidi.com
  13. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
  14. */
  15. class Settings_Model extends ORM {
  16. /**
  17. * Database table name
  18. * @var string
  19. */
  20. protected $table_name = 'settings';
  21. // Prevents cached items from being reloaded
  22. protected $reload_on_wakeup = FALSE;
  23. /**
  24. * Check if settings table is still using old schema
  25. * @return bool
  26. */
  27. protected static function new_schema($force = FALSE)
  28. {
  29. return array_key_exists('key', ORM::factory('settings')->reload_columns($force)->table_columns);
  30. }
  31. /**
  32. * Given the setting identifier, returns its value. If the identifier
  33. * is non-existed, a NULL value is returned
  34. *
  35. * @param string $key UniqueID of the settings item
  36. *
  37. * @return string
  38. */
  39. public static function get_setting($key)
  40. {
  41. if (self::new_schema())
  42. {
  43. $setting = ORM::factory('settings')->where('key', $key)->find();
  44. return ($setting->loaded) ? $setting->value : NULL;
  45. }
  46. else
  47. {
  48. $setting = Database::instance()->query('SELECT * FROM `'.Kohana::config('database.default.table_prefix').'settings` LIMIT 1', $key)->current();
  49. return isset($setting->$key) ? $setting->$key : NULL;
  50. }
  51. }
  52. /**
  53. * Convenience method for the settings ORM when not loaded
  54. * with a specific settings value
  55. * @return string
  56. */
  57. public function get($key)
  58. {
  59. return self::get_setting($key);
  60. }
  61. /**
  62. * Convenience method to save a single setting value
  63. *
  64. * @param string key Unique ID of the setting
  65. * @param string value Value for the setting item
  66. */
  67. public static function save_setting($key, $value)
  68. {
  69. if (self::new_schema())
  70. {
  71. $setting = ORM::factory('settings')->where('key', $key)->find();
  72. $setting->key = $key;
  73. $setting->value = $value;
  74. $setting->save();
  75. }
  76. else
  77. {
  78. try
  79. {
  80. $settings = ORM::factory('settings', 1);
  81. $settings->$key = $value;
  82. $settings->save();
  83. }
  84. // Catch errors from missing settings and log
  85. catch (Exception $e)
  86. {
  87. Kohana::log('alert',(string)$e);
  88. }
  89. }
  90. }
  91. /**
  92. * Returns a key=>value array of the unique setting identifier
  93. * and its corresponding value
  94. *
  95. * @return array
  96. */
  97. public static function get_array()
  98. {
  99. if (self::new_schema())
  100. {
  101. $all_settings = ORM::factory('settings')->find_all();
  102. $settings = array();
  103. foreach ($all_settings as $setting)
  104. {
  105. $settings[$setting->key] = $setting->value;
  106. }
  107. }
  108. else
  109. {
  110. $settings = ORM::factory('settings', 1)->as_array();
  111. }
  112. return $settings;
  113. }
  114. /**
  115. * Given a validation object, updates the settings table
  116. * with the values assigned to its properties
  117. *
  118. * @param Validation $settings Validation object
  119. */
  120. public static function save_all(Validation $settings)
  121. {
  122. // For old schema throw error
  123. if (! self::new_schema())
  124. throw new Kohana_User_Exception('Settings database schema out of date');
  125. // Get all the settings
  126. $all_settings = self::get_array();
  127. // Settings update query
  128. $query = sprintf("UPDATE `%ssettings` SET `value` = CASE `key` ",
  129. Kohana::config('database.default.table_prefix'));
  130. // Used for building the query clauses for the final query
  131. $values = array();
  132. $keys = array();
  133. // Modification date
  134. $settings['date_modify'] = date("Y-m-d H:i:s",time());
  135. // List of value to skip
  136. $skip = array('api_live');
  137. $value_expr = new Database_Expression("WHEN :key THEN :value ");
  138. foreach ($settings as $key => $value)
  139. {
  140. // If an item has been marked for skipping or is a
  141. // non-existent setting, skip current iteration
  142. if (in_array($key, $skip) OR empty($key) OR ! array_key_exists($key, $all_settings))
  143. continue;
  144. // Check for the timezone
  145. if ($key === 'timezone' AND $value == 0)
  146. {
  147. $value = NULL;
  148. }
  149. $value_expr->param(':key', $key);
  150. $value_expr->param(':value', $value);
  151. $keys[] = $key;
  152. $values[] = $value_expr->compile();
  153. }
  154. // Construct the final query
  155. $query .= implode(" ", $values)."END WHERE `key` IN :keys";
  156. // Performa batch update
  157. Database::instance()->query($query, array(':keys' => $keys));
  158. }
  159. /**
  160. * Given an array of settings identifiers (unique values in the 'key' column),
  161. * returns a key => value array of the identifiers with their corresponding values
  162. * An exception is thrown if the parameter is not an array or is an empty
  163. * array
  164. *
  165. * @param array $keys
  166. * @return array
  167. */
  168. public function get_settings($keys)
  169. {
  170. // For old schema just return everything
  171. if (! self::new_schema())
  172. return Settings::get_array();
  173. if ( ! is_array($keys) OR empty($keys))
  174. throw new Kohana_Exception("Invalid parameters");
  175. $selected_settings = ORM::factory('settings')
  176. ->in('key', $keys)
  177. ->find_all();
  178. $settings = array();
  179. foreach ($selected_settings as $setting)
  180. {
  181. $settings[$setting->key] = $setting->value;
  182. }
  183. return $settings;
  184. }
  185. }