@@ -220,8 +220,7 @@ discard block |
||
| 220 | 220 | //first check for CSRF |
| 221 | 221 | if ((get_config('csrf_enable', false) || $this->enableCsrfCheck) && ! Security::validateCSRF()){ |
| 222 | 222 | show_error('Invalide data, Cross Site Request Forgery do his job, the data to validate is corrupted.'); |
| 223 | - } |
|
| 224 | - else{ |
|
| 223 | + } else{ |
|
| 225 | 224 | $this->logger->info('CSRF is not enabled in configuration or not set manully, no need to check it'); |
| 226 | 225 | } |
| 227 | 226 | } |
@@ -231,8 +230,7 @@ discard block |
||
| 231 | 230 | foreach ($this->getData() as $inputName => $inputVal) { |
| 232 | 231 | if(is_array($this->data[$inputName])){ |
| 233 | 232 | $this->data[$inputName] = array_map('trim', $this->data[$inputName]); |
| 234 | - } |
|
| 235 | - else{ |
|
| 233 | + } else{ |
|
| 236 | 234 | $this->data[$inputName] = trim($this->data[$inputName]); |
| 237 | 235 | } |
| 238 | 236 | |
@@ -600,8 +598,7 @@ discard block |
||
| 600 | 598 | if ($inputVal == '' && $callbackReturn == true) { |
| 601 | 599 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
| 602 | 600 | } |
| 603 | - } |
|
| 604 | - else if($inputVal == '') { |
|
| 601 | + } else if($inputVal == '') { |
|
| 605 | 602 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
| 606 | 603 | } |
| 607 | 604 | } |
@@ -660,8 +657,7 @@ discard block |
||
| 660 | 657 | $this->_setError($inputName, $ruleName . ',post:key', array($this->_getLabel($inputName), $this->_getLabel(str_replace('post:', '', $doNotEqual)))); |
| 661 | 658 | continue; |
| 662 | 659 | } |
| 663 | - } |
|
| 664 | - else{ |
|
| 660 | + } else{ |
|
| 665 | 661 | if ($inputVal == $doNotEqual) { |
| 666 | 662 | $this->_setError($inputName, $ruleName . ',string', array($this->_getLabel($inputName), $doNotEqual)); |
| 667 | 663 | continue; |
@@ -1,922 +1,922 @@ |
||
| 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 | - class FormValidation{ |
|
| 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 | + class FormValidation{ |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * The form validation status |
|
| 32 | - * @var boolean |
|
| 33 | - */ |
|
| 34 | - protected $_success = false; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * The list of errors messages |
|
| 38 | - * @var array |
|
| 39 | - */ |
|
| 40 | - protected $_errorsMessages = array(); |
|
| 30 | + /** |
|
| 31 | + * The form validation status |
|
| 32 | + * @var boolean |
|
| 33 | + */ |
|
| 34 | + protected $_success = false; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * The list of errors messages |
|
| 38 | + * @var array |
|
| 39 | + */ |
|
| 40 | + protected $_errorsMessages = array(); |
|
| 41 | 41 | |
| 42 | - // Array of rule sets, fieldName => PIPE seperated ruleString |
|
| 43 | - protected $_rules = array(); |
|
| 42 | + // Array of rule sets, fieldName => PIPE seperated ruleString |
|
| 43 | + protected $_rules = array(); |
|
| 44 | 44 | |
| 45 | - // Array of errors, niceName => Error Message |
|
| 46 | - protected $_errors = array(); |
|
| 45 | + // Array of errors, niceName => Error Message |
|
| 46 | + protected $_errors = array(); |
|
| 47 | 47 | |
| 48 | - // Array of post Key => Nice name labels |
|
| 49 | - protected $_labels = array(); |
|
| 48 | + // Array of post Key => Nice name labels |
|
| 49 | + protected $_labels = array(); |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * The errors delimiters |
|
| 53 | - * @var array |
|
| 54 | - */ |
|
| 55 | - protected $_allErrorsDelimiter = array('<div class="error">', '</div>'); |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * The each error delimiter |
|
| 59 | - * @var array |
|
| 60 | - */ |
|
| 61 | - protected $_eachErrorDelimiter = array('<p class="error">', '</p>'); |
|
| 51 | + /** |
|
| 52 | + * The errors delimiters |
|
| 53 | + * @var array |
|
| 54 | + */ |
|
| 55 | + protected $_allErrorsDelimiter = array('<div class="error">', '</div>'); |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * The each error delimiter |
|
| 59 | + * @var array |
|
| 60 | + */ |
|
| 61 | + protected $_eachErrorDelimiter = array('<p class="error">', '</p>'); |
|
| 62 | 62 | |
| 63 | 63 | /** |
| 64 | - * Indicated if need force the validation to be failed |
|
| 65 | - * @var boolean |
|
| 66 | - */ |
|
| 67 | - protected $_forceFail = false; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * The list of the error messages overrides by the original |
|
| 71 | - * @var array |
|
| 72 | - */ |
|
| 73 | - protected $_errorPhraseOverrides = array(); |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * The logger instance |
|
| 77 | - * @var Log |
|
| 78 | - */ |
|
| 79 | - private $logger; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * The data to be validated, the default is to use $_POST |
|
| 83 | - * @var array |
|
| 84 | - */ |
|
| 85 | - private $data = array(); |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Whether to check the CSRF. This attribute is just a way to allow custom change of the |
|
| 64 | + * Indicated if need force the validation to be failed |
|
| 65 | + * @var boolean |
|
| 66 | + */ |
|
| 67 | + protected $_forceFail = false; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * The list of the error messages overrides by the original |
|
| 71 | + * @var array |
|
| 72 | + */ |
|
| 73 | + protected $_errorPhraseOverrides = array(); |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * The logger instance |
|
| 77 | + * @var Log |
|
| 78 | + */ |
|
| 79 | + private $logger; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * The data to be validated, the default is to use $_POST |
|
| 83 | + * @var array |
|
| 84 | + */ |
|
| 85 | + private $data = array(); |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Whether to check the CSRF. This attribute is just a way to allow custom change of the |
|
| 89 | 89 | * CSRF global configuration |
| 90 | 90 | * |
| 91 | - * @var boolean |
|
| 92 | - */ |
|
| 93 | - public $enableCsrfCheck = false; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Set all errors and rule sets empty, and sets success to false. |
|
| 97 | - * |
|
| 98 | - * @return void |
|
| 99 | - */ |
|
| 100 | - public function __construct() { |
|
| 101 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 102 | - $this->logger->setLogger('Library::FormValidation'); |
|
| 91 | + * @var boolean |
|
| 92 | + */ |
|
| 93 | + public $enableCsrfCheck = false; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Set all errors and rule sets empty, and sets success to false. |
|
| 97 | + * |
|
| 98 | + * @return void |
|
| 99 | + */ |
|
| 100 | + public function __construct() { |
|
| 101 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 102 | + $this->logger->setLogger('Library::FormValidation'); |
|
| 103 | 103 | |
| 104 | 104 | //Load form validation language message |
| 105 | - Loader::lang('form_validation'); |
|
| 106 | - $obj = & get_instance(); |
|
| 107 | - $this->_errorsMessages = array( |
|
| 108 | - 'required' => $obj->lang->get('fv_required'), |
|
| 109 | - 'min_length' => $obj->lang->get('fv_min_length'), |
|
| 110 | - 'max_length' => $obj->lang->get('fv_max_length'), |
|
| 111 | - 'exact_length' => $obj->lang->get('fv_exact_length'), |
|
| 112 | - 'less_than' => $obj->lang->get('fv_less_than'), |
|
| 113 | - 'greater_than' => $obj->lang->get('fv_greater_than'), |
|
| 114 | - 'matches' => $obj->lang->get('fv_matches'), |
|
| 115 | - 'valid_email' => $obj->lang->get('fv_valid_email'), |
|
| 116 | - 'not_equal' => array( |
|
| 117 | - 'post:key' => $obj->lang->get('fv_not_equal_post_key'), |
|
| 118 | - 'string' => $obj->lang->get('fv_not_equal_string') |
|
| 119 | - ), |
|
| 120 | - 'depends' => $obj->lang->get('fv_depends'), |
|
| 121 | - 'is_unique' => $obj->lang->get('fv_is_unique'), |
|
| 122 | - 'is_unique_update' => $obj->lang->get('fv_is_unique_update'), |
|
| 123 | - 'exists' => $obj->lang->get('fv_exists'), |
|
| 124 | - 'regex' => $obj->lang->get('fv_regex'), |
|
| 125 | - 'in_list' => $obj->lang->get('fv_in_list'), |
|
| 126 | - 'numeric' => $obj->lang->get('fv_numeric'), |
|
| 127 | - 'callback' => $obj->lang->get('fv_callback'), |
|
| 128 | - ); |
|
| 129 | - $this->_resetValidation(); |
|
| 130 | - $this->setData($obj->request->post(null)); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Reset the form validation instance |
|
| 135 | - */ |
|
| 136 | - protected function _resetValidation() { |
|
| 137 | - $this->_rules = array(); |
|
| 138 | - $this->_labels = array(); |
|
| 139 | - $this->_errorPhraseOverrides = array(); |
|
| 140 | - $this->_errors = array(); |
|
| 141 | - $this->_success = false; |
|
| 142 | - $this->_forceFail = false; |
|
| 143 | - $this->data = array(); |
|
| 105 | + Loader::lang('form_validation'); |
|
| 106 | + $obj = & get_instance(); |
|
| 107 | + $this->_errorsMessages = array( |
|
| 108 | + 'required' => $obj->lang->get('fv_required'), |
|
| 109 | + 'min_length' => $obj->lang->get('fv_min_length'), |
|
| 110 | + 'max_length' => $obj->lang->get('fv_max_length'), |
|
| 111 | + 'exact_length' => $obj->lang->get('fv_exact_length'), |
|
| 112 | + 'less_than' => $obj->lang->get('fv_less_than'), |
|
| 113 | + 'greater_than' => $obj->lang->get('fv_greater_than'), |
|
| 114 | + 'matches' => $obj->lang->get('fv_matches'), |
|
| 115 | + 'valid_email' => $obj->lang->get('fv_valid_email'), |
|
| 116 | + 'not_equal' => array( |
|
| 117 | + 'post:key' => $obj->lang->get('fv_not_equal_post_key'), |
|
| 118 | + 'string' => $obj->lang->get('fv_not_equal_string') |
|
| 119 | + ), |
|
| 120 | + 'depends' => $obj->lang->get('fv_depends'), |
|
| 121 | + 'is_unique' => $obj->lang->get('fv_is_unique'), |
|
| 122 | + 'is_unique_update' => $obj->lang->get('fv_is_unique_update'), |
|
| 123 | + 'exists' => $obj->lang->get('fv_exists'), |
|
| 124 | + 'regex' => $obj->lang->get('fv_regex'), |
|
| 125 | + 'in_list' => $obj->lang->get('fv_in_list'), |
|
| 126 | + 'numeric' => $obj->lang->get('fv_numeric'), |
|
| 127 | + 'callback' => $obj->lang->get('fv_callback'), |
|
| 128 | + ); |
|
| 129 | + $this->_resetValidation(); |
|
| 130 | + $this->setData($obj->request->post(null)); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Reset the form validation instance |
|
| 135 | + */ |
|
| 136 | + protected function _resetValidation() { |
|
| 137 | + $this->_rules = array(); |
|
| 138 | + $this->_labels = array(); |
|
| 139 | + $this->_errorPhraseOverrides = array(); |
|
| 140 | + $this->_errors = array(); |
|
| 141 | + $this->_success = false; |
|
| 142 | + $this->_forceFail = false; |
|
| 143 | + $this->data = array(); |
|
| 144 | 144 | $this->enableCsrfCheck = false; |
| 145 | - } |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - /** |
|
| 148 | - * Set the form validation data |
|
| 149 | - * @param array $data the values to be validated |
|
| 147 | + /** |
|
| 148 | + * Set the form validation data |
|
| 149 | + * @param array $data the values to be validated |
|
| 150 | 150 | * |
| 151 | - * @return FormValidation Current instance of object. |
|
| 152 | - */ |
|
| 153 | - public function setData(array $data){ |
|
| 154 | - $this->logger->debug('Setting the form validation data, the values are: ' . stringfy_vars($data)); |
|
| 155 | - $this->data = $data; |
|
| 151 | + * @return FormValidation Current instance of object. |
|
| 152 | + */ |
|
| 153 | + public function setData(array $data){ |
|
| 154 | + $this->logger->debug('Setting the form validation data, the values are: ' . stringfy_vars($data)); |
|
| 155 | + $this->data = $data; |
|
| 156 | 156 | return $this; |
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * Get the form validation data |
|
| 161 | - * @return array the form validation data to be validated |
|
| 162 | - */ |
|
| 163 | - public function getData(){ |
|
| 164 | - return $this->data; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Get the validation function name to validate a rule |
|
| 169 | - * |
|
| 170 | - * @return string the function name |
|
| 171 | - */ |
|
| 172 | - protected function _toCallCase($funcName, $prefix='_validate') { |
|
| 173 | - $funcName = strtolower($funcName); |
|
| 174 | - $finalFuncName = $prefix; |
|
| 175 | - foreach (explode('_', $funcName) as $funcNamePart) { |
|
| 176 | - $finalFuncName .= strtoupper($funcNamePart[0]) . substr($funcNamePart, 1); |
|
| 177 | - } |
|
| 178 | - return $finalFuncName; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Returns the boolean of the data status success. It goes by the simple |
|
| 183 | - * |
|
| 184 | - * @return boolean Whether or not the data validation has succeeded |
|
| 185 | - */ |
|
| 186 | - public function isSuccess() { |
|
| 187 | - return $this->_success; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Checks if the request method is POST or the Data to be validated is set |
|
| 192 | - * |
|
| 193 | - * @return boolean Whether or not the form has been submitted or the data is available for validation. |
|
| 194 | - */ |
|
| 195 | - public function canDoValidation() { |
|
| 196 | - return get_instance()->request->method() === 'POST' || ! empty($this->data); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Runs _run once POST data has been submitted or data is set manually. |
|
| 201 | - * |
|
| 202 | - * @return boolean |
|
| 203 | - */ |
|
| 204 | - public function run() { |
|
| 205 | - if ($this->canDoValidation()) { |
|
| 206 | - $this->logger->info('The data to validate are listed below: ' . stringfy_vars($this->getData())); |
|
| 207 | - $this->_run(); |
|
| 208 | - } |
|
| 209 | - return $this->isSuccess(); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Validate the CSRF |
|
| 214 | - * @return void |
|
| 215 | - */ |
|
| 216 | - protected function validateCSRF(){ |
|
| 217 | - if(get_instance()->request->method() == 'POST' || $this->enableCsrfCheck){ |
|
| 218 | - $this->logger->debug('Check if CSRF is enabled in configuration'); |
|
| 219 | - //first check for CSRF |
|
| 220 | - if ((get_config('csrf_enable', false) || $this->enableCsrfCheck) && ! Security::validateCSRF()){ |
|
| 221 | - show_error('Invalide data, Cross Site Request Forgery do his job, the data to validate is corrupted.'); |
|
| 222 | - } |
|
| 223 | - else{ |
|
| 224 | - $this->logger->info('CSRF is not enabled in configuration or not set manully, no need to check it'); |
|
| 225 | - } |
|
| 226 | - } |
|
| 227 | - } |
|
| 228 | - /** |
|
| 229 | - * Takes and trims each data, if it has any rules, we parse the rule string and run |
|
| 230 | - * each rule against the data value. Sets _success to true if there are no errors |
|
| 231 | - * afterwards. |
|
| 232 | - */ |
|
| 233 | - protected function _run() { |
|
| 234 | - //validate CSRF |
|
| 235 | - $this->validateCSRF(); |
|
| 236 | - ///////////////////////////////////////////// |
|
| 237 | - $this->_forceFail = false; |
|
| 238 | - |
|
| 239 | - foreach ($this->getData() as $inputName => $inputVal) { |
|
| 240 | - if(is_array($this->data[$inputName])){ |
|
| 241 | - $this->data[$inputName] = array_map('trim', $this->data[$inputName]); |
|
| 242 | - } |
|
| 243 | - else{ |
|
| 244 | - $this->data[$inputName] = trim($this->data[$inputName]); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - if (array_key_exists($inputName, $this->_rules)) { |
|
| 248 | - foreach ($this->_parseRuleString($this->_rules[$inputName]) as $eachRule) { |
|
| 249 | - $this->_validateRule($inputName, $this->data[$inputName], $eachRule); |
|
| 250 | - } |
|
| 251 | - } |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - if (empty($this->_errors) && $this->_forceFail === false) { |
|
| 255 | - $this->_success = true; |
|
| 256 | - } |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * Adds a rule to a form data validation field. |
|
| 261 | - * |
|
| 262 | - * @param string $inputField Name of the field or the data key to add a rule to |
|
| 263 | - * @param string $ruleSets PIPE seperated string of rules |
|
| 264 | - * |
|
| 265 | - * @return FormValidation Current instance of object. |
|
| 266 | - */ |
|
| 267 | - public function setRule($inputField, $inputLabel, $ruleSets) { |
|
| 268 | - $this->_rules[$inputField] = $ruleSets; |
|
| 269 | - $this->_labels[$inputField] = $inputLabel; |
|
| 270 | - $this->logger->info('Set the field rule: name [' .$inputField. '], label [' .$inputLabel. '], rules [' .$ruleSets. ']'); |
|
| 271 | - return $this; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * Takes an array of rules and uses setRule() to set them, accepts an array |
|
| 276 | - * of rule names rather than a pipe-delimited string as well. |
|
| 277 | - * @param array $ruleSets |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * Get the form validation data |
|
| 161 | + * @return array the form validation data to be validated |
|
| 162 | + */ |
|
| 163 | + public function getData(){ |
|
| 164 | + return $this->data; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Get the validation function name to validate a rule |
|
| 169 | + * |
|
| 170 | + * @return string the function name |
|
| 171 | + */ |
|
| 172 | + protected function _toCallCase($funcName, $prefix='_validate') { |
|
| 173 | + $funcName = strtolower($funcName); |
|
| 174 | + $finalFuncName = $prefix; |
|
| 175 | + foreach (explode('_', $funcName) as $funcNamePart) { |
|
| 176 | + $finalFuncName .= strtoupper($funcNamePart[0]) . substr($funcNamePart, 1); |
|
| 177 | + } |
|
| 178 | + return $finalFuncName; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Returns the boolean of the data status success. It goes by the simple |
|
| 183 | + * |
|
| 184 | + * @return boolean Whether or not the data validation has succeeded |
|
| 185 | + */ |
|
| 186 | + public function isSuccess() { |
|
| 187 | + return $this->_success; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Checks if the request method is POST or the Data to be validated is set |
|
| 192 | + * |
|
| 193 | + * @return boolean Whether or not the form has been submitted or the data is available for validation. |
|
| 194 | + */ |
|
| 195 | + public function canDoValidation() { |
|
| 196 | + return get_instance()->request->method() === 'POST' || ! empty($this->data); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Runs _run once POST data has been submitted or data is set manually. |
|
| 201 | + * |
|
| 202 | + * @return boolean |
|
| 203 | + */ |
|
| 204 | + public function run() { |
|
| 205 | + if ($this->canDoValidation()) { |
|
| 206 | + $this->logger->info('The data to validate are listed below: ' . stringfy_vars($this->getData())); |
|
| 207 | + $this->_run(); |
|
| 208 | + } |
|
| 209 | + return $this->isSuccess(); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Validate the CSRF |
|
| 214 | + * @return void |
|
| 215 | + */ |
|
| 216 | + protected function validateCSRF(){ |
|
| 217 | + if(get_instance()->request->method() == 'POST' || $this->enableCsrfCheck){ |
|
| 218 | + $this->logger->debug('Check if CSRF is enabled in configuration'); |
|
| 219 | + //first check for CSRF |
|
| 220 | + if ((get_config('csrf_enable', false) || $this->enableCsrfCheck) && ! Security::validateCSRF()){ |
|
| 221 | + show_error('Invalide data, Cross Site Request Forgery do his job, the data to validate is corrupted.'); |
|
| 222 | + } |
|
| 223 | + else{ |
|
| 224 | + $this->logger->info('CSRF is not enabled in configuration or not set manully, no need to check it'); |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | + } |
|
| 228 | + /** |
|
| 229 | + * Takes and trims each data, if it has any rules, we parse the rule string and run |
|
| 230 | + * each rule against the data value. Sets _success to true if there are no errors |
|
| 231 | + * afterwards. |
|
| 232 | + */ |
|
| 233 | + protected function _run() { |
|
| 234 | + //validate CSRF |
|
| 235 | + $this->validateCSRF(); |
|
| 236 | + ///////////////////////////////////////////// |
|
| 237 | + $this->_forceFail = false; |
|
| 238 | + |
|
| 239 | + foreach ($this->getData() as $inputName => $inputVal) { |
|
| 240 | + if(is_array($this->data[$inputName])){ |
|
| 241 | + $this->data[$inputName] = array_map('trim', $this->data[$inputName]); |
|
| 242 | + } |
|
| 243 | + else{ |
|
| 244 | + $this->data[$inputName] = trim($this->data[$inputName]); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + if (array_key_exists($inputName, $this->_rules)) { |
|
| 248 | + foreach ($this->_parseRuleString($this->_rules[$inputName]) as $eachRule) { |
|
| 249 | + $this->_validateRule($inputName, $this->data[$inputName], $eachRule); |
|
| 250 | + } |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + if (empty($this->_errors) && $this->_forceFail === false) { |
|
| 255 | + $this->_success = true; |
|
| 256 | + } |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * Adds a rule to a form data validation field. |
|
| 261 | + * |
|
| 262 | + * @param string $inputField Name of the field or the data key to add a rule to |
|
| 263 | + * @param string $ruleSets PIPE seperated string of rules |
|
| 278 | 264 | * |
| 279 | 265 | * @return FormValidation Current instance of object. |
| 280 | - */ |
|
| 281 | - public function setRules(array $ruleSets) { |
|
| 282 | - foreach ($ruleSets as $ruleSet) { |
|
| 283 | - $pipeDelimitedRules = null; |
|
| 284 | - if (is_array($ruleSet['rules'])) { |
|
| 285 | - $pipeDelimitedRules = implode('|', $ruleSet['rules']); |
|
| 286 | - } else { |
|
| 287 | - $pipeDelimitedRules = $ruleSet['rules']; |
|
| 288 | - } |
|
| 289 | - $this->setRule($ruleSet['name'], $ruleSet['label'], $pipeDelimitedRules); |
|
| 290 | - } |
|
| 291 | - return $this; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * This method creates the global errors delimiter, each argument occurs once, at the beginning, and |
|
| 296 | - * end of the errors block respectively. |
|
| 297 | - * |
|
| 298 | - * @param string $start Before block of errors gets displayed, HTML allowed. |
|
| 299 | - * @param string $end After the block of errors gets displayed, HTML allowed. |
|
| 300 | - * |
|
| 266 | + */ |
|
| 267 | + public function setRule($inputField, $inputLabel, $ruleSets) { |
|
| 268 | + $this->_rules[$inputField] = $ruleSets; |
|
| 269 | + $this->_labels[$inputField] = $inputLabel; |
|
| 270 | + $this->logger->info('Set the field rule: name [' .$inputField. '], label [' .$inputLabel. '], rules [' .$ruleSets. ']'); |
|
| 271 | + return $this; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * Takes an array of rules and uses setRule() to set them, accepts an array |
|
| 276 | + * of rule names rather than a pipe-delimited string as well. |
|
| 277 | + * @param array $ruleSets |
|
| 278 | + * |
|
| 301 | 279 | * @return FormValidation Current instance of object. |
| 302 | - */ |
|
| 303 | - public function setErrorsDelimiter($start, $end) { |
|
| 304 | - $this->_allErrorsDelimiter[0] = $start; |
|
| 305 | - $this->_allErrorsDelimiter[1] = $end; |
|
| 306 | - return $this; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * This is the individual error delimiter, each argument occurs once before and after |
|
| 311 | - * each individual error listed. |
|
| 312 | - * |
|
| 313 | - * @param string $start Displayed before each error. |
|
| 314 | - * @param string $end Displayed after each error. |
|
| 315 | - * |
|
| 280 | + */ |
|
| 281 | + public function setRules(array $ruleSets) { |
|
| 282 | + foreach ($ruleSets as $ruleSet) { |
|
| 283 | + $pipeDelimitedRules = null; |
|
| 284 | + if (is_array($ruleSet['rules'])) { |
|
| 285 | + $pipeDelimitedRules = implode('|', $ruleSet['rules']); |
|
| 286 | + } else { |
|
| 287 | + $pipeDelimitedRules = $ruleSet['rules']; |
|
| 288 | + } |
|
| 289 | + $this->setRule($ruleSet['name'], $ruleSet['label'], $pipeDelimitedRules); |
|
| 290 | + } |
|
| 291 | + return $this; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * This method creates the global errors delimiter, each argument occurs once, at the beginning, and |
|
| 296 | + * end of the errors block respectively. |
|
| 297 | + * |
|
| 298 | + * @param string $start Before block of errors gets displayed, HTML allowed. |
|
| 299 | + * @param string $end After the block of errors gets displayed, HTML allowed. |
|
| 300 | + * |
|
| 316 | 301 | * @return FormValidation Current instance of object. |
| 317 | - */ |
|
| 318 | - public function setErrorDelimiter($start, $end) { |
|
| 319 | - $this->_eachErrorDelimiter[0] = $start; |
|
| 320 | - $this->_eachErrorDelimiter[1] = $end; |
|
| 321 | - return $this; |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - /** |
|
| 325 | - * Get the each errors delimiters |
|
| 326 | - * |
|
| 327 | - * @return array |
|
| 328 | - */ |
|
| 329 | - public function getErrorDelimiter() { |
|
| 330 | - return $this->_eachErrorDelimiter; |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * Get the all errors delimiters |
|
| 335 | - * |
|
| 336 | - * @return array |
|
| 337 | - */ |
|
| 338 | - public function getErrorsDelimiter() { |
|
| 339 | - return $this->_allErrorsDelimiter; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * This sets a custom error message that can override the default error phrase provided |
|
| 344 | - * by FormValidation, it can be used in the format of setMessage('rule', 'error phrase') |
|
| 345 | - * which will globally change the error phrase of that rule, or in the format of: |
|
| 346 | - * setMessage('rule', 'fieldname', 'error phrase') - which will only change the error phrase for |
|
| 347 | - * that rule, applied on that field. |
|
| 348 | - * |
|
| 349 | - * @return boolean True on success, false on failure. |
|
| 350 | - */ |
|
| 351 | - public function setMessage() { |
|
| 352 | - $numArgs = func_num_args(); |
|
| 353 | - switch ($numArgs) { |
|
| 354 | - default: |
|
| 355 | - return false; |
|
| 356 | - // A global rule error message |
|
| 357 | - case 2: |
|
| 358 | - foreach ($this->post(null) as $key => $val) { |
|
| 359 | - $this->_errorPhraseOverrides[$key][func_get_arg(0)] = func_get_arg(1); |
|
| 360 | - } |
|
| 361 | - break; |
|
| 362 | - // Field specific rule error message |
|
| 363 | - case 3: |
|
| 364 | - $this->_errorPhraseOverrides[func_get_arg(1)][func_get_arg(0)] = func_get_arg(2); |
|
| 365 | - break; |
|
| 366 | - } |
|
| 367 | - return true; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * Adds a custom error message in the errorSet array, that will |
|
| 372 | - * forcibly display it. |
|
| 373 | - * |
|
| 374 | - * @param string $inputName The form input name or data key |
|
| 375 | - * @param string $errorMessage Error to display |
|
| 376 | - * |
|
| 377 | - * @return formValidation Current instance of the object |
|
| 378 | - */ |
|
| 379 | - public function setCustomError($inputName, $errorMessage) { |
|
| 380 | - $errorMessage = str_replace('%1', $this->_labels[$inputName], $errorMessage); |
|
| 381 | - $this->_errors[$inputName] = $errorMessage; |
|
| 382 | - return $this; |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - /** |
|
| 386 | - * Allows for an accesor to any/all post values, if a value of null is passed as the key, it |
|
| 387 | - * will recursively find all keys/values of the $_POST array or data array. It also automatically trims |
|
| 388 | - * all values. |
|
| 389 | - * |
|
| 390 | - * @param string $key Key of $this->data to be found, pass null for all Key => Val pairs. |
|
| 391 | - * @param boolean $trim Defaults to true, trims all $this->data values. |
|
| 392 | - * @return string|array Array of post or data values if null is passed as key, string if only one key is desired. |
|
| 393 | - */ |
|
| 394 | - public function post($key = null, $trim = true) { |
|
| 395 | - $returnValue = null; |
|
| 396 | - if (is_null($key)) { |
|
| 397 | - $returnValue = array(); |
|
| 398 | - foreach ($this->getData() as $key => $val) { |
|
| 399 | - $returnValue[$key] = $this->post($key, $trim); |
|
| 400 | - } |
|
| 401 | - } else { |
|
| 402 | - $returnValue = (array_key_exists($key, $this->getData())) ? (($trim) ? trim($this->data[$key]) : $this->data[$key]) : null; |
|
| 403 | - } |
|
| 404 | - return $returnValue; |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - /** |
|
| 408 | - * Gets all errors from errorSet and displays them, can be echo out from the |
|
| 409 | - * function or just returned. |
|
| 410 | - * |
|
| 411 | - * @param boolean $limit number of error to display or return |
|
| 412 | - * @param boolean $echo Whether or not the values are to be returned or displayed |
|
| 413 | - * |
|
| 414 | - * @return string Errors formatted for output |
|
| 415 | - */ |
|
| 416 | - public function displayErrors($limit = null, $echo = true) { |
|
| 417 | - list($errorsStart, $errorsEnd) = $this->_allErrorsDelimiter; |
|
| 418 | - list($errorStart, $errorEnd) = $this->_eachErrorDelimiter; |
|
| 419 | - $errorOutput = $errorsStart; |
|
| 420 | - $i = 0; |
|
| 421 | - if (!empty($this->_errors)) { |
|
| 422 | - foreach ($this->_errors as $fieldName => $error) { |
|
| 423 | - if ($i === $limit) { |
|
| 424 | - break; |
|
| 425 | - } |
|
| 426 | - $errorOutput .= $errorStart; |
|
| 427 | - $errorOutput .= $error; |
|
| 428 | - $errorOutput .= $errorEnd; |
|
| 429 | - $i++; |
|
| 430 | - } |
|
| 431 | - } |
|
| 432 | - $errorOutput .= $errorsEnd; |
|
| 433 | - echo ($echo) ? $errorOutput : ''; |
|
| 434 | - return (! $echo) ? $errorOutput : null; |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - /** |
|
| 438 | - * Returns raw array of errors in no format instead of displaying them |
|
| 439 | - * formatted. |
|
| 440 | - * |
|
| 441 | - * @return array |
|
| 442 | - */ |
|
| 443 | - public function returnErrors() { |
|
| 444 | - return $this->_errors; |
|
| 445 | - } |
|
| 446 | - |
|
| 447 | - /** |
|
| 448 | - * Breaks up a PIPE seperated string of rules, and puts them into an array. |
|
| 449 | - * |
|
| 450 | - * @param string $ruleString String to be parsed. |
|
| 451 | - * |
|
| 452 | - * @return array Array of each value in original string. |
|
| 453 | - */ |
|
| 454 | - protected function _parseRuleString($ruleString) { |
|
| 455 | - $ruleSets = array(); |
|
| 456 | - /* |
|
| 302 | + */ |
|
| 303 | + public function setErrorsDelimiter($start, $end) { |
|
| 304 | + $this->_allErrorsDelimiter[0] = $start; |
|
| 305 | + $this->_allErrorsDelimiter[1] = $end; |
|
| 306 | + return $this; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * This is the individual error delimiter, each argument occurs once before and after |
|
| 311 | + * each individual error listed. |
|
| 312 | + * |
|
| 313 | + * @param string $start Displayed before each error. |
|
| 314 | + * @param string $end Displayed after each error. |
|
| 315 | + * |
|
| 316 | + * @return FormValidation Current instance of object. |
|
| 317 | + */ |
|
| 318 | + public function setErrorDelimiter($start, $end) { |
|
| 319 | + $this->_eachErrorDelimiter[0] = $start; |
|
| 320 | + $this->_eachErrorDelimiter[1] = $end; |
|
| 321 | + return $this; |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + /** |
|
| 325 | + * Get the each errors delimiters |
|
| 326 | + * |
|
| 327 | + * @return array |
|
| 328 | + */ |
|
| 329 | + public function getErrorDelimiter() { |
|
| 330 | + return $this->_eachErrorDelimiter; |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * Get the all errors delimiters |
|
| 335 | + * |
|
| 336 | + * @return array |
|
| 337 | + */ |
|
| 338 | + public function getErrorsDelimiter() { |
|
| 339 | + return $this->_allErrorsDelimiter; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * This sets a custom error message that can override the default error phrase provided |
|
| 344 | + * by FormValidation, it can be used in the format of setMessage('rule', 'error phrase') |
|
| 345 | + * which will globally change the error phrase of that rule, or in the format of: |
|
| 346 | + * setMessage('rule', 'fieldname', 'error phrase') - which will only change the error phrase for |
|
| 347 | + * that rule, applied on that field. |
|
| 348 | + * |
|
| 349 | + * @return boolean True on success, false on failure. |
|
| 350 | + */ |
|
| 351 | + public function setMessage() { |
|
| 352 | + $numArgs = func_num_args(); |
|
| 353 | + switch ($numArgs) { |
|
| 354 | + default: |
|
| 355 | + return false; |
|
| 356 | + // A global rule error message |
|
| 357 | + case 2: |
|
| 358 | + foreach ($this->post(null) as $key => $val) { |
|
| 359 | + $this->_errorPhraseOverrides[$key][func_get_arg(0)] = func_get_arg(1); |
|
| 360 | + } |
|
| 361 | + break; |
|
| 362 | + // Field specific rule error message |
|
| 363 | + case 3: |
|
| 364 | + $this->_errorPhraseOverrides[func_get_arg(1)][func_get_arg(0)] = func_get_arg(2); |
|
| 365 | + break; |
|
| 366 | + } |
|
| 367 | + return true; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * Adds a custom error message in the errorSet array, that will |
|
| 372 | + * forcibly display it. |
|
| 373 | + * |
|
| 374 | + * @param string $inputName The form input name or data key |
|
| 375 | + * @param string $errorMessage Error to display |
|
| 376 | + * |
|
| 377 | + * @return formValidation Current instance of the object |
|
| 378 | + */ |
|
| 379 | + public function setCustomError($inputName, $errorMessage) { |
|
| 380 | + $errorMessage = str_replace('%1', $this->_labels[$inputName], $errorMessage); |
|
| 381 | + $this->_errors[$inputName] = $errorMessage; |
|
| 382 | + return $this; |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + /** |
|
| 386 | + * Allows for an accesor to any/all post values, if a value of null is passed as the key, it |
|
| 387 | + * will recursively find all keys/values of the $_POST array or data array. It also automatically trims |
|
| 388 | + * all values. |
|
| 389 | + * |
|
| 390 | + * @param string $key Key of $this->data to be found, pass null for all Key => Val pairs. |
|
| 391 | + * @param boolean $trim Defaults to true, trims all $this->data values. |
|
| 392 | + * @return string|array Array of post or data values if null is passed as key, string if only one key is desired. |
|
| 393 | + */ |
|
| 394 | + public function post($key = null, $trim = true) { |
|
| 395 | + $returnValue = null; |
|
| 396 | + if (is_null($key)) { |
|
| 397 | + $returnValue = array(); |
|
| 398 | + foreach ($this->getData() as $key => $val) { |
|
| 399 | + $returnValue[$key] = $this->post($key, $trim); |
|
| 400 | + } |
|
| 401 | + } else { |
|
| 402 | + $returnValue = (array_key_exists($key, $this->getData())) ? (($trim) ? trim($this->data[$key]) : $this->data[$key]) : null; |
|
| 403 | + } |
|
| 404 | + return $returnValue; |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * Gets all errors from errorSet and displays them, can be echo out from the |
|
| 409 | + * function or just returned. |
|
| 410 | + * |
|
| 411 | + * @param boolean $limit number of error to display or return |
|
| 412 | + * @param boolean $echo Whether or not the values are to be returned or displayed |
|
| 413 | + * |
|
| 414 | + * @return string Errors formatted for output |
|
| 415 | + */ |
|
| 416 | + public function displayErrors($limit = null, $echo = true) { |
|
| 417 | + list($errorsStart, $errorsEnd) = $this->_allErrorsDelimiter; |
|
| 418 | + list($errorStart, $errorEnd) = $this->_eachErrorDelimiter; |
|
| 419 | + $errorOutput = $errorsStart; |
|
| 420 | + $i = 0; |
|
| 421 | + if (!empty($this->_errors)) { |
|
| 422 | + foreach ($this->_errors as $fieldName => $error) { |
|
| 423 | + if ($i === $limit) { |
|
| 424 | + break; |
|
| 425 | + } |
|
| 426 | + $errorOutput .= $errorStart; |
|
| 427 | + $errorOutput .= $error; |
|
| 428 | + $errorOutput .= $errorEnd; |
|
| 429 | + $i++; |
|
| 430 | + } |
|
| 431 | + } |
|
| 432 | + $errorOutput .= $errorsEnd; |
|
| 433 | + echo ($echo) ? $errorOutput : ''; |
|
| 434 | + return (! $echo) ? $errorOutput : null; |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + /** |
|
| 438 | + * Returns raw array of errors in no format instead of displaying them |
|
| 439 | + * formatted. |
|
| 440 | + * |
|
| 441 | + * @return array |
|
| 442 | + */ |
|
| 443 | + public function returnErrors() { |
|
| 444 | + return $this->_errors; |
|
| 445 | + } |
|
| 446 | + |
|
| 447 | + /** |
|
| 448 | + * Breaks up a PIPE seperated string of rules, and puts them into an array. |
|
| 449 | + * |
|
| 450 | + * @param string $ruleString String to be parsed. |
|
| 451 | + * |
|
| 452 | + * @return array Array of each value in original string. |
|
| 453 | + */ |
|
| 454 | + protected function _parseRuleString($ruleString) { |
|
| 455 | + $ruleSets = array(); |
|
| 456 | + /* |
|
| 457 | 457 | //////////////// hack for regex rule that can contain "|" |
| 458 | 458 | */ |
| 459 | - if(strpos($ruleString, 'regex') !== false){ |
|
| 460 | - $regexRule = array(); |
|
| 461 | - $rule = '#regex\[\/(.*)\/([a-zA-Z0-9]?)\]#'; |
|
| 462 | - preg_match($rule, $ruleString, $regexRule); |
|
| 463 | - $ruleStringTemp = preg_replace($rule, '', $ruleString); |
|
| 464 | - if(!empty($regexRule[0])){ |
|
| 465 | - $ruleSets[] = $regexRule[0]; |
|
| 466 | - } |
|
| 467 | - $ruleStringRegex = explode('|', $ruleStringTemp); |
|
| 468 | - foreach ($ruleStringRegex as $rule) { |
|
| 469 | - $rule = trim($rule); |
|
| 470 | - if($rule){ |
|
| 471 | - $ruleSets[] = $rule; |
|
| 472 | - } |
|
| 473 | - } |
|
| 459 | + if(strpos($ruleString, 'regex') !== false){ |
|
| 460 | + $regexRule = array(); |
|
| 461 | + $rule = '#regex\[\/(.*)\/([a-zA-Z0-9]?)\]#'; |
|
| 462 | + preg_match($rule, $ruleString, $regexRule); |
|
| 463 | + $ruleStringTemp = preg_replace($rule, '', $ruleString); |
|
| 464 | + if(!empty($regexRule[0])){ |
|
| 465 | + $ruleSets[] = $regexRule[0]; |
|
| 466 | + } |
|
| 467 | + $ruleStringRegex = explode('|', $ruleStringTemp); |
|
| 468 | + foreach ($ruleStringRegex as $rule) { |
|
| 469 | + $rule = trim($rule); |
|
| 470 | + if($rule){ |
|
| 471 | + $ruleSets[] = $rule; |
|
| 472 | + } |
|
| 473 | + } |
|
| 474 | 474 | |
| 475 | - } |
|
| 476 | - /***********************************/ |
|
| 477 | - else{ |
|
| 478 | - if (strpos($ruleString, '|') !== FALSE) { |
|
| 479 | - $ruleSets = explode('|', $ruleString); |
|
| 480 | - } else { |
|
| 481 | - $ruleSets[] = $ruleString; |
|
| 482 | - } |
|
| 483 | - } |
|
| 484 | - return $ruleSets; |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - /** |
|
| 488 | - * Returns whether or not a field obtains the rule "required". |
|
| 489 | - * |
|
| 490 | - * @param string $fieldName Field to check if required. |
|
| 491 | - * |
|
| 492 | - * @return boolean Whether or not the field is required. |
|
| 493 | - */ |
|
| 494 | - protected function _fieldIsRequired($fieldName) { |
|
| 495 | - $rules = $this->_parseRuleString($this->_rules[$fieldName]); |
|
| 496 | - return (in_array('required', $rules)); |
|
| 497 | - } |
|
| 498 | - |
|
| 499 | - /** |
|
| 500 | - * Takes a data input name, it's value, and the rule it's being validated against (ex: max_length[16]) |
|
| 501 | - * and adds an error to the errorSet if it fails validation of the rule. |
|
| 502 | - * |
|
| 503 | - * @param string $inputName Name or key of the validation data |
|
| 504 | - * @param string $inputVal Value of the validation data |
|
| 505 | - * @param string $ruleName Rule to be validated against, including args (exact_length[5]) |
|
| 506 | - * @return void |
|
| 507 | - */ |
|
| 508 | - protected function _validateRule($inputName, $inputVal, $ruleName) { |
|
| 509 | - $this->logger->debug('Rule validation of field [' .$inputName. '], value [' .$inputVal. '], rule [' .$ruleName. ']'); |
|
| 510 | - // Array to store args |
|
| 511 | - $ruleArgs = array(); |
|
| 512 | - |
|
| 513 | - preg_match('/\[(.*)\]/', $ruleName, $ruleArgs); |
|
| 514 | - |
|
| 515 | - // Get the rule arguments, realRule is just the base rule name |
|
| 516 | - // Like min_length instead of min_length[3] |
|
| 517 | - $ruleName = preg_replace('/\[(.*)\]/', '', $ruleName); |
|
| 475 | + } |
|
| 476 | + /***********************************/ |
|
| 477 | + else{ |
|
| 478 | + if (strpos($ruleString, '|') !== FALSE) { |
|
| 479 | + $ruleSets = explode('|', $ruleString); |
|
| 480 | + } else { |
|
| 481 | + $ruleSets[] = $ruleString; |
|
| 482 | + } |
|
| 483 | + } |
|
| 484 | + return $ruleSets; |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + /** |
|
| 488 | + * Returns whether or not a field obtains the rule "required". |
|
| 489 | + * |
|
| 490 | + * @param string $fieldName Field to check if required. |
|
| 491 | + * |
|
| 492 | + * @return boolean Whether or not the field is required. |
|
| 493 | + */ |
|
| 494 | + protected function _fieldIsRequired($fieldName) { |
|
| 495 | + $rules = $this->_parseRuleString($this->_rules[$fieldName]); |
|
| 496 | + return (in_array('required', $rules)); |
|
| 497 | + } |
|
| 498 | + |
|
| 499 | + /** |
|
| 500 | + * Takes a data input name, it's value, and the rule it's being validated against (ex: max_length[16]) |
|
| 501 | + * and adds an error to the errorSet if it fails validation of the rule. |
|
| 502 | + * |
|
| 503 | + * @param string $inputName Name or key of the validation data |
|
| 504 | + * @param string $inputVal Value of the validation data |
|
| 505 | + * @param string $ruleName Rule to be validated against, including args (exact_length[5]) |
|
| 506 | + * @return void |
|
| 507 | + */ |
|
| 508 | + protected function _validateRule($inputName, $inputVal, $ruleName) { |
|
| 509 | + $this->logger->debug('Rule validation of field [' .$inputName. '], value [' .$inputVal. '], rule [' .$ruleName. ']'); |
|
| 510 | + // Array to store args |
|
| 511 | + $ruleArgs = array(); |
|
| 512 | + |
|
| 513 | + preg_match('/\[(.*)\]/', $ruleName, $ruleArgs); |
|
| 514 | + |
|
| 515 | + // Get the rule arguments, realRule is just the base rule name |
|
| 516 | + // Like min_length instead of min_length[3] |
|
| 517 | + $ruleName = preg_replace('/\[(.*)\]/', '', $ruleName); |
|
| 518 | 518 | |
| 519 | - if (method_exists($this, $this->_toCallCase($ruleName))) { |
|
| 520 | - $methodToCall = $this->_toCallCase($ruleName); |
|
| 521 | - call_user_func(array($this, $methodToCall), $inputName, $ruleName, $ruleArgs); |
|
| 522 | - } |
|
| 523 | - return; |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - /** |
|
| 527 | - * Set error for the given field or key |
|
| 528 | - * |
|
| 529 | - * @param string $inputName the input or key name |
|
| 530 | - * @param string $ruleName the rule name |
|
| 531 | - * @param array|string $replacements |
|
| 532 | - */ |
|
| 533 | - protected function _setError($inputName, $ruleName, $replacements = array()) { |
|
| 534 | - $rulePhraseKeyParts = explode(',', $ruleName); |
|
| 535 | - $rulePhrase = null; |
|
| 536 | - foreach ($rulePhraseKeyParts as $rulePhraseKeyPart) { |
|
| 537 | - if (array_key_exists($rulePhraseKeyPart, $this->_errorsMessages)) { |
|
| 538 | - $rulePhrase = $this->_errorsMessages[$rulePhraseKeyPart]; |
|
| 539 | - } else { |
|
| 540 | - $rulePhrase = $rulePhrase[$rulePhraseKeyPart]; |
|
| 541 | - } |
|
| 542 | - } |
|
| 543 | - // Any overrides? |
|
| 544 | - if (array_key_exists($inputName, $this->_errorPhraseOverrides) && array_key_exists($ruleName, $this->_errorPhraseOverrides[$inputName])) { |
|
| 545 | - $rulePhrase = $this->_errorPhraseOverrides[$inputName][$ruleName]; |
|
| 546 | - } |
|
| 547 | - // Type cast to array in case it's a string |
|
| 548 | - $replacements = (array) $replacements; |
|
| 519 | + if (method_exists($this, $this->_toCallCase($ruleName))) { |
|
| 520 | + $methodToCall = $this->_toCallCase($ruleName); |
|
| 521 | + call_user_func(array($this, $methodToCall), $inputName, $ruleName, $ruleArgs); |
|
| 522 | + } |
|
| 523 | + return; |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + /** |
|
| 527 | + * Set error for the given field or key |
|
| 528 | + * |
|
| 529 | + * @param string $inputName the input or key name |
|
| 530 | + * @param string $ruleName the rule name |
|
| 531 | + * @param array|string $replacements |
|
| 532 | + */ |
|
| 533 | + protected function _setError($inputName, $ruleName, $replacements = array()) { |
|
| 534 | + $rulePhraseKeyParts = explode(',', $ruleName); |
|
| 535 | + $rulePhrase = null; |
|
| 536 | + foreach ($rulePhraseKeyParts as $rulePhraseKeyPart) { |
|
| 537 | + if (array_key_exists($rulePhraseKeyPart, $this->_errorsMessages)) { |
|
| 538 | + $rulePhrase = $this->_errorsMessages[$rulePhraseKeyPart]; |
|
| 539 | + } else { |
|
| 540 | + $rulePhrase = $rulePhrase[$rulePhraseKeyPart]; |
|
| 541 | + } |
|
| 542 | + } |
|
| 543 | + // Any overrides? |
|
| 544 | + if (array_key_exists($inputName, $this->_errorPhraseOverrides) && array_key_exists($ruleName, $this->_errorPhraseOverrides[$inputName])) { |
|
| 545 | + $rulePhrase = $this->_errorPhraseOverrides[$inputName][$ruleName]; |
|
| 546 | + } |
|
| 547 | + // Type cast to array in case it's a string |
|
| 548 | + $replacements = (array) $replacements; |
|
| 549 | 549 | $replacementCount = count($replacements); |
| 550 | - for ($i = 1; $i <= $replacementCount; $i++) { |
|
| 551 | - $key = $i - 1; |
|
| 552 | - $rulePhrase = str_replace('%' . $i, $replacements[$key], $rulePhrase); |
|
| 553 | - } |
|
| 554 | - if (! array_key_exists($inputName, $this->_errors)) { |
|
| 555 | - $this->_errors[$inputName] = $rulePhrase; |
|
| 556 | - } |
|
| 557 | - } |
|
| 558 | - |
|
| 559 | - /** |
|
| 560 | - * Used to run a callback for the callback rule, as well as pass in a default |
|
| 561 | - * argument of the post value. For example the username field having a rule: |
|
| 562 | - * callback[userExists] will eval userExists(data[username]) - Note the use |
|
| 563 | - * of eval over call_user_func is in case the function is not user defined. |
|
| 564 | - * |
|
| 565 | - * @param type $inputArg |
|
| 566 | - * @param string $callbackFunc |
|
| 567 | - * |
|
| 568 | - * @return mixed |
|
| 569 | - */ |
|
| 570 | - protected function _runCallback($inputArg, $callbackFunc) { |
|
| 550 | + for ($i = 1; $i <= $replacementCount; $i++) { |
|
| 551 | + $key = $i - 1; |
|
| 552 | + $rulePhrase = str_replace('%' . $i, $replacements[$key], $rulePhrase); |
|
| 553 | + } |
|
| 554 | + if (! array_key_exists($inputName, $this->_errors)) { |
|
| 555 | + $this->_errors[$inputName] = $rulePhrase; |
|
| 556 | + } |
|
| 557 | + } |
|
| 558 | + |
|
| 559 | + /** |
|
| 560 | + * Used to run a callback for the callback rule, as well as pass in a default |
|
| 561 | + * argument of the post value. For example the username field having a rule: |
|
| 562 | + * callback[userExists] will eval userExists(data[username]) - Note the use |
|
| 563 | + * of eval over call_user_func is in case the function is not user defined. |
|
| 564 | + * |
|
| 565 | + * @param type $inputArg |
|
| 566 | + * @param string $callbackFunc |
|
| 567 | + * |
|
| 568 | + * @return mixed |
|
| 569 | + */ |
|
| 570 | + protected function _runCallback($inputArg, $callbackFunc) { |
|
| 571 | 571 | return eval('return ' . $callbackFunc . '("' . $inputArg . '");'); |
| 572 | - } |
|
| 573 | - |
|
| 574 | - /** |
|
| 575 | - * Used for applying a rule only if the empty callback evaluates to true, |
|
| 576 | - * for example required[funcName] - This runs funcName without passing any |
|
| 577 | - * arguments. |
|
| 578 | - * |
|
| 579 | - * @param string $callbackFunc |
|
| 580 | - * |
|
| 581 | - * @return mixed |
|
| 582 | - */ |
|
| 583 | - protected function _runEmptyCallback($callbackFunc) { |
|
| 584 | - return eval('return ' . $callbackFunc . '();'); |
|
| 585 | - } |
|
| 586 | - |
|
| 587 | - /** |
|
| 588 | - * Gets a specific label of a specific field input name. |
|
| 589 | - * |
|
| 590 | - * @param string $inputName |
|
| 591 | - * |
|
| 592 | - * @return string |
|
| 593 | - */ |
|
| 594 | - protected function _getLabel($inputName) { |
|
| 595 | - return (array_key_exists($inputName, $this->_labels)) ? $this->_labels[$inputName] : $inputName; |
|
| 596 | - } |
|
| 572 | + } |
|
| 573 | + |
|
| 574 | + /** |
|
| 575 | + * Used for applying a rule only if the empty callback evaluates to true, |
|
| 576 | + * for example required[funcName] - This runs funcName without passing any |
|
| 577 | + * arguments. |
|
| 578 | + * |
|
| 579 | + * @param string $callbackFunc |
|
| 580 | + * |
|
| 581 | + * @return mixed |
|
| 582 | + */ |
|
| 583 | + protected function _runEmptyCallback($callbackFunc) { |
|
| 584 | + return eval('return ' . $callbackFunc . '();'); |
|
| 585 | + } |
|
| 586 | + |
|
| 587 | + /** |
|
| 588 | + * Gets a specific label of a specific field input name. |
|
| 589 | + * |
|
| 590 | + * @param string $inputName |
|
| 591 | + * |
|
| 592 | + * @return string |
|
| 593 | + */ |
|
| 594 | + protected function _getLabel($inputName) { |
|
| 595 | + return (array_key_exists($inputName, $this->_labels)) ? $this->_labels[$inputName] : $inputName; |
|
| 596 | + } |
|
| 597 | 597 | |
| 598 | - /** |
|
| 599 | - * Peform validation for the rule "required" |
|
| 600 | - * @param string $inputName the form field or data key name used |
|
| 601 | - * @param string $ruleName the rule name for this validation ("required") |
|
| 602 | - * @param array $ruleArgs the rules argument |
|
| 603 | - */ |
|
| 598 | + /** |
|
| 599 | + * Peform validation for the rule "required" |
|
| 600 | + * @param string $inputName the form field or data key name used |
|
| 601 | + * @param string $ruleName the rule name for this validation ("required") |
|
| 602 | + * @param array $ruleArgs the rules argument |
|
| 603 | + */ |
|
| 604 | 604 | protected function _validateRequired($inputName, $ruleName, array $ruleArgs) { |
| 605 | - $inputVal = $this->post($inputName); |
|
| 606 | - if(array_key_exists(1, $ruleArgs) && function_exists($ruleArgs[1])) { |
|
| 607 | - $callbackReturn = $this->_runEmptyCallback($ruleArgs[1]); |
|
| 608 | - if ($inputVal == '' && $callbackReturn == true) { |
|
| 609 | - $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
| 610 | - } |
|
| 611 | - } |
|
| 605 | + $inputVal = $this->post($inputName); |
|
| 606 | + if(array_key_exists(1, $ruleArgs) && function_exists($ruleArgs[1])) { |
|
| 607 | + $callbackReturn = $this->_runEmptyCallback($ruleArgs[1]); |
|
| 608 | + if ($inputVal == '' && $callbackReturn == true) { |
|
| 609 | + $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
| 610 | + } |
|
| 611 | + } |
|
| 612 | 612 | else if($inputVal == '') { |
| 613 | 613 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
| 614 | - } |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - /** |
|
| 618 | - * Perform validation for the honey pot so means for the validation to be failed |
|
| 619 | - * @param string $inputName the form field or data key name used |
|
| 620 | - * @param string $ruleName the rule name for this validation |
|
| 621 | - * @param array $ruleArgs the rules argument |
|
| 622 | - */ |
|
| 623 | - protected function _validateHoneypot($inputName, $ruleName, array $ruleArgs) { |
|
| 624 | - if ($this->data[$inputName] != '') { |
|
| 625 | - $this->_forceFail = true; |
|
| 626 | - } |
|
| 627 | - } |
|
| 628 | - |
|
| 629 | - /** |
|
| 630 | - * Peform validation for the rule "callback" |
|
| 631 | - * @param string $inputName the form field or data key name used |
|
| 632 | - * @param string $ruleName the rule name for this validation ("callback") |
|
| 633 | - * @param array $ruleArgs the rules argument |
|
| 634 | - */ |
|
| 635 | - protected function _validateCallback($inputName, $ruleName, array $ruleArgs) { |
|
| 636 | - if (function_exists($ruleArgs[1]) && !empty($this->data[$inputName])) { |
|
| 614 | + } |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + /** |
|
| 618 | + * Perform validation for the honey pot so means for the validation to be failed |
|
| 619 | + * @param string $inputName the form field or data key name used |
|
| 620 | + * @param string $ruleName the rule name for this validation |
|
| 621 | + * @param array $ruleArgs the rules argument |
|
| 622 | + */ |
|
| 623 | + protected function _validateHoneypot($inputName, $ruleName, array $ruleArgs) { |
|
| 624 | + if ($this->data[$inputName] != '') { |
|
| 625 | + $this->_forceFail = true; |
|
| 626 | + } |
|
| 627 | + } |
|
| 628 | + |
|
| 629 | + /** |
|
| 630 | + * Peform validation for the rule "callback" |
|
| 631 | + * @param string $inputName the form field or data key name used |
|
| 632 | + * @param string $ruleName the rule name for this validation ("callback") |
|
| 633 | + * @param array $ruleArgs the rules argument |
|
| 634 | + */ |
|
| 635 | + protected function _validateCallback($inputName, $ruleName, array $ruleArgs) { |
|
| 636 | + if (function_exists($ruleArgs[1]) && !empty($this->data[$inputName])) { |
|
| 637 | 637 | $result = $this->_runCallback($this->data[$inputName], $ruleArgs[1]); |
| 638 | 638 | if(! $result){ |
| 639 | 639 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
| 640 | 640 | } |
| 641 | - } |
|
| 642 | - } |
|
| 643 | - |
|
| 644 | - /** |
|
| 645 | - * Peform validation for the rule "depends" |
|
| 646 | - * @param string $inputName the form field or data key name used |
|
| 647 | - * @param string $ruleName the rule name for this validation ("depends") |
|
| 648 | - * @param array $ruleArgs the rules argument |
|
| 649 | - */ |
|
| 650 | - protected function _validateDepends($inputName, $ruleName, array $ruleArgs) { |
|
| 651 | - if (array_key_exists($ruleArgs[1], $this->_errors)) { |
|
| 652 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 653 | - } |
|
| 654 | - } |
|
| 655 | - |
|
| 656 | - /** |
|
| 657 | - * Peform validation for the rule "not_equal" |
|
| 658 | - * @param string $inputName the form field or data key name used |
|
| 659 | - * @param string $ruleName the rule name for this validation ("not_equal") |
|
| 660 | - * @param array $ruleArgs the rules argument |
|
| 661 | - */ |
|
| 662 | - protected function _validateNotEqual($inputName, $ruleName, array $ruleArgs) { |
|
| 663 | - $canNotEqual = explode(',', $ruleArgs[1]); |
|
| 664 | - foreach ($canNotEqual as $doNotEqual) { |
|
| 665 | - $inputVal = $this->post($inputName); |
|
| 666 | - if (preg_match('/post:(.*)/', $doNotEqual)) { |
|
| 667 | - if ($inputVal == $this->data[str_replace('post:', '', $doNotEqual)]) { |
|
| 668 | - $this->_setError($inputName, $ruleName . ',post:key', array($this->_getLabel($inputName), $this->_getLabel(str_replace('post:', '', $doNotEqual)))); |
|
| 669 | - continue; |
|
| 670 | - } |
|
| 671 | - } |
|
| 641 | + } |
|
| 642 | + } |
|
| 643 | + |
|
| 644 | + /** |
|
| 645 | + * Peform validation for the rule "depends" |
|
| 646 | + * @param string $inputName the form field or data key name used |
|
| 647 | + * @param string $ruleName the rule name for this validation ("depends") |
|
| 648 | + * @param array $ruleArgs the rules argument |
|
| 649 | + */ |
|
| 650 | + protected function _validateDepends($inputName, $ruleName, array $ruleArgs) { |
|
| 651 | + if (array_key_exists($ruleArgs[1], $this->_errors)) { |
|
| 652 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 653 | + } |
|
| 654 | + } |
|
| 655 | + |
|
| 656 | + /** |
|
| 657 | + * Peform validation for the rule "not_equal" |
|
| 658 | + * @param string $inputName the form field or data key name used |
|
| 659 | + * @param string $ruleName the rule name for this validation ("not_equal") |
|
| 660 | + * @param array $ruleArgs the rules argument |
|
| 661 | + */ |
|
| 662 | + protected function _validateNotEqual($inputName, $ruleName, array $ruleArgs) { |
|
| 663 | + $canNotEqual = explode(',', $ruleArgs[1]); |
|
| 664 | + foreach ($canNotEqual as $doNotEqual) { |
|
| 665 | + $inputVal = $this->post($inputName); |
|
| 666 | + if (preg_match('/post:(.*)/', $doNotEqual)) { |
|
| 667 | + if ($inputVal == $this->data[str_replace('post:', '', $doNotEqual)]) { |
|
| 668 | + $this->_setError($inputName, $ruleName . ',post:key', array($this->_getLabel($inputName), $this->_getLabel(str_replace('post:', '', $doNotEqual)))); |
|
| 669 | + continue; |
|
| 670 | + } |
|
| 671 | + } |
|
| 672 | 672 | else{ |
| 673 | - if ($inputVal == $doNotEqual) { |
|
| 674 | - $this->_setError($inputName, $ruleName . ',string', array($this->_getLabel($inputName), $doNotEqual)); |
|
| 675 | - continue; |
|
| 676 | - } |
|
| 677 | - } |
|
| 678 | - } |
|
| 679 | - } |
|
| 680 | - |
|
| 681 | - /** |
|
| 682 | - * Peform validation for the rule "matches" |
|
| 683 | - * @param string $inputName the form field or data key name used |
|
| 684 | - * @param string $ruleName the rule name for this validation ("matches") |
|
| 685 | - * @param array $ruleArgs the rules argument |
|
| 686 | - */ |
|
| 687 | - protected function _validateMatches($inputName, $ruleName, array $ruleArgs) { |
|
| 688 | - $inputVal = $this->post($inputName); |
|
| 689 | - if ($inputVal != $this->data[$ruleArgs[1]]) { |
|
| 690 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 691 | - } |
|
| 692 | - } |
|
| 693 | - |
|
| 694 | - /** |
|
| 695 | - * Peform validation for the rule "valid_email" |
|
| 696 | - * @param string $inputName the form field or data key name used |
|
| 697 | - * @param string $ruleName the rule name for this validation ("valid_email") |
|
| 698 | - * @param array $ruleArgs the rules argument |
|
| 699 | - */ |
|
| 700 | - protected function _validateValidEmail($inputName, $ruleName, array $ruleArgs) { |
|
| 701 | - $inputVal = $this->post($inputName); |
|
| 702 | - if (! preg_match("/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i", $inputVal)) { |
|
| 703 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 704 | - return; |
|
| 705 | - } |
|
| 706 | - $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
| 707 | - } |
|
| 708 | - } |
|
| 709 | - |
|
| 710 | - /** |
|
| 711 | - * Peform validation for the rule "exact_length" |
|
| 712 | - * @param string $inputName the form field or data key name used |
|
| 713 | - * @param string $ruleName the rule name for this validation ("exact_length") |
|
| 714 | - * @param array $ruleArgs the rules argument |
|
| 715 | - */ |
|
| 716 | - protected function _validateExactLength($inputName, $ruleName, array $ruleArgs) { |
|
| 717 | - $inputVal = $this->post($inputName); |
|
| 718 | - if (strlen($inputVal) != $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 719 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 720 | - return; |
|
| 721 | - } |
|
| 722 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 723 | - } |
|
| 724 | - } |
|
| 725 | - |
|
| 726 | - /** |
|
| 727 | - * Peform validation for the rule "max_length" |
|
| 728 | - * @param string $inputName the form field or data key name used |
|
| 729 | - * @param string $ruleName the rule name for this validation ("max_length") |
|
| 730 | - * @param array $ruleArgs the rules argument |
|
| 731 | - */ |
|
| 732 | - protected function _validateMaxLength($inputName, $ruleName, array $ruleArgs) { |
|
| 733 | - $inputVal = $this->post($inputName); |
|
| 734 | - if (strlen($inputVal) > $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 735 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 736 | - return; |
|
| 737 | - } |
|
| 738 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 739 | - } |
|
| 740 | - } |
|
| 741 | - |
|
| 742 | - /** |
|
| 743 | - * Peform validation for the rule "min_length" |
|
| 744 | - * @param string $inputName the form field or data key name used |
|
| 745 | - * @param string $ruleName the rule name for this validation ("min_length") |
|
| 746 | - * @param array $ruleArgs the rules argument |
|
| 747 | - */ |
|
| 748 | - protected function _validateMinLength($inputName, $ruleName, array $ruleArgs) { |
|
| 749 | - $inputVal = $this->post($inputName); |
|
| 750 | - if (strlen($inputVal) < $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 751 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 752 | - return; |
|
| 753 | - } |
|
| 754 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 755 | - } |
|
| 756 | - } |
|
| 673 | + if ($inputVal == $doNotEqual) { |
|
| 674 | + $this->_setError($inputName, $ruleName . ',string', array($this->_getLabel($inputName), $doNotEqual)); |
|
| 675 | + continue; |
|
| 676 | + } |
|
| 677 | + } |
|
| 678 | + } |
|
| 679 | + } |
|
| 680 | + |
|
| 681 | + /** |
|
| 682 | + * Peform validation for the rule "matches" |
|
| 683 | + * @param string $inputName the form field or data key name used |
|
| 684 | + * @param string $ruleName the rule name for this validation ("matches") |
|
| 685 | + * @param array $ruleArgs the rules argument |
|
| 686 | + */ |
|
| 687 | + protected function _validateMatches($inputName, $ruleName, array $ruleArgs) { |
|
| 688 | + $inputVal = $this->post($inputName); |
|
| 689 | + if ($inputVal != $this->data[$ruleArgs[1]]) { |
|
| 690 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 691 | + } |
|
| 692 | + } |
|
| 693 | + |
|
| 694 | + /** |
|
| 695 | + * Peform validation for the rule "valid_email" |
|
| 696 | + * @param string $inputName the form field or data key name used |
|
| 697 | + * @param string $ruleName the rule name for this validation ("valid_email") |
|
| 698 | + * @param array $ruleArgs the rules argument |
|
| 699 | + */ |
|
| 700 | + protected function _validateValidEmail($inputName, $ruleName, array $ruleArgs) { |
|
| 701 | + $inputVal = $this->post($inputName); |
|
| 702 | + if (! preg_match("/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i", $inputVal)) { |
|
| 703 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 704 | + return; |
|
| 705 | + } |
|
| 706 | + $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
| 707 | + } |
|
| 708 | + } |
|
| 709 | + |
|
| 710 | + /** |
|
| 711 | + * Peform validation for the rule "exact_length" |
|
| 712 | + * @param string $inputName the form field or data key name used |
|
| 713 | + * @param string $ruleName the rule name for this validation ("exact_length") |
|
| 714 | + * @param array $ruleArgs the rules argument |
|
| 715 | + */ |
|
| 716 | + protected function _validateExactLength($inputName, $ruleName, array $ruleArgs) { |
|
| 717 | + $inputVal = $this->post($inputName); |
|
| 718 | + if (strlen($inputVal) != $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 719 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 720 | + return; |
|
| 721 | + } |
|
| 722 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 723 | + } |
|
| 724 | + } |
|
| 725 | + |
|
| 726 | + /** |
|
| 727 | + * Peform validation for the rule "max_length" |
|
| 728 | + * @param string $inputName the form field or data key name used |
|
| 729 | + * @param string $ruleName the rule name for this validation ("max_length") |
|
| 730 | + * @param array $ruleArgs the rules argument |
|
| 731 | + */ |
|
| 732 | + protected function _validateMaxLength($inputName, $ruleName, array $ruleArgs) { |
|
| 733 | + $inputVal = $this->post($inputName); |
|
| 734 | + if (strlen($inputVal) > $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 735 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 736 | + return; |
|
| 737 | + } |
|
| 738 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 739 | + } |
|
| 740 | + } |
|
| 741 | + |
|
| 742 | + /** |
|
| 743 | + * Peform validation for the rule "min_length" |
|
| 744 | + * @param string $inputName the form field or data key name used |
|
| 745 | + * @param string $ruleName the rule name for this validation ("min_length") |
|
| 746 | + * @param array $ruleArgs the rules argument |
|
| 747 | + */ |
|
| 748 | + protected function _validateMinLength($inputName, $ruleName, array $ruleArgs) { |
|
| 749 | + $inputVal = $this->post($inputName); |
|
| 750 | + if (strlen($inputVal) < $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 751 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 752 | + return; |
|
| 753 | + } |
|
| 754 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 755 | + } |
|
| 756 | + } |
|
| 757 | 757 | |
| 758 | - /** |
|
| 759 | - * Peform validation for the rule "less_than" |
|
| 760 | - * @param string $inputName the form field or data key name used |
|
| 761 | - * @param string $ruleName the rule name for this validation ("less_than") |
|
| 762 | - * @param array $ruleArgs the rules argument |
|
| 763 | - */ |
|
| 764 | - protected function _validateLessThan($inputName, $ruleName, array $ruleArgs) { |
|
| 765 | - $inputVal = $this->post($inputName); |
|
| 766 | - if ($inputVal >= $ruleArgs[1]) { |
|
| 767 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 768 | - return; |
|
| 769 | - } |
|
| 770 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 771 | - } |
|
| 772 | - } |
|
| 758 | + /** |
|
| 759 | + * Peform validation for the rule "less_than" |
|
| 760 | + * @param string $inputName the form field or data key name used |
|
| 761 | + * @param string $ruleName the rule name for this validation ("less_than") |
|
| 762 | + * @param array $ruleArgs the rules argument |
|
| 763 | + */ |
|
| 764 | + protected function _validateLessThan($inputName, $ruleName, array $ruleArgs) { |
|
| 765 | + $inputVal = $this->post($inputName); |
|
| 766 | + if ($inputVal >= $ruleArgs[1]) { |
|
| 767 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 768 | + return; |
|
| 769 | + } |
|
| 770 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 771 | + } |
|
| 772 | + } |
|
| 773 | 773 | |
| 774 | - /** |
|
| 775 | - * Peform validation for the rule "greater_than" |
|
| 776 | - * @param string $inputName the form field or data key name used |
|
| 777 | - * @param string $ruleName the rule name for this validation ("greater_than") |
|
| 778 | - * @param array $ruleArgs the rules argument |
|
| 779 | - */ |
|
| 780 | - protected function _validateGreaterThan($inputName, $ruleName, array $ruleArgs) { |
|
| 781 | - $inputVal = $this->post($inputName); |
|
| 782 | - if ($inputVal <= $ruleArgs[1]) { |
|
| 783 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 784 | - return; |
|
| 785 | - } |
|
| 786 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 787 | - } |
|
| 788 | - } |
|
| 774 | + /** |
|
| 775 | + * Peform validation for the rule "greater_than" |
|
| 776 | + * @param string $inputName the form field or data key name used |
|
| 777 | + * @param string $ruleName the rule name for this validation ("greater_than") |
|
| 778 | + * @param array $ruleArgs the rules argument |
|
| 779 | + */ |
|
| 780 | + protected function _validateGreaterThan($inputName, $ruleName, array $ruleArgs) { |
|
| 781 | + $inputVal = $this->post($inputName); |
|
| 782 | + if ($inputVal <= $ruleArgs[1]) { |
|
| 783 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 784 | + return; |
|
| 785 | + } |
|
| 786 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 787 | + } |
|
| 788 | + } |
|
| 789 | 789 | |
| 790 | - /** |
|
| 791 | - * Peform validation for the rule "numeric" |
|
| 792 | - * @param string $inputName the form field or data key name used |
|
| 793 | - * @param string $ruleName the rule name for this validation ("numeric") |
|
| 794 | - * @param array $ruleArgs the rules argument |
|
| 795 | - */ |
|
| 796 | - protected function _validateNumeric($inputName, $ruleName, array $ruleArgs) { |
|
| 797 | - $inputVal = $this->post($inputName); |
|
| 798 | - if (! is_numeric($inputVal)) { |
|
| 799 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 800 | - return; |
|
| 801 | - } |
|
| 802 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 803 | - } |
|
| 804 | - } |
|
| 790 | + /** |
|
| 791 | + * Peform validation for the rule "numeric" |
|
| 792 | + * @param string $inputName the form field or data key name used |
|
| 793 | + * @param string $ruleName the rule name for this validation ("numeric") |
|
| 794 | + * @param array $ruleArgs the rules argument |
|
| 795 | + */ |
|
| 796 | + protected function _validateNumeric($inputName, $ruleName, array $ruleArgs) { |
|
| 797 | + $inputVal = $this->post($inputName); |
|
| 798 | + if (! is_numeric($inputVal)) { |
|
| 799 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 800 | + return; |
|
| 801 | + } |
|
| 802 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 803 | + } |
|
| 804 | + } |
|
| 805 | 805 | |
| 806 | - /** |
|
| 807 | - * Peform validation for the rule "exists" |
|
| 808 | - * @param string $inputName the form field or data key name used |
|
| 809 | - * @param string $ruleName the rule name for this validation ("exists") |
|
| 810 | - * @param array $ruleArgs the rules argument |
|
| 811 | - */ |
|
| 806 | + /** |
|
| 807 | + * Peform validation for the rule "exists" |
|
| 808 | + * @param string $inputName the form field or data key name used |
|
| 809 | + * @param string $ruleName the rule name for this validation ("exists") |
|
| 810 | + * @param array $ruleArgs the rules argument |
|
| 811 | + */ |
|
| 812 | 812 | protected function _validateExists($inputName, $ruleName, array $ruleArgs) { |
| 813 | - $inputVal = $this->post($inputName); |
|
| 814 | - $obj = & get_instance(); |
|
| 815 | - if(! isset($obj->database)){ |
|
| 816 | - return; |
|
| 817 | - } |
|
| 818 | - list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 819 | - $obj->database->from($table) |
|
| 820 | - ->where($column, $inputVal) |
|
| 821 | - ->get(); |
|
| 822 | - $nb = $obj->database->numRows(); |
|
| 823 | - if ($nb == 0) { |
|
| 824 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 825 | - return; |
|
| 826 | - } |
|
| 827 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 828 | - } |
|
| 829 | - } |
|
| 830 | - |
|
| 831 | - /** |
|
| 832 | - * Peform validation for the rule "is_unique" |
|
| 833 | - * @param string $inputName the form field or data key name used |
|
| 834 | - * @param string $ruleName the rule name for this validation ("is_unique") |
|
| 835 | - * @param array $ruleArgs the rules argument |
|
| 836 | - */ |
|
| 837 | - protected function _validateIsUnique($inputName, $ruleName, array $ruleArgs) { |
|
| 838 | - $inputVal = $this->post($inputName); |
|
| 839 | - $obj = & get_instance(); |
|
| 840 | - if(! isset($obj->database)){ |
|
| 841 | - return; |
|
| 842 | - } |
|
| 843 | - list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 844 | - $obj->database->from($table) |
|
| 845 | - ->where($column, $inputVal) |
|
| 846 | - ->get(); |
|
| 847 | - $nb = $obj->database->numRows(); |
|
| 848 | - if ($nb != 0) { |
|
| 849 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 850 | - return; |
|
| 851 | - } |
|
| 852 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 853 | - } |
|
| 854 | - } |
|
| 813 | + $inputVal = $this->post($inputName); |
|
| 814 | + $obj = & get_instance(); |
|
| 815 | + if(! isset($obj->database)){ |
|
| 816 | + return; |
|
| 817 | + } |
|
| 818 | + list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 819 | + $obj->database->from($table) |
|
| 820 | + ->where($column, $inputVal) |
|
| 821 | + ->get(); |
|
| 822 | + $nb = $obj->database->numRows(); |
|
| 823 | + if ($nb == 0) { |
|
| 824 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 825 | + return; |
|
| 826 | + } |
|
| 827 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 828 | + } |
|
| 829 | + } |
|
| 830 | + |
|
| 831 | + /** |
|
| 832 | + * Peform validation for the rule "is_unique" |
|
| 833 | + * @param string $inputName the form field or data key name used |
|
| 834 | + * @param string $ruleName the rule name for this validation ("is_unique") |
|
| 835 | + * @param array $ruleArgs the rules argument |
|
| 836 | + */ |
|
| 837 | + protected function _validateIsUnique($inputName, $ruleName, array $ruleArgs) { |
|
| 838 | + $inputVal = $this->post($inputName); |
|
| 839 | + $obj = & get_instance(); |
|
| 840 | + if(! isset($obj->database)){ |
|
| 841 | + return; |
|
| 842 | + } |
|
| 843 | + list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 844 | + $obj->database->from($table) |
|
| 845 | + ->where($column, $inputVal) |
|
| 846 | + ->get(); |
|
| 847 | + $nb = $obj->database->numRows(); |
|
| 848 | + if ($nb != 0) { |
|
| 849 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 850 | + return; |
|
| 851 | + } |
|
| 852 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 853 | + } |
|
| 854 | + } |
|
| 855 | 855 | |
| 856 | - /** |
|
| 857 | - * Peform validation for the rule "is_unique_update" |
|
| 858 | - * @param string $inputName the form field or data key name used |
|
| 859 | - * @param string $ruleName the rule name for this validation ("is_unique_update") |
|
| 860 | - * @param array $ruleArgs the rules argument |
|
| 861 | - */ |
|
| 862 | - protected function _validateIsUniqueUpdate($inputName, $ruleName, array $ruleArgs) { |
|
| 863 | - $inputVal = $this->post($inputName); |
|
| 864 | - $obj = & get_instance(); |
|
| 865 | - if(! isset($obj->database)){ |
|
| 866 | - return; |
|
| 867 | - } |
|
| 868 | - $data = explode(',', $ruleArgs[1]); |
|
| 869 | - if(count($data) < 2){ |
|
| 870 | - return; |
|
| 871 | - } |
|
| 872 | - list($table, $column) = explode('.', $data[0]); |
|
| 873 | - list($field, $val) = explode('=', $data[1]); |
|
| 874 | - $obj->database->from($table) |
|
| 875 | - ->where($column, $inputVal) |
|
| 876 | - ->where($field, '!=', trim($val)) |
|
| 877 | - ->get(); |
|
| 878 | - $nb = $obj->database->numRows(); |
|
| 879 | - if ($nb != 0) { |
|
| 880 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 881 | - return; |
|
| 882 | - } |
|
| 883 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 884 | - } |
|
| 885 | - } |
|
| 886 | - |
|
| 887 | - /** |
|
| 888 | - * Peform validation for the rule "in_list" |
|
| 889 | - * @param string $inputName the form field or data key name used |
|
| 890 | - * @param string $ruleName the rule name for this validation ("in_list") |
|
| 891 | - * @param array $ruleArgs the rules argument |
|
| 892 | - */ |
|
| 893 | - protected function _validateInList($inputName, $ruleName, array $ruleArgs) { |
|
| 894 | - $inputVal = $this->post($inputName); |
|
| 895 | - $list = explode(',', $ruleArgs[1]); |
|
| 896 | - $list = array_map('trim', $list); |
|
| 897 | - if (! in_array($inputVal, $list)) { |
|
| 898 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 899 | - return; |
|
| 900 | - } |
|
| 901 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 902 | - } |
|
| 903 | - } |
|
| 904 | - |
|
| 905 | - /** |
|
| 906 | - * Peform validation for the rule "regex" |
|
| 907 | - * @param string $inputName the form field or data key name used |
|
| 908 | - * @param string $ruleName the rule name for this validation ("regex") |
|
| 909 | - * @param array $ruleArgs the rules argument |
|
| 910 | - */ |
|
| 911 | - protected function _validateRegex($inputName, $ruleName, array $ruleArgs) { |
|
| 912 | - $inputVal = $this->post($inputName); |
|
| 913 | - $regex = $ruleArgs[1]; |
|
| 914 | - if (! preg_match($regex, $inputVal)) { |
|
| 915 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 916 | - return; |
|
| 917 | - } |
|
| 918 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 919 | - } |
|
| 920 | - } |
|
| 856 | + /** |
|
| 857 | + * Peform validation for the rule "is_unique_update" |
|
| 858 | + * @param string $inputName the form field or data key name used |
|
| 859 | + * @param string $ruleName the rule name for this validation ("is_unique_update") |
|
| 860 | + * @param array $ruleArgs the rules argument |
|
| 861 | + */ |
|
| 862 | + protected function _validateIsUniqueUpdate($inputName, $ruleName, array $ruleArgs) { |
|
| 863 | + $inputVal = $this->post($inputName); |
|
| 864 | + $obj = & get_instance(); |
|
| 865 | + if(! isset($obj->database)){ |
|
| 866 | + return; |
|
| 867 | + } |
|
| 868 | + $data = explode(',', $ruleArgs[1]); |
|
| 869 | + if(count($data) < 2){ |
|
| 870 | + return; |
|
| 871 | + } |
|
| 872 | + list($table, $column) = explode('.', $data[0]); |
|
| 873 | + list($field, $val) = explode('=', $data[1]); |
|
| 874 | + $obj->database->from($table) |
|
| 875 | + ->where($column, $inputVal) |
|
| 876 | + ->where($field, '!=', trim($val)) |
|
| 877 | + ->get(); |
|
| 878 | + $nb = $obj->database->numRows(); |
|
| 879 | + if ($nb != 0) { |
|
| 880 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 881 | + return; |
|
| 882 | + } |
|
| 883 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 884 | + } |
|
| 885 | + } |
|
| 886 | + |
|
| 887 | + /** |
|
| 888 | + * Peform validation for the rule "in_list" |
|
| 889 | + * @param string $inputName the form field or data key name used |
|
| 890 | + * @param string $ruleName the rule name for this validation ("in_list") |
|
| 891 | + * @param array $ruleArgs the rules argument |
|
| 892 | + */ |
|
| 893 | + protected function _validateInList($inputName, $ruleName, array $ruleArgs) { |
|
| 894 | + $inputVal = $this->post($inputName); |
|
| 895 | + $list = explode(',', $ruleArgs[1]); |
|
| 896 | + $list = array_map('trim', $list); |
|
| 897 | + if (! in_array($inputVal, $list)) { |
|
| 898 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 899 | + return; |
|
| 900 | + } |
|
| 901 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 902 | + } |
|
| 903 | + } |
|
| 904 | + |
|
| 905 | + /** |
|
| 906 | + * Peform validation for the rule "regex" |
|
| 907 | + * @param string $inputName the form field or data key name used |
|
| 908 | + * @param string $ruleName the rule name for this validation ("regex") |
|
| 909 | + * @param array $ruleArgs the rules argument |
|
| 910 | + */ |
|
| 911 | + protected function _validateRegex($inputName, $ruleName, array $ruleArgs) { |
|
| 912 | + $inputVal = $this->post($inputName); |
|
| 913 | + $regex = $ruleArgs[1]; |
|
| 914 | + if (! preg_match($regex, $inputVal)) { |
|
| 915 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 916 | + return; |
|
| 917 | + } |
|
| 918 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 919 | + } |
|
| 920 | + } |
|
| 921 | 921 | |
| 922 | - } |
|
| 922 | + } |
|
@@ -25,13 +25,13 @@ discard block |
||
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | 27 | |
| 28 | - class FormValidation{ |
|
| 28 | + class FormValidation { |
|
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | 31 | * The form validation status |
| 32 | 32 | * @var boolean |
| 33 | 33 | */ |
| 34 | - protected $_success = false; |
|
| 34 | + protected $_success = false; |
|
| 35 | 35 | |
| 36 | 36 | /** |
| 37 | 37 | * The list of errors messages |
@@ -40,31 +40,31 @@ discard block |
||
| 40 | 40 | protected $_errorsMessages = array(); |
| 41 | 41 | |
| 42 | 42 | // Array of rule sets, fieldName => PIPE seperated ruleString |
| 43 | - protected $_rules = array(); |
|
| 43 | + protected $_rules = array(); |
|
| 44 | 44 | |
| 45 | 45 | // Array of errors, niceName => Error Message |
| 46 | - protected $_errors = array(); |
|
| 46 | + protected $_errors = array(); |
|
| 47 | 47 | |
| 48 | 48 | // Array of post Key => Nice name labels |
| 49 | - protected $_labels = array(); |
|
| 49 | + protected $_labels = array(); |
|
| 50 | 50 | |
| 51 | 51 | /** |
| 52 | 52 | * The errors delimiters |
| 53 | 53 | * @var array |
| 54 | 54 | */ |
| 55 | - protected $_allErrorsDelimiter = array('<div class="error">', '</div>'); |
|
| 55 | + protected $_allErrorsDelimiter = array('<div class="error">', '</div>'); |
|
| 56 | 56 | |
| 57 | 57 | /** |
| 58 | 58 | * The each error delimiter |
| 59 | 59 | * @var array |
| 60 | 60 | */ |
| 61 | - protected $_eachErrorDelimiter = array('<p class="error">', '</p>'); |
|
| 61 | + protected $_eachErrorDelimiter = array('<p class="error">', '</p>'); |
|
| 62 | 62 | |
| 63 | 63 | /** |
| 64 | 64 | * Indicated if need force the validation to be failed |
| 65 | 65 | * @var boolean |
| 66 | 66 | */ |
| 67 | - protected $_forceFail = false; |
|
| 67 | + protected $_forceFail = false; |
|
| 68 | 68 | |
| 69 | 69 | /** |
| 70 | 70 | * The list of the error messages overrides by the original |
@@ -98,13 +98,13 @@ discard block |
||
| 98 | 98 | * @return void |
| 99 | 99 | */ |
| 100 | 100 | public function __construct() { |
| 101 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 101 | + $this->logger = & class_loader('Log', 'classes'); |
|
| 102 | 102 | $this->logger->setLogger('Library::FormValidation'); |
| 103 | 103 | |
| 104 | 104 | //Load form validation language message |
| 105 | 105 | Loader::lang('form_validation'); |
| 106 | 106 | $obj = & get_instance(); |
| 107 | - $this->_errorsMessages = array( |
|
| 107 | + $this->_errorsMessages = array( |
|
| 108 | 108 | 'required' => $obj->lang->get('fv_required'), |
| 109 | 109 | 'min_length' => $obj->lang->get('fv_min_length'), |
| 110 | 110 | 'max_length' => $obj->lang->get('fv_max_length'), |
@@ -141,7 +141,7 @@ discard block |
||
| 141 | 141 | $this->_success = false; |
| 142 | 142 | $this->_forceFail = false; |
| 143 | 143 | $this->data = array(); |
| 144 | - $this->enableCsrfCheck = false; |
|
| 144 | + $this->enableCsrfCheck = false; |
|
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | /** |
@@ -150,7 +150,7 @@ discard block |
||
| 150 | 150 | * |
| 151 | 151 | * @return FormValidation Current instance of object. |
| 152 | 152 | */ |
| 153 | - public function setData(array $data){ |
|
| 153 | + public function setData(array $data) { |
|
| 154 | 154 | $this->logger->debug('Setting the form validation data, the values are: ' . stringfy_vars($data)); |
| 155 | 155 | $this->data = $data; |
| 156 | 156 | return $this; |
@@ -160,7 +160,7 @@ discard block |
||
| 160 | 160 | * Get the form validation data |
| 161 | 161 | * @return array the form validation data to be validated |
| 162 | 162 | */ |
| 163 | - public function getData(){ |
|
| 163 | + public function getData() { |
|
| 164 | 164 | return $this->data; |
| 165 | 165 | } |
| 166 | 166 | |
@@ -169,7 +169,7 @@ discard block |
||
| 169 | 169 | * |
| 170 | 170 | * @return string the function name |
| 171 | 171 | */ |
| 172 | - protected function _toCallCase($funcName, $prefix='_validate') { |
|
| 172 | + protected function _toCallCase($funcName, $prefix = '_validate') { |
|
| 173 | 173 | $funcName = strtolower($funcName); |
| 174 | 174 | $finalFuncName = $prefix; |
| 175 | 175 | foreach (explode('_', $funcName) as $funcNamePart) { |
@@ -193,7 +193,7 @@ discard block |
||
| 193 | 193 | * @return boolean Whether or not the form has been submitted or the data is available for validation. |
| 194 | 194 | */ |
| 195 | 195 | public function canDoValidation() { |
| 196 | - return get_instance()->request->method() === 'POST' || ! empty($this->data); |
|
| 196 | + return get_instance()->request->method() === 'POST' || !empty($this->data); |
|
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | /** |
@@ -213,14 +213,14 @@ discard block |
||
| 213 | 213 | * Validate the CSRF |
| 214 | 214 | * @return void |
| 215 | 215 | */ |
| 216 | - protected function validateCSRF(){ |
|
| 217 | - if(get_instance()->request->method() == 'POST' || $this->enableCsrfCheck){ |
|
| 216 | + protected function validateCSRF() { |
|
| 217 | + if (get_instance()->request->method() == 'POST' || $this->enableCsrfCheck) { |
|
| 218 | 218 | $this->logger->debug('Check if CSRF is enabled in configuration'); |
| 219 | 219 | //first check for CSRF |
| 220 | - if ((get_config('csrf_enable', false) || $this->enableCsrfCheck) && ! Security::validateCSRF()){ |
|
| 220 | + if ((get_config('csrf_enable', false) || $this->enableCsrfCheck) && !Security::validateCSRF()) { |
|
| 221 | 221 | show_error('Invalide data, Cross Site Request Forgery do his job, the data to validate is corrupted.'); |
| 222 | 222 | } |
| 223 | - else{ |
|
| 223 | + else { |
|
| 224 | 224 | $this->logger->info('CSRF is not enabled in configuration or not set manully, no need to check it'); |
| 225 | 225 | } |
| 226 | 226 | } |
@@ -237,10 +237,10 @@ discard block |
||
| 237 | 237 | $this->_forceFail = false; |
| 238 | 238 | |
| 239 | 239 | foreach ($this->getData() as $inputName => $inputVal) { |
| 240 | - if(is_array($this->data[$inputName])){ |
|
| 240 | + if (is_array($this->data[$inputName])) { |
|
| 241 | 241 | $this->data[$inputName] = array_map('trim', $this->data[$inputName]); |
| 242 | 242 | } |
| 243 | - else{ |
|
| 243 | + else { |
|
| 244 | 244 | $this->data[$inputName] = trim($this->data[$inputName]); |
| 245 | 245 | } |
| 246 | 246 | |
@@ -267,7 +267,7 @@ discard block |
||
| 267 | 267 | public function setRule($inputField, $inputLabel, $ruleSets) { |
| 268 | 268 | $this->_rules[$inputField] = $ruleSets; |
| 269 | 269 | $this->_labels[$inputField] = $inputLabel; |
| 270 | - $this->logger->info('Set the field rule: name [' .$inputField. '], label [' .$inputLabel. '], rules [' .$ruleSets. ']'); |
|
| 270 | + $this->logger->info('Set the field rule: name [' . $inputField . '], label [' . $inputLabel . '], rules [' . $ruleSets . ']'); |
|
| 271 | 271 | return $this; |
| 272 | 272 | } |
| 273 | 273 | |
@@ -431,7 +431,7 @@ discard block |
||
| 431 | 431 | } |
| 432 | 432 | $errorOutput .= $errorsEnd; |
| 433 | 433 | echo ($echo) ? $errorOutput : ''; |
| 434 | - return (! $echo) ? $errorOutput : null; |
|
| 434 | + return (!$echo) ? $errorOutput : null; |
|
| 435 | 435 | } |
| 436 | 436 | |
| 437 | 437 | /** |
@@ -456,25 +456,25 @@ discard block |
||
| 456 | 456 | /* |
| 457 | 457 | //////////////// hack for regex rule that can contain "|" |
| 458 | 458 | */ |
| 459 | - if(strpos($ruleString, 'regex') !== false){ |
|
| 459 | + if (strpos($ruleString, 'regex') !== false) { |
|
| 460 | 460 | $regexRule = array(); |
| 461 | 461 | $rule = '#regex\[\/(.*)\/([a-zA-Z0-9]?)\]#'; |
| 462 | 462 | preg_match($rule, $ruleString, $regexRule); |
| 463 | 463 | $ruleStringTemp = preg_replace($rule, '', $ruleString); |
| 464 | - if(!empty($regexRule[0])){ |
|
| 464 | + if (!empty($regexRule[0])) { |
|
| 465 | 465 | $ruleSets[] = $regexRule[0]; |
| 466 | 466 | } |
| 467 | 467 | $ruleStringRegex = explode('|', $ruleStringTemp); |
| 468 | 468 | foreach ($ruleStringRegex as $rule) { |
| 469 | 469 | $rule = trim($rule); |
| 470 | - if($rule){ |
|
| 470 | + if ($rule) { |
|
| 471 | 471 | $ruleSets[] = $rule; |
| 472 | 472 | } |
| 473 | 473 | } |
| 474 | 474 | |
| 475 | 475 | } |
| 476 | 476 | /***********************************/ |
| 477 | - else{ |
|
| 477 | + else { |
|
| 478 | 478 | if (strpos($ruleString, '|') !== FALSE) { |
| 479 | 479 | $ruleSets = explode('|', $ruleString); |
| 480 | 480 | } else { |
@@ -506,7 +506,7 @@ discard block |
||
| 506 | 506 | * @return void |
| 507 | 507 | */ |
| 508 | 508 | protected function _validateRule($inputName, $inputVal, $ruleName) { |
| 509 | - $this->logger->debug('Rule validation of field [' .$inputName. '], value [' .$inputVal. '], rule [' .$ruleName. ']'); |
|
| 509 | + $this->logger->debug('Rule validation of field [' . $inputName . '], value [' . $inputVal . '], rule [' . $ruleName . ']'); |
|
| 510 | 510 | // Array to store args |
| 511 | 511 | $ruleArgs = array(); |
| 512 | 512 | |
@@ -551,7 +551,7 @@ discard block |
||
| 551 | 551 | $key = $i - 1; |
| 552 | 552 | $rulePhrase = str_replace('%' . $i, $replacements[$key], $rulePhrase); |
| 553 | 553 | } |
| 554 | - if (! array_key_exists($inputName, $this->_errors)) { |
|
| 554 | + if (!array_key_exists($inputName, $this->_errors)) { |
|
| 555 | 555 | $this->_errors[$inputName] = $rulePhrase; |
| 556 | 556 | } |
| 557 | 557 | } |
@@ -603,13 +603,13 @@ discard block |
||
| 603 | 603 | */ |
| 604 | 604 | protected function _validateRequired($inputName, $ruleName, array $ruleArgs) { |
| 605 | 605 | $inputVal = $this->post($inputName); |
| 606 | - if(array_key_exists(1, $ruleArgs) && function_exists($ruleArgs[1])) { |
|
| 606 | + if (array_key_exists(1, $ruleArgs) && function_exists($ruleArgs[1])) { |
|
| 607 | 607 | $callbackReturn = $this->_runEmptyCallback($ruleArgs[1]); |
| 608 | 608 | if ($inputVal == '' && $callbackReturn == true) { |
| 609 | 609 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
| 610 | 610 | } |
| 611 | 611 | } |
| 612 | - else if($inputVal == '') { |
|
| 612 | + else if ($inputVal == '') { |
|
| 613 | 613 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
| 614 | 614 | } |
| 615 | 615 | } |
@@ -635,7 +635,7 @@ discard block |
||
| 635 | 635 | protected function _validateCallback($inputName, $ruleName, array $ruleArgs) { |
| 636 | 636 | if (function_exists($ruleArgs[1]) && !empty($this->data[$inputName])) { |
| 637 | 637 | $result = $this->_runCallback($this->data[$inputName], $ruleArgs[1]); |
| 638 | - if(! $result){ |
|
| 638 | + if (!$result) { |
|
| 639 | 639 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
| 640 | 640 | } |
| 641 | 641 | } |
@@ -669,7 +669,7 @@ discard block |
||
| 669 | 669 | continue; |
| 670 | 670 | } |
| 671 | 671 | } |
| 672 | - else{ |
|
| 672 | + else { |
|
| 673 | 673 | if ($inputVal == $doNotEqual) { |
| 674 | 674 | $this->_setError($inputName, $ruleName . ',string', array($this->_getLabel($inputName), $doNotEqual)); |
| 675 | 675 | continue; |
@@ -699,8 +699,8 @@ discard block |
||
| 699 | 699 | */ |
| 700 | 700 | protected function _validateValidEmail($inputName, $ruleName, array $ruleArgs) { |
| 701 | 701 | $inputVal = $this->post($inputName); |
| 702 | - if (! preg_match("/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i", $inputVal)) { |
|
| 703 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 702 | + if (!preg_match("/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i", $inputVal)) { |
|
| 703 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 704 | 704 | return; |
| 705 | 705 | } |
| 706 | 706 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
@@ -716,7 +716,7 @@ discard block |
||
| 716 | 716 | protected function _validateExactLength($inputName, $ruleName, array $ruleArgs) { |
| 717 | 717 | $inputVal = $this->post($inputName); |
| 718 | 718 | if (strlen($inputVal) != $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
| 719 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 719 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 720 | 720 | return; |
| 721 | 721 | } |
| 722 | 722 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
@@ -732,7 +732,7 @@ discard block |
||
| 732 | 732 | protected function _validateMaxLength($inputName, $ruleName, array $ruleArgs) { |
| 733 | 733 | $inputVal = $this->post($inputName); |
| 734 | 734 | if (strlen($inputVal) > $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
| 735 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 735 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 736 | 736 | return; |
| 737 | 737 | } |
| 738 | 738 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
@@ -748,7 +748,7 @@ discard block |
||
| 748 | 748 | protected function _validateMinLength($inputName, $ruleName, array $ruleArgs) { |
| 749 | 749 | $inputVal = $this->post($inputName); |
| 750 | 750 | if (strlen($inputVal) < $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
| 751 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 751 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 752 | 752 | return; |
| 753 | 753 | } |
| 754 | 754 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
@@ -764,7 +764,7 @@ discard block |
||
| 764 | 764 | protected function _validateLessThan($inputName, $ruleName, array $ruleArgs) { |
| 765 | 765 | $inputVal = $this->post($inputName); |
| 766 | 766 | if ($inputVal >= $ruleArgs[1]) { |
| 767 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 767 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 768 | 768 | return; |
| 769 | 769 | } |
| 770 | 770 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
@@ -780,7 +780,7 @@ discard block |
||
| 780 | 780 | protected function _validateGreaterThan($inputName, $ruleName, array $ruleArgs) { |
| 781 | 781 | $inputVal = $this->post($inputName); |
| 782 | 782 | if ($inputVal <= $ruleArgs[1]) { |
| 783 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 783 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 784 | 784 | return; |
| 785 | 785 | } |
| 786 | 786 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
@@ -795,8 +795,8 @@ discard block |
||
| 795 | 795 | */ |
| 796 | 796 | protected function _validateNumeric($inputName, $ruleName, array $ruleArgs) { |
| 797 | 797 | $inputVal = $this->post($inputName); |
| 798 | - if (! is_numeric($inputVal)) { |
|
| 799 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 798 | + if (!is_numeric($inputVal)) { |
|
| 799 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 800 | 800 | return; |
| 801 | 801 | } |
| 802 | 802 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
@@ -812,7 +812,7 @@ discard block |
||
| 812 | 812 | protected function _validateExists($inputName, $ruleName, array $ruleArgs) { |
| 813 | 813 | $inputVal = $this->post($inputName); |
| 814 | 814 | $obj = & get_instance(); |
| 815 | - if(! isset($obj->database)){ |
|
| 815 | + if (!isset($obj->database)) { |
|
| 816 | 816 | return; |
| 817 | 817 | } |
| 818 | 818 | list($table, $column) = explode('.', $ruleArgs[1]); |
@@ -821,7 +821,7 @@ discard block |
||
| 821 | 821 | ->get(); |
| 822 | 822 | $nb = $obj->database->numRows(); |
| 823 | 823 | if ($nb == 0) { |
| 824 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 824 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 825 | 825 | return; |
| 826 | 826 | } |
| 827 | 827 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
@@ -837,7 +837,7 @@ discard block |
||
| 837 | 837 | protected function _validateIsUnique($inputName, $ruleName, array $ruleArgs) { |
| 838 | 838 | $inputVal = $this->post($inputName); |
| 839 | 839 | $obj = & get_instance(); |
| 840 | - if(! isset($obj->database)){ |
|
| 840 | + if (!isset($obj->database)) { |
|
| 841 | 841 | return; |
| 842 | 842 | } |
| 843 | 843 | list($table, $column) = explode('.', $ruleArgs[1]); |
@@ -846,7 +846,7 @@ discard block |
||
| 846 | 846 | ->get(); |
| 847 | 847 | $nb = $obj->database->numRows(); |
| 848 | 848 | if ($nb != 0) { |
| 849 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 849 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 850 | 850 | return; |
| 851 | 851 | } |
| 852 | 852 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
@@ -862,11 +862,11 @@ discard block |
||
| 862 | 862 | protected function _validateIsUniqueUpdate($inputName, $ruleName, array $ruleArgs) { |
| 863 | 863 | $inputVal = $this->post($inputName); |
| 864 | 864 | $obj = & get_instance(); |
| 865 | - if(! isset($obj->database)){ |
|
| 865 | + if (!isset($obj->database)) { |
|
| 866 | 866 | return; |
| 867 | 867 | } |
| 868 | 868 | $data = explode(',', $ruleArgs[1]); |
| 869 | - if(count($data) < 2){ |
|
| 869 | + if (count($data) < 2) { |
|
| 870 | 870 | return; |
| 871 | 871 | } |
| 872 | 872 | list($table, $column) = explode('.', $data[0]); |
@@ -877,7 +877,7 @@ discard block |
||
| 877 | 877 | ->get(); |
| 878 | 878 | $nb = $obj->database->numRows(); |
| 879 | 879 | if ($nb != 0) { |
| 880 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 880 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 881 | 881 | return; |
| 882 | 882 | } |
| 883 | 883 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
@@ -894,8 +894,8 @@ discard block |
||
| 894 | 894 | $inputVal = $this->post($inputName); |
| 895 | 895 | $list = explode(',', $ruleArgs[1]); |
| 896 | 896 | $list = array_map('trim', $list); |
| 897 | - if (! in_array($inputVal, $list)) { |
|
| 898 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 897 | + if (!in_array($inputVal, $list)) { |
|
| 898 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 899 | 899 | return; |
| 900 | 900 | } |
| 901 | 901 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
@@ -911,8 +911,8 @@ discard block |
||
| 911 | 911 | protected function _validateRegex($inputName, $ruleName, array $ruleArgs) { |
| 912 | 912 | $inputVal = $this->post($inputName); |
| 913 | 913 | $regex = $ruleArgs[1]; |
| 914 | - if (! preg_match($regex, $inputVal)) { |
|
| 915 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 914 | + if (!preg_match($regex, $inputVal)) { |
|
| 915 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 916 | 916 | return; |
| 917 | 917 | } |
| 918 | 918 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | |
| 14 | 14 | private static $config = null; |
| 15 | 15 | |
| 16 | - public function __construct(){ |
|
| 16 | + public function __construct() { |
|
| 17 | 17 | $this->db = new Database(array( |
| 18 | 18 | 'driver' => 'sqlite', |
| 19 | 19 | 'database' => TESTS_PATH . 'assets/db_tests.db', |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | |
| 50 | 50 | |
| 51 | 51 | |
| 52 | - public function testUsingSessionConfiguration(){ |
|
| 52 | + public function testUsingSessionConfiguration() { |
|
| 53 | 53 | //using value in the configuration |
| 54 | 54 | self::$config->set('session_save_path', 'DBSessionModel'); |
| 55 | 55 | self::$config->set('session_secret', $this->secret); |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | $this->assertEquals($dbsh->decode($encoded), 'foo'); |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - public function testWhenDataIsExpired(){ |
|
| 78 | + public function testWhenDataIsExpired() { |
|
| 79 | 79 | $dbsh = new DBSessionHandler($this->model); |
| 80 | 80 | $dbsh->setSessionSecret($this->secret); |
| 81 | 81 | |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | $this->assertNull($dbsh->read('foo')); |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | - public function testWhenDataAlreadyExistDoUpdate(){ |
|
| 91 | + public function testWhenDataAlreadyExistDoUpdate() { |
|
| 92 | 92 | $dbsh = new DBSessionHandler($this->model); |
| 93 | 93 | $dbsh->setSessionSecret($this->secret); |
| 94 | 94 | |
@@ -101,7 +101,7 @@ discard block |
||
| 101 | 101 | $this->assertEquals($dbsh->read('foo'), '445'); |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | - public function testUsingCustomModelInstance(){ |
|
| 104 | + public function testUsingCustomModelInstance() { |
|
| 105 | 105 | $dbsh = new DBSessionHandler($this->model); |
| 106 | 106 | $dbsh->setSessionSecret($this->secret); |
| 107 | 107 | |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | |
| 132 | - public function testUsingCustomLogInstance(){ |
|
| 132 | + public function testUsingCustomLogInstance() { |
|
| 133 | 133 | $dbsh = new DBSessionHandler($this->model, new Log()); |
| 134 | 134 | $dbsh->setSessionSecret($this->secret); |
| 135 | 135 | |
@@ -156,7 +156,7 @@ discard block |
||
| 156 | 156 | $this->assertEquals($dbsh->decode($encoded), 'foo'); |
| 157 | 157 | } |
| 158 | 158 | |
| 159 | - public function testUsingCustomLoaderInstance(){ |
|
| 159 | + public function testUsingCustomLoaderInstance() { |
|
| 160 | 160 | $dbsh = new DBSessionHandler($this->model, null, new Loader()); |
| 161 | 161 | $dbsh->setSessionSecret($this->secret); |
| 162 | 162 | |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | |
| 187 | - public function testWhenModelInsanceIsNotSet(){ |
|
| 187 | + public function testWhenModelInsanceIsNotSet() { |
|
| 188 | 188 | $dbsh = new DBSessionHandler(null, null, new Loader()); |
| 189 | 189 | $dbsh->setSessionSecret($this->secret); |
| 190 | 190 | |
@@ -212,7 +212,7 @@ discard block |
||
| 212 | 212 | $this->assertEquals($dbsh->decode($encoded), 'foo'); |
| 213 | 213 | } |
| 214 | 214 | |
| 215 | - public function testWhenModelTableColumnsIsNotSet(){ |
|
| 215 | + public function testWhenModelTableColumnsIsNotSet() { |
|
| 216 | 216 | //session table is empty |
| 217 | 217 | $this->model->setSessionTableColumns(array()); |
| 218 | 218 | $dbsh = new DBSessionHandler($this->model); |
@@ -21,21 +21,21 @@ discard block |
||
| 21 | 21 | * You should have received a copy of the GNU General Public License |
| 22 | 22 | * along with this program; if not, write to the Free Software |
| 23 | 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 24 | - */ |
|
| 24 | + */ |
|
| 25 | 25 | |
| 26 | 26 | error_reporting(E_ALL | E_STRICT); |
| 27 | 27 | ini_set('display_errors', 1); |
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | - * the directory separator, under windows it is \ and unix, linux / |
|
| 31 | - */ |
|
| 30 | + * the directory separator, under windows it is \ and unix, linux / |
|
| 31 | + */ |
|
| 32 | 32 | define('DS', DIRECTORY_SEPARATOR); |
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | - * The root directory of the application. |
|
| 36 | - * |
|
| 37 | - * you can place this directory outside of your web directory, for example "/home/your_app", etc. |
|
| 38 | - */ |
|
| 35 | + * The root directory of the application. |
|
| 36 | + * |
|
| 37 | + * you can place this directory outside of your web directory, for example "/home/your_app", etc. |
|
| 38 | + */ |
|
| 39 | 39 | define('ROOT_PATH', dirname(realpath(__FILE__)) . DS . '..' . DS); |
| 40 | 40 | |
| 41 | 41 | //tests dir path |
@@ -43,176 +43,176 @@ discard block |
||
| 43 | 43 | |
| 44 | 44 | |
| 45 | 45 | /** |
| 46 | - * The path to the directory. |
|
| 47 | - * |
|
| 48 | - * That contains your static files (javascript, css, images, etc.) |
|
| 49 | - * Note: the path must be relative to the file index.php (the front-end controller). |
|
| 50 | - */ |
|
| 46 | + * The path to the directory. |
|
| 47 | + * |
|
| 48 | + * That contains your static files (javascript, css, images, etc.) |
|
| 49 | + * Note: the path must be relative to the file index.php (the front-end controller). |
|
| 50 | + */ |
|
| 51 | 51 | define('ASSETS_PATH', 'assets/'); |
| 52 | 52 | |
| 53 | 53 | /** |
| 54 | - * The path to the directory of your cache files. |
|
| 55 | - * |
|
| 56 | - * This feature is available currently for database and views. |
|
| 57 | - */ |
|
| 54 | + * The path to the directory of your cache files. |
|
| 55 | + * |
|
| 56 | + * This feature is available currently for database and views. |
|
| 57 | + */ |
|
| 58 | 58 | define('CACHE_PATH', ROOT_PATH . 'cache' . DS); |
| 59 | 59 | |
| 60 | 60 | /** |
| 61 | - * Custom application path for tests |
|
| 62 | - */ |
|
| 61 | + * Custom application path for tests |
|
| 62 | + */ |
|
| 63 | 63 | define('APPS_PATH', TESTS_PATH .'hmvc' . DS); |
| 64 | 64 | |
| 65 | 65 | /** |
| 66 | - * The path to the controller directory of your application. |
|
| 67 | - * |
|
| 68 | - * If you already know the MVC architecture you know what a controller means; |
|
| 69 | - * it is he who makes the business logic of your application in general. |
|
| 70 | - */ |
|
| 66 | + * The path to the controller directory of your application. |
|
| 67 | + * |
|
| 68 | + * If you already know the MVC architecture you know what a controller means; |
|
| 69 | + * it is he who makes the business logic of your application in general. |
|
| 70 | + */ |
|
| 71 | 71 | define('APPS_CONTROLLER_PATH', APPS_PATH . 'controllers' . DS); |
| 72 | 72 | |
| 73 | 73 | /** |
| 74 | - * The path to the directory of your model classes of your application. |
|
| 75 | - * |
|
| 76 | - * If you already know the MVC architecture you know what a model means; |
|
| 77 | - * it's the one who interacts with the database, in one word persistent data from your application. |
|
| 78 | - */ |
|
| 74 | + * The path to the directory of your model classes of your application. |
|
| 75 | + * |
|
| 76 | + * If you already know the MVC architecture you know what a model means; |
|
| 77 | + * it's the one who interacts with the database, in one word persistent data from your application. |
|
| 78 | + */ |
|
| 79 | 79 | define('APPS_MODEL_PATH', APPS_PATH . 'models' . DS); |
| 80 | 80 | |
| 81 | 81 | /** |
| 82 | - * The path to the directory of your views. |
|
| 83 | - * |
|
| 84 | - * If you already know the MVC architecture you know what a view means, |
|
| 85 | - * a view is just a user interface (html page, form, etc.) that is to say |
|
| 86 | - * everything displayed in the browser interface, etc. |
|
| 87 | - */ |
|
| 82 | + * The path to the directory of your views. |
|
| 83 | + * |
|
| 84 | + * If you already know the MVC architecture you know what a view means, |
|
| 85 | + * a view is just a user interface (html page, form, etc.) that is to say |
|
| 86 | + * everything displayed in the browser interface, etc. |
|
| 87 | + */ |
|
| 88 | 88 | define('APPS_VIEWS_PATH', APPS_PATH . 'views' . DS); |
| 89 | 89 | |
| 90 | 90 | /** |
| 91 | - * The path to the configuration directory. |
|
| 92 | - * |
|
| 93 | - * That contains most of the configuration files for your |
|
| 94 | - * application (database, class loading file, functions, etc.) |
|
| 95 | - */ |
|
| 91 | + * The path to the configuration directory. |
|
| 92 | + * |
|
| 93 | + * That contains most of the configuration files for your |
|
| 94 | + * application (database, class loading file, functions, etc.) |
|
| 95 | + */ |
|
| 96 | 96 | define('CONFIG_PATH', ROOT_PATH . 'config' . DS); |
| 97 | 97 | |
| 98 | 98 | /** |
| 99 | - * The core directory |
|
| 100 | - * |
|
| 101 | - * It is recommended to put this folder out of the web directory of your server and |
|
| 102 | - * you should not change its content because in case of update you could lose the modified files. |
|
| 103 | - */ |
|
| 99 | + * The core directory |
|
| 100 | + * |
|
| 101 | + * It is recommended to put this folder out of the web directory of your server and |
|
| 102 | + * you should not change its content because in case of update you could lose the modified files. |
|
| 103 | + */ |
|
| 104 | 104 | define('CORE_PATH', ROOT_PATH . 'core' . DS); |
| 105 | 105 | |
| 106 | 106 | /** |
| 107 | - * The path to the directory of core classes that used by the system. |
|
| 108 | - * |
|
| 109 | - * It contains PHP classes that are used by the framework internally. |
|
| 110 | - */ |
|
| 107 | + * The path to the directory of core classes that used by the system. |
|
| 108 | + * |
|
| 109 | + * It contains PHP classes that are used by the framework internally. |
|
| 110 | + */ |
|
| 111 | 111 | define('CORE_CLASSES_PATH', CORE_PATH . 'classes' . DS); |
| 112 | 112 | |
| 113 | 113 | /** |
| 114 | - * The path to the directory of core classes for the cache used by the system. |
|
| 115 | - * |
|
| 116 | - * It contains PHP classes for the cache drivers. |
|
| 117 | - */ |
|
| 114 | + * The path to the directory of core classes for the cache used by the system. |
|
| 115 | + * |
|
| 116 | + * It contains PHP classes for the cache drivers. |
|
| 117 | + */ |
|
| 118 | 118 | define('CORE_CLASSES_CACHE_PATH', CORE_CLASSES_PATH . 'cache' . DS); |
| 119 | 119 | |
| 120 | 120 | /** |
| 121 | - * The path to the directory of core classes for the model used by the system. |
|
| 122 | - * |
|
| 123 | - * It contains PHP classes for the models. |
|
| 124 | - */ |
|
| 121 | + * The path to the directory of core classes for the model used by the system. |
|
| 122 | + * |
|
| 123 | + * It contains PHP classes for the models. |
|
| 124 | + */ |
|
| 125 | 125 | define('CORE_CLASSES_MODEL_PATH', CORE_CLASSES_PATH . 'model' . DS); |
| 126 | 126 | |
| 127 | 127 | /** |
| 128 | - * The path to the directory of functions or helper systems. |
|
| 129 | - * |
|
| 130 | - * It contains PHP functions that perform a particular task: character string processing, URL, etc. |
|
| 131 | - */ |
|
| 128 | + * The path to the directory of functions or helper systems. |
|
| 129 | + * |
|
| 130 | + * It contains PHP functions that perform a particular task: character string processing, URL, etc. |
|
| 131 | + */ |
|
| 132 | 132 | define('CORE_FUNCTIONS_PATH', CORE_PATH . 'functions' . DS); |
| 133 | 133 | |
| 134 | 134 | /** |
| 135 | - * The path to the core directory of languages files. |
|
| 136 | - * |
|
| 137 | - */ |
|
| 135 | + * The path to the core directory of languages files. |
|
| 136 | + * |
|
| 137 | + */ |
|
| 138 | 138 | define('CORE_LANG_PATH', CORE_PATH . 'lang' . DS); |
| 139 | 139 | |
| 140 | 140 | /** |
| 141 | - * The path to the system library directory. |
|
| 142 | - * |
|
| 143 | - * Which contains the libraries most often used in your web application, as for the |
|
| 144 | - * core directory it is advisable to put it out of the root directory of your application. |
|
| 145 | - */ |
|
| 141 | + * The path to the system library directory. |
|
| 142 | + * |
|
| 143 | + * Which contains the libraries most often used in your web application, as for the |
|
| 144 | + * core directory it is advisable to put it out of the root directory of your application. |
|
| 145 | + */ |
|
| 146 | 146 | define('CORE_LIBRARY_PATH', CORE_PATH . 'libraries' . DS); |
| 147 | 147 | |
| 148 | 148 | /** |
| 149 | - * The path to the system view directory. |
|
| 150 | - * |
|
| 151 | - * That contains the views used for the system, such as error messages, and so on. |
|
| 152 | - */ |
|
| 149 | + * The path to the system view directory. |
|
| 150 | + * |
|
| 151 | + * That contains the views used for the system, such as error messages, and so on. |
|
| 152 | + */ |
|
| 153 | 153 | define('CORE_VIEWS_PATH', CORE_PATH . 'views' . DS); |
| 154 | 154 | |
| 155 | 155 | /** |
| 156 | - * The path to the directory of your PHP personal functions or helper. |
|
| 157 | - * |
|
| 158 | - * It contains your PHP functions that perform a particular task: utilities, etc. |
|
| 159 | - * Note: Do not put your personal functions or helpers in the system functions directory, |
|
| 160 | - * because if you update the system you may lose them. |
|
| 161 | - */ |
|
| 156 | + * The path to the directory of your PHP personal functions or helper. |
|
| 157 | + * |
|
| 158 | + * It contains your PHP functions that perform a particular task: utilities, etc. |
|
| 159 | + * Note: Do not put your personal functions or helpers in the system functions directory, |
|
| 160 | + * because if you update the system you may lose them. |
|
| 161 | + */ |
|
| 162 | 162 | define('FUNCTIONS_PATH', ROOT_PATH . 'functions' . DS); |
| 163 | 163 | |
| 164 | 164 | /** |
| 165 | - * The path to the app directory of personal language. |
|
| 166 | - * |
|
| 167 | - * This feature is not yet available. |
|
| 168 | - * You can help us do this if you are nice or wish to see the developed framework. |
|
| 169 | - */ |
|
| 165 | + * The path to the app directory of personal language. |
|
| 166 | + * |
|
| 167 | + * This feature is not yet available. |
|
| 168 | + * You can help us do this if you are nice or wish to see the developed framework. |
|
| 169 | + */ |
|
| 170 | 170 | define('APP_LANG_PATH', ROOT_PATH . 'lang' . DS); |
| 171 | 171 | |
| 172 | 172 | /** |
| 173 | - * The path to the directory of your personal libraries |
|
| 174 | - * |
|
| 175 | - * It contains your PHP classes, package, etc. |
|
| 176 | - * Note: you should not put your personal libraries in the system library directory, |
|
| 177 | - * because it is recalled in case of updating the system you might have surprises. |
|
| 178 | - */ |
|
| 173 | + * The path to the directory of your personal libraries |
|
| 174 | + * |
|
| 175 | + * It contains your PHP classes, package, etc. |
|
| 176 | + * Note: you should not put your personal libraries in the system library directory, |
|
| 177 | + * because it is recalled in case of updating the system you might have surprises. |
|
| 178 | + */ |
|
| 179 | 179 | define('LIBRARY_PATH', ROOT_PATH . 'libraries' . DS); |
| 180 | 180 | |
| 181 | 181 | /** |
| 182 | - * The path to the directory that contains the log files. |
|
| 183 | - * |
|
| 184 | - * Note: This directory must be available in writing and if possible must have as owner the user who launches your web server, |
|
| 185 | - * under unix or linux most often with the apache web server it is "www-data" or "httpd" even "nobody" for more |
|
| 186 | - * details see the documentation of your web server. |
|
| 187 | - * Example for Unix or linux with apache web server: |
|
| 188 | - * # chmod -R 700 /path/to/your/logs/directory/ |
|
| 189 | - * # chown -R www-data:www-data /path/to/your/logs/directory/ |
|
| 190 | - */ |
|
| 182 | + * The path to the directory that contains the log files. |
|
| 183 | + * |
|
| 184 | + * Note: This directory must be available in writing and if possible must have as owner the user who launches your web server, |
|
| 185 | + * under unix or linux most often with the apache web server it is "www-data" or "httpd" even "nobody" for more |
|
| 186 | + * details see the documentation of your web server. |
|
| 187 | + * Example for Unix or linux with apache web server: |
|
| 188 | + * # chmod -R 700 /path/to/your/logs/directory/ |
|
| 189 | + * # chown -R www-data:www-data /path/to/your/logs/directory/ |
|
| 190 | + */ |
|
| 191 | 191 | define('LOGS_PATH', ROOT_PATH . 'logs' . DS); |
| 192 | 192 | |
| 193 | 193 | /** |
| 194 | - * The path to the modules directory. |
|
| 195 | - * |
|
| 196 | - * It contains your modules used files (config, controllers, libraries, etc.) that is to say which contains your files of the modules, |
|
| 197 | - * in HMVC architecture (hierichical, controllers, models, views). |
|
| 198 | - */ |
|
| 194 | + * The path to the modules directory. |
|
| 195 | + * |
|
| 196 | + * It contains your modules used files (config, controllers, libraries, etc.) that is to say which contains your files of the modules, |
|
| 197 | + * in HMVC architecture (hierichical, controllers, models, views). |
|
| 198 | + */ |
|
| 199 | 199 | define('MODULE_PATH', dirname(realpath(__FILE__)) . DS .'hmvc' . DS . 'modules' . DS); |
| 200 | 200 | |
| 201 | 201 | /** |
| 202 | - * The path to the directory of sources external to your application. |
|
| 203 | - * |
|
| 204 | - * If you have already used "composer" you know what that means. |
|
| 205 | - */ |
|
| 202 | + * The path to the directory of sources external to your application. |
|
| 203 | + * |
|
| 204 | + * If you have already used "composer" you know what that means. |
|
| 205 | + */ |
|
| 206 | 206 | define('VENDOR_PATH', ROOT_PATH . 'vendor' . DS); |
| 207 | 207 | |
| 208 | 208 | /** |
| 209 | - * The front controller of your application. |
|
| 210 | - * |
|
| 211 | - * "index.php" it is through this file that all the requests come, there is a possibility to hidden it in the url of |
|
| 212 | - * your application by using the rewrite module URL of your web server . |
|
| 213 | - * For example, under apache web server, there is a configuration example file that is located at the root |
|
| 214 | - * of your framework folder : "htaccess.txt" rename it to ".htaccess". |
|
| 215 | - */ |
|
| 209 | + * The front controller of your application. |
|
| 210 | + * |
|
| 211 | + * "index.php" it is through this file that all the requests come, there is a possibility to hidden it in the url of |
|
| 212 | + * your application by using the rewrite module URL of your web server . |
|
| 213 | + * For example, under apache web server, there is a configuration example file that is located at the root |
|
| 214 | + * of your framework folder : "htaccess.txt" rename it to ".htaccess". |
|
| 215 | + */ |
|
| 216 | 216 | define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME)); |
| 217 | 217 | |
| 218 | 218 | /** |
@@ -221,14 +221,14 @@ discard block |
||
| 221 | 221 | define('IS_CLI', stripos('cli', php_sapi_name()) !== false); |
| 222 | 222 | |
| 223 | 223 | /** |
| 224 | - * The environment of your application (production, test, development). |
|
| 225 | - * |
|
| 226 | - * if your application is still in development you use the value "development" |
|
| 227 | - * so you will have the display of the error messages, etc. |
|
| 228 | - * Once you finish the development of your application that is to put it online |
|
| 229 | - * you change this value to "production" or "testing", in this case there will be deactivation of error messages, |
|
| 230 | - * the loading of the system, will be fast. |
|
| 231 | - */ |
|
| 224 | + * The environment of your application (production, test, development). |
|
| 225 | + * |
|
| 226 | + * if your application is still in development you use the value "development" |
|
| 227 | + * so you will have the display of the error messages, etc. |
|
| 228 | + * Once you finish the development of your application that is to put it online |
|
| 229 | + * you change this value to "production" or "testing", in this case there will be deactivation of error messages, |
|
| 230 | + * the loading of the system, will be fast. |
|
| 231 | + */ |
|
| 232 | 232 | define('ENVIRONMENT', 'testing'); |
| 233 | 233 | |
| 234 | 234 | |
@@ -262,13 +262,13 @@ discard block |
||
| 262 | 262 | require_once 'include/testsUtil.php'; |
| 263 | 263 | |
| 264 | 264 | /** |
| 265 | - * Setting of the PHP error message handling function |
|
| 266 | - */ |
|
| 265 | + * Setting of the PHP error message handling function |
|
| 266 | + */ |
|
| 267 | 267 | set_error_handler('php_error_handler'); |
| 268 | 268 | |
| 269 | 269 | /** |
| 270 | - * Setting of the PHP error exception handling function |
|
| 271 | - */ |
|
| 270 | + * Setting of the PHP error exception handling function |
|
| 271 | + */ |
|
| 272 | 272 | set_exception_handler('php_exception_handler'); |
| 273 | 273 | |
| 274 | 274 | /** |
@@ -277,8 +277,8 @@ discard block |
||
| 277 | 277 | register_shutdown_function('php_shudown_handler'); |
| 278 | 278 | |
| 279 | 279 | /** |
| 280 | - * Register the tests autoload |
|
| 281 | - */ |
|
| 280 | + * Register the tests autoload |
|
| 281 | + */ |
|
| 282 | 282 | spl_autoload_register('tests_autoload'); |
| 283 | 283 | |
| 284 | 284 | |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | /** |
| 61 | 61 | * Custom application path for tests |
| 62 | 62 | */ |
| 63 | - define('APPS_PATH', TESTS_PATH .'hmvc' . DS); |
|
| 63 | + define('APPS_PATH', TESTS_PATH . 'hmvc' . DS); |
|
| 64 | 64 | |
| 65 | 65 | /** |
| 66 | 66 | * The path to the controller directory of your application. |
@@ -196,7 +196,7 @@ discard block |
||
| 196 | 196 | * It contains your modules used files (config, controllers, libraries, etc.) that is to say which contains your files of the modules, |
| 197 | 197 | * in HMVC architecture (hierichical, controllers, models, views). |
| 198 | 198 | */ |
| 199 | - define('MODULE_PATH', dirname(realpath(__FILE__)) . DS .'hmvc' . DS . 'modules' . DS); |
|
| 199 | + define('MODULE_PATH', dirname(realpath(__FILE__)) . DS . 'hmvc' . DS . 'modules' . DS); |
|
| 200 | 200 | |
| 201 | 201 | /** |
| 202 | 202 | * The path to the directory of sources external to your application. |
@@ -235,12 +235,12 @@ discard block |
||
| 235 | 235 | //Fix to allow test as if application is running in CLI mode $_SESSION global variable is not available |
| 236 | 236 | $_SESSION = array(); |
| 237 | 237 | |
| 238 | - if(! isset($_SERVER['REMOTE_ADDR'])){ |
|
| 238 | + if (!isset($_SERVER['REMOTE_ADDR'])) { |
|
| 239 | 239 | $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; |
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | //check for composer autoload file if exists include it |
| 243 | - if (file_exists(VENDOR_PATH . 'autoload.php')){ |
|
| 243 | + if (file_exists(VENDOR_PATH . 'autoload.php')) { |
|
| 244 | 244 | require_once VENDOR_PATH . 'autoload.php'; |
| 245 | 245 | |
| 246 | 246 | //define the class alias for vstream |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | - class DBSessionModel extends DBSessionHandlerModel{ |
|
| 2 | + class DBSessionModel extends DBSessionHandlerModel { |
|
| 3 | 3 | |
| 4 | 4 | protected $_table = 'ses'; |
| 5 | 5 | protected $primary_key = 's_id'; |
@@ -14,14 +14,14 @@ discard block |
||
| 14 | 14 | 'skey' => 'test_id' //VARCHAR(255) |
| 15 | 15 | ); |
| 16 | 16 | |
| 17 | - public function deleteByTime($time){ |
|
| 17 | + public function deleteByTime($time) { |
|
| 18 | 18 | $this->getQueryBuilder()->from($this->_table) |
| 19 | 19 | ->where('s_time', '<', $time); |
| 20 | 20 | $this->_database->delete(); |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | |
| 24 | - public function getKeyValue(){ |
|
| 24 | + public function getKeyValue() { |
|
| 25 | 25 | $user_id = 0; |
| 26 | 26 | return $user_id; |
| 27 | 27 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | //Autoload function |
| 3 | - function tests_autoload($class){ |
|
| 3 | + function tests_autoload($class) { |
|
| 4 | 4 | $classesMap = array( |
| 5 | 5 | //Caches |
| 6 | 6 | 'ApcCache' => CORE_CLASSES_CACHE_PATH . 'ApcCache.php', |
@@ -40,11 +40,11 @@ discard block |
||
| 40 | 40 | 'StringHash' => CORE_LIBRARY_PATH . 'StringHash.php', |
| 41 | 41 | 'Upload' => CORE_LIBRARY_PATH . 'Upload.php', |
| 42 | 42 | ); |
| 43 | - if(isset($classesMap[$class])){ |
|
| 44 | - if(file_exists($classesMap[$class])){ |
|
| 43 | + if (isset($classesMap[$class])) { |
|
| 44 | + if (file_exists($classesMap[$class])) { |
|
| 45 | 45 | require_once $classesMap[$class]; |
| 46 | 46 | } |
| 47 | - else{ |
|
| 47 | + else { |
|
| 48 | 48 | echo 'File for class ' . $class . ' not found'; |
| 49 | 49 | } |
| 50 | 50 | } |
@@ -43,8 +43,7 @@ |
||
| 43 | 43 | if(isset($classesMap[$class])){ |
| 44 | 44 | if(file_exists($classesMap[$class])){ |
| 45 | 45 | require_once $classesMap[$class]; |
| 46 | - } |
|
| 47 | - else{ |
|
| 46 | + } else{ |
|
| 48 | 47 | echo 'File for class ' . $class . ' not found'; |
| 49 | 48 | } |
| 50 | 49 | } |
@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | * @copyright Copyright (c) 2012, Jamie Rumbelow <http://jamierumbelow.net> |
| 34 | 34 | */ |
| 35 | 35 | |
| 36 | - class Model{ |
|
| 36 | + class Model { |
|
| 37 | 37 | |
| 38 | 38 | /* -------------------------------------------------------------- |
| 39 | 39 | * VARIABLES |
@@ -140,13 +140,13 @@ discard block |
||
| 140 | 140 | * Initialise the model, tie into the CodeIgniter superobject and |
| 141 | 141 | * try our best to guess the table name. |
| 142 | 142 | */ |
| 143 | - public function __construct(Database $db = null){ |
|
| 144 | - if (is_object($db)){ |
|
| 143 | + public function __construct(Database $db = null) { |
|
| 144 | + if (is_object($db)) { |
|
| 145 | 145 | $this->setDatabaseInstance($db); |
| 146 | 146 | } |
| 147 | - else{ |
|
| 147 | + else { |
|
| 148 | 148 | $obj = & get_instance(); |
| 149 | - if (isset($obj->database) && is_object($obj->database)){ |
|
| 149 | + if (isset($obj->database) && is_object($obj->database)) { |
|
| 150 | 150 | /** |
| 151 | 151 | * NOTE: Need use "clone" because some Model need have the personal instance of the database library |
| 152 | 152 | * to prevent duplication |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | |
| 183 | 183 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
| 184 | 184 | { |
| 185 | - $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 185 | + $this->getQueryBuilder()->where($this->soft_delete_key, (bool) $this->_temporary_only_deleted); |
|
| 186 | 186 | } |
| 187 | 187 | $this->_set_where($where); |
| 188 | 188 | |
@@ -224,9 +224,9 @@ discard block |
||
| 224 | 224 | $this->trigger('before_get'); |
| 225 | 225 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
| 226 | 226 | { |
| 227 | - $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 227 | + $this->getQueryBuilder()->where($this->soft_delete_key, (bool) $this->_temporary_only_deleted); |
|
| 228 | 228 | } |
| 229 | - $type = $this->_temporary_return_type == 'array' ? 'array':false; |
|
| 229 | + $type = $this->_temporary_return_type == 'array' ? 'array' : false; |
|
| 230 | 230 | $this->getQueryBuilder()->from($this->_table); |
| 231 | 231 | $result = $this->_database->getAll($type); |
| 232 | 232 | $this->_temporary_return_type = $this->return_type; |
@@ -259,7 +259,7 @@ discard block |
||
| 259 | 259 | $insert_id = $this->_database->insertId(); |
| 260 | 260 | $this->trigger('after_create', $insert_id); |
| 261 | 261 | //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
| 262 | - return ! $insert_id ? true : $insert_id; |
|
| 262 | + return !$insert_id ? true : $insert_id; |
|
| 263 | 263 | } |
| 264 | 264 | else |
| 265 | 265 | { |
@@ -336,13 +336,13 @@ discard block |
||
| 336 | 336 | { |
| 337 | 337 | $args = func_get_args(); |
| 338 | 338 | $data = array(); |
| 339 | - if (count($args) == 2){ |
|
| 340 | - if (is_array($args[1])){ |
|
| 339 | + if (count($args) == 2) { |
|
| 340 | + if (is_array($args[1])) { |
|
| 341 | 341 | $data = array_pop($args); |
| 342 | 342 | } |
| 343 | 343 | } |
| 344 | - else if (count($args) == 3){ |
|
| 345 | - if (is_array($args[2])){ |
|
| 344 | + else if (count($args) == 3) { |
|
| 345 | + if (is_array($args[2])) { |
|
| 346 | 346 | $data = array_pop($args); |
| 347 | 347 | } |
| 348 | 348 | } |
@@ -384,7 +384,7 @@ discard block |
||
| 384 | 384 | if ($this->soft_delete) |
| 385 | 385 | { |
| 386 | 386 | $this->getQueryBuilder()->from($this->_table); |
| 387 | - $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
|
| 387 | + $result = $this->_database->update(array($this->soft_delete_key => TRUE)); |
|
| 388 | 388 | } |
| 389 | 389 | else |
| 390 | 390 | { |
@@ -408,7 +408,7 @@ discard block |
||
| 408 | 408 | if ($this->soft_delete) |
| 409 | 409 | { |
| 410 | 410 | $this->getQueryBuilder()->from($this->_table); |
| 411 | - $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
|
| 411 | + $result = $this->_database->update(array($this->soft_delete_key => TRUE)); |
|
| 412 | 412 | } |
| 413 | 413 | else |
| 414 | 414 | { |
@@ -430,7 +430,7 @@ discard block |
||
| 430 | 430 | if ($this->soft_delete) |
| 431 | 431 | { |
| 432 | 432 | $this->getQueryBuilder()->from($this->_table); |
| 433 | - $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
|
| 433 | + $result = $this->_database->update(array($this->soft_delete_key => TRUE)); |
|
| 434 | 434 | } |
| 435 | 435 | else |
| 436 | 436 | { |
@@ -500,7 +500,7 @@ discard block |
||
| 500 | 500 | $key = $this->primary_key; |
| 501 | 501 | $value = $args[0]; |
| 502 | 502 | } |
| 503 | - $this->trigger('before_dropdown', array( $key, $value )); |
|
| 503 | + $this->trigger('before_dropdown', array($key, $value)); |
|
| 504 | 504 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
| 505 | 505 | { |
| 506 | 506 | $this->getQueryBuilder()->where($this->soft_delete_key, FALSE); |
@@ -525,7 +525,7 @@ discard block |
||
| 525 | 525 | { |
| 526 | 526 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
| 527 | 527 | { |
| 528 | - $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 528 | + $this->getQueryBuilder()->where($this->soft_delete_key, (bool) $this->_temporary_only_deleted); |
|
| 529 | 529 | } |
| 530 | 530 | $where = func_get_args(); |
| 531 | 531 | $this->_set_where($where); |
@@ -541,7 +541,7 @@ discard block |
||
| 541 | 541 | { |
| 542 | 542 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
| 543 | 543 | { |
| 544 | - $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 544 | + $this->getQueryBuilder()->where($this->soft_delete_key, (bool) $this->_temporary_only_deleted); |
|
| 545 | 545 | } |
| 546 | 546 | $this->getQueryBuilder()->from($this->_table); |
| 547 | 547 | $this->_database->getAll(); |
@@ -551,8 +551,8 @@ discard block |
||
| 551 | 551 | /** |
| 552 | 552 | * Enabled cache temporary |
| 553 | 553 | */ |
| 554 | - public function cached($ttl = 0){ |
|
| 555 | - if ($ttl > 0){ |
|
| 554 | + public function cached($ttl = 0) { |
|
| 555 | + if ($ttl > 0) { |
|
| 556 | 556 | $this->_database = $this->_database->cached($ttl); |
| 557 | 557 | } |
| 558 | 558 | return $this; |
@@ -706,13 +706,13 @@ discard block |
||
| 706 | 706 | { |
| 707 | 707 | if (is_object($row)) |
| 708 | 708 | { |
| 709 | - if (isset($row->$attr)){ |
|
| 709 | + if (isset($row->$attr)) { |
|
| 710 | 710 | unset($row->$attr); |
| 711 | 711 | } |
| 712 | 712 | } |
| 713 | 713 | else |
| 714 | 714 | { |
| 715 | - if (isset($row[$attr])){ |
|
| 715 | + if (isset($row[$attr])) { |
|
| 716 | 716 | unset($row[$attr]); |
| 717 | 717 | } |
| 718 | 718 | } |
@@ -724,7 +724,7 @@ discard block |
||
| 724 | 724 | * Return the database instance |
| 725 | 725 | * @return Database the database instance |
| 726 | 726 | */ |
| 727 | - public function getDatabaseInstance(){ |
|
| 727 | + public function getDatabaseInstance() { |
|
| 728 | 728 | return $this->_database; |
| 729 | 729 | } |
| 730 | 730 | |
@@ -732,9 +732,9 @@ discard block |
||
| 732 | 732 | * set the Database instance for future use |
| 733 | 733 | * @param Database $db the database object |
| 734 | 734 | */ |
| 735 | - public function setDatabaseInstance($db){ |
|
| 735 | + public function setDatabaseInstance($db) { |
|
| 736 | 736 | $this->_database = $db; |
| 737 | - if ($this->dbCacheTime > 0){ |
|
| 737 | + if ($this->dbCacheTime > 0) { |
|
| 738 | 738 | $this->_database->setCache($this->dbCacheTime); |
| 739 | 739 | } |
| 740 | 740 | return $this; |
@@ -744,7 +744,7 @@ discard block |
||
| 744 | 744 | * Return the loader instance |
| 745 | 745 | * @return Loader the loader instance |
| 746 | 746 | */ |
| 747 | - public function getLoader(){ |
|
| 747 | + public function getLoader() { |
|
| 748 | 748 | return $this->loaderInstance; |
| 749 | 749 | } |
| 750 | 750 | |
@@ -753,7 +753,7 @@ discard block |
||
| 753 | 753 | * @param Loader $loader the loader object |
| 754 | 754 | * @return object |
| 755 | 755 | */ |
| 756 | - public function setLoader($loader){ |
|
| 756 | + public function setLoader($loader) { |
|
| 757 | 757 | $this->loaderInstance = $loader; |
| 758 | 758 | return $this; |
| 759 | 759 | } |
@@ -762,7 +762,7 @@ discard block |
||
| 762 | 762 | * Return the queryBuilder instance this is the shortcut to database queryBuilder |
| 763 | 763 | * @return object the DatabaseQueryBuilder instance |
| 764 | 764 | */ |
| 765 | - public function getQueryBuilder(){ |
|
| 765 | + public function getQueryBuilder() { |
|
| 766 | 766 | return $this->_database->getQueryBuilder(); |
| 767 | 767 | } |
| 768 | 768 | |
@@ -771,7 +771,7 @@ discard block |
||
| 771 | 771 | * @param object $queryBuilder the DatabaseQueryBuilder object |
| 772 | 772 | * @return object |
| 773 | 773 | */ |
| 774 | - public function setQueryBuilder($queryBuilder){ |
|
| 774 | + public function setQueryBuilder($queryBuilder) { |
|
| 775 | 775 | $this->_database->setQueryBuilder($queryBuilder); |
| 776 | 776 | return $this; |
| 777 | 777 | } |
@@ -781,7 +781,7 @@ discard block |
||
| 781 | 781 | * Return the FormValidation instance |
| 782 | 782 | * @return FormValidation the form validation instance |
| 783 | 783 | */ |
| 784 | - public function getFormValidation(){ |
|
| 784 | + public function getFormValidation() { |
|
| 785 | 785 | return $this->formValidationInstance; |
| 786 | 786 | } |
| 787 | 787 | |
@@ -790,7 +790,7 @@ discard block |
||
| 790 | 790 | * @param FormValidation $fv the form validation object |
| 791 | 791 | * @return object |
| 792 | 792 | */ |
| 793 | - public function setFormValidation($fv){ |
|
| 793 | + public function setFormValidation($fv) { |
|
| 794 | 794 | $this->formValidationInstance = $fv; |
| 795 | 795 | return $this; |
| 796 | 796 | } |
@@ -804,7 +804,7 @@ discard block |
||
| 804 | 804 | */ |
| 805 | 805 | public function order_by($criteria, $order = 'ASC') |
| 806 | 806 | { |
| 807 | - if ( is_array($criteria) ) |
|
| 807 | + if (is_array($criteria)) |
|
| 808 | 808 | { |
| 809 | 809 | foreach ($criteria as $key => $value) |
| 810 | 810 | { |
@@ -835,13 +835,13 @@ discard block |
||
| 835 | 835 | * relate for the relation "belongs_to" |
| 836 | 836 | * @return mixed |
| 837 | 837 | */ |
| 838 | - protected function relateBelongsTo($row){ |
|
| 838 | + protected function relateBelongsTo($row) { |
|
| 839 | 839 | foreach ($this->belongs_to as $key => $value) |
| 840 | 840 | { |
| 841 | 841 | if (is_string($value)) |
| 842 | 842 | { |
| 843 | 843 | $relationship = $value; |
| 844 | - $options = array( 'primary_key' => $value . '_id', 'model' => $value . '_model' ); |
|
| 844 | + $options = array('primary_key' => $value . '_id', 'model' => $value . '_model'); |
|
| 845 | 845 | } |
| 846 | 846 | else |
| 847 | 847 | { |
@@ -851,10 +851,10 @@ discard block |
||
| 851 | 851 | |
| 852 | 852 | if (in_array($relationship, $this->_with)) |
| 853 | 853 | { |
| 854 | - if (is_object($this->loaderInstance)){ |
|
| 854 | + if (is_object($this->loaderInstance)) { |
|
| 855 | 855 | $this->loaderInstance->model($options['model'], $relationship . '_model'); |
| 856 | 856 | } |
| 857 | - else{ |
|
| 857 | + else { |
|
| 858 | 858 | Loader::model($options['model'], $relationship . '_model'); |
| 859 | 859 | } |
| 860 | 860 | if (is_object($row)) |
@@ -874,13 +874,13 @@ discard block |
||
| 874 | 874 | * relate for the relation "has_many" |
| 875 | 875 | * @return mixed |
| 876 | 876 | */ |
| 877 | - protected function relateHasMany($row){ |
|
| 877 | + protected function relateHasMany($row) { |
|
| 878 | 878 | foreach ($this->has_many as $key => $value) |
| 879 | 879 | { |
| 880 | 880 | if (is_string($value)) |
| 881 | 881 | { |
| 882 | 882 | $relationship = $value; |
| 883 | - $options = array( 'primary_key' => $this->_table . '_id', 'model' => $value . '_model' ); |
|
| 883 | + $options = array('primary_key' => $this->_table . '_id', 'model' => $value . '_model'); |
|
| 884 | 884 | } |
| 885 | 885 | else |
| 886 | 886 | { |
@@ -890,10 +890,10 @@ discard block |
||
| 890 | 890 | |
| 891 | 891 | if (in_array($relationship, $this->_with)) |
| 892 | 892 | { |
| 893 | - if (is_object($this->loaderInstance)){ |
|
| 893 | + if (is_object($this->loaderInstance)) { |
|
| 894 | 894 | $this->loaderInstance->model($options['model'], $relationship . '_model'); |
| 895 | 895 | } |
| 896 | - else{ |
|
| 896 | + else { |
|
| 897 | 897 | Loader::model($options['model'], $relationship . '_model'); |
| 898 | 898 | } |
| 899 | 899 | if (is_object($row)) |
@@ -944,10 +944,10 @@ discard block |
||
| 944 | 944 | if (!empty($this->validate)) |
| 945 | 945 | { |
| 946 | 946 | $fv = null; |
| 947 | - if (is_object($this->formValidationInstance)){ |
|
| 947 | + if (is_object($this->formValidationInstance)) { |
|
| 948 | 948 | $fv = $this->formValidationInstance; |
| 949 | 949 | } |
| 950 | - else{ |
|
| 950 | + else { |
|
| 951 | 951 | Loader::library('FormValidation'); |
| 952 | 952 | $fv = $this->formvalidation; |
| 953 | 953 | $this->setFormValidation($fv); |
@@ -976,7 +976,7 @@ discard block |
||
| 976 | 976 | * Set WHERE parameters, when is array |
| 977 | 977 | * @param array $params |
| 978 | 978 | */ |
| 979 | - protected function _set_where_array(array $params){ |
|
| 979 | + protected function _set_where_array(array $params) { |
|
| 980 | 980 | foreach ($params as $field => $filter) |
| 981 | 981 | { |
| 982 | 982 | if (is_array($filter)) |
@@ -1042,7 +1042,7 @@ discard block |
||
| 1042 | 1042 | /** |
| 1043 | 1043 | Shortcut to controller |
| 1044 | 1044 | */ |
| 1045 | - public function __get($key){ |
|
| 1045 | + public function __get($key) { |
|
| 1046 | 1046 | return get_instance()->{$key}; |
| 1047 | 1047 | } |
| 1048 | 1048 | |
@@ -143,8 +143,7 @@ discard block |
||
| 143 | 143 | public function __construct(Database $db = null){ |
| 144 | 144 | if (is_object($db)){ |
| 145 | 145 | $this->setDatabaseInstance($db); |
| 146 | - } |
|
| 147 | - else{ |
|
| 146 | + } else{ |
|
| 148 | 147 | $obj = & get_instance(); |
| 149 | 148 | if (isset($obj->database) && is_object($obj->database)){ |
| 150 | 149 | /** |
@@ -260,8 +259,7 @@ discard block |
||
| 260 | 259 | $this->trigger('after_create', $insert_id); |
| 261 | 260 | //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
| 262 | 261 | return ! $insert_id ? true : $insert_id; |
| 263 | - } |
|
| 264 | - else |
|
| 262 | + } else |
|
| 265 | 263 | { |
| 266 | 264 | return FALSE; |
| 267 | 265 | } |
@@ -298,8 +296,7 @@ discard block |
||
| 298 | 296 | $result = $this->_database->update($data, $escape); |
| 299 | 297 | $this->trigger('after_update', array($data, $result)); |
| 300 | 298 | return $result; |
| 301 | - } |
|
| 302 | - else |
|
| 299 | + } else |
|
| 303 | 300 | { |
| 304 | 301 | return FALSE; |
| 305 | 302 | } |
@@ -322,8 +319,7 @@ discard block |
||
| 322 | 319 | $result = $this->_database->update($data, $escape); |
| 323 | 320 | $this->trigger('after_update', array($data, $result)); |
| 324 | 321 | return $result; |
| 325 | - } |
|
| 326 | - else |
|
| 322 | + } else |
|
| 327 | 323 | { |
| 328 | 324 | return FALSE; |
| 329 | 325 | } |
@@ -340,8 +336,7 @@ discard block |
||
| 340 | 336 | if (is_array($args[1])){ |
| 341 | 337 | $data = array_pop($args); |
| 342 | 338 | } |
| 343 | - } |
|
| 344 | - else if (count($args) == 3){ |
|
| 339 | + } else if (count($args) == 3){ |
|
| 345 | 340 | if (is_array($args[2])){ |
| 346 | 341 | $data = array_pop($args); |
| 347 | 342 | } |
@@ -354,8 +349,7 @@ discard block |
||
| 354 | 349 | $result = $this->_database->update($data); |
| 355 | 350 | $this->trigger('after_update', array($data, $result)); |
| 356 | 351 | return $result; |
| 357 | - } |
|
| 358 | - else |
|
| 352 | + } else |
|
| 359 | 353 | { |
| 360 | 354 | return FALSE; |
| 361 | 355 | } |
@@ -385,8 +379,7 @@ discard block |
||
| 385 | 379 | { |
| 386 | 380 | $this->getQueryBuilder()->from($this->_table); |
| 387 | 381 | $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
| 388 | - } |
|
| 389 | - else |
|
| 382 | + } else |
|
| 390 | 383 | { |
| 391 | 384 | $this->getQueryBuilder()->from($this->_table); |
| 392 | 385 | $result = $this->_database->delete(); |
@@ -409,8 +402,7 @@ discard block |
||
| 409 | 402 | { |
| 410 | 403 | $this->getQueryBuilder()->from($this->_table); |
| 411 | 404 | $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
| 412 | - } |
|
| 413 | - else |
|
| 405 | + } else |
|
| 414 | 406 | { |
| 415 | 407 | $this->getQueryBuilder()->from($this->_table); |
| 416 | 408 | $result = $this->_database->delete(); |
@@ -431,8 +423,7 @@ discard block |
||
| 431 | 423 | { |
| 432 | 424 | $this->getQueryBuilder()->from($this->_table); |
| 433 | 425 | $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
| 434 | - } |
|
| 435 | - else |
|
| 426 | + } else |
|
| 436 | 427 | { |
| 437 | 428 | $this->getQueryBuilder()->from($this->_table); |
| 438 | 429 | $result = $this->_database->delete(); |
@@ -494,8 +485,7 @@ discard block |
||
| 494 | 485 | if (count($args) == 2) |
| 495 | 486 | { |
| 496 | 487 | list($key, $value) = $args; |
| 497 | - } |
|
| 498 | - else |
|
| 488 | + } else |
|
| 499 | 489 | { |
| 500 | 490 | $key = $this->primary_key; |
| 501 | 491 | $value = $args[0]; |
@@ -647,8 +637,7 @@ discard block |
||
| 647 | 637 | if (is_object($row)) |
| 648 | 638 | { |
| 649 | 639 | $row->created_at = date('Y-m-d H:i:s'); |
| 650 | - } |
|
| 651 | - else |
|
| 640 | + } else |
|
| 652 | 641 | { |
| 653 | 642 | $row['created_at'] = date('Y-m-d H:i:s'); |
| 654 | 643 | } |
@@ -660,8 +649,7 @@ discard block |
||
| 660 | 649 | if (is_object($row)) |
| 661 | 650 | { |
| 662 | 651 | $row->updated_at = date('Y-m-d H:i:s'); |
| 663 | - } |
|
| 664 | - else |
|
| 652 | + } else |
|
| 665 | 653 | { |
| 666 | 654 | $row['updated_at'] = date('Y-m-d H:i:s'); |
| 667 | 655 | } |
@@ -688,8 +676,7 @@ discard block |
||
| 688 | 676 | if (is_array($row)) |
| 689 | 677 | { |
| 690 | 678 | $row[$column] = unserialize($row[$column]); |
| 691 | - } |
|
| 692 | - else |
|
| 679 | + } else |
|
| 693 | 680 | { |
| 694 | 681 | $row->$column = unserialize($row->$column); |
| 695 | 682 | } |
@@ -709,8 +696,7 @@ discard block |
||
| 709 | 696 | if (isset($row->$attr)){ |
| 710 | 697 | unset($row->$attr); |
| 711 | 698 | } |
| 712 | - } |
|
| 713 | - else |
|
| 699 | + } else |
|
| 714 | 700 | { |
| 715 | 701 | if (isset($row[$attr])){ |
| 716 | 702 | unset($row[$attr]); |
@@ -810,8 +796,7 @@ discard block |
||
| 810 | 796 | { |
| 811 | 797 | $this->getQueryBuilder()->orderBy($key, $value); |
| 812 | 798 | } |
| 813 | - } |
|
| 814 | - else |
|
| 799 | + } else |
|
| 815 | 800 | { |
| 816 | 801 | $this->getQueryBuilder()->orderBy($criteria, $order); |
| 817 | 802 | } |
@@ -842,8 +827,7 @@ discard block |
||
| 842 | 827 | { |
| 843 | 828 | $relationship = $value; |
| 844 | 829 | $options = array( 'primary_key' => $value . '_id', 'model' => $value . '_model' ); |
| 845 | - } |
|
| 846 | - else |
|
| 830 | + } else |
|
| 847 | 831 | { |
| 848 | 832 | $relationship = $key; |
| 849 | 833 | $options = $value; |
@@ -853,15 +837,13 @@ discard block |
||
| 853 | 837 | { |
| 854 | 838 | if (is_object($this->loaderInstance)){ |
| 855 | 839 | $this->loaderInstance->model($options['model'], $relationship . '_model'); |
| 856 | - } |
|
| 857 | - else{ |
|
| 840 | + } else{ |
|
| 858 | 841 | Loader::model($options['model'], $relationship . '_model'); |
| 859 | 842 | } |
| 860 | 843 | if (is_object($row)) |
| 861 | 844 | { |
| 862 | 845 | $row->{$relationship} = $this->{$relationship . '_model'}->get($row->{$options['primary_key']}); |
| 863 | - } |
|
| 864 | - else |
|
| 846 | + } else |
|
| 865 | 847 | { |
| 866 | 848 | $row[$relationship] = $this->{$relationship . '_model'}->get($row[$options['primary_key']]); |
| 867 | 849 | } |
@@ -881,8 +863,7 @@ discard block |
||
| 881 | 863 | { |
| 882 | 864 | $relationship = $value; |
| 883 | 865 | $options = array( 'primary_key' => $this->_table . '_id', 'model' => $value . '_model' ); |
| 884 | - } |
|
| 885 | - else |
|
| 866 | + } else |
|
| 886 | 867 | { |
| 887 | 868 | $relationship = $key; |
| 888 | 869 | $options = $value; |
@@ -892,15 +873,13 @@ discard block |
||
| 892 | 873 | { |
| 893 | 874 | if (is_object($this->loaderInstance)){ |
| 894 | 875 | $this->loaderInstance->model($options['model'], $relationship . '_model'); |
| 895 | - } |
|
| 896 | - else{ |
|
| 876 | + } else{ |
|
| 897 | 877 | Loader::model($options['model'], $relationship . '_model'); |
| 898 | 878 | } |
| 899 | 879 | if (is_object($row)) |
| 900 | 880 | { |
| 901 | 881 | $row->{$relationship} = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row->{$this->primary_key}); |
| 902 | - } |
|
| 903 | - else |
|
| 882 | + } else |
|
| 904 | 883 | { |
| 905 | 884 | $row[$relationship] = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row[$this->primary_key]); |
| 906 | 885 | } |
@@ -946,8 +925,7 @@ discard block |
||
| 946 | 925 | $fv = null; |
| 947 | 926 | if (is_object($this->formValidationInstance)){ |
| 948 | 927 | $fv = $this->formValidationInstance; |
| 949 | - } |
|
| 950 | - else{ |
|
| 928 | + } else{ |
|
| 951 | 929 | Loader::library('FormValidation'); |
| 952 | 930 | $fv = $this->formvalidation; |
| 953 | 931 | $this->setFormValidation($fv); |
@@ -959,13 +937,11 @@ discard block |
||
| 959 | 937 | if ($fv->run()) |
| 960 | 938 | { |
| 961 | 939 | return $data; |
| 962 | - } |
|
| 963 | - else |
|
| 940 | + } else |
|
| 964 | 941 | { |
| 965 | 942 | return FALSE; |
| 966 | 943 | } |
| 967 | - } |
|
| 968 | - else |
|
| 944 | + } else |
|
| 969 | 945 | { |
| 970 | 946 | return $data; |
| 971 | 947 | } |
@@ -982,14 +958,12 @@ discard block |
||
| 982 | 958 | if (is_array($filter)) |
| 983 | 959 | { |
| 984 | 960 | $this->getQueryBuilder()->in($field, $filter); |
| 985 | - } |
|
| 986 | - else |
|
| 961 | + } else |
|
| 987 | 962 | { |
| 988 | 963 | if (is_int($field)) |
| 989 | 964 | { |
| 990 | 965 | $this->getQueryBuilder()->where($filter); |
| 991 | - } |
|
| 992 | - else |
|
| 966 | + } else |
|
| 993 | 967 | { |
| 994 | 968 | $this->getQueryBuilder()->where($field, $filter); |
| 995 | 969 | } |
@@ -1006,33 +980,27 @@ discard block |
||
| 1006 | 980 | if (count($params) == 1 && is_array($params[0])) |
| 1007 | 981 | { |
| 1008 | 982 | $this->_set_where_array($params[0]); |
| 1009 | - } |
|
| 1010 | - else if (count($params) == 1) |
|
| 983 | + } else if (count($params) == 1) |
|
| 1011 | 984 | { |
| 1012 | 985 | $this->getQueryBuilder()->where($params[0]); |
| 1013 | - } |
|
| 1014 | - else if (count($params) == 2) |
|
| 986 | + } else if (count($params) == 2) |
|
| 1015 | 987 | { |
| 1016 | 988 | if (is_array($params[1])) |
| 1017 | 989 | { |
| 1018 | 990 | $this->getQueryBuilder()->in($params[0], $params[1]); |
| 1019 | - } |
|
| 1020 | - else |
|
| 991 | + } else |
|
| 1021 | 992 | { |
| 1022 | 993 | $this->getQueryBuilder()->where($params[0], $params[1]); |
| 1023 | 994 | } |
| 1024 | - } |
|
| 1025 | - else if (count($params) == 3) |
|
| 995 | + } else if (count($params) == 3) |
|
| 1026 | 996 | { |
| 1027 | 997 | $this->getQueryBuilder()->where($params[0], $params[1], $params[2]); |
| 1028 | - } |
|
| 1029 | - else |
|
| 998 | + } else |
|
| 1030 | 999 | { |
| 1031 | 1000 | if (is_array($params[1])) |
| 1032 | 1001 | { |
| 1033 | 1002 | $this->getQueryBuilder()->in($params[0], $params[1]); |
| 1034 | - } |
|
| 1035 | - else |
|
| 1003 | + } else |
|
| 1036 | 1004 | { |
| 1037 | 1005 | $this->getQueryBuilder()->where($params[0], $params[1]); |
| 1038 | 1006 | } |
@@ -1,561 +1,561 @@ 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 | - |
|
| 116 | - /** |
|
| 117 | - * Set return type array or object |
|
| 118 | - * @var string |
|
| 119 | - */ |
|
| 120 | - 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 | + |
|
| 116 | + /** |
|
| 117 | + * Set return type array or object |
|
| 118 | + * @var string |
|
| 119 | + */ |
|
| 120 | + protected $_temporary_return_type = NULL; |
|
| 121 | 121 | |
| 122 | 122 | |
| 123 | - /** |
|
| 123 | + /** |
|
| 124 | 124 | The database cache time |
| 125 | - */ |
|
| 126 | - protected $dbCacheTime = 0; |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Instance of the Loader class |
|
| 130 | - * @var Loader |
|
| 131 | - */ |
|
| 132 | - protected $loaderInstance = null; |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Instance of the FormValidation library |
|
| 136 | - * @var FormValidation |
|
| 137 | - */ |
|
| 138 | - protected $formValidationInstance = null; |
|
| 125 | + */ |
|
| 126 | + protected $dbCacheTime = 0; |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Instance of the Loader class |
|
| 130 | + * @var Loader |
|
| 131 | + */ |
|
| 132 | + protected $loaderInstance = null; |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Instance of the FormValidation library |
|
| 136 | + * @var FormValidation |
|
| 137 | + */ |
|
| 138 | + protected $formValidationInstance = null; |
|
| 139 | 139 | |
| 140 | - /* -------------------------------------------------------------- |
|
| 140 | + /* -------------------------------------------------------------- |
|
| 141 | 141 | * GENERIC METHODS |
| 142 | 142 | * ------------------------------------------------------------ */ |
| 143 | 143 | |
| 144 | - /** |
|
| 145 | - * Initialise the model, tie into the CodeIgniter superobject and |
|
| 146 | - * try our best to guess the table name. |
|
| 147 | - */ |
|
| 148 | - public function __construct(Database $db = null){ |
|
| 149 | - if (is_object($db)){ |
|
| 150 | - $this->setDatabaseInstance($db); |
|
| 151 | - } |
|
| 152 | - else{ |
|
| 153 | - $obj = & get_instance(); |
|
| 154 | - if (isset($obj->database) && is_object($obj->database)){ |
|
| 155 | - /** |
|
| 156 | - * NOTE: Need use "clone" because some Model need have the personal instance of the database library |
|
| 157 | - * to prevent duplication |
|
| 158 | - */ |
|
| 159 | - $this->setDatabaseInstance(clone $obj->database); |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - array_unshift($this->before_create, 'protect_attributes'); |
|
| 164 | - array_unshift($this->before_update, 'protect_attributes'); |
|
| 165 | - $this->_temporary_return_type = $this->return_type; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /* -------------------------------------------------------------- |
|
| 144 | + /** |
|
| 145 | + * Initialise the model, tie into the CodeIgniter superobject and |
|
| 146 | + * try our best to guess the table name. |
|
| 147 | + */ |
|
| 148 | + public function __construct(Database $db = null){ |
|
| 149 | + if (is_object($db)){ |
|
| 150 | + $this->setDatabaseInstance($db); |
|
| 151 | + } |
|
| 152 | + else{ |
|
| 153 | + $obj = & get_instance(); |
|
| 154 | + if (isset($obj->database) && is_object($obj->database)){ |
|
| 155 | + /** |
|
| 156 | + * NOTE: Need use "clone" because some Model need have the personal instance of the database library |
|
| 157 | + * to prevent duplication |
|
| 158 | + */ |
|
| 159 | + $this->setDatabaseInstance(clone $obj->database); |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + array_unshift($this->before_create, 'protect_attributes'); |
|
| 164 | + array_unshift($this->before_update, 'protect_attributes'); |
|
| 165 | + $this->_temporary_return_type = $this->return_type; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /* -------------------------------------------------------------- |
|
| 169 | 169 | * CRUD INTERFACE |
| 170 | 170 | * ------------------------------------------------------------ */ |
| 171 | 171 | |
| 172 | - /** |
|
| 173 | - * Fetch a single record based on the primary key. Returns an object. |
|
| 174 | - */ |
|
| 175 | - public function get($primary_value) |
|
| 176 | - { |
|
| 177 | - return $this->get_by($this->primary_key, $primary_value); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Fetch a single record based on an arbitrary WHERE call. Can be |
|
| 182 | - * any valid value to DatabaseQueryBuilder->where(). |
|
| 183 | - */ |
|
| 184 | - public function get_by() |
|
| 185 | - { |
|
| 186 | - $where = func_get_args(); |
|
| 187 | - |
|
| 188 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 189 | - { |
|
| 190 | - $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 191 | - } |
|
| 192 | - $this->_set_where($where); |
|
| 193 | - |
|
| 194 | - $this->trigger('before_get'); |
|
| 172 | + /** |
|
| 173 | + * Fetch a single record based on the primary key. Returns an object. |
|
| 174 | + */ |
|
| 175 | + public function get($primary_value) |
|
| 176 | + { |
|
| 177 | + return $this->get_by($this->primary_key, $primary_value); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Fetch a single record based on an arbitrary WHERE call. Can be |
|
| 182 | + * any valid value to DatabaseQueryBuilder->where(). |
|
| 183 | + */ |
|
| 184 | + public function get_by() |
|
| 185 | + { |
|
| 186 | + $where = func_get_args(); |
|
| 187 | + |
|
| 188 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 189 | + { |
|
| 190 | + $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 191 | + } |
|
| 192 | + $this->_set_where($where); |
|
| 193 | + |
|
| 194 | + $this->trigger('before_get'); |
|
| 195 | 195 | $type = $this->_temporary_return_type == 'array' ? 'array' : false; |
| 196 | - $this->getQueryBuilder()->from($this->_table); |
|
| 196 | + $this->getQueryBuilder()->from($this->_table); |
|
| 197 | 197 | $row = $this->_database->get($type); |
| 198 | - $this->_temporary_return_type = $this->return_type; |
|
| 199 | - $row = $this->trigger('after_get', $row); |
|
| 200 | - $this->_with = array(); |
|
| 201 | - return $row; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * Fetch an array of records based on an array of primary values. |
|
| 206 | - */ |
|
| 207 | - public function get_many($values) |
|
| 208 | - { |
|
| 209 | - $this->getQueryBuilder()->in($this->primary_key, $values); |
|
| 210 | - return $this->get_all(); |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * Fetch an array of records based on an arbitrary WHERE call. |
|
| 215 | - */ |
|
| 216 | - public function get_many_by() |
|
| 217 | - { |
|
| 218 | - $where = func_get_args(); |
|
| 219 | - $this->_set_where($where); |
|
| 220 | - return $this->get_all(); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Fetch all the records in the table. Can be used as a generic call |
|
| 225 | - * to $this->_database->get() with scoped methods. |
|
| 226 | - */ |
|
| 227 | - public function get_all() |
|
| 228 | - { |
|
| 229 | - $this->trigger('before_get'); |
|
| 230 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 231 | - { |
|
| 232 | - $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 233 | - } |
|
| 198 | + $this->_temporary_return_type = $this->return_type; |
|
| 199 | + $row = $this->trigger('after_get', $row); |
|
| 200 | + $this->_with = array(); |
|
| 201 | + return $row; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * Fetch an array of records based on an array of primary values. |
|
| 206 | + */ |
|
| 207 | + public function get_many($values) |
|
| 208 | + { |
|
| 209 | + $this->getQueryBuilder()->in($this->primary_key, $values); |
|
| 210 | + return $this->get_all(); |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * Fetch an array of records based on an arbitrary WHERE call. |
|
| 215 | + */ |
|
| 216 | + public function get_many_by() |
|
| 217 | + { |
|
| 218 | + $where = func_get_args(); |
|
| 219 | + $this->_set_where($where); |
|
| 220 | + return $this->get_all(); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Fetch all the records in the table. Can be used as a generic call |
|
| 225 | + * to $this->_database->get() with scoped methods. |
|
| 226 | + */ |
|
| 227 | + public function get_all() |
|
| 228 | + { |
|
| 229 | + $this->trigger('before_get'); |
|
| 230 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 231 | + { |
|
| 232 | + $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 233 | + } |
|
| 234 | 234 | $type = $this->_temporary_return_type == 'array' ? 'array':false; |
| 235 | - $this->getQueryBuilder()->from($this->_table); |
|
| 235 | + $this->getQueryBuilder()->from($this->_table); |
|
| 236 | 236 | $result = $this->_database->getAll($type); |
| 237 | - $this->_temporary_return_type = $this->return_type; |
|
| 238 | - |
|
| 239 | - foreach ($result as $key => &$row) |
|
| 240 | - { |
|
| 241 | - $row = $this->trigger('after_get', $row, ($key == count($result) - 1)); |
|
| 242 | - } |
|
| 243 | - $this->_with = array(); |
|
| 244 | - return $result; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Insert a new row into the table. $data should be an associative array |
|
| 249 | - * of data to be inserted. Returns newly created ID. |
|
| 237 | + $this->_temporary_return_type = $this->return_type; |
|
| 238 | + |
|
| 239 | + foreach ($result as $key => &$row) |
|
| 240 | + { |
|
| 241 | + $row = $this->trigger('after_get', $row, ($key == count($result) - 1)); |
|
| 242 | + } |
|
| 243 | + $this->_with = array(); |
|
| 244 | + return $result; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Insert a new row into the table. $data should be an associative array |
|
| 249 | + * of data to be inserted. Returns newly created ID. |
|
| 250 | 250 | * @see Database::insert |
| 251 | - */ |
|
| 252 | - public function insert($data = array(), $skip_validation = FALSE, $escape = true) |
|
| 253 | - { |
|
| 254 | - if ($skip_validation === FALSE) |
|
| 255 | - { |
|
| 256 | - $data = $this->validate($data); |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - if ($data !== FALSE) |
|
| 260 | - { |
|
| 261 | - $data = $this->trigger('before_create', $data); |
|
| 262 | - $this->getQueryBuilder()->from($this->_table); |
|
| 251 | + */ |
|
| 252 | + public function insert($data = array(), $skip_validation = FALSE, $escape = true) |
|
| 253 | + { |
|
| 254 | + if ($skip_validation === FALSE) |
|
| 255 | + { |
|
| 256 | + $data = $this->validate($data); |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + if ($data !== FALSE) |
|
| 260 | + { |
|
| 261 | + $data = $this->trigger('before_create', $data); |
|
| 262 | + $this->getQueryBuilder()->from($this->_table); |
|
| 263 | 263 | $this->_database->insert($data, $escape); |
| 264 | - $insert_id = $this->_database->insertId(); |
|
| 265 | - $this->trigger('after_create', $insert_id); |
|
| 264 | + $insert_id = $this->_database->insertId(); |
|
| 265 | + $this->trigger('after_create', $insert_id); |
|
| 266 | 266 | //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
| 267 | 267 | return ! $insert_id ? true : $insert_id; |
| 268 | - } |
|
| 269 | - else |
|
| 270 | - { |
|
| 271 | - return FALSE; |
|
| 272 | - } |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * Insert multiple rows into the table. Returns an array of multiple IDs. |
|
| 277 | - */ |
|
| 278 | - public function insert_many($data = array(), $skip_validation = FALSE, $escape = true) |
|
| 279 | - { |
|
| 280 | - $ids = array(); |
|
| 281 | - foreach ($data as $key => $row) |
|
| 282 | - { |
|
| 283 | - $ids[] = $this->insert($row, $skip_validation, $escape); |
|
| 284 | - } |
|
| 285 | - return $ids; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Updated a record based on the primary value. |
|
| 290 | - */ |
|
| 291 | - public function update($primary_value, $data = array(), $skip_validation = FALSE, $escape = true) |
|
| 292 | - { |
|
| 293 | - $data = $this->trigger('before_update', $data); |
|
| 294 | - if ($skip_validation === FALSE) |
|
| 295 | - { |
|
| 296 | - $data = $this->validate($data); |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - if ($data !== FALSE) |
|
| 300 | - { |
|
| 301 | - $this->getQueryBuilder()->where($this->primary_key, $primary_value) |
|
| 302 | - ->from($this->_table); |
|
| 303 | - $result = $this->_database->update($data, $escape); |
|
| 304 | - $this->trigger('after_update', array($data, $result)); |
|
| 305 | - return $result; |
|
| 306 | - } |
|
| 307 | - else |
|
| 308 | - { |
|
| 309 | - return FALSE; |
|
| 310 | - } |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Update many records, based on an array of primary values. |
|
| 315 | - */ |
|
| 316 | - public function update_many($primary_values, $data = array(), $skip_validation = FALSE, $escape = true) |
|
| 317 | - { |
|
| 318 | - $data = $this->trigger('before_update', $data); |
|
| 319 | - if ($skip_validation === FALSE) |
|
| 320 | - { |
|
| 321 | - $data = $this->validate($data); |
|
| 322 | - } |
|
| 323 | - if ($data !== FALSE) |
|
| 324 | - { |
|
| 325 | - $this->getQueryBuilder()->in($this->primary_key, $primary_values) |
|
| 326 | - ->from($this->_table); |
|
| 268 | + } |
|
| 269 | + else |
|
| 270 | + { |
|
| 271 | + return FALSE; |
|
| 272 | + } |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * Insert multiple rows into the table. Returns an array of multiple IDs. |
|
| 277 | + */ |
|
| 278 | + public function insert_many($data = array(), $skip_validation = FALSE, $escape = true) |
|
| 279 | + { |
|
| 280 | + $ids = array(); |
|
| 281 | + foreach ($data as $key => $row) |
|
| 282 | + { |
|
| 283 | + $ids[] = $this->insert($row, $skip_validation, $escape); |
|
| 284 | + } |
|
| 285 | + return $ids; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Updated a record based on the primary value. |
|
| 290 | + */ |
|
| 291 | + public function update($primary_value, $data = array(), $skip_validation = FALSE, $escape = true) |
|
| 292 | + { |
|
| 293 | + $data = $this->trigger('before_update', $data); |
|
| 294 | + if ($skip_validation === FALSE) |
|
| 295 | + { |
|
| 296 | + $data = $this->validate($data); |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + if ($data !== FALSE) |
|
| 300 | + { |
|
| 301 | + $this->getQueryBuilder()->where($this->primary_key, $primary_value) |
|
| 302 | + ->from($this->_table); |
|
| 303 | + $result = $this->_database->update($data, $escape); |
|
| 304 | + $this->trigger('after_update', array($data, $result)); |
|
| 305 | + return $result; |
|
| 306 | + } |
|
| 307 | + else |
|
| 308 | + { |
|
| 309 | + return FALSE; |
|
| 310 | + } |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Update many records, based on an array of primary values. |
|
| 315 | + */ |
|
| 316 | + public function update_many($primary_values, $data = array(), $skip_validation = FALSE, $escape = true) |
|
| 317 | + { |
|
| 318 | + $data = $this->trigger('before_update', $data); |
|
| 319 | + if ($skip_validation === FALSE) |
|
| 320 | + { |
|
| 321 | + $data = $this->validate($data); |
|
| 322 | + } |
|
| 323 | + if ($data !== FALSE) |
|
| 324 | + { |
|
| 325 | + $this->getQueryBuilder()->in($this->primary_key, $primary_values) |
|
| 326 | + ->from($this->_table); |
|
| 327 | 327 | $result = $this->_database->update($data, $escape); |
| 328 | - $this->trigger('after_update', array($data, $result)); |
|
| 329 | - return $result; |
|
| 330 | - } |
|
| 331 | - else |
|
| 332 | - { |
|
| 333 | - return FALSE; |
|
| 334 | - } |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - /** |
|
| 338 | - * Updated a record based on an arbitrary WHERE clause. |
|
| 339 | - */ |
|
| 340 | - public function update_by() |
|
| 341 | - { |
|
| 342 | - $args = func_get_args(); |
|
| 343 | - $data = array(); |
|
| 344 | - if (count($args) == 2){ |
|
| 345 | - if (is_array($args[1])){ |
|
| 346 | - $data = array_pop($args); |
|
| 347 | - } |
|
| 348 | - } |
|
| 349 | - else if (count($args) == 3){ |
|
| 350 | - if (is_array($args[2])){ |
|
| 351 | - $data = array_pop($args); |
|
| 352 | - } |
|
| 353 | - } |
|
| 354 | - $data = $this->trigger('before_update', $data); |
|
| 355 | - if ($this->validate($data) !== FALSE) |
|
| 356 | - { |
|
| 357 | - $this->_set_where($args); |
|
| 358 | - $this->getQueryBuilder()->from($this->_table); |
|
| 328 | + $this->trigger('after_update', array($data, $result)); |
|
| 329 | + return $result; |
|
| 330 | + } |
|
| 331 | + else |
|
| 332 | + { |
|
| 333 | + return FALSE; |
|
| 334 | + } |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + /** |
|
| 338 | + * Updated a record based on an arbitrary WHERE clause. |
|
| 339 | + */ |
|
| 340 | + public function update_by() |
|
| 341 | + { |
|
| 342 | + $args = func_get_args(); |
|
| 343 | + $data = array(); |
|
| 344 | + if (count($args) == 2){ |
|
| 345 | + if (is_array($args[1])){ |
|
| 346 | + $data = array_pop($args); |
|
| 347 | + } |
|
| 348 | + } |
|
| 349 | + else if (count($args) == 3){ |
|
| 350 | + if (is_array($args[2])){ |
|
| 351 | + $data = array_pop($args); |
|
| 352 | + } |
|
| 353 | + } |
|
| 354 | + $data = $this->trigger('before_update', $data); |
|
| 355 | + if ($this->validate($data) !== FALSE) |
|
| 356 | + { |
|
| 357 | + $this->_set_where($args); |
|
| 358 | + $this->getQueryBuilder()->from($this->_table); |
|
| 359 | 359 | $result = $this->_database->update($data); |
| 360 | - $this->trigger('after_update', array($data, $result)); |
|
| 361 | - return $result; |
|
| 362 | - } |
|
| 363 | - else |
|
| 364 | - { |
|
| 365 | - return FALSE; |
|
| 366 | - } |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * Update all records |
|
| 371 | - */ |
|
| 372 | - public function update_all($data = array(), $escape = true) |
|
| 373 | - { |
|
| 374 | - $data = $this->trigger('before_update', $data); |
|
| 375 | - $this->getQueryBuilder()->from($this->_table); |
|
| 360 | + $this->trigger('after_update', array($data, $result)); |
|
| 361 | + return $result; |
|
| 362 | + } |
|
| 363 | + else |
|
| 364 | + { |
|
| 365 | + return FALSE; |
|
| 366 | + } |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * Update all records |
|
| 371 | + */ |
|
| 372 | + public function update_all($data = array(), $escape = true) |
|
| 373 | + { |
|
| 374 | + $data = $this->trigger('before_update', $data); |
|
| 375 | + $this->getQueryBuilder()->from($this->_table); |
|
| 376 | 376 | $result = $this->_database->update($data, $escape); |
| 377 | - $this->trigger('after_update', array($data, $result)); |
|
| 378 | - return $result; |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - /** |
|
| 382 | - * Delete a row from the table by the primary value |
|
| 383 | - */ |
|
| 384 | - public function delete($id) |
|
| 385 | - { |
|
| 386 | - $this->trigger('before_delete', $id); |
|
| 387 | - $this->getQueryBuilder()->where($this->primary_key, $id); |
|
| 377 | + $this->trigger('after_update', array($data, $result)); |
|
| 378 | + return $result; |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + /** |
|
| 382 | + * Delete a row from the table by the primary value |
|
| 383 | + */ |
|
| 384 | + public function delete($id) |
|
| 385 | + { |
|
| 386 | + $this->trigger('before_delete', $id); |
|
| 387 | + $this->getQueryBuilder()->where($this->primary_key, $id); |
|
| 388 | 388 | $result = false; |
| 389 | - if ($this->soft_delete) |
|
| 390 | - { |
|
| 391 | - $this->getQueryBuilder()->from($this->_table); |
|
| 389 | + if ($this->soft_delete) |
|
| 390 | + { |
|
| 391 | + $this->getQueryBuilder()->from($this->_table); |
|
| 392 | 392 | $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
| 393 | - } |
|
| 394 | - else |
|
| 395 | - { |
|
| 396 | - $this->getQueryBuilder()->from($this->_table); |
|
| 393 | + } |
|
| 394 | + else |
|
| 395 | + { |
|
| 396 | + $this->getQueryBuilder()->from($this->_table); |
|
| 397 | 397 | $result = $this->_database->delete(); |
| 398 | - } |
|
| 399 | - |
|
| 400 | - $this->trigger('after_delete', $result); |
|
| 401 | - return $result; |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - /** |
|
| 405 | - * Delete a row from the database table by an arbitrary WHERE clause |
|
| 406 | - */ |
|
| 407 | - public function delete_by() |
|
| 408 | - { |
|
| 409 | - $where = func_get_args(); |
|
| 410 | - $where = $this->trigger('before_delete', $where); |
|
| 411 | - $this->_set_where($where); |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + $this->trigger('after_delete', $result); |
|
| 401 | + return $result; |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + /** |
|
| 405 | + * Delete a row from the database table by an arbitrary WHERE clause |
|
| 406 | + */ |
|
| 407 | + public function delete_by() |
|
| 408 | + { |
|
| 409 | + $where = func_get_args(); |
|
| 410 | + $where = $this->trigger('before_delete', $where); |
|
| 411 | + $this->_set_where($where); |
|
| 412 | 412 | $result = false; |
| 413 | - if ($this->soft_delete) |
|
| 414 | - { |
|
| 415 | - $this->getQueryBuilder()->from($this->_table); |
|
| 413 | + if ($this->soft_delete) |
|
| 414 | + { |
|
| 415 | + $this->getQueryBuilder()->from($this->_table); |
|
| 416 | 416 | $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
| 417 | - } |
|
| 418 | - else |
|
| 419 | - { |
|
| 420 | - $this->getQueryBuilder()->from($this->_table); |
|
| 417 | + } |
|
| 418 | + else |
|
| 419 | + { |
|
| 420 | + $this->getQueryBuilder()->from($this->_table); |
|
| 421 | 421 | $result = $this->_database->delete(); |
| 422 | - } |
|
| 423 | - $this->trigger('after_delete', $result); |
|
| 424 | - return $result; |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - /** |
|
| 428 | - * Delete many rows from the database table by multiple primary values |
|
| 429 | - */ |
|
| 430 | - public function delete_many($primary_values) |
|
| 431 | - { |
|
| 432 | - $primary_values = $this->trigger('before_delete', $primary_values); |
|
| 433 | - $this->getQueryBuilder()->in($this->primary_key, $primary_values); |
|
| 422 | + } |
|
| 423 | + $this->trigger('after_delete', $result); |
|
| 424 | + return $result; |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + /** |
|
| 428 | + * Delete many rows from the database table by multiple primary values |
|
| 429 | + */ |
|
| 430 | + public function delete_many($primary_values) |
|
| 431 | + { |
|
| 432 | + $primary_values = $this->trigger('before_delete', $primary_values); |
|
| 433 | + $this->getQueryBuilder()->in($this->primary_key, $primary_values); |
|
| 434 | 434 | $result = false; |
| 435 | - if ($this->soft_delete) |
|
| 436 | - { |
|
| 437 | - $this->getQueryBuilder()->from($this->_table); |
|
| 435 | + if ($this->soft_delete) |
|
| 436 | + { |
|
| 437 | + $this->getQueryBuilder()->from($this->_table); |
|
| 438 | 438 | $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
| 439 | - } |
|
| 440 | - else |
|
| 441 | - { |
|
| 442 | - $this->getQueryBuilder()->from($this->_table); |
|
| 439 | + } |
|
| 440 | + else |
|
| 441 | + { |
|
| 442 | + $this->getQueryBuilder()->from($this->_table); |
|
| 443 | 443 | $result = $this->_database->delete(); |
| 444 | - } |
|
| 445 | - $this->trigger('after_delete', $result); |
|
| 446 | - return $result; |
|
| 447 | - } |
|
| 444 | + } |
|
| 445 | + $this->trigger('after_delete', $result); |
|
| 446 | + return $result; |
|
| 447 | + } |
|
| 448 | 448 | |
| 449 | 449 | |
| 450 | - /** |
|
| 451 | - * Truncates the table |
|
| 452 | - */ |
|
| 453 | - public function truncate() |
|
| 454 | - { |
|
| 450 | + /** |
|
| 451 | + * Truncates the table |
|
| 452 | + */ |
|
| 453 | + public function truncate() |
|
| 454 | + { |
|
| 455 | 455 | $this->getQueryBuilder()->from($this->_table); |
| 456 | 456 | $result = $this->_database->delete(); |
| 457 | - return $result; |
|
| 458 | - } |
|
| 457 | + return $result; |
|
| 458 | + } |
|
| 459 | 459 | |
| 460 | - /* -------------------------------------------------------------- |
|
| 460 | + /* -------------------------------------------------------------- |
|
| 461 | 461 | * RELATIONSHIPS |
| 462 | 462 | * ------------------------------------------------------------ */ |
| 463 | 463 | |
| 464 | - public function with($relationship) |
|
| 465 | - { |
|
| 466 | - $this->_with[] = $relationship; |
|
| 467 | - if (!in_array('relate', $this->after_get)) |
|
| 468 | - { |
|
| 469 | - $this->after_get[] = 'relate'; |
|
| 470 | - } |
|
| 471 | - return $this; |
|
| 472 | - } |
|
| 464 | + public function with($relationship) |
|
| 465 | + { |
|
| 466 | + $this->_with[] = $relationship; |
|
| 467 | + if (!in_array('relate', $this->after_get)) |
|
| 468 | + { |
|
| 469 | + $this->after_get[] = 'relate'; |
|
| 470 | + } |
|
| 471 | + return $this; |
|
| 472 | + } |
|
| 473 | 473 | |
| 474 | 474 | /** |
| 475 | - * Relationship |
|
| 476 | - */ |
|
| 477 | - public function relate($row) |
|
| 478 | - { |
|
| 479 | - if (empty($row)) |
|
| 480 | - { |
|
| 481 | - return $row; |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - $row = $this->relateBelongsTo($row); |
|
| 485 | - $row = $this->relateHasMany($row); |
|
| 486 | - return $row; |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - /* -------------------------------------------------------------- |
|
| 475 | + * Relationship |
|
| 476 | + */ |
|
| 477 | + public function relate($row) |
|
| 478 | + { |
|
| 479 | + if (empty($row)) |
|
| 480 | + { |
|
| 481 | + return $row; |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + $row = $this->relateBelongsTo($row); |
|
| 485 | + $row = $this->relateHasMany($row); |
|
| 486 | + return $row; |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + /* -------------------------------------------------------------- |
|
| 490 | 490 | * UTILITY METHODS |
| 491 | 491 | * ------------------------------------------------------------ */ |
| 492 | 492 | |
| 493 | - /** |
|
| 494 | - * Retrieve and generate a form_dropdown friendly array |
|
| 495 | - */ |
|
| 496 | - public function dropdown() |
|
| 497 | - { |
|
| 498 | - $args = func_get_args(); |
|
| 499 | - if (count($args) == 2) |
|
| 500 | - { |
|
| 501 | - list($key, $value) = $args; |
|
| 502 | - } |
|
| 503 | - else |
|
| 504 | - { |
|
| 505 | - $key = $this->primary_key; |
|
| 506 | - $value = $args[0]; |
|
| 507 | - } |
|
| 508 | - $this->trigger('before_dropdown', array( $key, $value )); |
|
| 509 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 510 | - { |
|
| 511 | - $this->getQueryBuilder()->where($this->soft_delete_key, FALSE); |
|
| 512 | - } |
|
| 513 | - $this->getQueryBuilder() |
|
| 493 | + /** |
|
| 494 | + * Retrieve and generate a form_dropdown friendly array |
|
| 495 | + */ |
|
| 496 | + public function dropdown() |
|
| 497 | + { |
|
| 498 | + $args = func_get_args(); |
|
| 499 | + if (count($args) == 2) |
|
| 500 | + { |
|
| 501 | + list($key, $value) = $args; |
|
| 502 | + } |
|
| 503 | + else |
|
| 504 | + { |
|
| 505 | + $key = $this->primary_key; |
|
| 506 | + $value = $args[0]; |
|
| 507 | + } |
|
| 508 | + $this->trigger('before_dropdown', array( $key, $value )); |
|
| 509 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 510 | + { |
|
| 511 | + $this->getQueryBuilder()->where($this->soft_delete_key, FALSE); |
|
| 512 | + } |
|
| 513 | + $this->getQueryBuilder() |
|
| 514 | 514 | ->select(array($key, $value)) |
| 515 | 515 | ->from($this->_table); |
| 516 | 516 | $result = $this->_database->getAll(); |
| 517 | - $options = array(); |
|
| 518 | - foreach ($result as $row) |
|
| 519 | - { |
|
| 520 | - $options[$row->{$key}] = $row->{$value}; |
|
| 521 | - } |
|
| 522 | - $options = $this->trigger('after_dropdown', $options); |
|
| 523 | - return $options; |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - /** |
|
| 527 | - * Fetch a count of rows based on an arbitrary WHERE call. |
|
| 528 | - */ |
|
| 529 | - public function count_by() |
|
| 530 | - { |
|
| 531 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 532 | - { |
|
| 533 | - $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 534 | - } |
|
| 535 | - $where = func_get_args(); |
|
| 536 | - $this->_set_where($where); |
|
| 537 | - $this->getQueryBuilder()->from($this->_table); |
|
| 517 | + $options = array(); |
|
| 518 | + foreach ($result as $row) |
|
| 519 | + { |
|
| 520 | + $options[$row->{$key}] = $row->{$value}; |
|
| 521 | + } |
|
| 522 | + $options = $this->trigger('after_dropdown', $options); |
|
| 523 | + return $options; |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + /** |
|
| 527 | + * Fetch a count of rows based on an arbitrary WHERE call. |
|
| 528 | + */ |
|
| 529 | + public function count_by() |
|
| 530 | + { |
|
| 531 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 532 | + { |
|
| 533 | + $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 534 | + } |
|
| 535 | + $where = func_get_args(); |
|
| 536 | + $this->_set_where($where); |
|
| 537 | + $this->getQueryBuilder()->from($this->_table); |
|
| 538 | 538 | $this->_database->getAll(); |
| 539 | - return $this->_database->numRows(); |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - /** |
|
| 543 | - * Fetch a total count of rows, disregarding any previous conditions |
|
| 544 | - */ |
|
| 545 | - public function count_all() |
|
| 546 | - { |
|
| 547 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 548 | - { |
|
| 549 | - $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 550 | - } |
|
| 539 | + return $this->_database->numRows(); |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + /** |
|
| 543 | + * Fetch a total count of rows, disregarding any previous conditions |
|
| 544 | + */ |
|
| 545 | + public function count_all() |
|
| 546 | + { |
|
| 547 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 548 | + { |
|
| 549 | + $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 550 | + } |
|
| 551 | 551 | $this->getQueryBuilder()->from($this->_table); |
| 552 | 552 | $this->_database->getAll(); |
| 553 | - return $this->_database->numRows(); |
|
| 554 | - } |
|
| 553 | + return $this->_database->numRows(); |
|
| 554 | + } |
|
| 555 | 555 | |
| 556 | 556 | /** |
| 557 | - * Enabled cache temporary |
|
| 558 | - */ |
|
| 557 | + * Enabled cache temporary |
|
| 558 | + */ |
|
| 559 | 559 | public function cached($ttl = 0){ |
| 560 | 560 | if ($ttl > 0){ |
| 561 | 561 | $this->_database = $this->_database->cached($ttl); |
@@ -563,424 +563,424 @@ discard block |
||
| 563 | 563 | return $this; |
| 564 | 564 | } |
| 565 | 565 | |
| 566 | - /** |
|
| 567 | - * Tell the class to skip the insert validation |
|
| 568 | - */ |
|
| 569 | - public function skip_validation() |
|
| 570 | - { |
|
| 571 | - $this->skip_validation = TRUE; |
|
| 572 | - return $this; |
|
| 573 | - } |
|
| 574 | - |
|
| 575 | - /** |
|
| 576 | - * Get the skip validation status |
|
| 577 | - */ |
|
| 578 | - public function get_skip_validation() |
|
| 579 | - { |
|
| 580 | - return $this->skip_validation; |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - /** |
|
| 584 | - * Return the next auto increment of the table. Only tested on MySQL. |
|
| 585 | - */ |
|
| 586 | - public function get_next_id() |
|
| 587 | - { |
|
| 566 | + /** |
|
| 567 | + * Tell the class to skip the insert validation |
|
| 568 | + */ |
|
| 569 | + public function skip_validation() |
|
| 570 | + { |
|
| 571 | + $this->skip_validation = TRUE; |
|
| 572 | + return $this; |
|
| 573 | + } |
|
| 574 | + |
|
| 575 | + /** |
|
| 576 | + * Get the skip validation status |
|
| 577 | + */ |
|
| 578 | + public function get_skip_validation() |
|
| 579 | + { |
|
| 580 | + return $this->skip_validation; |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + /** |
|
| 584 | + * Return the next auto increment of the table. Only tested on MySQL. |
|
| 585 | + */ |
|
| 586 | + public function get_next_id() |
|
| 587 | + { |
|
| 588 | 588 | $this->getQueryBuilder()->select('AUTO_INCREMENT') |
| 589 | 589 | ->from('information_schema.TABLES') |
| 590 | 590 | ->where('TABLE_NAME', $this->_table) |
| 591 | 591 | ->where('TABLE_SCHEMA', $this->_database->getDatabaseName()); |
| 592 | - return (int) $this->_database->get()->AUTO_INCREMENT; |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - /** |
|
| 596 | - * Getter for the table name |
|
| 597 | - */ |
|
| 598 | - public function table() |
|
| 599 | - { |
|
| 600 | - return $this->_table; |
|
| 601 | - } |
|
| 602 | - |
|
| 603 | - /* -------------------------------------------------------------- |
|
| 592 | + return (int) $this->_database->get()->AUTO_INCREMENT; |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + /** |
|
| 596 | + * Getter for the table name |
|
| 597 | + */ |
|
| 598 | + public function table() |
|
| 599 | + { |
|
| 600 | + return $this->_table; |
|
| 601 | + } |
|
| 602 | + |
|
| 603 | + /* -------------------------------------------------------------- |
|
| 604 | 604 | * GLOBAL SCOPES |
| 605 | 605 | * ------------------------------------------------------------ */ |
| 606 | 606 | |
| 607 | - /** |
|
| 608 | - * Return the next call as an array rather than an object |
|
| 609 | - */ |
|
| 610 | - public function as_array() |
|
| 611 | - { |
|
| 612 | - $this->_temporary_return_type = 'array'; |
|
| 613 | - return $this; |
|
| 614 | - } |
|
| 615 | - |
|
| 616 | - /** |
|
| 617 | - * Return the next call as an object rather than an array |
|
| 618 | - */ |
|
| 619 | - public function as_object() |
|
| 620 | - { |
|
| 621 | - $this->_temporary_return_type = 'object'; |
|
| 622 | - return $this; |
|
| 623 | - } |
|
| 624 | - |
|
| 625 | - /** |
|
| 626 | - * Don't care about soft deleted rows on the next call |
|
| 627 | - */ |
|
| 628 | - public function with_deleted() |
|
| 629 | - { |
|
| 630 | - $this->_temporary_with_deleted = TRUE; |
|
| 631 | - return $this; |
|
| 632 | - } |
|
| 633 | - |
|
| 634 | - /** |
|
| 635 | - * Only get deleted rows on the next call |
|
| 636 | - */ |
|
| 637 | - public function only_deleted() |
|
| 638 | - { |
|
| 639 | - $this->_temporary_only_deleted = TRUE; |
|
| 640 | - return $this; |
|
| 641 | - } |
|
| 642 | - |
|
| 643 | - /* -------------------------------------------------------------- |
|
| 607 | + /** |
|
| 608 | + * Return the next call as an array rather than an object |
|
| 609 | + */ |
|
| 610 | + public function as_array() |
|
| 611 | + { |
|
| 612 | + $this->_temporary_return_type = 'array'; |
|
| 613 | + return $this; |
|
| 614 | + } |
|
| 615 | + |
|
| 616 | + /** |
|
| 617 | + * Return the next call as an object rather than an array |
|
| 618 | + */ |
|
| 619 | + public function as_object() |
|
| 620 | + { |
|
| 621 | + $this->_temporary_return_type = 'object'; |
|
| 622 | + return $this; |
|
| 623 | + } |
|
| 624 | + |
|
| 625 | + /** |
|
| 626 | + * Don't care about soft deleted rows on the next call |
|
| 627 | + */ |
|
| 628 | + public function with_deleted() |
|
| 629 | + { |
|
| 630 | + $this->_temporary_with_deleted = TRUE; |
|
| 631 | + return $this; |
|
| 632 | + } |
|
| 633 | + |
|
| 634 | + /** |
|
| 635 | + * Only get deleted rows on the next call |
|
| 636 | + */ |
|
| 637 | + public function only_deleted() |
|
| 638 | + { |
|
| 639 | + $this->_temporary_only_deleted = TRUE; |
|
| 640 | + return $this; |
|
| 641 | + } |
|
| 642 | + |
|
| 643 | + /* -------------------------------------------------------------- |
|
| 644 | 644 | * OBSERVERS |
| 645 | 645 | * ------------------------------------------------------------ */ |
| 646 | 646 | |
| 647 | - /** |
|
| 648 | - * MySQL DATETIME created_at and updated_at |
|
| 649 | - */ |
|
| 650 | - public function created_at($row) |
|
| 651 | - { |
|
| 652 | - if (is_object($row)) |
|
| 653 | - { |
|
| 654 | - $row->created_at = date('Y-m-d H:i:s'); |
|
| 655 | - } |
|
| 656 | - else |
|
| 657 | - { |
|
| 658 | - $row['created_at'] = date('Y-m-d H:i:s'); |
|
| 659 | - } |
|
| 660 | - return $row; |
|
| 661 | - } |
|
| 662 | - |
|
| 663 | - public function updated_at($row) |
|
| 664 | - { |
|
| 665 | - if (is_object($row)) |
|
| 666 | - { |
|
| 667 | - $row->updated_at = date('Y-m-d H:i:s'); |
|
| 668 | - } |
|
| 669 | - else |
|
| 670 | - { |
|
| 671 | - $row['updated_at'] = date('Y-m-d H:i:s'); |
|
| 672 | - } |
|
| 673 | - return $row; |
|
| 674 | - } |
|
| 675 | - |
|
| 676 | - /** |
|
| 677 | - * Serialises data for you automatically, allowing you to pass |
|
| 678 | - * through objects and let it handle the serialisation in the background |
|
| 679 | - */ |
|
| 680 | - public function serialize($row) |
|
| 681 | - { |
|
| 682 | - foreach ($this->callback_parameters as $column) |
|
| 683 | - { |
|
| 684 | - $row[$column] = serialize($row[$column]); |
|
| 685 | - } |
|
| 686 | - return $row; |
|
| 687 | - } |
|
| 688 | - |
|
| 689 | - public function unserialize($row) |
|
| 690 | - { |
|
| 691 | - foreach ($this->callback_parameters as $column) |
|
| 692 | - { |
|
| 693 | - if (is_array($row)) |
|
| 694 | - { |
|
| 695 | - $row[$column] = unserialize($row[$column]); |
|
| 696 | - } |
|
| 697 | - else |
|
| 698 | - { |
|
| 699 | - $row->$column = unserialize($row->$column); |
|
| 700 | - } |
|
| 701 | - } |
|
| 702 | - return $row; |
|
| 703 | - } |
|
| 704 | - |
|
| 705 | - /** |
|
| 706 | - * Protect attributes by removing them from $row array |
|
| 707 | - */ |
|
| 708 | - public function protect_attributes($row) |
|
| 709 | - { |
|
| 710 | - foreach ($this->protected_attributes as $attr) |
|
| 711 | - { |
|
| 712 | - if (is_object($row)) |
|
| 713 | - { |
|
| 647 | + /** |
|
| 648 | + * MySQL DATETIME created_at and updated_at |
|
| 649 | + */ |
|
| 650 | + public function created_at($row) |
|
| 651 | + { |
|
| 652 | + if (is_object($row)) |
|
| 653 | + { |
|
| 654 | + $row->created_at = date('Y-m-d H:i:s'); |
|
| 655 | + } |
|
| 656 | + else |
|
| 657 | + { |
|
| 658 | + $row['created_at'] = date('Y-m-d H:i:s'); |
|
| 659 | + } |
|
| 660 | + return $row; |
|
| 661 | + } |
|
| 662 | + |
|
| 663 | + public function updated_at($row) |
|
| 664 | + { |
|
| 665 | + if (is_object($row)) |
|
| 666 | + { |
|
| 667 | + $row->updated_at = date('Y-m-d H:i:s'); |
|
| 668 | + } |
|
| 669 | + else |
|
| 670 | + { |
|
| 671 | + $row['updated_at'] = date('Y-m-d H:i:s'); |
|
| 672 | + } |
|
| 673 | + return $row; |
|
| 674 | + } |
|
| 675 | + |
|
| 676 | + /** |
|
| 677 | + * Serialises data for you automatically, allowing you to pass |
|
| 678 | + * through objects and let it handle the serialisation in the background |
|
| 679 | + */ |
|
| 680 | + public function serialize($row) |
|
| 681 | + { |
|
| 682 | + foreach ($this->callback_parameters as $column) |
|
| 683 | + { |
|
| 684 | + $row[$column] = serialize($row[$column]); |
|
| 685 | + } |
|
| 686 | + return $row; |
|
| 687 | + } |
|
| 688 | + |
|
| 689 | + public function unserialize($row) |
|
| 690 | + { |
|
| 691 | + foreach ($this->callback_parameters as $column) |
|
| 692 | + { |
|
| 693 | + if (is_array($row)) |
|
| 694 | + { |
|
| 695 | + $row[$column] = unserialize($row[$column]); |
|
| 696 | + } |
|
| 697 | + else |
|
| 698 | + { |
|
| 699 | + $row->$column = unserialize($row->$column); |
|
| 700 | + } |
|
| 701 | + } |
|
| 702 | + return $row; |
|
| 703 | + } |
|
| 704 | + |
|
| 705 | + /** |
|
| 706 | + * Protect attributes by removing them from $row array |
|
| 707 | + */ |
|
| 708 | + public function protect_attributes($row) |
|
| 709 | + { |
|
| 710 | + foreach ($this->protected_attributes as $attr) |
|
| 711 | + { |
|
| 712 | + if (is_object($row)) |
|
| 713 | + { |
|
| 714 | 714 | if (isset($row->$attr)){ |
| 715 | 715 | unset($row->$attr); |
| 716 | 716 | } |
| 717 | - } |
|
| 718 | - else |
|
| 719 | - { |
|
| 717 | + } |
|
| 718 | + else |
|
| 719 | + { |
|
| 720 | 720 | if (isset($row[$attr])){ |
| 721 | 721 | unset($row[$attr]); |
| 722 | 722 | } |
| 723 | - } |
|
| 724 | - } |
|
| 725 | - return $row; |
|
| 726 | - } |
|
| 723 | + } |
|
| 724 | + } |
|
| 725 | + return $row; |
|
| 726 | + } |
|
| 727 | 727 | |
| 728 | 728 | /** |
| 729 | - * Return the database instance |
|
| 730 | - * @return Database the database instance |
|
| 731 | - */ |
|
| 732 | - public function getDatabaseInstance(){ |
|
| 733 | - return $this->_database; |
|
| 734 | - } |
|
| 735 | - |
|
| 736 | - /** |
|
| 737 | - * set the Database instance for future use |
|
| 738 | - * @param Database $db the database object |
|
| 739 | - */ |
|
| 740 | - public function setDatabaseInstance($db){ |
|
| 741 | - $this->_database = $db; |
|
| 742 | - if ($this->dbCacheTime > 0){ |
|
| 743 | - $this->_database->setCache($this->dbCacheTime); |
|
| 744 | - } |
|
| 745 | - return $this; |
|
| 746 | - } |
|
| 747 | - |
|
| 748 | - /** |
|
| 749 | - * Return the loader instance |
|
| 750 | - * @return Loader the loader instance |
|
| 751 | - */ |
|
| 752 | - public function getLoader(){ |
|
| 753 | - return $this->loaderInstance; |
|
| 754 | - } |
|
| 755 | - |
|
| 756 | - /** |
|
| 757 | - * Set the loader instance for future use |
|
| 758 | - * @param Loader $loader the loader object |
|
| 729 | + * Return the database instance |
|
| 730 | + * @return Database the database instance |
|
| 731 | + */ |
|
| 732 | + public function getDatabaseInstance(){ |
|
| 733 | + return $this->_database; |
|
| 734 | + } |
|
| 735 | + |
|
| 736 | + /** |
|
| 737 | + * set the Database instance for future use |
|
| 738 | + * @param Database $db the database object |
|
| 739 | + */ |
|
| 740 | + public function setDatabaseInstance($db){ |
|
| 741 | + $this->_database = $db; |
|
| 742 | + if ($this->dbCacheTime > 0){ |
|
| 743 | + $this->_database->setCache($this->dbCacheTime); |
|
| 744 | + } |
|
| 745 | + return $this; |
|
| 746 | + } |
|
| 747 | + |
|
| 748 | + /** |
|
| 749 | + * Return the loader instance |
|
| 750 | + * @return Loader the loader instance |
|
| 751 | + */ |
|
| 752 | + public function getLoader(){ |
|
| 753 | + return $this->loaderInstance; |
|
| 754 | + } |
|
| 755 | + |
|
| 756 | + /** |
|
| 757 | + * Set the loader instance for future use |
|
| 758 | + * @param Loader $loader the loader object |
|
| 759 | 759 | * @return object |
| 760 | - */ |
|
| 761 | - public function setLoader($loader){ |
|
| 762 | - $this->loaderInstance = $loader; |
|
| 763 | - return $this; |
|
| 764 | - } |
|
| 760 | + */ |
|
| 761 | + public function setLoader($loader){ |
|
| 762 | + $this->loaderInstance = $loader; |
|
| 763 | + return $this; |
|
| 764 | + } |
|
| 765 | + |
|
| 766 | + /** |
|
| 767 | + * Return the queryBuilder instance this is the shortcut to database queryBuilder |
|
| 768 | + * @return object the DatabaseQueryBuilder instance |
|
| 769 | + */ |
|
| 770 | + public function getQueryBuilder(){ |
|
| 771 | + return $this->_database->getQueryBuilder(); |
|
| 772 | + } |
|
| 765 | 773 | |
| 766 | 774 | /** |
| 767 | - * Return the queryBuilder instance this is the shortcut to database queryBuilder |
|
| 768 | - * @return object the DatabaseQueryBuilder instance |
|
| 769 | - */ |
|
| 770 | - public function getQueryBuilder(){ |
|
| 771 | - return $this->_database->getQueryBuilder(); |
|
| 772 | - } |
|
| 773 | - |
|
| 774 | - /** |
|
| 775 | - * Set the DatabaseQueryBuilder instance for future use |
|
| 776 | - * @param object $queryBuilder the DatabaseQueryBuilder object |
|
| 775 | + * Set the DatabaseQueryBuilder instance for future use |
|
| 776 | + * @param object $queryBuilder the DatabaseQueryBuilder object |
|
| 777 | 777 | * @return object |
| 778 | - */ |
|
| 779 | - public function setQueryBuilder($queryBuilder){ |
|
| 780 | - $this->_database->setQueryBuilder($queryBuilder); |
|
| 781 | - return $this; |
|
| 782 | - } |
|
| 778 | + */ |
|
| 779 | + public function setQueryBuilder($queryBuilder){ |
|
| 780 | + $this->_database->setQueryBuilder($queryBuilder); |
|
| 781 | + return $this; |
|
| 782 | + } |
|
| 783 | 783 | |
| 784 | 784 | |
| 785 | - /** |
|
| 786 | - * Return the FormValidation instance |
|
| 787 | - * @return FormValidation the form validation instance |
|
| 788 | - */ |
|
| 789 | - public function getFormValidation(){ |
|
| 790 | - return $this->formValidationInstance; |
|
| 791 | - } |
|
| 792 | - |
|
| 793 | - /** |
|
| 794 | - * Set the form validation instance for future use |
|
| 795 | - * @param FormValidation $fv the form validation object |
|
| 785 | + /** |
|
| 786 | + * Return the FormValidation instance |
|
| 787 | + * @return FormValidation the form validation instance |
|
| 788 | + */ |
|
| 789 | + public function getFormValidation(){ |
|
| 790 | + return $this->formValidationInstance; |
|
| 791 | + } |
|
| 792 | + |
|
| 793 | + /** |
|
| 794 | + * Set the form validation instance for future use |
|
| 795 | + * @param FormValidation $fv the form validation object |
|
| 796 | 796 | * @return object |
| 797 | - */ |
|
| 798 | - public function setFormValidation($fv){ |
|
| 799 | - $this->formValidationInstance = $fv; |
|
| 800 | - return $this; |
|
| 801 | - } |
|
| 797 | + */ |
|
| 798 | + public function setFormValidation($fv){ |
|
| 799 | + $this->formValidationInstance = $fv; |
|
| 800 | + return $this; |
|
| 801 | + } |
|
| 802 | 802 | |
| 803 | - /* -------------------------------------------------------------- |
|
| 803 | + /* -------------------------------------------------------------- |
|
| 804 | 804 | * QUERY BUILDER DIRECT ACCESS METHODS |
| 805 | 805 | * ------------------------------------------------------------ */ |
| 806 | 806 | |
| 807 | - /** |
|
| 808 | - * A wrapper to $this->getQueryBuilder()->orderBy() |
|
| 809 | - */ |
|
| 810 | - public function order_by($criteria, $order = 'ASC') |
|
| 811 | - { |
|
| 812 | - if ( is_array($criteria) ) |
|
| 813 | - { |
|
| 814 | - foreach ($criteria as $key => $value) |
|
| 815 | - { |
|
| 816 | - $this->getQueryBuilder()->orderBy($key, $value); |
|
| 817 | - } |
|
| 818 | - } |
|
| 819 | - else |
|
| 820 | - { |
|
| 821 | - $this->getQueryBuilder()->orderBy($criteria, $order); |
|
| 822 | - } |
|
| 823 | - return $this; |
|
| 824 | - } |
|
| 825 | - |
|
| 826 | - /** |
|
| 827 | - * A wrapper to $this->getQueryBuilder()->limit() |
|
| 828 | - */ |
|
| 829 | - public function limit($offset = 0, $limit = 10) |
|
| 830 | - { |
|
| 831 | - $this->getQueryBuilder()->limit($offset, $limit); |
|
| 832 | - return $this; |
|
| 833 | - } |
|
| 834 | - |
|
| 835 | - /* -------------------------------------------------------------- |
|
| 807 | + /** |
|
| 808 | + * A wrapper to $this->getQueryBuilder()->orderBy() |
|
| 809 | + */ |
|
| 810 | + public function order_by($criteria, $order = 'ASC') |
|
| 811 | + { |
|
| 812 | + if ( is_array($criteria) ) |
|
| 813 | + { |
|
| 814 | + foreach ($criteria as $key => $value) |
|
| 815 | + { |
|
| 816 | + $this->getQueryBuilder()->orderBy($key, $value); |
|
| 817 | + } |
|
| 818 | + } |
|
| 819 | + else |
|
| 820 | + { |
|
| 821 | + $this->getQueryBuilder()->orderBy($criteria, $order); |
|
| 822 | + } |
|
| 823 | + return $this; |
|
| 824 | + } |
|
| 825 | + |
|
| 826 | + /** |
|
| 827 | + * A wrapper to $this->getQueryBuilder()->limit() |
|
| 828 | + */ |
|
| 829 | + public function limit($offset = 0, $limit = 10) |
|
| 830 | + { |
|
| 831 | + $this->getQueryBuilder()->limit($offset, $limit); |
|
| 832 | + return $this; |
|
| 833 | + } |
|
| 834 | + |
|
| 835 | + /* -------------------------------------------------------------- |
|
| 836 | 836 | * INTERNAL METHODS |
| 837 | 837 | * ------------------------------------------------------------ */ |
| 838 | 838 | |
| 839 | 839 | /** |
| 840 | - * relate for the relation "belongs_to" |
|
| 841 | - * @return mixed |
|
| 842 | - */ |
|
| 840 | + * relate for the relation "belongs_to" |
|
| 841 | + * @return mixed |
|
| 842 | + */ |
|
| 843 | 843 | protected function relateBelongsTo($row){ |
| 844 | 844 | foreach ($this->belongs_to as $key => $value) |
| 845 | - { |
|
| 846 | - if (is_string($value)) |
|
| 847 | - { |
|
| 848 | - $relationship = $value; |
|
| 849 | - $options = array( 'primary_key' => $value . '_id', 'model' => $value . '_model' ); |
|
| 850 | - } |
|
| 851 | - else |
|
| 852 | - { |
|
| 853 | - $relationship = $key; |
|
| 854 | - $options = $value; |
|
| 855 | - } |
|
| 856 | - |
|
| 857 | - if (in_array($relationship, $this->_with)) |
|
| 858 | - { |
|
| 859 | - if (is_object($this->loaderInstance)){ |
|
| 860 | - $this->loaderInstance->model($options['model'], $relationship . '_model'); |
|
| 861 | - } |
|
| 862 | - else{ |
|
| 863 | - Loader::model($options['model'], $relationship . '_model'); |
|
| 864 | - } |
|
| 865 | - if (is_object($row)) |
|
| 866 | - { |
|
| 867 | - $row->{$relationship} = $this->{$relationship . '_model'}->get($row->{$options['primary_key']}); |
|
| 868 | - } |
|
| 869 | - else |
|
| 870 | - { |
|
| 871 | - $row[$relationship] = $this->{$relationship . '_model'}->get($row[$options['primary_key']]); |
|
| 872 | - } |
|
| 873 | - } |
|
| 874 | - } |
|
| 845 | + { |
|
| 846 | + if (is_string($value)) |
|
| 847 | + { |
|
| 848 | + $relationship = $value; |
|
| 849 | + $options = array( 'primary_key' => $value . '_id', 'model' => $value . '_model' ); |
|
| 850 | + } |
|
| 851 | + else |
|
| 852 | + { |
|
| 853 | + $relationship = $key; |
|
| 854 | + $options = $value; |
|
| 855 | + } |
|
| 856 | + |
|
| 857 | + if (in_array($relationship, $this->_with)) |
|
| 858 | + { |
|
| 859 | + if (is_object($this->loaderInstance)){ |
|
| 860 | + $this->loaderInstance->model($options['model'], $relationship . '_model'); |
|
| 861 | + } |
|
| 862 | + else{ |
|
| 863 | + Loader::model($options['model'], $relationship . '_model'); |
|
| 864 | + } |
|
| 865 | + if (is_object($row)) |
|
| 866 | + { |
|
| 867 | + $row->{$relationship} = $this->{$relationship . '_model'}->get($row->{$options['primary_key']}); |
|
| 868 | + } |
|
| 869 | + else |
|
| 870 | + { |
|
| 871 | + $row[$relationship] = $this->{$relationship . '_model'}->get($row[$options['primary_key']]); |
|
| 872 | + } |
|
| 873 | + } |
|
| 874 | + } |
|
| 875 | 875 | return $row; |
| 876 | 876 | } |
| 877 | 877 | |
| 878 | 878 | /** |
| 879 | - * relate for the relation "has_many" |
|
| 880 | - * @return mixed |
|
| 881 | - */ |
|
| 879 | + * relate for the relation "has_many" |
|
| 880 | + * @return mixed |
|
| 881 | + */ |
|
| 882 | 882 | protected function relateHasMany($row){ |
| 883 | 883 | foreach ($this->has_many as $key => $value) |
| 884 | - { |
|
| 885 | - if (is_string($value)) |
|
| 886 | - { |
|
| 887 | - $relationship = $value; |
|
| 888 | - $options = array( 'primary_key' => $this->_table . '_id', 'model' => $value . '_model' ); |
|
| 889 | - } |
|
| 890 | - else |
|
| 891 | - { |
|
| 892 | - $relationship = $key; |
|
| 893 | - $options = $value; |
|
| 894 | - } |
|
| 895 | - |
|
| 896 | - if (in_array($relationship, $this->_with)) |
|
| 897 | - { |
|
| 898 | - if (is_object($this->loaderInstance)){ |
|
| 899 | - $this->loaderInstance->model($options['model'], $relationship . '_model'); |
|
| 900 | - } |
|
| 901 | - else{ |
|
| 902 | - Loader::model($options['model'], $relationship . '_model'); |
|
| 903 | - } |
|
| 904 | - if (is_object($row)) |
|
| 905 | - { |
|
| 906 | - $row->{$relationship} = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row->{$this->primary_key}); |
|
| 907 | - } |
|
| 908 | - else |
|
| 909 | - { |
|
| 910 | - $row[$relationship] = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row[$this->primary_key]); |
|
| 911 | - } |
|
| 912 | - } |
|
| 913 | - } |
|
| 884 | + { |
|
| 885 | + if (is_string($value)) |
|
| 886 | + { |
|
| 887 | + $relationship = $value; |
|
| 888 | + $options = array( 'primary_key' => $this->_table . '_id', 'model' => $value . '_model' ); |
|
| 889 | + } |
|
| 890 | + else |
|
| 891 | + { |
|
| 892 | + $relationship = $key; |
|
| 893 | + $options = $value; |
|
| 894 | + } |
|
| 895 | + |
|
| 896 | + if (in_array($relationship, $this->_with)) |
|
| 897 | + { |
|
| 898 | + if (is_object($this->loaderInstance)){ |
|
| 899 | + $this->loaderInstance->model($options['model'], $relationship . '_model'); |
|
| 900 | + } |
|
| 901 | + else{ |
|
| 902 | + Loader::model($options['model'], $relationship . '_model'); |
|
| 903 | + } |
|
| 904 | + if (is_object($row)) |
|
| 905 | + { |
|
| 906 | + $row->{$relationship} = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row->{$this->primary_key}); |
|
| 907 | + } |
|
| 908 | + else |
|
| 909 | + { |
|
| 910 | + $row[$relationship] = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row[$this->primary_key]); |
|
| 911 | + } |
|
| 912 | + } |
|
| 913 | + } |
|
| 914 | 914 | return $row; |
| 915 | 915 | } |
| 916 | 916 | |
| 917 | - /** |
|
| 918 | - * Trigger an event and call its observers. Pass through the event name |
|
| 919 | - * (which looks for an instance variable $this->event_name), an array of |
|
| 920 | - * parameters to pass through and an optional 'last in interation' boolean |
|
| 921 | - */ |
|
| 922 | - protected function trigger($event, $data = FALSE, $last = TRUE) |
|
| 923 | - { |
|
| 924 | - if (isset($this->$event) && is_array($this->$event)) |
|
| 925 | - { |
|
| 926 | - foreach ($this->$event as $method) |
|
| 927 | - { |
|
| 928 | - if (strpos($method, '(')) |
|
| 929 | - { |
|
| 930 | - preg_match('/([a-zA-Z0-9\_\-]+)(\(([a-zA-Z0-9\_\-\., ]+)\))?/', $method, $matches); |
|
| 931 | - $method = $matches[1]; |
|
| 932 | - $this->callback_parameters = explode(',', $matches[3]); |
|
| 933 | - } |
|
| 934 | - $data = call_user_func_array(array($this, $method), array($data, $last)); |
|
| 935 | - } |
|
| 936 | - } |
|
| 937 | - return $data; |
|
| 938 | - } |
|
| 939 | - |
|
| 940 | - /** |
|
| 941 | - * Run validation on the passed data |
|
| 942 | - */ |
|
| 943 | - protected function validate(array $data) |
|
| 944 | - { |
|
| 945 | - if ($this->skip_validation) |
|
| 946 | - { |
|
| 947 | - return $data; |
|
| 948 | - } |
|
| 949 | - if (!empty($this->validate)) |
|
| 950 | - { |
|
| 951 | - $fv = null; |
|
| 952 | - if (is_object($this->formValidationInstance)){ |
|
| 953 | - $fv = $this->formValidationInstance; |
|
| 954 | - } |
|
| 955 | - else{ |
|
| 956 | - Loader::library('FormValidation'); |
|
| 957 | - $fv = $this->formvalidation; |
|
| 958 | - $this->setFormValidation($fv); |
|
| 959 | - } |
|
| 917 | + /** |
|
| 918 | + * Trigger an event and call its observers. Pass through the event name |
|
| 919 | + * (which looks for an instance variable $this->event_name), an array of |
|
| 920 | + * parameters to pass through and an optional 'last in interation' boolean |
|
| 921 | + */ |
|
| 922 | + protected function trigger($event, $data = FALSE, $last = TRUE) |
|
| 923 | + { |
|
| 924 | + if (isset($this->$event) && is_array($this->$event)) |
|
| 925 | + { |
|
| 926 | + foreach ($this->$event as $method) |
|
| 927 | + { |
|
| 928 | + if (strpos($method, '(')) |
|
| 929 | + { |
|
| 930 | + preg_match('/([a-zA-Z0-9\_\-]+)(\(([a-zA-Z0-9\_\-\., ]+)\))?/', $method, $matches); |
|
| 931 | + $method = $matches[1]; |
|
| 932 | + $this->callback_parameters = explode(',', $matches[3]); |
|
| 933 | + } |
|
| 934 | + $data = call_user_func_array(array($this, $method), array($data, $last)); |
|
| 935 | + } |
|
| 936 | + } |
|
| 937 | + return $data; |
|
| 938 | + } |
|
| 939 | + |
|
| 940 | + /** |
|
| 941 | + * Run validation on the passed data |
|
| 942 | + */ |
|
| 943 | + protected function validate(array $data) |
|
| 944 | + { |
|
| 945 | + if ($this->skip_validation) |
|
| 946 | + { |
|
| 947 | + return $data; |
|
| 948 | + } |
|
| 949 | + if (!empty($this->validate)) |
|
| 950 | + { |
|
| 951 | + $fv = null; |
|
| 952 | + if (is_object($this->formValidationInstance)){ |
|
| 953 | + $fv = $this->formValidationInstance; |
|
| 954 | + } |
|
| 955 | + else{ |
|
| 956 | + Loader::library('FormValidation'); |
|
| 957 | + $fv = $this->formvalidation; |
|
| 958 | + $this->setFormValidation($fv); |
|
| 959 | + } |
|
| 960 | 960 | |
| 961 | - $fv->setData($data); |
|
| 962 | - $fv->setRules($this->validate); |
|
| 963 | - |
|
| 964 | - if ($fv->run()) |
|
| 965 | - { |
|
| 966 | - return $data; |
|
| 967 | - } |
|
| 968 | - else |
|
| 969 | - { |
|
| 970 | - return FALSE; |
|
| 971 | - } |
|
| 972 | - } |
|
| 973 | - else |
|
| 974 | - { |
|
| 975 | - return $data; |
|
| 976 | - } |
|
| 977 | - } |
|
| 961 | + $fv->setData($data); |
|
| 962 | + $fv->setRules($this->validate); |
|
| 963 | + |
|
| 964 | + if ($fv->run()) |
|
| 965 | + { |
|
| 966 | + return $data; |
|
| 967 | + } |
|
| 968 | + else |
|
| 969 | + { |
|
| 970 | + return FALSE; |
|
| 971 | + } |
|
| 972 | + } |
|
| 973 | + else |
|
| 974 | + { |
|
| 975 | + return $data; |
|
| 976 | + } |
|
| 977 | + } |
|
| 978 | 978 | |
| 979 | 979 | |
| 980 | 980 | /** |
| 981 | - * Set WHERE parameters, when is array |
|
| 982 | - * @param array $params |
|
| 983 | - */ |
|
| 981 | + * Set WHERE parameters, when is array |
|
| 982 | + * @param array $params |
|
| 983 | + */ |
|
| 984 | 984 | protected function _set_where_array(array $params){ |
| 985 | 985 | foreach ($params as $field => $filter) |
| 986 | 986 | { |
@@ -1003,52 +1003,52 @@ discard block |
||
| 1003 | 1003 | } |
| 1004 | 1004 | |
| 1005 | 1005 | |
| 1006 | - /** |
|
| 1007 | - * Set WHERE parameters, cleverly |
|
| 1008 | - */ |
|
| 1009 | - protected function _set_where($params) |
|
| 1010 | - { |
|
| 1011 | - if (count($params) == 1 && is_array($params[0])) |
|
| 1012 | - { |
|
| 1013 | - $this->_set_where_array($params[0]); |
|
| 1014 | - } |
|
| 1015 | - else if (count($params) == 1) |
|
| 1016 | - { |
|
| 1017 | - $this->getQueryBuilder()->where($params[0]); |
|
| 1018 | - } |
|
| 1019 | - else if (count($params) == 2) |
|
| 1020 | - { |
|
| 1021 | - if (is_array($params[1])) |
|
| 1022 | - { |
|
| 1023 | - $this->getQueryBuilder()->in($params[0], $params[1]); |
|
| 1024 | - } |
|
| 1025 | - else |
|
| 1026 | - { |
|
| 1027 | - $this->getQueryBuilder()->where($params[0], $params[1]); |
|
| 1028 | - } |
|
| 1029 | - } |
|
| 1030 | - else if (count($params) == 3) |
|
| 1031 | - { |
|
| 1032 | - $this->getQueryBuilder()->where($params[0], $params[1], $params[2]); |
|
| 1033 | - } |
|
| 1034 | - else |
|
| 1035 | - { |
|
| 1036 | - if (is_array($params[1])) |
|
| 1037 | - { |
|
| 1038 | - $this->getQueryBuilder()->in($params[0], $params[1]); |
|
| 1039 | - } |
|
| 1040 | - else |
|
| 1041 | - { |
|
| 1042 | - $this->getQueryBuilder()->where($params[0], $params[1]); |
|
| 1043 | - } |
|
| 1044 | - } |
|
| 1045 | - } |
|
| 1046 | - |
|
| 1047 | - /** |
|
| 1006 | + /** |
|
| 1007 | + * Set WHERE parameters, cleverly |
|
| 1008 | + */ |
|
| 1009 | + protected function _set_where($params) |
|
| 1010 | + { |
|
| 1011 | + if (count($params) == 1 && is_array($params[0])) |
|
| 1012 | + { |
|
| 1013 | + $this->_set_where_array($params[0]); |
|
| 1014 | + } |
|
| 1015 | + else if (count($params) == 1) |
|
| 1016 | + { |
|
| 1017 | + $this->getQueryBuilder()->where($params[0]); |
|
| 1018 | + } |
|
| 1019 | + else if (count($params) == 2) |
|
| 1020 | + { |
|
| 1021 | + if (is_array($params[1])) |
|
| 1022 | + { |
|
| 1023 | + $this->getQueryBuilder()->in($params[0], $params[1]); |
|
| 1024 | + } |
|
| 1025 | + else |
|
| 1026 | + { |
|
| 1027 | + $this->getQueryBuilder()->where($params[0], $params[1]); |
|
| 1028 | + } |
|
| 1029 | + } |
|
| 1030 | + else if (count($params) == 3) |
|
| 1031 | + { |
|
| 1032 | + $this->getQueryBuilder()->where($params[0], $params[1], $params[2]); |
|
| 1033 | + } |
|
| 1034 | + else |
|
| 1035 | + { |
|
| 1036 | + if (is_array($params[1])) |
|
| 1037 | + { |
|
| 1038 | + $this->getQueryBuilder()->in($params[0], $params[1]); |
|
| 1039 | + } |
|
| 1040 | + else |
|
| 1041 | + { |
|
| 1042 | + $this->getQueryBuilder()->where($params[0], $params[1]); |
|
| 1043 | + } |
|
| 1044 | + } |
|
| 1045 | + } |
|
| 1046 | + |
|
| 1047 | + /** |
|
| 1048 | 1048 | Shortcut to controller |
| 1049 | - */ |
|
| 1050 | - public function __get($key){ |
|
| 1051 | - return get_instance()->{$key}; |
|
| 1052 | - } |
|
| 1049 | + */ |
|
| 1050 | + public function __get($key){ |
|
| 1051 | + return get_instance()->{$key}; |
|
| 1052 | + } |
|
| 1053 | 1053 | |
| 1054 | - } |
|
| 1054 | + } |
|
@@ -153,14 +153,12 @@ discard block |
||
| 153 | 153 | $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
| 154 | 154 | $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
| 155 | 155 | return true; |
| 156 | - } |
|
| 157 | - catch (PDOException $e){ |
|
| 156 | + } catch (PDOException $e){ |
|
| 158 | 157 | $this->logger->fatal($e->getMessage()); |
| 159 | 158 | show_error('Cannot connect to Database.'); |
| 160 | 159 | return false; |
| 161 | 160 | } |
| 162 | - } |
|
| 163 | - else{ |
|
| 161 | + } else{ |
|
| 164 | 162 | show_error('Database configuration is not set.'); |
| 165 | 163 | return false; |
| 166 | 164 | } |
@@ -203,8 +201,7 @@ discard block |
||
| 203 | 201 | $query = $this->getAll(true); |
| 204 | 202 | if ($returnSQLQueryOrResultType === true){ |
| 205 | 203 | return $query; |
| 206 | - } |
|
| 207 | - else{ |
|
| 204 | + } else{ |
|
| 208 | 205 | return $this->query($query, false, $returnSQLQueryOrResultType == 'array'); |
| 209 | 206 | } |
| 210 | 207 | } |
@@ -628,15 +625,13 @@ discard block |
||
| 628 | 625 | //if need return all result like list of record |
| 629 | 626 | if (is_bool($all) && $all){ |
| 630 | 627 | $this->result = ($array === false) ? $pdoStatment->fetchAll(PDO::FETCH_OBJ) : $pdoStatment->fetchAll(PDO::FETCH_ASSOC); |
| 631 | - } |
|
| 632 | - else{ |
|
| 628 | + } else{ |
|
| 633 | 629 | $this->result = ($array === false) ? $pdoStatment->fetch(PDO::FETCH_OBJ) : $pdoStatment->fetch(PDO::FETCH_ASSOC); |
| 634 | 630 | } |
| 635 | 631 | //Sqlite and pgsql always return 0 when using rowCount() |
| 636 | 632 | if (in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
| 637 | 633 | $this->numRows = count($this->result); |
| 638 | - } |
|
| 639 | - else{ |
|
| 634 | + } else{ |
|
| 640 | 635 | $this->numRows = $pdoStatment->rowCount(); |
| 641 | 636 | } |
| 642 | 637 | } |
@@ -650,8 +645,7 @@ discard block |
||
| 650 | 645 | if (in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
| 651 | 646 | $this->result = true; //to test the result for the query like UPDATE, INSERT, DELETE |
| 652 | 647 | $this->numRows = 1; //TODO use the correct method to get the exact affected row |
| 653 | - } |
|
| 654 | - else{ |
|
| 648 | + } else{ |
|
| 655 | 649 | $this->result = $pdoStatment->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
| 656 | 650 | $this->numRows = $pdoStatment->rowCount(); |
| 657 | 651 | } |
@@ -776,8 +770,7 @@ discard block |
||
| 776 | 770 | protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
| 777 | 771 | if ($logger !== null){ |
| 778 | 772 | $this->setLogger($logger); |
| 779 | - } |
|
| 780 | - else{ |
|
| 773 | + } else{ |
|
| 781 | 774 | $this->logger =& class_loader('Log', 'classes'); |
| 782 | 775 | $this->logger->setLogger('Library::Database'); |
| 783 | 776 | } |
@@ -790,8 +783,7 @@ discard block |
||
| 790 | 783 | protected function setQueryBuilderFromParamOrCreateNewInstance(DatabaseQueryBuilder $queryBuilder = null){ |
| 791 | 784 | if ($queryBuilder !== null){ |
| 792 | 785 | $this->setQueryBuilder($queryBuilder); |
| 793 | - } |
|
| 794 | - else{ |
|
| 786 | + } else{ |
|
| 795 | 787 | $this->queryBuilder =& class_loader('DatabaseQueryBuilder', 'classes'); |
| 796 | 788 | } |
| 797 | 789 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | 3 | /** |
| 4 | 4 | * TNH Framework |
| 5 | 5 | * |
@@ -22,266 +22,266 @@ 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 | class Database{ |
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * The PDO instance |
| 30 | 30 | * @var object |
| 31 | - */ |
|
| 32 | - private $pdo = null; |
|
| 31 | + */ |
|
| 32 | + private $pdo = null; |
|
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | 35 | * The database name used for the application |
| 36 | 36 | * @var string |
| 37 | - */ |
|
| 37 | + */ |
|
| 38 | 38 | private $databaseName = null; |
| 39 | 39 | |
| 40 | 40 | /** |
| 41 | 41 | * The number of rows returned by the last query |
| 42 | 42 | * @var int |
| 43 | - */ |
|
| 44 | - private $numRows = 0; |
|
| 43 | + */ |
|
| 44 | + private $numRows = 0; |
|
| 45 | 45 | |
| 46 | 46 | /** |
| 47 | 47 | * The last insert id for the primary key column that have auto increment or sequence |
| 48 | 48 | * @var mixed |
| 49 | - */ |
|
| 50 | - private $insertId = null; |
|
| 49 | + */ |
|
| 50 | + private $insertId = null; |
|
| 51 | 51 | |
| 52 | 52 | /** |
| 53 | 53 | * The full SQL query statment after build for each command |
| 54 | 54 | * @var string |
| 55 | - */ |
|
| 56 | - private $query = null; |
|
| 55 | + */ |
|
| 56 | + private $query = null; |
|
| 57 | 57 | |
| 58 | 58 | /** |
| 59 | 59 | * The error returned for the last query |
| 60 | 60 | * @var string |
| 61 | - */ |
|
| 62 | - private $error = null; |
|
| 61 | + */ |
|
| 62 | + private $error = null; |
|
| 63 | 63 | |
| 64 | 64 | /** |
| 65 | 65 | * The result returned for the last query |
| 66 | 66 | * @var mixed |
| 67 | - */ |
|
| 68 | - private $result = array(); |
|
| 67 | + */ |
|
| 68 | + private $result = array(); |
|
| 69 | 69 | |
| 70 | 70 | /** |
| 71 | 71 | * The cache default time to live in second. 0 means no need to use the cache feature |
| 72 | 72 | * @var int |
| 73 | - */ |
|
| 73 | + */ |
|
| 74 | 74 | private $cacheTtl = 0; |
| 75 | 75 | |
| 76 | 76 | /** |
| 77 | 77 | * The cache current time to live. 0 means no need to use the cache feature |
| 78 | 78 | * @var int |
| 79 | - */ |
|
| 80 | - private $temporaryCacheTtl = 0; |
|
| 79 | + */ |
|
| 80 | + private $temporaryCacheTtl = 0; |
|
| 81 | 81 | |
| 82 | 82 | /** |
| 83 | 83 | * The number of executed query for the current request |
| 84 | 84 | * @var int |
| 85 | - */ |
|
| 86 | - private $queryCount = 0; |
|
| 85 | + */ |
|
| 86 | + private $queryCount = 0; |
|
| 87 | 87 | |
| 88 | 88 | /** |
| 89 | 89 | * The default data to be used in the statments query INSERT, UPDATE |
| 90 | 90 | * @var array |
| 91 | - */ |
|
| 92 | - private $data = array(); |
|
| 91 | + */ |
|
| 92 | + private $data = array(); |
|
| 93 | 93 | |
| 94 | 94 | /** |
| 95 | 95 | * The database configuration |
| 96 | 96 | * @var array |
| 97 | - */ |
|
| 98 | - private $config = array(); |
|
| 97 | + */ |
|
| 98 | + private $config = array(); |
|
| 99 | 99 | |
| 100 | 100 | /** |
| 101 | 101 | * The logger instance |
| 102 | 102 | * @var object |
| 103 | 103 | */ |
| 104 | - private $logger = null; |
|
| 104 | + private $logger = null; |
|
| 105 | 105 | |
| 106 | - /** |
|
| 107 | - * The cache instance |
|
| 108 | - * @var object |
|
| 109 | - */ |
|
| 110 | - private $cacheInstance = null; |
|
| 106 | + /** |
|
| 107 | + * The cache instance |
|
| 108 | + * @var object |
|
| 109 | + */ |
|
| 110 | + private $cacheInstance = null; |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * The benchmark instance |
|
| 114 | - * @var object |
|
| 115 | - */ |
|
| 116 | - private $benchmarkInstance = null; |
|
| 112 | + /** |
|
| 113 | + * The benchmark instance |
|
| 114 | + * @var object |
|
| 115 | + */ |
|
| 116 | + private $benchmarkInstance = null; |
|
| 117 | 117 | |
| 118 | 118 | /** |
| 119 | - * The DatabaseQueryBuilder instance |
|
| 120 | - * @var object |
|
| 121 | - */ |
|
| 122 | - private $queryBuilder = null; |
|
| 119 | + * The DatabaseQueryBuilder instance |
|
| 120 | + * @var object |
|
| 121 | + */ |
|
| 122 | + private $queryBuilder = null; |
|
| 123 | 123 | |
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * Construct new database |
|
| 127 | - * @param array $overwriteConfig the config to overwrite with the config set in database.php |
|
| 128 | - */ |
|
| 129 | - public function __construct($overwriteConfig = array()){ |
|
| 130 | - //Set Log instance to use |
|
| 131 | - $this->setLoggerFromParamOrCreateNewInstance(null); |
|
| 125 | + /** |
|
| 126 | + * Construct new database |
|
| 127 | + * @param array $overwriteConfig the config to overwrite with the config set in database.php |
|
| 128 | + */ |
|
| 129 | + public function __construct($overwriteConfig = array()){ |
|
| 130 | + //Set Log instance to use |
|
| 131 | + $this->setLoggerFromParamOrCreateNewInstance(null); |
|
| 132 | 132 | |
| 133 | - //Set DatabaseQueryBuilder instance to use |
|
| 134 | - $this->setQueryBuilderFromParamOrCreateNewInstance(null); |
|
| 133 | + //Set DatabaseQueryBuilder instance to use |
|
| 134 | + $this->setQueryBuilderFromParamOrCreateNewInstance(null); |
|
| 135 | 135 | |
| 136 | - //Set database configuration |
|
| 137 | - $this->setDatabaseConfiguration($overwriteConfig); |
|
| 136 | + //Set database configuration |
|
| 137 | + $this->setDatabaseConfiguration($overwriteConfig); |
|
| 138 | 138 | |
| 139 | - //cache time to live |
|
| 140 | - $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * This is used to connect to database |
|
| 145 | - * @return bool |
|
| 146 | - */ |
|
| 147 | - public function connect(){ |
|
| 148 | - $config = $this->getDatabaseConfiguration(); |
|
| 149 | - if (! empty($config)){ |
|
| 150 | - try{ |
|
| 151 | - $this->pdo = new PDO($this->getDsnFromDriver(), $config['username'], $config['password']); |
|
| 152 | - $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
|
| 153 | - $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
|
| 154 | - $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
|
| 155 | - return true; |
|
| 156 | - } |
|
| 157 | - catch (PDOException $e){ |
|
| 158 | - $this->logger->fatal($e->getMessage()); |
|
| 159 | - show_error('Cannot connect to Database.'); |
|
| 160 | - return false; |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - else{ |
|
| 164 | - show_error('Database configuration is not set.'); |
|
| 165 | - return false; |
|
| 166 | - } |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Return the number of rows returned by the current query |
|
| 172 | - * @return int |
|
| 173 | - */ |
|
| 174 | - public function numRows(){ |
|
| 175 | - return $this->numRows; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Return the last insert id value |
|
| 180 | - * @return mixed |
|
| 181 | - */ |
|
| 182 | - public function insertId(){ |
|
| 183 | - return $this->insertId; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * Show an error got from the current query (SQL command synthax error, database driver returned error, etc.) |
|
| 188 | - */ |
|
| 189 | - public function error(){ |
|
| 139 | + //cache time to live |
|
| 140 | + $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * This is used to connect to database |
|
| 145 | + * @return bool |
|
| 146 | + */ |
|
| 147 | + public function connect(){ |
|
| 148 | + $config = $this->getDatabaseConfiguration(); |
|
| 149 | + if (! empty($config)){ |
|
| 150 | + try{ |
|
| 151 | + $this->pdo = new PDO($this->getDsnFromDriver(), $config['username'], $config['password']); |
|
| 152 | + $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
|
| 153 | + $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
|
| 154 | + $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
|
| 155 | + return true; |
|
| 156 | + } |
|
| 157 | + catch (PDOException $e){ |
|
| 158 | + $this->logger->fatal($e->getMessage()); |
|
| 159 | + show_error('Cannot connect to Database.'); |
|
| 160 | + return false; |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + else{ |
|
| 164 | + show_error('Database configuration is not set.'); |
|
| 165 | + return false; |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Return the number of rows returned by the current query |
|
| 172 | + * @return int |
|
| 173 | + */ |
|
| 174 | + public function numRows(){ |
|
| 175 | + return $this->numRows; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Return the last insert id value |
|
| 180 | + * @return mixed |
|
| 181 | + */ |
|
| 182 | + public function insertId(){ |
|
| 183 | + return $this->insertId; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * Show an error got from the current query (SQL command synthax error, database driver returned error, etc.) |
|
| 188 | + */ |
|
| 189 | + public function error(){ |
|
| 190 | 190 | if ($this->error){ |
| 191 | 191 | show_error('Query: "' . $this->query . '" Error: ' . $this->error, 'Database Error'); |
| 192 | 192 | } |
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Get the result of one record rows returned by the current query |
|
| 197 | - * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 198 | - * If is string will determine the result type "array" or "object" |
|
| 199 | - * @return mixed the query SQL string or the record result |
|
| 200 | - */ |
|
| 201 | - public function get($returnSQLQueryOrResultType = false){ |
|
| 202 | - $this->getQueryBuilder()->limit(1); |
|
| 203 | - $query = $this->getAll(true); |
|
| 204 | - if ($returnSQLQueryOrResultType === true){ |
|
| 205 | - return $query; |
|
| 206 | - } |
|
| 207 | - else{ |
|
| 208 | - return $this->query($query, false, $returnSQLQueryOrResultType == 'array'); |
|
| 209 | - } |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Get the result of record rows list returned by the current query |
|
| 214 | - * @param boolean|string $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 215 | - * If is string will determine the result type "array" or "object" |
|
| 216 | - * @return mixed the query SQL string or the record result |
|
| 217 | - */ |
|
| 218 | - public function getAll($returnSQLQueryOrResultType = false){ |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Get the result of one record rows returned by the current query |
|
| 197 | + * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 198 | + * If is string will determine the result type "array" or "object" |
|
| 199 | + * @return mixed the query SQL string or the record result |
|
| 200 | + */ |
|
| 201 | + public function get($returnSQLQueryOrResultType = false){ |
|
| 202 | + $this->getQueryBuilder()->limit(1); |
|
| 203 | + $query = $this->getAll(true); |
|
| 204 | + if ($returnSQLQueryOrResultType === true){ |
|
| 205 | + return $query; |
|
| 206 | + } |
|
| 207 | + else{ |
|
| 208 | + return $this->query($query, false, $returnSQLQueryOrResultType == 'array'); |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Get the result of record rows list returned by the current query |
|
| 214 | + * @param boolean|string $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 215 | + * If is string will determine the result type "array" or "object" |
|
| 216 | + * @return mixed the query SQL string or the record result |
|
| 217 | + */ |
|
| 218 | + public function getAll($returnSQLQueryOrResultType = false){ |
|
| 219 | 219 | $query = $this->getQueryBuilder()->getQuery(); |
| 220 | 220 | if ($returnSQLQueryOrResultType === true){ |
| 221 | - return $query; |
|
| 222 | - } |
|
| 223 | - return $this->query($query, true, $returnSQLQueryOrResultType == 'array'); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Insert new record in the database |
|
| 228 | - * @param array $data the record data if is empty will use the $this->data array. |
|
| 229 | - * @param boolean $escape whether to escape or not the values |
|
| 230 | - * @return mixed the insert id of the new record or null |
|
| 231 | - */ |
|
| 232 | - public function insert($data = array(), $escape = true){ |
|
| 233 | - if (empty($data) && $this->getData()){ |
|
| 234 | - //as when using $this->setData() may be the data already escaped |
|
| 235 | - $escape = false; |
|
| 236 | - $data = $this->getData(); |
|
| 237 | - } |
|
| 238 | - $query = $this->getQueryBuilder()->insert($data, $escape)->getQuery(); |
|
| 239 | - $result = $this->query($query); |
|
| 240 | - if ($result){ |
|
| 241 | - $this->insertId = $this->pdo->lastInsertId(); |
|
| 221 | + return $query; |
|
| 222 | + } |
|
| 223 | + return $this->query($query, true, $returnSQLQueryOrResultType == 'array'); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Insert new record in the database |
|
| 228 | + * @param array $data the record data if is empty will use the $this->data array. |
|
| 229 | + * @param boolean $escape whether to escape or not the values |
|
| 230 | + * @return mixed the insert id of the new record or null |
|
| 231 | + */ |
|
| 232 | + public function insert($data = array(), $escape = true){ |
|
| 233 | + if (empty($data) && $this->getData()){ |
|
| 234 | + //as when using $this->setData() may be the data already escaped |
|
| 235 | + $escape = false; |
|
| 236 | + $data = $this->getData(); |
|
| 237 | + } |
|
| 238 | + $query = $this->getQueryBuilder()->insert($data, $escape)->getQuery(); |
|
| 239 | + $result = $this->query($query); |
|
| 240 | + if ($result){ |
|
| 241 | + $this->insertId = $this->pdo->lastInsertId(); |
|
| 242 | 242 | //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
| 243 | - return ! $this->insertId() ? true : $this->insertId(); |
|
| 244 | - } |
|
| 245 | - return false; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * Update record in the database |
|
| 250 | - * @param array $data the record data if is empty will use the $this->data array. |
|
| 251 | - * @param boolean $escape whether to escape or not the values |
|
| 252 | - * @return mixed the update status |
|
| 253 | - */ |
|
| 254 | - public function update($data = array(), $escape = true){ |
|
| 255 | - if (empty($data) && $this->getData()){ |
|
| 256 | - //as when using $this->setData() may be the data already escaped |
|
| 257 | - $escape = false; |
|
| 258 | - $data = $this->getData(); |
|
| 259 | - } |
|
| 260 | - $query = $this->getQueryBuilder()->update($data, $escape)->getQuery(); |
|
| 261 | - return $this->query($query); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * Delete the record in database |
|
| 266 | - * @return mixed the delete status |
|
| 267 | - */ |
|
| 268 | - public function delete(){ |
|
| 243 | + return ! $this->insertId() ? true : $this->insertId(); |
|
| 244 | + } |
|
| 245 | + return false; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * Update record in the database |
|
| 250 | + * @param array $data the record data if is empty will use the $this->data array. |
|
| 251 | + * @param boolean $escape whether to escape or not the values |
|
| 252 | + * @return mixed the update status |
|
| 253 | + */ |
|
| 254 | + public function update($data = array(), $escape = true){ |
|
| 255 | + if (empty($data) && $this->getData()){ |
|
| 256 | + //as when using $this->setData() may be the data already escaped |
|
| 257 | + $escape = false; |
|
| 258 | + $data = $this->getData(); |
|
| 259 | + } |
|
| 260 | + $query = $this->getQueryBuilder()->update($data, $escape)->getQuery(); |
|
| 261 | + return $this->query($query); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * Delete the record in database |
|
| 266 | + * @return mixed the delete status |
|
| 267 | + */ |
|
| 268 | + public function delete(){ |
|
| 269 | 269 | $query = $this->getQueryBuilder()->delete()->getQuery(); |
| 270 | - return $this->query($query); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * Set database cache time to live |
|
| 275 | - * @param integer $ttl the cache time to live in second |
|
| 276 | - * @return object the current Database instance |
|
| 277 | - */ |
|
| 278 | - public function setCache($ttl = 0){ |
|
| 279 | - if ($ttl > 0){ |
|
| 280 | - $this->cacheTtl = $ttl; |
|
| 281 | - $this->temporaryCacheTtl = $ttl; |
|
| 282 | - } |
|
| 283 | - return $this; |
|
| 284 | - } |
|
| 270 | + return $this->query($query); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * Set database cache time to live |
|
| 275 | + * @param integer $ttl the cache time to live in second |
|
| 276 | + * @return object the current Database instance |
|
| 277 | + */ |
|
| 278 | + public function setCache($ttl = 0){ |
|
| 279 | + if ($ttl > 0){ |
|
| 280 | + $this->cacheTtl = $ttl; |
|
| 281 | + $this->temporaryCacheTtl = $ttl; |
|
| 282 | + } |
|
| 283 | + return $this; |
|
| 284 | + } |
|
| 285 | 285 | |
| 286 | 286 | /** |
| 287 | 287 | * Enabled cache temporary for the current query not globally |
@@ -289,272 +289,272 @@ discard block |
||
| 289 | 289 | * @return object the current Database instance |
| 290 | 290 | */ |
| 291 | 291 | public function cached($ttl = 0){ |
| 292 | - if ($ttl > 0){ |
|
| 293 | - $this->temporaryCacheTtl = $ttl; |
|
| 294 | - } |
|
| 295 | - return $this; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * Escape the data before execute query useful for security. |
|
| 300 | - * @param mixed $data the data to be escaped |
|
| 301 | - * @param boolean $escaped whether we can do escape of not |
|
| 302 | - * @return mixed the data after escaped or the same data if not |
|
| 303 | - */ |
|
| 304 | - public function escape($data, $escaped = true){ |
|
| 305 | - return $escaped ? |
|
| 306 | - $this->getPdo()->quote(trim($data)) |
|
| 307 | - : $data; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * Return the number query executed count for the current request |
|
| 312 | - * @return int |
|
| 313 | - */ |
|
| 314 | - public function queryCount(){ |
|
| 315 | - return $this->queryCount; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * Return the current query SQL string |
|
| 320 | - * @return string |
|
| 321 | - */ |
|
| 322 | - public function getQuery(){ |
|
| 323 | - return $this->query; |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * Return the application database name |
|
| 328 | - * @return string |
|
| 329 | - */ |
|
| 330 | - public function getDatabaseName(){ |
|
| 331 | - return $this->databaseName; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * Return the PDO instance |
|
| 336 | - * @return object |
|
| 337 | - */ |
|
| 338 | - public function getPdo(){ |
|
| 339 | - return $this->pdo; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Set the PDO instance |
|
| 344 | - * @param object $pdo the pdo object |
|
| 292 | + if ($ttl > 0){ |
|
| 293 | + $this->temporaryCacheTtl = $ttl; |
|
| 294 | + } |
|
| 295 | + return $this; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * Escape the data before execute query useful for security. |
|
| 300 | + * @param mixed $data the data to be escaped |
|
| 301 | + * @param boolean $escaped whether we can do escape of not |
|
| 302 | + * @return mixed the data after escaped or the same data if not |
|
| 303 | + */ |
|
| 304 | + public function escape($data, $escaped = true){ |
|
| 305 | + return $escaped ? |
|
| 306 | + $this->getPdo()->quote(trim($data)) |
|
| 307 | + : $data; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * Return the number query executed count for the current request |
|
| 312 | + * @return int |
|
| 313 | + */ |
|
| 314 | + public function queryCount(){ |
|
| 315 | + return $this->queryCount; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * Return the current query SQL string |
|
| 320 | + * @return string |
|
| 321 | + */ |
|
| 322 | + public function getQuery(){ |
|
| 323 | + return $this->query; |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * Return the application database name |
|
| 328 | + * @return string |
|
| 329 | + */ |
|
| 330 | + public function getDatabaseName(){ |
|
| 331 | + return $this->databaseName; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * Return the PDO instance |
|
| 336 | + * @return object |
|
| 337 | + */ |
|
| 338 | + public function getPdo(){ |
|
| 339 | + return $this->pdo; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Set the PDO instance |
|
| 344 | + * @param object $pdo the pdo object |
|
| 345 | 345 | * @return object Database |
| 346 | - */ |
|
| 347 | - public function setPdo(PDO $pdo){ |
|
| 348 | - $this->pdo = $pdo; |
|
| 349 | - return $this; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * Return the Log instance |
|
| 355 | - * @return Log |
|
| 356 | - */ |
|
| 357 | - public function getLogger(){ |
|
| 358 | - return $this->logger; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * Set the log instance |
|
| 363 | - * @param Log $logger the log object |
|
| 346 | + */ |
|
| 347 | + public function setPdo(PDO $pdo){ |
|
| 348 | + $this->pdo = $pdo; |
|
| 349 | + return $this; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * Return the Log instance |
|
| 355 | + * @return Log |
|
| 356 | + */ |
|
| 357 | + public function getLogger(){ |
|
| 358 | + return $this->logger; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * Set the log instance |
|
| 363 | + * @param Log $logger the log object |
|
| 364 | 364 | * @return object Database |
| 365 | - */ |
|
| 366 | - public function setLogger($logger){ |
|
| 367 | - $this->logger = $logger; |
|
| 368 | - return $this; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * Return the cache instance |
|
| 373 | - * @return CacheInterface |
|
| 374 | - */ |
|
| 375 | - public function getCacheInstance(){ |
|
| 376 | - return $this->cacheInstance; |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - /** |
|
| 380 | - * Set the cache instance |
|
| 381 | - * @param CacheInterface $cache the cache object |
|
| 365 | + */ |
|
| 366 | + public function setLogger($logger){ |
|
| 367 | + $this->logger = $logger; |
|
| 368 | + return $this; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * Return the cache instance |
|
| 373 | + * @return CacheInterface |
|
| 374 | + */ |
|
| 375 | + public function getCacheInstance(){ |
|
| 376 | + return $this->cacheInstance; |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + /** |
|
| 380 | + * Set the cache instance |
|
| 381 | + * @param CacheInterface $cache the cache object |
|
| 382 | 382 | * @return object Database |
| 383 | - */ |
|
| 384 | - public function setCacheInstance($cache){ |
|
| 385 | - $this->cacheInstance = $cache; |
|
| 386 | - return $this; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - /** |
|
| 390 | - * Return the benchmark instance |
|
| 391 | - * @return Benchmark |
|
| 392 | - */ |
|
| 393 | - public function getBenchmark(){ |
|
| 394 | - return $this->benchmarkInstance; |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * Set the benchmark instance |
|
| 399 | - * @param Benchmark $benchmark the benchmark object |
|
| 383 | + */ |
|
| 384 | + public function setCacheInstance($cache){ |
|
| 385 | + $this->cacheInstance = $cache; |
|
| 386 | + return $this; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + /** |
|
| 390 | + * Return the benchmark instance |
|
| 391 | + * @return Benchmark |
|
| 392 | + */ |
|
| 393 | + public function getBenchmark(){ |
|
| 394 | + return $this->benchmarkInstance; |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * Set the benchmark instance |
|
| 399 | + * @param Benchmark $benchmark the benchmark object |
|
| 400 | 400 | * @return object Database |
| 401 | - */ |
|
| 402 | - public function setBenchmark($benchmark){ |
|
| 403 | - $this->benchmarkInstance = $benchmark; |
|
| 404 | - return $this; |
|
| 405 | - } |
|
| 401 | + */ |
|
| 402 | + public function setBenchmark($benchmark){ |
|
| 403 | + $this->benchmarkInstance = $benchmark; |
|
| 404 | + return $this; |
|
| 405 | + } |
|
| 406 | 406 | |
| 407 | 407 | |
| 408 | 408 | /** |
| 409 | - * Return the DatabaseQueryBuilder instance |
|
| 410 | - * @return object DatabaseQueryBuilder |
|
| 411 | - */ |
|
| 412 | - public function getQueryBuilder(){ |
|
| 413 | - return $this->queryBuilder; |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * Set the DatabaseQueryBuilder instance |
|
| 418 | - * @param object DatabaseQueryBuilder $queryBuilder the DatabaseQueryBuilder object |
|
| 419 | - */ |
|
| 420 | - public function setQueryBuilder(DatabaseQueryBuilder $queryBuilder){ |
|
| 421 | - $this->queryBuilder = $queryBuilder; |
|
| 422 | - return $this; |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - /** |
|
| 426 | - * Return the data to be used for insert, update, etc. |
|
| 427 | - * @return array |
|
| 428 | - */ |
|
| 429 | - public function getData(){ |
|
| 430 | - return $this->data; |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - /** |
|
| 434 | - * Set the data to be used for insert, update, etc. |
|
| 435 | - * @param string|array $key the data key identified |
|
| 436 | - * @param mixed $value the data value |
|
| 437 | - * @param boolean $escape whether to escape or not the $value |
|
| 438 | - * @return object the current Database instance |
|
| 439 | - */ |
|
| 440 | - public function setData($key, $value = null, $escape = true){ |
|
| 409 | + * Return the DatabaseQueryBuilder instance |
|
| 410 | + * @return object DatabaseQueryBuilder |
|
| 411 | + */ |
|
| 412 | + public function getQueryBuilder(){ |
|
| 413 | + return $this->queryBuilder; |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * Set the DatabaseQueryBuilder instance |
|
| 418 | + * @param object DatabaseQueryBuilder $queryBuilder the DatabaseQueryBuilder object |
|
| 419 | + */ |
|
| 420 | + public function setQueryBuilder(DatabaseQueryBuilder $queryBuilder){ |
|
| 421 | + $this->queryBuilder = $queryBuilder; |
|
| 422 | + return $this; |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + /** |
|
| 426 | + * Return the data to be used for insert, update, etc. |
|
| 427 | + * @return array |
|
| 428 | + */ |
|
| 429 | + public function getData(){ |
|
| 430 | + return $this->data; |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + /** |
|
| 434 | + * Set the data to be used for insert, update, etc. |
|
| 435 | + * @param string|array $key the data key identified |
|
| 436 | + * @param mixed $value the data value |
|
| 437 | + * @param boolean $escape whether to escape or not the $value |
|
| 438 | + * @return object the current Database instance |
|
| 439 | + */ |
|
| 440 | + public function setData($key, $value = null, $escape = true){ |
|
| 441 | 441 | if(is_array($key)){ |
| 442 | - foreach($key as $k => $v){ |
|
| 443 | - $this->setData($k, $v, $escape); |
|
| 444 | - } |
|
| 442 | + foreach($key as $k => $v){ |
|
| 443 | + $this->setData($k, $v, $escape); |
|
| 444 | + } |
|
| 445 | 445 | } else { |
| 446 | - $this->data[$key] = $this->escape($value, $escape); |
|
| 446 | + $this->data[$key] = $this->escape($value, $escape); |
|
| 447 | 447 | } |
| 448 | - return $this; |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - /** |
|
| 452 | - * Execute an SQL query |
|
| 453 | - * @param string $query the query SQL string |
|
| 454 | - * @param boolean|array $all if boolean this indicate whether to return all record or not, if array |
|
| 455 | - * will |
|
| 456 | - * @param boolean $array return the result as array |
|
| 457 | - * @return mixed the query result |
|
| 458 | - */ |
|
| 459 | - public function query($query, $all = true, $array = false){ |
|
| 460 | - $this->reset(); |
|
| 461 | - $query = $this->getPreparedQuery($query, $all); |
|
| 462 | - $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
| 448 | + return $this; |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + /** |
|
| 452 | + * Execute an SQL query |
|
| 453 | + * @param string $query the query SQL string |
|
| 454 | + * @param boolean|array $all if boolean this indicate whether to return all record or not, if array |
|
| 455 | + * will |
|
| 456 | + * @param boolean $array return the result as array |
|
| 457 | + * @return mixed the query result |
|
| 458 | + */ |
|
| 459 | + public function query($query, $all = true, $array = false){ |
|
| 460 | + $this->reset(); |
|
| 461 | + $query = $this->getPreparedQuery($query, $all); |
|
| 462 | + $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
| 463 | 463 | |
| 464 | - $isSqlSELECTQuery = stristr($this->query, 'SELECT') !== false; |
|
| 465 | - |
|
| 466 | - $this->logger->info( |
|
| 467 | - 'Execute SQL query ['.$this->query.'], return type: ' |
|
| 468 | - . ($array?'ARRAY':'OBJECT') .', return as list: ' |
|
| 469 | - . (is_bool($all) && $all ? 'YES':'NO') |
|
| 470 | - ); |
|
| 471 | - //cache expire time |
|
| 472 | - $cacheExpire = $this->temporaryCacheTtl; |
|
| 464 | + $isSqlSELECTQuery = stristr($this->query, 'SELECT') !== false; |
|
| 465 | + |
|
| 466 | + $this->logger->info( |
|
| 467 | + 'Execute SQL query ['.$this->query.'], return type: ' |
|
| 468 | + . ($array?'ARRAY':'OBJECT') .', return as list: ' |
|
| 469 | + . (is_bool($all) && $all ? 'YES':'NO') |
|
| 470 | + ); |
|
| 471 | + //cache expire time |
|
| 472 | + $cacheExpire = $this->temporaryCacheTtl; |
|
| 473 | 473 | |
| 474 | - //return to the initial cache time |
|
| 475 | - $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 474 | + //return to the initial cache time |
|
| 475 | + $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 476 | 476 | |
| 477 | - //config for cache |
|
| 478 | - $cacheEnable = get_config('cache_enable'); |
|
| 477 | + //config for cache |
|
| 478 | + $cacheEnable = get_config('cache_enable'); |
|
| 479 | 479 | |
| 480 | - //the database cache content |
|
| 481 | - $cacheContent = null; |
|
| 480 | + //the database cache content |
|
| 481 | + $cacheContent = null; |
|
| 482 | 482 | |
| 483 | - //if can use cache feature for this query |
|
| 484 | - $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
|
| 483 | + //if can use cache feature for this query |
|
| 484 | + $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
|
| 485 | 485 | |
| 486 | - if ($dbCacheStatus && $isSqlSELECTQuery){ |
|
| 487 | - $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
| 488 | - $cacheContent = $this->getCacheContentForQuery($query, $all, $array); |
|
| 489 | - } |
|
| 486 | + if ($dbCacheStatus && $isSqlSELECTQuery){ |
|
| 487 | + $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
| 488 | + $cacheContent = $this->getCacheContentForQuery($query, $all, $array); |
|
| 489 | + } |
|
| 490 | 490 | |
| 491 | - if ( !$cacheContent){ |
|
| 492 | - $sqlQuery = $this->runSqlQuery($query, $all, $array); |
|
| 493 | - if (is_object($sqlQuery)){ |
|
| 494 | - if ($isSqlSELECTQuery){ |
|
| 495 | - $this->setQueryResultForSelect($sqlQuery, $all, $array); |
|
| 496 | - $this->setCacheContentForQuery( |
|
| 497 | - $this->query, |
|
| 498 | - $this->getCacheBenchmarkKeyForQuery($this->query, $all, $array), |
|
| 499 | - $this->result, |
|
| 500 | - $dbCacheStatus && $isSqlSELECTQuery, |
|
| 501 | - $cacheExpire |
|
| 502 | - ); |
|
| 503 | - if (! $this->result){ |
|
| 504 | - $this->logger->info('No result where found for the query [' . $query . ']'); |
|
| 505 | - } |
|
| 506 | - } else { |
|
| 507 | - $this->setQueryResultForNonSelect($sqlQuery); |
|
| 508 | - } |
|
| 509 | - } |
|
| 510 | - } else if ($isSqlSELECTQuery){ |
|
| 511 | - $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 512 | - $this->result = $cacheContent; |
|
| 513 | - $this->numRows = count($this->result); |
|
| 514 | - } |
|
| 515 | - return $this->result; |
|
| 516 | - } |
|
| 491 | + if ( !$cacheContent){ |
|
| 492 | + $sqlQuery = $this->runSqlQuery($query, $all, $array); |
|
| 493 | + if (is_object($sqlQuery)){ |
|
| 494 | + if ($isSqlSELECTQuery){ |
|
| 495 | + $this->setQueryResultForSelect($sqlQuery, $all, $array); |
|
| 496 | + $this->setCacheContentForQuery( |
|
| 497 | + $this->query, |
|
| 498 | + $this->getCacheBenchmarkKeyForQuery($this->query, $all, $array), |
|
| 499 | + $this->result, |
|
| 500 | + $dbCacheStatus && $isSqlSELECTQuery, |
|
| 501 | + $cacheExpire |
|
| 502 | + ); |
|
| 503 | + if (! $this->result){ |
|
| 504 | + $this->logger->info('No result where found for the query [' . $query . ']'); |
|
| 505 | + } |
|
| 506 | + } else { |
|
| 507 | + $this->setQueryResultForNonSelect($sqlQuery); |
|
| 508 | + } |
|
| 509 | + } |
|
| 510 | + } else if ($isSqlSELECTQuery){ |
|
| 511 | + $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 512 | + $this->result = $cacheContent; |
|
| 513 | + $this->numRows = count($this->result); |
|
| 514 | + } |
|
| 515 | + return $this->result; |
|
| 516 | + } |
|
| 517 | 517 | |
| 518 | 518 | /** |
| 519 | - * Run the database SQL query and return the PDOStatment object |
|
| 520 | - * @see Database::query |
|
| 521 | - * |
|
| 522 | - * @return object|void |
|
| 523 | - */ |
|
| 524 | - public function runSqlQuery($query, $all, $array){ |
|
| 525 | - //for database query execution time |
|
| 526 | - $benchmarkMarkerKey = $this->getCacheBenchmarkKeyForQuery($query, $all, $array); |
|
| 527 | - $benchmarkInstance = $this->getBenchmark(); |
|
| 528 | - if (! is_object($benchmarkInstance)){ |
|
| 529 | - $obj = & get_instance(); |
|
| 530 | - $benchmarkInstance = $obj->benchmark; |
|
| 531 | - $this->setBenchmark($benchmarkInstance); |
|
| 532 | - } |
|
| 519 | + * Run the database SQL query and return the PDOStatment object |
|
| 520 | + * @see Database::query |
|
| 521 | + * |
|
| 522 | + * @return object|void |
|
| 523 | + */ |
|
| 524 | + public function runSqlQuery($query, $all, $array){ |
|
| 525 | + //for database query execution time |
|
| 526 | + $benchmarkMarkerKey = $this->getCacheBenchmarkKeyForQuery($query, $all, $array); |
|
| 527 | + $benchmarkInstance = $this->getBenchmark(); |
|
| 528 | + if (! is_object($benchmarkInstance)){ |
|
| 529 | + $obj = & get_instance(); |
|
| 530 | + $benchmarkInstance = $obj->benchmark; |
|
| 531 | + $this->setBenchmark($benchmarkInstance); |
|
| 532 | + } |
|
| 533 | 533 | |
| 534 | - $benchmarkInstance->mark('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')'); |
|
| 535 | - //Now execute the query |
|
| 536 | - $sqlQuery = $this->pdo->query($query); |
|
| 534 | + $benchmarkInstance->mark('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')'); |
|
| 535 | + //Now execute the query |
|
| 536 | + $sqlQuery = $this->pdo->query($query); |
|
| 537 | 537 | |
| 538 | - //get response time for this query |
|
| 539 | - $responseTime = $benchmarkInstance->elapsedTime('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')'); |
|
| 540 | - //TODO use the configuration value for the high response time currently is 1 second |
|
| 541 | - if ($responseTime >= 1 ){ |
|
| 542 | - $this->logger->warning('High response time while processing database query [' .$query. ']. The response time is [' .$responseTime. '] sec.'); |
|
| 543 | - } |
|
| 544 | - //count the number of query execution to server |
|
| 545 | - $this->queryCount++; |
|
| 538 | + //get response time for this query |
|
| 539 | + $responseTime = $benchmarkInstance->elapsedTime('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')'); |
|
| 540 | + //TODO use the configuration value for the high response time currently is 1 second |
|
| 541 | + if ($responseTime >= 1 ){ |
|
| 542 | + $this->logger->warning('High response time while processing database query [' .$query. ']. The response time is [' .$responseTime. '] sec.'); |
|
| 543 | + } |
|
| 544 | + //count the number of query execution to server |
|
| 545 | + $this->queryCount++; |
|
| 546 | 546 | |
| 547 | - if ($sqlQuery !== false){ |
|
| 548 | - return $sqlQuery; |
|
| 549 | - } |
|
| 550 | - $this->setQueryError(); |
|
| 551 | - } |
|
| 547 | + if ($sqlQuery !== false){ |
|
| 548 | + return $sqlQuery; |
|
| 549 | + } |
|
| 550 | + $this->setQueryError(); |
|
| 551 | + } |
|
| 552 | 552 | |
| 553 | 553 | |
| 554 | 554 | /** |
| 555 | - * Return the database configuration |
|
| 556 | - * @return array |
|
| 557 | - */ |
|
| 555 | + * Return the database configuration |
|
| 556 | + * @return array |
|
| 557 | + */ |
|
| 558 | 558 | public function getDatabaseConfiguration(){ |
| 559 | 559 | return $this->config; |
| 560 | 560 | } |
@@ -563,39 +563,39 @@ discard block |
||
| 563 | 563 | * Setting the database configuration using the configuration file and additional configuration from param |
| 564 | 564 | * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
| 565 | 565 | * @param boolean $useConfigFile whether to use database configuration file |
| 566 | - * @return object Database |
|
| 566 | + * @return object Database |
|
| 567 | 567 | */ |
| 568 | - public function setDatabaseConfiguration(array $overwriteConfig = array(), $useConfigFile = true){ |
|
| 569 | - $db = array(); |
|
| 570 | - if ($useConfigFile && file_exists(CONFIG_PATH . 'database.php')){ |
|
| 571 | - //here don't use require_once because somewhere user can create database instance directly |
|
| 572 | - require CONFIG_PATH . 'database.php'; |
|
| 573 | - } |
|
| 568 | + public function setDatabaseConfiguration(array $overwriteConfig = array(), $useConfigFile = true){ |
|
| 569 | + $db = array(); |
|
| 570 | + if ($useConfigFile && file_exists(CONFIG_PATH . 'database.php')){ |
|
| 571 | + //here don't use require_once because somewhere user can create database instance directly |
|
| 572 | + require CONFIG_PATH . 'database.php'; |
|
| 573 | + } |
|
| 574 | 574 | |
| 575 | - //merge with the parameter |
|
| 576 | - $db = array_merge($db, $overwriteConfig); |
|
| 575 | + //merge with the parameter |
|
| 576 | + $db = array_merge($db, $overwriteConfig); |
|
| 577 | 577 | |
| 578 | - //default configuration |
|
| 579 | - $config = array( |
|
| 580 | - 'driver' => 'mysql', |
|
| 581 | - 'username' => 'root', |
|
| 582 | - 'password' => '', |
|
| 583 | - 'database' => '', |
|
| 584 | - 'hostname' => 'localhost', |
|
| 585 | - 'charset' => 'utf8', |
|
| 586 | - 'collation' => 'utf8_general_ci', |
|
| 587 | - 'prefix' => '', |
|
| 588 | - 'port' => '' |
|
| 589 | - ); |
|
| 578 | + //default configuration |
|
| 579 | + $config = array( |
|
| 580 | + 'driver' => 'mysql', |
|
| 581 | + 'username' => 'root', |
|
| 582 | + 'password' => '', |
|
| 583 | + 'database' => '', |
|
| 584 | + 'hostname' => 'localhost', |
|
| 585 | + 'charset' => 'utf8', |
|
| 586 | + 'collation' => 'utf8_general_ci', |
|
| 587 | + 'prefix' => '', |
|
| 588 | + 'port' => '' |
|
| 589 | + ); |
|
| 590 | 590 | |
| 591 | - $config = array_merge($config, $db); |
|
| 592 | - //determine the port using the hostname like localhost:3307 |
|
| 593 | - //hostname will be "localhost", and port "3307" |
|
| 594 | - $p = explode(':', $config['hostname']); |
|
| 595 | - if (count($p) >= 2){ |
|
| 596 | - $config['hostname'] = $p[0]; |
|
| 597 | - $config['port'] = $p[1]; |
|
| 598 | - } |
|
| 591 | + $config = array_merge($config, $db); |
|
| 592 | + //determine the port using the hostname like localhost:3307 |
|
| 593 | + //hostname will be "localhost", and port "3307" |
|
| 594 | + $p = explode(':', $config['hostname']); |
|
| 595 | + if (count($p) >= 2){ |
|
| 596 | + $config['hostname'] = $p[0]; |
|
| 597 | + $config['port'] = $p[1]; |
|
| 598 | + } |
|
| 599 | 599 | |
| 600 | 600 | $this->databaseName = $config['database']; |
| 601 | 601 | $this->config = $config; |
@@ -620,77 +620,77 @@ discard block |
||
| 620 | 620 | } |
| 621 | 621 | |
| 622 | 622 | /** |
| 623 | - * Set the result for SELECT query using PDOStatment |
|
| 624 | - * @see Database::query |
|
| 625 | - */ |
|
| 626 | - protected function setQueryResultForSelect($pdoStatment, $all, $array){ |
|
| 627 | - //if need return all result like list of record |
|
| 628 | - if (is_bool($all) && $all){ |
|
| 629 | - $this->result = ($array === false) ? $pdoStatment->fetchAll(PDO::FETCH_OBJ) : $pdoStatment->fetchAll(PDO::FETCH_ASSOC); |
|
| 630 | - } |
|
| 631 | - else{ |
|
| 632 | - $this->result = ($array === false) ? $pdoStatment->fetch(PDO::FETCH_OBJ) : $pdoStatment->fetch(PDO::FETCH_ASSOC); |
|
| 633 | - } |
|
| 634 | - //Sqlite and pgsql always return 0 when using rowCount() |
|
| 635 | - if (in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 636 | - $this->numRows = count($this->result); |
|
| 637 | - } |
|
| 638 | - else{ |
|
| 639 | - $this->numRows = $pdoStatment->rowCount(); |
|
| 640 | - } |
|
| 641 | - } |
|
| 642 | - |
|
| 643 | - /** |
|
| 644 | - * Set the result for other command than SELECT query using PDOStatment |
|
| 645 | - * @see Database::query |
|
| 646 | - */ |
|
| 647 | - protected function setQueryResultForNonSelect($pdoStatment){ |
|
| 648 | - //Sqlite and pgsql always return 0 when using rowCount() |
|
| 649 | - if (in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 650 | - $this->result = true; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 651 | - $this->numRows = 1; //TODO use the correct method to get the exact affected row |
|
| 652 | - } |
|
| 653 | - else{ |
|
| 654 | - $this->result = $pdoStatment->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 655 | - $this->numRows = $pdoStatment->rowCount(); |
|
| 656 | - } |
|
| 657 | - } |
|
| 623 | + * Set the result for SELECT query using PDOStatment |
|
| 624 | + * @see Database::query |
|
| 625 | + */ |
|
| 626 | + protected function setQueryResultForSelect($pdoStatment, $all, $array){ |
|
| 627 | + //if need return all result like list of record |
|
| 628 | + if (is_bool($all) && $all){ |
|
| 629 | + $this->result = ($array === false) ? $pdoStatment->fetchAll(PDO::FETCH_OBJ) : $pdoStatment->fetchAll(PDO::FETCH_ASSOC); |
|
| 630 | + } |
|
| 631 | + else{ |
|
| 632 | + $this->result = ($array === false) ? $pdoStatment->fetch(PDO::FETCH_OBJ) : $pdoStatment->fetch(PDO::FETCH_ASSOC); |
|
| 633 | + } |
|
| 634 | + //Sqlite and pgsql always return 0 when using rowCount() |
|
| 635 | + if (in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 636 | + $this->numRows = count($this->result); |
|
| 637 | + } |
|
| 638 | + else{ |
|
| 639 | + $this->numRows = $pdoStatment->rowCount(); |
|
| 640 | + } |
|
| 641 | + } |
|
| 642 | + |
|
| 643 | + /** |
|
| 644 | + * Set the result for other command than SELECT query using PDOStatment |
|
| 645 | + * @see Database::query |
|
| 646 | + */ |
|
| 647 | + protected function setQueryResultForNonSelect($pdoStatment){ |
|
| 648 | + //Sqlite and pgsql always return 0 when using rowCount() |
|
| 649 | + if (in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 650 | + $this->result = true; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 651 | + $this->numRows = 1; //TODO use the correct method to get the exact affected row |
|
| 652 | + } |
|
| 653 | + else{ |
|
| 654 | + $this->result = $pdoStatment->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 655 | + $this->numRows = $pdoStatment->rowCount(); |
|
| 656 | + } |
|
| 657 | + } |
|
| 658 | 658 | |
| 659 | 659 | |
| 660 | 660 | |
| 661 | - /** |
|
| 662 | - * This method is used to get the PDO DSN string using the configured driver |
|
| 663 | - * @return string the DSN string |
|
| 664 | - */ |
|
| 665 | - protected function getDsnFromDriver(){ |
|
| 666 | - $config = $this->getDatabaseConfiguration(); |
|
| 667 | - if (! empty($config)){ |
|
| 668 | - $driver = $config['driver']; |
|
| 669 | - $driverDsnMap = array( |
|
| 670 | - 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
|
| 671 | - . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 672 | - . 'dbname=' . $config['database'], |
|
| 673 | - 'pgsql' => 'pgsql:host=' . $config['hostname'] . ';' |
|
| 674 | - . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 675 | - . 'dbname=' . $config['database'], |
|
| 676 | - 'sqlite' => 'sqlite:' . $config['database'], |
|
| 677 | - 'oracle' => 'oci:dbname=' . $config['hostname'] |
|
| 678 | - . (($config['port']) != '' ? ':' . $config['port'] : '') |
|
| 679 | - . '/' . $config['database'] |
|
| 680 | - ); |
|
| 681 | - return isset($driverDsnMap[$driver]) ? $driverDsnMap[$driver] : ''; |
|
| 682 | - } |
|
| 683 | - return null; |
|
| 684 | - } |
|
| 685 | - |
|
| 686 | - /** |
|
| 687 | - * Transform the prepared query like (?, ?, ?) into string format |
|
| 688 | - * @see Database::query |
|
| 689 | - * |
|
| 690 | - * @return string |
|
| 691 | - */ |
|
| 692 | - protected function getPreparedQuery($query, $data){ |
|
| 693 | - if (is_array($data)){ |
|
| 661 | + /** |
|
| 662 | + * This method is used to get the PDO DSN string using the configured driver |
|
| 663 | + * @return string the DSN string |
|
| 664 | + */ |
|
| 665 | + protected function getDsnFromDriver(){ |
|
| 666 | + $config = $this->getDatabaseConfiguration(); |
|
| 667 | + if (! empty($config)){ |
|
| 668 | + $driver = $config['driver']; |
|
| 669 | + $driverDsnMap = array( |
|
| 670 | + 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
|
| 671 | + . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 672 | + . 'dbname=' . $config['database'], |
|
| 673 | + 'pgsql' => 'pgsql:host=' . $config['hostname'] . ';' |
|
| 674 | + . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 675 | + . 'dbname=' . $config['database'], |
|
| 676 | + 'sqlite' => 'sqlite:' . $config['database'], |
|
| 677 | + 'oracle' => 'oci:dbname=' . $config['hostname'] |
|
| 678 | + . (($config['port']) != '' ? ':' . $config['port'] : '') |
|
| 679 | + . '/' . $config['database'] |
|
| 680 | + ); |
|
| 681 | + return isset($driverDsnMap[$driver]) ? $driverDsnMap[$driver] : ''; |
|
| 682 | + } |
|
| 683 | + return null; |
|
| 684 | + } |
|
| 685 | + |
|
| 686 | + /** |
|
| 687 | + * Transform the prepared query like (?, ?, ?) into string format |
|
| 688 | + * @see Database::query |
|
| 689 | + * |
|
| 690 | + * @return string |
|
| 691 | + */ |
|
| 692 | + protected function getPreparedQuery($query, $data){ |
|
| 693 | + if (is_array($data)){ |
|
| 694 | 694 | $x = explode('?', $query); |
| 695 | 695 | $q = ''; |
| 696 | 696 | foreach($x as $k => $v){ |
@@ -699,121 +699,121 @@ discard block |
||
| 699 | 699 | } |
| 700 | 700 | } |
| 701 | 701 | return $q; |
| 702 | - } |
|
| 703 | - return $query; |
|
| 704 | - } |
|
| 705 | - |
|
| 706 | - /** |
|
| 707 | - * Get the cache content for this query |
|
| 708 | - * @see Database::query |
|
| 709 | - * |
|
| 710 | - * @return mixed |
|
| 711 | - */ |
|
| 712 | - protected function getCacheContentForQuery($query, $all, $array){ |
|
| 713 | - $cacheKey = $this->getCacheBenchmarkKeyForQuery($query, $all, $array); |
|
| 714 | - if (! is_object($this->getCacheInstance())){ |
|
| 715 | - //can not call method with reference in argument |
|
| 716 | - //like $this->setCacheInstance(& get_instance()->cache); |
|
| 717 | - //use temporary variable |
|
| 718 | - $instance = & get_instance()->cache; |
|
| 719 | - $this->setCacheInstance($instance); |
|
| 720 | - } |
|
| 721 | - return $this->getCacheInstance()->get($cacheKey); |
|
| 722 | - } |
|
| 723 | - |
|
| 724 | - /** |
|
| 725 | - * Save the result of query into cache |
|
| 726 | - * @param string $query the SQL query |
|
| 727 | - * @param string $key the cache key |
|
| 728 | - * @param mixed $result the query result to save |
|
| 729 | - * @param boolean $status whether can save the query result into cache |
|
| 730 | - * @param int $expire the cache TTL |
|
| 731 | - */ |
|
| 732 | - protected function setCacheContentForQuery($query, $key, $result, $status, $expire){ |
|
| 733 | - if ($status){ |
|
| 734 | - $this->logger->info('Save the result for query [' .$query. '] into cache for future use'); |
|
| 735 | - if (! is_object($this->getCacheInstance())){ |
|
| 736 | - //can not call method with reference in argument |
|
| 737 | - //like $this->setCacheInstance(& get_instance()->cache); |
|
| 738 | - //use temporary variable |
|
| 739 | - $instance = & get_instance()->cache; |
|
| 740 | - $this->setCacheInstance($instance); |
|
| 741 | - } |
|
| 742 | - $this->getCacheInstance()->set($key, $result, $expire); |
|
| 743 | - } |
|
| 744 | - } |
|
| 702 | + } |
|
| 703 | + return $query; |
|
| 704 | + } |
|
| 705 | + |
|
| 706 | + /** |
|
| 707 | + * Get the cache content for this query |
|
| 708 | + * @see Database::query |
|
| 709 | + * |
|
| 710 | + * @return mixed |
|
| 711 | + */ |
|
| 712 | + protected function getCacheContentForQuery($query, $all, $array){ |
|
| 713 | + $cacheKey = $this->getCacheBenchmarkKeyForQuery($query, $all, $array); |
|
| 714 | + if (! is_object($this->getCacheInstance())){ |
|
| 715 | + //can not call method with reference in argument |
|
| 716 | + //like $this->setCacheInstance(& get_instance()->cache); |
|
| 717 | + //use temporary variable |
|
| 718 | + $instance = & get_instance()->cache; |
|
| 719 | + $this->setCacheInstance($instance); |
|
| 720 | + } |
|
| 721 | + return $this->getCacheInstance()->get($cacheKey); |
|
| 722 | + } |
|
| 723 | + |
|
| 724 | + /** |
|
| 725 | + * Save the result of query into cache |
|
| 726 | + * @param string $query the SQL query |
|
| 727 | + * @param string $key the cache key |
|
| 728 | + * @param mixed $result the query result to save |
|
| 729 | + * @param boolean $status whether can save the query result into cache |
|
| 730 | + * @param int $expire the cache TTL |
|
| 731 | + */ |
|
| 732 | + protected function setCacheContentForQuery($query, $key, $result, $status, $expire){ |
|
| 733 | + if ($status){ |
|
| 734 | + $this->logger->info('Save the result for query [' .$query. '] into cache for future use'); |
|
| 735 | + if (! is_object($this->getCacheInstance())){ |
|
| 736 | + //can not call method with reference in argument |
|
| 737 | + //like $this->setCacheInstance(& get_instance()->cache); |
|
| 738 | + //use temporary variable |
|
| 739 | + $instance = & get_instance()->cache; |
|
| 740 | + $this->setCacheInstance($instance); |
|
| 741 | + } |
|
| 742 | + $this->getCacheInstance()->set($key, $result, $expire); |
|
| 743 | + } |
|
| 744 | + } |
|
| 745 | 745 | |
| 746 | 746 | |
| 747 | - /** |
|
| 748 | - * Set error for database query execution |
|
| 749 | - */ |
|
| 750 | - protected function setQueryError(){ |
|
| 751 | - $error = $this->pdo->errorInfo(); |
|
| 752 | - $this->error = isset($error[2]) ? $error[2] : ''; |
|
| 753 | - $this->logger->error('The database query execution got error: ' . stringfy_vars($error)); |
|
| 754 | - //show error message |
|
| 755 | - $this->error(); |
|
| 756 | - } |
|
| 747 | + /** |
|
| 748 | + * Set error for database query execution |
|
| 749 | + */ |
|
| 750 | + protected function setQueryError(){ |
|
| 751 | + $error = $this->pdo->errorInfo(); |
|
| 752 | + $this->error = isset($error[2]) ? $error[2] : ''; |
|
| 753 | + $this->logger->error('The database query execution got error: ' . stringfy_vars($error)); |
|
| 754 | + //show error message |
|
| 755 | + $this->error(); |
|
| 756 | + } |
|
| 757 | 757 | |
| 758 | 758 | /** |
| 759 | - * Return the cache key for the given query |
|
| 760 | - * @see Database::query |
|
| 761 | - * |
|
| 762 | - * @return string |
|
| 763 | - */ |
|
| 764 | - protected function getCacheBenchmarkKeyForQuery($query, $all, $array){ |
|
| 765 | - if (is_array($all)){ |
|
| 766 | - $all = 'array'; |
|
| 767 | - } |
|
| 768 | - return md5($query . $all . $array); |
|
| 769 | - } |
|
| 759 | + * Return the cache key for the given query |
|
| 760 | + * @see Database::query |
|
| 761 | + * |
|
| 762 | + * @return string |
|
| 763 | + */ |
|
| 764 | + protected function getCacheBenchmarkKeyForQuery($query, $all, $array){ |
|
| 765 | + if (is_array($all)){ |
|
| 766 | + $all = 'array'; |
|
| 767 | + } |
|
| 768 | + return md5($query . $all . $array); |
|
| 769 | + } |
|
| 770 | 770 | |
| 771 | 771 | /** |
| 772 | - * Set the Log instance using argument or create new instance |
|
| 773 | - * @param object $logger the Log instance if not null |
|
| 774 | - */ |
|
| 775 | - protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
| 776 | - if ($logger !== null){ |
|
| 777 | - $this->setLogger($logger); |
|
| 778 | - } |
|
| 779 | - else{ |
|
| 780 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 781 | - $this->logger->setLogger('Library::Database'); |
|
| 782 | - } |
|
| 783 | - } |
|
| 772 | + * Set the Log instance using argument or create new instance |
|
| 773 | + * @param object $logger the Log instance if not null |
|
| 774 | + */ |
|
| 775 | + protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
| 776 | + if ($logger !== null){ |
|
| 777 | + $this->setLogger($logger); |
|
| 778 | + } |
|
| 779 | + else{ |
|
| 780 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 781 | + $this->logger->setLogger('Library::Database'); |
|
| 782 | + } |
|
| 783 | + } |
|
| 784 | 784 | |
| 785 | 785 | /** |
| 786 | - * Set the DatabaseQueryBuilder instance using argument or create new instance |
|
| 787 | - * @param object $queryBuilder the DatabaseQueryBuilder instance if not null |
|
| 788 | - */ |
|
| 786 | + * Set the DatabaseQueryBuilder instance using argument or create new instance |
|
| 787 | + * @param object $queryBuilder the DatabaseQueryBuilder instance if not null |
|
| 788 | + */ |
|
| 789 | 789 | protected function setQueryBuilderFromParamOrCreateNewInstance(DatabaseQueryBuilder $queryBuilder = null){ |
| 790 | 790 | if ($queryBuilder !== null){ |
| 791 | - $this->setQueryBuilder($queryBuilder); |
|
| 791 | + $this->setQueryBuilder($queryBuilder); |
|
| 792 | 792 | } |
| 793 | 793 | else{ |
| 794 | 794 | $this->queryBuilder =& class_loader('DatabaseQueryBuilder', 'classes'); |
| 795 | 795 | } |
| 796 | 796 | } |
| 797 | 797 | |
| 798 | - /** |
|
| 799 | - * Reset the database class attributs to the initail values before each query. |
|
| 800 | - */ |
|
| 801 | - private function reset(){ |
|
| 798 | + /** |
|
| 799 | + * Reset the database class attributs to the initail values before each query. |
|
| 800 | + */ |
|
| 801 | + private function reset(){ |
|
| 802 | 802 | //query builder reset |
| 803 | - $this->getQueryBuilder()->reset(); |
|
| 804 | - $this->numRows = 0; |
|
| 805 | - $this->insertId = null; |
|
| 806 | - $this->query = null; |
|
| 807 | - $this->error = null; |
|
| 808 | - $this->result = array(); |
|
| 809 | - $this->data = array(); |
|
| 810 | - } |
|
| 811 | - |
|
| 812 | - /** |
|
| 813 | - * The class destructor |
|
| 814 | - */ |
|
| 815 | - public function __destruct(){ |
|
| 816 | - $this->pdo = null; |
|
| 817 | - } |
|
| 803 | + $this->getQueryBuilder()->reset(); |
|
| 804 | + $this->numRows = 0; |
|
| 805 | + $this->insertId = null; |
|
| 806 | + $this->query = null; |
|
| 807 | + $this->error = null; |
|
| 808 | + $this->result = array(); |
|
| 809 | + $this->data = array(); |
|
| 810 | + } |
|
| 811 | + |
|
| 812 | + /** |
|
| 813 | + * The class destructor |
|
| 814 | + */ |
|
| 815 | + public function __destruct(){ |
|
| 816 | + $this->pdo = null; |
|
| 817 | + } |
|
| 818 | 818 | |
| 819 | 819 | } |
@@ -23,110 +23,110 @@ discard block |
||
| 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 | - class Database{ |
|
| 26 | + class Database { |
|
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * The PDO instance |
| 30 | 30 | * @var object |
| 31 | 31 | */ |
| 32 | - private $pdo = null; |
|
| 32 | + private $pdo = null; |
|
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | 35 | * The database name used for the application |
| 36 | 36 | * @var string |
| 37 | 37 | */ |
| 38 | - private $databaseName = null; |
|
| 38 | + private $databaseName = null; |
|
| 39 | 39 | |
| 40 | 40 | /** |
| 41 | 41 | * The number of rows returned by the last query |
| 42 | 42 | * @var int |
| 43 | 43 | */ |
| 44 | - private $numRows = 0; |
|
| 44 | + private $numRows = 0; |
|
| 45 | 45 | |
| 46 | 46 | /** |
| 47 | 47 | * The last insert id for the primary key column that have auto increment or sequence |
| 48 | 48 | * @var mixed |
| 49 | 49 | */ |
| 50 | - private $insertId = null; |
|
| 50 | + private $insertId = null; |
|
| 51 | 51 | |
| 52 | 52 | /** |
| 53 | 53 | * The full SQL query statment after build for each command |
| 54 | 54 | * @var string |
| 55 | 55 | */ |
| 56 | - private $query = null; |
|
| 56 | + private $query = null; |
|
| 57 | 57 | |
| 58 | 58 | /** |
| 59 | 59 | * The error returned for the last query |
| 60 | 60 | * @var string |
| 61 | 61 | */ |
| 62 | - private $error = null; |
|
| 62 | + private $error = null; |
|
| 63 | 63 | |
| 64 | 64 | /** |
| 65 | 65 | * The result returned for the last query |
| 66 | 66 | * @var mixed |
| 67 | 67 | */ |
| 68 | - private $result = array(); |
|
| 68 | + private $result = array(); |
|
| 69 | 69 | |
| 70 | 70 | /** |
| 71 | 71 | * The cache default time to live in second. 0 means no need to use the cache feature |
| 72 | 72 | * @var int |
| 73 | 73 | */ |
| 74 | - private $cacheTtl = 0; |
|
| 74 | + private $cacheTtl = 0; |
|
| 75 | 75 | |
| 76 | 76 | /** |
| 77 | 77 | * The cache current time to live. 0 means no need to use the cache feature |
| 78 | 78 | * @var int |
| 79 | 79 | */ |
| 80 | - private $temporaryCacheTtl = 0; |
|
| 80 | + private $temporaryCacheTtl = 0; |
|
| 81 | 81 | |
| 82 | 82 | /** |
| 83 | 83 | * The number of executed query for the current request |
| 84 | 84 | * @var int |
| 85 | 85 | */ |
| 86 | - private $queryCount = 0; |
|
| 86 | + private $queryCount = 0; |
|
| 87 | 87 | |
| 88 | 88 | /** |
| 89 | 89 | * The default data to be used in the statments query INSERT, UPDATE |
| 90 | 90 | * @var array |
| 91 | 91 | */ |
| 92 | - private $data = array(); |
|
| 92 | + private $data = array(); |
|
| 93 | 93 | |
| 94 | 94 | /** |
| 95 | 95 | * The database configuration |
| 96 | 96 | * @var array |
| 97 | 97 | */ |
| 98 | - private $config = array(); |
|
| 98 | + private $config = array(); |
|
| 99 | 99 | |
| 100 | 100 | /** |
| 101 | 101 | * The logger instance |
| 102 | 102 | * @var object |
| 103 | 103 | */ |
| 104 | - private $logger = null; |
|
| 104 | + private $logger = null; |
|
| 105 | 105 | |
| 106 | 106 | /** |
| 107 | 107 | * The cache instance |
| 108 | 108 | * @var object |
| 109 | 109 | */ |
| 110 | - private $cacheInstance = null; |
|
| 110 | + private $cacheInstance = null; |
|
| 111 | 111 | |
| 112 | 112 | /** |
| 113 | 113 | * The benchmark instance |
| 114 | 114 | * @var object |
| 115 | 115 | */ |
| 116 | - private $benchmarkInstance = null; |
|
| 116 | + private $benchmarkInstance = null; |
|
| 117 | 117 | |
| 118 | 118 | /** |
| 119 | 119 | * The DatabaseQueryBuilder instance |
| 120 | 120 | * @var object |
| 121 | 121 | */ |
| 122 | - private $queryBuilder = null; |
|
| 122 | + private $queryBuilder = null; |
|
| 123 | 123 | |
| 124 | 124 | |
| 125 | 125 | /** |
| 126 | 126 | * Construct new database |
| 127 | 127 | * @param array $overwriteConfig the config to overwrite with the config set in database.php |
| 128 | 128 | */ |
| 129 | - public function __construct($overwriteConfig = array()){ |
|
| 129 | + public function __construct($overwriteConfig = array()) { |
|
| 130 | 130 | //Set Log instance to use |
| 131 | 131 | $this->setLoggerFromParamOrCreateNewInstance(null); |
| 132 | 132 | |
@@ -144,23 +144,23 @@ discard block |
||
| 144 | 144 | * This is used to connect to database |
| 145 | 145 | * @return bool |
| 146 | 146 | */ |
| 147 | - public function connect(){ |
|
| 147 | + public function connect() { |
|
| 148 | 148 | $config = $this->getDatabaseConfiguration(); |
| 149 | - if (! empty($config)){ |
|
| 150 | - try{ |
|
| 149 | + if (!empty($config)) { |
|
| 150 | + try { |
|
| 151 | 151 | $this->pdo = new PDO($this->getDsnFromDriver(), $config['username'], $config['password']); |
| 152 | 152 | $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
| 153 | 153 | $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
| 154 | 154 | $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
| 155 | 155 | return true; |
| 156 | 156 | } |
| 157 | - catch (PDOException $e){ |
|
| 157 | + catch (PDOException $e) { |
|
| 158 | 158 | $this->logger->fatal($e->getMessage()); |
| 159 | 159 | show_error('Cannot connect to Database.'); |
| 160 | 160 | return false; |
| 161 | 161 | } |
| 162 | 162 | } |
| 163 | - else{ |
|
| 163 | + else { |
|
| 164 | 164 | show_error('Database configuration is not set.'); |
| 165 | 165 | return false; |
| 166 | 166 | } |
@@ -171,7 +171,7 @@ discard block |
||
| 171 | 171 | * Return the number of rows returned by the current query |
| 172 | 172 | * @return int |
| 173 | 173 | */ |
| 174 | - public function numRows(){ |
|
| 174 | + public function numRows() { |
|
| 175 | 175 | return $this->numRows; |
| 176 | 176 | } |
| 177 | 177 | |
@@ -179,15 +179,15 @@ discard block |
||
| 179 | 179 | * Return the last insert id value |
| 180 | 180 | * @return mixed |
| 181 | 181 | */ |
| 182 | - public function insertId(){ |
|
| 182 | + public function insertId() { |
|
| 183 | 183 | return $this->insertId; |
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | /** |
| 187 | 187 | * Show an error got from the current query (SQL command synthax error, database driver returned error, etc.) |
| 188 | 188 | */ |
| 189 | - public function error(){ |
|
| 190 | - if ($this->error){ |
|
| 189 | + public function error() { |
|
| 190 | + if ($this->error) { |
|
| 191 | 191 | show_error('Query: "' . $this->query . '" Error: ' . $this->error, 'Database Error'); |
| 192 | 192 | } |
| 193 | 193 | } |
@@ -198,13 +198,13 @@ discard block |
||
| 198 | 198 | * If is string will determine the result type "array" or "object" |
| 199 | 199 | * @return mixed the query SQL string or the record result |
| 200 | 200 | */ |
| 201 | - public function get($returnSQLQueryOrResultType = false){ |
|
| 201 | + public function get($returnSQLQueryOrResultType = false) { |
|
| 202 | 202 | $this->getQueryBuilder()->limit(1); |
| 203 | 203 | $query = $this->getAll(true); |
| 204 | - if ($returnSQLQueryOrResultType === true){ |
|
| 204 | + if ($returnSQLQueryOrResultType === true) { |
|
| 205 | 205 | return $query; |
| 206 | 206 | } |
| 207 | - else{ |
|
| 207 | + else { |
|
| 208 | 208 | return $this->query($query, false, $returnSQLQueryOrResultType == 'array'); |
| 209 | 209 | } |
| 210 | 210 | } |
@@ -215,9 +215,9 @@ discard block |
||
| 215 | 215 | * If is string will determine the result type "array" or "object" |
| 216 | 216 | * @return mixed the query SQL string or the record result |
| 217 | 217 | */ |
| 218 | - public function getAll($returnSQLQueryOrResultType = false){ |
|
| 218 | + public function getAll($returnSQLQueryOrResultType = false) { |
|
| 219 | 219 | $query = $this->getQueryBuilder()->getQuery(); |
| 220 | - if ($returnSQLQueryOrResultType === true){ |
|
| 220 | + if ($returnSQLQueryOrResultType === true) { |
|
| 221 | 221 | return $query; |
| 222 | 222 | } |
| 223 | 223 | return $this->query($query, true, $returnSQLQueryOrResultType == 'array'); |
@@ -229,18 +229,18 @@ discard block |
||
| 229 | 229 | * @param boolean $escape whether to escape or not the values |
| 230 | 230 | * @return mixed the insert id of the new record or null |
| 231 | 231 | */ |
| 232 | - public function insert($data = array(), $escape = true){ |
|
| 233 | - if (empty($data) && $this->getData()){ |
|
| 232 | + public function insert($data = array(), $escape = true) { |
|
| 233 | + if (empty($data) && $this->getData()) { |
|
| 234 | 234 | //as when using $this->setData() may be the data already escaped |
| 235 | 235 | $escape = false; |
| 236 | 236 | $data = $this->getData(); |
| 237 | 237 | } |
| 238 | 238 | $query = $this->getQueryBuilder()->insert($data, $escape)->getQuery(); |
| 239 | 239 | $result = $this->query($query); |
| 240 | - if ($result){ |
|
| 240 | + if ($result) { |
|
| 241 | 241 | $this->insertId = $this->pdo->lastInsertId(); |
| 242 | 242 | //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
| 243 | - return ! $this->insertId() ? true : $this->insertId(); |
|
| 243 | + return !$this->insertId() ? true : $this->insertId(); |
|
| 244 | 244 | } |
| 245 | 245 | return false; |
| 246 | 246 | } |
@@ -251,8 +251,8 @@ discard block |
||
| 251 | 251 | * @param boolean $escape whether to escape or not the values |
| 252 | 252 | * @return mixed the update status |
| 253 | 253 | */ |
| 254 | - public function update($data = array(), $escape = true){ |
|
| 255 | - if (empty($data) && $this->getData()){ |
|
| 254 | + public function update($data = array(), $escape = true) { |
|
| 255 | + if (empty($data) && $this->getData()) { |
|
| 256 | 256 | //as when using $this->setData() may be the data already escaped |
| 257 | 257 | $escape = false; |
| 258 | 258 | $data = $this->getData(); |
@@ -265,7 +265,7 @@ discard block |
||
| 265 | 265 | * Delete the record in database |
| 266 | 266 | * @return mixed the delete status |
| 267 | 267 | */ |
| 268 | - public function delete(){ |
|
| 268 | + public function delete() { |
|
| 269 | 269 | $query = $this->getQueryBuilder()->delete()->getQuery(); |
| 270 | 270 | return $this->query($query); |
| 271 | 271 | } |
@@ -275,8 +275,8 @@ discard block |
||
| 275 | 275 | * @param integer $ttl the cache time to live in second |
| 276 | 276 | * @return object the current Database instance |
| 277 | 277 | */ |
| 278 | - public function setCache($ttl = 0){ |
|
| 279 | - if ($ttl > 0){ |
|
| 278 | + public function setCache($ttl = 0) { |
|
| 279 | + if ($ttl > 0) { |
|
| 280 | 280 | $this->cacheTtl = $ttl; |
| 281 | 281 | $this->temporaryCacheTtl = $ttl; |
| 282 | 282 | } |
@@ -288,8 +288,8 @@ discard block |
||
| 288 | 288 | * @param integer $ttl the cache time to live in second |
| 289 | 289 | * @return object the current Database instance |
| 290 | 290 | */ |
| 291 | - public function cached($ttl = 0){ |
|
| 292 | - if ($ttl > 0){ |
|
| 291 | + public function cached($ttl = 0) { |
|
| 292 | + if ($ttl > 0) { |
|
| 293 | 293 | $this->temporaryCacheTtl = $ttl; |
| 294 | 294 | } |
| 295 | 295 | return $this; |
@@ -301,7 +301,7 @@ discard block |
||
| 301 | 301 | * @param boolean $escaped whether we can do escape of not |
| 302 | 302 | * @return mixed the data after escaped or the same data if not |
| 303 | 303 | */ |
| 304 | - public function escape($data, $escaped = true){ |
|
| 304 | + public function escape($data, $escaped = true) { |
|
| 305 | 305 | return $escaped ? |
| 306 | 306 | $this->getPdo()->quote(trim($data)) |
| 307 | 307 | : $data; |
@@ -311,7 +311,7 @@ discard block |
||
| 311 | 311 | * Return the number query executed count for the current request |
| 312 | 312 | * @return int |
| 313 | 313 | */ |
| 314 | - public function queryCount(){ |
|
| 314 | + public function queryCount() { |
|
| 315 | 315 | return $this->queryCount; |
| 316 | 316 | } |
| 317 | 317 | |
@@ -319,7 +319,7 @@ discard block |
||
| 319 | 319 | * Return the current query SQL string |
| 320 | 320 | * @return string |
| 321 | 321 | */ |
| 322 | - public function getQuery(){ |
|
| 322 | + public function getQuery() { |
|
| 323 | 323 | return $this->query; |
| 324 | 324 | } |
| 325 | 325 | |
@@ -327,7 +327,7 @@ discard block |
||
| 327 | 327 | * Return the application database name |
| 328 | 328 | * @return string |
| 329 | 329 | */ |
| 330 | - public function getDatabaseName(){ |
|
| 330 | + public function getDatabaseName() { |
|
| 331 | 331 | return $this->databaseName; |
| 332 | 332 | } |
| 333 | 333 | |
@@ -335,7 +335,7 @@ discard block |
||
| 335 | 335 | * Return the PDO instance |
| 336 | 336 | * @return object |
| 337 | 337 | */ |
| 338 | - public function getPdo(){ |
|
| 338 | + public function getPdo() { |
|
| 339 | 339 | return $this->pdo; |
| 340 | 340 | } |
| 341 | 341 | |
@@ -344,7 +344,7 @@ discard block |
||
| 344 | 344 | * @param object $pdo the pdo object |
| 345 | 345 | * @return object Database |
| 346 | 346 | */ |
| 347 | - public function setPdo(PDO $pdo){ |
|
| 347 | + public function setPdo(PDO $pdo) { |
|
| 348 | 348 | $this->pdo = $pdo; |
| 349 | 349 | return $this; |
| 350 | 350 | } |
@@ -354,7 +354,7 @@ discard block |
||
| 354 | 354 | * Return the Log instance |
| 355 | 355 | * @return Log |
| 356 | 356 | */ |
| 357 | - public function getLogger(){ |
|
| 357 | + public function getLogger() { |
|
| 358 | 358 | return $this->logger; |
| 359 | 359 | } |
| 360 | 360 | |
@@ -363,7 +363,7 @@ discard block |
||
| 363 | 363 | * @param Log $logger the log object |
| 364 | 364 | * @return object Database |
| 365 | 365 | */ |
| 366 | - public function setLogger($logger){ |
|
| 366 | + public function setLogger($logger) { |
|
| 367 | 367 | $this->logger = $logger; |
| 368 | 368 | return $this; |
| 369 | 369 | } |
@@ -372,7 +372,7 @@ discard block |
||
| 372 | 372 | * Return the cache instance |
| 373 | 373 | * @return CacheInterface |
| 374 | 374 | */ |
| 375 | - public function getCacheInstance(){ |
|
| 375 | + public function getCacheInstance() { |
|
| 376 | 376 | return $this->cacheInstance; |
| 377 | 377 | } |
| 378 | 378 | |
@@ -381,7 +381,7 @@ discard block |
||
| 381 | 381 | * @param CacheInterface $cache the cache object |
| 382 | 382 | * @return object Database |
| 383 | 383 | */ |
| 384 | - public function setCacheInstance($cache){ |
|
| 384 | + public function setCacheInstance($cache) { |
|
| 385 | 385 | $this->cacheInstance = $cache; |
| 386 | 386 | return $this; |
| 387 | 387 | } |
@@ -390,7 +390,7 @@ discard block |
||
| 390 | 390 | * Return the benchmark instance |
| 391 | 391 | * @return Benchmark |
| 392 | 392 | */ |
| 393 | - public function getBenchmark(){ |
|
| 393 | + public function getBenchmark() { |
|
| 394 | 394 | return $this->benchmarkInstance; |
| 395 | 395 | } |
| 396 | 396 | |
@@ -399,7 +399,7 @@ discard block |
||
| 399 | 399 | * @param Benchmark $benchmark the benchmark object |
| 400 | 400 | * @return object Database |
| 401 | 401 | */ |
| 402 | - public function setBenchmark($benchmark){ |
|
| 402 | + public function setBenchmark($benchmark) { |
|
| 403 | 403 | $this->benchmarkInstance = $benchmark; |
| 404 | 404 | return $this; |
| 405 | 405 | } |
@@ -409,7 +409,7 @@ discard block |
||
| 409 | 409 | * Return the DatabaseQueryBuilder instance |
| 410 | 410 | * @return object DatabaseQueryBuilder |
| 411 | 411 | */ |
| 412 | - public function getQueryBuilder(){ |
|
| 412 | + public function getQueryBuilder() { |
|
| 413 | 413 | return $this->queryBuilder; |
| 414 | 414 | } |
| 415 | 415 | |
@@ -417,7 +417,7 @@ discard block |
||
| 417 | 417 | * Set the DatabaseQueryBuilder instance |
| 418 | 418 | * @param object DatabaseQueryBuilder $queryBuilder the DatabaseQueryBuilder object |
| 419 | 419 | */ |
| 420 | - public function setQueryBuilder(DatabaseQueryBuilder $queryBuilder){ |
|
| 420 | + public function setQueryBuilder(DatabaseQueryBuilder $queryBuilder) { |
|
| 421 | 421 | $this->queryBuilder = $queryBuilder; |
| 422 | 422 | return $this; |
| 423 | 423 | } |
@@ -426,7 +426,7 @@ discard block |
||
| 426 | 426 | * Return the data to be used for insert, update, etc. |
| 427 | 427 | * @return array |
| 428 | 428 | */ |
| 429 | - public function getData(){ |
|
| 429 | + public function getData() { |
|
| 430 | 430 | return $this->data; |
| 431 | 431 | } |
| 432 | 432 | |
@@ -437,9 +437,9 @@ discard block |
||
| 437 | 437 | * @param boolean $escape whether to escape or not the $value |
| 438 | 438 | * @return object the current Database instance |
| 439 | 439 | */ |
| 440 | - public function setData($key, $value = null, $escape = true){ |
|
| 441 | - if(is_array($key)){ |
|
| 442 | - foreach($key as $k => $v){ |
|
| 440 | + public function setData($key, $value = null, $escape = true) { |
|
| 441 | + if (is_array($key)) { |
|
| 442 | + foreach ($key as $k => $v) { |
|
| 443 | 443 | $this->setData($k, $v, $escape); |
| 444 | 444 | } |
| 445 | 445 | } else { |
@@ -456,7 +456,7 @@ discard block |
||
| 456 | 456 | * @param boolean $array return the result as array |
| 457 | 457 | * @return mixed the query result |
| 458 | 458 | */ |
| 459 | - public function query($query, $all = true, $array = false){ |
|
| 459 | + public function query($query, $all = true, $array = false) { |
|
| 460 | 460 | $this->reset(); |
| 461 | 461 | $query = $this->getPreparedQuery($query, $all); |
| 462 | 462 | $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
@@ -464,9 +464,9 @@ discard block |
||
| 464 | 464 | $isSqlSELECTQuery = stristr($this->query, 'SELECT') !== false; |
| 465 | 465 | |
| 466 | 466 | $this->logger->info( |
| 467 | - 'Execute SQL query ['.$this->query.'], return type: ' |
|
| 468 | - . ($array?'ARRAY':'OBJECT') .', return as list: ' |
|
| 469 | - . (is_bool($all) && $all ? 'YES':'NO') |
|
| 467 | + 'Execute SQL query [' . $this->query . '], return type: ' |
|
| 468 | + . ($array ? 'ARRAY' : 'OBJECT') . ', return as list: ' |
|
| 469 | + . (is_bool($all) && $all ? 'YES' : 'NO') |
|
| 470 | 470 | ); |
| 471 | 471 | //cache expire time |
| 472 | 472 | $cacheExpire = $this->temporaryCacheTtl; |
@@ -483,15 +483,15 @@ discard block |
||
| 483 | 483 | //if can use cache feature for this query |
| 484 | 484 | $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
| 485 | 485 | |
| 486 | - if ($dbCacheStatus && $isSqlSELECTQuery){ |
|
| 486 | + if ($dbCacheStatus && $isSqlSELECTQuery) { |
|
| 487 | 487 | $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
| 488 | 488 | $cacheContent = $this->getCacheContentForQuery($query, $all, $array); |
| 489 | 489 | } |
| 490 | 490 | |
| 491 | - if ( !$cacheContent){ |
|
| 491 | + if (!$cacheContent) { |
|
| 492 | 492 | $sqlQuery = $this->runSqlQuery($query, $all, $array); |
| 493 | - if (is_object($sqlQuery)){ |
|
| 494 | - if ($isSqlSELECTQuery){ |
|
| 493 | + if (is_object($sqlQuery)) { |
|
| 494 | + if ($isSqlSELECTQuery) { |
|
| 495 | 495 | $this->setQueryResultForSelect($sqlQuery, $all, $array); |
| 496 | 496 | $this->setCacheContentForQuery( |
| 497 | 497 | $this->query, |
@@ -500,15 +500,15 @@ discard block |
||
| 500 | 500 | $dbCacheStatus && $isSqlSELECTQuery, |
| 501 | 501 | $cacheExpire |
| 502 | 502 | ); |
| 503 | - if (! $this->result){ |
|
| 503 | + if (!$this->result) { |
|
| 504 | 504 | $this->logger->info('No result where found for the query [' . $query . ']'); |
| 505 | 505 | } |
| 506 | 506 | } else { |
| 507 | 507 | $this->setQueryResultForNonSelect($sqlQuery); |
| 508 | 508 | } |
| 509 | 509 | } |
| 510 | - } else if ($isSqlSELECTQuery){ |
|
| 511 | - $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 510 | + } else if ($isSqlSELECTQuery) { |
|
| 511 | + $this->logger->info('The result for query [' . $this->query . '] already cached use it'); |
|
| 512 | 512 | $this->result = $cacheContent; |
| 513 | 513 | $this->numRows = count($this->result); |
| 514 | 514 | } |
@@ -521,11 +521,11 @@ discard block |
||
| 521 | 521 | * |
| 522 | 522 | * @return object|void |
| 523 | 523 | */ |
| 524 | - public function runSqlQuery($query, $all, $array){ |
|
| 524 | + public function runSqlQuery($query, $all, $array) { |
|
| 525 | 525 | //for database query execution time |
| 526 | 526 | $benchmarkMarkerKey = $this->getCacheBenchmarkKeyForQuery($query, $all, $array); |
| 527 | 527 | $benchmarkInstance = $this->getBenchmark(); |
| 528 | - if (! is_object($benchmarkInstance)){ |
|
| 528 | + if (!is_object($benchmarkInstance)) { |
|
| 529 | 529 | $obj = & get_instance(); |
| 530 | 530 | $benchmarkInstance = $obj->benchmark; |
| 531 | 531 | $this->setBenchmark($benchmarkInstance); |
@@ -538,13 +538,13 @@ discard block |
||
| 538 | 538 | //get response time for this query |
| 539 | 539 | $responseTime = $benchmarkInstance->elapsedTime('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')'); |
| 540 | 540 | //TODO use the configuration value for the high response time currently is 1 second |
| 541 | - if ($responseTime >= 1 ){ |
|
| 542 | - $this->logger->warning('High response time while processing database query [' .$query. ']. The response time is [' .$responseTime. '] sec.'); |
|
| 541 | + if ($responseTime >= 1) { |
|
| 542 | + $this->logger->warning('High response time while processing database query [' . $query . ']. The response time is [' . $responseTime . '] sec.'); |
|
| 543 | 543 | } |
| 544 | 544 | //count the number of query execution to server |
| 545 | 545 | $this->queryCount++; |
| 546 | 546 | |
| 547 | - if ($sqlQuery !== false){ |
|
| 547 | + if ($sqlQuery !== false) { |
|
| 548 | 548 | return $sqlQuery; |
| 549 | 549 | } |
| 550 | 550 | $this->setQueryError(); |
@@ -555,7 +555,7 @@ discard block |
||
| 555 | 555 | * Return the database configuration |
| 556 | 556 | * @return array |
| 557 | 557 | */ |
| 558 | - public function getDatabaseConfiguration(){ |
|
| 558 | + public function getDatabaseConfiguration() { |
|
| 559 | 559 | return $this->config; |
| 560 | 560 | } |
| 561 | 561 | |
@@ -565,9 +565,9 @@ discard block |
||
| 565 | 565 | * @param boolean $useConfigFile whether to use database configuration file |
| 566 | 566 | * @return object Database |
| 567 | 567 | */ |
| 568 | - public function setDatabaseConfiguration(array $overwriteConfig = array(), $useConfigFile = true){ |
|
| 568 | + public function setDatabaseConfiguration(array $overwriteConfig = array(), $useConfigFile = true) { |
|
| 569 | 569 | $db = array(); |
| 570 | - if ($useConfigFile && file_exists(CONFIG_PATH . 'database.php')){ |
|
| 570 | + if ($useConfigFile && file_exists(CONFIG_PATH . 'database.php')) { |
|
| 571 | 571 | //here don't use require_once because somewhere user can create database instance directly |
| 572 | 572 | require CONFIG_PATH . 'database.php'; |
| 573 | 573 | } |
@@ -592,7 +592,7 @@ discard block |
||
| 592 | 592 | //determine the port using the hostname like localhost:3307 |
| 593 | 593 | //hostname will be "localhost", and port "3307" |
| 594 | 594 | $p = explode(':', $config['hostname']); |
| 595 | - if (count($p) >= 2){ |
|
| 595 | + if (count($p) >= 2) { |
|
| 596 | 596 | $config['hostname'] = $p[0]; |
| 597 | 597 | $config['port'] = $p[1]; |
| 598 | 598 | } |
@@ -611,7 +611,7 @@ discard block |
||
| 611 | 611 | $this->connect(); |
| 612 | 612 | |
| 613 | 613 | //update queryBuilder with some properties needed |
| 614 | - if(is_object($this->getQueryBuilder())){ |
|
| 614 | + if (is_object($this->getQueryBuilder())) { |
|
| 615 | 615 | $this->getQueryBuilder()->setDriver($config['driver']); |
| 616 | 616 | $this->getQueryBuilder()->setPrefix($config['prefix']); |
| 617 | 617 | $this->getQueryBuilder()->setPdo($this->getPdo()); |
@@ -623,19 +623,19 @@ discard block |
||
| 623 | 623 | * Set the result for SELECT query using PDOStatment |
| 624 | 624 | * @see Database::query |
| 625 | 625 | */ |
| 626 | - protected function setQueryResultForSelect($pdoStatment, $all, $array){ |
|
| 626 | + protected function setQueryResultForSelect($pdoStatment, $all, $array) { |
|
| 627 | 627 | //if need return all result like list of record |
| 628 | - if (is_bool($all) && $all){ |
|
| 628 | + if (is_bool($all) && $all) { |
|
| 629 | 629 | $this->result = ($array === false) ? $pdoStatment->fetchAll(PDO::FETCH_OBJ) : $pdoStatment->fetchAll(PDO::FETCH_ASSOC); |
| 630 | 630 | } |
| 631 | - else{ |
|
| 631 | + else { |
|
| 632 | 632 | $this->result = ($array === false) ? $pdoStatment->fetch(PDO::FETCH_OBJ) : $pdoStatment->fetch(PDO::FETCH_ASSOC); |
| 633 | 633 | } |
| 634 | 634 | //Sqlite and pgsql always return 0 when using rowCount() |
| 635 | - if (in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 635 | + if (in_array($this->config['driver'], array('sqlite', 'pgsql'))) { |
|
| 636 | 636 | $this->numRows = count($this->result); |
| 637 | 637 | } |
| 638 | - else{ |
|
| 638 | + else { |
|
| 639 | 639 | $this->numRows = $pdoStatment->rowCount(); |
| 640 | 640 | } |
| 641 | 641 | } |
@@ -644,13 +644,13 @@ discard block |
||
| 644 | 644 | * Set the result for other command than SELECT query using PDOStatment |
| 645 | 645 | * @see Database::query |
| 646 | 646 | */ |
| 647 | - protected function setQueryResultForNonSelect($pdoStatment){ |
|
| 647 | + protected function setQueryResultForNonSelect($pdoStatment) { |
|
| 648 | 648 | //Sqlite and pgsql always return 0 when using rowCount() |
| 649 | - if (in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 649 | + if (in_array($this->config['driver'], array('sqlite', 'pgsql'))) { |
|
| 650 | 650 | $this->result = true; //to test the result for the query like UPDATE, INSERT, DELETE |
| 651 | 651 | $this->numRows = 1; //TODO use the correct method to get the exact affected row |
| 652 | 652 | } |
| 653 | - else{ |
|
| 653 | + else { |
|
| 654 | 654 | $this->result = $pdoStatment->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
| 655 | 655 | $this->numRows = $pdoStatment->rowCount(); |
| 656 | 656 | } |
@@ -662,9 +662,9 @@ discard block |
||
| 662 | 662 | * This method is used to get the PDO DSN string using the configured driver |
| 663 | 663 | * @return string the DSN string |
| 664 | 664 | */ |
| 665 | - protected function getDsnFromDriver(){ |
|
| 665 | + protected function getDsnFromDriver() { |
|
| 666 | 666 | $config = $this->getDatabaseConfiguration(); |
| 667 | - if (! empty($config)){ |
|
| 667 | + if (!empty($config)) { |
|
| 668 | 668 | $driver = $config['driver']; |
| 669 | 669 | $driverDsnMap = array( |
| 670 | 670 | 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
@@ -689,12 +689,12 @@ discard block |
||
| 689 | 689 | * |
| 690 | 690 | * @return string |
| 691 | 691 | */ |
| 692 | - protected function getPreparedQuery($query, $data){ |
|
| 693 | - if (is_array($data)){ |
|
| 692 | + protected function getPreparedQuery($query, $data) { |
|
| 693 | + if (is_array($data)) { |
|
| 694 | 694 | $x = explode('?', $query); |
| 695 | 695 | $q = ''; |
| 696 | - foreach($x as $k => $v){ |
|
| 697 | - if (! empty($v)){ |
|
| 696 | + foreach ($x as $k => $v) { |
|
| 697 | + if (!empty($v)) { |
|
| 698 | 698 | $q .= $v . (isset($data[$k]) ? $this->escape($data[$k]) : ''); |
| 699 | 699 | } |
| 700 | 700 | } |
@@ -709,9 +709,9 @@ discard block |
||
| 709 | 709 | * |
| 710 | 710 | * @return mixed |
| 711 | 711 | */ |
| 712 | - protected function getCacheContentForQuery($query, $all, $array){ |
|
| 712 | + protected function getCacheContentForQuery($query, $all, $array) { |
|
| 713 | 713 | $cacheKey = $this->getCacheBenchmarkKeyForQuery($query, $all, $array); |
| 714 | - if (! is_object($this->getCacheInstance())){ |
|
| 714 | + if (!is_object($this->getCacheInstance())) { |
|
| 715 | 715 | //can not call method with reference in argument |
| 716 | 716 | //like $this->setCacheInstance(& get_instance()->cache); |
| 717 | 717 | //use temporary variable |
@@ -729,10 +729,10 @@ discard block |
||
| 729 | 729 | * @param boolean $status whether can save the query result into cache |
| 730 | 730 | * @param int $expire the cache TTL |
| 731 | 731 | */ |
| 732 | - protected function setCacheContentForQuery($query, $key, $result, $status, $expire){ |
|
| 733 | - if ($status){ |
|
| 734 | - $this->logger->info('Save the result for query [' .$query. '] into cache for future use'); |
|
| 735 | - if (! is_object($this->getCacheInstance())){ |
|
| 732 | + protected function setCacheContentForQuery($query, $key, $result, $status, $expire) { |
|
| 733 | + if ($status) { |
|
| 734 | + $this->logger->info('Save the result for query [' . $query . '] into cache for future use'); |
|
| 735 | + if (!is_object($this->getCacheInstance())) { |
|
| 736 | 736 | //can not call method with reference in argument |
| 737 | 737 | //like $this->setCacheInstance(& get_instance()->cache); |
| 738 | 738 | //use temporary variable |
@@ -747,7 +747,7 @@ discard block |
||
| 747 | 747 | /** |
| 748 | 748 | * Set error for database query execution |
| 749 | 749 | */ |
| 750 | - protected function setQueryError(){ |
|
| 750 | + protected function setQueryError() { |
|
| 751 | 751 | $error = $this->pdo->errorInfo(); |
| 752 | 752 | $this->error = isset($error[2]) ? $error[2] : ''; |
| 753 | 753 | $this->logger->error('The database query execution got error: ' . stringfy_vars($error)); |
@@ -761,8 +761,8 @@ discard block |
||
| 761 | 761 | * |
| 762 | 762 | * @return string |
| 763 | 763 | */ |
| 764 | - protected function getCacheBenchmarkKeyForQuery($query, $all, $array){ |
|
| 765 | - if (is_array($all)){ |
|
| 764 | + protected function getCacheBenchmarkKeyForQuery($query, $all, $array) { |
|
| 765 | + if (is_array($all)) { |
|
| 766 | 766 | $all = 'array'; |
| 767 | 767 | } |
| 768 | 768 | return md5($query . $all . $array); |
@@ -772,12 +772,12 @@ discard block |
||
| 772 | 772 | * Set the Log instance using argument or create new instance |
| 773 | 773 | * @param object $logger the Log instance if not null |
| 774 | 774 | */ |
| 775 | - protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
| 776 | - if ($logger !== null){ |
|
| 775 | + protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null) { |
|
| 776 | + if ($logger !== null) { |
|
| 777 | 777 | $this->setLogger($logger); |
| 778 | 778 | } |
| 779 | - else{ |
|
| 780 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 779 | + else { |
|
| 780 | + $this->logger = & class_loader('Log', 'classes'); |
|
| 781 | 781 | $this->logger->setLogger('Library::Database'); |
| 782 | 782 | } |
| 783 | 783 | } |
@@ -786,19 +786,19 @@ discard block |
||
| 786 | 786 | * Set the DatabaseQueryBuilder instance using argument or create new instance |
| 787 | 787 | * @param object $queryBuilder the DatabaseQueryBuilder instance if not null |
| 788 | 788 | */ |
| 789 | - protected function setQueryBuilderFromParamOrCreateNewInstance(DatabaseQueryBuilder $queryBuilder = null){ |
|
| 790 | - if ($queryBuilder !== null){ |
|
| 789 | + protected function setQueryBuilderFromParamOrCreateNewInstance(DatabaseQueryBuilder $queryBuilder = null) { |
|
| 790 | + if ($queryBuilder !== null) { |
|
| 791 | 791 | $this->setQueryBuilder($queryBuilder); |
| 792 | 792 | } |
| 793 | - else{ |
|
| 794 | - $this->queryBuilder =& class_loader('DatabaseQueryBuilder', 'classes'); |
|
| 793 | + else { |
|
| 794 | + $this->queryBuilder = & class_loader('DatabaseQueryBuilder', 'classes'); |
|
| 795 | 795 | } |
| 796 | 796 | } |
| 797 | 797 | |
| 798 | 798 | /** |
| 799 | 799 | * Reset the database class attributs to the initail values before each query. |
| 800 | 800 | */ |
| 801 | - private function reset(){ |
|
| 801 | + private function reset() { |
|
| 802 | 802 | //query builder reset |
| 803 | 803 | $this->getQueryBuilder()->reset(); |
| 804 | 804 | $this->numRows = 0; |
@@ -812,7 +812,7 @@ discard block |
||
| 812 | 812 | /** |
| 813 | 813 | * The class destructor |
| 814 | 814 | */ |
| 815 | - public function __destruct(){ |
|
| 815 | + public function __destruct() { |
|
| 816 | 816 | $this->pdo = null; |
| 817 | 817 | } |
| 818 | 818 | |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | 3 | /** |
| 4 | 4 | * TNH Framework |
| 5 | 5 | * |
@@ -22,740 +22,740 @@ 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 | class DatabaseQueryBuilder{ |
| 27 | 27 | /** |
| 28 | 28 | * The SQL SELECT statment |
| 29 | 29 | * @var string |
| 30 | - */ |
|
| 30 | + */ |
|
| 31 | 31 | private $select = '*'; |
| 32 | 32 | |
| 33 | 33 | /** |
| 34 | 34 | * The SQL FROM statment |
| 35 | 35 | * @var string |
| 36 | - */ |
|
| 37 | - private $from = null; |
|
| 36 | + */ |
|
| 37 | + private $from = null; |
|
| 38 | 38 | |
| 39 | 39 | /** |
| 40 | 40 | * The SQL WHERE statment |
| 41 | 41 | * @var string |
| 42 | - */ |
|
| 43 | - private $where = null; |
|
| 42 | + */ |
|
| 43 | + private $where = null; |
|
| 44 | 44 | |
| 45 | 45 | /** |
| 46 | 46 | * The SQL LIMIT statment |
| 47 | 47 | * @var string |
| 48 | - */ |
|
| 49 | - private $limit = null; |
|
| 48 | + */ |
|
| 49 | + private $limit = null; |
|
| 50 | 50 | |
| 51 | 51 | /** |
| 52 | 52 | * The SQL JOIN statment |
| 53 | 53 | * @var string |
| 54 | - */ |
|
| 55 | - private $join = null; |
|
| 54 | + */ |
|
| 55 | + private $join = null; |
|
| 56 | 56 | |
| 57 | 57 | /** |
| 58 | 58 | * The SQL ORDER BY statment |
| 59 | 59 | * @var string |
| 60 | - */ |
|
| 61 | - private $orderBy = null; |
|
| 60 | + */ |
|
| 61 | + private $orderBy = null; |
|
| 62 | 62 | |
| 63 | 63 | /** |
| 64 | 64 | * The SQL GROUP BY statment |
| 65 | 65 | * @var string |
| 66 | - */ |
|
| 67 | - private $groupBy = null; |
|
| 66 | + */ |
|
| 67 | + private $groupBy = null; |
|
| 68 | 68 | |
| 69 | 69 | /** |
| 70 | 70 | * The SQL HAVING statment |
| 71 | 71 | * @var string |
| 72 | - */ |
|
| 73 | - private $having = null; |
|
| 72 | + */ |
|
| 73 | + private $having = null; |
|
| 74 | 74 | |
| 75 | 75 | /** |
| 76 | 76 | * The full SQL query statment after build for each command |
| 77 | 77 | * @var string |
| 78 | - */ |
|
| 79 | - private $query = null; |
|
| 78 | + */ |
|
| 79 | + private $query = null; |
|
| 80 | 80 | |
| 81 | 81 | /** |
| 82 | 82 | * The list of SQL valid operators |
| 83 | 83 | * @var array |
| 84 | - */ |
|
| 85 | - private $operatorList = array('=','!=','<','>','<=','>=','<>'); |
|
| 84 | + */ |
|
| 85 | + private $operatorList = array('=','!=','<','>','<=','>=','<>'); |
|
| 86 | 86 | |
| 87 | 87 | |
| 88 | 88 | /** |
| 89 | 89 | * The prefix used in each database table |
| 90 | 90 | * @var string |
| 91 | - */ |
|
| 92 | - private $prefix = null; |
|
| 91 | + */ |
|
| 92 | + private $prefix = null; |
|
| 93 | 93 | |
| 94 | 94 | |
| 95 | 95 | /** |
| 96 | - * The PDO instance |
|
| 97 | - * @var object |
|
| 98 | - */ |
|
| 96 | + * The PDO instance |
|
| 97 | + * @var object |
|
| 98 | + */ |
|
| 99 | 99 | private $pdo = null; |
| 100 | 100 | |
| 101 | 101 | /** |
| 102 | 102 | * The database driver name used |
| 103 | 103 | * @var string |
| 104 | - */ |
|
| 104 | + */ |
|
| 105 | 105 | private $driver = null; |
| 106 | 106 | |
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * Construct new DatabaseQueryBuilder |
|
| 110 | - * @param object $pdo the PDO object |
|
| 111 | - */ |
|
| 112 | - public function __construct(PDO $pdo = null){ |
|
| 113 | - if (is_object($pdo)){ |
|
| 114 | - $this->setPdo($pdo); |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Set the SQL FROM statment |
|
| 120 | - * @param string|array $table the table name or array of table list |
|
| 121 | - * @return object the current DatabaseQueryBuilder instance |
|
| 122 | - */ |
|
| 123 | - public function from($table){ |
|
| 108 | + /** |
|
| 109 | + * Construct new DatabaseQueryBuilder |
|
| 110 | + * @param object $pdo the PDO object |
|
| 111 | + */ |
|
| 112 | + public function __construct(PDO $pdo = null){ |
|
| 113 | + if (is_object($pdo)){ |
|
| 114 | + $this->setPdo($pdo); |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Set the SQL FROM statment |
|
| 120 | + * @param string|array $table the table name or array of table list |
|
| 121 | + * @return object the current DatabaseQueryBuilder instance |
|
| 122 | + */ |
|
| 123 | + public function from($table){ |
|
| 124 | 124 | if (is_array($table)){ |
| 125 | - $froms = ''; |
|
| 126 | - foreach($table as $key){ |
|
| 127 | - $froms .= $this->getPrefix() . $key . ', '; |
|
| 128 | - } |
|
| 129 | - $this->from = rtrim($froms, ', '); |
|
| 130 | - } else { |
|
| 131 | - $this->from = $this->getPrefix() . $table; |
|
| 132 | - } |
|
| 133 | - return $this; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Set the SQL SELECT statment |
|
| 138 | - * @param string|array $fields the field name or array of field list |
|
| 139 | - * @return object the current DatabaseQueryBuilder instance |
|
| 140 | - */ |
|
| 141 | - public function select($fields){ |
|
| 142 | - $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
|
| 143 | - $this->select = (($this->select == '*' || empty($this->select)) ? $select : $this->select . ', ' . $select); |
|
| 144 | - return $this; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Set the SQL SELECT DISTINCT statment |
|
| 149 | - * @param string $field the field name to distinct |
|
| 150 | - * @return object the current DatabaseQueryBuilder instance |
|
| 151 | - */ |
|
| 152 | - public function distinct($field){ |
|
| 153 | - $distinct = ' DISTINCT ' . $field; |
|
| 154 | - $this->select = (($this->select == '*' || empty($this->select)) ? $distinct : $this->select . ', ' . $distinct); |
|
| 155 | - return $this; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Set the SQL function COUNT in SELECT statment |
|
| 160 | - * @param string $field the field name |
|
| 161 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 162 | - * @return object the current DatabaseQueryBuilder instance |
|
| 163 | - */ |
|
| 164 | - public function count($field = '*', $name = null){ |
|
| 165 | - return $this->select_min_max_sum_count_avg('COUNT', $field, $name); |
|
| 166 | - } |
|
| 125 | + $froms = ''; |
|
| 126 | + foreach($table as $key){ |
|
| 127 | + $froms .= $this->getPrefix() . $key . ', '; |
|
| 128 | + } |
|
| 129 | + $this->from = rtrim($froms, ', '); |
|
| 130 | + } else { |
|
| 131 | + $this->from = $this->getPrefix() . $table; |
|
| 132 | + } |
|
| 133 | + return $this; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Set the SQL SELECT statment |
|
| 138 | + * @param string|array $fields the field name or array of field list |
|
| 139 | + * @return object the current DatabaseQueryBuilder instance |
|
| 140 | + */ |
|
| 141 | + public function select($fields){ |
|
| 142 | + $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
|
| 143 | + $this->select = (($this->select == '*' || empty($this->select)) ? $select : $this->select . ', ' . $select); |
|
| 144 | + return $this; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Set the SQL SELECT DISTINCT statment |
|
| 149 | + * @param string $field the field name to distinct |
|
| 150 | + * @return object the current DatabaseQueryBuilder instance |
|
| 151 | + */ |
|
| 152 | + public function distinct($field){ |
|
| 153 | + $distinct = ' DISTINCT ' . $field; |
|
| 154 | + $this->select = (($this->select == '*' || empty($this->select)) ? $distinct : $this->select . ', ' . $distinct); |
|
| 155 | + return $this; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Set the SQL function COUNT in SELECT statment |
|
| 160 | + * @param string $field the field name |
|
| 161 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 162 | + * @return object the current DatabaseQueryBuilder instance |
|
| 163 | + */ |
|
| 164 | + public function count($field = '*', $name = null){ |
|
| 165 | + return $this->select_min_max_sum_count_avg('COUNT', $field, $name); |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - /** |
|
| 169 | - * Set the SQL function MIN in SELECT statment |
|
| 170 | - * @param string $field the field name |
|
| 171 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 172 | - * @return object the current DatabaseQueryBuilder instance |
|
| 173 | - */ |
|
| 174 | - public function min($field, $name = null){ |
|
| 175 | - return $this->select_min_max_sum_count_avg('MIN', $field, $name); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Set the SQL function MAX in SELECT statment |
|
| 180 | - * @param string $field the field name |
|
| 181 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 182 | - * @return object the current DatabaseQueryBuilder instance |
|
| 183 | - */ |
|
| 184 | - public function max($field, $name = null){ |
|
| 185 | - return $this->select_min_max_sum_count_avg('MAX', $field, $name); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Set the SQL function SUM in SELECT statment |
|
| 190 | - * @param string $field the field name |
|
| 191 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 192 | - * @return object the current DatabaseQueryBuilder instance |
|
| 193 | - */ |
|
| 194 | - public function sum($field, $name = null){ |
|
| 195 | - return $this->select_min_max_sum_count_avg('SUM', $field, $name); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Set the SQL function AVG in SELECT statment |
|
| 200 | - * @param string $field the field name |
|
| 201 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 202 | - * @return object the current DatabaseQueryBuilder instance |
|
| 203 | - */ |
|
| 204 | - public function avg($field, $name = null){ |
|
| 205 | - return $this->select_min_max_sum_count_avg('AVG', $field, $name); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Set the SQL JOIN statment |
|
| 211 | - * @param string $table the join table name |
|
| 212 | - * @param string $field1 the first field for join conditions |
|
| 213 | - * @param string $op the join condition operator. If is null the default will be "=" |
|
| 214 | - * @param string $field2 the second field for join conditions |
|
| 215 | - * @param string $type the type of join (INNER, LEFT, RIGHT) |
|
| 216 | - * @return object the current DatabaseQueryBuilder instance |
|
| 217 | - */ |
|
| 218 | - public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){ |
|
| 219 | - $on = $field1; |
|
| 220 | - $table = $this->getPrefix() . $table; |
|
| 221 | - if (! is_null($op)){ |
|
| 222 | - $on = (! in_array($op, $this->operatorList) |
|
| 168 | + /** |
|
| 169 | + * Set the SQL function MIN in SELECT statment |
|
| 170 | + * @param string $field the field name |
|
| 171 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 172 | + * @return object the current DatabaseQueryBuilder instance |
|
| 173 | + */ |
|
| 174 | + public function min($field, $name = null){ |
|
| 175 | + return $this->select_min_max_sum_count_avg('MIN', $field, $name); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Set the SQL function MAX in SELECT statment |
|
| 180 | + * @param string $field the field name |
|
| 181 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 182 | + * @return object the current DatabaseQueryBuilder instance |
|
| 183 | + */ |
|
| 184 | + public function max($field, $name = null){ |
|
| 185 | + return $this->select_min_max_sum_count_avg('MAX', $field, $name); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Set the SQL function SUM in SELECT statment |
|
| 190 | + * @param string $field the field name |
|
| 191 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 192 | + * @return object the current DatabaseQueryBuilder instance |
|
| 193 | + */ |
|
| 194 | + public function sum($field, $name = null){ |
|
| 195 | + return $this->select_min_max_sum_count_avg('SUM', $field, $name); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Set the SQL function AVG in SELECT statment |
|
| 200 | + * @param string $field the field name |
|
| 201 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 202 | + * @return object the current DatabaseQueryBuilder instance |
|
| 203 | + */ |
|
| 204 | + public function avg($field, $name = null){ |
|
| 205 | + return $this->select_min_max_sum_count_avg('AVG', $field, $name); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Set the SQL JOIN statment |
|
| 211 | + * @param string $table the join table name |
|
| 212 | + * @param string $field1 the first field for join conditions |
|
| 213 | + * @param string $op the join condition operator. If is null the default will be "=" |
|
| 214 | + * @param string $field2 the second field for join conditions |
|
| 215 | + * @param string $type the type of join (INNER, LEFT, RIGHT) |
|
| 216 | + * @return object the current DatabaseQueryBuilder instance |
|
| 217 | + */ |
|
| 218 | + public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){ |
|
| 219 | + $on = $field1; |
|
| 220 | + $table = $this->getPrefix() . $table; |
|
| 221 | + if (! is_null($op)){ |
|
| 222 | + $on = (! in_array($op, $this->operatorList) |
|
| 223 | 223 | ? ($this->getPrefix() . $field1 . ' = ' . $this->getPrefix() . $op) |
| 224 | 224 | : ($this->getPrefix() . $field1 . ' ' . $op . ' ' . $this->getPrefix() . $field2)); |
| 225 | - } |
|
| 226 | - if (empty($this->join)){ |
|
| 227 | - $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
| 228 | - } |
|
| 229 | - else{ |
|
| 230 | - $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
| 231 | - } |
|
| 232 | - return $this; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * Set the SQL INNER JOIN statment |
|
| 237 | - * @see DatabaseQueryBuilder::join() |
|
| 238 | - * @return object the current DatabaseQueryBuilder instance |
|
| 239 | - */ |
|
| 240 | - public function innerJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 241 | - return $this->join($table, $field1, $op, $field2, 'INNER '); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * Set the SQL LEFT JOIN statment |
|
| 246 | - * @see DatabaseQueryBuilder::join() |
|
| 247 | - * @return object the current DatabaseQueryBuilder instance |
|
| 248 | - */ |
|
| 249 | - public function leftJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 250 | - return $this->join($table, $field1, $op, $field2, 'LEFT '); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * Set the SQL RIGHT JOIN statment |
|
| 255 | - * @see DatabaseQueryBuilder::join() |
|
| 256 | - * @return object the current DatabaseQueryBuilder instance |
|
| 257 | - */ |
|
| 258 | - public function rightJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 259 | - return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * Set the SQL FULL OUTER JOIN statment |
|
| 264 | - * @see DatabaseQueryBuilder::join() |
|
| 265 | - * @return object the current DatabaseQueryBuilder instance |
|
| 266 | - */ |
|
| 267 | - public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 268 | - return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * Set the SQL LEFT OUTER JOIN statment |
|
| 273 | - * @see DatabaseQueryBuilder::join() |
|
| 274 | - * @return object the current DatabaseQueryBuilder instance |
|
| 275 | - */ |
|
| 276 | - public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 277 | - return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * Set the SQL RIGHT OUTER JOIN statment |
|
| 282 | - * @see DatabaseQueryBuilder::join() |
|
| 283 | - * @return object the current DatabaseQueryBuilder instance |
|
| 284 | - */ |
|
| 285 | - public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 286 | - return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Set the SQL WHERE CLAUSE for IS NULL |
|
| 291 | - * @param string|array $field the field name or array of field list |
|
| 292 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 293 | - * @return object the current DatabaseQueryBuilder instance |
|
| 294 | - */ |
|
| 295 | - public function whereIsNull($field, $andOr = 'AND'){ |
|
| 296 | - if (is_array($field)){ |
|
| 297 | - foreach($field as $f){ |
|
| 298 | - $this->whereIsNull($f, $andOr); |
|
| 299 | - } |
|
| 300 | - } else { |
|
| 301 | - $this->setWhereStr($field.' IS NULL ', $andOr); |
|
| 302 | - } |
|
| 303 | - return $this; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * Set the SQL WHERE CLAUSE for IS NOT NULL |
|
| 308 | - * @param string|array $field the field name or array of field list |
|
| 309 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 310 | - * @return object the current DatabaseQueryBuilder instance |
|
| 311 | - */ |
|
| 312 | - public function whereIsNotNull($field, $andOr = 'AND'){ |
|
| 313 | - if (is_array($field)){ |
|
| 314 | - foreach($field as $f){ |
|
| 315 | - $this->whereIsNotNull($f, $andOr); |
|
| 316 | - } |
|
| 317 | - } else { |
|
| 318 | - $this->setWhereStr($field.' IS NOT NULL ', $andOr); |
|
| 319 | - } |
|
| 320 | - return $this; |
|
| 321 | - } |
|
| 225 | + } |
|
| 226 | + if (empty($this->join)){ |
|
| 227 | + $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
| 228 | + } |
|
| 229 | + else{ |
|
| 230 | + $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
| 231 | + } |
|
| 232 | + return $this; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * Set the SQL INNER JOIN statment |
|
| 237 | + * @see DatabaseQueryBuilder::join() |
|
| 238 | + * @return object the current DatabaseQueryBuilder instance |
|
| 239 | + */ |
|
| 240 | + public function innerJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 241 | + return $this->join($table, $field1, $op, $field2, 'INNER '); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * Set the SQL LEFT JOIN statment |
|
| 246 | + * @see DatabaseQueryBuilder::join() |
|
| 247 | + * @return object the current DatabaseQueryBuilder instance |
|
| 248 | + */ |
|
| 249 | + public function leftJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 250 | + return $this->join($table, $field1, $op, $field2, 'LEFT '); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * Set the SQL RIGHT JOIN statment |
|
| 255 | + * @see DatabaseQueryBuilder::join() |
|
| 256 | + * @return object the current DatabaseQueryBuilder instance |
|
| 257 | + */ |
|
| 258 | + public function rightJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 259 | + return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * Set the SQL FULL OUTER JOIN statment |
|
| 264 | + * @see DatabaseQueryBuilder::join() |
|
| 265 | + * @return object the current DatabaseQueryBuilder instance |
|
| 266 | + */ |
|
| 267 | + public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 268 | + return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * Set the SQL LEFT OUTER JOIN statment |
|
| 273 | + * @see DatabaseQueryBuilder::join() |
|
| 274 | + * @return object the current DatabaseQueryBuilder instance |
|
| 275 | + */ |
|
| 276 | + public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 277 | + return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * Set the SQL RIGHT OUTER JOIN statment |
|
| 282 | + * @see DatabaseQueryBuilder::join() |
|
| 283 | + * @return object the current DatabaseQueryBuilder instance |
|
| 284 | + */ |
|
| 285 | + public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 286 | + return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Set the SQL WHERE CLAUSE for IS NULL |
|
| 291 | + * @param string|array $field the field name or array of field list |
|
| 292 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 293 | + * @return object the current DatabaseQueryBuilder instance |
|
| 294 | + */ |
|
| 295 | + public function whereIsNull($field, $andOr = 'AND'){ |
|
| 296 | + if (is_array($field)){ |
|
| 297 | + foreach($field as $f){ |
|
| 298 | + $this->whereIsNull($f, $andOr); |
|
| 299 | + } |
|
| 300 | + } else { |
|
| 301 | + $this->setWhereStr($field.' IS NULL ', $andOr); |
|
| 302 | + } |
|
| 303 | + return $this; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * Set the SQL WHERE CLAUSE for IS NOT NULL |
|
| 308 | + * @param string|array $field the field name or array of field list |
|
| 309 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 310 | + * @return object the current DatabaseQueryBuilder instance |
|
| 311 | + */ |
|
| 312 | + public function whereIsNotNull($field, $andOr = 'AND'){ |
|
| 313 | + if (is_array($field)){ |
|
| 314 | + foreach($field as $f){ |
|
| 315 | + $this->whereIsNotNull($f, $andOr); |
|
| 316 | + } |
|
| 317 | + } else { |
|
| 318 | + $this->setWhereStr($field.' IS NOT NULL ', $andOr); |
|
| 319 | + } |
|
| 320 | + return $this; |
|
| 321 | + } |
|
| 322 | 322 | |
| 323 | - /** |
|
| 324 | - * Set the SQL WHERE CLAUSE statment |
|
| 325 | - * @param string|array $where the where field or array of field list |
|
| 326 | - * @param array|string $op the condition operator. If is null the default will be "=" |
|
| 327 | - * @param mixed $val the where value |
|
| 328 | - * @param string $type the type used for this where clause (NOT, etc.) |
|
| 329 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 330 | - * @param boolean $escape whether to escape or not the $val |
|
| 331 | - * @return object the current DatabaseQueryBuilder instance |
|
| 332 | - */ |
|
| 333 | - public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
|
| 334 | - $whereStr = ''; |
|
| 335 | - if (is_array($where)){ |
|
| 336 | - $whereStr = $this->getWhereStrIfIsArray($where, $type, $andOr, $escape); |
|
| 337 | - } |
|
| 338 | - else{ |
|
| 339 | - if (is_array($op)){ |
|
| 340 | - $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
|
| 341 | - } else { |
|
| 342 | - $whereStr = $this->getWhereStrForOperator($where, $op, $val, $type, $escape = true); |
|
| 343 | - } |
|
| 344 | - } |
|
| 345 | - $this->setWhereStr($whereStr, $andOr); |
|
| 346 | - return $this; |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - /** |
|
| 350 | - * Set the SQL WHERE CLAUSE statment using OR |
|
| 351 | - * @see DatabaseQueryBuilder::where() |
|
| 352 | - * @return object the current DatabaseQueryBuilder instance |
|
| 353 | - */ |
|
| 354 | - public function orWhere($where, $op = null, $val = null, $escape = true){ |
|
| 355 | - return $this->where($where, $op, $val, '', 'OR', $escape); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * Set the SQL WHERE CLAUSE statment using AND and NOT |
|
| 361 | - * @see DatabaseQueryBuilder::where() |
|
| 362 | - * @return object the current DatabaseQueryBuilder instance |
|
| 363 | - */ |
|
| 364 | - public function notWhere($where, $op = null, $val = null, $escape = true){ |
|
| 365 | - return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - /** |
|
| 369 | - * Set the SQL WHERE CLAUSE statment using OR and NOT |
|
| 370 | - * @see DatabaseQueryBuilder::where() |
|
| 371 | - * @return object the current DatabaseQueryBuilder instance |
|
| 372 | - */ |
|
| 373 | - public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
|
| 374 | - return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - /** |
|
| 378 | - * Set the opened parenthesis for the complex SQL query |
|
| 379 | - * @param string $type the type of this grouped (NOT, etc.) |
|
| 380 | - * @param string $andOr the multiple conditions separator (AND, OR, etc.) |
|
| 381 | - * @return object the current DatabaseQueryBuilder instance |
|
| 382 | - */ |
|
| 383 | - public function groupStart($type = '', $andOr = ' AND'){ |
|
| 384 | - if (empty($this->where)){ |
|
| 385 | - $this->where = $type . ' ('; |
|
| 386 | - } else { |
|
| 387 | - if (substr(trim($this->where), -1) == '('){ |
|
| 388 | - $this->where .= $type . ' ('; |
|
| 389 | - } else { |
|
| 390 | - $this->where .= $andOr . ' ' . $type . ' ('; |
|
| 391 | - } |
|
| 392 | - } |
|
| 393 | - return $this; |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * Set the opened parenthesis for the complex SQL query using NOT type |
|
| 398 | - * @see DatabaseQueryBuilder::groupStart() |
|
| 399 | - * @return object the current DatabaseQueryBuilder instance |
|
| 400 | - */ |
|
| 401 | - public function notGroupStart(){ |
|
| 402 | - return $this->groupStart('NOT'); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - /** |
|
| 406 | - * Set the opened parenthesis for the complex SQL query using OR for separator |
|
| 407 | - * @see DatabaseQueryBuilder::groupStart() |
|
| 408 | - * @return object the current DatabaseQueryBuilder instance |
|
| 409 | - */ |
|
| 410 | - public function orGroupStart(){ |
|
| 411 | - return $this->groupStart('', ' OR'); |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - /** |
|
| 415 | - * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type |
|
| 416 | - * @see DatabaseQueryBuilder::groupStart() |
|
| 417 | - * @return object the current DatabaseQueryBuilder instance |
|
| 418 | - */ |
|
| 419 | - public function orNotGroupStart(){ |
|
| 420 | - return $this->groupStart('NOT', ' OR'); |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * Close the parenthesis for the grouped SQL |
|
| 425 | - * @return object the current DatabaseQueryBuilder instance |
|
| 426 | - */ |
|
| 427 | - public function groupEnd(){ |
|
| 428 | - $this->where .= ')'; |
|
| 429 | - return $this; |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - /** |
|
| 433 | - * Set the SQL WHERE CLAUSE statment for IN |
|
| 434 | - * @param string $field the field name for IN statment |
|
| 435 | - * @param array $keys the list of values used |
|
| 436 | - * @param string $type the condition separator type (NOT) |
|
| 437 | - * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 438 | - * @param boolean $escape whether to escape or not the values |
|
| 439 | - * @return object the current DatabaseQueryBuilder instance |
|
| 440 | - */ |
|
| 441 | - public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
|
| 442 | - $_keys = array(); |
|
| 443 | - foreach ($keys as $k => $v){ |
|
| 444 | - if (is_null($v)){ |
|
| 445 | - $v = ''; |
|
| 446 | - } |
|
| 447 | - $_keys[] = (is_numeric($v) ? $v : $this->escape($v, $escape)); |
|
| 448 | - } |
|
| 449 | - $keys = implode(', ', $_keys); |
|
| 450 | - $whereStr = $field . ' ' . $type . ' IN (' . $keys . ')'; |
|
| 451 | - $this->setWhereStr($whereStr, $andOr); |
|
| 452 | - return $this; |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * Set the SQL WHERE CLAUSE statment for NOT IN with AND separator |
|
| 457 | - * @see DatabaseQueryBuilder::in() |
|
| 458 | - * @return object the current DatabaseQueryBuilder instance |
|
| 459 | - */ |
|
| 460 | - public function notIn($field, array $keys, $escape = true){ |
|
| 461 | - return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * Set the SQL WHERE CLAUSE statment for IN with OR separator |
|
| 466 | - * @see DatabaseQueryBuilder::in() |
|
| 467 | - * @return object the current DatabaseQueryBuilder instance |
|
| 468 | - */ |
|
| 469 | - public function orIn($field, array $keys, $escape = true){ |
|
| 470 | - return $this->in($field, $keys, '', 'OR', $escape); |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - /** |
|
| 474 | - * Set the SQL WHERE CLAUSE statment for NOT IN with OR separator |
|
| 475 | - * @see DatabaseQueryBuilder::in() |
|
| 476 | - * @return object the current DatabaseQueryBuilder instance |
|
| 477 | - */ |
|
| 478 | - public function orNotIn($field, array $keys, $escape = true){ |
|
| 479 | - return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - /** |
|
| 483 | - * Set the SQL WHERE CLAUSE statment for BETWEEN |
|
| 484 | - * @param string $field the field used for the BETWEEN statment |
|
| 485 | - * @param mixed $value1 the BETWEEN begin value |
|
| 486 | - * @param mixed $value2 the BETWEEN end value |
|
| 487 | - * @param string $type the condition separator type (NOT) |
|
| 488 | - * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 489 | - * @param boolean $escape whether to escape or not the values |
|
| 490 | - * @return object the current DatabaseQueryBuilder instance |
|
| 491 | - */ |
|
| 492 | - public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
|
| 493 | - if (is_null($value1)){ |
|
| 494 | - $value1 = ''; |
|
| 495 | - } |
|
| 496 | - if (is_null($value2)){ |
|
| 497 | - $value2 = ''; |
|
| 498 | - } |
|
| 499 | - $whereStr = $field . ' ' . $type . ' BETWEEN ' . $this->escape($value1, $escape) . ' AND ' . $this->escape($value2, $escape); |
|
| 500 | - $this->setWhereStr($whereStr, $andOr); |
|
| 501 | - return $this; |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - /** |
|
| 505 | - * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and AND separator |
|
| 506 | - * @see DatabaseQueryBuilder::between() |
|
| 507 | - * @return object the current DatabaseQueryBuilder instance |
|
| 508 | - */ |
|
| 509 | - public function notBetween($field, $value1, $value2, $escape = true){ |
|
| 510 | - return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - /** |
|
| 514 | - * Set the SQL WHERE CLAUSE statment for BETWEEN with OR separator |
|
| 515 | - * @see DatabaseQueryBuilder::between() |
|
| 516 | - * @return object the current DatabaseQueryBuilder instance |
|
| 517 | - */ |
|
| 518 | - public function orBetween($field, $value1, $value2, $escape = true){ |
|
| 519 | - return $this->between($field, $value1, $value2, '', 'OR', $escape); |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - /** |
|
| 523 | - * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and OR separator |
|
| 524 | - * @see DatabaseQueryBuilder::between() |
|
| 525 | - * @return object the current DatabaseQueryBuilder instance |
|
| 526 | - */ |
|
| 527 | - public function orNotBetween($field, $value1, $value2, $escape = true){ |
|
| 528 | - return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
|
| 529 | - } |
|
| 530 | - |
|
| 531 | - /** |
|
| 532 | - * Set the SQL WHERE CLAUSE statment for LIKE |
|
| 533 | - * @param string $field the field name used in LIKE statment |
|
| 534 | - * @param string $data the LIKE value for this field including the '%', and '_' part |
|
| 535 | - * @param string $type the condition separator type (NOT) |
|
| 536 | - * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 537 | - * @param boolean $escape whether to escape or not the values |
|
| 538 | - * @return object the current DatabaseQueryBuilder instance |
|
| 539 | - */ |
|
| 540 | - public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
|
| 541 | - if (empty($data)){ |
|
| 542 | - $data = ''; |
|
| 543 | - } |
|
| 544 | - $this->setWhereStr($field . ' ' . $type . ' LIKE ' . ($this->escape($data, $escape)), $andOr); |
|
| 545 | - return $this; |
|
| 546 | - } |
|
| 547 | - |
|
| 548 | - /** |
|
| 549 | - * Set the SQL WHERE CLAUSE statment for LIKE with OR separator |
|
| 550 | - * @see DatabaseQueryBuilder::like() |
|
| 551 | - * @return object the current DatabaseQueryBuilder instance |
|
| 552 | - */ |
|
| 553 | - public function orLike($field, $data, $escape = true){ |
|
| 554 | - return $this->like($field, $data, '', 'OR', $escape); |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - /** |
|
| 558 | - * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and AND separator |
|
| 559 | - * @see DatabaseQueryBuilder::like() |
|
| 560 | - * @return object the current DatabaseQueryBuilder instance |
|
| 561 | - */ |
|
| 562 | - public function notLike($field, $data, $escape = true){ |
|
| 563 | - return $this->like($field, $data, 'NOT ', 'AND', $escape); |
|
| 564 | - } |
|
| 565 | - |
|
| 566 | - /** |
|
| 567 | - * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and OR separator |
|
| 568 | - * @see DatabaseQueryBuilder::like() |
|
| 569 | - * @return object the current DatabaseQueryBuilder instance |
|
| 570 | - */ |
|
| 571 | - public function orNotLike($field, $data, $escape = true){ |
|
| 572 | - return $this->like($field, $data, 'NOT ', 'OR', $escape); |
|
| 573 | - } |
|
| 574 | - |
|
| 575 | - /** |
|
| 576 | - * Set the SQL LIMIT statment |
|
| 577 | - * @param int $limit the limit offset. If $limitEnd is null this will be the limit count |
|
| 578 | - * like LIMIT n; |
|
| 579 | - * @param int $limitEnd the limit count |
|
| 580 | - * @return object the current DatabaseQueryBuilder instance |
|
| 581 | - */ |
|
| 582 | - public function limit($limit, $limitEnd = null){ |
|
| 583 | - if (empty($limit)){ |
|
| 584 | - $limit = 0; |
|
| 585 | - } |
|
| 586 | - if (! is_null($limitEnd)){ |
|
| 587 | - $this->limit = $limit . ', ' . $limitEnd; |
|
| 588 | - } |
|
| 589 | - else{ |
|
| 590 | - $this->limit = $limit; |
|
| 591 | - } |
|
| 592 | - return $this; |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - /** |
|
| 596 | - * Set the SQL ORDER BY CLAUSE statment |
|
| 597 | - * @param string $orderBy the field name used for order |
|
| 598 | - * @param string $orderDir the order direction (ASC or DESC) |
|
| 599 | - * @return object the current DatabaseQueryBuilder instance |
|
| 600 | - */ |
|
| 601 | - public function orderBy($orderBy, $orderDir = ' ASC'){ |
|
| 602 | - if (stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
| 603 | - $this->orderBy = empty($this->orderBy) ? $orderBy : $this->orderBy . ', ' . $orderBy; |
|
| 604 | - } |
|
| 605 | - else{ |
|
| 606 | - $this->orderBy = empty($this->orderBy) |
|
| 323 | + /** |
|
| 324 | + * Set the SQL WHERE CLAUSE statment |
|
| 325 | + * @param string|array $where the where field or array of field list |
|
| 326 | + * @param array|string $op the condition operator. If is null the default will be "=" |
|
| 327 | + * @param mixed $val the where value |
|
| 328 | + * @param string $type the type used for this where clause (NOT, etc.) |
|
| 329 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 330 | + * @param boolean $escape whether to escape or not the $val |
|
| 331 | + * @return object the current DatabaseQueryBuilder instance |
|
| 332 | + */ |
|
| 333 | + public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
|
| 334 | + $whereStr = ''; |
|
| 335 | + if (is_array($where)){ |
|
| 336 | + $whereStr = $this->getWhereStrIfIsArray($where, $type, $andOr, $escape); |
|
| 337 | + } |
|
| 338 | + else{ |
|
| 339 | + if (is_array($op)){ |
|
| 340 | + $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
|
| 341 | + } else { |
|
| 342 | + $whereStr = $this->getWhereStrForOperator($where, $op, $val, $type, $escape = true); |
|
| 343 | + } |
|
| 344 | + } |
|
| 345 | + $this->setWhereStr($whereStr, $andOr); |
|
| 346 | + return $this; |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + /** |
|
| 350 | + * Set the SQL WHERE CLAUSE statment using OR |
|
| 351 | + * @see DatabaseQueryBuilder::where() |
|
| 352 | + * @return object the current DatabaseQueryBuilder instance |
|
| 353 | + */ |
|
| 354 | + public function orWhere($where, $op = null, $val = null, $escape = true){ |
|
| 355 | + return $this->where($where, $op, $val, '', 'OR', $escape); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * Set the SQL WHERE CLAUSE statment using AND and NOT |
|
| 361 | + * @see DatabaseQueryBuilder::where() |
|
| 362 | + * @return object the current DatabaseQueryBuilder instance |
|
| 363 | + */ |
|
| 364 | + public function notWhere($where, $op = null, $val = null, $escape = true){ |
|
| 365 | + return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + /** |
|
| 369 | + * Set the SQL WHERE CLAUSE statment using OR and NOT |
|
| 370 | + * @see DatabaseQueryBuilder::where() |
|
| 371 | + * @return object the current DatabaseQueryBuilder instance |
|
| 372 | + */ |
|
| 373 | + public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
|
| 374 | + return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + /** |
|
| 378 | + * Set the opened parenthesis for the complex SQL query |
|
| 379 | + * @param string $type the type of this grouped (NOT, etc.) |
|
| 380 | + * @param string $andOr the multiple conditions separator (AND, OR, etc.) |
|
| 381 | + * @return object the current DatabaseQueryBuilder instance |
|
| 382 | + */ |
|
| 383 | + public function groupStart($type = '', $andOr = ' AND'){ |
|
| 384 | + if (empty($this->where)){ |
|
| 385 | + $this->where = $type . ' ('; |
|
| 386 | + } else { |
|
| 387 | + if (substr(trim($this->where), -1) == '('){ |
|
| 388 | + $this->where .= $type . ' ('; |
|
| 389 | + } else { |
|
| 390 | + $this->where .= $andOr . ' ' . $type . ' ('; |
|
| 391 | + } |
|
| 392 | + } |
|
| 393 | + return $this; |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + /** |
|
| 397 | + * Set the opened parenthesis for the complex SQL query using NOT type |
|
| 398 | + * @see DatabaseQueryBuilder::groupStart() |
|
| 399 | + * @return object the current DatabaseQueryBuilder instance |
|
| 400 | + */ |
|
| 401 | + public function notGroupStart(){ |
|
| 402 | + return $this->groupStart('NOT'); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + /** |
|
| 406 | + * Set the opened parenthesis for the complex SQL query using OR for separator |
|
| 407 | + * @see DatabaseQueryBuilder::groupStart() |
|
| 408 | + * @return object the current DatabaseQueryBuilder instance |
|
| 409 | + */ |
|
| 410 | + public function orGroupStart(){ |
|
| 411 | + return $this->groupStart('', ' OR'); |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + /** |
|
| 415 | + * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type |
|
| 416 | + * @see DatabaseQueryBuilder::groupStart() |
|
| 417 | + * @return object the current DatabaseQueryBuilder instance |
|
| 418 | + */ |
|
| 419 | + public function orNotGroupStart(){ |
|
| 420 | + return $this->groupStart('NOT', ' OR'); |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * Close the parenthesis for the grouped SQL |
|
| 425 | + * @return object the current DatabaseQueryBuilder instance |
|
| 426 | + */ |
|
| 427 | + public function groupEnd(){ |
|
| 428 | + $this->where .= ')'; |
|
| 429 | + return $this; |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + /** |
|
| 433 | + * Set the SQL WHERE CLAUSE statment for IN |
|
| 434 | + * @param string $field the field name for IN statment |
|
| 435 | + * @param array $keys the list of values used |
|
| 436 | + * @param string $type the condition separator type (NOT) |
|
| 437 | + * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 438 | + * @param boolean $escape whether to escape or not the values |
|
| 439 | + * @return object the current DatabaseQueryBuilder instance |
|
| 440 | + */ |
|
| 441 | + public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
|
| 442 | + $_keys = array(); |
|
| 443 | + foreach ($keys as $k => $v){ |
|
| 444 | + if (is_null($v)){ |
|
| 445 | + $v = ''; |
|
| 446 | + } |
|
| 447 | + $_keys[] = (is_numeric($v) ? $v : $this->escape($v, $escape)); |
|
| 448 | + } |
|
| 449 | + $keys = implode(', ', $_keys); |
|
| 450 | + $whereStr = $field . ' ' . $type . ' IN (' . $keys . ')'; |
|
| 451 | + $this->setWhereStr($whereStr, $andOr); |
|
| 452 | + return $this; |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * Set the SQL WHERE CLAUSE statment for NOT IN with AND separator |
|
| 457 | + * @see DatabaseQueryBuilder::in() |
|
| 458 | + * @return object the current DatabaseQueryBuilder instance |
|
| 459 | + */ |
|
| 460 | + public function notIn($field, array $keys, $escape = true){ |
|
| 461 | + return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * Set the SQL WHERE CLAUSE statment for IN with OR separator |
|
| 466 | + * @see DatabaseQueryBuilder::in() |
|
| 467 | + * @return object the current DatabaseQueryBuilder instance |
|
| 468 | + */ |
|
| 469 | + public function orIn($field, array $keys, $escape = true){ |
|
| 470 | + return $this->in($field, $keys, '', 'OR', $escape); |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + /** |
|
| 474 | + * Set the SQL WHERE CLAUSE statment for NOT IN with OR separator |
|
| 475 | + * @see DatabaseQueryBuilder::in() |
|
| 476 | + * @return object the current DatabaseQueryBuilder instance |
|
| 477 | + */ |
|
| 478 | + public function orNotIn($field, array $keys, $escape = true){ |
|
| 479 | + return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + /** |
|
| 483 | + * Set the SQL WHERE CLAUSE statment for BETWEEN |
|
| 484 | + * @param string $field the field used for the BETWEEN statment |
|
| 485 | + * @param mixed $value1 the BETWEEN begin value |
|
| 486 | + * @param mixed $value2 the BETWEEN end value |
|
| 487 | + * @param string $type the condition separator type (NOT) |
|
| 488 | + * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 489 | + * @param boolean $escape whether to escape or not the values |
|
| 490 | + * @return object the current DatabaseQueryBuilder instance |
|
| 491 | + */ |
|
| 492 | + public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
|
| 493 | + if (is_null($value1)){ |
|
| 494 | + $value1 = ''; |
|
| 495 | + } |
|
| 496 | + if (is_null($value2)){ |
|
| 497 | + $value2 = ''; |
|
| 498 | + } |
|
| 499 | + $whereStr = $field . ' ' . $type . ' BETWEEN ' . $this->escape($value1, $escape) . ' AND ' . $this->escape($value2, $escape); |
|
| 500 | + $this->setWhereStr($whereStr, $andOr); |
|
| 501 | + return $this; |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + /** |
|
| 505 | + * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and AND separator |
|
| 506 | + * @see DatabaseQueryBuilder::between() |
|
| 507 | + * @return object the current DatabaseQueryBuilder instance |
|
| 508 | + */ |
|
| 509 | + public function notBetween($field, $value1, $value2, $escape = true){ |
|
| 510 | + return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + /** |
|
| 514 | + * Set the SQL WHERE CLAUSE statment for BETWEEN with OR separator |
|
| 515 | + * @see DatabaseQueryBuilder::between() |
|
| 516 | + * @return object the current DatabaseQueryBuilder instance |
|
| 517 | + */ |
|
| 518 | + public function orBetween($field, $value1, $value2, $escape = true){ |
|
| 519 | + return $this->between($field, $value1, $value2, '', 'OR', $escape); |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + /** |
|
| 523 | + * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and OR separator |
|
| 524 | + * @see DatabaseQueryBuilder::between() |
|
| 525 | + * @return object the current DatabaseQueryBuilder instance |
|
| 526 | + */ |
|
| 527 | + public function orNotBetween($field, $value1, $value2, $escape = true){ |
|
| 528 | + return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
|
| 529 | + } |
|
| 530 | + |
|
| 531 | + /** |
|
| 532 | + * Set the SQL WHERE CLAUSE statment for LIKE |
|
| 533 | + * @param string $field the field name used in LIKE statment |
|
| 534 | + * @param string $data the LIKE value for this field including the '%', and '_' part |
|
| 535 | + * @param string $type the condition separator type (NOT) |
|
| 536 | + * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 537 | + * @param boolean $escape whether to escape or not the values |
|
| 538 | + * @return object the current DatabaseQueryBuilder instance |
|
| 539 | + */ |
|
| 540 | + public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
|
| 541 | + if (empty($data)){ |
|
| 542 | + $data = ''; |
|
| 543 | + } |
|
| 544 | + $this->setWhereStr($field . ' ' . $type . ' LIKE ' . ($this->escape($data, $escape)), $andOr); |
|
| 545 | + return $this; |
|
| 546 | + } |
|
| 547 | + |
|
| 548 | + /** |
|
| 549 | + * Set the SQL WHERE CLAUSE statment for LIKE with OR separator |
|
| 550 | + * @see DatabaseQueryBuilder::like() |
|
| 551 | + * @return object the current DatabaseQueryBuilder instance |
|
| 552 | + */ |
|
| 553 | + public function orLike($field, $data, $escape = true){ |
|
| 554 | + return $this->like($field, $data, '', 'OR', $escape); |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + /** |
|
| 558 | + * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and AND separator |
|
| 559 | + * @see DatabaseQueryBuilder::like() |
|
| 560 | + * @return object the current DatabaseQueryBuilder instance |
|
| 561 | + */ |
|
| 562 | + public function notLike($field, $data, $escape = true){ |
|
| 563 | + return $this->like($field, $data, 'NOT ', 'AND', $escape); |
|
| 564 | + } |
|
| 565 | + |
|
| 566 | + /** |
|
| 567 | + * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and OR separator |
|
| 568 | + * @see DatabaseQueryBuilder::like() |
|
| 569 | + * @return object the current DatabaseQueryBuilder instance |
|
| 570 | + */ |
|
| 571 | + public function orNotLike($field, $data, $escape = true){ |
|
| 572 | + return $this->like($field, $data, 'NOT ', 'OR', $escape); |
|
| 573 | + } |
|
| 574 | + |
|
| 575 | + /** |
|
| 576 | + * Set the SQL LIMIT statment |
|
| 577 | + * @param int $limit the limit offset. If $limitEnd is null this will be the limit count |
|
| 578 | + * like LIMIT n; |
|
| 579 | + * @param int $limitEnd the limit count |
|
| 580 | + * @return object the current DatabaseQueryBuilder instance |
|
| 581 | + */ |
|
| 582 | + public function limit($limit, $limitEnd = null){ |
|
| 583 | + if (empty($limit)){ |
|
| 584 | + $limit = 0; |
|
| 585 | + } |
|
| 586 | + if (! is_null($limitEnd)){ |
|
| 587 | + $this->limit = $limit . ', ' . $limitEnd; |
|
| 588 | + } |
|
| 589 | + else{ |
|
| 590 | + $this->limit = $limit; |
|
| 591 | + } |
|
| 592 | + return $this; |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + /** |
|
| 596 | + * Set the SQL ORDER BY CLAUSE statment |
|
| 597 | + * @param string $orderBy the field name used for order |
|
| 598 | + * @param string $orderDir the order direction (ASC or DESC) |
|
| 599 | + * @return object the current DatabaseQueryBuilder instance |
|
| 600 | + */ |
|
| 601 | + public function orderBy($orderBy, $orderDir = ' ASC'){ |
|
| 602 | + if (stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
| 603 | + $this->orderBy = empty($this->orderBy) ? $orderBy : $this->orderBy . ', ' . $orderBy; |
|
| 604 | + } |
|
| 605 | + else{ |
|
| 606 | + $this->orderBy = empty($this->orderBy) |
|
| 607 | 607 | ? ($orderBy . ' ' . strtoupper($orderDir)) |
| 608 | 608 | : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
| 609 | - } |
|
| 610 | - return $this; |
|
| 611 | - } |
|
| 612 | - |
|
| 613 | - /** |
|
| 614 | - * Set the SQL GROUP BY CLAUSE statment |
|
| 615 | - * @param string|array $field the field name used or array of field list |
|
| 616 | - * @return object the current DatabaseQueryBuilder instance |
|
| 617 | - */ |
|
| 618 | - public function groupBy($field){ |
|
| 619 | - if (is_array($field)){ |
|
| 620 | - $this->groupBy = implode(', ', $field); |
|
| 621 | - } |
|
| 622 | - else{ |
|
| 623 | - $this->groupBy = $field; |
|
| 624 | - } |
|
| 625 | - return $this; |
|
| 626 | - } |
|
| 627 | - |
|
| 628 | - /** |
|
| 629 | - * Set the SQL HAVING CLAUSE statment |
|
| 630 | - * @param string $field the field name used for HAVING statment |
|
| 631 | - * @param string|array $op the operator used or array |
|
| 632 | - * @param mixed $val the value for HAVING comparaison |
|
| 633 | - * @param boolean $escape whether to escape or not the values |
|
| 634 | - * @return object the current DatabaseQueryBuilder instance |
|
| 635 | - */ |
|
| 636 | - public function having($field, $op = null, $val = null, $escape = true){ |
|
| 637 | - if (is_array($op)){ |
|
| 638 | - $this->having = $this->getHavingStrIfOperatorIsArray($field, $op, $escape); |
|
| 639 | - } |
|
| 640 | - else if (! in_array($op, $this->operatorList)){ |
|
| 641 | - if (is_null($op)){ |
|
| 642 | - $op = ''; |
|
| 643 | - } |
|
| 644 | - $this->having = $field . ' > ' . ($this->escape($op, $escape)); |
|
| 645 | - } |
|
| 646 | - else{ |
|
| 647 | - if (is_null($val)){ |
|
| 648 | - $val = ''; |
|
| 649 | - } |
|
| 650 | - $this->having = $field . ' ' . $op . ' ' . ($this->escape($val, $escape)); |
|
| 651 | - } |
|
| 652 | - return $this; |
|
| 653 | - } |
|
| 654 | - |
|
| 655 | - /** |
|
| 656 | - * Insert new record in the database |
|
| 657 | - * @param array $data the record data |
|
| 658 | - * @param boolean $escape whether to escape or not the values |
|
| 659 | - * @return object the current DatabaseQueryBuilder instance |
|
| 660 | - */ |
|
| 661 | - public function insert($data = array(), $escape = true){ |
|
| 662 | - $columns = array_keys($data); |
|
| 663 | - $column = implode(',', $columns); |
|
| 664 | - $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
|
| 665 | - |
|
| 666 | - $this->query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
|
| 667 | - return $this; |
|
| 668 | - } |
|
| 669 | - |
|
| 670 | - /** |
|
| 671 | - * Update record in the database |
|
| 672 | - * @param array $data the record data if is empty will use the $this->data array. |
|
| 673 | - * @param boolean $escape whether to escape or not the values |
|
| 674 | - * @return object the current DatabaseQueryBuilder instance |
|
| 675 | - */ |
|
| 676 | - public function update($data = array(), $escape = true){ |
|
| 677 | - $query = 'UPDATE ' . $this->from . ' SET '; |
|
| 678 | - $values = array(); |
|
| 679 | - foreach ($data as $column => $val){ |
|
| 680 | - $values[] = $column . ' = ' . ($this->escape($val, $escape)); |
|
| 681 | - } |
|
| 682 | - $query .= implode(', ', $values); |
|
| 683 | - if (! empty($this->where)){ |
|
| 684 | - $query .= ' WHERE ' . $this->where; |
|
| 685 | - } |
|
| 686 | - |
|
| 687 | - if (! empty($this->orderBy)){ |
|
| 688 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
| 689 | - } |
|
| 690 | - |
|
| 691 | - if (! empty($this->limit)){ |
|
| 692 | - $query .= ' LIMIT ' . $this->limit; |
|
| 693 | - } |
|
| 694 | - $this->query = $query; |
|
| 695 | - return $this; |
|
| 696 | - } |
|
| 697 | - |
|
| 698 | - /** |
|
| 699 | - * Delete the record in database |
|
| 700 | - * @return object the current DatabaseQueryBuilder instance |
|
| 701 | - */ |
|
| 702 | - public function delete(){ |
|
| 703 | - $query = 'DELETE FROM ' . $this->from; |
|
| 704 | - $isTruncate = $query; |
|
| 705 | - if (! empty($this->where)){ |
|
| 609 | + } |
|
| 610 | + return $this; |
|
| 611 | + } |
|
| 612 | + |
|
| 613 | + /** |
|
| 614 | + * Set the SQL GROUP BY CLAUSE statment |
|
| 615 | + * @param string|array $field the field name used or array of field list |
|
| 616 | + * @return object the current DatabaseQueryBuilder instance |
|
| 617 | + */ |
|
| 618 | + public function groupBy($field){ |
|
| 619 | + if (is_array($field)){ |
|
| 620 | + $this->groupBy = implode(', ', $field); |
|
| 621 | + } |
|
| 622 | + else{ |
|
| 623 | + $this->groupBy = $field; |
|
| 624 | + } |
|
| 625 | + return $this; |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + /** |
|
| 629 | + * Set the SQL HAVING CLAUSE statment |
|
| 630 | + * @param string $field the field name used for HAVING statment |
|
| 631 | + * @param string|array $op the operator used or array |
|
| 632 | + * @param mixed $val the value for HAVING comparaison |
|
| 633 | + * @param boolean $escape whether to escape or not the values |
|
| 634 | + * @return object the current DatabaseQueryBuilder instance |
|
| 635 | + */ |
|
| 636 | + public function having($field, $op = null, $val = null, $escape = true){ |
|
| 637 | + if (is_array($op)){ |
|
| 638 | + $this->having = $this->getHavingStrIfOperatorIsArray($field, $op, $escape); |
|
| 639 | + } |
|
| 640 | + else if (! in_array($op, $this->operatorList)){ |
|
| 641 | + if (is_null($op)){ |
|
| 642 | + $op = ''; |
|
| 643 | + } |
|
| 644 | + $this->having = $field . ' > ' . ($this->escape($op, $escape)); |
|
| 645 | + } |
|
| 646 | + else{ |
|
| 647 | + if (is_null($val)){ |
|
| 648 | + $val = ''; |
|
| 649 | + } |
|
| 650 | + $this->having = $field . ' ' . $op . ' ' . ($this->escape($val, $escape)); |
|
| 651 | + } |
|
| 652 | + return $this; |
|
| 653 | + } |
|
| 654 | + |
|
| 655 | + /** |
|
| 656 | + * Insert new record in the database |
|
| 657 | + * @param array $data the record data |
|
| 658 | + * @param boolean $escape whether to escape or not the values |
|
| 659 | + * @return object the current DatabaseQueryBuilder instance |
|
| 660 | + */ |
|
| 661 | + public function insert($data = array(), $escape = true){ |
|
| 662 | + $columns = array_keys($data); |
|
| 663 | + $column = implode(',', $columns); |
|
| 664 | + $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
|
| 665 | + |
|
| 666 | + $this->query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
|
| 667 | + return $this; |
|
| 668 | + } |
|
| 669 | + |
|
| 670 | + /** |
|
| 671 | + * Update record in the database |
|
| 672 | + * @param array $data the record data if is empty will use the $this->data array. |
|
| 673 | + * @param boolean $escape whether to escape or not the values |
|
| 674 | + * @return object the current DatabaseQueryBuilder instance |
|
| 675 | + */ |
|
| 676 | + public function update($data = array(), $escape = true){ |
|
| 677 | + $query = 'UPDATE ' . $this->from . ' SET '; |
|
| 678 | + $values = array(); |
|
| 679 | + foreach ($data as $column => $val){ |
|
| 680 | + $values[] = $column . ' = ' . ($this->escape($val, $escape)); |
|
| 681 | + } |
|
| 682 | + $query .= implode(', ', $values); |
|
| 683 | + if (! empty($this->where)){ |
|
| 684 | + $query .= ' WHERE ' . $this->where; |
|
| 685 | + } |
|
| 686 | + |
|
| 687 | + if (! empty($this->orderBy)){ |
|
| 688 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
| 689 | + } |
|
| 690 | + |
|
| 691 | + if (! empty($this->limit)){ |
|
| 692 | + $query .= ' LIMIT ' . $this->limit; |
|
| 693 | + } |
|
| 694 | + $this->query = $query; |
|
| 695 | + return $this; |
|
| 696 | + } |
|
| 697 | + |
|
| 698 | + /** |
|
| 699 | + * Delete the record in database |
|
| 700 | + * @return object the current DatabaseQueryBuilder instance |
|
| 701 | + */ |
|
| 702 | + public function delete(){ |
|
| 703 | + $query = 'DELETE FROM ' . $this->from; |
|
| 704 | + $isTruncate = $query; |
|
| 705 | + if (! empty($this->where)){ |
|
| 706 | 706 | $query .= ' WHERE ' . $this->where; |
| 707 | - } |
|
| 707 | + } |
|
| 708 | 708 | |
| 709 | - if (! empty($this->orderBy)){ |
|
| 710 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
| 711 | - } |
|
| 709 | + if (! empty($this->orderBy)){ |
|
| 710 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
| 711 | + } |
|
| 712 | 712 | |
| 713 | - if (! empty($this->limit)){ |
|
| 714 | - $query .= ' LIMIT ' . $this->limit; |
|
| 715 | - } |
|
| 713 | + if (! empty($this->limit)){ |
|
| 714 | + $query .= ' LIMIT ' . $this->limit; |
|
| 715 | + } |
|
| 716 | 716 | |
| 717 | 717 | if ($isTruncate == $query && $this->driver != 'sqlite'){ |
| 718 | - $query = 'TRUNCATE TABLE ' . $this->from; |
|
| 718 | + $query = 'TRUNCATE TABLE ' . $this->from; |
|
| 719 | 719 | } |
| 720 | 720 | $this->query = $query; |
| 721 | 721 | return $this; |
| 722 | - } |
|
| 723 | - |
|
| 724 | - /** |
|
| 725 | - * Escape the data before execute query useful for security. |
|
| 726 | - * @param mixed $data the data to be escaped |
|
| 727 | - * @param boolean $escaped whether we can do escape of not |
|
| 728 | - * @return mixed the data after escaped or the same data if not |
|
| 729 | - */ |
|
| 730 | - public function escape($data, $escaped = true){ |
|
| 731 | - return $escaped |
|
| 732 | - ? $this->getPdo()->quote(trim($data)) |
|
| 733 | - : $data; |
|
| 734 | - } |
|
| 735 | - |
|
| 736 | - |
|
| 737 | - /** |
|
| 738 | - * Return the current SQL query string |
|
| 739 | - * @return string |
|
| 740 | - */ |
|
| 741 | - public function getQuery(){ |
|
| 722 | + } |
|
| 723 | + |
|
| 724 | + /** |
|
| 725 | + * Escape the data before execute query useful for security. |
|
| 726 | + * @param mixed $data the data to be escaped |
|
| 727 | + * @param boolean $escaped whether we can do escape of not |
|
| 728 | + * @return mixed the data after escaped or the same data if not |
|
| 729 | + */ |
|
| 730 | + public function escape($data, $escaped = true){ |
|
| 731 | + return $escaped |
|
| 732 | + ? $this->getPdo()->quote(trim($data)) |
|
| 733 | + : $data; |
|
| 734 | + } |
|
| 735 | + |
|
| 736 | + |
|
| 737 | + /** |
|
| 738 | + * Return the current SQL query string |
|
| 739 | + * @return string |
|
| 740 | + */ |
|
| 741 | + public function getQuery(){ |
|
| 742 | 742 | //INSERT, UPDATE, DELETE already set it, if is the SELECT we need set it now |
| 743 | 743 | if(empty($this->query)){ |
| 744 | 744 | $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
| 745 | 745 | if (! empty($this->join)){ |
| 746 | - $query .= $this->join; |
|
| 746 | + $query .= $this->join; |
|
| 747 | 747 | } |
| 748 | 748 | |
| 749 | 749 | if (! empty($this->where)){ |
| 750 | - $query .= ' WHERE ' . $this->where; |
|
| 750 | + $query .= ' WHERE ' . $this->where; |
|
| 751 | 751 | } |
| 752 | 752 | |
| 753 | 753 | if (! empty($this->groupBy)){ |
| 754 | - $query .= ' GROUP BY ' . $this->groupBy; |
|
| 754 | + $query .= ' GROUP BY ' . $this->groupBy; |
|
| 755 | 755 | } |
| 756 | 756 | |
| 757 | 757 | if (! empty($this->having)){ |
| 758 | - $query .= ' HAVING ' . $this->having; |
|
| 758 | + $query .= ' HAVING ' . $this->having; |
|
| 759 | 759 | } |
| 760 | 760 | |
| 761 | 761 | if (! empty($this->orderBy)){ |
@@ -763,198 +763,198 @@ discard block |
||
| 763 | 763 | } |
| 764 | 764 | |
| 765 | 765 | if (! empty($this->limit)){ |
| 766 | - $query .= ' LIMIT ' . $this->limit; |
|
| 766 | + $query .= ' LIMIT ' . $this->limit; |
|
| 767 | 767 | } |
| 768 | 768 | $this->query = $query; |
| 769 | 769 | } |
| 770 | - return $this->query; |
|
| 771 | - } |
|
| 770 | + return $this->query; |
|
| 771 | + } |
|
| 772 | 772 | |
| 773 | 773 | |
| 774 | 774 | /** |
| 775 | - * Return the PDO instance |
|
| 776 | - * @return PDO |
|
| 777 | - */ |
|
| 778 | - public function getPdo(){ |
|
| 779 | - return $this->pdo; |
|
| 780 | - } |
|
| 781 | - |
|
| 782 | - /** |
|
| 783 | - * Set the PDO instance |
|
| 784 | - * @param PDO $pdo the pdo object |
|
| 775 | + * Return the PDO instance |
|
| 776 | + * @return PDO |
|
| 777 | + */ |
|
| 778 | + public function getPdo(){ |
|
| 779 | + return $this->pdo; |
|
| 780 | + } |
|
| 781 | + |
|
| 782 | + /** |
|
| 783 | + * Set the PDO instance |
|
| 784 | + * @param PDO $pdo the pdo object |
|
| 785 | 785 | * @return object DatabaseQueryBuilder |
| 786 | - */ |
|
| 787 | - public function setPdo(PDO $pdo = null){ |
|
| 788 | - $this->pdo = $pdo; |
|
| 789 | - return $this; |
|
| 790 | - } |
|
| 786 | + */ |
|
| 787 | + public function setPdo(PDO $pdo = null){ |
|
| 788 | + $this->pdo = $pdo; |
|
| 789 | + return $this; |
|
| 790 | + } |
|
| 791 | 791 | |
| 792 | 792 | /** |
| 793 | - * Return the database table prefix |
|
| 794 | - * @return string |
|
| 795 | - */ |
|
| 796 | - public function getPrefix(){ |
|
| 797 | - return $this->prefix; |
|
| 798 | - } |
|
| 799 | - |
|
| 800 | - /** |
|
| 801 | - * Set the database table prefix |
|
| 802 | - * @param string $prefix the new prefix |
|
| 803 | - * @return object DatabaseQueryBuilder |
|
| 804 | - */ |
|
| 805 | - public function setPrefix($prefix){ |
|
| 806 | - $this->prefix = $prefix; |
|
| 807 | - return $this; |
|
| 808 | - } |
|
| 793 | + * Return the database table prefix |
|
| 794 | + * @return string |
|
| 795 | + */ |
|
| 796 | + public function getPrefix(){ |
|
| 797 | + return $this->prefix; |
|
| 798 | + } |
|
| 799 | + |
|
| 800 | + /** |
|
| 801 | + * Set the database table prefix |
|
| 802 | + * @param string $prefix the new prefix |
|
| 803 | + * @return object DatabaseQueryBuilder |
|
| 804 | + */ |
|
| 805 | + public function setPrefix($prefix){ |
|
| 806 | + $this->prefix = $prefix; |
|
| 807 | + return $this; |
|
| 808 | + } |
|
| 809 | 809 | |
| 810 | 810 | /** |
| 811 | - * Return the database driver |
|
| 812 | - * @return string |
|
| 813 | - */ |
|
| 814 | - public function getDriver(){ |
|
| 815 | - return $this->driver; |
|
| 816 | - } |
|
| 817 | - |
|
| 818 | - /** |
|
| 819 | - * Set the database driver |
|
| 820 | - * @param string $driver the new driver |
|
| 821 | - * @return object DatabaseQueryBuilder |
|
| 822 | - */ |
|
| 823 | - public function setDriver($driver){ |
|
| 824 | - $this->driver = $driver; |
|
| 825 | - return $this; |
|
| 826 | - } |
|
| 811 | + * Return the database driver |
|
| 812 | + * @return string |
|
| 813 | + */ |
|
| 814 | + public function getDriver(){ |
|
| 815 | + return $this->driver; |
|
| 816 | + } |
|
| 817 | + |
|
| 818 | + /** |
|
| 819 | + * Set the database driver |
|
| 820 | + * @param string $driver the new driver |
|
| 821 | + * @return object DatabaseQueryBuilder |
|
| 822 | + */ |
|
| 823 | + public function setDriver($driver){ |
|
| 824 | + $this->driver = $driver; |
|
| 825 | + return $this; |
|
| 826 | + } |
|
| 827 | 827 | |
| 828 | 828 | /** |
| 829 | - * Reset the DatabaseQueryBuilder class attributs to the initial values before each query. |
|
| 830 | - * @return object the current DatabaseQueryBuilder instance |
|
| 831 | - */ |
|
| 832 | - public function reset(){ |
|
| 833 | - $this->select = '*'; |
|
| 834 | - $this->from = null; |
|
| 835 | - $this->where = null; |
|
| 836 | - $this->limit = null; |
|
| 837 | - $this->orderBy = null; |
|
| 838 | - $this->groupBy = null; |
|
| 839 | - $this->having = null; |
|
| 840 | - $this->join = null; |
|
| 841 | - $this->query = null; |
|
| 842 | - return $this; |
|
| 843 | - } |
|
| 829 | + * Reset the DatabaseQueryBuilder class attributs to the initial values before each query. |
|
| 830 | + * @return object the current DatabaseQueryBuilder instance |
|
| 831 | + */ |
|
| 832 | + public function reset(){ |
|
| 833 | + $this->select = '*'; |
|
| 834 | + $this->from = null; |
|
| 835 | + $this->where = null; |
|
| 836 | + $this->limit = null; |
|
| 837 | + $this->orderBy = null; |
|
| 838 | + $this->groupBy = null; |
|
| 839 | + $this->having = null; |
|
| 840 | + $this->join = null; |
|
| 841 | + $this->query = null; |
|
| 842 | + return $this; |
|
| 843 | + } |
|
| 844 | 844 | |
| 845 | 845 | /** |
| 846 | - * Get the SQL HAVING clause when operator argument is an array |
|
| 847 | - * @see DatabaseQueryBuilder::having |
|
| 848 | - * |
|
| 849 | - * @return string |
|
| 850 | - */ |
|
| 851 | - protected function getHavingStrIfOperatorIsArray($field, $op = null, $escape = true){ |
|
| 852 | - $x = explode('?', $field); |
|
| 853 | - $w = ''; |
|
| 854 | - foreach($x as $k => $v){ |
|
| 855 | - if (!empty($v)){ |
|
| 856 | - if (! isset($op[$k])){ |
|
| 857 | - $op[$k] = ''; |
|
| 858 | - } |
|
| 859 | - $w .= $v . (isset($op[$k]) ? $this->escape($op[$k], $escape) : ''); |
|
| 860 | - } |
|
| 861 | - } |
|
| 862 | - return $w; |
|
| 863 | - } |
|
| 864 | - |
|
| 865 | - |
|
| 866 | - /** |
|
| 867 | - * Get the SQL WHERE clause using array column => value |
|
| 868 | - * @see DatabaseQueryBuilder::where |
|
| 869 | - * |
|
| 870 | - * @return string |
|
| 871 | - */ |
|
| 872 | - protected function getWhereStrIfIsArray(array $where, $type = '', $andOr = 'AND', $escape = true){ |
|
| 873 | - $_where = array(); |
|
| 874 | - foreach ($where as $column => $data){ |
|
| 875 | - if (is_null($data)){ |
|
| 876 | - $data = ''; |
|
| 877 | - } |
|
| 878 | - $_where[] = $type . $column . ' = ' . ($this->escape($data, $escape)); |
|
| 879 | - } |
|
| 880 | - $where = implode(' '.$andOr.' ', $_where); |
|
| 881 | - return $where; |
|
| 882 | - } |
|
| 883 | - |
|
| 884 | - /** |
|
| 885 | - * Get the SQL WHERE clause when operator argument is an array |
|
| 886 | - * @see DatabaseQueryBuilder::where |
|
| 887 | - * |
|
| 888 | - * @return string |
|
| 889 | - */ |
|
| 890 | - protected function getWhereStrIfOperatorIsArray($where, array $op, $type = '', $escape = true){ |
|
| 891 | - $x = explode('?', $where); |
|
| 892 | - $w = ''; |
|
| 893 | - foreach($x as $k => $v){ |
|
| 894 | - if (! empty($v)){ |
|
| 895 | - if (isset($op[$k]) && is_null($op[$k])){ |
|
| 896 | - $op[$k] = ''; |
|
| 897 | - } |
|
| 898 | - $w .= $type . $v . (isset($op[$k]) ? ($this->escape($op[$k], $escape)) : ''); |
|
| 899 | - } |
|
| 900 | - } |
|
| 901 | - return $w; |
|
| 902 | - } |
|
| 903 | - |
|
| 904 | - /** |
|
| 905 | - * Get the default SQL WHERE clause using operator = or the operator argument |
|
| 906 | - * @see DatabaseQueryBuilder::where |
|
| 907 | - * |
|
| 908 | - * @return string |
|
| 909 | - */ |
|
| 910 | - protected function getWhereStrForOperator($where, $op = null, $val = null, $type = '', $escape = true){ |
|
| 911 | - $w = ''; |
|
| 912 | - if (! in_array((string)$op, $this->operatorList)){ |
|
| 913 | - if (is_null($op)){ |
|
| 914 | - $op = ''; |
|
| 915 | - } |
|
| 916 | - $w = $type . $where . ' = ' . ($this->escape($op, $escape)); |
|
| 917 | - } else { |
|
| 918 | - if (is_null($val)){ |
|
| 919 | - $val = ''; |
|
| 920 | - } |
|
| 921 | - $w = $type . $where . $op . ($this->escape($val, $escape)); |
|
| 922 | - } |
|
| 923 | - return $w; |
|
| 924 | - } |
|
| 925 | - |
|
| 926 | - /** |
|
| 927 | - * Set the $this->where property |
|
| 928 | - * @param string $whereStr the WHERE clause string |
|
| 929 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 930 | - */ |
|
| 931 | - protected function setWhereStr($whereStr, $andOr = 'AND'){ |
|
| 932 | - if (empty($this->where)){ |
|
| 933 | - $this->where = $whereStr; |
|
| 934 | - } else { |
|
| 935 | - if (substr(trim($this->where), -1) == '('){ |
|
| 936 | - $this->where = $this->where . ' ' . $whereStr; |
|
| 937 | - } else { |
|
| 938 | - $this->where = $this->where . ' '.$andOr.' ' . $whereStr; |
|
| 939 | - } |
|
| 940 | - } |
|
| 941 | - } |
|
| 846 | + * Get the SQL HAVING clause when operator argument is an array |
|
| 847 | + * @see DatabaseQueryBuilder::having |
|
| 848 | + * |
|
| 849 | + * @return string |
|
| 850 | + */ |
|
| 851 | + protected function getHavingStrIfOperatorIsArray($field, $op = null, $escape = true){ |
|
| 852 | + $x = explode('?', $field); |
|
| 853 | + $w = ''; |
|
| 854 | + foreach($x as $k => $v){ |
|
| 855 | + if (!empty($v)){ |
|
| 856 | + if (! isset($op[$k])){ |
|
| 857 | + $op[$k] = ''; |
|
| 858 | + } |
|
| 859 | + $w .= $v . (isset($op[$k]) ? $this->escape($op[$k], $escape) : ''); |
|
| 860 | + } |
|
| 861 | + } |
|
| 862 | + return $w; |
|
| 863 | + } |
|
| 864 | + |
|
| 865 | + |
|
| 866 | + /** |
|
| 867 | + * Get the SQL WHERE clause using array column => value |
|
| 868 | + * @see DatabaseQueryBuilder::where |
|
| 869 | + * |
|
| 870 | + * @return string |
|
| 871 | + */ |
|
| 872 | + protected function getWhereStrIfIsArray(array $where, $type = '', $andOr = 'AND', $escape = true){ |
|
| 873 | + $_where = array(); |
|
| 874 | + foreach ($where as $column => $data){ |
|
| 875 | + if (is_null($data)){ |
|
| 876 | + $data = ''; |
|
| 877 | + } |
|
| 878 | + $_where[] = $type . $column . ' = ' . ($this->escape($data, $escape)); |
|
| 879 | + } |
|
| 880 | + $where = implode(' '.$andOr.' ', $_where); |
|
| 881 | + return $where; |
|
| 882 | + } |
|
| 883 | + |
|
| 884 | + /** |
|
| 885 | + * Get the SQL WHERE clause when operator argument is an array |
|
| 886 | + * @see DatabaseQueryBuilder::where |
|
| 887 | + * |
|
| 888 | + * @return string |
|
| 889 | + */ |
|
| 890 | + protected function getWhereStrIfOperatorIsArray($where, array $op, $type = '', $escape = true){ |
|
| 891 | + $x = explode('?', $where); |
|
| 892 | + $w = ''; |
|
| 893 | + foreach($x as $k => $v){ |
|
| 894 | + if (! empty($v)){ |
|
| 895 | + if (isset($op[$k]) && is_null($op[$k])){ |
|
| 896 | + $op[$k] = ''; |
|
| 897 | + } |
|
| 898 | + $w .= $type . $v . (isset($op[$k]) ? ($this->escape($op[$k], $escape)) : ''); |
|
| 899 | + } |
|
| 900 | + } |
|
| 901 | + return $w; |
|
| 902 | + } |
|
| 903 | + |
|
| 904 | + /** |
|
| 905 | + * Get the default SQL WHERE clause using operator = or the operator argument |
|
| 906 | + * @see DatabaseQueryBuilder::where |
|
| 907 | + * |
|
| 908 | + * @return string |
|
| 909 | + */ |
|
| 910 | + protected function getWhereStrForOperator($where, $op = null, $val = null, $type = '', $escape = true){ |
|
| 911 | + $w = ''; |
|
| 912 | + if (! in_array((string)$op, $this->operatorList)){ |
|
| 913 | + if (is_null($op)){ |
|
| 914 | + $op = ''; |
|
| 915 | + } |
|
| 916 | + $w = $type . $where . ' = ' . ($this->escape($op, $escape)); |
|
| 917 | + } else { |
|
| 918 | + if (is_null($val)){ |
|
| 919 | + $val = ''; |
|
| 920 | + } |
|
| 921 | + $w = $type . $where . $op . ($this->escape($val, $escape)); |
|
| 922 | + } |
|
| 923 | + return $w; |
|
| 924 | + } |
|
| 925 | + |
|
| 926 | + /** |
|
| 927 | + * Set the $this->where property |
|
| 928 | + * @param string $whereStr the WHERE clause string |
|
| 929 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 930 | + */ |
|
| 931 | + protected function setWhereStr($whereStr, $andOr = 'AND'){ |
|
| 932 | + if (empty($this->where)){ |
|
| 933 | + $this->where = $whereStr; |
|
| 934 | + } else { |
|
| 935 | + if (substr(trim($this->where), -1) == '('){ |
|
| 936 | + $this->where = $this->where . ' ' . $whereStr; |
|
| 937 | + } else { |
|
| 938 | + $this->where = $this->where . ' '.$andOr.' ' . $whereStr; |
|
| 939 | + } |
|
| 940 | + } |
|
| 941 | + } |
|
| 942 | 942 | |
| 943 | 943 | |
| 944 | 944 | /** |
| 945 | - * Set the SQL SELECT for function MIN, MAX, SUM, AVG, COUNT, AVG |
|
| 946 | - * @param string $clause the clause type like MIN, MAX, etc. |
|
| 947 | - * @see DatabaseQueryBuilder::min |
|
| 948 | - * @see DatabaseQueryBuilder::max |
|
| 949 | - * @see DatabaseQueryBuilder::sum |
|
| 950 | - * @see DatabaseQueryBuilder::count |
|
| 951 | - * @see DatabaseQueryBuilder::avg |
|
| 952 | - * @return object |
|
| 953 | - */ |
|
| 954 | - protected function select_min_max_sum_count_avg($clause, $field, $name = null){ |
|
| 955 | - $clause = strtoupper($clause); |
|
| 956 | - $func = $clause . '(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 957 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 958 | - return $this; |
|
| 959 | - } |
|
| 945 | + * Set the SQL SELECT for function MIN, MAX, SUM, AVG, COUNT, AVG |
|
| 946 | + * @param string $clause the clause type like MIN, MAX, etc. |
|
| 947 | + * @see DatabaseQueryBuilder::min |
|
| 948 | + * @see DatabaseQueryBuilder::max |
|
| 949 | + * @see DatabaseQueryBuilder::sum |
|
| 950 | + * @see DatabaseQueryBuilder::count |
|
| 951 | + * @see DatabaseQueryBuilder::avg |
|
| 952 | + * @return object |
|
| 953 | + */ |
|
| 954 | + protected function select_min_max_sum_count_avg($clause, $field, $name = null){ |
|
| 955 | + $clause = strtoupper($clause); |
|
| 956 | + $func = $clause . '(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 957 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 958 | + return $this; |
|
| 959 | + } |
|
| 960 | 960 | } |
@@ -23,94 +23,94 @@ discard block |
||
| 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 | - class DatabaseQueryBuilder{ |
|
| 26 | + class DatabaseQueryBuilder { |
|
| 27 | 27 | /** |
| 28 | 28 | * The SQL SELECT statment |
| 29 | 29 | * @var string |
| 30 | 30 | */ |
| 31 | - private $select = '*'; |
|
| 31 | + private $select = '*'; |
|
| 32 | 32 | |
| 33 | 33 | /** |
| 34 | 34 | * The SQL FROM statment |
| 35 | 35 | * @var string |
| 36 | 36 | */ |
| 37 | - private $from = null; |
|
| 37 | + private $from = null; |
|
| 38 | 38 | |
| 39 | 39 | /** |
| 40 | 40 | * The SQL WHERE statment |
| 41 | 41 | * @var string |
| 42 | 42 | */ |
| 43 | - private $where = null; |
|
| 43 | + private $where = null; |
|
| 44 | 44 | |
| 45 | 45 | /** |
| 46 | 46 | * The SQL LIMIT statment |
| 47 | 47 | * @var string |
| 48 | 48 | */ |
| 49 | - private $limit = null; |
|
| 49 | + private $limit = null; |
|
| 50 | 50 | |
| 51 | 51 | /** |
| 52 | 52 | * The SQL JOIN statment |
| 53 | 53 | * @var string |
| 54 | 54 | */ |
| 55 | - private $join = null; |
|
| 55 | + private $join = null; |
|
| 56 | 56 | |
| 57 | 57 | /** |
| 58 | 58 | * The SQL ORDER BY statment |
| 59 | 59 | * @var string |
| 60 | 60 | */ |
| 61 | - private $orderBy = null; |
|
| 61 | + private $orderBy = null; |
|
| 62 | 62 | |
| 63 | 63 | /** |
| 64 | 64 | * The SQL GROUP BY statment |
| 65 | 65 | * @var string |
| 66 | 66 | */ |
| 67 | - private $groupBy = null; |
|
| 67 | + private $groupBy = null; |
|
| 68 | 68 | |
| 69 | 69 | /** |
| 70 | 70 | * The SQL HAVING statment |
| 71 | 71 | * @var string |
| 72 | 72 | */ |
| 73 | - private $having = null; |
|
| 73 | + private $having = null; |
|
| 74 | 74 | |
| 75 | 75 | /** |
| 76 | 76 | * The full SQL query statment after build for each command |
| 77 | 77 | * @var string |
| 78 | 78 | */ |
| 79 | - private $query = null; |
|
| 79 | + private $query = null; |
|
| 80 | 80 | |
| 81 | 81 | /** |
| 82 | 82 | * The list of SQL valid operators |
| 83 | 83 | * @var array |
| 84 | 84 | */ |
| 85 | - private $operatorList = array('=','!=','<','>','<=','>=','<>'); |
|
| 85 | + private $operatorList = array('=', '!=', '<', '>', '<=', '>=', '<>'); |
|
| 86 | 86 | |
| 87 | 87 | |
| 88 | 88 | /** |
| 89 | 89 | * The prefix used in each database table |
| 90 | 90 | * @var string |
| 91 | 91 | */ |
| 92 | - private $prefix = null; |
|
| 92 | + private $prefix = null; |
|
| 93 | 93 | |
| 94 | 94 | |
| 95 | 95 | /** |
| 96 | 96 | * The PDO instance |
| 97 | 97 | * @var object |
| 98 | 98 | */ |
| 99 | - private $pdo = null; |
|
| 99 | + private $pdo = null; |
|
| 100 | 100 | |
| 101 | 101 | /** |
| 102 | 102 | * The database driver name used |
| 103 | 103 | * @var string |
| 104 | 104 | */ |
| 105 | - private $driver = null; |
|
| 105 | + private $driver = null; |
|
| 106 | 106 | |
| 107 | 107 | |
| 108 | 108 | /** |
| 109 | 109 | * Construct new DatabaseQueryBuilder |
| 110 | 110 | * @param object $pdo the PDO object |
| 111 | 111 | */ |
| 112 | - public function __construct(PDO $pdo = null){ |
|
| 113 | - if (is_object($pdo)){ |
|
| 112 | + public function __construct(PDO $pdo = null) { |
|
| 113 | + if (is_object($pdo)) { |
|
| 114 | 114 | $this->setPdo($pdo); |
| 115 | 115 | } |
| 116 | 116 | } |
@@ -120,10 +120,10 @@ discard block |
||
| 120 | 120 | * @param string|array $table the table name or array of table list |
| 121 | 121 | * @return object the current DatabaseQueryBuilder instance |
| 122 | 122 | */ |
| 123 | - public function from($table){ |
|
| 124 | - if (is_array($table)){ |
|
| 123 | + public function from($table) { |
|
| 124 | + if (is_array($table)) { |
|
| 125 | 125 | $froms = ''; |
| 126 | - foreach($table as $key){ |
|
| 126 | + foreach ($table as $key) { |
|
| 127 | 127 | $froms .= $this->getPrefix() . $key . ', '; |
| 128 | 128 | } |
| 129 | 129 | $this->from = rtrim($froms, ', '); |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | * @param string|array $fields the field name or array of field list |
| 139 | 139 | * @return object the current DatabaseQueryBuilder instance |
| 140 | 140 | */ |
| 141 | - public function select($fields){ |
|
| 141 | + public function select($fields) { |
|
| 142 | 142 | $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
| 143 | 143 | $this->select = (($this->select == '*' || empty($this->select)) ? $select : $this->select . ', ' . $select); |
| 144 | 144 | return $this; |
@@ -149,7 +149,7 @@ discard block |
||
| 149 | 149 | * @param string $field the field name to distinct |
| 150 | 150 | * @return object the current DatabaseQueryBuilder instance |
| 151 | 151 | */ |
| 152 | - public function distinct($field){ |
|
| 152 | + public function distinct($field) { |
|
| 153 | 153 | $distinct = ' DISTINCT ' . $field; |
| 154 | 154 | $this->select = (($this->select == '*' || empty($this->select)) ? $distinct : $this->select . ', ' . $distinct); |
| 155 | 155 | return $this; |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | * @param string $name if is not null represent the alias used for this field in the result |
| 162 | 162 | * @return object the current DatabaseQueryBuilder instance |
| 163 | 163 | */ |
| 164 | - public function count($field = '*', $name = null){ |
|
| 164 | + public function count($field = '*', $name = null) { |
|
| 165 | 165 | return $this->select_min_max_sum_count_avg('COUNT', $field, $name); |
| 166 | 166 | } |
| 167 | 167 | |
@@ -171,7 +171,7 @@ discard block |
||
| 171 | 171 | * @param string $name if is not null represent the alias used for this field in the result |
| 172 | 172 | * @return object the current DatabaseQueryBuilder instance |
| 173 | 173 | */ |
| 174 | - public function min($field, $name = null){ |
|
| 174 | + public function min($field, $name = null) { |
|
| 175 | 175 | return $this->select_min_max_sum_count_avg('MIN', $field, $name); |
| 176 | 176 | } |
| 177 | 177 | |
@@ -181,7 +181,7 @@ discard block |
||
| 181 | 181 | * @param string $name if is not null represent the alias used for this field in the result |
| 182 | 182 | * @return object the current DatabaseQueryBuilder instance |
| 183 | 183 | */ |
| 184 | - public function max($field, $name = null){ |
|
| 184 | + public function max($field, $name = null) { |
|
| 185 | 185 | return $this->select_min_max_sum_count_avg('MAX', $field, $name); |
| 186 | 186 | } |
| 187 | 187 | |
@@ -191,7 +191,7 @@ discard block |
||
| 191 | 191 | * @param string $name if is not null represent the alias used for this field in the result |
| 192 | 192 | * @return object the current DatabaseQueryBuilder instance |
| 193 | 193 | */ |
| 194 | - public function sum($field, $name = null){ |
|
| 194 | + public function sum($field, $name = null) { |
|
| 195 | 195 | return $this->select_min_max_sum_count_avg('SUM', $field, $name); |
| 196 | 196 | } |
| 197 | 197 | |
@@ -201,7 +201,7 @@ discard block |
||
| 201 | 201 | * @param string $name if is not null represent the alias used for this field in the result |
| 202 | 202 | * @return object the current DatabaseQueryBuilder instance |
| 203 | 203 | */ |
| 204 | - public function avg($field, $name = null){ |
|
| 204 | + public function avg($field, $name = null) { |
|
| 205 | 205 | return $this->select_min_max_sum_count_avg('AVG', $field, $name); |
| 206 | 206 | } |
| 207 | 207 | |
@@ -215,18 +215,18 @@ discard block |
||
| 215 | 215 | * @param string $type the type of join (INNER, LEFT, RIGHT) |
| 216 | 216 | * @return object the current DatabaseQueryBuilder instance |
| 217 | 217 | */ |
| 218 | - public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){ |
|
| 218 | + public function join($table, $field1 = null, $op = null, $field2 = null, $type = '') { |
|
| 219 | 219 | $on = $field1; |
| 220 | 220 | $table = $this->getPrefix() . $table; |
| 221 | - if (! is_null($op)){ |
|
| 222 | - $on = (! in_array($op, $this->operatorList) |
|
| 221 | + if (!is_null($op)) { |
|
| 222 | + $on = (!in_array($op, $this->operatorList) |
|
| 223 | 223 | ? ($this->getPrefix() . $field1 . ' = ' . $this->getPrefix() . $op) |
| 224 | 224 | : ($this->getPrefix() . $field1 . ' ' . $op . ' ' . $this->getPrefix() . $field2)); |
| 225 | 225 | } |
| 226 | - if (empty($this->join)){ |
|
| 226 | + if (empty($this->join)) { |
|
| 227 | 227 | $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 228 | 228 | } |
| 229 | - else{ |
|
| 229 | + else { |
|
| 230 | 230 | $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 231 | 231 | } |
| 232 | 232 | return $this; |
@@ -237,7 +237,7 @@ discard block |
||
| 237 | 237 | * @see DatabaseQueryBuilder::join() |
| 238 | 238 | * @return object the current DatabaseQueryBuilder instance |
| 239 | 239 | */ |
| 240 | - public function innerJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 240 | + public function innerJoin($table, $field1, $op = null, $field2 = '') { |
|
| 241 | 241 | return $this->join($table, $field1, $op, $field2, 'INNER '); |
| 242 | 242 | } |
| 243 | 243 | |
@@ -246,7 +246,7 @@ discard block |
||
| 246 | 246 | * @see DatabaseQueryBuilder::join() |
| 247 | 247 | * @return object the current DatabaseQueryBuilder instance |
| 248 | 248 | */ |
| 249 | - public function leftJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 249 | + public function leftJoin($table, $field1, $op = null, $field2 = '') { |
|
| 250 | 250 | return $this->join($table, $field1, $op, $field2, 'LEFT '); |
| 251 | 251 | } |
| 252 | 252 | |
@@ -255,7 +255,7 @@ discard block |
||
| 255 | 255 | * @see DatabaseQueryBuilder::join() |
| 256 | 256 | * @return object the current DatabaseQueryBuilder instance |
| 257 | 257 | */ |
| 258 | - public function rightJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 258 | + public function rightJoin($table, $field1, $op = null, $field2 = '') { |
|
| 259 | 259 | return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
| 260 | 260 | } |
| 261 | 261 | |
@@ -264,7 +264,7 @@ discard block |
||
| 264 | 264 | * @see DatabaseQueryBuilder::join() |
| 265 | 265 | * @return object the current DatabaseQueryBuilder instance |
| 266 | 266 | */ |
| 267 | - public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 267 | + public function fullOuterJoin($table, $field1, $op = null, $field2 = '') { |
|
| 268 | 268 | return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
| 269 | 269 | } |
| 270 | 270 | |
@@ -273,7 +273,7 @@ discard block |
||
| 273 | 273 | * @see DatabaseQueryBuilder::join() |
| 274 | 274 | * @return object the current DatabaseQueryBuilder instance |
| 275 | 275 | */ |
| 276 | - public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 276 | + public function leftOuterJoin($table, $field1, $op = null, $field2 = '') { |
|
| 277 | 277 | return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
| 278 | 278 | } |
| 279 | 279 | |
@@ -282,7 +282,7 @@ discard block |
||
| 282 | 282 | * @see DatabaseQueryBuilder::join() |
| 283 | 283 | * @return object the current DatabaseQueryBuilder instance |
| 284 | 284 | */ |
| 285 | - public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 285 | + public function rightOuterJoin($table, $field1, $op = null, $field2 = '') { |
|
| 286 | 286 | return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
| 287 | 287 | } |
| 288 | 288 | |
@@ -292,13 +292,13 @@ discard block |
||
| 292 | 292 | * @param string $andOr the separator type used 'AND', 'OR', etc. |
| 293 | 293 | * @return object the current DatabaseQueryBuilder instance |
| 294 | 294 | */ |
| 295 | - public function whereIsNull($field, $andOr = 'AND'){ |
|
| 296 | - if (is_array($field)){ |
|
| 297 | - foreach($field as $f){ |
|
| 295 | + public function whereIsNull($field, $andOr = 'AND') { |
|
| 296 | + if (is_array($field)) { |
|
| 297 | + foreach ($field as $f) { |
|
| 298 | 298 | $this->whereIsNull($f, $andOr); |
| 299 | 299 | } |
| 300 | 300 | } else { |
| 301 | - $this->setWhereStr($field.' IS NULL ', $andOr); |
|
| 301 | + $this->setWhereStr($field . ' IS NULL ', $andOr); |
|
| 302 | 302 | } |
| 303 | 303 | return $this; |
| 304 | 304 | } |
@@ -309,13 +309,13 @@ discard block |
||
| 309 | 309 | * @param string $andOr the separator type used 'AND', 'OR', etc. |
| 310 | 310 | * @return object the current DatabaseQueryBuilder instance |
| 311 | 311 | */ |
| 312 | - public function whereIsNotNull($field, $andOr = 'AND'){ |
|
| 313 | - if (is_array($field)){ |
|
| 314 | - foreach($field as $f){ |
|
| 312 | + public function whereIsNotNull($field, $andOr = 'AND') { |
|
| 313 | + if (is_array($field)) { |
|
| 314 | + foreach ($field as $f) { |
|
| 315 | 315 | $this->whereIsNotNull($f, $andOr); |
| 316 | 316 | } |
| 317 | 317 | } else { |
| 318 | - $this->setWhereStr($field.' IS NOT NULL ', $andOr); |
|
| 318 | + $this->setWhereStr($field . ' IS NOT NULL ', $andOr); |
|
| 319 | 319 | } |
| 320 | 320 | return $this; |
| 321 | 321 | } |
@@ -330,13 +330,13 @@ discard block |
||
| 330 | 330 | * @param boolean $escape whether to escape or not the $val |
| 331 | 331 | * @return object the current DatabaseQueryBuilder instance |
| 332 | 332 | */ |
| 333 | - public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
|
| 333 | + public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true) { |
|
| 334 | 334 | $whereStr = ''; |
| 335 | - if (is_array($where)){ |
|
| 335 | + if (is_array($where)) { |
|
| 336 | 336 | $whereStr = $this->getWhereStrIfIsArray($where, $type, $andOr, $escape); |
| 337 | 337 | } |
| 338 | - else{ |
|
| 339 | - if (is_array($op)){ |
|
| 338 | + else { |
|
| 339 | + if (is_array($op)) { |
|
| 340 | 340 | $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
| 341 | 341 | } else { |
| 342 | 342 | $whereStr = $this->getWhereStrForOperator($where, $op, $val, $type, $escape = true); |
@@ -351,7 +351,7 @@ discard block |
||
| 351 | 351 | * @see DatabaseQueryBuilder::where() |
| 352 | 352 | * @return object the current DatabaseQueryBuilder instance |
| 353 | 353 | */ |
| 354 | - public function orWhere($where, $op = null, $val = null, $escape = true){ |
|
| 354 | + public function orWhere($where, $op = null, $val = null, $escape = true) { |
|
| 355 | 355 | return $this->where($where, $op, $val, '', 'OR', $escape); |
| 356 | 356 | } |
| 357 | 357 | |
@@ -361,7 +361,7 @@ discard block |
||
| 361 | 361 | * @see DatabaseQueryBuilder::where() |
| 362 | 362 | * @return object the current DatabaseQueryBuilder instance |
| 363 | 363 | */ |
| 364 | - public function notWhere($where, $op = null, $val = null, $escape = true){ |
|
| 364 | + public function notWhere($where, $op = null, $val = null, $escape = true) { |
|
| 365 | 365 | return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
| 366 | 366 | } |
| 367 | 367 | |
@@ -370,7 +370,7 @@ discard block |
||
| 370 | 370 | * @see DatabaseQueryBuilder::where() |
| 371 | 371 | * @return object the current DatabaseQueryBuilder instance |
| 372 | 372 | */ |
| 373 | - public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
|
| 373 | + public function orNotWhere($where, $op = null, $val = null, $escape = true) { |
|
| 374 | 374 | return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
| 375 | 375 | } |
| 376 | 376 | |
@@ -380,11 +380,11 @@ discard block |
||
| 380 | 380 | * @param string $andOr the multiple conditions separator (AND, OR, etc.) |
| 381 | 381 | * @return object the current DatabaseQueryBuilder instance |
| 382 | 382 | */ |
| 383 | - public function groupStart($type = '', $andOr = ' AND'){ |
|
| 384 | - if (empty($this->where)){ |
|
| 383 | + public function groupStart($type = '', $andOr = ' AND') { |
|
| 384 | + if (empty($this->where)) { |
|
| 385 | 385 | $this->where = $type . ' ('; |
| 386 | 386 | } else { |
| 387 | - if (substr(trim($this->where), -1) == '('){ |
|
| 387 | + if (substr(trim($this->where), -1) == '(') { |
|
| 388 | 388 | $this->where .= $type . ' ('; |
| 389 | 389 | } else { |
| 390 | 390 | $this->where .= $andOr . ' ' . $type . ' ('; |
@@ -398,7 +398,7 @@ discard block |
||
| 398 | 398 | * @see DatabaseQueryBuilder::groupStart() |
| 399 | 399 | * @return object the current DatabaseQueryBuilder instance |
| 400 | 400 | */ |
| 401 | - public function notGroupStart(){ |
|
| 401 | + public function notGroupStart() { |
|
| 402 | 402 | return $this->groupStart('NOT'); |
| 403 | 403 | } |
| 404 | 404 | |
@@ -407,7 +407,7 @@ discard block |
||
| 407 | 407 | * @see DatabaseQueryBuilder::groupStart() |
| 408 | 408 | * @return object the current DatabaseQueryBuilder instance |
| 409 | 409 | */ |
| 410 | - public function orGroupStart(){ |
|
| 410 | + public function orGroupStart() { |
|
| 411 | 411 | return $this->groupStart('', ' OR'); |
| 412 | 412 | } |
| 413 | 413 | |
@@ -416,7 +416,7 @@ discard block |
||
| 416 | 416 | * @see DatabaseQueryBuilder::groupStart() |
| 417 | 417 | * @return object the current DatabaseQueryBuilder instance |
| 418 | 418 | */ |
| 419 | - public function orNotGroupStart(){ |
|
| 419 | + public function orNotGroupStart() { |
|
| 420 | 420 | return $this->groupStart('NOT', ' OR'); |
| 421 | 421 | } |
| 422 | 422 | |
@@ -424,7 +424,7 @@ discard block |
||
| 424 | 424 | * Close the parenthesis for the grouped SQL |
| 425 | 425 | * @return object the current DatabaseQueryBuilder instance |
| 426 | 426 | */ |
| 427 | - public function groupEnd(){ |
|
| 427 | + public function groupEnd() { |
|
| 428 | 428 | $this->where .= ')'; |
| 429 | 429 | return $this; |
| 430 | 430 | } |
@@ -438,10 +438,10 @@ discard block |
||
| 438 | 438 | * @param boolean $escape whether to escape or not the values |
| 439 | 439 | * @return object the current DatabaseQueryBuilder instance |
| 440 | 440 | */ |
| 441 | - public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
|
| 441 | + public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true) { |
|
| 442 | 442 | $_keys = array(); |
| 443 | - foreach ($keys as $k => $v){ |
|
| 444 | - if (is_null($v)){ |
|
| 443 | + foreach ($keys as $k => $v) { |
|
| 444 | + if (is_null($v)) { |
|
| 445 | 445 | $v = ''; |
| 446 | 446 | } |
| 447 | 447 | $_keys[] = (is_numeric($v) ? $v : $this->escape($v, $escape)); |
@@ -457,7 +457,7 @@ discard block |
||
| 457 | 457 | * @see DatabaseQueryBuilder::in() |
| 458 | 458 | * @return object the current DatabaseQueryBuilder instance |
| 459 | 459 | */ |
| 460 | - public function notIn($field, array $keys, $escape = true){ |
|
| 460 | + public function notIn($field, array $keys, $escape = true) { |
|
| 461 | 461 | return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
| 462 | 462 | } |
| 463 | 463 | |
@@ -466,7 +466,7 @@ discard block |
||
| 466 | 466 | * @see DatabaseQueryBuilder::in() |
| 467 | 467 | * @return object the current DatabaseQueryBuilder instance |
| 468 | 468 | */ |
| 469 | - public function orIn($field, array $keys, $escape = true){ |
|
| 469 | + public function orIn($field, array $keys, $escape = true) { |
|
| 470 | 470 | return $this->in($field, $keys, '', 'OR', $escape); |
| 471 | 471 | } |
| 472 | 472 | |
@@ -475,7 +475,7 @@ discard block |
||
| 475 | 475 | * @see DatabaseQueryBuilder::in() |
| 476 | 476 | * @return object the current DatabaseQueryBuilder instance |
| 477 | 477 | */ |
| 478 | - public function orNotIn($field, array $keys, $escape = true){ |
|
| 478 | + public function orNotIn($field, array $keys, $escape = true) { |
|
| 479 | 479 | return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
| 480 | 480 | } |
| 481 | 481 | |
@@ -489,11 +489,11 @@ discard block |
||
| 489 | 489 | * @param boolean $escape whether to escape or not the values |
| 490 | 490 | * @return object the current DatabaseQueryBuilder instance |
| 491 | 491 | */ |
| 492 | - public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
|
| 493 | - if (is_null($value1)){ |
|
| 492 | + public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true) { |
|
| 493 | + if (is_null($value1)) { |
|
| 494 | 494 | $value1 = ''; |
| 495 | 495 | } |
| 496 | - if (is_null($value2)){ |
|
| 496 | + if (is_null($value2)) { |
|
| 497 | 497 | $value2 = ''; |
| 498 | 498 | } |
| 499 | 499 | $whereStr = $field . ' ' . $type . ' BETWEEN ' . $this->escape($value1, $escape) . ' AND ' . $this->escape($value2, $escape); |
@@ -506,7 +506,7 @@ discard block |
||
| 506 | 506 | * @see DatabaseQueryBuilder::between() |
| 507 | 507 | * @return object the current DatabaseQueryBuilder instance |
| 508 | 508 | */ |
| 509 | - public function notBetween($field, $value1, $value2, $escape = true){ |
|
| 509 | + public function notBetween($field, $value1, $value2, $escape = true) { |
|
| 510 | 510 | return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
| 511 | 511 | } |
| 512 | 512 | |
@@ -515,7 +515,7 @@ discard block |
||
| 515 | 515 | * @see DatabaseQueryBuilder::between() |
| 516 | 516 | * @return object the current DatabaseQueryBuilder instance |
| 517 | 517 | */ |
| 518 | - public function orBetween($field, $value1, $value2, $escape = true){ |
|
| 518 | + public function orBetween($field, $value1, $value2, $escape = true) { |
|
| 519 | 519 | return $this->between($field, $value1, $value2, '', 'OR', $escape); |
| 520 | 520 | } |
| 521 | 521 | |
@@ -524,7 +524,7 @@ discard block |
||
| 524 | 524 | * @see DatabaseQueryBuilder::between() |
| 525 | 525 | * @return object the current DatabaseQueryBuilder instance |
| 526 | 526 | */ |
| 527 | - public function orNotBetween($field, $value1, $value2, $escape = true){ |
|
| 527 | + public function orNotBetween($field, $value1, $value2, $escape = true) { |
|
| 528 | 528 | return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
| 529 | 529 | } |
| 530 | 530 | |
@@ -537,8 +537,8 @@ discard block |
||
| 537 | 537 | * @param boolean $escape whether to escape or not the values |
| 538 | 538 | * @return object the current DatabaseQueryBuilder instance |
| 539 | 539 | */ |
| 540 | - public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
|
| 541 | - if (empty($data)){ |
|
| 540 | + public function like($field, $data, $type = '', $andOr = 'AND', $escape = true) { |
|
| 541 | + if (empty($data)) { |
|
| 542 | 542 | $data = ''; |
| 543 | 543 | } |
| 544 | 544 | $this->setWhereStr($field . ' ' . $type . ' LIKE ' . ($this->escape($data, $escape)), $andOr); |
@@ -550,7 +550,7 @@ discard block |
||
| 550 | 550 | * @see DatabaseQueryBuilder::like() |
| 551 | 551 | * @return object the current DatabaseQueryBuilder instance |
| 552 | 552 | */ |
| 553 | - public function orLike($field, $data, $escape = true){ |
|
| 553 | + public function orLike($field, $data, $escape = true) { |
|
| 554 | 554 | return $this->like($field, $data, '', 'OR', $escape); |
| 555 | 555 | } |
| 556 | 556 | |
@@ -559,7 +559,7 @@ discard block |
||
| 559 | 559 | * @see DatabaseQueryBuilder::like() |
| 560 | 560 | * @return object the current DatabaseQueryBuilder instance |
| 561 | 561 | */ |
| 562 | - public function notLike($field, $data, $escape = true){ |
|
| 562 | + public function notLike($field, $data, $escape = true) { |
|
| 563 | 563 | return $this->like($field, $data, 'NOT ', 'AND', $escape); |
| 564 | 564 | } |
| 565 | 565 | |
@@ -568,7 +568,7 @@ discard block |
||
| 568 | 568 | * @see DatabaseQueryBuilder::like() |
| 569 | 569 | * @return object the current DatabaseQueryBuilder instance |
| 570 | 570 | */ |
| 571 | - public function orNotLike($field, $data, $escape = true){ |
|
| 571 | + public function orNotLike($field, $data, $escape = true) { |
|
| 572 | 572 | return $this->like($field, $data, 'NOT ', 'OR', $escape); |
| 573 | 573 | } |
| 574 | 574 | |
@@ -579,14 +579,14 @@ discard block |
||
| 579 | 579 | * @param int $limitEnd the limit count |
| 580 | 580 | * @return object the current DatabaseQueryBuilder instance |
| 581 | 581 | */ |
| 582 | - public function limit($limit, $limitEnd = null){ |
|
| 583 | - if (empty($limit)){ |
|
| 582 | + public function limit($limit, $limitEnd = null) { |
|
| 583 | + if (empty($limit)) { |
|
| 584 | 584 | $limit = 0; |
| 585 | 585 | } |
| 586 | - if (! is_null($limitEnd)){ |
|
| 586 | + if (!is_null($limitEnd)) { |
|
| 587 | 587 | $this->limit = $limit . ', ' . $limitEnd; |
| 588 | 588 | } |
| 589 | - else{ |
|
| 589 | + else { |
|
| 590 | 590 | $this->limit = $limit; |
| 591 | 591 | } |
| 592 | 592 | return $this; |
@@ -598,11 +598,11 @@ discard block |
||
| 598 | 598 | * @param string $orderDir the order direction (ASC or DESC) |
| 599 | 599 | * @return object the current DatabaseQueryBuilder instance |
| 600 | 600 | */ |
| 601 | - public function orderBy($orderBy, $orderDir = ' ASC'){ |
|
| 602 | - if (stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
| 601 | + public function orderBy($orderBy, $orderDir = ' ASC') { |
|
| 602 | + if (stristr($orderBy, ' ') || $orderBy == 'rand()') { |
|
| 603 | 603 | $this->orderBy = empty($this->orderBy) ? $orderBy : $this->orderBy . ', ' . $orderBy; |
| 604 | 604 | } |
| 605 | - else{ |
|
| 605 | + else { |
|
| 606 | 606 | $this->orderBy = empty($this->orderBy) |
| 607 | 607 | ? ($orderBy . ' ' . strtoupper($orderDir)) |
| 608 | 608 | : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
@@ -615,11 +615,11 @@ discard block |
||
| 615 | 615 | * @param string|array $field the field name used or array of field list |
| 616 | 616 | * @return object the current DatabaseQueryBuilder instance |
| 617 | 617 | */ |
| 618 | - public function groupBy($field){ |
|
| 619 | - if (is_array($field)){ |
|
| 618 | + public function groupBy($field) { |
|
| 619 | + if (is_array($field)) { |
|
| 620 | 620 | $this->groupBy = implode(', ', $field); |
| 621 | 621 | } |
| 622 | - else{ |
|
| 622 | + else { |
|
| 623 | 623 | $this->groupBy = $field; |
| 624 | 624 | } |
| 625 | 625 | return $this; |
@@ -633,18 +633,18 @@ discard block |
||
| 633 | 633 | * @param boolean $escape whether to escape or not the values |
| 634 | 634 | * @return object the current DatabaseQueryBuilder instance |
| 635 | 635 | */ |
| 636 | - public function having($field, $op = null, $val = null, $escape = true){ |
|
| 637 | - if (is_array($op)){ |
|
| 636 | + public function having($field, $op = null, $val = null, $escape = true) { |
|
| 637 | + if (is_array($op)) { |
|
| 638 | 638 | $this->having = $this->getHavingStrIfOperatorIsArray($field, $op, $escape); |
| 639 | 639 | } |
| 640 | - else if (! in_array($op, $this->operatorList)){ |
|
| 641 | - if (is_null($op)){ |
|
| 640 | + else if (!in_array($op, $this->operatorList)) { |
|
| 641 | + if (is_null($op)) { |
|
| 642 | 642 | $op = ''; |
| 643 | 643 | } |
| 644 | 644 | $this->having = $field . ' > ' . ($this->escape($op, $escape)); |
| 645 | 645 | } |
| 646 | - else{ |
|
| 647 | - if (is_null($val)){ |
|
| 646 | + else { |
|
| 647 | + if (is_null($val)) { |
|
| 648 | 648 | $val = ''; |
| 649 | 649 | } |
| 650 | 650 | $this->having = $field . ' ' . $op . ' ' . ($this->escape($val, $escape)); |
@@ -658,7 +658,7 @@ discard block |
||
| 658 | 658 | * @param boolean $escape whether to escape or not the values |
| 659 | 659 | * @return object the current DatabaseQueryBuilder instance |
| 660 | 660 | */ |
| 661 | - public function insert($data = array(), $escape = true){ |
|
| 661 | + public function insert($data = array(), $escape = true) { |
|
| 662 | 662 | $columns = array_keys($data); |
| 663 | 663 | $column = implode(',', $columns); |
| 664 | 664 | $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
@@ -673,22 +673,22 @@ discard block |
||
| 673 | 673 | * @param boolean $escape whether to escape or not the values |
| 674 | 674 | * @return object the current DatabaseQueryBuilder instance |
| 675 | 675 | */ |
| 676 | - public function update($data = array(), $escape = true){ |
|
| 676 | + public function update($data = array(), $escape = true) { |
|
| 677 | 677 | $query = 'UPDATE ' . $this->from . ' SET '; |
| 678 | 678 | $values = array(); |
| 679 | - foreach ($data as $column => $val){ |
|
| 679 | + foreach ($data as $column => $val) { |
|
| 680 | 680 | $values[] = $column . ' = ' . ($this->escape($val, $escape)); |
| 681 | 681 | } |
| 682 | 682 | $query .= implode(', ', $values); |
| 683 | - if (! empty($this->where)){ |
|
| 683 | + if (!empty($this->where)) { |
|
| 684 | 684 | $query .= ' WHERE ' . $this->where; |
| 685 | 685 | } |
| 686 | 686 | |
| 687 | - if (! empty($this->orderBy)){ |
|
| 687 | + if (!empty($this->orderBy)) { |
|
| 688 | 688 | $query .= ' ORDER BY ' . $this->orderBy; |
| 689 | 689 | } |
| 690 | 690 | |
| 691 | - if (! empty($this->limit)){ |
|
| 691 | + if (!empty($this->limit)) { |
|
| 692 | 692 | $query .= ' LIMIT ' . $this->limit; |
| 693 | 693 | } |
| 694 | 694 | $this->query = $query; |
@@ -699,22 +699,22 @@ discard block |
||
| 699 | 699 | * Delete the record in database |
| 700 | 700 | * @return object the current DatabaseQueryBuilder instance |
| 701 | 701 | */ |
| 702 | - public function delete(){ |
|
| 702 | + public function delete() { |
|
| 703 | 703 | $query = 'DELETE FROM ' . $this->from; |
| 704 | 704 | $isTruncate = $query; |
| 705 | - if (! empty($this->where)){ |
|
| 705 | + if (!empty($this->where)) { |
|
| 706 | 706 | $query .= ' WHERE ' . $this->where; |
| 707 | 707 | } |
| 708 | 708 | |
| 709 | - if (! empty($this->orderBy)){ |
|
| 709 | + if (!empty($this->orderBy)) { |
|
| 710 | 710 | $query .= ' ORDER BY ' . $this->orderBy; |
| 711 | 711 | } |
| 712 | 712 | |
| 713 | - if (! empty($this->limit)){ |
|
| 713 | + if (!empty($this->limit)) { |
|
| 714 | 714 | $query .= ' LIMIT ' . $this->limit; |
| 715 | 715 | } |
| 716 | 716 | |
| 717 | - if ($isTruncate == $query && $this->driver != 'sqlite'){ |
|
| 717 | + if ($isTruncate == $query && $this->driver != 'sqlite') { |
|
| 718 | 718 | $query = 'TRUNCATE TABLE ' . $this->from; |
| 719 | 719 | } |
| 720 | 720 | $this->query = $query; |
@@ -727,7 +727,7 @@ discard block |
||
| 727 | 727 | * @param boolean $escaped whether we can do escape of not |
| 728 | 728 | * @return mixed the data after escaped or the same data if not |
| 729 | 729 | */ |
| 730 | - public function escape($data, $escaped = true){ |
|
| 730 | + public function escape($data, $escaped = true) { |
|
| 731 | 731 | return $escaped |
| 732 | 732 | ? $this->getPdo()->quote(trim($data)) |
| 733 | 733 | : $data; |
@@ -738,31 +738,31 @@ discard block |
||
| 738 | 738 | * Return the current SQL query string |
| 739 | 739 | * @return string |
| 740 | 740 | */ |
| 741 | - public function getQuery(){ |
|
| 741 | + public function getQuery() { |
|
| 742 | 742 | //INSERT, UPDATE, DELETE already set it, if is the SELECT we need set it now |
| 743 | - if(empty($this->query)){ |
|
| 743 | + if (empty($this->query)) { |
|
| 744 | 744 | $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
| 745 | - if (! empty($this->join)){ |
|
| 745 | + if (!empty($this->join)) { |
|
| 746 | 746 | $query .= $this->join; |
| 747 | 747 | } |
| 748 | 748 | |
| 749 | - if (! empty($this->where)){ |
|
| 749 | + if (!empty($this->where)) { |
|
| 750 | 750 | $query .= ' WHERE ' . $this->where; |
| 751 | 751 | } |
| 752 | 752 | |
| 753 | - if (! empty($this->groupBy)){ |
|
| 753 | + if (!empty($this->groupBy)) { |
|
| 754 | 754 | $query .= ' GROUP BY ' . $this->groupBy; |
| 755 | 755 | } |
| 756 | 756 | |
| 757 | - if (! empty($this->having)){ |
|
| 757 | + if (!empty($this->having)) { |
|
| 758 | 758 | $query .= ' HAVING ' . $this->having; |
| 759 | 759 | } |
| 760 | 760 | |
| 761 | - if (! empty($this->orderBy)){ |
|
| 761 | + if (!empty($this->orderBy)) { |
|
| 762 | 762 | $query .= ' ORDER BY ' . $this->orderBy; |
| 763 | 763 | } |
| 764 | 764 | |
| 765 | - if (! empty($this->limit)){ |
|
| 765 | + if (!empty($this->limit)) { |
|
| 766 | 766 | $query .= ' LIMIT ' . $this->limit; |
| 767 | 767 | } |
| 768 | 768 | $this->query = $query; |
@@ -775,7 +775,7 @@ discard block |
||
| 775 | 775 | * Return the PDO instance |
| 776 | 776 | * @return PDO |
| 777 | 777 | */ |
| 778 | - public function getPdo(){ |
|
| 778 | + public function getPdo() { |
|
| 779 | 779 | return $this->pdo; |
| 780 | 780 | } |
| 781 | 781 | |
@@ -784,7 +784,7 @@ discard block |
||
| 784 | 784 | * @param PDO $pdo the pdo object |
| 785 | 785 | * @return object DatabaseQueryBuilder |
| 786 | 786 | */ |
| 787 | - public function setPdo(PDO $pdo = null){ |
|
| 787 | + public function setPdo(PDO $pdo = null) { |
|
| 788 | 788 | $this->pdo = $pdo; |
| 789 | 789 | return $this; |
| 790 | 790 | } |
@@ -793,7 +793,7 @@ discard block |
||
| 793 | 793 | * Return the database table prefix |
| 794 | 794 | * @return string |
| 795 | 795 | */ |
| 796 | - public function getPrefix(){ |
|
| 796 | + public function getPrefix() { |
|
| 797 | 797 | return $this->prefix; |
| 798 | 798 | } |
| 799 | 799 | |
@@ -802,7 +802,7 @@ discard block |
||
| 802 | 802 | * @param string $prefix the new prefix |
| 803 | 803 | * @return object DatabaseQueryBuilder |
| 804 | 804 | */ |
| 805 | - public function setPrefix($prefix){ |
|
| 805 | + public function setPrefix($prefix) { |
|
| 806 | 806 | $this->prefix = $prefix; |
| 807 | 807 | return $this; |
| 808 | 808 | } |
@@ -811,7 +811,7 @@ discard block |
||
| 811 | 811 | * Return the database driver |
| 812 | 812 | * @return string |
| 813 | 813 | */ |
| 814 | - public function getDriver(){ |
|
| 814 | + public function getDriver() { |
|
| 815 | 815 | return $this->driver; |
| 816 | 816 | } |
| 817 | 817 | |
@@ -820,7 +820,7 @@ discard block |
||
| 820 | 820 | * @param string $driver the new driver |
| 821 | 821 | * @return object DatabaseQueryBuilder |
| 822 | 822 | */ |
| 823 | - public function setDriver($driver){ |
|
| 823 | + public function setDriver($driver) { |
|
| 824 | 824 | $this->driver = $driver; |
| 825 | 825 | return $this; |
| 826 | 826 | } |
@@ -829,7 +829,7 @@ discard block |
||
| 829 | 829 | * Reset the DatabaseQueryBuilder class attributs to the initial values before each query. |
| 830 | 830 | * @return object the current DatabaseQueryBuilder instance |
| 831 | 831 | */ |
| 832 | - public function reset(){ |
|
| 832 | + public function reset() { |
|
| 833 | 833 | $this->select = '*'; |
| 834 | 834 | $this->from = null; |
| 835 | 835 | $this->where = null; |
@@ -848,12 +848,12 @@ discard block |
||
| 848 | 848 | * |
| 849 | 849 | * @return string |
| 850 | 850 | */ |
| 851 | - protected function getHavingStrIfOperatorIsArray($field, $op = null, $escape = true){ |
|
| 851 | + protected function getHavingStrIfOperatorIsArray($field, $op = null, $escape = true) { |
|
| 852 | 852 | $x = explode('?', $field); |
| 853 | 853 | $w = ''; |
| 854 | - foreach($x as $k => $v){ |
|
| 855 | - if (!empty($v)){ |
|
| 856 | - if (! isset($op[$k])){ |
|
| 854 | + foreach ($x as $k => $v) { |
|
| 855 | + if (!empty($v)) { |
|
| 856 | + if (!isset($op[$k])) { |
|
| 857 | 857 | $op[$k] = ''; |
| 858 | 858 | } |
| 859 | 859 | $w .= $v . (isset($op[$k]) ? $this->escape($op[$k], $escape) : ''); |
@@ -869,15 +869,15 @@ discard block |
||
| 869 | 869 | * |
| 870 | 870 | * @return string |
| 871 | 871 | */ |
| 872 | - protected function getWhereStrIfIsArray(array $where, $type = '', $andOr = 'AND', $escape = true){ |
|
| 872 | + protected function getWhereStrIfIsArray(array $where, $type = '', $andOr = 'AND', $escape = true) { |
|
| 873 | 873 | $_where = array(); |
| 874 | - foreach ($where as $column => $data){ |
|
| 875 | - if (is_null($data)){ |
|
| 874 | + foreach ($where as $column => $data) { |
|
| 875 | + if (is_null($data)) { |
|
| 876 | 876 | $data = ''; |
| 877 | 877 | } |
| 878 | 878 | $_where[] = $type . $column . ' = ' . ($this->escape($data, $escape)); |
| 879 | 879 | } |
| 880 | - $where = implode(' '.$andOr.' ', $_where); |
|
| 880 | + $where = implode(' ' . $andOr . ' ', $_where); |
|
| 881 | 881 | return $where; |
| 882 | 882 | } |
| 883 | 883 | |
@@ -887,12 +887,12 @@ discard block |
||
| 887 | 887 | * |
| 888 | 888 | * @return string |
| 889 | 889 | */ |
| 890 | - protected function getWhereStrIfOperatorIsArray($where, array $op, $type = '', $escape = true){ |
|
| 890 | + protected function getWhereStrIfOperatorIsArray($where, array $op, $type = '', $escape = true) { |
|
| 891 | 891 | $x = explode('?', $where); |
| 892 | 892 | $w = ''; |
| 893 | - foreach($x as $k => $v){ |
|
| 894 | - if (! empty($v)){ |
|
| 895 | - if (isset($op[$k]) && is_null($op[$k])){ |
|
| 893 | + foreach ($x as $k => $v) { |
|
| 894 | + if (!empty($v)) { |
|
| 895 | + if (isset($op[$k]) && is_null($op[$k])) { |
|
| 896 | 896 | $op[$k] = ''; |
| 897 | 897 | } |
| 898 | 898 | $w .= $type . $v . (isset($op[$k]) ? ($this->escape($op[$k], $escape)) : ''); |
@@ -907,15 +907,15 @@ discard block |
||
| 907 | 907 | * |
| 908 | 908 | * @return string |
| 909 | 909 | */ |
| 910 | - protected function getWhereStrForOperator($where, $op = null, $val = null, $type = '', $escape = true){ |
|
| 910 | + protected function getWhereStrForOperator($where, $op = null, $val = null, $type = '', $escape = true) { |
|
| 911 | 911 | $w = ''; |
| 912 | - if (! in_array((string)$op, $this->operatorList)){ |
|
| 913 | - if (is_null($op)){ |
|
| 912 | + if (!in_array((string) $op, $this->operatorList)) { |
|
| 913 | + if (is_null($op)) { |
|
| 914 | 914 | $op = ''; |
| 915 | 915 | } |
| 916 | 916 | $w = $type . $where . ' = ' . ($this->escape($op, $escape)); |
| 917 | 917 | } else { |
| 918 | - if (is_null($val)){ |
|
| 918 | + if (is_null($val)) { |
|
| 919 | 919 | $val = ''; |
| 920 | 920 | } |
| 921 | 921 | $w = $type . $where . $op . ($this->escape($val, $escape)); |
@@ -928,14 +928,14 @@ discard block |
||
| 928 | 928 | * @param string $whereStr the WHERE clause string |
| 929 | 929 | * @param string $andOr the separator type used 'AND', 'OR', etc. |
| 930 | 930 | */ |
| 931 | - protected function setWhereStr($whereStr, $andOr = 'AND'){ |
|
| 932 | - if (empty($this->where)){ |
|
| 931 | + protected function setWhereStr($whereStr, $andOr = 'AND') { |
|
| 932 | + if (empty($this->where)) { |
|
| 933 | 933 | $this->where = $whereStr; |
| 934 | 934 | } else { |
| 935 | - if (substr(trim($this->where), -1) == '('){ |
|
| 935 | + if (substr(trim($this->where), -1) == '(') { |
|
| 936 | 936 | $this->where = $this->where . ' ' . $whereStr; |
| 937 | 937 | } else { |
| 938 | - $this->where = $this->where . ' '.$andOr.' ' . $whereStr; |
|
| 938 | + $this->where = $this->where . ' ' . $andOr . ' ' . $whereStr; |
|
| 939 | 939 | } |
| 940 | 940 | } |
| 941 | 941 | } |
@@ -951,7 +951,7 @@ discard block |
||
| 951 | 951 | * @see DatabaseQueryBuilder::avg |
| 952 | 952 | * @return object |
| 953 | 953 | */ |
| 954 | - protected function select_min_max_sum_count_avg($clause, $field, $name = null){ |
|
| 954 | + protected function select_min_max_sum_count_avg($clause, $field, $name = null) { |
|
| 955 | 955 | $clause = strtoupper($clause); |
| 956 | 956 | $func = $clause . '(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
| 957 | 957 | $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
@@ -225,8 +225,7 @@ discard block |
||
| 225 | 225 | } |
| 226 | 226 | if (empty($this->join)){ |
| 227 | 227 | $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 228 | - } |
|
| 229 | - else{ |
|
| 228 | + } else{ |
|
| 230 | 229 | $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 231 | 230 | } |
| 232 | 231 | return $this; |
@@ -334,8 +333,7 @@ discard block |
||
| 334 | 333 | $whereStr = ''; |
| 335 | 334 | if (is_array($where)){ |
| 336 | 335 | $whereStr = $this->getWhereStrIfIsArray($where, $type, $andOr, $escape); |
| 337 | - } |
|
| 338 | - else{ |
|
| 336 | + } else{ |
|
| 339 | 337 | if (is_array($op)){ |
| 340 | 338 | $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
| 341 | 339 | } else { |
@@ -585,8 +583,7 @@ discard block |
||
| 585 | 583 | } |
| 586 | 584 | if (! is_null($limitEnd)){ |
| 587 | 585 | $this->limit = $limit . ', ' . $limitEnd; |
| 588 | - } |
|
| 589 | - else{ |
|
| 586 | + } else{ |
|
| 590 | 587 | $this->limit = $limit; |
| 591 | 588 | } |
| 592 | 589 | return $this; |
@@ -601,8 +598,7 @@ discard block |
||
| 601 | 598 | public function orderBy($orderBy, $orderDir = ' ASC'){ |
| 602 | 599 | if (stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
| 603 | 600 | $this->orderBy = empty($this->orderBy) ? $orderBy : $this->orderBy . ', ' . $orderBy; |
| 604 | - } |
|
| 605 | - else{ |
|
| 601 | + } else{ |
|
| 606 | 602 | $this->orderBy = empty($this->orderBy) |
| 607 | 603 | ? ($orderBy . ' ' . strtoupper($orderDir)) |
| 608 | 604 | : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
@@ -618,8 +614,7 @@ discard block |
||
| 618 | 614 | public function groupBy($field){ |
| 619 | 615 | if (is_array($field)){ |
| 620 | 616 | $this->groupBy = implode(', ', $field); |
| 621 | - } |
|
| 622 | - else{ |
|
| 617 | + } else{ |
|
| 623 | 618 | $this->groupBy = $field; |
| 624 | 619 | } |
| 625 | 620 | return $this; |
@@ -636,14 +631,12 @@ discard block |
||
| 636 | 631 | public function having($field, $op = null, $val = null, $escape = true){ |
| 637 | 632 | if (is_array($op)){ |
| 638 | 633 | $this->having = $this->getHavingStrIfOperatorIsArray($field, $op, $escape); |
| 639 | - } |
|
| 640 | - else if (! in_array($op, $this->operatorList)){ |
|
| 634 | + } else if (! in_array($op, $this->operatorList)){ |
|
| 641 | 635 | if (is_null($op)){ |
| 642 | 636 | $op = ''; |
| 643 | 637 | } |
| 644 | 638 | $this->having = $field . ' > ' . ($this->escape($op, $escape)); |
| 645 | - } |
|
| 646 | - else{ |
|
| 639 | + } else{ |
|
| 647 | 640 | if (is_null($val)){ |
| 648 | 641 | $val = ''; |
| 649 | 642 | } |
@@ -1,807 +1,807 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or 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 | - /** |
|
| 30 | - * Upload |
|
| 31 | - * |
|
| 32 | - * A complete class to upload files with php 5 or higher, but the best: very simple to use. |
|
| 33 | - * |
|
| 34 | - * @author Olaf Erlandsen <[email protected]> |
|
| 35 | - * @author http://www.webdevfreelance.com/ |
|
| 36 | - * |
|
| 37 | - * @package FileUpload |
|
| 38 | - * @version 1.5 |
|
| 39 | - */ |
|
| 40 | - class Upload{ |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Version |
|
| 44 | - * |
|
| 45 | - * @since 1.5 |
|
| 46 | - * @version 1.0 |
|
| 47 | - */ |
|
| 48 | - const VERSION = '1.5'; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Upload function name |
|
| 52 | - * Remember: |
|
| 53 | - * Default function: move_uploaded_file |
|
| 54 | - * Native options: |
|
| 55 | - * - move_uploaded_file (Default and best option) |
|
| 56 | - * - copy |
|
| 57 | - * |
|
| 58 | - * @since 1.0 |
|
| 59 | - * @version 1.0 |
|
| 60 | - * @var string |
|
| 61 | - */ |
|
| 62 | - private $upload_function = 'move_uploaded_file'; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Array with the information obtained from the |
|
| 66 | - * variable $_FILES or $HTTP_POST_FILES. |
|
| 67 | - * |
|
| 68 | - * @since 1.0 |
|
| 69 | - * @version 1.0 |
|
| 70 | - * @var array |
|
| 71 | - */ |
|
| 72 | - private $file_array = array(); |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * If the file you are trying to upload already exists it will |
|
| 76 | - * be overwritten if you set the variable to true. |
|
| 77 | - * |
|
| 78 | - * @since 1.0 |
|
| 79 | - * @version 1.0 |
|
| 80 | - * @var boolean |
|
| 81 | - */ |
|
| 82 | - private $overwrite_file = false; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Input element |
|
| 86 | - * Example: |
|
| 87 | - * <input type="file" name="file" /> |
|
| 88 | - * Result: |
|
| 89 | - * FileUpload::$input = file |
|
| 90 | - * |
|
| 91 | - * @since 1.0 |
|
| 92 | - * @version 1.0 |
|
| 93 | - * @var string |
|
| 94 | - */ |
|
| 95 | - private $input; |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Path output |
|
| 99 | - * |
|
| 100 | - * @since 1.0 |
|
| 101 | - * @version 1.0 |
|
| 102 | - * @var string |
|
| 103 | - */ |
|
| 104 | - private $destination_directory; |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Output filename |
|
| 108 | - * |
|
| 109 | - * @since 1.0 |
|
| 110 | - * @version 1.0 |
|
| 111 | - * @var string |
|
| 112 | - */ |
|
| 113 | - private $filename; |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Max file size |
|
| 117 | - * |
|
| 118 | - * @since 1.0 |
|
| 119 | - * @version 1.0 |
|
| 120 | - * @var float |
|
| 121 | - */ |
|
| 122 | - private $max_file_size= 0.0; |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * List of allowed mime types |
|
| 126 | - * |
|
| 127 | - * @since 1.0 |
|
| 128 | - * @version 1.0 |
|
| 129 | - * @var array |
|
| 130 | - */ |
|
| 131 | - private $allowed_mime_types = array(); |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Callbacks |
|
| 135 | - * |
|
| 136 | - * @since 1.0 |
|
| 137 | - * @version 1.0 |
|
| 138 | - * @var array |
|
| 139 | - */ |
|
| 140 | - private $callbacks = array('before' => null, 'after' => null); |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * File object |
|
| 144 | - * |
|
| 145 | - * @since 1.0 |
|
| 146 | - * @version 1.0 |
|
| 147 | - * @var object |
|
| 148 | - */ |
|
| 149 | - private $file; |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Helping mime types |
|
| 153 | - * |
|
| 154 | - * @since 1.0 |
|
| 155 | - * @version 1.0 |
|
| 156 | - * @var array |
|
| 157 | - */ |
|
| 158 | - private $mime_helping = array( |
|
| 159 | - 'text' => array('text/plain',), |
|
| 160 | - 'image' => array( |
|
| 161 | - 'image/jpeg', |
|
| 162 | - 'image/jpg', |
|
| 163 | - 'image/pjpeg', |
|
| 164 | - 'image/png', |
|
| 165 | - 'image/gif', |
|
| 166 | - ), |
|
| 167 | - 'document' => array( |
|
| 168 | - 'application/pdf', |
|
| 169 | - 'application/msword', |
|
| 170 | - 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
|
| 171 | - 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
|
| 172 | - 'application/vnd.ms-powerpoint', |
|
| 173 | - 'application/vnd.ms-excel', |
|
| 174 | - 'application/vnd.oasis.opendocument.spreadsheet', |
|
| 175 | - 'application/vnd.oasis.opendocument.presentation', |
|
| 176 | - ), |
|
| 177 | - 'video' => array( |
|
| 178 | - 'video/3gpp', |
|
| 179 | - 'video/3gpp', |
|
| 180 | - 'video/x-msvideo', |
|
| 181 | - 'video/avi', |
|
| 182 | - 'video/mpeg4', |
|
| 183 | - 'video/mp4', |
|
| 184 | - 'video/mpeg', |
|
| 185 | - 'video/mpg', |
|
| 186 | - 'video/quicktime', |
|
| 187 | - 'video/x-sgi-movie', |
|
| 188 | - 'video/x-ms-wmv', |
|
| 189 | - 'video/x-flv', |
|
| 190 | - ), |
|
| 191 | - ); |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * The upload error message |
|
| 195 | - * @var array |
|
| 196 | - */ |
|
| 197 | - public $error_messages = array(); |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * The upload error message |
|
| 201 | - * @var string |
|
| 202 | - */ |
|
| 203 | - protected $error = null; |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * The logger instance |
|
| 207 | - * @var Log |
|
| 208 | - */ |
|
| 209 | - private $logger; |
|
| 210 | - |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Construct |
|
| 214 | - * |
|
| 215 | - * @since 0.1 |
|
| 216 | - * @version 1.0.1 |
|
| 217 | - * @return object |
|
| 218 | - * @method object __construct |
|
| 219 | - */ |
|
| 220 | - public function __construct(){ |
|
| 221 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 222 | - $this->logger->setLogger('Library::Upload'); |
|
| 223 | - |
|
| 224 | - Loader::lang('file_upload'); |
|
| 225 | - $obj =& get_instance(); |
|
| 226 | - |
|
| 227 | - $this->error_messages = array( |
|
| 228 | - 'upload_err_ini_size' => $obj->lang->get('fu_upload_err_ini_size'), |
|
| 229 | - 'upload_err_form_size' => $obj->lang->get('fu_upload_err_form_size'), |
|
| 230 | - 'upload_err_partial' => $obj->lang->get('fu_upload_err_partial'), |
|
| 231 | - 'upload_err_no_file' => $obj->lang->get('fu_upload_err_no_file'), |
|
| 232 | - 'upload_err_no_tmp_dir' => $obj->lang->get('fu_upload_err_no_tmp_dir'), |
|
| 233 | - 'upload_err_cant_write' => $obj->lang->get('fu_upload_err_cant_write'), |
|
| 234 | - 'upload_err_extension' => $obj->lang->get('fu_upload_err_extension'), |
|
| 235 | - 'accept_file_types' => $obj->lang->get('fu_accept_file_types'), |
|
| 236 | - 'file_uploads' => $obj->lang->get('fu_file_uploads_disabled'), |
|
| 237 | - 'max_file_size' => $obj->lang->get('fu_max_file_size'), |
|
| 238 | - 'overwritten_not_allowed' => $obj->lang->get('fu_overwritten_not_allowed'), |
|
| 239 | - ); |
|
| 240 | - |
|
| 241 | - $this->file = array( |
|
| 242 | - 'status' => false, // True: success upload |
|
| 243 | - 'mime' => '', // Empty string |
|
| 244 | - 'filename' => '', // Empty string |
|
| 245 | - 'original' => '', // Empty string |
|
| 246 | - 'size' => 0, // 0 Bytes |
|
| 247 | - 'sizeFormated' => '0B', // 0 Bytes |
|
| 248 | - 'destination' => './', // Default: ./ |
|
| 249 | - 'allowed_mime_types' => array(), // Allowed mime types |
|
| 250 | - 'error' => null, // File error |
|
| 251 | - ); |
|
| 252 | - |
|
| 253 | - // Change dir to current dir |
|
| 254 | - $this->destination_directory = dirname(__FILE__) . DIRECTORY_SEPARATOR; |
|
| 255 | - |
|
| 256 | - // Set file array |
|
| 257 | - if (isset($_FILES) && is_array($_FILES)) { |
|
| 258 | - $this->file_array = $_FILES; |
|
| 259 | - } |
|
| 260 | - $this->logger->info('The upload file information are : ' .stringfy_vars($this->file_array)); |
|
| 261 | - } |
|
| 262 | - /** |
|
| 263 | - * Set input. |
|
| 264 | - * If you have $_FILES["file"], you must use the key "file" |
|
| 265 | - * Example: |
|
| 266 | - * $object->setInput("file"); |
|
| 267 | - * |
|
| 268 | - * @since 1.0 |
|
| 269 | - * @version 1.0 |
|
| 270 | - * @param string $input |
|
| 271 | - * @return object |
|
| 272 | - * @method boolean setInput |
|
| 273 | - */ |
|
| 274 | - public function setInput($input) |
|
| 275 | - { |
|
| 276 | - if (!empty($input) && (is_string($input) || is_numeric($input) )) { |
|
| 277 | - $this->input = $input; |
|
| 278 | - } |
|
| 279 | - return $this; |
|
| 280 | - } |
|
| 281 | - /** |
|
| 282 | - * Set new filename |
|
| 283 | - * Example: |
|
| 284 | - * FileUpload::setFilename("new file.txt") |
|
| 285 | - * Remember: |
|
| 286 | - * Use %s to retrive file extension |
|
| 287 | - * |
|
| 288 | - * @since 1.0 |
|
| 289 | - * @version 1.0 |
|
| 290 | - * @param string $filename |
|
| 291 | - * @return object |
|
| 292 | - * @method boolean setFilename |
|
| 293 | - */ |
|
| 294 | - public function setFilename($filename) |
|
| 295 | - { |
|
| 296 | - if ($this->isFilename($filename)) { |
|
| 297 | - $this->filename = $filename; |
|
| 298 | - } |
|
| 299 | - return $this; |
|
| 300 | - } |
|
| 301 | - /** |
|
| 302 | - * Set automatic filename |
|
| 303 | - * |
|
| 304 | - * @since 1.0 |
|
| 305 | - * @version 1.5 |
|
| 306 | - * @param string $extension |
|
| 307 | - * @return object |
|
| 308 | - * @method boolean setAutoFilename |
|
| 309 | - */ |
|
| 310 | - public function setAutoFilename() |
|
| 311 | - { |
|
| 312 | - $this->filename = sha1(mt_rand(1, 9999).uniqid()); |
|
| 313 | - $this->filename .= time(); |
|
| 314 | - return $this; |
|
| 315 | - } |
|
| 316 | - /** |
|
| 317 | - * Set file size limit |
|
| 318 | - * |
|
| 319 | - * @since 1.0 |
|
| 320 | - * @version 1.0 |
|
| 321 | - * @param double $file_size |
|
| 322 | - * @return object |
|
| 323 | - * @method boolean setMaxFileSize |
|
| 324 | - */ |
|
| 325 | - public function setMaxFileSize($file_size) |
|
| 326 | - { |
|
| 327 | - $file_size = $this->sizeInBytes($file_size); |
|
| 328 | - if (is_numeric($file_size) && $file_size > -1) { |
|
| 329 | - // Get php config |
|
| 330 | - $php_size = $this->sizeInBytes((int) ini_get('upload_max_filesize')); |
|
| 331 | - // Calculate difference |
|
| 332 | - if ($php_size < $file_size) { |
|
| 333 | - $this->logger->warning('The upload max file size you set [' .$file_size. '] is greather than the PHP configuration for upload max file size [' .$php_size. ']'); |
|
| 334 | - } |
|
| 335 | - $this->max_file_size = $file_size; |
|
| 336 | - } |
|
| 337 | - return $this; |
|
| 338 | - } |
|
| 339 | - /** |
|
| 340 | - * Set array mime types |
|
| 341 | - * |
|
| 342 | - * @since 1.0 |
|
| 343 | - * @version 1.0 |
|
| 344 | - * @param array $mimes |
|
| 345 | - * @return object |
|
| 346 | - * @method boolean setAllowedMimeTypes |
|
| 347 | - */ |
|
| 348 | - public function setAllowedMimeTypes(array $mimes) |
|
| 349 | - { |
|
| 350 | - if (count($mimes) > 0) { |
|
| 351 | - array_map(array($this , 'setAllowMimeType'), $mimes); |
|
| 352 | - } |
|
| 353 | - return $this; |
|
| 354 | - } |
|
| 355 | - /** |
|
| 356 | - * Set input callback |
|
| 357 | - * |
|
| 358 | - * @since 1.0 |
|
| 359 | - * @version 1.0 |
|
| 360 | - * @param mixed $callback |
|
| 361 | - * @return object |
|
| 362 | - * @method boolean setCallbackInput |
|
| 363 | - */ |
|
| 364 | - public function setCallbackInput($callback) |
|
| 365 | - { |
|
| 366 | - if (is_callable($callback, false)) { |
|
| 367 | - $this->callbacks['input'] = $callback; |
|
| 368 | - } |
|
| 369 | - return $this; |
|
| 370 | - } |
|
| 371 | - /** |
|
| 372 | - * Set output callback |
|
| 373 | - * |
|
| 374 | - * @since 1.0 |
|
| 375 | - * @version 1.0 |
|
| 376 | - * @param mixed $callback |
|
| 377 | - * @return object |
|
| 378 | - * @method boolean setCallbackOutput |
|
| 379 | - */ |
|
| 380 | - public function setCallbackOutput($callback) |
|
| 381 | - { |
|
| 382 | - if (is_callable($callback, false)) { |
|
| 383 | - $this->callbacks['output'] = $callback; |
|
| 384 | - } |
|
| 385 | - return $this; |
|
| 386 | - } |
|
| 387 | - /** |
|
| 388 | - * Append a mime type to allowed mime types |
|
| 389 | - * |
|
| 390 | - * @since 1.0 |
|
| 391 | - * @version 1.0.1 |
|
| 392 | - * @param string $mime |
|
| 393 | - * @return object |
|
| 394 | - * @method boolean setAllowMimeType |
|
| 395 | - */ |
|
| 396 | - public function setAllowMimeType($mime) |
|
| 397 | - { |
|
| 398 | - if (!empty($mime) && is_string($mime)) { |
|
| 399 | - $this->allowed_mime_types[] = strtolower($mime); |
|
| 400 | - $this->file['allowed_mime_types'][] = strtolower($mime); |
|
| 401 | - } |
|
| 402 | - return $this; |
|
| 403 | - } |
|
| 404 | - /** |
|
| 405 | - * Set allowed mime types from mime helping |
|
| 406 | - * |
|
| 407 | - * @since 1.0.1 |
|
| 408 | - * @version 1.0.1 |
|
| 409 | - * @return object |
|
| 410 | - * @method boolean setMimeHelping |
|
| 411 | - */ |
|
| 412 | - public function setMimeHelping($name) |
|
| 413 | - { |
|
| 414 | - if (!empty($name) && is_string($name)) { |
|
| 415 | - if (array_key_exists($name, $this->mime_helping)) { |
|
| 416 | - return $this->setAllowedMimeTypes($this->mime_helping[ $name ]); |
|
| 417 | - } |
|
| 418 | - } |
|
| 419 | - return $this; |
|
| 420 | - } |
|
| 421 | - /** |
|
| 422 | - * Set function to upload file |
|
| 423 | - * Examples: |
|
| 424 | - * 1.- FileUpload::setUploadFunction("move_uploaded_file"); |
|
| 425 | - * 2.- FileUpload::setUploadFunction("copy"); |
|
| 426 | - * |
|
| 427 | - * @since 1.0 |
|
| 428 | - * @version 1.0 |
|
| 429 | - * @param string $function |
|
| 430 | - * @return object |
|
| 431 | - * @method boolean setUploadFunction |
|
| 432 | - */ |
|
| 433 | - public function setUploadFunction($function) |
|
| 434 | - { |
|
| 435 | - if (!empty($function) && (is_array($function) || is_string($function) )) { |
|
| 436 | - if (is_callable( $function)) { |
|
| 437 | - $this->upload_function = $function; |
|
| 438 | - } |
|
| 439 | - } |
|
| 440 | - return $this; |
|
| 441 | - } |
|
| 442 | - /** |
|
| 443 | - * Clear allowed mime types cache |
|
| 444 | - * |
|
| 445 | - * @since 1.0 |
|
| 446 | - * @version 1.0 |
|
| 447 | - * @return object |
|
| 448 | - * @method boolean clearAllowedMimeTypes |
|
| 449 | - */ |
|
| 450 | - public function clearAllowedMimeTypes() |
|
| 451 | - { |
|
| 452 | - $this->allowed_mime_types = array(); |
|
| 453 | - $this->file['allowed_mime_types'] = array(); |
|
| 454 | - return $this; |
|
| 455 | - } |
|
| 456 | - /** |
|
| 457 | - * Set destination output |
|
| 458 | - * |
|
| 459 | - * @since 1.0 |
|
| 460 | - * @version 1.0 |
|
| 461 | - * @param string $destination_directory Destination path |
|
| 462 | - * @param boolean $create_if_not_exist |
|
| 463 | - * @return object |
|
| 464 | - * @method boolean setDestinationDirectory |
|
| 465 | - */ |
|
| 466 | - public function setDestinationDirectory($destination_directory, $create_if_not_exist = false) { |
|
| 467 | - $destination_directory = realpath($destination_directory); |
|
| 468 | - if (substr($destination_directory, -1) != DIRECTORY_SEPARATOR) { |
|
| 469 | - $destination_directory .= DIRECTORY_SEPARATOR; |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - if ($this->isDirpath($destination_directory)) { |
|
| 473 | - if ($this->dirExists($destination_directory)) { |
|
| 474 | - $this->destination_directory = $destination_directory; |
|
| 475 | - chdir($destination_directory); |
|
| 476 | - } else if ($create_if_not_exist === true) { |
|
| 477 | - if (mkdir($destination_directory, 0775, true)) { |
|
| 478 | - $this->destination_directory = $destination_directory; |
|
| 479 | - chdir($destination_directory); |
|
| 480 | - } |
|
| 481 | - else{ |
|
| 482 | - $this->logger->warning('Can not create the upload directory [' .$destination_directory. ']'); |
|
| 483 | - } |
|
| 484 | - } |
|
| 485 | - } |
|
| 486 | - return $this; |
|
| 487 | - } |
|
| 488 | - /** |
|
| 489 | - * Check file exists |
|
| 490 | - * |
|
| 491 | - * @since 1.0 |
|
| 492 | - * @version 1.0.1 |
|
| 493 | - * @param string $file_destination |
|
| 494 | - * @return boolean |
|
| 495 | - * @method boolean fileExists |
|
| 496 | - */ |
|
| 497 | - public function fileExists($file_destination) |
|
| 498 | - { |
|
| 499 | - if ($this->isFilename($file_destination)) { |
|
| 500 | - return (file_exists($file_destination) && is_file($file_destination)); |
|
| 501 | - } |
|
| 502 | - return false; |
|
| 503 | - } |
|
| 504 | - /** |
|
| 505 | - * Check dir exists |
|
| 506 | - * |
|
| 507 | - * @since 1.0 |
|
| 508 | - * @version 1.0.1 |
|
| 509 | - * @param string $path |
|
| 510 | - * @return boolean |
|
| 511 | - * @method boolean dirExists |
|
| 512 | - */ |
|
| 513 | - public function dirExists($path) |
|
| 514 | - { |
|
| 515 | - if ($this->isDirpath($path)) { |
|
| 516 | - return (file_exists($path) && is_dir($path)); |
|
| 517 | - } |
|
| 518 | - return false; |
|
| 519 | - } |
|
| 520 | - /** |
|
| 521 | - * Check valid filename |
|
| 522 | - * |
|
| 523 | - * @since 1.0 |
|
| 524 | - * @version 1.0.1 |
|
| 525 | - * @param string $filename |
|
| 526 | - * @return boolean |
|
| 527 | - * @method boolean isFilename |
|
| 528 | - */ |
|
| 529 | - public function isFilename($filename) |
|
| 530 | - { |
|
| 531 | - $filename = basename($filename); |
|
| 532 | - return (!empty($filename) && (is_string( $filename) || is_numeric($filename))); |
|
| 533 | - } |
|
| 534 | - /** |
|
| 535 | - * Validate mime type with allowed mime types, |
|
| 536 | - * but if allowed mime types is empty, this method return true |
|
| 537 | - * |
|
| 538 | - * @since 1.0 |
|
| 539 | - * @version 1.0 |
|
| 540 | - * @param string $mime |
|
| 541 | - * @return boolean |
|
| 542 | - * @method boolean checkMimeType |
|
| 543 | - */ |
|
| 544 | - public function checkMimeType($mime) |
|
| 545 | - { |
|
| 546 | - if (count($this->allowed_mime_types) == 0) { |
|
| 547 | - return true; |
|
| 548 | - } |
|
| 549 | - return in_array(strtolower($mime), $this->allowed_mime_types); |
|
| 550 | - } |
|
| 551 | - /** |
|
| 552 | - * Retrive status of upload |
|
| 553 | - * |
|
| 554 | - * @since 1.0 |
|
| 555 | - * @version 1.0 |
|
| 556 | - * @return boolean |
|
| 557 | - * @method boolean getStatus |
|
| 558 | - */ |
|
| 559 | - public function getStatus() |
|
| 560 | - { |
|
| 561 | - return $this->file['status']; |
|
| 562 | - } |
|
| 563 | - /** |
|
| 564 | - * Check valid path |
|
| 565 | - * |
|
| 566 | - * @since 1.0 |
|
| 567 | - * @version 1.0.1 |
|
| 568 | - * @param string $filename |
|
| 569 | - * @return boolean |
|
| 570 | - * @method boolean isDirpath |
|
| 571 | - */ |
|
| 572 | - public function isDirpath($path) |
|
| 573 | - { |
|
| 574 | - if (!empty( $path) && (is_string( $path) || is_numeric($path) )) { |
|
| 575 | - if (DIRECTORY_SEPARATOR == '/') { |
|
| 576 | - return (preg_match( '/^[^*?"<>|:]*$/' , $path) == 1 ); |
|
| 577 | - } else { |
|
| 578 | - return (preg_match( "/^[^*?\"<>|:]*$/" , substr($path,2) ) == 1); |
|
| 579 | - } |
|
| 580 | - } |
|
| 581 | - return false; |
|
| 582 | - } |
|
| 583 | - /** |
|
| 584 | - * Allow overwriting files |
|
| 585 | - * |
|
| 586 | - * @since 1.0 |
|
| 587 | - * @version 1.0 |
|
| 588 | - * @return object |
|
| 589 | - * @method boolean allowOverwriting |
|
| 590 | - */ |
|
| 591 | - public function allowOverwriting() |
|
| 592 | - { |
|
| 593 | - $this->overwrite_file = true; |
|
| 594 | - return $this; |
|
| 595 | - } |
|
| 596 | - /** |
|
| 597 | - * File info |
|
| 598 | - * |
|
| 599 | - * @since 1.0 |
|
| 600 | - * @version 1.0 |
|
| 601 | - * @return object |
|
| 602 | - * @method object getInfo |
|
| 603 | - */ |
|
| 604 | - public function getInfo() |
|
| 605 | - { |
|
| 606 | - return (object)$this->file; |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - |
|
| 610 | - /** |
|
| 611 | - * Check if the file is uploaded |
|
| 612 | - * @return boolean |
|
| 613 | - */ |
|
| 614 | - public function isUploaded(){ |
|
| 615 | - return isset($this->file_array[$this->input]) |
|
| 616 | - && is_uploaded_file($this->file_array[$this->input]['tmp_name']); |
|
| 617 | - } |
|
| 2 | + defined('ROOT_PATH') or 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 | + /** |
|
| 30 | + * Upload |
|
| 31 | + * |
|
| 32 | + * A complete class to upload files with php 5 or higher, but the best: very simple to use. |
|
| 33 | + * |
|
| 34 | + * @author Olaf Erlandsen <[email protected]> |
|
| 35 | + * @author http://www.webdevfreelance.com/ |
|
| 36 | + * |
|
| 37 | + * @package FileUpload |
|
| 38 | + * @version 1.5 |
|
| 39 | + */ |
|
| 40 | + class Upload{ |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Version |
|
| 44 | + * |
|
| 45 | + * @since 1.5 |
|
| 46 | + * @version 1.0 |
|
| 47 | + */ |
|
| 48 | + const VERSION = '1.5'; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Upload function name |
|
| 52 | + * Remember: |
|
| 53 | + * Default function: move_uploaded_file |
|
| 54 | + * Native options: |
|
| 55 | + * - move_uploaded_file (Default and best option) |
|
| 56 | + * - copy |
|
| 57 | + * |
|
| 58 | + * @since 1.0 |
|
| 59 | + * @version 1.0 |
|
| 60 | + * @var string |
|
| 61 | + */ |
|
| 62 | + private $upload_function = 'move_uploaded_file'; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Array with the information obtained from the |
|
| 66 | + * variable $_FILES or $HTTP_POST_FILES. |
|
| 67 | + * |
|
| 68 | + * @since 1.0 |
|
| 69 | + * @version 1.0 |
|
| 70 | + * @var array |
|
| 71 | + */ |
|
| 72 | + private $file_array = array(); |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * If the file you are trying to upload already exists it will |
|
| 76 | + * be overwritten if you set the variable to true. |
|
| 77 | + * |
|
| 78 | + * @since 1.0 |
|
| 79 | + * @version 1.0 |
|
| 80 | + * @var boolean |
|
| 81 | + */ |
|
| 82 | + private $overwrite_file = false; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Input element |
|
| 86 | + * Example: |
|
| 87 | + * <input type="file" name="file" /> |
|
| 88 | + * Result: |
|
| 89 | + * FileUpload::$input = file |
|
| 90 | + * |
|
| 91 | + * @since 1.0 |
|
| 92 | + * @version 1.0 |
|
| 93 | + * @var string |
|
| 94 | + */ |
|
| 95 | + private $input; |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Path output |
|
| 99 | + * |
|
| 100 | + * @since 1.0 |
|
| 101 | + * @version 1.0 |
|
| 102 | + * @var string |
|
| 103 | + */ |
|
| 104 | + private $destination_directory; |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Output filename |
|
| 108 | + * |
|
| 109 | + * @since 1.0 |
|
| 110 | + * @version 1.0 |
|
| 111 | + * @var string |
|
| 112 | + */ |
|
| 113 | + private $filename; |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Max file size |
|
| 117 | + * |
|
| 118 | + * @since 1.0 |
|
| 119 | + * @version 1.0 |
|
| 120 | + * @var float |
|
| 121 | + */ |
|
| 122 | + private $max_file_size= 0.0; |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * List of allowed mime types |
|
| 126 | + * |
|
| 127 | + * @since 1.0 |
|
| 128 | + * @version 1.0 |
|
| 129 | + * @var array |
|
| 130 | + */ |
|
| 131 | + private $allowed_mime_types = array(); |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Callbacks |
|
| 135 | + * |
|
| 136 | + * @since 1.0 |
|
| 137 | + * @version 1.0 |
|
| 138 | + * @var array |
|
| 139 | + */ |
|
| 140 | + private $callbacks = array('before' => null, 'after' => null); |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * File object |
|
| 144 | + * |
|
| 145 | + * @since 1.0 |
|
| 146 | + * @version 1.0 |
|
| 147 | + * @var object |
|
| 148 | + */ |
|
| 149 | + private $file; |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Helping mime types |
|
| 153 | + * |
|
| 154 | + * @since 1.0 |
|
| 155 | + * @version 1.0 |
|
| 156 | + * @var array |
|
| 157 | + */ |
|
| 158 | + private $mime_helping = array( |
|
| 159 | + 'text' => array('text/plain',), |
|
| 160 | + 'image' => array( |
|
| 161 | + 'image/jpeg', |
|
| 162 | + 'image/jpg', |
|
| 163 | + 'image/pjpeg', |
|
| 164 | + 'image/png', |
|
| 165 | + 'image/gif', |
|
| 166 | + ), |
|
| 167 | + 'document' => array( |
|
| 168 | + 'application/pdf', |
|
| 169 | + 'application/msword', |
|
| 170 | + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
|
| 171 | + 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
|
| 172 | + 'application/vnd.ms-powerpoint', |
|
| 173 | + 'application/vnd.ms-excel', |
|
| 174 | + 'application/vnd.oasis.opendocument.spreadsheet', |
|
| 175 | + 'application/vnd.oasis.opendocument.presentation', |
|
| 176 | + ), |
|
| 177 | + 'video' => array( |
|
| 178 | + 'video/3gpp', |
|
| 179 | + 'video/3gpp', |
|
| 180 | + 'video/x-msvideo', |
|
| 181 | + 'video/avi', |
|
| 182 | + 'video/mpeg4', |
|
| 183 | + 'video/mp4', |
|
| 184 | + 'video/mpeg', |
|
| 185 | + 'video/mpg', |
|
| 186 | + 'video/quicktime', |
|
| 187 | + 'video/x-sgi-movie', |
|
| 188 | + 'video/x-ms-wmv', |
|
| 189 | + 'video/x-flv', |
|
| 190 | + ), |
|
| 191 | + ); |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * The upload error message |
|
| 195 | + * @var array |
|
| 196 | + */ |
|
| 197 | + public $error_messages = array(); |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * The upload error message |
|
| 201 | + * @var string |
|
| 202 | + */ |
|
| 203 | + protected $error = null; |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * The logger instance |
|
| 207 | + * @var Log |
|
| 208 | + */ |
|
| 209 | + private $logger; |
|
| 210 | + |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Construct |
|
| 214 | + * |
|
| 215 | + * @since 0.1 |
|
| 216 | + * @version 1.0.1 |
|
| 217 | + * @return object |
|
| 218 | + * @method object __construct |
|
| 219 | + */ |
|
| 220 | + public function __construct(){ |
|
| 221 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 222 | + $this->logger->setLogger('Library::Upload'); |
|
| 223 | + |
|
| 224 | + Loader::lang('file_upload'); |
|
| 225 | + $obj =& get_instance(); |
|
| 226 | + |
|
| 227 | + $this->error_messages = array( |
|
| 228 | + 'upload_err_ini_size' => $obj->lang->get('fu_upload_err_ini_size'), |
|
| 229 | + 'upload_err_form_size' => $obj->lang->get('fu_upload_err_form_size'), |
|
| 230 | + 'upload_err_partial' => $obj->lang->get('fu_upload_err_partial'), |
|
| 231 | + 'upload_err_no_file' => $obj->lang->get('fu_upload_err_no_file'), |
|
| 232 | + 'upload_err_no_tmp_dir' => $obj->lang->get('fu_upload_err_no_tmp_dir'), |
|
| 233 | + 'upload_err_cant_write' => $obj->lang->get('fu_upload_err_cant_write'), |
|
| 234 | + 'upload_err_extension' => $obj->lang->get('fu_upload_err_extension'), |
|
| 235 | + 'accept_file_types' => $obj->lang->get('fu_accept_file_types'), |
|
| 236 | + 'file_uploads' => $obj->lang->get('fu_file_uploads_disabled'), |
|
| 237 | + 'max_file_size' => $obj->lang->get('fu_max_file_size'), |
|
| 238 | + 'overwritten_not_allowed' => $obj->lang->get('fu_overwritten_not_allowed'), |
|
| 239 | + ); |
|
| 240 | + |
|
| 241 | + $this->file = array( |
|
| 242 | + 'status' => false, // True: success upload |
|
| 243 | + 'mime' => '', // Empty string |
|
| 244 | + 'filename' => '', // Empty string |
|
| 245 | + 'original' => '', // Empty string |
|
| 246 | + 'size' => 0, // 0 Bytes |
|
| 247 | + 'sizeFormated' => '0B', // 0 Bytes |
|
| 248 | + 'destination' => './', // Default: ./ |
|
| 249 | + 'allowed_mime_types' => array(), // Allowed mime types |
|
| 250 | + 'error' => null, // File error |
|
| 251 | + ); |
|
| 252 | + |
|
| 253 | + // Change dir to current dir |
|
| 254 | + $this->destination_directory = dirname(__FILE__) . DIRECTORY_SEPARATOR; |
|
| 255 | + |
|
| 256 | + // Set file array |
|
| 257 | + if (isset($_FILES) && is_array($_FILES)) { |
|
| 258 | + $this->file_array = $_FILES; |
|
| 259 | + } |
|
| 260 | + $this->logger->info('The upload file information are : ' .stringfy_vars($this->file_array)); |
|
| 261 | + } |
|
| 262 | + /** |
|
| 263 | + * Set input. |
|
| 264 | + * If you have $_FILES["file"], you must use the key "file" |
|
| 265 | + * Example: |
|
| 266 | + * $object->setInput("file"); |
|
| 267 | + * |
|
| 268 | + * @since 1.0 |
|
| 269 | + * @version 1.0 |
|
| 270 | + * @param string $input |
|
| 271 | + * @return object |
|
| 272 | + * @method boolean setInput |
|
| 273 | + */ |
|
| 274 | + public function setInput($input) |
|
| 275 | + { |
|
| 276 | + if (!empty($input) && (is_string($input) || is_numeric($input) )) { |
|
| 277 | + $this->input = $input; |
|
| 278 | + } |
|
| 279 | + return $this; |
|
| 280 | + } |
|
| 281 | + /** |
|
| 282 | + * Set new filename |
|
| 283 | + * Example: |
|
| 284 | + * FileUpload::setFilename("new file.txt") |
|
| 285 | + * Remember: |
|
| 286 | + * Use %s to retrive file extension |
|
| 287 | + * |
|
| 288 | + * @since 1.0 |
|
| 289 | + * @version 1.0 |
|
| 290 | + * @param string $filename |
|
| 291 | + * @return object |
|
| 292 | + * @method boolean setFilename |
|
| 293 | + */ |
|
| 294 | + public function setFilename($filename) |
|
| 295 | + { |
|
| 296 | + if ($this->isFilename($filename)) { |
|
| 297 | + $this->filename = $filename; |
|
| 298 | + } |
|
| 299 | + return $this; |
|
| 300 | + } |
|
| 301 | + /** |
|
| 302 | + * Set automatic filename |
|
| 303 | + * |
|
| 304 | + * @since 1.0 |
|
| 305 | + * @version 1.5 |
|
| 306 | + * @param string $extension |
|
| 307 | + * @return object |
|
| 308 | + * @method boolean setAutoFilename |
|
| 309 | + */ |
|
| 310 | + public function setAutoFilename() |
|
| 311 | + { |
|
| 312 | + $this->filename = sha1(mt_rand(1, 9999).uniqid()); |
|
| 313 | + $this->filename .= time(); |
|
| 314 | + return $this; |
|
| 315 | + } |
|
| 316 | + /** |
|
| 317 | + * Set file size limit |
|
| 318 | + * |
|
| 319 | + * @since 1.0 |
|
| 320 | + * @version 1.0 |
|
| 321 | + * @param double $file_size |
|
| 322 | + * @return object |
|
| 323 | + * @method boolean setMaxFileSize |
|
| 324 | + */ |
|
| 325 | + public function setMaxFileSize($file_size) |
|
| 326 | + { |
|
| 327 | + $file_size = $this->sizeInBytes($file_size); |
|
| 328 | + if (is_numeric($file_size) && $file_size > -1) { |
|
| 329 | + // Get php config |
|
| 330 | + $php_size = $this->sizeInBytes((int) ini_get('upload_max_filesize')); |
|
| 331 | + // Calculate difference |
|
| 332 | + if ($php_size < $file_size) { |
|
| 333 | + $this->logger->warning('The upload max file size you set [' .$file_size. '] is greather than the PHP configuration for upload max file size [' .$php_size. ']'); |
|
| 334 | + } |
|
| 335 | + $this->max_file_size = $file_size; |
|
| 336 | + } |
|
| 337 | + return $this; |
|
| 338 | + } |
|
| 339 | + /** |
|
| 340 | + * Set array mime types |
|
| 341 | + * |
|
| 342 | + * @since 1.0 |
|
| 343 | + * @version 1.0 |
|
| 344 | + * @param array $mimes |
|
| 345 | + * @return object |
|
| 346 | + * @method boolean setAllowedMimeTypes |
|
| 347 | + */ |
|
| 348 | + public function setAllowedMimeTypes(array $mimes) |
|
| 349 | + { |
|
| 350 | + if (count($mimes) > 0) { |
|
| 351 | + array_map(array($this , 'setAllowMimeType'), $mimes); |
|
| 352 | + } |
|
| 353 | + return $this; |
|
| 354 | + } |
|
| 355 | + /** |
|
| 356 | + * Set input callback |
|
| 357 | + * |
|
| 358 | + * @since 1.0 |
|
| 359 | + * @version 1.0 |
|
| 360 | + * @param mixed $callback |
|
| 361 | + * @return object |
|
| 362 | + * @method boolean setCallbackInput |
|
| 363 | + */ |
|
| 364 | + public function setCallbackInput($callback) |
|
| 365 | + { |
|
| 366 | + if (is_callable($callback, false)) { |
|
| 367 | + $this->callbacks['input'] = $callback; |
|
| 368 | + } |
|
| 369 | + return $this; |
|
| 370 | + } |
|
| 371 | + /** |
|
| 372 | + * Set output callback |
|
| 373 | + * |
|
| 374 | + * @since 1.0 |
|
| 375 | + * @version 1.0 |
|
| 376 | + * @param mixed $callback |
|
| 377 | + * @return object |
|
| 378 | + * @method boolean setCallbackOutput |
|
| 379 | + */ |
|
| 380 | + public function setCallbackOutput($callback) |
|
| 381 | + { |
|
| 382 | + if (is_callable($callback, false)) { |
|
| 383 | + $this->callbacks['output'] = $callback; |
|
| 384 | + } |
|
| 385 | + return $this; |
|
| 386 | + } |
|
| 387 | + /** |
|
| 388 | + * Append a mime type to allowed mime types |
|
| 389 | + * |
|
| 390 | + * @since 1.0 |
|
| 391 | + * @version 1.0.1 |
|
| 392 | + * @param string $mime |
|
| 393 | + * @return object |
|
| 394 | + * @method boolean setAllowMimeType |
|
| 395 | + */ |
|
| 396 | + public function setAllowMimeType($mime) |
|
| 397 | + { |
|
| 398 | + if (!empty($mime) && is_string($mime)) { |
|
| 399 | + $this->allowed_mime_types[] = strtolower($mime); |
|
| 400 | + $this->file['allowed_mime_types'][] = strtolower($mime); |
|
| 401 | + } |
|
| 402 | + return $this; |
|
| 403 | + } |
|
| 404 | + /** |
|
| 405 | + * Set allowed mime types from mime helping |
|
| 406 | + * |
|
| 407 | + * @since 1.0.1 |
|
| 408 | + * @version 1.0.1 |
|
| 409 | + * @return object |
|
| 410 | + * @method boolean setMimeHelping |
|
| 411 | + */ |
|
| 412 | + public function setMimeHelping($name) |
|
| 413 | + { |
|
| 414 | + if (!empty($name) && is_string($name)) { |
|
| 415 | + if (array_key_exists($name, $this->mime_helping)) { |
|
| 416 | + return $this->setAllowedMimeTypes($this->mime_helping[ $name ]); |
|
| 417 | + } |
|
| 418 | + } |
|
| 419 | + return $this; |
|
| 420 | + } |
|
| 421 | + /** |
|
| 422 | + * Set function to upload file |
|
| 423 | + * Examples: |
|
| 424 | + * 1.- FileUpload::setUploadFunction("move_uploaded_file"); |
|
| 425 | + * 2.- FileUpload::setUploadFunction("copy"); |
|
| 426 | + * |
|
| 427 | + * @since 1.0 |
|
| 428 | + * @version 1.0 |
|
| 429 | + * @param string $function |
|
| 430 | + * @return object |
|
| 431 | + * @method boolean setUploadFunction |
|
| 432 | + */ |
|
| 433 | + public function setUploadFunction($function) |
|
| 434 | + { |
|
| 435 | + if (!empty($function) && (is_array($function) || is_string($function) )) { |
|
| 436 | + if (is_callable( $function)) { |
|
| 437 | + $this->upload_function = $function; |
|
| 438 | + } |
|
| 439 | + } |
|
| 440 | + return $this; |
|
| 441 | + } |
|
| 442 | + /** |
|
| 443 | + * Clear allowed mime types cache |
|
| 444 | + * |
|
| 445 | + * @since 1.0 |
|
| 446 | + * @version 1.0 |
|
| 447 | + * @return object |
|
| 448 | + * @method boolean clearAllowedMimeTypes |
|
| 449 | + */ |
|
| 450 | + public function clearAllowedMimeTypes() |
|
| 451 | + { |
|
| 452 | + $this->allowed_mime_types = array(); |
|
| 453 | + $this->file['allowed_mime_types'] = array(); |
|
| 454 | + return $this; |
|
| 455 | + } |
|
| 456 | + /** |
|
| 457 | + * Set destination output |
|
| 458 | + * |
|
| 459 | + * @since 1.0 |
|
| 460 | + * @version 1.0 |
|
| 461 | + * @param string $destination_directory Destination path |
|
| 462 | + * @param boolean $create_if_not_exist |
|
| 463 | + * @return object |
|
| 464 | + * @method boolean setDestinationDirectory |
|
| 465 | + */ |
|
| 466 | + public function setDestinationDirectory($destination_directory, $create_if_not_exist = false) { |
|
| 467 | + $destination_directory = realpath($destination_directory); |
|
| 468 | + if (substr($destination_directory, -1) != DIRECTORY_SEPARATOR) { |
|
| 469 | + $destination_directory .= DIRECTORY_SEPARATOR; |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + if ($this->isDirpath($destination_directory)) { |
|
| 473 | + if ($this->dirExists($destination_directory)) { |
|
| 474 | + $this->destination_directory = $destination_directory; |
|
| 475 | + chdir($destination_directory); |
|
| 476 | + } else if ($create_if_not_exist === true) { |
|
| 477 | + if (mkdir($destination_directory, 0775, true)) { |
|
| 478 | + $this->destination_directory = $destination_directory; |
|
| 479 | + chdir($destination_directory); |
|
| 480 | + } |
|
| 481 | + else{ |
|
| 482 | + $this->logger->warning('Can not create the upload directory [' .$destination_directory. ']'); |
|
| 483 | + } |
|
| 484 | + } |
|
| 485 | + } |
|
| 486 | + return $this; |
|
| 487 | + } |
|
| 488 | + /** |
|
| 489 | + * Check file exists |
|
| 490 | + * |
|
| 491 | + * @since 1.0 |
|
| 492 | + * @version 1.0.1 |
|
| 493 | + * @param string $file_destination |
|
| 494 | + * @return boolean |
|
| 495 | + * @method boolean fileExists |
|
| 496 | + */ |
|
| 497 | + public function fileExists($file_destination) |
|
| 498 | + { |
|
| 499 | + if ($this->isFilename($file_destination)) { |
|
| 500 | + return (file_exists($file_destination) && is_file($file_destination)); |
|
| 501 | + } |
|
| 502 | + return false; |
|
| 503 | + } |
|
| 504 | + /** |
|
| 505 | + * Check dir exists |
|
| 506 | + * |
|
| 507 | + * @since 1.0 |
|
| 508 | + * @version 1.0.1 |
|
| 509 | + * @param string $path |
|
| 510 | + * @return boolean |
|
| 511 | + * @method boolean dirExists |
|
| 512 | + */ |
|
| 513 | + public function dirExists($path) |
|
| 514 | + { |
|
| 515 | + if ($this->isDirpath($path)) { |
|
| 516 | + return (file_exists($path) && is_dir($path)); |
|
| 517 | + } |
|
| 518 | + return false; |
|
| 519 | + } |
|
| 520 | + /** |
|
| 521 | + * Check valid filename |
|
| 522 | + * |
|
| 523 | + * @since 1.0 |
|
| 524 | + * @version 1.0.1 |
|
| 525 | + * @param string $filename |
|
| 526 | + * @return boolean |
|
| 527 | + * @method boolean isFilename |
|
| 528 | + */ |
|
| 529 | + public function isFilename($filename) |
|
| 530 | + { |
|
| 531 | + $filename = basename($filename); |
|
| 532 | + return (!empty($filename) && (is_string( $filename) || is_numeric($filename))); |
|
| 533 | + } |
|
| 534 | + /** |
|
| 535 | + * Validate mime type with allowed mime types, |
|
| 536 | + * but if allowed mime types is empty, this method return true |
|
| 537 | + * |
|
| 538 | + * @since 1.0 |
|
| 539 | + * @version 1.0 |
|
| 540 | + * @param string $mime |
|
| 541 | + * @return boolean |
|
| 542 | + * @method boolean checkMimeType |
|
| 543 | + */ |
|
| 544 | + public function checkMimeType($mime) |
|
| 545 | + { |
|
| 546 | + if (count($this->allowed_mime_types) == 0) { |
|
| 547 | + return true; |
|
| 548 | + } |
|
| 549 | + return in_array(strtolower($mime), $this->allowed_mime_types); |
|
| 550 | + } |
|
| 551 | + /** |
|
| 552 | + * Retrive status of upload |
|
| 553 | + * |
|
| 554 | + * @since 1.0 |
|
| 555 | + * @version 1.0 |
|
| 556 | + * @return boolean |
|
| 557 | + * @method boolean getStatus |
|
| 558 | + */ |
|
| 559 | + public function getStatus() |
|
| 560 | + { |
|
| 561 | + return $this->file['status']; |
|
| 562 | + } |
|
| 563 | + /** |
|
| 564 | + * Check valid path |
|
| 565 | + * |
|
| 566 | + * @since 1.0 |
|
| 567 | + * @version 1.0.1 |
|
| 568 | + * @param string $filename |
|
| 569 | + * @return boolean |
|
| 570 | + * @method boolean isDirpath |
|
| 571 | + */ |
|
| 572 | + public function isDirpath($path) |
|
| 573 | + { |
|
| 574 | + if (!empty( $path) && (is_string( $path) || is_numeric($path) )) { |
|
| 575 | + if (DIRECTORY_SEPARATOR == '/') { |
|
| 576 | + return (preg_match( '/^[^*?"<>|:]*$/' , $path) == 1 ); |
|
| 577 | + } else { |
|
| 578 | + return (preg_match( "/^[^*?\"<>|:]*$/" , substr($path,2) ) == 1); |
|
| 579 | + } |
|
| 580 | + } |
|
| 581 | + return false; |
|
| 582 | + } |
|
| 583 | + /** |
|
| 584 | + * Allow overwriting files |
|
| 585 | + * |
|
| 586 | + * @since 1.0 |
|
| 587 | + * @version 1.0 |
|
| 588 | + * @return object |
|
| 589 | + * @method boolean allowOverwriting |
|
| 590 | + */ |
|
| 591 | + public function allowOverwriting() |
|
| 592 | + { |
|
| 593 | + $this->overwrite_file = true; |
|
| 594 | + return $this; |
|
| 595 | + } |
|
| 596 | + /** |
|
| 597 | + * File info |
|
| 598 | + * |
|
| 599 | + * @since 1.0 |
|
| 600 | + * @version 1.0 |
|
| 601 | + * @return object |
|
| 602 | + * @method object getInfo |
|
| 603 | + */ |
|
| 604 | + public function getInfo() |
|
| 605 | + { |
|
| 606 | + return (object)$this->file; |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + |
|
| 610 | + /** |
|
| 611 | + * Check if the file is uploaded |
|
| 612 | + * @return boolean |
|
| 613 | + */ |
|
| 614 | + public function isUploaded(){ |
|
| 615 | + return isset($this->file_array[$this->input]) |
|
| 616 | + && is_uploaded_file($this->file_array[$this->input]['tmp_name']); |
|
| 617 | + } |
|
| 618 | 618 | |
| 619 | 619 | |
| 620 | - /** |
|
| 621 | - * Upload file |
|
| 622 | - * |
|
| 623 | - * @since 1.0 |
|
| 624 | - * @version 1.0.1 |
|
| 625 | - * @return boolean |
|
| 626 | - * @method boolean save |
|
| 627 | - */ |
|
| 628 | - public function save(){ |
|
| 629 | - if (count($this->file_array) > 0 && array_key_exists($this->input, $this->file_array)) { |
|
| 630 | - // set original filename if not have a new name |
|
| 631 | - if (empty($this->filename)) { |
|
| 632 | - $this->filename = $this->file_array[$this->input]['name']; |
|
| 633 | - } |
|
| 634 | - else{ |
|
| 635 | - // Replace %s for extension in filename |
|
| 636 | - // Before: /[\w\d]*(.[\d\w]+)$/i |
|
| 637 | - // After: /^[\s[:alnum:]\-\_\.]*\.([\d\w]+)$/iu |
|
| 638 | - // Support unicode(utf-8) characters |
|
| 639 | - // Example: "русские.jpeg" is valid; "Zhōngguó.jpeg" is valid; "Tønsberg.jpeg" is valid |
|
| 640 | - $extension = preg_replace( |
|
| 641 | - '/^[\p{L}\d\s\-\_\.\(\)]*\.([\d\w]+)$/iu', |
|
| 642 | - '$1', |
|
| 643 | - $this->file_array[$this->input]['name'] |
|
| 644 | - ); |
|
| 645 | - $this->filename = $this->filename . '.' . $extension; |
|
| 646 | - } |
|
| 647 | - |
|
| 648 | - // set file info |
|
| 649 | - $this->file['mime'] = $this->file_array[$this->input]['type']; |
|
| 650 | - $this->file['tmp'] = $this->file_array[$this->input]['tmp_name']; |
|
| 651 | - $this->file['original'] = $this->file_array[$this->input]['name']; |
|
| 652 | - $this->file['size'] = $this->file_array[$this->input]['size']; |
|
| 653 | - $this->file['sizeFormated'] = $this->sizeFormat($this->file['size']); |
|
| 654 | - $this->file['destination'] = $this->destination_directory . $this->filename; |
|
| 655 | - $this->file['filename'] = $this->filename; |
|
| 656 | - $this->file['error'] = $this->file_array[$this->input]['error']; |
|
| 657 | - |
|
| 658 | - $this->logger->info('The upload file information to process is : ' .stringfy_vars($this->file)); |
|
| 659 | - |
|
| 660 | - $error = $this->uploadHasError(); |
|
| 661 | - if($error){ |
|
| 662 | - return false; |
|
| 663 | - } |
|
| 664 | - // Execute input callback |
|
| 665 | - $this->runCallback('input'); |
|
| 666 | - |
|
| 667 | - $this->file['status'] = call_user_func_array( |
|
| 668 | - $this->upload_function, array( |
|
| 669 | - $this->file_array[$this->input]['tmp_name'], |
|
| 670 | - $this->destination_directory . $this->filename |
|
| 671 | - ) |
|
| 672 | - ); |
|
| 673 | - |
|
| 674 | - // Execute output callback |
|
| 675 | - $this->runCallback('output'); |
|
| 676 | - |
|
| 677 | - return $this->file['status']; |
|
| 678 | - } |
|
| 679 | - return false; |
|
| 680 | - } |
|
| 681 | - |
|
| 682 | - |
|
| 683 | - /** |
|
| 684 | - * File size for humans. |
|
| 685 | - * |
|
| 686 | - * @since 1.0 |
|
| 687 | - * @version 1.0 |
|
| 688 | - * @param integer $bytes |
|
| 689 | - * @param integer $precision |
|
| 690 | - * @return string |
|
| 691 | - * @method string sizeFormat |
|
| 692 | - */ |
|
| 693 | - public function sizeFormat($size, $precision = 2) |
|
| 694 | - { |
|
| 695 | - if($size > 0){ |
|
| 696 | - $base = log($size) / log(1024); |
|
| 697 | - $suffixes = array('B', 'K', 'M', 'G', 'T'); |
|
| 698 | - return round(pow(1024, $base - floor($base)), $precision) . ( isset($suffixes[floor($base)]) ? $suffixes[floor($base)] : ''); |
|
| 699 | - } |
|
| 700 | - return null; |
|
| 701 | - } |
|
| 620 | + /** |
|
| 621 | + * Upload file |
|
| 622 | + * |
|
| 623 | + * @since 1.0 |
|
| 624 | + * @version 1.0.1 |
|
| 625 | + * @return boolean |
|
| 626 | + * @method boolean save |
|
| 627 | + */ |
|
| 628 | + public function save(){ |
|
| 629 | + if (count($this->file_array) > 0 && array_key_exists($this->input, $this->file_array)) { |
|
| 630 | + // set original filename if not have a new name |
|
| 631 | + if (empty($this->filename)) { |
|
| 632 | + $this->filename = $this->file_array[$this->input]['name']; |
|
| 633 | + } |
|
| 634 | + else{ |
|
| 635 | + // Replace %s for extension in filename |
|
| 636 | + // Before: /[\w\d]*(.[\d\w]+)$/i |
|
| 637 | + // After: /^[\s[:alnum:]\-\_\.]*\.([\d\w]+)$/iu |
|
| 638 | + // Support unicode(utf-8) characters |
|
| 639 | + // Example: "русские.jpeg" is valid; "Zhōngguó.jpeg" is valid; "Tønsberg.jpeg" is valid |
|
| 640 | + $extension = preg_replace( |
|
| 641 | + '/^[\p{L}\d\s\-\_\.\(\)]*\.([\d\w]+)$/iu', |
|
| 642 | + '$1', |
|
| 643 | + $this->file_array[$this->input]['name'] |
|
| 644 | + ); |
|
| 645 | + $this->filename = $this->filename . '.' . $extension; |
|
| 646 | + } |
|
| 647 | + |
|
| 648 | + // set file info |
|
| 649 | + $this->file['mime'] = $this->file_array[$this->input]['type']; |
|
| 650 | + $this->file['tmp'] = $this->file_array[$this->input]['tmp_name']; |
|
| 651 | + $this->file['original'] = $this->file_array[$this->input]['name']; |
|
| 652 | + $this->file['size'] = $this->file_array[$this->input]['size']; |
|
| 653 | + $this->file['sizeFormated'] = $this->sizeFormat($this->file['size']); |
|
| 654 | + $this->file['destination'] = $this->destination_directory . $this->filename; |
|
| 655 | + $this->file['filename'] = $this->filename; |
|
| 656 | + $this->file['error'] = $this->file_array[$this->input]['error']; |
|
| 657 | + |
|
| 658 | + $this->logger->info('The upload file information to process is : ' .stringfy_vars($this->file)); |
|
| 659 | + |
|
| 660 | + $error = $this->uploadHasError(); |
|
| 661 | + if($error){ |
|
| 662 | + return false; |
|
| 663 | + } |
|
| 664 | + // Execute input callback |
|
| 665 | + $this->runCallback('input'); |
|
| 666 | + |
|
| 667 | + $this->file['status'] = call_user_func_array( |
|
| 668 | + $this->upload_function, array( |
|
| 669 | + $this->file_array[$this->input]['tmp_name'], |
|
| 670 | + $this->destination_directory . $this->filename |
|
| 671 | + ) |
|
| 672 | + ); |
|
| 673 | + |
|
| 674 | + // Execute output callback |
|
| 675 | + $this->runCallback('output'); |
|
| 676 | + |
|
| 677 | + return $this->file['status']; |
|
| 678 | + } |
|
| 679 | + return false; |
|
| 680 | + } |
|
| 681 | + |
|
| 682 | + |
|
| 683 | + /** |
|
| 684 | + * File size for humans. |
|
| 685 | + * |
|
| 686 | + * @since 1.0 |
|
| 687 | + * @version 1.0 |
|
| 688 | + * @param integer $bytes |
|
| 689 | + * @param integer $precision |
|
| 690 | + * @return string |
|
| 691 | + * @method string sizeFormat |
|
| 692 | + */ |
|
| 693 | + public function sizeFormat($size, $precision = 2) |
|
| 694 | + { |
|
| 695 | + if($size > 0){ |
|
| 696 | + $base = log($size) / log(1024); |
|
| 697 | + $suffixes = array('B', 'K', 'M', 'G', 'T'); |
|
| 698 | + return round(pow(1024, $base - floor($base)), $precision) . ( isset($suffixes[floor($base)]) ? $suffixes[floor($base)] : ''); |
|
| 699 | + } |
|
| 700 | + return null; |
|
| 701 | + } |
|
| 702 | 702 | |
| 703 | 703 | |
| 704 | - /** |
|
| 705 | - * Convert human file size to bytes |
|
| 706 | - * |
|
| 707 | - * @since 1.0 |
|
| 708 | - * @version 1.0.1 |
|
| 709 | - * @param integer|double $size |
|
| 710 | - * @return integer|double |
|
| 711 | - * @method string sizeInBytes |
|
| 712 | - */ |
|
| 713 | - public function sizeInBytes($size) |
|
| 714 | - { |
|
| 715 | - $unit = 'B'; |
|
| 716 | - $units = array('B' => 0, 'K' => 1, 'M' => 2, 'G' => 3, 'T' => 4); |
|
| 717 | - $matches = array(); |
|
| 718 | - preg_match('/(?<size>[\d\.]+)\s*(?<unit>b|k|m|g|t)?/i', $size, $matches); |
|
| 719 | - if (array_key_exists('unit', $matches)) { |
|
| 720 | - $unit = strtoupper($matches['unit']); |
|
| 721 | - } |
|
| 722 | - return (floatval($matches['size']) * pow(1024, $units[$unit]) ) ; |
|
| 723 | - } |
|
| 724 | - |
|
| 725 | - /** |
|
| 726 | - * Get the upload error message |
|
| 727 | - * @return string |
|
| 728 | - */ |
|
| 729 | - public function getError(){ |
|
| 730 | - return $this->error; |
|
| 731 | - } |
|
| 732 | - |
|
| 733 | - /** |
|
| 734 | - * Set the upload error message |
|
| 735 | - * @param string $message the upload error message to set |
|
| 736 | - */ |
|
| 737 | - public function setError($message){ |
|
| 738 | - $this->logger->info('The file upload got error : ' . $message); |
|
| 739 | - $this->error = $message; |
|
| 740 | - } |
|
| 741 | - |
|
| 742 | - /** |
|
| 743 | - * Run the callbacks in the file uploaded |
|
| 744 | - * @param string $type the type of callback "input" or "output" |
|
| 745 | - * @return void |
|
| 746 | - */ |
|
| 747 | - protected function runCallback($type){ |
|
| 748 | - if (!empty( $this->callbacks[$type])) { |
|
| 749 | - call_user_func($this->callbacks[$type], (object)$this->file); |
|
| 750 | - } |
|
| 751 | - } |
|
| 752 | - |
|
| 753 | - /** |
|
| 754 | - * Check if file upload has error |
|
| 755 | - * @return boolean |
|
| 756 | - */ |
|
| 757 | - protected function uploadHasError(){ |
|
| 758 | - //check if file upload is allowed in the configuration |
|
| 759 | - if(! ini_get('file_uploads')){ |
|
| 760 | - $this->setError($this->error_messages['file_uploads']); |
|
| 761 | - return true; |
|
| 762 | - } |
|
| 763 | - |
|
| 764 | - //check for php upload error |
|
| 765 | - if(is_numeric($this->file['error']) && $this->file['error'] > 0){ |
|
| 766 | - $this->setError($this->getPhpUploadErrorMessageByCode($this->file['error'])); |
|
| 767 | - return true; |
|
| 768 | - } |
|
| 704 | + /** |
|
| 705 | + * Convert human file size to bytes |
|
| 706 | + * |
|
| 707 | + * @since 1.0 |
|
| 708 | + * @version 1.0.1 |
|
| 709 | + * @param integer|double $size |
|
| 710 | + * @return integer|double |
|
| 711 | + * @method string sizeInBytes |
|
| 712 | + */ |
|
| 713 | + public function sizeInBytes($size) |
|
| 714 | + { |
|
| 715 | + $unit = 'B'; |
|
| 716 | + $units = array('B' => 0, 'K' => 1, 'M' => 2, 'G' => 3, 'T' => 4); |
|
| 717 | + $matches = array(); |
|
| 718 | + preg_match('/(?<size>[\d\.]+)\s*(?<unit>b|k|m|g|t)?/i', $size, $matches); |
|
| 719 | + if (array_key_exists('unit', $matches)) { |
|
| 720 | + $unit = strtoupper($matches['unit']); |
|
| 721 | + } |
|
| 722 | + return (floatval($matches['size']) * pow(1024, $units[$unit]) ) ; |
|
| 723 | + } |
|
| 724 | + |
|
| 725 | + /** |
|
| 726 | + * Get the upload error message |
|
| 727 | + * @return string |
|
| 728 | + */ |
|
| 729 | + public function getError(){ |
|
| 730 | + return $this->error; |
|
| 731 | + } |
|
| 732 | + |
|
| 733 | + /** |
|
| 734 | + * Set the upload error message |
|
| 735 | + * @param string $message the upload error message to set |
|
| 736 | + */ |
|
| 737 | + public function setError($message){ |
|
| 738 | + $this->logger->info('The file upload got error : ' . $message); |
|
| 739 | + $this->error = $message; |
|
| 740 | + } |
|
| 741 | + |
|
| 742 | + /** |
|
| 743 | + * Run the callbacks in the file uploaded |
|
| 744 | + * @param string $type the type of callback "input" or "output" |
|
| 745 | + * @return void |
|
| 746 | + */ |
|
| 747 | + protected function runCallback($type){ |
|
| 748 | + if (!empty( $this->callbacks[$type])) { |
|
| 749 | + call_user_func($this->callbacks[$type], (object)$this->file); |
|
| 750 | + } |
|
| 751 | + } |
|
| 752 | + |
|
| 753 | + /** |
|
| 754 | + * Check if file upload has error |
|
| 755 | + * @return boolean |
|
| 756 | + */ |
|
| 757 | + protected function uploadHasError(){ |
|
| 758 | + //check if file upload is allowed in the configuration |
|
| 759 | + if(! ini_get('file_uploads')){ |
|
| 760 | + $this->setError($this->error_messages['file_uploads']); |
|
| 761 | + return true; |
|
| 762 | + } |
|
| 763 | + |
|
| 764 | + //check for php upload error |
|
| 765 | + if(is_numeric($this->file['error']) && $this->file['error'] > 0){ |
|
| 766 | + $this->setError($this->getPhpUploadErrorMessageByCode($this->file['error'])); |
|
| 767 | + return true; |
|
| 768 | + } |
|
| 769 | 769 | |
| 770 | - //check for mime type |
|
| 771 | - if (! $this->checkMimeType($this->file['mime'])) { |
|
| 772 | - $this->setError($this->error_messages['accept_file_types']); |
|
| 773 | - return true; |
|
| 774 | - } |
|
| 775 | - |
|
| 776 | - // Check file size |
|
| 777 | - if ($this->max_file_size > 0 && $this->max_file_size < $this->file['size']) { |
|
| 778 | - $this->setError(sprintf($this->error_messages['max_file_size'], $this->sizeFormat($this->max_file_size))); |
|
| 779 | - return true; |
|
| 780 | - } |
|
| 781 | - |
|
| 782 | - // Check if exists file |
|
| 783 | - if ($this->fileExists($this->destination_directory . $this->filename) && $this->overwrite_file === false) { |
|
| 784 | - $this->setError($this->error_messages['overwritten_not_allowed']); |
|
| 785 | - return true; |
|
| 786 | - } |
|
| 787 | - return false; |
|
| 788 | - } |
|
| 789 | - |
|
| 790 | - /** |
|
| 791 | - * Get the PHP upload error message for the given code |
|
| 792 | - * @param int $code the error code |
|
| 793 | - * @return string the error message |
|
| 794 | - */ |
|
| 795 | - private function getPhpUploadErrorMessageByCode($code){ |
|
| 796 | - $codeMessageMaps = array( |
|
| 797 | - 1 => $this->error_messages['upload_err_ini_size'], |
|
| 798 | - 2 => $this->error_messages['upload_err_form_size'], |
|
| 799 | - 3 => $this->error_messages['upload_err_partial'], |
|
| 800 | - 4 => $this->error_messages['upload_err_no_file'], |
|
| 801 | - 6 => $this->error_messages['upload_err_no_tmp_dir'], |
|
| 802 | - 7 => $this->error_messages['upload_err_cant_write'], |
|
| 803 | - 8 => $this->error_messages['upload_err_extension'], |
|
| 804 | - ); |
|
| 805 | - return isset($codeMessageMaps[$code]) ? $codeMessageMaps[$code] : null; |
|
| 806 | - } |
|
| 807 | - } |
|
| 770 | + //check for mime type |
|
| 771 | + if (! $this->checkMimeType($this->file['mime'])) { |
|
| 772 | + $this->setError($this->error_messages['accept_file_types']); |
|
| 773 | + return true; |
|
| 774 | + } |
|
| 775 | + |
|
| 776 | + // Check file size |
|
| 777 | + if ($this->max_file_size > 0 && $this->max_file_size < $this->file['size']) { |
|
| 778 | + $this->setError(sprintf($this->error_messages['max_file_size'], $this->sizeFormat($this->max_file_size))); |
|
| 779 | + return true; |
|
| 780 | + } |
|
| 781 | + |
|
| 782 | + // Check if exists file |
|
| 783 | + if ($this->fileExists($this->destination_directory . $this->filename) && $this->overwrite_file === false) { |
|
| 784 | + $this->setError($this->error_messages['overwritten_not_allowed']); |
|
| 785 | + return true; |
|
| 786 | + } |
|
| 787 | + return false; |
|
| 788 | + } |
|
| 789 | + |
|
| 790 | + /** |
|
| 791 | + * Get the PHP upload error message for the given code |
|
| 792 | + * @param int $code the error code |
|
| 793 | + * @return string the error message |
|
| 794 | + */ |
|
| 795 | + private function getPhpUploadErrorMessageByCode($code){ |
|
| 796 | + $codeMessageMaps = array( |
|
| 797 | + 1 => $this->error_messages['upload_err_ini_size'], |
|
| 798 | + 2 => $this->error_messages['upload_err_form_size'], |
|
| 799 | + 3 => $this->error_messages['upload_err_partial'], |
|
| 800 | + 4 => $this->error_messages['upload_err_no_file'], |
|
| 801 | + 6 => $this->error_messages['upload_err_no_tmp_dir'], |
|
| 802 | + 7 => $this->error_messages['upload_err_cant_write'], |
|
| 803 | + 8 => $this->error_messages['upload_err_extension'], |
|
| 804 | + ); |
|
| 805 | + return isset($codeMessageMaps[$code]) ? $codeMessageMaps[$code] : null; |
|
| 806 | + } |
|
| 807 | + } |
|
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | * @package FileUpload |
| 38 | 38 | * @version 1.5 |
| 39 | 39 | */ |
| 40 | - class Upload{ |
|
| 40 | + class Upload { |
|
| 41 | 41 | |
| 42 | 42 | /** |
| 43 | 43 | * Version |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | * @version 1.0 |
| 70 | 70 | * @var array |
| 71 | 71 | */ |
| 72 | - private $file_array = array(); |
|
| 72 | + private $file_array = array(); |
|
| 73 | 73 | |
| 74 | 74 | /** |
| 75 | 75 | * If the file you are trying to upload already exists it will |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | * @version 1.0 |
| 120 | 120 | * @var float |
| 121 | 121 | */ |
| 122 | - private $max_file_size= 0.0; |
|
| 122 | + private $max_file_size = 0.0; |
|
| 123 | 123 | |
| 124 | 124 | /** |
| 125 | 125 | * List of allowed mime types |
@@ -217,12 +217,12 @@ discard block |
||
| 217 | 217 | * @return object |
| 218 | 218 | * @method object __construct |
| 219 | 219 | */ |
| 220 | - public function __construct(){ |
|
| 221 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 220 | + public function __construct() { |
|
| 221 | + $this->logger = & class_loader('Log', 'classes'); |
|
| 222 | 222 | $this->logger->setLogger('Library::Upload'); |
| 223 | 223 | |
| 224 | 224 | Loader::lang('file_upload'); |
| 225 | - $obj =& get_instance(); |
|
| 225 | + $obj = & get_instance(); |
|
| 226 | 226 | |
| 227 | 227 | $this->error_messages = array( |
| 228 | 228 | 'upload_err_ini_size' => $obj->lang->get('fu_upload_err_ini_size'), |
@@ -239,15 +239,15 @@ discard block |
||
| 239 | 239 | ); |
| 240 | 240 | |
| 241 | 241 | $this->file = array( |
| 242 | - 'status' => false, // True: success upload |
|
| 243 | - 'mime' => '', // Empty string |
|
| 244 | - 'filename' => '', // Empty string |
|
| 245 | - 'original' => '', // Empty string |
|
| 246 | - 'size' => 0, // 0 Bytes |
|
| 247 | - 'sizeFormated' => '0B', // 0 Bytes |
|
| 248 | - 'destination' => './', // Default: ./ |
|
| 249 | - 'allowed_mime_types' => array(), // Allowed mime types |
|
| 250 | - 'error' => null, // File error |
|
| 242 | + 'status' => false, // True: success upload |
|
| 243 | + 'mime' => '', // Empty string |
|
| 244 | + 'filename' => '', // Empty string |
|
| 245 | + 'original' => '', // Empty string |
|
| 246 | + 'size' => 0, // 0 Bytes |
|
| 247 | + 'sizeFormated' => '0B', // 0 Bytes |
|
| 248 | + 'destination' => './', // Default: ./ |
|
| 249 | + 'allowed_mime_types' => array(), // Allowed mime types |
|
| 250 | + 'error' => null, // File error |
|
| 251 | 251 | ); |
| 252 | 252 | |
| 253 | 253 | // Change dir to current dir |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | if (isset($_FILES) && is_array($_FILES)) { |
| 258 | 258 | $this->file_array = $_FILES; |
| 259 | 259 | } |
| 260 | - $this->logger->info('The upload file information are : ' .stringfy_vars($this->file_array)); |
|
| 260 | + $this->logger->info('The upload file information are : ' . stringfy_vars($this->file_array)); |
|
| 261 | 261 | } |
| 262 | 262 | /** |
| 263 | 263 | * Set input. |
@@ -273,7 +273,7 @@ discard block |
||
| 273 | 273 | */ |
| 274 | 274 | public function setInput($input) |
| 275 | 275 | { |
| 276 | - if (!empty($input) && (is_string($input) || is_numeric($input) )) { |
|
| 276 | + if (!empty($input) && (is_string($input) || is_numeric($input))) { |
|
| 277 | 277 | $this->input = $input; |
| 278 | 278 | } |
| 279 | 279 | return $this; |
@@ -309,7 +309,7 @@ discard block |
||
| 309 | 309 | */ |
| 310 | 310 | public function setAutoFilename() |
| 311 | 311 | { |
| 312 | - $this->filename = sha1(mt_rand(1, 9999).uniqid()); |
|
| 312 | + $this->filename = sha1(mt_rand(1, 9999) . uniqid()); |
|
| 313 | 313 | $this->filename .= time(); |
| 314 | 314 | return $this; |
| 315 | 315 | } |
@@ -330,7 +330,7 @@ discard block |
||
| 330 | 330 | $php_size = $this->sizeInBytes((int) ini_get('upload_max_filesize')); |
| 331 | 331 | // Calculate difference |
| 332 | 332 | if ($php_size < $file_size) { |
| 333 | - $this->logger->warning('The upload max file size you set [' .$file_size. '] is greather than the PHP configuration for upload max file size [' .$php_size. ']'); |
|
| 333 | + $this->logger->warning('The upload max file size you set [' . $file_size . '] is greather than the PHP configuration for upload max file size [' . $php_size . ']'); |
|
| 334 | 334 | } |
| 335 | 335 | $this->max_file_size = $file_size; |
| 336 | 336 | } |
@@ -348,7 +348,7 @@ discard block |
||
| 348 | 348 | public function setAllowedMimeTypes(array $mimes) |
| 349 | 349 | { |
| 350 | 350 | if (count($mimes) > 0) { |
| 351 | - array_map(array($this , 'setAllowMimeType'), $mimes); |
|
| 351 | + array_map(array($this, 'setAllowMimeType'), $mimes); |
|
| 352 | 352 | } |
| 353 | 353 | return $this; |
| 354 | 354 | } |
@@ -413,7 +413,7 @@ discard block |
||
| 413 | 413 | { |
| 414 | 414 | if (!empty($name) && is_string($name)) { |
| 415 | 415 | if (array_key_exists($name, $this->mime_helping)) { |
| 416 | - return $this->setAllowedMimeTypes($this->mime_helping[ $name ]); |
|
| 416 | + return $this->setAllowedMimeTypes($this->mime_helping[$name]); |
|
| 417 | 417 | } |
| 418 | 418 | } |
| 419 | 419 | return $this; |
@@ -432,8 +432,8 @@ discard block |
||
| 432 | 432 | */ |
| 433 | 433 | public function setUploadFunction($function) |
| 434 | 434 | { |
| 435 | - if (!empty($function) && (is_array($function) || is_string($function) )) { |
|
| 436 | - if (is_callable( $function)) { |
|
| 435 | + if (!empty($function) && (is_array($function) || is_string($function))) { |
|
| 436 | + if (is_callable($function)) { |
|
| 437 | 437 | $this->upload_function = $function; |
| 438 | 438 | } |
| 439 | 439 | } |
@@ -478,8 +478,8 @@ discard block |
||
| 478 | 478 | $this->destination_directory = $destination_directory; |
| 479 | 479 | chdir($destination_directory); |
| 480 | 480 | } |
| 481 | - else{ |
|
| 482 | - $this->logger->warning('Can not create the upload directory [' .$destination_directory. ']'); |
|
| 481 | + else { |
|
| 482 | + $this->logger->warning('Can not create the upload directory [' . $destination_directory . ']'); |
|
| 483 | 483 | } |
| 484 | 484 | } |
| 485 | 485 | } |
@@ -529,7 +529,7 @@ discard block |
||
| 529 | 529 | public function isFilename($filename) |
| 530 | 530 | { |
| 531 | 531 | $filename = basename($filename); |
| 532 | - return (!empty($filename) && (is_string( $filename) || is_numeric($filename))); |
|
| 532 | + return (!empty($filename) && (is_string($filename) || is_numeric($filename))); |
|
| 533 | 533 | } |
| 534 | 534 | /** |
| 535 | 535 | * Validate mime type with allowed mime types, |
@@ -571,11 +571,11 @@ discard block |
||
| 571 | 571 | */ |
| 572 | 572 | public function isDirpath($path) |
| 573 | 573 | { |
| 574 | - if (!empty( $path) && (is_string( $path) || is_numeric($path) )) { |
|
| 574 | + if (!empty($path) && (is_string($path) || is_numeric($path))) { |
|
| 575 | 575 | if (DIRECTORY_SEPARATOR == '/') { |
| 576 | - return (preg_match( '/^[^*?"<>|:]*$/' , $path) == 1 ); |
|
| 576 | + return (preg_match('/^[^*?"<>|:]*$/', $path) == 1); |
|
| 577 | 577 | } else { |
| 578 | - return (preg_match( "/^[^*?\"<>|:]*$/" , substr($path,2) ) == 1); |
|
| 578 | + return (preg_match("/^[^*?\"<>|:]*$/", substr($path, 2)) == 1); |
|
| 579 | 579 | } |
| 580 | 580 | } |
| 581 | 581 | return false; |
@@ -603,7 +603,7 @@ discard block |
||
| 603 | 603 | */ |
| 604 | 604 | public function getInfo() |
| 605 | 605 | { |
| 606 | - return (object)$this->file; |
|
| 606 | + return (object) $this->file; |
|
| 607 | 607 | } |
| 608 | 608 | |
| 609 | 609 | |
@@ -611,7 +611,7 @@ discard block |
||
| 611 | 611 | * Check if the file is uploaded |
| 612 | 612 | * @return boolean |
| 613 | 613 | */ |
| 614 | - public function isUploaded(){ |
|
| 614 | + public function isUploaded() { |
|
| 615 | 615 | return isset($this->file_array[$this->input]) |
| 616 | 616 | && is_uploaded_file($this->file_array[$this->input]['tmp_name']); |
| 617 | 617 | } |
@@ -625,13 +625,13 @@ discard block |
||
| 625 | 625 | * @return boolean |
| 626 | 626 | * @method boolean save |
| 627 | 627 | */ |
| 628 | - public function save(){ |
|
| 628 | + public function save() { |
|
| 629 | 629 | if (count($this->file_array) > 0 && array_key_exists($this->input, $this->file_array)) { |
| 630 | 630 | // set original filename if not have a new name |
| 631 | 631 | if (empty($this->filename)) { |
| 632 | 632 | $this->filename = $this->file_array[$this->input]['name']; |
| 633 | 633 | } |
| 634 | - else{ |
|
| 634 | + else { |
|
| 635 | 635 | // Replace %s for extension in filename |
| 636 | 636 | // Before: /[\w\d]*(.[\d\w]+)$/i |
| 637 | 637 | // After: /^[\s[:alnum:]\-\_\.]*\.([\d\w]+)$/iu |
@@ -655,10 +655,10 @@ discard block |
||
| 655 | 655 | $this->file['filename'] = $this->filename; |
| 656 | 656 | $this->file['error'] = $this->file_array[$this->input]['error']; |
| 657 | 657 | |
| 658 | - $this->logger->info('The upload file information to process is : ' .stringfy_vars($this->file)); |
|
| 658 | + $this->logger->info('The upload file information to process is : ' . stringfy_vars($this->file)); |
|
| 659 | 659 | |
| 660 | 660 | $error = $this->uploadHasError(); |
| 661 | - if($error){ |
|
| 661 | + if ($error) { |
|
| 662 | 662 | return false; |
| 663 | 663 | } |
| 664 | 664 | // Execute input callback |
@@ -692,10 +692,10 @@ discard block |
||
| 692 | 692 | */ |
| 693 | 693 | public function sizeFormat($size, $precision = 2) |
| 694 | 694 | { |
| 695 | - if($size > 0){ |
|
| 695 | + if ($size > 0) { |
|
| 696 | 696 | $base = log($size) / log(1024); |
| 697 | 697 | $suffixes = array('B', 'K', 'M', 'G', 'T'); |
| 698 | - return round(pow(1024, $base - floor($base)), $precision) . ( isset($suffixes[floor($base)]) ? $suffixes[floor($base)] : ''); |
|
| 698 | + return round(pow(1024, $base - floor($base)), $precision) . (isset($suffixes[floor($base)]) ? $suffixes[floor($base)] : ''); |
|
| 699 | 699 | } |
| 700 | 700 | return null; |
| 701 | 701 | } |
@@ -719,14 +719,14 @@ discard block |
||
| 719 | 719 | if (array_key_exists('unit', $matches)) { |
| 720 | 720 | $unit = strtoupper($matches['unit']); |
| 721 | 721 | } |
| 722 | - return (floatval($matches['size']) * pow(1024, $units[$unit]) ) ; |
|
| 722 | + return (floatval($matches['size']) * pow(1024, $units[$unit])); |
|
| 723 | 723 | } |
| 724 | 724 | |
| 725 | 725 | /** |
| 726 | 726 | * Get the upload error message |
| 727 | 727 | * @return string |
| 728 | 728 | */ |
| 729 | - public function getError(){ |
|
| 729 | + public function getError() { |
|
| 730 | 730 | return $this->error; |
| 731 | 731 | } |
| 732 | 732 | |
@@ -734,7 +734,7 @@ discard block |
||
| 734 | 734 | * Set the upload error message |
| 735 | 735 | * @param string $message the upload error message to set |
| 736 | 736 | */ |
| 737 | - public function setError($message){ |
|
| 737 | + public function setError($message) { |
|
| 738 | 738 | $this->logger->info('The file upload got error : ' . $message); |
| 739 | 739 | $this->error = $message; |
| 740 | 740 | } |
@@ -744,9 +744,9 @@ discard block |
||
| 744 | 744 | * @param string $type the type of callback "input" or "output" |
| 745 | 745 | * @return void |
| 746 | 746 | */ |
| 747 | - protected function runCallback($type){ |
|
| 748 | - if (!empty( $this->callbacks[$type])) { |
|
| 749 | - call_user_func($this->callbacks[$type], (object)$this->file); |
|
| 747 | + protected function runCallback($type) { |
|
| 748 | + if (!empty($this->callbacks[$type])) { |
|
| 749 | + call_user_func($this->callbacks[$type], (object) $this->file); |
|
| 750 | 750 | } |
| 751 | 751 | } |
| 752 | 752 | |
@@ -754,21 +754,21 @@ discard block |
||
| 754 | 754 | * Check if file upload has error |
| 755 | 755 | * @return boolean |
| 756 | 756 | */ |
| 757 | - protected function uploadHasError(){ |
|
| 757 | + protected function uploadHasError() { |
|
| 758 | 758 | //check if file upload is allowed in the configuration |
| 759 | - if(! ini_get('file_uploads')){ |
|
| 759 | + if (!ini_get('file_uploads')) { |
|
| 760 | 760 | $this->setError($this->error_messages['file_uploads']); |
| 761 | 761 | return true; |
| 762 | 762 | } |
| 763 | 763 | |
| 764 | 764 | //check for php upload error |
| 765 | - if(is_numeric($this->file['error']) && $this->file['error'] > 0){ |
|
| 765 | + if (is_numeric($this->file['error']) && $this->file['error'] > 0) { |
|
| 766 | 766 | $this->setError($this->getPhpUploadErrorMessageByCode($this->file['error'])); |
| 767 | 767 | return true; |
| 768 | 768 | } |
| 769 | 769 | |
| 770 | 770 | //check for mime type |
| 771 | - if (! $this->checkMimeType($this->file['mime'])) { |
|
| 771 | + if (!$this->checkMimeType($this->file['mime'])) { |
|
| 772 | 772 | $this->setError($this->error_messages['accept_file_types']); |
| 773 | 773 | return true; |
| 774 | 774 | } |
@@ -792,7 +792,7 @@ discard block |
||
| 792 | 792 | * @param int $code the error code |
| 793 | 793 | * @return string the error message |
| 794 | 794 | */ |
| 795 | - private function getPhpUploadErrorMessageByCode($code){ |
|
| 795 | + private function getPhpUploadErrorMessageByCode($code) { |
|
| 796 | 796 | $codeMessageMaps = array( |
| 797 | 797 | 1 => $this->error_messages['upload_err_ini_size'], |
| 798 | 798 | 2 => $this->error_messages['upload_err_form_size'], |
@@ -477,8 +477,7 @@ discard block |
||
| 477 | 477 | if (mkdir($destination_directory, 0775, true)) { |
| 478 | 478 | $this->destination_directory = $destination_directory; |
| 479 | 479 | chdir($destination_directory); |
| 480 | - } |
|
| 481 | - else{ |
|
| 480 | + } else{ |
|
| 482 | 481 | $this->logger->warning('Can not create the upload directory [' .$destination_directory. ']'); |
| 483 | 482 | } |
| 484 | 483 | } |
@@ -630,8 +629,7 @@ discard block |
||
| 630 | 629 | // set original filename if not have a new name |
| 631 | 630 | if (empty($this->filename)) { |
| 632 | 631 | $this->filename = $this->file_array[$this->input]['name']; |
| 633 | - } |
|
| 634 | - else{ |
|
| 632 | + } else{ |
|
| 635 | 633 | // Replace %s for extension in filename |
| 636 | 634 | // Before: /[\w\d]*(.[\d\w]+)$/i |
| 637 | 635 | // After: /^[\s[:alnum:]\-\_\.]*\.([\d\w]+)$/iu |