@@ -1,8 +1,8 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | - * Function to test private & protected method |
|
| 5 | - */ |
|
| 4 | + * Function to test private & protected method |
|
| 5 | + */ |
|
| 6 | 6 | function runPrivateOrProtectedMethod($object, $method, array $args = array()){ |
| 7 | 7 | $r = new ReflectionClass(get_class($object)); |
| 8 | 8 | $m = $r->getMethod($method); |
@@ -3,66 +3,66 @@ discard block |
||
| 3 | 3 | class FormValidationTest extends TestCase |
| 4 | 4 | { |
| 5 | 5 | public static function setUpBeforeClass() |
| 6 | - { |
|
| 6 | + { |
|
| 7 | 7 | |
| 8 | - } |
|
| 8 | + } |
|
| 9 | 9 | |
| 10 | 10 | public static function tearDownAfterClass() |
| 11 | - { |
|
| 11 | + { |
|
| 12 | 12 | |
| 13 | - } |
|
| 13 | + } |
|
| 14 | 14 | |
| 15 | - protected function setUp() |
|
| 16 | - { |
|
| 15 | + protected function setUp() |
|
| 16 | + { |
|
| 17 | 17 | |
| 18 | - } |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | - protected function tearDown() |
|
| 21 | - { |
|
| 20 | + protected function tearDown() |
|
| 21 | + { |
|
| 22 | 22 | |
| 23 | - } |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | 25 | |
| 26 | 26 | |
| 27 | - // tests |
|
| 28 | - public function testValidationDataIsEmpty() |
|
| 29 | - { |
|
| 27 | + // tests |
|
| 28 | + public function testValidationDataIsEmpty() |
|
| 29 | + { |
|
| 30 | 30 | $fv = new FormValidation(); |
| 31 | 31 | $this->assertEmpty($fv->getData()); |
| 32 | - } |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | 34 | public function testValidationDataIsNotEmpty() |
| 35 | - { |
|
| 35 | + { |
|
| 36 | 36 | $fv = new FormValidation(); |
| 37 | 37 | $fv->setData(array('name' => 'mike')); |
| 38 | 38 | $this->assertNotEmpty($fv->getData()); |
| 39 | 39 | $this->assertArrayHasKey('name', $fv->getData()); |
| 40 | - } |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | 42 | public function testCannotDoValidation() |
| 43 | - { |
|
| 43 | + { |
|
| 44 | 44 | $fv = new FormValidation(); |
| 45 | 45 | $this->assertFalse($fv->canDoValidation()); |
| 46 | - } |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | 48 | public function testSettingErrorDelimiter() |
| 49 | - { |
|
| 49 | + { |
|
| 50 | 50 | $fv = new FormValidation(); |
| 51 | 51 | $fv->setErrorDelimiter('<a>', '</b>'); |
| 52 | 52 | $this->assertContains('<a>', $fv->getErrorDelimiter()); |
| 53 | 53 | $this->assertContains('</b>', $fv->getErrorDelimiter()); |
| 54 | - } |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | 56 | public function testSettingErrorsDelimiter() |
| 57 | - { |
|
| 57 | + { |
|
| 58 | 58 | $fv = new FormValidation(); |
| 59 | 59 | $fv->setErrorsDelimiter('<foo>', '</bar>'); |
| 60 | 60 | $this->assertContains('<foo>', $fv->getErrorsDelimiter()); |
| 61 | 61 | $this->assertContains('</bar>', $fv->getErrorsDelimiter()); |
| 62 | - } |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | 64 | public function testSettingErrorMessageOverride() |
| 65 | - { |
|
| 65 | + { |
|
| 66 | 66 | |
| 67 | 67 | //field specific message for the rule |
| 68 | 68 | $fv = new FormValidation(); |
@@ -82,10 +82,10 @@ discard block |
||
| 82 | 82 | |
| 83 | 83 | $this->assertFalse($fv->run()); |
| 84 | 84 | $this->assertContains('global required message error', $fv->returnErrors()); |
| 85 | - } |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | 87 | public function testSettingCustomErrorMessage() |
| 88 | - { |
|
| 88 | + { |
|
| 89 | 89 | |
| 90 | 90 | $fv = new FormValidation(); |
| 91 | 91 | $fv->setData(array('foo' => '')); |
@@ -103,20 +103,20 @@ discard block |
||
| 103 | 103 | |
| 104 | 104 | $this->assertFalse($fv->run()); |
| 105 | 105 | $this->assertContains('custom "bar" message error', $fv->returnErrors()); |
| 106 | - } |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | 108 | public function testReturnErrorsArray() |
| 109 | - { |
|
| 109 | + { |
|
| 110 | 110 | $fv = new FormValidation(); |
| 111 | 111 | $fv->setRule('name', 'name', 'required'); |
| 112 | 112 | $fv->setData(array('name' => '')); |
| 113 | 113 | $this->assertFalse($fv->run()); |
| 114 | 114 | $this->assertNotEmpty($fv->returnErrors()); |
| 115 | - } |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | 117 | |
| 118 | 118 | public function testRequiredRule() |
| 119 | - { |
|
| 119 | + { |
|
| 120 | 120 | $fv = new FormValidation(); |
| 121 | 121 | $fv->setRule('name', 'name', 'required'); |
| 122 | 122 | $fv->setData(array('name' => '')); |
@@ -131,5 +131,5 @@ discard block |
||
| 131 | 131 | $fv->setRule('name', 'name', 'required'); |
| 132 | 132 | $fv->setData(array('name' => 'tony')); |
| 133 | 133 | $this->assertTrue($fv->run()); |
| 134 | - } |
|
| 134 | + } |
|
| 135 | 135 | } |
| 136 | 136 | \ No newline at end of file |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | * You should have received a copy of the GNU General Public License |
| 23 | 23 | * along with this program; if not, write to the Free Software |
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | - */ |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | 27 | /** |
| 28 | 28 | * @file common.php |
@@ -438,13 +438,13 @@ discard block |
||
| 438 | 438 | |
| 439 | 439 | |
| 440 | 440 | /** |
| 441 | - * Function to stringfy PHP variable, useful in debug situation |
|
| 442 | - * |
|
| 443 | - * @param mixed $var the variable to stringfy |
|
| 444 | - * @codeCoverageIgnore |
|
| 445 | - * |
|
| 446 | - * @return string the stringfy value |
|
| 447 | - */ |
|
| 441 | + * Function to stringfy PHP variable, useful in debug situation |
|
| 442 | + * |
|
| 443 | + * @param mixed $var the variable to stringfy |
|
| 444 | + * @codeCoverageIgnore |
|
| 445 | + * |
|
| 446 | + * @return string the stringfy value |
|
| 447 | + */ |
|
| 448 | 448 | function stringfy_vars($var){ |
| 449 | 449 | return print_r($var, true); |
| 450 | 450 | } |
@@ -586,15 +586,15 @@ discard block |
||
| 586 | 586 | } |
| 587 | 587 | |
| 588 | 588 | /** |
| 589 | - * This function is very useful, it allows to recover the instance of the global controller. |
|
| 590 | - * Note this function always returns the address of the super instance. |
|
| 591 | - * For example : |
|
| 592 | - * $obj = & get_instance(); |
|
| 593 | - * |
|
| 594 | - * @codeCoverageIgnore |
|
| 595 | - * |
|
| 596 | - * @return object the instance of the "Controller" class |
|
| 597 | - */ |
|
| 589 | + * This function is very useful, it allows to recover the instance of the global controller. |
|
| 590 | + * Note this function always returns the address of the super instance. |
|
| 591 | + * For example : |
|
| 592 | + * $obj = & get_instance(); |
|
| 593 | + * |
|
| 594 | + * @codeCoverageIgnore |
|
| 595 | + * |
|
| 596 | + * @return object the instance of the "Controller" class |
|
| 597 | + */ |
|
| 598 | 598 | function & get_instance(){ |
| 599 | 599 | return Controller::get_instance(); |
| 600 | 600 | } |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | * You should have received a copy of the GNU General Public License |
| 23 | 23 | * along with this program; if not, write to the Free Software |
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | - */ |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | 27 | /** |
| 28 | 28 | * check if the interface "SessionHandlerInterface" exists (normally in PHP 5.4 this already exists) |
@@ -76,27 +76,27 @@ discard block |
||
| 76 | 76 | private $logger; |
| 77 | 77 | |
| 78 | 78 | /** |
| 79 | - * Instance of the Loader class |
|
| 80 | - * @var Loader |
|
| 81 | - */ |
|
| 82 | - protected $loader = null; |
|
| 79 | + * Instance of the Loader class |
|
| 80 | + * @var Loader |
|
| 81 | + */ |
|
| 82 | + protected $loader = null; |
|
| 83 | 83 | |
| 84 | 84 | public function __construct(DBSessionHandlerModel $modelInstance = null, Log $logger = null, Loader $loader = null){ |
| 85 | 85 | /** |
| 86 | - * instance of the Log class |
|
| 87 | - */ |
|
| 88 | - if(is_object($logger)){ |
|
| 89 | - $this->setLogger($logger); |
|
| 90 | - } |
|
| 91 | - else{ |
|
| 92 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 93 | - $this->logger->setLogger('Library::DBSessionHandler'); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - if(is_object($loader)){ |
|
| 97 | - $this->setLoader($loader); |
|
| 98 | - } |
|
| 99 | - $this->OBJ = & get_instance(); |
|
| 86 | + * instance of the Log class |
|
| 87 | + */ |
|
| 88 | + if(is_object($logger)){ |
|
| 89 | + $this->setLogger($logger); |
|
| 90 | + } |
|
| 91 | + else{ |
|
| 92 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 93 | + $this->logger->setLogger('Library::DBSessionHandler'); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + if(is_object($loader)){ |
|
| 97 | + $this->setLoader($loader); |
|
| 98 | + } |
|
| 99 | + $this->OBJ = & get_instance(); |
|
| 100 | 100 | |
| 101 | 101 | |
| 102 | 102 | if(is_object($modelInstance)){ |
@@ -199,9 +199,9 @@ discard block |
||
| 199 | 199 | $this->getLoader()->library('Browser'); |
| 200 | 200 | } |
| 201 | 201 | else{ |
| 202 | - Loader::functions('user_agent'); |
|
| 203 | - Loader::library('Browser'); |
|
| 204 | - } |
|
| 202 | + Loader::functions('user_agent'); |
|
| 203 | + Loader::library('Browser'); |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | 206 | $ip = get_ip(); |
| 207 | 207 | $keyValue = $instance->getKeyValue(); |
@@ -239,9 +239,9 @@ discard block |
||
| 239 | 239 | $this->getLoader()->library('Browser'); |
| 240 | 240 | } |
| 241 | 241 | else{ |
| 242 | - Loader::functions('user_agent'); |
|
| 243 | - Loader::library('Browser'); |
|
| 244 | - } |
|
| 242 | + Loader::functions('user_agent'); |
|
| 243 | + Loader::library('Browser'); |
|
| 244 | + } |
|
| 245 | 245 | |
| 246 | 246 | $ip = get_ip(); |
| 247 | 247 | $keyValue = $instance->getKeyValue(); |
@@ -325,75 +325,75 @@ discard block |
||
| 325 | 325 | |
| 326 | 326 | |
| 327 | 327 | /** |
| 328 | - * Return the loader instance |
|
| 329 | - * @return Loader the loader instance |
|
| 330 | - */ |
|
| 331 | - public function getLoader(){ |
|
| 332 | - return $this->loader; |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * set the loader instance for future use |
|
| 337 | - * @param Loader $loader the loader object |
|
| 338 | - */ |
|
| 339 | - public function setLoader($loader){ |
|
| 340 | - $this->loader = $loader; |
|
| 341 | - return $this; |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * Return the model instance |
|
| 346 | - * @return DBSessionHandlerModel the model instance |
|
| 347 | - */ |
|
| 348 | - public function getModelInstance(){ |
|
| 349 | - return $this->modelInstanceName; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * set the model instance for future use |
|
| 354 | - * @param DBSessionHandlerModel $modelInstance the model object |
|
| 355 | - */ |
|
| 356 | - public function setModelInstance(DBSessionHandlerModel $modelInstance){ |
|
| 357 | - $this->modelInstanceName = $modelInstance; |
|
| 358 | - return $this; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * Return the Log instance |
|
| 363 | - * @return Log |
|
| 364 | - */ |
|
| 365 | - public function getLogger(){ |
|
| 366 | - return $this->logger; |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * Set the log instance |
|
| 371 | - * @param Log $logger the log object |
|
| 372 | - */ |
|
| 373 | - public function setLogger(Log $logger){ |
|
| 374 | - $this->logger = $logger; |
|
| 375 | - return $this; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * Set the model instance using the configuration for session |
|
| 380 | - */ |
|
| 381 | - private function setModelInstanceFromConfig(){ |
|
| 382 | - $modelName = get_config('session_save_path'); |
|
| 328 | + * Return the loader instance |
|
| 329 | + * @return Loader the loader instance |
|
| 330 | + */ |
|
| 331 | + public function getLoader(){ |
|
| 332 | + return $this->loader; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * set the loader instance for future use |
|
| 337 | + * @param Loader $loader the loader object |
|
| 338 | + */ |
|
| 339 | + public function setLoader($loader){ |
|
| 340 | + $this->loader = $loader; |
|
| 341 | + return $this; |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * Return the model instance |
|
| 346 | + * @return DBSessionHandlerModel the model instance |
|
| 347 | + */ |
|
| 348 | + public function getModelInstance(){ |
|
| 349 | + return $this->modelInstanceName; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * set the model instance for future use |
|
| 354 | + * @param DBSessionHandlerModel $modelInstance the model object |
|
| 355 | + */ |
|
| 356 | + public function setModelInstance(DBSessionHandlerModel $modelInstance){ |
|
| 357 | + $this->modelInstanceName = $modelInstance; |
|
| 358 | + return $this; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * Return the Log instance |
|
| 363 | + * @return Log |
|
| 364 | + */ |
|
| 365 | + public function getLogger(){ |
|
| 366 | + return $this->logger; |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * Set the log instance |
|
| 371 | + * @param Log $logger the log object |
|
| 372 | + */ |
|
| 373 | + public function setLogger(Log $logger){ |
|
| 374 | + $this->logger = $logger; |
|
| 375 | + return $this; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * Set the model instance using the configuration for session |
|
| 380 | + */ |
|
| 381 | + private function setModelInstanceFromConfig(){ |
|
| 382 | + $modelName = get_config('session_save_path'); |
|
| 383 | 383 | $this->logger->info('The database session model: ' . $modelName); |
| 384 | 384 | if($this->getLoader()){ |
| 385 | 385 | $this->getLoader()->model($modelName, 'dbsessionhandlerinstance'); |
| 386 | 386 | } |
| 387 | 387 | //@codeCoverageIgnoreStart |
| 388 | 388 | else{ |
| 389 | - Loader::model($modelName, 'dbsessionhandlerinstance'); |
|
| 390 | - } |
|
| 391 | - if(isset($this->OBJ->dbsessionhandlerinstance) && ! $this->OBJ->dbsessionhandlerinstance instanceof DBSessionHandlerModel){ |
|
| 389 | + Loader::model($modelName, 'dbsessionhandlerinstance'); |
|
| 390 | + } |
|
| 391 | + if(isset($this->OBJ->dbsessionhandlerinstance) && ! $this->OBJ->dbsessionhandlerinstance instanceof DBSessionHandlerModel){ |
|
| 392 | 392 | show_error('To use database session handler, your class model "'.get_class($this->OBJ->dbsessionhandlerinstance).'" need extends "DBSessionHandlerModel"'); |
| 393 | 393 | } |
| 394 | 394 | //@codeCoverageIgnoreEnd |
| 395 | 395 | |
| 396 | 396 | //set model instance |
| 397 | 397 | $this->setModelInstance($this->OBJ->dbsessionhandlerinstance); |
| 398 | - } |
|
| 398 | + } |
|
| 399 | 399 | } |
@@ -1,595 +1,595 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 26 | - |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * A base model with a series of CRUD functions (powered by CI's query builder), |
|
| 30 | - * validation-in-model support, event callbacks and more. |
|
| 31 | - * |
|
| 32 | - * @link http://github.com/jamierumbelow/codeigniter-base-model |
|
| 33 | - * @copyright Copyright (c) 2012, Jamie Rumbelow <http://jamierumbelow.net> |
|
| 34 | - */ |
|
| 35 | - |
|
| 36 | - class Model{ |
|
| 37 | - |
|
| 38 | - /* -------------------------------------------------------------- |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | + |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * A base model with a series of CRUD functions (powered by CI's query builder), |
|
| 30 | + * validation-in-model support, event callbacks and more. |
|
| 31 | + * |
|
| 32 | + * @link http://github.com/jamierumbelow/codeigniter-base-model |
|
| 33 | + * @copyright Copyright (c) 2012, Jamie Rumbelow <http://jamierumbelow.net> |
|
| 34 | + */ |
|
| 35 | + |
|
| 36 | + class Model{ |
|
| 37 | + |
|
| 38 | + /* -------------------------------------------------------------- |
|
| 39 | 39 | * VARIABLES |
| 40 | 40 | * ------------------------------------------------------------ */ |
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * This model's default database table. Automatically |
|
| 44 | - * guessed by pluralising the model name. |
|
| 45 | - */ |
|
| 46 | - protected $_table; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * The database connection object. Will be set to the default |
|
| 50 | - * connection. This allows individual models to use different DBs |
|
| 51 | - * without overwriting the global database connection. |
|
| 52 | - */ |
|
| 53 | - protected $_database; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * This model's default primary key or unique identifier. |
|
| 57 | - * Used by the get(), update() and delete() functions. |
|
| 58 | - */ |
|
| 59 | - protected $primary_key = 'id'; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Support for soft deletes and this model's 'deleted' key |
|
| 63 | - */ |
|
| 64 | - protected $soft_delete = false; |
|
| 65 | - protected $soft_delete_key = 'is_deleted'; |
|
| 66 | - protected $_temporary_with_deleted = FALSE; |
|
| 67 | - protected $_temporary_only_deleted = FALSE; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * The various callbacks available to the model. Each are |
|
| 71 | - * simple lists of method names (methods will be run on $this). |
|
| 72 | - */ |
|
| 73 | - protected $before_create = array(); |
|
| 74 | - protected $after_create = array(); |
|
| 75 | - protected $before_update = array(); |
|
| 76 | - protected $after_update = array(); |
|
| 77 | - protected $before_get = array(); |
|
| 78 | - protected $after_get = array(); |
|
| 79 | - protected $before_delete = array(); |
|
| 80 | - protected $after_delete = array(); |
|
| 81 | - |
|
| 82 | - protected $callback_parameters = array(); |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Protected, non-modifiable attributes |
|
| 86 | - */ |
|
| 87 | - protected $protected_attributes = array(); |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Relationship arrays. Use flat strings for defaults or string |
|
| 91 | - * => array to customise the class name and primary key |
|
| 92 | - */ |
|
| 93 | - protected $belongs_to = array(); |
|
| 94 | - protected $has_many = array(); |
|
| 95 | - |
|
| 96 | - protected $_with = array(); |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * An array of validation rules. This needs to be the same format |
|
| 100 | - * as validation rules passed to the FormValidation library. |
|
| 101 | - */ |
|
| 102 | - protected $validate = array(); |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Optionally skip the validation. Used in conjunction with |
|
| 106 | - * skip_validation() to skip data validation for any future calls. |
|
| 107 | - */ |
|
| 108 | - protected $skip_validation = FALSE; |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * By default we return our results as objects. If we need to override |
|
| 112 | - * this, we can, or, we could use the `as_array()` and `as_object()` scopes. |
|
| 113 | - */ |
|
| 114 | - protected $return_type = 'object'; |
|
| 115 | - protected $_temporary_return_type = NULL; |
|
| 42 | + /** |
|
| 43 | + * This model's default database table. Automatically |
|
| 44 | + * guessed by pluralising the model name. |
|
| 45 | + */ |
|
| 46 | + protected $_table; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * The database connection object. Will be set to the default |
|
| 50 | + * connection. This allows individual models to use different DBs |
|
| 51 | + * without overwriting the global database connection. |
|
| 52 | + */ |
|
| 53 | + protected $_database; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * This model's default primary key or unique identifier. |
|
| 57 | + * Used by the get(), update() and delete() functions. |
|
| 58 | + */ |
|
| 59 | + protected $primary_key = 'id'; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Support for soft deletes and this model's 'deleted' key |
|
| 63 | + */ |
|
| 64 | + protected $soft_delete = false; |
|
| 65 | + protected $soft_delete_key = 'is_deleted'; |
|
| 66 | + protected $_temporary_with_deleted = FALSE; |
|
| 67 | + protected $_temporary_only_deleted = FALSE; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * The various callbacks available to the model. Each are |
|
| 71 | + * simple lists of method names (methods will be run on $this). |
|
| 72 | + */ |
|
| 73 | + protected $before_create = array(); |
|
| 74 | + protected $after_create = array(); |
|
| 75 | + protected $before_update = array(); |
|
| 76 | + protected $after_update = array(); |
|
| 77 | + protected $before_get = array(); |
|
| 78 | + protected $after_get = array(); |
|
| 79 | + protected $before_delete = array(); |
|
| 80 | + protected $after_delete = array(); |
|
| 81 | + |
|
| 82 | + protected $callback_parameters = array(); |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Protected, non-modifiable attributes |
|
| 86 | + */ |
|
| 87 | + protected $protected_attributes = array(); |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Relationship arrays. Use flat strings for defaults or string |
|
| 91 | + * => array to customise the class name and primary key |
|
| 92 | + */ |
|
| 93 | + protected $belongs_to = array(); |
|
| 94 | + protected $has_many = array(); |
|
| 95 | + |
|
| 96 | + protected $_with = array(); |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * An array of validation rules. This needs to be the same format |
|
| 100 | + * as validation rules passed to the FormValidation library. |
|
| 101 | + */ |
|
| 102 | + protected $validate = array(); |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Optionally skip the validation. Used in conjunction with |
|
| 106 | + * skip_validation() to skip data validation for any future calls. |
|
| 107 | + */ |
|
| 108 | + protected $skip_validation = FALSE; |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * By default we return our results as objects. If we need to override |
|
| 112 | + * this, we can, or, we could use the `as_array()` and `as_object()` scopes. |
|
| 113 | + */ |
|
| 114 | + protected $return_type = 'object'; |
|
| 115 | + protected $_temporary_return_type = NULL; |
|
| 116 | 116 | |
| 117 | 117 | |
| 118 | - /** |
|
| 118 | + /** |
|
| 119 | 119 | The database cache time |
| 120 | - */ |
|
| 121 | - protected $dbCacheTime = 0; |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Instance of the Loader class |
|
| 125 | - * @var Loader |
|
| 126 | - */ |
|
| 127 | - protected $loaderInstance = null; |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Instance of the FormValidation library |
|
| 131 | - * @var FormValidation |
|
| 132 | - */ |
|
| 133 | - protected $formValidationInstance = null; |
|
| 134 | - |
|
| 135 | - /* -------------------------------------------------------------- |
|
| 120 | + */ |
|
| 121 | + protected $dbCacheTime = 0; |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Instance of the Loader class |
|
| 125 | + * @var Loader |
|
| 126 | + */ |
|
| 127 | + protected $loaderInstance = null; |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Instance of the FormValidation library |
|
| 131 | + * @var FormValidation |
|
| 132 | + */ |
|
| 133 | + protected $formValidationInstance = null; |
|
| 134 | + |
|
| 135 | + /* -------------------------------------------------------------- |
|
| 136 | 136 | * GENERIC METHODS |
| 137 | 137 | * ------------------------------------------------------------ */ |
| 138 | 138 | |
| 139 | - /** |
|
| 140 | - * Initialise the model, tie into the CodeIgniter superobject and |
|
| 141 | - * try our best to guess the table name. |
|
| 142 | - */ |
|
| 143 | - public function __construct(Database $db = null){ |
|
| 144 | - if(is_object($db)){ |
|
| 145 | - $this->setDatabaseInstance($db); |
|
| 146 | - } |
|
| 147 | - else{ |
|
| 148 | - $obj = & get_instance(); |
|
| 149 | - if(isset($obj->database) && is_object($obj->database)){ |
|
| 150 | - /** |
|
| 151 | - * NOTE: Need use "clone" because some Model need have the personal instance of the database library |
|
| 152 | - * to prevent duplication |
|
| 153 | - */ |
|
| 154 | - $this->setDatabaseInstance(clone $obj->database); |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - array_unshift($this->before_create, 'protect_attributes'); |
|
| 159 | - array_unshift($this->before_update, 'protect_attributes'); |
|
| 160 | - $this->_temporary_return_type = $this->return_type; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /* -------------------------------------------------------------- |
|
| 139 | + /** |
|
| 140 | + * Initialise the model, tie into the CodeIgniter superobject and |
|
| 141 | + * try our best to guess the table name. |
|
| 142 | + */ |
|
| 143 | + public function __construct(Database $db = null){ |
|
| 144 | + if(is_object($db)){ |
|
| 145 | + $this->setDatabaseInstance($db); |
|
| 146 | + } |
|
| 147 | + else{ |
|
| 148 | + $obj = & get_instance(); |
|
| 149 | + if(isset($obj->database) && is_object($obj->database)){ |
|
| 150 | + /** |
|
| 151 | + * NOTE: Need use "clone" because some Model need have the personal instance of the database library |
|
| 152 | + * to prevent duplication |
|
| 153 | + */ |
|
| 154 | + $this->setDatabaseInstance(clone $obj->database); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + array_unshift($this->before_create, 'protect_attributes'); |
|
| 159 | + array_unshift($this->before_update, 'protect_attributes'); |
|
| 160 | + $this->_temporary_return_type = $this->return_type; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /* -------------------------------------------------------------- |
|
| 164 | 164 | * CRUD INTERFACE |
| 165 | 165 | * ------------------------------------------------------------ */ |
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * Fetch a single record based on the primary key. Returns an object. |
|
| 169 | - */ |
|
| 170 | - public function get($primary_value) |
|
| 171 | - { |
|
| 172 | - return $this->get_by($this->primary_key, $primary_value); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Fetch a single record based on an arbitrary WHERE call. Can be |
|
| 177 | - * any valid value to $this->_database->where(). |
|
| 178 | - */ |
|
| 179 | - public function get_by() |
|
| 180 | - { |
|
| 181 | - $where = func_get_args(); |
|
| 182 | - |
|
| 183 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 184 | - { |
|
| 185 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - $this->_set_where($where); |
|
| 189 | - |
|
| 190 | - $this->trigger('before_get'); |
|
| 167 | + /** |
|
| 168 | + * Fetch a single record based on the primary key. Returns an object. |
|
| 169 | + */ |
|
| 170 | + public function get($primary_value) |
|
| 171 | + { |
|
| 172 | + return $this->get_by($this->primary_key, $primary_value); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Fetch a single record based on an arbitrary WHERE call. Can be |
|
| 177 | + * any valid value to $this->_database->where(). |
|
| 178 | + */ |
|
| 179 | + public function get_by() |
|
| 180 | + { |
|
| 181 | + $where = func_get_args(); |
|
| 182 | + |
|
| 183 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 184 | + { |
|
| 185 | + $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + $this->_set_where($where); |
|
| 189 | + |
|
| 190 | + $this->trigger('before_get'); |
|
| 191 | 191 | $type = $this->_temporary_return_type == 'array' ? 'array':false; |
| 192 | - $row = $this->_database->from($this->_table)->get($type); |
|
| 193 | - $this->_temporary_return_type = $this->return_type; |
|
| 194 | - $row = $this->trigger('after_get', $row); |
|
| 195 | - $this->_with = array(); |
|
| 196 | - return $row; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Fetch an array of records based on an array of primary values. |
|
| 201 | - */ |
|
| 202 | - public function get_many($values) |
|
| 203 | - { |
|
| 204 | - $this->_database->in($this->primary_key, $values); |
|
| 205 | - return $this->get_all(); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * Fetch an array of records based on an arbitrary WHERE call. |
|
| 210 | - */ |
|
| 211 | - public function get_many_by() |
|
| 212 | - { |
|
| 213 | - $where = func_get_args(); |
|
| 214 | - $this->_set_where($where); |
|
| 215 | - return $this->get_all(); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Fetch all the records in the table. Can be used as a generic call |
|
| 220 | - * to $this->_database->get() with scoped methods. |
|
| 221 | - */ |
|
| 222 | - public function get_all() |
|
| 223 | - { |
|
| 224 | - $this->trigger('before_get'); |
|
| 225 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 226 | - { |
|
| 227 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 228 | - } |
|
| 192 | + $row = $this->_database->from($this->_table)->get($type); |
|
| 193 | + $this->_temporary_return_type = $this->return_type; |
|
| 194 | + $row = $this->trigger('after_get', $row); |
|
| 195 | + $this->_with = array(); |
|
| 196 | + return $row; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Fetch an array of records based on an array of primary values. |
|
| 201 | + */ |
|
| 202 | + public function get_many($values) |
|
| 203 | + { |
|
| 204 | + $this->_database->in($this->primary_key, $values); |
|
| 205 | + return $this->get_all(); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * Fetch an array of records based on an arbitrary WHERE call. |
|
| 210 | + */ |
|
| 211 | + public function get_many_by() |
|
| 212 | + { |
|
| 213 | + $where = func_get_args(); |
|
| 214 | + $this->_set_where($where); |
|
| 215 | + return $this->get_all(); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Fetch all the records in the table. Can be used as a generic call |
|
| 220 | + * to $this->_database->get() with scoped methods. |
|
| 221 | + */ |
|
| 222 | + public function get_all() |
|
| 223 | + { |
|
| 224 | + $this->trigger('before_get'); |
|
| 225 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 226 | + { |
|
| 227 | + $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 228 | + } |
|
| 229 | 229 | $type = $this->_temporary_return_type == 'array' ? 'array':false; |
| 230 | - $result = $this->_database->from($this->_table)->getAll($type); |
|
| 231 | - $this->_temporary_return_type = $this->return_type; |
|
| 232 | - |
|
| 233 | - foreach ($result as $key => &$row) |
|
| 234 | - { |
|
| 235 | - $row = $this->trigger('after_get', $row, ($key == count($result) - 1)); |
|
| 236 | - } |
|
| 237 | - $this->_with = array(); |
|
| 238 | - return $result; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Insert a new row into the table. $data should be an associative array |
|
| 243 | - * of data to be inserted. Returns newly created ID. |
|
| 244 | - */ |
|
| 245 | - public function insert($data = array(), $skip_validation = FALSE, $escape = true) |
|
| 246 | - { |
|
| 247 | - if ($skip_validation === FALSE) |
|
| 248 | - { |
|
| 249 | - $data = $this->validate($data); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - if ($data !== FALSE) |
|
| 253 | - { |
|
| 254 | - $data = $this->trigger('before_create', $data); |
|
| 255 | - $this->_database->from($this->_table)->insert($data, $escape); |
|
| 256 | - $insert_id = $this->_database->insertId(); |
|
| 257 | - $this->trigger('after_create', $insert_id); |
|
| 258 | - return $insert_id; |
|
| 259 | - } |
|
| 260 | - else |
|
| 261 | - { |
|
| 262 | - return FALSE; |
|
| 263 | - } |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * Insert multiple rows into the table. Returns an array of multiple IDs. |
|
| 268 | - */ |
|
| 269 | - public function insert_many($data = array(), $skip_validation = FALSE, $escape = true) |
|
| 270 | - { |
|
| 271 | - $ids = array(); |
|
| 272 | - foreach ($data as $key => $row) |
|
| 273 | - { |
|
| 274 | - $ids[] = $this->insert($row, $skip_validation, $escape); |
|
| 275 | - } |
|
| 276 | - return $ids; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * Updated a record based on the primary value. |
|
| 281 | - */ |
|
| 282 | - public function update($primary_value, $data = array(), $skip_validation = FALSE, $escape = true) |
|
| 283 | - { |
|
| 284 | - $data = $this->trigger('before_update', $data); |
|
| 285 | - if ($skip_validation === FALSE) |
|
| 286 | - { |
|
| 287 | - $data = $this->validate($data); |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - if ($data !== FALSE) |
|
| 291 | - { |
|
| 292 | - $result = $this->_database->where($this->primary_key, $primary_value) |
|
| 293 | - ->from($this->_table) |
|
| 294 | - ->update($data, $escape); |
|
| 295 | - $this->trigger('after_update', array($data, $result)); |
|
| 296 | - return $result; |
|
| 297 | - } |
|
| 298 | - else |
|
| 299 | - { |
|
| 300 | - return FALSE; |
|
| 301 | - } |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * Update many records, based on an array of primary values. |
|
| 306 | - */ |
|
| 307 | - public function update_many($primary_values, $data = array(), $skip_validation = FALSE, $escape = true) |
|
| 308 | - { |
|
| 309 | - $data = $this->trigger('before_update', $data); |
|
| 310 | - if ($skip_validation === FALSE) |
|
| 311 | - { |
|
| 312 | - $data = $this->validate($data); |
|
| 313 | - } |
|
| 314 | - if ($data !== FALSE) |
|
| 315 | - { |
|
| 316 | - $result = $this->_database->in($this->primary_key, $primary_values) |
|
| 317 | - ->from($this->_table) |
|
| 318 | - ->update($data, $escape); |
|
| 319 | - $this->trigger('after_update', array($data, $result)); |
|
| 320 | - return $result; |
|
| 321 | - } |
|
| 322 | - else |
|
| 323 | - { |
|
| 324 | - return FALSE; |
|
| 325 | - } |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * Updated a record based on an arbitrary WHERE clause. |
|
| 330 | - */ |
|
| 331 | - public function update_by() |
|
| 332 | - { |
|
| 333 | - $args = func_get_args(); |
|
| 334 | - $data = array(); |
|
| 335 | - if(count($args) == 2){ |
|
| 336 | - if(is_array($args[1])){ |
|
| 337 | - $data = array_pop($args); |
|
| 338 | - } |
|
| 339 | - } |
|
| 340 | - else if(count($args) == 3){ |
|
| 341 | - if(is_array($args[2])){ |
|
| 342 | - $data = array_pop($args); |
|
| 343 | - } |
|
| 344 | - } |
|
| 345 | - $data = $this->trigger('before_update', $data); |
|
| 346 | - if ($this->validate($data) !== FALSE) |
|
| 347 | - { |
|
| 348 | - $this->_set_where($args); |
|
| 349 | - $result = $this->_database->from($this->_table)->update($data); |
|
| 350 | - $this->trigger('after_update', array($data, $result)); |
|
| 351 | - return $result; |
|
| 352 | - } |
|
| 353 | - else |
|
| 354 | - { |
|
| 355 | - return FALSE; |
|
| 356 | - } |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * Update all records |
|
| 361 | - */ |
|
| 362 | - public function update_all($data = array(), $escape = true) |
|
| 363 | - { |
|
| 364 | - $data = $this->trigger('before_update', $data); |
|
| 365 | - $result = $this->_database->from($this->_table)->update($data, $escape); |
|
| 366 | - $this->trigger('after_update', array($data, $result)); |
|
| 367 | - return $result; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * Delete a row from the table by the primary value |
|
| 372 | - */ |
|
| 373 | - public function delete($id) |
|
| 374 | - { |
|
| 375 | - $this->trigger('before_delete', $id); |
|
| 376 | - $this->_database->where($this->primary_key, $id); |
|
| 377 | - if ($this->soft_delete) |
|
| 378 | - { |
|
| 379 | - $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
| 380 | - } |
|
| 381 | - else |
|
| 382 | - { |
|
| 383 | - $result = $this->_database->from($this->_table)->delete(); |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - $this->trigger('after_delete', $result); |
|
| 387 | - return $result; |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - /** |
|
| 391 | - * Delete a row from the database table by an arbitrary WHERE clause |
|
| 392 | - */ |
|
| 393 | - public function delete_by() |
|
| 394 | - { |
|
| 395 | - $where = func_get_args(); |
|
| 396 | - $where = $this->trigger('before_delete', $where); |
|
| 397 | - $this->_set_where($where); |
|
| 398 | - if ($this->soft_delete) |
|
| 399 | - { |
|
| 400 | - $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
| 401 | - } |
|
| 402 | - else |
|
| 403 | - { |
|
| 404 | - $result = $this->_database->from($this->_table)->delete(); |
|
| 405 | - } |
|
| 406 | - $this->trigger('after_delete', $result); |
|
| 407 | - return $result; |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - /** |
|
| 411 | - * Delete many rows from the database table by multiple primary values |
|
| 412 | - */ |
|
| 413 | - public function delete_many($primary_values) |
|
| 414 | - { |
|
| 415 | - $primary_values = $this->trigger('before_delete', $primary_values); |
|
| 416 | - $this->_database->in($this->primary_key, $primary_values); |
|
| 417 | - if ($this->soft_delete) |
|
| 418 | - { |
|
| 419 | - $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
| 420 | - } |
|
| 421 | - else |
|
| 422 | - { |
|
| 423 | - $result = $this->_database->from($this->_table)->delete(); |
|
| 424 | - } |
|
| 425 | - $this->trigger('after_delete', $result); |
|
| 426 | - return $result; |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - |
|
| 430 | - /** |
|
| 431 | - * Truncates the table |
|
| 432 | - */ |
|
| 433 | - public function truncate() |
|
| 434 | - { |
|
| 435 | - $result = $this->_database->from($this->_table)->delete(); |
|
| 436 | - return $result; |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - /* -------------------------------------------------------------- |
|
| 230 | + $result = $this->_database->from($this->_table)->getAll($type); |
|
| 231 | + $this->_temporary_return_type = $this->return_type; |
|
| 232 | + |
|
| 233 | + foreach ($result as $key => &$row) |
|
| 234 | + { |
|
| 235 | + $row = $this->trigger('after_get', $row, ($key == count($result) - 1)); |
|
| 236 | + } |
|
| 237 | + $this->_with = array(); |
|
| 238 | + return $result; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Insert a new row into the table. $data should be an associative array |
|
| 243 | + * of data to be inserted. Returns newly created ID. |
|
| 244 | + */ |
|
| 245 | + public function insert($data = array(), $skip_validation = FALSE, $escape = true) |
|
| 246 | + { |
|
| 247 | + if ($skip_validation === FALSE) |
|
| 248 | + { |
|
| 249 | + $data = $this->validate($data); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + if ($data !== FALSE) |
|
| 253 | + { |
|
| 254 | + $data = $this->trigger('before_create', $data); |
|
| 255 | + $this->_database->from($this->_table)->insert($data, $escape); |
|
| 256 | + $insert_id = $this->_database->insertId(); |
|
| 257 | + $this->trigger('after_create', $insert_id); |
|
| 258 | + return $insert_id; |
|
| 259 | + } |
|
| 260 | + else |
|
| 261 | + { |
|
| 262 | + return FALSE; |
|
| 263 | + } |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * Insert multiple rows into the table. Returns an array of multiple IDs. |
|
| 268 | + */ |
|
| 269 | + public function insert_many($data = array(), $skip_validation = FALSE, $escape = true) |
|
| 270 | + { |
|
| 271 | + $ids = array(); |
|
| 272 | + foreach ($data as $key => $row) |
|
| 273 | + { |
|
| 274 | + $ids[] = $this->insert($row, $skip_validation, $escape); |
|
| 275 | + } |
|
| 276 | + return $ids; |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * Updated a record based on the primary value. |
|
| 281 | + */ |
|
| 282 | + public function update($primary_value, $data = array(), $skip_validation = FALSE, $escape = true) |
|
| 283 | + { |
|
| 284 | + $data = $this->trigger('before_update', $data); |
|
| 285 | + if ($skip_validation === FALSE) |
|
| 286 | + { |
|
| 287 | + $data = $this->validate($data); |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + if ($data !== FALSE) |
|
| 291 | + { |
|
| 292 | + $result = $this->_database->where($this->primary_key, $primary_value) |
|
| 293 | + ->from($this->_table) |
|
| 294 | + ->update($data, $escape); |
|
| 295 | + $this->trigger('after_update', array($data, $result)); |
|
| 296 | + return $result; |
|
| 297 | + } |
|
| 298 | + else |
|
| 299 | + { |
|
| 300 | + return FALSE; |
|
| 301 | + } |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * Update many records, based on an array of primary values. |
|
| 306 | + */ |
|
| 307 | + public function update_many($primary_values, $data = array(), $skip_validation = FALSE, $escape = true) |
|
| 308 | + { |
|
| 309 | + $data = $this->trigger('before_update', $data); |
|
| 310 | + if ($skip_validation === FALSE) |
|
| 311 | + { |
|
| 312 | + $data = $this->validate($data); |
|
| 313 | + } |
|
| 314 | + if ($data !== FALSE) |
|
| 315 | + { |
|
| 316 | + $result = $this->_database->in($this->primary_key, $primary_values) |
|
| 317 | + ->from($this->_table) |
|
| 318 | + ->update($data, $escape); |
|
| 319 | + $this->trigger('after_update', array($data, $result)); |
|
| 320 | + return $result; |
|
| 321 | + } |
|
| 322 | + else |
|
| 323 | + { |
|
| 324 | + return FALSE; |
|
| 325 | + } |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Updated a record based on an arbitrary WHERE clause. |
|
| 330 | + */ |
|
| 331 | + public function update_by() |
|
| 332 | + { |
|
| 333 | + $args = func_get_args(); |
|
| 334 | + $data = array(); |
|
| 335 | + if(count($args) == 2){ |
|
| 336 | + if(is_array($args[1])){ |
|
| 337 | + $data = array_pop($args); |
|
| 338 | + } |
|
| 339 | + } |
|
| 340 | + else if(count($args) == 3){ |
|
| 341 | + if(is_array($args[2])){ |
|
| 342 | + $data = array_pop($args); |
|
| 343 | + } |
|
| 344 | + } |
|
| 345 | + $data = $this->trigger('before_update', $data); |
|
| 346 | + if ($this->validate($data) !== FALSE) |
|
| 347 | + { |
|
| 348 | + $this->_set_where($args); |
|
| 349 | + $result = $this->_database->from($this->_table)->update($data); |
|
| 350 | + $this->trigger('after_update', array($data, $result)); |
|
| 351 | + return $result; |
|
| 352 | + } |
|
| 353 | + else |
|
| 354 | + { |
|
| 355 | + return FALSE; |
|
| 356 | + } |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * Update all records |
|
| 361 | + */ |
|
| 362 | + public function update_all($data = array(), $escape = true) |
|
| 363 | + { |
|
| 364 | + $data = $this->trigger('before_update', $data); |
|
| 365 | + $result = $this->_database->from($this->_table)->update($data, $escape); |
|
| 366 | + $this->trigger('after_update', array($data, $result)); |
|
| 367 | + return $result; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * Delete a row from the table by the primary value |
|
| 372 | + */ |
|
| 373 | + public function delete($id) |
|
| 374 | + { |
|
| 375 | + $this->trigger('before_delete', $id); |
|
| 376 | + $this->_database->where($this->primary_key, $id); |
|
| 377 | + if ($this->soft_delete) |
|
| 378 | + { |
|
| 379 | + $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
| 380 | + } |
|
| 381 | + else |
|
| 382 | + { |
|
| 383 | + $result = $this->_database->from($this->_table)->delete(); |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + $this->trigger('after_delete', $result); |
|
| 387 | + return $result; |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + /** |
|
| 391 | + * Delete a row from the database table by an arbitrary WHERE clause |
|
| 392 | + */ |
|
| 393 | + public function delete_by() |
|
| 394 | + { |
|
| 395 | + $where = func_get_args(); |
|
| 396 | + $where = $this->trigger('before_delete', $where); |
|
| 397 | + $this->_set_where($where); |
|
| 398 | + if ($this->soft_delete) |
|
| 399 | + { |
|
| 400 | + $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
| 401 | + } |
|
| 402 | + else |
|
| 403 | + { |
|
| 404 | + $result = $this->_database->from($this->_table)->delete(); |
|
| 405 | + } |
|
| 406 | + $this->trigger('after_delete', $result); |
|
| 407 | + return $result; |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + /** |
|
| 411 | + * Delete many rows from the database table by multiple primary values |
|
| 412 | + */ |
|
| 413 | + public function delete_many($primary_values) |
|
| 414 | + { |
|
| 415 | + $primary_values = $this->trigger('before_delete', $primary_values); |
|
| 416 | + $this->_database->in($this->primary_key, $primary_values); |
|
| 417 | + if ($this->soft_delete) |
|
| 418 | + { |
|
| 419 | + $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
| 420 | + } |
|
| 421 | + else |
|
| 422 | + { |
|
| 423 | + $result = $this->_database->from($this->_table)->delete(); |
|
| 424 | + } |
|
| 425 | + $this->trigger('after_delete', $result); |
|
| 426 | + return $result; |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + |
|
| 430 | + /** |
|
| 431 | + * Truncates the table |
|
| 432 | + */ |
|
| 433 | + public function truncate() |
|
| 434 | + { |
|
| 435 | + $result = $this->_database->from($this->_table)->delete(); |
|
| 436 | + return $result; |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + /* -------------------------------------------------------------- |
|
| 440 | 440 | * RELATIONSHIPS |
| 441 | 441 | * ------------------------------------------------------------ */ |
| 442 | 442 | |
| 443 | - public function with($relationship) |
|
| 444 | - { |
|
| 445 | - $this->_with[] = $relationship; |
|
| 446 | - if (!in_array('relate', $this->after_get)) |
|
| 447 | - { |
|
| 448 | - $this->after_get[] = 'relate'; |
|
| 449 | - } |
|
| 450 | - return $this; |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - public function relate($row) |
|
| 454 | - { |
|
| 455 | - if (empty($row)) |
|
| 456 | - { |
|
| 457 | - return $row; |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - foreach ($this->belongs_to as $key => $value) |
|
| 461 | - { |
|
| 462 | - if (is_string($value)) |
|
| 463 | - { |
|
| 464 | - $relationship = $value; |
|
| 465 | - $options = array( 'primary_key' => $value . '_id', 'model' => $value . '_model' ); |
|
| 466 | - } |
|
| 467 | - else |
|
| 468 | - { |
|
| 469 | - $relationship = $key; |
|
| 470 | - $options = $value; |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - if (in_array($relationship, $this->_with)) |
|
| 474 | - { |
|
| 475 | - if(is_object($this->loaderInstance)){ |
|
| 476 | - $this->loaderInstance->model($options['model'], $relationship . '_model'); |
|
| 477 | - } |
|
| 478 | - else{ |
|
| 479 | - Loader::model($options['model'], $relationship . '_model'); |
|
| 480 | - } |
|
| 481 | - if (is_object($row)) |
|
| 482 | - { |
|
| 483 | - $row->{$relationship} = $this->{$relationship . '_model'}->get($row->{$options['primary_key']}); |
|
| 484 | - } |
|
| 485 | - else |
|
| 486 | - { |
|
| 487 | - $row[$relationship] = $this->{$relationship . '_model'}->get($row[$options['primary_key']]); |
|
| 488 | - } |
|
| 489 | - } |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - foreach ($this->has_many as $key => $value) |
|
| 493 | - { |
|
| 494 | - if (is_string($value)) |
|
| 495 | - { |
|
| 496 | - $relationship = $value; |
|
| 497 | - $options = array( 'primary_key' => $this->_table . '_id', 'model' => $value . '_model' ); |
|
| 498 | - } |
|
| 499 | - else |
|
| 500 | - { |
|
| 501 | - $relationship = $key; |
|
| 502 | - $options = $value; |
|
| 503 | - } |
|
| 504 | - |
|
| 505 | - if (in_array($relationship, $this->_with)) |
|
| 506 | - { |
|
| 507 | - if(is_object($this->loaderInstance)){ |
|
| 508 | - $this->loaderInstance->model($options['model'], $relationship . '_model'); |
|
| 509 | - } |
|
| 510 | - else{ |
|
| 511 | - Loader::model($options['model'], $relationship . '_model'); |
|
| 512 | - } |
|
| 513 | - if (is_object($row)) |
|
| 514 | - { |
|
| 515 | - $row->{$relationship} = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row->{$this->primary_key}); |
|
| 516 | - } |
|
| 517 | - else |
|
| 518 | - { |
|
| 519 | - $row[$relationship] = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row[$this->primary_key]); |
|
| 520 | - } |
|
| 521 | - } |
|
| 522 | - } |
|
| 523 | - return $row; |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - /* -------------------------------------------------------------- |
|
| 443 | + public function with($relationship) |
|
| 444 | + { |
|
| 445 | + $this->_with[] = $relationship; |
|
| 446 | + if (!in_array('relate', $this->after_get)) |
|
| 447 | + { |
|
| 448 | + $this->after_get[] = 'relate'; |
|
| 449 | + } |
|
| 450 | + return $this; |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + public function relate($row) |
|
| 454 | + { |
|
| 455 | + if (empty($row)) |
|
| 456 | + { |
|
| 457 | + return $row; |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + foreach ($this->belongs_to as $key => $value) |
|
| 461 | + { |
|
| 462 | + if (is_string($value)) |
|
| 463 | + { |
|
| 464 | + $relationship = $value; |
|
| 465 | + $options = array( 'primary_key' => $value . '_id', 'model' => $value . '_model' ); |
|
| 466 | + } |
|
| 467 | + else |
|
| 468 | + { |
|
| 469 | + $relationship = $key; |
|
| 470 | + $options = $value; |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + if (in_array($relationship, $this->_with)) |
|
| 474 | + { |
|
| 475 | + if(is_object($this->loaderInstance)){ |
|
| 476 | + $this->loaderInstance->model($options['model'], $relationship . '_model'); |
|
| 477 | + } |
|
| 478 | + else{ |
|
| 479 | + Loader::model($options['model'], $relationship . '_model'); |
|
| 480 | + } |
|
| 481 | + if (is_object($row)) |
|
| 482 | + { |
|
| 483 | + $row->{$relationship} = $this->{$relationship . '_model'}->get($row->{$options['primary_key']}); |
|
| 484 | + } |
|
| 485 | + else |
|
| 486 | + { |
|
| 487 | + $row[$relationship] = $this->{$relationship . '_model'}->get($row[$options['primary_key']]); |
|
| 488 | + } |
|
| 489 | + } |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + foreach ($this->has_many as $key => $value) |
|
| 493 | + { |
|
| 494 | + if (is_string($value)) |
|
| 495 | + { |
|
| 496 | + $relationship = $value; |
|
| 497 | + $options = array( 'primary_key' => $this->_table . '_id', 'model' => $value . '_model' ); |
|
| 498 | + } |
|
| 499 | + else |
|
| 500 | + { |
|
| 501 | + $relationship = $key; |
|
| 502 | + $options = $value; |
|
| 503 | + } |
|
| 504 | + |
|
| 505 | + if (in_array($relationship, $this->_with)) |
|
| 506 | + { |
|
| 507 | + if(is_object($this->loaderInstance)){ |
|
| 508 | + $this->loaderInstance->model($options['model'], $relationship . '_model'); |
|
| 509 | + } |
|
| 510 | + else{ |
|
| 511 | + Loader::model($options['model'], $relationship . '_model'); |
|
| 512 | + } |
|
| 513 | + if (is_object($row)) |
|
| 514 | + { |
|
| 515 | + $row->{$relationship} = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row->{$this->primary_key}); |
|
| 516 | + } |
|
| 517 | + else |
|
| 518 | + { |
|
| 519 | + $row[$relationship] = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row[$this->primary_key]); |
|
| 520 | + } |
|
| 521 | + } |
|
| 522 | + } |
|
| 523 | + return $row; |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + /* -------------------------------------------------------------- |
|
| 527 | 527 | * UTILITY METHODS |
| 528 | 528 | * ------------------------------------------------------------ */ |
| 529 | 529 | |
| 530 | - /** |
|
| 531 | - * Retrieve and generate a form_dropdown friendly array |
|
| 532 | - */ |
|
| 533 | - public function dropdown() |
|
| 534 | - { |
|
| 535 | - $args = func_get_args(); |
|
| 536 | - if(count($args) == 2) |
|
| 537 | - { |
|
| 538 | - list($key, $value) = $args; |
|
| 539 | - } |
|
| 540 | - else |
|
| 541 | - { |
|
| 542 | - $key = $this->primary_key; |
|
| 543 | - $value = $args[0]; |
|
| 544 | - } |
|
| 545 | - $this->trigger('before_dropdown', array( $key, $value )); |
|
| 546 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 547 | - { |
|
| 548 | - $this->_database->where($this->soft_delete_key, FALSE); |
|
| 549 | - } |
|
| 550 | - $result = $this->_database->select(array($key, $value)) |
|
| 551 | - ->from($this->_table) |
|
| 552 | - ->getAll(); |
|
| 553 | - $options = array(); |
|
| 554 | - foreach ($result as $row) |
|
| 555 | - { |
|
| 556 | - $options[$row->{$key}] = $row->{$value}; |
|
| 557 | - } |
|
| 558 | - $options = $this->trigger('after_dropdown', $options); |
|
| 559 | - return $options; |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - /** |
|
| 563 | - * Fetch a count of rows based on an arbitrary WHERE call. |
|
| 564 | - */ |
|
| 565 | - public function count_by() |
|
| 566 | - { |
|
| 567 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 568 | - { |
|
| 569 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 570 | - } |
|
| 571 | - $where = func_get_args(); |
|
| 572 | - $this->_set_where($where); |
|
| 573 | - $this->_database->from($this->_table)->getAll(); |
|
| 574 | - return $this->_database->numRows(); |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - /** |
|
| 578 | - * Fetch a total count of rows, disregarding any previous conditions |
|
| 579 | - */ |
|
| 580 | - public function count_all() |
|
| 581 | - { |
|
| 582 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 583 | - { |
|
| 584 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 585 | - } |
|
| 586 | - $this->_database->from($this->_table)->getAll(); |
|
| 587 | - return $this->_database->numRows(); |
|
| 588 | - } |
|
| 530 | + /** |
|
| 531 | + * Retrieve and generate a form_dropdown friendly array |
|
| 532 | + */ |
|
| 533 | + public function dropdown() |
|
| 534 | + { |
|
| 535 | + $args = func_get_args(); |
|
| 536 | + if(count($args) == 2) |
|
| 537 | + { |
|
| 538 | + list($key, $value) = $args; |
|
| 539 | + } |
|
| 540 | + else |
|
| 541 | + { |
|
| 542 | + $key = $this->primary_key; |
|
| 543 | + $value = $args[0]; |
|
| 544 | + } |
|
| 545 | + $this->trigger('before_dropdown', array( $key, $value )); |
|
| 546 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 547 | + { |
|
| 548 | + $this->_database->where($this->soft_delete_key, FALSE); |
|
| 549 | + } |
|
| 550 | + $result = $this->_database->select(array($key, $value)) |
|
| 551 | + ->from($this->_table) |
|
| 552 | + ->getAll(); |
|
| 553 | + $options = array(); |
|
| 554 | + foreach ($result as $row) |
|
| 555 | + { |
|
| 556 | + $options[$row->{$key}] = $row->{$value}; |
|
| 557 | + } |
|
| 558 | + $options = $this->trigger('after_dropdown', $options); |
|
| 559 | + return $options; |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + /** |
|
| 563 | + * Fetch a count of rows based on an arbitrary WHERE call. |
|
| 564 | + */ |
|
| 565 | + public function count_by() |
|
| 566 | + { |
|
| 567 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 568 | + { |
|
| 569 | + $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 570 | + } |
|
| 571 | + $where = func_get_args(); |
|
| 572 | + $this->_set_where($where); |
|
| 573 | + $this->_database->from($this->_table)->getAll(); |
|
| 574 | + return $this->_database->numRows(); |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + /** |
|
| 578 | + * Fetch a total count of rows, disregarding any previous conditions |
|
| 579 | + */ |
|
| 580 | + public function count_all() |
|
| 581 | + { |
|
| 582 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 583 | + { |
|
| 584 | + $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 585 | + } |
|
| 586 | + $this->_database->from($this->_table)->getAll(); |
|
| 587 | + return $this->_database->numRows(); |
|
| 588 | + } |
|
| 589 | 589 | |
| 590 | 590 | /** |
| 591 | - * Enabled cache temporary |
|
| 592 | - */ |
|
| 591 | + * Enabled cache temporary |
|
| 592 | + */ |
|
| 593 | 593 | public function cached($ttl = 0){ |
| 594 | 594 | if($ttl > 0){ |
| 595 | 595 | $this->_database = $this->_database->cached($ttl); |
@@ -597,390 +597,390 @@ discard block |
||
| 597 | 597 | return $this; |
| 598 | 598 | } |
| 599 | 599 | |
| 600 | - /** |
|
| 601 | - * Tell the class to skip the insert validation |
|
| 602 | - */ |
|
| 603 | - public function skip_validation() |
|
| 604 | - { |
|
| 605 | - $this->skip_validation = TRUE; |
|
| 606 | - return $this; |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - /** |
|
| 610 | - * Get the skip validation status |
|
| 611 | - */ |
|
| 612 | - public function get_skip_validation() |
|
| 613 | - { |
|
| 614 | - return $this->skip_validation; |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - /** |
|
| 618 | - * Return the next auto increment of the table. Only tested on MySQL. |
|
| 619 | - */ |
|
| 620 | - public function get_next_id() |
|
| 621 | - { |
|
| 622 | - return (int) $this->_database->select('AUTO_INCREMENT') |
|
| 623 | - ->from('information_schema.TABLES') |
|
| 624 | - ->where('TABLE_NAME', $this->_table) |
|
| 625 | - ->where('TABLE_SCHEMA', $this->_database->getDatabaseName())->get()->AUTO_INCREMENT; |
|
| 626 | - } |
|
| 627 | - |
|
| 628 | - /** |
|
| 629 | - * Getter for the table name |
|
| 630 | - */ |
|
| 631 | - public function table() |
|
| 632 | - { |
|
| 633 | - return $this->_table; |
|
| 634 | - } |
|
| 635 | - |
|
| 636 | - /* -------------------------------------------------------------- |
|
| 600 | + /** |
|
| 601 | + * Tell the class to skip the insert validation |
|
| 602 | + */ |
|
| 603 | + public function skip_validation() |
|
| 604 | + { |
|
| 605 | + $this->skip_validation = TRUE; |
|
| 606 | + return $this; |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + /** |
|
| 610 | + * Get the skip validation status |
|
| 611 | + */ |
|
| 612 | + public function get_skip_validation() |
|
| 613 | + { |
|
| 614 | + return $this->skip_validation; |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + /** |
|
| 618 | + * Return the next auto increment of the table. Only tested on MySQL. |
|
| 619 | + */ |
|
| 620 | + public function get_next_id() |
|
| 621 | + { |
|
| 622 | + return (int) $this->_database->select('AUTO_INCREMENT') |
|
| 623 | + ->from('information_schema.TABLES') |
|
| 624 | + ->where('TABLE_NAME', $this->_table) |
|
| 625 | + ->where('TABLE_SCHEMA', $this->_database->getDatabaseName())->get()->AUTO_INCREMENT; |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + /** |
|
| 629 | + * Getter for the table name |
|
| 630 | + */ |
|
| 631 | + public function table() |
|
| 632 | + { |
|
| 633 | + return $this->_table; |
|
| 634 | + } |
|
| 635 | + |
|
| 636 | + /* -------------------------------------------------------------- |
|
| 637 | 637 | * GLOBAL SCOPES |
| 638 | 638 | * ------------------------------------------------------------ */ |
| 639 | 639 | |
| 640 | - /** |
|
| 641 | - * Return the next call as an array rather than an object |
|
| 642 | - */ |
|
| 643 | - public function as_array() |
|
| 644 | - { |
|
| 645 | - $this->_temporary_return_type = 'array'; |
|
| 646 | - return $this; |
|
| 647 | - } |
|
| 648 | - |
|
| 649 | - /** |
|
| 650 | - * Return the next call as an object rather than an array |
|
| 651 | - */ |
|
| 652 | - public function as_object() |
|
| 653 | - { |
|
| 654 | - $this->_temporary_return_type = 'object'; |
|
| 655 | - return $this; |
|
| 656 | - } |
|
| 657 | - |
|
| 658 | - /** |
|
| 659 | - * Don't care about soft deleted rows on the next call |
|
| 660 | - */ |
|
| 661 | - public function with_deleted() |
|
| 662 | - { |
|
| 663 | - $this->_temporary_with_deleted = TRUE; |
|
| 664 | - return $this; |
|
| 665 | - } |
|
| 666 | - |
|
| 667 | - /** |
|
| 668 | - * Only get deleted rows on the next call |
|
| 669 | - */ |
|
| 670 | - public function only_deleted() |
|
| 671 | - { |
|
| 672 | - $this->_temporary_only_deleted = TRUE; |
|
| 673 | - return $this; |
|
| 674 | - } |
|
| 675 | - |
|
| 676 | - /* -------------------------------------------------------------- |
|
| 640 | + /** |
|
| 641 | + * Return the next call as an array rather than an object |
|
| 642 | + */ |
|
| 643 | + public function as_array() |
|
| 644 | + { |
|
| 645 | + $this->_temporary_return_type = 'array'; |
|
| 646 | + return $this; |
|
| 647 | + } |
|
| 648 | + |
|
| 649 | + /** |
|
| 650 | + * Return the next call as an object rather than an array |
|
| 651 | + */ |
|
| 652 | + public function as_object() |
|
| 653 | + { |
|
| 654 | + $this->_temporary_return_type = 'object'; |
|
| 655 | + return $this; |
|
| 656 | + } |
|
| 657 | + |
|
| 658 | + /** |
|
| 659 | + * Don't care about soft deleted rows on the next call |
|
| 660 | + */ |
|
| 661 | + public function with_deleted() |
|
| 662 | + { |
|
| 663 | + $this->_temporary_with_deleted = TRUE; |
|
| 664 | + return $this; |
|
| 665 | + } |
|
| 666 | + |
|
| 667 | + /** |
|
| 668 | + * Only get deleted rows on the next call |
|
| 669 | + */ |
|
| 670 | + public function only_deleted() |
|
| 671 | + { |
|
| 672 | + $this->_temporary_only_deleted = TRUE; |
|
| 673 | + return $this; |
|
| 674 | + } |
|
| 675 | + |
|
| 676 | + /* -------------------------------------------------------------- |
|
| 677 | 677 | * OBSERVERS |
| 678 | 678 | * ------------------------------------------------------------ */ |
| 679 | 679 | |
| 680 | - /** |
|
| 681 | - * MySQL DATETIME created_at and updated_at |
|
| 682 | - */ |
|
| 683 | - public function created_at($row) |
|
| 684 | - { |
|
| 685 | - if (is_object($row)) |
|
| 686 | - { |
|
| 687 | - $row->created_at = date('Y-m-d H:i:s'); |
|
| 688 | - } |
|
| 689 | - else |
|
| 690 | - { |
|
| 691 | - $row['created_at'] = date('Y-m-d H:i:s'); |
|
| 692 | - } |
|
| 693 | - |
|
| 694 | - return $row; |
|
| 695 | - } |
|
| 696 | - |
|
| 697 | - public function updated_at($row) |
|
| 698 | - { |
|
| 699 | - if (is_object($row)) |
|
| 700 | - { |
|
| 701 | - $row->updated_at = date('Y-m-d H:i:s'); |
|
| 702 | - } |
|
| 703 | - else |
|
| 704 | - { |
|
| 705 | - $row['updated_at'] = date('Y-m-d H:i:s'); |
|
| 706 | - } |
|
| 707 | - return $row; |
|
| 708 | - } |
|
| 709 | - |
|
| 710 | - /** |
|
| 711 | - * Serialises data for you automatically, allowing you to pass |
|
| 712 | - * through objects and let it handle the serialisation in the background |
|
| 713 | - */ |
|
| 714 | - public function serialize($row) |
|
| 715 | - { |
|
| 716 | - foreach ($this->callback_parameters as $column) |
|
| 717 | - { |
|
| 718 | - $row[$column] = serialize($row[$column]); |
|
| 719 | - } |
|
| 720 | - return $row; |
|
| 721 | - } |
|
| 722 | - |
|
| 723 | - public function unserialize($row) |
|
| 724 | - { |
|
| 725 | - foreach ($this->callback_parameters as $column) |
|
| 726 | - { |
|
| 727 | - if (is_array($row)) |
|
| 728 | - { |
|
| 729 | - $row[$column] = unserialize($row[$column]); |
|
| 730 | - } |
|
| 731 | - else |
|
| 732 | - { |
|
| 733 | - $row->$column = unserialize($row->$column); |
|
| 734 | - } |
|
| 735 | - } |
|
| 736 | - return $row; |
|
| 737 | - } |
|
| 738 | - |
|
| 739 | - /** |
|
| 740 | - * Protect attributes by removing them from $row array |
|
| 741 | - */ |
|
| 742 | - public function protect_attributes($row) |
|
| 743 | - { |
|
| 744 | - foreach ($this->protected_attributes as $attr) |
|
| 745 | - { |
|
| 746 | - if (is_object($row)) |
|
| 747 | - { |
|
| 680 | + /** |
|
| 681 | + * MySQL DATETIME created_at and updated_at |
|
| 682 | + */ |
|
| 683 | + public function created_at($row) |
|
| 684 | + { |
|
| 685 | + if (is_object($row)) |
|
| 686 | + { |
|
| 687 | + $row->created_at = date('Y-m-d H:i:s'); |
|
| 688 | + } |
|
| 689 | + else |
|
| 690 | + { |
|
| 691 | + $row['created_at'] = date('Y-m-d H:i:s'); |
|
| 692 | + } |
|
| 693 | + |
|
| 694 | + return $row; |
|
| 695 | + } |
|
| 696 | + |
|
| 697 | + public function updated_at($row) |
|
| 698 | + { |
|
| 699 | + if (is_object($row)) |
|
| 700 | + { |
|
| 701 | + $row->updated_at = date('Y-m-d H:i:s'); |
|
| 702 | + } |
|
| 703 | + else |
|
| 704 | + { |
|
| 705 | + $row['updated_at'] = date('Y-m-d H:i:s'); |
|
| 706 | + } |
|
| 707 | + return $row; |
|
| 708 | + } |
|
| 709 | + |
|
| 710 | + /** |
|
| 711 | + * Serialises data for you automatically, allowing you to pass |
|
| 712 | + * through objects and let it handle the serialisation in the background |
|
| 713 | + */ |
|
| 714 | + public function serialize($row) |
|
| 715 | + { |
|
| 716 | + foreach ($this->callback_parameters as $column) |
|
| 717 | + { |
|
| 718 | + $row[$column] = serialize($row[$column]); |
|
| 719 | + } |
|
| 720 | + return $row; |
|
| 721 | + } |
|
| 722 | + |
|
| 723 | + public function unserialize($row) |
|
| 724 | + { |
|
| 725 | + foreach ($this->callback_parameters as $column) |
|
| 726 | + { |
|
| 727 | + if (is_array($row)) |
|
| 728 | + { |
|
| 729 | + $row[$column] = unserialize($row[$column]); |
|
| 730 | + } |
|
| 731 | + else |
|
| 732 | + { |
|
| 733 | + $row->$column = unserialize($row->$column); |
|
| 734 | + } |
|
| 735 | + } |
|
| 736 | + return $row; |
|
| 737 | + } |
|
| 738 | + |
|
| 739 | + /** |
|
| 740 | + * Protect attributes by removing them from $row array |
|
| 741 | + */ |
|
| 742 | + public function protect_attributes($row) |
|
| 743 | + { |
|
| 744 | + foreach ($this->protected_attributes as $attr) |
|
| 745 | + { |
|
| 746 | + if (is_object($row)) |
|
| 747 | + { |
|
| 748 | 748 | if(isset($row->$attr)){ |
| 749 | 749 | unset($row->$attr); |
| 750 | 750 | } |
| 751 | - } |
|
| 752 | - else |
|
| 753 | - { |
|
| 751 | + } |
|
| 752 | + else |
|
| 753 | + { |
|
| 754 | 754 | if(isset($row[$attr])){ |
| 755 | 755 | unset($row[$attr]); |
| 756 | 756 | } |
| 757 | - } |
|
| 758 | - } |
|
| 759 | - return $row; |
|
| 760 | - } |
|
| 757 | + } |
|
| 758 | + } |
|
| 759 | + return $row; |
|
| 760 | + } |
|
| 761 | 761 | |
| 762 | 762 | /** |
| 763 | - * Return the database instance |
|
| 764 | - * @return Database the database instance |
|
| 765 | - */ |
|
| 766 | - public function getDatabaseInstance(){ |
|
| 767 | - return $this->_database; |
|
| 768 | - } |
|
| 769 | - |
|
| 770 | - /** |
|
| 771 | - * set the Database instance for future use |
|
| 772 | - * @param Database $db the database object |
|
| 773 | - */ |
|
| 774 | - public function setDatabaseInstance($db){ |
|
| 775 | - $this->_database = $db; |
|
| 776 | - if($this->dbCacheTime > 0){ |
|
| 777 | - $this->_database->setCache($this->dbCacheTime); |
|
| 778 | - } |
|
| 779 | - return $this; |
|
| 780 | - } |
|
| 781 | - |
|
| 782 | - /** |
|
| 783 | - * Return the loader instance |
|
| 784 | - * @return Loader the loader instance |
|
| 785 | - */ |
|
| 786 | - public function getLoader(){ |
|
| 787 | - return $this->loaderInstance; |
|
| 788 | - } |
|
| 789 | - |
|
| 790 | - /** |
|
| 791 | - * set the loader instance for future use |
|
| 792 | - * @param Loader $loader the loader object |
|
| 793 | - */ |
|
| 794 | - public function setLoader($loader){ |
|
| 795 | - $this->loaderInstance = $loader; |
|
| 796 | - return $this; |
|
| 797 | - } |
|
| 798 | - |
|
| 799 | - /** |
|
| 800 | - * Return the FormValidation instance |
|
| 801 | - * @return FormValidation the form validation instance |
|
| 802 | - */ |
|
| 803 | - public function getFormValidation(){ |
|
| 804 | - return $this->formValidationInstance; |
|
| 805 | - } |
|
| 806 | - |
|
| 807 | - /** |
|
| 808 | - * set the form validation instance for future use |
|
| 809 | - * @param FormValidation $fv the form validation object |
|
| 810 | - */ |
|
| 811 | - public function setFormValidation($fv){ |
|
| 812 | - $this->formValidationInstance = $fv; |
|
| 813 | - return $this; |
|
| 814 | - } |
|
| 815 | - |
|
| 816 | - /* -------------------------------------------------------------- |
|
| 763 | + * Return the database instance |
|
| 764 | + * @return Database the database instance |
|
| 765 | + */ |
|
| 766 | + public function getDatabaseInstance(){ |
|
| 767 | + return $this->_database; |
|
| 768 | + } |
|
| 769 | + |
|
| 770 | + /** |
|
| 771 | + * set the Database instance for future use |
|
| 772 | + * @param Database $db the database object |
|
| 773 | + */ |
|
| 774 | + public function setDatabaseInstance($db){ |
|
| 775 | + $this->_database = $db; |
|
| 776 | + if($this->dbCacheTime > 0){ |
|
| 777 | + $this->_database->setCache($this->dbCacheTime); |
|
| 778 | + } |
|
| 779 | + return $this; |
|
| 780 | + } |
|
| 781 | + |
|
| 782 | + /** |
|
| 783 | + * Return the loader instance |
|
| 784 | + * @return Loader the loader instance |
|
| 785 | + */ |
|
| 786 | + public function getLoader(){ |
|
| 787 | + return $this->loaderInstance; |
|
| 788 | + } |
|
| 789 | + |
|
| 790 | + /** |
|
| 791 | + * set the loader instance for future use |
|
| 792 | + * @param Loader $loader the loader object |
|
| 793 | + */ |
|
| 794 | + public function setLoader($loader){ |
|
| 795 | + $this->loaderInstance = $loader; |
|
| 796 | + return $this; |
|
| 797 | + } |
|
| 798 | + |
|
| 799 | + /** |
|
| 800 | + * Return the FormValidation instance |
|
| 801 | + * @return FormValidation the form validation instance |
|
| 802 | + */ |
|
| 803 | + public function getFormValidation(){ |
|
| 804 | + return $this->formValidationInstance; |
|
| 805 | + } |
|
| 806 | + |
|
| 807 | + /** |
|
| 808 | + * set the form validation instance for future use |
|
| 809 | + * @param FormValidation $fv the form validation object |
|
| 810 | + */ |
|
| 811 | + public function setFormValidation($fv){ |
|
| 812 | + $this->formValidationInstance = $fv; |
|
| 813 | + return $this; |
|
| 814 | + } |
|
| 815 | + |
|
| 816 | + /* -------------------------------------------------------------- |
|
| 817 | 817 | * QUERY BUILDER DIRECT ACCESS METHODS |
| 818 | 818 | * ------------------------------------------------------------ */ |
| 819 | 819 | |
| 820 | - /** |
|
| 821 | - * A wrapper to $this->_database->orderBy() |
|
| 822 | - */ |
|
| 823 | - public function order_by($criteria, $order = 'ASC') |
|
| 824 | - { |
|
| 825 | - if ( is_array($criteria) ) |
|
| 826 | - { |
|
| 827 | - foreach ($criteria as $key => $value) |
|
| 828 | - { |
|
| 829 | - $this->_database->orderBy($key, $value); |
|
| 830 | - } |
|
| 831 | - } |
|
| 832 | - else |
|
| 833 | - { |
|
| 834 | - $this->_database->orderBy($criteria, $order); |
|
| 835 | - } |
|
| 836 | - return $this; |
|
| 837 | - } |
|
| 838 | - |
|
| 839 | - /** |
|
| 840 | - * A wrapper to $this->_database->limit() |
|
| 841 | - */ |
|
| 842 | - public function limit($offset = 0, $limit = 10) |
|
| 843 | - { |
|
| 844 | - $this->_database->limit($offset, $limit); |
|
| 845 | - return $this; |
|
| 846 | - } |
|
| 847 | - |
|
| 848 | - /* -------------------------------------------------------------- |
|
| 820 | + /** |
|
| 821 | + * A wrapper to $this->_database->orderBy() |
|
| 822 | + */ |
|
| 823 | + public function order_by($criteria, $order = 'ASC') |
|
| 824 | + { |
|
| 825 | + if ( is_array($criteria) ) |
|
| 826 | + { |
|
| 827 | + foreach ($criteria as $key => $value) |
|
| 828 | + { |
|
| 829 | + $this->_database->orderBy($key, $value); |
|
| 830 | + } |
|
| 831 | + } |
|
| 832 | + else |
|
| 833 | + { |
|
| 834 | + $this->_database->orderBy($criteria, $order); |
|
| 835 | + } |
|
| 836 | + return $this; |
|
| 837 | + } |
|
| 838 | + |
|
| 839 | + /** |
|
| 840 | + * A wrapper to $this->_database->limit() |
|
| 841 | + */ |
|
| 842 | + public function limit($offset = 0, $limit = 10) |
|
| 843 | + { |
|
| 844 | + $this->_database->limit($offset, $limit); |
|
| 845 | + return $this; |
|
| 846 | + } |
|
| 847 | + |
|
| 848 | + /* -------------------------------------------------------------- |
|
| 849 | 849 | * INTERNAL METHODS |
| 850 | 850 | * ------------------------------------------------------------ */ |
| 851 | 851 | |
| 852 | - /** |
|
| 853 | - * Trigger an event and call its observers. Pass through the event name |
|
| 854 | - * (which looks for an instance variable $this->event_name), an array of |
|
| 855 | - * parameters to pass through and an optional 'last in interation' boolean |
|
| 856 | - */ |
|
| 857 | - protected function trigger($event, $data = FALSE, $last = TRUE) |
|
| 858 | - { |
|
| 859 | - if (isset($this->$event) && is_array($this->$event)) |
|
| 860 | - { |
|
| 861 | - foreach ($this->$event as $method) |
|
| 862 | - { |
|
| 863 | - if (strpos($method, '(')) |
|
| 864 | - { |
|
| 865 | - preg_match('/([a-zA-Z0-9\_\-]+)(\(([a-zA-Z0-9\_\-\., ]+)\))?/', $method, $matches); |
|
| 866 | - |
|
| 867 | - $method = $matches[1]; |
|
| 868 | - $this->callback_parameters = explode(',', $matches[3]); |
|
| 869 | - } |
|
| 870 | - $data = call_user_func_array(array($this, $method), array($data, $last)); |
|
| 871 | - } |
|
| 872 | - } |
|
| 873 | - return $data; |
|
| 874 | - } |
|
| 875 | - |
|
| 876 | - /** |
|
| 877 | - * Run validation on the passed data |
|
| 878 | - */ |
|
| 879 | - protected function validate(array $data) |
|
| 880 | - { |
|
| 881 | - if($this->skip_validation) |
|
| 882 | - { |
|
| 883 | - return $data; |
|
| 884 | - } |
|
| 885 | - if(!empty($this->validate)) |
|
| 886 | - { |
|
| 887 | - $fv = null; |
|
| 888 | - if(is_object($this->formValidationInstance)){ |
|
| 889 | - $fv = $this->formValidationInstance; |
|
| 890 | - } |
|
| 891 | - else{ |
|
| 892 | - Loader::library('FormValidation'); |
|
| 893 | - $fv = $this->formvalidation; |
|
| 894 | - $this->setFormValidation($fv); |
|
| 895 | - } |
|
| 896 | - if(is_array($this->validate)) |
|
| 897 | - { |
|
| 898 | - $fv->setData($data); |
|
| 899 | - $fv->setRules($this->validate); |
|
| 900 | - |
|
| 901 | - if ($fv->run()) |
|
| 902 | - { |
|
| 903 | - return $data; |
|
| 904 | - } |
|
| 905 | - else |
|
| 906 | - { |
|
| 907 | - return FALSE; |
|
| 908 | - } |
|
| 909 | - } |
|
| 910 | - else { |
|
| 911 | - return $data; |
|
| 912 | - } |
|
| 913 | - } |
|
| 914 | - else |
|
| 915 | - { |
|
| 916 | - return $data; |
|
| 917 | - } |
|
| 918 | - } |
|
| 919 | - |
|
| 920 | - |
|
| 921 | - /** |
|
| 922 | - * Set WHERE parameters, cleverly |
|
| 923 | - */ |
|
| 924 | - protected function _set_where($params) |
|
| 925 | - { |
|
| 926 | - if (count($params) == 1 && is_array($params[0])) |
|
| 927 | - { |
|
| 928 | - foreach ($params[0] as $field => $filter) |
|
| 929 | - { |
|
| 930 | - if (is_array($filter)) |
|
| 931 | - { |
|
| 932 | - $this->_database->in($field, $filter); |
|
| 933 | - } |
|
| 934 | - else |
|
| 935 | - { |
|
| 936 | - if (is_int($field)) |
|
| 937 | - { |
|
| 938 | - $this->_database->where($filter); |
|
| 939 | - } |
|
| 940 | - else |
|
| 941 | - { |
|
| 942 | - $this->_database->where($field, $filter); |
|
| 943 | - } |
|
| 944 | - } |
|
| 945 | - } |
|
| 946 | - } |
|
| 947 | - else if (count($params) == 1) |
|
| 948 | - { |
|
| 949 | - $this->_database->where($params[0]); |
|
| 950 | - } |
|
| 951 | - else if(count($params) == 2) |
|
| 952 | - { |
|
| 953 | - if (is_array($params[1])) |
|
| 954 | - { |
|
| 955 | - $this->_database->in($params[0], $params[1]); |
|
| 956 | - } |
|
| 957 | - else |
|
| 958 | - { |
|
| 959 | - $this->_database->where($params[0], $params[1]); |
|
| 960 | - } |
|
| 961 | - } |
|
| 962 | - else if(count($params) == 3) |
|
| 963 | - { |
|
| 964 | - $this->_database->where($params[0], $params[1], $params[2]); |
|
| 965 | - } |
|
| 966 | - else |
|
| 967 | - { |
|
| 968 | - if (is_array($params[1])) |
|
| 969 | - { |
|
| 970 | - $this->_database->in($params[0], $params[1]); |
|
| 971 | - } |
|
| 972 | - else |
|
| 973 | - { |
|
| 974 | - $this->_database->where($params[0], $params[1]); |
|
| 975 | - } |
|
| 976 | - } |
|
| 977 | - } |
|
| 978 | - |
|
| 979 | - /** |
|
| 852 | + /** |
|
| 853 | + * Trigger an event and call its observers. Pass through the event name |
|
| 854 | + * (which looks for an instance variable $this->event_name), an array of |
|
| 855 | + * parameters to pass through and an optional 'last in interation' boolean |
|
| 856 | + */ |
|
| 857 | + protected function trigger($event, $data = FALSE, $last = TRUE) |
|
| 858 | + { |
|
| 859 | + if (isset($this->$event) && is_array($this->$event)) |
|
| 860 | + { |
|
| 861 | + foreach ($this->$event as $method) |
|
| 862 | + { |
|
| 863 | + if (strpos($method, '(')) |
|
| 864 | + { |
|
| 865 | + preg_match('/([a-zA-Z0-9\_\-]+)(\(([a-zA-Z0-9\_\-\., ]+)\))?/', $method, $matches); |
|
| 866 | + |
|
| 867 | + $method = $matches[1]; |
|
| 868 | + $this->callback_parameters = explode(',', $matches[3]); |
|
| 869 | + } |
|
| 870 | + $data = call_user_func_array(array($this, $method), array($data, $last)); |
|
| 871 | + } |
|
| 872 | + } |
|
| 873 | + return $data; |
|
| 874 | + } |
|
| 875 | + |
|
| 876 | + /** |
|
| 877 | + * Run validation on the passed data |
|
| 878 | + */ |
|
| 879 | + protected function validate(array $data) |
|
| 880 | + { |
|
| 881 | + if($this->skip_validation) |
|
| 882 | + { |
|
| 883 | + return $data; |
|
| 884 | + } |
|
| 885 | + if(!empty($this->validate)) |
|
| 886 | + { |
|
| 887 | + $fv = null; |
|
| 888 | + if(is_object($this->formValidationInstance)){ |
|
| 889 | + $fv = $this->formValidationInstance; |
|
| 890 | + } |
|
| 891 | + else{ |
|
| 892 | + Loader::library('FormValidation'); |
|
| 893 | + $fv = $this->formvalidation; |
|
| 894 | + $this->setFormValidation($fv); |
|
| 895 | + } |
|
| 896 | + if(is_array($this->validate)) |
|
| 897 | + { |
|
| 898 | + $fv->setData($data); |
|
| 899 | + $fv->setRules($this->validate); |
|
| 900 | + |
|
| 901 | + if ($fv->run()) |
|
| 902 | + { |
|
| 903 | + return $data; |
|
| 904 | + } |
|
| 905 | + else |
|
| 906 | + { |
|
| 907 | + return FALSE; |
|
| 908 | + } |
|
| 909 | + } |
|
| 910 | + else { |
|
| 911 | + return $data; |
|
| 912 | + } |
|
| 913 | + } |
|
| 914 | + else |
|
| 915 | + { |
|
| 916 | + return $data; |
|
| 917 | + } |
|
| 918 | + } |
|
| 919 | + |
|
| 920 | + |
|
| 921 | + /** |
|
| 922 | + * Set WHERE parameters, cleverly |
|
| 923 | + */ |
|
| 924 | + protected function _set_where($params) |
|
| 925 | + { |
|
| 926 | + if (count($params) == 1 && is_array($params[0])) |
|
| 927 | + { |
|
| 928 | + foreach ($params[0] as $field => $filter) |
|
| 929 | + { |
|
| 930 | + if (is_array($filter)) |
|
| 931 | + { |
|
| 932 | + $this->_database->in($field, $filter); |
|
| 933 | + } |
|
| 934 | + else |
|
| 935 | + { |
|
| 936 | + if (is_int($field)) |
|
| 937 | + { |
|
| 938 | + $this->_database->where($filter); |
|
| 939 | + } |
|
| 940 | + else |
|
| 941 | + { |
|
| 942 | + $this->_database->where($field, $filter); |
|
| 943 | + } |
|
| 944 | + } |
|
| 945 | + } |
|
| 946 | + } |
|
| 947 | + else if (count($params) == 1) |
|
| 948 | + { |
|
| 949 | + $this->_database->where($params[0]); |
|
| 950 | + } |
|
| 951 | + else if(count($params) == 2) |
|
| 952 | + { |
|
| 953 | + if (is_array($params[1])) |
|
| 954 | + { |
|
| 955 | + $this->_database->in($params[0], $params[1]); |
|
| 956 | + } |
|
| 957 | + else |
|
| 958 | + { |
|
| 959 | + $this->_database->where($params[0], $params[1]); |
|
| 960 | + } |
|
| 961 | + } |
|
| 962 | + else if(count($params) == 3) |
|
| 963 | + { |
|
| 964 | + $this->_database->where($params[0], $params[1], $params[2]); |
|
| 965 | + } |
|
| 966 | + else |
|
| 967 | + { |
|
| 968 | + if (is_array($params[1])) |
|
| 969 | + { |
|
| 970 | + $this->_database->in($params[0], $params[1]); |
|
| 971 | + } |
|
| 972 | + else |
|
| 973 | + { |
|
| 974 | + $this->_database->where($params[0], $params[1]); |
|
| 975 | + } |
|
| 976 | + } |
|
| 977 | + } |
|
| 978 | + |
|
| 979 | + /** |
|
| 980 | 980 | Shortcut to controller |
| 981 | - */ |
|
| 982 | - public function __get($key){ |
|
| 983 | - return get_instance()->{$key}; |
|
| 984 | - } |
|
| 981 | + */ |
|
| 982 | + public function __get($key){ |
|
| 983 | + return get_instance()->{$key}; |
|
| 984 | + } |
|
| 985 | 985 | |
| 986 | - } |
|
| 986 | + } |
|
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | * You should have received a copy of the GNU General Public License |
| 23 | 23 | * along with this program; if not, write to the Free Software |
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | - */ |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | 27 | class FileCache implements CacheInterface{ |
| 28 | 28 | |
@@ -44,15 +44,15 @@ discard block |
||
| 44 | 44 | show_error('The cache for file system is not available. Check the cache directory if is exists or is writable.'); |
| 45 | 45 | } |
| 46 | 46 | /** |
| 47 | - * instance of the Log class |
|
| 48 | - */ |
|
| 49 | - if(is_object($logger)){ |
|
| 50 | - $this->logger = $logger; |
|
| 51 | - } |
|
| 52 | - else{ |
|
| 53 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 54 | - $this->logger->setLogger('Library::FileCache'); |
|
| 55 | - } |
|
| 47 | + * instance of the Log class |
|
| 48 | + */ |
|
| 49 | + if(is_object($logger)){ |
|
| 50 | + $this->logger = $logger; |
|
| 51 | + } |
|
| 52 | + else{ |
|
| 53 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 54 | + $this->logger->setLogger('Library::FileCache'); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | 57 | //if Zlib extension is not loaded set compressCacheData to false |
| 58 | 58 | if(! extension_loaded('zlib')){ |
@@ -80,26 +80,26 @@ discard block |
||
| 80 | 80 | return false; |
| 81 | 81 | } |
| 82 | 82 | // Getting a shared lock |
| 83 | - flock($handle, LOCK_SH); |
|
| 84 | - $data = file_get_contents($filePath); |
|
| 85 | - fclose($handle); |
|
| 86 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 87 | - if (! $data) { |
|
| 88 | - $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
| 89 | - // If unserializing somehow didn't work out, we'll delete the file |
|
| 90 | - unlink($filePath); |
|
| 91 | - return false; |
|
| 92 | - } |
|
| 93 | - if (time() > $data['expire']) { |
|
| 94 | - $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
| 95 | - // Unlinking when the file was expired |
|
| 96 | - unlink($filePath); |
|
| 97 | - return false; |
|
| 98 | - } |
|
| 99 | - else{ |
|
| 100 | - $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
|
| 101 | - return $data['data']; |
|
| 102 | - } |
|
| 83 | + flock($handle, LOCK_SH); |
|
| 84 | + $data = file_get_contents($filePath); |
|
| 85 | + fclose($handle); |
|
| 86 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 87 | + if (! $data) { |
|
| 88 | + $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
| 89 | + // If unserializing somehow didn't work out, we'll delete the file |
|
| 90 | + unlink($filePath); |
|
| 91 | + return false; |
|
| 92 | + } |
|
| 93 | + if (time() > $data['expire']) { |
|
| 94 | + $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
| 95 | + // Unlinking when the file was expired |
|
| 96 | + unlink($filePath); |
|
| 97 | + return false; |
|
| 98 | + } |
|
| 99 | + else{ |
|
| 100 | + $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
|
| 101 | + return $data['data']; |
|
| 102 | + } |
|
| 103 | 103 | return false; |
| 104 | 104 | } |
| 105 | 105 | |
@@ -122,25 +122,25 @@ discard block |
||
| 122 | 122 | } |
| 123 | 123 | flock($handle, LOCK_EX); // exclusive lock, will get released when the file is closed |
| 124 | 124 | //Serializing along with the TTL |
| 125 | - $cacheData = serialize(array( |
|
| 125 | + $cacheData = serialize(array( |
|
| 126 | 126 | 'mtime' => time(), |
| 127 | 127 | 'expire' => $expire, |
| 128 | 128 | 'data' => $data, |
| 129 | 129 | 'ttl' => $ttl |
| 130 | 130 | ) |
| 131 | 131 | ); |
| 132 | - $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
|
| 133 | - if(! $result){ |
|
| 134 | - $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 135 | - fclose($handle); |
|
| 136 | - return false; |
|
| 137 | - } |
|
| 138 | - else{ |
|
| 139 | - $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
| 140 | - fclose($handle); |
|
| 132 | + $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
|
| 133 | + if(! $result){ |
|
| 134 | + $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 135 | + fclose($handle); |
|
| 136 | + return false; |
|
| 137 | + } |
|
| 138 | + else{ |
|
| 139 | + $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
| 140 | + fclose($handle); |
|
| 141 | 141 | chmod($filePath, 0640); |
| 142 | 142 | return true; |
| 143 | - } |
|
| 143 | + } |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | |
@@ -160,7 +160,7 @@ discard block |
||
| 160 | 160 | } |
| 161 | 161 | else{ |
| 162 | 162 | $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
| 163 | - @unlink($filePath); |
|
| 163 | + @unlink($filePath); |
|
| 164 | 164 | return true; |
| 165 | 165 | } |
| 166 | 166 | } |
@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | } |
| 184 | 184 | else{ |
| 185 | 185 | $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
| 186 | - $data = file_get_contents($filePath); |
|
| 186 | + $data = file_get_contents($filePath); |
|
| 187 | 187 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 188 | 188 | if(! $data){ |
| 189 | 189 | $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
@@ -223,17 +223,17 @@ discard block |
||
| 223 | 223 | foreach ($list as $file) { |
| 224 | 224 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
| 225 | 225 | $data = file_get_contents($file); |
| 226 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 227 | - if(! $data){ |
|
| 228 | - $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
|
| 229 | - } |
|
| 230 | - else if(time() > $data['expire']){ |
|
| 231 | - $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
|
| 232 | - @unlink($file); |
|
| 233 | - } |
|
| 234 | - else{ |
|
| 235 | - $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
|
| 236 | - } |
|
| 226 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 227 | + if(! $data){ |
|
| 228 | + $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
|
| 229 | + } |
|
| 230 | + else if(time() > $data['expire']){ |
|
| 231 | + $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
|
| 232 | + @unlink($file); |
|
| 233 | + } |
|
| 234 | + else{ |
|
| 235 | + $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
|
| 236 | + } |
|
| 237 | 237 | } |
| 238 | 238 | } |
| 239 | 239 | } |
@@ -256,19 +256,19 @@ discard block |
||
| 256 | 256 | } |
| 257 | 257 | } |
| 258 | 258 | |
| 259 | - /** |
|
| 260 | - * @return boolean |
|
| 261 | - */ |
|
| 262 | - public function isCompressCacheData(){ |
|
| 263 | - return $this->compressCacheData; |
|
| 264 | - } |
|
| 259 | + /** |
|
| 260 | + * @return boolean |
|
| 261 | + */ |
|
| 262 | + public function isCompressCacheData(){ |
|
| 263 | + return $this->compressCacheData; |
|
| 264 | + } |
|
| 265 | 265 | |
| 266 | - /** |
|
| 267 | - * @param boolean $compressCacheData |
|
| 268 | - * |
|
| 269 | - * @return self |
|
| 270 | - */ |
|
| 271 | - public function setCompressCacheData($status = true){ |
|
| 266 | + /** |
|
| 267 | + * @param boolean $compressCacheData |
|
| 268 | + * |
|
| 269 | + * @return self |
|
| 270 | + */ |
|
| 271 | + public function setCompressCacheData($status = true){ |
|
| 272 | 272 | //if Zlib extension is not loaded set compressCacheData to false |
| 273 | 273 | if($status === true && ! extension_loaded('zlib')){ |
| 274 | 274 | |
@@ -279,7 +279,7 @@ discard block |
||
| 279 | 279 | $this->compressCacheData = $status; |
| 280 | 280 | } |
| 281 | 281 | return $this; |
| 282 | - } |
|
| 282 | + } |
|
| 283 | 283 | |
| 284 | 284 | /** |
| 285 | 285 | * Check whether the cache feature for the handle is supported |
@@ -291,28 +291,28 @@ discard block |
||
| 291 | 291 | } |
| 292 | 292 | |
| 293 | 293 | /** |
| 294 | - * Return the Log instance |
|
| 295 | - * @return Log |
|
| 296 | - */ |
|
| 297 | - public function getLogger(){ |
|
| 298 | - return $this->logger; |
|
| 299 | - } |
|
| 294 | + * Return the Log instance |
|
| 295 | + * @return Log |
|
| 296 | + */ |
|
| 297 | + public function getLogger(){ |
|
| 298 | + return $this->logger; |
|
| 299 | + } |
|
| 300 | 300 | |
| 301 | - /** |
|
| 302 | - * Set the log instance |
|
| 303 | - * @param Log $logger the log object |
|
| 304 | - */ |
|
| 305 | - public function setLogger(Log $logger){ |
|
| 306 | - $this->logger = $logger; |
|
| 307 | - return $this; |
|
| 308 | - } |
|
| 301 | + /** |
|
| 302 | + * Set the log instance |
|
| 303 | + * @param Log $logger the log object |
|
| 304 | + */ |
|
| 305 | + public function setLogger(Log $logger){ |
|
| 306 | + $this->logger = $logger; |
|
| 307 | + return $this; |
|
| 308 | + } |
|
| 309 | 309 | |
| 310 | 310 | /** |
| 311 | - * Get the cache file full path for the given key |
|
| 312 | - * |
|
| 313 | - * @param $key the cache item key |
|
| 314 | - * @return string the full cache file path for this key |
|
| 315 | - */ |
|
| 311 | + * Get the cache file full path for the given key |
|
| 312 | + * |
|
| 313 | + * @param $key the cache item key |
|
| 314 | + * @return string the full cache file path for this key |
|
| 315 | + */ |
|
| 316 | 316 | private function getFilePath($key){ |
| 317 | 317 | return CACHE_PATH . md5($key) . '.cache'; |
| 318 | 318 | } |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | * You should have received a copy of the GNU General Public License |
| 23 | 23 | * along with this program; if not, write to the Free Software |
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | - */ |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | 27 | class ApcCache implements CacheInterface{ |
| 28 | 28 | |
@@ -39,15 +39,15 @@ discard block |
||
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | /** |
| 42 | - * instance of the Log class |
|
| 43 | - */ |
|
| 44 | - if(is_object($logger)){ |
|
| 45 | - $this->logger = $logger; |
|
| 46 | - } |
|
| 47 | - else{ |
|
| 48 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 49 | - $this->logger->setLogger('Library::ApcCache'); |
|
| 50 | - } |
|
| 42 | + * instance of the Log class |
|
| 43 | + */ |
|
| 44 | + if(is_object($logger)){ |
|
| 45 | + $this->logger = $logger; |
|
| 46 | + } |
|
| 47 | + else{ |
|
| 48 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 49 | + $this->logger->setLogger('Library::ApcCache'); |
|
| 50 | + } |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | /** |
@@ -87,13 +87,13 @@ discard block |
||
| 87 | 87 | $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
| 88 | 88 | $result = apc_store($key, $data, $ttl); |
| 89 | 89 | if($result === false){ |
| 90 | - $this->logger->error('Can not write cache data for the key ['. $key .'], return false'); |
|
| 91 | - return false; |
|
| 92 | - } |
|
| 93 | - else{ |
|
| 94 | - $this->logger->info('Cache data saved for the key ['. $key .']'); |
|
| 95 | - return true; |
|
| 96 | - } |
|
| 90 | + $this->logger->error('Can not write cache data for the key ['. $key .'], return false'); |
|
| 91 | + return false; |
|
| 92 | + } |
|
| 93 | + else{ |
|
| 94 | + $this->logger->info('Cache data saved for the key ['. $key .']'); |
|
| 95 | + return true; |
|
| 96 | + } |
|
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | } |
| 113 | 113 | else{ |
| 114 | 114 | $this->logger->info('Found cache data for the key [' .$key. '] remove it'); |
| 115 | - return apc_delete($key); |
|
| 115 | + return apc_delete($key); |
|
| 116 | 116 | } |
| 117 | 117 | return false; |
| 118 | 118 | } |
@@ -178,28 +178,28 @@ discard block |
||
| 178 | 178 | } |
| 179 | 179 | |
| 180 | 180 | /** |
| 181 | - * Return the Log instance |
|
| 182 | - * @return Log |
|
| 183 | - */ |
|
| 184 | - public function getLogger(){ |
|
| 185 | - return $this->logger; |
|
| 186 | - } |
|
| 181 | + * Return the Log instance |
|
| 182 | + * @return Log |
|
| 183 | + */ |
|
| 184 | + public function getLogger(){ |
|
| 185 | + return $this->logger; |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | - /** |
|
| 189 | - * Set the log instance |
|
| 190 | - * @param Log $logger the log object |
|
| 191 | - */ |
|
| 192 | - public function setLogger(Log $logger){ |
|
| 193 | - $this->logger = $logger; |
|
| 194 | - return $this; |
|
| 195 | - } |
|
| 188 | + /** |
|
| 189 | + * Set the log instance |
|
| 190 | + * @param Log $logger the log object |
|
| 191 | + */ |
|
| 192 | + public function setLogger(Log $logger){ |
|
| 193 | + $this->logger = $logger; |
|
| 194 | + return $this; |
|
| 195 | + } |
|
| 196 | 196 | |
| 197 | 197 | /** |
| 198 | - * Return the array of cache information |
|
| 199 | - * |
|
| 200 | - * @param string $key the cache key to get the cache information |
|
| 201 | - * @return array |
|
| 202 | - */ |
|
| 198 | + * Return the array of cache information |
|
| 199 | + * |
|
| 200 | + * @param string $key the cache key to get the cache information |
|
| 201 | + * @return array |
|
| 202 | + */ |
|
| 203 | 203 | private function _getCacheInfo($key){ |
| 204 | 204 | $caches = apc_cache_info('user'); |
| 205 | 205 | if(! empty($caches['cache_list'])){ |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | * You should have received a copy of the GNU General Public License |
| 23 | 23 | * along with this program; if not, write to the Free Software |
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | - */ |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | 27 | /** |
| 28 | 28 | * For application languages management |
@@ -68,8 +68,8 @@ discard block |
||
| 68 | 68 | * Construct new Lang instance |
| 69 | 69 | */ |
| 70 | 70 | public function __construct(){ |
| 71 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 72 | - $this->logger->setLogger('Library::Lang'); |
|
| 71 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 72 | + $this->logger->setLogger('Library::Lang'); |
|
| 73 | 73 | |
| 74 | 74 | $this->default = get_config('default_language', 'en'); |
| 75 | 75 | $this->logger->debug('Setting the supported languages'); |
@@ -1,10 +1,10 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - $pdo = new PDO('sqlite:D:\wamp\www\tnh-fw\tests\assets\db_tests.db', '', ''); |
|
| 4 | - $pdo->exec("SET NAMES 'UTF8' COLLATE 'utf8_general_ci'"); |
|
| 5 | - $pdo->exec("SET CHARACTER SET 'UTF8'"); |
|
| 6 | - $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
|
| 7 | - $pdo->setAttribute(PDO::ATTR_CURSOR, PDO::CURSOR_FWDONLY); |
|
| 3 | + $pdo = new PDO('sqlite:D:\wamp\www\tnh-fw\tests\assets\db_tests.db', '', ''); |
|
| 4 | + $pdo->exec("SET NAMES 'UTF8' COLLATE 'utf8_general_ci'"); |
|
| 5 | + $pdo->exec("SET CHARACTER SET 'UTF8'"); |
|
| 6 | + $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
|
| 7 | + $pdo->setAttribute(PDO::ATTR_CURSOR, PDO::CURSOR_FWDONLY); |
|
| 8 | 8 | $r = $pdo->query("SELECT * FROM ses WHERE s_id = 'tony' LIMIT 1"); |
| 9 | 9 | |
| 10 | 10 | $nb = $r->rowCount(); |