PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Settings/Model/Setting.php

https://github.com/kareypowell/croogo
PHP | 224 lines | 119 code | 21 blank | 84 comment | 6 complexity | f50358d907ea5b61f3dff2aa78e2b925 MD5 | raw file
  1. <?php
  2. App::uses('AppModel', 'Model');
  3. App::uses('File', 'Utility');
  4. /**
  5. * Setting
  6. *
  7. * @category Model
  8. * @package Croogo.Settings.Model
  9. * @version 1.0
  10. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  11. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  12. * @link http://www.croogo.org
  13. */
  14. class Setting extends SettingsAppModel {
  15. /**
  16. * Model name
  17. *
  18. * @var string
  19. * @access public
  20. */
  21. public $name = 'Setting';
  22. /**
  23. * Path to settings file
  24. *
  25. * @var string
  26. */
  27. public $settingsPath = '';
  28. /**
  29. * Behaviors used by the Model
  30. *
  31. * @var array
  32. * @access public
  33. */
  34. public $actsAs = array(
  35. 'Croogo.Ordered' => array(
  36. 'field' => 'weight',
  37. 'foreign_key' => false,
  38. ),
  39. 'Croogo.Cached' => array(
  40. 'groups' => array(
  41. 'settings',
  42. ),
  43. ),
  44. 'Croogo.Trackable',
  45. 'Search.Searchable',
  46. );
  47. /**
  48. * Validation
  49. *
  50. * @var array
  51. * @access public
  52. */
  53. public $validate = array(
  54. 'key' => array(
  55. 'isUnique' => array(
  56. 'rule' => 'isUnique',
  57. 'message' => 'This key has already been taken.',
  58. ),
  59. 'minLength' => array(
  60. 'rule' => array('minLength', 1),
  61. 'message' => 'Key cannot be empty.',
  62. ),
  63. ),
  64. );
  65. /**
  66. * Filter search fields
  67. */
  68. public $filterArgs = array(
  69. 'key' => array('type' => 'like', 'field' => 'Setting.key'),
  70. );
  71. /**
  72. * __construct
  73. *
  74. * @param mixed $id
  75. * @param string $table
  76. * @param DataSource $ds
  77. */
  78. public function __construct($id = false, $table = null, $ds = null) {
  79. parent::__construct($id, $table, $ds);
  80. $this->settingsPath = APP . 'Config' . DS . 'settings.json';
  81. }
  82. /**
  83. * afterSave callback
  84. *
  85. * @return void
  86. */
  87. public function afterSave($created, $options = array()) {
  88. $this->updateJson();
  89. $this->writeConfiguration();
  90. }
  91. /**
  92. * afterDelete callback
  93. *
  94. * @return void
  95. */
  96. public function afterDelete() {
  97. $this->updateJson();
  98. $this->writeConfiguration();
  99. }
  100. /**
  101. * Creates a new record with key/value pair if key does not exist.
  102. *
  103. * @param string $key
  104. * @param string $value
  105. * @param array $options
  106. * @return boolean
  107. */
  108. public function write($key, $value, $options = array()) {
  109. $setting = $this->findByKey($key);
  110. if (isset($setting['Setting']['id'])) {
  111. $setting['Setting']['id'] = $setting['Setting']['id'];
  112. $setting['Setting']['value'] = $value;
  113. $setting['Setting'] = $options + $setting['Setting'];
  114. } else {
  115. $options = array_merge(array(
  116. 'title' => '',
  117. 'description' => '',
  118. 'input_type' => '',
  119. 'editable' => 0,
  120. 'weight' => 0,
  121. 'params' => '',
  122. ), $options);
  123. $setting = array();
  124. $setting['key'] = $key;
  125. $setting['value'] = $value;
  126. $setting['title'] = $options['title'];
  127. $setting['description'] = $options['description'];
  128. $setting['input_type'] = $options['input_type'];
  129. $setting['editable'] = $options['editable'];
  130. $setting['weight'] = $options['weight'];
  131. $setting['params'] = $options['params'];
  132. }
  133. $this->id = false;
  134. if ($this->save($setting)) {
  135. Configure::write($key, $value);
  136. return true;
  137. } else {
  138. return false;
  139. }
  140. }
  141. /**
  142. * Deletes setting record for given key
  143. *
  144. * @param string $key
  145. * @return boolean
  146. */
  147. public function deleteKey($key) {
  148. $setting = $this->findByKey($key);
  149. if (isset($setting['Setting']['id']) &&
  150. $this->delete($setting['Setting']['id'])) {
  151. return true;
  152. }
  153. return false;
  154. }
  155. /**
  156. * All key/value pairs are made accessible from Configure class
  157. *
  158. * @return void
  159. */
  160. public function writeConfiguration() {
  161. Configure::load('settings', 'settings');
  162. }
  163. /**
  164. * Find list and save yaml dump in app/Config/settings.json file.
  165. * Data required in bootstrap.
  166. *
  167. * @return void
  168. */
  169. public function updateJson() {
  170. $settings = $this->find('all', array(
  171. 'fields' => array(
  172. 'key',
  173. 'value',
  174. ),
  175. 'order' => array(
  176. 'Setting.key' => 'ASC',
  177. ),
  178. ));
  179. $settings = array_combine(
  180. Hash::extract($settings, '{n}.Setting.key'),
  181. Hash::extract($settings, '{n}.Setting.value')
  182. );
  183. Configure::write($settings);
  184. foreach ($settings as $key => $setting) {
  185. list($key, $ignore) = explode('.', $key, 2);
  186. $keys[] = $key;
  187. }
  188. $keys = array_unique($keys);
  189. Configure::dump('settings.json', 'settings', $keys);
  190. }
  191. /**
  192. * beforeSave
  193. *
  194. * if 'values' is present, serialize it with json_encode and save it in 'value'.
  195. * this is used for allowing 'multiple' input_type (select|checkbox) feature
  196. */
  197. public function beforeSave($options = array()) {
  198. if (isset($this->data[$this->alias]['values'])) {
  199. $this->data[$this->alias]['value'] = json_encode($this->data[$this->alias]['values']);
  200. }
  201. return true;
  202. }
  203. }