@@ -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,1019 +22,1019 @@ 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 SQL SELECT statment |
| 42 | 42 | * @var string |
| 43 | - */ |
|
| 43 | + */ |
|
| 44 | 44 | private $select = '*'; |
| 45 | 45 | |
| 46 | 46 | /** |
| 47 | 47 | * The SQL FROM statment |
| 48 | 48 | * @var string |
| 49 | - */ |
|
| 50 | - private $from = null; |
|
| 49 | + */ |
|
| 50 | + private $from = null; |
|
| 51 | 51 | |
| 52 | 52 | /** |
| 53 | 53 | * The SQL WHERE statment |
| 54 | 54 | * @var string |
| 55 | - */ |
|
| 56 | - private $where = null; |
|
| 55 | + */ |
|
| 56 | + private $where = null; |
|
| 57 | 57 | |
| 58 | 58 | /** |
| 59 | 59 | * The SQL LIMIT statment |
| 60 | 60 | * @var string |
| 61 | - */ |
|
| 62 | - private $limit = null; |
|
| 61 | + */ |
|
| 62 | + private $limit = null; |
|
| 63 | 63 | |
| 64 | 64 | /** |
| 65 | 65 | * The SQL JOIN statment |
| 66 | 66 | * @var string |
| 67 | - */ |
|
| 68 | - private $join = null; |
|
| 67 | + */ |
|
| 68 | + private $join = null; |
|
| 69 | 69 | |
| 70 | 70 | /** |
| 71 | 71 | * The SQL ORDER BY statment |
| 72 | 72 | * @var string |
| 73 | - */ |
|
| 74 | - private $orderBy = null; |
|
| 73 | + */ |
|
| 74 | + private $orderBy = null; |
|
| 75 | 75 | |
| 76 | 76 | /** |
| 77 | 77 | * The SQL GROUP BY statment |
| 78 | 78 | * @var string |
| 79 | - */ |
|
| 80 | - private $groupBy = null; |
|
| 79 | + */ |
|
| 80 | + private $groupBy = null; |
|
| 81 | 81 | |
| 82 | 82 | /** |
| 83 | 83 | * The SQL HAVING statment |
| 84 | 84 | * @var string |
| 85 | - */ |
|
| 86 | - private $having = null; |
|
| 85 | + */ |
|
| 86 | + private $having = null; |
|
| 87 | 87 | |
| 88 | 88 | /** |
| 89 | 89 | * The number of rows returned by the last query |
| 90 | 90 | * @var int |
| 91 | - */ |
|
| 92 | - private $numRows = 0; |
|
| 91 | + */ |
|
| 92 | + private $numRows = 0; |
|
| 93 | 93 | |
| 94 | 94 | /** |
| 95 | 95 | * The last insert id for the primary key column that have auto increment or sequence |
| 96 | 96 | * @var mixed |
| 97 | - */ |
|
| 98 | - private $insertId = null; |
|
| 97 | + */ |
|
| 98 | + private $insertId = null; |
|
| 99 | 99 | |
| 100 | 100 | /** |
| 101 | 101 | * The full SQL query statment after build for each command |
| 102 | 102 | * @var string |
| 103 | - */ |
|
| 104 | - private $query = null; |
|
| 103 | + */ |
|
| 104 | + private $query = null; |
|
| 105 | 105 | |
| 106 | 106 | /** |
| 107 | 107 | * The error returned for the last query |
| 108 | 108 | * @var string |
| 109 | - */ |
|
| 110 | - private $error = null; |
|
| 109 | + */ |
|
| 110 | + private $error = null; |
|
| 111 | 111 | |
| 112 | 112 | /** |
| 113 | 113 | * The result returned for the last query |
| 114 | 114 | * @var mixed |
| 115 | - */ |
|
| 116 | - private $result = array(); |
|
| 115 | + */ |
|
| 116 | + private $result = array(); |
|
| 117 | 117 | |
| 118 | 118 | /** |
| 119 | 119 | * The prefix used in each database table |
| 120 | 120 | * @var string |
| 121 | - */ |
|
| 122 | - private $prefix = null; |
|
| 121 | + */ |
|
| 122 | + private $prefix = null; |
|
| 123 | 123 | |
| 124 | 124 | /** |
| 125 | 125 | * The list of SQL valid operators |
| 126 | 126 | * @var array |
| 127 | - */ |
|
| 128 | - private $operatorList = array('=','!=','<','>','<=','>=','<>'); |
|
| 127 | + */ |
|
| 128 | + private $operatorList = array('=','!=','<','>','<=','>=','<>'); |
|
| 129 | 129 | |
| 130 | 130 | /** |
| 131 | 131 | * The cache default time to live in second. 0 means no need to use the cache feature |
| 132 | 132 | * @var int |
| 133 | - */ |
|
| 133 | + */ |
|
| 134 | 134 | private $cacheTtl = 0; |
| 135 | 135 | |
| 136 | 136 | /** |
| 137 | 137 | * The cache current time to live. 0 means no need to use the cache feature |
| 138 | 138 | * @var int |
| 139 | - */ |
|
| 140 | - private $temporaryCacheTtl = 0; |
|
| 139 | + */ |
|
| 140 | + private $temporaryCacheTtl = 0; |
|
| 141 | 141 | |
| 142 | 142 | /** |
| 143 | 143 | * The number of executed query for the current request |
| 144 | 144 | * @var int |
| 145 | - */ |
|
| 146 | - private $queryCount = 0; |
|
| 145 | + */ |
|
| 146 | + private $queryCount = 0; |
|
| 147 | 147 | |
| 148 | 148 | /** |
| 149 | 149 | * The default data to be used in the statments query INSERT, UPDATE |
| 150 | 150 | * @var array |
| 151 | - */ |
|
| 152 | - private $data = array(); |
|
| 151 | + */ |
|
| 152 | + private $data = array(); |
|
| 153 | 153 | |
| 154 | 154 | /** |
| 155 | 155 | * The database configuration |
| 156 | 156 | * @var array |
| 157 | - */ |
|
| 158 | - private $config = array(); |
|
| 157 | + */ |
|
| 158 | + private $config = array(); |
|
| 159 | 159 | |
| 160 | 160 | /** |
| 161 | 161 | * The logger instance |
| 162 | 162 | * @var Log |
| 163 | 163 | */ |
| 164 | - private $logger = null; |
|
| 164 | + private $logger = null; |
|
| 165 | 165 | |
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * The cache instance |
|
| 169 | - * @var CacheInterface |
|
| 170 | - */ |
|
| 171 | - private $cacheInstance = null; |
|
| 167 | + /** |
|
| 168 | + * The cache instance |
|
| 169 | + * @var CacheInterface |
|
| 170 | + */ |
|
| 171 | + private $cacheInstance = null; |
|
| 172 | 172 | |
| 173 | - /** |
|
| 174 | - * The benchmark instance |
|
| 175 | - * @var Benchmark |
|
| 176 | - */ |
|
| 177 | - private $benchmarkInstance = null; |
|
| 173 | + /** |
|
| 174 | + * The benchmark instance |
|
| 175 | + * @var Benchmark |
|
| 176 | + */ |
|
| 177 | + private $benchmarkInstance = null; |
|
| 178 | 178 | |
| 179 | 179 | |
| 180 | - /** |
|
| 181 | - * Construct new database |
|
| 182 | - * @param array $overwriteConfig the config to overwrite with the config set in database.php |
|
| 183 | - */ |
|
| 184 | - public function __construct($overwriteConfig = array()){ |
|
| 185 | - //Set Log instance to use |
|
| 186 | - $this->setLoggerFromParamOrCreateNewInstance(null); |
|
| 180 | + /** |
|
| 181 | + * Construct new database |
|
| 182 | + * @param array $overwriteConfig the config to overwrite with the config set in database.php |
|
| 183 | + */ |
|
| 184 | + public function __construct($overwriteConfig = array()){ |
|
| 185 | + //Set Log instance to use |
|
| 186 | + $this->setLoggerFromParamOrCreateNewInstance(null); |
|
| 187 | 187 | |
| 188 | - //Set global configuration using the config file |
|
| 189 | - $this->setDatabaseConfigurationFromConfigFile($overwriteConfig); |
|
| 188 | + //Set global configuration using the config file |
|
| 189 | + $this->setDatabaseConfigurationFromConfigFile($overwriteConfig); |
|
| 190 | 190 | |
| 191 | - $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * This is used to connect to database |
|
| 196 | - * @return bool |
|
| 197 | - */ |
|
| 198 | - public function connect(){ |
|
| 199 | - $config = $this->getDatabaseConfiguration(); |
|
| 200 | - if(! empty($config)){ |
|
| 201 | - try{ |
|
| 202 | - $this->pdo = new PDO($this->getDsnFromDriver(), $config['username'], $config['password']); |
|
| 203 | - $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
|
| 204 | - $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
|
| 205 | - $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
|
| 206 | - return true; |
|
| 207 | - } |
|
| 208 | - catch (PDOException $e){ |
|
| 209 | - $this->logger->fatal($e->getMessage()); |
|
| 210 | - show_error('Cannot connect to Database.'); |
|
| 211 | - return false; |
|
| 212 | - } |
|
| 213 | - } |
|
| 214 | - else{ |
|
| 215 | - show_error('Database configuration is not set.'); |
|
| 216 | - return false; |
|
| 217 | - } |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * Set the SQL FROM statment |
|
| 222 | - * @param string|array $table the table name or array of table list |
|
| 223 | - * @return object the current Database instance |
|
| 224 | - */ |
|
| 225 | - public function from($table){ |
|
| 226 | - if(is_array($table)){ |
|
| 227 | - $froms = ''; |
|
| 228 | - foreach($table as $key){ |
|
| 229 | - $froms .= $this->prefix . $key . ', '; |
|
| 230 | - } |
|
| 231 | - $this->from = rtrim($froms, ', '); |
|
| 232 | - } |
|
| 233 | - else{ |
|
| 234 | - $this->from = $this->prefix . $table; |
|
| 235 | - } |
|
| 236 | - return $this; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Set the SQL SELECT statment |
|
| 241 | - * @param string|array $fields the field name or array of field list |
|
| 242 | - * @return object the current Database instance |
|
| 243 | - */ |
|
| 244 | - public function select($fields){ |
|
| 245 | - $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
|
| 246 | - $this->select = ($this->select == '*' ? $select : $this->select . ', ' . $select); |
|
| 247 | - return $this; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * Set the SQL SELECT DISTINCT statment |
|
| 252 | - * @param string $field the field name to distinct |
|
| 253 | - * @return object the current Database instance |
|
| 254 | - */ |
|
| 255 | - public function distinct($field){ |
|
| 256 | - $distinct = ' DISTINCT ' . $field; |
|
| 257 | - $this->select = ($this->select == '*' ? $distinct : $this->select . ', ' . $distinct); |
|
| 258 | - |
|
| 259 | - return $this; |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * Set the SQL function MAX in SELECT statment |
|
| 264 | - * @param string $field the field name |
|
| 265 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 266 | - * @return object the current Database instance |
|
| 267 | - */ |
|
| 268 | - public function max($field, $name = null){ |
|
| 269 | - $func = 'MAX(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 270 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 271 | - return $this; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * Set the SQL function MIN in SELECT statment |
|
| 276 | - * @param string $field the field name |
|
| 277 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 278 | - * @return object the current Database instance |
|
| 279 | - */ |
|
| 280 | - public function min($field, $name = null){ |
|
| 281 | - $func = 'MIN(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 282 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 283 | - return $this; |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * Set the SQL function SUM in SELECT statment |
|
| 288 | - * @param string $field the field name |
|
| 289 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 290 | - * @return object the current Database instance |
|
| 291 | - */ |
|
| 292 | - public function sum($field, $name = null){ |
|
| 293 | - $func = 'SUM(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 294 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 295 | - return $this; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * Set the SQL function COUNT in SELECT statment |
|
| 300 | - * @param string $field the field name |
|
| 301 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 302 | - * @return object the current Database instance |
|
| 303 | - */ |
|
| 304 | - public function count($field = '*', $name = null){ |
|
| 305 | - $func = 'COUNT(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 306 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 307 | - return $this; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * Set the SQL function AVG in SELECT statment |
|
| 312 | - * @param string $field the field name |
|
| 313 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 314 | - * @return object the current Database instance |
|
| 315 | - */ |
|
| 316 | - public function avg($field, $name = null){ |
|
| 317 | - $func = 'AVG(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 318 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 319 | - return $this; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * Set the SQL JOIN statment |
|
| 324 | - * @param string $table the join table name |
|
| 325 | - * @param string $field1 the first field for join conditions |
|
| 326 | - * @param string $op the join condition operator. If is null the default will be "=" |
|
| 327 | - * @param string $field2 the second field for join conditions |
|
| 328 | - * @param string $type the type of join (INNER, LEFT, RIGHT) |
|
| 329 | - * @return object the current Database instance |
|
| 330 | - */ |
|
| 331 | - public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){ |
|
| 332 | - $on = $field1; |
|
| 333 | - $table = $this->prefix . $table; |
|
| 334 | - if(! is_null($op)){ |
|
| 335 | - $on = (! in_array($op, $this->operatorList) ? $this->prefix . $field1 . ' = ' . $this->prefix . $op : $this->prefix . $field1 . ' ' . $op . ' ' . $this->prefix . $field2); |
|
| 336 | - } |
|
| 337 | - if (empty($this->join)){ |
|
| 338 | - $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
| 339 | - } |
|
| 340 | - else{ |
|
| 341 | - $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
| 342 | - } |
|
| 343 | - return $this; |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * Set the SQL INNER JOIN statment |
|
| 348 | - * @see Database::join() |
|
| 349 | - * @return object the current Database instance |
|
| 350 | - */ |
|
| 351 | - public function innerJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 352 | - return $this->join($table, $field1, $op, $field2, 'INNER '); |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * Set the SQL LEFT JOIN statment |
|
| 357 | - * @see Database::join() |
|
| 358 | - * @return object the current Database instance |
|
| 359 | - */ |
|
| 360 | - public function leftJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 361 | - return $this->join($table, $field1, $op, $field2, 'LEFT '); |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - /** |
|
| 365 | - * Set the SQL RIGHT JOIN statment |
|
| 366 | - * @see Database::join() |
|
| 367 | - * @return object the current Database instance |
|
| 368 | - */ |
|
| 369 | - public function rightJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 370 | - return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /** |
|
| 374 | - * Set the SQL FULL OUTER JOIN statment |
|
| 375 | - * @see Database::join() |
|
| 376 | - * @return object the current Database instance |
|
| 377 | - */ |
|
| 378 | - public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 379 | - return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * Set the SQL LEFT OUTER JOIN statment |
|
| 384 | - * @see Database::join() |
|
| 385 | - * @return object the current Database instance |
|
| 386 | - */ |
|
| 387 | - public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 388 | - return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - /** |
|
| 392 | - * Set the SQL RIGHT OUTER JOIN statment |
|
| 393 | - * @see Database::join() |
|
| 394 | - * @return object the current Database instance |
|
| 395 | - */ |
|
| 396 | - public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 397 | - return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - /** |
|
| 401 | - * Set the SQL WHERE CLAUSE for IS NULL |
|
| 402 | - * @param string|array $field the field name or array of field list |
|
| 403 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 404 | - * @return object the current Database instance |
|
| 405 | - */ |
|
| 406 | - public function whereIsNull($field, $andOr = 'AND'){ |
|
| 407 | - if(is_array($field)){ |
|
| 408 | - foreach($field as $f){ |
|
| 409 | - $this->whereIsNull($f, $andOr); |
|
| 410 | - } |
|
| 411 | - } |
|
| 412 | - else{ |
|
| 413 | - $this->setWhereStr($field.' IS NULL ', $andOr); |
|
| 414 | - } |
|
| 415 | - return $this; |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - /** |
|
| 419 | - * Set the SQL WHERE CLAUSE for IS NOT NULL |
|
| 420 | - * @param string|array $field the field name or array of field list |
|
| 421 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 422 | - * @return object the current Database instance |
|
| 423 | - */ |
|
| 424 | - public function whereIsNotNull($field, $andOr = 'AND'){ |
|
| 425 | - if(is_array($field)){ |
|
| 426 | - foreach($field as $f){ |
|
| 427 | - $this->whereIsNotNull($f, $andOr); |
|
| 428 | - } |
|
| 429 | - } |
|
| 430 | - else{ |
|
| 431 | - $this->setWhereStr($field.' IS NOT NULL ', $andOr); |
|
| 432 | - } |
|
| 433 | - return $this; |
|
| 434 | - } |
|
| 191 | + $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * This is used to connect to database |
|
| 196 | + * @return bool |
|
| 197 | + */ |
|
| 198 | + public function connect(){ |
|
| 199 | + $config = $this->getDatabaseConfiguration(); |
|
| 200 | + if(! empty($config)){ |
|
| 201 | + try{ |
|
| 202 | + $this->pdo = new PDO($this->getDsnFromDriver(), $config['username'], $config['password']); |
|
| 203 | + $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
|
| 204 | + $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
|
| 205 | + $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
|
| 206 | + return true; |
|
| 207 | + } |
|
| 208 | + catch (PDOException $e){ |
|
| 209 | + $this->logger->fatal($e->getMessage()); |
|
| 210 | + show_error('Cannot connect to Database.'); |
|
| 211 | + return false; |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | + else{ |
|
| 215 | + show_error('Database configuration is not set.'); |
|
| 216 | + return false; |
|
| 217 | + } |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * Set the SQL FROM statment |
|
| 222 | + * @param string|array $table the table name or array of table list |
|
| 223 | + * @return object the current Database instance |
|
| 224 | + */ |
|
| 225 | + public function from($table){ |
|
| 226 | + if(is_array($table)){ |
|
| 227 | + $froms = ''; |
|
| 228 | + foreach($table as $key){ |
|
| 229 | + $froms .= $this->prefix . $key . ', '; |
|
| 230 | + } |
|
| 231 | + $this->from = rtrim($froms, ', '); |
|
| 232 | + } |
|
| 233 | + else{ |
|
| 234 | + $this->from = $this->prefix . $table; |
|
| 235 | + } |
|
| 236 | + return $this; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Set the SQL SELECT statment |
|
| 241 | + * @param string|array $fields the field name or array of field list |
|
| 242 | + * @return object the current Database instance |
|
| 243 | + */ |
|
| 244 | + public function select($fields){ |
|
| 245 | + $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
|
| 246 | + $this->select = ($this->select == '*' ? $select : $this->select . ', ' . $select); |
|
| 247 | + return $this; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * Set the SQL SELECT DISTINCT statment |
|
| 252 | + * @param string $field the field name to distinct |
|
| 253 | + * @return object the current Database instance |
|
| 254 | + */ |
|
| 255 | + public function distinct($field){ |
|
| 256 | + $distinct = ' DISTINCT ' . $field; |
|
| 257 | + $this->select = ($this->select == '*' ? $distinct : $this->select . ', ' . $distinct); |
|
| 258 | + |
|
| 259 | + return $this; |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * Set the SQL function MAX in SELECT statment |
|
| 264 | + * @param string $field the field name |
|
| 265 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 266 | + * @return object the current Database instance |
|
| 267 | + */ |
|
| 268 | + public function max($field, $name = null){ |
|
| 269 | + $func = 'MAX(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 270 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 271 | + return $this; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * Set the SQL function MIN in SELECT statment |
|
| 276 | + * @param string $field the field name |
|
| 277 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 278 | + * @return object the current Database instance |
|
| 279 | + */ |
|
| 280 | + public function min($field, $name = null){ |
|
| 281 | + $func = 'MIN(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 282 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 283 | + return $this; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * Set the SQL function SUM in SELECT statment |
|
| 288 | + * @param string $field the field name |
|
| 289 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 290 | + * @return object the current Database instance |
|
| 291 | + */ |
|
| 292 | + public function sum($field, $name = null){ |
|
| 293 | + $func = 'SUM(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 294 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 295 | + return $this; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * Set the SQL function COUNT in SELECT statment |
|
| 300 | + * @param string $field the field name |
|
| 301 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 302 | + * @return object the current Database instance |
|
| 303 | + */ |
|
| 304 | + public function count($field = '*', $name = null){ |
|
| 305 | + $func = 'COUNT(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 306 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 307 | + return $this; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * Set the SQL function AVG in SELECT statment |
|
| 312 | + * @param string $field the field name |
|
| 313 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 314 | + * @return object the current Database instance |
|
| 315 | + */ |
|
| 316 | + public function avg($field, $name = null){ |
|
| 317 | + $func = 'AVG(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 318 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 319 | + return $this; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * Set the SQL JOIN statment |
|
| 324 | + * @param string $table the join table name |
|
| 325 | + * @param string $field1 the first field for join conditions |
|
| 326 | + * @param string $op the join condition operator. If is null the default will be "=" |
|
| 327 | + * @param string $field2 the second field for join conditions |
|
| 328 | + * @param string $type the type of join (INNER, LEFT, RIGHT) |
|
| 329 | + * @return object the current Database instance |
|
| 330 | + */ |
|
| 331 | + public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){ |
|
| 332 | + $on = $field1; |
|
| 333 | + $table = $this->prefix . $table; |
|
| 334 | + if(! is_null($op)){ |
|
| 335 | + $on = (! in_array($op, $this->operatorList) ? $this->prefix . $field1 . ' = ' . $this->prefix . $op : $this->prefix . $field1 . ' ' . $op . ' ' . $this->prefix . $field2); |
|
| 336 | + } |
|
| 337 | + if (empty($this->join)){ |
|
| 338 | + $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
| 339 | + } |
|
| 340 | + else{ |
|
| 341 | + $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
| 342 | + } |
|
| 343 | + return $this; |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * Set the SQL INNER JOIN statment |
|
| 348 | + * @see Database::join() |
|
| 349 | + * @return object the current Database instance |
|
| 350 | + */ |
|
| 351 | + public function innerJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 352 | + return $this->join($table, $field1, $op, $field2, 'INNER '); |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * Set the SQL LEFT JOIN statment |
|
| 357 | + * @see Database::join() |
|
| 358 | + * @return object the current Database instance |
|
| 359 | + */ |
|
| 360 | + public function leftJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 361 | + return $this->join($table, $field1, $op, $field2, 'LEFT '); |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + /** |
|
| 365 | + * Set the SQL RIGHT JOIN statment |
|
| 366 | + * @see Database::join() |
|
| 367 | + * @return object the current Database instance |
|
| 368 | + */ |
|
| 369 | + public function rightJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 370 | + return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | + * Set the SQL FULL OUTER JOIN statment |
|
| 375 | + * @see Database::join() |
|
| 376 | + * @return object the current Database instance |
|
| 377 | + */ |
|
| 378 | + public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 379 | + return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * Set the SQL LEFT OUTER JOIN statment |
|
| 384 | + * @see Database::join() |
|
| 385 | + * @return object the current Database instance |
|
| 386 | + */ |
|
| 387 | + public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 388 | + return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + /** |
|
| 392 | + * Set the SQL RIGHT OUTER JOIN statment |
|
| 393 | + * @see Database::join() |
|
| 394 | + * @return object the current Database instance |
|
| 395 | + */ |
|
| 396 | + public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 397 | + return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + /** |
|
| 401 | + * Set the SQL WHERE CLAUSE for IS NULL |
|
| 402 | + * @param string|array $field the field name or array of field list |
|
| 403 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 404 | + * @return object the current Database instance |
|
| 405 | + */ |
|
| 406 | + public function whereIsNull($field, $andOr = 'AND'){ |
|
| 407 | + if(is_array($field)){ |
|
| 408 | + foreach($field as $f){ |
|
| 409 | + $this->whereIsNull($f, $andOr); |
|
| 410 | + } |
|
| 411 | + } |
|
| 412 | + else{ |
|
| 413 | + $this->setWhereStr($field.' IS NULL ', $andOr); |
|
| 414 | + } |
|
| 415 | + return $this; |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + /** |
|
| 419 | + * Set the SQL WHERE CLAUSE for IS NOT NULL |
|
| 420 | + * @param string|array $field the field name or array of field list |
|
| 421 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 422 | + * @return object the current Database instance |
|
| 423 | + */ |
|
| 424 | + public function whereIsNotNull($field, $andOr = 'AND'){ |
|
| 425 | + if(is_array($field)){ |
|
| 426 | + foreach($field as $f){ |
|
| 427 | + $this->whereIsNotNull($f, $andOr); |
|
| 428 | + } |
|
| 429 | + } |
|
| 430 | + else{ |
|
| 431 | + $this->setWhereStr($field.' IS NOT NULL ', $andOr); |
|
| 432 | + } |
|
| 433 | + return $this; |
|
| 434 | + } |
|
| 435 | 435 | |
| 436 | - /** |
|
| 437 | - * Set the SQL WHERE CLAUSE statment |
|
| 438 | - * @param string|array $where the where field or array of field list |
|
| 439 | - * @param array|string $op the condition operator. If is null the default will be "=" |
|
| 440 | - * @param mixed $val the where value |
|
| 441 | - * @param string $type the type used for this where clause (NOT, etc.) |
|
| 442 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 443 | - * @param boolean $escape whether to escape or not the $val |
|
| 444 | - * @return object the current Database instance |
|
| 445 | - */ |
|
| 446 | - public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
|
| 447 | - $whereStr = ''; |
|
| 448 | - if (is_array($where)){ |
|
| 449 | - $whereStr = $this->getWhereStrIfIsArray($where, $type, $andOr, $escape); |
|
| 450 | - } |
|
| 451 | - else{ |
|
| 452 | - if(is_array($op)){ |
|
| 453 | - $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
|
| 454 | - } else { |
|
| 455 | - $whereStr = $this->getWhereStrForOperator($where, $op, $val, $type, $escape = true); |
|
| 456 | - } |
|
| 457 | - } |
|
| 458 | - $this->setWhereStr($whereStr, $andOr); |
|
| 459 | - return $this; |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - /** |
|
| 463 | - * Set the SQL WHERE CLAUSE statment using OR |
|
| 464 | - * @see Database::where() |
|
| 465 | - * @return object the current Database instance |
|
| 466 | - */ |
|
| 467 | - public function orWhere($where, $op = null, $val = null, $escape = true){ |
|
| 468 | - return $this->where($where, $op, $val, '', 'OR', $escape); |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - |
|
| 472 | - /** |
|
| 473 | - * Set the SQL WHERE CLAUSE statment using AND and NOT |
|
| 474 | - * @see Database::where() |
|
| 475 | - * @return object the current Database instance |
|
| 476 | - */ |
|
| 477 | - public function notWhere($where, $op = null, $val = null, $escape = true){ |
|
| 478 | - return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - /** |
|
| 482 | - * Set the SQL WHERE CLAUSE statment using OR and NOT |
|
| 483 | - * @see Database::where() |
|
| 484 | - * @return object the current Database instance |
|
| 485 | - */ |
|
| 486 | - public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
|
| 487 | - return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - /** |
|
| 491 | - * Set the opened parenthesis for the complex SQL query |
|
| 492 | - * @param string $type the type of this grouped (NOT, etc.) |
|
| 493 | - * @param string $andOr the multiple conditions separator (AND, OR, etc.) |
|
| 494 | - * @return object the current Database instance |
|
| 495 | - */ |
|
| 496 | - public function groupStart($type = '', $andOr = ' AND'){ |
|
| 497 | - if (empty($this->where)){ |
|
| 498 | - $this->where = $type . ' ('; |
|
| 499 | - } |
|
| 500 | - else{ |
|
| 501 | - if(substr($this->where, -1) == '('){ |
|
| 502 | - $this->where .= $type . ' ('; |
|
| 503 | - } |
|
| 504 | - else{ |
|
| 505 | - $this->where .= $andOr . ' ' . $type . ' ('; |
|
| 506 | - } |
|
| 507 | - } |
|
| 508 | - return $this; |
|
| 509 | - } |
|
| 510 | - |
|
| 511 | - /** |
|
| 512 | - * Set the opened parenthesis for the complex SQL query using NOT type |
|
| 513 | - * @see Database::groupStart() |
|
| 514 | - * @return object the current Database instance |
|
| 515 | - */ |
|
| 516 | - public function notGroupStart(){ |
|
| 517 | - return $this->groupStart('NOT'); |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - /** |
|
| 521 | - * Set the opened parenthesis for the complex SQL query using OR for separator |
|
| 522 | - * @see Database::groupStart() |
|
| 523 | - * @return object the current Database instance |
|
| 524 | - */ |
|
| 525 | - public function orGroupStart(){ |
|
| 526 | - return $this->groupStart('', ' OR'); |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - /** |
|
| 530 | - * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type |
|
| 531 | - * @see Database::groupStart() |
|
| 532 | - * @return object the current Database instance |
|
| 533 | - */ |
|
| 534 | - public function orNotGroupStart(){ |
|
| 535 | - return $this->groupStart('NOT', ' OR'); |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - /** |
|
| 539 | - * Close the parenthesis for the grouped SQL |
|
| 540 | - * @return object the current Database instance |
|
| 541 | - */ |
|
| 542 | - public function groupEnd(){ |
|
| 543 | - $this->where .= ')'; |
|
| 544 | - return $this; |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - /** |
|
| 548 | - * Set the SQL WHERE CLAUSE statment for IN |
|
| 549 | - * @param string $field the field name for IN statment |
|
| 550 | - * @param array $keys the list of values used |
|
| 551 | - * @param string $type the condition separator type (NOT) |
|
| 552 | - * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 553 | - * @param boolean $escape whether to escape or not the values |
|
| 554 | - * @return object the current Database instance |
|
| 555 | - */ |
|
| 556 | - public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
|
| 557 | - $_keys = array(); |
|
| 558 | - foreach ($keys as $k => $v){ |
|
| 559 | - if(is_null($v)){ |
|
| 560 | - $v = ''; |
|
| 561 | - } |
|
| 562 | - $_keys[] = (is_numeric($v) ? $v : $this->escape($v, $escape)); |
|
| 563 | - } |
|
| 564 | - $keys = implode(', ', $_keys); |
|
| 565 | - $whereStr = $field . ' ' . $type . ' IN (' . $keys . ')'; |
|
| 566 | - $this->setWhereStr($whereStr, $andOr); |
|
| 567 | - return $this; |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - /** |
|
| 571 | - * Set the SQL WHERE CLAUSE statment for NOT IN with AND separator |
|
| 572 | - * @see Database::in() |
|
| 573 | - * @return object the current Database instance |
|
| 574 | - */ |
|
| 575 | - public function notIn($field, array $keys, $escape = true){ |
|
| 576 | - return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
|
| 577 | - } |
|
| 578 | - |
|
| 579 | - /** |
|
| 580 | - * Set the SQL WHERE CLAUSE statment for IN with OR separator |
|
| 581 | - * @see Database::in() |
|
| 582 | - * @return object the current Database instance |
|
| 583 | - */ |
|
| 584 | - public function orIn($field, array $keys, $escape = true){ |
|
| 585 | - return $this->in($field, $keys, '', 'OR', $escape); |
|
| 586 | - } |
|
| 587 | - |
|
| 588 | - /** |
|
| 589 | - * Set the SQL WHERE CLAUSE statment for NOT IN with OR separator |
|
| 590 | - * @see Database::in() |
|
| 591 | - * @return object the current Database instance |
|
| 592 | - */ |
|
| 593 | - public function orNotIn($field, array $keys, $escape = true){ |
|
| 594 | - return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
|
| 595 | - } |
|
| 596 | - |
|
| 597 | - /** |
|
| 598 | - * Set the SQL WHERE CLAUSE statment for BETWEEN |
|
| 599 | - * @param string $field the field used for the BETWEEN statment |
|
| 600 | - * @param mixed $value1 the BETWEEN begin value |
|
| 601 | - * @param mixed $value2 the BETWEEN end value |
|
| 602 | - * @param string $type the condition separator type (NOT) |
|
| 603 | - * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 604 | - * @param boolean $escape whether to escape or not the values |
|
| 605 | - * @return object the current Database instance |
|
| 606 | - */ |
|
| 607 | - public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
|
| 608 | - if(is_null($value1)){ |
|
| 609 | - $value1 = ''; |
|
| 610 | - } |
|
| 611 | - if(is_null($value2)){ |
|
| 612 | - $value2 = ''; |
|
| 613 | - } |
|
| 614 | - $whereStr = $field . ' ' . $type . ' BETWEEN ' . $this->escape($value1, $escape) . ' AND ' . $this->escape($value2, $escape); |
|
| 615 | - $this->setWhereStr($whereStr, $andOr); |
|
| 616 | - return $this; |
|
| 617 | - } |
|
| 618 | - |
|
| 619 | - /** |
|
| 620 | - * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and AND separator |
|
| 621 | - * @see Database::between() |
|
| 622 | - * @return object the current Database instance |
|
| 623 | - */ |
|
| 624 | - public function notBetween($field, $value1, $value2, $escape = true){ |
|
| 625 | - return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
|
| 626 | - } |
|
| 627 | - |
|
| 628 | - /** |
|
| 629 | - * Set the SQL WHERE CLAUSE statment for BETWEEN with OR separator |
|
| 630 | - * @see Database::between() |
|
| 631 | - * @return object the current Database instance |
|
| 632 | - */ |
|
| 633 | - public function orBetween($field, $value1, $value2, $escape = true){ |
|
| 634 | - return $this->between($field, $value1, $value2, '', 'OR', $escape); |
|
| 635 | - } |
|
| 636 | - |
|
| 637 | - /** |
|
| 638 | - * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and OR separator |
|
| 639 | - * @see Database::between() |
|
| 640 | - * @return object the current Database instance |
|
| 641 | - */ |
|
| 642 | - public function orNotBetween($field, $value1, $value2, $escape = true){ |
|
| 643 | - return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
|
| 644 | - } |
|
| 645 | - |
|
| 646 | - /** |
|
| 647 | - * Set the SQL WHERE CLAUSE statment for LIKE |
|
| 648 | - * @param string $field the field name used in LIKE statment |
|
| 649 | - * @param string $data the LIKE value for this field including the '%', and '_' part |
|
| 650 | - * @param string $type the condition separator type (NOT) |
|
| 651 | - * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 652 | - * @param boolean $escape whether to escape or not the values |
|
| 653 | - * @return object the current Database instance |
|
| 654 | - */ |
|
| 655 | - public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
|
| 656 | - if(empty($data)){ |
|
| 657 | - $data = ''; |
|
| 658 | - } |
|
| 659 | - $this->setWhereStr($field . ' ' . $type . ' LIKE ' . ($this->escape($data, $escape)), $andOr); |
|
| 660 | - return $this; |
|
| 661 | - } |
|
| 662 | - |
|
| 663 | - /** |
|
| 664 | - * Set the SQL WHERE CLAUSE statment for LIKE with OR separator |
|
| 665 | - * @see Database::like() |
|
| 666 | - * @return object the current Database instance |
|
| 667 | - */ |
|
| 668 | - public function orLike($field, $data, $escape = true){ |
|
| 669 | - return $this->like($field, $data, '', 'OR', $escape); |
|
| 670 | - } |
|
| 671 | - |
|
| 672 | - /** |
|
| 673 | - * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and AND separator |
|
| 674 | - * @see Database::like() |
|
| 675 | - * @return object the current Database instance |
|
| 676 | - */ |
|
| 677 | - public function notLike($field, $data, $escape = true){ |
|
| 678 | - return $this->like($field, $data, 'NOT ', 'AND', $escape); |
|
| 679 | - } |
|
| 680 | - |
|
| 681 | - /** |
|
| 682 | - * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and OR separator |
|
| 683 | - * @see Database::like() |
|
| 684 | - * @return object the current Database instance |
|
| 685 | - */ |
|
| 686 | - public function orNotLike($field, $data, $escape = true){ |
|
| 687 | - return $this->like($field, $data, 'NOT ', 'OR', $escape); |
|
| 688 | - } |
|
| 689 | - |
|
| 690 | - /** |
|
| 691 | - * Set the SQL LIMIT statment |
|
| 692 | - * @param int $limit the limit offset. If $limitEnd is null this will be the limit count |
|
| 693 | - * like LIMIT n; |
|
| 694 | - * @param int $limitEnd the limit count |
|
| 695 | - * @return object the current Database instance |
|
| 696 | - */ |
|
| 697 | - public function limit($limit, $limitEnd = null){ |
|
| 698 | - if(empty($limit)){ |
|
| 699 | - return; |
|
| 700 | - } |
|
| 701 | - if (! is_null($limitEnd)){ |
|
| 702 | - $this->limit = $limit . ', ' . $limitEnd; |
|
| 703 | - } |
|
| 704 | - else{ |
|
| 705 | - $this->limit = $limit; |
|
| 706 | - } |
|
| 707 | - return $this; |
|
| 708 | - } |
|
| 709 | - |
|
| 710 | - /** |
|
| 711 | - * Set the SQL ORDER BY CLAUSE statment |
|
| 712 | - * @param string $orderBy the field name used for order |
|
| 713 | - * @param string $orderDir the order direction (ASC or DESC) |
|
| 714 | - * @return object the current Database instance |
|
| 715 | - */ |
|
| 716 | - public function orderBy($orderBy, $orderDir = ' ASC'){ |
|
| 717 | - if(stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
| 718 | - $this->orderBy = empty($this->orderBy) ? $orderBy : $this->orderBy . ', ' . $orderBy; |
|
| 719 | - } |
|
| 720 | - else{ |
|
| 721 | - $this->orderBy = empty($this->orderBy) ? ($orderBy . ' ' |
|
| 722 | - . strtoupper($orderDir)) : $this->orderBy |
|
| 723 | - . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
| 724 | - } |
|
| 725 | - return $this; |
|
| 726 | - } |
|
| 727 | - |
|
| 728 | - /** |
|
| 729 | - * Set the SQL GROUP BY CLAUSE statment |
|
| 730 | - * @param string|array $field the field name used or array of field list |
|
| 731 | - * @return object the current Database instance |
|
| 732 | - */ |
|
| 733 | - public function groupBy($field){ |
|
| 734 | - if(is_array($field)){ |
|
| 735 | - $this->groupBy = implode(', ', $field); |
|
| 736 | - } |
|
| 737 | - else{ |
|
| 738 | - $this->groupBy = $field; |
|
| 739 | - } |
|
| 740 | - return $this; |
|
| 741 | - } |
|
| 742 | - |
|
| 743 | - /** |
|
| 744 | - * Set the SQL HAVING CLAUSE statment |
|
| 745 | - * @param string $field the field name used for HAVING statment |
|
| 746 | - * @param string|array $op the operator used or array |
|
| 747 | - * @param mixed $val the value for HAVING comparaison |
|
| 748 | - * @param boolean $escape whether to escape or not the values |
|
| 749 | - * @return object the current Database instance |
|
| 750 | - */ |
|
| 751 | - public function having($field, $op = null, $val = null, $escape = true){ |
|
| 752 | - if(is_array($op)){ |
|
| 753 | - $x = explode('?', $field); |
|
| 754 | - $w = ''; |
|
| 755 | - foreach($x as $k => $v){ |
|
| 756 | - if(!empty($v)){ |
|
| 757 | - if(isset($op[$k]) && is_null($op[$k])){ |
|
| 758 | - $op[$k] = ''; |
|
| 759 | - } |
|
| 760 | - $w .= $v . (isset($op[$k]) ? $this->escape($op[$k], $escape) : ''); |
|
| 761 | - } |
|
| 762 | - } |
|
| 763 | - $this->having = $w; |
|
| 764 | - } |
|
| 765 | - else if (! in_array($op, $this->operatorList)){ |
|
| 766 | - if(is_null($op)){ |
|
| 767 | - $op = ''; |
|
| 768 | - } |
|
| 769 | - $this->having = $field . ' > ' . ($this->escape($op, $escape)); |
|
| 770 | - } |
|
| 771 | - else{ |
|
| 772 | - if(is_null($val)){ |
|
| 773 | - $val = ''; |
|
| 774 | - } |
|
| 775 | - $this->having = $field . ' ' . $op . ' ' . ($this->escape($val, $escape)); |
|
| 776 | - } |
|
| 777 | - return $this; |
|
| 778 | - } |
|
| 779 | - |
|
| 780 | - /** |
|
| 781 | - * Return the number of rows returned by the current query |
|
| 782 | - * @return int |
|
| 783 | - */ |
|
| 784 | - public function numRows(){ |
|
| 785 | - return $this->numRows; |
|
| 786 | - } |
|
| 787 | - |
|
| 788 | - /** |
|
| 789 | - * Return the last insert id value |
|
| 790 | - * @return mixed |
|
| 791 | - */ |
|
| 792 | - public function insertId(){ |
|
| 793 | - return $this->insertId; |
|
| 794 | - } |
|
| 795 | - |
|
| 796 | - /** |
|
| 797 | - * Show an error got from the current query (SQL command synthax error, database driver returned error, etc.) |
|
| 798 | - */ |
|
| 799 | - public function error(){ |
|
| 436 | + /** |
|
| 437 | + * Set the SQL WHERE CLAUSE statment |
|
| 438 | + * @param string|array $where the where field or array of field list |
|
| 439 | + * @param array|string $op the condition operator. If is null the default will be "=" |
|
| 440 | + * @param mixed $val the where value |
|
| 441 | + * @param string $type the type used for this where clause (NOT, etc.) |
|
| 442 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 443 | + * @param boolean $escape whether to escape or not the $val |
|
| 444 | + * @return object the current Database instance |
|
| 445 | + */ |
|
| 446 | + public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
|
| 447 | + $whereStr = ''; |
|
| 448 | + if (is_array($where)){ |
|
| 449 | + $whereStr = $this->getWhereStrIfIsArray($where, $type, $andOr, $escape); |
|
| 450 | + } |
|
| 451 | + else{ |
|
| 452 | + if(is_array($op)){ |
|
| 453 | + $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
|
| 454 | + } else { |
|
| 455 | + $whereStr = $this->getWhereStrForOperator($where, $op, $val, $type, $escape = true); |
|
| 456 | + } |
|
| 457 | + } |
|
| 458 | + $this->setWhereStr($whereStr, $andOr); |
|
| 459 | + return $this; |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + /** |
|
| 463 | + * Set the SQL WHERE CLAUSE statment using OR |
|
| 464 | + * @see Database::where() |
|
| 465 | + * @return object the current Database instance |
|
| 466 | + */ |
|
| 467 | + public function orWhere($where, $op = null, $val = null, $escape = true){ |
|
| 468 | + return $this->where($where, $op, $val, '', 'OR', $escape); |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + |
|
| 472 | + /** |
|
| 473 | + * Set the SQL WHERE CLAUSE statment using AND and NOT |
|
| 474 | + * @see Database::where() |
|
| 475 | + * @return object the current Database instance |
|
| 476 | + */ |
|
| 477 | + public function notWhere($where, $op = null, $val = null, $escape = true){ |
|
| 478 | + return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + /** |
|
| 482 | + * Set the SQL WHERE CLAUSE statment using OR and NOT |
|
| 483 | + * @see Database::where() |
|
| 484 | + * @return object the current Database instance |
|
| 485 | + */ |
|
| 486 | + public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
|
| 487 | + return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + /** |
|
| 491 | + * Set the opened parenthesis for the complex SQL query |
|
| 492 | + * @param string $type the type of this grouped (NOT, etc.) |
|
| 493 | + * @param string $andOr the multiple conditions separator (AND, OR, etc.) |
|
| 494 | + * @return object the current Database instance |
|
| 495 | + */ |
|
| 496 | + public function groupStart($type = '', $andOr = ' AND'){ |
|
| 497 | + if (empty($this->where)){ |
|
| 498 | + $this->where = $type . ' ('; |
|
| 499 | + } |
|
| 500 | + else{ |
|
| 501 | + if(substr($this->where, -1) == '('){ |
|
| 502 | + $this->where .= $type . ' ('; |
|
| 503 | + } |
|
| 504 | + else{ |
|
| 505 | + $this->where .= $andOr . ' ' . $type . ' ('; |
|
| 506 | + } |
|
| 507 | + } |
|
| 508 | + return $this; |
|
| 509 | + } |
|
| 510 | + |
|
| 511 | + /** |
|
| 512 | + * Set the opened parenthesis for the complex SQL query using NOT type |
|
| 513 | + * @see Database::groupStart() |
|
| 514 | + * @return object the current Database instance |
|
| 515 | + */ |
|
| 516 | + public function notGroupStart(){ |
|
| 517 | + return $this->groupStart('NOT'); |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + /** |
|
| 521 | + * Set the opened parenthesis for the complex SQL query using OR for separator |
|
| 522 | + * @see Database::groupStart() |
|
| 523 | + * @return object the current Database instance |
|
| 524 | + */ |
|
| 525 | + public function orGroupStart(){ |
|
| 526 | + return $this->groupStart('', ' OR'); |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + /** |
|
| 530 | + * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type |
|
| 531 | + * @see Database::groupStart() |
|
| 532 | + * @return object the current Database instance |
|
| 533 | + */ |
|
| 534 | + public function orNotGroupStart(){ |
|
| 535 | + return $this->groupStart('NOT', ' OR'); |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + /** |
|
| 539 | + * Close the parenthesis for the grouped SQL |
|
| 540 | + * @return object the current Database instance |
|
| 541 | + */ |
|
| 542 | + public function groupEnd(){ |
|
| 543 | + $this->where .= ')'; |
|
| 544 | + return $this; |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + /** |
|
| 548 | + * Set the SQL WHERE CLAUSE statment for IN |
|
| 549 | + * @param string $field the field name for IN statment |
|
| 550 | + * @param array $keys the list of values used |
|
| 551 | + * @param string $type the condition separator type (NOT) |
|
| 552 | + * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 553 | + * @param boolean $escape whether to escape or not the values |
|
| 554 | + * @return object the current Database instance |
|
| 555 | + */ |
|
| 556 | + public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
|
| 557 | + $_keys = array(); |
|
| 558 | + foreach ($keys as $k => $v){ |
|
| 559 | + if(is_null($v)){ |
|
| 560 | + $v = ''; |
|
| 561 | + } |
|
| 562 | + $_keys[] = (is_numeric($v) ? $v : $this->escape($v, $escape)); |
|
| 563 | + } |
|
| 564 | + $keys = implode(', ', $_keys); |
|
| 565 | + $whereStr = $field . ' ' . $type . ' IN (' . $keys . ')'; |
|
| 566 | + $this->setWhereStr($whereStr, $andOr); |
|
| 567 | + return $this; |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + /** |
|
| 571 | + * Set the SQL WHERE CLAUSE statment for NOT IN with AND separator |
|
| 572 | + * @see Database::in() |
|
| 573 | + * @return object the current Database instance |
|
| 574 | + */ |
|
| 575 | + public function notIn($field, array $keys, $escape = true){ |
|
| 576 | + return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
|
| 577 | + } |
|
| 578 | + |
|
| 579 | + /** |
|
| 580 | + * Set the SQL WHERE CLAUSE statment for IN with OR separator |
|
| 581 | + * @see Database::in() |
|
| 582 | + * @return object the current Database instance |
|
| 583 | + */ |
|
| 584 | + public function orIn($field, array $keys, $escape = true){ |
|
| 585 | + return $this->in($field, $keys, '', 'OR', $escape); |
|
| 586 | + } |
|
| 587 | + |
|
| 588 | + /** |
|
| 589 | + * Set the SQL WHERE CLAUSE statment for NOT IN with OR separator |
|
| 590 | + * @see Database::in() |
|
| 591 | + * @return object the current Database instance |
|
| 592 | + */ |
|
| 593 | + public function orNotIn($field, array $keys, $escape = true){ |
|
| 594 | + return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
|
| 595 | + } |
|
| 596 | + |
|
| 597 | + /** |
|
| 598 | + * Set the SQL WHERE CLAUSE statment for BETWEEN |
|
| 599 | + * @param string $field the field used for the BETWEEN statment |
|
| 600 | + * @param mixed $value1 the BETWEEN begin value |
|
| 601 | + * @param mixed $value2 the BETWEEN end value |
|
| 602 | + * @param string $type the condition separator type (NOT) |
|
| 603 | + * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 604 | + * @param boolean $escape whether to escape or not the values |
|
| 605 | + * @return object the current Database instance |
|
| 606 | + */ |
|
| 607 | + public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
|
| 608 | + if(is_null($value1)){ |
|
| 609 | + $value1 = ''; |
|
| 610 | + } |
|
| 611 | + if(is_null($value2)){ |
|
| 612 | + $value2 = ''; |
|
| 613 | + } |
|
| 614 | + $whereStr = $field . ' ' . $type . ' BETWEEN ' . $this->escape($value1, $escape) . ' AND ' . $this->escape($value2, $escape); |
|
| 615 | + $this->setWhereStr($whereStr, $andOr); |
|
| 616 | + return $this; |
|
| 617 | + } |
|
| 618 | + |
|
| 619 | + /** |
|
| 620 | + * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and AND separator |
|
| 621 | + * @see Database::between() |
|
| 622 | + * @return object the current Database instance |
|
| 623 | + */ |
|
| 624 | + public function notBetween($field, $value1, $value2, $escape = true){ |
|
| 625 | + return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + /** |
|
| 629 | + * Set the SQL WHERE CLAUSE statment for BETWEEN with OR separator |
|
| 630 | + * @see Database::between() |
|
| 631 | + * @return object the current Database instance |
|
| 632 | + */ |
|
| 633 | + public function orBetween($field, $value1, $value2, $escape = true){ |
|
| 634 | + return $this->between($field, $value1, $value2, '', 'OR', $escape); |
|
| 635 | + } |
|
| 636 | + |
|
| 637 | + /** |
|
| 638 | + * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and OR separator |
|
| 639 | + * @see Database::between() |
|
| 640 | + * @return object the current Database instance |
|
| 641 | + */ |
|
| 642 | + public function orNotBetween($field, $value1, $value2, $escape = true){ |
|
| 643 | + return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
|
| 644 | + } |
|
| 645 | + |
|
| 646 | + /** |
|
| 647 | + * Set the SQL WHERE CLAUSE statment for LIKE |
|
| 648 | + * @param string $field the field name used in LIKE statment |
|
| 649 | + * @param string $data the LIKE value for this field including the '%', and '_' part |
|
| 650 | + * @param string $type the condition separator type (NOT) |
|
| 651 | + * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 652 | + * @param boolean $escape whether to escape or not the values |
|
| 653 | + * @return object the current Database instance |
|
| 654 | + */ |
|
| 655 | + public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
|
| 656 | + if(empty($data)){ |
|
| 657 | + $data = ''; |
|
| 658 | + } |
|
| 659 | + $this->setWhereStr($field . ' ' . $type . ' LIKE ' . ($this->escape($data, $escape)), $andOr); |
|
| 660 | + return $this; |
|
| 661 | + } |
|
| 662 | + |
|
| 663 | + /** |
|
| 664 | + * Set the SQL WHERE CLAUSE statment for LIKE with OR separator |
|
| 665 | + * @see Database::like() |
|
| 666 | + * @return object the current Database instance |
|
| 667 | + */ |
|
| 668 | + public function orLike($field, $data, $escape = true){ |
|
| 669 | + return $this->like($field, $data, '', 'OR', $escape); |
|
| 670 | + } |
|
| 671 | + |
|
| 672 | + /** |
|
| 673 | + * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and AND separator |
|
| 674 | + * @see Database::like() |
|
| 675 | + * @return object the current Database instance |
|
| 676 | + */ |
|
| 677 | + public function notLike($field, $data, $escape = true){ |
|
| 678 | + return $this->like($field, $data, 'NOT ', 'AND', $escape); |
|
| 679 | + } |
|
| 680 | + |
|
| 681 | + /** |
|
| 682 | + * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and OR separator |
|
| 683 | + * @see Database::like() |
|
| 684 | + * @return object the current Database instance |
|
| 685 | + */ |
|
| 686 | + public function orNotLike($field, $data, $escape = true){ |
|
| 687 | + return $this->like($field, $data, 'NOT ', 'OR', $escape); |
|
| 688 | + } |
|
| 689 | + |
|
| 690 | + /** |
|
| 691 | + * Set the SQL LIMIT statment |
|
| 692 | + * @param int $limit the limit offset. If $limitEnd is null this will be the limit count |
|
| 693 | + * like LIMIT n; |
|
| 694 | + * @param int $limitEnd the limit count |
|
| 695 | + * @return object the current Database instance |
|
| 696 | + */ |
|
| 697 | + public function limit($limit, $limitEnd = null){ |
|
| 698 | + if(empty($limit)){ |
|
| 699 | + return; |
|
| 700 | + } |
|
| 701 | + if (! is_null($limitEnd)){ |
|
| 702 | + $this->limit = $limit . ', ' . $limitEnd; |
|
| 703 | + } |
|
| 704 | + else{ |
|
| 705 | + $this->limit = $limit; |
|
| 706 | + } |
|
| 707 | + return $this; |
|
| 708 | + } |
|
| 709 | + |
|
| 710 | + /** |
|
| 711 | + * Set the SQL ORDER BY CLAUSE statment |
|
| 712 | + * @param string $orderBy the field name used for order |
|
| 713 | + * @param string $orderDir the order direction (ASC or DESC) |
|
| 714 | + * @return object the current Database instance |
|
| 715 | + */ |
|
| 716 | + public function orderBy($orderBy, $orderDir = ' ASC'){ |
|
| 717 | + if(stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
| 718 | + $this->orderBy = empty($this->orderBy) ? $orderBy : $this->orderBy . ', ' . $orderBy; |
|
| 719 | + } |
|
| 720 | + else{ |
|
| 721 | + $this->orderBy = empty($this->orderBy) ? ($orderBy . ' ' |
|
| 722 | + . strtoupper($orderDir)) : $this->orderBy |
|
| 723 | + . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
| 724 | + } |
|
| 725 | + return $this; |
|
| 726 | + } |
|
| 727 | + |
|
| 728 | + /** |
|
| 729 | + * Set the SQL GROUP BY CLAUSE statment |
|
| 730 | + * @param string|array $field the field name used or array of field list |
|
| 731 | + * @return object the current Database instance |
|
| 732 | + */ |
|
| 733 | + public function groupBy($field){ |
|
| 734 | + if(is_array($field)){ |
|
| 735 | + $this->groupBy = implode(', ', $field); |
|
| 736 | + } |
|
| 737 | + else{ |
|
| 738 | + $this->groupBy = $field; |
|
| 739 | + } |
|
| 740 | + return $this; |
|
| 741 | + } |
|
| 742 | + |
|
| 743 | + /** |
|
| 744 | + * Set the SQL HAVING CLAUSE statment |
|
| 745 | + * @param string $field the field name used for HAVING statment |
|
| 746 | + * @param string|array $op the operator used or array |
|
| 747 | + * @param mixed $val the value for HAVING comparaison |
|
| 748 | + * @param boolean $escape whether to escape or not the values |
|
| 749 | + * @return object the current Database instance |
|
| 750 | + */ |
|
| 751 | + public function having($field, $op = null, $val = null, $escape = true){ |
|
| 752 | + if(is_array($op)){ |
|
| 753 | + $x = explode('?', $field); |
|
| 754 | + $w = ''; |
|
| 755 | + foreach($x as $k => $v){ |
|
| 756 | + if(!empty($v)){ |
|
| 757 | + if(isset($op[$k]) && is_null($op[$k])){ |
|
| 758 | + $op[$k] = ''; |
|
| 759 | + } |
|
| 760 | + $w .= $v . (isset($op[$k]) ? $this->escape($op[$k], $escape) : ''); |
|
| 761 | + } |
|
| 762 | + } |
|
| 763 | + $this->having = $w; |
|
| 764 | + } |
|
| 765 | + else if (! in_array($op, $this->operatorList)){ |
|
| 766 | + if(is_null($op)){ |
|
| 767 | + $op = ''; |
|
| 768 | + } |
|
| 769 | + $this->having = $field . ' > ' . ($this->escape($op, $escape)); |
|
| 770 | + } |
|
| 771 | + else{ |
|
| 772 | + if(is_null($val)){ |
|
| 773 | + $val = ''; |
|
| 774 | + } |
|
| 775 | + $this->having = $field . ' ' . $op . ' ' . ($this->escape($val, $escape)); |
|
| 776 | + } |
|
| 777 | + return $this; |
|
| 778 | + } |
|
| 779 | + |
|
| 780 | + /** |
|
| 781 | + * Return the number of rows returned by the current query |
|
| 782 | + * @return int |
|
| 783 | + */ |
|
| 784 | + public function numRows(){ |
|
| 785 | + return $this->numRows; |
|
| 786 | + } |
|
| 787 | + |
|
| 788 | + /** |
|
| 789 | + * Return the last insert id value |
|
| 790 | + * @return mixed |
|
| 791 | + */ |
|
| 792 | + public function insertId(){ |
|
| 793 | + return $this->insertId; |
|
| 794 | + } |
|
| 795 | + |
|
| 796 | + /** |
|
| 797 | + * Show an error got from the current query (SQL command synthax error, database driver returned error, etc.) |
|
| 798 | + */ |
|
| 799 | + public function error(){ |
|
| 800 | 800 | if($this->error){ |
| 801 | 801 | show_error('Query: "' . $this->query . '" Error: ' . $this->error, 'Database Error'); |
| 802 | 802 | } |
| 803 | - } |
|
| 804 | - |
|
| 805 | - /** |
|
| 806 | - * Get the result of one record rows returned by the current query |
|
| 807 | - * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 808 | - * If is string will determine the result type "array" or "object" |
|
| 809 | - * @return mixed the query SQL string or the record result |
|
| 810 | - */ |
|
| 811 | - public function get($returnSQLQueryOrResultType = false){ |
|
| 812 | - $this->limit = 1; |
|
| 813 | - $query = $this->getAll(true); |
|
| 814 | - if($returnSQLQueryOrResultType === true){ |
|
| 815 | - return $query; |
|
| 816 | - } |
|
| 817 | - else{ |
|
| 818 | - return $this->query( $query, false, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 819 | - } |
|
| 820 | - } |
|
| 821 | - |
|
| 822 | - /** |
|
| 823 | - * Get the result of record rows list returned by the current query |
|
| 824 | - * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 825 | - * If is string will determine the result type "array" or "object" |
|
| 826 | - * @return mixed the query SQL string or the record result |
|
| 827 | - */ |
|
| 828 | - public function getAll($returnSQLQueryOrResultType = false){ |
|
| 829 | - $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
|
| 830 | - if (! empty($this->join)){ |
|
| 831 | - $query .= $this->join; |
|
| 832 | - } |
|
| 803 | + } |
|
| 804 | + |
|
| 805 | + /** |
|
| 806 | + * Get the result of one record rows returned by the current query |
|
| 807 | + * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 808 | + * If is string will determine the result type "array" or "object" |
|
| 809 | + * @return mixed the query SQL string or the record result |
|
| 810 | + */ |
|
| 811 | + public function get($returnSQLQueryOrResultType = false){ |
|
| 812 | + $this->limit = 1; |
|
| 813 | + $query = $this->getAll(true); |
|
| 814 | + if($returnSQLQueryOrResultType === true){ |
|
| 815 | + return $query; |
|
| 816 | + } |
|
| 817 | + else{ |
|
| 818 | + return $this->query( $query, false, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 819 | + } |
|
| 820 | + } |
|
| 821 | + |
|
| 822 | + /** |
|
| 823 | + * Get the result of record rows list returned by the current query |
|
| 824 | + * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 825 | + * If is string will determine the result type "array" or "object" |
|
| 826 | + * @return mixed the query SQL string or the record result |
|
| 827 | + */ |
|
| 828 | + public function getAll($returnSQLQueryOrResultType = false){ |
|
| 829 | + $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
|
| 830 | + if (! empty($this->join)){ |
|
| 831 | + $query .= $this->join; |
|
| 832 | + } |
|
| 833 | 833 | |
| 834 | - if (! empty($this->where)){ |
|
| 835 | - $query .= ' WHERE ' . $this->where; |
|
| 836 | - } |
|
| 834 | + if (! empty($this->where)){ |
|
| 835 | + $query .= ' WHERE ' . $this->where; |
|
| 836 | + } |
|
| 837 | 837 | |
| 838 | - if (! empty($this->groupBy)){ |
|
| 839 | - $query .= ' GROUP BY ' . $this->groupBy; |
|
| 840 | - } |
|
| 838 | + if (! empty($this->groupBy)){ |
|
| 839 | + $query .= ' GROUP BY ' . $this->groupBy; |
|
| 840 | + } |
|
| 841 | 841 | |
| 842 | - if (! empty($this->having)){ |
|
| 843 | - $query .= ' HAVING ' . $this->having; |
|
| 844 | - } |
|
| 842 | + if (! empty($this->having)){ |
|
| 843 | + $query .= ' HAVING ' . $this->having; |
|
| 844 | + } |
|
| 845 | 845 | |
| 846 | - if (! empty($this->orderBy)){ |
|
| 847 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
| 848 | - } |
|
| 846 | + if (! empty($this->orderBy)){ |
|
| 847 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
| 848 | + } |
|
| 849 | 849 | |
| 850 | - if(! empty($this->limit)){ |
|
| 851 | - $query .= ' LIMIT ' . $this->limit; |
|
| 852 | - } |
|
| 850 | + if(! empty($this->limit)){ |
|
| 851 | + $query .= ' LIMIT ' . $this->limit; |
|
| 852 | + } |
|
| 853 | 853 | |
| 854 | 854 | if($returnSQLQueryOrResultType === true){ |
| 855 | - return $query; |
|
| 856 | - } |
|
| 857 | - else{ |
|
| 858 | - return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 859 | - } |
|
| 860 | - } |
|
| 861 | - |
|
| 862 | - /** |
|
| 863 | - * Insert new record in the database |
|
| 864 | - * @param array $data the record data if is empty will use the $this->data array. |
|
| 865 | - * @param boolean $escape whether to escape or not the values |
|
| 866 | - * @return mixed the insert id of the new record or null |
|
| 867 | - */ |
|
| 868 | - public function insert($data = array(), $escape = true){ |
|
| 869 | - $column = array(); |
|
| 870 | - $val = array(); |
|
| 871 | - if(empty($data) && $this->getData()){ |
|
| 872 | - //as when using $this->setData() the data already escaped |
|
| 873 | - $escape = false; |
|
| 874 | - $data = $this->getData(); |
|
| 875 | - } |
|
| 876 | - |
|
| 877 | - $columns = array_keys($data); |
|
| 878 | - $column = implode(',', $columns); |
|
| 879 | - $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
|
| 880 | - |
|
| 881 | - $query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
|
| 882 | - $query = $this->query($query); |
|
| 883 | - |
|
| 884 | - if ($query){ |
|
| 885 | - if(! $this->pdo){ |
|
| 886 | - $this->connect(); |
|
| 887 | - } |
|
| 888 | - $this->insertId = $this->pdo->lastInsertId(); |
|
| 889 | - return $this->insertId(); |
|
| 890 | - } |
|
| 891 | - else{ |
|
| 855 | + return $query; |
|
| 856 | + } |
|
| 857 | + else{ |
|
| 858 | + return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 859 | + } |
|
| 860 | + } |
|
| 861 | + |
|
| 862 | + /** |
|
| 863 | + * Insert new record in the database |
|
| 864 | + * @param array $data the record data if is empty will use the $this->data array. |
|
| 865 | + * @param boolean $escape whether to escape or not the values |
|
| 866 | + * @return mixed the insert id of the new record or null |
|
| 867 | + */ |
|
| 868 | + public function insert($data = array(), $escape = true){ |
|
| 869 | + $column = array(); |
|
| 870 | + $val = array(); |
|
| 871 | + if(empty($data) && $this->getData()){ |
|
| 872 | + //as when using $this->setData() the data already escaped |
|
| 873 | + $escape = false; |
|
| 874 | + $data = $this->getData(); |
|
| 875 | + } |
|
| 876 | + |
|
| 877 | + $columns = array_keys($data); |
|
| 878 | + $column = implode(',', $columns); |
|
| 879 | + $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
|
| 880 | + |
|
| 881 | + $query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
|
| 882 | + $query = $this->query($query); |
|
| 883 | + |
|
| 884 | + if ($query){ |
|
| 885 | + if(! $this->pdo){ |
|
| 886 | + $this->connect(); |
|
| 887 | + } |
|
| 888 | + $this->insertId = $this->pdo->lastInsertId(); |
|
| 889 | + return $this->insertId(); |
|
| 890 | + } |
|
| 891 | + else{ |
|
| 892 | 892 | return false; |
| 893 | - } |
|
| 894 | - } |
|
| 895 | - |
|
| 896 | - /** |
|
| 897 | - * Update record in the database |
|
| 898 | - * @param array $data the record data if is empty will use the $this->data array. |
|
| 899 | - * @param boolean $escape whether to escape or not the values |
|
| 900 | - * @return mixed the update status |
|
| 901 | - */ |
|
| 902 | - public function update($data = array(), $escape = true){ |
|
| 903 | - $query = 'UPDATE ' . $this->from . ' SET '; |
|
| 904 | - $values = array(); |
|
| 905 | - if(empty($data) && $this->getData()){ |
|
| 906 | - //as when using $this->setData() the data already escaped |
|
| 907 | - $escape = false; |
|
| 908 | - $data = $this->getData(); |
|
| 909 | - } |
|
| 910 | - foreach ($data as $column => $val){ |
|
| 911 | - $values[] = $column . ' = ' . ($this->escape($val, $escape)); |
|
| 912 | - } |
|
| 913 | - $query .= implode(', ', $values); |
|
| 914 | - if (! empty($this->where)){ |
|
| 915 | - $query .= ' WHERE ' . $this->where; |
|
| 916 | - } |
|
| 917 | - |
|
| 918 | - if (! empty($this->orderBy)){ |
|
| 919 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
| 920 | - } |
|
| 921 | - |
|
| 922 | - if (! empty($this->limit)){ |
|
| 923 | - $query .= ' LIMIT ' . $this->limit; |
|
| 924 | - } |
|
| 925 | - return $this->query($query); |
|
| 926 | - } |
|
| 927 | - |
|
| 928 | - /** |
|
| 929 | - * Delete the record in database |
|
| 930 | - * @return mixed the delete status |
|
| 931 | - */ |
|
| 932 | - public function delete(){ |
|
| 933 | - $query = 'DELETE FROM ' . $this->from; |
|
| 934 | - |
|
| 935 | - if (! empty($this->where)){ |
|
| 936 | - $query .= ' WHERE ' . $this->where; |
|
| 937 | - } |
|
| 938 | - |
|
| 939 | - if (! empty($this->orderBy)){ |
|
| 940 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
| 941 | - } |
|
| 942 | - |
|
| 943 | - if (! empty($this->limit)){ |
|
| 944 | - $query .= ' LIMIT ' . $this->limit; |
|
| 945 | - } |
|
| 946 | - |
|
| 947 | - if($query == 'DELETE FROM ' . $this->from && $this->config['driver'] != 'sqlite'){ |
|
| 948 | - $query = 'TRUNCATE TABLE ' . $this->from; |
|
| 949 | - } |
|
| 950 | - return $this->query($query); |
|
| 951 | - } |
|
| 952 | - |
|
| 953 | - |
|
| 954 | - /** |
|
| 955 | - * Execute an SQL query |
|
| 956 | - * @param string $query the query SQL string |
|
| 957 | - * @param boolean|array $all if boolean this indicate whether to return all record or not, if array |
|
| 958 | - * will |
|
| 959 | - * @param boolean $array return the result as array |
|
| 960 | - * @return mixed the query result |
|
| 961 | - */ |
|
| 962 | - public function query($query, $all = true, $array = false){ |
|
| 963 | - $this->reset(); |
|
| 964 | - $query = $this->transformPreparedQuery($query, $all); |
|
| 965 | - $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
| 893 | + } |
|
| 894 | + } |
|
| 895 | + |
|
| 896 | + /** |
|
| 897 | + * Update record in the database |
|
| 898 | + * @param array $data the record data if is empty will use the $this->data array. |
|
| 899 | + * @param boolean $escape whether to escape or not the values |
|
| 900 | + * @return mixed the update status |
|
| 901 | + */ |
|
| 902 | + public function update($data = array(), $escape = true){ |
|
| 903 | + $query = 'UPDATE ' . $this->from . ' SET '; |
|
| 904 | + $values = array(); |
|
| 905 | + if(empty($data) && $this->getData()){ |
|
| 906 | + //as when using $this->setData() the data already escaped |
|
| 907 | + $escape = false; |
|
| 908 | + $data = $this->getData(); |
|
| 909 | + } |
|
| 910 | + foreach ($data as $column => $val){ |
|
| 911 | + $values[] = $column . ' = ' . ($this->escape($val, $escape)); |
|
| 912 | + } |
|
| 913 | + $query .= implode(', ', $values); |
|
| 914 | + if (! empty($this->where)){ |
|
| 915 | + $query .= ' WHERE ' . $this->where; |
|
| 916 | + } |
|
| 917 | + |
|
| 918 | + if (! empty($this->orderBy)){ |
|
| 919 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
| 920 | + } |
|
| 921 | + |
|
| 922 | + if (! empty($this->limit)){ |
|
| 923 | + $query .= ' LIMIT ' . $this->limit; |
|
| 924 | + } |
|
| 925 | + return $this->query($query); |
|
| 926 | + } |
|
| 927 | + |
|
| 928 | + /** |
|
| 929 | + * Delete the record in database |
|
| 930 | + * @return mixed the delete status |
|
| 931 | + */ |
|
| 932 | + public function delete(){ |
|
| 933 | + $query = 'DELETE FROM ' . $this->from; |
|
| 934 | + |
|
| 935 | + if (! empty($this->where)){ |
|
| 936 | + $query .= ' WHERE ' . $this->where; |
|
| 937 | + } |
|
| 938 | + |
|
| 939 | + if (! empty($this->orderBy)){ |
|
| 940 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
| 941 | + } |
|
| 942 | + |
|
| 943 | + if (! empty($this->limit)){ |
|
| 944 | + $query .= ' LIMIT ' . $this->limit; |
|
| 945 | + } |
|
| 946 | + |
|
| 947 | + if($query == 'DELETE FROM ' . $this->from && $this->config['driver'] != 'sqlite'){ |
|
| 948 | + $query = 'TRUNCATE TABLE ' . $this->from; |
|
| 949 | + } |
|
| 950 | + return $this->query($query); |
|
| 951 | + } |
|
| 952 | + |
|
| 953 | + |
|
| 954 | + /** |
|
| 955 | + * Execute an SQL query |
|
| 956 | + * @param string $query the query SQL string |
|
| 957 | + * @param boolean|array $all if boolean this indicate whether to return all record or not, if array |
|
| 958 | + * will |
|
| 959 | + * @param boolean $array return the result as array |
|
| 960 | + * @return mixed the query result |
|
| 961 | + */ |
|
| 962 | + public function query($query, $all = true, $array = false){ |
|
| 963 | + $this->reset(); |
|
| 964 | + $query = $this->transformPreparedQuery($query, $all); |
|
| 965 | + $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
| 966 | 966 | |
| 967 | - $isSqlSELECTQuery = stristr($this->query, 'SELECT'); |
|
| 967 | + $isSqlSELECTQuery = stristr($this->query, 'SELECT'); |
|
| 968 | 968 | |
| 969 | - $this->logger->info('Execute SQL query ['.$this->query.'], return type: ' . ($array?'ARRAY':'OBJECT') .', return as list: ' . ($all ? 'YES':'NO')); |
|
| 970 | - //cache expire time |
|
| 969 | + $this->logger->info('Execute SQL query ['.$this->query.'], return type: ' . ($array?'ARRAY':'OBJECT') .', return as list: ' . ($all ? 'YES':'NO')); |
|
| 970 | + //cache expire time |
|
| 971 | 971 | $cacheExpire = $this->temporaryCacheTtl; |
| 972 | 972 | |
| 973 | 973 | //return to the initial cache time |
| 974 | 974 | $this->temporaryCacheTtl = $this->cacheTtl; |
| 975 | 975 | |
| 976 | 976 | //config for cache |
| 977 | - $cacheEnable = get_config('cache_enable'); |
|
| 977 | + $cacheEnable = get_config('cache_enable'); |
|
| 978 | 978 | |
| 979 | 979 | //the database cache content |
| 980 | - $cacheContent = null; |
|
| 980 | + $cacheContent = null; |
|
| 981 | 981 | |
| 982 | 982 | //if can use cache feature for this query |
| 983 | 983 | $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
| 984 | 984 | |
| 985 | - if ($dbCacheStatus && $isSqlSELECTQuery){ |
|
| 986 | - $cacheContent = $this->getCacheContentForQuery($query, $all, $array); |
|
| 987 | - } |
|
| 988 | - else{ |
|
| 989 | - $this->logger->info('The cache is not enabled for this query or is not the SELECT query, get the result directly from real database'); |
|
| 990 | - } |
|
| 985 | + if ($dbCacheStatus && $isSqlSELECTQuery){ |
|
| 986 | + $cacheContent = $this->getCacheContentForQuery($query, $all, $array); |
|
| 987 | + } |
|
| 988 | + else{ |
|
| 989 | + $this->logger->info('The cache is not enabled for this query or is not the SELECT query, get the result directly from real database'); |
|
| 990 | + } |
|
| 991 | 991 | |
| 992 | - if (! $cacheContent && $isSqlSELECTQuery){ |
|
| 993 | - $sqlQuery = $this->runSqlQuery($query, $all, $array); |
|
| 994 | - if ($sqlQuery){ |
|
| 995 | - $this->setQueryResultForSelect($sqlQuery, $all, $array); |
|
| 996 | - $this->setCacheContentForQuery( |
|
| 997 | - $this->query, |
|
| 998 | - $this->getCacheBenchmarkKeyForQuery($this->query, $all, $array), |
|
| 999 | - $this->result, |
|
| 1000 | - $dbCacheStatus && $isSqlSELECTQuery, |
|
| 1001 | - $this->temporaryCacheTtl |
|
| 1002 | - ); |
|
| 1003 | - } |
|
| 1004 | - } |
|
| 1005 | - else if ((! $cacheContent && !$isSqlSELECTQuery) || ($cacheContent && !$isSqlSELECTQuery)){ |
|
| 1006 | - $sqlQuery = $this->runSqlQuery($query, $all, $array); |
|
| 1007 | - if($sqlQuery){ |
|
| 1008 | - $this->setQueryResultForNonSelect($sqlQuery, $all, $array); |
|
| 1009 | - } |
|
| 1010 | - if (! $this->result){ |
|
| 1011 | - $this->setQueryError(); |
|
| 1012 | - } |
|
| 1013 | - } |
|
| 1014 | - else{ |
|
| 1015 | - $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 1016 | - $this->result = $cacheContent; |
|
| 1017 | - $this->numRows = count($this->result); |
|
| 1018 | - } |
|
| 1019 | - $this->queryCount++; |
|
| 1020 | - if(! $this->result){ |
|
| 1021 | - $this->logger->info('No result where found for the query [' . $query . ']'); |
|
| 1022 | - } |
|
| 1023 | - return $this->result; |
|
| 1024 | - } |
|
| 1025 | - |
|
| 1026 | - /** |
|
| 1027 | - * Set database cache time to live |
|
| 1028 | - * @param integer $ttl the cache time to live in second |
|
| 1029 | - * @return object the current Database instance |
|
| 1030 | - */ |
|
| 1031 | - public function setCache($ttl = 0){ |
|
| 1032 | - if($ttl > 0){ |
|
| 1033 | - $this->cacheTtl = $ttl; |
|
| 1034 | - $this->temporaryCacheTtl = $ttl; |
|
| 1035 | - } |
|
| 1036 | - return $this; |
|
| 1037 | - } |
|
| 992 | + if (! $cacheContent && $isSqlSELECTQuery){ |
|
| 993 | + $sqlQuery = $this->runSqlQuery($query, $all, $array); |
|
| 994 | + if ($sqlQuery){ |
|
| 995 | + $this->setQueryResultForSelect($sqlQuery, $all, $array); |
|
| 996 | + $this->setCacheContentForQuery( |
|
| 997 | + $this->query, |
|
| 998 | + $this->getCacheBenchmarkKeyForQuery($this->query, $all, $array), |
|
| 999 | + $this->result, |
|
| 1000 | + $dbCacheStatus && $isSqlSELECTQuery, |
|
| 1001 | + $this->temporaryCacheTtl |
|
| 1002 | + ); |
|
| 1003 | + } |
|
| 1004 | + } |
|
| 1005 | + else if ((! $cacheContent && !$isSqlSELECTQuery) || ($cacheContent && !$isSqlSELECTQuery)){ |
|
| 1006 | + $sqlQuery = $this->runSqlQuery($query, $all, $array); |
|
| 1007 | + if($sqlQuery){ |
|
| 1008 | + $this->setQueryResultForNonSelect($sqlQuery, $all, $array); |
|
| 1009 | + } |
|
| 1010 | + if (! $this->result){ |
|
| 1011 | + $this->setQueryError(); |
|
| 1012 | + } |
|
| 1013 | + } |
|
| 1014 | + else{ |
|
| 1015 | + $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 1016 | + $this->result = $cacheContent; |
|
| 1017 | + $this->numRows = count($this->result); |
|
| 1018 | + } |
|
| 1019 | + $this->queryCount++; |
|
| 1020 | + if(! $this->result){ |
|
| 1021 | + $this->logger->info('No result where found for the query [' . $query . ']'); |
|
| 1022 | + } |
|
| 1023 | + return $this->result; |
|
| 1024 | + } |
|
| 1025 | + |
|
| 1026 | + /** |
|
| 1027 | + * Set database cache time to live |
|
| 1028 | + * @param integer $ttl the cache time to live in second |
|
| 1029 | + * @return object the current Database instance |
|
| 1030 | + */ |
|
| 1031 | + public function setCache($ttl = 0){ |
|
| 1032 | + if($ttl > 0){ |
|
| 1033 | + $this->cacheTtl = $ttl; |
|
| 1034 | + $this->temporaryCacheTtl = $ttl; |
|
| 1035 | + } |
|
| 1036 | + return $this; |
|
| 1037 | + } |
|
| 1038 | 1038 | |
| 1039 | 1039 | /** |
| 1040 | 1040 | * Enabled cache temporary for the current query not globally |
@@ -1042,493 +1042,493 @@ discard block |
||
| 1042 | 1042 | * @return object the current Database instance |
| 1043 | 1043 | */ |
| 1044 | 1044 | public function cached($ttl = 0){ |
| 1045 | - if($ttl > 0){ |
|
| 1046 | - $this->temporaryCacheTtl = $ttl; |
|
| 1047 | - } |
|
| 1045 | + if($ttl > 0){ |
|
| 1046 | + $this->temporaryCacheTtl = $ttl; |
|
| 1047 | + } |
|
| 1048 | 1048 | return $this; |
| 1049 | - } |
|
| 1050 | - |
|
| 1051 | - /** |
|
| 1052 | - * Escape the data before execute query useful for security. |
|
| 1053 | - * @param mixed $data the data to be escaped |
|
| 1054 | - * @param boolean $escaped whether we can do escape of not |
|
| 1055 | - * @return mixed the data after escaped or the same data if not |
|
| 1056 | - */ |
|
| 1057 | - public function escape($data, $escaped = true){ |
|
| 1058 | - if($escaped){ |
|
| 1059 | - if(! $this->pdo){ |
|
| 1060 | - $this->connect(); |
|
| 1061 | - } |
|
| 1062 | - return $this->pdo->quote(trim($data)); |
|
| 1063 | - } |
|
| 1064 | - return $data; |
|
| 1065 | - } |
|
| 1066 | - |
|
| 1067 | - /** |
|
| 1068 | - * Return the number query executed count for the current request |
|
| 1069 | - * @return int |
|
| 1070 | - */ |
|
| 1071 | - public function queryCount(){ |
|
| 1072 | - return $this->queryCount; |
|
| 1073 | - } |
|
| 1074 | - |
|
| 1075 | - /** |
|
| 1076 | - * Return the current query SQL string |
|
| 1077 | - * @return string |
|
| 1078 | - */ |
|
| 1079 | - public function getQuery(){ |
|
| 1080 | - return $this->query; |
|
| 1081 | - } |
|
| 1082 | - |
|
| 1083 | - /** |
|
| 1084 | - * Return the application database name |
|
| 1085 | - * @return string |
|
| 1086 | - */ |
|
| 1087 | - public function getDatabaseName(){ |
|
| 1088 | - return $this->databaseName; |
|
| 1089 | - } |
|
| 1090 | - |
|
| 1091 | - /** |
|
| 1092 | - * Return the database configuration |
|
| 1093 | - * @return array |
|
| 1094 | - */ |
|
| 1095 | - public function getDatabaseConfiguration(){ |
|
| 1096 | - return $this->config; |
|
| 1097 | - } |
|
| 1098 | - |
|
| 1099 | - /** |
|
| 1100 | - * set the database configuration |
|
| 1101 | - * @param array $config the configuration |
|
| 1102 | - */ |
|
| 1103 | - public function setDatabaseConfiguration(array $config){ |
|
| 1104 | - $this->config = array_merge($this->config, $config); |
|
| 1105 | - $this->prefix = $this->config['prefix']; |
|
| 1106 | - $this->databaseName = $this->config['database']; |
|
| 1107 | - $this->logger->info('The database configuration are listed below: ' . stringfy_vars(array_merge($this->config, array('password' => string_hidden($this->config['password']))))); |
|
| 1108 | - return $this; |
|
| 1109 | - } |
|
| 1110 | - |
|
| 1111 | - /** |
|
| 1112 | - * Return the PDO instance |
|
| 1113 | - * @return PDO |
|
| 1114 | - */ |
|
| 1115 | - public function getPdo(){ |
|
| 1116 | - return $this->pdo; |
|
| 1117 | - } |
|
| 1118 | - |
|
| 1119 | - /** |
|
| 1120 | - * Set the PDO instance |
|
| 1121 | - * @param PDO $pdo the pdo object |
|
| 1122 | - */ |
|
| 1123 | - public function setPdo(PDO $pdo){ |
|
| 1124 | - $this->pdo = $pdo; |
|
| 1125 | - return $this; |
|
| 1126 | - } |
|
| 1127 | - |
|
| 1128 | - |
|
| 1129 | - /** |
|
| 1130 | - * Return the Log instance |
|
| 1131 | - * @return Log |
|
| 1132 | - */ |
|
| 1133 | - public function getLogger(){ |
|
| 1134 | - return $this->logger; |
|
| 1135 | - } |
|
| 1136 | - |
|
| 1137 | - /** |
|
| 1138 | - * Set the log instance |
|
| 1139 | - * @param Log $logger the log object |
|
| 1140 | - */ |
|
| 1141 | - public function setLogger($logger){ |
|
| 1142 | - $this->logger = $logger; |
|
| 1143 | - return $this; |
|
| 1144 | - } |
|
| 1145 | - |
|
| 1146 | - /** |
|
| 1147 | - * Return the cache instance |
|
| 1148 | - * @return CacheInterface |
|
| 1149 | - */ |
|
| 1150 | - public function getCacheInstance(){ |
|
| 1151 | - return $this->cacheInstance; |
|
| 1152 | - } |
|
| 1153 | - |
|
| 1154 | - /** |
|
| 1155 | - * Set the cache instance |
|
| 1156 | - * @param CacheInterface $cache the cache object |
|
| 1157 | - */ |
|
| 1158 | - public function setCacheInstance($cache){ |
|
| 1159 | - $this->cacheInstance = $cache; |
|
| 1160 | - return $this; |
|
| 1161 | - } |
|
| 1162 | - |
|
| 1163 | - /** |
|
| 1164 | - * Return the benchmark instance |
|
| 1165 | - * @return Benchmark |
|
| 1166 | - */ |
|
| 1167 | - public function getBenchmark(){ |
|
| 1168 | - return $this->benchmarkInstance; |
|
| 1169 | - } |
|
| 1170 | - |
|
| 1171 | - /** |
|
| 1172 | - * Set the benchmark instance |
|
| 1173 | - * @param Benchmark $cache the cache object |
|
| 1174 | - */ |
|
| 1175 | - public function setBenchmark($benchmark){ |
|
| 1176 | - $this->benchmarkInstance = $benchmark; |
|
| 1177 | - return $this; |
|
| 1178 | - } |
|
| 1179 | - |
|
| 1180 | - /** |
|
| 1181 | - * Return the data to be used for insert, update, etc. |
|
| 1182 | - * @return array |
|
| 1183 | - */ |
|
| 1184 | - public function getData(){ |
|
| 1185 | - return $this->data; |
|
| 1186 | - } |
|
| 1187 | - |
|
| 1188 | - /** |
|
| 1189 | - * Set the data to be used for insert, update, etc. |
|
| 1190 | - * @param string $key the data key identified |
|
| 1191 | - * @param mixed $value the data value |
|
| 1192 | - * @param boolean $escape whether to escape or not the $value |
|
| 1193 | - * @return object the current Database instance |
|
| 1194 | - */ |
|
| 1195 | - public function setData($key, $value, $escape = true){ |
|
| 1196 | - $this->data[$key] = $this->escape($value, $escape); |
|
| 1197 | - return $this; |
|
| 1198 | - } |
|
| 1199 | - |
|
| 1200 | - /** |
|
| 1201 | - * Set the Log instance using argument or create new instance |
|
| 1202 | - * @param object $logger the Log instance if not null |
|
| 1203 | - */ |
|
| 1204 | - protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
| 1205 | - if($logger !== null){ |
|
| 1206 | - $this->logger = $logger; |
|
| 1207 | - } |
|
| 1208 | - else{ |
|
| 1209 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 1210 | - $this->logger->setLogger('Library::Database'); |
|
| 1211 | - } |
|
| 1212 | - } |
|
| 1049 | + } |
|
| 1050 | + |
|
| 1051 | + /** |
|
| 1052 | + * Escape the data before execute query useful for security. |
|
| 1053 | + * @param mixed $data the data to be escaped |
|
| 1054 | + * @param boolean $escaped whether we can do escape of not |
|
| 1055 | + * @return mixed the data after escaped or the same data if not |
|
| 1056 | + */ |
|
| 1057 | + public function escape($data, $escaped = true){ |
|
| 1058 | + if($escaped){ |
|
| 1059 | + if(! $this->pdo){ |
|
| 1060 | + $this->connect(); |
|
| 1061 | + } |
|
| 1062 | + return $this->pdo->quote(trim($data)); |
|
| 1063 | + } |
|
| 1064 | + return $data; |
|
| 1065 | + } |
|
| 1066 | + |
|
| 1067 | + /** |
|
| 1068 | + * Return the number query executed count for the current request |
|
| 1069 | + * @return int |
|
| 1070 | + */ |
|
| 1071 | + public function queryCount(){ |
|
| 1072 | + return $this->queryCount; |
|
| 1073 | + } |
|
| 1074 | + |
|
| 1075 | + /** |
|
| 1076 | + * Return the current query SQL string |
|
| 1077 | + * @return string |
|
| 1078 | + */ |
|
| 1079 | + public function getQuery(){ |
|
| 1080 | + return $this->query; |
|
| 1081 | + } |
|
| 1082 | + |
|
| 1083 | + /** |
|
| 1084 | + * Return the application database name |
|
| 1085 | + * @return string |
|
| 1086 | + */ |
|
| 1087 | + public function getDatabaseName(){ |
|
| 1088 | + return $this->databaseName; |
|
| 1089 | + } |
|
| 1090 | + |
|
| 1091 | + /** |
|
| 1092 | + * Return the database configuration |
|
| 1093 | + * @return array |
|
| 1094 | + */ |
|
| 1095 | + public function getDatabaseConfiguration(){ |
|
| 1096 | + return $this->config; |
|
| 1097 | + } |
|
| 1098 | + |
|
| 1099 | + /** |
|
| 1100 | + * set the database configuration |
|
| 1101 | + * @param array $config the configuration |
|
| 1102 | + */ |
|
| 1103 | + public function setDatabaseConfiguration(array $config){ |
|
| 1104 | + $this->config = array_merge($this->config, $config); |
|
| 1105 | + $this->prefix = $this->config['prefix']; |
|
| 1106 | + $this->databaseName = $this->config['database']; |
|
| 1107 | + $this->logger->info('The database configuration are listed below: ' . stringfy_vars(array_merge($this->config, array('password' => string_hidden($this->config['password']))))); |
|
| 1108 | + return $this; |
|
| 1109 | + } |
|
| 1110 | + |
|
| 1111 | + /** |
|
| 1112 | + * Return the PDO instance |
|
| 1113 | + * @return PDO |
|
| 1114 | + */ |
|
| 1115 | + public function getPdo(){ |
|
| 1116 | + return $this->pdo; |
|
| 1117 | + } |
|
| 1118 | + |
|
| 1119 | + /** |
|
| 1120 | + * Set the PDO instance |
|
| 1121 | + * @param PDO $pdo the pdo object |
|
| 1122 | + */ |
|
| 1123 | + public function setPdo(PDO $pdo){ |
|
| 1124 | + $this->pdo = $pdo; |
|
| 1125 | + return $this; |
|
| 1126 | + } |
|
| 1127 | + |
|
| 1128 | + |
|
| 1129 | + /** |
|
| 1130 | + * Return the Log instance |
|
| 1131 | + * @return Log |
|
| 1132 | + */ |
|
| 1133 | + public function getLogger(){ |
|
| 1134 | + return $this->logger; |
|
| 1135 | + } |
|
| 1136 | + |
|
| 1137 | + /** |
|
| 1138 | + * Set the log instance |
|
| 1139 | + * @param Log $logger the log object |
|
| 1140 | + */ |
|
| 1141 | + public function setLogger($logger){ |
|
| 1142 | + $this->logger = $logger; |
|
| 1143 | + return $this; |
|
| 1144 | + } |
|
| 1145 | + |
|
| 1146 | + /** |
|
| 1147 | + * Return the cache instance |
|
| 1148 | + * @return CacheInterface |
|
| 1149 | + */ |
|
| 1150 | + public function getCacheInstance(){ |
|
| 1151 | + return $this->cacheInstance; |
|
| 1152 | + } |
|
| 1153 | + |
|
| 1154 | + /** |
|
| 1155 | + * Set the cache instance |
|
| 1156 | + * @param CacheInterface $cache the cache object |
|
| 1157 | + */ |
|
| 1158 | + public function setCacheInstance($cache){ |
|
| 1159 | + $this->cacheInstance = $cache; |
|
| 1160 | + return $this; |
|
| 1161 | + } |
|
| 1162 | + |
|
| 1163 | + /** |
|
| 1164 | + * Return the benchmark instance |
|
| 1165 | + * @return Benchmark |
|
| 1166 | + */ |
|
| 1167 | + public function getBenchmark(){ |
|
| 1168 | + return $this->benchmarkInstance; |
|
| 1169 | + } |
|
| 1170 | + |
|
| 1171 | + /** |
|
| 1172 | + * Set the benchmark instance |
|
| 1173 | + * @param Benchmark $cache the cache object |
|
| 1174 | + */ |
|
| 1175 | + public function setBenchmark($benchmark){ |
|
| 1176 | + $this->benchmarkInstance = $benchmark; |
|
| 1177 | + return $this; |
|
| 1178 | + } |
|
| 1179 | + |
|
| 1180 | + /** |
|
| 1181 | + * Return the data to be used for insert, update, etc. |
|
| 1182 | + * @return array |
|
| 1183 | + */ |
|
| 1184 | + public function getData(){ |
|
| 1185 | + return $this->data; |
|
| 1186 | + } |
|
| 1187 | + |
|
| 1188 | + /** |
|
| 1189 | + * Set the data to be used for insert, update, etc. |
|
| 1190 | + * @param string $key the data key identified |
|
| 1191 | + * @param mixed $value the data value |
|
| 1192 | + * @param boolean $escape whether to escape or not the $value |
|
| 1193 | + * @return object the current Database instance |
|
| 1194 | + */ |
|
| 1195 | + public function setData($key, $value, $escape = true){ |
|
| 1196 | + $this->data[$key] = $this->escape($value, $escape); |
|
| 1197 | + return $this; |
|
| 1198 | + } |
|
| 1199 | + |
|
| 1200 | + /** |
|
| 1201 | + * Set the Log instance using argument or create new instance |
|
| 1202 | + * @param object $logger the Log instance if not null |
|
| 1203 | + */ |
|
| 1204 | + protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
| 1205 | + if($logger !== null){ |
|
| 1206 | + $this->logger = $logger; |
|
| 1207 | + } |
|
| 1208 | + else{ |
|
| 1209 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 1210 | + $this->logger->setLogger('Library::Database'); |
|
| 1211 | + } |
|
| 1212 | + } |
|
| 1213 | 1213 | |
| 1214 | 1214 | /** |
| 1215 | 1215 | * Setting the database configuration using the configuration file |
| 1216 | 1216 | * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
| 1217 | 1217 | */ |
| 1218 | - protected function setDatabaseConfigurationFromConfigFile(array $overwriteConfig = array()){ |
|
| 1219 | - $db = array(); |
|
| 1220 | - if(file_exists(CONFIG_PATH . 'database.php')){ |
|
| 1221 | - //here don't use require_once because somewhere user can create database instance directly |
|
| 1222 | - require CONFIG_PATH . 'database.php'; |
|
| 1223 | - } |
|
| 1218 | + protected function setDatabaseConfigurationFromConfigFile(array $overwriteConfig = array()){ |
|
| 1219 | + $db = array(); |
|
| 1220 | + if(file_exists(CONFIG_PATH . 'database.php')){ |
|
| 1221 | + //here don't use require_once because somewhere user can create database instance directly |
|
| 1222 | + require CONFIG_PATH . 'database.php'; |
|
| 1223 | + } |
|
| 1224 | 1224 | |
| 1225 | - if(! empty($overwriteConfig)){ |
|
| 1226 | - $db = array_merge($db, $overwriteConfig); |
|
| 1227 | - } |
|
| 1228 | - $config = array( |
|
| 1229 | - 'driver' => 'mysql', |
|
| 1230 | - 'username' => 'root', |
|
| 1231 | - 'password' => '', |
|
| 1232 | - 'database' => '', |
|
| 1233 | - 'hostname' => 'localhost', |
|
| 1234 | - 'charset' => 'utf8', |
|
| 1235 | - 'collation' => 'utf8_general_ci', |
|
| 1236 | - 'prefix' => '', |
|
| 1237 | - 'port' => '' |
|
| 1238 | - ); |
|
| 1239 | - $this->setDatabaseConfiguration(array_merge($config, $db)); |
|
| 1240 | - $this->determinePortConfigurationFromHostname(); |
|
| 1241 | - } |
|
| 1242 | - |
|
| 1243 | - /** |
|
| 1244 | - * This method is used to get the PDO DSN string using th configured driver |
|
| 1245 | - * @return string the DSN string |
|
| 1246 | - */ |
|
| 1247 | - protected function getDsnFromDriver(){ |
|
| 1248 | - $config = $this->getDatabaseConfiguration(); |
|
| 1249 | - if(! empty($config)){ |
|
| 1250 | - $driverDsnMap = array( |
|
| 1251 | - 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
|
| 1252 | - . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 1253 | - . 'dbname=' . $config['database'], |
|
| 1254 | - 'pgsql' => 'pgsql:host=' . $config['hostname'] . ';' |
|
| 1255 | - . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 1256 | - . 'dbname=' . $config['database'], |
|
| 1257 | - 'sqlite' => 'sqlite:' . $config['database'], |
|
| 1258 | - 'oracle' => 'oci:dbname=' . $config['hostname'] |
|
| 1259 | - . (($config['port']) != '' ? ':' . $config['port'] : '') |
|
| 1260 | - . '/' . $config['database'] |
|
| 1261 | - ); |
|
| 1262 | - return isset($driverDsnMap[$config['driver']]) ? $driverDsnMap[$config['driver']] : ''; |
|
| 1263 | - } |
|
| 1264 | - return null; |
|
| 1265 | - } |
|
| 1266 | - |
|
| 1267 | - /** |
|
| 1268 | - * Set the database server port configuration using the current hostname like localhost:3309 |
|
| 1269 | - * @return void |
|
| 1270 | - */ |
|
| 1271 | - protected function determinePortConfigurationFromHostname(){ |
|
| 1272 | - if(strstr($this->config['hostname'], ':')){ |
|
| 1273 | - $p = explode(':', $this->config['hostname']); |
|
| 1274 | - if(count($p) > 2){ |
|
| 1275 | - $this->setDatabaseConfiguration(array( |
|
| 1276 | - 'hostname' => $p[0], |
|
| 1277 | - 'port' => $p[1] |
|
| 1278 | - )); |
|
| 1279 | - } |
|
| 1280 | - } |
|
| 1281 | - } |
|
| 1225 | + if(! empty($overwriteConfig)){ |
|
| 1226 | + $db = array_merge($db, $overwriteConfig); |
|
| 1227 | + } |
|
| 1228 | + $config = array( |
|
| 1229 | + 'driver' => 'mysql', |
|
| 1230 | + 'username' => 'root', |
|
| 1231 | + 'password' => '', |
|
| 1232 | + 'database' => '', |
|
| 1233 | + 'hostname' => 'localhost', |
|
| 1234 | + 'charset' => 'utf8', |
|
| 1235 | + 'collation' => 'utf8_general_ci', |
|
| 1236 | + 'prefix' => '', |
|
| 1237 | + 'port' => '' |
|
| 1238 | + ); |
|
| 1239 | + $this->setDatabaseConfiguration(array_merge($config, $db)); |
|
| 1240 | + $this->determinePortConfigurationFromHostname(); |
|
| 1241 | + } |
|
| 1242 | + |
|
| 1243 | + /** |
|
| 1244 | + * This method is used to get the PDO DSN string using th configured driver |
|
| 1245 | + * @return string the DSN string |
|
| 1246 | + */ |
|
| 1247 | + protected function getDsnFromDriver(){ |
|
| 1248 | + $config = $this->getDatabaseConfiguration(); |
|
| 1249 | + if(! empty($config)){ |
|
| 1250 | + $driverDsnMap = array( |
|
| 1251 | + 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
|
| 1252 | + . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 1253 | + . 'dbname=' . $config['database'], |
|
| 1254 | + 'pgsql' => 'pgsql:host=' . $config['hostname'] . ';' |
|
| 1255 | + . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 1256 | + . 'dbname=' . $config['database'], |
|
| 1257 | + 'sqlite' => 'sqlite:' . $config['database'], |
|
| 1258 | + 'oracle' => 'oci:dbname=' . $config['hostname'] |
|
| 1259 | + . (($config['port']) != '' ? ':' . $config['port'] : '') |
|
| 1260 | + . '/' . $config['database'] |
|
| 1261 | + ); |
|
| 1262 | + return isset($driverDsnMap[$config['driver']]) ? $driverDsnMap[$config['driver']] : ''; |
|
| 1263 | + } |
|
| 1264 | + return null; |
|
| 1265 | + } |
|
| 1266 | + |
|
| 1267 | + /** |
|
| 1268 | + * Set the database server port configuration using the current hostname like localhost:3309 |
|
| 1269 | + * @return void |
|
| 1270 | + */ |
|
| 1271 | + protected function determinePortConfigurationFromHostname(){ |
|
| 1272 | + if(strstr($this->config['hostname'], ':')){ |
|
| 1273 | + $p = explode(':', $this->config['hostname']); |
|
| 1274 | + if(count($p) > 2){ |
|
| 1275 | + $this->setDatabaseConfiguration(array( |
|
| 1276 | + 'hostname' => $p[0], |
|
| 1277 | + 'port' => $p[1] |
|
| 1278 | + )); |
|
| 1279 | + } |
|
| 1280 | + } |
|
| 1281 | + } |
|
| 1282 | 1282 | |
| 1283 | 1283 | /** |
| 1284 | - * Get the SQL WHERE clause using array column => value |
|
| 1285 | - * @see Database::where |
|
| 1286 | - * |
|
| 1287 | - * @return string |
|
| 1288 | - */ |
|
| 1289 | - protected function getWhereStrIfIsArray(array $where, $type = '', $andOr = 'AND', $escape = true){ |
|
| 1290 | - $_where = array(); |
|
| 1291 | - foreach ($where as $column => $data){ |
|
| 1292 | - if(is_null($data)){ |
|
| 1293 | - $data = ''; |
|
| 1294 | - } |
|
| 1295 | - $_where[] = $type . $column . ' = ' . ($this->escape($data, $escape)); |
|
| 1296 | - } |
|
| 1297 | - $where = implode(' '.$andOr.' ', $_where); |
|
| 1298 | - return $where; |
|
| 1299 | - } |
|
| 1300 | - |
|
| 1301 | - /** |
|
| 1302 | - * Get the SQL WHERE clause when operator argument is an array |
|
| 1303 | - * @see Database::where |
|
| 1304 | - * |
|
| 1305 | - * @return string |
|
| 1306 | - */ |
|
| 1307 | - protected function getWhereStrIfOperatorIsArray($where, array $op, $type = '', $escape = true){ |
|
| 1308 | - $x = explode('?', $where); |
|
| 1309 | - $w = ''; |
|
| 1310 | - foreach($x as $k => $v){ |
|
| 1311 | - if(! empty($v)){ |
|
| 1312 | - if(isset($op[$k]) && is_null($op[$k])){ |
|
| 1313 | - $op[$k] = ''; |
|
| 1314 | - } |
|
| 1315 | - $w .= $type . $v . (isset($op[$k]) ? ($this->escape($op[$k], $escape)) : ''); |
|
| 1316 | - } |
|
| 1317 | - } |
|
| 1318 | - return $w; |
|
| 1319 | - } |
|
| 1320 | - |
|
| 1321 | - /** |
|
| 1322 | - * Get the default SQL WHERE clause using operator = or the operator argument |
|
| 1323 | - * @see Database::where |
|
| 1324 | - * |
|
| 1325 | - * @return string |
|
| 1326 | - */ |
|
| 1327 | - protected function getWhereStrForOperator($where, $op = null, $val = null, $type = '', $escape = true){ |
|
| 1328 | - $w = ''; |
|
| 1329 | - if (! in_array((string)$op, $this->operatorList)){ |
|
| 1330 | - if(is_null($op)){ |
|
| 1331 | - $op = ''; |
|
| 1332 | - } |
|
| 1333 | - $w = $type . $where . ' = ' . ($this->escape($op, $escape)); |
|
| 1334 | - } |
|
| 1335 | - else{ |
|
| 1336 | - if(is_null($val)){ |
|
| 1337 | - $val = ''; |
|
| 1338 | - } |
|
| 1339 | - $w = $type . $where . $op . ($this->escape($val, $escape)); |
|
| 1340 | - } |
|
| 1341 | - return $w; |
|
| 1342 | - } |
|
| 1343 | - |
|
| 1344 | - /** |
|
| 1345 | - * Set the $this->where property |
|
| 1346 | - * @param string $whereStr the WHERE clause string |
|
| 1347 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 1348 | - */ |
|
| 1349 | - protected function setWhereStr($whereStr, $andOr = 'AND'){ |
|
| 1350 | - if (empty($this->where)){ |
|
| 1351 | - $this->where = $whereStr; |
|
| 1352 | - } |
|
| 1353 | - else{ |
|
| 1354 | - if(substr($this->where, -1) == '('){ |
|
| 1355 | - $this->where = $this->where . ' ' . $whereStr; |
|
| 1356 | - } |
|
| 1357 | - else{ |
|
| 1358 | - $this->where = $this->where . ' '.$andOr.' ' . $whereStr; |
|
| 1359 | - } |
|
| 1360 | - } |
|
| 1361 | - } |
|
| 1362 | - |
|
| 1363 | - /** |
|
| 1364 | - * Transform the prepared query like (?, ?, ?) into string format |
|
| 1365 | - * @see Database::query |
|
| 1366 | - * |
|
| 1367 | - * @return string |
|
| 1368 | - */ |
|
| 1369 | - protected function transformPreparedQuery($query, $data){ |
|
| 1370 | - if(is_array($data)){ |
|
| 1371 | - $x = explode('?', $query); |
|
| 1372 | - $q = ''; |
|
| 1373 | - foreach($x as $k => $v){ |
|
| 1374 | - if(! empty($v)){ |
|
| 1375 | - $q .= $v . (isset($data[$k]) ? $this->escape($data[$k]) : ''); |
|
| 1376 | - } |
|
| 1377 | - } |
|
| 1378 | - return $q; |
|
| 1379 | - } |
|
| 1380 | - return $query; |
|
| 1381 | - } |
|
| 1382 | - |
|
| 1383 | - /** |
|
| 1384 | - * Return the cache key for the query |
|
| 1385 | - * @see Database::query |
|
| 1386 | - * |
|
| 1387 | - * @return string |
|
| 1388 | - */ |
|
| 1389 | - protected function getCacheBenchmarkKeyForQuery($query, $all, $array){ |
|
| 1390 | - return md5($query . $all . $array); |
|
| 1391 | - } |
|
| 1392 | - |
|
| 1393 | - /** |
|
| 1394 | - * Get the cache content for this query |
|
| 1395 | - * @see Database::query |
|
| 1396 | - * |
|
| 1397 | - * @return mixed |
|
| 1398 | - */ |
|
| 1399 | - protected function getCacheContentForQuery($query, $all, $array){ |
|
| 1400 | - $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
| 1401 | - $cacheKey = $this->getCacheBenchmarkKeyForQuery($query, $all, $array); |
|
| 1402 | - if(is_object($this->cacheInstance)){ |
|
| 1403 | - return $this->cacheInstance->get($cacheKey); |
|
| 1404 | - } |
|
| 1405 | - $instance = & get_instance()->cache; |
|
| 1406 | - $this->setCacheInstance($instance); |
|
| 1407 | - return $instance->get($cacheKey); |
|
| 1408 | - } |
|
| 1409 | - |
|
| 1410 | - /** |
|
| 1411 | - * Save the result of query into cache |
|
| 1412 | - * @param string $query the SQL query |
|
| 1413 | - * @param string $key the cache key |
|
| 1414 | - * @param mixed $result the query result to save |
|
| 1415 | - * @param boolean $status whether can save the query result into cache |
|
| 1416 | - * @param int $expire the cache TTL |
|
| 1417 | - */ |
|
| 1418 | - protected function setCacheContentForQuery($query, $key, $result, $status, $expire){ |
|
| 1419 | - if ($status){ |
|
| 1420 | - $this->logger->info('Save the result for query [' .$query. '] into cache for future use'); |
|
| 1421 | - $this->getCacheInstance()->set($key, $result, $expire); |
|
| 1422 | - } |
|
| 1423 | - } |
|
| 1424 | - |
|
| 1425 | - /** |
|
| 1426 | - * Set the result for SELECT query using PDOStatment |
|
| 1427 | - * @see Database::query |
|
| 1428 | - */ |
|
| 1429 | - protected function setQueryResultForSelect($pdoStatment, $all = true, $array = false){ |
|
| 1430 | - //if need return all result like list of record |
|
| 1431 | - if ($all){ |
|
| 1432 | - $this->result = ($array === false) ? $pdoStatment->fetchAll(PDO::FETCH_OBJ) : $pdoStatment->fetchAll(PDO::FETCH_ASSOC); |
|
| 1433 | - } |
|
| 1434 | - else{ |
|
| 1435 | - $this->result = ($array === false) ? $pdoStatment->fetch(PDO::FETCH_OBJ) : $pdoStatment->fetch(PDO::FETCH_ASSOC); |
|
| 1436 | - } |
|
| 1437 | - //Sqlite and pgsql always return 0 when using rowCount() |
|
| 1438 | - if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 1439 | - $this->numRows = count($this->result); |
|
| 1440 | - } |
|
| 1441 | - else{ |
|
| 1442 | - $this->numRows = $pdoStatment->rowCount(); |
|
| 1443 | - } |
|
| 1444 | - } |
|
| 1445 | - |
|
| 1446 | - /** |
|
| 1447 | - * Set the result for other command than SELECT query using PDOStatment |
|
| 1448 | - * @see Database::query |
|
| 1449 | - */ |
|
| 1450 | - protected function setQueryResultForNonSelect($pdoStatment, $all = true, $array = false){ |
|
| 1451 | - //Sqlite and pgsql always return 0 when using rowCount() |
|
| 1452 | - if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 1453 | - $this->result = 1; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 1454 | - $this->numRows = 1; |
|
| 1455 | - } |
|
| 1456 | - else{ |
|
| 1457 | - $this->result = $pdoStatment->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 1458 | - $this->numRows = $pdoStatment->rowCount(); |
|
| 1459 | - } |
|
| 1460 | - } |
|
| 1461 | - |
|
| 1462 | - /** |
|
| 1463 | - * Set error for database query execution |
|
| 1464 | - */ |
|
| 1465 | - protected function setQueryError(){ |
|
| 1466 | - $error = $this->pdo->errorInfo(); |
|
| 1467 | - $this->error = isset($error[2]) ? $error[2] : ''; |
|
| 1468 | - $this->logger->error('The database query execution got error: ' . stringfy_vars($error)); |
|
| 1469 | - $this->error(); |
|
| 1470 | - } |
|
| 1471 | - |
|
| 1472 | - /** |
|
| 1473 | - * Run the database SQL query and return the PDOStatment object |
|
| 1474 | - * @see Database::query |
|
| 1475 | - * |
|
| 1476 | - * @return object|void |
|
| 1477 | - */ |
|
| 1478 | - protected function runSqlQuery($query, $all, $array){ |
|
| 1479 | - //for database query execution time |
|
| 1480 | - $benchmarkMarkerKey = $this->getCacheBenchmarkKeyForQuery($query, $all, $array); |
|
| 1481 | - $benchmarkInstance = $this->getBenchmark(); |
|
| 1482 | - if(! is_object($benchmarkInstance)){ |
|
| 1483 | - $obj = & get_instance(); |
|
| 1484 | - $benchmarkInstance = $obj->benchmark; |
|
| 1485 | - $this->setBenchmark($benchmarkInstance); |
|
| 1486 | - } |
|
| 1487 | - if(! $this->pdo){ |
|
| 1488 | - $this->connect(); |
|
| 1489 | - } |
|
| 1284 | + * Get the SQL WHERE clause using array column => value |
|
| 1285 | + * @see Database::where |
|
| 1286 | + * |
|
| 1287 | + * @return string |
|
| 1288 | + */ |
|
| 1289 | + protected function getWhereStrIfIsArray(array $where, $type = '', $andOr = 'AND', $escape = true){ |
|
| 1290 | + $_where = array(); |
|
| 1291 | + foreach ($where as $column => $data){ |
|
| 1292 | + if(is_null($data)){ |
|
| 1293 | + $data = ''; |
|
| 1294 | + } |
|
| 1295 | + $_where[] = $type . $column . ' = ' . ($this->escape($data, $escape)); |
|
| 1296 | + } |
|
| 1297 | + $where = implode(' '.$andOr.' ', $_where); |
|
| 1298 | + return $where; |
|
| 1299 | + } |
|
| 1300 | + |
|
| 1301 | + /** |
|
| 1302 | + * Get the SQL WHERE clause when operator argument is an array |
|
| 1303 | + * @see Database::where |
|
| 1304 | + * |
|
| 1305 | + * @return string |
|
| 1306 | + */ |
|
| 1307 | + protected function getWhereStrIfOperatorIsArray($where, array $op, $type = '', $escape = true){ |
|
| 1308 | + $x = explode('?', $where); |
|
| 1309 | + $w = ''; |
|
| 1310 | + foreach($x as $k => $v){ |
|
| 1311 | + if(! empty($v)){ |
|
| 1312 | + if(isset($op[$k]) && is_null($op[$k])){ |
|
| 1313 | + $op[$k] = ''; |
|
| 1314 | + } |
|
| 1315 | + $w .= $type . $v . (isset($op[$k]) ? ($this->escape($op[$k], $escape)) : ''); |
|
| 1316 | + } |
|
| 1317 | + } |
|
| 1318 | + return $w; |
|
| 1319 | + } |
|
| 1320 | + |
|
| 1321 | + /** |
|
| 1322 | + * Get the default SQL WHERE clause using operator = or the operator argument |
|
| 1323 | + * @see Database::where |
|
| 1324 | + * |
|
| 1325 | + * @return string |
|
| 1326 | + */ |
|
| 1327 | + protected function getWhereStrForOperator($where, $op = null, $val = null, $type = '', $escape = true){ |
|
| 1328 | + $w = ''; |
|
| 1329 | + if (! in_array((string)$op, $this->operatorList)){ |
|
| 1330 | + if(is_null($op)){ |
|
| 1331 | + $op = ''; |
|
| 1332 | + } |
|
| 1333 | + $w = $type . $where . ' = ' . ($this->escape($op, $escape)); |
|
| 1334 | + } |
|
| 1335 | + else{ |
|
| 1336 | + if(is_null($val)){ |
|
| 1337 | + $val = ''; |
|
| 1338 | + } |
|
| 1339 | + $w = $type . $where . $op . ($this->escape($val, $escape)); |
|
| 1340 | + } |
|
| 1341 | + return $w; |
|
| 1342 | + } |
|
| 1343 | + |
|
| 1344 | + /** |
|
| 1345 | + * Set the $this->where property |
|
| 1346 | + * @param string $whereStr the WHERE clause string |
|
| 1347 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 1348 | + */ |
|
| 1349 | + protected function setWhereStr($whereStr, $andOr = 'AND'){ |
|
| 1350 | + if (empty($this->where)){ |
|
| 1351 | + $this->where = $whereStr; |
|
| 1352 | + } |
|
| 1353 | + else{ |
|
| 1354 | + if(substr($this->where, -1) == '('){ |
|
| 1355 | + $this->where = $this->where . ' ' . $whereStr; |
|
| 1356 | + } |
|
| 1357 | + else{ |
|
| 1358 | + $this->where = $this->where . ' '.$andOr.' ' . $whereStr; |
|
| 1359 | + } |
|
| 1360 | + } |
|
| 1361 | + } |
|
| 1362 | + |
|
| 1363 | + /** |
|
| 1364 | + * Transform the prepared query like (?, ?, ?) into string format |
|
| 1365 | + * @see Database::query |
|
| 1366 | + * |
|
| 1367 | + * @return string |
|
| 1368 | + */ |
|
| 1369 | + protected function transformPreparedQuery($query, $data){ |
|
| 1370 | + if(is_array($data)){ |
|
| 1371 | + $x = explode('?', $query); |
|
| 1372 | + $q = ''; |
|
| 1373 | + foreach($x as $k => $v){ |
|
| 1374 | + if(! empty($v)){ |
|
| 1375 | + $q .= $v . (isset($data[$k]) ? $this->escape($data[$k]) : ''); |
|
| 1376 | + } |
|
| 1377 | + } |
|
| 1378 | + return $q; |
|
| 1379 | + } |
|
| 1380 | + return $query; |
|
| 1381 | + } |
|
| 1382 | + |
|
| 1383 | + /** |
|
| 1384 | + * Return the cache key for the query |
|
| 1385 | + * @see Database::query |
|
| 1386 | + * |
|
| 1387 | + * @return string |
|
| 1388 | + */ |
|
| 1389 | + protected function getCacheBenchmarkKeyForQuery($query, $all, $array){ |
|
| 1390 | + return md5($query . $all . $array); |
|
| 1391 | + } |
|
| 1392 | + |
|
| 1393 | + /** |
|
| 1394 | + * Get the cache content for this query |
|
| 1395 | + * @see Database::query |
|
| 1396 | + * |
|
| 1397 | + * @return mixed |
|
| 1398 | + */ |
|
| 1399 | + protected function getCacheContentForQuery($query, $all, $array){ |
|
| 1400 | + $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
| 1401 | + $cacheKey = $this->getCacheBenchmarkKeyForQuery($query, $all, $array); |
|
| 1402 | + if(is_object($this->cacheInstance)){ |
|
| 1403 | + return $this->cacheInstance->get($cacheKey); |
|
| 1404 | + } |
|
| 1405 | + $instance = & get_instance()->cache; |
|
| 1406 | + $this->setCacheInstance($instance); |
|
| 1407 | + return $instance->get($cacheKey); |
|
| 1408 | + } |
|
| 1409 | + |
|
| 1410 | + /** |
|
| 1411 | + * Save the result of query into cache |
|
| 1412 | + * @param string $query the SQL query |
|
| 1413 | + * @param string $key the cache key |
|
| 1414 | + * @param mixed $result the query result to save |
|
| 1415 | + * @param boolean $status whether can save the query result into cache |
|
| 1416 | + * @param int $expire the cache TTL |
|
| 1417 | + */ |
|
| 1418 | + protected function setCacheContentForQuery($query, $key, $result, $status, $expire){ |
|
| 1419 | + if ($status){ |
|
| 1420 | + $this->logger->info('Save the result for query [' .$query. '] into cache for future use'); |
|
| 1421 | + $this->getCacheInstance()->set($key, $result, $expire); |
|
| 1422 | + } |
|
| 1423 | + } |
|
| 1424 | + |
|
| 1425 | + /** |
|
| 1426 | + * Set the result for SELECT query using PDOStatment |
|
| 1427 | + * @see Database::query |
|
| 1428 | + */ |
|
| 1429 | + protected function setQueryResultForSelect($pdoStatment, $all = true, $array = false){ |
|
| 1430 | + //if need return all result like list of record |
|
| 1431 | + if ($all){ |
|
| 1432 | + $this->result = ($array === false) ? $pdoStatment->fetchAll(PDO::FETCH_OBJ) : $pdoStatment->fetchAll(PDO::FETCH_ASSOC); |
|
| 1433 | + } |
|
| 1434 | + else{ |
|
| 1435 | + $this->result = ($array === false) ? $pdoStatment->fetch(PDO::FETCH_OBJ) : $pdoStatment->fetch(PDO::FETCH_ASSOC); |
|
| 1436 | + } |
|
| 1437 | + //Sqlite and pgsql always return 0 when using rowCount() |
|
| 1438 | + if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 1439 | + $this->numRows = count($this->result); |
|
| 1440 | + } |
|
| 1441 | + else{ |
|
| 1442 | + $this->numRows = $pdoStatment->rowCount(); |
|
| 1443 | + } |
|
| 1444 | + } |
|
| 1445 | + |
|
| 1446 | + /** |
|
| 1447 | + * Set the result for other command than SELECT query using PDOStatment |
|
| 1448 | + * @see Database::query |
|
| 1449 | + */ |
|
| 1450 | + protected function setQueryResultForNonSelect($pdoStatment, $all = true, $array = false){ |
|
| 1451 | + //Sqlite and pgsql always return 0 when using rowCount() |
|
| 1452 | + if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 1453 | + $this->result = 1; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 1454 | + $this->numRows = 1; |
|
| 1455 | + } |
|
| 1456 | + else{ |
|
| 1457 | + $this->result = $pdoStatment->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 1458 | + $this->numRows = $pdoStatment->rowCount(); |
|
| 1459 | + } |
|
| 1460 | + } |
|
| 1461 | + |
|
| 1462 | + /** |
|
| 1463 | + * Set error for database query execution |
|
| 1464 | + */ |
|
| 1465 | + protected function setQueryError(){ |
|
| 1466 | + $error = $this->pdo->errorInfo(); |
|
| 1467 | + $this->error = isset($error[2]) ? $error[2] : ''; |
|
| 1468 | + $this->logger->error('The database query execution got error: ' . stringfy_vars($error)); |
|
| 1469 | + $this->error(); |
|
| 1470 | + } |
|
| 1471 | + |
|
| 1472 | + /** |
|
| 1473 | + * Run the database SQL query and return the PDOStatment object |
|
| 1474 | + * @see Database::query |
|
| 1475 | + * |
|
| 1476 | + * @return object|void |
|
| 1477 | + */ |
|
| 1478 | + protected function runSqlQuery($query, $all, $array){ |
|
| 1479 | + //for database query execution time |
|
| 1480 | + $benchmarkMarkerKey = $this->getCacheBenchmarkKeyForQuery($query, $all, $array); |
|
| 1481 | + $benchmarkInstance = $this->getBenchmark(); |
|
| 1482 | + if(! is_object($benchmarkInstance)){ |
|
| 1483 | + $obj = & get_instance(); |
|
| 1484 | + $benchmarkInstance = $obj->benchmark; |
|
| 1485 | + $this->setBenchmark($benchmarkInstance); |
|
| 1486 | + } |
|
| 1487 | + if(! $this->pdo){ |
|
| 1488 | + $this->connect(); |
|
| 1489 | + } |
|
| 1490 | 1490 | |
| 1491 | - $benchmarkInstance->mark('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')'); |
|
| 1492 | - //Now execute the query |
|
| 1493 | - $sqlQuery = $this->pdo->query($query); |
|
| 1491 | + $benchmarkInstance->mark('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')'); |
|
| 1492 | + //Now execute the query |
|
| 1493 | + $sqlQuery = $this->pdo->query($query); |
|
| 1494 | 1494 | |
| 1495 | - //get response time for this query |
|
| 1496 | - $responseTime = $benchmarkInstance->elapsedTime('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')'); |
|
| 1497 | - //TODO use the configuration value for the high response time currently is 1 second |
|
| 1498 | - if($responseTime >= 1 ){ |
|
| 1499 | - $this->logger->warning('High response time while processing database query [' .$query. ']. The response time is [' .$responseTime. '] sec.'); |
|
| 1500 | - } |
|
| 1501 | - if($sqlQuery){ |
|
| 1502 | - return $sqlQuery; |
|
| 1503 | - } |
|
| 1504 | - $this->setQueryError(); |
|
| 1505 | - } |
|
| 1495 | + //get response time for this query |
|
| 1496 | + $responseTime = $benchmarkInstance->elapsedTime('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')'); |
|
| 1497 | + //TODO use the configuration value for the high response time currently is 1 second |
|
| 1498 | + if($responseTime >= 1 ){ |
|
| 1499 | + $this->logger->warning('High response time while processing database query [' .$query. ']. The response time is [' .$responseTime. '] sec.'); |
|
| 1500 | + } |
|
| 1501 | + if($sqlQuery){ |
|
| 1502 | + return $sqlQuery; |
|
| 1503 | + } |
|
| 1504 | + $this->setQueryError(); |
|
| 1505 | + } |
|
| 1506 | 1506 | |
| 1507 | 1507 | /** |
| 1508 | 1508 | * Reset the database class attributs to the initail values before each query. |
| 1509 | 1509 | */ |
| 1510 | 1510 | private function reset(){ |
| 1511 | - $this->select = '*'; |
|
| 1512 | - $this->from = null; |
|
| 1513 | - $this->where = null; |
|
| 1514 | - $this->limit = null; |
|
| 1515 | - $this->orderBy = null; |
|
| 1516 | - $this->groupBy = null; |
|
| 1517 | - $this->having = null; |
|
| 1518 | - $this->join = null; |
|
| 1519 | - $this->numRows = 0; |
|
| 1520 | - $this->insertId = null; |
|
| 1521 | - $this->query = null; |
|
| 1522 | - $this->error = null; |
|
| 1523 | - $this->result = array(); |
|
| 1524 | - $this->data = array(); |
|
| 1511 | + $this->select = '*'; |
|
| 1512 | + $this->from = null; |
|
| 1513 | + $this->where = null; |
|
| 1514 | + $this->limit = null; |
|
| 1515 | + $this->orderBy = null; |
|
| 1516 | + $this->groupBy = null; |
|
| 1517 | + $this->having = null; |
|
| 1518 | + $this->join = null; |
|
| 1519 | + $this->numRows = 0; |
|
| 1520 | + $this->insertId = null; |
|
| 1521 | + $this->query = null; |
|
| 1522 | + $this->error = null; |
|
| 1523 | + $this->result = array(); |
|
| 1524 | + $this->data = array(); |
|
| 1525 | 1525 | } |
| 1526 | 1526 | |
| 1527 | 1527 | /** |
| 1528 | 1528 | * The class destructor |
| 1529 | 1529 | */ |
| 1530 | 1530 | public function __destruct(){ |
| 1531 | - $this->pdo = null; |
|
| 1531 | + $this->pdo = null; |
|
| 1532 | 1532 | } |
| 1533 | 1533 | |
| 1534 | 1534 | } |
@@ -23,165 +23,165 @@ 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 SQL SELECT statment |
| 42 | 42 | * @var string |
| 43 | 43 | */ |
| 44 | - private $select = '*'; |
|
| 44 | + private $select = '*'; |
|
| 45 | 45 | |
| 46 | 46 | /** |
| 47 | 47 | * The SQL FROM statment |
| 48 | 48 | * @var string |
| 49 | 49 | */ |
| 50 | - private $from = null; |
|
| 50 | + private $from = null; |
|
| 51 | 51 | |
| 52 | 52 | /** |
| 53 | 53 | * The SQL WHERE statment |
| 54 | 54 | * @var string |
| 55 | 55 | */ |
| 56 | - private $where = null; |
|
| 56 | + private $where = null; |
|
| 57 | 57 | |
| 58 | 58 | /** |
| 59 | 59 | * The SQL LIMIT statment |
| 60 | 60 | * @var string |
| 61 | 61 | */ |
| 62 | - private $limit = null; |
|
| 62 | + private $limit = null; |
|
| 63 | 63 | |
| 64 | 64 | /** |
| 65 | 65 | * The SQL JOIN statment |
| 66 | 66 | * @var string |
| 67 | 67 | */ |
| 68 | - private $join = null; |
|
| 68 | + private $join = null; |
|
| 69 | 69 | |
| 70 | 70 | /** |
| 71 | 71 | * The SQL ORDER BY statment |
| 72 | 72 | * @var string |
| 73 | 73 | */ |
| 74 | - private $orderBy = null; |
|
| 74 | + private $orderBy = null; |
|
| 75 | 75 | |
| 76 | 76 | /** |
| 77 | 77 | * The SQL GROUP BY statment |
| 78 | 78 | * @var string |
| 79 | 79 | */ |
| 80 | - private $groupBy = null; |
|
| 80 | + private $groupBy = null; |
|
| 81 | 81 | |
| 82 | 82 | /** |
| 83 | 83 | * The SQL HAVING statment |
| 84 | 84 | * @var string |
| 85 | 85 | */ |
| 86 | - private $having = null; |
|
| 86 | + private $having = null; |
|
| 87 | 87 | |
| 88 | 88 | /** |
| 89 | 89 | * The number of rows returned by the last query |
| 90 | 90 | * @var int |
| 91 | 91 | */ |
| 92 | - private $numRows = 0; |
|
| 92 | + private $numRows = 0; |
|
| 93 | 93 | |
| 94 | 94 | /** |
| 95 | 95 | * The last insert id for the primary key column that have auto increment or sequence |
| 96 | 96 | * @var mixed |
| 97 | 97 | */ |
| 98 | - private $insertId = null; |
|
| 98 | + private $insertId = null; |
|
| 99 | 99 | |
| 100 | 100 | /** |
| 101 | 101 | * The full SQL query statment after build for each command |
| 102 | 102 | * @var string |
| 103 | 103 | */ |
| 104 | - private $query = null; |
|
| 104 | + private $query = null; |
|
| 105 | 105 | |
| 106 | 106 | /** |
| 107 | 107 | * The error returned for the last query |
| 108 | 108 | * @var string |
| 109 | 109 | */ |
| 110 | - private $error = null; |
|
| 110 | + private $error = null; |
|
| 111 | 111 | |
| 112 | 112 | /** |
| 113 | 113 | * The result returned for the last query |
| 114 | 114 | * @var mixed |
| 115 | 115 | */ |
| 116 | - private $result = array(); |
|
| 116 | + private $result = array(); |
|
| 117 | 117 | |
| 118 | 118 | /** |
| 119 | 119 | * The prefix used in each database table |
| 120 | 120 | * @var string |
| 121 | 121 | */ |
| 122 | - private $prefix = null; |
|
| 122 | + private $prefix = null; |
|
| 123 | 123 | |
| 124 | 124 | /** |
| 125 | 125 | * The list of SQL valid operators |
| 126 | 126 | * @var array |
| 127 | 127 | */ |
| 128 | - private $operatorList = array('=','!=','<','>','<=','>=','<>'); |
|
| 128 | + private $operatorList = array('=', '!=', '<', '>', '<=', '>=', '<>'); |
|
| 129 | 129 | |
| 130 | 130 | /** |
| 131 | 131 | * The cache default time to live in second. 0 means no need to use the cache feature |
| 132 | 132 | * @var int |
| 133 | 133 | */ |
| 134 | - private $cacheTtl = 0; |
|
| 134 | + private $cacheTtl = 0; |
|
| 135 | 135 | |
| 136 | 136 | /** |
| 137 | 137 | * The cache current time to live. 0 means no need to use the cache feature |
| 138 | 138 | * @var int |
| 139 | 139 | */ |
| 140 | - private $temporaryCacheTtl = 0; |
|
| 140 | + private $temporaryCacheTtl = 0; |
|
| 141 | 141 | |
| 142 | 142 | /** |
| 143 | 143 | * The number of executed query for the current request |
| 144 | 144 | * @var int |
| 145 | 145 | */ |
| 146 | - private $queryCount = 0; |
|
| 146 | + private $queryCount = 0; |
|
| 147 | 147 | |
| 148 | 148 | /** |
| 149 | 149 | * The default data to be used in the statments query INSERT, UPDATE |
| 150 | 150 | * @var array |
| 151 | 151 | */ |
| 152 | - private $data = array(); |
|
| 152 | + private $data = array(); |
|
| 153 | 153 | |
| 154 | 154 | /** |
| 155 | 155 | * The database configuration |
| 156 | 156 | * @var array |
| 157 | 157 | */ |
| 158 | - private $config = array(); |
|
| 158 | + private $config = array(); |
|
| 159 | 159 | |
| 160 | 160 | /** |
| 161 | 161 | * The logger instance |
| 162 | 162 | * @var Log |
| 163 | 163 | */ |
| 164 | - private $logger = null; |
|
| 164 | + private $logger = null; |
|
| 165 | 165 | |
| 166 | 166 | |
| 167 | 167 | /** |
| 168 | 168 | * The cache instance |
| 169 | 169 | * @var CacheInterface |
| 170 | 170 | */ |
| 171 | - private $cacheInstance = null; |
|
| 171 | + private $cacheInstance = null; |
|
| 172 | 172 | |
| 173 | 173 | /** |
| 174 | 174 | * The benchmark instance |
| 175 | 175 | * @var Benchmark |
| 176 | 176 | */ |
| 177 | - private $benchmarkInstance = null; |
|
| 177 | + private $benchmarkInstance = null; |
|
| 178 | 178 | |
| 179 | 179 | |
| 180 | 180 | /** |
| 181 | 181 | * Construct new database |
| 182 | 182 | * @param array $overwriteConfig the config to overwrite with the config set in database.php |
| 183 | 183 | */ |
| 184 | - public function __construct($overwriteConfig = array()){ |
|
| 184 | + public function __construct($overwriteConfig = array()) { |
|
| 185 | 185 | //Set Log instance to use |
| 186 | 186 | $this->setLoggerFromParamOrCreateNewInstance(null); |
| 187 | 187 | |
@@ -195,23 +195,23 @@ discard block |
||
| 195 | 195 | * This is used to connect to database |
| 196 | 196 | * @return bool |
| 197 | 197 | */ |
| 198 | - public function connect(){ |
|
| 198 | + public function connect() { |
|
| 199 | 199 | $config = $this->getDatabaseConfiguration(); |
| 200 | - if(! empty($config)){ |
|
| 201 | - try{ |
|
| 200 | + if (!empty($config)) { |
|
| 201 | + try { |
|
| 202 | 202 | $this->pdo = new PDO($this->getDsnFromDriver(), $config['username'], $config['password']); |
| 203 | 203 | $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
| 204 | 204 | $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
| 205 | 205 | $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
| 206 | 206 | return true; |
| 207 | 207 | } |
| 208 | - catch (PDOException $e){ |
|
| 208 | + catch (PDOException $e) { |
|
| 209 | 209 | $this->logger->fatal($e->getMessage()); |
| 210 | 210 | show_error('Cannot connect to Database.'); |
| 211 | 211 | return false; |
| 212 | 212 | } |
| 213 | 213 | } |
| 214 | - else{ |
|
| 214 | + else { |
|
| 215 | 215 | show_error('Database configuration is not set.'); |
| 216 | 216 | return false; |
| 217 | 217 | } |
@@ -222,15 +222,15 @@ discard block |
||
| 222 | 222 | * @param string|array $table the table name or array of table list |
| 223 | 223 | * @return object the current Database instance |
| 224 | 224 | */ |
| 225 | - public function from($table){ |
|
| 226 | - if(is_array($table)){ |
|
| 225 | + public function from($table) { |
|
| 226 | + if (is_array($table)) { |
|
| 227 | 227 | $froms = ''; |
| 228 | - foreach($table as $key){ |
|
| 228 | + foreach ($table as $key) { |
|
| 229 | 229 | $froms .= $this->prefix . $key . ', '; |
| 230 | 230 | } |
| 231 | 231 | $this->from = rtrim($froms, ', '); |
| 232 | 232 | } |
| 233 | - else{ |
|
| 233 | + else { |
|
| 234 | 234 | $this->from = $this->prefix . $table; |
| 235 | 235 | } |
| 236 | 236 | return $this; |
@@ -241,7 +241,7 @@ discard block |
||
| 241 | 241 | * @param string|array $fields the field name or array of field list |
| 242 | 242 | * @return object the current Database instance |
| 243 | 243 | */ |
| 244 | - public function select($fields){ |
|
| 244 | + public function select($fields) { |
|
| 245 | 245 | $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
| 246 | 246 | $this->select = ($this->select == '*' ? $select : $this->select . ', ' . $select); |
| 247 | 247 | return $this; |
@@ -252,7 +252,7 @@ discard block |
||
| 252 | 252 | * @param string $field the field name to distinct |
| 253 | 253 | * @return object the current Database instance |
| 254 | 254 | */ |
| 255 | - public function distinct($field){ |
|
| 255 | + public function distinct($field) { |
|
| 256 | 256 | $distinct = ' DISTINCT ' . $field; |
| 257 | 257 | $this->select = ($this->select == '*' ? $distinct : $this->select . ', ' . $distinct); |
| 258 | 258 | |
@@ -265,7 +265,7 @@ discard block |
||
| 265 | 265 | * @param string $name if is not null represent the alias used for this field in the result |
| 266 | 266 | * @return object the current Database instance |
| 267 | 267 | */ |
| 268 | - public function max($field, $name = null){ |
|
| 268 | + public function max($field, $name = null) { |
|
| 269 | 269 | $func = 'MAX(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
| 270 | 270 | $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
| 271 | 271 | return $this; |
@@ -277,7 +277,7 @@ discard block |
||
| 277 | 277 | * @param string $name if is not null represent the alias used for this field in the result |
| 278 | 278 | * @return object the current Database instance |
| 279 | 279 | */ |
| 280 | - public function min($field, $name = null){ |
|
| 280 | + public function min($field, $name = null) { |
|
| 281 | 281 | $func = 'MIN(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
| 282 | 282 | $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
| 283 | 283 | return $this; |
@@ -289,7 +289,7 @@ discard block |
||
| 289 | 289 | * @param string $name if is not null represent the alias used for this field in the result |
| 290 | 290 | * @return object the current Database instance |
| 291 | 291 | */ |
| 292 | - public function sum($field, $name = null){ |
|
| 292 | + public function sum($field, $name = null) { |
|
| 293 | 293 | $func = 'SUM(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
| 294 | 294 | $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
| 295 | 295 | return $this; |
@@ -301,7 +301,7 @@ discard block |
||
| 301 | 301 | * @param string $name if is not null represent the alias used for this field in the result |
| 302 | 302 | * @return object the current Database instance |
| 303 | 303 | */ |
| 304 | - public function count($field = '*', $name = null){ |
|
| 304 | + public function count($field = '*', $name = null) { |
|
| 305 | 305 | $func = 'COUNT(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
| 306 | 306 | $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
| 307 | 307 | return $this; |
@@ -313,7 +313,7 @@ discard block |
||
| 313 | 313 | * @param string $name if is not null represent the alias used for this field in the result |
| 314 | 314 | * @return object the current Database instance |
| 315 | 315 | */ |
| 316 | - public function avg($field, $name = null){ |
|
| 316 | + public function avg($field, $name = null) { |
|
| 317 | 317 | $func = 'AVG(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
| 318 | 318 | $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
| 319 | 319 | return $this; |
@@ -328,16 +328,16 @@ discard block |
||
| 328 | 328 | * @param string $type the type of join (INNER, LEFT, RIGHT) |
| 329 | 329 | * @return object the current Database instance |
| 330 | 330 | */ |
| 331 | - public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){ |
|
| 331 | + public function join($table, $field1 = null, $op = null, $field2 = null, $type = '') { |
|
| 332 | 332 | $on = $field1; |
| 333 | 333 | $table = $this->prefix . $table; |
| 334 | - if(! is_null($op)){ |
|
| 335 | - $on = (! in_array($op, $this->operatorList) ? $this->prefix . $field1 . ' = ' . $this->prefix . $op : $this->prefix . $field1 . ' ' . $op . ' ' . $this->prefix . $field2); |
|
| 334 | + if (!is_null($op)) { |
|
| 335 | + $on = (!in_array($op, $this->operatorList) ? $this->prefix . $field1 . ' = ' . $this->prefix . $op : $this->prefix . $field1 . ' ' . $op . ' ' . $this->prefix . $field2); |
|
| 336 | 336 | } |
| 337 | - if (empty($this->join)){ |
|
| 337 | + if (empty($this->join)) { |
|
| 338 | 338 | $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 339 | 339 | } |
| 340 | - else{ |
|
| 340 | + else { |
|
| 341 | 341 | $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 342 | 342 | } |
| 343 | 343 | return $this; |
@@ -348,7 +348,7 @@ discard block |
||
| 348 | 348 | * @see Database::join() |
| 349 | 349 | * @return object the current Database instance |
| 350 | 350 | */ |
| 351 | - public function innerJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 351 | + public function innerJoin($table, $field1, $op = null, $field2 = '') { |
|
| 352 | 352 | return $this->join($table, $field1, $op, $field2, 'INNER '); |
| 353 | 353 | } |
| 354 | 354 | |
@@ -357,7 +357,7 @@ discard block |
||
| 357 | 357 | * @see Database::join() |
| 358 | 358 | * @return object the current Database instance |
| 359 | 359 | */ |
| 360 | - public function leftJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 360 | + public function leftJoin($table, $field1, $op = null, $field2 = '') { |
|
| 361 | 361 | return $this->join($table, $field1, $op, $field2, 'LEFT '); |
| 362 | 362 | } |
| 363 | 363 | |
@@ -366,7 +366,7 @@ discard block |
||
| 366 | 366 | * @see Database::join() |
| 367 | 367 | * @return object the current Database instance |
| 368 | 368 | */ |
| 369 | - public function rightJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 369 | + public function rightJoin($table, $field1, $op = null, $field2 = '') { |
|
| 370 | 370 | return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
| 371 | 371 | } |
| 372 | 372 | |
@@ -375,7 +375,7 @@ discard block |
||
| 375 | 375 | * @see Database::join() |
| 376 | 376 | * @return object the current Database instance |
| 377 | 377 | */ |
| 378 | - public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 378 | + public function fullOuterJoin($table, $field1, $op = null, $field2 = '') { |
|
| 379 | 379 | return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
| 380 | 380 | } |
| 381 | 381 | |
@@ -384,7 +384,7 @@ discard block |
||
| 384 | 384 | * @see Database::join() |
| 385 | 385 | * @return object the current Database instance |
| 386 | 386 | */ |
| 387 | - public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 387 | + public function leftOuterJoin($table, $field1, $op = null, $field2 = '') { |
|
| 388 | 388 | return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
| 389 | 389 | } |
| 390 | 390 | |
@@ -393,7 +393,7 @@ discard block |
||
| 393 | 393 | * @see Database::join() |
| 394 | 394 | * @return object the current Database instance |
| 395 | 395 | */ |
| 396 | - public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 396 | + public function rightOuterJoin($table, $field1, $op = null, $field2 = '') { |
|
| 397 | 397 | return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
| 398 | 398 | } |
| 399 | 399 | |
@@ -403,14 +403,14 @@ discard block |
||
| 403 | 403 | * @param string $andOr the separator type used 'AND', 'OR', etc. |
| 404 | 404 | * @return object the current Database instance |
| 405 | 405 | */ |
| 406 | - public function whereIsNull($field, $andOr = 'AND'){ |
|
| 407 | - if(is_array($field)){ |
|
| 408 | - foreach($field as $f){ |
|
| 406 | + public function whereIsNull($field, $andOr = 'AND') { |
|
| 407 | + if (is_array($field)) { |
|
| 408 | + foreach ($field as $f) { |
|
| 409 | 409 | $this->whereIsNull($f, $andOr); |
| 410 | 410 | } |
| 411 | 411 | } |
| 412 | - else{ |
|
| 413 | - $this->setWhereStr($field.' IS NULL ', $andOr); |
|
| 412 | + else { |
|
| 413 | + $this->setWhereStr($field . ' IS NULL ', $andOr); |
|
| 414 | 414 | } |
| 415 | 415 | return $this; |
| 416 | 416 | } |
@@ -421,14 +421,14 @@ discard block |
||
| 421 | 421 | * @param string $andOr the separator type used 'AND', 'OR', etc. |
| 422 | 422 | * @return object the current Database instance |
| 423 | 423 | */ |
| 424 | - public function whereIsNotNull($field, $andOr = 'AND'){ |
|
| 425 | - if(is_array($field)){ |
|
| 426 | - foreach($field as $f){ |
|
| 424 | + public function whereIsNotNull($field, $andOr = 'AND') { |
|
| 425 | + if (is_array($field)) { |
|
| 426 | + foreach ($field as $f) { |
|
| 427 | 427 | $this->whereIsNotNull($f, $andOr); |
| 428 | 428 | } |
| 429 | 429 | } |
| 430 | - else{ |
|
| 431 | - $this->setWhereStr($field.' IS NOT NULL ', $andOr); |
|
| 430 | + else { |
|
| 431 | + $this->setWhereStr($field . ' IS NOT NULL ', $andOr); |
|
| 432 | 432 | } |
| 433 | 433 | return $this; |
| 434 | 434 | } |
@@ -443,13 +443,13 @@ discard block |
||
| 443 | 443 | * @param boolean $escape whether to escape or not the $val |
| 444 | 444 | * @return object the current Database instance |
| 445 | 445 | */ |
| 446 | - public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
|
| 446 | + public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true) { |
|
| 447 | 447 | $whereStr = ''; |
| 448 | - if (is_array($where)){ |
|
| 448 | + if (is_array($where)) { |
|
| 449 | 449 | $whereStr = $this->getWhereStrIfIsArray($where, $type, $andOr, $escape); |
| 450 | 450 | } |
| 451 | - else{ |
|
| 452 | - if(is_array($op)){ |
|
| 451 | + else { |
|
| 452 | + if (is_array($op)) { |
|
| 453 | 453 | $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
| 454 | 454 | } else { |
| 455 | 455 | $whereStr = $this->getWhereStrForOperator($where, $op, $val, $type, $escape = true); |
@@ -464,7 +464,7 @@ discard block |
||
| 464 | 464 | * @see Database::where() |
| 465 | 465 | * @return object the current Database instance |
| 466 | 466 | */ |
| 467 | - public function orWhere($where, $op = null, $val = null, $escape = true){ |
|
| 467 | + public function orWhere($where, $op = null, $val = null, $escape = true) { |
|
| 468 | 468 | return $this->where($where, $op, $val, '', 'OR', $escape); |
| 469 | 469 | } |
| 470 | 470 | |
@@ -474,7 +474,7 @@ discard block |
||
| 474 | 474 | * @see Database::where() |
| 475 | 475 | * @return object the current Database instance |
| 476 | 476 | */ |
| 477 | - public function notWhere($where, $op = null, $val = null, $escape = true){ |
|
| 477 | + public function notWhere($where, $op = null, $val = null, $escape = true) { |
|
| 478 | 478 | return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
| 479 | 479 | } |
| 480 | 480 | |
@@ -483,7 +483,7 @@ discard block |
||
| 483 | 483 | * @see Database::where() |
| 484 | 484 | * @return object the current Database instance |
| 485 | 485 | */ |
| 486 | - public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
|
| 486 | + public function orNotWhere($where, $op = null, $val = null, $escape = true) { |
|
| 487 | 487 | return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
| 488 | 488 | } |
| 489 | 489 | |
@@ -493,15 +493,15 @@ discard block |
||
| 493 | 493 | * @param string $andOr the multiple conditions separator (AND, OR, etc.) |
| 494 | 494 | * @return object the current Database instance |
| 495 | 495 | */ |
| 496 | - public function groupStart($type = '', $andOr = ' AND'){ |
|
| 497 | - if (empty($this->where)){ |
|
| 496 | + public function groupStart($type = '', $andOr = ' AND') { |
|
| 497 | + if (empty($this->where)) { |
|
| 498 | 498 | $this->where = $type . ' ('; |
| 499 | 499 | } |
| 500 | - else{ |
|
| 501 | - if(substr($this->where, -1) == '('){ |
|
| 500 | + else { |
|
| 501 | + if (substr($this->where, -1) == '(') { |
|
| 502 | 502 | $this->where .= $type . ' ('; |
| 503 | 503 | } |
| 504 | - else{ |
|
| 504 | + else { |
|
| 505 | 505 | $this->where .= $andOr . ' ' . $type . ' ('; |
| 506 | 506 | } |
| 507 | 507 | } |
@@ -513,7 +513,7 @@ discard block |
||
| 513 | 513 | * @see Database::groupStart() |
| 514 | 514 | * @return object the current Database instance |
| 515 | 515 | */ |
| 516 | - public function notGroupStart(){ |
|
| 516 | + public function notGroupStart() { |
|
| 517 | 517 | return $this->groupStart('NOT'); |
| 518 | 518 | } |
| 519 | 519 | |
@@ -522,7 +522,7 @@ discard block |
||
| 522 | 522 | * @see Database::groupStart() |
| 523 | 523 | * @return object the current Database instance |
| 524 | 524 | */ |
| 525 | - public function orGroupStart(){ |
|
| 525 | + public function orGroupStart() { |
|
| 526 | 526 | return $this->groupStart('', ' OR'); |
| 527 | 527 | } |
| 528 | 528 | |
@@ -531,7 +531,7 @@ discard block |
||
| 531 | 531 | * @see Database::groupStart() |
| 532 | 532 | * @return object the current Database instance |
| 533 | 533 | */ |
| 534 | - public function orNotGroupStart(){ |
|
| 534 | + public function orNotGroupStart() { |
|
| 535 | 535 | return $this->groupStart('NOT', ' OR'); |
| 536 | 536 | } |
| 537 | 537 | |
@@ -539,7 +539,7 @@ discard block |
||
| 539 | 539 | * Close the parenthesis for the grouped SQL |
| 540 | 540 | * @return object the current Database instance |
| 541 | 541 | */ |
| 542 | - public function groupEnd(){ |
|
| 542 | + public function groupEnd() { |
|
| 543 | 543 | $this->where .= ')'; |
| 544 | 544 | return $this; |
| 545 | 545 | } |
@@ -553,10 +553,10 @@ discard block |
||
| 553 | 553 | * @param boolean $escape whether to escape or not the values |
| 554 | 554 | * @return object the current Database instance |
| 555 | 555 | */ |
| 556 | - public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
|
| 556 | + public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true) { |
|
| 557 | 557 | $_keys = array(); |
| 558 | - foreach ($keys as $k => $v){ |
|
| 559 | - if(is_null($v)){ |
|
| 558 | + foreach ($keys as $k => $v) { |
|
| 559 | + if (is_null($v)) { |
|
| 560 | 560 | $v = ''; |
| 561 | 561 | } |
| 562 | 562 | $_keys[] = (is_numeric($v) ? $v : $this->escape($v, $escape)); |
@@ -572,7 +572,7 @@ discard block |
||
| 572 | 572 | * @see Database::in() |
| 573 | 573 | * @return object the current Database instance |
| 574 | 574 | */ |
| 575 | - public function notIn($field, array $keys, $escape = true){ |
|
| 575 | + public function notIn($field, array $keys, $escape = true) { |
|
| 576 | 576 | return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
| 577 | 577 | } |
| 578 | 578 | |
@@ -581,7 +581,7 @@ discard block |
||
| 581 | 581 | * @see Database::in() |
| 582 | 582 | * @return object the current Database instance |
| 583 | 583 | */ |
| 584 | - public function orIn($field, array $keys, $escape = true){ |
|
| 584 | + public function orIn($field, array $keys, $escape = true) { |
|
| 585 | 585 | return $this->in($field, $keys, '', 'OR', $escape); |
| 586 | 586 | } |
| 587 | 587 | |
@@ -590,7 +590,7 @@ discard block |
||
| 590 | 590 | * @see Database::in() |
| 591 | 591 | * @return object the current Database instance |
| 592 | 592 | */ |
| 593 | - public function orNotIn($field, array $keys, $escape = true){ |
|
| 593 | + public function orNotIn($field, array $keys, $escape = true) { |
|
| 594 | 594 | return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
| 595 | 595 | } |
| 596 | 596 | |
@@ -604,11 +604,11 @@ discard block |
||
| 604 | 604 | * @param boolean $escape whether to escape or not the values |
| 605 | 605 | * @return object the current Database instance |
| 606 | 606 | */ |
| 607 | - public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
|
| 608 | - if(is_null($value1)){ |
|
| 607 | + public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true) { |
|
| 608 | + if (is_null($value1)) { |
|
| 609 | 609 | $value1 = ''; |
| 610 | 610 | } |
| 611 | - if(is_null($value2)){ |
|
| 611 | + if (is_null($value2)) { |
|
| 612 | 612 | $value2 = ''; |
| 613 | 613 | } |
| 614 | 614 | $whereStr = $field . ' ' . $type . ' BETWEEN ' . $this->escape($value1, $escape) . ' AND ' . $this->escape($value2, $escape); |
@@ -621,7 +621,7 @@ discard block |
||
| 621 | 621 | * @see Database::between() |
| 622 | 622 | * @return object the current Database instance |
| 623 | 623 | */ |
| 624 | - public function notBetween($field, $value1, $value2, $escape = true){ |
|
| 624 | + public function notBetween($field, $value1, $value2, $escape = true) { |
|
| 625 | 625 | return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
| 626 | 626 | } |
| 627 | 627 | |
@@ -630,7 +630,7 @@ discard block |
||
| 630 | 630 | * @see Database::between() |
| 631 | 631 | * @return object the current Database instance |
| 632 | 632 | */ |
| 633 | - public function orBetween($field, $value1, $value2, $escape = true){ |
|
| 633 | + public function orBetween($field, $value1, $value2, $escape = true) { |
|
| 634 | 634 | return $this->between($field, $value1, $value2, '', 'OR', $escape); |
| 635 | 635 | } |
| 636 | 636 | |
@@ -639,7 +639,7 @@ discard block |
||
| 639 | 639 | * @see Database::between() |
| 640 | 640 | * @return object the current Database instance |
| 641 | 641 | */ |
| 642 | - public function orNotBetween($field, $value1, $value2, $escape = true){ |
|
| 642 | + public function orNotBetween($field, $value1, $value2, $escape = true) { |
|
| 643 | 643 | return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
| 644 | 644 | } |
| 645 | 645 | |
@@ -652,8 +652,8 @@ discard block |
||
| 652 | 652 | * @param boolean $escape whether to escape or not the values |
| 653 | 653 | * @return object the current Database instance |
| 654 | 654 | */ |
| 655 | - public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
|
| 656 | - if(empty($data)){ |
|
| 655 | + public function like($field, $data, $type = '', $andOr = 'AND', $escape = true) { |
|
| 656 | + if (empty($data)) { |
|
| 657 | 657 | $data = ''; |
| 658 | 658 | } |
| 659 | 659 | $this->setWhereStr($field . ' ' . $type . ' LIKE ' . ($this->escape($data, $escape)), $andOr); |
@@ -665,7 +665,7 @@ discard block |
||
| 665 | 665 | * @see Database::like() |
| 666 | 666 | * @return object the current Database instance |
| 667 | 667 | */ |
| 668 | - public function orLike($field, $data, $escape = true){ |
|
| 668 | + public function orLike($field, $data, $escape = true) { |
|
| 669 | 669 | return $this->like($field, $data, '', 'OR', $escape); |
| 670 | 670 | } |
| 671 | 671 | |
@@ -674,7 +674,7 @@ discard block |
||
| 674 | 674 | * @see Database::like() |
| 675 | 675 | * @return object the current Database instance |
| 676 | 676 | */ |
| 677 | - public function notLike($field, $data, $escape = true){ |
|
| 677 | + public function notLike($field, $data, $escape = true) { |
|
| 678 | 678 | return $this->like($field, $data, 'NOT ', 'AND', $escape); |
| 679 | 679 | } |
| 680 | 680 | |
@@ -683,7 +683,7 @@ discard block |
||
| 683 | 683 | * @see Database::like() |
| 684 | 684 | * @return object the current Database instance |
| 685 | 685 | */ |
| 686 | - public function orNotLike($field, $data, $escape = true){ |
|
| 686 | + public function orNotLike($field, $data, $escape = true) { |
|
| 687 | 687 | return $this->like($field, $data, 'NOT ', 'OR', $escape); |
| 688 | 688 | } |
| 689 | 689 | |
@@ -694,14 +694,14 @@ discard block |
||
| 694 | 694 | * @param int $limitEnd the limit count |
| 695 | 695 | * @return object the current Database instance |
| 696 | 696 | */ |
| 697 | - public function limit($limit, $limitEnd = null){ |
|
| 698 | - if(empty($limit)){ |
|
| 697 | + public function limit($limit, $limitEnd = null) { |
|
| 698 | + if (empty($limit)) { |
|
| 699 | 699 | return; |
| 700 | 700 | } |
| 701 | - if (! is_null($limitEnd)){ |
|
| 701 | + if (!is_null($limitEnd)) { |
|
| 702 | 702 | $this->limit = $limit . ', ' . $limitEnd; |
| 703 | 703 | } |
| 704 | - else{ |
|
| 704 | + else { |
|
| 705 | 705 | $this->limit = $limit; |
| 706 | 706 | } |
| 707 | 707 | return $this; |
@@ -713,11 +713,11 @@ discard block |
||
| 713 | 713 | * @param string $orderDir the order direction (ASC or DESC) |
| 714 | 714 | * @return object the current Database instance |
| 715 | 715 | */ |
| 716 | - public function orderBy($orderBy, $orderDir = ' ASC'){ |
|
| 717 | - if(stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
| 716 | + public function orderBy($orderBy, $orderDir = ' ASC') { |
|
| 717 | + if (stristr($orderBy, ' ') || $orderBy == 'rand()') { |
|
| 718 | 718 | $this->orderBy = empty($this->orderBy) ? $orderBy : $this->orderBy . ', ' . $orderBy; |
| 719 | 719 | } |
| 720 | - else{ |
|
| 720 | + else { |
|
| 721 | 721 | $this->orderBy = empty($this->orderBy) ? ($orderBy . ' ' |
| 722 | 722 | . strtoupper($orderDir)) : $this->orderBy |
| 723 | 723 | . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
@@ -730,11 +730,11 @@ discard block |
||
| 730 | 730 | * @param string|array $field the field name used or array of field list |
| 731 | 731 | * @return object the current Database instance |
| 732 | 732 | */ |
| 733 | - public function groupBy($field){ |
|
| 734 | - if(is_array($field)){ |
|
| 733 | + public function groupBy($field) { |
|
| 734 | + if (is_array($field)) { |
|
| 735 | 735 | $this->groupBy = implode(', ', $field); |
| 736 | 736 | } |
| 737 | - else{ |
|
| 737 | + else { |
|
| 738 | 738 | $this->groupBy = $field; |
| 739 | 739 | } |
| 740 | 740 | return $this; |
@@ -748,13 +748,13 @@ discard block |
||
| 748 | 748 | * @param boolean $escape whether to escape or not the values |
| 749 | 749 | * @return object the current Database instance |
| 750 | 750 | */ |
| 751 | - public function having($field, $op = null, $val = null, $escape = true){ |
|
| 752 | - if(is_array($op)){ |
|
| 751 | + public function having($field, $op = null, $val = null, $escape = true) { |
|
| 752 | + if (is_array($op)) { |
|
| 753 | 753 | $x = explode('?', $field); |
| 754 | 754 | $w = ''; |
| 755 | - foreach($x as $k => $v){ |
|
| 756 | - if(!empty($v)){ |
|
| 757 | - if(isset($op[$k]) && is_null($op[$k])){ |
|
| 755 | + foreach ($x as $k => $v) { |
|
| 756 | + if (!empty($v)) { |
|
| 757 | + if (isset($op[$k]) && is_null($op[$k])) { |
|
| 758 | 758 | $op[$k] = ''; |
| 759 | 759 | } |
| 760 | 760 | $w .= $v . (isset($op[$k]) ? $this->escape($op[$k], $escape) : ''); |
@@ -762,14 +762,14 @@ discard block |
||
| 762 | 762 | } |
| 763 | 763 | $this->having = $w; |
| 764 | 764 | } |
| 765 | - else if (! in_array($op, $this->operatorList)){ |
|
| 766 | - if(is_null($op)){ |
|
| 765 | + else if (!in_array($op, $this->operatorList)) { |
|
| 766 | + if (is_null($op)) { |
|
| 767 | 767 | $op = ''; |
| 768 | 768 | } |
| 769 | 769 | $this->having = $field . ' > ' . ($this->escape($op, $escape)); |
| 770 | 770 | } |
| 771 | - else{ |
|
| 772 | - if(is_null($val)){ |
|
| 771 | + else { |
|
| 772 | + if (is_null($val)) { |
|
| 773 | 773 | $val = ''; |
| 774 | 774 | } |
| 775 | 775 | $this->having = $field . ' ' . $op . ' ' . ($this->escape($val, $escape)); |
@@ -781,7 +781,7 @@ discard block |
||
| 781 | 781 | * Return the number of rows returned by the current query |
| 782 | 782 | * @return int |
| 783 | 783 | */ |
| 784 | - public function numRows(){ |
|
| 784 | + public function numRows() { |
|
| 785 | 785 | return $this->numRows; |
| 786 | 786 | } |
| 787 | 787 | |
@@ -789,15 +789,15 @@ discard block |
||
| 789 | 789 | * Return the last insert id value |
| 790 | 790 | * @return mixed |
| 791 | 791 | */ |
| 792 | - public function insertId(){ |
|
| 792 | + public function insertId() { |
|
| 793 | 793 | return $this->insertId; |
| 794 | 794 | } |
| 795 | 795 | |
| 796 | 796 | /** |
| 797 | 797 | * Show an error got from the current query (SQL command synthax error, database driver returned error, etc.) |
| 798 | 798 | */ |
| 799 | - public function error(){ |
|
| 800 | - if($this->error){ |
|
| 799 | + public function error() { |
|
| 800 | + if ($this->error) { |
|
| 801 | 801 | show_error('Query: "' . $this->query . '" Error: ' . $this->error, 'Database Error'); |
| 802 | 802 | } |
| 803 | 803 | } |
@@ -808,14 +808,14 @@ discard block |
||
| 808 | 808 | * If is string will determine the result type "array" or "object" |
| 809 | 809 | * @return mixed the query SQL string or the record result |
| 810 | 810 | */ |
| 811 | - public function get($returnSQLQueryOrResultType = false){ |
|
| 811 | + public function get($returnSQLQueryOrResultType = false) { |
|
| 812 | 812 | $this->limit = 1; |
| 813 | 813 | $query = $this->getAll(true); |
| 814 | - if($returnSQLQueryOrResultType === true){ |
|
| 814 | + if ($returnSQLQueryOrResultType === true) { |
|
| 815 | 815 | return $query; |
| 816 | 816 | } |
| 817 | - else{ |
|
| 818 | - return $this->query( $query, false, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 817 | + else { |
|
| 818 | + return $this->query($query, false, (($returnSQLQueryOrResultType == 'array') ? true : false)); |
|
| 819 | 819 | } |
| 820 | 820 | } |
| 821 | 821 | |
@@ -825,37 +825,37 @@ discard block |
||
| 825 | 825 | * If is string will determine the result type "array" or "object" |
| 826 | 826 | * @return mixed the query SQL string or the record result |
| 827 | 827 | */ |
| 828 | - public function getAll($returnSQLQueryOrResultType = false){ |
|
| 828 | + public function getAll($returnSQLQueryOrResultType = false) { |
|
| 829 | 829 | $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
| 830 | - if (! empty($this->join)){ |
|
| 830 | + if (!empty($this->join)) { |
|
| 831 | 831 | $query .= $this->join; |
| 832 | 832 | } |
| 833 | 833 | |
| 834 | - if (! empty($this->where)){ |
|
| 834 | + if (!empty($this->where)) { |
|
| 835 | 835 | $query .= ' WHERE ' . $this->where; |
| 836 | 836 | } |
| 837 | 837 | |
| 838 | - if (! empty($this->groupBy)){ |
|
| 838 | + if (!empty($this->groupBy)) { |
|
| 839 | 839 | $query .= ' GROUP BY ' . $this->groupBy; |
| 840 | 840 | } |
| 841 | 841 | |
| 842 | - if (! empty($this->having)){ |
|
| 842 | + if (!empty($this->having)) { |
|
| 843 | 843 | $query .= ' HAVING ' . $this->having; |
| 844 | 844 | } |
| 845 | 845 | |
| 846 | - if (! empty($this->orderBy)){ |
|
| 846 | + if (!empty($this->orderBy)) { |
|
| 847 | 847 | $query .= ' ORDER BY ' . $this->orderBy; |
| 848 | 848 | } |
| 849 | 849 | |
| 850 | - if(! empty($this->limit)){ |
|
| 850 | + if (!empty($this->limit)) { |
|
| 851 | 851 | $query .= ' LIMIT ' . $this->limit; |
| 852 | 852 | } |
| 853 | 853 | |
| 854 | - if($returnSQLQueryOrResultType === true){ |
|
| 854 | + if ($returnSQLQueryOrResultType === true) { |
|
| 855 | 855 | return $query; |
| 856 | 856 | } |
| 857 | - else{ |
|
| 858 | - return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 857 | + else { |
|
| 858 | + return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false)); |
|
| 859 | 859 | } |
| 860 | 860 | } |
| 861 | 861 | |
@@ -865,10 +865,10 @@ discard block |
||
| 865 | 865 | * @param boolean $escape whether to escape or not the values |
| 866 | 866 | * @return mixed the insert id of the new record or null |
| 867 | 867 | */ |
| 868 | - public function insert($data = array(), $escape = true){ |
|
| 868 | + public function insert($data = array(), $escape = true) { |
|
| 869 | 869 | $column = array(); |
| 870 | 870 | $val = array(); |
| 871 | - if(empty($data) && $this->getData()){ |
|
| 871 | + if (empty($data) && $this->getData()) { |
|
| 872 | 872 | //as when using $this->setData() the data already escaped |
| 873 | 873 | $escape = false; |
| 874 | 874 | $data = $this->getData(); |
@@ -881,14 +881,14 @@ discard block |
||
| 881 | 881 | $query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
| 882 | 882 | $query = $this->query($query); |
| 883 | 883 | |
| 884 | - if ($query){ |
|
| 885 | - if(! $this->pdo){ |
|
| 884 | + if ($query) { |
|
| 885 | + if (!$this->pdo) { |
|
| 886 | 886 | $this->connect(); |
| 887 | 887 | } |
| 888 | 888 | $this->insertId = $this->pdo->lastInsertId(); |
| 889 | 889 | return $this->insertId(); |
| 890 | 890 | } |
| 891 | - else{ |
|
| 891 | + else { |
|
| 892 | 892 | return false; |
| 893 | 893 | } |
| 894 | 894 | } |
@@ -899,27 +899,27 @@ discard block |
||
| 899 | 899 | * @param boolean $escape whether to escape or not the values |
| 900 | 900 | * @return mixed the update status |
| 901 | 901 | */ |
| 902 | - public function update($data = array(), $escape = true){ |
|
| 902 | + public function update($data = array(), $escape = true) { |
|
| 903 | 903 | $query = 'UPDATE ' . $this->from . ' SET '; |
| 904 | 904 | $values = array(); |
| 905 | - if(empty($data) && $this->getData()){ |
|
| 905 | + if (empty($data) && $this->getData()) { |
|
| 906 | 906 | //as when using $this->setData() the data already escaped |
| 907 | 907 | $escape = false; |
| 908 | 908 | $data = $this->getData(); |
| 909 | 909 | } |
| 910 | - foreach ($data as $column => $val){ |
|
| 910 | + foreach ($data as $column => $val) { |
|
| 911 | 911 | $values[] = $column . ' = ' . ($this->escape($val, $escape)); |
| 912 | 912 | } |
| 913 | 913 | $query .= implode(', ', $values); |
| 914 | - if (! empty($this->where)){ |
|
| 914 | + if (!empty($this->where)) { |
|
| 915 | 915 | $query .= ' WHERE ' . $this->where; |
| 916 | 916 | } |
| 917 | 917 | |
| 918 | - if (! empty($this->orderBy)){ |
|
| 918 | + if (!empty($this->orderBy)) { |
|
| 919 | 919 | $query .= ' ORDER BY ' . $this->orderBy; |
| 920 | 920 | } |
| 921 | 921 | |
| 922 | - if (! empty($this->limit)){ |
|
| 922 | + if (!empty($this->limit)) { |
|
| 923 | 923 | $query .= ' LIMIT ' . $this->limit; |
| 924 | 924 | } |
| 925 | 925 | return $this->query($query); |
@@ -929,22 +929,22 @@ discard block |
||
| 929 | 929 | * Delete the record in database |
| 930 | 930 | * @return mixed the delete status |
| 931 | 931 | */ |
| 932 | - public function delete(){ |
|
| 932 | + public function delete() { |
|
| 933 | 933 | $query = 'DELETE FROM ' . $this->from; |
| 934 | 934 | |
| 935 | - if (! empty($this->where)){ |
|
| 935 | + if (!empty($this->where)) { |
|
| 936 | 936 | $query .= ' WHERE ' . $this->where; |
| 937 | 937 | } |
| 938 | 938 | |
| 939 | - if (! empty($this->orderBy)){ |
|
| 939 | + if (!empty($this->orderBy)) { |
|
| 940 | 940 | $query .= ' ORDER BY ' . $this->orderBy; |
| 941 | 941 | } |
| 942 | 942 | |
| 943 | - if (! empty($this->limit)){ |
|
| 943 | + if (!empty($this->limit)) { |
|
| 944 | 944 | $query .= ' LIMIT ' . $this->limit; |
| 945 | 945 | } |
| 946 | 946 | |
| 947 | - if($query == 'DELETE FROM ' . $this->from && $this->config['driver'] != 'sqlite'){ |
|
| 947 | + if ($query == 'DELETE FROM ' . $this->from && $this->config['driver'] != 'sqlite') { |
|
| 948 | 948 | $query = 'TRUNCATE TABLE ' . $this->from; |
| 949 | 949 | } |
| 950 | 950 | return $this->query($query); |
@@ -959,14 +959,14 @@ discard block |
||
| 959 | 959 | * @param boolean $array return the result as array |
| 960 | 960 | * @return mixed the query result |
| 961 | 961 | */ |
| 962 | - public function query($query, $all = true, $array = false){ |
|
| 962 | + public function query($query, $all = true, $array = false) { |
|
| 963 | 963 | $this->reset(); |
| 964 | 964 | $query = $this->transformPreparedQuery($query, $all); |
| 965 | 965 | $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
| 966 | 966 | |
| 967 | 967 | $isSqlSELECTQuery = stristr($this->query, 'SELECT'); |
| 968 | 968 | |
| 969 | - $this->logger->info('Execute SQL query ['.$this->query.'], return type: ' . ($array?'ARRAY':'OBJECT') .', return as list: ' . ($all ? 'YES':'NO')); |
|
| 969 | + $this->logger->info('Execute SQL query [' . $this->query . '], return type: ' . ($array ? 'ARRAY' : 'OBJECT') . ', return as list: ' . ($all ? 'YES' : 'NO')); |
|
| 970 | 970 | //cache expire time |
| 971 | 971 | $cacheExpire = $this->temporaryCacheTtl; |
| 972 | 972 | |
@@ -982,16 +982,16 @@ discard block |
||
| 982 | 982 | //if can use cache feature for this query |
| 983 | 983 | $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
| 984 | 984 | |
| 985 | - if ($dbCacheStatus && $isSqlSELECTQuery){ |
|
| 985 | + if ($dbCacheStatus && $isSqlSELECTQuery) { |
|
| 986 | 986 | $cacheContent = $this->getCacheContentForQuery($query, $all, $array); |
| 987 | 987 | } |
| 988 | - else{ |
|
| 988 | + else { |
|
| 989 | 989 | $this->logger->info('The cache is not enabled for this query or is not the SELECT query, get the result directly from real database'); |
| 990 | 990 | } |
| 991 | 991 | |
| 992 | - if (! $cacheContent && $isSqlSELECTQuery){ |
|
| 992 | + if (!$cacheContent && $isSqlSELECTQuery) { |
|
| 993 | 993 | $sqlQuery = $this->runSqlQuery($query, $all, $array); |
| 994 | - if ($sqlQuery){ |
|
| 994 | + if ($sqlQuery) { |
|
| 995 | 995 | $this->setQueryResultForSelect($sqlQuery, $all, $array); |
| 996 | 996 | $this->setCacheContentForQuery( |
| 997 | 997 | $this->query, |
@@ -1002,22 +1002,22 @@ discard block |
||
| 1002 | 1002 | ); |
| 1003 | 1003 | } |
| 1004 | 1004 | } |
| 1005 | - else if ((! $cacheContent && !$isSqlSELECTQuery) || ($cacheContent && !$isSqlSELECTQuery)){ |
|
| 1005 | + else if ((!$cacheContent && !$isSqlSELECTQuery) || ($cacheContent && !$isSqlSELECTQuery)) { |
|
| 1006 | 1006 | $sqlQuery = $this->runSqlQuery($query, $all, $array); |
| 1007 | - if($sqlQuery){ |
|
| 1007 | + if ($sqlQuery) { |
|
| 1008 | 1008 | $this->setQueryResultForNonSelect($sqlQuery, $all, $array); |
| 1009 | 1009 | } |
| 1010 | - if (! $this->result){ |
|
| 1010 | + if (!$this->result) { |
|
| 1011 | 1011 | $this->setQueryError(); |
| 1012 | 1012 | } |
| 1013 | 1013 | } |
| 1014 | - else{ |
|
| 1015 | - $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 1014 | + else { |
|
| 1015 | + $this->logger->info('The result for query [' . $this->query . '] already cached use it'); |
|
| 1016 | 1016 | $this->result = $cacheContent; |
| 1017 | 1017 | $this->numRows = count($this->result); |
| 1018 | 1018 | } |
| 1019 | 1019 | $this->queryCount++; |
| 1020 | - if(! $this->result){ |
|
| 1020 | + if (!$this->result) { |
|
| 1021 | 1021 | $this->logger->info('No result where found for the query [' . $query . ']'); |
| 1022 | 1022 | } |
| 1023 | 1023 | return $this->result; |
@@ -1028,8 +1028,8 @@ discard block |
||
| 1028 | 1028 | * @param integer $ttl the cache time to live in second |
| 1029 | 1029 | * @return object the current Database instance |
| 1030 | 1030 | */ |
| 1031 | - public function setCache($ttl = 0){ |
|
| 1032 | - if($ttl > 0){ |
|
| 1031 | + public function setCache($ttl = 0) { |
|
| 1032 | + if ($ttl > 0) { |
|
| 1033 | 1033 | $this->cacheTtl = $ttl; |
| 1034 | 1034 | $this->temporaryCacheTtl = $ttl; |
| 1035 | 1035 | } |
@@ -1041,8 +1041,8 @@ discard block |
||
| 1041 | 1041 | * @param integer $ttl the cache time to live in second |
| 1042 | 1042 | * @return object the current Database instance |
| 1043 | 1043 | */ |
| 1044 | - public function cached($ttl = 0){ |
|
| 1045 | - if($ttl > 0){ |
|
| 1044 | + public function cached($ttl = 0) { |
|
| 1045 | + if ($ttl > 0) { |
|
| 1046 | 1046 | $this->temporaryCacheTtl = $ttl; |
| 1047 | 1047 | } |
| 1048 | 1048 | return $this; |
@@ -1054,9 +1054,9 @@ discard block |
||
| 1054 | 1054 | * @param boolean $escaped whether we can do escape of not |
| 1055 | 1055 | * @return mixed the data after escaped or the same data if not |
| 1056 | 1056 | */ |
| 1057 | - public function escape($data, $escaped = true){ |
|
| 1058 | - if($escaped){ |
|
| 1059 | - if(! $this->pdo){ |
|
| 1057 | + public function escape($data, $escaped = true) { |
|
| 1058 | + if ($escaped) { |
|
| 1059 | + if (!$this->pdo) { |
|
| 1060 | 1060 | $this->connect(); |
| 1061 | 1061 | } |
| 1062 | 1062 | return $this->pdo->quote(trim($data)); |
@@ -1068,7 +1068,7 @@ discard block |
||
| 1068 | 1068 | * Return the number query executed count for the current request |
| 1069 | 1069 | * @return int |
| 1070 | 1070 | */ |
| 1071 | - public function queryCount(){ |
|
| 1071 | + public function queryCount() { |
|
| 1072 | 1072 | return $this->queryCount; |
| 1073 | 1073 | } |
| 1074 | 1074 | |
@@ -1076,7 +1076,7 @@ discard block |
||
| 1076 | 1076 | * Return the current query SQL string |
| 1077 | 1077 | * @return string |
| 1078 | 1078 | */ |
| 1079 | - public function getQuery(){ |
|
| 1079 | + public function getQuery() { |
|
| 1080 | 1080 | return $this->query; |
| 1081 | 1081 | } |
| 1082 | 1082 | |
@@ -1084,7 +1084,7 @@ discard block |
||
| 1084 | 1084 | * Return the application database name |
| 1085 | 1085 | * @return string |
| 1086 | 1086 | */ |
| 1087 | - public function getDatabaseName(){ |
|
| 1087 | + public function getDatabaseName() { |
|
| 1088 | 1088 | return $this->databaseName; |
| 1089 | 1089 | } |
| 1090 | 1090 | |
@@ -1092,7 +1092,7 @@ discard block |
||
| 1092 | 1092 | * Return the database configuration |
| 1093 | 1093 | * @return array |
| 1094 | 1094 | */ |
| 1095 | - public function getDatabaseConfiguration(){ |
|
| 1095 | + public function getDatabaseConfiguration() { |
|
| 1096 | 1096 | return $this->config; |
| 1097 | 1097 | } |
| 1098 | 1098 | |
@@ -1100,7 +1100,7 @@ discard block |
||
| 1100 | 1100 | * set the database configuration |
| 1101 | 1101 | * @param array $config the configuration |
| 1102 | 1102 | */ |
| 1103 | - public function setDatabaseConfiguration(array $config){ |
|
| 1103 | + public function setDatabaseConfiguration(array $config) { |
|
| 1104 | 1104 | $this->config = array_merge($this->config, $config); |
| 1105 | 1105 | $this->prefix = $this->config['prefix']; |
| 1106 | 1106 | $this->databaseName = $this->config['database']; |
@@ -1112,7 +1112,7 @@ discard block |
||
| 1112 | 1112 | * Return the PDO instance |
| 1113 | 1113 | * @return PDO |
| 1114 | 1114 | */ |
| 1115 | - public function getPdo(){ |
|
| 1115 | + public function getPdo() { |
|
| 1116 | 1116 | return $this->pdo; |
| 1117 | 1117 | } |
| 1118 | 1118 | |
@@ -1120,7 +1120,7 @@ discard block |
||
| 1120 | 1120 | * Set the PDO instance |
| 1121 | 1121 | * @param PDO $pdo the pdo object |
| 1122 | 1122 | */ |
| 1123 | - public function setPdo(PDO $pdo){ |
|
| 1123 | + public function setPdo(PDO $pdo) { |
|
| 1124 | 1124 | $this->pdo = $pdo; |
| 1125 | 1125 | return $this; |
| 1126 | 1126 | } |
@@ -1130,7 +1130,7 @@ discard block |
||
| 1130 | 1130 | * Return the Log instance |
| 1131 | 1131 | * @return Log |
| 1132 | 1132 | */ |
| 1133 | - public function getLogger(){ |
|
| 1133 | + public function getLogger() { |
|
| 1134 | 1134 | return $this->logger; |
| 1135 | 1135 | } |
| 1136 | 1136 | |
@@ -1138,7 +1138,7 @@ discard block |
||
| 1138 | 1138 | * Set the log instance |
| 1139 | 1139 | * @param Log $logger the log object |
| 1140 | 1140 | */ |
| 1141 | - public function setLogger($logger){ |
|
| 1141 | + public function setLogger($logger) { |
|
| 1142 | 1142 | $this->logger = $logger; |
| 1143 | 1143 | return $this; |
| 1144 | 1144 | } |
@@ -1147,7 +1147,7 @@ discard block |
||
| 1147 | 1147 | * Return the cache instance |
| 1148 | 1148 | * @return CacheInterface |
| 1149 | 1149 | */ |
| 1150 | - public function getCacheInstance(){ |
|
| 1150 | + public function getCacheInstance() { |
|
| 1151 | 1151 | return $this->cacheInstance; |
| 1152 | 1152 | } |
| 1153 | 1153 | |
@@ -1155,7 +1155,7 @@ discard block |
||
| 1155 | 1155 | * Set the cache instance |
| 1156 | 1156 | * @param CacheInterface $cache the cache object |
| 1157 | 1157 | */ |
| 1158 | - public function setCacheInstance($cache){ |
|
| 1158 | + public function setCacheInstance($cache) { |
|
| 1159 | 1159 | $this->cacheInstance = $cache; |
| 1160 | 1160 | return $this; |
| 1161 | 1161 | } |
@@ -1164,7 +1164,7 @@ discard block |
||
| 1164 | 1164 | * Return the benchmark instance |
| 1165 | 1165 | * @return Benchmark |
| 1166 | 1166 | */ |
| 1167 | - public function getBenchmark(){ |
|
| 1167 | + public function getBenchmark() { |
|
| 1168 | 1168 | return $this->benchmarkInstance; |
| 1169 | 1169 | } |
| 1170 | 1170 | |
@@ -1172,7 +1172,7 @@ discard block |
||
| 1172 | 1172 | * Set the benchmark instance |
| 1173 | 1173 | * @param Benchmark $cache the cache object |
| 1174 | 1174 | */ |
| 1175 | - public function setBenchmark($benchmark){ |
|
| 1175 | + public function setBenchmark($benchmark) { |
|
| 1176 | 1176 | $this->benchmarkInstance = $benchmark; |
| 1177 | 1177 | return $this; |
| 1178 | 1178 | } |
@@ -1181,7 +1181,7 @@ discard block |
||
| 1181 | 1181 | * Return the data to be used for insert, update, etc. |
| 1182 | 1182 | * @return array |
| 1183 | 1183 | */ |
| 1184 | - public function getData(){ |
|
| 1184 | + public function getData() { |
|
| 1185 | 1185 | return $this->data; |
| 1186 | 1186 | } |
| 1187 | 1187 | |
@@ -1192,7 +1192,7 @@ discard block |
||
| 1192 | 1192 | * @param boolean $escape whether to escape or not the $value |
| 1193 | 1193 | * @return object the current Database instance |
| 1194 | 1194 | */ |
| 1195 | - public function setData($key, $value, $escape = true){ |
|
| 1195 | + public function setData($key, $value, $escape = true) { |
|
| 1196 | 1196 | $this->data[$key] = $this->escape($value, $escape); |
| 1197 | 1197 | return $this; |
| 1198 | 1198 | } |
@@ -1201,12 +1201,12 @@ discard block |
||
| 1201 | 1201 | * Set the Log instance using argument or create new instance |
| 1202 | 1202 | * @param object $logger the Log instance if not null |
| 1203 | 1203 | */ |
| 1204 | - protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
| 1205 | - if($logger !== null){ |
|
| 1204 | + protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null) { |
|
| 1205 | + if ($logger !== null) { |
|
| 1206 | 1206 | $this->logger = $logger; |
| 1207 | 1207 | } |
| 1208 | - else{ |
|
| 1209 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 1208 | + else { |
|
| 1209 | + $this->logger = & class_loader('Log', 'classes'); |
|
| 1210 | 1210 | $this->logger->setLogger('Library::Database'); |
| 1211 | 1211 | } |
| 1212 | 1212 | } |
@@ -1215,14 +1215,14 @@ discard block |
||
| 1215 | 1215 | * Setting the database configuration using the configuration file |
| 1216 | 1216 | * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
| 1217 | 1217 | */ |
| 1218 | - protected function setDatabaseConfigurationFromConfigFile(array $overwriteConfig = array()){ |
|
| 1218 | + protected function setDatabaseConfigurationFromConfigFile(array $overwriteConfig = array()) { |
|
| 1219 | 1219 | $db = array(); |
| 1220 | - if(file_exists(CONFIG_PATH . 'database.php')){ |
|
| 1220 | + if (file_exists(CONFIG_PATH . 'database.php')) { |
|
| 1221 | 1221 | //here don't use require_once because somewhere user can create database instance directly |
| 1222 | 1222 | require CONFIG_PATH . 'database.php'; |
| 1223 | 1223 | } |
| 1224 | 1224 | |
| 1225 | - if(! empty($overwriteConfig)){ |
|
| 1225 | + if (!empty($overwriteConfig)) { |
|
| 1226 | 1226 | $db = array_merge($db, $overwriteConfig); |
| 1227 | 1227 | } |
| 1228 | 1228 | $config = array( |
@@ -1244,9 +1244,9 @@ discard block |
||
| 1244 | 1244 | * This method is used to get the PDO DSN string using th configured driver |
| 1245 | 1245 | * @return string the DSN string |
| 1246 | 1246 | */ |
| 1247 | - protected function getDsnFromDriver(){ |
|
| 1247 | + protected function getDsnFromDriver() { |
|
| 1248 | 1248 | $config = $this->getDatabaseConfiguration(); |
| 1249 | - if(! empty($config)){ |
|
| 1249 | + if (!empty($config)) { |
|
| 1250 | 1250 | $driverDsnMap = array( |
| 1251 | 1251 | 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
| 1252 | 1252 | . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
@@ -1268,10 +1268,10 @@ discard block |
||
| 1268 | 1268 | * Set the database server port configuration using the current hostname like localhost:3309 |
| 1269 | 1269 | * @return void |
| 1270 | 1270 | */ |
| 1271 | - protected function determinePortConfigurationFromHostname(){ |
|
| 1272 | - if(strstr($this->config['hostname'], ':')){ |
|
| 1271 | + protected function determinePortConfigurationFromHostname() { |
|
| 1272 | + if (strstr($this->config['hostname'], ':')) { |
|
| 1273 | 1273 | $p = explode(':', $this->config['hostname']); |
| 1274 | - if(count($p) > 2){ |
|
| 1274 | + if (count($p) > 2) { |
|
| 1275 | 1275 | $this->setDatabaseConfiguration(array( |
| 1276 | 1276 | 'hostname' => $p[0], |
| 1277 | 1277 | 'port' => $p[1] |
@@ -1286,15 +1286,15 @@ discard block |
||
| 1286 | 1286 | * |
| 1287 | 1287 | * @return string |
| 1288 | 1288 | */ |
| 1289 | - protected function getWhereStrIfIsArray(array $where, $type = '', $andOr = 'AND', $escape = true){ |
|
| 1289 | + protected function getWhereStrIfIsArray(array $where, $type = '', $andOr = 'AND', $escape = true) { |
|
| 1290 | 1290 | $_where = array(); |
| 1291 | - foreach ($where as $column => $data){ |
|
| 1292 | - if(is_null($data)){ |
|
| 1291 | + foreach ($where as $column => $data) { |
|
| 1292 | + if (is_null($data)) { |
|
| 1293 | 1293 | $data = ''; |
| 1294 | 1294 | } |
| 1295 | 1295 | $_where[] = $type . $column . ' = ' . ($this->escape($data, $escape)); |
| 1296 | 1296 | } |
| 1297 | - $where = implode(' '.$andOr.' ', $_where); |
|
| 1297 | + $where = implode(' ' . $andOr . ' ', $_where); |
|
| 1298 | 1298 | return $where; |
| 1299 | 1299 | } |
| 1300 | 1300 | |
@@ -1304,12 +1304,12 @@ discard block |
||
| 1304 | 1304 | * |
| 1305 | 1305 | * @return string |
| 1306 | 1306 | */ |
| 1307 | - protected function getWhereStrIfOperatorIsArray($where, array $op, $type = '', $escape = true){ |
|
| 1307 | + protected function getWhereStrIfOperatorIsArray($where, array $op, $type = '', $escape = true) { |
|
| 1308 | 1308 | $x = explode('?', $where); |
| 1309 | 1309 | $w = ''; |
| 1310 | - foreach($x as $k => $v){ |
|
| 1311 | - if(! empty($v)){ |
|
| 1312 | - if(isset($op[$k]) && is_null($op[$k])){ |
|
| 1310 | + foreach ($x as $k => $v) { |
|
| 1311 | + if (!empty($v)) { |
|
| 1312 | + if (isset($op[$k]) && is_null($op[$k])) { |
|
| 1313 | 1313 | $op[$k] = ''; |
| 1314 | 1314 | } |
| 1315 | 1315 | $w .= $type . $v . (isset($op[$k]) ? ($this->escape($op[$k], $escape)) : ''); |
@@ -1324,16 +1324,16 @@ discard block |
||
| 1324 | 1324 | * |
| 1325 | 1325 | * @return string |
| 1326 | 1326 | */ |
| 1327 | - protected function getWhereStrForOperator($where, $op = null, $val = null, $type = '', $escape = true){ |
|
| 1327 | + protected function getWhereStrForOperator($where, $op = null, $val = null, $type = '', $escape = true) { |
|
| 1328 | 1328 | $w = ''; |
| 1329 | - if (! in_array((string)$op, $this->operatorList)){ |
|
| 1330 | - if(is_null($op)){ |
|
| 1329 | + if (!in_array((string) $op, $this->operatorList)) { |
|
| 1330 | + if (is_null($op)) { |
|
| 1331 | 1331 | $op = ''; |
| 1332 | 1332 | } |
| 1333 | 1333 | $w = $type . $where . ' = ' . ($this->escape($op, $escape)); |
| 1334 | 1334 | } |
| 1335 | - else{ |
|
| 1336 | - if(is_null($val)){ |
|
| 1335 | + else { |
|
| 1336 | + if (is_null($val)) { |
|
| 1337 | 1337 | $val = ''; |
| 1338 | 1338 | } |
| 1339 | 1339 | $w = $type . $where . $op . ($this->escape($val, $escape)); |
@@ -1346,16 +1346,16 @@ discard block |
||
| 1346 | 1346 | * @param string $whereStr the WHERE clause string |
| 1347 | 1347 | * @param string $andOr the separator type used 'AND', 'OR', etc. |
| 1348 | 1348 | */ |
| 1349 | - protected function setWhereStr($whereStr, $andOr = 'AND'){ |
|
| 1350 | - if (empty($this->where)){ |
|
| 1349 | + protected function setWhereStr($whereStr, $andOr = 'AND') { |
|
| 1350 | + if (empty($this->where)) { |
|
| 1351 | 1351 | $this->where = $whereStr; |
| 1352 | 1352 | } |
| 1353 | - else{ |
|
| 1354 | - if(substr($this->where, -1) == '('){ |
|
| 1353 | + else { |
|
| 1354 | + if (substr($this->where, -1) == '(') { |
|
| 1355 | 1355 | $this->where = $this->where . ' ' . $whereStr; |
| 1356 | 1356 | } |
| 1357 | - else{ |
|
| 1358 | - $this->where = $this->where . ' '.$andOr.' ' . $whereStr; |
|
| 1357 | + else { |
|
| 1358 | + $this->where = $this->where . ' ' . $andOr . ' ' . $whereStr; |
|
| 1359 | 1359 | } |
| 1360 | 1360 | } |
| 1361 | 1361 | } |
@@ -1366,12 +1366,12 @@ discard block |
||
| 1366 | 1366 | * |
| 1367 | 1367 | * @return string |
| 1368 | 1368 | */ |
| 1369 | - protected function transformPreparedQuery($query, $data){ |
|
| 1370 | - if(is_array($data)){ |
|
| 1369 | + protected function transformPreparedQuery($query, $data) { |
|
| 1370 | + if (is_array($data)) { |
|
| 1371 | 1371 | $x = explode('?', $query); |
| 1372 | 1372 | $q = ''; |
| 1373 | - foreach($x as $k => $v){ |
|
| 1374 | - if(! empty($v)){ |
|
| 1373 | + foreach ($x as $k => $v) { |
|
| 1374 | + if (!empty($v)) { |
|
| 1375 | 1375 | $q .= $v . (isset($data[$k]) ? $this->escape($data[$k]) : ''); |
| 1376 | 1376 | } |
| 1377 | 1377 | } |
@@ -1386,7 +1386,7 @@ discard block |
||
| 1386 | 1386 | * |
| 1387 | 1387 | * @return string |
| 1388 | 1388 | */ |
| 1389 | - protected function getCacheBenchmarkKeyForQuery($query, $all, $array){ |
|
| 1389 | + protected function getCacheBenchmarkKeyForQuery($query, $all, $array) { |
|
| 1390 | 1390 | return md5($query . $all . $array); |
| 1391 | 1391 | } |
| 1392 | 1392 | |
@@ -1396,10 +1396,10 @@ discard block |
||
| 1396 | 1396 | * |
| 1397 | 1397 | * @return mixed |
| 1398 | 1398 | */ |
| 1399 | - protected function getCacheContentForQuery($query, $all, $array){ |
|
| 1399 | + protected function getCacheContentForQuery($query, $all, $array) { |
|
| 1400 | 1400 | $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
| 1401 | 1401 | $cacheKey = $this->getCacheBenchmarkKeyForQuery($query, $all, $array); |
| 1402 | - if(is_object($this->cacheInstance)){ |
|
| 1402 | + if (is_object($this->cacheInstance)) { |
|
| 1403 | 1403 | return $this->cacheInstance->get($cacheKey); |
| 1404 | 1404 | } |
| 1405 | 1405 | $instance = & get_instance()->cache; |
@@ -1415,9 +1415,9 @@ discard block |
||
| 1415 | 1415 | * @param boolean $status whether can save the query result into cache |
| 1416 | 1416 | * @param int $expire the cache TTL |
| 1417 | 1417 | */ |
| 1418 | - protected function setCacheContentForQuery($query, $key, $result, $status, $expire){ |
|
| 1419 | - if ($status){ |
|
| 1420 | - $this->logger->info('Save the result for query [' .$query. '] into cache for future use'); |
|
| 1418 | + protected function setCacheContentForQuery($query, $key, $result, $status, $expire) { |
|
| 1419 | + if ($status) { |
|
| 1420 | + $this->logger->info('Save the result for query [' . $query . '] into cache for future use'); |
|
| 1421 | 1421 | $this->getCacheInstance()->set($key, $result, $expire); |
| 1422 | 1422 | } |
| 1423 | 1423 | } |
@@ -1426,19 +1426,19 @@ discard block |
||
| 1426 | 1426 | * Set the result for SELECT query using PDOStatment |
| 1427 | 1427 | * @see Database::query |
| 1428 | 1428 | */ |
| 1429 | - protected function setQueryResultForSelect($pdoStatment, $all = true, $array = false){ |
|
| 1429 | + protected function setQueryResultForSelect($pdoStatment, $all = true, $array = false) { |
|
| 1430 | 1430 | //if need return all result like list of record |
| 1431 | - if ($all){ |
|
| 1431 | + if ($all) { |
|
| 1432 | 1432 | $this->result = ($array === false) ? $pdoStatment->fetchAll(PDO::FETCH_OBJ) : $pdoStatment->fetchAll(PDO::FETCH_ASSOC); |
| 1433 | 1433 | } |
| 1434 | - else{ |
|
| 1434 | + else { |
|
| 1435 | 1435 | $this->result = ($array === false) ? $pdoStatment->fetch(PDO::FETCH_OBJ) : $pdoStatment->fetch(PDO::FETCH_ASSOC); |
| 1436 | 1436 | } |
| 1437 | 1437 | //Sqlite and pgsql always return 0 when using rowCount() |
| 1438 | - if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 1438 | + if (in_array($this->config['driver'], array('sqlite', 'pgsql'))) { |
|
| 1439 | 1439 | $this->numRows = count($this->result); |
| 1440 | 1440 | } |
| 1441 | - else{ |
|
| 1441 | + else { |
|
| 1442 | 1442 | $this->numRows = $pdoStatment->rowCount(); |
| 1443 | 1443 | } |
| 1444 | 1444 | } |
@@ -1447,13 +1447,13 @@ discard block |
||
| 1447 | 1447 | * Set the result for other command than SELECT query using PDOStatment |
| 1448 | 1448 | * @see Database::query |
| 1449 | 1449 | */ |
| 1450 | - protected function setQueryResultForNonSelect($pdoStatment, $all = true, $array = false){ |
|
| 1450 | + protected function setQueryResultForNonSelect($pdoStatment, $all = true, $array = false) { |
|
| 1451 | 1451 | //Sqlite and pgsql always return 0 when using rowCount() |
| 1452 | - if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 1452 | + if (in_array($this->config['driver'], array('sqlite', 'pgsql'))) { |
|
| 1453 | 1453 | $this->result = 1; //to test the result for the query like UPDATE, INSERT, DELETE |
| 1454 | 1454 | $this->numRows = 1; |
| 1455 | 1455 | } |
| 1456 | - else{ |
|
| 1456 | + else { |
|
| 1457 | 1457 | $this->result = $pdoStatment->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
| 1458 | 1458 | $this->numRows = $pdoStatment->rowCount(); |
| 1459 | 1459 | } |
@@ -1462,7 +1462,7 @@ discard block |
||
| 1462 | 1462 | /** |
| 1463 | 1463 | * Set error for database query execution |
| 1464 | 1464 | */ |
| 1465 | - protected function setQueryError(){ |
|
| 1465 | + protected function setQueryError() { |
|
| 1466 | 1466 | $error = $this->pdo->errorInfo(); |
| 1467 | 1467 | $this->error = isset($error[2]) ? $error[2] : ''; |
| 1468 | 1468 | $this->logger->error('The database query execution got error: ' . stringfy_vars($error)); |
@@ -1475,16 +1475,16 @@ discard block |
||
| 1475 | 1475 | * |
| 1476 | 1476 | * @return object|void |
| 1477 | 1477 | */ |
| 1478 | - protected function runSqlQuery($query, $all, $array){ |
|
| 1478 | + protected function runSqlQuery($query, $all, $array) { |
|
| 1479 | 1479 | //for database query execution time |
| 1480 | 1480 | $benchmarkMarkerKey = $this->getCacheBenchmarkKeyForQuery($query, $all, $array); |
| 1481 | 1481 | $benchmarkInstance = $this->getBenchmark(); |
| 1482 | - if(! is_object($benchmarkInstance)){ |
|
| 1482 | + if (!is_object($benchmarkInstance)) { |
|
| 1483 | 1483 | $obj = & get_instance(); |
| 1484 | 1484 | $benchmarkInstance = $obj->benchmark; |
| 1485 | 1485 | $this->setBenchmark($benchmarkInstance); |
| 1486 | 1486 | } |
| 1487 | - if(! $this->pdo){ |
|
| 1487 | + if (!$this->pdo) { |
|
| 1488 | 1488 | $this->connect(); |
| 1489 | 1489 | } |
| 1490 | 1490 | |
@@ -1495,10 +1495,10 @@ discard block |
||
| 1495 | 1495 | //get response time for this query |
| 1496 | 1496 | $responseTime = $benchmarkInstance->elapsedTime('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')'); |
| 1497 | 1497 | //TODO use the configuration value for the high response time currently is 1 second |
| 1498 | - if($responseTime >= 1 ){ |
|
| 1499 | - $this->logger->warning('High response time while processing database query [' .$query. ']. The response time is [' .$responseTime. '] sec.'); |
|
| 1498 | + if ($responseTime >= 1) { |
|
| 1499 | + $this->logger->warning('High response time while processing database query [' . $query . ']. The response time is [' . $responseTime . '] sec.'); |
|
| 1500 | 1500 | } |
| 1501 | - if($sqlQuery){ |
|
| 1501 | + if ($sqlQuery) { |
|
| 1502 | 1502 | return $sqlQuery; |
| 1503 | 1503 | } |
| 1504 | 1504 | $this->setQueryError(); |
@@ -1507,7 +1507,7 @@ discard block |
||
| 1507 | 1507 | /** |
| 1508 | 1508 | * Reset the database class attributs to the initail values before each query. |
| 1509 | 1509 | */ |
| 1510 | - private function reset(){ |
|
| 1510 | + private function reset() { |
|
| 1511 | 1511 | $this->select = '*'; |
| 1512 | 1512 | $this->from = null; |
| 1513 | 1513 | $this->where = null; |
@@ -1527,7 +1527,7 @@ discard block |
||
| 1527 | 1527 | /** |
| 1528 | 1528 | * The class destructor |
| 1529 | 1529 | */ |
| 1530 | - public function __destruct(){ |
|
| 1530 | + public function __destruct() { |
|
| 1531 | 1531 | $this->pdo = null; |
| 1532 | 1532 | } |
| 1533 | 1533 | |
@@ -204,14 +204,12 @@ discard block |
||
| 204 | 204 | $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
| 205 | 205 | $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
| 206 | 206 | return true; |
| 207 | - } |
|
| 208 | - catch (PDOException $e){ |
|
| 207 | + } catch (PDOException $e){ |
|
| 209 | 208 | $this->logger->fatal($e->getMessage()); |
| 210 | 209 | show_error('Cannot connect to Database.'); |
| 211 | 210 | return false; |
| 212 | 211 | } |
| 213 | - } |
|
| 214 | - else{ |
|
| 212 | + } else{ |
|
| 215 | 213 | show_error('Database configuration is not set.'); |
| 216 | 214 | return false; |
| 217 | 215 | } |
@@ -229,8 +227,7 @@ discard block |
||
| 229 | 227 | $froms .= $this->prefix . $key . ', '; |
| 230 | 228 | } |
| 231 | 229 | $this->from = rtrim($froms, ', '); |
| 232 | - } |
|
| 233 | - else{ |
|
| 230 | + } else{ |
|
| 234 | 231 | $this->from = $this->prefix . $table; |
| 235 | 232 | } |
| 236 | 233 | return $this; |
@@ -336,8 +333,7 @@ discard block |
||
| 336 | 333 | } |
| 337 | 334 | if (empty($this->join)){ |
| 338 | 335 | $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 339 | - } |
|
| 340 | - else{ |
|
| 336 | + } else{ |
|
| 341 | 337 | $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 342 | 338 | } |
| 343 | 339 | return $this; |
@@ -408,8 +404,7 @@ discard block |
||
| 408 | 404 | foreach($field as $f){ |
| 409 | 405 | $this->whereIsNull($f, $andOr); |
| 410 | 406 | } |
| 411 | - } |
|
| 412 | - else{ |
|
| 407 | + } else{ |
|
| 413 | 408 | $this->setWhereStr($field.' IS NULL ', $andOr); |
| 414 | 409 | } |
| 415 | 410 | return $this; |
@@ -426,8 +421,7 @@ discard block |
||
| 426 | 421 | foreach($field as $f){ |
| 427 | 422 | $this->whereIsNotNull($f, $andOr); |
| 428 | 423 | } |
| 429 | - } |
|
| 430 | - else{ |
|
| 424 | + } else{ |
|
| 431 | 425 | $this->setWhereStr($field.' IS NOT NULL ', $andOr); |
| 432 | 426 | } |
| 433 | 427 | return $this; |
@@ -447,8 +441,7 @@ discard block |
||
| 447 | 441 | $whereStr = ''; |
| 448 | 442 | if (is_array($where)){ |
| 449 | 443 | $whereStr = $this->getWhereStrIfIsArray($where, $type, $andOr, $escape); |
| 450 | - } |
|
| 451 | - else{ |
|
| 444 | + } else{ |
|
| 452 | 445 | if(is_array($op)){ |
| 453 | 446 | $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
| 454 | 447 | } else { |
@@ -496,12 +489,10 @@ discard block |
||
| 496 | 489 | public function groupStart($type = '', $andOr = ' AND'){ |
| 497 | 490 | if (empty($this->where)){ |
| 498 | 491 | $this->where = $type . ' ('; |
| 499 | - } |
|
| 500 | - else{ |
|
| 492 | + } else{ |
|
| 501 | 493 | if(substr($this->where, -1) == '('){ |
| 502 | 494 | $this->where .= $type . ' ('; |
| 503 | - } |
|
| 504 | - else{ |
|
| 495 | + } else{ |
|
| 505 | 496 | $this->where .= $andOr . ' ' . $type . ' ('; |
| 506 | 497 | } |
| 507 | 498 | } |
@@ -700,8 +691,7 @@ discard block |
||
| 700 | 691 | } |
| 701 | 692 | if (! is_null($limitEnd)){ |
| 702 | 693 | $this->limit = $limit . ', ' . $limitEnd; |
| 703 | - } |
|
| 704 | - else{ |
|
| 694 | + } else{ |
|
| 705 | 695 | $this->limit = $limit; |
| 706 | 696 | } |
| 707 | 697 | return $this; |
@@ -716,8 +706,7 @@ discard block |
||
| 716 | 706 | public function orderBy($orderBy, $orderDir = ' ASC'){ |
| 717 | 707 | if(stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
| 718 | 708 | $this->orderBy = empty($this->orderBy) ? $orderBy : $this->orderBy . ', ' . $orderBy; |
| 719 | - } |
|
| 720 | - else{ |
|
| 709 | + } else{ |
|
| 721 | 710 | $this->orderBy = empty($this->orderBy) ? ($orderBy . ' ' |
| 722 | 711 | . strtoupper($orderDir)) : $this->orderBy |
| 723 | 712 | . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
@@ -733,8 +722,7 @@ discard block |
||
| 733 | 722 | public function groupBy($field){ |
| 734 | 723 | if(is_array($field)){ |
| 735 | 724 | $this->groupBy = implode(', ', $field); |
| 736 | - } |
|
| 737 | - else{ |
|
| 725 | + } else{ |
|
| 738 | 726 | $this->groupBy = $field; |
| 739 | 727 | } |
| 740 | 728 | return $this; |
@@ -761,14 +749,12 @@ discard block |
||
| 761 | 749 | } |
| 762 | 750 | } |
| 763 | 751 | $this->having = $w; |
| 764 | - } |
|
| 765 | - else if (! in_array($op, $this->operatorList)){ |
|
| 752 | + } else if (! in_array($op, $this->operatorList)){ |
|
| 766 | 753 | if(is_null($op)){ |
| 767 | 754 | $op = ''; |
| 768 | 755 | } |
| 769 | 756 | $this->having = $field . ' > ' . ($this->escape($op, $escape)); |
| 770 | - } |
|
| 771 | - else{ |
|
| 757 | + } else{ |
|
| 772 | 758 | if(is_null($val)){ |
| 773 | 759 | $val = ''; |
| 774 | 760 | } |
@@ -813,8 +799,7 @@ discard block |
||
| 813 | 799 | $query = $this->getAll(true); |
| 814 | 800 | if($returnSQLQueryOrResultType === true){ |
| 815 | 801 | return $query; |
| 816 | - } |
|
| 817 | - else{ |
|
| 802 | + } else{ |
|
| 818 | 803 | return $this->query( $query, false, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
| 819 | 804 | } |
| 820 | 805 | } |
@@ -853,8 +838,7 @@ discard block |
||
| 853 | 838 | |
| 854 | 839 | if($returnSQLQueryOrResultType === true){ |
| 855 | 840 | return $query; |
| 856 | - } |
|
| 857 | - else{ |
|
| 841 | + } else{ |
|
| 858 | 842 | return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
| 859 | 843 | } |
| 860 | 844 | } |
@@ -887,8 +871,7 @@ discard block |
||
| 887 | 871 | } |
| 888 | 872 | $this->insertId = $this->pdo->lastInsertId(); |
| 889 | 873 | return $this->insertId(); |
| 890 | - } |
|
| 891 | - else{ |
|
| 874 | + } else{ |
|
| 892 | 875 | return false; |
| 893 | 876 | } |
| 894 | 877 | } |
@@ -984,8 +967,7 @@ discard block |
||
| 984 | 967 | |
| 985 | 968 | if ($dbCacheStatus && $isSqlSELECTQuery){ |
| 986 | 969 | $cacheContent = $this->getCacheContentForQuery($query, $all, $array); |
| 987 | - } |
|
| 988 | - else{ |
|
| 970 | + } else{ |
|
| 989 | 971 | $this->logger->info('The cache is not enabled for this query or is not the SELECT query, get the result directly from real database'); |
| 990 | 972 | } |
| 991 | 973 | |
@@ -1001,8 +983,7 @@ discard block |
||
| 1001 | 983 | $this->temporaryCacheTtl |
| 1002 | 984 | ); |
| 1003 | 985 | } |
| 1004 | - } |
|
| 1005 | - else if ((! $cacheContent && !$isSqlSELECTQuery) || ($cacheContent && !$isSqlSELECTQuery)){ |
|
| 986 | + } else if ((! $cacheContent && !$isSqlSELECTQuery) || ($cacheContent && !$isSqlSELECTQuery)){ |
|
| 1006 | 987 | $sqlQuery = $this->runSqlQuery($query, $all, $array); |
| 1007 | 988 | if($sqlQuery){ |
| 1008 | 989 | $this->setQueryResultForNonSelect($sqlQuery, $all, $array); |
@@ -1010,8 +991,7 @@ discard block |
||
| 1010 | 991 | if (! $this->result){ |
| 1011 | 992 | $this->setQueryError(); |
| 1012 | 993 | } |
| 1013 | - } |
|
| 1014 | - else{ |
|
| 994 | + } else{ |
|
| 1015 | 995 | $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
| 1016 | 996 | $this->result = $cacheContent; |
| 1017 | 997 | $this->numRows = count($this->result); |
@@ -1204,8 +1184,7 @@ discard block |
||
| 1204 | 1184 | protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
| 1205 | 1185 | if($logger !== null){ |
| 1206 | 1186 | $this->logger = $logger; |
| 1207 | - } |
|
| 1208 | - else{ |
|
| 1187 | + } else{ |
|
| 1209 | 1188 | $this->logger =& class_loader('Log', 'classes'); |
| 1210 | 1189 | $this->logger->setLogger('Library::Database'); |
| 1211 | 1190 | } |
@@ -1331,8 +1310,7 @@ discard block |
||
| 1331 | 1310 | $op = ''; |
| 1332 | 1311 | } |
| 1333 | 1312 | $w = $type . $where . ' = ' . ($this->escape($op, $escape)); |
| 1334 | - } |
|
| 1335 | - else{ |
|
| 1313 | + } else{ |
|
| 1336 | 1314 | if(is_null($val)){ |
| 1337 | 1315 | $val = ''; |
| 1338 | 1316 | } |
@@ -1349,12 +1327,10 @@ discard block |
||
| 1349 | 1327 | protected function setWhereStr($whereStr, $andOr = 'AND'){ |
| 1350 | 1328 | if (empty($this->where)){ |
| 1351 | 1329 | $this->where = $whereStr; |
| 1352 | - } |
|
| 1353 | - else{ |
|
| 1330 | + } else{ |
|
| 1354 | 1331 | if(substr($this->where, -1) == '('){ |
| 1355 | 1332 | $this->where = $this->where . ' ' . $whereStr; |
| 1356 | - } |
|
| 1357 | - else{ |
|
| 1333 | + } else{ |
|
| 1358 | 1334 | $this->where = $this->where . ' '.$andOr.' ' . $whereStr; |
| 1359 | 1335 | } |
| 1360 | 1336 | } |
@@ -1430,15 +1406,13 @@ discard block |
||
| 1430 | 1406 | //if need return all result like list of record |
| 1431 | 1407 | if ($all){ |
| 1432 | 1408 | $this->result = ($array === false) ? $pdoStatment->fetchAll(PDO::FETCH_OBJ) : $pdoStatment->fetchAll(PDO::FETCH_ASSOC); |
| 1433 | - } |
|
| 1434 | - else{ |
|
| 1409 | + } else{ |
|
| 1435 | 1410 | $this->result = ($array === false) ? $pdoStatment->fetch(PDO::FETCH_OBJ) : $pdoStatment->fetch(PDO::FETCH_ASSOC); |
| 1436 | 1411 | } |
| 1437 | 1412 | //Sqlite and pgsql always return 0 when using rowCount() |
| 1438 | 1413 | if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
| 1439 | 1414 | $this->numRows = count($this->result); |
| 1440 | - } |
|
| 1441 | - else{ |
|
| 1415 | + } else{ |
|
| 1442 | 1416 | $this->numRows = $pdoStatment->rowCount(); |
| 1443 | 1417 | } |
| 1444 | 1418 | } |
@@ -1452,8 +1426,7 @@ discard block |
||
| 1452 | 1426 | if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
| 1453 | 1427 | $this->result = 1; //to test the result for the query like UPDATE, INSERT, DELETE |
| 1454 | 1428 | $this->numRows = 1; |
| 1455 | - } |
|
| 1456 | - else{ |
|
| 1429 | + } else{ |
|
| 1457 | 1430 | $this->result = $pdoStatment->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
| 1458 | 1431 | $this->numRows = $pdoStatment->rowCount(); |
| 1459 | 1432 | } |
@@ -23,7 +23,7 @@ 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 Loader{ |
|
| 26 | + class Loader { |
|
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * List of loaded resources |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | private static $logger; |
| 39 | 39 | |
| 40 | 40 | |
| 41 | - public function __construct(){ |
|
| 41 | + public function __construct() { |
|
| 42 | 42 | //add the resources already loaded during application bootstrap |
| 43 | 43 | //in the list to prevent duplicate or loading the resources again. |
| 44 | 44 | static::$loaded = class_loaded(); |
@@ -51,9 +51,9 @@ discard block |
||
| 51 | 51 | * Get the logger singleton instance |
| 52 | 52 | * @return Log the logger instance |
| 53 | 53 | */ |
| 54 | - private static function getLogger(){ |
|
| 55 | - if(self::$logger == null){ |
|
| 56 | - self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 54 | + private static function getLogger() { |
|
| 55 | + if (self::$logger == null) { |
|
| 56 | + self::$logger[0] = & class_loader('Log', 'classes'); |
|
| 57 | 57 | self::$logger[0]->setLogger('Library::Loader'); |
| 58 | 58 | } |
| 59 | 59 | return self::$logger[0]; |
@@ -67,25 +67,25 @@ discard block |
||
| 67 | 67 | * |
| 68 | 68 | * @return void |
| 69 | 69 | */ |
| 70 | - public static function model($class, $instance = null){ |
|
| 70 | + public static function model($class, $instance = null) { |
|
| 71 | 71 | $logger = static::getLogger(); |
| 72 | 72 | $class = str_ireplace('.php', '', $class); |
| 73 | 73 | $class = trim($class, '/\\'); |
| 74 | - $file = ucfirst($class).'.php'; |
|
| 74 | + $file = ucfirst($class) . '.php'; |
|
| 75 | 75 | $logger->debug('Loading model [' . $class . '] ...'); |
| 76 | - if(! $instance){ |
|
| 76 | + if (!$instance) { |
|
| 77 | 77 | //for module |
| 78 | - if(strpos($class, '/') !== false){ |
|
| 78 | + if (strpos($class, '/') !== false) { |
|
| 79 | 79 | $path = explode('/', $class); |
| 80 | - if(isset($path[1])){ |
|
| 80 | + if (isset($path[1])) { |
|
| 81 | 81 | $instance = strtolower($path[1]); |
| 82 | 82 | } |
| 83 | 83 | } |
| 84 | - else{ |
|
| 84 | + else { |
|
| 85 | 85 | $instance = strtolower($class); |
| 86 | 86 | } |
| 87 | 87 | } |
| 88 | - if(isset(static::$loaded[$instance])){ |
|
| 88 | + if (isset(static::$loaded[$instance])) { |
|
| 89 | 89 | $logger->info('Model [' . $class . '] already loaded no need to load it again, cost in performance'); |
| 90 | 90 | return; |
| 91 | 91 | } |
@@ -95,43 +95,43 @@ discard block |
||
| 95 | 95 | $searchModuleName = null; |
| 96 | 96 | $obj = & get_instance(); |
| 97 | 97 | //check if the request class contains module name |
| 98 | - if(strpos($class, '/') !== false){ |
|
| 98 | + if (strpos($class, '/') !== false) { |
|
| 99 | 99 | $path = explode('/', $class); |
| 100 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 100 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
| 101 | 101 | $searchModuleName = $path[0]; |
| 102 | 102 | $class = ucfirst($path[1]); |
| 103 | 103 | } |
| 104 | 104 | } |
| 105 | - else{ |
|
| 105 | + else { |
|
| 106 | 106 | $class = ucfirst($class); |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 109 | + if (!$searchModuleName && !empty($obj->moduleName)) { |
|
| 110 | 110 | $searchModuleName = $obj->moduleName; |
| 111 | 111 | } |
| 112 | 112 | $moduleModelFilePath = Module::findModelFullPath($class, $searchModuleName); |
| 113 | - if($moduleModelFilePath){ |
|
| 114 | - $logger->info('Found model [' . $class . '] from module [' .$searchModuleName. '], the file path is [' .$moduleModelFilePath. '] we will used it'); |
|
| 113 | + if ($moduleModelFilePath) { |
|
| 114 | + $logger->info('Found model [' . $class . '] from module [' . $searchModuleName . '], the file path is [' . $moduleModelFilePath . '] we will used it'); |
|
| 115 | 115 | $classFilePath = $moduleModelFilePath; |
| 116 | 116 | } |
| 117 | - else{ |
|
| 117 | + else { |
|
| 118 | 118 | $logger->info('Cannot find model [' . $class . '] from modules using the default location'); |
| 119 | 119 | } |
| 120 | 120 | $logger->info('The model file path to be loaded is [' . $classFilePath . ']'); |
| 121 | - if(file_exists($classFilePath)){ |
|
| 121 | + if (file_exists($classFilePath)) { |
|
| 122 | 122 | require_once $classFilePath; |
| 123 | - if(class_exists($class)){ |
|
| 123 | + if (class_exists($class)) { |
|
| 124 | 124 | $c = new $class(); |
| 125 | 125 | $obj = & get_instance(); |
| 126 | 126 | $obj->{$instance} = $c; |
| 127 | 127 | static::$loaded[$instance] = $class; |
| 128 | 128 | $logger->info('Model [' . $class . '] --> ' . $classFilePath . ' loaded successfully.'); |
| 129 | 129 | } |
| 130 | - else{ |
|
| 131 | - show_error('The file '.$classFilePath.' exists but does not contain the class ['. $class . ']'); |
|
| 130 | + else { |
|
| 131 | + show_error('The file ' . $classFilePath . ' exists but does not contain the class [' . $class . ']'); |
|
| 132 | 132 | } |
| 133 | 133 | } |
| 134 | - else{ |
|
| 134 | + else { |
|
| 135 | 135 | show_error('Unable to find the model [' . $class . ']'); |
| 136 | 136 | } |
| 137 | 137 | } |
@@ -146,31 +146,31 @@ discard block |
||
| 146 | 146 | * |
| 147 | 147 | * @return void |
| 148 | 148 | */ |
| 149 | - public static function library($class, $instance = null, array $params = array()){ |
|
| 149 | + public static function library($class, $instance = null, array $params = array()) { |
|
| 150 | 150 | $logger = static::getLogger(); |
| 151 | 151 | $class = str_ireplace('.php', '', $class); |
| 152 | 152 | $class = trim($class, '/\\'); |
| 153 | - $file = ucfirst($class) .'.php'; |
|
| 153 | + $file = ucfirst($class) . '.php'; |
|
| 154 | 154 | $logger->debug('Loading library [' . $class . '] ...'); |
| 155 | - if(! $instance){ |
|
| 155 | + if (!$instance) { |
|
| 156 | 156 | //for module |
| 157 | - if(strpos($class, '/') !== false){ |
|
| 157 | + if (strpos($class, '/') !== false) { |
|
| 158 | 158 | $path = explode('/', $class); |
| 159 | - if(isset($path[1])){ |
|
| 159 | + if (isset($path[1])) { |
|
| 160 | 160 | $instance = strtolower($path[1]); |
| 161 | 161 | } |
| 162 | 162 | } |
| 163 | - else{ |
|
| 163 | + else { |
|
| 164 | 164 | $instance = strtolower($class); |
| 165 | 165 | } |
| 166 | 166 | } |
| 167 | - if(isset(static::$loaded[$instance])){ |
|
| 167 | + if (isset(static::$loaded[$instance])) { |
|
| 168 | 168 | $logger->info('Library [' . $class . '] already loaded no need to load it again, cost in performance'); |
| 169 | 169 | return; |
| 170 | 170 | } |
| 171 | 171 | $obj = & get_instance(); |
| 172 | 172 | //TODO for Database library |
| 173 | - if(strtolower($class) == 'database'){ |
|
| 173 | + if (strtolower($class) == 'database') { |
|
| 174 | 174 | $logger->info('This is the Database library ...'); |
| 175 | 175 | $dbInstance = & class_loader('Database', 'classes', $params); |
| 176 | 176 | $obj->{$instance} = $dbInstance; |
@@ -180,44 +180,44 @@ discard block |
||
| 180 | 180 | } |
| 181 | 181 | $libraryFilePath = null; |
| 182 | 182 | $logger->debug('Check if this is a system library ...'); |
| 183 | - if(file_exists(CORE_LIBRARY_PATH . $file)){ |
|
| 183 | + if (file_exists(CORE_LIBRARY_PATH . $file)) { |
|
| 184 | 184 | $libraryFilePath = CORE_LIBRARY_PATH . $file; |
| 185 | 185 | $class = ucfirst($class); |
| 186 | 186 | $logger->info('This library is a system library'); |
| 187 | 187 | } |
| 188 | - else{ |
|
| 188 | + else { |
|
| 189 | 189 | $logger->info('This library is not a system library'); |
| 190 | 190 | //first check if this library is in the module |
| 191 | 191 | $logger->debug('Checking library [' . $class . '] from module list ...'); |
| 192 | 192 | $searchModuleName = null; |
| 193 | 193 | //check if the request class contains module name |
| 194 | - if(strpos($class, '/') !== false){ |
|
| 194 | + if (strpos($class, '/') !== false) { |
|
| 195 | 195 | $path = explode('/', $class); |
| 196 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 196 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
| 197 | 197 | $searchModuleName = $path[0]; |
| 198 | 198 | $class = ucfirst($path[1]); |
| 199 | 199 | } |
| 200 | 200 | } |
| 201 | - else{ |
|
| 201 | + else { |
|
| 202 | 202 | $class = ucfirst($class); |
| 203 | 203 | } |
| 204 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 204 | + if (!$searchModuleName && !empty($obj->moduleName)) { |
|
| 205 | 205 | $searchModuleName = $obj->moduleName; |
| 206 | 206 | } |
| 207 | 207 | $moduleLibraryPath = Module::findLibraryFullPath($class, $searchModuleName); |
| 208 | - if($moduleLibraryPath){ |
|
| 209 | - $logger->info('Found library [' . $class . '] from module [' .$searchModuleName. '], the file path is [' .$moduleLibraryPath. '] we will used it'); |
|
| 208 | + if ($moduleLibraryPath) { |
|
| 209 | + $logger->info('Found library [' . $class . '] from module [' . $searchModuleName . '], the file path is [' . $moduleLibraryPath . '] we will used it'); |
|
| 210 | 210 | $libraryFilePath = $moduleLibraryPath; |
| 211 | 211 | } |
| 212 | - else{ |
|
| 212 | + else { |
|
| 213 | 213 | $logger->info('Cannot find library [' . $class . '] from modules using the default location'); |
| 214 | 214 | } |
| 215 | 215 | } |
| 216 | - if(! $libraryFilePath){ |
|
| 216 | + if (!$libraryFilePath) { |
|
| 217 | 217 | $searchDir = array(LIBRARY_PATH); |
| 218 | - foreach($searchDir as $dir){ |
|
| 218 | + foreach ($searchDir as $dir) { |
|
| 219 | 219 | $filePath = $dir . $file; |
| 220 | - if(file_exists($filePath)){ |
|
| 220 | + if (file_exists($filePath)) { |
|
| 221 | 221 | $libraryFilePath = $filePath; |
| 222 | 222 | //is already found not to continue |
| 223 | 223 | break; |
@@ -225,20 +225,20 @@ discard block |
||
| 225 | 225 | } |
| 226 | 226 | } |
| 227 | 227 | $logger->info('The library file path to be loaded is [' . $libraryFilePath . ']'); |
| 228 | - if($libraryFilePath){ |
|
| 228 | + if ($libraryFilePath) { |
|
| 229 | 229 | require_once $libraryFilePath; |
| 230 | - if(class_exists($class)){ |
|
| 230 | + if (class_exists($class)) { |
|
| 231 | 231 | $c = $params ? new $class($params) : new $class(); |
| 232 | 232 | $obj = & get_instance(); |
| 233 | 233 | $obj->{$instance} = $c; |
| 234 | 234 | static::$loaded[$instance] = $class; |
| 235 | 235 | $logger->info('Library [' . $class . '] --> ' . $libraryFilePath . ' loaded successfully.'); |
| 236 | 236 | } |
| 237 | - else{ |
|
| 238 | - show_error('The file '.$libraryFilePath.' exists but does not contain the class '.$class); |
|
| 237 | + else { |
|
| 238 | + show_error('The file ' . $libraryFilePath . ' exists but does not contain the class ' . $class); |
|
| 239 | 239 | } |
| 240 | 240 | } |
| 241 | - else{ |
|
| 241 | + else { |
|
| 242 | 242 | show_error('Unable to find library class [' . $class . ']'); |
| 243 | 243 | } |
| 244 | 244 | } |
@@ -250,14 +250,14 @@ discard block |
||
| 250 | 250 | * |
| 251 | 251 | * @return void |
| 252 | 252 | */ |
| 253 | - public static function functions($function){ |
|
| 253 | + public static function functions($function) { |
|
| 254 | 254 | $logger = static::getLogger(); |
| 255 | 255 | $function = str_ireplace('.php', '', $function); |
| 256 | 256 | $function = trim($function, '/\\'); |
| 257 | 257 | $function = str_ireplace('function_', '', $function); |
| 258 | - $file = 'function_'.$function.'.php'; |
|
| 258 | + $file = 'function_' . $function . '.php'; |
|
| 259 | 259 | $logger->debug('Loading helper [' . $function . '] ...'); |
| 260 | - if(isset(static::$loaded['function_' . $function])){ |
|
| 260 | + if (isset(static::$loaded['function_' . $function])) { |
|
| 261 | 261 | $logger->info('Helper [' . $function . '] already loaded no need to load it again, cost in performance'); |
| 262 | 262 | return; |
| 263 | 263 | } |
@@ -267,30 +267,30 @@ discard block |
||
| 267 | 267 | $searchModuleName = null; |
| 268 | 268 | $obj = & get_instance(); |
| 269 | 269 | //check if the request class contains module name |
| 270 | - if(strpos($function, '/') !== false){ |
|
| 270 | + if (strpos($function, '/') !== false) { |
|
| 271 | 271 | $path = explode('/', $function); |
| 272 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 272 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
| 273 | 273 | $searchModuleName = $path[0]; |
| 274 | 274 | $function = 'function_' . $path[1] . '.php'; |
| 275 | - $file = $path[0] . DS . 'function_'.$function.'.php'; |
|
| 275 | + $file = $path[0] . DS . 'function_' . $function . '.php'; |
|
| 276 | 276 | } |
| 277 | 277 | } |
| 278 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 278 | + if (!$searchModuleName && !empty($obj->moduleName)) { |
|
| 279 | 279 | $searchModuleName = $obj->moduleName; |
| 280 | 280 | } |
| 281 | 281 | $moduleFunctionPath = Module::findFunctionFullPath($function, $searchModuleName); |
| 282 | - if($moduleFunctionPath){ |
|
| 283 | - $logger->info('Found helper [' . $function . '] from module [' .$searchModuleName. '], the file path is [' .$moduleFunctionPath. '] we will used it'); |
|
| 282 | + if ($moduleFunctionPath) { |
|
| 283 | + $logger->info('Found helper [' . $function . '] from module [' . $searchModuleName . '], the file path is [' . $moduleFunctionPath . '] we will used it'); |
|
| 284 | 284 | $functionFilePath = $moduleFunctionPath; |
| 285 | 285 | } |
| 286 | - else{ |
|
| 286 | + else { |
|
| 287 | 287 | $logger->info('Cannot find helper [' . $function . '] from modules using the default location'); |
| 288 | 288 | } |
| 289 | - if(! $functionFilePath){ |
|
| 289 | + if (!$functionFilePath) { |
|
| 290 | 290 | $searchDir = array(FUNCTIONS_PATH, CORE_FUNCTIONS_PATH); |
| 291 | - foreach($searchDir as $dir){ |
|
| 291 | + foreach ($searchDir as $dir) { |
|
| 292 | 292 | $filePath = $dir . $file; |
| 293 | - if(file_exists($filePath)){ |
|
| 293 | + if (file_exists($filePath)) { |
|
| 294 | 294 | $functionFilePath = $filePath; |
| 295 | 295 | //is already found not to continue |
| 296 | 296 | break; |
@@ -298,12 +298,12 @@ discard block |
||
| 298 | 298 | } |
| 299 | 299 | } |
| 300 | 300 | $logger->info('The helper file path to be loaded is [' . $functionFilePath . ']'); |
| 301 | - if($functionFilePath){ |
|
| 301 | + if ($functionFilePath) { |
|
| 302 | 302 | require_once $functionFilePath; |
| 303 | 303 | static::$loaded['function_' . $function] = $functionFilePath; |
| 304 | 304 | $logger->info('Helper [' . $function . '] --> ' . $functionFilePath . ' loaded successfully.'); |
| 305 | 305 | } |
| 306 | - else{ |
|
| 306 | + else { |
|
| 307 | 307 | show_error('Unable to find helper file [' . $file . ']'); |
| 308 | 308 | } |
| 309 | 309 | } |
@@ -315,14 +315,14 @@ discard block |
||
| 315 | 315 | * |
| 316 | 316 | * @return void |
| 317 | 317 | */ |
| 318 | - public static function config($filename){ |
|
| 318 | + public static function config($filename) { |
|
| 319 | 319 | $logger = static::getLogger(); |
| 320 | 320 | $filename = str_ireplace('.php', '', $filename); |
| 321 | 321 | $filename = trim($filename, '/\\'); |
| 322 | 322 | $filename = str_ireplace('config_', '', $filename); |
| 323 | - $file = 'config_'.$filename.'.php'; |
|
| 323 | + $file = 'config_' . $filename . '.php'; |
|
| 324 | 324 | $logger->debug('Loading configuration [' . $filename . '] ...'); |
| 325 | - if(isset(static::$loaded['config_' . $filename])){ |
|
| 325 | + if (isset(static::$loaded['config_' . $filename])) { |
|
| 326 | 326 | $logger->info('Configuration [' . $file . '] already loaded no need to load it again, cost in performance'); |
| 327 | 327 | return; |
| 328 | 328 | } |
@@ -332,33 +332,33 @@ discard block |
||
| 332 | 332 | $searchModuleName = null; |
| 333 | 333 | $obj = & get_instance(); |
| 334 | 334 | //check if the request class contains module name |
| 335 | - if(strpos($filename, '/') !== false){ |
|
| 335 | + if (strpos($filename, '/') !== false) { |
|
| 336 | 336 | $path = explode('/', $filename); |
| 337 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 337 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
| 338 | 338 | $searchModuleName = $path[0]; |
| 339 | 339 | $filename = $path[1] . '.php'; |
| 340 | 340 | } |
| 341 | 341 | } |
| 342 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 342 | + if (!$searchModuleName && !empty($obj->moduleName)) { |
|
| 343 | 343 | $searchModuleName = $obj->moduleName; |
| 344 | 344 | } |
| 345 | 345 | $moduleConfigPath = Module::findConfigFullPath($filename, $searchModuleName); |
| 346 | - if($moduleConfigPath){ |
|
| 347 | - $logger->info('Found config [' . $filename . '] from module [' .$searchModuleName. '], the file path is [' .$moduleConfigPath. '] we will used it'); |
|
| 346 | + if ($moduleConfigPath) { |
|
| 347 | + $logger->info('Found config [' . $filename . '] from module [' . $searchModuleName . '], the file path is [' . $moduleConfigPath . '] we will used it'); |
|
| 348 | 348 | $configFilePath = $moduleConfigPath; |
| 349 | 349 | } |
| 350 | - else{ |
|
| 350 | + else { |
|
| 351 | 351 | $logger->info('Cannot find config [' . $filename . '] from modules using the default location'); |
| 352 | 352 | } |
| 353 | 353 | $logger->info('The config file path to be loaded is [' . $configFilePath . ']'); |
| 354 | - if(file_exists($configFilePath)){ |
|
| 354 | + if (file_exists($configFilePath)) { |
|
| 355 | 355 | require_once $configFilePath; |
| 356 | - if(! empty($config) && is_array($config)){ |
|
| 356 | + if (!empty($config) && is_array($config)) { |
|
| 357 | 357 | Config::setAll($config); |
| 358 | 358 | } |
| 359 | 359 | } |
| 360 | - else{ |
|
| 361 | - show_error('Unable to find config file ['. $configFilePath . ']'); |
|
| 360 | + else { |
|
| 361 | + show_error('Unable to find config file [' . $configFilePath . ']'); |
|
| 362 | 362 | } |
| 363 | 363 | static::$loaded['config_' . $filename] = $configFilePath; |
| 364 | 364 | $logger->info('Configuration [' . $configFilePath . '] loaded successfully.'); |
@@ -374,14 +374,14 @@ discard block |
||
| 374 | 374 | * |
| 375 | 375 | * @return void |
| 376 | 376 | */ |
| 377 | - public static function lang($language){ |
|
| 377 | + public static function lang($language) { |
|
| 378 | 378 | $logger = static::getLogger(); |
| 379 | 379 | $language = str_ireplace('.php', '', $language); |
| 380 | 380 | $language = trim($language, '/\\'); |
| 381 | 381 | $language = str_ireplace('lang_', '', $language); |
| 382 | - $file = 'lang_'.$language.'.php'; |
|
| 382 | + $file = 'lang_' . $language . '.php'; |
|
| 383 | 383 | $logger->debug('Loading language [' . $language . '] ...'); |
| 384 | - if(isset(static::$loaded['lang_' . $language])){ |
|
| 384 | + if (isset(static::$loaded['lang_' . $language])) { |
|
| 385 | 385 | $logger->info('Language [' . $language . '] already loaded no need to load it again, cost in performance'); |
| 386 | 386 | return; |
| 387 | 387 | } |
@@ -391,7 +391,7 @@ discard block |
||
| 391 | 391 | $cfgKey = get_config('language_cookie_name'); |
| 392 | 392 | $objCookie = & class_loader('Cookie'); |
| 393 | 393 | $cookieLang = $objCookie->get($cfgKey); |
| 394 | - if($cookieLang){ |
|
| 394 | + if ($cookieLang) { |
|
| 395 | 395 | $appLang = $cookieLang; |
| 396 | 396 | } |
| 397 | 397 | $languageFilePath = null; |
@@ -400,30 +400,30 @@ discard block |
||
| 400 | 400 | $searchModuleName = null; |
| 401 | 401 | $obj = & get_instance(); |
| 402 | 402 | //check if the request class contains module name |
| 403 | - if(strpos($language, '/') !== false){ |
|
| 403 | + if (strpos($language, '/') !== false) { |
|
| 404 | 404 | $path = explode('/', $language); |
| 405 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 405 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
| 406 | 406 | $searchModuleName = $path[0]; |
| 407 | 407 | $language = 'lang_' . $path[1] . '.php'; |
| 408 | - $file = $path[0] . DS .$language; |
|
| 408 | + $file = $path[0] . DS . $language; |
|
| 409 | 409 | } |
| 410 | 410 | } |
| 411 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 411 | + if (!$searchModuleName && !empty($obj->moduleName)) { |
|
| 412 | 412 | $searchModuleName = $obj->moduleName; |
| 413 | 413 | } |
| 414 | 414 | $moduleLanguagePath = Module::findLanguageFullPath($language, $searchModuleName, $appLang); |
| 415 | - if($moduleLanguagePath){ |
|
| 416 | - $logger->info('Found language [' . $language . '] from module [' .$searchModuleName. '], the file path is [' .$moduleLanguagePath. '] we will used it'); |
|
| 415 | + if ($moduleLanguagePath) { |
|
| 416 | + $logger->info('Found language [' . $language . '] from module [' . $searchModuleName . '], the file path is [' . $moduleLanguagePath . '] we will used it'); |
|
| 417 | 417 | $languageFilePath = $moduleLanguagePath; |
| 418 | 418 | } |
| 419 | - else{ |
|
| 419 | + else { |
|
| 420 | 420 | $logger->info('Cannot find language [' . $language . '] from modules using the default location'); |
| 421 | 421 | } |
| 422 | - if(! $languageFilePath){ |
|
| 422 | + if (!$languageFilePath) { |
|
| 423 | 423 | $searchDir = array(APP_LANG_PATH, CORE_LANG_PATH); |
| 424 | - foreach($searchDir as $dir){ |
|
| 424 | + foreach ($searchDir as $dir) { |
|
| 425 | 425 | $filePath = $dir . $appLang . DS . $file; |
| 426 | - if(file_exists($filePath)){ |
|
| 426 | + if (file_exists($filePath)) { |
|
| 427 | 427 | $languageFilePath = $filePath; |
| 428 | 428 | //is already found not to continue |
| 429 | 429 | break; |
@@ -431,12 +431,12 @@ discard block |
||
| 431 | 431 | } |
| 432 | 432 | } |
| 433 | 433 | $logger->info('The language file path to be loaded is [' . $languageFilePath . ']'); |
| 434 | - if($languageFilePath){ |
|
| 434 | + if ($languageFilePath) { |
|
| 435 | 435 | require_once $languageFilePath; |
| 436 | - if(! empty($lang) && is_array($lang)){ |
|
| 437 | - $logger->info('Language file [' .$languageFilePath. '] contains the valid languages keys add them to language list'); |
|
| 436 | + if (!empty($lang) && is_array($lang)) { |
|
| 437 | + $logger->info('Language file [' . $languageFilePath . '] contains the valid languages keys add them to language list'); |
|
| 438 | 438 | //Note: may be here the class 'Lang' not yet loaded |
| 439 | - $langObj =& class_loader('Lang', 'classes'); |
|
| 439 | + $langObj = & class_loader('Lang', 'classes'); |
|
| 440 | 440 | $langObj->addLangMessages($lang); |
| 441 | 441 | //free the memory |
| 442 | 442 | unset($lang); |
@@ -444,13 +444,13 @@ discard block |
||
| 444 | 444 | static::$loaded['lang_' . $language] = $languageFilePath; |
| 445 | 445 | $logger->info('Language [' . $language . '] --> ' . $languageFilePath . ' loaded successfully.'); |
| 446 | 446 | } |
| 447 | - else{ |
|
| 447 | + else { |
|
| 448 | 448 | show_error('Unable to find language file [' . $file . ']'); |
| 449 | 449 | } |
| 450 | 450 | } |
| 451 | 451 | |
| 452 | 452 | |
| 453 | - private function getResourcesFromAutoloadConfig(){ |
|
| 453 | + private function getResourcesFromAutoloadConfig() { |
|
| 454 | 454 | $autoloads = array(); |
| 455 | 455 | $autoloads['config'] = array(); |
| 456 | 456 | $autoloads['languages'] = array(); |
@@ -458,22 +458,22 @@ discard block |
||
| 458 | 458 | $autoloads['models'] = array(); |
| 459 | 459 | $autoloads['functions'] = array(); |
| 460 | 460 | //loading of the resources in autoload.php configuration file |
| 461 | - if(file_exists(CONFIG_PATH . 'autoload.php')){ |
|
| 461 | + if (file_exists(CONFIG_PATH . 'autoload.php')) { |
|
| 462 | 462 | require_once CONFIG_PATH . 'autoload.php'; |
| 463 | - if(! empty($autoload) && is_array($autoload)){ |
|
| 463 | + if (!empty($autoload) && is_array($autoload)) { |
|
| 464 | 464 | $autoloads = array_merge($autoloads, $autoload); |
| 465 | 465 | unset($autoload); |
| 466 | 466 | } |
| 467 | 467 | } |
| 468 | 468 | //loading autoload configuration for modules |
| 469 | 469 | $modulesAutoloads = Module::getModulesAutoloadConfig(); |
| 470 | - if(! empty($modulesAutoloads) && is_array($modulesAutoloads)){ |
|
| 470 | + if (!empty($modulesAutoloads) && is_array($modulesAutoloads)) { |
|
| 471 | 471 | $autoloads = array_merge_recursive($autoloads, $modulesAutoloads); |
| 472 | 472 | } |
| 473 | 473 | return $autoloads; |
| 474 | 474 | } |
| 475 | 475 | |
| 476 | - private function loadResourcesFromAutoloadConfig(){ |
|
| 476 | + private function loadResourcesFromAutoloadConfig() { |
|
| 477 | 477 | $autoloads = array(); |
| 478 | 478 | $autoloads['config'] = array(); |
| 479 | 479 | $autoloads['languages'] = array(); |
@@ -485,29 +485,29 @@ discard block |
||
| 485 | 485 | |
| 486 | 486 | $autoloads = array_merge($autoloads, $list); |
| 487 | 487 | //config autoload |
| 488 | - foreach($autoloads['config'] as $c){ |
|
| 488 | + foreach ($autoloads['config'] as $c) { |
|
| 489 | 489 | $this->config($c); |
| 490 | 490 | } |
| 491 | 491 | |
| 492 | 492 | //languages autoload |
| 493 | - foreach($autoloads['languages'] as $language){ |
|
| 493 | + foreach ($autoloads['languages'] as $language) { |
|
| 494 | 494 | $this->lang($language); |
| 495 | 495 | } |
| 496 | 496 | |
| 497 | 497 | //libraries autoload |
| 498 | - foreach($autoloads['libraries'] as $library){ |
|
| 498 | + foreach ($autoloads['libraries'] as $library) { |
|
| 499 | 499 | $this->library($library); |
| 500 | 500 | } |
| 501 | 501 | |
| 502 | 502 | //models autoload |
| 503 | - if(! empty($autoloads['models']) && is_array($autoloads['models'])){ |
|
| 503 | + if (!empty($autoloads['models']) && is_array($autoloads['models'])) { |
|
| 504 | 504 | require_once CORE_CLASSES_MODEL_PATH . 'Model.php'; |
| 505 | - foreach($autoloads['models'] as $model){ |
|
| 505 | + foreach ($autoloads['models'] as $model) { |
|
| 506 | 506 | $this->model($model); |
| 507 | 507 | } |
| 508 | 508 | } |
| 509 | 509 | |
| 510 | - foreach($autoloads['functions'] as $function){ |
|
| 510 | + foreach ($autoloads['functions'] as $function) { |
|
| 511 | 511 | $this->functions($function); |
| 512 | 512 | } |
| 513 | 513 | } |
@@ -1,914 +1,914 @@ |
||
| 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 | - * Takes and trims each data, if it has any rules, we parse the rule string and run |
|
| 214 | - * each rule against the data value. Sets _success to true if there are no errors |
|
| 215 | - * afterwards. |
|
| 216 | - */ |
|
| 217 | - protected function _run() { |
|
| 218 | - if(get_instance()->request->method() == 'POST' || $this->enableCsrfCheck){ |
|
| 219 | - $this->logger->debug('Check if CSRF is enabled in configuration'); |
|
| 220 | - //first check for CSRF |
|
| 221 | - if ((get_config('csrf_enable', false) || $this->enableCsrfCheck) && ! Security::validateCSRF()){ |
|
| 222 | - show_error('Invalide data, Cross Site Request Forgery do his job, the data to validate is corrupted.'); |
|
| 223 | - } |
|
| 224 | - else{ |
|
| 225 | - $this->logger->info('CSRF is not enabled in configuration or not set manully, no need to check it'); |
|
| 226 | - } |
|
| 227 | - } |
|
| 228 | - ///////////////////////////////////////////// |
|
| 229 | - $this->_forceFail = false; |
|
| 230 | - |
|
| 231 | - foreach ($this->getData() as $inputName => $inputVal) { |
|
| 232 | - if(is_array($this->data[$inputName])){ |
|
| 233 | - $this->data[$inputName] = array_map('trim', $this->data[$inputName]); |
|
| 234 | - } |
|
| 235 | - else{ |
|
| 236 | - $this->data[$inputName] = trim($this->data[$inputName]); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - if (array_key_exists($inputName, $this->_rules)) { |
|
| 240 | - foreach ($this->_parseRuleString($this->_rules[$inputName]) as $eachRule) { |
|
| 241 | - $this->_validateRule($inputName, $this->data[$inputName], $eachRule); |
|
| 242 | - } |
|
| 243 | - } |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - if (empty($this->_errors) && $this->_forceFail === false) { |
|
| 247 | - $this->_success = true; |
|
| 248 | - } |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * Adds a rule to a form data validation field. |
|
| 253 | - * |
|
| 254 | - * @param string $inputField Name of the field or the data key to add a rule to |
|
| 255 | - * @param string $ruleSets PIPE seperated string of rules |
|
| 256 | - * |
|
| 257 | - * @return FormValidation Current instance of object. |
|
| 258 | - */ |
|
| 259 | - public function setRule($inputField, $inputLabel, $ruleSets) { |
|
| 260 | - $this->_rules[$inputField] = $ruleSets; |
|
| 261 | - $this->_labels[$inputField] = $inputLabel; |
|
| 262 | - $this->logger->info('Set the field rule: name [' .$inputField. '], label [' .$inputLabel. '], rules [' .$ruleSets. ']'); |
|
| 263 | - return $this; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * Takes an array of rules and uses setRule() to set them, accepts an array |
|
| 268 | - * of rule names rather than a pipe-delimited string as well. |
|
| 269 | - * @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 | + * Takes and trims each data, if it has any rules, we parse the rule string and run |
|
| 214 | + * each rule against the data value. Sets _success to true if there are no errors |
|
| 215 | + * afterwards. |
|
| 216 | + */ |
|
| 217 | + protected function _run() { |
|
| 218 | + if(get_instance()->request->method() == 'POST' || $this->enableCsrfCheck){ |
|
| 219 | + $this->logger->debug('Check if CSRF is enabled in configuration'); |
|
| 220 | + //first check for CSRF |
|
| 221 | + if ((get_config('csrf_enable', false) || $this->enableCsrfCheck) && ! Security::validateCSRF()){ |
|
| 222 | + show_error('Invalide data, Cross Site Request Forgery do his job, the data to validate is corrupted.'); |
|
| 223 | + } |
|
| 224 | + else{ |
|
| 225 | + $this->logger->info('CSRF is not enabled in configuration or not set manully, no need to check it'); |
|
| 226 | + } |
|
| 227 | + } |
|
| 228 | + ///////////////////////////////////////////// |
|
| 229 | + $this->_forceFail = false; |
|
| 230 | + |
|
| 231 | + foreach ($this->getData() as $inputName => $inputVal) { |
|
| 232 | + if(is_array($this->data[$inputName])){ |
|
| 233 | + $this->data[$inputName] = array_map('trim', $this->data[$inputName]); |
|
| 234 | + } |
|
| 235 | + else{ |
|
| 236 | + $this->data[$inputName] = trim($this->data[$inputName]); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + if (array_key_exists($inputName, $this->_rules)) { |
|
| 240 | + foreach ($this->_parseRuleString($this->_rules[$inputName]) as $eachRule) { |
|
| 241 | + $this->_validateRule($inputName, $this->data[$inputName], $eachRule); |
|
| 242 | + } |
|
| 243 | + } |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + if (empty($this->_errors) && $this->_forceFail === false) { |
|
| 247 | + $this->_success = true; |
|
| 248 | + } |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * Adds a rule to a form data validation field. |
|
| 253 | + * |
|
| 254 | + * @param string $inputField Name of the field or the data key to add a rule to |
|
| 255 | + * @param string $ruleSets PIPE seperated string of rules |
|
| 270 | 256 | * |
| 271 | 257 | * @return FormValidation Current instance of object. |
| 272 | - */ |
|
| 273 | - public function setRules(array $ruleSets) { |
|
| 274 | - foreach ($ruleSets as $ruleSet) { |
|
| 275 | - $pipeDelimitedRules = null; |
|
| 276 | - if (is_array($ruleSet['rules'])) { |
|
| 277 | - $pipeDelimitedRules = implode('|', $ruleSet['rules']); |
|
| 278 | - } else { |
|
| 279 | - $pipeDelimitedRules = $ruleSet['rules']; |
|
| 280 | - } |
|
| 281 | - $this->setRule($ruleSet['name'], $ruleSet['label'], $pipeDelimitedRules); |
|
| 282 | - } |
|
| 283 | - return $this; |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * This method creates the global errors delimiter, each argument occurs once, at the beginning, and |
|
| 288 | - * end of the errors block respectively. |
|
| 289 | - * |
|
| 290 | - * @param string $start Before block of errors gets displayed, HTML allowed. |
|
| 291 | - * @param string $end After the block of errors gets displayed, HTML allowed. |
|
| 292 | - * |
|
| 258 | + */ |
|
| 259 | + public function setRule($inputField, $inputLabel, $ruleSets) { |
|
| 260 | + $this->_rules[$inputField] = $ruleSets; |
|
| 261 | + $this->_labels[$inputField] = $inputLabel; |
|
| 262 | + $this->logger->info('Set the field rule: name [' .$inputField. '], label [' .$inputLabel. '], rules [' .$ruleSets. ']'); |
|
| 263 | + return $this; |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * Takes an array of rules and uses setRule() to set them, accepts an array |
|
| 268 | + * of rule names rather than a pipe-delimited string as well. |
|
| 269 | + * @param array $ruleSets |
|
| 270 | + * |
|
| 293 | 271 | * @return FormValidation Current instance of object. |
| 294 | - */ |
|
| 295 | - public function setErrorsDelimiter($start, $end) { |
|
| 296 | - $this->_allErrorsDelimiter[0] = $start; |
|
| 297 | - $this->_allErrorsDelimiter[1] = $end; |
|
| 298 | - return $this; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * This is the individual error delimiter, each argument occurs once before and after |
|
| 303 | - * each individual error listed. |
|
| 304 | - * |
|
| 305 | - * @param string $start Displayed before each error. |
|
| 306 | - * @param string $end Displayed after each error. |
|
| 307 | - * |
|
| 272 | + */ |
|
| 273 | + public function setRules(array $ruleSets) { |
|
| 274 | + foreach ($ruleSets as $ruleSet) { |
|
| 275 | + $pipeDelimitedRules = null; |
|
| 276 | + if (is_array($ruleSet['rules'])) { |
|
| 277 | + $pipeDelimitedRules = implode('|', $ruleSet['rules']); |
|
| 278 | + } else { |
|
| 279 | + $pipeDelimitedRules = $ruleSet['rules']; |
|
| 280 | + } |
|
| 281 | + $this->setRule($ruleSet['name'], $ruleSet['label'], $pipeDelimitedRules); |
|
| 282 | + } |
|
| 283 | + return $this; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * This method creates the global errors delimiter, each argument occurs once, at the beginning, and |
|
| 288 | + * end of the errors block respectively. |
|
| 289 | + * |
|
| 290 | + * @param string $start Before block of errors gets displayed, HTML allowed. |
|
| 291 | + * @param string $end After the block of errors gets displayed, HTML allowed. |
|
| 292 | + * |
|
| 308 | 293 | * @return FormValidation Current instance of object. |
| 309 | - */ |
|
| 310 | - public function setErrorDelimiter($start, $end) { |
|
| 311 | - $this->_eachErrorDelimiter[0] = $start; |
|
| 312 | - $this->_eachErrorDelimiter[1] = $end; |
|
| 313 | - return $this; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - /** |
|
| 317 | - * Get the each errors delimiters |
|
| 318 | - * |
|
| 319 | - * @return array |
|
| 320 | - */ |
|
| 321 | - public function getErrorDelimiter() { |
|
| 322 | - return $this->_eachErrorDelimiter; |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - /** |
|
| 326 | - * Get the all errors delimiters |
|
| 327 | - * |
|
| 328 | - * @return array |
|
| 329 | - */ |
|
| 330 | - public function getErrorsDelimiter() { |
|
| 331 | - return $this->_allErrorsDelimiter; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * This sets a custom error message that can override the default error phrase provided |
|
| 336 | - * by FormValidation, it can be used in the format of setMessage('rule', 'error phrase') |
|
| 337 | - * which will globally change the error phrase of that rule, or in the format of: |
|
| 338 | - * setMessage('rule', 'fieldname', 'error phrase') - which will only change the error phrase for |
|
| 339 | - * that rule, applied on that field. |
|
| 340 | - * |
|
| 341 | - * @return boolean True on success, false on failure. |
|
| 342 | - */ |
|
| 343 | - public function setMessage() { |
|
| 344 | - $numArgs = func_num_args(); |
|
| 345 | - switch ($numArgs) { |
|
| 346 | - default: |
|
| 347 | - return false; |
|
| 348 | - // A global rule error message |
|
| 349 | - case 2: |
|
| 350 | - foreach ($this->post(null) as $key => $val) { |
|
| 351 | - $this->_errorPhraseOverrides[$key][func_get_arg(0)] = func_get_arg(1); |
|
| 352 | - } |
|
| 353 | - break; |
|
| 354 | - // Field specific rule error message |
|
| 355 | - case 3: |
|
| 356 | - $this->_errorPhraseOverrides[func_get_arg(1)][func_get_arg(0)] = func_get_arg(2); |
|
| 357 | - break; |
|
| 358 | - } |
|
| 359 | - return true; |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * Adds a custom error message in the errorSet array, that will |
|
| 364 | - * forcibly display it. |
|
| 365 | - * |
|
| 366 | - * @param string $inputName The form input name or data key |
|
| 367 | - * @param string $errorMessage Error to display |
|
| 368 | - * |
|
| 369 | - * @return formValidation Current instance of the object |
|
| 370 | - */ |
|
| 371 | - public function setCustomError($inputName, $errorMessage) { |
|
| 372 | - $errorMessage = str_replace('%1', $this->_labels[$inputName], $errorMessage); |
|
| 373 | - $this->_errors[$inputName] = $errorMessage; |
|
| 374 | - return $this; |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - /** |
|
| 378 | - * Allows for an accesor to any/all post values, if a value of null is passed as the key, it |
|
| 379 | - * will recursively find all keys/values of the $_POST array or data array. It also automatically trims |
|
| 380 | - * all values. |
|
| 381 | - * |
|
| 382 | - * @param string $key Key of $this->data to be found, pass null for all Key => Val pairs. |
|
| 383 | - * @param boolean $trim Defaults to true, trims all $this->data values. |
|
| 384 | - * @return string|array Array of post or data values if null is passed as key, string if only one key is desired. |
|
| 385 | - */ |
|
| 386 | - public function post($key = null, $trim = true) { |
|
| 387 | - $returnValue = null; |
|
| 388 | - if (is_null($key)) { |
|
| 389 | - $returnValue = array(); |
|
| 390 | - foreach ($this->getData() as $key => $val) { |
|
| 391 | - $returnValue[$key] = $this->post($key, $trim); |
|
| 392 | - } |
|
| 393 | - } else { |
|
| 394 | - $returnValue = (array_key_exists($key, $this->getData())) ? (($trim) ? trim($this->data[$key]) : $this->data[$key]) : null; |
|
| 395 | - } |
|
| 396 | - return $returnValue; |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - /** |
|
| 400 | - * Gets all errors from errorSet and displays them, can be echo out from the |
|
| 401 | - * function or just returned. |
|
| 402 | - * |
|
| 403 | - * @param boolean $limit number of error to display or return |
|
| 404 | - * @param boolean $echo Whether or not the values are to be returned or displayed |
|
| 405 | - * |
|
| 406 | - * @return string Errors formatted for output |
|
| 407 | - */ |
|
| 408 | - public function displayErrors($limit = null, $echo = true) { |
|
| 409 | - list($errorsStart, $errorsEnd) = $this->_allErrorsDelimiter; |
|
| 410 | - list($errorStart, $errorEnd) = $this->_eachErrorDelimiter; |
|
| 411 | - $errorOutput = $errorsStart; |
|
| 412 | - $i = 0; |
|
| 413 | - if (!empty($this->_errors)) { |
|
| 414 | - foreach ($this->_errors as $fieldName => $error) { |
|
| 415 | - if ($i === $limit) { |
|
| 416 | - break; |
|
| 417 | - } |
|
| 418 | - $errorOutput .= $errorStart; |
|
| 419 | - $errorOutput .= $error; |
|
| 420 | - $errorOutput .= $errorEnd; |
|
| 421 | - $i++; |
|
| 422 | - } |
|
| 423 | - } |
|
| 424 | - $errorOutput .= $errorsEnd; |
|
| 425 | - echo ($echo) ? $errorOutput : ''; |
|
| 426 | - return (! $echo) ? $errorOutput : null; |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - /** |
|
| 430 | - * Returns raw array of errors in no format instead of displaying them |
|
| 431 | - * formatted. |
|
| 432 | - * |
|
| 433 | - * @return array |
|
| 434 | - */ |
|
| 435 | - public function returnErrors() { |
|
| 436 | - return $this->_errors; |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - /** |
|
| 440 | - * Breaks up a PIPE seperated string of rules, and puts them into an array. |
|
| 441 | - * |
|
| 442 | - * @param string $ruleString String to be parsed. |
|
| 443 | - * |
|
| 444 | - * @return array Array of each value in original string. |
|
| 445 | - */ |
|
| 446 | - protected function _parseRuleString($ruleString) { |
|
| 447 | - $ruleSets = array(); |
|
| 448 | - /* |
|
| 294 | + */ |
|
| 295 | + public function setErrorsDelimiter($start, $end) { |
|
| 296 | + $this->_allErrorsDelimiter[0] = $start; |
|
| 297 | + $this->_allErrorsDelimiter[1] = $end; |
|
| 298 | + return $this; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * This is the individual error delimiter, each argument occurs once before and after |
|
| 303 | + * each individual error listed. |
|
| 304 | + * |
|
| 305 | + * @param string $start Displayed before each error. |
|
| 306 | + * @param string $end Displayed after each error. |
|
| 307 | + * |
|
| 308 | + * @return FormValidation Current instance of object. |
|
| 309 | + */ |
|
| 310 | + public function setErrorDelimiter($start, $end) { |
|
| 311 | + $this->_eachErrorDelimiter[0] = $start; |
|
| 312 | + $this->_eachErrorDelimiter[1] = $end; |
|
| 313 | + return $this; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + /** |
|
| 317 | + * Get the each errors delimiters |
|
| 318 | + * |
|
| 319 | + * @return array |
|
| 320 | + */ |
|
| 321 | + public function getErrorDelimiter() { |
|
| 322 | + return $this->_eachErrorDelimiter; |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + /** |
|
| 326 | + * Get the all errors delimiters |
|
| 327 | + * |
|
| 328 | + * @return array |
|
| 329 | + */ |
|
| 330 | + public function getErrorsDelimiter() { |
|
| 331 | + return $this->_allErrorsDelimiter; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * This sets a custom error message that can override the default error phrase provided |
|
| 336 | + * by FormValidation, it can be used in the format of setMessage('rule', 'error phrase') |
|
| 337 | + * which will globally change the error phrase of that rule, or in the format of: |
|
| 338 | + * setMessage('rule', 'fieldname', 'error phrase') - which will only change the error phrase for |
|
| 339 | + * that rule, applied on that field. |
|
| 340 | + * |
|
| 341 | + * @return boolean True on success, false on failure. |
|
| 342 | + */ |
|
| 343 | + public function setMessage() { |
|
| 344 | + $numArgs = func_num_args(); |
|
| 345 | + switch ($numArgs) { |
|
| 346 | + default: |
|
| 347 | + return false; |
|
| 348 | + // A global rule error message |
|
| 349 | + case 2: |
|
| 350 | + foreach ($this->post(null) as $key => $val) { |
|
| 351 | + $this->_errorPhraseOverrides[$key][func_get_arg(0)] = func_get_arg(1); |
|
| 352 | + } |
|
| 353 | + break; |
|
| 354 | + // Field specific rule error message |
|
| 355 | + case 3: |
|
| 356 | + $this->_errorPhraseOverrides[func_get_arg(1)][func_get_arg(0)] = func_get_arg(2); |
|
| 357 | + break; |
|
| 358 | + } |
|
| 359 | + return true; |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * Adds a custom error message in the errorSet array, that will |
|
| 364 | + * forcibly display it. |
|
| 365 | + * |
|
| 366 | + * @param string $inputName The form input name or data key |
|
| 367 | + * @param string $errorMessage Error to display |
|
| 368 | + * |
|
| 369 | + * @return formValidation Current instance of the object |
|
| 370 | + */ |
|
| 371 | + public function setCustomError($inputName, $errorMessage) { |
|
| 372 | + $errorMessage = str_replace('%1', $this->_labels[$inputName], $errorMessage); |
|
| 373 | + $this->_errors[$inputName] = $errorMessage; |
|
| 374 | + return $this; |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + /** |
|
| 378 | + * Allows for an accesor to any/all post values, if a value of null is passed as the key, it |
|
| 379 | + * will recursively find all keys/values of the $_POST array or data array. It also automatically trims |
|
| 380 | + * all values. |
|
| 381 | + * |
|
| 382 | + * @param string $key Key of $this->data to be found, pass null for all Key => Val pairs. |
|
| 383 | + * @param boolean $trim Defaults to true, trims all $this->data values. |
|
| 384 | + * @return string|array Array of post or data values if null is passed as key, string if only one key is desired. |
|
| 385 | + */ |
|
| 386 | + public function post($key = null, $trim = true) { |
|
| 387 | + $returnValue = null; |
|
| 388 | + if (is_null($key)) { |
|
| 389 | + $returnValue = array(); |
|
| 390 | + foreach ($this->getData() as $key => $val) { |
|
| 391 | + $returnValue[$key] = $this->post($key, $trim); |
|
| 392 | + } |
|
| 393 | + } else { |
|
| 394 | + $returnValue = (array_key_exists($key, $this->getData())) ? (($trim) ? trim($this->data[$key]) : $this->data[$key]) : null; |
|
| 395 | + } |
|
| 396 | + return $returnValue; |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + /** |
|
| 400 | + * Gets all errors from errorSet and displays them, can be echo out from the |
|
| 401 | + * function or just returned. |
|
| 402 | + * |
|
| 403 | + * @param boolean $limit number of error to display or return |
|
| 404 | + * @param boolean $echo Whether or not the values are to be returned or displayed |
|
| 405 | + * |
|
| 406 | + * @return string Errors formatted for output |
|
| 407 | + */ |
|
| 408 | + public function displayErrors($limit = null, $echo = true) { |
|
| 409 | + list($errorsStart, $errorsEnd) = $this->_allErrorsDelimiter; |
|
| 410 | + list($errorStart, $errorEnd) = $this->_eachErrorDelimiter; |
|
| 411 | + $errorOutput = $errorsStart; |
|
| 412 | + $i = 0; |
|
| 413 | + if (!empty($this->_errors)) { |
|
| 414 | + foreach ($this->_errors as $fieldName => $error) { |
|
| 415 | + if ($i === $limit) { |
|
| 416 | + break; |
|
| 417 | + } |
|
| 418 | + $errorOutput .= $errorStart; |
|
| 419 | + $errorOutput .= $error; |
|
| 420 | + $errorOutput .= $errorEnd; |
|
| 421 | + $i++; |
|
| 422 | + } |
|
| 423 | + } |
|
| 424 | + $errorOutput .= $errorsEnd; |
|
| 425 | + echo ($echo) ? $errorOutput : ''; |
|
| 426 | + return (! $echo) ? $errorOutput : null; |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + /** |
|
| 430 | + * Returns raw array of errors in no format instead of displaying them |
|
| 431 | + * formatted. |
|
| 432 | + * |
|
| 433 | + * @return array |
|
| 434 | + */ |
|
| 435 | + public function returnErrors() { |
|
| 436 | + return $this->_errors; |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + /** |
|
| 440 | + * Breaks up a PIPE seperated string of rules, and puts them into an array. |
|
| 441 | + * |
|
| 442 | + * @param string $ruleString String to be parsed. |
|
| 443 | + * |
|
| 444 | + * @return array Array of each value in original string. |
|
| 445 | + */ |
|
| 446 | + protected function _parseRuleString($ruleString) { |
|
| 447 | + $ruleSets = array(); |
|
| 448 | + /* |
|
| 449 | 449 | //////////////// hack for regex rule that can contain "|" |
| 450 | 450 | */ |
| 451 | - if(strpos($ruleString, 'regex') !== false){ |
|
| 452 | - $regexRule = array(); |
|
| 453 | - $rule = '#regex\[\/(.*)\/([a-zA-Z0-9]?)\]#'; |
|
| 454 | - preg_match($rule, $ruleString, $regexRule); |
|
| 455 | - $ruleStringTemp = preg_replace($rule, '', $ruleString); |
|
| 456 | - if(!empty($regexRule[0])){ |
|
| 457 | - $ruleSets[] = $regexRule[0]; |
|
| 458 | - } |
|
| 459 | - $ruleStringRegex = explode('|', $ruleStringTemp); |
|
| 460 | - foreach ($ruleStringRegex as $rule) { |
|
| 461 | - $rule = trim($rule); |
|
| 462 | - if($rule){ |
|
| 463 | - $ruleSets[] = $rule; |
|
| 464 | - } |
|
| 465 | - } |
|
| 451 | + if(strpos($ruleString, 'regex') !== false){ |
|
| 452 | + $regexRule = array(); |
|
| 453 | + $rule = '#regex\[\/(.*)\/([a-zA-Z0-9]?)\]#'; |
|
| 454 | + preg_match($rule, $ruleString, $regexRule); |
|
| 455 | + $ruleStringTemp = preg_replace($rule, '', $ruleString); |
|
| 456 | + if(!empty($regexRule[0])){ |
|
| 457 | + $ruleSets[] = $regexRule[0]; |
|
| 458 | + } |
|
| 459 | + $ruleStringRegex = explode('|', $ruleStringTemp); |
|
| 460 | + foreach ($ruleStringRegex as $rule) { |
|
| 461 | + $rule = trim($rule); |
|
| 462 | + if($rule){ |
|
| 463 | + $ruleSets[] = $rule; |
|
| 464 | + } |
|
| 465 | + } |
|
| 466 | 466 | |
| 467 | - } |
|
| 468 | - /***********************************/ |
|
| 469 | - else{ |
|
| 470 | - if (strpos($ruleString, '|') !== FALSE) { |
|
| 471 | - $ruleSets = explode('|', $ruleString); |
|
| 472 | - } else { |
|
| 473 | - $ruleSets[] = $ruleString; |
|
| 474 | - } |
|
| 475 | - } |
|
| 476 | - return $ruleSets; |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - /** |
|
| 480 | - * Returns whether or not a field obtains the rule "required". |
|
| 481 | - * |
|
| 482 | - * @param string $fieldName Field to check if required. |
|
| 483 | - * |
|
| 484 | - * @return boolean Whether or not the field is required. |
|
| 485 | - */ |
|
| 486 | - protected function _fieldIsRequired($fieldName) { |
|
| 487 | - $rules = $this->_parseRuleString($this->_rules[$fieldName]); |
|
| 488 | - return (in_array('required', $rules)); |
|
| 489 | - } |
|
| 490 | - |
|
| 491 | - /** |
|
| 492 | - * Takes a data input name, it's value, and the rule it's being validated against (ex: max_length[16]) |
|
| 493 | - * and adds an error to the errorSet if it fails validation of the rule. |
|
| 494 | - * |
|
| 495 | - * @param string $inputName Name or key of the validation data |
|
| 496 | - * @param string $inputVal Value of the validation data |
|
| 497 | - * @param string $ruleName Rule to be validated against, including args (exact_length[5]) |
|
| 498 | - * @return void |
|
| 499 | - */ |
|
| 500 | - protected function _validateRule($inputName, $inputVal, $ruleName) { |
|
| 501 | - $this->logger->debug('Rule validation of field [' .$inputName. '], value [' .$inputVal. '], rule [' .$ruleName. ']'); |
|
| 502 | - // Array to store args |
|
| 503 | - $ruleArgs = array(); |
|
| 504 | - |
|
| 505 | - preg_match('/\[(.*)\]/', $ruleName, $ruleArgs); |
|
| 506 | - |
|
| 507 | - // Get the rule arguments, realRule is just the base rule name |
|
| 508 | - // Like min_length instead of min_length[3] |
|
| 509 | - $ruleName = preg_replace('/\[(.*)\]/', '', $ruleName); |
|
| 467 | + } |
|
| 468 | + /***********************************/ |
|
| 469 | + else{ |
|
| 470 | + if (strpos($ruleString, '|') !== FALSE) { |
|
| 471 | + $ruleSets = explode('|', $ruleString); |
|
| 472 | + } else { |
|
| 473 | + $ruleSets[] = $ruleString; |
|
| 474 | + } |
|
| 475 | + } |
|
| 476 | + return $ruleSets; |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + /** |
|
| 480 | + * Returns whether or not a field obtains the rule "required". |
|
| 481 | + * |
|
| 482 | + * @param string $fieldName Field to check if required. |
|
| 483 | + * |
|
| 484 | + * @return boolean Whether or not the field is required. |
|
| 485 | + */ |
|
| 486 | + protected function _fieldIsRequired($fieldName) { |
|
| 487 | + $rules = $this->_parseRuleString($this->_rules[$fieldName]); |
|
| 488 | + return (in_array('required', $rules)); |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + /** |
|
| 492 | + * Takes a data input name, it's value, and the rule it's being validated against (ex: max_length[16]) |
|
| 493 | + * and adds an error to the errorSet if it fails validation of the rule. |
|
| 494 | + * |
|
| 495 | + * @param string $inputName Name or key of the validation data |
|
| 496 | + * @param string $inputVal Value of the validation data |
|
| 497 | + * @param string $ruleName Rule to be validated against, including args (exact_length[5]) |
|
| 498 | + * @return void |
|
| 499 | + */ |
|
| 500 | + protected function _validateRule($inputName, $inputVal, $ruleName) { |
|
| 501 | + $this->logger->debug('Rule validation of field [' .$inputName. '], value [' .$inputVal. '], rule [' .$ruleName. ']'); |
|
| 502 | + // Array to store args |
|
| 503 | + $ruleArgs = array(); |
|
| 504 | + |
|
| 505 | + preg_match('/\[(.*)\]/', $ruleName, $ruleArgs); |
|
| 506 | + |
|
| 507 | + // Get the rule arguments, realRule is just the base rule name |
|
| 508 | + // Like min_length instead of min_length[3] |
|
| 509 | + $ruleName = preg_replace('/\[(.*)\]/', '', $ruleName); |
|
| 510 | 510 | |
| 511 | - if (method_exists($this, $this->_toCallCase($ruleName))) { |
|
| 512 | - $methodToCall = $this->_toCallCase($ruleName); |
|
| 513 | - call_user_func(array($this, $methodToCall), $inputName, $ruleName, $ruleArgs); |
|
| 514 | - } |
|
| 515 | - return; |
|
| 516 | - } |
|
| 517 | - |
|
| 518 | - /** |
|
| 519 | - * Set error for the given field or key |
|
| 520 | - * |
|
| 521 | - * @param string $inputName the input or key name |
|
| 522 | - * @param string $ruleName the rule name |
|
| 523 | - * @param array|string $replacements |
|
| 524 | - */ |
|
| 525 | - protected function _setError($inputName, $ruleName, $replacements = array()) { |
|
| 526 | - $rulePhraseKeyParts = explode(',', $ruleName); |
|
| 527 | - $rulePhrase = null; |
|
| 528 | - foreach ($rulePhraseKeyParts as $rulePhraseKeyPart) { |
|
| 529 | - if (array_key_exists($rulePhraseKeyPart, $this->_errorsMessages)) { |
|
| 530 | - $rulePhrase = $this->_errorsMessages[$rulePhraseKeyPart]; |
|
| 531 | - } else { |
|
| 532 | - $rulePhrase = $rulePhrase[$rulePhraseKeyPart]; |
|
| 533 | - } |
|
| 534 | - } |
|
| 535 | - // Any overrides? |
|
| 536 | - if (array_key_exists($inputName, $this->_errorPhraseOverrides) && array_key_exists($ruleName, $this->_errorPhraseOverrides[$inputName])) { |
|
| 537 | - $rulePhrase = $this->_errorPhraseOverrides[$inputName][$ruleName]; |
|
| 538 | - } |
|
| 539 | - // Type cast to array in case it's a string |
|
| 540 | - $replacements = (array) $replacements; |
|
| 511 | + if (method_exists($this, $this->_toCallCase($ruleName))) { |
|
| 512 | + $methodToCall = $this->_toCallCase($ruleName); |
|
| 513 | + call_user_func(array($this, $methodToCall), $inputName, $ruleName, $ruleArgs); |
|
| 514 | + } |
|
| 515 | + return; |
|
| 516 | + } |
|
| 517 | + |
|
| 518 | + /** |
|
| 519 | + * Set error for the given field or key |
|
| 520 | + * |
|
| 521 | + * @param string $inputName the input or key name |
|
| 522 | + * @param string $ruleName the rule name |
|
| 523 | + * @param array|string $replacements |
|
| 524 | + */ |
|
| 525 | + protected function _setError($inputName, $ruleName, $replacements = array()) { |
|
| 526 | + $rulePhraseKeyParts = explode(',', $ruleName); |
|
| 527 | + $rulePhrase = null; |
|
| 528 | + foreach ($rulePhraseKeyParts as $rulePhraseKeyPart) { |
|
| 529 | + if (array_key_exists($rulePhraseKeyPart, $this->_errorsMessages)) { |
|
| 530 | + $rulePhrase = $this->_errorsMessages[$rulePhraseKeyPart]; |
|
| 531 | + } else { |
|
| 532 | + $rulePhrase = $rulePhrase[$rulePhraseKeyPart]; |
|
| 533 | + } |
|
| 534 | + } |
|
| 535 | + // Any overrides? |
|
| 536 | + if (array_key_exists($inputName, $this->_errorPhraseOverrides) && array_key_exists($ruleName, $this->_errorPhraseOverrides[$inputName])) { |
|
| 537 | + $rulePhrase = $this->_errorPhraseOverrides[$inputName][$ruleName]; |
|
| 538 | + } |
|
| 539 | + // Type cast to array in case it's a string |
|
| 540 | + $replacements = (array) $replacements; |
|
| 541 | 541 | $replacementCount = count($replacements); |
| 542 | - for ($i = 1; $i <= $replacementCount; $i++) { |
|
| 543 | - $key = $i - 1; |
|
| 544 | - $rulePhrase = str_replace('%' . $i, $replacements[$key], $rulePhrase); |
|
| 545 | - } |
|
| 546 | - if (! array_key_exists($inputName, $this->_errors)) { |
|
| 547 | - $this->_errors[$inputName] = $rulePhrase; |
|
| 548 | - } |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - /** |
|
| 552 | - * Used to run a callback for the callback rule, as well as pass in a default |
|
| 553 | - * argument of the post value. For example the username field having a rule: |
|
| 554 | - * callback[userExists] will eval userExists(data[username]) - Note the use |
|
| 555 | - * of eval over call_user_func is in case the function is not user defined. |
|
| 556 | - * |
|
| 557 | - * @param type $inputArg |
|
| 558 | - * @param string $callbackFunc |
|
| 559 | - * |
|
| 560 | - * @return mixed |
|
| 561 | - */ |
|
| 562 | - protected function _runCallback($inputArg, $callbackFunc) { |
|
| 542 | + for ($i = 1; $i <= $replacementCount; $i++) { |
|
| 543 | + $key = $i - 1; |
|
| 544 | + $rulePhrase = str_replace('%' . $i, $replacements[$key], $rulePhrase); |
|
| 545 | + } |
|
| 546 | + if (! array_key_exists($inputName, $this->_errors)) { |
|
| 547 | + $this->_errors[$inputName] = $rulePhrase; |
|
| 548 | + } |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + /** |
|
| 552 | + * Used to run a callback for the callback rule, as well as pass in a default |
|
| 553 | + * argument of the post value. For example the username field having a rule: |
|
| 554 | + * callback[userExists] will eval userExists(data[username]) - Note the use |
|
| 555 | + * of eval over call_user_func is in case the function is not user defined. |
|
| 556 | + * |
|
| 557 | + * @param type $inputArg |
|
| 558 | + * @param string $callbackFunc |
|
| 559 | + * |
|
| 560 | + * @return mixed |
|
| 561 | + */ |
|
| 562 | + protected function _runCallback($inputArg, $callbackFunc) { |
|
| 563 | 563 | return eval('return ' . $callbackFunc . '("' . $inputArg . '");'); |
| 564 | - } |
|
| 565 | - |
|
| 566 | - /** |
|
| 567 | - * Used for applying a rule only if the empty callback evaluates to true, |
|
| 568 | - * for example required[funcName] - This runs funcName without passing any |
|
| 569 | - * arguments. |
|
| 570 | - * |
|
| 571 | - * @param string $callbackFunc |
|
| 572 | - * |
|
| 573 | - * @return anything |
|
| 574 | - */ |
|
| 575 | - protected function _runEmptyCallback($callbackFunc) { |
|
| 576 | - return eval('return ' . $callbackFunc . '();'); |
|
| 577 | - } |
|
| 578 | - |
|
| 579 | - /** |
|
| 580 | - * Gets a specific label of a specific field input name. |
|
| 581 | - * |
|
| 582 | - * @param string $inputName |
|
| 583 | - * |
|
| 584 | - * @return string |
|
| 585 | - */ |
|
| 586 | - protected function _getLabel($inputName) { |
|
| 587 | - return (array_key_exists($inputName, $this->_labels)) ? $this->_labels[$inputName] : $inputName; |
|
| 588 | - } |
|
| 564 | + } |
|
| 565 | + |
|
| 566 | + /** |
|
| 567 | + * Used for applying a rule only if the empty callback evaluates to true, |
|
| 568 | + * for example required[funcName] - This runs funcName without passing any |
|
| 569 | + * arguments. |
|
| 570 | + * |
|
| 571 | + * @param string $callbackFunc |
|
| 572 | + * |
|
| 573 | + * @return anything |
|
| 574 | + */ |
|
| 575 | + protected function _runEmptyCallback($callbackFunc) { |
|
| 576 | + return eval('return ' . $callbackFunc . '();'); |
|
| 577 | + } |
|
| 578 | + |
|
| 579 | + /** |
|
| 580 | + * Gets a specific label of a specific field input name. |
|
| 581 | + * |
|
| 582 | + * @param string $inputName |
|
| 583 | + * |
|
| 584 | + * @return string |
|
| 585 | + */ |
|
| 586 | + protected function _getLabel($inputName) { |
|
| 587 | + return (array_key_exists($inputName, $this->_labels)) ? $this->_labels[$inputName] : $inputName; |
|
| 588 | + } |
|
| 589 | 589 | |
| 590 | - /** |
|
| 591 | - * Peform validation for the rule "required" |
|
| 592 | - * @param string $inputName the form field or data key name used |
|
| 593 | - * @param string $ruleName the rule name for this validation ("required") |
|
| 594 | - * @param array $ruleArgs the rules argument |
|
| 595 | - */ |
|
| 590 | + /** |
|
| 591 | + * Peform validation for the rule "required" |
|
| 592 | + * @param string $inputName the form field or data key name used |
|
| 593 | + * @param string $ruleName the rule name for this validation ("required") |
|
| 594 | + * @param array $ruleArgs the rules argument |
|
| 595 | + */ |
|
| 596 | 596 | protected function _validateRequired($inputName, $ruleName, array $ruleArgs) { |
| 597 | - $inputVal = $this->post($inputName); |
|
| 598 | - if(array_key_exists(1, $ruleArgs) && function_exists($ruleArgs[1])) { |
|
| 599 | - $callbackReturn = $this->_runEmptyCallback($ruleArgs[1]); |
|
| 600 | - if ($inputVal == '' && $callbackReturn == true) { |
|
| 601 | - $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
| 602 | - } |
|
| 603 | - } |
|
| 597 | + $inputVal = $this->post($inputName); |
|
| 598 | + if(array_key_exists(1, $ruleArgs) && function_exists($ruleArgs[1])) { |
|
| 599 | + $callbackReturn = $this->_runEmptyCallback($ruleArgs[1]); |
|
| 600 | + if ($inputVal == '' && $callbackReturn == true) { |
|
| 601 | + $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
| 602 | + } |
|
| 603 | + } |
|
| 604 | 604 | else if($inputVal == '') { |
| 605 | 605 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
| 606 | - } |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - /** |
|
| 610 | - * Perform validation for the honey pot so means for the validation to be failed |
|
| 611 | - * @param string $inputName the form field or data key name used |
|
| 612 | - * @param string $ruleName the rule name for this validation |
|
| 613 | - * @param array $ruleArgs the rules argument |
|
| 614 | - */ |
|
| 615 | - protected function _validateHoneypot($inputName, $ruleName, array $ruleArgs) { |
|
| 616 | - if ($this->data[$inputName] != '') { |
|
| 617 | - $this->_forceFail = true; |
|
| 618 | - } |
|
| 619 | - } |
|
| 620 | - |
|
| 621 | - /** |
|
| 622 | - * Peform validation for the rule "callback" |
|
| 623 | - * @param string $inputName the form field or data key name used |
|
| 624 | - * @param string $ruleName the rule name for this validation ("callback") |
|
| 625 | - * @param array $ruleArgs the rules argument |
|
| 626 | - */ |
|
| 627 | - protected function _validateCallback($inputName, $ruleName, array $ruleArgs) { |
|
| 628 | - if (function_exists($ruleArgs[1]) && !empty($this->data[$inputName])) { |
|
| 606 | + } |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + /** |
|
| 610 | + * Perform validation for the honey pot so means for the validation to be failed |
|
| 611 | + * @param string $inputName the form field or data key name used |
|
| 612 | + * @param string $ruleName the rule name for this validation |
|
| 613 | + * @param array $ruleArgs the rules argument |
|
| 614 | + */ |
|
| 615 | + protected function _validateHoneypot($inputName, $ruleName, array $ruleArgs) { |
|
| 616 | + if ($this->data[$inputName] != '') { |
|
| 617 | + $this->_forceFail = true; |
|
| 618 | + } |
|
| 619 | + } |
|
| 620 | + |
|
| 621 | + /** |
|
| 622 | + * Peform validation for the rule "callback" |
|
| 623 | + * @param string $inputName the form field or data key name used |
|
| 624 | + * @param string $ruleName the rule name for this validation ("callback") |
|
| 625 | + * @param array $ruleArgs the rules argument |
|
| 626 | + */ |
|
| 627 | + protected function _validateCallback($inputName, $ruleName, array $ruleArgs) { |
|
| 628 | + if (function_exists($ruleArgs[1]) && !empty($this->data[$inputName])) { |
|
| 629 | 629 | $result = $this->_runCallback($this->data[$inputName], $ruleArgs[1]); |
| 630 | 630 | if(! $result){ |
| 631 | 631 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
| 632 | 632 | } |
| 633 | - } |
|
| 634 | - } |
|
| 635 | - |
|
| 636 | - /** |
|
| 637 | - * Peform validation for the rule "depends" |
|
| 638 | - * @param string $inputName the form field or data key name used |
|
| 639 | - * @param string $ruleName the rule name for this validation ("depends") |
|
| 640 | - * @param array $ruleArgs the rules argument |
|
| 641 | - */ |
|
| 642 | - protected function _validateDepends($inputName, $ruleName, array $ruleArgs) { |
|
| 643 | - if (array_key_exists($ruleArgs[1], $this->_errors)) { |
|
| 644 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 645 | - } |
|
| 646 | - } |
|
| 647 | - |
|
| 648 | - /** |
|
| 649 | - * Peform validation for the rule "not_equal" |
|
| 650 | - * @param string $inputName the form field or data key name used |
|
| 651 | - * @param string $ruleName the rule name for this validation ("not_equal") |
|
| 652 | - * @param array $ruleArgs the rules argument |
|
| 653 | - */ |
|
| 654 | - protected function _validateNotEqual($inputName, $ruleName, array $ruleArgs) { |
|
| 655 | - $canNotEqual = explode(',', $ruleArgs[1]); |
|
| 656 | - foreach ($canNotEqual as $doNotEqual) { |
|
| 657 | - $inputVal = $this->post($inputName); |
|
| 658 | - if (preg_match('/post:(.*)/', $doNotEqual)) { |
|
| 659 | - if ($inputVal == $this->data[str_replace('post:', '', $doNotEqual)]) { |
|
| 660 | - $this->_setError($inputName, $ruleName . ',post:key', array($this->_getLabel($inputName), $this->_getLabel(str_replace('post:', '', $doNotEqual)))); |
|
| 661 | - continue; |
|
| 662 | - } |
|
| 663 | - } |
|
| 633 | + } |
|
| 634 | + } |
|
| 635 | + |
|
| 636 | + /** |
|
| 637 | + * Peform validation for the rule "depends" |
|
| 638 | + * @param string $inputName the form field or data key name used |
|
| 639 | + * @param string $ruleName the rule name for this validation ("depends") |
|
| 640 | + * @param array $ruleArgs the rules argument |
|
| 641 | + */ |
|
| 642 | + protected function _validateDepends($inputName, $ruleName, array $ruleArgs) { |
|
| 643 | + if (array_key_exists($ruleArgs[1], $this->_errors)) { |
|
| 644 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 645 | + } |
|
| 646 | + } |
|
| 647 | + |
|
| 648 | + /** |
|
| 649 | + * Peform validation for the rule "not_equal" |
|
| 650 | + * @param string $inputName the form field or data key name used |
|
| 651 | + * @param string $ruleName the rule name for this validation ("not_equal") |
|
| 652 | + * @param array $ruleArgs the rules argument |
|
| 653 | + */ |
|
| 654 | + protected function _validateNotEqual($inputName, $ruleName, array $ruleArgs) { |
|
| 655 | + $canNotEqual = explode(',', $ruleArgs[1]); |
|
| 656 | + foreach ($canNotEqual as $doNotEqual) { |
|
| 657 | + $inputVal = $this->post($inputName); |
|
| 658 | + if (preg_match('/post:(.*)/', $doNotEqual)) { |
|
| 659 | + if ($inputVal == $this->data[str_replace('post:', '', $doNotEqual)]) { |
|
| 660 | + $this->_setError($inputName, $ruleName . ',post:key', array($this->_getLabel($inputName), $this->_getLabel(str_replace('post:', '', $doNotEqual)))); |
|
| 661 | + continue; |
|
| 662 | + } |
|
| 663 | + } |
|
| 664 | 664 | else{ |
| 665 | - if ($inputVal == $doNotEqual) { |
|
| 666 | - $this->_setError($inputName, $ruleName . ',string', array($this->_getLabel($inputName), $doNotEqual)); |
|
| 667 | - continue; |
|
| 668 | - } |
|
| 669 | - } |
|
| 670 | - } |
|
| 671 | - } |
|
| 672 | - |
|
| 673 | - /** |
|
| 674 | - * Peform validation for the rule "matches" |
|
| 675 | - * @param string $inputName the form field or data key name used |
|
| 676 | - * @param string $ruleName the rule name for this validation ("matches") |
|
| 677 | - * @param array $ruleArgs the rules argument |
|
| 678 | - */ |
|
| 679 | - protected function _validateMatches($inputName, $ruleName, array $ruleArgs) { |
|
| 680 | - $inputVal = $this->post($inputName); |
|
| 681 | - if ($inputVal != $this->data[$ruleArgs[1]]) { |
|
| 682 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 683 | - } |
|
| 684 | - } |
|
| 685 | - |
|
| 686 | - /** |
|
| 687 | - * Peform validation for the rule "valid_email" |
|
| 688 | - * @param string $inputName the form field or data key name used |
|
| 689 | - * @param string $ruleName the rule name for this validation ("valid_email") |
|
| 690 | - * @param array $ruleArgs the rules argument |
|
| 691 | - */ |
|
| 692 | - protected function _validateValidEmail($inputName, $ruleName, array $ruleArgs) { |
|
| 693 | - $inputVal = $this->post($inputName); |
|
| 694 | - 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)) { |
|
| 695 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 696 | - return; |
|
| 697 | - } |
|
| 698 | - $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
| 699 | - } |
|
| 700 | - } |
|
| 701 | - |
|
| 702 | - /** |
|
| 703 | - * Peform validation for the rule "exact_length" |
|
| 704 | - * @param string $inputName the form field or data key name used |
|
| 705 | - * @param string $ruleName the rule name for this validation ("exact_length") |
|
| 706 | - * @param array $ruleArgs the rules argument |
|
| 707 | - */ |
|
| 708 | - protected function _validateExactLength($inputName, $ruleName, array $ruleArgs) { |
|
| 709 | - $inputVal = $this->post($inputName); |
|
| 710 | - if (strlen($inputVal) != $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 711 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 712 | - return; |
|
| 713 | - } |
|
| 714 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 715 | - } |
|
| 716 | - } |
|
| 717 | - |
|
| 718 | - /** |
|
| 719 | - * Peform validation for the rule "max_length" |
|
| 720 | - * @param string $inputName the form field or data key name used |
|
| 721 | - * @param string $ruleName the rule name for this validation ("max_length") |
|
| 722 | - * @param array $ruleArgs the rules argument |
|
| 723 | - */ |
|
| 724 | - protected function _validateMaxLength($inputName, $ruleName, array $ruleArgs) { |
|
| 725 | - $inputVal = $this->post($inputName); |
|
| 726 | - if (strlen($inputVal) > $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 727 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 728 | - return; |
|
| 729 | - } |
|
| 730 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 731 | - } |
|
| 732 | - } |
|
| 733 | - |
|
| 734 | - /** |
|
| 735 | - * Peform validation for the rule "min_length" |
|
| 736 | - * @param string $inputName the form field or data key name used |
|
| 737 | - * @param string $ruleName the rule name for this validation ("min_length") |
|
| 738 | - * @param array $ruleArgs the rules argument |
|
| 739 | - */ |
|
| 740 | - protected function _validateMinLength($inputName, $ruleName, array $ruleArgs) { |
|
| 741 | - $inputVal = $this->post($inputName); |
|
| 742 | - if (strlen($inputVal) < $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 743 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 744 | - return; |
|
| 745 | - } |
|
| 746 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 747 | - } |
|
| 748 | - } |
|
| 665 | + if ($inputVal == $doNotEqual) { |
|
| 666 | + $this->_setError($inputName, $ruleName . ',string', array($this->_getLabel($inputName), $doNotEqual)); |
|
| 667 | + continue; |
|
| 668 | + } |
|
| 669 | + } |
|
| 670 | + } |
|
| 671 | + } |
|
| 672 | + |
|
| 673 | + /** |
|
| 674 | + * Peform validation for the rule "matches" |
|
| 675 | + * @param string $inputName the form field or data key name used |
|
| 676 | + * @param string $ruleName the rule name for this validation ("matches") |
|
| 677 | + * @param array $ruleArgs the rules argument |
|
| 678 | + */ |
|
| 679 | + protected function _validateMatches($inputName, $ruleName, array $ruleArgs) { |
|
| 680 | + $inputVal = $this->post($inputName); |
|
| 681 | + if ($inputVal != $this->data[$ruleArgs[1]]) { |
|
| 682 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 683 | + } |
|
| 684 | + } |
|
| 685 | + |
|
| 686 | + /** |
|
| 687 | + * Peform validation for the rule "valid_email" |
|
| 688 | + * @param string $inputName the form field or data key name used |
|
| 689 | + * @param string $ruleName the rule name for this validation ("valid_email") |
|
| 690 | + * @param array $ruleArgs the rules argument |
|
| 691 | + */ |
|
| 692 | + protected function _validateValidEmail($inputName, $ruleName, array $ruleArgs) { |
|
| 693 | + $inputVal = $this->post($inputName); |
|
| 694 | + 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)) { |
|
| 695 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 696 | + return; |
|
| 697 | + } |
|
| 698 | + $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
| 699 | + } |
|
| 700 | + } |
|
| 701 | + |
|
| 702 | + /** |
|
| 703 | + * Peform validation for the rule "exact_length" |
|
| 704 | + * @param string $inputName the form field or data key name used |
|
| 705 | + * @param string $ruleName the rule name for this validation ("exact_length") |
|
| 706 | + * @param array $ruleArgs the rules argument |
|
| 707 | + */ |
|
| 708 | + protected function _validateExactLength($inputName, $ruleName, array $ruleArgs) { |
|
| 709 | + $inputVal = $this->post($inputName); |
|
| 710 | + if (strlen($inputVal) != $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 711 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 712 | + return; |
|
| 713 | + } |
|
| 714 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 715 | + } |
|
| 716 | + } |
|
| 717 | + |
|
| 718 | + /** |
|
| 719 | + * Peform validation for the rule "max_length" |
|
| 720 | + * @param string $inputName the form field or data key name used |
|
| 721 | + * @param string $ruleName the rule name for this validation ("max_length") |
|
| 722 | + * @param array $ruleArgs the rules argument |
|
| 723 | + */ |
|
| 724 | + protected function _validateMaxLength($inputName, $ruleName, array $ruleArgs) { |
|
| 725 | + $inputVal = $this->post($inputName); |
|
| 726 | + if (strlen($inputVal) > $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 727 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 728 | + return; |
|
| 729 | + } |
|
| 730 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 731 | + } |
|
| 732 | + } |
|
| 733 | + |
|
| 734 | + /** |
|
| 735 | + * Peform validation for the rule "min_length" |
|
| 736 | + * @param string $inputName the form field or data key name used |
|
| 737 | + * @param string $ruleName the rule name for this validation ("min_length") |
|
| 738 | + * @param array $ruleArgs the rules argument |
|
| 739 | + */ |
|
| 740 | + protected function _validateMinLength($inputName, $ruleName, array $ruleArgs) { |
|
| 741 | + $inputVal = $this->post($inputName); |
|
| 742 | + if (strlen($inputVal) < $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 743 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 744 | + return; |
|
| 745 | + } |
|
| 746 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 747 | + } |
|
| 748 | + } |
|
| 749 | 749 | |
| 750 | - /** |
|
| 751 | - * Peform validation for the rule "less_than" |
|
| 752 | - * @param string $inputName the form field or data key name used |
|
| 753 | - * @param string $ruleName the rule name for this validation ("less_than") |
|
| 754 | - * @param array $ruleArgs the rules argument |
|
| 755 | - */ |
|
| 756 | - protected function _validateLessThan($inputName, $ruleName, array $ruleArgs) { |
|
| 757 | - $inputVal = $this->post($inputName); |
|
| 758 | - if ($inputVal >= $ruleArgs[1]) { |
|
| 759 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 760 | - return; |
|
| 761 | - } |
|
| 762 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 763 | - } |
|
| 764 | - } |
|
| 750 | + /** |
|
| 751 | + * Peform validation for the rule "less_than" |
|
| 752 | + * @param string $inputName the form field or data key name used |
|
| 753 | + * @param string $ruleName the rule name for this validation ("less_than") |
|
| 754 | + * @param array $ruleArgs the rules argument |
|
| 755 | + */ |
|
| 756 | + protected function _validateLessThan($inputName, $ruleName, array $ruleArgs) { |
|
| 757 | + $inputVal = $this->post($inputName); |
|
| 758 | + if ($inputVal >= $ruleArgs[1]) { |
|
| 759 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 760 | + return; |
|
| 761 | + } |
|
| 762 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 763 | + } |
|
| 764 | + } |
|
| 765 | 765 | |
| 766 | - /** |
|
| 767 | - * Peform validation for the rule "greater_than" |
|
| 768 | - * @param string $inputName the form field or data key name used |
|
| 769 | - * @param string $ruleName the rule name for this validation ("greater_than") |
|
| 770 | - * @param array $ruleArgs the rules argument |
|
| 771 | - */ |
|
| 772 | - protected function _validateGreaterThan($inputName, $ruleName, array $ruleArgs) { |
|
| 773 | - $inputVal = $this->post($inputName); |
|
| 774 | - if ($inputVal <= $ruleArgs[1]) { |
|
| 775 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 776 | - return; |
|
| 777 | - } |
|
| 778 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 779 | - } |
|
| 780 | - } |
|
| 766 | + /** |
|
| 767 | + * Peform validation for the rule "greater_than" |
|
| 768 | + * @param string $inputName the form field or data key name used |
|
| 769 | + * @param string $ruleName the rule name for this validation ("greater_than") |
|
| 770 | + * @param array $ruleArgs the rules argument |
|
| 771 | + */ |
|
| 772 | + protected function _validateGreaterThan($inputName, $ruleName, array $ruleArgs) { |
|
| 773 | + $inputVal = $this->post($inputName); |
|
| 774 | + if ($inputVal <= $ruleArgs[1]) { |
|
| 775 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 776 | + return; |
|
| 777 | + } |
|
| 778 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 779 | + } |
|
| 780 | + } |
|
| 781 | 781 | |
| 782 | - /** |
|
| 783 | - * Peform validation for the rule "numeric" |
|
| 784 | - * @param string $inputName the form field or data key name used |
|
| 785 | - * @param string $ruleName the rule name for this validation ("numeric") |
|
| 786 | - * @param array $ruleArgs the rules argument |
|
| 787 | - */ |
|
| 788 | - protected function _validateNumeric($inputName, $ruleName, array $ruleArgs) { |
|
| 789 | - $inputVal = $this->post($inputName); |
|
| 790 | - if (! is_numeric($inputVal)) { |
|
| 791 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 792 | - return; |
|
| 793 | - } |
|
| 794 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 795 | - } |
|
| 796 | - } |
|
| 782 | + /** |
|
| 783 | + * Peform validation for the rule "numeric" |
|
| 784 | + * @param string $inputName the form field or data key name used |
|
| 785 | + * @param string $ruleName the rule name for this validation ("numeric") |
|
| 786 | + * @param array $ruleArgs the rules argument |
|
| 787 | + */ |
|
| 788 | + protected function _validateNumeric($inputName, $ruleName, array $ruleArgs) { |
|
| 789 | + $inputVal = $this->post($inputName); |
|
| 790 | + if (! is_numeric($inputVal)) { |
|
| 791 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 792 | + return; |
|
| 793 | + } |
|
| 794 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 795 | + } |
|
| 796 | + } |
|
| 797 | 797 | |
| 798 | - /** |
|
| 799 | - * Peform validation for the rule "exists" |
|
| 800 | - * @param string $inputName the form field or data key name used |
|
| 801 | - * @param string $ruleName the rule name for this validation ("exists") |
|
| 802 | - * @param array $ruleArgs the rules argument |
|
| 803 | - */ |
|
| 798 | + /** |
|
| 799 | + * Peform validation for the rule "exists" |
|
| 800 | + * @param string $inputName the form field or data key name used |
|
| 801 | + * @param string $ruleName the rule name for this validation ("exists") |
|
| 802 | + * @param array $ruleArgs the rules argument |
|
| 803 | + */ |
|
| 804 | 804 | protected function _validateExists($inputName, $ruleName, array $ruleArgs) { |
| 805 | - $inputVal = $this->post($inputName); |
|
| 806 | - $obj = & get_instance(); |
|
| 807 | - if(! isset($obj->database)){ |
|
| 808 | - return; |
|
| 809 | - } |
|
| 810 | - list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 811 | - $obj->database->from($table) |
|
| 812 | - ->where($column, $inputVal) |
|
| 813 | - ->get(); |
|
| 814 | - $nb = $obj->database->numRows(); |
|
| 815 | - if ($nb == 0) { |
|
| 816 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 817 | - return; |
|
| 818 | - } |
|
| 819 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 820 | - } |
|
| 821 | - } |
|
| 822 | - |
|
| 823 | - /** |
|
| 824 | - * Peform validation for the rule "is_unique" |
|
| 825 | - * @param string $inputName the form field or data key name used |
|
| 826 | - * @param string $ruleName the rule name for this validation ("is_unique") |
|
| 827 | - * @param array $ruleArgs the rules argument |
|
| 828 | - */ |
|
| 829 | - protected function _validateIsUnique($inputName, $ruleName, array $ruleArgs) { |
|
| 830 | - $inputVal = $this->post($inputName); |
|
| 831 | - $obj = & get_instance(); |
|
| 832 | - if(! isset($obj->database)){ |
|
| 833 | - return; |
|
| 834 | - } |
|
| 835 | - list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 836 | - $obj->database->from($table) |
|
| 837 | - ->where($column, $inputVal) |
|
| 838 | - ->get(); |
|
| 839 | - $nb = $obj->database->numRows(); |
|
| 840 | - if ($nb != 0) { |
|
| 841 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 842 | - return; |
|
| 843 | - } |
|
| 844 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 845 | - } |
|
| 846 | - } |
|
| 805 | + $inputVal = $this->post($inputName); |
|
| 806 | + $obj = & get_instance(); |
|
| 807 | + if(! isset($obj->database)){ |
|
| 808 | + return; |
|
| 809 | + } |
|
| 810 | + list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 811 | + $obj->database->from($table) |
|
| 812 | + ->where($column, $inputVal) |
|
| 813 | + ->get(); |
|
| 814 | + $nb = $obj->database->numRows(); |
|
| 815 | + if ($nb == 0) { |
|
| 816 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 817 | + return; |
|
| 818 | + } |
|
| 819 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 820 | + } |
|
| 821 | + } |
|
| 822 | + |
|
| 823 | + /** |
|
| 824 | + * Peform validation for the rule "is_unique" |
|
| 825 | + * @param string $inputName the form field or data key name used |
|
| 826 | + * @param string $ruleName the rule name for this validation ("is_unique") |
|
| 827 | + * @param array $ruleArgs the rules argument |
|
| 828 | + */ |
|
| 829 | + protected function _validateIsUnique($inputName, $ruleName, array $ruleArgs) { |
|
| 830 | + $inputVal = $this->post($inputName); |
|
| 831 | + $obj = & get_instance(); |
|
| 832 | + if(! isset($obj->database)){ |
|
| 833 | + return; |
|
| 834 | + } |
|
| 835 | + list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 836 | + $obj->database->from($table) |
|
| 837 | + ->where($column, $inputVal) |
|
| 838 | + ->get(); |
|
| 839 | + $nb = $obj->database->numRows(); |
|
| 840 | + if ($nb != 0) { |
|
| 841 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 842 | + return; |
|
| 843 | + } |
|
| 844 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 845 | + } |
|
| 846 | + } |
|
| 847 | 847 | |
| 848 | - /** |
|
| 849 | - * Peform validation for the rule "is_unique_update" |
|
| 850 | - * @param string $inputName the form field or data key name used |
|
| 851 | - * @param string $ruleName the rule name for this validation ("is_unique_update") |
|
| 852 | - * @param array $ruleArgs the rules argument |
|
| 853 | - */ |
|
| 854 | - protected function _validateIsUniqueUpdate($inputName, $ruleName, array $ruleArgs) { |
|
| 855 | - $inputVal = $this->post($inputName); |
|
| 856 | - $obj = & get_instance(); |
|
| 857 | - if(! isset($obj->database)){ |
|
| 858 | - return; |
|
| 859 | - } |
|
| 860 | - $data = explode(',', $ruleArgs[1]); |
|
| 861 | - if(count($data) < 2){ |
|
| 862 | - return; |
|
| 863 | - } |
|
| 864 | - list($table, $column) = explode('.', $data[0]); |
|
| 865 | - list($field, $val) = explode('=', $data[1]); |
|
| 866 | - $obj->database->from($table) |
|
| 867 | - ->where($column, $inputVal) |
|
| 868 | - ->where($field, '!=', trim($val)) |
|
| 869 | - ->get(); |
|
| 870 | - $nb = $obj->database->numRows(); |
|
| 871 | - if ($nb != 0) { |
|
| 872 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 873 | - return; |
|
| 874 | - } |
|
| 875 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 876 | - } |
|
| 877 | - } |
|
| 878 | - |
|
| 879 | - /** |
|
| 880 | - * Peform validation for the rule "in_list" |
|
| 881 | - * @param string $inputName the form field or data key name used |
|
| 882 | - * @param string $ruleName the rule name for this validation ("in_list") |
|
| 883 | - * @param array $ruleArgs the rules argument |
|
| 884 | - */ |
|
| 885 | - protected function _validateInList($inputName, $ruleName, array $ruleArgs) { |
|
| 886 | - $inputVal = $this->post($inputName); |
|
| 887 | - $list = explode(',', $ruleArgs[1]); |
|
| 888 | - $list = array_map('trim', $list); |
|
| 889 | - if (! in_array($inputVal, $list)) { |
|
| 890 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 891 | - return; |
|
| 892 | - } |
|
| 893 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 894 | - } |
|
| 895 | - } |
|
| 896 | - |
|
| 897 | - /** |
|
| 898 | - * Peform validation for the rule "regex" |
|
| 899 | - * @param string $inputName the form field or data key name used |
|
| 900 | - * @param string $ruleName the rule name for this validation ("regex") |
|
| 901 | - * @param array $ruleArgs the rules argument |
|
| 902 | - */ |
|
| 903 | - protected function _validateRegex($inputName, $ruleName, array $ruleArgs) { |
|
| 904 | - $inputVal = $this->post($inputName); |
|
| 905 | - $regex = $ruleArgs[1]; |
|
| 906 | - if (! preg_match($regex, $inputVal)) { |
|
| 907 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 908 | - return; |
|
| 909 | - } |
|
| 910 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 911 | - } |
|
| 912 | - } |
|
| 848 | + /** |
|
| 849 | + * Peform validation for the rule "is_unique_update" |
|
| 850 | + * @param string $inputName the form field or data key name used |
|
| 851 | + * @param string $ruleName the rule name for this validation ("is_unique_update") |
|
| 852 | + * @param array $ruleArgs the rules argument |
|
| 853 | + */ |
|
| 854 | + protected function _validateIsUniqueUpdate($inputName, $ruleName, array $ruleArgs) { |
|
| 855 | + $inputVal = $this->post($inputName); |
|
| 856 | + $obj = & get_instance(); |
|
| 857 | + if(! isset($obj->database)){ |
|
| 858 | + return; |
|
| 859 | + } |
|
| 860 | + $data = explode(',', $ruleArgs[1]); |
|
| 861 | + if(count($data) < 2){ |
|
| 862 | + return; |
|
| 863 | + } |
|
| 864 | + list($table, $column) = explode('.', $data[0]); |
|
| 865 | + list($field, $val) = explode('=', $data[1]); |
|
| 866 | + $obj->database->from($table) |
|
| 867 | + ->where($column, $inputVal) |
|
| 868 | + ->where($field, '!=', trim($val)) |
|
| 869 | + ->get(); |
|
| 870 | + $nb = $obj->database->numRows(); |
|
| 871 | + if ($nb != 0) { |
|
| 872 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 873 | + return; |
|
| 874 | + } |
|
| 875 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 876 | + } |
|
| 877 | + } |
|
| 878 | + |
|
| 879 | + /** |
|
| 880 | + * Peform validation for the rule "in_list" |
|
| 881 | + * @param string $inputName the form field or data key name used |
|
| 882 | + * @param string $ruleName the rule name for this validation ("in_list") |
|
| 883 | + * @param array $ruleArgs the rules argument |
|
| 884 | + */ |
|
| 885 | + protected function _validateInList($inputName, $ruleName, array $ruleArgs) { |
|
| 886 | + $inputVal = $this->post($inputName); |
|
| 887 | + $list = explode(',', $ruleArgs[1]); |
|
| 888 | + $list = array_map('trim', $list); |
|
| 889 | + if (! in_array($inputVal, $list)) { |
|
| 890 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 891 | + return; |
|
| 892 | + } |
|
| 893 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 894 | + } |
|
| 895 | + } |
|
| 896 | + |
|
| 897 | + /** |
|
| 898 | + * Peform validation for the rule "regex" |
|
| 899 | + * @param string $inputName the form field or data key name used |
|
| 900 | + * @param string $ruleName the rule name for this validation ("regex") |
|
| 901 | + * @param array $ruleArgs the rules argument |
|
| 902 | + */ |
|
| 903 | + protected function _validateRegex($inputName, $ruleName, array $ruleArgs) { |
|
| 904 | + $inputVal = $this->post($inputName); |
|
| 905 | + $regex = $ruleArgs[1]; |
|
| 906 | + if (! preg_match($regex, $inputVal)) { |
|
| 907 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 908 | + return; |
|
| 909 | + } |
|
| 910 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 911 | + } |
|
| 912 | + } |
|
| 913 | 913 | |
| 914 | - } |
|
| 914 | + } |
|
@@ -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 | /** |
@@ -215,13 +215,13 @@ discard block |
||
| 215 | 215 | * afterwards. |
| 216 | 216 | */ |
| 217 | 217 | protected function _run() { |
| 218 | - if(get_instance()->request->method() == 'POST' || $this->enableCsrfCheck){ |
|
| 218 | + if (get_instance()->request->method() == 'POST' || $this->enableCsrfCheck) { |
|
| 219 | 219 | $this->logger->debug('Check if CSRF is enabled in configuration'); |
| 220 | 220 | //first check for CSRF |
| 221 | - if ((get_config('csrf_enable', false) || $this->enableCsrfCheck) && ! Security::validateCSRF()){ |
|
| 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 | 223 | } |
| 224 | - else{ |
|
| 224 | + else { |
|
| 225 | 225 | $this->logger->info('CSRF is not enabled in configuration or not set manully, no need to check it'); |
| 226 | 226 | } |
| 227 | 227 | } |
@@ -229,10 +229,10 @@ discard block |
||
| 229 | 229 | $this->_forceFail = false; |
| 230 | 230 | |
| 231 | 231 | foreach ($this->getData() as $inputName => $inputVal) { |
| 232 | - if(is_array($this->data[$inputName])){ |
|
| 232 | + if (is_array($this->data[$inputName])) { |
|
| 233 | 233 | $this->data[$inputName] = array_map('trim', $this->data[$inputName]); |
| 234 | 234 | } |
| 235 | - else{ |
|
| 235 | + else { |
|
| 236 | 236 | $this->data[$inputName] = trim($this->data[$inputName]); |
| 237 | 237 | } |
| 238 | 238 | |
@@ -259,7 +259,7 @@ discard block |
||
| 259 | 259 | public function setRule($inputField, $inputLabel, $ruleSets) { |
| 260 | 260 | $this->_rules[$inputField] = $ruleSets; |
| 261 | 261 | $this->_labels[$inputField] = $inputLabel; |
| 262 | - $this->logger->info('Set the field rule: name [' .$inputField. '], label [' .$inputLabel. '], rules [' .$ruleSets. ']'); |
|
| 262 | + $this->logger->info('Set the field rule: name [' . $inputField . '], label [' . $inputLabel . '], rules [' . $ruleSets . ']'); |
|
| 263 | 263 | return $this; |
| 264 | 264 | } |
| 265 | 265 | |
@@ -423,7 +423,7 @@ discard block |
||
| 423 | 423 | } |
| 424 | 424 | $errorOutput .= $errorsEnd; |
| 425 | 425 | echo ($echo) ? $errorOutput : ''; |
| 426 | - return (! $echo) ? $errorOutput : null; |
|
| 426 | + return (!$echo) ? $errorOutput : null; |
|
| 427 | 427 | } |
| 428 | 428 | |
| 429 | 429 | /** |
@@ -448,25 +448,25 @@ discard block |
||
| 448 | 448 | /* |
| 449 | 449 | //////////////// hack for regex rule that can contain "|" |
| 450 | 450 | */ |
| 451 | - if(strpos($ruleString, 'regex') !== false){ |
|
| 451 | + if (strpos($ruleString, 'regex') !== false) { |
|
| 452 | 452 | $regexRule = array(); |
| 453 | 453 | $rule = '#regex\[\/(.*)\/([a-zA-Z0-9]?)\]#'; |
| 454 | 454 | preg_match($rule, $ruleString, $regexRule); |
| 455 | 455 | $ruleStringTemp = preg_replace($rule, '', $ruleString); |
| 456 | - if(!empty($regexRule[0])){ |
|
| 456 | + if (!empty($regexRule[0])) { |
|
| 457 | 457 | $ruleSets[] = $regexRule[0]; |
| 458 | 458 | } |
| 459 | 459 | $ruleStringRegex = explode('|', $ruleStringTemp); |
| 460 | 460 | foreach ($ruleStringRegex as $rule) { |
| 461 | 461 | $rule = trim($rule); |
| 462 | - if($rule){ |
|
| 462 | + if ($rule) { |
|
| 463 | 463 | $ruleSets[] = $rule; |
| 464 | 464 | } |
| 465 | 465 | } |
| 466 | 466 | |
| 467 | 467 | } |
| 468 | 468 | /***********************************/ |
| 469 | - else{ |
|
| 469 | + else { |
|
| 470 | 470 | if (strpos($ruleString, '|') !== FALSE) { |
| 471 | 471 | $ruleSets = explode('|', $ruleString); |
| 472 | 472 | } else { |
@@ -498,7 +498,7 @@ discard block |
||
| 498 | 498 | * @return void |
| 499 | 499 | */ |
| 500 | 500 | protected function _validateRule($inputName, $inputVal, $ruleName) { |
| 501 | - $this->logger->debug('Rule validation of field [' .$inputName. '], value [' .$inputVal. '], rule [' .$ruleName. ']'); |
|
| 501 | + $this->logger->debug('Rule validation of field [' . $inputName . '], value [' . $inputVal . '], rule [' . $ruleName . ']'); |
|
| 502 | 502 | // Array to store args |
| 503 | 503 | $ruleArgs = array(); |
| 504 | 504 | |
@@ -543,7 +543,7 @@ discard block |
||
| 543 | 543 | $key = $i - 1; |
| 544 | 544 | $rulePhrase = str_replace('%' . $i, $replacements[$key], $rulePhrase); |
| 545 | 545 | } |
| 546 | - if (! array_key_exists($inputName, $this->_errors)) { |
|
| 546 | + if (!array_key_exists($inputName, $this->_errors)) { |
|
| 547 | 547 | $this->_errors[$inputName] = $rulePhrase; |
| 548 | 548 | } |
| 549 | 549 | } |
@@ -595,13 +595,13 @@ discard block |
||
| 595 | 595 | */ |
| 596 | 596 | protected function _validateRequired($inputName, $ruleName, array $ruleArgs) { |
| 597 | 597 | $inputVal = $this->post($inputName); |
| 598 | - if(array_key_exists(1, $ruleArgs) && function_exists($ruleArgs[1])) { |
|
| 598 | + if (array_key_exists(1, $ruleArgs) && function_exists($ruleArgs[1])) { |
|
| 599 | 599 | $callbackReturn = $this->_runEmptyCallback($ruleArgs[1]); |
| 600 | 600 | if ($inputVal == '' && $callbackReturn == true) { |
| 601 | 601 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
| 602 | 602 | } |
| 603 | 603 | } |
| 604 | - else if($inputVal == '') { |
|
| 604 | + else if ($inputVal == '') { |
|
| 605 | 605 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
| 606 | 606 | } |
| 607 | 607 | } |
@@ -627,7 +627,7 @@ discard block |
||
| 627 | 627 | protected function _validateCallback($inputName, $ruleName, array $ruleArgs) { |
| 628 | 628 | if (function_exists($ruleArgs[1]) && !empty($this->data[$inputName])) { |
| 629 | 629 | $result = $this->_runCallback($this->data[$inputName], $ruleArgs[1]); |
| 630 | - if(! $result){ |
|
| 630 | + if (!$result) { |
|
| 631 | 631 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
| 632 | 632 | } |
| 633 | 633 | } |
@@ -661,7 +661,7 @@ discard block |
||
| 661 | 661 | continue; |
| 662 | 662 | } |
| 663 | 663 | } |
| 664 | - else{ |
|
| 664 | + else { |
|
| 665 | 665 | if ($inputVal == $doNotEqual) { |
| 666 | 666 | $this->_setError($inputName, $ruleName . ',string', array($this->_getLabel($inputName), $doNotEqual)); |
| 667 | 667 | continue; |
@@ -691,8 +691,8 @@ discard block |
||
| 691 | 691 | */ |
| 692 | 692 | protected function _validateValidEmail($inputName, $ruleName, array $ruleArgs) { |
| 693 | 693 | $inputVal = $this->post($inputName); |
| 694 | - 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)) { |
|
| 695 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 694 | + 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)) { |
|
| 695 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 696 | 696 | return; |
| 697 | 697 | } |
| 698 | 698 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
@@ -708,7 +708,7 @@ discard block |
||
| 708 | 708 | protected function _validateExactLength($inputName, $ruleName, array $ruleArgs) { |
| 709 | 709 | $inputVal = $this->post($inputName); |
| 710 | 710 | if (strlen($inputVal) != $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
| 711 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 711 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 712 | 712 | return; |
| 713 | 713 | } |
| 714 | 714 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
@@ -724,7 +724,7 @@ discard block |
||
| 724 | 724 | protected function _validateMaxLength($inputName, $ruleName, array $ruleArgs) { |
| 725 | 725 | $inputVal = $this->post($inputName); |
| 726 | 726 | if (strlen($inputVal) > $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
| 727 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 727 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 728 | 728 | return; |
| 729 | 729 | } |
| 730 | 730 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
@@ -740,7 +740,7 @@ discard block |
||
| 740 | 740 | protected function _validateMinLength($inputName, $ruleName, array $ruleArgs) { |
| 741 | 741 | $inputVal = $this->post($inputName); |
| 742 | 742 | if (strlen($inputVal) < $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
| 743 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 743 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 744 | 744 | return; |
| 745 | 745 | } |
| 746 | 746 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
@@ -756,7 +756,7 @@ discard block |
||
| 756 | 756 | protected function _validateLessThan($inputName, $ruleName, array $ruleArgs) { |
| 757 | 757 | $inputVal = $this->post($inputName); |
| 758 | 758 | if ($inputVal >= $ruleArgs[1]) { |
| 759 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 759 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 760 | 760 | return; |
| 761 | 761 | } |
| 762 | 762 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
@@ -772,7 +772,7 @@ discard block |
||
| 772 | 772 | protected function _validateGreaterThan($inputName, $ruleName, array $ruleArgs) { |
| 773 | 773 | $inputVal = $this->post($inputName); |
| 774 | 774 | if ($inputVal <= $ruleArgs[1]) { |
| 775 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 775 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 776 | 776 | return; |
| 777 | 777 | } |
| 778 | 778 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
@@ -787,8 +787,8 @@ discard block |
||
| 787 | 787 | */ |
| 788 | 788 | protected function _validateNumeric($inputName, $ruleName, array $ruleArgs) { |
| 789 | 789 | $inputVal = $this->post($inputName); |
| 790 | - if (! is_numeric($inputVal)) { |
|
| 791 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 790 | + if (!is_numeric($inputVal)) { |
|
| 791 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 792 | 792 | return; |
| 793 | 793 | } |
| 794 | 794 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
@@ -804,7 +804,7 @@ discard block |
||
| 804 | 804 | protected function _validateExists($inputName, $ruleName, array $ruleArgs) { |
| 805 | 805 | $inputVal = $this->post($inputName); |
| 806 | 806 | $obj = & get_instance(); |
| 807 | - if(! isset($obj->database)){ |
|
| 807 | + if (!isset($obj->database)) { |
|
| 808 | 808 | return; |
| 809 | 809 | } |
| 810 | 810 | list($table, $column) = explode('.', $ruleArgs[1]); |
@@ -813,7 +813,7 @@ discard block |
||
| 813 | 813 | ->get(); |
| 814 | 814 | $nb = $obj->database->numRows(); |
| 815 | 815 | if ($nb == 0) { |
| 816 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 816 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 817 | 817 | return; |
| 818 | 818 | } |
| 819 | 819 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
@@ -829,7 +829,7 @@ discard block |
||
| 829 | 829 | protected function _validateIsUnique($inputName, $ruleName, array $ruleArgs) { |
| 830 | 830 | $inputVal = $this->post($inputName); |
| 831 | 831 | $obj = & get_instance(); |
| 832 | - if(! isset($obj->database)){ |
|
| 832 | + if (!isset($obj->database)) { |
|
| 833 | 833 | return; |
| 834 | 834 | } |
| 835 | 835 | list($table, $column) = explode('.', $ruleArgs[1]); |
@@ -838,7 +838,7 @@ discard block |
||
| 838 | 838 | ->get(); |
| 839 | 839 | $nb = $obj->database->numRows(); |
| 840 | 840 | if ($nb != 0) { |
| 841 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 841 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 842 | 842 | return; |
| 843 | 843 | } |
| 844 | 844 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
@@ -854,11 +854,11 @@ discard block |
||
| 854 | 854 | protected function _validateIsUniqueUpdate($inputName, $ruleName, array $ruleArgs) { |
| 855 | 855 | $inputVal = $this->post($inputName); |
| 856 | 856 | $obj = & get_instance(); |
| 857 | - if(! isset($obj->database)){ |
|
| 857 | + if (!isset($obj->database)) { |
|
| 858 | 858 | return; |
| 859 | 859 | } |
| 860 | 860 | $data = explode(',', $ruleArgs[1]); |
| 861 | - if(count($data) < 2){ |
|
| 861 | + if (count($data) < 2) { |
|
| 862 | 862 | return; |
| 863 | 863 | } |
| 864 | 864 | list($table, $column) = explode('.', $data[0]); |
@@ -869,7 +869,7 @@ discard block |
||
| 869 | 869 | ->get(); |
| 870 | 870 | $nb = $obj->database->numRows(); |
| 871 | 871 | if ($nb != 0) { |
| 872 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 872 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 873 | 873 | return; |
| 874 | 874 | } |
| 875 | 875 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
@@ -886,8 +886,8 @@ discard block |
||
| 886 | 886 | $inputVal = $this->post($inputName); |
| 887 | 887 | $list = explode(',', $ruleArgs[1]); |
| 888 | 888 | $list = array_map('trim', $list); |
| 889 | - if (! in_array($inputVal, $list)) { |
|
| 890 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 889 | + if (!in_array($inputVal, $list)) { |
|
| 890 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 891 | 891 | return; |
| 892 | 892 | } |
| 893 | 893 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
@@ -903,8 +903,8 @@ discard block |
||
| 903 | 903 | protected function _validateRegex($inputName, $ruleName, array $ruleArgs) { |
| 904 | 904 | $inputVal = $this->post($inputName); |
| 905 | 905 | $regex = $ruleArgs[1]; |
| 906 | - if (! preg_match($regex, $inputVal)) { |
|
| 907 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 906 | + if (!preg_match($regex, $inputVal)) { |
|
| 907 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 908 | 908 | return; |
| 909 | 909 | } |
| 910 | 910 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
@@ -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; |