croogo /Settings/Model/Setting.php

Language PHP Lines 225
MD5 Hash f50358d907ea5b61f3dff2aa78e2b925
Repository https://github.com/kareypowell/croogo.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php

App::uses('AppModel', 'Model');
App::uses('File', 'Utility');

/**
 * Setting
 *
 * @category Model
 * @package  Croogo.Settings.Model
 * @version  1.0
 * @author   Fahad Ibnay Heylaal <contact@fahad19.com>
 * @license  http://www.opensource.org/licenses/mit-license.php The MIT License
 * @link     http://www.croogo.org
 */
class Setting extends SettingsAppModel {

/**
 * Model name
 *
 * @var string
 * @access public
 */
	public $name = 'Setting';

/**
 * Path to settings file
 *
 * @var string
 */
	public $settingsPath = '';

/**
 * Behaviors used by the Model
 *
 * @var array
 * @access public
 */
	public $actsAs = array(
		'Croogo.Ordered' => array(
			'field' => 'weight',
			'foreign_key' => false,
		),
		'Croogo.Cached' => array(
			'groups' => array(
				'settings',
			),
		),
		'Croogo.Trackable',
		'Search.Searchable',
	);

/**
 * Validation
 *
 * @var array
 * @access public
 */
	public $validate = array(
		'key' => array(
			'isUnique' => array(
				'rule' => 'isUnique',
				'message' => 'This key has already been taken.',
			),
			'minLength' => array(
				'rule' => array('minLength', 1),
				'message' => 'Key cannot be empty.',
			),
		),
	);

/**
 * Filter search fields
 */
	public $filterArgs = array(
		'key' => array('type' => 'like', 'field' => 'Setting.key'),
	);

/**
 * __construct
 *
 * @param mixed $id
 * @param string $table
 * @param DataSource $ds
 */
	public function __construct($id = false, $table = null, $ds = null) {
		parent::__construct($id, $table, $ds);
		$this->settingsPath = APP . 'Config' . DS . 'settings.json';
	}

/**
 * afterSave callback
 *
 * @return void
 */
	public function afterSave($created, $options = array()) {
		$this->updateJson();
		$this->writeConfiguration();
	}

/**
 * afterDelete callback
 *
 * @return void
 */
	public function afterDelete() {
		$this->updateJson();
		$this->writeConfiguration();
	}

/**
 * Creates a new record with key/value pair if key does not exist.
 *
 * @param string $key
 * @param string $value
 * @param array $options
 * @return boolean
 */
	public function write($key, $value, $options = array()) {
		$setting = $this->findByKey($key);
		if (isset($setting['Setting']['id'])) {
			$setting['Setting']['id'] = $setting['Setting']['id'];
			$setting['Setting']['value'] = $value;

			$setting['Setting'] = $options + $setting['Setting'];

		} else {

			$options = array_merge(array(
				'title' => '',
				'description' => '',
				'input_type' => '',
				'editable' => 0,
				'weight' => 0,
				'params' => '',
			), $options);

			$setting = array();
			$setting['key'] = $key;
			$setting['value'] = $value;
			$setting['title'] = $options['title'];
			$setting['description'] = $options['description'];
			$setting['input_type'] = $options['input_type'];
			$setting['editable'] = $options['editable'];
			$setting['weight'] = $options['weight'];
			$setting['params'] = $options['params'];
		}

		$this->id = false;
		if ($this->save($setting)) {
			Configure::write($key, $value);
			return true;
		} else {
			return false;
		}
	}

/**
 * Deletes setting record for given key
 *
 * @param string $key
 * @return boolean
 */
	public function deleteKey($key) {
		$setting = $this->findByKey($key);
		if (isset($setting['Setting']['id']) &&
			$this->delete($setting['Setting']['id'])) {
			return true;
		}
		return false;
	}

/**
 * All key/value pairs are made accessible from Configure class
 *
 * @return void
 */
	public function writeConfiguration() {
		Configure::load('settings', 'settings');
	}

/**
 * Find list and save yaml dump in app/Config/settings.json file.
 * Data required in bootstrap.
 *
 * @return void
 */
	public function updateJson() {
		$settings = $this->find('all', array(
			'fields' => array(
				'key',
				'value',
			),
			'order' => array(
				'Setting.key' => 'ASC',
			),
		));
		$settings = array_combine(
			Hash::extract($settings, '{n}.Setting.key'),
			Hash::extract($settings, '{n}.Setting.value')
			);
		Configure::write($settings);
		foreach ($settings as $key => $setting) {
			list($key, $ignore) = explode('.', $key, 2);
			$keys[] = $key;
		}
		$keys = array_unique($keys);
		Configure::dump('settings.json', 'settings', $keys);
	}

/**
 * beforeSave
 *
 * if 'values' is present, serialize it with json_encode and save it in 'value'.
 * this is used for allowing 'multiple' input_type (select|checkbox) feature
 */
	public function beforeSave($options = array()) {
		if (isset($this->data[$this->alias]['values'])) {
			$this->data[$this->alias]['value'] = json_encode($this->data[$this->alias]['values']);
		}
		return true;
	}

}
Back to Top