@@ -1,34 +1,34 @@ discard block |
||
1 | 1 | <?php namespace Myth\Models; |
2 | 2 | /** |
3 | - * Sprint |
|
4 | - * |
|
5 | - * A set of power tools to enhance the CodeIgniter framework and provide consistent workflow. |
|
6 | - * |
|
7 | - * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 | - * of this software and associated documentation files (the "Software"), to deal |
|
9 | - * in the Software without restriction, including without limitation the rights |
|
10 | - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11 | - * copies of the Software, and to permit persons to whom the Software is |
|
12 | - * furnished to do so, subject to the following conditions: |
|
13 | - * |
|
14 | - * The above copyright notice and this permission notice shall be included in |
|
15 | - * all copies or substantial portions of the Software. |
|
16 | - * |
|
17 | - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18 | - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19 | - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20 | - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21 | - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22 | - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
23 | - * THE SOFTWARE. |
|
24 | - * |
|
25 | - * @package Sprint |
|
26 | - * @author Lonnie Ezell |
|
27 | - * @copyright Copyright 2014-2015, New Myth Media, LLC (http://newmythmedia.com) |
|
28 | - * @license http://opensource.org/licenses/MIT (MIT) |
|
29 | - * @link http://sprintphp.com |
|
30 | - * @since Version 1.0 |
|
31 | - */ |
|
3 | + * Sprint |
|
4 | + * |
|
5 | + * A set of power tools to enhance the CodeIgniter framework and provide consistent workflow. |
|
6 | + * |
|
7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 | + * of this software and associated documentation files (the "Software"), to deal |
|
9 | + * in the Software without restriction, including without limitation the rights |
|
10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11 | + * copies of the Software, and to permit persons to whom the Software is |
|
12 | + * furnished to do so, subject to the following conditions: |
|
13 | + * |
|
14 | + * The above copyright notice and this permission notice shall be included in |
|
15 | + * all copies or substantial portions of the Software. |
|
16 | + * |
|
17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
23 | + * THE SOFTWARE. |
|
24 | + * |
|
25 | + * @package Sprint |
|
26 | + * @author Lonnie Ezell |
|
27 | + * @copyright Copyright 2014-2015, New Myth Media, LLC (http://newmythmedia.com) |
|
28 | + * @license http://opensource.org/licenses/MIT (MIT) |
|
29 | + * @link http://sprintphp.com |
|
30 | + * @since Version 1.0 |
|
31 | + */ |
|
32 | 32 | |
33 | 33 | //-------------------------------------------------------------------- |
34 | 34 | |
@@ -102,1479 +102,1479 @@ discard block |
||
102 | 102 | class CIDbModel |
103 | 103 | { |
104 | 104 | |
105 | - /** |
|
106 | - * The model's default table name. |
|
107 | - * |
|
108 | - * @var string; |
|
109 | - */ |
|
110 | - protected $table_name; |
|
111 | - |
|
112 | - /** |
|
113 | - * The model's default primary key. |
|
114 | - * |
|
115 | - * @var string |
|
116 | - */ |
|
117 | - protected $primary_key = 'id'; |
|
118 | - |
|
119 | - /** |
|
120 | - * The type of date/time field used for created_on and modified_on fields. |
|
121 | - * Valid types are: 'int', 'datetime', 'date' |
|
122 | - * |
|
123 | - * @var string |
|
124 | - * @access protected |
|
125 | - */ |
|
126 | - protected $date_format = 'datetime'; |
|
127 | - |
|
128 | - /* |
|
105 | + /** |
|
106 | + * The model's default table name. |
|
107 | + * |
|
108 | + * @var string; |
|
109 | + */ |
|
110 | + protected $table_name; |
|
111 | + |
|
112 | + /** |
|
113 | + * The model's default primary key. |
|
114 | + * |
|
115 | + * @var string |
|
116 | + */ |
|
117 | + protected $primary_key = 'id'; |
|
118 | + |
|
119 | + /** |
|
120 | + * The type of date/time field used for created_on and modified_on fields. |
|
121 | + * Valid types are: 'int', 'datetime', 'date' |
|
122 | + * |
|
123 | + * @var string |
|
124 | + * @access protected |
|
125 | + */ |
|
126 | + protected $date_format = 'datetime'; |
|
127 | + |
|
128 | + /* |
|
129 | 129 | Var: $log_user |
130 | 130 | If TRUE, will log user id for 'created_by', 'modified_by' and 'deleted_by'. |
131 | 131 | |
132 | 132 | Access: |
133 | 133 | Protected |
134 | 134 | */ |
135 | - protected $log_user = FALSE; |
|
135 | + protected $log_user = FALSE; |
|
136 | 136 | |
137 | 137 | |
138 | 138 | |
139 | - /** |
|
140 | - * Whether or not to auto-fill a 'created_on' field on inserts. |
|
141 | - * |
|
142 | - * @var boolean |
|
143 | - * @access protected |
|
144 | - */ |
|
145 | - protected $set_created = TRUE; |
|
139 | + /** |
|
140 | + * Whether or not to auto-fill a 'created_on' field on inserts. |
|
141 | + * |
|
142 | + * @var boolean |
|
143 | + * @access protected |
|
144 | + */ |
|
145 | + protected $set_created = TRUE; |
|
146 | 146 | |
147 | - /** |
|
148 | - * Field name to use to the created time column in the DB table. |
|
149 | - * |
|
150 | - * @var string |
|
151 | - * @access protected |
|
152 | - */ |
|
153 | - protected $created_field = 'created_on'; |
|
147 | + /** |
|
148 | + * Field name to use to the created time column in the DB table. |
|
149 | + * |
|
150 | + * @var string |
|
151 | + * @access protected |
|
152 | + */ |
|
153 | + protected $created_field = 'created_on'; |
|
154 | 154 | |
155 | - /* |
|
155 | + /* |
|
156 | 156 | Var: $created_by_field |
157 | 157 | Field name to use to the created by column in the DB table. |
158 | 158 | |
159 | 159 | Access: |
160 | 160 | Protected |
161 | 161 | */ |
162 | - protected $created_by_field = 'created_by'; |
|
162 | + protected $created_by_field = 'created_by'; |
|
163 | 163 | |
164 | 164 | |
165 | 165 | |
166 | - /** |
|
167 | - * Whether or not to auto-fill a 'modified_on' field on updates. |
|
168 | - * |
|
169 | - * @var boolean |
|
170 | - * @access protected |
|
171 | - */ |
|
172 | - protected $set_modified = TRUE; |
|
166 | + /** |
|
167 | + * Whether or not to auto-fill a 'modified_on' field on updates. |
|
168 | + * |
|
169 | + * @var boolean |
|
170 | + * @access protected |
|
171 | + */ |
|
172 | + protected $set_modified = TRUE; |
|
173 | 173 | |
174 | - /** |
|
175 | - * Field name to use to the modified time column in the DB table. |
|
176 | - * |
|
177 | - * @var string |
|
178 | - * @access protected |
|
179 | - */ |
|
180 | - protected $modified_field = 'modified_on'; |
|
174 | + /** |
|
175 | + * Field name to use to the modified time column in the DB table. |
|
176 | + * |
|
177 | + * @var string |
|
178 | + * @access protected |
|
179 | + */ |
|
180 | + protected $modified_field = 'modified_on'; |
|
181 | 181 | |
182 | - /* |
|
182 | + /* |
|
183 | 183 | Var: $modified_by_field |
184 | 184 | Field name to use to the modified by column in the DB table. |
185 | 185 | |
186 | 186 | Access: |
187 | 187 | Protected |
188 | 188 | */ |
189 | - protected $modified_by_field = 'modified_by'; |
|
189 | + protected $modified_by_field = 'modified_by'; |
|
190 | 190 | |
191 | 191 | |
192 | - /** |
|
193 | - * Support for soft_deletes. |
|
194 | - */ |
|
195 | - protected $soft_deletes = FALSE; |
|
196 | - protected $soft_delete_key = 'deleted'; |
|
197 | - protected $temp_with_deleted = FALSE; |
|
192 | + /** |
|
193 | + * Support for soft_deletes. |
|
194 | + */ |
|
195 | + protected $soft_deletes = FALSE; |
|
196 | + protected $soft_delete_key = 'deleted'; |
|
197 | + protected $temp_with_deleted = FALSE; |
|
198 | 198 | |
199 | - /* |
|
199 | + /* |
|
200 | 200 | Var: $deleted_by_field |
201 | 201 | Field name to use for the deleted by column in the DB table. |
202 | 202 | |
203 | 203 | Access: |
204 | 204 | Protected |
205 | 205 | */ |
206 | - protected $deleted_by_field = 'deleted_by'; |
|
206 | + protected $deleted_by_field = 'deleted_by'; |
|
207 | 207 | |
208 | 208 | |
209 | 209 | |
210 | - /** |
|
211 | - * Various callbacks available to the class. They are simple lists of |
|
212 | - * method names (methods will be ran on $this). |
|
213 | - */ |
|
214 | - protected $before_insert = array(); |
|
215 | - protected $after_insert = array(); |
|
216 | - protected $before_update = array(); |
|
217 | - protected $after_update = array(); |
|
218 | - protected $before_find = array(); |
|
219 | - protected $after_find = array(); |
|
220 | - protected $before_delete = array(); |
|
221 | - protected $after_delete = array(); |
|
210 | + /** |
|
211 | + * Various callbacks available to the class. They are simple lists of |
|
212 | + * method names (methods will be ran on $this). |
|
213 | + */ |
|
214 | + protected $before_insert = array(); |
|
215 | + protected $after_insert = array(); |
|
216 | + protected $before_update = array(); |
|
217 | + protected $after_update = array(); |
|
218 | + protected $before_find = array(); |
|
219 | + protected $after_find = array(); |
|
220 | + protected $before_delete = array(); |
|
221 | + protected $after_delete = array(); |
|
222 | 222 | |
223 | - protected $callback_parameters = array(); |
|
223 | + protected $callback_parameters = array(); |
|
224 | 224 | |
225 | 225 | |
226 | 226 | |
227 | - /* |
|
227 | + /* |
|
228 | 228 | If TRUE, inserts will return the last_insert_id. However, |
229 | 229 | this can potentially slow down large imports drastically |
230 | 230 | so you can turn it off with the return_insert_id(false) method. |
231 | 231 | */ |
232 | - protected $return_insert_id = true; |
|
233 | - |
|
234 | - /** |
|
235 | - * By default, we return items as objects. You can change this for the |
|
236 | - * entire class by setting this value to 'array' instead of 'object'. |
|
237 | - * Alternatively, you can do it on a per-instance basis using the |
|
238 | - * 'as_array()' and 'as_object()' methods. |
|
239 | - */ |
|
240 | - protected $return_type = 'object'; |
|
241 | - protected $temp_return_type = NULL; |
|
242 | - |
|
243 | - /** |
|
244 | - * Protected, non-modifiable attributes |
|
245 | - */ |
|
246 | - protected $protected_attributes = array(); |
|
247 | - |
|
248 | - |
|
249 | - |
|
250 | - /** |
|
251 | - * An array of validation rules. This needs to be the same format |
|
252 | - * as validation rules passed to the Form_validation library. |
|
253 | - */ |
|
254 | - protected $validation_rules = array(); |
|
255 | - |
|
256 | - /** |
|
257 | - * Optionally skip the validation. Used in conjunction with |
|
258 | - * skip_validation() to skip data validation for any future calls. |
|
259 | - */ |
|
260 | - protected $skip_validation = FALSE; |
|
261 | - |
|
262 | - /** |
|
263 | - * An array of extra rules to add to validation rules during inserts only. |
|
264 | - * Often used for adding 'required' rules to fields on insert, but not udpates. |
|
265 | - * |
|
266 | - * array( 'username' => 'required|strip_tags' ); |
|
267 | - * @var array |
|
268 | - */ |
|
269 | - protected $insert_validate_rules = array(); |
|
270 | - |
|
271 | - |
|
272 | - |
|
273 | - /** |
|
274 | - * @var Array Columns for the model's database fields |
|
275 | - * |
|
276 | - * This can be set to avoid a database call if using $this->prep_data() |
|
277 | - */ |
|
278 | - protected $fields = array(); |
|
279 | - |
|
280 | - //-------------------------------------------------------------------- |
|
281 | - |
|
282 | - /** |
|
283 | - * Does basic setup. Can pass the database connection into the constructor |
|
284 | - * to use that $db instead of the global CI one. |
|
285 | - * |
|
286 | - * @param object $db // A database/driver instance |
|
287 | - * @param object $form_validation // A form_validation library instance |
|
288 | - */ |
|
289 | - public function __construct($db = null, $form_validation = null) |
|
290 | - { |
|
291 | - // Always protect our attributes |
|
292 | - array_unshift($this->before_insert, 'protect_attributes'); |
|
293 | - array_unshift($this->before_update, 'protect_attributes'); |
|
294 | - |
|
295 | - // Check our auto-set features and make sure they are part of |
|
296 | - // our observer system. |
|
297 | - if ($this->set_created === true) array_unshift($this->before_insert, 'created_on'); |
|
298 | - if ($this->set_modified === true) array_unshift($this->before_update, 'modified_on'); |
|
299 | - |
|
300 | - // Make sure our temp return type is correct. |
|
301 | - $this->temp_return_type = $this->return_type; |
|
302 | - |
|
303 | - // Make sure our database is loaded |
|
304 | - if (!is_null($db)) { |
|
305 | - $this->db = $db; |
|
306 | - } |
|
307 | - else { |
|
308 | - // Auto Init the damn database.... |
|
309 | - $this->load->database(); |
|
310 | - } |
|
311 | - |
|
312 | - // Do we have a form_validation library? |
|
313 | - if (! is_null($form_validation)) { |
|
314 | - $this->form_validation = $form_validation; |
|
315 | - } |
|
316 | - else { |
|
317 | - $this->load->library('form_validation'); |
|
318 | - } |
|
232 | + protected $return_insert_id = true; |
|
233 | + |
|
234 | + /** |
|
235 | + * By default, we return items as objects. You can change this for the |
|
236 | + * entire class by setting this value to 'array' instead of 'object'. |
|
237 | + * Alternatively, you can do it on a per-instance basis using the |
|
238 | + * 'as_array()' and 'as_object()' methods. |
|
239 | + */ |
|
240 | + protected $return_type = 'object'; |
|
241 | + protected $temp_return_type = NULL; |
|
242 | + |
|
243 | + /** |
|
244 | + * Protected, non-modifiable attributes |
|
245 | + */ |
|
246 | + protected $protected_attributes = array(); |
|
247 | + |
|
248 | + |
|
249 | + |
|
250 | + /** |
|
251 | + * An array of validation rules. This needs to be the same format |
|
252 | + * as validation rules passed to the Form_validation library. |
|
253 | + */ |
|
254 | + protected $validation_rules = array(); |
|
255 | + |
|
256 | + /** |
|
257 | + * Optionally skip the validation. Used in conjunction with |
|
258 | + * skip_validation() to skip data validation for any future calls. |
|
259 | + */ |
|
260 | + protected $skip_validation = FALSE; |
|
261 | + |
|
262 | + /** |
|
263 | + * An array of extra rules to add to validation rules during inserts only. |
|
264 | + * Often used for adding 'required' rules to fields on insert, but not udpates. |
|
265 | + * |
|
266 | + * array( 'username' => 'required|strip_tags' ); |
|
267 | + * @var array |
|
268 | + */ |
|
269 | + protected $insert_validate_rules = array(); |
|
270 | + |
|
271 | + |
|
272 | + |
|
273 | + /** |
|
274 | + * @var Array Columns for the model's database fields |
|
275 | + * |
|
276 | + * This can be set to avoid a database call if using $this->prep_data() |
|
277 | + */ |
|
278 | + protected $fields = array(); |
|
279 | + |
|
280 | + //-------------------------------------------------------------------- |
|
281 | + |
|
282 | + /** |
|
283 | + * Does basic setup. Can pass the database connection into the constructor |
|
284 | + * to use that $db instead of the global CI one. |
|
285 | + * |
|
286 | + * @param object $db // A database/driver instance |
|
287 | + * @param object $form_validation // A form_validation library instance |
|
288 | + */ |
|
289 | + public function __construct($db = null, $form_validation = null) |
|
290 | + { |
|
291 | + // Always protect our attributes |
|
292 | + array_unshift($this->before_insert, 'protect_attributes'); |
|
293 | + array_unshift($this->before_update, 'protect_attributes'); |
|
294 | + |
|
295 | + // Check our auto-set features and make sure they are part of |
|
296 | + // our observer system. |
|
297 | + if ($this->set_created === true) array_unshift($this->before_insert, 'created_on'); |
|
298 | + if ($this->set_modified === true) array_unshift($this->before_update, 'modified_on'); |
|
299 | + |
|
300 | + // Make sure our temp return type is correct. |
|
301 | + $this->temp_return_type = $this->return_type; |
|
302 | + |
|
303 | + // Make sure our database is loaded |
|
304 | + if (!is_null($db)) { |
|
305 | + $this->db = $db; |
|
306 | + } |
|
307 | + else { |
|
308 | + // Auto Init the damn database.... |
|
309 | + $this->load->database(); |
|
310 | + } |
|
311 | + |
|
312 | + // Do we have a form_validation library? |
|
313 | + if (! is_null($form_validation)) { |
|
314 | + $this->form_validation = $form_validation; |
|
315 | + } |
|
316 | + else { |
|
317 | + $this->load->library('form_validation'); |
|
318 | + } |
|
319 | 319 | |
320 | - log_message('debug', 'CIDbModel Class Initialized'); |
|
321 | - } |
|
322 | - |
|
323 | - //-------------------------------------------------------------------- |
|
324 | - |
|
325 | - //-------------------------------------------------------------------- |
|
326 | - // CRUD Methods |
|
327 | - //-------------------------------------------------------------------- |
|
328 | - |
|
329 | - /** |
|
330 | - * A simple way to grab the first result of a search only. |
|
331 | - */ |
|
332 | - public function first() |
|
333 | - { |
|
334 | - $rows = $this->limit(1, 0)->find_all(); |
|
335 | - |
|
336 | - if (is_array($rows) && count($rows)) { |
|
337 | - return $rows[0]; |
|
338 | - } |
|
339 | - |
|
340 | - return $rows; |
|
341 | - } |
|
342 | - |
|
343 | - //-------------------------------------------------------------------- |
|
344 | - |
|
345 | - |
|
346 | - /** |
|
347 | - * Finds a single record based on it's primary key. Will ignore deleted rows. |
|
348 | - * |
|
349 | - * @param mixed $id The primary_key value of the object to retrieve. |
|
350 | - * @return object |
|
351 | - */ |
|
352 | - public function find($id) |
|
353 | - { |
|
354 | - $this->trigger('before_find', ['id' => $id, 'method' => 'find']); |
|
355 | - |
|
356 | - // Ignore any soft-deleted rows |
|
357 | - if ($this->soft_deletes) { |
|
358 | - // We only need to modify the where statement if |
|
359 | - // temp_with_deleted is false. |
|
360 | - if ($this->temp_with_deleted !== true) { |
|
361 | - $this->db->where($this->soft_delete_key, false); |
|
362 | - } |
|
363 | - |
|
364 | - $this->temp_with_deleted = false; |
|
365 | - } |
|
366 | - |
|
367 | - $this->db->where($this->primary_key, $id); |
|
368 | - $row = $this->db->get($this->table_name); |
|
369 | - $row = $this->temp_return_type == 'array' ? $row->row_array() : $row->row(0, $this->temp_return_type); |
|
370 | - |
|
371 | - if ( ! empty($row)) |
|
372 | - { |
|
373 | - $row = $this->trigger('after_find', ['id' => $id, 'method' => 'find', 'fields' => $row]); |
|
374 | - } |
|
375 | - |
|
376 | - // Reset our return type |
|
377 | - $this->temp_return_type = $this->return_type; |
|
378 | - |
|
379 | - return $row; |
|
380 | - } |
|
381 | - |
|
382 | - //-------------------------------------------------------------------- |
|
383 | - |
|
384 | - /** |
|
385 | - * Fetch a single record based on an arbitrary WHERE call. Can be |
|
386 | - * any valid value to $this->db->where(). Will not pull in deleted rows |
|
387 | - * if using soft deletes. |
|
388 | - * |
|
389 | - * @return object |
|
390 | - */ |
|
391 | - public function find_by() |
|
392 | - { |
|
393 | - $where = func_get_args(); |
|
394 | - $this->_set_where($where); |
|
395 | - |
|
396 | - // Ignore any soft-deleted rows |
|
397 | - if ($this->soft_deletes) { |
|
398 | - // We only need to modify the where statement if |
|
399 | - // temp_with_deleted is false. |
|
400 | - if ($this->temp_with_deleted !== true) { |
|
401 | - $this->db->where($this->soft_delete_key, false); |
|
402 | - } |
|
403 | - |
|
404 | - $this->temp_with_deleted = false; |
|
405 | - } |
|
406 | - |
|
407 | - $this->trigger('before_find', ['method' => 'find_by', 'fields' => $where]); |
|
408 | - |
|
409 | - $row = $this->db->get($this->table_name); |
|
410 | - $row = $this->temp_return_type == 'array' ? $row->row_array() : $row->row(0, $this->temp_return_type); |
|
411 | - |
|
412 | - if ( ! empty($row)) |
|
413 | - { |
|
414 | - $row = $this->trigger('after_find', ['method' => 'find_by', 'fields' => $row]); |
|
415 | - } |
|
416 | - |
|
417 | - // Reset our return type |
|
418 | - $this->temp_return_type = $this->return_type; |
|
419 | - |
|
420 | - return $row; |
|
421 | - } |
|
422 | - |
|
423 | - //-------------------------------------------------------------------- |
|
424 | - |
|
425 | - /** |
|
426 | - * Retrieves a number of items based on an array of primary_values passed in. |
|
427 | - * |
|
428 | - * @param array $values An array of primary key values to find. |
|
429 | - * |
|
430 | - * @return object or FALSE |
|
431 | - */ |
|
432 | - public function find_many($values) |
|
433 | - { |
|
434 | - $this->db->where_in($this->primary_key, $values); |
|
435 | - |
|
436 | - return $this->find_all(); |
|
437 | - } |
|
438 | - |
|
439 | - //-------------------------------------------------------------------- |
|
440 | - |
|
441 | - /** |
|
442 | - * Retrieves a number of items based on an arbitrary WHERE call. Can be |
|
443 | - * any set of parameters valid to $db->where. |
|
444 | - * |
|
445 | - * @return object or FALSE |
|
446 | - */ |
|
447 | - public function find_many_by() |
|
448 | - { |
|
449 | - $where = func_get_args(); |
|
450 | - $this->_set_where($where); |
|
451 | - |
|
452 | - return $this->find_all(); |
|
453 | - } |
|
454 | - |
|
455 | - //-------------------------------------------------------------------- |
|
456 | - |
|
457 | - /** |
|
458 | - * Fetch all of the records in the table. Can be used with scoped calls |
|
459 | - * to restrict the results. |
|
460 | - * |
|
461 | - * @return object or FALSE |
|
462 | - */ |
|
463 | - public function find_all() |
|
464 | - { |
|
465 | - $this->trigger('before_find', ['method' => 'find_all']); |
|
466 | - |
|
467 | - // Ignore any soft-deleted rows |
|
468 | - if ($this->soft_deletes) { |
|
469 | - // We only need to modify the where statement if |
|
470 | - // temp_with_deleted is false. |
|
471 | - if ($this->temp_with_deleted !== true) { |
|
472 | - $this->db->where($this->soft_delete_key, false); |
|
473 | - } |
|
474 | - |
|
475 | - $this->temp_with_deleted = false; |
|
476 | - } |
|
477 | - |
|
478 | - $rows = $this->db->get($this->table_name); |
|
479 | - $rows = $this->temp_return_type == 'array' ? $rows->result_array() : $rows->result($this->temp_return_type); |
|
480 | - |
|
481 | - if (is_array($rows)) { |
|
482 | - foreach ($rows as $key => &$row) { |
|
483 | - $row = $this->trigger('after_find', ['method' => 'find_all', 'fields' => $row] ); |
|
484 | - } |
|
485 | - } |
|
486 | - |
|
487 | - // Reset our return type |
|
488 | - $this->temp_return_type = $this->return_type; |
|
489 | - |
|
490 | - return $rows; |
|
491 | - } |
|
492 | - |
|
493 | - //-------------------------------------------------------------------- |
|
494 | - |
|
495 | - /** |
|
496 | - * Inserts data into the database. |
|
497 | - * |
|
498 | - * @param array $data An array of key/value pairs to insert to database. |
|
499 | - * @param array $skip_validation If TRUE, will skip validation of data for this call only. |
|
500 | - * @return mixed The primary_key value of the inserted record, or FALSE. |
|
501 | - */ |
|
502 | - public function insert($data, $skip_validation = null) |
|
503 | - { |
|
504 | - $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
505 | - |
|
506 | - if ($skip_validation === FALSE) { |
|
507 | - $data = $this->validate($data, 'insert', $skip_validation); |
|
508 | - } |
|
509 | - |
|
510 | - if ($data !== FALSE) { |
|
511 | - $data = $this->trigger('before_insert', ['method' => 'insert', 'fields' => $data]); |
|
512 | - |
|
513 | - $this->db->insert($this->table_name, $this->prep_data($data) ); |
|
514 | - |
|
515 | - if ($this->return_insert_id) { |
|
516 | - $id = $this->db->insert_id(); |
|
517 | - |
|
518 | - $this->trigger('after_insert', ['id' => $id, 'fields' => $data, 'method' => 'insert']); |
|
519 | - |
|
520 | - return $id; |
|
521 | - } |
|
522 | - |
|
523 | - return TRUE; |
|
524 | - } else { |
|
525 | - return FALSE; |
|
526 | - } |
|
527 | - } |
|
528 | - |
|
529 | - //-------------------------------------------------------------------- |
|
530 | - |
|
531 | - /** |
|
532 | - * Inserts multiple rows into the database at once. Takes an associative |
|
533 | - * array of value pairs. |
|
534 | - * |
|
535 | - * $data = array( |
|
536 | - * array( |
|
537 | - * 'title' => 'My title' |
|
538 | - * ), |
|
539 | - * array( |
|
540 | - * 'title' => 'My Other Title' |
|
541 | - * ) |
|
542 | - * ); |
|
543 | - * |
|
544 | - * @param array $data An associate array of rows to insert |
|
545 | - * @param array $skip_validation If TRUE, will skip validation of data for this call only. |
|
546 | - * @return bool |
|
547 | - */ |
|
548 | - public function insert_batch($data, $skip_validation = null) |
|
549 | - { |
|
550 | - $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
551 | - |
|
552 | - if ($skip_validation === FALSE) { |
|
553 | - $data = $this->validate($data, 'insert', $skip_validation); |
|
554 | - } |
|
555 | - |
|
556 | - if ($data !== FALSE) { |
|
557 | - $data['batch'] = true; |
|
558 | - $data = $this->trigger('before_insert', ['method' => 'insert_batch', 'fields' => $data] ); |
|
559 | - unset($data['batch']); |
|
560 | - |
|
561 | - return $this->db->insert_batch($this->table_name, $data); |
|
562 | - } else { |
|
563 | - return FALSE; |
|
564 | - } |
|
565 | - } |
|
566 | - |
|
567 | - //-------------------------------------------------------------------- |
|
568 | - |
|
569 | - /** |
|
570 | - * Performs the SQL standard for a combined DELETE + INSERT, using |
|
571 | - * PRIMARY and UNIQUE keys to determine which row to replace. |
|
572 | - * |
|
573 | - * See CI's documentation for the replace method. We simply wrap |
|
574 | - * our validation and triggers around their method. |
|
575 | - * |
|
576 | - * @param $data |
|
577 | - * @param null $skip_validation |
|
578 | - * @return bool |
|
579 | - */ |
|
580 | - public function replace($data, $skip_validation=null) |
|
581 | - { |
|
582 | - $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
583 | - |
|
584 | - if ($skip_validation === FALSE) { |
|
585 | - $data = $this->validate($data, 'insert', $skip_validation); |
|
586 | - } |
|
587 | - |
|
588 | - if ($data !== FALSE) { |
|
589 | - $this->db->replace($this->table_name, $this->prep_data($data)); |
|
590 | - |
|
591 | - if ($this->return_insert_id) { |
|
592 | - $id = $this->db->insert_id(); |
|
593 | - |
|
594 | - $this->trigger('after_insert', ['id' => $id, 'fields' => $data, 'method'=>'replace']); |
|
595 | - |
|
596 | - return $id; |
|
597 | - } |
|
598 | - |
|
599 | - return TRUE; |
|
600 | - } else { |
|
601 | - return FALSE; |
|
602 | - } |
|
603 | - } |
|
604 | - |
|
605 | - //-------------------------------------------------------------------- |
|
606 | - |
|
607 | - |
|
608 | - /** |
|
609 | - * Updates an existing record in the database. |
|
610 | - * |
|
611 | - * @param mixed $id The primary_key value of the record to update. |
|
612 | - * @param array $data An array of value pairs to update in the record. |
|
613 | - * @param array $skip_validation If TRUE, will skip validation of data for this call only. |
|
614 | - * @return bool |
|
615 | - */ |
|
616 | - public function update($id, $data, $skip_validation = null) |
|
617 | - { |
|
618 | - $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
619 | - |
|
620 | - if ($skip_validation === FALSE) { |
|
621 | - $data = $this->validate($data); |
|
622 | - } |
|
623 | - |
|
624 | - // Will be false if it didn't validate. |
|
625 | - if ($data !== FALSE) { |
|
320 | + log_message('debug', 'CIDbModel Class Initialized'); |
|
321 | + } |
|
322 | + |
|
323 | + //-------------------------------------------------------------------- |
|
324 | + |
|
325 | + //-------------------------------------------------------------------- |
|
326 | + // CRUD Methods |
|
327 | + //-------------------------------------------------------------------- |
|
328 | + |
|
329 | + /** |
|
330 | + * A simple way to grab the first result of a search only. |
|
331 | + */ |
|
332 | + public function first() |
|
333 | + { |
|
334 | + $rows = $this->limit(1, 0)->find_all(); |
|
335 | + |
|
336 | + if (is_array($rows) && count($rows)) { |
|
337 | + return $rows[0]; |
|
338 | + } |
|
339 | + |
|
340 | + return $rows; |
|
341 | + } |
|
342 | + |
|
343 | + //-------------------------------------------------------------------- |
|
344 | + |
|
345 | + |
|
346 | + /** |
|
347 | + * Finds a single record based on it's primary key. Will ignore deleted rows. |
|
348 | + * |
|
349 | + * @param mixed $id The primary_key value of the object to retrieve. |
|
350 | + * @return object |
|
351 | + */ |
|
352 | + public function find($id) |
|
353 | + { |
|
354 | + $this->trigger('before_find', ['id' => $id, 'method' => 'find']); |
|
355 | + |
|
356 | + // Ignore any soft-deleted rows |
|
357 | + if ($this->soft_deletes) { |
|
358 | + // We only need to modify the where statement if |
|
359 | + // temp_with_deleted is false. |
|
360 | + if ($this->temp_with_deleted !== true) { |
|
361 | + $this->db->where($this->soft_delete_key, false); |
|
362 | + } |
|
363 | + |
|
364 | + $this->temp_with_deleted = false; |
|
365 | + } |
|
366 | + |
|
367 | + $this->db->where($this->primary_key, $id); |
|
368 | + $row = $this->db->get($this->table_name); |
|
369 | + $row = $this->temp_return_type == 'array' ? $row->row_array() : $row->row(0, $this->temp_return_type); |
|
370 | + |
|
371 | + if ( ! empty($row)) |
|
372 | + { |
|
373 | + $row = $this->trigger('after_find', ['id' => $id, 'method' => 'find', 'fields' => $row]); |
|
374 | + } |
|
375 | + |
|
376 | + // Reset our return type |
|
377 | + $this->temp_return_type = $this->return_type; |
|
378 | + |
|
379 | + return $row; |
|
380 | + } |
|
381 | + |
|
382 | + //-------------------------------------------------------------------- |
|
383 | + |
|
384 | + /** |
|
385 | + * Fetch a single record based on an arbitrary WHERE call. Can be |
|
386 | + * any valid value to $this->db->where(). Will not pull in deleted rows |
|
387 | + * if using soft deletes. |
|
388 | + * |
|
389 | + * @return object |
|
390 | + */ |
|
391 | + public function find_by() |
|
392 | + { |
|
393 | + $where = func_get_args(); |
|
394 | + $this->_set_where($where); |
|
395 | + |
|
396 | + // Ignore any soft-deleted rows |
|
397 | + if ($this->soft_deletes) { |
|
398 | + // We only need to modify the where statement if |
|
399 | + // temp_with_deleted is false. |
|
400 | + if ($this->temp_with_deleted !== true) { |
|
401 | + $this->db->where($this->soft_delete_key, false); |
|
402 | + } |
|
403 | + |
|
404 | + $this->temp_with_deleted = false; |
|
405 | + } |
|
406 | + |
|
407 | + $this->trigger('before_find', ['method' => 'find_by', 'fields' => $where]); |
|
408 | + |
|
409 | + $row = $this->db->get($this->table_name); |
|
410 | + $row = $this->temp_return_type == 'array' ? $row->row_array() : $row->row(0, $this->temp_return_type); |
|
411 | + |
|
412 | + if ( ! empty($row)) |
|
413 | + { |
|
414 | + $row = $this->trigger('after_find', ['method' => 'find_by', 'fields' => $row]); |
|
415 | + } |
|
416 | + |
|
417 | + // Reset our return type |
|
418 | + $this->temp_return_type = $this->return_type; |
|
419 | + |
|
420 | + return $row; |
|
421 | + } |
|
422 | + |
|
423 | + //-------------------------------------------------------------------- |
|
424 | + |
|
425 | + /** |
|
426 | + * Retrieves a number of items based on an array of primary_values passed in. |
|
427 | + * |
|
428 | + * @param array $values An array of primary key values to find. |
|
429 | + * |
|
430 | + * @return object or FALSE |
|
431 | + */ |
|
432 | + public function find_many($values) |
|
433 | + { |
|
434 | + $this->db->where_in($this->primary_key, $values); |
|
435 | + |
|
436 | + return $this->find_all(); |
|
437 | + } |
|
438 | + |
|
439 | + //-------------------------------------------------------------------- |
|
440 | + |
|
441 | + /** |
|
442 | + * Retrieves a number of items based on an arbitrary WHERE call. Can be |
|
443 | + * any set of parameters valid to $db->where. |
|
444 | + * |
|
445 | + * @return object or FALSE |
|
446 | + */ |
|
447 | + public function find_many_by() |
|
448 | + { |
|
449 | + $where = func_get_args(); |
|
450 | + $this->_set_where($where); |
|
451 | + |
|
452 | + return $this->find_all(); |
|
453 | + } |
|
454 | + |
|
455 | + //-------------------------------------------------------------------- |
|
456 | + |
|
457 | + /** |
|
458 | + * Fetch all of the records in the table. Can be used with scoped calls |
|
459 | + * to restrict the results. |
|
460 | + * |
|
461 | + * @return object or FALSE |
|
462 | + */ |
|
463 | + public function find_all() |
|
464 | + { |
|
465 | + $this->trigger('before_find', ['method' => 'find_all']); |
|
466 | + |
|
467 | + // Ignore any soft-deleted rows |
|
468 | + if ($this->soft_deletes) { |
|
469 | + // We only need to modify the where statement if |
|
470 | + // temp_with_deleted is false. |
|
471 | + if ($this->temp_with_deleted !== true) { |
|
472 | + $this->db->where($this->soft_delete_key, false); |
|
473 | + } |
|
474 | + |
|
475 | + $this->temp_with_deleted = false; |
|
476 | + } |
|
477 | + |
|
478 | + $rows = $this->db->get($this->table_name); |
|
479 | + $rows = $this->temp_return_type == 'array' ? $rows->result_array() : $rows->result($this->temp_return_type); |
|
480 | + |
|
481 | + if (is_array($rows)) { |
|
482 | + foreach ($rows as $key => &$row) { |
|
483 | + $row = $this->trigger('after_find', ['method' => 'find_all', 'fields' => $row] ); |
|
484 | + } |
|
485 | + } |
|
486 | + |
|
487 | + // Reset our return type |
|
488 | + $this->temp_return_type = $this->return_type; |
|
489 | + |
|
490 | + return $rows; |
|
491 | + } |
|
492 | + |
|
493 | + //-------------------------------------------------------------------- |
|
494 | + |
|
495 | + /** |
|
496 | + * Inserts data into the database. |
|
497 | + * |
|
498 | + * @param array $data An array of key/value pairs to insert to database. |
|
499 | + * @param array $skip_validation If TRUE, will skip validation of data for this call only. |
|
500 | + * @return mixed The primary_key value of the inserted record, or FALSE. |
|
501 | + */ |
|
502 | + public function insert($data, $skip_validation = null) |
|
503 | + { |
|
504 | + $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
505 | + |
|
506 | + if ($skip_validation === FALSE) { |
|
507 | + $data = $this->validate($data, 'insert', $skip_validation); |
|
508 | + } |
|
509 | + |
|
510 | + if ($data !== FALSE) { |
|
511 | + $data = $this->trigger('before_insert', ['method' => 'insert', 'fields' => $data]); |
|
512 | + |
|
513 | + $this->db->insert($this->table_name, $this->prep_data($data) ); |
|
514 | + |
|
515 | + if ($this->return_insert_id) { |
|
516 | + $id = $this->db->insert_id(); |
|
517 | + |
|
518 | + $this->trigger('after_insert', ['id' => $id, 'fields' => $data, 'method' => 'insert']); |
|
519 | + |
|
520 | + return $id; |
|
521 | + } |
|
522 | + |
|
523 | + return TRUE; |
|
524 | + } else { |
|
525 | + return FALSE; |
|
526 | + } |
|
527 | + } |
|
528 | + |
|
529 | + //-------------------------------------------------------------------- |
|
530 | + |
|
531 | + /** |
|
532 | + * Inserts multiple rows into the database at once. Takes an associative |
|
533 | + * array of value pairs. |
|
534 | + * |
|
535 | + * $data = array( |
|
536 | + * array( |
|
537 | + * 'title' => 'My title' |
|
538 | + * ), |
|
539 | + * array( |
|
540 | + * 'title' => 'My Other Title' |
|
541 | + * ) |
|
542 | + * ); |
|
543 | + * |
|
544 | + * @param array $data An associate array of rows to insert |
|
545 | + * @param array $skip_validation If TRUE, will skip validation of data for this call only. |
|
546 | + * @return bool |
|
547 | + */ |
|
548 | + public function insert_batch($data, $skip_validation = null) |
|
549 | + { |
|
550 | + $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
551 | + |
|
552 | + if ($skip_validation === FALSE) { |
|
553 | + $data = $this->validate($data, 'insert', $skip_validation); |
|
554 | + } |
|
555 | + |
|
556 | + if ($data !== FALSE) { |
|
557 | + $data['batch'] = true; |
|
558 | + $data = $this->trigger('before_insert', ['method' => 'insert_batch', 'fields' => $data] ); |
|
559 | + unset($data['batch']); |
|
560 | + |
|
561 | + return $this->db->insert_batch($this->table_name, $data); |
|
562 | + } else { |
|
563 | + return FALSE; |
|
564 | + } |
|
565 | + } |
|
566 | + |
|
567 | + //-------------------------------------------------------------------- |
|
568 | + |
|
569 | + /** |
|
570 | + * Performs the SQL standard for a combined DELETE + INSERT, using |
|
571 | + * PRIMARY and UNIQUE keys to determine which row to replace. |
|
572 | + * |
|
573 | + * See CI's documentation for the replace method. We simply wrap |
|
574 | + * our validation and triggers around their method. |
|
575 | + * |
|
576 | + * @param $data |
|
577 | + * @param null $skip_validation |
|
578 | + * @return bool |
|
579 | + */ |
|
580 | + public function replace($data, $skip_validation=null) |
|
581 | + { |
|
582 | + $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
583 | + |
|
584 | + if ($skip_validation === FALSE) { |
|
585 | + $data = $this->validate($data, 'insert', $skip_validation); |
|
586 | + } |
|
587 | + |
|
588 | + if ($data !== FALSE) { |
|
589 | + $this->db->replace($this->table_name, $this->prep_data($data)); |
|
590 | + |
|
591 | + if ($this->return_insert_id) { |
|
592 | + $id = $this->db->insert_id(); |
|
593 | + |
|
594 | + $this->trigger('after_insert', ['id' => $id, 'fields' => $data, 'method'=>'replace']); |
|
595 | + |
|
596 | + return $id; |
|
597 | + } |
|
598 | + |
|
599 | + return TRUE; |
|
600 | + } else { |
|
601 | + return FALSE; |
|
602 | + } |
|
603 | + } |
|
604 | + |
|
605 | + //-------------------------------------------------------------------- |
|
606 | + |
|
607 | + |
|
608 | + /** |
|
609 | + * Updates an existing record in the database. |
|
610 | + * |
|
611 | + * @param mixed $id The primary_key value of the record to update. |
|
612 | + * @param array $data An array of value pairs to update in the record. |
|
613 | + * @param array $skip_validation If TRUE, will skip validation of data for this call only. |
|
614 | + * @return bool |
|
615 | + */ |
|
616 | + public function update($id, $data, $skip_validation = null) |
|
617 | + { |
|
618 | + $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
619 | + |
|
620 | + if ($skip_validation === FALSE) { |
|
621 | + $data = $this->validate($data); |
|
622 | + } |
|
623 | + |
|
624 | + // Will be false if it didn't validate. |
|
625 | + if ($data !== FALSE) { |
|
626 | 626 | |
627 | - $data = $this->trigger('before_update', ['id' => $id, 'method' =>'update', 'fields' => $data] ); |
|
627 | + $data = $this->trigger('before_update', ['id' => $id, 'method' =>'update', 'fields' => $data] ); |
|
628 | 628 | |
629 | - $this->db->where($this->primary_key, $id); |
|
630 | - $this->db->set( $this->prep_data($data) ); |
|
631 | - $result = $this->db->update($this->table_name); |
|
632 | - |
|
633 | - $this->trigger('after_update', ['id' => $id, 'fields' => $data, 'result' => $result, 'method' => 'update']); |
|
634 | - |
|
635 | - return $result; |
|
636 | - } else { |
|
637 | - return FALSE; |
|
638 | - } |
|
639 | - } |
|
640 | - |
|
641 | - //-------------------------------------------------------------------- |
|
642 | - |
|
643 | - /** |
|
644 | - * Updates multiple records in the database at once. |
|
645 | - * |
|
646 | - * $data = array( |
|
647 | - * array( |
|
648 | - * 'title' => 'My title', |
|
649 | - * 'body' => 'body 1' |
|
650 | - * ), |
|
651 | - * array( |
|
652 | - * 'title' => 'Another Title', |
|
653 | - * 'body' => 'body 2' |
|
654 | - * ) |
|
655 | - * ); |
|
656 | - * |
|
657 | - * The $where_key should be the name of the column to match the record on. |
|
658 | - * If $where_key == 'title', then each record would be matched on that |
|
659 | - * 'title' value of the array. This does mean that the array key needs |
|
660 | - * to be provided with each row's data. |
|
661 | - * |
|
662 | - * @param array $data An associate array of row data to update. |
|
663 | - * @param string $where_key The column name to match on. |
|
664 | - * @return bool |
|
665 | - */ |
|
666 | - public function update_batch($data, $where_key) |
|
667 | - { |
|
668 | - foreach ($data as &$row) { |
|
669 | - $row = $this->trigger('before_update', ['method' => 'update_batch', 'fields' => $row] ); |
|
670 | - } |
|
671 | - |
|
672 | - $result = $this->db->update_batch($this->table_name, $data, $where_key); |
|
673 | - |
|
674 | - foreach ($data as &$row) { |
|
675 | - $this->trigger('after_update', ['fields' => $data, 'result' => $result, 'method' => 'update_batch']); |
|
676 | - } |
|
677 | - |
|
678 | - return $result; |
|
679 | - } |
|
680 | - |
|
681 | - //-------------------------------------------------------------------- |
|
682 | - |
|
683 | - /** |
|
684 | - * Updates many records by an array of ids. |
|
685 | - * |
|
686 | - * While update_batch() allows modifying multiple, arbitrary rows of data |
|
687 | - * on each row, update_many() sets the same values for each row. |
|
688 | - * |
|
689 | - * $ids = array(1, 2, 3, 5, 12); |
|
690 | - * $data = array( |
|
691 | - * 'deleted_by' => 1 |
|
692 | - * ); |
|
693 | - * |
|
694 | - * $this->model->update_many($ids, $data); |
|
695 | - * |
|
696 | - * @param array $ids An array of primary_key values to update. |
|
697 | - * @param array $data An array of value pairs to modify in each row. |
|
698 | - * @param array $skip_validation If TRUE, will skip validation of data for this call only. |
|
699 | - * @return bool |
|
700 | - */ |
|
701 | - public function update_many($ids, $data, $skip_validation = null) |
|
702 | - { |
|
703 | - if (!is_array($ids) || count($ids) == 0) return NULL; |
|
704 | - |
|
705 | - $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
706 | - |
|
707 | - if ($skip_validation === FALSE) { |
|
708 | - $data = $this->validate($data, 'update', $skip_validation); |
|
709 | - } |
|
710 | - |
|
711 | - $data = $this->trigger('before_update', ['ids' => $ids, 'method' => 'update_many', 'fields' => $data]); |
|
712 | - |
|
713 | - // Will be false if it didn't validate. |
|
714 | - if ($data !== FALSE) { |
|
715 | - $this->db->where_in($this->primary_key, $ids); |
|
716 | - $this->db->set($data); |
|
717 | - $result = $this->db->update($this->table_name); |
|
718 | - |
|
719 | - $this->trigger('after_update', ['ids' => $ids, 'fields' => $data, 'result'=>$result, 'method' => 'update_many']); |
|
720 | - |
|
721 | - return $result; |
|
722 | - } else { |
|
723 | - return FALSE; |
|
724 | - } |
|
725 | - } |
|
726 | - |
|
727 | - //-------------------------------------------------------------------- |
|
728 | - |
|
729 | - /** |
|
730 | - * Update records in the database using a standard WHERE clause. |
|
731 | - * |
|
732 | - * Your last parameter should be the $data array with values to update |
|
733 | - * on the rows. Any additional parameters should be provided to make up |
|
734 | - * a typical WHERE clause. This could be a single array, or a column name |
|
735 | - * and a value. |
|
736 | - * |
|
737 | - * $data = array('deleted_by' => 1); |
|
738 | - * $wheres = array('user_id' => 15); |
|
739 | - * |
|
740 | - * $this->update_by($wheres, $data); |
|
741 | - * $this->update_by('user_id', 15, $data); |
|
742 | - * |
|
743 | - * @param array $data An array of data pairs to update |
|
744 | - * @param one or more WHERE-acceptable entries. |
|
745 | - * @return bool |
|
746 | - */ |
|
747 | - public function update_by() |
|
748 | - { |
|
749 | - $args = func_get_args(); |
|
750 | - $data = array_pop($args); |
|
751 | - $this->_set_where($args); |
|
752 | - |
|
753 | - $data = $this->trigger('before_update', ['method' => 'update_by', 'fields' => $data]); |
|
754 | - |
|
755 | - // Will be false if it didn't validate. |
|
756 | - if ($this->validate($data) !== FALSE) { |
|
757 | - $this->db->set( $this->prep_data($data) ); |
|
758 | - $result = $this->db->update($this->table_name); |
|
759 | - |
|
760 | - $this->trigger('after_update', ['method' => 'update_by', 'fields' => $data, 'result' => $result] ); |
|
761 | - |
|
762 | - return $result; |
|
763 | - } else { |
|
764 | - return FALSE; |
|
765 | - } |
|
766 | - } |
|
767 | - |
|
768 | - //-------------------------------------------------------------------- |
|
769 | - |
|
770 | - /** |
|
771 | - * Updates all records and sets the value pairs passed in the array. |
|
772 | - * |
|
773 | - * @param array $data An array of value pairs with the data to change. |
|
774 | - * @param array $skip_validation If TRUE, will skip validation of data for this call only. |
|
775 | - * @return bool |
|
776 | - */ |
|
777 | - public function update_all($data, $skip_validation = FALSE) |
|
778 | - { |
|
779 | - $data = $this->trigger('before_update', ['method' => 'update_all', 'fields' => $data] ); |
|
780 | - |
|
781 | - $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
782 | - |
|
783 | - if ($skip_validation === FALSE) { |
|
784 | - $data = $this->validate($data); |
|
785 | - } |
|
786 | - |
|
787 | - // Will be false if it didn't validate. |
|
788 | - if ($data !== FALSE) { |
|
789 | - $this->db->set( $this->prep_data($data) ); |
|
790 | - $result = $this->db->update($this->table_name); |
|
791 | - |
|
792 | - $this->trigger('after_update', ['method' => 'update_all', 'fields' => $data, 'result' => $result] ); |
|
793 | - |
|
794 | - return $result; |
|
795 | - } else { |
|
796 | - return FALSE; |
|
797 | - } |
|
798 | - } |
|
799 | - |
|
800 | - //-------------------------------------------------------------------- |
|
801 | - |
|
802 | - /** |
|
803 | - * Increments the value of field for a given row, selected by the |
|
804 | - * primary key for the table. |
|
805 | - * |
|
806 | - * @param $id |
|
807 | - * @param $field |
|
808 | - * @param int $value |
|
809 | - * @return mixed |
|
810 | - */ |
|
811 | - public function increment($id, $field, $value=1) |
|
812 | - { |
|
813 | - $value = (int)abs($value); |
|
814 | - |
|
815 | - $this->db->where($this->primary_key, $id); |
|
816 | - $this->db->set($field, "{$field}+{$value}", false); |
|
817 | - |
|
818 | - return $this->db->update($this->table_name); |
|
819 | - } |
|
820 | - |
|
821 | - //-------------------------------------------------------------------- |
|
822 | - |
|
823 | - /** |
|
824 | - * Increments the value of field for a given row, selected by the |
|
825 | - * primary key for the table. |
|
826 | - * |
|
827 | - * @param $id |
|
828 | - * @param $field |
|
829 | - * @param int $value |
|
830 | - * @return mixed |
|
831 | - */ |
|
832 | - public function decrement($id, $field, $value=1) |
|
833 | - { |
|
834 | - $value = (int)abs($value); |
|
835 | - |
|
836 | - $this->db->where($this->primary_key, $id); |
|
837 | - $this->db->set($field, "{$field}-{$value}", false); |
|
838 | - |
|
839 | - return $this->db->update($this->table_name); |
|
840 | - } |
|
841 | - |
|
842 | - //-------------------------------------------------------------------- |
|
843 | - |
|
844 | - /** |
|
845 | - * Deletes a row by it's primary key value. |
|
846 | - * |
|
847 | - * @param mixed $id The primary key value of the row to delete. |
|
848 | - * @return bool |
|
849 | - */ |
|
850 | - public function delete($id) |
|
851 | - { |
|
852 | - $this->trigger('before_delete', ['id' => $id, 'method' => 'delete'] ); |
|
853 | - |
|
854 | - $this->db->where($this->primary_key, $id); |
|
629 | + $this->db->where($this->primary_key, $id); |
|
630 | + $this->db->set( $this->prep_data($data) ); |
|
631 | + $result = $this->db->update($this->table_name); |
|
632 | + |
|
633 | + $this->trigger('after_update', ['id' => $id, 'fields' => $data, 'result' => $result, 'method' => 'update']); |
|
634 | + |
|
635 | + return $result; |
|
636 | + } else { |
|
637 | + return FALSE; |
|
638 | + } |
|
639 | + } |
|
640 | + |
|
641 | + //-------------------------------------------------------------------- |
|
642 | + |
|
643 | + /** |
|
644 | + * Updates multiple records in the database at once. |
|
645 | + * |
|
646 | + * $data = array( |
|
647 | + * array( |
|
648 | + * 'title' => 'My title', |
|
649 | + * 'body' => 'body 1' |
|
650 | + * ), |
|
651 | + * array( |
|
652 | + * 'title' => 'Another Title', |
|
653 | + * 'body' => 'body 2' |
|
654 | + * ) |
|
655 | + * ); |
|
656 | + * |
|
657 | + * The $where_key should be the name of the column to match the record on. |
|
658 | + * If $where_key == 'title', then each record would be matched on that |
|
659 | + * 'title' value of the array. This does mean that the array key needs |
|
660 | + * to be provided with each row's data. |
|
661 | + * |
|
662 | + * @param array $data An associate array of row data to update. |
|
663 | + * @param string $where_key The column name to match on. |
|
664 | + * @return bool |
|
665 | + */ |
|
666 | + public function update_batch($data, $where_key) |
|
667 | + { |
|
668 | + foreach ($data as &$row) { |
|
669 | + $row = $this->trigger('before_update', ['method' => 'update_batch', 'fields' => $row] ); |
|
670 | + } |
|
671 | + |
|
672 | + $result = $this->db->update_batch($this->table_name, $data, $where_key); |
|
673 | + |
|
674 | + foreach ($data as &$row) { |
|
675 | + $this->trigger('after_update', ['fields' => $data, 'result' => $result, 'method' => 'update_batch']); |
|
676 | + } |
|
677 | + |
|
678 | + return $result; |
|
679 | + } |
|
680 | + |
|
681 | + //-------------------------------------------------------------------- |
|
682 | + |
|
683 | + /** |
|
684 | + * Updates many records by an array of ids. |
|
685 | + * |
|
686 | + * While update_batch() allows modifying multiple, arbitrary rows of data |
|
687 | + * on each row, update_many() sets the same values for each row. |
|
688 | + * |
|
689 | + * $ids = array(1, 2, 3, 5, 12); |
|
690 | + * $data = array( |
|
691 | + * 'deleted_by' => 1 |
|
692 | + * ); |
|
693 | + * |
|
694 | + * $this->model->update_many($ids, $data); |
|
695 | + * |
|
696 | + * @param array $ids An array of primary_key values to update. |
|
697 | + * @param array $data An array of value pairs to modify in each row. |
|
698 | + * @param array $skip_validation If TRUE, will skip validation of data for this call only. |
|
699 | + * @return bool |
|
700 | + */ |
|
701 | + public function update_many($ids, $data, $skip_validation = null) |
|
702 | + { |
|
703 | + if (!is_array($ids) || count($ids) == 0) return NULL; |
|
704 | + |
|
705 | + $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
706 | + |
|
707 | + if ($skip_validation === FALSE) { |
|
708 | + $data = $this->validate($data, 'update', $skip_validation); |
|
709 | + } |
|
710 | + |
|
711 | + $data = $this->trigger('before_update', ['ids' => $ids, 'method' => 'update_many', 'fields' => $data]); |
|
712 | + |
|
713 | + // Will be false if it didn't validate. |
|
714 | + if ($data !== FALSE) { |
|
715 | + $this->db->where_in($this->primary_key, $ids); |
|
716 | + $this->db->set($data); |
|
717 | + $result = $this->db->update($this->table_name); |
|
718 | + |
|
719 | + $this->trigger('after_update', ['ids' => $ids, 'fields' => $data, 'result'=>$result, 'method' => 'update_many']); |
|
720 | + |
|
721 | + return $result; |
|
722 | + } else { |
|
723 | + return FALSE; |
|
724 | + } |
|
725 | + } |
|
726 | + |
|
727 | + //-------------------------------------------------------------------- |
|
728 | + |
|
729 | + /** |
|
730 | + * Update records in the database using a standard WHERE clause. |
|
731 | + * |
|
732 | + * Your last parameter should be the $data array with values to update |
|
733 | + * on the rows. Any additional parameters should be provided to make up |
|
734 | + * a typical WHERE clause. This could be a single array, or a column name |
|
735 | + * and a value. |
|
736 | + * |
|
737 | + * $data = array('deleted_by' => 1); |
|
738 | + * $wheres = array('user_id' => 15); |
|
739 | + * |
|
740 | + * $this->update_by($wheres, $data); |
|
741 | + * $this->update_by('user_id', 15, $data); |
|
742 | + * |
|
743 | + * @param array $data An array of data pairs to update |
|
744 | + * @param one or more WHERE-acceptable entries. |
|
745 | + * @return bool |
|
746 | + */ |
|
747 | + public function update_by() |
|
748 | + { |
|
749 | + $args = func_get_args(); |
|
750 | + $data = array_pop($args); |
|
751 | + $this->_set_where($args); |
|
752 | + |
|
753 | + $data = $this->trigger('before_update', ['method' => 'update_by', 'fields' => $data]); |
|
754 | + |
|
755 | + // Will be false if it didn't validate. |
|
756 | + if ($this->validate($data) !== FALSE) { |
|
757 | + $this->db->set( $this->prep_data($data) ); |
|
758 | + $result = $this->db->update($this->table_name); |
|
759 | + |
|
760 | + $this->trigger('after_update', ['method' => 'update_by', 'fields' => $data, 'result' => $result] ); |
|
761 | + |
|
762 | + return $result; |
|
763 | + } else { |
|
764 | + return FALSE; |
|
765 | + } |
|
766 | + } |
|
767 | + |
|
768 | + //-------------------------------------------------------------------- |
|
769 | + |
|
770 | + /** |
|
771 | + * Updates all records and sets the value pairs passed in the array. |
|
772 | + * |
|
773 | + * @param array $data An array of value pairs with the data to change. |
|
774 | + * @param array $skip_validation If TRUE, will skip validation of data for this call only. |
|
775 | + * @return bool |
|
776 | + */ |
|
777 | + public function update_all($data, $skip_validation = FALSE) |
|
778 | + { |
|
779 | + $data = $this->trigger('before_update', ['method' => 'update_all', 'fields' => $data] ); |
|
780 | + |
|
781 | + $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
782 | + |
|
783 | + if ($skip_validation === FALSE) { |
|
784 | + $data = $this->validate($data); |
|
785 | + } |
|
786 | + |
|
787 | + // Will be false if it didn't validate. |
|
788 | + if ($data !== FALSE) { |
|
789 | + $this->db->set( $this->prep_data($data) ); |
|
790 | + $result = $this->db->update($this->table_name); |
|
791 | + |
|
792 | + $this->trigger('after_update', ['method' => 'update_all', 'fields' => $data, 'result' => $result] ); |
|
793 | + |
|
794 | + return $result; |
|
795 | + } else { |
|
796 | + return FALSE; |
|
797 | + } |
|
798 | + } |
|
799 | + |
|
800 | + //-------------------------------------------------------------------- |
|
801 | + |
|
802 | + /** |
|
803 | + * Increments the value of field for a given row, selected by the |
|
804 | + * primary key for the table. |
|
805 | + * |
|
806 | + * @param $id |
|
807 | + * @param $field |
|
808 | + * @param int $value |
|
809 | + * @return mixed |
|
810 | + */ |
|
811 | + public function increment($id, $field, $value=1) |
|
812 | + { |
|
813 | + $value = (int)abs($value); |
|
814 | + |
|
815 | + $this->db->where($this->primary_key, $id); |
|
816 | + $this->db->set($field, "{$field}+{$value}", false); |
|
817 | + |
|
818 | + return $this->db->update($this->table_name); |
|
819 | + } |
|
820 | + |
|
821 | + //-------------------------------------------------------------------- |
|
822 | + |
|
823 | + /** |
|
824 | + * Increments the value of field for a given row, selected by the |
|
825 | + * primary key for the table. |
|
826 | + * |
|
827 | + * @param $id |
|
828 | + * @param $field |
|
829 | + * @param int $value |
|
830 | + * @return mixed |
|
831 | + */ |
|
832 | + public function decrement($id, $field, $value=1) |
|
833 | + { |
|
834 | + $value = (int)abs($value); |
|
835 | + |
|
836 | + $this->db->where($this->primary_key, $id); |
|
837 | + $this->db->set($field, "{$field}-{$value}", false); |
|
838 | + |
|
839 | + return $this->db->update($this->table_name); |
|
840 | + } |
|
841 | + |
|
842 | + //-------------------------------------------------------------------- |
|
843 | + |
|
844 | + /** |
|
845 | + * Deletes a row by it's primary key value. |
|
846 | + * |
|
847 | + * @param mixed $id The primary key value of the row to delete. |
|
848 | + * @return bool |
|
849 | + */ |
|
850 | + public function delete($id) |
|
851 | + { |
|
852 | + $this->trigger('before_delete', ['id' => $id, 'method' => 'delete'] ); |
|
853 | + |
|
854 | + $this->db->where($this->primary_key, $id); |
|
855 | + |
|
856 | + if ($this->soft_deletes) { |
|
857 | + $sets = $this->log_user && is_object($this->authenticate) |
|
858 | + ? array($this->soft_delete_key => 1, $this->deleted_by_field => $this->authenticate->id()) |
|
859 | + : array($this->soft_delete_key => 1); |
|
860 | + |
|
861 | + $result = $this->db->update($this->table_name, $sets); |
|
862 | + } // Hard Delete |
|
863 | + else { |
|
864 | + $result = $this->db->delete($this->table_name); |
|
865 | + } |
|
866 | + |
|
867 | + $this->trigger('after_delete', ['id' => $id, 'method' => 'delete', 'result' => $result] ); |
|
868 | + |
|
869 | + return $result; |
|
870 | + } |
|
871 | + |
|
872 | + //-------------------------------------------------------------------- |
|
873 | + |
|
874 | + public function delete_by() |
|
875 | + { |
|
876 | + $where = func_get_args(); |
|
877 | + $this->_set_where($where); |
|
878 | + |
|
879 | + $where = $this->trigger('before_delete', ['method' => 'delete_by', 'fields' => $where]); |
|
880 | + |
|
881 | + if ($this->soft_deletes) { |
|
882 | + $sets = $this->log_user && is_object($this->authenticate) |
|
883 | + ? array($this->soft_delete_key => 1, $this->deleted_by_field => $this->authenticate->id()) |
|
884 | + : array($this->soft_delete_key => 1); |
|
885 | + |
|
886 | + $result = $this->db->update($this->table_name, $sets); |
|
887 | + } else { |
|
888 | + $result = $this->db->delete($this->table_name); |
|
889 | + } |
|
890 | + |
|
891 | + $this->trigger('after_delete', ['method' => 'delete_by', 'fields' => $where, 'result' => $result] ); |
|
892 | + |
|
893 | + return $result; |
|
894 | + } |
|
895 | + |
|
896 | + //-------------------------------------------------------------------- |
|
897 | + |
|
898 | + public function delete_many($ids) |
|
899 | + { |
|
900 | + if (!is_array($ids) || count($ids) == 0) return NULL; |
|
901 | + |
|
902 | + $ids = $this->trigger('before_delete', ['ids' => $ids, 'method' => 'delete_many'] ); |
|
903 | + |
|
904 | + $this->db->where_in($this->primary_key, $ids); |
|
905 | + |
|
906 | + if ($this->soft_deletes) { |
|
907 | + $sets = $this->log_user && is_object($this->authenticate) |
|
908 | + ? array($this->soft_delete_key => 1, $this->deleted_by_field => $this->authenticate->id()) |
|
909 | + : array($this->soft_delete_key => 1); |
|
910 | + |
|
911 | + $result = $this->db->update($this->table_name, $sets); |
|
912 | + } else { |
|
913 | + $result = $this->db->delete($this->table_name); |
|
914 | + } |
|
915 | + |
|
916 | + $this->trigger('after_delete', ['ids' => $ids, 'method' => 'delete_many', 'result' => $result]); |
|
855 | 917 | |
856 | - if ($this->soft_deletes) { |
|
857 | - $sets = $this->log_user && is_object($this->authenticate) |
|
858 | - ? array($this->soft_delete_key => 1, $this->deleted_by_field => $this->authenticate->id()) |
|
859 | - : array($this->soft_delete_key => 1); |
|
918 | + return $result; |
|
919 | + } |
|
920 | + |
|
921 | + //-------------------------------------------------------------------- |
|
922 | + |
|
923 | + //-------------------------------------------------------------------- |
|
924 | + // Scope Methods |
|
925 | + //-------------------------------------------------------------------- |
|
926 | + |
|
927 | + /** |
|
928 | + * Sets the value of the soft deletes flag. |
|
929 | + * |
|
930 | + * @param boolean $val If TRUE, should perform a soft delete. If FALSE, a hard delete. |
|
931 | + */ |
|
932 | + public function soft_delete($val = TRUE) |
|
933 | + { |
|
934 | + $this->soft_deletes = $val; |
|
860 | 935 | |
861 | - $result = $this->db->update($this->table_name, $sets); |
|
862 | - } // Hard Delete |
|
863 | - else { |
|
864 | - $result = $this->db->delete($this->table_name); |
|
865 | - } |
|
866 | - |
|
867 | - $this->trigger('after_delete', ['id' => $id, 'method' => 'delete', 'result' => $result] ); |
|
868 | - |
|
869 | - return $result; |
|
870 | - } |
|
871 | - |
|
872 | - //-------------------------------------------------------------------- |
|
873 | - |
|
874 | - public function delete_by() |
|
875 | - { |
|
876 | - $where = func_get_args(); |
|
877 | - $this->_set_where($where); |
|
878 | - |
|
879 | - $where = $this->trigger('before_delete', ['method' => 'delete_by', 'fields' => $where]); |
|
880 | - |
|
881 | - if ($this->soft_deletes) { |
|
882 | - $sets = $this->log_user && is_object($this->authenticate) |
|
883 | - ? array($this->soft_delete_key => 1, $this->deleted_by_field => $this->authenticate->id()) |
|
884 | - : array($this->soft_delete_key => 1); |
|
885 | - |
|
886 | - $result = $this->db->update($this->table_name, $sets); |
|
887 | - } else { |
|
888 | - $result = $this->db->delete($this->table_name); |
|
889 | - } |
|
890 | - |
|
891 | - $this->trigger('after_delete', ['method' => 'delete_by', 'fields' => $where, 'result' => $result] ); |
|
892 | - |
|
893 | - return $result; |
|
894 | - } |
|
895 | - |
|
896 | - //-------------------------------------------------------------------- |
|
897 | - |
|
898 | - public function delete_many($ids) |
|
899 | - { |
|
900 | - if (!is_array($ids) || count($ids) == 0) return NULL; |
|
901 | - |
|
902 | - $ids = $this->trigger('before_delete', ['ids' => $ids, 'method' => 'delete_many'] ); |
|
903 | - |
|
904 | - $this->db->where_in($this->primary_key, $ids); |
|
905 | - |
|
906 | - if ($this->soft_deletes) { |
|
907 | - $sets = $this->log_user && is_object($this->authenticate) |
|
908 | - ? array($this->soft_delete_key => 1, $this->deleted_by_field => $this->authenticate->id()) |
|
909 | - : array($this->soft_delete_key => 1); |
|
910 | - |
|
911 | - $result = $this->db->update($this->table_name, $sets); |
|
912 | - } else { |
|
913 | - $result = $this->db->delete($this->table_name); |
|
914 | - } |
|
915 | - |
|
916 | - $this->trigger('after_delete', ['ids' => $ids, 'method' => 'delete_many', 'result' => $result]); |
|
917 | - |
|
918 | - return $result; |
|
919 | - } |
|
920 | - |
|
921 | - //-------------------------------------------------------------------- |
|
922 | - |
|
923 | - //-------------------------------------------------------------------- |
|
924 | - // Scope Methods |
|
925 | - //-------------------------------------------------------------------- |
|
926 | - |
|
927 | - /** |
|
928 | - * Sets the value of the soft deletes flag. |
|
929 | - * |
|
930 | - * @param boolean $val If TRUE, should perform a soft delete. If FALSE, a hard delete. |
|
931 | - */ |
|
932 | - public function soft_delete($val = TRUE) |
|
933 | - { |
|
934 | - $this->soft_deletes = $val; |
|
935 | - |
|
936 | - return $this; |
|
937 | - } |
|
938 | - |
|
939 | - //-------------------------------------------------------------------- |
|
940 | - |
|
941 | - /** |
|
942 | - * Temporarily sets our return type to an array. |
|
943 | - */ |
|
944 | - public function as_array() |
|
945 | - { |
|
946 | - $this->temp_return_type = 'array'; |
|
947 | - |
|
948 | - return $this; |
|
949 | - } |
|
950 | - |
|
951 | - //-------------------------------------------------------------------- |
|
952 | - |
|
953 | - /** |
|
954 | - * Temporarily sets our return type to an object. |
|
955 | - * |
|
956 | - * If $class is provided, the rows will be returned as objects that |
|
957 | - * are instances of that class. $class MUST be an fully qualified |
|
958 | - * class name, meaning that it must include the namespace, if applicable. |
|
959 | - * |
|
960 | - * @param string $class |
|
961 | - * @return $this |
|
962 | - */ |
|
963 | - public function as_object($class=null) |
|
964 | - { |
|
965 | - $this->temp_return_type = ! empty($class) ? $class : 'object'; |
|
966 | - |
|
967 | - return $this; |
|
968 | - } |
|
969 | - |
|
970 | - //-------------------------------------------------------------------- |
|
971 | - |
|
972 | - /** |
|
973 | - * Also fetches deleted items for this request only. |
|
974 | - */ |
|
975 | - public function with_deleted() |
|
976 | - { |
|
977 | - $this->temp_with_deleted = TRUE; |
|
978 | - |
|
979 | - return $this; |
|
980 | - } |
|
981 | - |
|
982 | - //-------------------------------------------------------------------- |
|
983 | - |
|
984 | - /** |
|
985 | - * Returns whether the current setup will return |
|
986 | - * soft deleted rows. |
|
987 | - * |
|
988 | - * @return bool |
|
989 | - */ |
|
990 | - public function get_with_deleted() |
|
991 | - { |
|
992 | - return $this->temp_with_deleted; |
|
993 | - } |
|
994 | - |
|
995 | - //-------------------------------------------------------------------- |
|
996 | - |
|
997 | - |
|
998 | - /** |
|
999 | - * Sets the $skip_validation parameter. |
|
1000 | - * |
|
1001 | - * @param bool $skip |
|
1002 | - * @return $this |
|
1003 | - */ |
|
1004 | - public function skip_validation($skip = true) |
|
1005 | - { |
|
1006 | - $this->skip_validation = $skip; |
|
1007 | - |
|
1008 | - return $this; |
|
1009 | - } |
|
1010 | - |
|
1011 | - //-------------------------------------------------------------------- |
|
1012 | - |
|
1013 | - |
|
1014 | - //-------------------------------------------------------------------- |
|
1015 | - // Utility Methods |
|
1016 | - //-------------------------------------------------------------------- |
|
1017 | - |
|
1018 | - /** |
|
1019 | - * Counts number of rows modified by an arbitrary WHERE call. |
|
1020 | - * @return INT |
|
1021 | - */ |
|
1022 | - public function count_by() |
|
1023 | - { |
|
1024 | - $where = func_get_args(); |
|
1025 | - $this->_set_where($where); |
|
1026 | - |
|
1027 | - return $this->db->count_all_results($this->table_name); |
|
1028 | - } |
|
1029 | - |
|
1030 | - //-------------------------------------------------------------------- |
|
1031 | - |
|
1032 | - /** |
|
1033 | - * Counts total number of records, disregarding any previous conditions. |
|
1034 | - * |
|
1035 | - * @return int |
|
1036 | - */ |
|
1037 | - public function count_all() |
|
1038 | - { |
|
1039 | - return $this->db->count_all($this->table_name); |
|
1040 | - } |
|
1041 | - |
|
1042 | - //-------------------------------------------------------------------- |
|
1043 | - |
|
1044 | - /** |
|
1045 | - * Getter for the table name. |
|
1046 | - * |
|
1047 | - * @return string The name of the table used by this class. |
|
1048 | - */ |
|
1049 | - public function table() |
|
1050 | - { |
|
1051 | - return $this->table_name; |
|
1052 | - } |
|
1053 | - |
|
1054 | - //-------------------------------------------------------------------- |
|
1055 | - |
|
1056 | - /** |
|
1057 | - * Set the return_insert_id value. |
|
1058 | - * |
|
1059 | - * @param boolean $return If TRUE, insert will return the insert_id. |
|
1060 | - */ |
|
1061 | - public function return_insert_id($return = true) |
|
1062 | - { |
|
1063 | - $this->return_insert_id = (bool)$return; |
|
1064 | - |
|
1065 | - return $this; |
|
1066 | - } |
|
1067 | - |
|
1068 | - //-------------------------------------------------------------------- |
|
1069 | - |
|
1070 | - /** |
|
1071 | - * A convenience method to return only a single field of the specified row. |
|
1072 | - * |
|
1073 | - * @param mixed $id The primary_key value to match against. |
|
1074 | - * @param string $field The field to search for. |
|
1075 | - * |
|
1076 | - * @return bool|mixed The value of the field. |
|
1077 | - */ |
|
1078 | - public function get_field($id = NULL, $field = '') |
|
1079 | - { |
|
1080 | - $this->db->select($field); |
|
1081 | - $this->db->where($this->primary_key, $id); |
|
1082 | - $query = $this->db->get($this->table_name); |
|
1083 | - |
|
1084 | - if ($query && $query->num_rows() > 0) { |
|
1085 | - return $query->row()->$field; |
|
1086 | - } |
|
1087 | - |
|
1088 | - return FALSE; |
|
1089 | - |
|
1090 | - } |
|
1091 | - |
|
1092 | - //--------------------------------------------------------------- |
|
1093 | - |
|
1094 | - /** |
|
1095 | - * Checks whether a field/value pair exists within the table. |
|
1096 | - * |
|
1097 | - * @param string $field The field to search for. |
|
1098 | - * @param string $value The value to match $field against. |
|
1099 | - * |
|
1100 | - * @return bool TRUE/FALSE |
|
1101 | - */ |
|
1102 | - public function is_unique($field, $value) |
|
1103 | - { |
|
1104 | - $this->db->where($field, $value); |
|
1105 | - $query = $this->db->get($this->table_name); |
|
1106 | - |
|
1107 | - if ($query && $query->num_rows() == 0) { |
|
1108 | - return TRUE; |
|
1109 | - } |
|
1110 | - |
|
1111 | - return FALSE; |
|
1112 | - |
|
1113 | - } |
|
1114 | - |
|
1115 | - //--------------------------------------------------------------- |
|
1116 | - |
|
1117 | - /** |
|
1118 | - * Adds a field to the protected_attributes array. |
|
1119 | - * |
|
1120 | - * @param $field |
|
1121 | - * |
|
1122 | - * @return mixed |
|
1123 | - */ |
|
1124 | - public function protect($field) |
|
1125 | - { |
|
1126 | - $this->protected_attributes[] = $field; |
|
1127 | - |
|
1128 | - return $this; |
|
1129 | - } |
|
1130 | - |
|
1131 | - //-------------------------------------------------------------------- |
|
1132 | - |
|
1133 | - /** |
|
1134 | - * Get the field names for this model's table. |
|
1135 | - * |
|
1136 | - * Returns the model's database fields stored in $this->fields |
|
1137 | - * if set, else it tries to retrieve the field list from |
|
1138 | - * $this->db->list_fields($this->table_name); |
|
1139 | - * |
|
1140 | - * @return array Returns the database fields for this model |
|
1141 | - */ |
|
1142 | - public function get_fields() |
|
1143 | - { |
|
1144 | - if (empty($this->fields)) { |
|
1145 | - $this->fields = $this->db->list_fields($this->table_name); |
|
1146 | - } |
|
1147 | - |
|
1148 | - return $this->fields; |
|
1149 | - } |
|
1150 | - |
|
1151 | - //-------------------------------------------------------------------- |
|
1152 | - |
|
1153 | - /** |
|
1154 | - * Extracts the model's fields (except the key and those handled by |
|
1155 | - * Observers) from the $post_data and returns an array of name => value pairs |
|
1156 | - * |
|
1157 | - * @param Array $post_data The post data, usually $this->input->post() when called from the controller |
|
1158 | - * |
|
1159 | - * @return Array An array of name => value pairs containing the data for the model's fields |
|
1160 | - */ |
|
1161 | - public function prep_data($post_data) |
|
1162 | - { |
|
1163 | - $data = array(); |
|
1164 | - $skippedFields = array(); |
|
1165 | - |
|
1166 | - if (empty($post_data)) |
|
1167 | - { |
|
1168 | - return []; |
|
1169 | - } |
|
1170 | - |
|
1171 | - // Though the model doesn't support multiple keys well, $this->key |
|
1172 | - // could be an array or a string... |
|
1173 | - $skippedFields = array_merge($skippedFields, (array)$this->primary_key); |
|
1174 | - |
|
1175 | - // Remove any protected attributes |
|
1176 | - $skippedFields = array_merge($skippedFields, $this->protected_attributes); |
|
1177 | - |
|
1178 | - $fields = $this->get_fields(); |
|
1179 | - |
|
1180 | - // If the field is the primary key, one of the created/modified/deleted |
|
1181 | - // fields, or has not been set in the $post_data, skip it |
|
1182 | - foreach ($post_data as $field => $value) { |
|
1183 | - if (in_array($field, $skippedFields) || |
|
1184 | - ! in_array($field, $fields)) |
|
1185 | - { |
|
1186 | - continue; |
|
1187 | - } |
|
1188 | - |
|
1189 | - $data[$field] = $value; |
|
1190 | - } |
|
1191 | - |
|
1192 | - return $data; |
|
1193 | - } |
|
1194 | - |
|
1195 | - //-------------------------------------------------------------------- |
|
1196 | - |
|
1197 | - /** |
|
1198 | - * Returns the last query string, if available. Simply a wrapper for |
|
1199 | - * CodeIgniter's database method of the same name. |
|
1200 | - * |
|
1201 | - * @return string |
|
1202 | - */ |
|
1203 | - public function last_query () |
|
1204 | - { |
|
1205 | - return $this->db->last_query(); |
|
1206 | - } |
|
1207 | - |
|
1208 | - //-------------------------------------------------------------------- |
|
1209 | - |
|
1210 | - /** |
|
1211 | - * Returns the elapsed time for the last query that was executed, if |
|
1212 | - * available, or NULL if not available, like if debug mode is off. |
|
1213 | - * |
|
1214 | - * @return mixed |
|
1215 | - */ |
|
1216 | - public function last_query_time () |
|
1217 | - { |
|
1218 | - $times = $this->db->query_times; |
|
1219 | - |
|
1220 | - if (! is_array($this->db->query_times) || ! count($this->db->query_times)) |
|
1221 | - { |
|
1222 | - return null; |
|
1223 | - } |
|
1224 | - |
|
1225 | - return end($times); |
|
1226 | - } |
|
1227 | - |
|
1228 | - //-------------------------------------------------------------------- |
|
1229 | - |
|
1230 | - //-------------------------------------------------------------------- |
|
1231 | - // Observers |
|
1232 | - //-------------------------------------------------------------------- |
|
1233 | - |
|
1234 | - /** |
|
1235 | - * Sets the created on date for the object based on the |
|
1236 | - * current date/time and date_format. Will not overwrite existing. |
|
1237 | - * |
|
1238 | - * @param array $row The array of data to be inserted |
|
1239 | - * |
|
1240 | - * @return array |
|
1241 | - */ |
|
1242 | - public function created_on($row) |
|
1243 | - { |
|
1244 | - if (empty($row['fields'])) |
|
1245 | - { |
|
1246 | - return null; |
|
1247 | - } |
|
1248 | - |
|
1249 | - $row = $row['fields']; |
|
1250 | - |
|
1251 | - // Created_on |
|
1252 | - if (! array_key_exists($this->created_field, $row)) |
|
1253 | - { |
|
1254 | - $row[$this->created_field] = $this->set_date(); |
|
1255 | - } |
|
1256 | - |
|
1257 | - // Created by |
|
1258 | - if ($this->log_user && ! array_key_exists($this->created_by_field, $row) && is_object($this->authenticate)) |
|
1259 | - { |
|
1260 | - // If you're here because of an error with $this->authenticate |
|
1261 | - // not being available, it's likely due to you not using |
|
1262 | - // the AuthTrait and/or setting log_user after model is instantiated. |
|
1263 | - $row[$this->created_by_field] = (int)$this->authenticate->id(); |
|
1264 | - } |
|
1265 | - |
|
1266 | - return $row; |
|
1267 | - } // end created_on() |
|
1268 | - |
|
1269 | - //-------------------------------------------------------------------- |
|
1270 | - |
|
1271 | - /** |
|
1272 | - * Sets the modified_on date for the object based on the |
|
1273 | - * current date/time and date_format. Will not overwrite existing. |
|
1274 | - * |
|
1275 | - * @param array $row The array of data to be inserted |
|
1276 | - * |
|
1277 | - * @return array |
|
1278 | - */ |
|
1279 | - public function modified_on($row) |
|
1280 | - { |
|
1281 | - if (empty($row['fields'])) |
|
1282 | - { |
|
1283 | - return null; |
|
1284 | - } |
|
1285 | - |
|
1286 | - $row = $row['fields']; |
|
1287 | - |
|
1288 | - if (is_array($row) && ! array_key_exists($this->modified_field, $row)) |
|
1289 | - { |
|
1290 | - $row[$this->modified_field] = $this->set_date(); |
|
1291 | - } |
|
1292 | - |
|
1293 | - // Modified by |
|
1294 | - if ($this->log_user && ! array_key_exists($this->modified_by_field, $row) && is_object($this->authenticate)) |
|
1295 | - { |
|
1296 | - // If you're here because of an error with $this->authenticate |
|
1297 | - // not being available, it's likely due to you not using |
|
1298 | - // the AuthTrait and/or setting log_user after model is instantiated. |
|
1299 | - $row[$this->modified_by_field] = $this->authenticate->id(); |
|
1300 | - } |
|
1301 | - |
|
1302 | - return $row; |
|
1303 | - } |
|
1304 | - |
|
1305 | - //-------------------------------------------------------------------- |
|
1306 | - |
|
1307 | - //-------------------------------------------------------------------- |
|
1308 | - // Internal Methods |
|
1309 | - //-------------------------------------------------------------------- |
|
1310 | - |
|
1311 | - /** |
|
1312 | - * Set WHERE parameters |
|
1313 | - */ |
|
1314 | - protected function _set_where($params) |
|
1315 | - { |
|
1316 | - if (count($params) == 1) { |
|
1317 | - $this->db->where($params[0]); |
|
1318 | - } else { |
|
1319 | - $this->db->where($params[0], $params[1]); |
|
1320 | - } |
|
1321 | - } |
|
1322 | - |
|
1323 | - //-------------------------------------------------------------------- |
|
1324 | - |
|
1325 | - /** |
|
1326 | - * Triggers a model-specific event and call each of it's observers. |
|
1327 | - * |
|
1328 | - * @param string $event The name of the event to trigger |
|
1329 | - * @param mixed $data The data to be passed to the callback functions. |
|
1330 | - * |
|
1331 | - * @return mixed |
|
1332 | - */ |
|
1333 | - public function trigger($event, $data = false) |
|
1334 | - { |
|
1335 | - if (! isset($this->$event) || ! is_array($this->$event)) |
|
1336 | - { |
|
1337 | - if (isset($data['fields'])) |
|
1338 | - { |
|
1339 | - return $data['fields']; |
|
1340 | - } |
|
1341 | - |
|
1342 | - return $data; |
|
1343 | - } |
|
1344 | - |
|
1345 | - foreach ($this->$event as $method) |
|
1346 | - { |
|
1347 | - if (strpos($method, '(')) |
|
1348 | - { |
|
1349 | - preg_match('/([a-zA-Z0-9\_\-]+)(\(([a-zA-Z0-9\_\-\., ]+)\))?/', $method, $matches); |
|
1350 | - $this->callback_parameters = explode(',', $matches[3]); |
|
1351 | - } |
|
1352 | - |
|
1353 | - $data = call_user_func_array(array($this, $method), array($data)); |
|
1354 | - } |
|
1355 | - |
|
1356 | - // In case no method called or method returned |
|
1357 | - // the entire data array, we typically just need the $fields |
|
1358 | - if (isset($data['fields'])) |
|
1359 | - { |
|
1360 | - return $data['fields']; |
|
1361 | - } |
|
1362 | - |
|
1363 | - // A few methods might need to return 'ids' |
|
1364 | - if (isset($data['ids'])) |
|
1365 | - { |
|
1366 | - return $data['ids']; |
|
1367 | - } |
|
1368 | - |
|
1369 | - return $data; |
|
1370 | - } |
|
1371 | - |
|
1372 | - //-------------------------------------------------------------------- |
|
1373 | - |
|
1374 | - /** |
|
1375 | - * Validates the data passed into it based upon the form_validation rules |
|
1376 | - * setup in the $this->validate property. |
|
1377 | - * |
|
1378 | - * If $type == 'insert', any additional rules in the class var $insert_validate_rules |
|
1379 | - * for that field will be added to the rules. |
|
1380 | - * |
|
1381 | - * @param array $data An array of validation rules |
|
1382 | - * @param string $type Either 'update' or 'insert'. |
|
1383 | - * @return array/bool The original data or FALSE |
|
1384 | - */ |
|
1385 | - public function validate($data, $type = 'update', $skip_validation = null) |
|
1386 | - { |
|
1387 | - $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
1388 | - |
|
1389 | - if ($skip_validation) { |
|
1390 | - return $data; |
|
1391 | - } |
|
1392 | - |
|
1393 | - // We need the database to be loaded up at this point in case |
|
1394 | - // we want to use callbacks that hit the database. |
|
1395 | - if (empty($this->db)) |
|
1396 | - { |
|
1397 | - $this->load->database(); |
|
1398 | - } |
|
1399 | - |
|
1400 | - if (!empty($this->validation_rules)) { |
|
1401 | - $this->form_validation->set_data($data); |
|
1402 | - |
|
1403 | - if (is_array($this->validation_rules)) { |
|
1404 | - // Any insert additions? |
|
1405 | - if ($type == 'insert' |
|
1406 | - && !empty($this->insert_validate_rules) |
|
1407 | - && is_array($this->insert_validate_rules) |
|
1408 | - ) { |
|
1409 | - foreach ($this->validation_rules as &$row) { |
|
1410 | - if (isset($this->insert_validate_rules[$row['field']])) { |
|
1411 | - $row ['rules'] .= '|' . $this->insert_validate_rules[$row['field']]; |
|
1412 | - } |
|
1413 | - } |
|
1414 | - } |
|
1415 | - |
|
1416 | - $this->form_validation->set_rules($this->validation_rules); |
|
1417 | - |
|
1418 | - if ($this->form_validation->run('', $this) === TRUE) { |
|
1419 | - return $data; |
|
1420 | - } else { |
|
1421 | - return FALSE; |
|
1422 | - } |
|
1423 | - } else { |
|
1424 | - if ($this->form_validation->run($this->validate, $this) === TRUE) { |
|
1425 | - return $data; |
|
1426 | - } else { |
|
1427 | - return FALSE; |
|
1428 | - } |
|
1429 | - } |
|
1430 | - } else { |
|
1431 | - return $data; |
|
1432 | - } |
|
1433 | - } |
|
1434 | - |
|
1435 | - //-------------------------------------------------------------------- |
|
1436 | - |
|
1437 | - /** |
|
1438 | - * Protect attributes by removing them from $row array. Useful for |
|
1439 | - * removing id, or submit buttons names if you simply throw your $_POST |
|
1440 | - * array at your model. :) |
|
1441 | - * |
|
1442 | - * @param object /array $row The value pair item to remove. |
|
1443 | - */ |
|
1444 | - public function protect_attributes($row) |
|
1445 | - { |
|
1446 | - foreach ($this->protected_attributes as $attr) { |
|
1447 | - if (is_object($row)) { |
|
1448 | - unset($row->$attr); |
|
1449 | - } else { |
|
1450 | - unset($row[$attr]); |
|
1451 | - } |
|
1452 | - } |
|
1453 | - |
|
1454 | - return $row; |
|
1455 | - } |
|
1456 | - |
|
1457 | - //-------------------------------------------------------------------- |
|
1458 | - |
|
1459 | - /** |
|
1460 | - * A utility function to allow child models to use the type of |
|
1461 | - * date/time format that they prefer. This is primarily used for |
|
1462 | - * setting created_on and modified_on values, but can be used by |
|
1463 | - * inheriting classes. |
|
1464 | - * |
|
1465 | - * The available time formats are: |
|
1466 | - * * 'int' - Stores the date as an integer timestamp. |
|
1467 | - * * 'datetime' - Stores the date and time in the SQL datetime format. |
|
1468 | - * * 'date' - Stores teh date (only) in the SQL date format. |
|
1469 | - * |
|
1470 | - * @param mixed $user_date An optional PHP timestamp to be converted. |
|
1471 | - * |
|
1472 | - * @access protected |
|
1473 | - * |
|
1474 | - * @return int|null|string The current/user time converted to the proper format. |
|
1475 | - */ |
|
1476 | - protected function set_date($user_date = NULL) |
|
1477 | - { |
|
1478 | - $curr_date = !empty($user_date) ? $user_date : time(); |
|
1479 | - |
|
1480 | - switch ($this->date_format) { |
|
1481 | - case 'int': |
|
1482 | - return $curr_date; |
|
1483 | - break; |
|
1484 | - case 'datetime': |
|
1485 | - return date('Y-m-d H:i:s', $curr_date); |
|
1486 | - break; |
|
1487 | - case 'date': |
|
1488 | - return date('Y-m-d', $curr_date); |
|
1489 | - break; |
|
1490 | - } |
|
1491 | - |
|
1492 | - }//end set_date() |
|
1493 | - |
|
1494 | - //-------------------------------------------------------------------- |
|
1495 | - |
|
1496 | - /** |
|
1497 | - * Returns an array containing the 'code' and 'message' of the |
|
1498 | - * database's error, as provided by CI's database drivers. |
|
1499 | - * |
|
1500 | - * @return mixed |
|
1501 | - */ |
|
1502 | - public function error($db_array_only=false) |
|
1503 | - { |
|
1504 | - // Send any validation errors if we have any. |
|
1505 | - if (function_exists('validation_errors') && validation_errors() && ! $db_array_only) |
|
1506 | - { |
|
1507 | - return validation_errors(); |
|
1508 | - } |
|
1509 | - |
|
1510 | - // No validation errors? Return the db error. |
|
1511 | - $error = $this->db->error(); |
|
1512 | - |
|
1513 | - if ($db_array_only) |
|
1514 | - { |
|
1515 | - return $error; |
|
1516 | - } |
|
1517 | - |
|
1518 | - if (! empty($error['code'])) |
|
1519 | - { |
|
1520 | - return "Database Error {$error['code']}: {$error['message']}."; |
|
1521 | - } |
|
1522 | - |
|
1523 | - // No errors found. |
|
1524 | - return ''; |
|
1525 | - } |
|
1526 | - |
|
1527 | - //-------------------------------------------------------------------- |
|
1528 | - |
|
1529 | - //-------------------------------------------------------------------- |
|
1530 | - // Magic Methods |
|
1531 | - //-------------------------------------------------------------------- |
|
1532 | - |
|
1533 | - /** |
|
1534 | - * __get magic |
|
1535 | - * |
|
1536 | - * Allows models to access CI's loaded classes using the same |
|
1537 | - * syntax as controllers. |
|
1538 | - * |
|
1539 | - * This is the same as what CI's model uses, but we keep it |
|
1540 | - * here since that's the ONLY thing that CI's model does. |
|
1541 | - * |
|
1542 | - * @param string $key |
|
1543 | - */ |
|
1544 | - public function __get($key) |
|
1545 | - { |
|
1546 | - // Give them first crack at any protected class vars |
|
1547 | - if (isset($this->$key)) |
|
1548 | - { |
|
1549 | - return $this->$key; |
|
1550 | - } |
|
1551 | - |
|
1552 | - // Debugging note: |
|
1553 | - // If you're here because you're getting an error message |
|
1554 | - // saying 'Undefined Property: system/core/Model.php', it's |
|
1555 | - // most likely a typo in your model code. |
|
1556 | - return get_instance()->$key; |
|
1557 | - } |
|
1558 | - |
|
1559 | - //-------------------------------------------------------------------- |
|
1560 | - |
|
1561 | - /** |
|
1562 | - * Provide direct access to any of CodeIgniter's DB methods but |
|
1563 | - * make it look like it's part of the class, purely for convenience. |
|
1564 | - * |
|
1565 | - * @param $name |
|
1566 | - * @param $params |
|
1567 | - */ |
|
1568 | - public function __call($name, $params=null) |
|
1569 | - { |
|
1570 | - if (method_exists($this->db, $name)) |
|
1571 | - { |
|
1572 | - call_user_func_array([$this->db, $name], $params); |
|
1573 | - return $this; |
|
1574 | - } |
|
1575 | - } |
|
1576 | - |
|
1577 | - //-------------------------------------------------------------------- |
|
936 | + return $this; |
|
937 | + } |
|
938 | + |
|
939 | + //-------------------------------------------------------------------- |
|
940 | + |
|
941 | + /** |
|
942 | + * Temporarily sets our return type to an array. |
|
943 | + */ |
|
944 | + public function as_array() |
|
945 | + { |
|
946 | + $this->temp_return_type = 'array'; |
|
947 | + |
|
948 | + return $this; |
|
949 | + } |
|
950 | + |
|
951 | + //-------------------------------------------------------------------- |
|
952 | + |
|
953 | + /** |
|
954 | + * Temporarily sets our return type to an object. |
|
955 | + * |
|
956 | + * If $class is provided, the rows will be returned as objects that |
|
957 | + * are instances of that class. $class MUST be an fully qualified |
|
958 | + * class name, meaning that it must include the namespace, if applicable. |
|
959 | + * |
|
960 | + * @param string $class |
|
961 | + * @return $this |
|
962 | + */ |
|
963 | + public function as_object($class=null) |
|
964 | + { |
|
965 | + $this->temp_return_type = ! empty($class) ? $class : 'object'; |
|
966 | + |
|
967 | + return $this; |
|
968 | + } |
|
969 | + |
|
970 | + //-------------------------------------------------------------------- |
|
971 | + |
|
972 | + /** |
|
973 | + * Also fetches deleted items for this request only. |
|
974 | + */ |
|
975 | + public function with_deleted() |
|
976 | + { |
|
977 | + $this->temp_with_deleted = TRUE; |
|
978 | + |
|
979 | + return $this; |
|
980 | + } |
|
981 | + |
|
982 | + //-------------------------------------------------------------------- |
|
983 | + |
|
984 | + /** |
|
985 | + * Returns whether the current setup will return |
|
986 | + * soft deleted rows. |
|
987 | + * |
|
988 | + * @return bool |
|
989 | + */ |
|
990 | + public function get_with_deleted() |
|
991 | + { |
|
992 | + return $this->temp_with_deleted; |
|
993 | + } |
|
994 | + |
|
995 | + //-------------------------------------------------------------------- |
|
996 | + |
|
997 | + |
|
998 | + /** |
|
999 | + * Sets the $skip_validation parameter. |
|
1000 | + * |
|
1001 | + * @param bool $skip |
|
1002 | + * @return $this |
|
1003 | + */ |
|
1004 | + public function skip_validation($skip = true) |
|
1005 | + { |
|
1006 | + $this->skip_validation = $skip; |
|
1007 | + |
|
1008 | + return $this; |
|
1009 | + } |
|
1010 | + |
|
1011 | + //-------------------------------------------------------------------- |
|
1012 | + |
|
1013 | + |
|
1014 | + //-------------------------------------------------------------------- |
|
1015 | + // Utility Methods |
|
1016 | + //-------------------------------------------------------------------- |
|
1017 | + |
|
1018 | + /** |
|
1019 | + * Counts number of rows modified by an arbitrary WHERE call. |
|
1020 | + * @return INT |
|
1021 | + */ |
|
1022 | + public function count_by() |
|
1023 | + { |
|
1024 | + $where = func_get_args(); |
|
1025 | + $this->_set_where($where); |
|
1026 | + |
|
1027 | + return $this->db->count_all_results($this->table_name); |
|
1028 | + } |
|
1029 | + |
|
1030 | + //-------------------------------------------------------------------- |
|
1031 | + |
|
1032 | + /** |
|
1033 | + * Counts total number of records, disregarding any previous conditions. |
|
1034 | + * |
|
1035 | + * @return int |
|
1036 | + */ |
|
1037 | + public function count_all() |
|
1038 | + { |
|
1039 | + return $this->db->count_all($this->table_name); |
|
1040 | + } |
|
1041 | + |
|
1042 | + //-------------------------------------------------------------------- |
|
1043 | + |
|
1044 | + /** |
|
1045 | + * Getter for the table name. |
|
1046 | + * |
|
1047 | + * @return string The name of the table used by this class. |
|
1048 | + */ |
|
1049 | + public function table() |
|
1050 | + { |
|
1051 | + return $this->table_name; |
|
1052 | + } |
|
1053 | + |
|
1054 | + //-------------------------------------------------------------------- |
|
1055 | + |
|
1056 | + /** |
|
1057 | + * Set the return_insert_id value. |
|
1058 | + * |
|
1059 | + * @param boolean $return If TRUE, insert will return the insert_id. |
|
1060 | + */ |
|
1061 | + public function return_insert_id($return = true) |
|
1062 | + { |
|
1063 | + $this->return_insert_id = (bool)$return; |
|
1064 | + |
|
1065 | + return $this; |
|
1066 | + } |
|
1067 | + |
|
1068 | + //-------------------------------------------------------------------- |
|
1069 | + |
|
1070 | + /** |
|
1071 | + * A convenience method to return only a single field of the specified row. |
|
1072 | + * |
|
1073 | + * @param mixed $id The primary_key value to match against. |
|
1074 | + * @param string $field The field to search for. |
|
1075 | + * |
|
1076 | + * @return bool|mixed The value of the field. |
|
1077 | + */ |
|
1078 | + public function get_field($id = NULL, $field = '') |
|
1079 | + { |
|
1080 | + $this->db->select($field); |
|
1081 | + $this->db->where($this->primary_key, $id); |
|
1082 | + $query = $this->db->get($this->table_name); |
|
1083 | + |
|
1084 | + if ($query && $query->num_rows() > 0) { |
|
1085 | + return $query->row()->$field; |
|
1086 | + } |
|
1087 | + |
|
1088 | + return FALSE; |
|
1089 | + |
|
1090 | + } |
|
1091 | + |
|
1092 | + //--------------------------------------------------------------- |
|
1093 | + |
|
1094 | + /** |
|
1095 | + * Checks whether a field/value pair exists within the table. |
|
1096 | + * |
|
1097 | + * @param string $field The field to search for. |
|
1098 | + * @param string $value The value to match $field against. |
|
1099 | + * |
|
1100 | + * @return bool TRUE/FALSE |
|
1101 | + */ |
|
1102 | + public function is_unique($field, $value) |
|
1103 | + { |
|
1104 | + $this->db->where($field, $value); |
|
1105 | + $query = $this->db->get($this->table_name); |
|
1106 | + |
|
1107 | + if ($query && $query->num_rows() == 0) { |
|
1108 | + return TRUE; |
|
1109 | + } |
|
1110 | + |
|
1111 | + return FALSE; |
|
1112 | + |
|
1113 | + } |
|
1114 | + |
|
1115 | + //--------------------------------------------------------------- |
|
1116 | + |
|
1117 | + /** |
|
1118 | + * Adds a field to the protected_attributes array. |
|
1119 | + * |
|
1120 | + * @param $field |
|
1121 | + * |
|
1122 | + * @return mixed |
|
1123 | + */ |
|
1124 | + public function protect($field) |
|
1125 | + { |
|
1126 | + $this->protected_attributes[] = $field; |
|
1127 | + |
|
1128 | + return $this; |
|
1129 | + } |
|
1130 | + |
|
1131 | + //-------------------------------------------------------------------- |
|
1132 | + |
|
1133 | + /** |
|
1134 | + * Get the field names for this model's table. |
|
1135 | + * |
|
1136 | + * Returns the model's database fields stored in $this->fields |
|
1137 | + * if set, else it tries to retrieve the field list from |
|
1138 | + * $this->db->list_fields($this->table_name); |
|
1139 | + * |
|
1140 | + * @return array Returns the database fields for this model |
|
1141 | + */ |
|
1142 | + public function get_fields() |
|
1143 | + { |
|
1144 | + if (empty($this->fields)) { |
|
1145 | + $this->fields = $this->db->list_fields($this->table_name); |
|
1146 | + } |
|
1147 | + |
|
1148 | + return $this->fields; |
|
1149 | + } |
|
1150 | + |
|
1151 | + //-------------------------------------------------------------------- |
|
1152 | + |
|
1153 | + /** |
|
1154 | + * Extracts the model's fields (except the key and those handled by |
|
1155 | + * Observers) from the $post_data and returns an array of name => value pairs |
|
1156 | + * |
|
1157 | + * @param Array $post_data The post data, usually $this->input->post() when called from the controller |
|
1158 | + * |
|
1159 | + * @return Array An array of name => value pairs containing the data for the model's fields |
|
1160 | + */ |
|
1161 | + public function prep_data($post_data) |
|
1162 | + { |
|
1163 | + $data = array(); |
|
1164 | + $skippedFields = array(); |
|
1165 | + |
|
1166 | + if (empty($post_data)) |
|
1167 | + { |
|
1168 | + return []; |
|
1169 | + } |
|
1170 | + |
|
1171 | + // Though the model doesn't support multiple keys well, $this->key |
|
1172 | + // could be an array or a string... |
|
1173 | + $skippedFields = array_merge($skippedFields, (array)$this->primary_key); |
|
1174 | + |
|
1175 | + // Remove any protected attributes |
|
1176 | + $skippedFields = array_merge($skippedFields, $this->protected_attributes); |
|
1177 | + |
|
1178 | + $fields = $this->get_fields(); |
|
1179 | + |
|
1180 | + // If the field is the primary key, one of the created/modified/deleted |
|
1181 | + // fields, or has not been set in the $post_data, skip it |
|
1182 | + foreach ($post_data as $field => $value) { |
|
1183 | + if (in_array($field, $skippedFields) || |
|
1184 | + ! in_array($field, $fields)) |
|
1185 | + { |
|
1186 | + continue; |
|
1187 | + } |
|
1188 | + |
|
1189 | + $data[$field] = $value; |
|
1190 | + } |
|
1191 | + |
|
1192 | + return $data; |
|
1193 | + } |
|
1194 | + |
|
1195 | + //-------------------------------------------------------------------- |
|
1196 | + |
|
1197 | + /** |
|
1198 | + * Returns the last query string, if available. Simply a wrapper for |
|
1199 | + * CodeIgniter's database method of the same name. |
|
1200 | + * |
|
1201 | + * @return string |
|
1202 | + */ |
|
1203 | + public function last_query () |
|
1204 | + { |
|
1205 | + return $this->db->last_query(); |
|
1206 | + } |
|
1207 | + |
|
1208 | + //-------------------------------------------------------------------- |
|
1209 | + |
|
1210 | + /** |
|
1211 | + * Returns the elapsed time for the last query that was executed, if |
|
1212 | + * available, or NULL if not available, like if debug mode is off. |
|
1213 | + * |
|
1214 | + * @return mixed |
|
1215 | + */ |
|
1216 | + public function last_query_time () |
|
1217 | + { |
|
1218 | + $times = $this->db->query_times; |
|
1219 | + |
|
1220 | + if (! is_array($this->db->query_times) || ! count($this->db->query_times)) |
|
1221 | + { |
|
1222 | + return null; |
|
1223 | + } |
|
1224 | + |
|
1225 | + return end($times); |
|
1226 | + } |
|
1227 | + |
|
1228 | + //-------------------------------------------------------------------- |
|
1229 | + |
|
1230 | + //-------------------------------------------------------------------- |
|
1231 | + // Observers |
|
1232 | + //-------------------------------------------------------------------- |
|
1233 | + |
|
1234 | + /** |
|
1235 | + * Sets the created on date for the object based on the |
|
1236 | + * current date/time and date_format. Will not overwrite existing. |
|
1237 | + * |
|
1238 | + * @param array $row The array of data to be inserted |
|
1239 | + * |
|
1240 | + * @return array |
|
1241 | + */ |
|
1242 | + public function created_on($row) |
|
1243 | + { |
|
1244 | + if (empty($row['fields'])) |
|
1245 | + { |
|
1246 | + return null; |
|
1247 | + } |
|
1248 | + |
|
1249 | + $row = $row['fields']; |
|
1250 | + |
|
1251 | + // Created_on |
|
1252 | + if (! array_key_exists($this->created_field, $row)) |
|
1253 | + { |
|
1254 | + $row[$this->created_field] = $this->set_date(); |
|
1255 | + } |
|
1256 | + |
|
1257 | + // Created by |
|
1258 | + if ($this->log_user && ! array_key_exists($this->created_by_field, $row) && is_object($this->authenticate)) |
|
1259 | + { |
|
1260 | + // If you're here because of an error with $this->authenticate |
|
1261 | + // not being available, it's likely due to you not using |
|
1262 | + // the AuthTrait and/or setting log_user after model is instantiated. |
|
1263 | + $row[$this->created_by_field] = (int)$this->authenticate->id(); |
|
1264 | + } |
|
1265 | + |
|
1266 | + return $row; |
|
1267 | + } // end created_on() |
|
1268 | + |
|
1269 | + //-------------------------------------------------------------------- |
|
1270 | + |
|
1271 | + /** |
|
1272 | + * Sets the modified_on date for the object based on the |
|
1273 | + * current date/time and date_format. Will not overwrite existing. |
|
1274 | + * |
|
1275 | + * @param array $row The array of data to be inserted |
|
1276 | + * |
|
1277 | + * @return array |
|
1278 | + */ |
|
1279 | + public function modified_on($row) |
|
1280 | + { |
|
1281 | + if (empty($row['fields'])) |
|
1282 | + { |
|
1283 | + return null; |
|
1284 | + } |
|
1285 | + |
|
1286 | + $row = $row['fields']; |
|
1287 | + |
|
1288 | + if (is_array($row) && ! array_key_exists($this->modified_field, $row)) |
|
1289 | + { |
|
1290 | + $row[$this->modified_field] = $this->set_date(); |
|
1291 | + } |
|
1292 | + |
|
1293 | + // Modified by |
|
1294 | + if ($this->log_user && ! array_key_exists($this->modified_by_field, $row) && is_object($this->authenticate)) |
|
1295 | + { |
|
1296 | + // If you're here because of an error with $this->authenticate |
|
1297 | + // not being available, it's likely due to you not using |
|
1298 | + // the AuthTrait and/or setting log_user after model is instantiated. |
|
1299 | + $row[$this->modified_by_field] = $this->authenticate->id(); |
|
1300 | + } |
|
1301 | + |
|
1302 | + return $row; |
|
1303 | + } |
|
1304 | + |
|
1305 | + //-------------------------------------------------------------------- |
|
1306 | + |
|
1307 | + //-------------------------------------------------------------------- |
|
1308 | + // Internal Methods |
|
1309 | + //-------------------------------------------------------------------- |
|
1310 | + |
|
1311 | + /** |
|
1312 | + * Set WHERE parameters |
|
1313 | + */ |
|
1314 | + protected function _set_where($params) |
|
1315 | + { |
|
1316 | + if (count($params) == 1) { |
|
1317 | + $this->db->where($params[0]); |
|
1318 | + } else { |
|
1319 | + $this->db->where($params[0], $params[1]); |
|
1320 | + } |
|
1321 | + } |
|
1322 | + |
|
1323 | + //-------------------------------------------------------------------- |
|
1324 | + |
|
1325 | + /** |
|
1326 | + * Triggers a model-specific event and call each of it's observers. |
|
1327 | + * |
|
1328 | + * @param string $event The name of the event to trigger |
|
1329 | + * @param mixed $data The data to be passed to the callback functions. |
|
1330 | + * |
|
1331 | + * @return mixed |
|
1332 | + */ |
|
1333 | + public function trigger($event, $data = false) |
|
1334 | + { |
|
1335 | + if (! isset($this->$event) || ! is_array($this->$event)) |
|
1336 | + { |
|
1337 | + if (isset($data['fields'])) |
|
1338 | + { |
|
1339 | + return $data['fields']; |
|
1340 | + } |
|
1341 | + |
|
1342 | + return $data; |
|
1343 | + } |
|
1344 | + |
|
1345 | + foreach ($this->$event as $method) |
|
1346 | + { |
|
1347 | + if (strpos($method, '(')) |
|
1348 | + { |
|
1349 | + preg_match('/([a-zA-Z0-9\_\-]+)(\(([a-zA-Z0-9\_\-\., ]+)\))?/', $method, $matches); |
|
1350 | + $this->callback_parameters = explode(',', $matches[3]); |
|
1351 | + } |
|
1352 | + |
|
1353 | + $data = call_user_func_array(array($this, $method), array($data)); |
|
1354 | + } |
|
1355 | + |
|
1356 | + // In case no method called or method returned |
|
1357 | + // the entire data array, we typically just need the $fields |
|
1358 | + if (isset($data['fields'])) |
|
1359 | + { |
|
1360 | + return $data['fields']; |
|
1361 | + } |
|
1362 | + |
|
1363 | + // A few methods might need to return 'ids' |
|
1364 | + if (isset($data['ids'])) |
|
1365 | + { |
|
1366 | + return $data['ids']; |
|
1367 | + } |
|
1368 | + |
|
1369 | + return $data; |
|
1370 | + } |
|
1371 | + |
|
1372 | + //-------------------------------------------------------------------- |
|
1373 | + |
|
1374 | + /** |
|
1375 | + * Validates the data passed into it based upon the form_validation rules |
|
1376 | + * setup in the $this->validate property. |
|
1377 | + * |
|
1378 | + * If $type == 'insert', any additional rules in the class var $insert_validate_rules |
|
1379 | + * for that field will be added to the rules. |
|
1380 | + * |
|
1381 | + * @param array $data An array of validation rules |
|
1382 | + * @param string $type Either 'update' or 'insert'. |
|
1383 | + * @return array/bool The original data or FALSE |
|
1384 | + */ |
|
1385 | + public function validate($data, $type = 'update', $skip_validation = null) |
|
1386 | + { |
|
1387 | + $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
1388 | + |
|
1389 | + if ($skip_validation) { |
|
1390 | + return $data; |
|
1391 | + } |
|
1392 | + |
|
1393 | + // We need the database to be loaded up at this point in case |
|
1394 | + // we want to use callbacks that hit the database. |
|
1395 | + if (empty($this->db)) |
|
1396 | + { |
|
1397 | + $this->load->database(); |
|
1398 | + } |
|
1399 | + |
|
1400 | + if (!empty($this->validation_rules)) { |
|
1401 | + $this->form_validation->set_data($data); |
|
1402 | + |
|
1403 | + if (is_array($this->validation_rules)) { |
|
1404 | + // Any insert additions? |
|
1405 | + if ($type == 'insert' |
|
1406 | + && !empty($this->insert_validate_rules) |
|
1407 | + && is_array($this->insert_validate_rules) |
|
1408 | + ) { |
|
1409 | + foreach ($this->validation_rules as &$row) { |
|
1410 | + if (isset($this->insert_validate_rules[$row['field']])) { |
|
1411 | + $row ['rules'] .= '|' . $this->insert_validate_rules[$row['field']]; |
|
1412 | + } |
|
1413 | + } |
|
1414 | + } |
|
1415 | + |
|
1416 | + $this->form_validation->set_rules($this->validation_rules); |
|
1417 | + |
|
1418 | + if ($this->form_validation->run('', $this) === TRUE) { |
|
1419 | + return $data; |
|
1420 | + } else { |
|
1421 | + return FALSE; |
|
1422 | + } |
|
1423 | + } else { |
|
1424 | + if ($this->form_validation->run($this->validate, $this) === TRUE) { |
|
1425 | + return $data; |
|
1426 | + } else { |
|
1427 | + return FALSE; |
|
1428 | + } |
|
1429 | + } |
|
1430 | + } else { |
|
1431 | + return $data; |
|
1432 | + } |
|
1433 | + } |
|
1434 | + |
|
1435 | + //-------------------------------------------------------------------- |
|
1436 | + |
|
1437 | + /** |
|
1438 | + * Protect attributes by removing them from $row array. Useful for |
|
1439 | + * removing id, or submit buttons names if you simply throw your $_POST |
|
1440 | + * array at your model. :) |
|
1441 | + * |
|
1442 | + * @param object /array $row The value pair item to remove. |
|
1443 | + */ |
|
1444 | + public function protect_attributes($row) |
|
1445 | + { |
|
1446 | + foreach ($this->protected_attributes as $attr) { |
|
1447 | + if (is_object($row)) { |
|
1448 | + unset($row->$attr); |
|
1449 | + } else { |
|
1450 | + unset($row[$attr]); |
|
1451 | + } |
|
1452 | + } |
|
1453 | + |
|
1454 | + return $row; |
|
1455 | + } |
|
1456 | + |
|
1457 | + //-------------------------------------------------------------------- |
|
1458 | + |
|
1459 | + /** |
|
1460 | + * A utility function to allow child models to use the type of |
|
1461 | + * date/time format that they prefer. This is primarily used for |
|
1462 | + * setting created_on and modified_on values, but can be used by |
|
1463 | + * inheriting classes. |
|
1464 | + * |
|
1465 | + * The available time formats are: |
|
1466 | + * * 'int' - Stores the date as an integer timestamp. |
|
1467 | + * * 'datetime' - Stores the date and time in the SQL datetime format. |
|
1468 | + * * 'date' - Stores teh date (only) in the SQL date format. |
|
1469 | + * |
|
1470 | + * @param mixed $user_date An optional PHP timestamp to be converted. |
|
1471 | + * |
|
1472 | + * @access protected |
|
1473 | + * |
|
1474 | + * @return int|null|string The current/user time converted to the proper format. |
|
1475 | + */ |
|
1476 | + protected function set_date($user_date = NULL) |
|
1477 | + { |
|
1478 | + $curr_date = !empty($user_date) ? $user_date : time(); |
|
1479 | + |
|
1480 | + switch ($this->date_format) { |
|
1481 | + case 'int': |
|
1482 | + return $curr_date; |
|
1483 | + break; |
|
1484 | + case 'datetime': |
|
1485 | + return date('Y-m-d H:i:s', $curr_date); |
|
1486 | + break; |
|
1487 | + case 'date': |
|
1488 | + return date('Y-m-d', $curr_date); |
|
1489 | + break; |
|
1490 | + } |
|
1491 | + |
|
1492 | + }//end set_date() |
|
1493 | + |
|
1494 | + //-------------------------------------------------------------------- |
|
1495 | + |
|
1496 | + /** |
|
1497 | + * Returns an array containing the 'code' and 'message' of the |
|
1498 | + * database's error, as provided by CI's database drivers. |
|
1499 | + * |
|
1500 | + * @return mixed |
|
1501 | + */ |
|
1502 | + public function error($db_array_only=false) |
|
1503 | + { |
|
1504 | + // Send any validation errors if we have any. |
|
1505 | + if (function_exists('validation_errors') && validation_errors() && ! $db_array_only) |
|
1506 | + { |
|
1507 | + return validation_errors(); |
|
1508 | + } |
|
1509 | + |
|
1510 | + // No validation errors? Return the db error. |
|
1511 | + $error = $this->db->error(); |
|
1512 | + |
|
1513 | + if ($db_array_only) |
|
1514 | + { |
|
1515 | + return $error; |
|
1516 | + } |
|
1517 | + |
|
1518 | + if (! empty($error['code'])) |
|
1519 | + { |
|
1520 | + return "Database Error {$error['code']}: {$error['message']}."; |
|
1521 | + } |
|
1522 | + |
|
1523 | + // No errors found. |
|
1524 | + return ''; |
|
1525 | + } |
|
1526 | + |
|
1527 | + //-------------------------------------------------------------------- |
|
1528 | + |
|
1529 | + //-------------------------------------------------------------------- |
|
1530 | + // Magic Methods |
|
1531 | + //-------------------------------------------------------------------- |
|
1532 | + |
|
1533 | + /** |
|
1534 | + * __get magic |
|
1535 | + * |
|
1536 | + * Allows models to access CI's loaded classes using the same |
|
1537 | + * syntax as controllers. |
|
1538 | + * |
|
1539 | + * This is the same as what CI's model uses, but we keep it |
|
1540 | + * here since that's the ONLY thing that CI's model does. |
|
1541 | + * |
|
1542 | + * @param string $key |
|
1543 | + */ |
|
1544 | + public function __get($key) |
|
1545 | + { |
|
1546 | + // Give them first crack at any protected class vars |
|
1547 | + if (isset($this->$key)) |
|
1548 | + { |
|
1549 | + return $this->$key; |
|
1550 | + } |
|
1551 | + |
|
1552 | + // Debugging note: |
|
1553 | + // If you're here because you're getting an error message |
|
1554 | + // saying 'Undefined Property: system/core/Model.php', it's |
|
1555 | + // most likely a typo in your model code. |
|
1556 | + return get_instance()->$key; |
|
1557 | + } |
|
1558 | + |
|
1559 | + //-------------------------------------------------------------------- |
|
1560 | + |
|
1561 | + /** |
|
1562 | + * Provide direct access to any of CodeIgniter's DB methods but |
|
1563 | + * make it look like it's part of the class, purely for convenience. |
|
1564 | + * |
|
1565 | + * @param $name |
|
1566 | + * @param $params |
|
1567 | + */ |
|
1568 | + public function __call($name, $params=null) |
|
1569 | + { |
|
1570 | + if (method_exists($this->db, $name)) |
|
1571 | + { |
|
1572 | + call_user_func_array([$this->db, $name], $params); |
|
1573 | + return $this; |
|
1574 | + } |
|
1575 | + } |
|
1576 | + |
|
1577 | + //-------------------------------------------------------------------- |
|
1578 | 1578 | |
1579 | 1579 | |
1580 | 1580 | } |
@@ -43,644 +43,644 @@ |
||
43 | 43 | class Route |
44 | 44 | { |
45 | 45 | |
46 | - // Our routes, ripe for the picking. |
|
47 | - public $routes = array(); |
|
48 | - |
|
49 | - // Holds key/value pairs of named routes |
|
50 | - public static $names = array(); |
|
51 | - |
|
52 | - // Used for grouping routes together. |
|
53 | - public $group = null; |
|
54 | - |
|
55 | - // Holds the 'areas' of the site. |
|
56 | - public static $areas = array(); |
|
57 | - |
|
58 | - // The default controller to use in case |
|
59 | - // 'default_controller' is not in the routes file. |
|
60 | - protected $default_home = 'home'; |
|
61 | - |
|
62 | - // The default constraint to use in route building |
|
63 | - protected $default_constraint = 'any'; |
|
64 | - |
|
65 | - protected $constraints = [ |
|
66 | - 'any' => '(:any)', |
|
67 | - 'num' => '(:num)', |
|
68 | - 'id' => '(:num)', |
|
69 | - 'name' => "([a-zA-Z']+)" |
|
70 | - ]; |
|
71 | - |
|
72 | - protected $current_subdomain = null; |
|
73 | - |
|
74 | - //-------------------------------------------------------------------- |
|
75 | - |
|
76 | - /** |
|
77 | - * Combines the routes that we've defined with the Route class with the |
|
78 | - * routes passed in. This is intended to be used after all routes have been |
|
79 | - * defined to merge CI's default $route array with our routes. |
|
80 | - * |
|
81 | - * Example: |
|
82 | - * $route['default_controller'] = 'home'; |
|
83 | - * Route::resource('posts'); |
|
84 | - * $route = Route::map($route); |
|
85 | - * |
|
86 | - * @param array $routes |
|
87 | - * @internal param array $route The array to merge |
|
88 | - * @return array The merge route array. |
|
89 | - */ |
|
90 | - public function map($routes = array()) |
|
91 | - { |
|
92 | - $controller = isset($routes['default_controller']) ? $routes['default_controller'] : $this->default_home; |
|
93 | - |
|
94 | - $routes = array_merge($routes, $this->routes); |
|
95 | - |
|
96 | - foreach ($routes as $from => $to) { |
|
97 | - $routes[$from] = str_ireplace('{default_controller}', $controller, $to); |
|
98 | - } |
|
99 | - |
|
100 | - return $routes; |
|
101 | - } |
|
102 | - |
|
103 | - //-------------------------------------------------------------------- |
|
104 | - |
|
105 | - /** |
|
106 | - * A single point to the basic routing. Can be used in place of CI's $route |
|
107 | - * array if desired. Used internally by many of the methods. |
|
108 | - * |
|
109 | - * Available options are currently: |
|
110 | - * 'as' - remembers the route via a name that can be called outside of it. |
|
111 | - * 'offset' - Offsets and parameters ($1, $2, etc) in routes by the specified amount. |
|
112 | - * Useful while doing versioning of API's, etc. |
|
113 | - * |
|
114 | - * Example: |
|
115 | - * $route->any('news', 'posts/index'); |
|
116 | - * |
|
117 | - * @param string $from |
|
118 | - * @param string $to |
|
119 | - * @param array $options |
|
120 | - * @return void |
|
121 | - */ |
|
122 | - public function any($from, $to, $options = array()) |
|
123 | - { |
|
124 | - $this->create($from, $to, $options); |
|
125 | - } |
|
126 | - |
|
127 | - //-------------------------------------------------------------------- |
|
128 | - |
|
129 | - /** |
|
130 | - * Sets the default constraint to be used in the system. Typically |
|
131 | - * for use with the 'resources' method. |
|
132 | - * |
|
133 | - * @param $constraint |
|
134 | - */ |
|
135 | - public function setDefaultConstraint($constraint) |
|
136 | - { |
|
137 | - if (array_key_exists($constraint, $this->constraints)) { |
|
138 | - $this->default_constraint = $constraint; |
|
139 | - } |
|
140 | - } |
|
141 | - |
|
142 | - //-------------------------------------------------------------------- |
|
143 | - |
|
144 | - /** |
|
145 | - * Registers a new constraint to be used internally. Useful for creating |
|
146 | - * very specific regex patterns, or simply to allow your routes to be |
|
147 | - * a tad more readable. |
|
148 | - * |
|
149 | - * Example: |
|
150 | - * $route->registerConstraint('hero', '(^.*)'); |
|
151 | - * |
|
152 | - * $route->any('home/{hero}', 'heroes/journey'); |
|
153 | - * |
|
154 | - * // Route then looks like: |
|
155 | - * $route['home/(^.*)'] = 'heroes/journey'; |
|
156 | - * |
|
157 | - * @param $name |
|
158 | - * @param $pattern |
|
159 | - * @param bool $overwrite |
|
160 | - */ |
|
161 | - public function registerConstraint($name, $pattern, $overwrite = false) |
|
162 | - { |
|
163 | - // Ensure consistency |
|
164 | - $name = trim($name, '{} '); |
|
165 | - $pattern = '(' . trim($pattern, '() ') . ')'; |
|
166 | - |
|
167 | - // Not here? Add it and leave... |
|
168 | - if (! array_key_exists($name, $this->constraints)) { |
|
169 | - $this->constraints[$name] = $pattern; |
|
170 | - |
|
171 | - return; |
|
172 | - } |
|
173 | - |
|
174 | - // Here? Then it exists. Should we overwrite it? |
|
175 | - if ($overwrite) { |
|
176 | - $this->constraints[$name] = $pattern; |
|
177 | - } |
|
178 | - } |
|
179 | - |
|
180 | - //-------------------------------------------------------------------- |
|
181 | - |
|
182 | - //-------------------------------------------------------------------- |
|
183 | - // Named Routes |
|
184 | - //-------------------------------------------------------------------- |
|
185 | - |
|
186 | - /** |
|
187 | - * Returns the value of a named route. Useful for getting named |
|
188 | - * routes for use while building with site_url() or in templates |
|
189 | - * where you don't need to instantiate the route class. |
|
190 | - * |
|
191 | - * Example: |
|
192 | - * $route->any('news', 'posts/index', ['as' => 'blog']); |
|
193 | - * |
|
194 | - * // Returns http://mysite.com/news |
|
195 | - * site_url( Route::named('blog') ); |
|
196 | - * |
|
197 | - * @param [type] $name [description] |
|
198 | - * @return [type] [description] |
|
199 | - */ |
|
200 | - public static function named($name) |
|
201 | - { |
|
202 | - if (isset(self::$names[$name])) { |
|
203 | - return self::$names[$name]; |
|
204 | - } |
|
205 | - |
|
206 | - return null; |
|
207 | - } |
|
208 | - |
|
209 | - //-------------------------------------------------------------------- |
|
210 | - |
|
211 | - //-------------------------------------------------------------------- |
|
212 | - // Grouping Routes |
|
213 | - //-------------------------------------------------------------------- |
|
214 | - |
|
215 | - /** |
|
216 | - * Group a series of routes under a single URL segment. This is handy |
|
217 | - * for grouping items into an admin area, like: |
|
218 | - * |
|
219 | - * Example: |
|
220 | - * $route->group('admin', function() { |
|
221 | - * $route->resources('users'); |
|
222 | - * }); |
|
223 | - * |
|
224 | - * @param string $name The name to group/prefix the routes with. |
|
225 | - * @param \Closure $callback An anonymous function that allows you route inside of this group. |
|
226 | - * @return void |
|
227 | - */ |
|
228 | - public function group($name, \Closure $callback) |
|
229 | - { |
|
230 | - $old_group = $this->group; |
|
231 | - |
|
232 | - // To register a route, we'll set a flag so that our router |
|
233 | - // so it will see the groupname. |
|
234 | - $this->group = ltrim($old_group . '/' . $name, '/'); |
|
235 | - |
|
236 | - call_user_func($callback); |
|
237 | - |
|
238 | - // Make sure to clear the group name so we don't accidentally |
|
239 | - // group any ones we didn't want to. |
|
240 | - $this->group = $old_group; |
|
241 | - } |
|
242 | - |
|
243 | - //-------------------------------------------------------------------- |
|
244 | - |
|
245 | - //-------------------------------------------------------------------- |
|
246 | - // HTTP Verb-based routing |
|
247 | - //-------------------------------------------------------------------- |
|
248 | - // Routing works here because, as the routes config file is read in, |
|
249 | - // the various HTTP verb-based routes will only be added to the in-memory |
|
250 | - // routes if it is a call that should respond to that verb. |
|
251 | - // |
|
252 | - // The options array is typically used to pass in an 'as' or var, but may |
|
253 | - // be expanded in the future. See the docblock for 'any' method above for |
|
254 | - // current list of globally available options. |
|
255 | - // |
|
256 | - |
|
257 | - /** |
|
258 | - * Specifies a single route to match for multiple HTTP Verbs. |
|
259 | - * |
|
260 | - * Example: |
|
261 | - * $route->match( ['get', 'post'], 'users/(:num)', 'users/$1); |
|
262 | - * |
|
263 | - * @param array $verbs |
|
264 | - * @param $from |
|
265 | - * @param $to |
|
266 | - * @param array $options |
|
267 | - */ |
|
268 | - public function match($verbs = [], $from, $to, $options = []) |
|
269 | - { |
|
270 | - foreach ($verbs as $verb) { |
|
271 | - $verb = strtolower($verb); |
|
272 | - |
|
273 | - $this->{$verb}($from, $to, $options); |
|
274 | - } |
|
275 | - } |
|
276 | - |
|
277 | - //-------------------------------------------------------------------- |
|
278 | - |
|
279 | - /** |
|
280 | - * Specifies a route that is only available to GET requests. |
|
281 | - * |
|
282 | - * @param $from |
|
283 | - * @param $to |
|
284 | - * @param array $options |
|
285 | - */ |
|
286 | - public function get($from, $to, $options = []) |
|
287 | - { |
|
288 | - if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'GET') { |
|
289 | - $this->create($from, $to, $options); |
|
290 | - } |
|
291 | - } |
|
292 | - |
|
293 | - //-------------------------------------------------------------------- |
|
294 | - |
|
295 | - /** |
|
296 | - * Specifies a route that is only available to POST requests. |
|
297 | - * |
|
298 | - * @param $from |
|
299 | - * @param $to |
|
300 | - * @param array $options |
|
301 | - */ |
|
302 | - public function post($from, $to, $options = []) |
|
303 | - { |
|
304 | - if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST') { |
|
305 | - $this->create($from, $to, $options); |
|
306 | - } |
|
307 | - } |
|
308 | - |
|
309 | - //-------------------------------------------------------------------- |
|
310 | - |
|
311 | - /** |
|
312 | - * Specifies a route that is only available to PUT requests. |
|
313 | - * |
|
314 | - * @param $from |
|
315 | - * @param $to |
|
316 | - * @param array $options |
|
317 | - */ |
|
318 | - public function put($from, $to, $options = []) |
|
319 | - { |
|
320 | - if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'PUT') { |
|
321 | - $this->create($from, $to, $options); |
|
322 | - } |
|
323 | - } |
|
324 | - |
|
325 | - //-------------------------------------------------------------------- |
|
326 | - |
|
327 | - /** |
|
328 | - * Specifies a route that is only available to DELETE requests. |
|
329 | - * |
|
330 | - * @param $from |
|
331 | - * @param $to |
|
332 | - * @param array $options |
|
333 | - */ |
|
334 | - public function delete($from, $to, $options = []) |
|
335 | - { |
|
336 | - if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'DELETE') { |
|
337 | - $this->create($from, $to, $options); |
|
338 | - } |
|
339 | - } |
|
340 | - |
|
341 | - //-------------------------------------------------------------------- |
|
342 | - |
|
343 | - /** |
|
344 | - * Specifies a route that is only available to HEAD requests. |
|
345 | - * |
|
346 | - * @param $from |
|
347 | - * @param $to |
|
348 | - * @param array $options |
|
349 | - */ |
|
350 | - public function head($from, $to, $options = []) |
|
351 | - { |
|
352 | - if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'HEAD') { |
|
353 | - $this->create($from, $to, $options); |
|
354 | - } |
|
355 | - } |
|
356 | - |
|
357 | - //-------------------------------------------------------------------- |
|
358 | - |
|
359 | - /** |
|
360 | - * Specifies a route that is only available to PATCH requests. |
|
361 | - * |
|
362 | - * @param $from |
|
363 | - * @param $to |
|
364 | - * @param array $options |
|
365 | - */ |
|
366 | - public function patch($from, $to, $options = []) |
|
367 | - { |
|
368 | - if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'PATCH') { |
|
369 | - $this->create($from, $to, $options); |
|
370 | - } |
|
371 | - } |
|
372 | - |
|
373 | - //-------------------------------------------------------------------- |
|
374 | - |
|
375 | - /** |
|
376 | - * Specifies a route that is only available to OPTIONS requests. |
|
377 | - * |
|
378 | - * @param $from |
|
379 | - * @param $to |
|
380 | - * @param array $options |
|
381 | - */ |
|
382 | - public function options($from, $to, $options = []) |
|
383 | - { |
|
384 | - if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'OPTIONS') { |
|
385 | - $this->create($from, $to, $options); |
|
386 | - } |
|
387 | - } |
|
388 | - |
|
389 | - //-------------------------------------------------------------------- |
|
390 | - |
|
391 | - /** |
|
392 | - * Creates a collections of HTTP-verb based routes for a controller. |
|
393 | - * |
|
394 | - * Possible Options: |
|
395 | - * 'controller' - Customize the name of the controller used in the 'to' route |
|
396 | - * 'module' - Prepend a module name to the generate 'to' routes |
|
397 | - * 'constraint' - The regex used by the Router. Defaults to '(:any)' |
|
398 | - * |
|
399 | - * Example: |
|
400 | - * $route->resources('photos'); |
|
401 | - * |
|
402 | - * // Generates the following routes: |
|
403 | - * HTTP Verb | Path | Action | Used for... |
|
404 | - * ----------+-------------+---------------+----------------- |
|
405 | - * GET /photos index display a list of photos |
|
406 | - * GET /photos/new creation_form return an HTML form for creating a new photo |
|
407 | - * GET /photos/{id} show display a specific photo |
|
408 | - * GET /photos/{id}/edit editing_form return an HTML form for editing the photo |
|
409 | - * POST /photos create create a new photo |
|
410 | - * PUT /photos/{id} update update an existing photo |
|
411 | - * DELETE /photos/{id}/delete delete delete an existing photo |
|
412 | - * |
|
413 | - * @param string $name The name of the controller to route to. |
|
414 | - * @param array $options An list of possible ways to customize the routing. |
|
415 | - */ |
|
416 | - public function resources($name, $options = []) |
|
417 | - { |
|
418 | - // In order to allow customization of the route the |
|
419 | - // resources are sent to, we need to have a new name |
|
420 | - // to store the values in. |
|
421 | - $new_name = $name; |
|
422 | - |
|
423 | - // If a new controller is specified, then we replace the |
|
424 | - // $name value with the name of the new controller. |
|
425 | - if (isset($options['controller'])) { |
|
426 | - $new_name = $options['controller']; |
|
427 | - } |
|
428 | - |
|
429 | - // If a new module was specified, simply put that path |
|
430 | - // in front of the controller. |
|
431 | - if (isset($options['module'])) { |
|
432 | - $new_name = $options['module'] . '/' . $new_name; |
|
433 | - } |
|
434 | - |
|
435 | - // In order to allow customization of allowed id values |
|
436 | - // we need someplace to store them. |
|
437 | - $id = isset($this->constraints[$this->default_constraint]) ? $this->constraints[$this->default_constraint] : |
|
438 | - '(:any)'; |
|
439 | - |
|
440 | - if (isset($options['constraint'])) { |
|
441 | - $id = $options['constraint']; |
|
442 | - } |
|
443 | - |
|
444 | - $this->get($name, $new_name . '/list_all', $options); |
|
445 | - $this->get($name . '/' . $id, $new_name . '/show/$1', $options); |
|
446 | - $this->post($name, $new_name . '/create', $options); |
|
447 | - $this->put($name . '/' . $id, $new_name . '/update/$1', $options); |
|
448 | - $this->delete($name . '/' . $id, $new_name . '/delete/$1', $options); |
|
449 | - $this->options($name, $new_name . '/index', $options); |
|
450 | - } |
|
451 | - |
|
452 | - //-------------------------------------------------------------------- |
|
453 | - |
|
454 | - /** |
|
455 | - * Lets the system know about different 'areas' within the site, like |
|
456 | - * the admin area, that maps to certain controllers. |
|
457 | - * |
|
458 | - * @param string $area The name of the area. |
|
459 | - * @param string $controller The controller name to look for. |
|
460 | - * @param $options |
|
461 | - */ |
|
462 | - public function area($area, $controller = null, $options = []) |
|
463 | - { |
|
464 | - // No controller? Match the area name. |
|
465 | - $controller = is_null($controller) ? $area : $controller; |
|
466 | - |
|
467 | - // Save the area so we can recognize it later. |
|
468 | - self::$areas[$area] = $controller; |
|
469 | - |
|
470 | - // Create routes for this area. |
|
471 | - $this->create($area . '/(:any)/(:any)/(:any)/(:any)/(:any)', '$1/' . $controller . '/$2/$3/$4/$5', $options); |
|
472 | - $this->create($area . '/(:any)/(:any)/(:any)/(:any)', '$1/' . $controller . '/$2/$3/$4', $options); |
|
473 | - $this->create($area . '/(:any)/(:any)/(:any)', '$1/' . $controller . '/$2/$3', $options); |
|
474 | - $this->create($area . '/(:any)/(:any)', '$1/' . $controller . '/$2', $options); |
|
475 | - $this->create($area . '/(:any)', '$1/' . $controller, $options); |
|
476 | - } |
|
477 | - |
|
478 | - //-------------------------------------------------------------------- |
|
479 | - |
|
480 | - /** |
|
481 | - * Returns the name of the area based on the controller name. |
|
482 | - * |
|
483 | - * @param string $controller The name of the controller |
|
484 | - * @return string The name of the corresponding area |
|
485 | - */ |
|
486 | - public static function getAreaName($controller) |
|
487 | - { |
|
488 | - foreach (self::$areas as $area => $cont) { |
|
489 | - if ($controller == $cont) { |
|
490 | - return $area; |
|
491 | - } |
|
492 | - } |
|
493 | - |
|
494 | - return null; |
|
495 | - } |
|
496 | - |
|
497 | - //-------------------------------------------------------------------- |
|
498 | - |
|
499 | - /** |
|
500 | - * Limits the routes to a specified ENVIRONMENT or they won't run. |
|
501 | - * |
|
502 | - * @param $env |
|
503 | - * @param callable $callback |
|
504 | - * |
|
505 | - * @return bool|null |
|
506 | - */ |
|
507 | - public function environment($env, \Closure $callback) |
|
508 | - { |
|
509 | - if (ENVIRONMENT == $env) |
|
510 | - { |
|
511 | - call_user_func($callback); |
|
512 | - return true; |
|
513 | - } |
|
514 | - |
|
515 | - return null; |
|
516 | - } |
|
517 | - |
|
518 | - //-------------------------------------------------------------------- |
|
519 | - |
|
520 | - |
|
521 | - |
|
522 | - /** |
|
523 | - * Allows you to easily block access to any number of routes by setting |
|
524 | - * that route to an empty path (''). |
|
525 | - * |
|
526 | - * Example: |
|
527 | - * Route::block('posts', 'photos/(:num)'); |
|
528 | - * |
|
529 | - * // Same as... |
|
530 | - * $route['posts'] = ''; |
|
531 | - * $route['photos/(:num)'] = ''; |
|
532 | - */ |
|
533 | - public function block() |
|
534 | - { |
|
535 | - $paths = func_get_args(); |
|
536 | - |
|
537 | - if (! is_array($paths) || ! count($paths)) { |
|
538 | - return; |
|
539 | - } |
|
540 | - |
|
541 | - foreach ($paths as $path) { |
|
542 | - $this->create($path, ''); |
|
543 | - } |
|
544 | - } |
|
545 | - |
|
546 | - //-------------------------------------------------------------------- |
|
547 | - |
|
548 | - /** |
|
549 | - * Empties all named and un-named routes from the system. |
|
550 | - * |
|
551 | - * @return void |
|
552 | - */ |
|
553 | - public function reset() |
|
554 | - { |
|
555 | - $this->routes = array(); |
|
556 | - $this->names = array(); |
|
557 | - $this->group = null; |
|
558 | - self::$areas = array(); |
|
559 | - } |
|
560 | - |
|
561 | - //-------------------------------------------------------------------- |
|
562 | - |
|
563 | - //-------------------------------------------------------------------- |
|
564 | - // Private Methods |
|
565 | - //-------------------------------------------------------------------- |
|
566 | - |
|
567 | - /** |
|
568 | - * Does the heavy lifting of creating an actual route. You must specify |
|
569 | - * the request method(s) that this route will work for. They can be separated |
|
570 | - * by a pipe character "|" if there is more than one. |
|
571 | - * |
|
572 | - * @param string $from |
|
573 | - * @param array $to |
|
574 | - * @param array $options |
|
575 | - * |
|
576 | - * @return array The built route. |
|
577 | - */ |
|
578 | - private function create($from, $to, $options = array()) |
|
579 | - { |
|
580 | - $prefix = is_null($this->group) ? '' : $this->group . '/'; |
|
581 | - |
|
582 | - $from = $prefix . $from; |
|
583 | - |
|
584 | - // Are we saving the name for this one? |
|
585 | - if (isset($options['as']) && !empty($options['as'])) { |
|
586 | - self::$names[$options['as']] = $from; |
|
587 | - } |
|
588 | - |
|
589 | - // Limiting to subdomains? |
|
590 | - if (isset($options['subdomain']) && !empty($options['subdomain'])) { |
|
591 | - // If we don't match the current subdomain, then |
|
592 | - // we don't need to add the route. |
|
593 | - if (!$this->checkSubdomains($options['subdomain'])) { |
|
594 | - return; |
|
595 | - } |
|
596 | - } |
|
597 | - |
|
598 | - // Are we offsetting the parameters? |
|
599 | - // If so, take care of them here in one |
|
600 | - // fell swoop. |
|
601 | - if (isset($options['offset'])) { |
|
602 | - // Get a constant string to work with. |
|
603 | - $to = preg_replace('/(\$\d+)/', '$X', $to); |
|
604 | - |
|
605 | - for ($i = (int)$options['offset'] + 1; $i < (int)$options['offset'] + 7; $i ++) { |
|
606 | - $to = preg_replace_callback( |
|
607 | - '/\$X/', |
|
608 | - function ($m) use ($i) { |
|
609 | - return '$' . $i; |
|
610 | - }, |
|
611 | - $to, |
|
612 | - 1 |
|
613 | - ); |
|
614 | - } |
|
615 | - } |
|
616 | - |
|
617 | - // Convert any custom constraints to the CI/pattern equivalent |
|
618 | - foreach ($this->constraints as $name => $pattern) { |
|
619 | - $from = str_replace('{' . $name . '}', $pattern, $from); |
|
620 | - } |
|
621 | - |
|
622 | - $this->routes[$from] = $to; |
|
623 | - } |
|
624 | - |
|
625 | - //-------------------------------------------------------------------- |
|
626 | - |
|
627 | - /** |
|
628 | - * Compares the subdomain(s) passed in against the current subdomain |
|
629 | - * on this page request. |
|
630 | - * |
|
631 | - * @param $subdomains |
|
632 | - * @return bool |
|
633 | - */ |
|
634 | - private function checkSubdomains($subdomains) |
|
635 | - { |
|
636 | - if (is_null($this->current_subdomain)) { |
|
637 | - $this->determineCurrentSubdomain(); |
|
638 | - } |
|
639 | - |
|
640 | - if (!is_array($subdomains)) { |
|
641 | - $subdomains = array($subdomains); |
|
642 | - } |
|
643 | - |
|
644 | - $matched = false; |
|
645 | - |
|
646 | - array_walk( |
|
647 | - $subdomains, |
|
648 | - function ($subdomain) use (&$matched) { |
|
649 | - if ($subdomain == $this->current_subdomain || $subdomain == '*') { |
|
650 | - $matched = true; |
|
651 | - } |
|
652 | - } |
|
653 | - ); |
|
654 | - |
|
655 | - return $matched; |
|
656 | - } |
|
657 | - |
|
658 | - //-------------------------------------------------------------------- |
|
659 | - |
|
660 | - /** |
|
661 | - * Examines the HTTP_HOST to get a best match for the subdomain. It |
|
662 | - * won't be perfect, but should work for our needs. |
|
663 | - */ |
|
664 | - private function determineCurrentSubdomain() |
|
665 | - { |
|
666 | - $parsedUrl = parse_url($_SERVER['HTTP_HOST']); |
|
667 | - |
|
668 | - $host = explode('.', $parsedUrl['host']); |
|
669 | - |
|
670 | - // If we only have 2 parts, then we don't have a subdomain. |
|
671 | - // This won't be totally accurate, since URL's like example.co.uk |
|
672 | - // would still pass, but it helps to separate the chaff... |
|
673 | - if (!is_array($host) || count($host) == 2) { |
|
674 | - // Set it to false so we don't make it back here again. |
|
675 | - $this->current_subdomain = false; |
|
676 | - return; |
|
677 | - } |
|
678 | - |
|
679 | - // Now, we'll simply take the first element of the array. This should |
|
680 | - // be fine even in cases like example.co.uk, since they won't be looking |
|
681 | - // for 'example' when they try to match the subdomain, in most all cases. |
|
682 | - $this->current_subdomain = array_shift($host); |
|
683 | - } |
|
684 | - //-------------------------------------------------------------------- |
|
46 | + // Our routes, ripe for the picking. |
|
47 | + public $routes = array(); |
|
48 | + |
|
49 | + // Holds key/value pairs of named routes |
|
50 | + public static $names = array(); |
|
51 | + |
|
52 | + // Used for grouping routes together. |
|
53 | + public $group = null; |
|
54 | + |
|
55 | + // Holds the 'areas' of the site. |
|
56 | + public static $areas = array(); |
|
57 | + |
|
58 | + // The default controller to use in case |
|
59 | + // 'default_controller' is not in the routes file. |
|
60 | + protected $default_home = 'home'; |
|
61 | + |
|
62 | + // The default constraint to use in route building |
|
63 | + protected $default_constraint = 'any'; |
|
64 | + |
|
65 | + protected $constraints = [ |
|
66 | + 'any' => '(:any)', |
|
67 | + 'num' => '(:num)', |
|
68 | + 'id' => '(:num)', |
|
69 | + 'name' => "([a-zA-Z']+)" |
|
70 | + ]; |
|
71 | + |
|
72 | + protected $current_subdomain = null; |
|
73 | + |
|
74 | + //-------------------------------------------------------------------- |
|
75 | + |
|
76 | + /** |
|
77 | + * Combines the routes that we've defined with the Route class with the |
|
78 | + * routes passed in. This is intended to be used after all routes have been |
|
79 | + * defined to merge CI's default $route array with our routes. |
|
80 | + * |
|
81 | + * Example: |
|
82 | + * $route['default_controller'] = 'home'; |
|
83 | + * Route::resource('posts'); |
|
84 | + * $route = Route::map($route); |
|
85 | + * |
|
86 | + * @param array $routes |
|
87 | + * @internal param array $route The array to merge |
|
88 | + * @return array The merge route array. |
|
89 | + */ |
|
90 | + public function map($routes = array()) |
|
91 | + { |
|
92 | + $controller = isset($routes['default_controller']) ? $routes['default_controller'] : $this->default_home; |
|
93 | + |
|
94 | + $routes = array_merge($routes, $this->routes); |
|
95 | + |
|
96 | + foreach ($routes as $from => $to) { |
|
97 | + $routes[$from] = str_ireplace('{default_controller}', $controller, $to); |
|
98 | + } |
|
99 | + |
|
100 | + return $routes; |
|
101 | + } |
|
102 | + |
|
103 | + //-------------------------------------------------------------------- |
|
104 | + |
|
105 | + /** |
|
106 | + * A single point to the basic routing. Can be used in place of CI's $route |
|
107 | + * array if desired. Used internally by many of the methods. |
|
108 | + * |
|
109 | + * Available options are currently: |
|
110 | + * 'as' - remembers the route via a name that can be called outside of it. |
|
111 | + * 'offset' - Offsets and parameters ($1, $2, etc) in routes by the specified amount. |
|
112 | + * Useful while doing versioning of API's, etc. |
|
113 | + * |
|
114 | + * Example: |
|
115 | + * $route->any('news', 'posts/index'); |
|
116 | + * |
|
117 | + * @param string $from |
|
118 | + * @param string $to |
|
119 | + * @param array $options |
|
120 | + * @return void |
|
121 | + */ |
|
122 | + public function any($from, $to, $options = array()) |
|
123 | + { |
|
124 | + $this->create($from, $to, $options); |
|
125 | + } |
|
126 | + |
|
127 | + //-------------------------------------------------------------------- |
|
128 | + |
|
129 | + /** |
|
130 | + * Sets the default constraint to be used in the system. Typically |
|
131 | + * for use with the 'resources' method. |
|
132 | + * |
|
133 | + * @param $constraint |
|
134 | + */ |
|
135 | + public function setDefaultConstraint($constraint) |
|
136 | + { |
|
137 | + if (array_key_exists($constraint, $this->constraints)) { |
|
138 | + $this->default_constraint = $constraint; |
|
139 | + } |
|
140 | + } |
|
141 | + |
|
142 | + //-------------------------------------------------------------------- |
|
143 | + |
|
144 | + /** |
|
145 | + * Registers a new constraint to be used internally. Useful for creating |
|
146 | + * very specific regex patterns, or simply to allow your routes to be |
|
147 | + * a tad more readable. |
|
148 | + * |
|
149 | + * Example: |
|
150 | + * $route->registerConstraint('hero', '(^.*)'); |
|
151 | + * |
|
152 | + * $route->any('home/{hero}', 'heroes/journey'); |
|
153 | + * |
|
154 | + * // Route then looks like: |
|
155 | + * $route['home/(^.*)'] = 'heroes/journey'; |
|
156 | + * |
|
157 | + * @param $name |
|
158 | + * @param $pattern |
|
159 | + * @param bool $overwrite |
|
160 | + */ |
|
161 | + public function registerConstraint($name, $pattern, $overwrite = false) |
|
162 | + { |
|
163 | + // Ensure consistency |
|
164 | + $name = trim($name, '{} '); |
|
165 | + $pattern = '(' . trim($pattern, '() ') . ')'; |
|
166 | + |
|
167 | + // Not here? Add it and leave... |
|
168 | + if (! array_key_exists($name, $this->constraints)) { |
|
169 | + $this->constraints[$name] = $pattern; |
|
170 | + |
|
171 | + return; |
|
172 | + } |
|
173 | + |
|
174 | + // Here? Then it exists. Should we overwrite it? |
|
175 | + if ($overwrite) { |
|
176 | + $this->constraints[$name] = $pattern; |
|
177 | + } |
|
178 | + } |
|
179 | + |
|
180 | + //-------------------------------------------------------------------- |
|
181 | + |
|
182 | + //-------------------------------------------------------------------- |
|
183 | + // Named Routes |
|
184 | + //-------------------------------------------------------------------- |
|
185 | + |
|
186 | + /** |
|
187 | + * Returns the value of a named route. Useful for getting named |
|
188 | + * routes for use while building with site_url() or in templates |
|
189 | + * where you don't need to instantiate the route class. |
|
190 | + * |
|
191 | + * Example: |
|
192 | + * $route->any('news', 'posts/index', ['as' => 'blog']); |
|
193 | + * |
|
194 | + * // Returns http://mysite.com/news |
|
195 | + * site_url( Route::named('blog') ); |
|
196 | + * |
|
197 | + * @param [type] $name [description] |
|
198 | + * @return [type] [description] |
|
199 | + */ |
|
200 | + public static function named($name) |
|
201 | + { |
|
202 | + if (isset(self::$names[$name])) { |
|
203 | + return self::$names[$name]; |
|
204 | + } |
|
205 | + |
|
206 | + return null; |
|
207 | + } |
|
208 | + |
|
209 | + //-------------------------------------------------------------------- |
|
210 | + |
|
211 | + //-------------------------------------------------------------------- |
|
212 | + // Grouping Routes |
|
213 | + //-------------------------------------------------------------------- |
|
214 | + |
|
215 | + /** |
|
216 | + * Group a series of routes under a single URL segment. This is handy |
|
217 | + * for grouping items into an admin area, like: |
|
218 | + * |
|
219 | + * Example: |
|
220 | + * $route->group('admin', function() { |
|
221 | + * $route->resources('users'); |
|
222 | + * }); |
|
223 | + * |
|
224 | + * @param string $name The name to group/prefix the routes with. |
|
225 | + * @param \Closure $callback An anonymous function that allows you route inside of this group. |
|
226 | + * @return void |
|
227 | + */ |
|
228 | + public function group($name, \Closure $callback) |
|
229 | + { |
|
230 | + $old_group = $this->group; |
|
231 | + |
|
232 | + // To register a route, we'll set a flag so that our router |
|
233 | + // so it will see the groupname. |
|
234 | + $this->group = ltrim($old_group . '/' . $name, '/'); |
|
235 | + |
|
236 | + call_user_func($callback); |
|
237 | + |
|
238 | + // Make sure to clear the group name so we don't accidentally |
|
239 | + // group any ones we didn't want to. |
|
240 | + $this->group = $old_group; |
|
241 | + } |
|
242 | + |
|
243 | + //-------------------------------------------------------------------- |
|
244 | + |
|
245 | + //-------------------------------------------------------------------- |
|
246 | + // HTTP Verb-based routing |
|
247 | + //-------------------------------------------------------------------- |
|
248 | + // Routing works here because, as the routes config file is read in, |
|
249 | + // the various HTTP verb-based routes will only be added to the in-memory |
|
250 | + // routes if it is a call that should respond to that verb. |
|
251 | + // |
|
252 | + // The options array is typically used to pass in an 'as' or var, but may |
|
253 | + // be expanded in the future. See the docblock for 'any' method above for |
|
254 | + // current list of globally available options. |
|
255 | + // |
|
256 | + |
|
257 | + /** |
|
258 | + * Specifies a single route to match for multiple HTTP Verbs. |
|
259 | + * |
|
260 | + * Example: |
|
261 | + * $route->match( ['get', 'post'], 'users/(:num)', 'users/$1); |
|
262 | + * |
|
263 | + * @param array $verbs |
|
264 | + * @param $from |
|
265 | + * @param $to |
|
266 | + * @param array $options |
|
267 | + */ |
|
268 | + public function match($verbs = [], $from, $to, $options = []) |
|
269 | + { |
|
270 | + foreach ($verbs as $verb) { |
|
271 | + $verb = strtolower($verb); |
|
272 | + |
|
273 | + $this->{$verb}($from, $to, $options); |
|
274 | + } |
|
275 | + } |
|
276 | + |
|
277 | + //-------------------------------------------------------------------- |
|
278 | + |
|
279 | + /** |
|
280 | + * Specifies a route that is only available to GET requests. |
|
281 | + * |
|
282 | + * @param $from |
|
283 | + * @param $to |
|
284 | + * @param array $options |
|
285 | + */ |
|
286 | + public function get($from, $to, $options = []) |
|
287 | + { |
|
288 | + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'GET') { |
|
289 | + $this->create($from, $to, $options); |
|
290 | + } |
|
291 | + } |
|
292 | + |
|
293 | + //-------------------------------------------------------------------- |
|
294 | + |
|
295 | + /** |
|
296 | + * Specifies a route that is only available to POST requests. |
|
297 | + * |
|
298 | + * @param $from |
|
299 | + * @param $to |
|
300 | + * @param array $options |
|
301 | + */ |
|
302 | + public function post($from, $to, $options = []) |
|
303 | + { |
|
304 | + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST') { |
|
305 | + $this->create($from, $to, $options); |
|
306 | + } |
|
307 | + } |
|
308 | + |
|
309 | + //-------------------------------------------------------------------- |
|
310 | + |
|
311 | + /** |
|
312 | + * Specifies a route that is only available to PUT requests. |
|
313 | + * |
|
314 | + * @param $from |
|
315 | + * @param $to |
|
316 | + * @param array $options |
|
317 | + */ |
|
318 | + public function put($from, $to, $options = []) |
|
319 | + { |
|
320 | + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'PUT') { |
|
321 | + $this->create($from, $to, $options); |
|
322 | + } |
|
323 | + } |
|
324 | + |
|
325 | + //-------------------------------------------------------------------- |
|
326 | + |
|
327 | + /** |
|
328 | + * Specifies a route that is only available to DELETE requests. |
|
329 | + * |
|
330 | + * @param $from |
|
331 | + * @param $to |
|
332 | + * @param array $options |
|
333 | + */ |
|
334 | + public function delete($from, $to, $options = []) |
|
335 | + { |
|
336 | + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'DELETE') { |
|
337 | + $this->create($from, $to, $options); |
|
338 | + } |
|
339 | + } |
|
340 | + |
|
341 | + //-------------------------------------------------------------------- |
|
342 | + |
|
343 | + /** |
|
344 | + * Specifies a route that is only available to HEAD requests. |
|
345 | + * |
|
346 | + * @param $from |
|
347 | + * @param $to |
|
348 | + * @param array $options |
|
349 | + */ |
|
350 | + public function head($from, $to, $options = []) |
|
351 | + { |
|
352 | + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'HEAD') { |
|
353 | + $this->create($from, $to, $options); |
|
354 | + } |
|
355 | + } |
|
356 | + |
|
357 | + //-------------------------------------------------------------------- |
|
358 | + |
|
359 | + /** |
|
360 | + * Specifies a route that is only available to PATCH requests. |
|
361 | + * |
|
362 | + * @param $from |
|
363 | + * @param $to |
|
364 | + * @param array $options |
|
365 | + */ |
|
366 | + public function patch($from, $to, $options = []) |
|
367 | + { |
|
368 | + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'PATCH') { |
|
369 | + $this->create($from, $to, $options); |
|
370 | + } |
|
371 | + } |
|
372 | + |
|
373 | + //-------------------------------------------------------------------- |
|
374 | + |
|
375 | + /** |
|
376 | + * Specifies a route that is only available to OPTIONS requests. |
|
377 | + * |
|
378 | + * @param $from |
|
379 | + * @param $to |
|
380 | + * @param array $options |
|
381 | + */ |
|
382 | + public function options($from, $to, $options = []) |
|
383 | + { |
|
384 | + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'OPTIONS') { |
|
385 | + $this->create($from, $to, $options); |
|
386 | + } |
|
387 | + } |
|
388 | + |
|
389 | + //-------------------------------------------------------------------- |
|
390 | + |
|
391 | + /** |
|
392 | + * Creates a collections of HTTP-verb based routes for a controller. |
|
393 | + * |
|
394 | + * Possible Options: |
|
395 | + * 'controller' - Customize the name of the controller used in the 'to' route |
|
396 | + * 'module' - Prepend a module name to the generate 'to' routes |
|
397 | + * 'constraint' - The regex used by the Router. Defaults to '(:any)' |
|
398 | + * |
|
399 | + * Example: |
|
400 | + * $route->resources('photos'); |
|
401 | + * |
|
402 | + * // Generates the following routes: |
|
403 | + * HTTP Verb | Path | Action | Used for... |
|
404 | + * ----------+-------------+---------------+----------------- |
|
405 | + * GET /photos index display a list of photos |
|
406 | + * GET /photos/new creation_form return an HTML form for creating a new photo |
|
407 | + * GET /photos/{id} show display a specific photo |
|
408 | + * GET /photos/{id}/edit editing_form return an HTML form for editing the photo |
|
409 | + * POST /photos create create a new photo |
|
410 | + * PUT /photos/{id} update update an existing photo |
|
411 | + * DELETE /photos/{id}/delete delete delete an existing photo |
|
412 | + * |
|
413 | + * @param string $name The name of the controller to route to. |
|
414 | + * @param array $options An list of possible ways to customize the routing. |
|
415 | + */ |
|
416 | + public function resources($name, $options = []) |
|
417 | + { |
|
418 | + // In order to allow customization of the route the |
|
419 | + // resources are sent to, we need to have a new name |
|
420 | + // to store the values in. |
|
421 | + $new_name = $name; |
|
422 | + |
|
423 | + // If a new controller is specified, then we replace the |
|
424 | + // $name value with the name of the new controller. |
|
425 | + if (isset($options['controller'])) { |
|
426 | + $new_name = $options['controller']; |
|
427 | + } |
|
428 | + |
|
429 | + // If a new module was specified, simply put that path |
|
430 | + // in front of the controller. |
|
431 | + if (isset($options['module'])) { |
|
432 | + $new_name = $options['module'] . '/' . $new_name; |
|
433 | + } |
|
434 | + |
|
435 | + // In order to allow customization of allowed id values |
|
436 | + // we need someplace to store them. |
|
437 | + $id = isset($this->constraints[$this->default_constraint]) ? $this->constraints[$this->default_constraint] : |
|
438 | + '(:any)'; |
|
439 | + |
|
440 | + if (isset($options['constraint'])) { |
|
441 | + $id = $options['constraint']; |
|
442 | + } |
|
443 | + |
|
444 | + $this->get($name, $new_name . '/list_all', $options); |
|
445 | + $this->get($name . '/' . $id, $new_name . '/show/$1', $options); |
|
446 | + $this->post($name, $new_name . '/create', $options); |
|
447 | + $this->put($name . '/' . $id, $new_name . '/update/$1', $options); |
|
448 | + $this->delete($name . '/' . $id, $new_name . '/delete/$1', $options); |
|
449 | + $this->options($name, $new_name . '/index', $options); |
|
450 | + } |
|
451 | + |
|
452 | + //-------------------------------------------------------------------- |
|
453 | + |
|
454 | + /** |
|
455 | + * Lets the system know about different 'areas' within the site, like |
|
456 | + * the admin area, that maps to certain controllers. |
|
457 | + * |
|
458 | + * @param string $area The name of the area. |
|
459 | + * @param string $controller The controller name to look for. |
|
460 | + * @param $options |
|
461 | + */ |
|
462 | + public function area($area, $controller = null, $options = []) |
|
463 | + { |
|
464 | + // No controller? Match the area name. |
|
465 | + $controller = is_null($controller) ? $area : $controller; |
|
466 | + |
|
467 | + // Save the area so we can recognize it later. |
|
468 | + self::$areas[$area] = $controller; |
|
469 | + |
|
470 | + // Create routes for this area. |
|
471 | + $this->create($area . '/(:any)/(:any)/(:any)/(:any)/(:any)', '$1/' . $controller . '/$2/$3/$4/$5', $options); |
|
472 | + $this->create($area . '/(:any)/(:any)/(:any)/(:any)', '$1/' . $controller . '/$2/$3/$4', $options); |
|
473 | + $this->create($area . '/(:any)/(:any)/(:any)', '$1/' . $controller . '/$2/$3', $options); |
|
474 | + $this->create($area . '/(:any)/(:any)', '$1/' . $controller . '/$2', $options); |
|
475 | + $this->create($area . '/(:any)', '$1/' . $controller, $options); |
|
476 | + } |
|
477 | + |
|
478 | + //-------------------------------------------------------------------- |
|
479 | + |
|
480 | + /** |
|
481 | + * Returns the name of the area based on the controller name. |
|
482 | + * |
|
483 | + * @param string $controller The name of the controller |
|
484 | + * @return string The name of the corresponding area |
|
485 | + */ |
|
486 | + public static function getAreaName($controller) |
|
487 | + { |
|
488 | + foreach (self::$areas as $area => $cont) { |
|
489 | + if ($controller == $cont) { |
|
490 | + return $area; |
|
491 | + } |
|
492 | + } |
|
493 | + |
|
494 | + return null; |
|
495 | + } |
|
496 | + |
|
497 | + //-------------------------------------------------------------------- |
|
498 | + |
|
499 | + /** |
|
500 | + * Limits the routes to a specified ENVIRONMENT or they won't run. |
|
501 | + * |
|
502 | + * @param $env |
|
503 | + * @param callable $callback |
|
504 | + * |
|
505 | + * @return bool|null |
|
506 | + */ |
|
507 | + public function environment($env, \Closure $callback) |
|
508 | + { |
|
509 | + if (ENVIRONMENT == $env) |
|
510 | + { |
|
511 | + call_user_func($callback); |
|
512 | + return true; |
|
513 | + } |
|
514 | + |
|
515 | + return null; |
|
516 | + } |
|
517 | + |
|
518 | + //-------------------------------------------------------------------- |
|
519 | + |
|
520 | + |
|
521 | + |
|
522 | + /** |
|
523 | + * Allows you to easily block access to any number of routes by setting |
|
524 | + * that route to an empty path (''). |
|
525 | + * |
|
526 | + * Example: |
|
527 | + * Route::block('posts', 'photos/(:num)'); |
|
528 | + * |
|
529 | + * // Same as... |
|
530 | + * $route['posts'] = ''; |
|
531 | + * $route['photos/(:num)'] = ''; |
|
532 | + */ |
|
533 | + public function block() |
|
534 | + { |
|
535 | + $paths = func_get_args(); |
|
536 | + |
|
537 | + if (! is_array($paths) || ! count($paths)) { |
|
538 | + return; |
|
539 | + } |
|
540 | + |
|
541 | + foreach ($paths as $path) { |
|
542 | + $this->create($path, ''); |
|
543 | + } |
|
544 | + } |
|
545 | + |
|
546 | + //-------------------------------------------------------------------- |
|
547 | + |
|
548 | + /** |
|
549 | + * Empties all named and un-named routes from the system. |
|
550 | + * |
|
551 | + * @return void |
|
552 | + */ |
|
553 | + public function reset() |
|
554 | + { |
|
555 | + $this->routes = array(); |
|
556 | + $this->names = array(); |
|
557 | + $this->group = null; |
|
558 | + self::$areas = array(); |
|
559 | + } |
|
560 | + |
|
561 | + //-------------------------------------------------------------------- |
|
562 | + |
|
563 | + //-------------------------------------------------------------------- |
|
564 | + // Private Methods |
|
565 | + //-------------------------------------------------------------------- |
|
566 | + |
|
567 | + /** |
|
568 | + * Does the heavy lifting of creating an actual route. You must specify |
|
569 | + * the request method(s) that this route will work for. They can be separated |
|
570 | + * by a pipe character "|" if there is more than one. |
|
571 | + * |
|
572 | + * @param string $from |
|
573 | + * @param array $to |
|
574 | + * @param array $options |
|
575 | + * |
|
576 | + * @return array The built route. |
|
577 | + */ |
|
578 | + private function create($from, $to, $options = array()) |
|
579 | + { |
|
580 | + $prefix = is_null($this->group) ? '' : $this->group . '/'; |
|
581 | + |
|
582 | + $from = $prefix . $from; |
|
583 | + |
|
584 | + // Are we saving the name for this one? |
|
585 | + if (isset($options['as']) && !empty($options['as'])) { |
|
586 | + self::$names[$options['as']] = $from; |
|
587 | + } |
|
588 | + |
|
589 | + // Limiting to subdomains? |
|
590 | + if (isset($options['subdomain']) && !empty($options['subdomain'])) { |
|
591 | + // If we don't match the current subdomain, then |
|
592 | + // we don't need to add the route. |
|
593 | + if (!$this->checkSubdomains($options['subdomain'])) { |
|
594 | + return; |
|
595 | + } |
|
596 | + } |
|
597 | + |
|
598 | + // Are we offsetting the parameters? |
|
599 | + // If so, take care of them here in one |
|
600 | + // fell swoop. |
|
601 | + if (isset($options['offset'])) { |
|
602 | + // Get a constant string to work with. |
|
603 | + $to = preg_replace('/(\$\d+)/', '$X', $to); |
|
604 | + |
|
605 | + for ($i = (int)$options['offset'] + 1; $i < (int)$options['offset'] + 7; $i ++) { |
|
606 | + $to = preg_replace_callback( |
|
607 | + '/\$X/', |
|
608 | + function ($m) use ($i) { |
|
609 | + return '$' . $i; |
|
610 | + }, |
|
611 | + $to, |
|
612 | + 1 |
|
613 | + ); |
|
614 | + } |
|
615 | + } |
|
616 | + |
|
617 | + // Convert any custom constraints to the CI/pattern equivalent |
|
618 | + foreach ($this->constraints as $name => $pattern) { |
|
619 | + $from = str_replace('{' . $name . '}', $pattern, $from); |
|
620 | + } |
|
621 | + |
|
622 | + $this->routes[$from] = $to; |
|
623 | + } |
|
624 | + |
|
625 | + //-------------------------------------------------------------------- |
|
626 | + |
|
627 | + /** |
|
628 | + * Compares the subdomain(s) passed in against the current subdomain |
|
629 | + * on this page request. |
|
630 | + * |
|
631 | + * @param $subdomains |
|
632 | + * @return bool |
|
633 | + */ |
|
634 | + private function checkSubdomains($subdomains) |
|
635 | + { |
|
636 | + if (is_null($this->current_subdomain)) { |
|
637 | + $this->determineCurrentSubdomain(); |
|
638 | + } |
|
639 | + |
|
640 | + if (!is_array($subdomains)) { |
|
641 | + $subdomains = array($subdomains); |
|
642 | + } |
|
643 | + |
|
644 | + $matched = false; |
|
645 | + |
|
646 | + array_walk( |
|
647 | + $subdomains, |
|
648 | + function ($subdomain) use (&$matched) { |
|
649 | + if ($subdomain == $this->current_subdomain || $subdomain == '*') { |
|
650 | + $matched = true; |
|
651 | + } |
|
652 | + } |
|
653 | + ); |
|
654 | + |
|
655 | + return $matched; |
|
656 | + } |
|
657 | + |
|
658 | + //-------------------------------------------------------------------- |
|
659 | + |
|
660 | + /** |
|
661 | + * Examines the HTTP_HOST to get a best match for the subdomain. It |
|
662 | + * won't be perfect, but should work for our needs. |
|
663 | + */ |
|
664 | + private function determineCurrentSubdomain() |
|
665 | + { |
|
666 | + $parsedUrl = parse_url($_SERVER['HTTP_HOST']); |
|
667 | + |
|
668 | + $host = explode('.', $parsedUrl['host']); |
|
669 | + |
|
670 | + // If we only have 2 parts, then we don't have a subdomain. |
|
671 | + // This won't be totally accurate, since URL's like example.co.uk |
|
672 | + // would still pass, but it helps to separate the chaff... |
|
673 | + if (!is_array($host) || count($host) == 2) { |
|
674 | + // Set it to false so we don't make it back here again. |
|
675 | + $this->current_subdomain = false; |
|
676 | + return; |
|
677 | + } |
|
678 | + |
|
679 | + // Now, we'll simply take the first element of the array. This should |
|
680 | + // be fine even in cases like example.co.uk, since they won't be looking |
|
681 | + // for 'example' when they try to match the subdomain, in most all cases. |
|
682 | + $this->current_subdomain = array_shift($host); |
|
683 | + } |
|
684 | + //-------------------------------------------------------------------- |
|
685 | 685 | |
686 | 686 | } |
@@ -49,143 +49,143 @@ |
||
49 | 49 | */ |
50 | 50 | class ConfigStore implements SettingsStoreInterface { |
51 | 51 | |
52 | - protected $ci; |
|
53 | - |
|
54 | - //-------------------------------------------------------------------- |
|
55 | - |
|
56 | - public function __construct( $ci=null ) |
|
57 | - { |
|
58 | - if (is_object($ci)) |
|
59 | - { |
|
60 | - $this->ci =& $ci; |
|
61 | - } |
|
62 | - else { |
|
63 | - $this->ci =& get_instance(); |
|
64 | - } |
|
65 | - } |
|
66 | - |
|
67 | - //-------------------------------------------------------------------- |
|
68 | - |
|
69 | - /** |
|
70 | - * Inserts/Replaces a single setting item. |
|
71 | - * |
|
72 | - * @param $key |
|
73 | - * @param null $value |
|
74 | - * @param string $group |
|
75 | - */ |
|
76 | - public function save($key, $value=null, $group='app') |
|
77 | - { |
|
78 | - // Treat the 'app' group as our primary, un-sectioned |
|
79 | - // config files. |
|
80 | - if ($group != 'app') |
|
81 | - { |
|
82 | - return $this->ci->config->set_item($key, $value); |
|
83 | - } |
|
84 | - |
|
85 | - // Otherwise, we'll need to ensure a section exists |
|
86 | - if (! isset($this->ci->config->config[$group])) |
|
87 | - { |
|
88 | - $this->ci->config->config[$group] = []; |
|
89 | - } |
|
90 | - |
|
91 | - $this->ci->config->config[$group][$key] = $value; |
|
92 | - } |
|
93 | - |
|
94 | - //-------------------------------------------------------------------- |
|
95 | - |
|
96 | - /** |
|
97 | - * Retrieves a single item. If the config system doesn't have the |
|
98 | - * value loaded, yet, it will attempt to load a config file matching |
|
99 | - * the 'group' name and grab the value from that. |
|
100 | - * |
|
101 | - * @param $key |
|
102 | - * @param string $group |
|
103 | - * @return mixed |
|
104 | - */ |
|
105 | - public function get($key, $group='application') |
|
106 | - { |
|
107 | - // First, see if CI already has this key loaded. |
|
108 | - $result = $this->ci->config->item($key); |
|
109 | - |
|
110 | - // This bit will fail when the actual value is NULL |
|
111 | - // but should result in false hits infrequently. |
|
112 | - if ($result !== null) |
|
113 | - { |
|
114 | - return $result; |
|
115 | - } |
|
116 | - |
|
117 | - // Try to load the 'group' file, then try to load a |
|
118 | - // config file that matches the group name |
|
119 | - $this->ci->load->config($group, false, true); |
|
52 | + protected $ci; |
|
53 | + |
|
54 | + //-------------------------------------------------------------------- |
|
55 | + |
|
56 | + public function __construct( $ci=null ) |
|
57 | + { |
|
58 | + if (is_object($ci)) |
|
59 | + { |
|
60 | + $this->ci =& $ci; |
|
61 | + } |
|
62 | + else { |
|
63 | + $this->ci =& get_instance(); |
|
64 | + } |
|
65 | + } |
|
66 | + |
|
67 | + //-------------------------------------------------------------------- |
|
68 | + |
|
69 | + /** |
|
70 | + * Inserts/Replaces a single setting item. |
|
71 | + * |
|
72 | + * @param $key |
|
73 | + * @param null $value |
|
74 | + * @param string $group |
|
75 | + */ |
|
76 | + public function save($key, $value=null, $group='app') |
|
77 | + { |
|
78 | + // Treat the 'app' group as our primary, un-sectioned |
|
79 | + // config files. |
|
80 | + if ($group != 'app') |
|
81 | + { |
|
82 | + return $this->ci->config->set_item($key, $value); |
|
83 | + } |
|
84 | + |
|
85 | + // Otherwise, we'll need to ensure a section exists |
|
86 | + if (! isset($this->ci->config->config[$group])) |
|
87 | + { |
|
88 | + $this->ci->config->config[$group] = []; |
|
89 | + } |
|
90 | + |
|
91 | + $this->ci->config->config[$group][$key] = $value; |
|
92 | + } |
|
93 | + |
|
94 | + //-------------------------------------------------------------------- |
|
95 | + |
|
96 | + /** |
|
97 | + * Retrieves a single item. If the config system doesn't have the |
|
98 | + * value loaded, yet, it will attempt to load a config file matching |
|
99 | + * the 'group' name and grab the value from that. |
|
100 | + * |
|
101 | + * @param $key |
|
102 | + * @param string $group |
|
103 | + * @return mixed |
|
104 | + */ |
|
105 | + public function get($key, $group='application') |
|
106 | + { |
|
107 | + // First, see if CI already has this key loaded. |
|
108 | + $result = $this->ci->config->item($key); |
|
109 | + |
|
110 | + // This bit will fail when the actual value is NULL |
|
111 | + // but should result in false hits infrequently. |
|
112 | + if ($result !== null) |
|
113 | + { |
|
114 | + return $result; |
|
115 | + } |
|
116 | + |
|
117 | + // Try to load the 'group' file, then try to load a |
|
118 | + // config file that matches the group name |
|
119 | + $this->ci->load->config($group, false, true); |
|
120 | 120 | |
121 | - $result = $this->ci->config->item($key); |
|
122 | - |
|
123 | - return $result; |
|
124 | - } |
|
125 | - |
|
126 | - //-------------------------------------------------------------------- |
|
127 | - |
|
128 | - /** |
|
129 | - * Deletes a single item. |
|
130 | - * |
|
131 | - * NOT supported by this store. |
|
132 | - * |
|
133 | - * @param $key |
|
134 | - * @param $group |
|
135 | - * @return mixed |
|
136 | - */ |
|
137 | - public function delete($key, $group='app') |
|
138 | - { |
|
139 | - return false; |
|
140 | - } |
|
141 | - |
|
142 | - //-------------------------------------------------------------------- |
|
143 | - |
|
144 | - /** |
|
145 | - * Searches the store for any items with $field = $value. |
|
146 | - * |
|
147 | - * Does not work. |
|
148 | - * |
|
149 | - * @param $field |
|
150 | - * @param $value |
|
151 | - * @return mixed |
|
152 | - */ |
|
153 | - public function findBy($field, $value) |
|
154 | - { |
|
155 | - return false; |
|
156 | - } |
|
157 | - |
|
158 | - //-------------------------------------------------------------------- |
|
159 | - |
|
160 | - /** |
|
161 | - * Retrieves all items in the store either globally or for a single group. |
|
162 | - * |
|
163 | - * @param string $group |
|
164 | - * @return mixed |
|
165 | - */ |
|
166 | - public function all($group=null) |
|
167 | - { |
|
168 | - if (empty($group)) |
|
169 | - { |
|
170 | - return $this->ci->config->config; |
|
171 | - } |
|
172 | - |
|
173 | - // If we're a group, does the group already exists |
|
174 | - // as a 'section' in the config array? |
|
175 | - if (isset($this->ci->config->config[$group])) |
|
176 | - { |
|
177 | - return $this->ci->config->config[$group]; |
|
178 | - } |
|
179 | - |
|
180 | - // Still here? Attempt to load the file into a section |
|
181 | - // and try one last time. |
|
182 | - if ($this->ci->load->config($group)) |
|
183 | - { |
|
184 | - return $this->ci->config->config($group); |
|
185 | - } |
|
186 | - |
|
187 | - return null; |
|
188 | - } |
|
189 | - |
|
190 | - //-------------------------------------------------------------------- |
|
121 | + $result = $this->ci->config->item($key); |
|
122 | + |
|
123 | + return $result; |
|
124 | + } |
|
125 | + |
|
126 | + //-------------------------------------------------------------------- |
|
127 | + |
|
128 | + /** |
|
129 | + * Deletes a single item. |
|
130 | + * |
|
131 | + * NOT supported by this store. |
|
132 | + * |
|
133 | + * @param $key |
|
134 | + * @param $group |
|
135 | + * @return mixed |
|
136 | + */ |
|
137 | + public function delete($key, $group='app') |
|
138 | + { |
|
139 | + return false; |
|
140 | + } |
|
141 | + |
|
142 | + //-------------------------------------------------------------------- |
|
143 | + |
|
144 | + /** |
|
145 | + * Searches the store for any items with $field = $value. |
|
146 | + * |
|
147 | + * Does not work. |
|
148 | + * |
|
149 | + * @param $field |
|
150 | + * @param $value |
|
151 | + * @return mixed |
|
152 | + */ |
|
153 | + public function findBy($field, $value) |
|
154 | + { |
|
155 | + return false; |
|
156 | + } |
|
157 | + |
|
158 | + //-------------------------------------------------------------------- |
|
159 | + |
|
160 | + /** |
|
161 | + * Retrieves all items in the store either globally or for a single group. |
|
162 | + * |
|
163 | + * @param string $group |
|
164 | + * @return mixed |
|
165 | + */ |
|
166 | + public function all($group=null) |
|
167 | + { |
|
168 | + if (empty($group)) |
|
169 | + { |
|
170 | + return $this->ci->config->config; |
|
171 | + } |
|
172 | + |
|
173 | + // If we're a group, does the group already exists |
|
174 | + // as a 'section' in the config array? |
|
175 | + if (isset($this->ci->config->config[$group])) |
|
176 | + { |
|
177 | + return $this->ci->config->config[$group]; |
|
178 | + } |
|
179 | + |
|
180 | + // Still here? Attempt to load the file into a section |
|
181 | + // and try one last time. |
|
182 | + if ($this->ci->load->config($group)) |
|
183 | + { |
|
184 | + return $this->ci->config->config($group); |
|
185 | + } |
|
186 | + |
|
187 | + return null; |
|
188 | + } |
|
189 | + |
|
190 | + //-------------------------------------------------------------------- |
|
191 | 191 | } |
@@ -1,214 +1,214 @@ |
||
1 | 1 | <?php namespace Myth\Settings; |
2 | 2 | /** |
3 | - * Sprint |
|
4 | - * |
|
5 | - * A set of power tools to enhance the CodeIgniter framework and provide consistent workflow. |
|
6 | - * |
|
7 | - * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 | - * of this software and associated documentation files (the "Software"), to deal |
|
9 | - * in the Software without restriction, including without limitation the rights |
|
10 | - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11 | - * copies of the Software, and to permit persons to whom the Software is |
|
12 | - * furnished to do so, subject to the following conditions: |
|
13 | - * |
|
14 | - * The above copyright notice and this permission notice shall be included in |
|
15 | - * all copies or substantial portions of the Software. |
|
16 | - * |
|
17 | - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18 | - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19 | - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20 | - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21 | - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22 | - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
23 | - * THE SOFTWARE. |
|
24 | - * |
|
25 | - * @package Sprint |
|
26 | - * @author Lonnie Ezell |
|
27 | - * @copyright Copyright 2014-2015, New Myth Media, LLC (http://newmythmedia.com) |
|
28 | - * @license http://opensource.org/licenses/MIT (MIT) |
|
29 | - * @link http://sprintphp.com |
|
30 | - * @since Version 1.0 |
|
31 | - */ |
|
3 | + * Sprint |
|
4 | + * |
|
5 | + * A set of power tools to enhance the CodeIgniter framework and provide consistent workflow. |
|
6 | + * |
|
7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 | + * of this software and associated documentation files (the "Software"), to deal |
|
9 | + * in the Software without restriction, including without limitation the rights |
|
10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11 | + * copies of the Software, and to permit persons to whom the Software is |
|
12 | + * furnished to do so, subject to the following conditions: |
|
13 | + * |
|
14 | + * The above copyright notice and this permission notice shall be included in |
|
15 | + * all copies or substantial portions of the Software. |
|
16 | + * |
|
17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
23 | + * THE SOFTWARE. |
|
24 | + * |
|
25 | + * @package Sprint |
|
26 | + * @author Lonnie Ezell |
|
27 | + * @copyright Copyright 2014-2015, New Myth Media, LLC (http://newmythmedia.com) |
|
28 | + * @license http://opensource.org/licenses/MIT (MIT) |
|
29 | + * @link http://sprintphp.com |
|
30 | + * @since Version 1.0 |
|
31 | + */ |
|
32 | 32 | |
33 | 33 | class DatabaseStore implements SettingsStoreInterface { |
34 | 34 | |
35 | - protected $ci; |
|
36 | - |
|
37 | - protected $db; |
|
38 | - |
|
39 | - //-------------------------------------------------------------------- |
|
40 | - |
|
41 | - public function __construct( $ci=null ) |
|
42 | - { |
|
43 | - if (is_object($ci)) |
|
44 | - { |
|
45 | - $this->ci =& $ci; |
|
46 | - } |
|
47 | - else { |
|
48 | - $this->ci =& get_instance(); |
|
49 | - } |
|
50 | - |
|
51 | - // Ensure that the database is loaded. |
|
52 | - if (empty($this->ci->db)) |
|
53 | - { |
|
54 | - $this->ci->load->database(); |
|
55 | - } |
|
56 | - } |
|
57 | - |
|
58 | - //-------------------------------------------------------------------- |
|
59 | - |
|
60 | - /** |
|
61 | - * Inserts or Replaces an setting value. |
|
62 | - * |
|
63 | - * @param $key |
|
64 | - * @param null $value |
|
65 | - * @param string $group |
|
66 | - * @return bool |
|
67 | - */ |
|
68 | - public function save($key, $value=null, $group='app') |
|
69 | - { |
|
70 | - if (empty($this->ci->db) || ! is_object($this->ci->db)) |
|
71 | - { |
|
72 | - return false; |
|
73 | - } |
|
74 | - |
|
75 | - $where = [ |
|
76 | - 'name' => $key, |
|
77 | - 'group' => $group |
|
78 | - ]; |
|
79 | - $this->ci->db->delete('settings', $where); |
|
80 | - |
|
81 | - if (is_array($value) || is_object($value)) |
|
82 | - { |
|
83 | - $value = serialize($value); |
|
84 | - } |
|
85 | - |
|
86 | - $data = [ |
|
87 | - 'name' => $key, |
|
88 | - 'value' => $value, |
|
89 | - 'group' => $group |
|
90 | - ]; |
|
91 | - return $this->ci->db->insert('settings', $data); |
|
92 | - } |
|
93 | - |
|
94 | - //-------------------------------------------------------------------- |
|
95 | - |
|
96 | - /** |
|
97 | - * Retrieves a single item. |
|
98 | - * |
|
99 | - * @param $key |
|
100 | - * @param string $group |
|
101 | - * @return mixed |
|
102 | - */ |
|
103 | - public function get($key, $group='app') |
|
104 | - { |
|
105 | - if (empty($this->ci->db) || ! is_object($this->ci->db)) |
|
106 | - { |
|
107 | - return false; |
|
108 | - } |
|
109 | - |
|
110 | - $where = [ |
|
111 | - 'name' => $key, |
|
112 | - 'group' => $group |
|
113 | - ]; |
|
114 | - |
|
115 | - $query = $this->ci->db->where($where) |
|
116 | - ->get('settings'); |
|
117 | - |
|
118 | - if (! $query->num_rows()) |
|
119 | - { |
|
120 | - return false; |
|
121 | - } |
|
122 | - |
|
123 | - $value = $query->row()->value; |
|
124 | - |
|
125 | - // Check to see if it needs to be unserialized |
|
126 | - $data = @unserialize($value); // We don't need to issue an E_NOTICE here... |
|
127 | - |
|
128 | - // Check for a value of false or |
|
129 | - if ($value === 'b:0;' || $data !== false) |
|
130 | - { |
|
131 | - $value = $data; |
|
132 | - } |
|
133 | - |
|
134 | - return $value; |
|
135 | - } |
|
136 | - |
|
137 | - //-------------------------------------------------------------------- |
|
138 | - |
|
139 | - /** |
|
140 | - * Deletes a single item. |
|
141 | - * |
|
142 | - * @param $key |
|
143 | - * @param $group |
|
144 | - * @return mixed |
|
145 | - */ |
|
146 | - public function delete($key, $group='app') |
|
147 | - { |
|
148 | - if (empty($this->ci->db) || ! is_object($this->ci->db)) |
|
149 | - { |
|
150 | - return false; |
|
151 | - } |
|
152 | - |
|
153 | - $where = [ |
|
154 | - 'name' => $key, |
|
155 | - 'group' => $group |
|
156 | - ]; |
|
157 | - |
|
158 | - return $this->ci->db->delete('settings', $where); |
|
159 | - } |
|
160 | - |
|
161 | - //-------------------------------------------------------------------- |
|
162 | - |
|
163 | - /** |
|
164 | - * Searches the store for any items with $field = $value. |
|
165 | - * |
|
166 | - * @param $field |
|
167 | - * @param $value |
|
168 | - * @return mixed |
|
169 | - */ |
|
170 | - public function findBy($field, $value) |
|
171 | - { |
|
172 | - if (empty($this->ci->db) || ! is_object($this->ci->db)) |
|
173 | - { |
|
174 | - return false; |
|
175 | - } |
|
176 | - |
|
177 | - $query = $this->ci->db->where($field, $value) |
|
178 | - ->get('settings'); |
|
179 | - |
|
180 | - if (! $query->num_rows()) |
|
181 | - { |
|
182 | - return false; |
|
183 | - } |
|
184 | - |
|
185 | - return $query->result_array(); |
|
186 | - } |
|
187 | - |
|
188 | - //-------------------------------------------------------------------- |
|
189 | - |
|
190 | - /** |
|
191 | - * Retrieves all items in the store either globally or for a single group. |
|
192 | - * |
|
193 | - * @param string $group |
|
194 | - * @return mixed |
|
195 | - */ |
|
196 | - public function all($group=null) |
|
197 | - { |
|
198 | - if (empty($this->ci->db) || ! is_object($this->ci->db)) |
|
199 | - { |
|
200 | - return false; |
|
201 | - } |
|
202 | - |
|
203 | - $query = $this->ci->db->get('settings'); |
|
204 | - |
|
205 | - if (! $query->num_rows()) |
|
206 | - { |
|
207 | - return false; |
|
208 | - } |
|
209 | - |
|
210 | - return $query->result_array(); |
|
211 | - } |
|
212 | - |
|
213 | - //-------------------------------------------------------------------- |
|
35 | + protected $ci; |
|
36 | + |
|
37 | + protected $db; |
|
38 | + |
|
39 | + //-------------------------------------------------------------------- |
|
40 | + |
|
41 | + public function __construct( $ci=null ) |
|
42 | + { |
|
43 | + if (is_object($ci)) |
|
44 | + { |
|
45 | + $this->ci =& $ci; |
|
46 | + } |
|
47 | + else { |
|
48 | + $this->ci =& get_instance(); |
|
49 | + } |
|
50 | + |
|
51 | + // Ensure that the database is loaded. |
|
52 | + if (empty($this->ci->db)) |
|
53 | + { |
|
54 | + $this->ci->load->database(); |
|
55 | + } |
|
56 | + } |
|
57 | + |
|
58 | + //-------------------------------------------------------------------- |
|
59 | + |
|
60 | + /** |
|
61 | + * Inserts or Replaces an setting value. |
|
62 | + * |
|
63 | + * @param $key |
|
64 | + * @param null $value |
|
65 | + * @param string $group |
|
66 | + * @return bool |
|
67 | + */ |
|
68 | + public function save($key, $value=null, $group='app') |
|
69 | + { |
|
70 | + if (empty($this->ci->db) || ! is_object($this->ci->db)) |
|
71 | + { |
|
72 | + return false; |
|
73 | + } |
|
74 | + |
|
75 | + $where = [ |
|
76 | + 'name' => $key, |
|
77 | + 'group' => $group |
|
78 | + ]; |
|
79 | + $this->ci->db->delete('settings', $where); |
|
80 | + |
|
81 | + if (is_array($value) || is_object($value)) |
|
82 | + { |
|
83 | + $value = serialize($value); |
|
84 | + } |
|
85 | + |
|
86 | + $data = [ |
|
87 | + 'name' => $key, |
|
88 | + 'value' => $value, |
|
89 | + 'group' => $group |
|
90 | + ]; |
|
91 | + return $this->ci->db->insert('settings', $data); |
|
92 | + } |
|
93 | + |
|
94 | + //-------------------------------------------------------------------- |
|
95 | + |
|
96 | + /** |
|
97 | + * Retrieves a single item. |
|
98 | + * |
|
99 | + * @param $key |
|
100 | + * @param string $group |
|
101 | + * @return mixed |
|
102 | + */ |
|
103 | + public function get($key, $group='app') |
|
104 | + { |
|
105 | + if (empty($this->ci->db) || ! is_object($this->ci->db)) |
|
106 | + { |
|
107 | + return false; |
|
108 | + } |
|
109 | + |
|
110 | + $where = [ |
|
111 | + 'name' => $key, |
|
112 | + 'group' => $group |
|
113 | + ]; |
|
114 | + |
|
115 | + $query = $this->ci->db->where($where) |
|
116 | + ->get('settings'); |
|
117 | + |
|
118 | + if (! $query->num_rows()) |
|
119 | + { |
|
120 | + return false; |
|
121 | + } |
|
122 | + |
|
123 | + $value = $query->row()->value; |
|
124 | + |
|
125 | + // Check to see if it needs to be unserialized |
|
126 | + $data = @unserialize($value); // We don't need to issue an E_NOTICE here... |
|
127 | + |
|
128 | + // Check for a value of false or |
|
129 | + if ($value === 'b:0;' || $data !== false) |
|
130 | + { |
|
131 | + $value = $data; |
|
132 | + } |
|
133 | + |
|
134 | + return $value; |
|
135 | + } |
|
136 | + |
|
137 | + //-------------------------------------------------------------------- |
|
138 | + |
|
139 | + /** |
|
140 | + * Deletes a single item. |
|
141 | + * |
|
142 | + * @param $key |
|
143 | + * @param $group |
|
144 | + * @return mixed |
|
145 | + */ |
|
146 | + public function delete($key, $group='app') |
|
147 | + { |
|
148 | + if (empty($this->ci->db) || ! is_object($this->ci->db)) |
|
149 | + { |
|
150 | + return false; |
|
151 | + } |
|
152 | + |
|
153 | + $where = [ |
|
154 | + 'name' => $key, |
|
155 | + 'group' => $group |
|
156 | + ]; |
|
157 | + |
|
158 | + return $this->ci->db->delete('settings', $where); |
|
159 | + } |
|
160 | + |
|
161 | + //-------------------------------------------------------------------- |
|
162 | + |
|
163 | + /** |
|
164 | + * Searches the store for any items with $field = $value. |
|
165 | + * |
|
166 | + * @param $field |
|
167 | + * @param $value |
|
168 | + * @return mixed |
|
169 | + */ |
|
170 | + public function findBy($field, $value) |
|
171 | + { |
|
172 | + if (empty($this->ci->db) || ! is_object($this->ci->db)) |
|
173 | + { |
|
174 | + return false; |
|
175 | + } |
|
176 | + |
|
177 | + $query = $this->ci->db->where($field, $value) |
|
178 | + ->get('settings'); |
|
179 | + |
|
180 | + if (! $query->num_rows()) |
|
181 | + { |
|
182 | + return false; |
|
183 | + } |
|
184 | + |
|
185 | + return $query->result_array(); |
|
186 | + } |
|
187 | + |
|
188 | + //-------------------------------------------------------------------- |
|
189 | + |
|
190 | + /** |
|
191 | + * Retrieves all items in the store either globally or for a single group. |
|
192 | + * |
|
193 | + * @param string $group |
|
194 | + * @return mixed |
|
195 | + */ |
|
196 | + public function all($group=null) |
|
197 | + { |
|
198 | + if (empty($this->ci->db) || ! is_object($this->ci->db)) |
|
199 | + { |
|
200 | + return false; |
|
201 | + } |
|
202 | + |
|
203 | + $query = $this->ci->db->get('settings'); |
|
204 | + |
|
205 | + if (! $query->num_rows()) |
|
206 | + { |
|
207 | + return false; |
|
208 | + } |
|
209 | + |
|
210 | + return $query->result_array(); |
|
211 | + } |
|
212 | + |
|
213 | + //-------------------------------------------------------------------- |
|
214 | 214 | } |
@@ -1,34 +1,34 @@ discard block |
||
1 | 1 | <?php namespace Myth\Settings; |
2 | 2 | /** |
3 | - * Sprint |
|
4 | - * |
|
5 | - * A set of power tools to enhance the CodeIgniter framework and provide consistent workflow. |
|
6 | - * |
|
7 | - * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 | - * of this software and associated documentation files (the "Software"), to deal |
|
9 | - * in the Software without restriction, including without limitation the rights |
|
10 | - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11 | - * copies of the Software, and to permit persons to whom the Software is |
|
12 | - * furnished to do so, subject to the following conditions: |
|
13 | - * |
|
14 | - * The above copyright notice and this permission notice shall be included in |
|
15 | - * all copies or substantial portions of the Software. |
|
16 | - * |
|
17 | - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18 | - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19 | - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20 | - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21 | - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22 | - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
23 | - * THE SOFTWARE. |
|
24 | - * |
|
25 | - * @package Sprint |
|
26 | - * @author Lonnie Ezell |
|
27 | - * @copyright Copyright 2014-2015, New Myth Media, LLC (http://newmythmedia.com) |
|
28 | - * @license http://opensource.org/licenses/MIT (MIT) |
|
29 | - * @link http://sprintphp.com |
|
30 | - * @since Version 1.0 |
|
31 | - */ |
|
3 | + * Sprint |
|
4 | + * |
|
5 | + * A set of power tools to enhance the CodeIgniter framework and provide consistent workflow. |
|
6 | + * |
|
7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 | + * of this software and associated documentation files (the "Software"), to deal |
|
9 | + * in the Software without restriction, including without limitation the rights |
|
10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11 | + * copies of the Software, and to permit persons to whom the Software is |
|
12 | + * furnished to do so, subject to the following conditions: |
|
13 | + * |
|
14 | + * The above copyright notice and this permission notice shall be included in |
|
15 | + * all copies or substantial portions of the Software. |
|
16 | + * |
|
17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
23 | + * THE SOFTWARE. |
|
24 | + * |
|
25 | + * @package Sprint |
|
26 | + * @author Lonnie Ezell |
|
27 | + * @copyright Copyright 2014-2015, New Myth Media, LLC (http://newmythmedia.com) |
|
28 | + * @license http://opensource.org/licenses/MIT (MIT) |
|
29 | + * @link http://sprintphp.com |
|
30 | + * @since Version 1.0 |
|
31 | + */ |
|
32 | 32 | |
33 | 33 | /** |
34 | 34 | * Class Settings |
@@ -40,253 +40,253 @@ discard block |
||
40 | 40 | */ |
41 | 41 | class Settings { |
42 | 42 | |
43 | - /** |
|
44 | - * Holds instantiated data stores. |
|
45 | - * |
|
46 | - * @var array |
|
47 | - */ |
|
48 | - protected static $stores = []; |
|
49 | - |
|
50 | - protected static $default_store = ''; |
|
51 | - |
|
52 | - protected static $initialized = false; |
|
53 | - |
|
54 | - //-------------------------------------------------------------------- |
|
55 | - |
|
56 | - /** |
|
57 | - * Fires up instances of the datastores and gets us ready to roll. |
|
58 | - */ |
|
59 | - public static function init() |
|
60 | - { |
|
61 | - if (self::$initialized === true) |
|
62 | - { |
|
63 | - return; |
|
64 | - } |
|
65 | - |
|
66 | - // Load up and instantiate our setting stores. |
|
67 | - $user_stores = config_item('settings.stores'); |
|
68 | - |
|
69 | - if (is_array($user_stores)) |
|
70 | - { |
|
71 | - foreach ($user_stores as $alias => $class) |
|
72 | - { |
|
73 | - self::$stores[ strtolower($alias) ] = new $class(); |
|
74 | - } |
|
75 | - } |
|
76 | - |
|
77 | - self::$default_store = config_item('settings.default_store'); |
|
78 | - |
|
79 | - if (empty(self::$stores[ self::$default_store ])) |
|
80 | - { |
|
81 | - show_error( lang('settings.bad_default') ); |
|
82 | - } |
|
83 | - } |
|
84 | - |
|
85 | - //-------------------------------------------------------------------- |
|
86 | - |
|
87 | - /** |
|
88 | - * Inserts/Replaces a single setting item. |
|
89 | - * |
|
90 | - * @param $key |
|
91 | - * @param null $value |
|
92 | - * @param string $group |
|
93 | - * @param string $store |
|
94 | - */ |
|
95 | - public static function save($key, $value=null, $group='app', $store=null) |
|
96 | - { |
|
97 | - self::init(); |
|
98 | - |
|
99 | - if (empty($store) || empty(self::$stores[$store])) |
|
100 | - { |
|
101 | - // we've already determined that the default store exists |
|
102 | - // in the init() method. |
|
103 | - $store = self::$default_store; |
|
104 | - } |
|
105 | - |
|
106 | - return self::$stores[ $store ]->save($key, $value, $group); |
|
107 | - } |
|
108 | - |
|
109 | - //-------------------------------------------------------------------- |
|
110 | - |
|
111 | - /** |
|
112 | - * Retrieves a single item. If not $store is specified, will loop |
|
113 | - * through the stores in order until it finds it. If $store is |
|
114 | - * specified, will only look within that store for it. |
|
115 | - * |
|
116 | - * @param $key |
|
117 | - * @param string $group |
|
118 | - * @param string $store |
|
119 | - * @return mixed |
|
120 | - */ |
|
121 | - public static function get($key, $group='app', $store=null) |
|
122 | - { |
|
123 | - self::init(); |
|
124 | - |
|
125 | - $group = strtolower($group); |
|
126 | - |
|
127 | - // If the store is specified but doesn't exist, crap out |
|
128 | - // so that they developer has a chance to fix it. |
|
129 | - if (! is_null($store) && empty(self::$stores[$store])) |
|
130 | - { |
|
131 | - show_error( sprintf( lang('settings.cant_retrieve'), $store ) ); |
|
132 | - } |
|
133 | - |
|
134 | - // If $store is set, get the value from that store only. |
|
135 | - if (! empty($store)) |
|
136 | - { |
|
137 | - return self::$stores[$store]->get($key, $group); |
|
138 | - } |
|
139 | - |
|
140 | - // Otherwise loop through each store until we find it |
|
141 | - foreach (self::$stores as $s) |
|
142 | - { |
|
143 | - if ($found = $s->get($key, $group)) |
|
144 | - { |
|
145 | - return $found; |
|
146 | - } |
|
147 | - } |
|
148 | - |
|
149 | - // Still here... guess we didn't find anything, then... |
|
150 | - return false; |
|
151 | - } |
|
152 | - |
|
153 | - //-------------------------------------------------------------------- |
|
154 | - |
|
155 | - /** |
|
156 | - * Deletes a single item. |
|
157 | - * |
|
158 | - * @param $key |
|
159 | - * @param $group |
|
160 | - * @param $store |
|
161 | - * @return mixed |
|
162 | - */ |
|
163 | - public static function delete($key, $group='app', $store=null) |
|
164 | - { |
|
165 | - self::init(); |
|
166 | - |
|
167 | - $group = strtolower($group); |
|
168 | - |
|
169 | - // If the store is specified but doesn't exist, crap out |
|
170 | - // so that they developer has a chance to fix it. |
|
171 | - if (! is_null($store) && empty(self::$stores[$store])) |
|
172 | - { |
|
173 | - show_error( sprintf( lang('settings.cant_retrieve'), $store ) ); |
|
174 | - } |
|
175 | - |
|
176 | - // If $store is set, get the value from that store only. |
|
177 | - if (! empty($store)) |
|
178 | - { |
|
179 | - return self::$stores[$store]->delete($key, $group); |
|
180 | - } |
|
181 | - |
|
182 | - // Otherwise delete from the default store |
|
183 | - return self::$stores[ self::$default_store ]->delete($key, $group); |
|
184 | - } |
|
185 | - |
|
186 | - //-------------------------------------------------------------------- |
|
187 | - |
|
188 | - /** |
|
189 | - * Searches the store for any items with $field = $value. |
|
190 | - * |
|
191 | - * @param $field |
|
192 | - * @param $value |
|
193 | - * @return mixed |
|
194 | - */ |
|
195 | - public static function findBy($field, $value) |
|
196 | - { |
|
197 | - self::init(); |
|
198 | - |
|
199 | - foreach (self::$stores as $s) |
|
200 | - { |
|
201 | - if ($found = $s->findBy($field, $value)) |
|
202 | - { |
|
203 | - return $found; |
|
204 | - } |
|
205 | - } |
|
206 | - |
|
207 | - return false; |
|
208 | - } |
|
209 | - |
|
210 | - //-------------------------------------------------------------------- |
|
211 | - |
|
212 | - /** |
|
213 | - * Retrieves all items in the store either globally or for a single group. |
|
214 | - * |
|
215 | - * @param string $group |
|
216 | - * @return mixed |
|
217 | - */ |
|
218 | - public static function all($group=null, $store=null) |
|
219 | - { |
|
220 | - self::init(); |
|
221 | - |
|
222 | - $group = strtolower($group); |
|
223 | - |
|
224 | - if (! empty($store)) |
|
225 | - { |
|
226 | - return self::$stores[ $store ]->all($group); |
|
227 | - } |
|
228 | - |
|
229 | - // Otherwise combine the results from all stores |
|
230 | - $results = []; |
|
231 | - |
|
232 | - foreach (self::$stores as $s) |
|
233 | - { |
|
234 | - $found = $s->all($group); |
|
235 | - if ($found) |
|
236 | - { |
|
237 | - $results = array_merge($results, (array)$found); |
|
238 | - } |
|
239 | - } |
|
240 | - |
|
241 | - return $results; |
|
242 | - } |
|
243 | - |
|
244 | - //-------------------------------------------------------------------- |
|
245 | - |
|
246 | - /** |
|
247 | - * Appends a new datastore the end of the curent stores. |
|
248 | - * |
|
249 | - * @param $alias |
|
250 | - * @param $class |
|
251 | - * @return bool |
|
252 | - */ |
|
253 | - public static function addStore($alias, $class) |
|
254 | - { |
|
255 | - self::init(); |
|
256 | - |
|
257 | - if (class_exists($class)) |
|
258 | - { |
|
259 | - self::$stores[ strtolower($alias) ] = new $class(); |
|
260 | - return true; |
|
261 | - } |
|
262 | - |
|
263 | - return false; |
|
264 | - } |
|
265 | - |
|
266 | - //-------------------------------------------------------------------- |
|
267 | - |
|
268 | - /** |
|
269 | - * Removes a datastore from the list of stores currently available. |
|
270 | - * |
|
271 | - * @param $alias |
|
272 | - * @return bool |
|
273 | - */ |
|
274 | - public static function removeStore($alias) |
|
275 | - { |
|
276 | - self::init(); |
|
277 | - |
|
278 | - $alias = strtolower($alias); |
|
279 | - |
|
280 | - if (empty(self::$stores[$alias])) |
|
281 | - { |
|
282 | - return false; |
|
283 | - } |
|
284 | - |
|
285 | - unset(self::$stores[$alias]); |
|
286 | - |
|
287 | - return true; |
|
288 | - } |
|
289 | - |
|
290 | - //-------------------------------------------------------------------- |
|
43 | + /** |
|
44 | + * Holds instantiated data stores. |
|
45 | + * |
|
46 | + * @var array |
|
47 | + */ |
|
48 | + protected static $stores = []; |
|
49 | + |
|
50 | + protected static $default_store = ''; |
|
51 | + |
|
52 | + protected static $initialized = false; |
|
53 | + |
|
54 | + //-------------------------------------------------------------------- |
|
55 | + |
|
56 | + /** |
|
57 | + * Fires up instances of the datastores and gets us ready to roll. |
|
58 | + */ |
|
59 | + public static function init() |
|
60 | + { |
|
61 | + if (self::$initialized === true) |
|
62 | + { |
|
63 | + return; |
|
64 | + } |
|
65 | + |
|
66 | + // Load up and instantiate our setting stores. |
|
67 | + $user_stores = config_item('settings.stores'); |
|
68 | + |
|
69 | + if (is_array($user_stores)) |
|
70 | + { |
|
71 | + foreach ($user_stores as $alias => $class) |
|
72 | + { |
|
73 | + self::$stores[ strtolower($alias) ] = new $class(); |
|
74 | + } |
|
75 | + } |
|
76 | + |
|
77 | + self::$default_store = config_item('settings.default_store'); |
|
78 | + |
|
79 | + if (empty(self::$stores[ self::$default_store ])) |
|
80 | + { |
|
81 | + show_error( lang('settings.bad_default') ); |
|
82 | + } |
|
83 | + } |
|
84 | + |
|
85 | + //-------------------------------------------------------------------- |
|
86 | + |
|
87 | + /** |
|
88 | + * Inserts/Replaces a single setting item. |
|
89 | + * |
|
90 | + * @param $key |
|
91 | + * @param null $value |
|
92 | + * @param string $group |
|
93 | + * @param string $store |
|
94 | + */ |
|
95 | + public static function save($key, $value=null, $group='app', $store=null) |
|
96 | + { |
|
97 | + self::init(); |
|
98 | + |
|
99 | + if (empty($store) || empty(self::$stores[$store])) |
|
100 | + { |
|
101 | + // we've already determined that the default store exists |
|
102 | + // in the init() method. |
|
103 | + $store = self::$default_store; |
|
104 | + } |
|
105 | + |
|
106 | + return self::$stores[ $store ]->save($key, $value, $group); |
|
107 | + } |
|
108 | + |
|
109 | + //-------------------------------------------------------------------- |
|
110 | + |
|
111 | + /** |
|
112 | + * Retrieves a single item. If not $store is specified, will loop |
|
113 | + * through the stores in order until it finds it. If $store is |
|
114 | + * specified, will only look within that store for it. |
|
115 | + * |
|
116 | + * @param $key |
|
117 | + * @param string $group |
|
118 | + * @param string $store |
|
119 | + * @return mixed |
|
120 | + */ |
|
121 | + public static function get($key, $group='app', $store=null) |
|
122 | + { |
|
123 | + self::init(); |
|
124 | + |
|
125 | + $group = strtolower($group); |
|
126 | + |
|
127 | + // If the store is specified but doesn't exist, crap out |
|
128 | + // so that they developer has a chance to fix it. |
|
129 | + if (! is_null($store) && empty(self::$stores[$store])) |
|
130 | + { |
|
131 | + show_error( sprintf( lang('settings.cant_retrieve'), $store ) ); |
|
132 | + } |
|
133 | + |
|
134 | + // If $store is set, get the value from that store only. |
|
135 | + if (! empty($store)) |
|
136 | + { |
|
137 | + return self::$stores[$store]->get($key, $group); |
|
138 | + } |
|
139 | + |
|
140 | + // Otherwise loop through each store until we find it |
|
141 | + foreach (self::$stores as $s) |
|
142 | + { |
|
143 | + if ($found = $s->get($key, $group)) |
|
144 | + { |
|
145 | + return $found; |
|
146 | + } |
|
147 | + } |
|
148 | + |
|
149 | + // Still here... guess we didn't find anything, then... |
|
150 | + return false; |
|
151 | + } |
|
152 | + |
|
153 | + //-------------------------------------------------------------------- |
|
154 | + |
|
155 | + /** |
|
156 | + * Deletes a single item. |
|
157 | + * |
|
158 | + * @param $key |
|
159 | + * @param $group |
|
160 | + * @param $store |
|
161 | + * @return mixed |
|
162 | + */ |
|
163 | + public static function delete($key, $group='app', $store=null) |
|
164 | + { |
|
165 | + self::init(); |
|
166 | + |
|
167 | + $group = strtolower($group); |
|
168 | + |
|
169 | + // If the store is specified but doesn't exist, crap out |
|
170 | + // so that they developer has a chance to fix it. |
|
171 | + if (! is_null($store) && empty(self::$stores[$store])) |
|
172 | + { |
|
173 | + show_error( sprintf( lang('settings.cant_retrieve'), $store ) ); |
|
174 | + } |
|
175 | + |
|
176 | + // If $store is set, get the value from that store only. |
|
177 | + if (! empty($store)) |
|
178 | + { |
|
179 | + return self::$stores[$store]->delete($key, $group); |
|
180 | + } |
|
181 | + |
|
182 | + // Otherwise delete from the default store |
|
183 | + return self::$stores[ self::$default_store ]->delete($key, $group); |
|
184 | + } |
|
185 | + |
|
186 | + //-------------------------------------------------------------------- |
|
187 | + |
|
188 | + /** |
|
189 | + * Searches the store for any items with $field = $value. |
|
190 | + * |
|
191 | + * @param $field |
|
192 | + * @param $value |
|
193 | + * @return mixed |
|
194 | + */ |
|
195 | + public static function findBy($field, $value) |
|
196 | + { |
|
197 | + self::init(); |
|
198 | + |
|
199 | + foreach (self::$stores as $s) |
|
200 | + { |
|
201 | + if ($found = $s->findBy($field, $value)) |
|
202 | + { |
|
203 | + return $found; |
|
204 | + } |
|
205 | + } |
|
206 | + |
|
207 | + return false; |
|
208 | + } |
|
209 | + |
|
210 | + //-------------------------------------------------------------------- |
|
211 | + |
|
212 | + /** |
|
213 | + * Retrieves all items in the store either globally or for a single group. |
|
214 | + * |
|
215 | + * @param string $group |
|
216 | + * @return mixed |
|
217 | + */ |
|
218 | + public static function all($group=null, $store=null) |
|
219 | + { |
|
220 | + self::init(); |
|
221 | + |
|
222 | + $group = strtolower($group); |
|
223 | + |
|
224 | + if (! empty($store)) |
|
225 | + { |
|
226 | + return self::$stores[ $store ]->all($group); |
|
227 | + } |
|
228 | + |
|
229 | + // Otherwise combine the results from all stores |
|
230 | + $results = []; |
|
231 | + |
|
232 | + foreach (self::$stores as $s) |
|
233 | + { |
|
234 | + $found = $s->all($group); |
|
235 | + if ($found) |
|
236 | + { |
|
237 | + $results = array_merge($results, (array)$found); |
|
238 | + } |
|
239 | + } |
|
240 | + |
|
241 | + return $results; |
|
242 | + } |
|
243 | + |
|
244 | + //-------------------------------------------------------------------- |
|
245 | + |
|
246 | + /** |
|
247 | + * Appends a new datastore the end of the curent stores. |
|
248 | + * |
|
249 | + * @param $alias |
|
250 | + * @param $class |
|
251 | + * @return bool |
|
252 | + */ |
|
253 | + public static function addStore($alias, $class) |
|
254 | + { |
|
255 | + self::init(); |
|
256 | + |
|
257 | + if (class_exists($class)) |
|
258 | + { |
|
259 | + self::$stores[ strtolower($alias) ] = new $class(); |
|
260 | + return true; |
|
261 | + } |
|
262 | + |
|
263 | + return false; |
|
264 | + } |
|
265 | + |
|
266 | + //-------------------------------------------------------------------- |
|
267 | + |
|
268 | + /** |
|
269 | + * Removes a datastore from the list of stores currently available. |
|
270 | + * |
|
271 | + * @param $alias |
|
272 | + * @return bool |
|
273 | + */ |
|
274 | + public static function removeStore($alias) |
|
275 | + { |
|
276 | + self::init(); |
|
277 | + |
|
278 | + $alias = strtolower($alias); |
|
279 | + |
|
280 | + if (empty(self::$stores[$alias])) |
|
281 | + { |
|
282 | + return false; |
|
283 | + } |
|
284 | + |
|
285 | + unset(self::$stores[$alias]); |
|
286 | + |
|
287 | + return true; |
|
288 | + } |
|
289 | + |
|
290 | + //-------------------------------------------------------------------- |
|
291 | 291 | |
292 | 292 | } |
@@ -40,58 +40,58 @@ |
||
40 | 40 | */ |
41 | 41 | interface SettingsStoreInterface { |
42 | 42 | |
43 | - /** |
|
44 | - * Inserts/Replaces a single setting item. |
|
45 | - * |
|
46 | - * @param $key |
|
47 | - * @param null $value |
|
48 | - * @param string $group |
|
49 | - */ |
|
50 | - public function save($key, $value=null, $group='app'); |
|
43 | + /** |
|
44 | + * Inserts/Replaces a single setting item. |
|
45 | + * |
|
46 | + * @param $key |
|
47 | + * @param null $value |
|
48 | + * @param string $group |
|
49 | + */ |
|
50 | + public function save($key, $value=null, $group='app'); |
|
51 | 51 | |
52 | - //-------------------------------------------------------------------- |
|
52 | + //-------------------------------------------------------------------- |
|
53 | 53 | |
54 | - /** |
|
55 | - * Retrieves a single item. |
|
56 | - * |
|
57 | - * @param $key |
|
58 | - * @param string $group |
|
59 | - * @return mixed |
|
60 | - */ |
|
61 | - public function get($key, $group='app'); |
|
54 | + /** |
|
55 | + * Retrieves a single item. |
|
56 | + * |
|
57 | + * @param $key |
|
58 | + * @param string $group |
|
59 | + * @return mixed |
|
60 | + */ |
|
61 | + public function get($key, $group='app'); |
|
62 | 62 | |
63 | - //-------------------------------------------------------------------- |
|
63 | + //-------------------------------------------------------------------- |
|
64 | 64 | |
65 | - /** |
|
66 | - * Deletes a single item. |
|
67 | - * |
|
68 | - * @param $key |
|
69 | - * @param $group |
|
70 | - * @return mixed |
|
71 | - */ |
|
72 | - public function delete($key, $group='app'); |
|
65 | + /** |
|
66 | + * Deletes a single item. |
|
67 | + * |
|
68 | + * @param $key |
|
69 | + * @param $group |
|
70 | + * @return mixed |
|
71 | + */ |
|
72 | + public function delete($key, $group='app'); |
|
73 | 73 | |
74 | - //-------------------------------------------------------------------- |
|
74 | + //-------------------------------------------------------------------- |
|
75 | 75 | |
76 | - /** |
|
77 | - * Searches the store for any items with $field = $value. |
|
78 | - * |
|
79 | - * @param $field |
|
80 | - * @param $value |
|
81 | - * @return mixed |
|
82 | - */ |
|
83 | - public function findBy($field, $value); |
|
76 | + /** |
|
77 | + * Searches the store for any items with $field = $value. |
|
78 | + * |
|
79 | + * @param $field |
|
80 | + * @param $value |
|
81 | + * @return mixed |
|
82 | + */ |
|
83 | + public function findBy($field, $value); |
|
84 | 84 | |
85 | - //-------------------------------------------------------------------- |
|
85 | + //-------------------------------------------------------------------- |
|
86 | 86 | |
87 | - /** |
|
88 | - * Retrieves all items in the store either globally or for a single group. |
|
89 | - * |
|
90 | - * @param string $group |
|
91 | - * @return mixed |
|
92 | - */ |
|
93 | - public function all($group=null); |
|
87 | + /** |
|
88 | + * Retrieves all items in the store either globally or for a single group. |
|
89 | + * |
|
90 | + * @param string $group |
|
91 | + * @return mixed |
|
92 | + */ |
|
93 | + public function all($group=null); |
|
94 | 94 | |
95 | - //-------------------------------------------------------------------- |
|
95 | + //-------------------------------------------------------------------- |
|
96 | 96 | |
97 | 97 | } |
@@ -35,219 +35,219 @@ |
||
35 | 35 | |
36 | 36 | class MetaCollection implements MetaInterface { |
37 | 37 | |
38 | - /** |
|
39 | - * Stores the meta values the user has set. |
|
40 | - * |
|
41 | - * @var array |
|
42 | - */ |
|
43 | - protected $meta = []; |
|
44 | - |
|
45 | - /** |
|
46 | - * Stores the standard meta-value names |
|
47 | - * Mostly here for reference, I guess. |
|
48 | - * |
|
49 | - * @var array |
|
50 | - */ |
|
51 | - protected $std_meta = [ |
|
52 | - 'application-name', |
|
53 | - 'author', |
|
54 | - 'copyright', |
|
55 | - 'description', |
|
56 | - 'generator', |
|
57 | - 'keywords', |
|
58 | - 'robots', |
|
59 | - 'googlebot' |
|
60 | - ]; |
|
61 | - |
|
62 | - /** |
|
63 | - * Stores the HTTP-Equiv meta tags |
|
64 | - * since they need to be rendered differently. |
|
65 | - * |
|
66 | - * @var array |
|
67 | - */ |
|
68 | - protected $http_equiv_meta = [ |
|
69 | - 'cache-control', |
|
70 | - 'content-language', |
|
71 | - 'content-type', |
|
72 | - 'default-style', |
|
73 | - 'expires', |
|
74 | - 'pragma', |
|
75 | - 'refresh', |
|
76 | - 'set-cookie' |
|
77 | - ]; |
|
78 | - |
|
79 | - /** |
|
80 | - * Stores the document's character encoding. |
|
81 | - * |
|
82 | - * @var string |
|
83 | - */ |
|
84 | - public $charset = 'utf-8'; |
|
85 | - |
|
86 | - //-------------------------------------------------------------------- |
|
87 | - |
|
88 | - public function __construct($ci) |
|
89 | - { |
|
90 | - $ci->config->load('html_meta', true); |
|
91 | - |
|
92 | - $config = $ci->config->item('html_meta'); |
|
93 | - |
|
94 | - $this->meta = $config['meta']; |
|
95 | - |
|
96 | - $this->http_equiv_meta = array_merge($this->http_equiv_meta, $config['http-equiv']); |
|
97 | - } |
|
98 | - |
|
99 | - //-------------------------------------------------------------------- |
|
100 | - |
|
101 | - |
|
102 | - /** |
|
103 | - * Sets a single meta item. |
|
104 | - * $alias can also be an array of key/value pairs to set. |
|
105 | - * |
|
106 | - * @param string|array $alias |
|
107 | - * @param null $value |
|
108 | - * |
|
109 | - * @return mixed |
|
110 | - */ |
|
111 | - public function set($alias, $value=null, $escape=true) |
|
112 | - { |
|
113 | - if (is_array($alias)) |
|
114 | - { |
|
115 | - foreach ($alias as $key => $val) |
|
116 | - { |
|
117 | - $this->set($key, $val); |
|
118 | - } |
|
119 | - |
|
120 | - return $this; |
|
121 | - } |
|
122 | - |
|
123 | - // Charset |
|
124 | - if (strtolower($alias) == 'charset') |
|
125 | - { |
|
126 | - $this->charset = $value; |
|
127 | - |
|
128 | - return $this; |
|
129 | - } |
|
130 | - |
|
131 | - $this->meta[ strtolower($alias) ] = $escape ? esc($value, 'htmlAttr') : $value; |
|
132 | - |
|
133 | - return $this; |
|
134 | - } |
|
135 | - |
|
136 | - //-------------------------------------------------------------------- |
|
137 | - |
|
138 | - /** |
|
139 | - * Returns a single meta item. |
|
140 | - * |
|
141 | - * @param $alias |
|
142 | - * |
|
143 | - * @return mixed |
|
144 | - */ |
|
145 | - public function get($alias) |
|
146 | - { |
|
147 | - $alias = strtolower($alias); |
|
148 | - |
|
149 | - return isset($this->meta[ $alias ]) ? $this->meta[$alias] : null; |
|
150 | - } |
|
151 | - |
|
152 | - //-------------------------------------------------------------------- |
|
153 | - |
|
154 | - /** |
|
155 | - * Renders out all defined meta tags. |
|
156 | - * |
|
157 | - * @return mixed |
|
158 | - */ |
|
159 | - public function renderTags() |
|
160 | - { |
|
161 | - if (! count($this->meta)) |
|
162 | - { |
|
163 | - return null; |
|
164 | - } |
|
165 | - |
|
166 | - $output = ''; |
|
167 | - |
|
168 | - // Character Encoding |
|
169 | - $output .= "\t<meta charset=\"{$this->charset}\" >"; |
|
170 | - |
|
171 | - // Everything else |
|
172 | - foreach ($this->meta as $name => $content) |
|
173 | - { |
|
174 | - if (is_array($content)) |
|
175 | - { |
|
176 | - $content = implode(',', $content); |
|
177 | - } |
|
178 | - |
|
179 | - if (empty($content)) |
|
180 | - { |
|
181 | - continue; |
|
182 | - } |
|
183 | - |
|
184 | - // Http Equivalent meta tags. |
|
185 | - if (in_array($name, $this->http_equiv_meta)) |
|
186 | - { |
|
187 | - $output .= "\t<meta http-equiv=\"{$name}\" content=\"{$content}\">\n"; |
|
188 | - } |
|
189 | - // Standard Meta Tag |
|
190 | - else { |
|
191 | - $output .= "\t<meta name=\"{$name}\" content=\"{$content}\">\n"; |
|
192 | - } |
|
193 | - } |
|
194 | - |
|
195 | - return $output; |
|
196 | - } |
|
197 | - |
|
198 | - //-------------------------------------------------------------------- |
|
199 | - |
|
200 | - /** |
|
201 | - * Registers a new HTTP Equivalent meta tag so it can be |
|
202 | - * rendered out properly. |
|
203 | - * |
|
204 | - * @param $name |
|
205 | - * |
|
206 | - * @return $this |
|
207 | - */ |
|
208 | - public function registerHTTPEquivTag($name) |
|
209 | - { |
|
210 | - if (is_null($name)) |
|
211 | - { |
|
212 | - return $this; |
|
213 | - } |
|
214 | - |
|
215 | - $this->http_equiv_meta[] = strtolower($name); |
|
216 | - |
|
217 | - return $this; |
|
218 | - } |
|
219 | - |
|
220 | - //-------------------------------------------------------------------- |
|
221 | - |
|
222 | - |
|
223 | - /** |
|
224 | - * Convenience implementation to set a value |
|
225 | - * as if it was a property of the class. |
|
226 | - * |
|
227 | - * @param $alias |
|
228 | - * @param null $value |
|
229 | - */ |
|
230 | - public function __set($alias, $value=null) |
|
231 | - { |
|
232 | - $this->set($alias, $value); |
|
233 | - } |
|
234 | - |
|
235 | - //-------------------------------------------------------------------- |
|
236 | - |
|
237 | - /** |
|
238 | - * Convenience method to access a value |
|
239 | - * as if it was a property of the class. |
|
240 | - * |
|
241 | - * @param $alias |
|
242 | - * |
|
243 | - * @return mixed |
|
244 | - */ |
|
245 | - public function __get($alias) |
|
246 | - { |
|
247 | - return $this->get($alias); |
|
248 | - } |
|
249 | - |
|
250 | - //-------------------------------------------------------------------- |
|
38 | + /** |
|
39 | + * Stores the meta values the user has set. |
|
40 | + * |
|
41 | + * @var array |
|
42 | + */ |
|
43 | + protected $meta = []; |
|
44 | + |
|
45 | + /** |
|
46 | + * Stores the standard meta-value names |
|
47 | + * Mostly here for reference, I guess. |
|
48 | + * |
|
49 | + * @var array |
|
50 | + */ |
|
51 | + protected $std_meta = [ |
|
52 | + 'application-name', |
|
53 | + 'author', |
|
54 | + 'copyright', |
|
55 | + 'description', |
|
56 | + 'generator', |
|
57 | + 'keywords', |
|
58 | + 'robots', |
|
59 | + 'googlebot' |
|
60 | + ]; |
|
61 | + |
|
62 | + /** |
|
63 | + * Stores the HTTP-Equiv meta tags |
|
64 | + * since they need to be rendered differently. |
|
65 | + * |
|
66 | + * @var array |
|
67 | + */ |
|
68 | + protected $http_equiv_meta = [ |
|
69 | + 'cache-control', |
|
70 | + 'content-language', |
|
71 | + 'content-type', |
|
72 | + 'default-style', |
|
73 | + 'expires', |
|
74 | + 'pragma', |
|
75 | + 'refresh', |
|
76 | + 'set-cookie' |
|
77 | + ]; |
|
78 | + |
|
79 | + /** |
|
80 | + * Stores the document's character encoding. |
|
81 | + * |
|
82 | + * @var string |
|
83 | + */ |
|
84 | + public $charset = 'utf-8'; |
|
85 | + |
|
86 | + //-------------------------------------------------------------------- |
|
87 | + |
|
88 | + public function __construct($ci) |
|
89 | + { |
|
90 | + $ci->config->load('html_meta', true); |
|
91 | + |
|
92 | + $config = $ci->config->item('html_meta'); |
|
93 | + |
|
94 | + $this->meta = $config['meta']; |
|
95 | + |
|
96 | + $this->http_equiv_meta = array_merge($this->http_equiv_meta, $config['http-equiv']); |
|
97 | + } |
|
98 | + |
|
99 | + //-------------------------------------------------------------------- |
|
100 | + |
|
101 | + |
|
102 | + /** |
|
103 | + * Sets a single meta item. |
|
104 | + * $alias can also be an array of key/value pairs to set. |
|
105 | + * |
|
106 | + * @param string|array $alias |
|
107 | + * @param null $value |
|
108 | + * |
|
109 | + * @return mixed |
|
110 | + */ |
|
111 | + public function set($alias, $value=null, $escape=true) |
|
112 | + { |
|
113 | + if (is_array($alias)) |
|
114 | + { |
|
115 | + foreach ($alias as $key => $val) |
|
116 | + { |
|
117 | + $this->set($key, $val); |
|
118 | + } |
|
119 | + |
|
120 | + return $this; |
|
121 | + } |
|
122 | + |
|
123 | + // Charset |
|
124 | + if (strtolower($alias) == 'charset') |
|
125 | + { |
|
126 | + $this->charset = $value; |
|
127 | + |
|
128 | + return $this; |
|
129 | + } |
|
130 | + |
|
131 | + $this->meta[ strtolower($alias) ] = $escape ? esc($value, 'htmlAttr') : $value; |
|
132 | + |
|
133 | + return $this; |
|
134 | + } |
|
135 | + |
|
136 | + //-------------------------------------------------------------------- |
|
137 | + |
|
138 | + /** |
|
139 | + * Returns a single meta item. |
|
140 | + * |
|
141 | + * @param $alias |
|
142 | + * |
|
143 | + * @return mixed |
|
144 | + */ |
|
145 | + public function get($alias) |
|
146 | + { |
|
147 | + $alias = strtolower($alias); |
|
148 | + |
|
149 | + return isset($this->meta[ $alias ]) ? $this->meta[$alias] : null; |
|
150 | + } |
|
151 | + |
|
152 | + //-------------------------------------------------------------------- |
|
153 | + |
|
154 | + /** |
|
155 | + * Renders out all defined meta tags. |
|
156 | + * |
|
157 | + * @return mixed |
|
158 | + */ |
|
159 | + public function renderTags() |
|
160 | + { |
|
161 | + if (! count($this->meta)) |
|
162 | + { |
|
163 | + return null; |
|
164 | + } |
|
165 | + |
|
166 | + $output = ''; |
|
167 | + |
|
168 | + // Character Encoding |
|
169 | + $output .= "\t<meta charset=\"{$this->charset}\" >"; |
|
170 | + |
|
171 | + // Everything else |
|
172 | + foreach ($this->meta as $name => $content) |
|
173 | + { |
|
174 | + if (is_array($content)) |
|
175 | + { |
|
176 | + $content = implode(',', $content); |
|
177 | + } |
|
178 | + |
|
179 | + if (empty($content)) |
|
180 | + { |
|
181 | + continue; |
|
182 | + } |
|
183 | + |
|
184 | + // Http Equivalent meta tags. |
|
185 | + if (in_array($name, $this->http_equiv_meta)) |
|
186 | + { |
|
187 | + $output .= "\t<meta http-equiv=\"{$name}\" content=\"{$content}\">\n"; |
|
188 | + } |
|
189 | + // Standard Meta Tag |
|
190 | + else { |
|
191 | + $output .= "\t<meta name=\"{$name}\" content=\"{$content}\">\n"; |
|
192 | + } |
|
193 | + } |
|
194 | + |
|
195 | + return $output; |
|
196 | + } |
|
197 | + |
|
198 | + //-------------------------------------------------------------------- |
|
199 | + |
|
200 | + /** |
|
201 | + * Registers a new HTTP Equivalent meta tag so it can be |
|
202 | + * rendered out properly. |
|
203 | + * |
|
204 | + * @param $name |
|
205 | + * |
|
206 | + * @return $this |
|
207 | + */ |
|
208 | + public function registerHTTPEquivTag($name) |
|
209 | + { |
|
210 | + if (is_null($name)) |
|
211 | + { |
|
212 | + return $this; |
|
213 | + } |
|
214 | + |
|
215 | + $this->http_equiv_meta[] = strtolower($name); |
|
216 | + |
|
217 | + return $this; |
|
218 | + } |
|
219 | + |
|
220 | + //-------------------------------------------------------------------- |
|
221 | + |
|
222 | + |
|
223 | + /** |
|
224 | + * Convenience implementation to set a value |
|
225 | + * as if it was a property of the class. |
|
226 | + * |
|
227 | + * @param $alias |
|
228 | + * @param null $value |
|
229 | + */ |
|
230 | + public function __set($alias, $value=null) |
|
231 | + { |
|
232 | + $this->set($alias, $value); |
|
233 | + } |
|
234 | + |
|
235 | + //-------------------------------------------------------------------- |
|
236 | + |
|
237 | + /** |
|
238 | + * Convenience method to access a value |
|
239 | + * as if it was a property of the class. |
|
240 | + * |
|
241 | + * @param $alias |
|
242 | + * |
|
243 | + * @return mixed |
|
244 | + */ |
|
245 | + public function __get($alias) |
|
246 | + { |
|
247 | + return $this->get($alias); |
|
248 | + } |
|
249 | + |
|
250 | + //-------------------------------------------------------------------- |
|
251 | 251 | |
252 | 252 | |
253 | 253 |
@@ -33,50 +33,50 @@ |
||
33 | 33 | |
34 | 34 | interface MetaInterface { |
35 | 35 | |
36 | - /** |
|
37 | - * Sets a single meta item. |
|
38 | - * |
|
39 | - * @param $alias |
|
40 | - * @param null $value |
|
41 | - * |
|
42 | - * @return mixed |
|
43 | - */ |
|
44 | - public function set($alias, $value=null); |
|
36 | + /** |
|
37 | + * Sets a single meta item. |
|
38 | + * |
|
39 | + * @param $alias |
|
40 | + * @param null $value |
|
41 | + * |
|
42 | + * @return mixed |
|
43 | + */ |
|
44 | + public function set($alias, $value=null); |
|
45 | 45 | |
46 | - //-------------------------------------------------------------------- |
|
46 | + //-------------------------------------------------------------------- |
|
47 | 47 | |
48 | - /** |
|
49 | - * Returns a single meta item. |
|
50 | - * |
|
51 | - * @param $alias |
|
52 | - * |
|
53 | - * @return mixed |
|
54 | - */ |
|
55 | - public function get($alias); |
|
48 | + /** |
|
49 | + * Returns a single meta item. |
|
50 | + * |
|
51 | + * @param $alias |
|
52 | + * |
|
53 | + * @return mixed |
|
54 | + */ |
|
55 | + public function get($alias); |
|
56 | 56 | |
57 | - //-------------------------------------------------------------------- |
|
57 | + //-------------------------------------------------------------------- |
|
58 | 58 | |
59 | - /** |
|
60 | - * Renders out all defined meta tags. |
|
61 | - * |
|
62 | - * @param array $show_tags |
|
63 | - * |
|
64 | - * @return mixed |
|
65 | - */ |
|
66 | - public function renderTags(); |
|
59 | + /** |
|
60 | + * Renders out all defined meta tags. |
|
61 | + * |
|
62 | + * @param array $show_tags |
|
63 | + * |
|
64 | + * @return mixed |
|
65 | + */ |
|
66 | + public function renderTags(); |
|
67 | 67 | |
68 | - //-------------------------------------------------------------------- |
|
68 | + //-------------------------------------------------------------------- |
|
69 | 69 | |
70 | - /** |
|
71 | - * Registers a new HTTP Equivalent meta tag so it can be |
|
72 | - * rendered out properly. |
|
73 | - * |
|
74 | - * @param $name |
|
75 | - * |
|
76 | - * @return $this |
|
77 | - */ |
|
78 | - public function registerHTTPEquivTag($name); |
|
70 | + /** |
|
71 | + * Registers a new HTTP Equivalent meta tag so it can be |
|
72 | + * rendered out properly. |
|
73 | + * |
|
74 | + * @param $name |
|
75 | + * |
|
76 | + * @return $this |
|
77 | + */ |
|
78 | + public function registerHTTPEquivTag($name); |
|
79 | 79 | |
80 | - //-------------------------------------------------------------------- |
|
80 | + //-------------------------------------------------------------------- |
|
81 | 81 | |
82 | 82 | } |
83 | 83 | \ No newline at end of file |
@@ -38,219 +38,219 @@ |
||
38 | 38 | */ |
39 | 39 | interface ThemerInterface |
40 | 40 | { |
41 | - /** |
|
42 | - * The main entryway into rendering a view. This is called from the |
|
43 | - * controller and is generally the last method called. |
|
44 | - * |
|
45 | - * @param string $layout If provided, will override the default layout. |
|
46 | - */ |
|
47 | - public function render($layout = null); |
|
48 | - |
|
49 | - //-------------------------------------------------------------------- |
|
50 | - |
|
51 | - /** |
|
52 | - * Used within the template layout file to render the current content. |
|
53 | - * This content is typically used to display the current view. |
|
54 | - */ |
|
55 | - public function content(); |
|
56 | - |
|
57 | - //-------------------------------------------------------------------- |
|
58 | - |
|
59 | - /** |
|
60 | - * Sets the active theme to use. This theme should be |
|
61 | - * relative to one of the 'theme_paths' folders. |
|
62 | - * |
|
63 | - * @param $theme |
|
64 | - */ |
|
65 | - public function setTheme($theme); |
|
66 | - |
|
67 | - //-------------------------------------------------------------------- |
|
68 | - |
|
69 | - /** |
|
70 | - * Returns the current theme. |
|
71 | - * |
|
72 | - * @return mixed|string |
|
73 | - */ |
|
74 | - public function theme(); |
|
75 | - |
|
76 | - //-------------------------------------------------------------------- |
|
77 | - |
|
78 | - /** |
|
79 | - * Sets the default theme to use. |
|
80 | - * |
|
81 | - * @param $theme |
|
82 | - * @return mixed |
|
83 | - */ |
|
84 | - public function setDefaultTheme($theme); |
|
85 | - |
|
86 | - //-------------------------------------------------------------------- |
|
87 | - |
|
88 | - /** |
|
89 | - * Sets the current view file to render. |
|
90 | - * |
|
91 | - * @param $file |
|
92 | - * @return mixed |
|
93 | - */ |
|
94 | - public function setView($file); |
|
95 | - |
|
96 | - //-------------------------------------------------------------------- |
|
97 | - |
|
98 | - /** |
|
99 | - * Returns the current view. |
|
100 | - * |
|
101 | - * @return mixed|string |
|
102 | - */ |
|
103 | - public function view(); |
|
104 | - |
|
105 | - //-------------------------------------------------------------------- |
|
106 | - |
|
107 | - /** |
|
108 | - * Sets the current layout to be used. MUST be the name of one of |
|
109 | - * the layout files within the current theme. Case-sensitive. |
|
110 | - * |
|
111 | - * @param $file |
|
112 | - * @return mixed |
|
113 | - */ |
|
114 | - public function setLayout($file); |
|
115 | - |
|
116 | - //-------------------------------------------------------------------- |
|
117 | - |
|
118 | - /** |
|
119 | - * Returns the active layout. |
|
120 | - * |
|
121 | - * @return mixed |
|
122 | - */ |
|
123 | - public function layout(); |
|
124 | - |
|
125 | - //-------------------------------------------------------------------- |
|
126 | - |
|
127 | - /** |
|
128 | - * Stores one or more pieces of data to be passed to the views when |
|
129 | - * they are rendered out. |
|
130 | - * |
|
131 | - * If both $key and $value are ! empty, then it will treat it as a |
|
132 | - * key/value pair. If $key is an array of key/value pairs, then $value |
|
133 | - * is ignored and each element of the array are made available to the |
|
134 | - * view as if it was a single $key/$value pair. |
|
135 | - * |
|
136 | - * @param string|array $key |
|
137 | - * @param mixed $value |
|
138 | - */ |
|
139 | - public function set($key, $value = null); |
|
140 | - |
|
141 | - //-------------------------------------------------------------------- |
|
142 | - |
|
143 | - /** |
|
144 | - * Returns a value that has been previously set(). |
|
145 | - * |
|
146 | - * @param $key |
|
147 | - * @return mixed |
|
148 | - */ |
|
149 | - public function get($key); |
|
150 | - |
|
151 | - //-------------------------------------------------------------------- |
|
152 | - |
|
153 | - /** |
|
154 | - * Determines whether or not the view should be parsed with the |
|
155 | - * CodeIgniter's parser. |
|
156 | - * |
|
157 | - * @param bool $parse |
|
158 | - * @return mixed |
|
159 | - */ |
|
160 | - public function parseViews($parse = false); |
|
161 | - |
|
162 | - //-------------------------------------------------------------------- |
|
163 | - |
|
164 | - /** |
|
165 | - * Theme paths allow you to have multiple locations for themes to be |
|
166 | - * stored. This might be used for separating themes for different sub- |
|
167 | - * applications, or a core theme and user-submitted themes. |
|
168 | - * |
|
169 | - * @param $alias The name the theme can be referenced by |
|
170 | - * @param $path A new path where themes can be found. |
|
171 | - * |
|
172 | - * @return mixed |
|
173 | - */ |
|
174 | - public function addThemePath($alias, $path); |
|
175 | - |
|
176 | - //-------------------------------------------------------------------- |
|
177 | - |
|
178 | - /** |
|
179 | - * Removes a single theme path. |
|
180 | - * |
|
181 | - * @param $alias |
|
182 | - */ |
|
183 | - public function removeThemePath($alias); |
|
184 | - //-------------------------------------------------------------------- |
|
185 | - |
|
186 | - /** |
|
187 | - * Loads a view file. Useful to control caching. Intended for use |
|
188 | - * from within view files. |
|
189 | - * |
|
190 | - * You can specify that a view should belong to a theme by prefixing |
|
191 | - * the name of the theme and a colon to the view name. For example, |
|
192 | - * "admin:header" would try to display the "header.php" file within |
|
193 | - * the "admin" theme. |
|
194 | - * |
|
195 | - * @param string $view |
|
196 | - * @param array $data |
|
197 | - * @param int $cache_time The number of MINUTES to cache the output |
|
198 | - * @return mixed |
|
199 | - */ |
|
200 | - public function display($view, $data = array(), $cache_time = 0); |
|
201 | - |
|
202 | - //-------------------------------------------------------------------- |
|
203 | - |
|
204 | - /** |
|
205 | - * Sets the variant used when creating view names. These variants can |
|
206 | - * be anything, but by default are used to render specific templates |
|
207 | - * for desktop, tablet, and phone. The name of the variant is added |
|
208 | - * to the view name, joined by a "+" symbol. |
|
209 | - * |
|
210 | - * Example: |
|
211 | - * $this->setVariant('phone'); |
|
212 | - * $this->display('header'); |
|
213 | - * |
|
214 | - * Tries to display "views/header+phone.php" |
|
215 | - * |
|
216 | - * @param $variant |
|
217 | - * @return mixed |
|
218 | - */ |
|
219 | - public function setVariant($variant); |
|
220 | - |
|
221 | - //-------------------------------------------------------------------- |
|
222 | - |
|
223 | - /** |
|
224 | - * Adds a new variant to the system. |
|
225 | - * |
|
226 | - * @param $name |
|
227 | - * @param $postfix |
|
228 | - * @return mixed |
|
229 | - */ |
|
230 | - public function addVariant($name, $postfix); |
|
231 | - |
|
232 | - //-------------------------------------------------------------------- |
|
233 | - |
|
234 | - /** |
|
235 | - * Removes a variant from the system. |
|
236 | - * |
|
237 | - * @param $name |
|
238 | - * @param $postfix |
|
239 | - * @return mixed |
|
240 | - */ |
|
241 | - public function removeVariant($name); |
|
242 | - |
|
243 | - //-------------------------------------------------------------------- |
|
244 | - |
|
245 | - /** |
|
246 | - * Runs a callback method and returns the contents to the view. |
|
247 | - * |
|
248 | - * @param $command |
|
249 | - * @param int $cache_minutes |
|
250 | - * @return mixed |
|
251 | - */ |
|
252 | - public function call($command, $cache_minutes=0); |
|
253 | - |
|
254 | - //-------------------------------------------------------------------- |
|
41 | + /** |
|
42 | + * The main entryway into rendering a view. This is called from the |
|
43 | + * controller and is generally the last method called. |
|
44 | + * |
|
45 | + * @param string $layout If provided, will override the default layout. |
|
46 | + */ |
|
47 | + public function render($layout = null); |
|
48 | + |
|
49 | + //-------------------------------------------------------------------- |
|
50 | + |
|
51 | + /** |
|
52 | + * Used within the template layout file to render the current content. |
|
53 | + * This content is typically used to display the current view. |
|
54 | + */ |
|
55 | + public function content(); |
|
56 | + |
|
57 | + //-------------------------------------------------------------------- |
|
58 | + |
|
59 | + /** |
|
60 | + * Sets the active theme to use. This theme should be |
|
61 | + * relative to one of the 'theme_paths' folders. |
|
62 | + * |
|
63 | + * @param $theme |
|
64 | + */ |
|
65 | + public function setTheme($theme); |
|
66 | + |
|
67 | + //-------------------------------------------------------------------- |
|
68 | + |
|
69 | + /** |
|
70 | + * Returns the current theme. |
|
71 | + * |
|
72 | + * @return mixed|string |
|
73 | + */ |
|
74 | + public function theme(); |
|
75 | + |
|
76 | + //-------------------------------------------------------------------- |
|
77 | + |
|
78 | + /** |
|
79 | + * Sets the default theme to use. |
|
80 | + * |
|
81 | + * @param $theme |
|
82 | + * @return mixed |
|
83 | + */ |
|
84 | + public function setDefaultTheme($theme); |
|
85 | + |
|
86 | + //-------------------------------------------------------------------- |
|
87 | + |
|
88 | + /** |
|
89 | + * Sets the current view file to render. |
|
90 | + * |
|
91 | + * @param $file |
|
92 | + * @return mixed |
|
93 | + */ |
|
94 | + public function setView($file); |
|
95 | + |
|
96 | + //-------------------------------------------------------------------- |
|
97 | + |
|
98 | + /** |
|
99 | + * Returns the current view. |
|
100 | + * |
|
101 | + * @return mixed|string |
|
102 | + */ |
|
103 | + public function view(); |
|
104 | + |
|
105 | + //-------------------------------------------------------------------- |
|
106 | + |
|
107 | + /** |
|
108 | + * Sets the current layout to be used. MUST be the name of one of |
|
109 | + * the layout files within the current theme. Case-sensitive. |
|
110 | + * |
|
111 | + * @param $file |
|
112 | + * @return mixed |
|
113 | + */ |
|
114 | + public function setLayout($file); |
|
115 | + |
|
116 | + //-------------------------------------------------------------------- |
|
117 | + |
|
118 | + /** |
|
119 | + * Returns the active layout. |
|
120 | + * |
|
121 | + * @return mixed |
|
122 | + */ |
|
123 | + public function layout(); |
|
124 | + |
|
125 | + //-------------------------------------------------------------------- |
|
126 | + |
|
127 | + /** |
|
128 | + * Stores one or more pieces of data to be passed to the views when |
|
129 | + * they are rendered out. |
|
130 | + * |
|
131 | + * If both $key and $value are ! empty, then it will treat it as a |
|
132 | + * key/value pair. If $key is an array of key/value pairs, then $value |
|
133 | + * is ignored and each element of the array are made available to the |
|
134 | + * view as if it was a single $key/$value pair. |
|
135 | + * |
|
136 | + * @param string|array $key |
|
137 | + * @param mixed $value |
|
138 | + */ |
|
139 | + public function set($key, $value = null); |
|
140 | + |
|
141 | + //-------------------------------------------------------------------- |
|
142 | + |
|
143 | + /** |
|
144 | + * Returns a value that has been previously set(). |
|
145 | + * |
|
146 | + * @param $key |
|
147 | + * @return mixed |
|
148 | + */ |
|
149 | + public function get($key); |
|
150 | + |
|
151 | + //-------------------------------------------------------------------- |
|
152 | + |
|
153 | + /** |
|
154 | + * Determines whether or not the view should be parsed with the |
|
155 | + * CodeIgniter's parser. |
|
156 | + * |
|
157 | + * @param bool $parse |
|
158 | + * @return mixed |
|
159 | + */ |
|
160 | + public function parseViews($parse = false); |
|
161 | + |
|
162 | + //-------------------------------------------------------------------- |
|
163 | + |
|
164 | + /** |
|
165 | + * Theme paths allow you to have multiple locations for themes to be |
|
166 | + * stored. This might be used for separating themes for different sub- |
|
167 | + * applications, or a core theme and user-submitted themes. |
|
168 | + * |
|
169 | + * @param $alias The name the theme can be referenced by |
|
170 | + * @param $path A new path where themes can be found. |
|
171 | + * |
|
172 | + * @return mixed |
|
173 | + */ |
|
174 | + public function addThemePath($alias, $path); |
|
175 | + |
|
176 | + //-------------------------------------------------------------------- |
|
177 | + |
|
178 | + /** |
|
179 | + * Removes a single theme path. |
|
180 | + * |
|
181 | + * @param $alias |
|
182 | + */ |
|
183 | + public function removeThemePath($alias); |
|
184 | + //-------------------------------------------------------------------- |
|
185 | + |
|
186 | + /** |
|
187 | + * Loads a view file. Useful to control caching. Intended for use |
|
188 | + * from within view files. |
|
189 | + * |
|
190 | + * You can specify that a view should belong to a theme by prefixing |
|
191 | + * the name of the theme and a colon to the view name. For example, |
|
192 | + * "admin:header" would try to display the "header.php" file within |
|
193 | + * the "admin" theme. |
|
194 | + * |
|
195 | + * @param string $view |
|
196 | + * @param array $data |
|
197 | + * @param int $cache_time The number of MINUTES to cache the output |
|
198 | + * @return mixed |
|
199 | + */ |
|
200 | + public function display($view, $data = array(), $cache_time = 0); |
|
201 | + |
|
202 | + //-------------------------------------------------------------------- |
|
203 | + |
|
204 | + /** |
|
205 | + * Sets the variant used when creating view names. These variants can |
|
206 | + * be anything, but by default are used to render specific templates |
|
207 | + * for desktop, tablet, and phone. The name of the variant is added |
|
208 | + * to the view name, joined by a "+" symbol. |
|
209 | + * |
|
210 | + * Example: |
|
211 | + * $this->setVariant('phone'); |
|
212 | + * $this->display('header'); |
|
213 | + * |
|
214 | + * Tries to display "views/header+phone.php" |
|
215 | + * |
|
216 | + * @param $variant |
|
217 | + * @return mixed |
|
218 | + */ |
|
219 | + public function setVariant($variant); |
|
220 | + |
|
221 | + //-------------------------------------------------------------------- |
|
222 | + |
|
223 | + /** |
|
224 | + * Adds a new variant to the system. |
|
225 | + * |
|
226 | + * @param $name |
|
227 | + * @param $postfix |
|
228 | + * @return mixed |
|
229 | + */ |
|
230 | + public function addVariant($name, $postfix); |
|
231 | + |
|
232 | + //-------------------------------------------------------------------- |
|
233 | + |
|
234 | + /** |
|
235 | + * Removes a variant from the system. |
|
236 | + * |
|
237 | + * @param $name |
|
238 | + * @param $postfix |
|
239 | + * @return mixed |
|
240 | + */ |
|
241 | + public function removeVariant($name); |
|
242 | + |
|
243 | + //-------------------------------------------------------------------- |
|
244 | + |
|
245 | + /** |
|
246 | + * Runs a callback method and returns the contents to the view. |
|
247 | + * |
|
248 | + * @param $command |
|
249 | + * @param int $cache_minutes |
|
250 | + * @return mixed |
|
251 | + */ |
|
252 | + public function call($command, $cache_minutes=0); |
|
253 | + |
|
254 | + //-------------------------------------------------------------------- |
|
255 | 255 | |
256 | 256 | } |