@@ -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,1198 +22,1198 @@ 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 | - if (! $this->where){ |
|
| 414 | - $this->where = $field.' IS NULL '; |
|
| 415 | - } |
|
| 416 | - else{ |
|
| 417 | - $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NULL '; |
|
| 418 | - } |
|
| 419 | - } |
|
| 420 | - return $this; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * Set the SQL WHERE CLAUSE for IS NOT NULL |
|
| 425 | - * @param string|array $field the field name or array of field list |
|
| 426 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 427 | - * @return object the current Database instance |
|
| 428 | - */ |
|
| 429 | - public function whereIsNotNull($field, $andOr = 'AND'){ |
|
| 430 | - if(is_array($field)){ |
|
| 431 | - foreach($field as $f){ |
|
| 432 | - $this->whereIsNotNull($f, $andOr); |
|
| 433 | - } |
|
| 434 | - } |
|
| 435 | - else{ |
|
| 436 | - if (! $this->where){ |
|
| 437 | - $this->where = $field.' IS NOT NULL '; |
|
| 438 | - } |
|
| 439 | - else{ |
|
| 440 | - $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NOT NULL '; |
|
| 441 | - } |
|
| 442 | - } |
|
| 443 | - return $this; |
|
| 444 | - } |
|
| 445 | - |
|
| 446 | - /** |
|
| 447 | - * Get the SQL WHERE clause using array column => value |
|
| 448 | - * @see Database::where |
|
| 449 | - * |
|
| 450 | - * @return string |
|
| 451 | - */ |
|
| 452 | - protected function getWhereStrIfIsArray(array $where, $type = '', $andOr = 'AND', $escape = true){ |
|
| 453 | - $_where = array(); |
|
| 454 | - foreach ($where as $column => $data){ |
|
| 455 | - if(is_null($data)){ |
|
| 456 | - $data = ''; |
|
| 457 | - } |
|
| 458 | - $_where[] = $type . $column . ' = ' . ($escape ? $this->escape($data) : $data); |
|
| 459 | - } |
|
| 460 | - $where = implode(' '.$andOr.' ', $_where); |
|
| 461 | - return $where; |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * Get the SQL WHERE clause when operator argument is an array |
|
| 466 | - * @see Database::where |
|
| 467 | - * |
|
| 468 | - * @return string |
|
| 469 | - */ |
|
| 470 | - protected function getWhereStrIfOperatorIsArray($where, array $op, $type = '', $escape = true){ |
|
| 471 | - $x = explode('?', $where); |
|
| 472 | - $w = ''; |
|
| 473 | - foreach($x as $k => $v){ |
|
| 474 | - if(! empty($v)){ |
|
| 475 | - if(isset($op[$k]) && is_null($op[$k])){ |
|
| 476 | - $op[$k] = ''; |
|
| 477 | - } |
|
| 478 | - $w .= $type . $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
|
| 479 | - } |
|
| 480 | - } |
|
| 481 | - return $w; |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - /** |
|
| 485 | - * Get the default SQL WHERE clause using operator = or the operator argument |
|
| 486 | - * @see Database::where |
|
| 487 | - * |
|
| 488 | - * @return string |
|
| 489 | - */ |
|
| 490 | - protected function getWhereStrForOperator($where, $op = null, $val = null, $type = '', $escape = true){ |
|
| 491 | - $w = ''; |
|
| 492 | - if (! in_array((string)$op, $this->operatorList)){ |
|
| 493 | - if(is_null($op)){ |
|
| 494 | - $op = ''; |
|
| 495 | - } |
|
| 496 | - $w = $type . $where . ' = ' . ($escape ? $this->escape($op) : $op); |
|
| 497 | - } |
|
| 498 | - else{ |
|
| 499 | - if(is_null($val)){ |
|
| 500 | - $val = ''; |
|
| 501 | - } |
|
| 502 | - $w = $type . $where . $op . ($escape ? $this->escape($val) : $val); |
|
| 503 | - } |
|
| 504 | - return $w; |
|
| 505 | - } |
|
| 506 | - |
|
| 507 | - /** |
|
| 508 | - * Set the $this->where property |
|
| 509 | - * @param string $whereStr the WHERE clause string |
|
| 510 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 511 | - */ |
|
| 512 | - protected function setWhereStr($whereStr, $andOr = 'AND'){ |
|
| 513 | - if (empty($this->where)){ |
|
| 514 | - $this->where = $whereStr; |
|
| 515 | - } |
|
| 516 | - else{ |
|
| 517 | - if(substr($this->where, -1) == '('){ |
|
| 518 | - $this->where = $this->where . ' ' . $whereStr; |
|
| 519 | - } |
|
| 520 | - else{ |
|
| 521 | - $this->where = $this->where . ' '.$andOr.' ' . $whereStr; |
|
| 522 | - } |
|
| 523 | - } |
|
| 524 | - } |
|
| 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 | + if (! $this->where){ |
|
| 414 | + $this->where = $field.' IS NULL '; |
|
| 415 | + } |
|
| 416 | + else{ |
|
| 417 | + $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NULL '; |
|
| 418 | + } |
|
| 419 | + } |
|
| 420 | + return $this; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * Set the SQL WHERE CLAUSE for IS NOT NULL |
|
| 425 | + * @param string|array $field the field name or array of field list |
|
| 426 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 427 | + * @return object the current Database instance |
|
| 428 | + */ |
|
| 429 | + public function whereIsNotNull($field, $andOr = 'AND'){ |
|
| 430 | + if(is_array($field)){ |
|
| 431 | + foreach($field as $f){ |
|
| 432 | + $this->whereIsNotNull($f, $andOr); |
|
| 433 | + } |
|
| 434 | + } |
|
| 435 | + else{ |
|
| 436 | + if (! $this->where){ |
|
| 437 | + $this->where = $field.' IS NOT NULL '; |
|
| 438 | + } |
|
| 439 | + else{ |
|
| 440 | + $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NOT NULL '; |
|
| 441 | + } |
|
| 442 | + } |
|
| 443 | + return $this; |
|
| 444 | + } |
|
| 445 | + |
|
| 446 | + /** |
|
| 447 | + * Get the SQL WHERE clause using array column => value |
|
| 448 | + * @see Database::where |
|
| 449 | + * |
|
| 450 | + * @return string |
|
| 451 | + */ |
|
| 452 | + protected function getWhereStrIfIsArray(array $where, $type = '', $andOr = 'AND', $escape = true){ |
|
| 453 | + $_where = array(); |
|
| 454 | + foreach ($where as $column => $data){ |
|
| 455 | + if(is_null($data)){ |
|
| 456 | + $data = ''; |
|
| 457 | + } |
|
| 458 | + $_where[] = $type . $column . ' = ' . ($escape ? $this->escape($data) : $data); |
|
| 459 | + } |
|
| 460 | + $where = implode(' '.$andOr.' ', $_where); |
|
| 461 | + return $where; |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * Get the SQL WHERE clause when operator argument is an array |
|
| 466 | + * @see Database::where |
|
| 467 | + * |
|
| 468 | + * @return string |
|
| 469 | + */ |
|
| 470 | + protected function getWhereStrIfOperatorIsArray($where, array $op, $type = '', $escape = true){ |
|
| 471 | + $x = explode('?', $where); |
|
| 472 | + $w = ''; |
|
| 473 | + foreach($x as $k => $v){ |
|
| 474 | + if(! empty($v)){ |
|
| 475 | + if(isset($op[$k]) && is_null($op[$k])){ |
|
| 476 | + $op[$k] = ''; |
|
| 477 | + } |
|
| 478 | + $w .= $type . $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
|
| 479 | + } |
|
| 480 | + } |
|
| 481 | + return $w; |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + /** |
|
| 485 | + * Get the default SQL WHERE clause using operator = or the operator argument |
|
| 486 | + * @see Database::where |
|
| 487 | + * |
|
| 488 | + * @return string |
|
| 489 | + */ |
|
| 490 | + protected function getWhereStrForOperator($where, $op = null, $val = null, $type = '', $escape = true){ |
|
| 491 | + $w = ''; |
|
| 492 | + if (! in_array((string)$op, $this->operatorList)){ |
|
| 493 | + if(is_null($op)){ |
|
| 494 | + $op = ''; |
|
| 495 | + } |
|
| 496 | + $w = $type . $where . ' = ' . ($escape ? $this->escape($op) : $op); |
|
| 497 | + } |
|
| 498 | + else{ |
|
| 499 | + if(is_null($val)){ |
|
| 500 | + $val = ''; |
|
| 501 | + } |
|
| 502 | + $w = $type . $where . $op . ($escape ? $this->escape($val) : $val); |
|
| 503 | + } |
|
| 504 | + return $w; |
|
| 505 | + } |
|
| 506 | + |
|
| 507 | + /** |
|
| 508 | + * Set the $this->where property |
|
| 509 | + * @param string $whereStr the WHERE clause string |
|
| 510 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 511 | + */ |
|
| 512 | + protected function setWhereStr($whereStr, $andOr = 'AND'){ |
|
| 513 | + if (empty($this->where)){ |
|
| 514 | + $this->where = $whereStr; |
|
| 515 | + } |
|
| 516 | + else{ |
|
| 517 | + if(substr($this->where, -1) == '('){ |
|
| 518 | + $this->where = $this->where . ' ' . $whereStr; |
|
| 519 | + } |
|
| 520 | + else{ |
|
| 521 | + $this->where = $this->where . ' '.$andOr.' ' . $whereStr; |
|
| 522 | + } |
|
| 523 | + } |
|
| 524 | + } |
|
| 525 | 525 | |
| 526 | - /** |
|
| 527 | - * Set the SQL WHERE CLAUSE statment |
|
| 528 | - * @param string|array $where the where field or array of field list |
|
| 529 | - * @param array|string $op the condition operator. If is null the default will be "=" |
|
| 530 | - * @param mixed $val the where value |
|
| 531 | - * @param string $type the type used for this where clause (NOT, etc.) |
|
| 532 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 533 | - * @param boolean $escape whether to escape or not the $val |
|
| 534 | - * @return object the current Database instance |
|
| 535 | - */ |
|
| 536 | - public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
|
| 537 | - $whereStr = ''; |
|
| 538 | - if (is_array($where)){ |
|
| 539 | - $whereStr = $this->getWhereStrIfIsArray($where, $type, $andOr, $escape); |
|
| 540 | - } |
|
| 541 | - else{ |
|
| 542 | - if(is_array($op)){ |
|
| 543 | - $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
|
| 544 | - } else { |
|
| 545 | - $whereStr = $this->getWhereStrForOperator($where, $op, $val, $type, $escape = true); |
|
| 546 | - } |
|
| 547 | - } |
|
| 548 | - $this->setWhereStr($whereStr, $andOr); |
|
| 549 | - return $this; |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - /** |
|
| 553 | - * Set the SQL WHERE CLAUSE statment using OR |
|
| 554 | - * @see Database::where() |
|
| 555 | - * @return object the current Database instance |
|
| 556 | - */ |
|
| 557 | - public function orWhere($where, $op = null, $val = null, $escape = true){ |
|
| 558 | - return $this->where($where, $op, $val, '', 'OR', $escape); |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - |
|
| 562 | - /** |
|
| 563 | - * Set the SQL WHERE CLAUSE statment using AND and NOT |
|
| 564 | - * @see Database::where() |
|
| 565 | - * @return object the current Database instance |
|
| 566 | - */ |
|
| 567 | - public function notWhere($where, $op = null, $val = null, $escape = true){ |
|
| 568 | - return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
|
| 569 | - } |
|
| 570 | - |
|
| 571 | - /** |
|
| 572 | - * Set the SQL WHERE CLAUSE statment using OR and NOT |
|
| 573 | - * @see Database::where() |
|
| 574 | - * @return object the current Database instance |
|
| 575 | - */ |
|
| 576 | - public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
|
| 577 | - return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - /** |
|
| 581 | - * Set the opened parenthesis for the complex SQL query |
|
| 582 | - * @param string $type the type of this grouped (NOT, etc.) |
|
| 583 | - * @param string $andOr the multiple conditions separator (AND, OR, etc.) |
|
| 584 | - * @return object the current Database instance |
|
| 585 | - */ |
|
| 586 | - public function groupStart($type = '', $andOr = ' AND'){ |
|
| 587 | - if (empty($this->where)){ |
|
| 588 | - $this->where = $type . ' ('; |
|
| 589 | - } |
|
| 590 | - else{ |
|
| 591 | - if(substr($this->where, -1) == '('){ |
|
| 592 | - $this->where .= $type . ' ('; |
|
| 593 | - } |
|
| 594 | - else{ |
|
| 595 | - $this->where .= $andOr . ' ' . $type . ' ('; |
|
| 596 | - } |
|
| 597 | - } |
|
| 598 | - return $this; |
|
| 599 | - } |
|
| 600 | - |
|
| 601 | - /** |
|
| 602 | - * Set the opened parenthesis for the complex SQL query using NOT type |
|
| 603 | - * @see Database::groupStart() |
|
| 604 | - * @return object the current Database instance |
|
| 605 | - */ |
|
| 606 | - public function notGroupStart(){ |
|
| 607 | - return $this->groupStart('NOT'); |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - /** |
|
| 611 | - * Set the opened parenthesis for the complex SQL query using OR for separator |
|
| 612 | - * @see Database::groupStart() |
|
| 613 | - * @return object the current Database instance |
|
| 614 | - */ |
|
| 615 | - public function orGroupStart(){ |
|
| 616 | - return $this->groupStart('', ' OR'); |
|
| 617 | - } |
|
| 618 | - |
|
| 619 | - /** |
|
| 620 | - * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type |
|
| 621 | - * @see Database::groupStart() |
|
| 622 | - * @return object the current Database instance |
|
| 623 | - */ |
|
| 624 | - public function orNotGroupStart(){ |
|
| 625 | - return $this->groupStart('NOT', ' OR'); |
|
| 626 | - } |
|
| 627 | - |
|
| 628 | - /** |
|
| 629 | - * Close the parenthesis for the grouped SQL |
|
| 630 | - * @return object the current Database instance |
|
| 631 | - */ |
|
| 632 | - public function groupEnd(){ |
|
| 633 | - $this->where .= ')'; |
|
| 634 | - return $this; |
|
| 635 | - } |
|
| 636 | - |
|
| 637 | - /** |
|
| 638 | - * Set the SQL WHERE CLAUSE statment for IN |
|
| 639 | - * @param string $field the field name for IN statment |
|
| 640 | - * @param array $keys the list of values used |
|
| 641 | - * @param string $type the condition separator type (NOT) |
|
| 642 | - * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 643 | - * @param boolean $escape whether to escape or not the values |
|
| 644 | - * @return object the current Database instance |
|
| 645 | - */ |
|
| 646 | - public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
|
| 647 | - $_keys = array(); |
|
| 648 | - foreach ($keys as $k => $v){ |
|
| 649 | - if(is_null($v)){ |
|
| 650 | - $v = ''; |
|
| 651 | - } |
|
| 652 | - $_keys[] = (is_numeric($v) ? $v : ($escape ? $this->escape($v) : $v)); |
|
| 653 | - } |
|
| 654 | - $keys = implode(', ', $_keys); |
|
| 655 | - $whereStr = $field . ' ' . $type . ' IN (' . $keys . ')'; |
|
| 656 | - $this->setWhereStr($whereStr, $andOr); |
|
| 657 | - return $this; |
|
| 658 | - } |
|
| 659 | - |
|
| 660 | - /** |
|
| 661 | - * Set the SQL WHERE CLAUSE statment for NOT IN with AND separator |
|
| 662 | - * @see Database::in() |
|
| 663 | - * @return object the current Database instance |
|
| 664 | - */ |
|
| 665 | - public function notIn($field, array $keys, $escape = true){ |
|
| 666 | - return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
|
| 667 | - } |
|
| 668 | - |
|
| 669 | - /** |
|
| 670 | - * Set the SQL WHERE CLAUSE statment for IN with OR separator |
|
| 671 | - * @see Database::in() |
|
| 672 | - * @return object the current Database instance |
|
| 673 | - */ |
|
| 674 | - public function orIn($field, array $keys, $escape = true){ |
|
| 675 | - return $this->in($field, $keys, '', 'OR', $escape); |
|
| 676 | - } |
|
| 677 | - |
|
| 678 | - /** |
|
| 679 | - * Set the SQL WHERE CLAUSE statment for NOT IN with OR separator |
|
| 680 | - * @see Database::in() |
|
| 681 | - * @return object the current Database instance |
|
| 682 | - */ |
|
| 683 | - public function orNotIn($field, array $keys, $escape = true){ |
|
| 684 | - return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
|
| 685 | - } |
|
| 686 | - |
|
| 687 | - /** |
|
| 688 | - * Set the SQL WHERE CLAUSE statment for BETWEEN |
|
| 689 | - * @param string $field the field used for the BETWEEN statment |
|
| 690 | - * @param mixed $value1 the BETWEEN begin value |
|
| 691 | - * @param mixed $value2 the BETWEEN end value |
|
| 692 | - * @param string $type the condition separator type (NOT) |
|
| 693 | - * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 694 | - * @param boolean $escape whether to escape or not the values |
|
| 695 | - * @return object the current Database instance |
|
| 696 | - */ |
|
| 697 | - public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
|
| 698 | - if(is_null($value1)){ |
|
| 699 | - $value1 = ''; |
|
| 700 | - } |
|
| 701 | - if(is_null($value2)){ |
|
| 702 | - $value2 = ''; |
|
| 703 | - } |
|
| 704 | - $whereStr = $field . ' ' . $type . ' BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
|
| 705 | - $this->setWhereStr($whereStr, $andOr); |
|
| 706 | - return $this; |
|
| 707 | - } |
|
| 708 | - |
|
| 709 | - /** |
|
| 710 | - * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and AND separator |
|
| 711 | - * @see Database::between() |
|
| 712 | - * @return object the current Database instance |
|
| 713 | - */ |
|
| 714 | - public function notBetween($field, $value1, $value2, $escape = true){ |
|
| 715 | - return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
|
| 716 | - } |
|
| 717 | - |
|
| 718 | - /** |
|
| 719 | - * Set the SQL WHERE CLAUSE statment for BETWEEN with OR separator |
|
| 720 | - * @see Database::between() |
|
| 721 | - * @return object the current Database instance |
|
| 722 | - */ |
|
| 723 | - public function orBetween($field, $value1, $value2, $escape = true){ |
|
| 724 | - return $this->between($field, $value1, $value2, '', 'OR', $escape); |
|
| 725 | - } |
|
| 726 | - |
|
| 727 | - /** |
|
| 728 | - * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and OR separator |
|
| 729 | - * @see Database::between() |
|
| 730 | - * @return object the current Database instance |
|
| 731 | - */ |
|
| 732 | - public function orNotBetween($field, $value1, $value2, $escape = true){ |
|
| 733 | - return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
|
| 734 | - } |
|
| 735 | - |
|
| 736 | - /** |
|
| 737 | - * Set the SQL WHERE CLAUSE statment for LIKE |
|
| 738 | - * @param string $field the field name used in LIKE statment |
|
| 739 | - * @param string $data the LIKE value for this field including the '%', and '_' part |
|
| 740 | - * @param string $type the condition separator type (NOT) |
|
| 741 | - * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 742 | - * @param boolean $escape whether to escape or not the values |
|
| 743 | - * @return object the current Database instance |
|
| 744 | - */ |
|
| 745 | - public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
|
| 746 | - if(empty($data)){ |
|
| 747 | - $data = ''; |
|
| 748 | - } |
|
| 749 | - $like = $escape ? $this->escape($data) : $data; |
|
| 750 | - if (empty($this->where)){ |
|
| 751 | - $this->where = $field . ' ' . $type . 'LIKE ' . $like; |
|
| 752 | - } |
|
| 753 | - else{ |
|
| 754 | - if(substr($this->where, -1) == '('){ |
|
| 755 | - $this->where = $this->where . ' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
| 756 | - } |
|
| 757 | - else{ |
|
| 758 | - $this->where = $this->where . ' '.$andOr.' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
| 759 | - } |
|
| 760 | - } |
|
| 761 | - return $this; |
|
| 762 | - } |
|
| 763 | - |
|
| 764 | - /** |
|
| 765 | - * Set the SQL WHERE CLAUSE statment for LIKE with OR separator |
|
| 766 | - * @see Database::like() |
|
| 767 | - * @return object the current Database instance |
|
| 768 | - */ |
|
| 769 | - public function orLike($field, $data, $escape = true){ |
|
| 770 | - return $this->like($field, $data, '', 'OR', $escape); |
|
| 771 | - } |
|
| 772 | - |
|
| 773 | - /** |
|
| 774 | - * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and AND separator |
|
| 775 | - * @see Database::like() |
|
| 776 | - * @return object the current Database instance |
|
| 777 | - */ |
|
| 778 | - public function notLike($field, $data, $escape = true){ |
|
| 779 | - return $this->like($field, $data, 'NOT ', 'AND', $escape); |
|
| 780 | - } |
|
| 781 | - |
|
| 782 | - /** |
|
| 783 | - * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and OR separator |
|
| 784 | - * @see Database::like() |
|
| 785 | - * @return object the current Database instance |
|
| 786 | - */ |
|
| 787 | - public function orNotLike($field, $data, $escape = true){ |
|
| 788 | - return $this->like($field, $data, 'NOT ', 'OR', $escape); |
|
| 789 | - } |
|
| 790 | - |
|
| 791 | - /** |
|
| 792 | - * Set the SQL LIMIT statment |
|
| 793 | - * @param int $limit the limit offset. If $limitEnd is null this will be the limit count |
|
| 794 | - * like LIMIT n; |
|
| 795 | - * @param int $limitEnd the limit count |
|
| 796 | - * @return object the current Database instance |
|
| 797 | - */ |
|
| 798 | - public function limit($limit, $limitEnd = null){ |
|
| 799 | - if(empty($limit)){ |
|
| 800 | - return; |
|
| 801 | - } |
|
| 802 | - if (! is_null($limitEnd)){ |
|
| 803 | - $this->limit = $limit . ', ' . $limitEnd; |
|
| 804 | - } |
|
| 805 | - else{ |
|
| 806 | - $this->limit = $limit; |
|
| 807 | - } |
|
| 808 | - return $this; |
|
| 809 | - } |
|
| 810 | - |
|
| 811 | - /** |
|
| 812 | - * Set the SQL ORDER BY CLAUSE statment |
|
| 813 | - * @param string $orderBy the field name used for order |
|
| 814 | - * @param string $orderDir the order direction (ASC or DESC) |
|
| 815 | - * @return object the current Database instance |
|
| 816 | - */ |
|
| 817 | - public function orderBy($orderBy, $orderDir = ' ASC'){ |
|
| 818 | - if (! empty($orderDir)){ |
|
| 819 | - $this->orderBy = ! $this->orderBy ? ($orderBy . ' ' . strtoupper($orderDir)) : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
| 820 | - } |
|
| 821 | - else{ |
|
| 822 | - if(stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
| 823 | - $this->orderBy = ! $this->orderBy ? $orderBy : $this->orderBy . ', ' . $orderBy; |
|
| 824 | - } |
|
| 825 | - else{ |
|
| 826 | - $this->orderBy = ! $this->orderBy ? ($orderBy . ' ASC') : $this->orderBy . ', ' . ($orderBy . ' ASC'); |
|
| 827 | - } |
|
| 828 | - } |
|
| 829 | - return $this; |
|
| 830 | - } |
|
| 831 | - |
|
| 832 | - /** |
|
| 833 | - * Set the SQL GROUP BY CLAUSE statment |
|
| 834 | - * @param string|array $field the field name used or array of field list |
|
| 835 | - * @return object the current Database instance |
|
| 836 | - */ |
|
| 837 | - public function groupBy($field){ |
|
| 838 | - if(is_array($field)){ |
|
| 839 | - $this->groupBy = implode(', ', $field); |
|
| 840 | - } |
|
| 841 | - else{ |
|
| 842 | - $this->groupBy = $field; |
|
| 843 | - } |
|
| 844 | - return $this; |
|
| 845 | - } |
|
| 846 | - |
|
| 847 | - /** |
|
| 848 | - * Set the SQL HAVING CLAUSE statment |
|
| 849 | - * @param string $field the field name used for HAVING statment |
|
| 850 | - * @param string|array $op the operator used or array |
|
| 851 | - * @param mixed $val the value for HAVING comparaison |
|
| 852 | - * @param boolean $escape whether to escape or not the values |
|
| 853 | - * @return object the current Database instance |
|
| 854 | - */ |
|
| 855 | - public function having($field, $op = null, $val = null, $escape = true){ |
|
| 856 | - if(is_array($op)){ |
|
| 857 | - $x = explode('?', $field); |
|
| 858 | - $w = ''; |
|
| 859 | - foreach($x as $k => $v){ |
|
| 860 | - if(!empty($v)){ |
|
| 861 | - if(isset($op[$k]) && is_null($op[$k])){ |
|
| 862 | - $op[$k] = ''; |
|
| 863 | - } |
|
| 864 | - $w .= $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
|
| 865 | - } |
|
| 866 | - } |
|
| 867 | - $this->having = $w; |
|
| 868 | - } |
|
| 869 | - else if (! in_array($op, $this->operatorList)){ |
|
| 870 | - if(is_null($op)){ |
|
| 871 | - $op = ''; |
|
| 872 | - } |
|
| 873 | - $this->having = $field . ' > ' . ($escape ? $this->escape($op) : $op); |
|
| 874 | - } |
|
| 875 | - else{ |
|
| 876 | - if(is_null($val)){ |
|
| 877 | - $val = ''; |
|
| 878 | - } |
|
| 879 | - $this->having = $field . ' ' . $op . ' ' . ($escape ? $this->escape($val) : $val); |
|
| 880 | - } |
|
| 881 | - return $this; |
|
| 882 | - } |
|
| 883 | - |
|
| 884 | - /** |
|
| 885 | - * Return the number of rows returned by the current query |
|
| 886 | - * @return int |
|
| 887 | - */ |
|
| 888 | - public function numRows(){ |
|
| 889 | - return $this->numRows; |
|
| 890 | - } |
|
| 891 | - |
|
| 892 | - /** |
|
| 893 | - * Return the last insert id value |
|
| 894 | - * @return mixed |
|
| 895 | - */ |
|
| 896 | - public function insertId(){ |
|
| 897 | - return $this->insertId; |
|
| 898 | - } |
|
| 899 | - |
|
| 900 | - /** |
|
| 901 | - * Show an error got from the current query (SQL command synthax error, database driver returned error, etc.) |
|
| 902 | - */ |
|
| 903 | - public function error(){ |
|
| 526 | + /** |
|
| 527 | + * Set the SQL WHERE CLAUSE statment |
|
| 528 | + * @param string|array $where the where field or array of field list |
|
| 529 | + * @param array|string $op the condition operator. If is null the default will be "=" |
|
| 530 | + * @param mixed $val the where value |
|
| 531 | + * @param string $type the type used for this where clause (NOT, etc.) |
|
| 532 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 533 | + * @param boolean $escape whether to escape or not the $val |
|
| 534 | + * @return object the current Database instance |
|
| 535 | + */ |
|
| 536 | + public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
|
| 537 | + $whereStr = ''; |
|
| 538 | + if (is_array($where)){ |
|
| 539 | + $whereStr = $this->getWhereStrIfIsArray($where, $type, $andOr, $escape); |
|
| 540 | + } |
|
| 541 | + else{ |
|
| 542 | + if(is_array($op)){ |
|
| 543 | + $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
|
| 544 | + } else { |
|
| 545 | + $whereStr = $this->getWhereStrForOperator($where, $op, $val, $type, $escape = true); |
|
| 546 | + } |
|
| 547 | + } |
|
| 548 | + $this->setWhereStr($whereStr, $andOr); |
|
| 549 | + return $this; |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + /** |
|
| 553 | + * Set the SQL WHERE CLAUSE statment using OR |
|
| 554 | + * @see Database::where() |
|
| 555 | + * @return object the current Database instance |
|
| 556 | + */ |
|
| 557 | + public function orWhere($where, $op = null, $val = null, $escape = true){ |
|
| 558 | + return $this->where($where, $op, $val, '', 'OR', $escape); |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + |
|
| 562 | + /** |
|
| 563 | + * Set the SQL WHERE CLAUSE statment using AND and NOT |
|
| 564 | + * @see Database::where() |
|
| 565 | + * @return object the current Database instance |
|
| 566 | + */ |
|
| 567 | + public function notWhere($where, $op = null, $val = null, $escape = true){ |
|
| 568 | + return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
|
| 569 | + } |
|
| 570 | + |
|
| 571 | + /** |
|
| 572 | + * Set the SQL WHERE CLAUSE statment using OR and NOT |
|
| 573 | + * @see Database::where() |
|
| 574 | + * @return object the current Database instance |
|
| 575 | + */ |
|
| 576 | + public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
|
| 577 | + return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + /** |
|
| 581 | + * Set the opened parenthesis for the complex SQL query |
|
| 582 | + * @param string $type the type of this grouped (NOT, etc.) |
|
| 583 | + * @param string $andOr the multiple conditions separator (AND, OR, etc.) |
|
| 584 | + * @return object the current Database instance |
|
| 585 | + */ |
|
| 586 | + public function groupStart($type = '', $andOr = ' AND'){ |
|
| 587 | + if (empty($this->where)){ |
|
| 588 | + $this->where = $type . ' ('; |
|
| 589 | + } |
|
| 590 | + else{ |
|
| 591 | + if(substr($this->where, -1) == '('){ |
|
| 592 | + $this->where .= $type . ' ('; |
|
| 593 | + } |
|
| 594 | + else{ |
|
| 595 | + $this->where .= $andOr . ' ' . $type . ' ('; |
|
| 596 | + } |
|
| 597 | + } |
|
| 598 | + return $this; |
|
| 599 | + } |
|
| 600 | + |
|
| 601 | + /** |
|
| 602 | + * Set the opened parenthesis for the complex SQL query using NOT type |
|
| 603 | + * @see Database::groupStart() |
|
| 604 | + * @return object the current Database instance |
|
| 605 | + */ |
|
| 606 | + public function notGroupStart(){ |
|
| 607 | + return $this->groupStart('NOT'); |
|
| 608 | + } |
|
| 609 | + |
|
| 610 | + /** |
|
| 611 | + * Set the opened parenthesis for the complex SQL query using OR for separator |
|
| 612 | + * @see Database::groupStart() |
|
| 613 | + * @return object the current Database instance |
|
| 614 | + */ |
|
| 615 | + public function orGroupStart(){ |
|
| 616 | + return $this->groupStart('', ' OR'); |
|
| 617 | + } |
|
| 618 | + |
|
| 619 | + /** |
|
| 620 | + * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type |
|
| 621 | + * @see Database::groupStart() |
|
| 622 | + * @return object the current Database instance |
|
| 623 | + */ |
|
| 624 | + public function orNotGroupStart(){ |
|
| 625 | + return $this->groupStart('NOT', ' OR'); |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + /** |
|
| 629 | + * Close the parenthesis for the grouped SQL |
|
| 630 | + * @return object the current Database instance |
|
| 631 | + */ |
|
| 632 | + public function groupEnd(){ |
|
| 633 | + $this->where .= ')'; |
|
| 634 | + return $this; |
|
| 635 | + } |
|
| 636 | + |
|
| 637 | + /** |
|
| 638 | + * Set the SQL WHERE CLAUSE statment for IN |
|
| 639 | + * @param string $field the field name for IN statment |
|
| 640 | + * @param array $keys the list of values used |
|
| 641 | + * @param string $type the condition separator type (NOT) |
|
| 642 | + * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 643 | + * @param boolean $escape whether to escape or not the values |
|
| 644 | + * @return object the current Database instance |
|
| 645 | + */ |
|
| 646 | + public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
|
| 647 | + $_keys = array(); |
|
| 648 | + foreach ($keys as $k => $v){ |
|
| 649 | + if(is_null($v)){ |
|
| 650 | + $v = ''; |
|
| 651 | + } |
|
| 652 | + $_keys[] = (is_numeric($v) ? $v : ($escape ? $this->escape($v) : $v)); |
|
| 653 | + } |
|
| 654 | + $keys = implode(', ', $_keys); |
|
| 655 | + $whereStr = $field . ' ' . $type . ' IN (' . $keys . ')'; |
|
| 656 | + $this->setWhereStr($whereStr, $andOr); |
|
| 657 | + return $this; |
|
| 658 | + } |
|
| 659 | + |
|
| 660 | + /** |
|
| 661 | + * Set the SQL WHERE CLAUSE statment for NOT IN with AND separator |
|
| 662 | + * @see Database::in() |
|
| 663 | + * @return object the current Database instance |
|
| 664 | + */ |
|
| 665 | + public function notIn($field, array $keys, $escape = true){ |
|
| 666 | + return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
|
| 667 | + } |
|
| 668 | + |
|
| 669 | + /** |
|
| 670 | + * Set the SQL WHERE CLAUSE statment for IN with OR separator |
|
| 671 | + * @see Database::in() |
|
| 672 | + * @return object the current Database instance |
|
| 673 | + */ |
|
| 674 | + public function orIn($field, array $keys, $escape = true){ |
|
| 675 | + return $this->in($field, $keys, '', 'OR', $escape); |
|
| 676 | + } |
|
| 677 | + |
|
| 678 | + /** |
|
| 679 | + * Set the SQL WHERE CLAUSE statment for NOT IN with OR separator |
|
| 680 | + * @see Database::in() |
|
| 681 | + * @return object the current Database instance |
|
| 682 | + */ |
|
| 683 | + public function orNotIn($field, array $keys, $escape = true){ |
|
| 684 | + return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
|
| 685 | + } |
|
| 686 | + |
|
| 687 | + /** |
|
| 688 | + * Set the SQL WHERE CLAUSE statment for BETWEEN |
|
| 689 | + * @param string $field the field used for the BETWEEN statment |
|
| 690 | + * @param mixed $value1 the BETWEEN begin value |
|
| 691 | + * @param mixed $value2 the BETWEEN end value |
|
| 692 | + * @param string $type the condition separator type (NOT) |
|
| 693 | + * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 694 | + * @param boolean $escape whether to escape or not the values |
|
| 695 | + * @return object the current Database instance |
|
| 696 | + */ |
|
| 697 | + public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
|
| 698 | + if(is_null($value1)){ |
|
| 699 | + $value1 = ''; |
|
| 700 | + } |
|
| 701 | + if(is_null($value2)){ |
|
| 702 | + $value2 = ''; |
|
| 703 | + } |
|
| 704 | + $whereStr = $field . ' ' . $type . ' BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
|
| 705 | + $this->setWhereStr($whereStr, $andOr); |
|
| 706 | + return $this; |
|
| 707 | + } |
|
| 708 | + |
|
| 709 | + /** |
|
| 710 | + * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and AND separator |
|
| 711 | + * @see Database::between() |
|
| 712 | + * @return object the current Database instance |
|
| 713 | + */ |
|
| 714 | + public function notBetween($field, $value1, $value2, $escape = true){ |
|
| 715 | + return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
|
| 716 | + } |
|
| 717 | + |
|
| 718 | + /** |
|
| 719 | + * Set the SQL WHERE CLAUSE statment for BETWEEN with OR separator |
|
| 720 | + * @see Database::between() |
|
| 721 | + * @return object the current Database instance |
|
| 722 | + */ |
|
| 723 | + public function orBetween($field, $value1, $value2, $escape = true){ |
|
| 724 | + return $this->between($field, $value1, $value2, '', 'OR', $escape); |
|
| 725 | + } |
|
| 726 | + |
|
| 727 | + /** |
|
| 728 | + * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and OR separator |
|
| 729 | + * @see Database::between() |
|
| 730 | + * @return object the current Database instance |
|
| 731 | + */ |
|
| 732 | + public function orNotBetween($field, $value1, $value2, $escape = true){ |
|
| 733 | + return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
|
| 734 | + } |
|
| 735 | + |
|
| 736 | + /** |
|
| 737 | + * Set the SQL WHERE CLAUSE statment for LIKE |
|
| 738 | + * @param string $field the field name used in LIKE statment |
|
| 739 | + * @param string $data the LIKE value for this field including the '%', and '_' part |
|
| 740 | + * @param string $type the condition separator type (NOT) |
|
| 741 | + * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 742 | + * @param boolean $escape whether to escape or not the values |
|
| 743 | + * @return object the current Database instance |
|
| 744 | + */ |
|
| 745 | + public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
|
| 746 | + if(empty($data)){ |
|
| 747 | + $data = ''; |
|
| 748 | + } |
|
| 749 | + $like = $escape ? $this->escape($data) : $data; |
|
| 750 | + if (empty($this->where)){ |
|
| 751 | + $this->where = $field . ' ' . $type . 'LIKE ' . $like; |
|
| 752 | + } |
|
| 753 | + else{ |
|
| 754 | + if(substr($this->where, -1) == '('){ |
|
| 755 | + $this->where = $this->where . ' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
| 756 | + } |
|
| 757 | + else{ |
|
| 758 | + $this->where = $this->where . ' '.$andOr.' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
| 759 | + } |
|
| 760 | + } |
|
| 761 | + return $this; |
|
| 762 | + } |
|
| 763 | + |
|
| 764 | + /** |
|
| 765 | + * Set the SQL WHERE CLAUSE statment for LIKE with OR separator |
|
| 766 | + * @see Database::like() |
|
| 767 | + * @return object the current Database instance |
|
| 768 | + */ |
|
| 769 | + public function orLike($field, $data, $escape = true){ |
|
| 770 | + return $this->like($field, $data, '', 'OR', $escape); |
|
| 771 | + } |
|
| 772 | + |
|
| 773 | + /** |
|
| 774 | + * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and AND separator |
|
| 775 | + * @see Database::like() |
|
| 776 | + * @return object the current Database instance |
|
| 777 | + */ |
|
| 778 | + public function notLike($field, $data, $escape = true){ |
|
| 779 | + return $this->like($field, $data, 'NOT ', 'AND', $escape); |
|
| 780 | + } |
|
| 781 | + |
|
| 782 | + /** |
|
| 783 | + * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and OR separator |
|
| 784 | + * @see Database::like() |
|
| 785 | + * @return object the current Database instance |
|
| 786 | + */ |
|
| 787 | + public function orNotLike($field, $data, $escape = true){ |
|
| 788 | + return $this->like($field, $data, 'NOT ', 'OR', $escape); |
|
| 789 | + } |
|
| 790 | + |
|
| 791 | + /** |
|
| 792 | + * Set the SQL LIMIT statment |
|
| 793 | + * @param int $limit the limit offset. If $limitEnd is null this will be the limit count |
|
| 794 | + * like LIMIT n; |
|
| 795 | + * @param int $limitEnd the limit count |
|
| 796 | + * @return object the current Database instance |
|
| 797 | + */ |
|
| 798 | + public function limit($limit, $limitEnd = null){ |
|
| 799 | + if(empty($limit)){ |
|
| 800 | + return; |
|
| 801 | + } |
|
| 802 | + if (! is_null($limitEnd)){ |
|
| 803 | + $this->limit = $limit . ', ' . $limitEnd; |
|
| 804 | + } |
|
| 805 | + else{ |
|
| 806 | + $this->limit = $limit; |
|
| 807 | + } |
|
| 808 | + return $this; |
|
| 809 | + } |
|
| 810 | + |
|
| 811 | + /** |
|
| 812 | + * Set the SQL ORDER BY CLAUSE statment |
|
| 813 | + * @param string $orderBy the field name used for order |
|
| 814 | + * @param string $orderDir the order direction (ASC or DESC) |
|
| 815 | + * @return object the current Database instance |
|
| 816 | + */ |
|
| 817 | + public function orderBy($orderBy, $orderDir = ' ASC'){ |
|
| 818 | + if (! empty($orderDir)){ |
|
| 819 | + $this->orderBy = ! $this->orderBy ? ($orderBy . ' ' . strtoupper($orderDir)) : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
| 820 | + } |
|
| 821 | + else{ |
|
| 822 | + if(stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
| 823 | + $this->orderBy = ! $this->orderBy ? $orderBy : $this->orderBy . ', ' . $orderBy; |
|
| 824 | + } |
|
| 825 | + else{ |
|
| 826 | + $this->orderBy = ! $this->orderBy ? ($orderBy . ' ASC') : $this->orderBy . ', ' . ($orderBy . ' ASC'); |
|
| 827 | + } |
|
| 828 | + } |
|
| 829 | + return $this; |
|
| 830 | + } |
|
| 831 | + |
|
| 832 | + /** |
|
| 833 | + * Set the SQL GROUP BY CLAUSE statment |
|
| 834 | + * @param string|array $field the field name used or array of field list |
|
| 835 | + * @return object the current Database instance |
|
| 836 | + */ |
|
| 837 | + public function groupBy($field){ |
|
| 838 | + if(is_array($field)){ |
|
| 839 | + $this->groupBy = implode(', ', $field); |
|
| 840 | + } |
|
| 841 | + else{ |
|
| 842 | + $this->groupBy = $field; |
|
| 843 | + } |
|
| 844 | + return $this; |
|
| 845 | + } |
|
| 846 | + |
|
| 847 | + /** |
|
| 848 | + * Set the SQL HAVING CLAUSE statment |
|
| 849 | + * @param string $field the field name used for HAVING statment |
|
| 850 | + * @param string|array $op the operator used or array |
|
| 851 | + * @param mixed $val the value for HAVING comparaison |
|
| 852 | + * @param boolean $escape whether to escape or not the values |
|
| 853 | + * @return object the current Database instance |
|
| 854 | + */ |
|
| 855 | + public function having($field, $op = null, $val = null, $escape = true){ |
|
| 856 | + if(is_array($op)){ |
|
| 857 | + $x = explode('?', $field); |
|
| 858 | + $w = ''; |
|
| 859 | + foreach($x as $k => $v){ |
|
| 860 | + if(!empty($v)){ |
|
| 861 | + if(isset($op[$k]) && is_null($op[$k])){ |
|
| 862 | + $op[$k] = ''; |
|
| 863 | + } |
|
| 864 | + $w .= $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
|
| 865 | + } |
|
| 866 | + } |
|
| 867 | + $this->having = $w; |
|
| 868 | + } |
|
| 869 | + else if (! in_array($op, $this->operatorList)){ |
|
| 870 | + if(is_null($op)){ |
|
| 871 | + $op = ''; |
|
| 872 | + } |
|
| 873 | + $this->having = $field . ' > ' . ($escape ? $this->escape($op) : $op); |
|
| 874 | + } |
|
| 875 | + else{ |
|
| 876 | + if(is_null($val)){ |
|
| 877 | + $val = ''; |
|
| 878 | + } |
|
| 879 | + $this->having = $field . ' ' . $op . ' ' . ($escape ? $this->escape($val) : $val); |
|
| 880 | + } |
|
| 881 | + return $this; |
|
| 882 | + } |
|
| 883 | + |
|
| 884 | + /** |
|
| 885 | + * Return the number of rows returned by the current query |
|
| 886 | + * @return int |
|
| 887 | + */ |
|
| 888 | + public function numRows(){ |
|
| 889 | + return $this->numRows; |
|
| 890 | + } |
|
| 891 | + |
|
| 892 | + /** |
|
| 893 | + * Return the last insert id value |
|
| 894 | + * @return mixed |
|
| 895 | + */ |
|
| 896 | + public function insertId(){ |
|
| 897 | + return $this->insertId; |
|
| 898 | + } |
|
| 899 | + |
|
| 900 | + /** |
|
| 901 | + * Show an error got from the current query (SQL command synthax error, database driver returned error, etc.) |
|
| 902 | + */ |
|
| 903 | + public function error(){ |
|
| 904 | 904 | if($this->error){ |
| 905 | 905 | show_error('Query: "' . $this->query . '" Error: ' . $this->error, 'Database Error'); |
| 906 | 906 | } |
| 907 | - } |
|
| 908 | - |
|
| 909 | - /** |
|
| 910 | - * Get the result of one record rows returned by the current query |
|
| 911 | - * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 912 | - * If is string will determine the result type "array" or "object" |
|
| 913 | - * @return mixed the query SQL string or the record result |
|
| 914 | - */ |
|
| 915 | - public function get($returnSQLQueryOrResultType = false){ |
|
| 916 | - $this->limit = 1; |
|
| 917 | - $query = $this->getAll(true); |
|
| 918 | - if($returnSQLQueryOrResultType === true){ |
|
| 919 | - return $query; |
|
| 920 | - } |
|
| 921 | - else{ |
|
| 922 | - return $this->query( $query, false, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 923 | - } |
|
| 924 | - } |
|
| 925 | - |
|
| 926 | - /** |
|
| 927 | - * Get the result of record rows list returned by the current query |
|
| 928 | - * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 929 | - * If is string will determine the result type "array" or "object" |
|
| 930 | - * @return mixed the query SQL string or the record result |
|
| 931 | - */ |
|
| 932 | - public function getAll($returnSQLQueryOrResultType = false){ |
|
| 933 | - $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
|
| 934 | - if (! empty($this->join)){ |
|
| 935 | - $query .= $this->join; |
|
| 936 | - } |
|
| 907 | + } |
|
| 908 | + |
|
| 909 | + /** |
|
| 910 | + * Get the result of one record rows returned by the current query |
|
| 911 | + * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 912 | + * If is string will determine the result type "array" or "object" |
|
| 913 | + * @return mixed the query SQL string or the record result |
|
| 914 | + */ |
|
| 915 | + public function get($returnSQLQueryOrResultType = false){ |
|
| 916 | + $this->limit = 1; |
|
| 917 | + $query = $this->getAll(true); |
|
| 918 | + if($returnSQLQueryOrResultType === true){ |
|
| 919 | + return $query; |
|
| 920 | + } |
|
| 921 | + else{ |
|
| 922 | + return $this->query( $query, false, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 923 | + } |
|
| 924 | + } |
|
| 925 | + |
|
| 926 | + /** |
|
| 927 | + * Get the result of record rows list returned by the current query |
|
| 928 | + * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 929 | + * If is string will determine the result type "array" or "object" |
|
| 930 | + * @return mixed the query SQL string or the record result |
|
| 931 | + */ |
|
| 932 | + public function getAll($returnSQLQueryOrResultType = false){ |
|
| 933 | + $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
|
| 934 | + if (! empty($this->join)){ |
|
| 935 | + $query .= $this->join; |
|
| 936 | + } |
|
| 937 | 937 | |
| 938 | - if (! empty($this->where)){ |
|
| 939 | - $query .= ' WHERE ' . $this->where; |
|
| 940 | - } |
|
| 938 | + if (! empty($this->where)){ |
|
| 939 | + $query .= ' WHERE ' . $this->where; |
|
| 940 | + } |
|
| 941 | 941 | |
| 942 | - if (! empty($this->groupBy)){ |
|
| 943 | - $query .= ' GROUP BY ' . $this->groupBy; |
|
| 944 | - } |
|
| 942 | + if (! empty($this->groupBy)){ |
|
| 943 | + $query .= ' GROUP BY ' . $this->groupBy; |
|
| 944 | + } |
|
| 945 | 945 | |
| 946 | - if (! empty($this->having)){ |
|
| 947 | - $query .= ' HAVING ' . $this->having; |
|
| 948 | - } |
|
| 946 | + if (! empty($this->having)){ |
|
| 947 | + $query .= ' HAVING ' . $this->having; |
|
| 948 | + } |
|
| 949 | 949 | |
| 950 | - if (! empty($this->orderBy)){ |
|
| 951 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
| 952 | - } |
|
| 950 | + if (! empty($this->orderBy)){ |
|
| 951 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
| 952 | + } |
|
| 953 | 953 | |
| 954 | - if(! empty($this->limit)){ |
|
| 955 | - $query .= ' LIMIT ' . $this->limit; |
|
| 956 | - } |
|
| 954 | + if(! empty($this->limit)){ |
|
| 955 | + $query .= ' LIMIT ' . $this->limit; |
|
| 956 | + } |
|
| 957 | 957 | |
| 958 | 958 | if($returnSQLQueryOrResultType === true){ |
| 959 | - return $query; |
|
| 960 | - } |
|
| 961 | - else{ |
|
| 962 | - return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 963 | - } |
|
| 964 | - } |
|
| 965 | - |
|
| 966 | - /** |
|
| 967 | - * Insert new record in the database |
|
| 968 | - * @param array $data the record data if is empty will use the $this->data array. |
|
| 969 | - * @param boolean $escape whether to escape or not the values |
|
| 970 | - * @return mixed the insert id of the new record or null |
|
| 971 | - */ |
|
| 972 | - public function insert($data = array(), $escape = true){ |
|
| 973 | - $column = array(); |
|
| 974 | - $val = array(); |
|
| 975 | - if(empty($data) && $this->getData()){ |
|
| 976 | - $columns = array_keys($this->getData()); |
|
| 977 | - $column = implode(',', $columns); |
|
| 978 | - $val = implode(', ', $this->getData()); |
|
| 979 | - } |
|
| 980 | - else{ |
|
| 981 | - $columns = array_keys($data); |
|
| 982 | - $column = implode(',', $columns); |
|
| 983 | - $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
|
| 984 | - } |
|
| 985 | - |
|
| 986 | - $query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
|
| 987 | - $query = $this->query($query); |
|
| 988 | - |
|
| 989 | - if ($query){ |
|
| 990 | - if(! $this->pdo){ |
|
| 991 | - $this->connect(); |
|
| 992 | - } |
|
| 993 | - $this->insertId = $this->pdo->lastInsertId(); |
|
| 994 | - return $this->insertId(); |
|
| 995 | - } |
|
| 996 | - else{ |
|
| 959 | + return $query; |
|
| 960 | + } |
|
| 961 | + else{ |
|
| 962 | + return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 963 | + } |
|
| 964 | + } |
|
| 965 | + |
|
| 966 | + /** |
|
| 967 | + * Insert new record in the database |
|
| 968 | + * @param array $data the record data if is empty will use the $this->data array. |
|
| 969 | + * @param boolean $escape whether to escape or not the values |
|
| 970 | + * @return mixed the insert id of the new record or null |
|
| 971 | + */ |
|
| 972 | + public function insert($data = array(), $escape = true){ |
|
| 973 | + $column = array(); |
|
| 974 | + $val = array(); |
|
| 975 | + if(empty($data) && $this->getData()){ |
|
| 976 | + $columns = array_keys($this->getData()); |
|
| 977 | + $column = implode(',', $columns); |
|
| 978 | + $val = implode(', ', $this->getData()); |
|
| 979 | + } |
|
| 980 | + else{ |
|
| 981 | + $columns = array_keys($data); |
|
| 982 | + $column = implode(',', $columns); |
|
| 983 | + $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
|
| 984 | + } |
|
| 985 | + |
|
| 986 | + $query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
|
| 987 | + $query = $this->query($query); |
|
| 988 | + |
|
| 989 | + if ($query){ |
|
| 990 | + if(! $this->pdo){ |
|
| 991 | + $this->connect(); |
|
| 992 | + } |
|
| 993 | + $this->insertId = $this->pdo->lastInsertId(); |
|
| 994 | + return $this->insertId(); |
|
| 995 | + } |
|
| 996 | + else{ |
|
| 997 | 997 | return false; |
| 998 | - } |
|
| 999 | - } |
|
| 1000 | - |
|
| 1001 | - /** |
|
| 1002 | - * Update record in the database |
|
| 1003 | - * @param array $data the record data if is empty will use the $this->data array. |
|
| 1004 | - * @param boolean $escape whether to escape or not the values |
|
| 1005 | - * @return mixed the update status |
|
| 1006 | - */ |
|
| 1007 | - public function update($data = array(), $escape = true){ |
|
| 1008 | - $query = 'UPDATE ' . $this->from . ' SET '; |
|
| 1009 | - $values = array(); |
|
| 1010 | - if(empty($data) && $this->getData()){ |
|
| 1011 | - foreach ($this->getData() as $column => $val){ |
|
| 1012 | - $values[] = $column . ' = ' . $val; |
|
| 1013 | - } |
|
| 1014 | - } |
|
| 1015 | - else{ |
|
| 1016 | - foreach ($data as $column => $val){ |
|
| 1017 | - $values[] = $column . '=' . ($escape ? $this->escape($val) : $val); |
|
| 1018 | - } |
|
| 1019 | - } |
|
| 1020 | - $query .= implode(', ', $values); |
|
| 1021 | - if (! empty($this->where)){ |
|
| 1022 | - $query .= ' WHERE ' . $this->where; |
|
| 1023 | - } |
|
| 1024 | - |
|
| 1025 | - if (! empty($this->orderBy)){ |
|
| 1026 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
| 1027 | - } |
|
| 1028 | - |
|
| 1029 | - if (! empty($this->limit)){ |
|
| 1030 | - $query .= ' LIMIT ' . $this->limit; |
|
| 1031 | - } |
|
| 1032 | - return $this->query($query); |
|
| 1033 | - } |
|
| 1034 | - |
|
| 1035 | - /** |
|
| 1036 | - * Delete the record in database |
|
| 1037 | - * @return mixed the delete status |
|
| 1038 | - */ |
|
| 1039 | - public function delete(){ |
|
| 1040 | - $query = 'DELETE FROM ' . $this->from; |
|
| 1041 | - |
|
| 1042 | - if (! empty($this->where)){ |
|
| 1043 | - $query .= ' WHERE ' . $this->where; |
|
| 1044 | - } |
|
| 1045 | - |
|
| 1046 | - if (! empty($this->orderBy)){ |
|
| 1047 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
| 1048 | - } |
|
| 1049 | - |
|
| 1050 | - if (! empty($this->limit)){ |
|
| 1051 | - $query .= ' LIMIT ' . $this->limit; |
|
| 1052 | - } |
|
| 1053 | - |
|
| 1054 | - if($query == 'DELETE FROM ' . $this->from && $this->config['driver'] != 'sqlite'){ |
|
| 1055 | - $query = 'TRUNCATE TABLE ' . $this->from; |
|
| 1056 | - } |
|
| 1057 | - return $this->query($query); |
|
| 1058 | - } |
|
| 1059 | - |
|
| 1060 | - /** |
|
| 1061 | - * Execute an SQL query |
|
| 1062 | - * @param string $query the query SQL string |
|
| 1063 | - * @param boolean $all whether to return all record or not |
|
| 1064 | - * @param boolean $array return the result as array |
|
| 1065 | - * @return mixed the query result |
|
| 1066 | - */ |
|
| 1067 | - public function query($query, $all = true, $array = false){ |
|
| 1068 | - $this->reset(); |
|
| 1069 | - if(is_array($all)){ |
|
| 1070 | - $x = explode('?', $query); |
|
| 1071 | - $q = ''; |
|
| 1072 | - foreach($x as $k => $v){ |
|
| 1073 | - if(! empty($v)){ |
|
| 1074 | - $q .= $v . (isset($all[$k]) ? $this->escape($all[$k]) : ''); |
|
| 1075 | - } |
|
| 1076 | - } |
|
| 1077 | - $query = $q; |
|
| 1078 | - } |
|
| 1079 | - |
|
| 1080 | - $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
| 1081 | - $sqlSELECTQuery = stristr($this->query, 'SELECT'); |
|
| 1082 | - $this->logger->info('Execute SQL query ['.$this->query.'], return type: ' . ($array?'ARRAY':'OBJECT') .', return as list: ' . ($all ? 'YES':'NO')); |
|
| 1083 | - //cache expire time |
|
| 998 | + } |
|
| 999 | + } |
|
| 1000 | + |
|
| 1001 | + /** |
|
| 1002 | + * Update record in the database |
|
| 1003 | + * @param array $data the record data if is empty will use the $this->data array. |
|
| 1004 | + * @param boolean $escape whether to escape or not the values |
|
| 1005 | + * @return mixed the update status |
|
| 1006 | + */ |
|
| 1007 | + public function update($data = array(), $escape = true){ |
|
| 1008 | + $query = 'UPDATE ' . $this->from . ' SET '; |
|
| 1009 | + $values = array(); |
|
| 1010 | + if(empty($data) && $this->getData()){ |
|
| 1011 | + foreach ($this->getData() as $column => $val){ |
|
| 1012 | + $values[] = $column . ' = ' . $val; |
|
| 1013 | + } |
|
| 1014 | + } |
|
| 1015 | + else{ |
|
| 1016 | + foreach ($data as $column => $val){ |
|
| 1017 | + $values[] = $column . '=' . ($escape ? $this->escape($val) : $val); |
|
| 1018 | + } |
|
| 1019 | + } |
|
| 1020 | + $query .= implode(', ', $values); |
|
| 1021 | + if (! empty($this->where)){ |
|
| 1022 | + $query .= ' WHERE ' . $this->where; |
|
| 1023 | + } |
|
| 1024 | + |
|
| 1025 | + if (! empty($this->orderBy)){ |
|
| 1026 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
| 1027 | + } |
|
| 1028 | + |
|
| 1029 | + if (! empty($this->limit)){ |
|
| 1030 | + $query .= ' LIMIT ' . $this->limit; |
|
| 1031 | + } |
|
| 1032 | + return $this->query($query); |
|
| 1033 | + } |
|
| 1034 | + |
|
| 1035 | + /** |
|
| 1036 | + * Delete the record in database |
|
| 1037 | + * @return mixed the delete status |
|
| 1038 | + */ |
|
| 1039 | + public function delete(){ |
|
| 1040 | + $query = 'DELETE FROM ' . $this->from; |
|
| 1041 | + |
|
| 1042 | + if (! empty($this->where)){ |
|
| 1043 | + $query .= ' WHERE ' . $this->where; |
|
| 1044 | + } |
|
| 1045 | + |
|
| 1046 | + if (! empty($this->orderBy)){ |
|
| 1047 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
| 1048 | + } |
|
| 1049 | + |
|
| 1050 | + if (! empty($this->limit)){ |
|
| 1051 | + $query .= ' LIMIT ' . $this->limit; |
|
| 1052 | + } |
|
| 1053 | + |
|
| 1054 | + if($query == 'DELETE FROM ' . $this->from && $this->config['driver'] != 'sqlite'){ |
|
| 1055 | + $query = 'TRUNCATE TABLE ' . $this->from; |
|
| 1056 | + } |
|
| 1057 | + return $this->query($query); |
|
| 1058 | + } |
|
| 1059 | + |
|
| 1060 | + /** |
|
| 1061 | + * Execute an SQL query |
|
| 1062 | + * @param string $query the query SQL string |
|
| 1063 | + * @param boolean $all whether to return all record or not |
|
| 1064 | + * @param boolean $array return the result as array |
|
| 1065 | + * @return mixed the query result |
|
| 1066 | + */ |
|
| 1067 | + public function query($query, $all = true, $array = false){ |
|
| 1068 | + $this->reset(); |
|
| 1069 | + if(is_array($all)){ |
|
| 1070 | + $x = explode('?', $query); |
|
| 1071 | + $q = ''; |
|
| 1072 | + foreach($x as $k => $v){ |
|
| 1073 | + if(! empty($v)){ |
|
| 1074 | + $q .= $v . (isset($all[$k]) ? $this->escape($all[$k]) : ''); |
|
| 1075 | + } |
|
| 1076 | + } |
|
| 1077 | + $query = $q; |
|
| 1078 | + } |
|
| 1079 | + |
|
| 1080 | + $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
| 1081 | + $sqlSELECTQuery = stristr($this->query, 'SELECT'); |
|
| 1082 | + $this->logger->info('Execute SQL query ['.$this->query.'], return type: ' . ($array?'ARRAY':'OBJECT') .', return as list: ' . ($all ? 'YES':'NO')); |
|
| 1083 | + //cache expire time |
|
| 1084 | 1084 | $cacheExpire = $this->temporaryCacheTtl; |
| 1085 | 1085 | |
| 1086 | 1086 | //return to the initial cache time |
| 1087 | 1087 | $this->temporaryCacheTtl = $this->cacheTtl; |
| 1088 | 1088 | |
| 1089 | 1089 | //config for cache |
| 1090 | - $cacheEnable = get_config('cache_enable'); |
|
| 1090 | + $cacheEnable = get_config('cache_enable'); |
|
| 1091 | 1091 | |
| 1092 | 1092 | //the database cache content |
| 1093 | - $cacheContent = null; |
|
| 1093 | + $cacheContent = null; |
|
| 1094 | 1094 | |
| 1095 | 1095 | //this database query cache key |
| 1096 | - $cacheKey = null; |
|
| 1096 | + $cacheKey = null; |
|
| 1097 | 1097 | |
| 1098 | 1098 | //the cache manager instance |
| 1099 | - $cacheInstance = null; |
|
| 1099 | + $cacheInstance = null; |
|
| 1100 | 1100 | |
| 1101 | 1101 | //if can use cache feature for this query |
| 1102 | 1102 | $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
| 1103 | 1103 | |
| 1104 | - if ($dbCacheStatus && $sqlSELECTQuery){ |
|
| 1105 | - $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
| 1106 | - $cacheKey = md5($query . $all . $array); |
|
| 1107 | - if(is_object($this->cacheInstance)){ |
|
| 1108 | - $cacheInstance = $this->cacheInstance; |
|
| 1109 | - } |
|
| 1110 | - else{ |
|
| 1111 | - $obj = & get_instance(); |
|
| 1112 | - $cacheInstance = $obj->cache; |
|
| 1113 | - } |
|
| 1114 | - $cacheContent = $cacheInstance->get($cacheKey); |
|
| 1115 | - } |
|
| 1116 | - else{ |
|
| 1104 | + if ($dbCacheStatus && $sqlSELECTQuery){ |
|
| 1105 | + $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
| 1106 | + $cacheKey = md5($query . $all . $array); |
|
| 1107 | + if(is_object($this->cacheInstance)){ |
|
| 1108 | + $cacheInstance = $this->cacheInstance; |
|
| 1109 | + } |
|
| 1110 | + else{ |
|
| 1111 | + $obj = & get_instance(); |
|
| 1112 | + $cacheInstance = $obj->cache; |
|
| 1113 | + } |
|
| 1114 | + $cacheContent = $cacheInstance->get($cacheKey); |
|
| 1115 | + } |
|
| 1116 | + else{ |
|
| 1117 | 1117 | $this->logger->info('The cache is not enabled for this query or is not the SELECT query, get the result directly from real database'); |
| 1118 | - } |
|
| 1118 | + } |
|
| 1119 | 1119 | |
| 1120 | - if(! $this->pdo){ |
|
| 1121 | - $this->connect(); |
|
| 1122 | - } |
|
| 1120 | + if(! $this->pdo){ |
|
| 1121 | + $this->connect(); |
|
| 1122 | + } |
|
| 1123 | 1123 | |
| 1124 | - if (! $cacheContent && $sqlSELECTQuery){ |
|
| 1125 | - //for database query execution time |
|
| 1126 | - $benchmarkMarkerKey = md5($query . $all . $array); |
|
| 1127 | - $bench = null; |
|
| 1128 | - if(is_object($this->benchmarkInstance)){ |
|
| 1129 | - $bench = $this->benchmarkInstance; |
|
| 1130 | - } |
|
| 1131 | - else{ |
|
| 1132 | - $obj = & get_instance(); |
|
| 1133 | - $bench = $obj->benchmark; |
|
| 1134 | - } |
|
| 1135 | - $bench->mark('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')'); |
|
| 1136 | - //Now execute the query |
|
| 1137 | - $sqlQuery = $this->pdo->query($this->query); |
|
| 1124 | + if (! $cacheContent && $sqlSELECTQuery){ |
|
| 1125 | + //for database query execution time |
|
| 1126 | + $benchmarkMarkerKey = md5($query . $all . $array); |
|
| 1127 | + $bench = null; |
|
| 1128 | + if(is_object($this->benchmarkInstance)){ |
|
| 1129 | + $bench = $this->benchmarkInstance; |
|
| 1130 | + } |
|
| 1131 | + else{ |
|
| 1132 | + $obj = & get_instance(); |
|
| 1133 | + $bench = $obj->benchmark; |
|
| 1134 | + } |
|
| 1135 | + $bench->mark('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')'); |
|
| 1136 | + //Now execute the query |
|
| 1137 | + $sqlQuery = $this->pdo->query($this->query); |
|
| 1138 | 1138 | |
| 1139 | - //get response time for this query |
|
| 1140 | - $responseTime = $bench->elapsedTime('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')'); |
|
| 1141 | - //TODO use the configuration value for the high response time currently is 1 second |
|
| 1142 | - if($responseTime >= 1 ){ |
|
| 1143 | - $this->logger->warning('High response time while processing database query [' .$query. ']. The response time is [' .$responseTime. '] sec.'); |
|
| 1144 | - } |
|
| 1145 | - if ($sqlQuery){ |
|
| 1146 | - //if need return all result like list of record |
|
| 1147 | - if ($all){ |
|
| 1148 | - $this->result = ($array === false) ? $sqlQuery->fetchAll(PDO::FETCH_OBJ) : $sqlQuery->fetchAll(PDO::FETCH_ASSOC); |
|
| 1149 | - } |
|
| 1150 | - else{ |
|
| 1151 | - $this->result = ($array === false) ? $sqlQuery->fetch(PDO::FETCH_OBJ) : $sqlQuery->fetch(PDO::FETCH_ASSOC); |
|
| 1152 | - } |
|
| 1153 | - //Sqlite and pgsql always return 0 when using rowCount() |
|
| 1154 | - if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 1155 | - $this->numRows = count($this->result); |
|
| 1156 | - } |
|
| 1157 | - else{ |
|
| 1158 | - $this->numRows = $sqlQuery->rowCount(); |
|
| 1159 | - } |
|
| 1160 | - |
|
| 1161 | - if ($dbCacheStatus && $sqlSELECTQuery){ |
|
| 1162 | - $this->logger->info('Save the result for query [' .$this->query. '] into cache for future use'); |
|
| 1163 | - $cacheInstance->set($cacheKey, $this->result, $cacheExpire); |
|
| 1164 | - } |
|
| 1165 | - } |
|
| 1166 | - else{ |
|
| 1167 | - $error = $this->pdo->errorInfo(); |
|
| 1168 | - $this->error = isset($error[2]) ? $error[2] : ''; |
|
| 1169 | - $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
|
| 1170 | - $this->error(); |
|
| 1171 | - } |
|
| 1172 | - } |
|
| 1173 | - else if ((! $cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)){ |
|
| 1174 | - $queryStr = $this->pdo->query($this->query); |
|
| 1175 | - if($queryStr){ |
|
| 1176 | - //Sqlite and pgsql always return 0 when using rowCount() |
|
| 1177 | - if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 1178 | - $this->result = 1; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 1179 | - $this->numRows = 1; |
|
| 1180 | - } |
|
| 1181 | - else{ |
|
| 1182 | - $this->result = $queryStr->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 1183 | - $this->numRows = $queryStr->rowCount(); |
|
| 1184 | - } |
|
| 1185 | - } |
|
| 1186 | - if (! $this->result){ |
|
| 1187 | - $error = $this->pdo->errorInfo(); |
|
| 1188 | - $this->error = isset($error[2]) ? $error[2] : ''; |
|
| 1189 | - $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
|
| 1190 | - $this->error(); |
|
| 1191 | - } |
|
| 1192 | - } |
|
| 1193 | - else{ |
|
| 1194 | - $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 1195 | - $this->result = $cacheContent; |
|
| 1196 | - $this->numRows = count($this->result); |
|
| 1197 | - } |
|
| 1198 | - $this->queryCount++; |
|
| 1199 | - if(! $this->result){ |
|
| 1200 | - $this->logger->info('No result where found for the query [' . $query . ']'); |
|
| 1201 | - } |
|
| 1202 | - return $this->result; |
|
| 1203 | - } |
|
| 1204 | - |
|
| 1205 | - /** |
|
| 1206 | - * Set database cache time to live |
|
| 1207 | - * @param integer $ttl the cache time to live in second |
|
| 1208 | - * @return object the current Database instance |
|
| 1209 | - */ |
|
| 1210 | - public function setCache($ttl = 0){ |
|
| 1211 | - if($ttl > 0){ |
|
| 1212 | - $this->cacheTtl = $ttl; |
|
| 1213 | - $this->temporaryCacheTtl = $ttl; |
|
| 1214 | - } |
|
| 1215 | - return $this; |
|
| 1216 | - } |
|
| 1139 | + //get response time for this query |
|
| 1140 | + $responseTime = $bench->elapsedTime('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')'); |
|
| 1141 | + //TODO use the configuration value for the high response time currently is 1 second |
|
| 1142 | + if($responseTime >= 1 ){ |
|
| 1143 | + $this->logger->warning('High response time while processing database query [' .$query. ']. The response time is [' .$responseTime. '] sec.'); |
|
| 1144 | + } |
|
| 1145 | + if ($sqlQuery){ |
|
| 1146 | + //if need return all result like list of record |
|
| 1147 | + if ($all){ |
|
| 1148 | + $this->result = ($array === false) ? $sqlQuery->fetchAll(PDO::FETCH_OBJ) : $sqlQuery->fetchAll(PDO::FETCH_ASSOC); |
|
| 1149 | + } |
|
| 1150 | + else{ |
|
| 1151 | + $this->result = ($array === false) ? $sqlQuery->fetch(PDO::FETCH_OBJ) : $sqlQuery->fetch(PDO::FETCH_ASSOC); |
|
| 1152 | + } |
|
| 1153 | + //Sqlite and pgsql always return 0 when using rowCount() |
|
| 1154 | + if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 1155 | + $this->numRows = count($this->result); |
|
| 1156 | + } |
|
| 1157 | + else{ |
|
| 1158 | + $this->numRows = $sqlQuery->rowCount(); |
|
| 1159 | + } |
|
| 1160 | + |
|
| 1161 | + if ($dbCacheStatus && $sqlSELECTQuery){ |
|
| 1162 | + $this->logger->info('Save the result for query [' .$this->query. '] into cache for future use'); |
|
| 1163 | + $cacheInstance->set($cacheKey, $this->result, $cacheExpire); |
|
| 1164 | + } |
|
| 1165 | + } |
|
| 1166 | + else{ |
|
| 1167 | + $error = $this->pdo->errorInfo(); |
|
| 1168 | + $this->error = isset($error[2]) ? $error[2] : ''; |
|
| 1169 | + $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
|
| 1170 | + $this->error(); |
|
| 1171 | + } |
|
| 1172 | + } |
|
| 1173 | + else if ((! $cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)){ |
|
| 1174 | + $queryStr = $this->pdo->query($this->query); |
|
| 1175 | + if($queryStr){ |
|
| 1176 | + //Sqlite and pgsql always return 0 when using rowCount() |
|
| 1177 | + if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 1178 | + $this->result = 1; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 1179 | + $this->numRows = 1; |
|
| 1180 | + } |
|
| 1181 | + else{ |
|
| 1182 | + $this->result = $queryStr->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 1183 | + $this->numRows = $queryStr->rowCount(); |
|
| 1184 | + } |
|
| 1185 | + } |
|
| 1186 | + if (! $this->result){ |
|
| 1187 | + $error = $this->pdo->errorInfo(); |
|
| 1188 | + $this->error = isset($error[2]) ? $error[2] : ''; |
|
| 1189 | + $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
|
| 1190 | + $this->error(); |
|
| 1191 | + } |
|
| 1192 | + } |
|
| 1193 | + else{ |
|
| 1194 | + $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 1195 | + $this->result = $cacheContent; |
|
| 1196 | + $this->numRows = count($this->result); |
|
| 1197 | + } |
|
| 1198 | + $this->queryCount++; |
|
| 1199 | + if(! $this->result){ |
|
| 1200 | + $this->logger->info('No result where found for the query [' . $query . ']'); |
|
| 1201 | + } |
|
| 1202 | + return $this->result; |
|
| 1203 | + } |
|
| 1204 | + |
|
| 1205 | + /** |
|
| 1206 | + * Set database cache time to live |
|
| 1207 | + * @param integer $ttl the cache time to live in second |
|
| 1208 | + * @return object the current Database instance |
|
| 1209 | + */ |
|
| 1210 | + public function setCache($ttl = 0){ |
|
| 1211 | + if($ttl > 0){ |
|
| 1212 | + $this->cacheTtl = $ttl; |
|
| 1213 | + $this->temporaryCacheTtl = $ttl; |
|
| 1214 | + } |
|
| 1215 | + return $this; |
|
| 1216 | + } |
|
| 1217 | 1217 | |
| 1218 | 1218 | /** |
| 1219 | 1219 | * Enabled cache temporary for the current query not globally |
@@ -1221,258 +1221,258 @@ discard block |
||
| 1221 | 1221 | * @return object the current Database instance |
| 1222 | 1222 | */ |
| 1223 | 1223 | public function cached($ttl = 0){ |
| 1224 | - if($ttl > 0){ |
|
| 1225 | - $this->temporaryCacheTtl = $ttl; |
|
| 1226 | - } |
|
| 1224 | + if($ttl > 0){ |
|
| 1225 | + $this->temporaryCacheTtl = $ttl; |
|
| 1226 | + } |
|
| 1227 | + return $this; |
|
| 1228 | + } |
|
| 1229 | + |
|
| 1230 | + /** |
|
| 1231 | + * Escape the data before execute query useful for security. |
|
| 1232 | + * @param mixed $data the data to be escaped |
|
| 1233 | + * @return mixed the data after escaped |
|
| 1234 | + */ |
|
| 1235 | + public function escape($data){ |
|
| 1236 | + if(is_null($data)){ |
|
| 1237 | + return null; |
|
| 1238 | + } |
|
| 1239 | + if(! $this->pdo){ |
|
| 1240 | + $this->connect(); |
|
| 1241 | + } |
|
| 1242 | + return $this->pdo->quote(trim($data)); |
|
| 1243 | + } |
|
| 1244 | + |
|
| 1245 | + /** |
|
| 1246 | + * Return the number query executed count for the current request |
|
| 1247 | + * @return int |
|
| 1248 | + */ |
|
| 1249 | + public function queryCount(){ |
|
| 1250 | + return $this->queryCount; |
|
| 1251 | + } |
|
| 1252 | + |
|
| 1253 | + /** |
|
| 1254 | + * Return the current query SQL string |
|
| 1255 | + * @return string |
|
| 1256 | + */ |
|
| 1257 | + public function getQuery(){ |
|
| 1258 | + return $this->query; |
|
| 1259 | + } |
|
| 1260 | + |
|
| 1261 | + /** |
|
| 1262 | + * Return the application database name |
|
| 1263 | + * @return string |
|
| 1264 | + */ |
|
| 1265 | + public function getDatabaseName(){ |
|
| 1266 | + return $this->databaseName; |
|
| 1267 | + } |
|
| 1268 | + |
|
| 1269 | + /** |
|
| 1270 | + * Return the database configuration |
|
| 1271 | + * @return array |
|
| 1272 | + */ |
|
| 1273 | + public function getDatabaseConfiguration(){ |
|
| 1274 | + return $this->config; |
|
| 1275 | + } |
|
| 1276 | + |
|
| 1277 | + /** |
|
| 1278 | + * set the database configuration |
|
| 1279 | + * @param array $config the configuration |
|
| 1280 | + */ |
|
| 1281 | + public function setDatabaseConfiguration(array $config){ |
|
| 1282 | + $this->config = array_merge($this->config, $config); |
|
| 1283 | + $this->prefix = $this->config['prefix']; |
|
| 1284 | + $this->databaseName = $this->config['database']; |
|
| 1285 | + $this->logger->info('The database configuration are listed below: ' . stringfy_vars(array_merge($this->config, array('password' => string_hidden($this->config['password']))))); |
|
| 1227 | 1286 | return $this; |
| 1228 | - } |
|
| 1229 | - |
|
| 1230 | - /** |
|
| 1231 | - * Escape the data before execute query useful for security. |
|
| 1232 | - * @param mixed $data the data to be escaped |
|
| 1233 | - * @return mixed the data after escaped |
|
| 1234 | - */ |
|
| 1235 | - public function escape($data){ |
|
| 1236 | - if(is_null($data)){ |
|
| 1237 | - return null; |
|
| 1238 | - } |
|
| 1239 | - if(! $this->pdo){ |
|
| 1240 | - $this->connect(); |
|
| 1241 | - } |
|
| 1242 | - return $this->pdo->quote(trim($data)); |
|
| 1243 | - } |
|
| 1244 | - |
|
| 1245 | - /** |
|
| 1246 | - * Return the number query executed count for the current request |
|
| 1247 | - * @return int |
|
| 1248 | - */ |
|
| 1249 | - public function queryCount(){ |
|
| 1250 | - return $this->queryCount; |
|
| 1251 | - } |
|
| 1252 | - |
|
| 1253 | - /** |
|
| 1254 | - * Return the current query SQL string |
|
| 1255 | - * @return string |
|
| 1256 | - */ |
|
| 1257 | - public function getQuery(){ |
|
| 1258 | - return $this->query; |
|
| 1259 | - } |
|
| 1260 | - |
|
| 1261 | - /** |
|
| 1262 | - * Return the application database name |
|
| 1263 | - * @return string |
|
| 1264 | - */ |
|
| 1265 | - public function getDatabaseName(){ |
|
| 1266 | - return $this->databaseName; |
|
| 1267 | - } |
|
| 1268 | - |
|
| 1269 | - /** |
|
| 1270 | - * Return the database configuration |
|
| 1271 | - * @return array |
|
| 1272 | - */ |
|
| 1273 | - public function getDatabaseConfiguration(){ |
|
| 1274 | - return $this->config; |
|
| 1275 | - } |
|
| 1276 | - |
|
| 1277 | - /** |
|
| 1278 | - * set the database configuration |
|
| 1279 | - * @param array $config the configuration |
|
| 1280 | - */ |
|
| 1281 | - public function setDatabaseConfiguration(array $config){ |
|
| 1282 | - $this->config = array_merge($this->config, $config); |
|
| 1283 | - $this->prefix = $this->config['prefix']; |
|
| 1284 | - $this->databaseName = $this->config['database']; |
|
| 1285 | - $this->logger->info('The database configuration are listed below: ' . stringfy_vars(array_merge($this->config, array('password' => string_hidden($this->config['password']))))); |
|
| 1286 | - return $this; |
|
| 1287 | - } |
|
| 1288 | - |
|
| 1289 | - /** |
|
| 1290 | - * Return the PDO instance |
|
| 1291 | - * @return PDO |
|
| 1292 | - */ |
|
| 1293 | - public function getPdo(){ |
|
| 1294 | - return $this->pdo; |
|
| 1295 | - } |
|
| 1296 | - |
|
| 1297 | - /** |
|
| 1298 | - * Set the PDO instance |
|
| 1299 | - * @param PDO $pdo the pdo object |
|
| 1300 | - */ |
|
| 1301 | - public function setPdo(PDO $pdo){ |
|
| 1302 | - $this->pdo = $pdo; |
|
| 1303 | - return $this; |
|
| 1304 | - } |
|
| 1305 | - |
|
| 1306 | - |
|
| 1307 | - /** |
|
| 1308 | - * Return the Log instance |
|
| 1309 | - * @return Log |
|
| 1310 | - */ |
|
| 1311 | - public function getLogger(){ |
|
| 1312 | - return $this->logger; |
|
| 1313 | - } |
|
| 1314 | - |
|
| 1315 | - /** |
|
| 1316 | - * Set the log instance |
|
| 1317 | - * @param Log $logger the log object |
|
| 1318 | - */ |
|
| 1319 | - public function setLogger($logger){ |
|
| 1320 | - $this->logger = $logger; |
|
| 1321 | - return $this; |
|
| 1322 | - } |
|
| 1323 | - |
|
| 1324 | - /** |
|
| 1325 | - * Return the cache instance |
|
| 1326 | - * @return CacheInterface |
|
| 1327 | - */ |
|
| 1328 | - public function getCacheInstance(){ |
|
| 1329 | - return $this->cacheInstance; |
|
| 1330 | - } |
|
| 1331 | - |
|
| 1332 | - /** |
|
| 1333 | - * Set the cache instance |
|
| 1334 | - * @param CacheInterface $cache the cache object |
|
| 1335 | - */ |
|
| 1336 | - public function setCacheInstance($cache){ |
|
| 1337 | - $this->cacheInstance = $cache; |
|
| 1338 | - return $this; |
|
| 1339 | - } |
|
| 1340 | - |
|
| 1341 | - /** |
|
| 1342 | - * Return the benchmark instance |
|
| 1343 | - * @return Benchmark |
|
| 1344 | - */ |
|
| 1345 | - public function getBenchmark(){ |
|
| 1346 | - return $this->benchmarkInstance; |
|
| 1347 | - } |
|
| 1348 | - |
|
| 1349 | - /** |
|
| 1350 | - * Set the benchmark instance |
|
| 1351 | - * @param Benchmark $cache the cache object |
|
| 1352 | - */ |
|
| 1353 | - public function setBenchmark($benchmark){ |
|
| 1354 | - $this->benchmarkInstance = $benchmark; |
|
| 1355 | - return $this; |
|
| 1356 | - } |
|
| 1357 | - |
|
| 1358 | - /** |
|
| 1359 | - * Return the data to be used for insert, update, etc. |
|
| 1360 | - * @return array |
|
| 1361 | - */ |
|
| 1362 | - public function getData(){ |
|
| 1363 | - return $this->data; |
|
| 1364 | - } |
|
| 1365 | - |
|
| 1366 | - /** |
|
| 1367 | - * Set the data to be used for insert, update, etc. |
|
| 1368 | - * @param string $key the data key identified |
|
| 1369 | - * @param mixed $value the data value |
|
| 1370 | - * @param boolean $escape whether to escape or not the $value |
|
| 1371 | - * @return object the current Database instance |
|
| 1372 | - */ |
|
| 1373 | - public function setData($key, $value, $escape = true){ |
|
| 1374 | - $this->data[$key] = $escape ? $this->escape($value) : $value; |
|
| 1375 | - return $this; |
|
| 1376 | - } |
|
| 1377 | - |
|
| 1378 | - /** |
|
| 1379 | - * Set the Log instance using argument or create new instance |
|
| 1380 | - * @param object $logger the Log instance if not null |
|
| 1381 | - */ |
|
| 1382 | - protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
| 1383 | - if($logger !== null){ |
|
| 1384 | - $this->logger = $logger; |
|
| 1385 | - } |
|
| 1386 | - else{ |
|
| 1387 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 1388 | - $this->logger->setLogger('Library::Database'); |
|
| 1389 | - } |
|
| 1390 | - } |
|
| 1287 | + } |
|
| 1288 | + |
|
| 1289 | + /** |
|
| 1290 | + * Return the PDO instance |
|
| 1291 | + * @return PDO |
|
| 1292 | + */ |
|
| 1293 | + public function getPdo(){ |
|
| 1294 | + return $this->pdo; |
|
| 1295 | + } |
|
| 1296 | + |
|
| 1297 | + /** |
|
| 1298 | + * Set the PDO instance |
|
| 1299 | + * @param PDO $pdo the pdo object |
|
| 1300 | + */ |
|
| 1301 | + public function setPdo(PDO $pdo){ |
|
| 1302 | + $this->pdo = $pdo; |
|
| 1303 | + return $this; |
|
| 1304 | + } |
|
| 1305 | + |
|
| 1306 | + |
|
| 1307 | + /** |
|
| 1308 | + * Return the Log instance |
|
| 1309 | + * @return Log |
|
| 1310 | + */ |
|
| 1311 | + public function getLogger(){ |
|
| 1312 | + return $this->logger; |
|
| 1313 | + } |
|
| 1314 | + |
|
| 1315 | + /** |
|
| 1316 | + * Set the log instance |
|
| 1317 | + * @param Log $logger the log object |
|
| 1318 | + */ |
|
| 1319 | + public function setLogger($logger){ |
|
| 1320 | + $this->logger = $logger; |
|
| 1321 | + return $this; |
|
| 1322 | + } |
|
| 1323 | + |
|
| 1324 | + /** |
|
| 1325 | + * Return the cache instance |
|
| 1326 | + * @return CacheInterface |
|
| 1327 | + */ |
|
| 1328 | + public function getCacheInstance(){ |
|
| 1329 | + return $this->cacheInstance; |
|
| 1330 | + } |
|
| 1331 | + |
|
| 1332 | + /** |
|
| 1333 | + * Set the cache instance |
|
| 1334 | + * @param CacheInterface $cache the cache object |
|
| 1335 | + */ |
|
| 1336 | + public function setCacheInstance($cache){ |
|
| 1337 | + $this->cacheInstance = $cache; |
|
| 1338 | + return $this; |
|
| 1339 | + } |
|
| 1340 | + |
|
| 1341 | + /** |
|
| 1342 | + * Return the benchmark instance |
|
| 1343 | + * @return Benchmark |
|
| 1344 | + */ |
|
| 1345 | + public function getBenchmark(){ |
|
| 1346 | + return $this->benchmarkInstance; |
|
| 1347 | + } |
|
| 1348 | + |
|
| 1349 | + /** |
|
| 1350 | + * Set the benchmark instance |
|
| 1351 | + * @param Benchmark $cache the cache object |
|
| 1352 | + */ |
|
| 1353 | + public function setBenchmark($benchmark){ |
|
| 1354 | + $this->benchmarkInstance = $benchmark; |
|
| 1355 | + return $this; |
|
| 1356 | + } |
|
| 1357 | + |
|
| 1358 | + /** |
|
| 1359 | + * Return the data to be used for insert, update, etc. |
|
| 1360 | + * @return array |
|
| 1361 | + */ |
|
| 1362 | + public function getData(){ |
|
| 1363 | + return $this->data; |
|
| 1364 | + } |
|
| 1365 | + |
|
| 1366 | + /** |
|
| 1367 | + * Set the data to be used for insert, update, etc. |
|
| 1368 | + * @param string $key the data key identified |
|
| 1369 | + * @param mixed $value the data value |
|
| 1370 | + * @param boolean $escape whether to escape or not the $value |
|
| 1371 | + * @return object the current Database instance |
|
| 1372 | + */ |
|
| 1373 | + public function setData($key, $value, $escape = true){ |
|
| 1374 | + $this->data[$key] = $escape ? $this->escape($value) : $value; |
|
| 1375 | + return $this; |
|
| 1376 | + } |
|
| 1377 | + |
|
| 1378 | + /** |
|
| 1379 | + * Set the Log instance using argument or create new instance |
|
| 1380 | + * @param object $logger the Log instance if not null |
|
| 1381 | + */ |
|
| 1382 | + protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
| 1383 | + if($logger !== null){ |
|
| 1384 | + $this->logger = $logger; |
|
| 1385 | + } |
|
| 1386 | + else{ |
|
| 1387 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 1388 | + $this->logger->setLogger('Library::Database'); |
|
| 1389 | + } |
|
| 1390 | + } |
|
| 1391 | 1391 | |
| 1392 | 1392 | /** |
| 1393 | 1393 | * Setting the database configuration using the configuration file |
| 1394 | 1394 | * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
| 1395 | 1395 | */ |
| 1396 | - protected function setDatabaseConfigurationFromConfigFile(array $overwriteConfig = array()){ |
|
| 1397 | - $db = array(); |
|
| 1398 | - if(file_exists(CONFIG_PATH . 'database.php')){ |
|
| 1399 | - //here don't use require_once because somewhere user can create database instance directly |
|
| 1400 | - require CONFIG_PATH . 'database.php'; |
|
| 1401 | - } |
|
| 1396 | + protected function setDatabaseConfigurationFromConfigFile(array $overwriteConfig = array()){ |
|
| 1397 | + $db = array(); |
|
| 1398 | + if(file_exists(CONFIG_PATH . 'database.php')){ |
|
| 1399 | + //here don't use require_once because somewhere user can create database instance directly |
|
| 1400 | + require CONFIG_PATH . 'database.php'; |
|
| 1401 | + } |
|
| 1402 | 1402 | |
| 1403 | - if(! empty($overwriteConfig)){ |
|
| 1404 | - $db = array_merge($db, $overwriteConfig); |
|
| 1405 | - } |
|
| 1406 | - $config = array(); |
|
| 1407 | - $config['driver'] = isset($db['driver']) ? $db['driver'] : 'mysql'; |
|
| 1408 | - $config['username'] = isset($db['username']) ? $db['username'] : 'root'; |
|
| 1409 | - $config['password'] = isset($db['password']) ? $db['password'] : ''; |
|
| 1410 | - $config['database'] = isset($db['database']) ? $db['database'] : ''; |
|
| 1411 | - $config['hostname'] = isset($db['hostname']) ? $db['hostname'] : 'localhost'; |
|
| 1412 | - $config['charset'] = isset($db['charset']) ? $db['charset'] : 'utf8'; |
|
| 1413 | - $config['collation'] = isset($db['collation']) ? $db['collation'] : 'utf8_general_ci'; |
|
| 1414 | - $config['prefix'] = isset($db['prefix']) ? $db['prefix'] : ''; |
|
| 1415 | - $port = ''; |
|
| 1416 | - if(strstr($config['hostname'], ':')){ |
|
| 1417 | - $p = explode(':', $config['hostname']); |
|
| 1418 | - $port = isset($p[1]) ? $p[1] : ''; |
|
| 1419 | - $config['hostname'] = isset($p[0]) ? $p[0] : ''; |
|
| 1420 | - } |
|
| 1421 | - $config['port'] = $port; |
|
| 1422 | - $this->setDatabaseConfiguration($config); |
|
| 1423 | - } |
|
| 1424 | - |
|
| 1425 | - /** |
|
| 1426 | - * This method is used to get the PDO DSN string using th configured driver |
|
| 1427 | - * @return string the DSN string |
|
| 1428 | - */ |
|
| 1429 | - protected function getDsnFromDriver(){ |
|
| 1430 | - $config = $this->getDatabaseConfiguration(); |
|
| 1431 | - if(! empty($config)){ |
|
| 1432 | - $driverDsnMap = array( |
|
| 1433 | - 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
|
| 1434 | - . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 1435 | - . 'dbname=' . $config['database'], |
|
| 1436 | - 'pgsql' => 'pgsql:host=' . $config['hostname'] . ';' |
|
| 1437 | - . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 1438 | - . 'dbname=' . $config['database'], |
|
| 1439 | - 'sqlite' => 'sqlite:' . $config['database'], |
|
| 1440 | - 'oracle' => 'oci:dbname=' . $config['hostname'] |
|
| 1441 | - . (($config['port']) != '' ? ':' . $config['port'] : '') |
|
| 1442 | - . '/' . $config['database'] |
|
| 1443 | - ); |
|
| 1444 | - return isset($driverDsnMap[$config['driver']]) ? $driverDsnMap[$config['driver']] : ''; |
|
| 1445 | - } |
|
| 1403 | + if(! empty($overwriteConfig)){ |
|
| 1404 | + $db = array_merge($db, $overwriteConfig); |
|
| 1405 | + } |
|
| 1406 | + $config = array(); |
|
| 1407 | + $config['driver'] = isset($db['driver']) ? $db['driver'] : 'mysql'; |
|
| 1408 | + $config['username'] = isset($db['username']) ? $db['username'] : 'root'; |
|
| 1409 | + $config['password'] = isset($db['password']) ? $db['password'] : ''; |
|
| 1410 | + $config['database'] = isset($db['database']) ? $db['database'] : ''; |
|
| 1411 | + $config['hostname'] = isset($db['hostname']) ? $db['hostname'] : 'localhost'; |
|
| 1412 | + $config['charset'] = isset($db['charset']) ? $db['charset'] : 'utf8'; |
|
| 1413 | + $config['collation'] = isset($db['collation']) ? $db['collation'] : 'utf8_general_ci'; |
|
| 1414 | + $config['prefix'] = isset($db['prefix']) ? $db['prefix'] : ''; |
|
| 1415 | + $port = ''; |
|
| 1416 | + if(strstr($config['hostname'], ':')){ |
|
| 1417 | + $p = explode(':', $config['hostname']); |
|
| 1418 | + $port = isset($p[1]) ? $p[1] : ''; |
|
| 1419 | + $config['hostname'] = isset($p[0]) ? $p[0] : ''; |
|
| 1420 | + } |
|
| 1421 | + $config['port'] = $port; |
|
| 1422 | + $this->setDatabaseConfiguration($config); |
|
| 1423 | + } |
|
| 1424 | + |
|
| 1425 | + /** |
|
| 1426 | + * This method is used to get the PDO DSN string using th configured driver |
|
| 1427 | + * @return string the DSN string |
|
| 1428 | + */ |
|
| 1429 | + protected function getDsnFromDriver(){ |
|
| 1430 | + $config = $this->getDatabaseConfiguration(); |
|
| 1431 | + if(! empty($config)){ |
|
| 1432 | + $driverDsnMap = array( |
|
| 1433 | + 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
|
| 1434 | + . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 1435 | + . 'dbname=' . $config['database'], |
|
| 1436 | + 'pgsql' => 'pgsql:host=' . $config['hostname'] . ';' |
|
| 1437 | + . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 1438 | + . 'dbname=' . $config['database'], |
|
| 1439 | + 'sqlite' => 'sqlite:' . $config['database'], |
|
| 1440 | + 'oracle' => 'oci:dbname=' . $config['hostname'] |
|
| 1441 | + . (($config['port']) != '' ? ':' . $config['port'] : '') |
|
| 1442 | + . '/' . $config['database'] |
|
| 1443 | + ); |
|
| 1444 | + return isset($driverDsnMap[$config['driver']]) ? $driverDsnMap[$config['driver']] : ''; |
|
| 1445 | + } |
|
| 1446 | 1446 | |
| 1447 | - return null; |
|
| 1448 | - } |
|
| 1447 | + return null; |
|
| 1448 | + } |
|
| 1449 | 1449 | |
| 1450 | 1450 | |
| 1451 | 1451 | /** |
| 1452 | 1452 | * Reset the database class attributs to the initail values before each query. |
| 1453 | 1453 | */ |
| 1454 | 1454 | private function reset(){ |
| 1455 | - $this->select = '*'; |
|
| 1456 | - $this->from = null; |
|
| 1457 | - $this->where = null; |
|
| 1458 | - $this->limit = null; |
|
| 1459 | - $this->orderBy = null; |
|
| 1460 | - $this->groupBy = null; |
|
| 1461 | - $this->having = null; |
|
| 1462 | - $this->join = null; |
|
| 1463 | - $this->numRows = 0; |
|
| 1464 | - $this->insertId = null; |
|
| 1465 | - $this->query = null; |
|
| 1466 | - $this->error = null; |
|
| 1467 | - $this->result = array(); |
|
| 1468 | - $this->data = array(); |
|
| 1455 | + $this->select = '*'; |
|
| 1456 | + $this->from = null; |
|
| 1457 | + $this->where = null; |
|
| 1458 | + $this->limit = null; |
|
| 1459 | + $this->orderBy = null; |
|
| 1460 | + $this->groupBy = null; |
|
| 1461 | + $this->having = null; |
|
| 1462 | + $this->join = null; |
|
| 1463 | + $this->numRows = 0; |
|
| 1464 | + $this->insertId = null; |
|
| 1465 | + $this->query = null; |
|
| 1466 | + $this->error = null; |
|
| 1467 | + $this->result = array(); |
|
| 1468 | + $this->data = array(); |
|
| 1469 | 1469 | } |
| 1470 | 1470 | |
| 1471 | 1471 | /** |
| 1472 | 1472 | * The class destructor |
| 1473 | 1473 | */ |
| 1474 | 1474 | public function __destruct(){ |
| 1475 | - $this->pdo = null; |
|
| 1475 | + $this->pdo = null; |
|
| 1476 | 1476 | } |
| 1477 | 1477 | |
| 1478 | 1478 | } |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | * You should have received a copy of the GNU General Public License |
| 23 | 23 | * along with this program; if not, write to the Free Software |
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | - */ |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | 27 | class Controller{ |
| 28 | 28 | |
@@ -114,12 +114,12 @@ discard block |
||
| 114 | 114 | */ |
| 115 | 115 | protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
| 116 | 116 | if($logger !== null){ |
| 117 | - $this->logger = $logger; |
|
| 118 | - } |
|
| 119 | - else{ |
|
| 120 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 117 | + $this->logger = $logger; |
|
| 118 | + } |
|
| 119 | + else{ |
|
| 120 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 121 | 121 | $this->logger->setLogger('MainController'); |
| 122 | - } |
|
| 122 | + } |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | /** |
@@ -1,917 +1,917 @@ |
||
| 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){ |
|
| 222 | - $this->logger->info('Check the CSRF value if is valid'); |
|
| 223 | - if(! Security::validateCSRF()){ |
|
| 224 | - show_error('Invalide data, Cross Site Request Forgery do his job, the data to validate is corrupted.'); |
|
| 225 | - } |
|
| 226 | - } |
|
| 227 | - else{ |
|
| 228 | - $this->logger->info('CSRF is not enabled in configuration or not set manully, no need to check it'); |
|
| 229 | - } |
|
| 230 | - } |
|
| 231 | - ///////////////////////////////////////////// |
|
| 232 | - $this->_forceFail = false; |
|
| 233 | - |
|
| 234 | - foreach ($this->getData() as $inputName => $inputVal) { |
|
| 235 | - if(is_array($this->data[$inputName])){ |
|
| 236 | - $this->data[$inputName] = array_map('trim', $this->data[$inputName]); |
|
| 237 | - } |
|
| 238 | - else{ |
|
| 239 | - $this->data[$inputName] = trim($this->data[$inputName]); |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - if (array_key_exists($inputName, $this->_rules)) { |
|
| 243 | - foreach ($this->_parseRuleString($this->_rules[$inputName]) as $eachRule) { |
|
| 244 | - $this->_validateRule($inputName, $this->data[$inputName], $eachRule); |
|
| 245 | - } |
|
| 246 | - } |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - if (empty($this->_errors) && $this->_forceFail === false) { |
|
| 250 | - $this->_success = true; |
|
| 251 | - } |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Adds a rule to a form data validation field. |
|
| 256 | - * |
|
| 257 | - * @param string $inputField Name of the field or the data key to add a rule to |
|
| 258 | - * @param string $ruleSets PIPE seperated string of rules |
|
| 259 | - * |
|
| 260 | - * @return FormValidation Current instance of object. |
|
| 261 | - */ |
|
| 262 | - public function setRule($inputField, $inputLabel, $ruleSets) { |
|
| 263 | - $this->_rules[$inputField] = $ruleSets; |
|
| 264 | - $this->_labels[$inputField] = $inputLabel; |
|
| 265 | - $this->logger->info('Set the field rule: name [' .$inputField. '], label [' .$inputLabel. '], rules [' .$ruleSets. ']'); |
|
| 266 | - return $this; |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * Takes an array of rules and uses setRule() to set them, accepts an array |
|
| 271 | - * of rule names rather than a pipe-delimited string as well. |
|
| 272 | - * @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){ |
|
| 222 | + $this->logger->info('Check the CSRF value if is valid'); |
|
| 223 | + if(! Security::validateCSRF()){ |
|
| 224 | + show_error('Invalide data, Cross Site Request Forgery do his job, the data to validate is corrupted.'); |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | + else{ |
|
| 228 | + $this->logger->info('CSRF is not enabled in configuration or not set manully, no need to check it'); |
|
| 229 | + } |
|
| 230 | + } |
|
| 231 | + ///////////////////////////////////////////// |
|
| 232 | + $this->_forceFail = false; |
|
| 233 | + |
|
| 234 | + foreach ($this->getData() as $inputName => $inputVal) { |
|
| 235 | + if(is_array($this->data[$inputName])){ |
|
| 236 | + $this->data[$inputName] = array_map('trim', $this->data[$inputName]); |
|
| 237 | + } |
|
| 238 | + else{ |
|
| 239 | + $this->data[$inputName] = trim($this->data[$inputName]); |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + if (array_key_exists($inputName, $this->_rules)) { |
|
| 243 | + foreach ($this->_parseRuleString($this->_rules[$inputName]) as $eachRule) { |
|
| 244 | + $this->_validateRule($inputName, $this->data[$inputName], $eachRule); |
|
| 245 | + } |
|
| 246 | + } |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + if (empty($this->_errors) && $this->_forceFail === false) { |
|
| 250 | + $this->_success = true; |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Adds a rule to a form data validation field. |
|
| 256 | + * |
|
| 257 | + * @param string $inputField Name of the field or the data key to add a rule to |
|
| 258 | + * @param string $ruleSets PIPE seperated string of rules |
|
| 273 | 259 | * |
| 274 | 260 | * @return FormValidation Current instance of object. |
| 275 | - */ |
|
| 276 | - public function setRules(array $ruleSets) { |
|
| 277 | - foreach ($ruleSets as $ruleSet) { |
|
| 278 | - $pipeDelimitedRules = null; |
|
| 279 | - if (is_array($ruleSet['rules'])) { |
|
| 280 | - $pipeDelimitedRules = implode('|', $ruleSet['rules']); |
|
| 281 | - } else { |
|
| 282 | - $pipeDelimitedRules = $ruleSet['rules']; |
|
| 283 | - } |
|
| 284 | - $this->setRule($ruleSet['name'], $ruleSet['label'], $pipeDelimitedRules); |
|
| 285 | - } |
|
| 286 | - return $this; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * This method creates the global errors delimiter, each argument occurs once, at the beginning, and |
|
| 291 | - * end of the errors block respectively. |
|
| 292 | - * |
|
| 293 | - * @param string $start Before block of errors gets displayed, HTML allowed. |
|
| 294 | - * @param string $end After the block of errors gets displayed, HTML allowed. |
|
| 295 | - * |
|
| 261 | + */ |
|
| 262 | + public function setRule($inputField, $inputLabel, $ruleSets) { |
|
| 263 | + $this->_rules[$inputField] = $ruleSets; |
|
| 264 | + $this->_labels[$inputField] = $inputLabel; |
|
| 265 | + $this->logger->info('Set the field rule: name [' .$inputField. '], label [' .$inputLabel. '], rules [' .$ruleSets. ']'); |
|
| 266 | + return $this; |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * Takes an array of rules and uses setRule() to set them, accepts an array |
|
| 271 | + * of rule names rather than a pipe-delimited string as well. |
|
| 272 | + * @param array $ruleSets |
|
| 273 | + * |
|
| 296 | 274 | * @return FormValidation Current instance of object. |
| 297 | - */ |
|
| 298 | - public function setErrorsDelimiter($start, $end) { |
|
| 299 | - $this->_allErrorsDelimiter[0] = $start; |
|
| 300 | - $this->_allErrorsDelimiter[1] = $end; |
|
| 301 | - return $this; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * This is the individual error delimiter, each argument occurs once before and after |
|
| 306 | - * each individual error listed. |
|
| 307 | - * |
|
| 308 | - * @param string $start Displayed before each error. |
|
| 309 | - * @param string $end Displayed after each error. |
|
| 310 | - * |
|
| 275 | + */ |
|
| 276 | + public function setRules(array $ruleSets) { |
|
| 277 | + foreach ($ruleSets as $ruleSet) { |
|
| 278 | + $pipeDelimitedRules = null; |
|
| 279 | + if (is_array($ruleSet['rules'])) { |
|
| 280 | + $pipeDelimitedRules = implode('|', $ruleSet['rules']); |
|
| 281 | + } else { |
|
| 282 | + $pipeDelimitedRules = $ruleSet['rules']; |
|
| 283 | + } |
|
| 284 | + $this->setRule($ruleSet['name'], $ruleSet['label'], $pipeDelimitedRules); |
|
| 285 | + } |
|
| 286 | + return $this; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * This method creates the global errors delimiter, each argument occurs once, at the beginning, and |
|
| 291 | + * end of the errors block respectively. |
|
| 292 | + * |
|
| 293 | + * @param string $start Before block of errors gets displayed, HTML allowed. |
|
| 294 | + * @param string $end After the block of errors gets displayed, HTML allowed. |
|
| 295 | + * |
|
| 311 | 296 | * @return FormValidation Current instance of object. |
| 312 | - */ |
|
| 313 | - public function setErrorDelimiter($start, $end) { |
|
| 314 | - $this->_eachErrorDelimiter[0] = $start; |
|
| 315 | - $this->_eachErrorDelimiter[1] = $end; |
|
| 316 | - return $this; |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * Get the each errors delimiters |
|
| 321 | - * |
|
| 322 | - * @return array |
|
| 323 | - */ |
|
| 324 | - public function getErrorDelimiter() { |
|
| 325 | - return $this->_eachErrorDelimiter; |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * Get the all errors delimiters |
|
| 330 | - * |
|
| 331 | - * @return array |
|
| 332 | - */ |
|
| 333 | - public function getErrorsDelimiter() { |
|
| 334 | - return $this->_allErrorsDelimiter; |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - /** |
|
| 338 | - * This sets a custom error message that can override the default error phrase provided |
|
| 339 | - * by FormValidation, it can be used in the format of setMessage('rule', 'error phrase') |
|
| 340 | - * which will globally change the error phrase of that rule, or in the format of: |
|
| 341 | - * setMessage('rule', 'fieldname', 'error phrase') - which will only change the error phrase for |
|
| 342 | - * that rule, applied on that field. |
|
| 343 | - * |
|
| 344 | - * @return boolean True on success, false on failure. |
|
| 345 | - */ |
|
| 346 | - public function setMessage() { |
|
| 347 | - $numArgs = func_num_args(); |
|
| 348 | - switch ($numArgs) { |
|
| 349 | - default: |
|
| 350 | - return false; |
|
| 351 | - // A global rule error message |
|
| 352 | - case 2: |
|
| 353 | - foreach ($this->post(null) as $key => $val) { |
|
| 354 | - $this->_errorPhraseOverrides[$key][func_get_arg(0)] = func_get_arg(1); |
|
| 355 | - } |
|
| 356 | - break; |
|
| 357 | - // Field specific rule error message |
|
| 358 | - case 3: |
|
| 359 | - $this->_errorPhraseOverrides[func_get_arg(1)][func_get_arg(0)] = func_get_arg(2); |
|
| 360 | - break; |
|
| 361 | - } |
|
| 362 | - return true; |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - /** |
|
| 366 | - * Adds a custom error message in the errorSet array, that will |
|
| 367 | - * forcibly display it. |
|
| 368 | - * |
|
| 369 | - * @param string $inputName The form input name or data key |
|
| 370 | - * @param string $errorMessage Error to display |
|
| 371 | - * |
|
| 372 | - * @return formValidation Current instance of the object |
|
| 373 | - */ |
|
| 374 | - public function setCustomError($inputName, $errorMessage) { |
|
| 375 | - $errorMessage = str_replace('%1', $this->_labels[$inputName], $errorMessage); |
|
| 376 | - $this->_errors[$inputName] = $errorMessage; |
|
| 377 | - return $this; |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * Allows for an accesor to any/all post values, if a value of null is passed as the key, it |
|
| 382 | - * will recursively find all keys/values of the $_POST array or data array. It also automatically trims |
|
| 383 | - * all values. |
|
| 384 | - * |
|
| 385 | - * @param string $key Key of $this->data to be found, pass null for all Key => Val pairs. |
|
| 386 | - * @param boolean $trim Defaults to true, trims all $this->data values. |
|
| 387 | - * @return string|array Array of post or data values if null is passed as key, string if only one key is desired. |
|
| 388 | - */ |
|
| 389 | - public function post($key = null, $trim = true) { |
|
| 390 | - $returnValue = null; |
|
| 391 | - if (is_null($key)) { |
|
| 392 | - $returnValue = array(); |
|
| 393 | - foreach ($this->getData() as $key => $val) { |
|
| 394 | - $returnValue[$key] = $this->post($key, $trim); |
|
| 395 | - } |
|
| 396 | - } else { |
|
| 397 | - $returnValue = (array_key_exists($key, $this->getData())) ? (($trim) ? trim($this->data[$key]) : $this->data[$key]) : null; |
|
| 398 | - } |
|
| 399 | - return $returnValue; |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - /** |
|
| 403 | - * Gets all errors from errorSet and displays them, can be echo out from the |
|
| 404 | - * function or just returned. |
|
| 405 | - * |
|
| 406 | - * @param boolean $limit number of error to display or return |
|
| 407 | - * @param boolean $echo Whether or not the values are to be returned or displayed |
|
| 408 | - * |
|
| 409 | - * @return string Errors formatted for output |
|
| 410 | - */ |
|
| 411 | - public function displayErrors($limit = null, $echo = true) { |
|
| 412 | - list($errorsStart, $errorsEnd) = $this->_allErrorsDelimiter; |
|
| 413 | - list($errorStart, $errorEnd) = $this->_eachErrorDelimiter; |
|
| 414 | - $errorOutput = $errorsStart; |
|
| 415 | - $i = 0; |
|
| 416 | - if (!empty($this->_errors)) { |
|
| 417 | - foreach ($this->_errors as $fieldName => $error) { |
|
| 418 | - if ($i === $limit) { |
|
| 419 | - break; |
|
| 420 | - } |
|
| 421 | - $errorOutput .= $errorStart; |
|
| 422 | - $errorOutput .= $error; |
|
| 423 | - $errorOutput .= $errorEnd; |
|
| 424 | - $i++; |
|
| 425 | - } |
|
| 426 | - } |
|
| 427 | - $errorOutput .= $errorsEnd; |
|
| 428 | - echo ($echo) ? $errorOutput : ''; |
|
| 429 | - return (! $echo) ? $errorOutput : null; |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - /** |
|
| 433 | - * Returns raw array of errors in no format instead of displaying them |
|
| 434 | - * formatted. |
|
| 435 | - * |
|
| 436 | - * @return array |
|
| 437 | - */ |
|
| 438 | - public function returnErrors() { |
|
| 439 | - return $this->_errors; |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - /** |
|
| 443 | - * Breaks up a PIPE seperated string of rules, and puts them into an array. |
|
| 444 | - * |
|
| 445 | - * @param string $ruleString String to be parsed. |
|
| 446 | - * |
|
| 447 | - * @return array Array of each value in original string. |
|
| 448 | - */ |
|
| 449 | - protected function _parseRuleString($ruleString) { |
|
| 450 | - $ruleSets = array(); |
|
| 451 | - /* |
|
| 297 | + */ |
|
| 298 | + public function setErrorsDelimiter($start, $end) { |
|
| 299 | + $this->_allErrorsDelimiter[0] = $start; |
|
| 300 | + $this->_allErrorsDelimiter[1] = $end; |
|
| 301 | + return $this; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * This is the individual error delimiter, each argument occurs once before and after |
|
| 306 | + * each individual error listed. |
|
| 307 | + * |
|
| 308 | + * @param string $start Displayed before each error. |
|
| 309 | + * @param string $end Displayed after each error. |
|
| 310 | + * |
|
| 311 | + * @return FormValidation Current instance of object. |
|
| 312 | + */ |
|
| 313 | + public function setErrorDelimiter($start, $end) { |
|
| 314 | + $this->_eachErrorDelimiter[0] = $start; |
|
| 315 | + $this->_eachErrorDelimiter[1] = $end; |
|
| 316 | + return $this; |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * Get the each errors delimiters |
|
| 321 | + * |
|
| 322 | + * @return array |
|
| 323 | + */ |
|
| 324 | + public function getErrorDelimiter() { |
|
| 325 | + return $this->_eachErrorDelimiter; |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Get the all errors delimiters |
|
| 330 | + * |
|
| 331 | + * @return array |
|
| 332 | + */ |
|
| 333 | + public function getErrorsDelimiter() { |
|
| 334 | + return $this->_allErrorsDelimiter; |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + /** |
|
| 338 | + * This sets a custom error message that can override the default error phrase provided |
|
| 339 | + * by FormValidation, it can be used in the format of setMessage('rule', 'error phrase') |
|
| 340 | + * which will globally change the error phrase of that rule, or in the format of: |
|
| 341 | + * setMessage('rule', 'fieldname', 'error phrase') - which will only change the error phrase for |
|
| 342 | + * that rule, applied on that field. |
|
| 343 | + * |
|
| 344 | + * @return boolean True on success, false on failure. |
|
| 345 | + */ |
|
| 346 | + public function setMessage() { |
|
| 347 | + $numArgs = func_num_args(); |
|
| 348 | + switch ($numArgs) { |
|
| 349 | + default: |
|
| 350 | + return false; |
|
| 351 | + // A global rule error message |
|
| 352 | + case 2: |
|
| 353 | + foreach ($this->post(null) as $key => $val) { |
|
| 354 | + $this->_errorPhraseOverrides[$key][func_get_arg(0)] = func_get_arg(1); |
|
| 355 | + } |
|
| 356 | + break; |
|
| 357 | + // Field specific rule error message |
|
| 358 | + case 3: |
|
| 359 | + $this->_errorPhraseOverrides[func_get_arg(1)][func_get_arg(0)] = func_get_arg(2); |
|
| 360 | + break; |
|
| 361 | + } |
|
| 362 | + return true; |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + /** |
|
| 366 | + * Adds a custom error message in the errorSet array, that will |
|
| 367 | + * forcibly display it. |
|
| 368 | + * |
|
| 369 | + * @param string $inputName The form input name or data key |
|
| 370 | + * @param string $errorMessage Error to display |
|
| 371 | + * |
|
| 372 | + * @return formValidation Current instance of the object |
|
| 373 | + */ |
|
| 374 | + public function setCustomError($inputName, $errorMessage) { |
|
| 375 | + $errorMessage = str_replace('%1', $this->_labels[$inputName], $errorMessage); |
|
| 376 | + $this->_errors[$inputName] = $errorMessage; |
|
| 377 | + return $this; |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * Allows for an accesor to any/all post values, if a value of null is passed as the key, it |
|
| 382 | + * will recursively find all keys/values of the $_POST array or data array. It also automatically trims |
|
| 383 | + * all values. |
|
| 384 | + * |
|
| 385 | + * @param string $key Key of $this->data to be found, pass null for all Key => Val pairs. |
|
| 386 | + * @param boolean $trim Defaults to true, trims all $this->data values. |
|
| 387 | + * @return string|array Array of post or data values if null is passed as key, string if only one key is desired. |
|
| 388 | + */ |
|
| 389 | + public function post($key = null, $trim = true) { |
|
| 390 | + $returnValue = null; |
|
| 391 | + if (is_null($key)) { |
|
| 392 | + $returnValue = array(); |
|
| 393 | + foreach ($this->getData() as $key => $val) { |
|
| 394 | + $returnValue[$key] = $this->post($key, $trim); |
|
| 395 | + } |
|
| 396 | + } else { |
|
| 397 | + $returnValue = (array_key_exists($key, $this->getData())) ? (($trim) ? trim($this->data[$key]) : $this->data[$key]) : null; |
|
| 398 | + } |
|
| 399 | + return $returnValue; |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + /** |
|
| 403 | + * Gets all errors from errorSet and displays them, can be echo out from the |
|
| 404 | + * function or just returned. |
|
| 405 | + * |
|
| 406 | + * @param boolean $limit number of error to display or return |
|
| 407 | + * @param boolean $echo Whether or not the values are to be returned or displayed |
|
| 408 | + * |
|
| 409 | + * @return string Errors formatted for output |
|
| 410 | + */ |
|
| 411 | + public function displayErrors($limit = null, $echo = true) { |
|
| 412 | + list($errorsStart, $errorsEnd) = $this->_allErrorsDelimiter; |
|
| 413 | + list($errorStart, $errorEnd) = $this->_eachErrorDelimiter; |
|
| 414 | + $errorOutput = $errorsStart; |
|
| 415 | + $i = 0; |
|
| 416 | + if (!empty($this->_errors)) { |
|
| 417 | + foreach ($this->_errors as $fieldName => $error) { |
|
| 418 | + if ($i === $limit) { |
|
| 419 | + break; |
|
| 420 | + } |
|
| 421 | + $errorOutput .= $errorStart; |
|
| 422 | + $errorOutput .= $error; |
|
| 423 | + $errorOutput .= $errorEnd; |
|
| 424 | + $i++; |
|
| 425 | + } |
|
| 426 | + } |
|
| 427 | + $errorOutput .= $errorsEnd; |
|
| 428 | + echo ($echo) ? $errorOutput : ''; |
|
| 429 | + return (! $echo) ? $errorOutput : null; |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + /** |
|
| 433 | + * Returns raw array of errors in no format instead of displaying them |
|
| 434 | + * formatted. |
|
| 435 | + * |
|
| 436 | + * @return array |
|
| 437 | + */ |
|
| 438 | + public function returnErrors() { |
|
| 439 | + return $this->_errors; |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + /** |
|
| 443 | + * Breaks up a PIPE seperated string of rules, and puts them into an array. |
|
| 444 | + * |
|
| 445 | + * @param string $ruleString String to be parsed. |
|
| 446 | + * |
|
| 447 | + * @return array Array of each value in original string. |
|
| 448 | + */ |
|
| 449 | + protected function _parseRuleString($ruleString) { |
|
| 450 | + $ruleSets = array(); |
|
| 451 | + /* |
|
| 452 | 452 | //////////////// hack for regex rule that can contain "|" |
| 453 | 453 | */ |
| 454 | - if(strpos($ruleString, 'regex') !== false){ |
|
| 455 | - $regexRule = array(); |
|
| 456 | - $rule = '#regex\[\/(.*)\/([a-zA-Z0-9]?)\]#'; |
|
| 457 | - preg_match($rule, $ruleString, $regexRule); |
|
| 458 | - $ruleStringTemp = preg_replace($rule, '', $ruleString); |
|
| 459 | - if(isset($regexRule[0]) && !empty($regexRule[0])){ |
|
| 460 | - $ruleSets[] = $regexRule[0]; |
|
| 461 | - } |
|
| 462 | - $ruleStringRegex = explode('|', $ruleStringTemp); |
|
| 463 | - foreach ($ruleStringRegex as $rule) { |
|
| 464 | - $rule = trim($rule); |
|
| 465 | - if($rule){ |
|
| 466 | - $ruleSets[] = $rule; |
|
| 467 | - } |
|
| 468 | - } |
|
| 454 | + if(strpos($ruleString, 'regex') !== false){ |
|
| 455 | + $regexRule = array(); |
|
| 456 | + $rule = '#regex\[\/(.*)\/([a-zA-Z0-9]?)\]#'; |
|
| 457 | + preg_match($rule, $ruleString, $regexRule); |
|
| 458 | + $ruleStringTemp = preg_replace($rule, '', $ruleString); |
|
| 459 | + if(isset($regexRule[0]) && !empty($regexRule[0])){ |
|
| 460 | + $ruleSets[] = $regexRule[0]; |
|
| 461 | + } |
|
| 462 | + $ruleStringRegex = explode('|', $ruleStringTemp); |
|
| 463 | + foreach ($ruleStringRegex as $rule) { |
|
| 464 | + $rule = trim($rule); |
|
| 465 | + if($rule){ |
|
| 466 | + $ruleSets[] = $rule; |
|
| 467 | + } |
|
| 468 | + } |
|
| 469 | 469 | |
| 470 | - } |
|
| 471 | - /***********************************/ |
|
| 472 | - else{ |
|
| 473 | - if (strpos($ruleString, '|') !== FALSE) { |
|
| 474 | - $ruleSets = explode('|', $ruleString); |
|
| 475 | - } else { |
|
| 476 | - $ruleSets[] = $ruleString; |
|
| 477 | - } |
|
| 478 | - } |
|
| 479 | - return $ruleSets; |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - /** |
|
| 483 | - * Returns whether or not a field obtains the rule "required". |
|
| 484 | - * |
|
| 485 | - * @param string $fieldName Field to check if required. |
|
| 486 | - * |
|
| 487 | - * @return boolean Whether or not the field is required. |
|
| 488 | - */ |
|
| 489 | - protected function _fieldIsRequired($fieldName) { |
|
| 490 | - $rules = $this->_parseRuleString($this->_rules[$fieldName]); |
|
| 491 | - return (in_array('required', $rules)); |
|
| 492 | - } |
|
| 493 | - |
|
| 494 | - /** |
|
| 495 | - * Takes a data input name, it's value, and the rule it's being validated against (ex: max_length[16]) |
|
| 496 | - * and adds an error to the errorSet if it fails validation of the rule. |
|
| 497 | - * |
|
| 498 | - * @param string $inputName Name or key of the validation data |
|
| 499 | - * @param string $inputVal Value of the validation data |
|
| 500 | - * @param string $ruleName Rule to be validated against, including args (exact_length[5]) |
|
| 501 | - * @return void |
|
| 502 | - */ |
|
| 503 | - protected function _validateRule($inputName, $inputVal, $ruleName) { |
|
| 504 | - $this->logger->debug('Rule validation of field [' .$inputName. '], value [' .$inputVal. '], rule [' .$ruleName. ']'); |
|
| 505 | - // Array to store args |
|
| 506 | - $ruleArgs = array(); |
|
| 507 | - |
|
| 508 | - preg_match('/\[(.*)\]/', $ruleName, $ruleArgs); |
|
| 509 | - |
|
| 510 | - // Get the rule arguments, realRule is just the base rule name |
|
| 511 | - // Like min_length instead of min_length[3] |
|
| 512 | - $ruleName = preg_replace('/\[(.*)\]/', '', $ruleName); |
|
| 470 | + } |
|
| 471 | + /***********************************/ |
|
| 472 | + else{ |
|
| 473 | + if (strpos($ruleString, '|') !== FALSE) { |
|
| 474 | + $ruleSets = explode('|', $ruleString); |
|
| 475 | + } else { |
|
| 476 | + $ruleSets[] = $ruleString; |
|
| 477 | + } |
|
| 478 | + } |
|
| 479 | + return $ruleSets; |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + /** |
|
| 483 | + * Returns whether or not a field obtains the rule "required". |
|
| 484 | + * |
|
| 485 | + * @param string $fieldName Field to check if required. |
|
| 486 | + * |
|
| 487 | + * @return boolean Whether or not the field is required. |
|
| 488 | + */ |
|
| 489 | + protected function _fieldIsRequired($fieldName) { |
|
| 490 | + $rules = $this->_parseRuleString($this->_rules[$fieldName]); |
|
| 491 | + return (in_array('required', $rules)); |
|
| 492 | + } |
|
| 493 | + |
|
| 494 | + /** |
|
| 495 | + * Takes a data input name, it's value, and the rule it's being validated against (ex: max_length[16]) |
|
| 496 | + * and adds an error to the errorSet if it fails validation of the rule. |
|
| 497 | + * |
|
| 498 | + * @param string $inputName Name or key of the validation data |
|
| 499 | + * @param string $inputVal Value of the validation data |
|
| 500 | + * @param string $ruleName Rule to be validated against, including args (exact_length[5]) |
|
| 501 | + * @return void |
|
| 502 | + */ |
|
| 503 | + protected function _validateRule($inputName, $inputVal, $ruleName) { |
|
| 504 | + $this->logger->debug('Rule validation of field [' .$inputName. '], value [' .$inputVal. '], rule [' .$ruleName. ']'); |
|
| 505 | + // Array to store args |
|
| 506 | + $ruleArgs = array(); |
|
| 507 | + |
|
| 508 | + preg_match('/\[(.*)\]/', $ruleName, $ruleArgs); |
|
| 509 | + |
|
| 510 | + // Get the rule arguments, realRule is just the base rule name |
|
| 511 | + // Like min_length instead of min_length[3] |
|
| 512 | + $ruleName = preg_replace('/\[(.*)\]/', '', $ruleName); |
|
| 513 | 513 | |
| 514 | - if (method_exists($this, $this->_toCallCase($ruleName))) { |
|
| 515 | - $methodToCall = $this->_toCallCase($ruleName); |
|
| 516 | - call_user_func(array($this, $methodToCall), $inputName, $ruleName, $ruleArgs); |
|
| 517 | - } |
|
| 518 | - return; |
|
| 519 | - } |
|
| 520 | - |
|
| 521 | - /** |
|
| 522 | - * Set error for the given field or key |
|
| 523 | - * |
|
| 524 | - * @param string $inputName the input or key name |
|
| 525 | - * @param string $ruleName the rule name |
|
| 526 | - * @param array|string $replacements |
|
| 527 | - */ |
|
| 528 | - protected function _setError($inputName, $ruleName, $replacements = array()) { |
|
| 529 | - $rulePhraseKeyParts = explode(',', $ruleName); |
|
| 530 | - $rulePhrase = null; |
|
| 531 | - foreach ($rulePhraseKeyParts as $rulePhraseKeyPart) { |
|
| 532 | - if (array_key_exists($rulePhraseKeyPart, $this->_errorsMessages)) { |
|
| 533 | - $rulePhrase = $this->_errorsMessages[$rulePhraseKeyPart]; |
|
| 534 | - } else { |
|
| 535 | - $rulePhrase = $rulePhrase[$rulePhraseKeyPart]; |
|
| 536 | - } |
|
| 537 | - } |
|
| 538 | - // Any overrides? |
|
| 539 | - if (array_key_exists($inputName, $this->_errorPhraseOverrides) && array_key_exists($ruleName, $this->_errorPhraseOverrides[$inputName])) { |
|
| 540 | - $rulePhrase = $this->_errorPhraseOverrides[$inputName][$ruleName]; |
|
| 541 | - } |
|
| 542 | - // Type cast to array in case it's a string |
|
| 543 | - $replacements = (array) $replacements; |
|
| 514 | + if (method_exists($this, $this->_toCallCase($ruleName))) { |
|
| 515 | + $methodToCall = $this->_toCallCase($ruleName); |
|
| 516 | + call_user_func(array($this, $methodToCall), $inputName, $ruleName, $ruleArgs); |
|
| 517 | + } |
|
| 518 | + return; |
|
| 519 | + } |
|
| 520 | + |
|
| 521 | + /** |
|
| 522 | + * Set error for the given field or key |
|
| 523 | + * |
|
| 524 | + * @param string $inputName the input or key name |
|
| 525 | + * @param string $ruleName the rule name |
|
| 526 | + * @param array|string $replacements |
|
| 527 | + */ |
|
| 528 | + protected function _setError($inputName, $ruleName, $replacements = array()) { |
|
| 529 | + $rulePhraseKeyParts = explode(',', $ruleName); |
|
| 530 | + $rulePhrase = null; |
|
| 531 | + foreach ($rulePhraseKeyParts as $rulePhraseKeyPart) { |
|
| 532 | + if (array_key_exists($rulePhraseKeyPart, $this->_errorsMessages)) { |
|
| 533 | + $rulePhrase = $this->_errorsMessages[$rulePhraseKeyPart]; |
|
| 534 | + } else { |
|
| 535 | + $rulePhrase = $rulePhrase[$rulePhraseKeyPart]; |
|
| 536 | + } |
|
| 537 | + } |
|
| 538 | + // Any overrides? |
|
| 539 | + if (array_key_exists($inputName, $this->_errorPhraseOverrides) && array_key_exists($ruleName, $this->_errorPhraseOverrides[$inputName])) { |
|
| 540 | + $rulePhrase = $this->_errorPhraseOverrides[$inputName][$ruleName]; |
|
| 541 | + } |
|
| 542 | + // Type cast to array in case it's a string |
|
| 543 | + $replacements = (array) $replacements; |
|
| 544 | 544 | $replacementCount = count($replacements); |
| 545 | - for ($i = 1; $i <= $replacementCount; $i++) { |
|
| 546 | - $key = $i - 1; |
|
| 547 | - $rulePhrase = str_replace('%' . $i, $replacements[$key], $rulePhrase); |
|
| 548 | - } |
|
| 549 | - if (! array_key_exists($inputName, $this->_errors)) { |
|
| 550 | - $this->_errors[$inputName] = $rulePhrase; |
|
| 551 | - } |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - /** |
|
| 555 | - * Used to run a callback for the callback rule, as well as pass in a default |
|
| 556 | - * argument of the post value. For example the username field having a rule: |
|
| 557 | - * callback[userExists] will eval userExists(data[username]) - Note the use |
|
| 558 | - * of eval over call_user_func is in case the function is not user defined. |
|
| 559 | - * |
|
| 560 | - * @param type $inputArg |
|
| 561 | - * @param string $callbackFunc |
|
| 562 | - * |
|
| 563 | - * @return mixed |
|
| 564 | - */ |
|
| 565 | - protected function _runCallback($inputArg, $callbackFunc) { |
|
| 545 | + for ($i = 1; $i <= $replacementCount; $i++) { |
|
| 546 | + $key = $i - 1; |
|
| 547 | + $rulePhrase = str_replace('%' . $i, $replacements[$key], $rulePhrase); |
|
| 548 | + } |
|
| 549 | + if (! array_key_exists($inputName, $this->_errors)) { |
|
| 550 | + $this->_errors[$inputName] = $rulePhrase; |
|
| 551 | + } |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + /** |
|
| 555 | + * Used to run a callback for the callback rule, as well as pass in a default |
|
| 556 | + * argument of the post value. For example the username field having a rule: |
|
| 557 | + * callback[userExists] will eval userExists(data[username]) - Note the use |
|
| 558 | + * of eval over call_user_func is in case the function is not user defined. |
|
| 559 | + * |
|
| 560 | + * @param type $inputArg |
|
| 561 | + * @param string $callbackFunc |
|
| 562 | + * |
|
| 563 | + * @return mixed |
|
| 564 | + */ |
|
| 565 | + protected function _runCallback($inputArg, $callbackFunc) { |
|
| 566 | 566 | return eval('return ' . $callbackFunc . '("' . $inputArg . '");'); |
| 567 | - } |
|
| 568 | - |
|
| 569 | - /** |
|
| 570 | - * Used for applying a rule only if the empty callback evaluates to true, |
|
| 571 | - * for example required[funcName] - This runs funcName without passing any |
|
| 572 | - * arguments. |
|
| 573 | - * |
|
| 574 | - * @param string $callbackFunc |
|
| 575 | - * |
|
| 576 | - * @return anything |
|
| 577 | - */ |
|
| 578 | - protected function _runEmptyCallback($callbackFunc) { |
|
| 579 | - return eval('return ' . $callbackFunc . '();'); |
|
| 580 | - } |
|
| 581 | - |
|
| 582 | - /** |
|
| 583 | - * Gets a specific label of a specific field input name. |
|
| 584 | - * |
|
| 585 | - * @param string $inputName |
|
| 586 | - * |
|
| 587 | - * @return string |
|
| 588 | - */ |
|
| 589 | - protected function _getLabel($inputName) { |
|
| 590 | - return (array_key_exists($inputName, $this->_labels)) ? $this->_labels[$inputName] : $inputName; |
|
| 591 | - } |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + /** |
|
| 570 | + * Used for applying a rule only if the empty callback evaluates to true, |
|
| 571 | + * for example required[funcName] - This runs funcName without passing any |
|
| 572 | + * arguments. |
|
| 573 | + * |
|
| 574 | + * @param string $callbackFunc |
|
| 575 | + * |
|
| 576 | + * @return anything |
|
| 577 | + */ |
|
| 578 | + protected function _runEmptyCallback($callbackFunc) { |
|
| 579 | + return eval('return ' . $callbackFunc . '();'); |
|
| 580 | + } |
|
| 581 | + |
|
| 582 | + /** |
|
| 583 | + * Gets a specific label of a specific field input name. |
|
| 584 | + * |
|
| 585 | + * @param string $inputName |
|
| 586 | + * |
|
| 587 | + * @return string |
|
| 588 | + */ |
|
| 589 | + protected function _getLabel($inputName) { |
|
| 590 | + return (array_key_exists($inputName, $this->_labels)) ? $this->_labels[$inputName] : $inputName; |
|
| 591 | + } |
|
| 592 | 592 | |
| 593 | - /** |
|
| 594 | - * Peform validation for the rule "required" |
|
| 595 | - * @param string $inputName the form field or data key name used |
|
| 596 | - * @param string $ruleName the rule name for this validation ("required") |
|
| 597 | - * @param array $ruleArgs the rules argument |
|
| 598 | - */ |
|
| 593 | + /** |
|
| 594 | + * Peform validation for the rule "required" |
|
| 595 | + * @param string $inputName the form field or data key name used |
|
| 596 | + * @param string $ruleName the rule name for this validation ("required") |
|
| 597 | + * @param array $ruleArgs the rules argument |
|
| 598 | + */ |
|
| 599 | 599 | protected function _validateRequired($inputName, $ruleName, array $ruleArgs) { |
| 600 | - $inputVal = $this->post($inputName); |
|
| 601 | - if(array_key_exists(1, $ruleArgs) && function_exists($ruleArgs[1])) { |
|
| 602 | - $callbackReturn = $this->_runEmptyCallback($ruleArgs[1]); |
|
| 603 | - if ($inputVal == '' && $callbackReturn == true) { |
|
| 604 | - $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
| 605 | - } |
|
| 606 | - } |
|
| 600 | + $inputVal = $this->post($inputName); |
|
| 601 | + if(array_key_exists(1, $ruleArgs) && function_exists($ruleArgs[1])) { |
|
| 602 | + $callbackReturn = $this->_runEmptyCallback($ruleArgs[1]); |
|
| 603 | + if ($inputVal == '' && $callbackReturn == true) { |
|
| 604 | + $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
| 605 | + } |
|
| 606 | + } |
|
| 607 | 607 | else if($inputVal == '') { |
| 608 | 608 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
| 609 | - } |
|
| 610 | - } |
|
| 611 | - |
|
| 612 | - /** |
|
| 613 | - * Perform validation for the honey pot so means for the validation to be failed |
|
| 614 | - * @param string $inputName the form field or data key name used |
|
| 615 | - * @param string $ruleName the rule name for this validation |
|
| 616 | - * @param array $ruleArgs the rules argument |
|
| 617 | - */ |
|
| 618 | - protected function _validateHoneypot($inputName, $ruleName, array $ruleArgs) { |
|
| 619 | - if ($this->data[$inputName] != '') { |
|
| 620 | - $this->_forceFail = true; |
|
| 621 | - } |
|
| 622 | - } |
|
| 623 | - |
|
| 624 | - /** |
|
| 625 | - * Peform validation for the rule "callback" |
|
| 626 | - * @param string $inputName the form field or data key name used |
|
| 627 | - * @param string $ruleName the rule name for this validation ("callback") |
|
| 628 | - * @param array $ruleArgs the rules argument |
|
| 629 | - */ |
|
| 630 | - protected function _validateCallback($inputName, $ruleName, array $ruleArgs) { |
|
| 631 | - if (function_exists($ruleArgs[1]) && !empty($this->data[$inputName])) { |
|
| 609 | + } |
|
| 610 | + } |
|
| 611 | + |
|
| 612 | + /** |
|
| 613 | + * Perform validation for the honey pot so means for the validation to be failed |
|
| 614 | + * @param string $inputName the form field or data key name used |
|
| 615 | + * @param string $ruleName the rule name for this validation |
|
| 616 | + * @param array $ruleArgs the rules argument |
|
| 617 | + */ |
|
| 618 | + protected function _validateHoneypot($inputName, $ruleName, array $ruleArgs) { |
|
| 619 | + if ($this->data[$inputName] != '') { |
|
| 620 | + $this->_forceFail = true; |
|
| 621 | + } |
|
| 622 | + } |
|
| 623 | + |
|
| 624 | + /** |
|
| 625 | + * Peform validation for the rule "callback" |
|
| 626 | + * @param string $inputName the form field or data key name used |
|
| 627 | + * @param string $ruleName the rule name for this validation ("callback") |
|
| 628 | + * @param array $ruleArgs the rules argument |
|
| 629 | + */ |
|
| 630 | + protected function _validateCallback($inputName, $ruleName, array $ruleArgs) { |
|
| 631 | + if (function_exists($ruleArgs[1]) && !empty($this->data[$inputName])) { |
|
| 632 | 632 | $result = $this->_runCallback($this->data[$inputName], $ruleArgs[1]); |
| 633 | 633 | if(! $result){ |
| 634 | 634 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
| 635 | 635 | } |
| 636 | - } |
|
| 637 | - } |
|
| 638 | - |
|
| 639 | - /** |
|
| 640 | - * Peform validation for the rule "depends" |
|
| 641 | - * @param string $inputName the form field or data key name used |
|
| 642 | - * @param string $ruleName the rule name for this validation ("depends") |
|
| 643 | - * @param array $ruleArgs the rules argument |
|
| 644 | - */ |
|
| 645 | - protected function _validateDepends($inputName, $ruleName, array $ruleArgs) { |
|
| 646 | - if (array_key_exists($ruleArgs[1], $this->_errors)) { |
|
| 647 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 648 | - } |
|
| 649 | - } |
|
| 650 | - |
|
| 651 | - /** |
|
| 652 | - * Peform validation for the rule "not_equal" |
|
| 653 | - * @param string $inputName the form field or data key name used |
|
| 654 | - * @param string $ruleName the rule name for this validation ("not_equal") |
|
| 655 | - * @param array $ruleArgs the rules argument |
|
| 656 | - */ |
|
| 657 | - protected function _validateNotEqual($inputName, $ruleName, array $ruleArgs) { |
|
| 658 | - $canNotEqual = explode(',', $ruleArgs[1]); |
|
| 659 | - foreach ($canNotEqual as $doNotEqual) { |
|
| 660 | - $inputVal = $this->post($inputName); |
|
| 661 | - if (preg_match('/post:(.*)/', $doNotEqual)) { |
|
| 662 | - if ($inputVal == $this->data[str_replace('post:', '', $doNotEqual)]) { |
|
| 663 | - $this->_setError($inputName, $ruleName . ',post:key', array($this->_getLabel($inputName), $this->_getLabel(str_replace('post:', '', $doNotEqual)))); |
|
| 664 | - continue; |
|
| 665 | - } |
|
| 666 | - } |
|
| 636 | + } |
|
| 637 | + } |
|
| 638 | + |
|
| 639 | + /** |
|
| 640 | + * Peform validation for the rule "depends" |
|
| 641 | + * @param string $inputName the form field or data key name used |
|
| 642 | + * @param string $ruleName the rule name for this validation ("depends") |
|
| 643 | + * @param array $ruleArgs the rules argument |
|
| 644 | + */ |
|
| 645 | + protected function _validateDepends($inputName, $ruleName, array $ruleArgs) { |
|
| 646 | + if (array_key_exists($ruleArgs[1], $this->_errors)) { |
|
| 647 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 648 | + } |
|
| 649 | + } |
|
| 650 | + |
|
| 651 | + /** |
|
| 652 | + * Peform validation for the rule "not_equal" |
|
| 653 | + * @param string $inputName the form field or data key name used |
|
| 654 | + * @param string $ruleName the rule name for this validation ("not_equal") |
|
| 655 | + * @param array $ruleArgs the rules argument |
|
| 656 | + */ |
|
| 657 | + protected function _validateNotEqual($inputName, $ruleName, array $ruleArgs) { |
|
| 658 | + $canNotEqual = explode(',', $ruleArgs[1]); |
|
| 659 | + foreach ($canNotEqual as $doNotEqual) { |
|
| 660 | + $inputVal = $this->post($inputName); |
|
| 661 | + if (preg_match('/post:(.*)/', $doNotEqual)) { |
|
| 662 | + if ($inputVal == $this->data[str_replace('post:', '', $doNotEqual)]) { |
|
| 663 | + $this->_setError($inputName, $ruleName . ',post:key', array($this->_getLabel($inputName), $this->_getLabel(str_replace('post:', '', $doNotEqual)))); |
|
| 664 | + continue; |
|
| 665 | + } |
|
| 666 | + } |
|
| 667 | 667 | else{ |
| 668 | - if ($inputVal == $doNotEqual) { |
|
| 669 | - $this->_setError($inputName, $ruleName . ',string', array($this->_getLabel($inputName), $doNotEqual)); |
|
| 670 | - continue; |
|
| 671 | - } |
|
| 672 | - } |
|
| 673 | - } |
|
| 674 | - } |
|
| 675 | - |
|
| 676 | - /** |
|
| 677 | - * Peform validation for the rule "matches" |
|
| 678 | - * @param string $inputName the form field or data key name used |
|
| 679 | - * @param string $ruleName the rule name for this validation ("matches") |
|
| 680 | - * @param array $ruleArgs the rules argument |
|
| 681 | - */ |
|
| 682 | - protected function _validateMatches($inputName, $ruleName, array $ruleArgs) { |
|
| 683 | - $inputVal = $this->post($inputName); |
|
| 684 | - if ($inputVal != $this->data[$ruleArgs[1]]) { |
|
| 685 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 686 | - } |
|
| 687 | - } |
|
| 688 | - |
|
| 689 | - /** |
|
| 690 | - * Peform validation for the rule "valid_email" |
|
| 691 | - * @param string $inputName the form field or data key name used |
|
| 692 | - * @param string $ruleName the rule name for this validation ("valid_email") |
|
| 693 | - * @param array $ruleArgs the rules argument |
|
| 694 | - */ |
|
| 695 | - protected function _validateValidEmail($inputName, $ruleName, array $ruleArgs) { |
|
| 696 | - $inputVal = $this->post($inputName); |
|
| 697 | - 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)) { |
|
| 698 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 699 | - return; |
|
| 700 | - } |
|
| 701 | - $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
| 702 | - } |
|
| 703 | - } |
|
| 704 | - |
|
| 705 | - /** |
|
| 706 | - * Peform validation for the rule "exact_length" |
|
| 707 | - * @param string $inputName the form field or data key name used |
|
| 708 | - * @param string $ruleName the rule name for this validation ("exact_length") |
|
| 709 | - * @param array $ruleArgs the rules argument |
|
| 710 | - */ |
|
| 711 | - protected function _validateExactLength($inputName, $ruleName, array $ruleArgs) { |
|
| 712 | - $inputVal = $this->post($inputName); |
|
| 713 | - if (strlen($inputVal) != $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 714 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 715 | - return; |
|
| 716 | - } |
|
| 717 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 718 | - } |
|
| 719 | - } |
|
| 720 | - |
|
| 721 | - /** |
|
| 722 | - * Peform validation for the rule "max_length" |
|
| 723 | - * @param string $inputName the form field or data key name used |
|
| 724 | - * @param string $ruleName the rule name for this validation ("max_length") |
|
| 725 | - * @param array $ruleArgs the rules argument |
|
| 726 | - */ |
|
| 727 | - protected function _validateMaxLength($inputName, $ruleName, array $ruleArgs) { |
|
| 728 | - $inputVal = $this->post($inputName); |
|
| 729 | - if (strlen($inputVal) > $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 730 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 731 | - return; |
|
| 732 | - } |
|
| 733 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 734 | - } |
|
| 735 | - } |
|
| 736 | - |
|
| 737 | - /** |
|
| 738 | - * Peform validation for the rule "min_length" |
|
| 739 | - * @param string $inputName the form field or data key name used |
|
| 740 | - * @param string $ruleName the rule name for this validation ("min_length") |
|
| 741 | - * @param array $ruleArgs the rules argument |
|
| 742 | - */ |
|
| 743 | - protected function _validateMinLength($inputName, $ruleName, array $ruleArgs) { |
|
| 744 | - $inputVal = $this->post($inputName); |
|
| 745 | - if (strlen($inputVal) < $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 746 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 747 | - return; |
|
| 748 | - } |
|
| 749 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 750 | - } |
|
| 751 | - } |
|
| 668 | + if ($inputVal == $doNotEqual) { |
|
| 669 | + $this->_setError($inputName, $ruleName . ',string', array($this->_getLabel($inputName), $doNotEqual)); |
|
| 670 | + continue; |
|
| 671 | + } |
|
| 672 | + } |
|
| 673 | + } |
|
| 674 | + } |
|
| 675 | + |
|
| 676 | + /** |
|
| 677 | + * Peform validation for the rule "matches" |
|
| 678 | + * @param string $inputName the form field or data key name used |
|
| 679 | + * @param string $ruleName the rule name for this validation ("matches") |
|
| 680 | + * @param array $ruleArgs the rules argument |
|
| 681 | + */ |
|
| 682 | + protected function _validateMatches($inputName, $ruleName, array $ruleArgs) { |
|
| 683 | + $inputVal = $this->post($inputName); |
|
| 684 | + if ($inputVal != $this->data[$ruleArgs[1]]) { |
|
| 685 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 686 | + } |
|
| 687 | + } |
|
| 688 | + |
|
| 689 | + /** |
|
| 690 | + * Peform validation for the rule "valid_email" |
|
| 691 | + * @param string $inputName the form field or data key name used |
|
| 692 | + * @param string $ruleName the rule name for this validation ("valid_email") |
|
| 693 | + * @param array $ruleArgs the rules argument |
|
| 694 | + */ |
|
| 695 | + protected function _validateValidEmail($inputName, $ruleName, array $ruleArgs) { |
|
| 696 | + $inputVal = $this->post($inputName); |
|
| 697 | + 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)) { |
|
| 698 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 699 | + return; |
|
| 700 | + } |
|
| 701 | + $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
| 702 | + } |
|
| 703 | + } |
|
| 704 | + |
|
| 705 | + /** |
|
| 706 | + * Peform validation for the rule "exact_length" |
|
| 707 | + * @param string $inputName the form field or data key name used |
|
| 708 | + * @param string $ruleName the rule name for this validation ("exact_length") |
|
| 709 | + * @param array $ruleArgs the rules argument |
|
| 710 | + */ |
|
| 711 | + protected function _validateExactLength($inputName, $ruleName, array $ruleArgs) { |
|
| 712 | + $inputVal = $this->post($inputName); |
|
| 713 | + if (strlen($inputVal) != $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 714 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 715 | + return; |
|
| 716 | + } |
|
| 717 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 718 | + } |
|
| 719 | + } |
|
| 720 | + |
|
| 721 | + /** |
|
| 722 | + * Peform validation for the rule "max_length" |
|
| 723 | + * @param string $inputName the form field or data key name used |
|
| 724 | + * @param string $ruleName the rule name for this validation ("max_length") |
|
| 725 | + * @param array $ruleArgs the rules argument |
|
| 726 | + */ |
|
| 727 | + protected function _validateMaxLength($inputName, $ruleName, array $ruleArgs) { |
|
| 728 | + $inputVal = $this->post($inputName); |
|
| 729 | + if (strlen($inputVal) > $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 730 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 731 | + return; |
|
| 732 | + } |
|
| 733 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 734 | + } |
|
| 735 | + } |
|
| 736 | + |
|
| 737 | + /** |
|
| 738 | + * Peform validation for the rule "min_length" |
|
| 739 | + * @param string $inputName the form field or data key name used |
|
| 740 | + * @param string $ruleName the rule name for this validation ("min_length") |
|
| 741 | + * @param array $ruleArgs the rules argument |
|
| 742 | + */ |
|
| 743 | + protected function _validateMinLength($inputName, $ruleName, array $ruleArgs) { |
|
| 744 | + $inputVal = $this->post($inputName); |
|
| 745 | + if (strlen($inputVal) < $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 746 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 747 | + return; |
|
| 748 | + } |
|
| 749 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 750 | + } |
|
| 751 | + } |
|
| 752 | 752 | |
| 753 | - /** |
|
| 754 | - * Peform validation for the rule "less_than" |
|
| 755 | - * @param string $inputName the form field or data key name used |
|
| 756 | - * @param string $ruleName the rule name for this validation ("less_than") |
|
| 757 | - * @param array $ruleArgs the rules argument |
|
| 758 | - */ |
|
| 759 | - protected function _validateLessThan($inputName, $ruleName, array $ruleArgs) { |
|
| 760 | - $inputVal = $this->post($inputName); |
|
| 761 | - if ($inputVal >= $ruleArgs[1]) { |
|
| 762 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 763 | - return; |
|
| 764 | - } |
|
| 765 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 766 | - } |
|
| 767 | - } |
|
| 753 | + /** |
|
| 754 | + * Peform validation for the rule "less_than" |
|
| 755 | + * @param string $inputName the form field or data key name used |
|
| 756 | + * @param string $ruleName the rule name for this validation ("less_than") |
|
| 757 | + * @param array $ruleArgs the rules argument |
|
| 758 | + */ |
|
| 759 | + protected function _validateLessThan($inputName, $ruleName, array $ruleArgs) { |
|
| 760 | + $inputVal = $this->post($inputName); |
|
| 761 | + if ($inputVal >= $ruleArgs[1]) { |
|
| 762 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 763 | + return; |
|
| 764 | + } |
|
| 765 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 766 | + } |
|
| 767 | + } |
|
| 768 | 768 | |
| 769 | - /** |
|
| 770 | - * Peform validation for the rule "greater_than" |
|
| 771 | - * @param string $inputName the form field or data key name used |
|
| 772 | - * @param string $ruleName the rule name for this validation ("greater_than") |
|
| 773 | - * @param array $ruleArgs the rules argument |
|
| 774 | - */ |
|
| 775 | - protected function _validateGreaterThan($inputName, $ruleName, array $ruleArgs) { |
|
| 776 | - $inputVal = $this->post($inputName); |
|
| 777 | - if ($inputVal <= $ruleArgs[1]) { |
|
| 778 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 779 | - return; |
|
| 780 | - } |
|
| 781 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 782 | - } |
|
| 783 | - } |
|
| 769 | + /** |
|
| 770 | + * Peform validation for the rule "greater_than" |
|
| 771 | + * @param string $inputName the form field or data key name used |
|
| 772 | + * @param string $ruleName the rule name for this validation ("greater_than") |
|
| 773 | + * @param array $ruleArgs the rules argument |
|
| 774 | + */ |
|
| 775 | + protected function _validateGreaterThan($inputName, $ruleName, array $ruleArgs) { |
|
| 776 | + $inputVal = $this->post($inputName); |
|
| 777 | + if ($inputVal <= $ruleArgs[1]) { |
|
| 778 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 779 | + return; |
|
| 780 | + } |
|
| 781 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 782 | + } |
|
| 783 | + } |
|
| 784 | 784 | |
| 785 | - /** |
|
| 786 | - * Peform validation for the rule "numeric" |
|
| 787 | - * @param string $inputName the form field or data key name used |
|
| 788 | - * @param string $ruleName the rule name for this validation ("numeric") |
|
| 789 | - * @param array $ruleArgs the rules argument |
|
| 790 | - */ |
|
| 791 | - protected function _validateNumeric($inputName, $ruleName, array $ruleArgs) { |
|
| 792 | - $inputVal = $this->post($inputName); |
|
| 793 | - if (! is_numeric($inputVal)) { |
|
| 794 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 795 | - return; |
|
| 796 | - } |
|
| 797 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 798 | - } |
|
| 799 | - } |
|
| 785 | + /** |
|
| 786 | + * Peform validation for the rule "numeric" |
|
| 787 | + * @param string $inputName the form field or data key name used |
|
| 788 | + * @param string $ruleName the rule name for this validation ("numeric") |
|
| 789 | + * @param array $ruleArgs the rules argument |
|
| 790 | + */ |
|
| 791 | + protected function _validateNumeric($inputName, $ruleName, array $ruleArgs) { |
|
| 792 | + $inputVal = $this->post($inputName); |
|
| 793 | + if (! is_numeric($inputVal)) { |
|
| 794 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 795 | + return; |
|
| 796 | + } |
|
| 797 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 798 | + } |
|
| 799 | + } |
|
| 800 | 800 | |
| 801 | - /** |
|
| 802 | - * Peform validation for the rule "exists" |
|
| 803 | - * @param string $inputName the form field or data key name used |
|
| 804 | - * @param string $ruleName the rule name for this validation ("exists") |
|
| 805 | - * @param array $ruleArgs the rules argument |
|
| 806 | - */ |
|
| 801 | + /** |
|
| 802 | + * Peform validation for the rule "exists" |
|
| 803 | + * @param string $inputName the form field or data key name used |
|
| 804 | + * @param string $ruleName the rule name for this validation ("exists") |
|
| 805 | + * @param array $ruleArgs the rules argument |
|
| 806 | + */ |
|
| 807 | 807 | protected function _validateExists($inputName, $ruleName, array $ruleArgs) { |
| 808 | - $inputVal = $this->post($inputName); |
|
| 809 | - $obj = & get_instance(); |
|
| 810 | - if(! isset($obj->database)){ |
|
| 811 | - return; |
|
| 812 | - } |
|
| 813 | - list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 814 | - $obj->database->from($table) |
|
| 815 | - ->where($column, $inputVal) |
|
| 816 | - ->get(); |
|
| 817 | - $nb = $obj->database->numRows(); |
|
| 818 | - if ($nb == 0) { |
|
| 819 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 820 | - return; |
|
| 821 | - } |
|
| 822 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 823 | - } |
|
| 824 | - } |
|
| 825 | - |
|
| 826 | - /** |
|
| 827 | - * Peform validation for the rule "is_unique" |
|
| 828 | - * @param string $inputName the form field or data key name used |
|
| 829 | - * @param string $ruleName the rule name for this validation ("is_unique") |
|
| 830 | - * @param array $ruleArgs the rules argument |
|
| 831 | - */ |
|
| 832 | - protected function _validateIsUnique($inputName, $ruleName, array $ruleArgs) { |
|
| 833 | - $inputVal = $this->post($inputName); |
|
| 834 | - $obj = & get_instance(); |
|
| 835 | - if(! isset($obj->database)){ |
|
| 836 | - return; |
|
| 837 | - } |
|
| 838 | - list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 839 | - $obj->database->from($table) |
|
| 840 | - ->where($column, $inputVal) |
|
| 841 | - ->get(); |
|
| 842 | - $nb = $obj->database->numRows(); |
|
| 843 | - if ($nb != 0) { |
|
| 844 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 845 | - return; |
|
| 846 | - } |
|
| 847 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 848 | - } |
|
| 849 | - } |
|
| 808 | + $inputVal = $this->post($inputName); |
|
| 809 | + $obj = & get_instance(); |
|
| 810 | + if(! isset($obj->database)){ |
|
| 811 | + return; |
|
| 812 | + } |
|
| 813 | + list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 814 | + $obj->database->from($table) |
|
| 815 | + ->where($column, $inputVal) |
|
| 816 | + ->get(); |
|
| 817 | + $nb = $obj->database->numRows(); |
|
| 818 | + if ($nb == 0) { |
|
| 819 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 820 | + return; |
|
| 821 | + } |
|
| 822 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 823 | + } |
|
| 824 | + } |
|
| 825 | + |
|
| 826 | + /** |
|
| 827 | + * Peform validation for the rule "is_unique" |
|
| 828 | + * @param string $inputName the form field or data key name used |
|
| 829 | + * @param string $ruleName the rule name for this validation ("is_unique") |
|
| 830 | + * @param array $ruleArgs the rules argument |
|
| 831 | + */ |
|
| 832 | + protected function _validateIsUnique($inputName, $ruleName, array $ruleArgs) { |
|
| 833 | + $inputVal = $this->post($inputName); |
|
| 834 | + $obj = & get_instance(); |
|
| 835 | + if(! isset($obj->database)){ |
|
| 836 | + return; |
|
| 837 | + } |
|
| 838 | + list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 839 | + $obj->database->from($table) |
|
| 840 | + ->where($column, $inputVal) |
|
| 841 | + ->get(); |
|
| 842 | + $nb = $obj->database->numRows(); |
|
| 843 | + if ($nb != 0) { |
|
| 844 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 845 | + return; |
|
| 846 | + } |
|
| 847 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 848 | + } |
|
| 849 | + } |
|
| 850 | 850 | |
| 851 | - /** |
|
| 852 | - * Peform validation for the rule "is_unique_update" |
|
| 853 | - * @param string $inputName the form field or data key name used |
|
| 854 | - * @param string $ruleName the rule name for this validation ("is_unique_update") |
|
| 855 | - * @param array $ruleArgs the rules argument |
|
| 856 | - */ |
|
| 857 | - protected function _validateIsUniqueUpdate($inputName, $ruleName, array $ruleArgs) { |
|
| 858 | - $inputVal = $this->post($inputName); |
|
| 859 | - $obj = & get_instance(); |
|
| 860 | - if(! isset($obj->database)){ |
|
| 861 | - return; |
|
| 862 | - } |
|
| 863 | - $data = explode(',', $ruleArgs[1]); |
|
| 864 | - if(count($data) < 2){ |
|
| 865 | - return; |
|
| 866 | - } |
|
| 867 | - list($table, $column) = explode('.', $data[0]); |
|
| 868 | - list($field, $val) = explode('=', $data[1]); |
|
| 869 | - $obj->database->from($table) |
|
| 870 | - ->where($column, $inputVal) |
|
| 871 | - ->where($field, '!=', trim($val)) |
|
| 872 | - ->get(); |
|
| 873 | - $nb = $obj->database->numRows(); |
|
| 874 | - if ($nb != 0) { |
|
| 875 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 876 | - return; |
|
| 877 | - } |
|
| 878 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 879 | - } |
|
| 880 | - } |
|
| 881 | - |
|
| 882 | - /** |
|
| 883 | - * Peform validation for the rule "in_list" |
|
| 884 | - * @param string $inputName the form field or data key name used |
|
| 885 | - * @param string $ruleName the rule name for this validation ("in_list") |
|
| 886 | - * @param array $ruleArgs the rules argument |
|
| 887 | - */ |
|
| 888 | - protected function _validateInList($inputName, $ruleName, array $ruleArgs) { |
|
| 889 | - $inputVal = $this->post($inputName); |
|
| 890 | - $list = explode(',', $ruleArgs[1]); |
|
| 891 | - $list = array_map('trim', $list); |
|
| 892 | - if (! in_array($inputVal, $list)) { |
|
| 893 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 894 | - return; |
|
| 895 | - } |
|
| 896 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 897 | - } |
|
| 898 | - } |
|
| 899 | - |
|
| 900 | - /** |
|
| 901 | - * Peform validation for the rule "regex" |
|
| 902 | - * @param string $inputName the form field or data key name used |
|
| 903 | - * @param string $ruleName the rule name for this validation ("regex") |
|
| 904 | - * @param array $ruleArgs the rules argument |
|
| 905 | - */ |
|
| 906 | - protected function _validateRegex($inputName, $ruleName, array $ruleArgs) { |
|
| 907 | - $inputVal = $this->post($inputName); |
|
| 908 | - $regex = $ruleArgs[1]; |
|
| 909 | - if (! preg_match($regex, $inputVal)) { |
|
| 910 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 911 | - return; |
|
| 912 | - } |
|
| 913 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 914 | - } |
|
| 915 | - } |
|
| 851 | + /** |
|
| 852 | + * Peform validation for the rule "is_unique_update" |
|
| 853 | + * @param string $inputName the form field or data key name used |
|
| 854 | + * @param string $ruleName the rule name for this validation ("is_unique_update") |
|
| 855 | + * @param array $ruleArgs the rules argument |
|
| 856 | + */ |
|
| 857 | + protected function _validateIsUniqueUpdate($inputName, $ruleName, array $ruleArgs) { |
|
| 858 | + $inputVal = $this->post($inputName); |
|
| 859 | + $obj = & get_instance(); |
|
| 860 | + if(! isset($obj->database)){ |
|
| 861 | + return; |
|
| 862 | + } |
|
| 863 | + $data = explode(',', $ruleArgs[1]); |
|
| 864 | + if(count($data) < 2){ |
|
| 865 | + return; |
|
| 866 | + } |
|
| 867 | + list($table, $column) = explode('.', $data[0]); |
|
| 868 | + list($field, $val) = explode('=', $data[1]); |
|
| 869 | + $obj->database->from($table) |
|
| 870 | + ->where($column, $inputVal) |
|
| 871 | + ->where($field, '!=', trim($val)) |
|
| 872 | + ->get(); |
|
| 873 | + $nb = $obj->database->numRows(); |
|
| 874 | + if ($nb != 0) { |
|
| 875 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 876 | + return; |
|
| 877 | + } |
|
| 878 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 879 | + } |
|
| 880 | + } |
|
| 881 | + |
|
| 882 | + /** |
|
| 883 | + * Peform validation for the rule "in_list" |
|
| 884 | + * @param string $inputName the form field or data key name used |
|
| 885 | + * @param string $ruleName the rule name for this validation ("in_list") |
|
| 886 | + * @param array $ruleArgs the rules argument |
|
| 887 | + */ |
|
| 888 | + protected function _validateInList($inputName, $ruleName, array $ruleArgs) { |
|
| 889 | + $inputVal = $this->post($inputName); |
|
| 890 | + $list = explode(',', $ruleArgs[1]); |
|
| 891 | + $list = array_map('trim', $list); |
|
| 892 | + if (! in_array($inputVal, $list)) { |
|
| 893 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 894 | + return; |
|
| 895 | + } |
|
| 896 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 897 | + } |
|
| 898 | + } |
|
| 899 | + |
|
| 900 | + /** |
|
| 901 | + * Peform validation for the rule "regex" |
|
| 902 | + * @param string $inputName the form field or data key name used |
|
| 903 | + * @param string $ruleName the rule name for this validation ("regex") |
|
| 904 | + * @param array $ruleArgs the rules argument |
|
| 905 | + */ |
|
| 906 | + protected function _validateRegex($inputName, $ruleName, array $ruleArgs) { |
|
| 907 | + $inputVal = $this->post($inputName); |
|
| 908 | + $regex = $ruleArgs[1]; |
|
| 909 | + if (! preg_match($regex, $inputVal)) { |
|
| 910 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 911 | + return; |
|
| 912 | + } |
|
| 913 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 914 | + } |
|
| 915 | + } |
|
| 916 | 916 | |
| 917 | - } |
|
| 917 | + } |
|