@@ -53,14 +53,14 @@ discard block |
||
53 | 53 | //put the first letter of class to upper case |
54 | 54 | $class = ucfirst($class); |
55 | 55 | static $classes = array(); |
56 | - if(isset($classes[$class]) /*hack for duplicate log Logger name*/ && $class != 'Log'){ |
|
56 | + if (isset($classes[$class]) /*hack for duplicate log Logger name*/ && $class != 'Log') { |
|
57 | 57 | return $classes[$class]; |
58 | 58 | } |
59 | 59 | $found = false; |
60 | 60 | foreach (array(ROOT_PATH, CORE_PATH) as $path) { |
61 | 61 | $file = $path . $dir . '/' . $class . '.php'; |
62 | - if(file_exists($file)){ |
|
63 | - if(class_exists($class, false) === false){ |
|
62 | + if (file_exists($file)) { |
|
63 | + if (class_exists($class, false) === false) { |
|
64 | 64 | require_once $file; |
65 | 65 | } |
66 | 66 | //already found |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | break; |
69 | 69 | } |
70 | 70 | } |
71 | - if(! $found){ |
|
71 | + if (!$found) { |
|
72 | 72 | //can't use show_error() at this time because some dependencies not yet loaded |
73 | 73 | set_http_status_header(503); |
74 | 74 | echo 'Cannot find the class [' . $class . ']'; |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | /* |
79 | 79 | TODO use the best method to get the Log instance |
80 | 80 | */ |
81 | - if($class == 'Log'){ |
|
81 | + if ($class == 'Log') { |
|
82 | 82 | //can't use the instruction like "return new Log()" |
83 | 83 | //because we need return the reference instance of the loaded class. |
84 | 84 | $log = new Log(); |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | */ |
103 | 103 | function & class_loaded($class = null){ |
104 | 104 | static $list = array(); |
105 | - if($class !== null){ |
|
105 | + if ($class !== null) { |
|
106 | 106 | $list[strtolower($class)] = $class; |
107 | 107 | } |
108 | 108 | return $list; |
@@ -117,14 +117,14 @@ discard block |
||
117 | 117 | */ |
118 | 118 | function & load_configurations(array $overwrite_values = array()){ |
119 | 119 | static $config; |
120 | - if(empty($config)){ |
|
120 | + if (empty($config)) { |
|
121 | 121 | $file = CONFIG_PATH . 'config.php'; |
122 | 122 | $found = false; |
123 | - if(file_exists($file)){ |
|
123 | + if (file_exists($file)) { |
|
124 | 124 | require_once $file; |
125 | 125 | $found = true; |
126 | 126 | } |
127 | - if(! $found){ |
|
127 | + if (!$found) { |
|
128 | 128 | set_http_status_header(503); |
129 | 129 | echo 'Unable to find the configuration file [' . $file . ']'; |
130 | 130 | die(); |
@@ -144,9 +144,9 @@ discard block |
||
144 | 144 | * |
145 | 145 | * @return mixed the config value |
146 | 146 | */ |
147 | - function get_config($key, $default = null){ |
|
147 | + function get_config($key, $default = null) { |
|
148 | 148 | static $cfg; |
149 | - if(empty($cfg)){ |
|
149 | + if (empty($cfg)) { |
|
150 | 150 | $cfg[0] = & load_configurations(); |
151 | 151 | } |
152 | 152 | return array_key_exists($key, $cfg[0]) ? $cfg[0][$key] : $default; |
@@ -160,9 +160,9 @@ discard block |
||
160 | 160 | * |
161 | 161 | * @codeCoverageIgnore |
162 | 162 | */ |
163 | - function save_to_log($level, $message, $logger = null){ |
|
164 | - $log =& class_loader('Log', 'classes'); |
|
165 | - if($logger){ |
|
163 | + function save_to_log($level, $message, $logger = null) { |
|
164 | + $log = & class_loader('Log', 'classes'); |
|
165 | + if ($logger) { |
|
166 | 166 | $log->setLogger($logger); |
167 | 167 | } |
168 | 168 | $log->writeLog($message, $level); |
@@ -175,11 +175,11 @@ discard block |
||
175 | 175 | * |
176 | 176 | * @codeCoverageIgnore |
177 | 177 | */ |
178 | - function set_http_status_header($code = 200, $text = null){ |
|
179 | - if(!is_numeric($code) || $code < 0 ){ |
|
178 | + function set_http_status_header($code = 200, $text = null) { |
|
179 | + if (!is_numeric($code) || $code < 0) { |
|
180 | 180 | show_error('HTTP status code must be an integer'); |
181 | 181 | } |
182 | - if(empty($text)){ |
|
182 | + if (empty($text)) { |
|
183 | 183 | $code = abs($code); |
184 | 184 | $http_status = array( |
185 | 185 | 100 => 'Continue', |
@@ -228,18 +228,18 @@ discard block |
||
228 | 228 | 504 => 'Gateway Timeout', |
229 | 229 | 505 => 'HTTP Version Not Supported', |
230 | 230 | ); |
231 | - if(isset($http_status[$code])){ |
|
231 | + if (isset($http_status[$code])) { |
|
232 | 232 | $text = $http_status[$code]; |
233 | 233 | } |
234 | - else{ |
|
234 | + else { |
|
235 | 235 | show_error('No HTTP status text found for your code please check it.'); |
236 | 236 | } |
237 | 237 | } |
238 | 238 | |
239 | - if(strpos(php_sapi_name(), 'cgi') === 0){ |
|
239 | + if (strpos(php_sapi_name(), 'cgi') === 0) { |
|
240 | 240 | header('Status: ' . $code . ' ' . $text, TRUE); |
241 | 241 | } |
242 | - else{ |
|
242 | + else { |
|
243 | 243 | $proto = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'; |
244 | 244 | header($proto . ' ' . $code . ' ' . $text, TRUE, $code); |
245 | 245 | } |
@@ -254,13 +254,13 @@ discard block |
||
254 | 254 | * |
255 | 255 | * @codeCoverageIgnore |
256 | 256 | */ |
257 | - function show_error($msg, $title = 'error', $logging = true){ |
|
257 | + function show_error($msg, $title = 'error', $logging = true) { |
|
258 | 258 | $title = strtoupper($title); |
259 | 259 | $data = array(); |
260 | 260 | $data['error'] = $msg; |
261 | 261 | $data['title'] = $title; |
262 | - if($logging){ |
|
263 | - save_to_log('error', '['.$title.'] '.strip_tags($msg), 'GLOBAL::ERROR'); |
|
262 | + if ($logging) { |
|
263 | + save_to_log('error', '[' . $title . '] ' . strip_tags($msg), 'GLOBAL::ERROR'); |
|
264 | 264 | } |
265 | 265 | $response = & class_loader('Response', 'classes'); |
266 | 266 | $response->sendError($data); |
@@ -274,18 +274,18 @@ discard block |
||
274 | 274 | * |
275 | 275 | * @return boolean true if the web server uses the https protocol, false if not. |
276 | 276 | */ |
277 | - function is_https(){ |
|
277 | + function is_https() { |
|
278 | 278 | /* |
279 | 279 | * some servers pass the "HTTPS" parameter in the server variable, |
280 | 280 | * if is the case, check if the value is "on", "true", "1". |
281 | 281 | */ |
282 | - if(isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off'){ |
|
282 | + if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') { |
|
283 | 283 | return true; |
284 | 284 | } |
285 | - else if(isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https'){ |
|
285 | + else if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') { |
|
286 | 286 | return true; |
287 | 287 | } |
288 | - else if(isset($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off'){ |
|
288 | + else if (isset($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') { |
|
289 | 289 | return true; |
290 | 290 | } |
291 | 291 | return false; |
@@ -300,7 +300,7 @@ discard block |
||
300 | 300 | * |
301 | 301 | * @return boolean true if is a valid URL address or false. |
302 | 302 | */ |
303 | - function is_url($url){ |
|
303 | + function is_url($url) { |
|
304 | 304 | return preg_match('/^(http|https|ftp):\/\/(.*)/', $url) == 1; |
305 | 305 | } |
306 | 306 | |
@@ -310,8 +310,8 @@ discard block |
||
310 | 310 | * @param string $controllerClass the controller class name to be loaded |
311 | 311 | * @codeCoverageIgnore |
312 | 312 | */ |
313 | - function autoload_controller($controllerClass){ |
|
314 | - if(file_exists($path = APPS_CONTROLLER_PATH . $controllerClass . '.php')){ |
|
313 | + function autoload_controller($controllerClass) { |
|
314 | + if (file_exists($path = APPS_CONTROLLER_PATH . $controllerClass . '.php')) { |
|
315 | 315 | require_once $path; |
316 | 316 | } |
317 | 317 | } |
@@ -325,11 +325,11 @@ discard block |
||
325 | 325 | * |
326 | 326 | * @return boolean |
327 | 327 | */ |
328 | - function php_exception_handler($ex){ |
|
329 | - if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors'))){ |
|
330 | - show_error('An exception is occured in file '. $ex->getFile() .' at line ' . $ex->getLine() . ' raison : ' . $ex->getMessage(), 'PHP Exception #' . $ex->getCode()); |
|
328 | + function php_exception_handler($ex) { |
|
329 | + if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors'))) { |
|
330 | + show_error('An exception is occured in file ' . $ex->getFile() . ' at line ' . $ex->getLine() . ' raison : ' . $ex->getMessage(), 'PHP Exception #' . $ex->getCode()); |
|
331 | 331 | } |
332 | - else{ |
|
332 | + else { |
|
333 | 333 | save_to_log('error', 'An exception is occured in file ' . $ex->getFile() . ' at line ' . $ex->getLine() . ' raison : ' . $ex->getMessage(), 'PHP Exception'); |
334 | 334 | } |
335 | 335 | return true; |
@@ -347,16 +347,16 @@ discard block |
||
347 | 347 | * |
348 | 348 | * @return boolean |
349 | 349 | */ |
350 | - function php_error_handler($errno , $errstr, $errfile , $errline, array $errcontext = array()){ |
|
350 | + function php_error_handler($errno, $errstr, $errfile, $errline, array $errcontext = array()) { |
|
351 | 351 | $isError = (((E_ERROR | E_COMPILE_ERROR | E_CORE_ERROR | E_USER_ERROR) & $errno) === $errno); |
352 | - if($isError){ |
|
352 | + if ($isError) { |
|
353 | 353 | set_http_status_header(500); |
354 | 354 | } |
355 | - if (! (error_reporting() & $errno)) { |
|
355 | + if (!(error_reporting() & $errno)) { |
|
356 | 356 | save_to_log('error', 'An error is occurred in the file ' . $errfile . ' at line ' . $errline . ' raison : ' . $errstr, 'PHP ERROR'); |
357 | 357 | return; |
358 | 358 | } |
359 | - if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors'))){ |
|
359 | + if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors'))) { |
|
360 | 360 | $errorType = 'error'; |
361 | 361 | switch ($errno) { |
362 | 362 | case E_USER_ERROR: |
@@ -372,9 +372,9 @@ discard block |
||
372 | 372 | $errorType = 'error'; |
373 | 373 | break; |
374 | 374 | } |
375 | - show_error('An error is occurred in the file <b>' . $errfile . '</b> at line <b>' . $errline .'</b> raison : ' . $errstr, 'PHP ' . $errorType); |
|
375 | + show_error('An error is occurred in the file <b>' . $errfile . '</b> at line <b>' . $errline . '</b> raison : ' . $errstr, 'PHP ' . $errorType); |
|
376 | 376 | } |
377 | - if ($isError){ |
|
377 | + if ($isError) { |
|
378 | 378 | die(); |
379 | 379 | } |
380 | 380 | return true; |
@@ -384,10 +384,10 @@ discard block |
||
384 | 384 | * This function is used to run in shutdown situation of the script |
385 | 385 | * @codeCoverageIgnore |
386 | 386 | */ |
387 | - function php_shudown_handler(){ |
|
387 | + function php_shudown_handler() { |
|
388 | 388 | $lastError = error_get_last(); |
389 | 389 | if (isset($lastError) && |
390 | - ($lastError['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING))){ |
|
390 | + ($lastError['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING))) { |
|
391 | 391 | php_error_handler($lastError['type'], $lastError['message'], $lastError['file'], $lastError['line']); |
392 | 392 | } |
393 | 393 | } |
@@ -405,11 +405,11 @@ discard block |
||
405 | 405 | * |
406 | 406 | * @return string string of the HTML attribute. |
407 | 407 | */ |
408 | - function attributes_to_string(array $attributes){ |
|
408 | + function attributes_to_string(array $attributes) { |
|
409 | 409 | $str = ' '; |
410 | 410 | //we check that the array passed as an argument is not empty. |
411 | - if(! empty($attributes)){ |
|
412 | - foreach($attributes as $key => $value){ |
|
411 | + if (!empty($attributes)) { |
|
412 | + foreach ($attributes as $key => $value) { |
|
413 | 413 | $key = trim(htmlspecialchars($key)); |
414 | 414 | $value = trim(htmlspecialchars($value)); |
415 | 415 | /* |
@@ -419,10 +419,10 @@ discard block |
||
419 | 419 | * $attr = array('placeholder' => 'I am a "puple"') |
420 | 420 | * $str = attributes_to_string($attr); => placeholder = "I am a \"puple\"" |
421 | 421 | */ |
422 | - if($value && strpos('"', $value) !== false){ |
|
422 | + if ($value && strpos('"', $value) !== false) { |
|
423 | 423 | $value = addslashes($value); |
424 | 424 | } |
425 | - $str .= $key.' = "'.$value.'" '; |
|
425 | + $str .= $key . ' = "' . $value . '" '; |
|
426 | 426 | } |
427 | 427 | } |
428 | 428 | //remove the space after using rtrim() |
@@ -438,7 +438,7 @@ discard block |
||
438 | 438 | * |
439 | 439 | * @return string the stringfy value |
440 | 440 | */ |
441 | - function stringfy_vars($var){ |
|
441 | + function stringfy_vars($var) { |
|
442 | 442 | return print_r($var, true); |
443 | 443 | } |
444 | 444 | |
@@ -449,18 +449,18 @@ discard block |
||
449 | 449 | * |
450 | 450 | * @return mixed the sanitize value |
451 | 451 | */ |
452 | - function clean_input($str){ |
|
453 | - if(is_array($str)){ |
|
452 | + function clean_input($str) { |
|
453 | + if (is_array($str)) { |
|
454 | 454 | $str = array_map('clean_input', $str); |
455 | 455 | } |
456 | - else if(is_object($str)){ |
|
456 | + else if (is_object($str)) { |
|
457 | 457 | $obj = $str; |
458 | 458 | foreach ($str as $var => $value) { |
459 | 459 | $obj->$var = clean_input($value); |
460 | 460 | } |
461 | 461 | $str = $obj; |
462 | 462 | } |
463 | - else{ |
|
463 | + else { |
|
464 | 464 | $str = htmlspecialchars(strip_tags($str), ENT_QUOTES, 'UTF-8'); |
465 | 465 | } |
466 | 466 | return $str; |
@@ -478,11 +478,11 @@ discard block |
||
478 | 478 | * |
479 | 479 | * @return string the string with the hidden part. |
480 | 480 | */ |
481 | - function string_hidden($str, $startCount = 0, $endCount = 0, $hiddenChar = '*'){ |
|
481 | + function string_hidden($str, $startCount = 0, $endCount = 0, $hiddenChar = '*') { |
|
482 | 482 | //get the string length |
483 | 483 | $len = strlen($str); |
484 | 484 | //if str is empty |
485 | - if($len <= 0){ |
|
485 | + if ($len <= 0) { |
|
486 | 486 | return str_repeat($hiddenChar, 6); |
487 | 487 | } |
488 | 488 | //if the length is less than startCount and endCount |
@@ -490,14 +490,14 @@ discard block |
||
490 | 490 | //or startCount is negative or endCount is negative |
491 | 491 | //return the full string hidden |
492 | 492 | |
493 | - if((($startCount + $endCount) > $len) || ($startCount == 0 && $endCount == 0) || ($startCount < 0 || $endCount < 0)){ |
|
493 | + if ((($startCount + $endCount) > $len) || ($startCount == 0 && $endCount == 0) || ($startCount < 0 || $endCount < 0)) { |
|
494 | 494 | return str_repeat($hiddenChar, $len); |
495 | 495 | } |
496 | 496 | //the start non hidden string |
497 | 497 | $startNonHiddenStr = substr($str, 0, $startCount); |
498 | 498 | //the end non hidden string |
499 | 499 | $endNonHiddenStr = null; |
500 | - if($endCount > 0){ |
|
500 | + if ($endCount > 0) { |
|
501 | 501 | $endNonHiddenStr = substr($str, - $endCount); |
502 | 502 | } |
503 | 503 | //the hidden string |
@@ -510,31 +510,31 @@ discard block |
||
510 | 510 | * This function is used to set the initial session config regarding the configuration |
511 | 511 | * @codeCoverageIgnore |
512 | 512 | */ |
513 | - function set_session_config(){ |
|
513 | + function set_session_config() { |
|
514 | 514 | //$_SESSION is not available on cli mode |
515 | - if(! IS_CLI){ |
|
516 | - $logger =& class_loader('Log', 'classes'); |
|
515 | + if (!IS_CLI) { |
|
516 | + $logger = & class_loader('Log', 'classes'); |
|
517 | 517 | $logger->setLogger('PHPSession'); |
518 | 518 | //set session params |
519 | 519 | $sessionHandler = get_config('session_handler', 'files'); //the default is to store in the files |
520 | 520 | $sessionName = get_config('session_name'); |
521 | - if($sessionName){ |
|
521 | + if ($sessionName) { |
|
522 | 522 | session_name($sessionName); |
523 | 523 | } |
524 | 524 | $logger->info('Session handler: ' . $sessionHandler); |
525 | 525 | $logger->info('Session name: ' . $sessionName); |
526 | 526 | |
527 | - if($sessionHandler == 'files'){ |
|
527 | + if ($sessionHandler == 'files') { |
|
528 | 528 | $sessionSavePath = get_config('session_save_path'); |
529 | - if($sessionSavePath){ |
|
530 | - if(! is_dir($sessionSavePath)){ |
|
529 | + if ($sessionSavePath) { |
|
530 | + if (!is_dir($sessionSavePath)) { |
|
531 | 531 | mkdir($sessionSavePath, 1773); |
532 | 532 | } |
533 | 533 | session_save_path($sessionSavePath); |
534 | 534 | $logger->info('Session save path: ' . $sessionSavePath); |
535 | 535 | } |
536 | 536 | } |
537 | - else if($sessionHandler == 'database'){ |
|
537 | + else if ($sessionHandler == 'database') { |
|
538 | 538 | //load database session handle library |
539 | 539 | //Model |
540 | 540 | require_once CORE_CLASSES_MODEL_PATH . 'Model.php'; |
@@ -542,11 +542,11 @@ discard block |
||
542 | 542 | //Database Session handler Model |
543 | 543 | require_once CORE_CLASSES_MODEL_PATH . 'DBSessionHandlerModel.php'; |
544 | 544 | |
545 | - $DBS =& class_loader('DBSessionHandler', 'classes'); |
|
545 | + $DBS = & class_loader('DBSessionHandler', 'classes'); |
|
546 | 546 | session_set_save_handler($DBS, true); |
547 | 547 | $logger->info('session save path: ' . get_config('session_save_path')); |
548 | 548 | } |
549 | - else{ |
|
549 | + else { |
|
550 | 550 | show_error('Invalid session handler configuration'); |
551 | 551 | } |
552 | 552 | $lifetime = get_config('session_cookie_lifetime', 0); |
@@ -569,9 +569,9 @@ discard block |
||
569 | 569 | $logger->info('Session lifetime: ' . $lifetime); |
570 | 570 | $logger->info('Session cookie path: ' . $path); |
571 | 571 | $logger->info('Session domain: ' . $domain); |
572 | - $logger->info('Session is secure: ' . ($secure ? 'TRUE':'FALSE')); |
|
572 | + $logger->info('Session is secure: ' . ($secure ? 'TRUE' : 'FALSE')); |
|
573 | 573 | |
574 | - if((function_exists('session_status') && session_status() !== PHP_SESSION_ACTIVE) || !session_id()){ |
|
574 | + if ((function_exists('session_status') && session_status() !== PHP_SESSION_ACTIVE) || !session_id()) { |
|
575 | 575 | $logger->info('Session not yet start, start it now'); |
576 | 576 | session_start(); |
577 | 577 | } |
@@ -230,16 +230,14 @@ discard block |
||
230 | 230 | ); |
231 | 231 | if(isset($http_status[$code])){ |
232 | 232 | $text = $http_status[$code]; |
233 | - } |
|
234 | - else{ |
|
233 | + } else{ |
|
235 | 234 | show_error('No HTTP status text found for your code please check it.'); |
236 | 235 | } |
237 | 236 | } |
238 | 237 | |
239 | 238 | if(strpos(php_sapi_name(), 'cgi') === 0){ |
240 | 239 | header('Status: ' . $code . ' ' . $text, TRUE); |
241 | - } |
|
242 | - else{ |
|
240 | + } else{ |
|
243 | 241 | $proto = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'; |
244 | 242 | header($proto . ' ' . $code . ' ' . $text, TRUE, $code); |
245 | 243 | } |
@@ -281,11 +279,9 @@ discard block |
||
281 | 279 | */ |
282 | 280 | if(isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off'){ |
283 | 281 | return true; |
284 | - } |
|
285 | - else if(isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https'){ |
|
282 | + } else if(isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https'){ |
|
286 | 283 | return true; |
287 | - } |
|
288 | - else if(isset($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off'){ |
|
284 | + } else if(isset($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off'){ |
|
289 | 285 | return true; |
290 | 286 | } |
291 | 287 | return false; |
@@ -328,8 +324,7 @@ discard block |
||
328 | 324 | function php_exception_handler($ex){ |
329 | 325 | if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors'))){ |
330 | 326 | show_error('An exception is occured in file '. $ex->getFile() .' at line ' . $ex->getLine() . ' raison : ' . $ex->getMessage(), 'PHP Exception #' . $ex->getCode()); |
331 | - } |
|
332 | - else{ |
|
327 | + } else{ |
|
333 | 328 | save_to_log('error', 'An exception is occured in file ' . $ex->getFile() . ' at line ' . $ex->getLine() . ' raison : ' . $ex->getMessage(), 'PHP Exception'); |
334 | 329 | } |
335 | 330 | return true; |
@@ -452,15 +447,13 @@ discard block |
||
452 | 447 | function clean_input($str){ |
453 | 448 | if(is_array($str)){ |
454 | 449 | $str = array_map('clean_input', $str); |
455 | - } |
|
456 | - else if(is_object($str)){ |
|
450 | + } else if(is_object($str)){ |
|
457 | 451 | $obj = $str; |
458 | 452 | foreach ($str as $var => $value) { |
459 | 453 | $obj->$var = clean_input($value); |
460 | 454 | } |
461 | 455 | $str = $obj; |
462 | - } |
|
463 | - else{ |
|
456 | + } else{ |
|
464 | 457 | $str = htmlspecialchars(strip_tags($str), ENT_QUOTES, 'UTF-8'); |
465 | 458 | } |
466 | 459 | return $str; |
@@ -533,8 +526,7 @@ discard block |
||
533 | 526 | session_save_path($sessionSavePath); |
534 | 527 | $logger->info('Session save path: ' . $sessionSavePath); |
535 | 528 | } |
536 | - } |
|
537 | - else if($sessionHandler == 'database'){ |
|
529 | + } else if($sessionHandler == 'database'){ |
|
538 | 530 | //load database session handle library |
539 | 531 | //Model |
540 | 532 | require_once CORE_CLASSES_MODEL_PATH . 'Model.php'; |
@@ -545,8 +537,7 @@ discard block |
||
545 | 537 | $DBS =& class_loader('DBSessionHandler', 'classes'); |
546 | 538 | session_set_save_handler($DBS, true); |
547 | 539 | $logger->info('session save path: ' . get_config('session_save_path')); |
548 | - } |
|
549 | - else{ |
|
540 | + } else{ |
|
550 | 541 | show_error('Invalid session handler configuration'); |
551 | 542 | } |
552 | 543 | $lifetime = get_config('session_cookie_lifetime', 0); |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | * also to dispatch the event |
30 | 30 | */ |
31 | 31 | |
32 | - class EventDispatcher{ |
|
32 | + class EventDispatcher { |
|
33 | 33 | |
34 | 34 | /** |
35 | 35 | * The list of the registered listeners |
@@ -44,8 +44,8 @@ discard block |
||
44 | 44 | */ |
45 | 45 | private $logger; |
46 | 46 | |
47 | - public function __construct(){ |
|
48 | - $this->logger =& class_loader('Log', 'classes'); |
|
47 | + public function __construct() { |
|
48 | + $this->logger = & class_loader('Log', 'classes'); |
|
49 | 49 | $this->logger->setLogger('Library::EventDispatcher'); |
50 | 50 | } |
51 | 51 | |
@@ -54,13 +54,13 @@ discard block |
||
54 | 54 | * @param string $eventName the name of the event to register for |
55 | 55 | * @param callable $listener the function or class method to receive the event information after dispatch |
56 | 56 | */ |
57 | - public function addListener($eventName, callable $listener){ |
|
58 | - $this->logger->debug('Adding new Event Listener for the event name [' .$eventName. '], listener [' .stringfy_vars($listener). ']'); |
|
59 | - if(! isset($this->listeners[$eventName])){ |
|
57 | + public function addListener($eventName, callable $listener) { |
|
58 | + $this->logger->debug('Adding new Event Listener for the event name [' . $eventName . '], listener [' . stringfy_vars($listener) . ']'); |
|
59 | + if (!isset($this->listeners[$eventName])) { |
|
60 | 60 | $this->logger->info('This event does not have the registered event listener before, adding new one'); |
61 | 61 | $this->listeners[$eventName] = array(); |
62 | 62 | } |
63 | - else{ |
|
63 | + else { |
|
64 | 64 | $this->logger->info('This event already have the registered listener, add this listener to the list'); |
65 | 65 | } |
66 | 66 | $this->listeners[$eventName][] = $listener; |
@@ -71,19 +71,19 @@ discard block |
||
71 | 71 | * @param string $eventName the event name |
72 | 72 | * @param callable $listener the listener callback |
73 | 73 | */ |
74 | - public function removeListener($eventName, callable $listener){ |
|
75 | - $this->logger->debug('Removing of the Event Listener, the event name [' .$eventName. '], listener [' .stringfy_vars($listener). ']'); |
|
76 | - if(isset($this->listeners[$eventName])){ |
|
74 | + public function removeListener($eventName, callable $listener) { |
|
75 | + $this->logger->debug('Removing of the Event Listener, the event name [' . $eventName . '], listener [' . stringfy_vars($listener) . ']'); |
|
76 | + if (isset($this->listeners[$eventName])) { |
|
77 | 77 | $this->logger->info('This event have the listeners, check if this listener exists'); |
78 | - if(false !== $index = array_search($listener, $this->listeners[$eventName], true)){ |
|
79 | - $this->logger->info('Found the listener at index [' .$index. '] remove it'); |
|
78 | + if (false !== $index = array_search($listener, $this->listeners[$eventName], true)) { |
|
79 | + $this->logger->info('Found the listener at index [' . $index . '] remove it'); |
|
80 | 80 | unset($this->listeners[$eventName][$index]); |
81 | 81 | } |
82 | - else{ |
|
82 | + else { |
|
83 | 83 | $this->logger->info('Cannot found this listener in the event listener list'); |
84 | 84 | } |
85 | 85 | } |
86 | - else{ |
|
86 | + else { |
|
87 | 87 | $this->logger->info('This event does not have this listener ignore remove'); |
88 | 88 | } |
89 | 89 | } |
@@ -93,13 +93,13 @@ discard block |
||
93 | 93 | * remove all listeners for this event |
94 | 94 | * @param string $eventName the event name |
95 | 95 | */ |
96 | - public function removeAllListener($eventName = null){ |
|
97 | - $this->logger->debug('Removing of all Event Listener, the event name [' .$eventName. ']'); |
|
98 | - if($eventName !== null && isset($this->listeners[$eventName])){ |
|
96 | + public function removeAllListener($eventName = null) { |
|
97 | + $this->logger->debug('Removing of all Event Listener, the event name [' . $eventName . ']'); |
|
98 | + if ($eventName !== null && isset($this->listeners[$eventName])) { |
|
99 | 99 | $this->logger->info('The Event name is set of exist in the listener just remove all Event Listener for this event'); |
100 | 100 | unset($this->listeners[$eventName]); |
101 | 101 | } |
102 | - else{ |
|
102 | + else { |
|
103 | 103 | $this->logger->info('The Event name is not set or does not exist in the listener, so remove all Event Listener'); |
104 | 104 | $this->listeners = array(); |
105 | 105 | } |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | * @param string $eventName the event name |
111 | 111 | * @return array the listeners for this event or empty array if this event does not contain any listener |
112 | 112 | */ |
113 | - public function getListeners($eventName){ |
|
113 | + public function getListeners($eventName) { |
|
114 | 114 | return isset($this->listeners[$eventName]) ? $this->listeners[$eventName] : array(); |
115 | 115 | } |
116 | 116 | |
@@ -119,21 +119,21 @@ discard block |
||
119 | 119 | * @param mixed|Event $event the event information |
120 | 120 | * @return void|Event if event need return, will return the final Event object. |
121 | 121 | */ |
122 | - public function dispatch($event){ |
|
123 | - if(! $event || !$event instanceof Event){ |
|
122 | + public function dispatch($event) { |
|
123 | + if (!$event || !$event instanceof Event) { |
|
124 | 124 | $this->logger->info('The event is not set or is not an instance of "Event" create the default "Event" object to use instead of.'); |
125 | 125 | $event = new Event((string) $event); |
126 | 126 | } |
127 | - $this->logger->debug('Dispatch to the Event Listener, the event [' .stringfy_vars($event). ']'); |
|
128 | - if(isset($event->stop) && $event->stop){ |
|
127 | + $this->logger->debug('Dispatch to the Event Listener, the event [' . stringfy_vars($event) . ']'); |
|
128 | + if (isset($event->stop) && $event->stop) { |
|
129 | 129 | $this->logger->info('This event need stopped, no need call any listener'); |
130 | 130 | return; |
131 | 131 | } |
132 | - if($event->returnBack){ |
|
132 | + if ($event->returnBack) { |
|
133 | 133 | $this->logger->info('This event need return back, return the result for future use'); |
134 | 134 | return $this->dispatchToListerners($event); |
135 | 135 | } |
136 | - else{ |
|
136 | + else { |
|
137 | 137 | $this->logger->info('This event no need return back the result, just dispatch it'); |
138 | 138 | $this->dispatchToListerners($event); |
139 | 139 | } |
@@ -144,38 +144,38 @@ discard block |
||
144 | 144 | * @param Event $event the event information |
145 | 145 | * @return void|Event if event need return, will return the final Event instance. |
146 | 146 | */ |
147 | - private function dispatchToListerners(Event $event){ |
|
147 | + private function dispatchToListerners(Event $event) { |
|
148 | 148 | $eBackup = $event; |
149 | 149 | $list = $this->getListeners($event->name); |
150 | - if(empty($list)){ |
|
151 | - $this->logger->info('No event listener is registered for the event [' .$event->name. '] skipping.'); |
|
152 | - if($event->returnBack){ |
|
150 | + if (empty($list)) { |
|
151 | + $this->logger->info('No event listener is registered for the event [' . $event->name . '] skipping.'); |
|
152 | + if ($event->returnBack) { |
|
153 | 153 | return $event; |
154 | 154 | } |
155 | 155 | return; |
156 | 156 | } |
157 | - else{ |
|
158 | - $this->logger->info('Found the registered Event listener for the event [' .$event->name. '] the list are: ' . stringfy_vars($list)); |
|
157 | + else { |
|
158 | + $this->logger->info('Found the registered Event listener for the event [' . $event->name . '] the list are: ' . stringfy_vars($list)); |
|
159 | 159 | } |
160 | - foreach($list as $listener){ |
|
161 | - if($eBackup->returnBack){ |
|
160 | + foreach ($list as $listener) { |
|
161 | + if ($eBackup->returnBack) { |
|
162 | 162 | $returnedEvent = call_user_func_array($listener, array($event)); |
163 | - if($returnedEvent instanceof Event){ |
|
163 | + if ($returnedEvent instanceof Event) { |
|
164 | 164 | $event = $returnedEvent; |
165 | 165 | } |
166 | - else{ |
|
167 | - show_error('This event [' .$event->name. '] need you return the event object after processing'); |
|
166 | + else { |
|
167 | + show_error('This event [' . $event->name . '] need you return the event object after processing'); |
|
168 | 168 | } |
169 | 169 | } |
170 | - else{ |
|
170 | + else { |
|
171 | 171 | call_user_func_array($listener, array($event)); |
172 | 172 | } |
173 | - if($event->stop){ |
|
173 | + if ($event->stop) { |
|
174 | 174 | break; |
175 | 175 | } |
176 | 176 | } |
177 | 177 | //only test for original event may be during the flow some listeners change this parameter |
178 | - if($eBackup->returnBack){ |
|
178 | + if ($eBackup->returnBack) { |
|
179 | 179 | return $event; |
180 | 180 | } |
181 | 181 | } |
@@ -59,8 +59,7 @@ discard block |
||
59 | 59 | if(! isset($this->listeners[$eventName])){ |
60 | 60 | $this->logger->info('This event does not have the registered event listener before, adding new one'); |
61 | 61 | $this->listeners[$eventName] = array(); |
62 | - } |
|
63 | - else{ |
|
62 | + } else{ |
|
64 | 63 | $this->logger->info('This event already have the registered listener, add this listener to the list'); |
65 | 64 | } |
66 | 65 | $this->listeners[$eventName][] = $listener; |
@@ -78,12 +77,10 @@ discard block |
||
78 | 77 | if(false !== $index = array_search($listener, $this->listeners[$eventName], true)){ |
79 | 78 | $this->logger->info('Found the listener at index [' .$index. '] remove it'); |
80 | 79 | unset($this->listeners[$eventName][$index]); |
81 | - } |
|
82 | - else{ |
|
80 | + } else{ |
|
83 | 81 | $this->logger->info('Cannot found this listener in the event listener list'); |
84 | 82 | } |
85 | - } |
|
86 | - else{ |
|
83 | + } else{ |
|
87 | 84 | $this->logger->info('This event does not have this listener ignore remove'); |
88 | 85 | } |
89 | 86 | } |
@@ -98,8 +95,7 @@ discard block |
||
98 | 95 | if($eventName !== null && isset($this->listeners[$eventName])){ |
99 | 96 | $this->logger->info('The Event name is set of exist in the listener just remove all Event Listener for this event'); |
100 | 97 | unset($this->listeners[$eventName]); |
101 | - } |
|
102 | - else{ |
|
98 | + } else{ |
|
103 | 99 | $this->logger->info('The Event name is not set or does not exist in the listener, so remove all Event Listener'); |
104 | 100 | $this->listeners = array(); |
105 | 101 | } |
@@ -132,8 +128,7 @@ discard block |
||
132 | 128 | if($event->returnBack){ |
133 | 129 | $this->logger->info('This event need return back, return the result for future use'); |
134 | 130 | return $this->dispatchToListerners($event); |
135 | - } |
|
136 | - else{ |
|
131 | + } else{ |
|
137 | 132 | $this->logger->info('This event no need return back the result, just dispatch it'); |
138 | 133 | $this->dispatchToListerners($event); |
139 | 134 | } |
@@ -153,8 +148,7 @@ discard block |
||
153 | 148 | return $event; |
154 | 149 | } |
155 | 150 | return; |
156 | - } |
|
157 | - else{ |
|
151 | + } else{ |
|
158 | 152 | $this->logger->info('Found the registered Event listener for the event [' .$event->name. '] the list are: ' . stringfy_vars($list)); |
159 | 153 | } |
160 | 154 | foreach($list as $listener){ |
@@ -162,12 +156,10 @@ discard block |
||
162 | 156 | $returnedEvent = call_user_func_array($listener, array($event)); |
163 | 157 | if($returnedEvent instanceof Event){ |
164 | 158 | $event = $returnedEvent; |
165 | - } |
|
166 | - else{ |
|
159 | + } else{ |
|
167 | 160 | show_error('This event [' .$event->name. '] need you return the event object after processing'); |
168 | 161 | } |
169 | - } |
|
170 | - else{ |
|
162 | + } else{ |
|
171 | 163 | call_user_func_array($listener, array($event)); |
172 | 164 | } |
173 | 165 | if($event->stop){ |
@@ -27,11 +27,11 @@ discard block |
||
27 | 27 | /** |
28 | 28 | * check if the interface "SessionHandlerInterface" exists (normally in PHP 5.4 this already exists) |
29 | 29 | */ |
30 | - if( !interface_exists('SessionHandlerInterface')){ |
|
30 | + if (!interface_exists('SessionHandlerInterface')) { |
|
31 | 31 | show_error('"SessionHandlerInterface" interface does not exists or is disabled can not use it to handler database session.'); |
32 | 32 | } |
33 | 33 | |
34 | - class DBSessionHandler implements SessionHandlerInterface{ |
|
34 | + class DBSessionHandler implements SessionHandlerInterface { |
|
35 | 35 | |
36 | 36 | /** |
37 | 37 | * The encryption method to use to encrypt session data in database |
@@ -81,25 +81,25 @@ discard block |
||
81 | 81 | */ |
82 | 82 | protected $loader = null; |
83 | 83 | |
84 | - public function __construct(DBSessionHandlerModel $modelInstance = null, Log $logger = null, Loader $loader = null){ |
|
84 | + public function __construct(DBSessionHandlerModel $modelInstance = null, Log $logger = null, Loader $loader = null) { |
|
85 | 85 | /** |
86 | 86 | * instance of the Log class |
87 | 87 | */ |
88 | - if(is_object($logger)){ |
|
88 | + if (is_object($logger)) { |
|
89 | 89 | $this->setLogger($logger); |
90 | 90 | } |
91 | - else{ |
|
92 | - $this->logger =& class_loader('Log', 'classes'); |
|
91 | + else { |
|
92 | + $this->logger = & class_loader('Log', 'classes'); |
|
93 | 93 | $this->logger->setLogger('Library::DBSessionHandler'); |
94 | 94 | } |
95 | 95 | |
96 | - if(is_object($loader)){ |
|
96 | + if (is_object($loader)) { |
|
97 | 97 | $this->setLoader($loader); |
98 | 98 | } |
99 | 99 | $this->OBJ = & get_instance(); |
100 | 100 | |
101 | 101 | |
102 | - if(is_object($modelInstance)){ |
|
102 | + if (is_object($modelInstance)) { |
|
103 | 103 | $this->setModelInstance($modelInstance); |
104 | 104 | } |
105 | 105 | } |
@@ -108,7 +108,7 @@ discard block |
||
108 | 108 | * Set the session secret used to encrypt the data in database |
109 | 109 | * @param string $secret the base64 string secret |
110 | 110 | */ |
111 | - public function setSessionSecret($secret){ |
|
111 | + public function setSessionSecret($secret) { |
|
112 | 112 | $this->sessionSecret = $secret; |
113 | 113 | return $this; |
114 | 114 | } |
@@ -117,7 +117,7 @@ discard block |
||
117 | 117 | * Return the session secret |
118 | 118 | * @return string |
119 | 119 | */ |
120 | - public function getSessionSecret(){ |
|
120 | + public function getSessionSecret() { |
|
121 | 121 | return $this->sessionSecret; |
122 | 122 | } |
123 | 123 | |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | * Set the initializer vector for openssl |
127 | 127 | * @param string $key the session secret used |
128 | 128 | */ |
129 | - public function setInitializerVector($key){ |
|
129 | + public function setInitializerVector($key) { |
|
130 | 130 | $iv_length = openssl_cipher_iv_length(self::DB_SESSION_HASH_METHOD); |
131 | 131 | $key = base64_decode($key); |
132 | 132 | $this->iv = substr(hash('sha256', $key), 0, $iv_length); |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | * Return the initializer vector |
138 | 138 | * @return string |
139 | 139 | */ |
140 | - public function getInitializerVector(){ |
|
140 | + public function getInitializerVector() { |
|
141 | 141 | return $this->iv; |
142 | 142 | } |
143 | 143 | |
@@ -147,17 +147,17 @@ discard block |
||
147 | 147 | * @param string $sessionName the session name |
148 | 148 | * @return boolean |
149 | 149 | */ |
150 | - public function open($savePath, $sessionName){ |
|
150 | + public function open($savePath, $sessionName) { |
|
151 | 151 | $this->logger->debug('Opening database session handler for [' . $sessionName . ']'); |
152 | 152 | //try to check if session secret is set before |
153 | 153 | $secret = $this->getSessionSecret(); |
154 | - if(empty($secret)){ |
|
154 | + if (empty($secret)) { |
|
155 | 155 | $secret = get_config('session_secret', false); |
156 | 156 | $this->setSessionSecret($secret); |
157 | 157 | } |
158 | 158 | $this->logger->info('Session secret: ' . $secret); |
159 | 159 | |
160 | - if(! $this->getModelInstance()){ |
|
160 | + if (!$this->getModelInstance()) { |
|
161 | 161 | $this->setModelInstanceFromConfig(); |
162 | 162 | } |
163 | 163 | $this->setInitializerVector($secret); |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | //set session tables columns |
166 | 166 | $this->sessionTableColumns = $this->getModelInstance()->getSessionTableColumns(); |
167 | 167 | |
168 | - if(empty($this->sessionTableColumns)){ |
|
168 | + if (empty($this->sessionTableColumns)) { |
|
169 | 169 | show_error('The session handler is "database" but the table columns not set'); |
170 | 170 | } |
171 | 171 | $this->logger->info('Database session, the model columns are listed below: ' . stringfy_vars($this->sessionTableColumns)); |
@@ -181,7 +181,7 @@ discard block |
||
181 | 181 | * Close the session |
182 | 182 | * @return boolean |
183 | 183 | */ |
184 | - public function close(){ |
|
184 | + public function close() { |
|
185 | 185 | $this->logger->debug('Closing database session handler'); |
186 | 186 | return true; |
187 | 187 | } |
@@ -191,28 +191,28 @@ discard block |
||
191 | 191 | * @param string $sid the session id to use |
192 | 192 | * @return string the session data in serialiaze format |
193 | 193 | */ |
194 | - public function read($sid){ |
|
194 | + public function read($sid) { |
|
195 | 195 | $this->logger->debug('Reading database session data for SID: ' . $sid); |
196 | 196 | $instance = $this->getModelInstance(); |
197 | 197 | $columns = $this->sessionTableColumns; |
198 | - if($this->getLoader()){ |
|
198 | + if ($this->getLoader()) { |
|
199 | 199 | $this->getLoader()->functions('user_agent'); |
200 | 200 | $this->getLoader()->library('Browser'); |
201 | 201 | } |
202 | - else{ |
|
202 | + else { |
|
203 | 203 | Loader::functions('user_agent'); |
204 | 204 | Loader::library('Browser'); |
205 | 205 | } |
206 | 206 | |
207 | 207 | $ip = get_ip(); |
208 | 208 | $host = @gethostbyaddr($ip) or null; |
209 | - $browser = $this->OBJ->browser->getPlatform().', '.$this->OBJ->browser->getBrowser().' '.$this->OBJ->browser->getVersion(); |
|
209 | + $browser = $this->OBJ->browser->getPlatform() . ', ' . $this->OBJ->browser->getBrowser() . ' ' . $this->OBJ->browser->getVersion(); |
|
210 | 210 | |
211 | 211 | $data = $instance->get_by(array($columns['sid'] => $sid, $columns['shost'] => $host, $columns['sbrowser'] => $browser)); |
212 | - if($data && isset($data->{$columns['sdata']})){ |
|
212 | + if ($data && isset($data->{$columns['sdata']})) { |
|
213 | 213 | //checking inactivity |
214 | 214 | $timeInactivity = time() - get_config('session_inactivity_time', 100); |
215 | - if($data->{$columns['stime']} < $timeInactivity){ |
|
215 | + if ($data->{$columns['stime']} < $timeInactivity) { |
|
216 | 216 | $this->logger->info('Database session data for SID: ' . $sid . ' already expired, destroy it'); |
217 | 217 | $this->destroy($sid); |
218 | 218 | return null; |
@@ -229,16 +229,16 @@ discard block |
||
229 | 229 | * @param mixed $data the session data to save in serialize format |
230 | 230 | * @return boolean |
231 | 231 | */ |
232 | - public function write($sid, $data){ |
|
232 | + public function write($sid, $data) { |
|
233 | 233 | $this->logger->debug('Saving database session data for SID: ' . $sid . ', data: ' . stringfy_vars($data)); |
234 | 234 | $instance = $this->getModelInstance(); |
235 | 235 | $columns = $this->sessionTableColumns; |
236 | 236 | |
237 | - if($this->getLoader()){ |
|
237 | + if ($this->getLoader()) { |
|
238 | 238 | $this->getLoader()->functions('user_agent'); |
239 | 239 | $this->getLoader()->library('Browser'); |
240 | 240 | } |
241 | - else{ |
|
241 | + else { |
|
242 | 242 | Loader::functions('user_agent'); |
243 | 243 | Loader::library('Browser'); |
244 | 244 | } |
@@ -246,7 +246,7 @@ discard block |
||
246 | 246 | $ip = get_ip(); |
247 | 247 | $keyValue = $instance->getKeyValue(); |
248 | 248 | $host = @gethostbyaddr($ip) or null; |
249 | - $browser = $this->OBJ->browser->getPlatform().', '.$this->OBJ->browser->getBrowser().' '.$this->OBJ->browser->getVersion(); |
|
249 | + $browser = $this->OBJ->browser->getPlatform() . ', ' . $this->OBJ->browser->getBrowser() . ' ' . $this->OBJ->browser->getVersion(); |
|
250 | 250 | $data = $this->encode($data); |
251 | 251 | $params = array( |
252 | 252 | $columns['sid'] => $sid, |
@@ -259,13 +259,13 @@ discard block |
||
259 | 259 | ); |
260 | 260 | $this->logger->info('Database session data to save are listed below :' . stringfy_vars($params)); |
261 | 261 | $exists = $instance->get($sid); |
262 | - if($exists){ |
|
262 | + if ($exists) { |
|
263 | 263 | $this->logger->info('Session data for SID: ' . $sid . ' already exists, just update it'); |
264 | 264 | //update |
265 | 265 | unset($params[$columns['sid']]); |
266 | 266 | $instance->update($sid, $params); |
267 | 267 | } |
268 | - else{ |
|
268 | + else { |
|
269 | 269 | $this->logger->info('Session data for SID: ' . $sid . ' not yet exists, insert it now'); |
270 | 270 | $instance->insert($params); |
271 | 271 | } |
@@ -278,7 +278,7 @@ discard block |
||
278 | 278 | * @param string $sid the session id value |
279 | 279 | * @return boolean |
280 | 280 | */ |
281 | - public function destroy($sid){ |
|
281 | + public function destroy($sid) { |
|
282 | 282 | $this->logger->debug('Destroy of session data for SID: ' . $sid); |
283 | 283 | $instance = $this->modelInstanceName; |
284 | 284 | $instance->delete($sid); |
@@ -290,7 +290,7 @@ discard block |
||
290 | 290 | * @param integer $maxLifetime the max lifetime |
291 | 291 | * @return boolean |
292 | 292 | */ |
293 | - public function gc($maxLifetime){ |
|
293 | + public function gc($maxLifetime) { |
|
294 | 294 | $instance = $this->modelInstanceName; |
295 | 295 | $time = time() - $maxLifetime; |
296 | 296 | $this->logger->debug('Garbage collector of expired session. maxLifetime [' . $maxLifetime . '] sec, expired time [' . $time . ']'); |
@@ -303,9 +303,9 @@ discard block |
||
303 | 303 | * @param mixed $data the session data to encode |
304 | 304 | * @return mixed the encoded session data |
305 | 305 | */ |
306 | - public function encode($data){ |
|
306 | + public function encode($data) { |
|
307 | 307 | $key = base64_decode($this->sessionSecret); |
308 | - $dataEncrypted = openssl_encrypt($data , self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
|
308 | + $dataEncrypted = openssl_encrypt($data, self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
|
309 | 309 | $output = base64_encode($dataEncrypted); |
310 | 310 | return $output; |
311 | 311 | } |
@@ -316,7 +316,7 @@ discard block |
||
316 | 316 | * @param mixed $data the data to decode |
317 | 317 | * @return mixed the decoded data |
318 | 318 | */ |
319 | - public function decode($data){ |
|
319 | + public function decode($data) { |
|
320 | 320 | $key = base64_decode($this->sessionSecret); |
321 | 321 | $data = base64_decode($data); |
322 | 322 | $data = openssl_decrypt($data, self::DB_SESSION_HASH_METHOD, $key, OPENSSL_RAW_DATA, $this->getInitializerVector()); |
@@ -328,7 +328,7 @@ discard block |
||
328 | 328 | * Return the loader instance |
329 | 329 | * @return object Loader the loader instance |
330 | 330 | */ |
331 | - public function getLoader(){ |
|
331 | + public function getLoader() { |
|
332 | 332 | return $this->loader; |
333 | 333 | } |
334 | 334 | |
@@ -336,7 +336,7 @@ discard block |
||
336 | 336 | * set the loader instance for future use |
337 | 337 | * @param object Loader $loader the loader object |
338 | 338 | */ |
339 | - public function setLoader($loader){ |
|
339 | + public function setLoader($loader) { |
|
340 | 340 | $this->loader = $loader; |
341 | 341 | return $this; |
342 | 342 | } |
@@ -345,7 +345,7 @@ discard block |
||
345 | 345 | * Return the model instance |
346 | 346 | * @return object DBSessionHandlerModel the model instance |
347 | 347 | */ |
348 | - public function getModelInstance(){ |
|
348 | + public function getModelInstance() { |
|
349 | 349 | return $this->modelInstanceName; |
350 | 350 | } |
351 | 351 | |
@@ -353,7 +353,7 @@ discard block |
||
353 | 353 | * set the model instance for future use |
354 | 354 | * @param DBSessionHandlerModel $modelInstance the model object |
355 | 355 | */ |
356 | - public function setModelInstance(DBSessionHandlerModel $modelInstance){ |
|
356 | + public function setModelInstance(DBSessionHandlerModel $modelInstance) { |
|
357 | 357 | $this->modelInstanceName = $modelInstance; |
358 | 358 | return $this; |
359 | 359 | } |
@@ -362,7 +362,7 @@ discard block |
||
362 | 362 | * Return the Log instance |
363 | 363 | * @return Log |
364 | 364 | */ |
365 | - public function getLogger(){ |
|
365 | + public function getLogger() { |
|
366 | 366 | return $this->logger; |
367 | 367 | } |
368 | 368 | |
@@ -370,7 +370,7 @@ discard block |
||
370 | 370 | * Set the log instance |
371 | 371 | * @param Log $logger the log object |
372 | 372 | */ |
373 | - public function setLogger(Log $logger){ |
|
373 | + public function setLogger(Log $logger) { |
|
374 | 374 | $this->logger = $logger; |
375 | 375 | return $this; |
376 | 376 | } |
@@ -378,18 +378,18 @@ discard block |
||
378 | 378 | /** |
379 | 379 | * Set the model instance using the configuration for session |
380 | 380 | */ |
381 | - private function setModelInstanceFromConfig(){ |
|
381 | + private function setModelInstanceFromConfig() { |
|
382 | 382 | $modelName = get_config('session_save_path'); |
383 | 383 | $this->logger->info('The database session model: ' . $modelName); |
384 | - if($this->getLoader()){ |
|
384 | + if ($this->getLoader()) { |
|
385 | 385 | $this->getLoader()->model($modelName, 'dbsessionhandlerinstance'); |
386 | 386 | } |
387 | 387 | //@codeCoverageIgnoreStart |
388 | - else{ |
|
388 | + else { |
|
389 | 389 | Loader::model($modelName, 'dbsessionhandlerinstance'); |
390 | 390 | } |
391 | - if(isset($this->OBJ->dbsessionhandlerinstance) && ! $this->OBJ->dbsessionhandlerinstance instanceof DBSessionHandlerModel){ |
|
392 | - show_error('To use database session handler, your class model "'.get_class($this->OBJ->dbsessionhandlerinstance).'" need extends "DBSessionHandlerModel"'); |
|
391 | + if (isset($this->OBJ->dbsessionhandlerinstance) && !$this->OBJ->dbsessionhandlerinstance instanceof DBSessionHandlerModel) { |
|
392 | + show_error('To use database session handler, your class model "' . get_class($this->OBJ->dbsessionhandlerinstance) . '" need extends "DBSessionHandlerModel"'); |
|
393 | 393 | } |
394 | 394 | //@codeCoverageIgnoreEnd |
395 | 395 |
@@ -1,595 +1,595 @@ discard block |
||
1 | 1 | <?php |
2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
3 | - /** |
|
4 | - * TNH Framework |
|
5 | - * |
|
6 | - * A simple PHP framework using HMVC architecture |
|
7 | - * |
|
8 | - * This content is released under the GNU GPL License (GPL) |
|
9 | - * |
|
10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
11 | - * |
|
12 | - * This program is free software; you can redistribute it and/or |
|
13 | - * modify it under the terms of the GNU General Public License |
|
14 | - * as published by the Free Software Foundation; either version 3 |
|
15 | - * of the License, or (at your option) any later version. |
|
16 | - * |
|
17 | - * This program is distributed in the hope that it will be useful, |
|
18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | - * GNU General Public License for more details. |
|
21 | - * |
|
22 | - * You should have received a copy of the GNU General Public License |
|
23 | - * along with this program; if not, write to the Free Software |
|
24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | - */ |
|
26 | - |
|
27 | - |
|
28 | - /** |
|
29 | - * A base model with a series of CRUD functions (powered by CI's query builder), |
|
30 | - * validation-in-model support, event callbacks and more. |
|
31 | - * |
|
32 | - * @link http://github.com/jamierumbelow/codeigniter-base-model |
|
33 | - * @copyright Copyright (c) 2012, Jamie Rumbelow <http://jamierumbelow.net> |
|
34 | - */ |
|
35 | - |
|
36 | - class Model{ |
|
37 | - |
|
38 | - /* -------------------------------------------------------------- |
|
2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
3 | + /** |
|
4 | + * TNH Framework |
|
5 | + * |
|
6 | + * A simple PHP framework using HMVC architecture |
|
7 | + * |
|
8 | + * This content is released under the GNU GPL License (GPL) |
|
9 | + * |
|
10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
11 | + * |
|
12 | + * This program is free software; you can redistribute it and/or |
|
13 | + * modify it under the terms of the GNU General Public License |
|
14 | + * as published by the Free Software Foundation; either version 3 |
|
15 | + * of the License, or (at your option) any later version. |
|
16 | + * |
|
17 | + * This program is distributed in the hope that it will be useful, |
|
18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | + * GNU General Public License for more details. |
|
21 | + * |
|
22 | + * You should have received a copy of the GNU General Public License |
|
23 | + * along with this program; if not, write to the Free Software |
|
24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | + */ |
|
26 | + |
|
27 | + |
|
28 | + /** |
|
29 | + * A base model with a series of CRUD functions (powered by CI's query builder), |
|
30 | + * validation-in-model support, event callbacks and more. |
|
31 | + * |
|
32 | + * @link http://github.com/jamierumbelow/codeigniter-base-model |
|
33 | + * @copyright Copyright (c) 2012, Jamie Rumbelow <http://jamierumbelow.net> |
|
34 | + */ |
|
35 | + |
|
36 | + class Model{ |
|
37 | + |
|
38 | + /* -------------------------------------------------------------- |
|
39 | 39 | * VARIABLES |
40 | 40 | * ------------------------------------------------------------ */ |
41 | 41 | |
42 | - /** |
|
43 | - * This model's default database table. Automatically |
|
44 | - * guessed by pluralising the model name. |
|
45 | - */ |
|
46 | - protected $_table; |
|
47 | - |
|
48 | - /** |
|
49 | - * The database connection object. Will be set to the default |
|
50 | - * connection. This allows individual models to use different DBs |
|
51 | - * without overwriting the global database connection. |
|
52 | - */ |
|
53 | - protected $_database; |
|
54 | - |
|
55 | - /** |
|
56 | - * This model's default primary key or unique identifier. |
|
57 | - * Used by the get(), update() and delete() functions. |
|
58 | - */ |
|
59 | - protected $primary_key = 'id'; |
|
60 | - |
|
61 | - /** |
|
62 | - * Support for soft deletes and this model's 'deleted' key |
|
63 | - */ |
|
64 | - protected $soft_delete = false; |
|
65 | - protected $soft_delete_key = 'is_deleted'; |
|
66 | - protected $_temporary_with_deleted = FALSE; |
|
67 | - protected $_temporary_only_deleted = FALSE; |
|
68 | - |
|
69 | - /** |
|
70 | - * The various callbacks available to the model. Each are |
|
71 | - * simple lists of method names (methods will be run on $this). |
|
72 | - */ |
|
73 | - protected $before_create = array(); |
|
74 | - protected $after_create = array(); |
|
75 | - protected $before_update = array(); |
|
76 | - protected $after_update = array(); |
|
77 | - protected $before_get = array(); |
|
78 | - protected $after_get = array(); |
|
79 | - protected $before_delete = array(); |
|
80 | - protected $after_delete = array(); |
|
81 | - |
|
82 | - protected $callback_parameters = array(); |
|
83 | - |
|
84 | - /** |
|
85 | - * Protected, non-modifiable attributes |
|
86 | - */ |
|
87 | - protected $protected_attributes = array(); |
|
88 | - |
|
89 | - /** |
|
90 | - * Relationship arrays. Use flat strings for defaults or string |
|
91 | - * => array to customise the class name and primary key |
|
92 | - */ |
|
93 | - protected $belongs_to = array(); |
|
94 | - protected $has_many = array(); |
|
95 | - |
|
96 | - protected $_with = array(); |
|
97 | - |
|
98 | - /** |
|
99 | - * An array of validation rules. This needs to be the same format |
|
100 | - * as validation rules passed to the FormValidation library. |
|
101 | - */ |
|
102 | - protected $validate = array(); |
|
103 | - |
|
104 | - /** |
|
105 | - * Optionally skip the validation. Used in conjunction with |
|
106 | - * skip_validation() to skip data validation for any future calls. |
|
107 | - */ |
|
108 | - protected $skip_validation = FALSE; |
|
109 | - |
|
110 | - /** |
|
111 | - * By default we return our results as objects. If we need to override |
|
112 | - * this, we can, or, we could use the `as_array()` and `as_object()` scopes. |
|
113 | - */ |
|
114 | - protected $return_type = 'object'; |
|
115 | - protected $_temporary_return_type = NULL; |
|
42 | + /** |
|
43 | + * This model's default database table. Automatically |
|
44 | + * guessed by pluralising the model name. |
|
45 | + */ |
|
46 | + protected $_table; |
|
47 | + |
|
48 | + /** |
|
49 | + * The database connection object. Will be set to the default |
|
50 | + * connection. This allows individual models to use different DBs |
|
51 | + * without overwriting the global database connection. |
|
52 | + */ |
|
53 | + protected $_database; |
|
54 | + |
|
55 | + /** |
|
56 | + * This model's default primary key or unique identifier. |
|
57 | + * Used by the get(), update() and delete() functions. |
|
58 | + */ |
|
59 | + protected $primary_key = 'id'; |
|
60 | + |
|
61 | + /** |
|
62 | + * Support for soft deletes and this model's 'deleted' key |
|
63 | + */ |
|
64 | + protected $soft_delete = false; |
|
65 | + protected $soft_delete_key = 'is_deleted'; |
|
66 | + protected $_temporary_with_deleted = FALSE; |
|
67 | + protected $_temporary_only_deleted = FALSE; |
|
68 | + |
|
69 | + /** |
|
70 | + * The various callbacks available to the model. Each are |
|
71 | + * simple lists of method names (methods will be run on $this). |
|
72 | + */ |
|
73 | + protected $before_create = array(); |
|
74 | + protected $after_create = array(); |
|
75 | + protected $before_update = array(); |
|
76 | + protected $after_update = array(); |
|
77 | + protected $before_get = array(); |
|
78 | + protected $after_get = array(); |
|
79 | + protected $before_delete = array(); |
|
80 | + protected $after_delete = array(); |
|
81 | + |
|
82 | + protected $callback_parameters = array(); |
|
83 | + |
|
84 | + /** |
|
85 | + * Protected, non-modifiable attributes |
|
86 | + */ |
|
87 | + protected $protected_attributes = array(); |
|
88 | + |
|
89 | + /** |
|
90 | + * Relationship arrays. Use flat strings for defaults or string |
|
91 | + * => array to customise the class name and primary key |
|
92 | + */ |
|
93 | + protected $belongs_to = array(); |
|
94 | + protected $has_many = array(); |
|
95 | + |
|
96 | + protected $_with = array(); |
|
97 | + |
|
98 | + /** |
|
99 | + * An array of validation rules. This needs to be the same format |
|
100 | + * as validation rules passed to the FormValidation library. |
|
101 | + */ |
|
102 | + protected $validate = array(); |
|
103 | + |
|
104 | + /** |
|
105 | + * Optionally skip the validation. Used in conjunction with |
|
106 | + * skip_validation() to skip data validation for any future calls. |
|
107 | + */ |
|
108 | + protected $skip_validation = FALSE; |
|
109 | + |
|
110 | + /** |
|
111 | + * By default we return our results as objects. If we need to override |
|
112 | + * this, we can, or, we could use the `as_array()` and `as_object()` scopes. |
|
113 | + */ |
|
114 | + protected $return_type = 'object'; |
|
115 | + protected $_temporary_return_type = NULL; |
|
116 | 116 | |
117 | 117 | |
118 | - /** |
|
118 | + /** |
|
119 | 119 | The database cache time |
120 | - */ |
|
121 | - protected $dbCacheTime = 0; |
|
122 | - |
|
123 | - /** |
|
124 | - * Instance of the Loader class |
|
125 | - * @var Loader |
|
126 | - */ |
|
127 | - protected $loaderInstance = null; |
|
128 | - |
|
129 | - /** |
|
130 | - * Instance of the FormValidation library |
|
131 | - * @var FormValidation |
|
132 | - */ |
|
133 | - protected $formValidationInstance = null; |
|
134 | - |
|
135 | - /* -------------------------------------------------------------- |
|
120 | + */ |
|
121 | + protected $dbCacheTime = 0; |
|
122 | + |
|
123 | + /** |
|
124 | + * Instance of the Loader class |
|
125 | + * @var Loader |
|
126 | + */ |
|
127 | + protected $loaderInstance = null; |
|
128 | + |
|
129 | + /** |
|
130 | + * Instance of the FormValidation library |
|
131 | + * @var FormValidation |
|
132 | + */ |
|
133 | + protected $formValidationInstance = null; |
|
134 | + |
|
135 | + /* -------------------------------------------------------------- |
|
136 | 136 | * GENERIC METHODS |
137 | 137 | * ------------------------------------------------------------ */ |
138 | 138 | |
139 | - /** |
|
140 | - * Initialise the model, tie into the CodeIgniter superobject and |
|
141 | - * try our best to guess the table name. |
|
142 | - */ |
|
143 | - public function __construct(Database $db = null){ |
|
144 | - if(is_object($db)){ |
|
145 | - $this->setDatabaseInstance($db); |
|
146 | - } |
|
147 | - else{ |
|
148 | - $obj = & get_instance(); |
|
149 | - if(isset($obj->database) && is_object($obj->database)){ |
|
150 | - /** |
|
151 | - * NOTE: Need use "clone" because some Model need have the personal instance of the database library |
|
152 | - * to prevent duplication |
|
153 | - */ |
|
154 | - $this->setDatabaseInstance(clone $obj->database); |
|
155 | - } |
|
156 | - } |
|
157 | - |
|
158 | - array_unshift($this->before_create, 'protect_attributes'); |
|
159 | - array_unshift($this->before_update, 'protect_attributes'); |
|
160 | - $this->_temporary_return_type = $this->return_type; |
|
161 | - } |
|
162 | - |
|
163 | - /* -------------------------------------------------------------- |
|
139 | + /** |
|
140 | + * Initialise the model, tie into the CodeIgniter superobject and |
|
141 | + * try our best to guess the table name. |
|
142 | + */ |
|
143 | + public function __construct(Database $db = null){ |
|
144 | + if(is_object($db)){ |
|
145 | + $this->setDatabaseInstance($db); |
|
146 | + } |
|
147 | + else{ |
|
148 | + $obj = & get_instance(); |
|
149 | + if(isset($obj->database) && is_object($obj->database)){ |
|
150 | + /** |
|
151 | + * NOTE: Need use "clone" because some Model need have the personal instance of the database library |
|
152 | + * to prevent duplication |
|
153 | + */ |
|
154 | + $this->setDatabaseInstance(clone $obj->database); |
|
155 | + } |
|
156 | + } |
|
157 | + |
|
158 | + array_unshift($this->before_create, 'protect_attributes'); |
|
159 | + array_unshift($this->before_update, 'protect_attributes'); |
|
160 | + $this->_temporary_return_type = $this->return_type; |
|
161 | + } |
|
162 | + |
|
163 | + /* -------------------------------------------------------------- |
|
164 | 164 | * CRUD INTERFACE |
165 | 165 | * ------------------------------------------------------------ */ |
166 | 166 | |
167 | - /** |
|
168 | - * Fetch a single record based on the primary key. Returns an object. |
|
169 | - */ |
|
170 | - public function get($primary_value) |
|
171 | - { |
|
172 | - return $this->get_by($this->primary_key, $primary_value); |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Fetch a single record based on an arbitrary WHERE call. Can be |
|
177 | - * any valid value to $this->_database->where(). |
|
178 | - */ |
|
179 | - public function get_by() |
|
180 | - { |
|
181 | - $where = func_get_args(); |
|
182 | - |
|
183 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
184 | - { |
|
185 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
186 | - } |
|
187 | - |
|
188 | - $this->_set_where($where); |
|
189 | - |
|
190 | - $this->trigger('before_get'); |
|
167 | + /** |
|
168 | + * Fetch a single record based on the primary key. Returns an object. |
|
169 | + */ |
|
170 | + public function get($primary_value) |
|
171 | + { |
|
172 | + return $this->get_by($this->primary_key, $primary_value); |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Fetch a single record based on an arbitrary WHERE call. Can be |
|
177 | + * any valid value to $this->_database->where(). |
|
178 | + */ |
|
179 | + public function get_by() |
|
180 | + { |
|
181 | + $where = func_get_args(); |
|
182 | + |
|
183 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
184 | + { |
|
185 | + $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
186 | + } |
|
187 | + |
|
188 | + $this->_set_where($where); |
|
189 | + |
|
190 | + $this->trigger('before_get'); |
|
191 | 191 | $type = $this->_temporary_return_type == 'array' ? 'array' : false; |
192 | - $row = $this->_database->from($this->_table)->get($type); |
|
193 | - $this->_temporary_return_type = $this->return_type; |
|
194 | - $row = $this->trigger('after_get', $row); |
|
195 | - $this->_with = array(); |
|
196 | - return $row; |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Fetch an array of records based on an array of primary values. |
|
201 | - */ |
|
202 | - public function get_many($values) |
|
203 | - { |
|
204 | - $this->_database->in($this->primary_key, $values); |
|
205 | - return $this->get_all(); |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * Fetch an array of records based on an arbitrary WHERE call. |
|
210 | - */ |
|
211 | - public function get_many_by() |
|
212 | - { |
|
213 | - $where = func_get_args(); |
|
214 | - $this->_set_where($where); |
|
215 | - return $this->get_all(); |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * Fetch all the records in the table. Can be used as a generic call |
|
220 | - * to $this->_database->get() with scoped methods. |
|
221 | - */ |
|
222 | - public function get_all() |
|
223 | - { |
|
224 | - $this->trigger('before_get'); |
|
225 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
226 | - { |
|
227 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
228 | - } |
|
192 | + $row = $this->_database->from($this->_table)->get($type); |
|
193 | + $this->_temporary_return_type = $this->return_type; |
|
194 | + $row = $this->trigger('after_get', $row); |
|
195 | + $this->_with = array(); |
|
196 | + return $row; |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Fetch an array of records based on an array of primary values. |
|
201 | + */ |
|
202 | + public function get_many($values) |
|
203 | + { |
|
204 | + $this->_database->in($this->primary_key, $values); |
|
205 | + return $this->get_all(); |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * Fetch an array of records based on an arbitrary WHERE call. |
|
210 | + */ |
|
211 | + public function get_many_by() |
|
212 | + { |
|
213 | + $where = func_get_args(); |
|
214 | + $this->_set_where($where); |
|
215 | + return $this->get_all(); |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * Fetch all the records in the table. Can be used as a generic call |
|
220 | + * to $this->_database->get() with scoped methods. |
|
221 | + */ |
|
222 | + public function get_all() |
|
223 | + { |
|
224 | + $this->trigger('before_get'); |
|
225 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
226 | + { |
|
227 | + $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
228 | + } |
|
229 | 229 | $type = $this->_temporary_return_type == 'array' ? 'array':false; |
230 | - $result = $this->_database->from($this->_table)->getAll($type); |
|
231 | - $this->_temporary_return_type = $this->return_type; |
|
232 | - |
|
233 | - foreach ($result as $key => &$row) |
|
234 | - { |
|
235 | - $row = $this->trigger('after_get', $row, ($key == count($result) - 1)); |
|
236 | - } |
|
237 | - $this->_with = array(); |
|
238 | - return $result; |
|
239 | - } |
|
240 | - |
|
241 | - /** |
|
242 | - * Insert a new row into the table. $data should be an associative array |
|
243 | - * of data to be inserted. Returns newly created ID. |
|
244 | - */ |
|
245 | - public function insert($data = array(), $skip_validation = FALSE, $escape = true) |
|
246 | - { |
|
247 | - if ($skip_validation === FALSE) |
|
248 | - { |
|
249 | - $data = $this->validate($data); |
|
250 | - } |
|
251 | - |
|
252 | - if ($data !== FALSE) |
|
253 | - { |
|
254 | - $data = $this->trigger('before_create', $data); |
|
255 | - $this->_database->from($this->_table)->insert($data, $escape); |
|
256 | - $insert_id = $this->_database->insertId(); |
|
257 | - $this->trigger('after_create', $insert_id); |
|
258 | - return $insert_id; |
|
259 | - } |
|
260 | - else |
|
261 | - { |
|
262 | - return FALSE; |
|
263 | - } |
|
264 | - } |
|
265 | - |
|
266 | - /** |
|
267 | - * Insert multiple rows into the table. Returns an array of multiple IDs. |
|
268 | - */ |
|
269 | - public function insert_many($data = array(), $skip_validation = FALSE, $escape = true) |
|
270 | - { |
|
271 | - $ids = array(); |
|
272 | - foreach ($data as $key => $row) |
|
273 | - { |
|
274 | - $ids[] = $this->insert($row, $skip_validation, $escape); |
|
275 | - } |
|
276 | - return $ids; |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Updated a record based on the primary value. |
|
281 | - */ |
|
282 | - public function update($primary_value, $data = array(), $skip_validation = FALSE, $escape = true) |
|
283 | - { |
|
284 | - $data = $this->trigger('before_update', $data); |
|
285 | - if ($skip_validation === FALSE) |
|
286 | - { |
|
287 | - $data = $this->validate($data); |
|
288 | - } |
|
289 | - |
|
290 | - if ($data !== FALSE) |
|
291 | - { |
|
292 | - $result = $this->_database->where($this->primary_key, $primary_value) |
|
293 | - ->from($this->_table) |
|
294 | - ->update($data, $escape); |
|
295 | - $this->trigger('after_update', array($data, $result)); |
|
296 | - return $result; |
|
297 | - } |
|
298 | - else |
|
299 | - { |
|
300 | - return FALSE; |
|
301 | - } |
|
302 | - } |
|
303 | - |
|
304 | - /** |
|
305 | - * Update many records, based on an array of primary values. |
|
306 | - */ |
|
307 | - public function update_many($primary_values, $data = array(), $skip_validation = FALSE, $escape = true) |
|
308 | - { |
|
309 | - $data = $this->trigger('before_update', $data); |
|
310 | - if ($skip_validation === FALSE) |
|
311 | - { |
|
312 | - $data = $this->validate($data); |
|
313 | - } |
|
314 | - if ($data !== FALSE) |
|
315 | - { |
|
316 | - $result = $this->_database->in($this->primary_key, $primary_values) |
|
317 | - ->from($this->_table) |
|
318 | - ->update($data, $escape); |
|
319 | - $this->trigger('after_update', array($data, $result)); |
|
320 | - return $result; |
|
321 | - } |
|
322 | - else |
|
323 | - { |
|
324 | - return FALSE; |
|
325 | - } |
|
326 | - } |
|
327 | - |
|
328 | - /** |
|
329 | - * Updated a record based on an arbitrary WHERE clause. |
|
330 | - */ |
|
331 | - public function update_by() |
|
332 | - { |
|
333 | - $args = func_get_args(); |
|
334 | - $data = array(); |
|
335 | - if(count($args) == 2){ |
|
336 | - if(is_array($args[1])){ |
|
337 | - $data = array_pop($args); |
|
338 | - } |
|
339 | - } |
|
340 | - else if(count($args) == 3){ |
|
341 | - if(is_array($args[2])){ |
|
342 | - $data = array_pop($args); |
|
343 | - } |
|
344 | - } |
|
345 | - $data = $this->trigger('before_update', $data); |
|
346 | - if ($this->validate($data) !== FALSE) |
|
347 | - { |
|
348 | - $this->_set_where($args); |
|
349 | - $result = $this->_database->from($this->_table)->update($data); |
|
350 | - $this->trigger('after_update', array($data, $result)); |
|
351 | - return $result; |
|
352 | - } |
|
353 | - else |
|
354 | - { |
|
355 | - return FALSE; |
|
356 | - } |
|
357 | - } |
|
358 | - |
|
359 | - /** |
|
360 | - * Update all records |
|
361 | - */ |
|
362 | - public function update_all($data = array(), $escape = true) |
|
363 | - { |
|
364 | - $data = $this->trigger('before_update', $data); |
|
365 | - $result = $this->_database->from($this->_table)->update($data, $escape); |
|
366 | - $this->trigger('after_update', array($data, $result)); |
|
367 | - return $result; |
|
368 | - } |
|
369 | - |
|
370 | - /** |
|
371 | - * Delete a row from the table by the primary value |
|
372 | - */ |
|
373 | - public function delete($id) |
|
374 | - { |
|
375 | - $this->trigger('before_delete', $id); |
|
376 | - $this->_database->where($this->primary_key, $id); |
|
377 | - if ($this->soft_delete) |
|
378 | - { |
|
379 | - $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
380 | - } |
|
381 | - else |
|
382 | - { |
|
383 | - $result = $this->_database->from($this->_table)->delete(); |
|
384 | - } |
|
385 | - |
|
386 | - $this->trigger('after_delete', $result); |
|
387 | - return $result; |
|
388 | - } |
|
389 | - |
|
390 | - /** |
|
391 | - * Delete a row from the database table by an arbitrary WHERE clause |
|
392 | - */ |
|
393 | - public function delete_by() |
|
394 | - { |
|
395 | - $where = func_get_args(); |
|
396 | - $where = $this->trigger('before_delete', $where); |
|
397 | - $this->_set_where($where); |
|
398 | - if ($this->soft_delete) |
|
399 | - { |
|
400 | - $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
401 | - } |
|
402 | - else |
|
403 | - { |
|
404 | - $result = $this->_database->from($this->_table)->delete(); |
|
405 | - } |
|
406 | - $this->trigger('after_delete', $result); |
|
407 | - return $result; |
|
408 | - } |
|
409 | - |
|
410 | - /** |
|
411 | - * Delete many rows from the database table by multiple primary values |
|
412 | - */ |
|
413 | - public function delete_many($primary_values) |
|
414 | - { |
|
415 | - $primary_values = $this->trigger('before_delete', $primary_values); |
|
416 | - $this->_database->in($this->primary_key, $primary_values); |
|
417 | - if ($this->soft_delete) |
|
418 | - { |
|
419 | - $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
420 | - } |
|
421 | - else |
|
422 | - { |
|
423 | - $result = $this->_database->from($this->_table)->delete(); |
|
424 | - } |
|
425 | - $this->trigger('after_delete', $result); |
|
426 | - return $result; |
|
427 | - } |
|
428 | - |
|
429 | - |
|
430 | - /** |
|
431 | - * Truncates the table |
|
432 | - */ |
|
433 | - public function truncate() |
|
434 | - { |
|
435 | - $result = $this->_database->from($this->_table)->delete(); |
|
436 | - return $result; |
|
437 | - } |
|
438 | - |
|
439 | - /* -------------------------------------------------------------- |
|
230 | + $result = $this->_database->from($this->_table)->getAll($type); |
|
231 | + $this->_temporary_return_type = $this->return_type; |
|
232 | + |
|
233 | + foreach ($result as $key => &$row) |
|
234 | + { |
|
235 | + $row = $this->trigger('after_get', $row, ($key == count($result) - 1)); |
|
236 | + } |
|
237 | + $this->_with = array(); |
|
238 | + return $result; |
|
239 | + } |
|
240 | + |
|
241 | + /** |
|
242 | + * Insert a new row into the table. $data should be an associative array |
|
243 | + * of data to be inserted. Returns newly created ID. |
|
244 | + */ |
|
245 | + public function insert($data = array(), $skip_validation = FALSE, $escape = true) |
|
246 | + { |
|
247 | + if ($skip_validation === FALSE) |
|
248 | + { |
|
249 | + $data = $this->validate($data); |
|
250 | + } |
|
251 | + |
|
252 | + if ($data !== FALSE) |
|
253 | + { |
|
254 | + $data = $this->trigger('before_create', $data); |
|
255 | + $this->_database->from($this->_table)->insert($data, $escape); |
|
256 | + $insert_id = $this->_database->insertId(); |
|
257 | + $this->trigger('after_create', $insert_id); |
|
258 | + return $insert_id; |
|
259 | + } |
|
260 | + else |
|
261 | + { |
|
262 | + return FALSE; |
|
263 | + } |
|
264 | + } |
|
265 | + |
|
266 | + /** |
|
267 | + * Insert multiple rows into the table. Returns an array of multiple IDs. |
|
268 | + */ |
|
269 | + public function insert_many($data = array(), $skip_validation = FALSE, $escape = true) |
|
270 | + { |
|
271 | + $ids = array(); |
|
272 | + foreach ($data as $key => $row) |
|
273 | + { |
|
274 | + $ids[] = $this->insert($row, $skip_validation, $escape); |
|
275 | + } |
|
276 | + return $ids; |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Updated a record based on the primary value. |
|
281 | + */ |
|
282 | + public function update($primary_value, $data = array(), $skip_validation = FALSE, $escape = true) |
|
283 | + { |
|
284 | + $data = $this->trigger('before_update', $data); |
|
285 | + if ($skip_validation === FALSE) |
|
286 | + { |
|
287 | + $data = $this->validate($data); |
|
288 | + } |
|
289 | + |
|
290 | + if ($data !== FALSE) |
|
291 | + { |
|
292 | + $result = $this->_database->where($this->primary_key, $primary_value) |
|
293 | + ->from($this->_table) |
|
294 | + ->update($data, $escape); |
|
295 | + $this->trigger('after_update', array($data, $result)); |
|
296 | + return $result; |
|
297 | + } |
|
298 | + else |
|
299 | + { |
|
300 | + return FALSE; |
|
301 | + } |
|
302 | + } |
|
303 | + |
|
304 | + /** |
|
305 | + * Update many records, based on an array of primary values. |
|
306 | + */ |
|
307 | + public function update_many($primary_values, $data = array(), $skip_validation = FALSE, $escape = true) |
|
308 | + { |
|
309 | + $data = $this->trigger('before_update', $data); |
|
310 | + if ($skip_validation === FALSE) |
|
311 | + { |
|
312 | + $data = $this->validate($data); |
|
313 | + } |
|
314 | + if ($data !== FALSE) |
|
315 | + { |
|
316 | + $result = $this->_database->in($this->primary_key, $primary_values) |
|
317 | + ->from($this->_table) |
|
318 | + ->update($data, $escape); |
|
319 | + $this->trigger('after_update', array($data, $result)); |
|
320 | + return $result; |
|
321 | + } |
|
322 | + else |
|
323 | + { |
|
324 | + return FALSE; |
|
325 | + } |
|
326 | + } |
|
327 | + |
|
328 | + /** |
|
329 | + * Updated a record based on an arbitrary WHERE clause. |
|
330 | + */ |
|
331 | + public function update_by() |
|
332 | + { |
|
333 | + $args = func_get_args(); |
|
334 | + $data = array(); |
|
335 | + if(count($args) == 2){ |
|
336 | + if(is_array($args[1])){ |
|
337 | + $data = array_pop($args); |
|
338 | + } |
|
339 | + } |
|
340 | + else if(count($args) == 3){ |
|
341 | + if(is_array($args[2])){ |
|
342 | + $data = array_pop($args); |
|
343 | + } |
|
344 | + } |
|
345 | + $data = $this->trigger('before_update', $data); |
|
346 | + if ($this->validate($data) !== FALSE) |
|
347 | + { |
|
348 | + $this->_set_where($args); |
|
349 | + $result = $this->_database->from($this->_table)->update($data); |
|
350 | + $this->trigger('after_update', array($data, $result)); |
|
351 | + return $result; |
|
352 | + } |
|
353 | + else |
|
354 | + { |
|
355 | + return FALSE; |
|
356 | + } |
|
357 | + } |
|
358 | + |
|
359 | + /** |
|
360 | + * Update all records |
|
361 | + */ |
|
362 | + public function update_all($data = array(), $escape = true) |
|
363 | + { |
|
364 | + $data = $this->trigger('before_update', $data); |
|
365 | + $result = $this->_database->from($this->_table)->update($data, $escape); |
|
366 | + $this->trigger('after_update', array($data, $result)); |
|
367 | + return $result; |
|
368 | + } |
|
369 | + |
|
370 | + /** |
|
371 | + * Delete a row from the table by the primary value |
|
372 | + */ |
|
373 | + public function delete($id) |
|
374 | + { |
|
375 | + $this->trigger('before_delete', $id); |
|
376 | + $this->_database->where($this->primary_key, $id); |
|
377 | + if ($this->soft_delete) |
|
378 | + { |
|
379 | + $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
380 | + } |
|
381 | + else |
|
382 | + { |
|
383 | + $result = $this->_database->from($this->_table)->delete(); |
|
384 | + } |
|
385 | + |
|
386 | + $this->trigger('after_delete', $result); |
|
387 | + return $result; |
|
388 | + } |
|
389 | + |
|
390 | + /** |
|
391 | + * Delete a row from the database table by an arbitrary WHERE clause |
|
392 | + */ |
|
393 | + public function delete_by() |
|
394 | + { |
|
395 | + $where = func_get_args(); |
|
396 | + $where = $this->trigger('before_delete', $where); |
|
397 | + $this->_set_where($where); |
|
398 | + if ($this->soft_delete) |
|
399 | + { |
|
400 | + $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
401 | + } |
|
402 | + else |
|
403 | + { |
|
404 | + $result = $this->_database->from($this->_table)->delete(); |
|
405 | + } |
|
406 | + $this->trigger('after_delete', $result); |
|
407 | + return $result; |
|
408 | + } |
|
409 | + |
|
410 | + /** |
|
411 | + * Delete many rows from the database table by multiple primary values |
|
412 | + */ |
|
413 | + public function delete_many($primary_values) |
|
414 | + { |
|
415 | + $primary_values = $this->trigger('before_delete', $primary_values); |
|
416 | + $this->_database->in($this->primary_key, $primary_values); |
|
417 | + if ($this->soft_delete) |
|
418 | + { |
|
419 | + $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
420 | + } |
|
421 | + else |
|
422 | + { |
|
423 | + $result = $this->_database->from($this->_table)->delete(); |
|
424 | + } |
|
425 | + $this->trigger('after_delete', $result); |
|
426 | + return $result; |
|
427 | + } |
|
428 | + |
|
429 | + |
|
430 | + /** |
|
431 | + * Truncates the table |
|
432 | + */ |
|
433 | + public function truncate() |
|
434 | + { |
|
435 | + $result = $this->_database->from($this->_table)->delete(); |
|
436 | + return $result; |
|
437 | + } |
|
438 | + |
|
439 | + /* -------------------------------------------------------------- |
|
440 | 440 | * RELATIONSHIPS |
441 | 441 | * ------------------------------------------------------------ */ |
442 | 442 | |
443 | - public function with($relationship) |
|
444 | - { |
|
445 | - $this->_with[] = $relationship; |
|
446 | - if (!in_array('relate', $this->after_get)) |
|
447 | - { |
|
448 | - $this->after_get[] = 'relate'; |
|
449 | - } |
|
450 | - return $this; |
|
451 | - } |
|
452 | - |
|
453 | - public function relate($row) |
|
454 | - { |
|
455 | - if (empty($row)) |
|
456 | - { |
|
457 | - return $row; |
|
458 | - } |
|
459 | - |
|
460 | - foreach ($this->belongs_to as $key => $value) |
|
461 | - { |
|
462 | - if (is_string($value)) |
|
463 | - { |
|
464 | - $relationship = $value; |
|
465 | - $options = array( 'primary_key' => $value . '_id', 'model' => $value . '_model' ); |
|
466 | - } |
|
467 | - else |
|
468 | - { |
|
469 | - $relationship = $key; |
|
470 | - $options = $value; |
|
471 | - } |
|
472 | - |
|
473 | - if (in_array($relationship, $this->_with)) |
|
474 | - { |
|
475 | - if(is_object($this->loaderInstance)){ |
|
476 | - $this->loaderInstance->model($options['model'], $relationship . '_model'); |
|
477 | - } |
|
478 | - else{ |
|
479 | - Loader::model($options['model'], $relationship . '_model'); |
|
480 | - } |
|
481 | - if (is_object($row)) |
|
482 | - { |
|
483 | - $row->{$relationship} = $this->{$relationship . '_model'}->get($row->{$options['primary_key']}); |
|
484 | - } |
|
485 | - else |
|
486 | - { |
|
487 | - $row[$relationship] = $this->{$relationship . '_model'}->get($row[$options['primary_key']]); |
|
488 | - } |
|
489 | - } |
|
490 | - } |
|
491 | - |
|
492 | - foreach ($this->has_many as $key => $value) |
|
493 | - { |
|
494 | - if (is_string($value)) |
|
495 | - { |
|
496 | - $relationship = $value; |
|
497 | - $options = array( 'primary_key' => $this->_table . '_id', 'model' => $value . '_model' ); |
|
498 | - } |
|
499 | - else |
|
500 | - { |
|
501 | - $relationship = $key; |
|
502 | - $options = $value; |
|
503 | - } |
|
504 | - |
|
505 | - if (in_array($relationship, $this->_with)) |
|
506 | - { |
|
507 | - if(is_object($this->loaderInstance)){ |
|
508 | - $this->loaderInstance->model($options['model'], $relationship . '_model'); |
|
509 | - } |
|
510 | - else{ |
|
511 | - Loader::model($options['model'], $relationship . '_model'); |
|
512 | - } |
|
513 | - if (is_object($row)) |
|
514 | - { |
|
515 | - $row->{$relationship} = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row->{$this->primary_key}); |
|
516 | - } |
|
517 | - else |
|
518 | - { |
|
519 | - $row[$relationship] = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row[$this->primary_key]); |
|
520 | - } |
|
521 | - } |
|
522 | - } |
|
523 | - return $row; |
|
524 | - } |
|
525 | - |
|
526 | - /* -------------------------------------------------------------- |
|
443 | + public function with($relationship) |
|
444 | + { |
|
445 | + $this->_with[] = $relationship; |
|
446 | + if (!in_array('relate', $this->after_get)) |
|
447 | + { |
|
448 | + $this->after_get[] = 'relate'; |
|
449 | + } |
|
450 | + return $this; |
|
451 | + } |
|
452 | + |
|
453 | + public function relate($row) |
|
454 | + { |
|
455 | + if (empty($row)) |
|
456 | + { |
|
457 | + return $row; |
|
458 | + } |
|
459 | + |
|
460 | + foreach ($this->belongs_to as $key => $value) |
|
461 | + { |
|
462 | + if (is_string($value)) |
|
463 | + { |
|
464 | + $relationship = $value; |
|
465 | + $options = array( 'primary_key' => $value . '_id', 'model' => $value . '_model' ); |
|
466 | + } |
|
467 | + else |
|
468 | + { |
|
469 | + $relationship = $key; |
|
470 | + $options = $value; |
|
471 | + } |
|
472 | + |
|
473 | + if (in_array($relationship, $this->_with)) |
|
474 | + { |
|
475 | + if(is_object($this->loaderInstance)){ |
|
476 | + $this->loaderInstance->model($options['model'], $relationship . '_model'); |
|
477 | + } |
|
478 | + else{ |
|
479 | + Loader::model($options['model'], $relationship . '_model'); |
|
480 | + } |
|
481 | + if (is_object($row)) |
|
482 | + { |
|
483 | + $row->{$relationship} = $this->{$relationship . '_model'}->get($row->{$options['primary_key']}); |
|
484 | + } |
|
485 | + else |
|
486 | + { |
|
487 | + $row[$relationship] = $this->{$relationship . '_model'}->get($row[$options['primary_key']]); |
|
488 | + } |
|
489 | + } |
|
490 | + } |
|
491 | + |
|
492 | + foreach ($this->has_many as $key => $value) |
|
493 | + { |
|
494 | + if (is_string($value)) |
|
495 | + { |
|
496 | + $relationship = $value; |
|
497 | + $options = array( 'primary_key' => $this->_table . '_id', 'model' => $value . '_model' ); |
|
498 | + } |
|
499 | + else |
|
500 | + { |
|
501 | + $relationship = $key; |
|
502 | + $options = $value; |
|
503 | + } |
|
504 | + |
|
505 | + if (in_array($relationship, $this->_with)) |
|
506 | + { |
|
507 | + if(is_object($this->loaderInstance)){ |
|
508 | + $this->loaderInstance->model($options['model'], $relationship . '_model'); |
|
509 | + } |
|
510 | + else{ |
|
511 | + Loader::model($options['model'], $relationship . '_model'); |
|
512 | + } |
|
513 | + if (is_object($row)) |
|
514 | + { |
|
515 | + $row->{$relationship} = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row->{$this->primary_key}); |
|
516 | + } |
|
517 | + else |
|
518 | + { |
|
519 | + $row[$relationship] = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row[$this->primary_key]); |
|
520 | + } |
|
521 | + } |
|
522 | + } |
|
523 | + return $row; |
|
524 | + } |
|
525 | + |
|
526 | + /* -------------------------------------------------------------- |
|
527 | 527 | * UTILITY METHODS |
528 | 528 | * ------------------------------------------------------------ */ |
529 | 529 | |
530 | - /** |
|
531 | - * Retrieve and generate a form_dropdown friendly array |
|
532 | - */ |
|
533 | - public function dropdown() |
|
534 | - { |
|
535 | - $args = func_get_args(); |
|
536 | - if(count($args) == 2) |
|
537 | - { |
|
538 | - list($key, $value) = $args; |
|
539 | - } |
|
540 | - else |
|
541 | - { |
|
542 | - $key = $this->primary_key; |
|
543 | - $value = $args[0]; |
|
544 | - } |
|
545 | - $this->trigger('before_dropdown', array( $key, $value )); |
|
546 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
547 | - { |
|
548 | - $this->_database->where($this->soft_delete_key, FALSE); |
|
549 | - } |
|
550 | - $result = $this->_database->select(array($key, $value)) |
|
551 | - ->from($this->_table) |
|
552 | - ->getAll(); |
|
553 | - $options = array(); |
|
554 | - foreach ($result as $row) |
|
555 | - { |
|
556 | - $options[$row->{$key}] = $row->{$value}; |
|
557 | - } |
|
558 | - $options = $this->trigger('after_dropdown', $options); |
|
559 | - return $options; |
|
560 | - } |
|
561 | - |
|
562 | - /** |
|
563 | - * Fetch a count of rows based on an arbitrary WHERE call. |
|
564 | - */ |
|
565 | - public function count_by() |
|
566 | - { |
|
567 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
568 | - { |
|
569 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
570 | - } |
|
571 | - $where = func_get_args(); |
|
572 | - $this->_set_where($where); |
|
573 | - $this->_database->from($this->_table)->getAll(); |
|
574 | - return $this->_database->numRows(); |
|
575 | - } |
|
576 | - |
|
577 | - /** |
|
578 | - * Fetch a total count of rows, disregarding any previous conditions |
|
579 | - */ |
|
580 | - public function count_all() |
|
581 | - { |
|
582 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
583 | - { |
|
584 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
585 | - } |
|
586 | - $this->_database->from($this->_table)->getAll(); |
|
587 | - return $this->_database->numRows(); |
|
588 | - } |
|
530 | + /** |
|
531 | + * Retrieve and generate a form_dropdown friendly array |
|
532 | + */ |
|
533 | + public function dropdown() |
|
534 | + { |
|
535 | + $args = func_get_args(); |
|
536 | + if(count($args) == 2) |
|
537 | + { |
|
538 | + list($key, $value) = $args; |
|
539 | + } |
|
540 | + else |
|
541 | + { |
|
542 | + $key = $this->primary_key; |
|
543 | + $value = $args[0]; |
|
544 | + } |
|
545 | + $this->trigger('before_dropdown', array( $key, $value )); |
|
546 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
547 | + { |
|
548 | + $this->_database->where($this->soft_delete_key, FALSE); |
|
549 | + } |
|
550 | + $result = $this->_database->select(array($key, $value)) |
|
551 | + ->from($this->_table) |
|
552 | + ->getAll(); |
|
553 | + $options = array(); |
|
554 | + foreach ($result as $row) |
|
555 | + { |
|
556 | + $options[$row->{$key}] = $row->{$value}; |
|
557 | + } |
|
558 | + $options = $this->trigger('after_dropdown', $options); |
|
559 | + return $options; |
|
560 | + } |
|
561 | + |
|
562 | + /** |
|
563 | + * Fetch a count of rows based on an arbitrary WHERE call. |
|
564 | + */ |
|
565 | + public function count_by() |
|
566 | + { |
|
567 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
568 | + { |
|
569 | + $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
570 | + } |
|
571 | + $where = func_get_args(); |
|
572 | + $this->_set_where($where); |
|
573 | + $this->_database->from($this->_table)->getAll(); |
|
574 | + return $this->_database->numRows(); |
|
575 | + } |
|
576 | + |
|
577 | + /** |
|
578 | + * Fetch a total count of rows, disregarding any previous conditions |
|
579 | + */ |
|
580 | + public function count_all() |
|
581 | + { |
|
582 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
583 | + { |
|
584 | + $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
585 | + } |
|
586 | + $this->_database->from($this->_table)->getAll(); |
|
587 | + return $this->_database->numRows(); |
|
588 | + } |
|
589 | 589 | |
590 | 590 | /** |
591 | - * Enabled cache temporary |
|
592 | - */ |
|
591 | + * Enabled cache temporary |
|
592 | + */ |
|
593 | 593 | public function cached($ttl = 0){ |
594 | 594 | if($ttl > 0){ |
595 | 595 | $this->_database = $this->_database->cached($ttl); |
@@ -597,385 +597,385 @@ discard block |
||
597 | 597 | return $this; |
598 | 598 | } |
599 | 599 | |
600 | - /** |
|
601 | - * Tell the class to skip the insert validation |
|
602 | - */ |
|
603 | - public function skip_validation() |
|
604 | - { |
|
605 | - $this->skip_validation = TRUE; |
|
606 | - return $this; |
|
607 | - } |
|
608 | - |
|
609 | - /** |
|
610 | - * Get the skip validation status |
|
611 | - */ |
|
612 | - public function get_skip_validation() |
|
613 | - { |
|
614 | - return $this->skip_validation; |
|
615 | - } |
|
616 | - |
|
617 | - /** |
|
618 | - * Return the next auto increment of the table. Only tested on MySQL. |
|
619 | - */ |
|
620 | - public function get_next_id() |
|
621 | - { |
|
622 | - return (int) $this->_database->select('AUTO_INCREMENT') |
|
623 | - ->from('information_schema.TABLES') |
|
624 | - ->where('TABLE_NAME', $this->_table) |
|
625 | - ->where('TABLE_SCHEMA', $this->_database->getDatabaseName())->get()->AUTO_INCREMENT; |
|
626 | - } |
|
627 | - |
|
628 | - /** |
|
629 | - * Getter for the table name |
|
630 | - */ |
|
631 | - public function table() |
|
632 | - { |
|
633 | - return $this->_table; |
|
634 | - } |
|
635 | - |
|
636 | - /* -------------------------------------------------------------- |
|
600 | + /** |
|
601 | + * Tell the class to skip the insert validation |
|
602 | + */ |
|
603 | + public function skip_validation() |
|
604 | + { |
|
605 | + $this->skip_validation = TRUE; |
|
606 | + return $this; |
|
607 | + } |
|
608 | + |
|
609 | + /** |
|
610 | + * Get the skip validation status |
|
611 | + */ |
|
612 | + public function get_skip_validation() |
|
613 | + { |
|
614 | + return $this->skip_validation; |
|
615 | + } |
|
616 | + |
|
617 | + /** |
|
618 | + * Return the next auto increment of the table. Only tested on MySQL. |
|
619 | + */ |
|
620 | + public function get_next_id() |
|
621 | + { |
|
622 | + return (int) $this->_database->select('AUTO_INCREMENT') |
|
623 | + ->from('information_schema.TABLES') |
|
624 | + ->where('TABLE_NAME', $this->_table) |
|
625 | + ->where('TABLE_SCHEMA', $this->_database->getDatabaseName())->get()->AUTO_INCREMENT; |
|
626 | + } |
|
627 | + |
|
628 | + /** |
|
629 | + * Getter for the table name |
|
630 | + */ |
|
631 | + public function table() |
|
632 | + { |
|
633 | + return $this->_table; |
|
634 | + } |
|
635 | + |
|
636 | + /* -------------------------------------------------------------- |
|
637 | 637 | * GLOBAL SCOPES |
638 | 638 | * ------------------------------------------------------------ */ |
639 | 639 | |
640 | - /** |
|
641 | - * Return the next call as an array rather than an object |
|
642 | - */ |
|
643 | - public function as_array() |
|
644 | - { |
|
645 | - $this->_temporary_return_type = 'array'; |
|
646 | - return $this; |
|
647 | - } |
|
648 | - |
|
649 | - /** |
|
650 | - * Return the next call as an object rather than an array |
|
651 | - */ |
|
652 | - public function as_object() |
|
653 | - { |
|
654 | - $this->_temporary_return_type = 'object'; |
|
655 | - return $this; |
|
656 | - } |
|
657 | - |
|
658 | - /** |
|
659 | - * Don't care about soft deleted rows on the next call |
|
660 | - */ |
|
661 | - public function with_deleted() |
|
662 | - { |
|
663 | - $this->_temporary_with_deleted = TRUE; |
|
664 | - return $this; |
|
665 | - } |
|
666 | - |
|
667 | - /** |
|
668 | - * Only get deleted rows on the next call |
|
669 | - */ |
|
670 | - public function only_deleted() |
|
671 | - { |
|
672 | - $this->_temporary_only_deleted = TRUE; |
|
673 | - return $this; |
|
674 | - } |
|
675 | - |
|
676 | - /* -------------------------------------------------------------- |
|
640 | + /** |
|
641 | + * Return the next call as an array rather than an object |
|
642 | + */ |
|
643 | + public function as_array() |
|
644 | + { |
|
645 | + $this->_temporary_return_type = 'array'; |
|
646 | + return $this; |
|
647 | + } |
|
648 | + |
|
649 | + /** |
|
650 | + * Return the next call as an object rather than an array |
|
651 | + */ |
|
652 | + public function as_object() |
|
653 | + { |
|
654 | + $this->_temporary_return_type = 'object'; |
|
655 | + return $this; |
|
656 | + } |
|
657 | + |
|
658 | + /** |
|
659 | + * Don't care about soft deleted rows on the next call |
|
660 | + */ |
|
661 | + public function with_deleted() |
|
662 | + { |
|
663 | + $this->_temporary_with_deleted = TRUE; |
|
664 | + return $this; |
|
665 | + } |
|
666 | + |
|
667 | + /** |
|
668 | + * Only get deleted rows on the next call |
|
669 | + */ |
|
670 | + public function only_deleted() |
|
671 | + { |
|
672 | + $this->_temporary_only_deleted = TRUE; |
|
673 | + return $this; |
|
674 | + } |
|
675 | + |
|
676 | + /* -------------------------------------------------------------- |
|
677 | 677 | * OBSERVERS |
678 | 678 | * ------------------------------------------------------------ */ |
679 | 679 | |
680 | - /** |
|
681 | - * MySQL DATETIME created_at and updated_at |
|
682 | - */ |
|
683 | - public function created_at($row) |
|
684 | - { |
|
685 | - if (is_object($row)) |
|
686 | - { |
|
687 | - $row->created_at = date('Y-m-d H:i:s'); |
|
688 | - } |
|
689 | - else |
|
690 | - { |
|
691 | - $row['created_at'] = date('Y-m-d H:i:s'); |
|
692 | - } |
|
693 | - |
|
694 | - return $row; |
|
695 | - } |
|
696 | - |
|
697 | - public function updated_at($row) |
|
698 | - { |
|
699 | - if (is_object($row)) |
|
700 | - { |
|
701 | - $row->updated_at = date('Y-m-d H:i:s'); |
|
702 | - } |
|
703 | - else |
|
704 | - { |
|
705 | - $row['updated_at'] = date('Y-m-d H:i:s'); |
|
706 | - } |
|
707 | - return $row; |
|
708 | - } |
|
709 | - |
|
710 | - /** |
|
711 | - * Serialises data for you automatically, allowing you to pass |
|
712 | - * through objects and let it handle the serialisation in the background |
|
713 | - */ |
|
714 | - public function serialize($row) |
|
715 | - { |
|
716 | - foreach ($this->callback_parameters as $column) |
|
717 | - { |
|
718 | - $row[$column] = serialize($row[$column]); |
|
719 | - } |
|
720 | - return $row; |
|
721 | - } |
|
722 | - |
|
723 | - public function unserialize($row) |
|
724 | - { |
|
725 | - foreach ($this->callback_parameters as $column) |
|
726 | - { |
|
727 | - if (is_array($row)) |
|
728 | - { |
|
729 | - $row[$column] = unserialize($row[$column]); |
|
730 | - } |
|
731 | - else |
|
732 | - { |
|
733 | - $row->$column = unserialize($row->$column); |
|
734 | - } |
|
735 | - } |
|
736 | - return $row; |
|
737 | - } |
|
738 | - |
|
739 | - /** |
|
740 | - * Protect attributes by removing them from $row array |
|
741 | - */ |
|
742 | - public function protect_attributes($row) |
|
743 | - { |
|
744 | - foreach ($this->protected_attributes as $attr) |
|
745 | - { |
|
746 | - if (is_object($row)) |
|
747 | - { |
|
680 | + /** |
|
681 | + * MySQL DATETIME created_at and updated_at |
|
682 | + */ |
|
683 | + public function created_at($row) |
|
684 | + { |
|
685 | + if (is_object($row)) |
|
686 | + { |
|
687 | + $row->created_at = date('Y-m-d H:i:s'); |
|
688 | + } |
|
689 | + else |
|
690 | + { |
|
691 | + $row['created_at'] = date('Y-m-d H:i:s'); |
|
692 | + } |
|
693 | + |
|
694 | + return $row; |
|
695 | + } |
|
696 | + |
|
697 | + public function updated_at($row) |
|
698 | + { |
|
699 | + if (is_object($row)) |
|
700 | + { |
|
701 | + $row->updated_at = date('Y-m-d H:i:s'); |
|
702 | + } |
|
703 | + else |
|
704 | + { |
|
705 | + $row['updated_at'] = date('Y-m-d H:i:s'); |
|
706 | + } |
|
707 | + return $row; |
|
708 | + } |
|
709 | + |
|
710 | + /** |
|
711 | + * Serialises data for you automatically, allowing you to pass |
|
712 | + * through objects and let it handle the serialisation in the background |
|
713 | + */ |
|
714 | + public function serialize($row) |
|
715 | + { |
|
716 | + foreach ($this->callback_parameters as $column) |
|
717 | + { |
|
718 | + $row[$column] = serialize($row[$column]); |
|
719 | + } |
|
720 | + return $row; |
|
721 | + } |
|
722 | + |
|
723 | + public function unserialize($row) |
|
724 | + { |
|
725 | + foreach ($this->callback_parameters as $column) |
|
726 | + { |
|
727 | + if (is_array($row)) |
|
728 | + { |
|
729 | + $row[$column] = unserialize($row[$column]); |
|
730 | + } |
|
731 | + else |
|
732 | + { |
|
733 | + $row->$column = unserialize($row->$column); |
|
734 | + } |
|
735 | + } |
|
736 | + return $row; |
|
737 | + } |
|
738 | + |
|
739 | + /** |
|
740 | + * Protect attributes by removing them from $row array |
|
741 | + */ |
|
742 | + public function protect_attributes($row) |
|
743 | + { |
|
744 | + foreach ($this->protected_attributes as $attr) |
|
745 | + { |
|
746 | + if (is_object($row)) |
|
747 | + { |
|
748 | 748 | if(isset($row->$attr)){ |
749 | 749 | unset($row->$attr); |
750 | 750 | } |
751 | - } |
|
752 | - else |
|
753 | - { |
|
751 | + } |
|
752 | + else |
|
753 | + { |
|
754 | 754 | if(isset($row[$attr])){ |
755 | 755 | unset($row[$attr]); |
756 | 756 | } |
757 | - } |
|
758 | - } |
|
759 | - return $row; |
|
760 | - } |
|
757 | + } |
|
758 | + } |
|
759 | + return $row; |
|
760 | + } |
|
761 | 761 | |
762 | 762 | /** |
763 | - * Return the database instance |
|
764 | - * @return Database the database instance |
|
765 | - */ |
|
766 | - public function getDatabaseInstance(){ |
|
767 | - return $this->_database; |
|
768 | - } |
|
769 | - |
|
770 | - /** |
|
771 | - * set the Database instance for future use |
|
772 | - * @param Database $db the database object |
|
773 | - */ |
|
774 | - public function setDatabaseInstance($db){ |
|
775 | - $this->_database = $db; |
|
776 | - if($this->dbCacheTime > 0){ |
|
777 | - $this->_database->setCache($this->dbCacheTime); |
|
778 | - } |
|
779 | - return $this; |
|
780 | - } |
|
781 | - |
|
782 | - /** |
|
783 | - * Return the loader instance |
|
784 | - * @return Loader the loader instance |
|
785 | - */ |
|
786 | - public function getLoader(){ |
|
787 | - return $this->loaderInstance; |
|
788 | - } |
|
789 | - |
|
790 | - /** |
|
791 | - * set the loader instance for future use |
|
792 | - * @param Loader $loader the loader object |
|
793 | - */ |
|
794 | - public function setLoader($loader){ |
|
795 | - $this->loaderInstance = $loader; |
|
796 | - return $this; |
|
797 | - } |
|
798 | - |
|
799 | - /** |
|
800 | - * Return the FormValidation instance |
|
801 | - * @return FormValidation the form validation instance |
|
802 | - */ |
|
803 | - public function getFormValidation(){ |
|
804 | - return $this->formValidationInstance; |
|
805 | - } |
|
806 | - |
|
807 | - /** |
|
808 | - * set the form validation instance for future use |
|
809 | - * @param FormValidation $fv the form validation object |
|
810 | - */ |
|
811 | - public function setFormValidation($fv){ |
|
812 | - $this->formValidationInstance = $fv; |
|
813 | - return $this; |
|
814 | - } |
|
815 | - |
|
816 | - /* -------------------------------------------------------------- |
|
763 | + * Return the database instance |
|
764 | + * @return Database the database instance |
|
765 | + */ |
|
766 | + public function getDatabaseInstance(){ |
|
767 | + return $this->_database; |
|
768 | + } |
|
769 | + |
|
770 | + /** |
|
771 | + * set the Database instance for future use |
|
772 | + * @param Database $db the database object |
|
773 | + */ |
|
774 | + public function setDatabaseInstance($db){ |
|
775 | + $this->_database = $db; |
|
776 | + if($this->dbCacheTime > 0){ |
|
777 | + $this->_database->setCache($this->dbCacheTime); |
|
778 | + } |
|
779 | + return $this; |
|
780 | + } |
|
781 | + |
|
782 | + /** |
|
783 | + * Return the loader instance |
|
784 | + * @return Loader the loader instance |
|
785 | + */ |
|
786 | + public function getLoader(){ |
|
787 | + return $this->loaderInstance; |
|
788 | + } |
|
789 | + |
|
790 | + /** |
|
791 | + * set the loader instance for future use |
|
792 | + * @param Loader $loader the loader object |
|
793 | + */ |
|
794 | + public function setLoader($loader){ |
|
795 | + $this->loaderInstance = $loader; |
|
796 | + return $this; |
|
797 | + } |
|
798 | + |
|
799 | + /** |
|
800 | + * Return the FormValidation instance |
|
801 | + * @return FormValidation the form validation instance |
|
802 | + */ |
|
803 | + public function getFormValidation(){ |
|
804 | + return $this->formValidationInstance; |
|
805 | + } |
|
806 | + |
|
807 | + /** |
|
808 | + * set the form validation instance for future use |
|
809 | + * @param FormValidation $fv the form validation object |
|
810 | + */ |
|
811 | + public function setFormValidation($fv){ |
|
812 | + $this->formValidationInstance = $fv; |
|
813 | + return $this; |
|
814 | + } |
|
815 | + |
|
816 | + /* -------------------------------------------------------------- |
|
817 | 817 | * QUERY BUILDER DIRECT ACCESS METHODS |
818 | 818 | * ------------------------------------------------------------ */ |
819 | 819 | |
820 | - /** |
|
821 | - * A wrapper to $this->_database->orderBy() |
|
822 | - */ |
|
823 | - public function order_by($criteria, $order = 'ASC') |
|
824 | - { |
|
825 | - if ( is_array($criteria) ) |
|
826 | - { |
|
827 | - foreach ($criteria as $key => $value) |
|
828 | - { |
|
829 | - $this->_database->orderBy($key, $value); |
|
830 | - } |
|
831 | - } |
|
832 | - else |
|
833 | - { |
|
834 | - $this->_database->orderBy($criteria, $order); |
|
835 | - } |
|
836 | - return $this; |
|
837 | - } |
|
838 | - |
|
839 | - /** |
|
840 | - * A wrapper to $this->_database->limit() |
|
841 | - */ |
|
842 | - public function limit($offset = 0, $limit = 10) |
|
843 | - { |
|
844 | - $this->_database->limit($offset, $limit); |
|
845 | - return $this; |
|
846 | - } |
|
847 | - |
|
848 | - /* -------------------------------------------------------------- |
|
820 | + /** |
|
821 | + * A wrapper to $this->_database->orderBy() |
|
822 | + */ |
|
823 | + public function order_by($criteria, $order = 'ASC') |
|
824 | + { |
|
825 | + if ( is_array($criteria) ) |
|
826 | + { |
|
827 | + foreach ($criteria as $key => $value) |
|
828 | + { |
|
829 | + $this->_database->orderBy($key, $value); |
|
830 | + } |
|
831 | + } |
|
832 | + else |
|
833 | + { |
|
834 | + $this->_database->orderBy($criteria, $order); |
|
835 | + } |
|
836 | + return $this; |
|
837 | + } |
|
838 | + |
|
839 | + /** |
|
840 | + * A wrapper to $this->_database->limit() |
|
841 | + */ |
|
842 | + public function limit($offset = 0, $limit = 10) |
|
843 | + { |
|
844 | + $this->_database->limit($offset, $limit); |
|
845 | + return $this; |
|
846 | + } |
|
847 | + |
|
848 | + /* -------------------------------------------------------------- |
|
849 | 849 | * INTERNAL METHODS |
850 | 850 | * ------------------------------------------------------------ */ |
851 | 851 | |
852 | - /** |
|
853 | - * Trigger an event and call its observers. Pass through the event name |
|
854 | - * (which looks for an instance variable $this->event_name), an array of |
|
855 | - * parameters to pass through and an optional 'last in interation' boolean |
|
856 | - */ |
|
857 | - protected function trigger($event, $data = FALSE, $last = TRUE) |
|
858 | - { |
|
859 | - if (isset($this->$event) && is_array($this->$event)) |
|
860 | - { |
|
861 | - foreach ($this->$event as $method) |
|
862 | - { |
|
863 | - if (strpos($method, '(')) |
|
864 | - { |
|
865 | - preg_match('/([a-zA-Z0-9\_\-]+)(\(([a-zA-Z0-9\_\-\., ]+)\))?/', $method, $matches); |
|
866 | - |
|
867 | - $method = $matches[1]; |
|
868 | - $this->callback_parameters = explode(',', $matches[3]); |
|
869 | - } |
|
870 | - $data = call_user_func_array(array($this, $method), array($data, $last)); |
|
871 | - } |
|
872 | - } |
|
873 | - return $data; |
|
874 | - } |
|
875 | - |
|
876 | - /** |
|
877 | - * Run validation on the passed data |
|
878 | - */ |
|
879 | - protected function validate(array $data) |
|
880 | - { |
|
881 | - if($this->skip_validation) |
|
882 | - { |
|
883 | - return $data; |
|
884 | - } |
|
885 | - if(!empty($this->validate)) |
|
886 | - { |
|
887 | - $fv = null; |
|
888 | - if(is_object($this->formValidationInstance)){ |
|
889 | - $fv = $this->formValidationInstance; |
|
890 | - } |
|
891 | - else{ |
|
892 | - Loader::library('FormValidation'); |
|
893 | - $fv = $this->formvalidation; |
|
894 | - $this->setFormValidation($fv); |
|
895 | - } |
|
852 | + /** |
|
853 | + * Trigger an event and call its observers. Pass through the event name |
|
854 | + * (which looks for an instance variable $this->event_name), an array of |
|
855 | + * parameters to pass through and an optional 'last in interation' boolean |
|
856 | + */ |
|
857 | + protected function trigger($event, $data = FALSE, $last = TRUE) |
|
858 | + { |
|
859 | + if (isset($this->$event) && is_array($this->$event)) |
|
860 | + { |
|
861 | + foreach ($this->$event as $method) |
|
862 | + { |
|
863 | + if (strpos($method, '(')) |
|
864 | + { |
|
865 | + preg_match('/([a-zA-Z0-9\_\-]+)(\(([a-zA-Z0-9\_\-\., ]+)\))?/', $method, $matches); |
|
866 | + |
|
867 | + $method = $matches[1]; |
|
868 | + $this->callback_parameters = explode(',', $matches[3]); |
|
869 | + } |
|
870 | + $data = call_user_func_array(array($this, $method), array($data, $last)); |
|
871 | + } |
|
872 | + } |
|
873 | + return $data; |
|
874 | + } |
|
875 | + |
|
876 | + /** |
|
877 | + * Run validation on the passed data |
|
878 | + */ |
|
879 | + protected function validate(array $data) |
|
880 | + { |
|
881 | + if($this->skip_validation) |
|
882 | + { |
|
883 | + return $data; |
|
884 | + } |
|
885 | + if(!empty($this->validate)) |
|
886 | + { |
|
887 | + $fv = null; |
|
888 | + if(is_object($this->formValidationInstance)){ |
|
889 | + $fv = $this->formValidationInstance; |
|
890 | + } |
|
891 | + else{ |
|
892 | + Loader::library('FormValidation'); |
|
893 | + $fv = $this->formvalidation; |
|
894 | + $this->setFormValidation($fv); |
|
895 | + } |
|
896 | 896 | |
897 | - $fv->setData($data); |
|
898 | - $fv->setRules($this->validate); |
|
899 | - |
|
900 | - if ($fv->run()) |
|
901 | - { |
|
902 | - return $data; |
|
903 | - } |
|
904 | - else |
|
905 | - { |
|
906 | - return FALSE; |
|
907 | - } |
|
908 | - } |
|
909 | - else |
|
910 | - { |
|
911 | - return $data; |
|
912 | - } |
|
913 | - } |
|
914 | - |
|
915 | - |
|
916 | - /** |
|
917 | - * Set WHERE parameters, cleverly |
|
918 | - */ |
|
919 | - protected function _set_where($params) |
|
920 | - { |
|
921 | - if (count($params) == 1 && is_array($params[0])) |
|
922 | - { |
|
923 | - foreach ($params[0] as $field => $filter) |
|
924 | - { |
|
925 | - if (is_array($filter)) |
|
926 | - { |
|
927 | - $this->_database->in($field, $filter); |
|
928 | - } |
|
929 | - else |
|
930 | - { |
|
931 | - if (is_int($field)) |
|
932 | - { |
|
933 | - $this->_database->where($filter); |
|
934 | - } |
|
935 | - else |
|
936 | - { |
|
937 | - $this->_database->where($field, $filter); |
|
938 | - } |
|
939 | - } |
|
940 | - } |
|
941 | - } |
|
942 | - else if (count($params) == 1) |
|
943 | - { |
|
944 | - $this->_database->where($params[0]); |
|
945 | - } |
|
946 | - else if(count($params) == 2) |
|
947 | - { |
|
948 | - if (is_array($params[1])) |
|
949 | - { |
|
950 | - $this->_database->in($params[0], $params[1]); |
|
951 | - } |
|
952 | - else |
|
953 | - { |
|
954 | - $this->_database->where($params[0], $params[1]); |
|
955 | - } |
|
956 | - } |
|
957 | - else if(count($params) == 3) |
|
958 | - { |
|
959 | - $this->_database->where($params[0], $params[1], $params[2]); |
|
960 | - } |
|
961 | - else |
|
962 | - { |
|
963 | - if (is_array($params[1])) |
|
964 | - { |
|
965 | - $this->_database->in($params[0], $params[1]); |
|
966 | - } |
|
967 | - else |
|
968 | - { |
|
969 | - $this->_database->where($params[0], $params[1]); |
|
970 | - } |
|
971 | - } |
|
972 | - } |
|
973 | - |
|
974 | - /** |
|
897 | + $fv->setData($data); |
|
898 | + $fv->setRules($this->validate); |
|
899 | + |
|
900 | + if ($fv->run()) |
|
901 | + { |
|
902 | + return $data; |
|
903 | + } |
|
904 | + else |
|
905 | + { |
|
906 | + return FALSE; |
|
907 | + } |
|
908 | + } |
|
909 | + else |
|
910 | + { |
|
911 | + return $data; |
|
912 | + } |
|
913 | + } |
|
914 | + |
|
915 | + |
|
916 | + /** |
|
917 | + * Set WHERE parameters, cleverly |
|
918 | + */ |
|
919 | + protected function _set_where($params) |
|
920 | + { |
|
921 | + if (count($params) == 1 && is_array($params[0])) |
|
922 | + { |
|
923 | + foreach ($params[0] as $field => $filter) |
|
924 | + { |
|
925 | + if (is_array($filter)) |
|
926 | + { |
|
927 | + $this->_database->in($field, $filter); |
|
928 | + } |
|
929 | + else |
|
930 | + { |
|
931 | + if (is_int($field)) |
|
932 | + { |
|
933 | + $this->_database->where($filter); |
|
934 | + } |
|
935 | + else |
|
936 | + { |
|
937 | + $this->_database->where($field, $filter); |
|
938 | + } |
|
939 | + } |
|
940 | + } |
|
941 | + } |
|
942 | + else if (count($params) == 1) |
|
943 | + { |
|
944 | + $this->_database->where($params[0]); |
|
945 | + } |
|
946 | + else if(count($params) == 2) |
|
947 | + { |
|
948 | + if (is_array($params[1])) |
|
949 | + { |
|
950 | + $this->_database->in($params[0], $params[1]); |
|
951 | + } |
|
952 | + else |
|
953 | + { |
|
954 | + $this->_database->where($params[0], $params[1]); |
|
955 | + } |
|
956 | + } |
|
957 | + else if(count($params) == 3) |
|
958 | + { |
|
959 | + $this->_database->where($params[0], $params[1], $params[2]); |
|
960 | + } |
|
961 | + else |
|
962 | + { |
|
963 | + if (is_array($params[1])) |
|
964 | + { |
|
965 | + $this->_database->in($params[0], $params[1]); |
|
966 | + } |
|
967 | + else |
|
968 | + { |
|
969 | + $this->_database->where($params[0], $params[1]); |
|
970 | + } |
|
971 | + } |
|
972 | + } |
|
973 | + |
|
974 | + /** |
|
975 | 975 | Shortcut to controller |
976 | - */ |
|
977 | - public function __get($key){ |
|
978 | - return get_instance()->{$key}; |
|
979 | - } |
|
976 | + */ |
|
977 | + public function __get($key){ |
|
978 | + return get_instance()->{$key}; |
|
979 | + } |
|
980 | 980 | |
981 | - } |
|
981 | + } |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | * @copyright Copyright (c) 2012, Jamie Rumbelow <http://jamierumbelow.net> |
34 | 34 | */ |
35 | 35 | |
36 | - class Model{ |
|
36 | + class Model { |
|
37 | 37 | |
38 | 38 | /* -------------------------------------------------------------- |
39 | 39 | * VARIABLES |
@@ -140,13 +140,13 @@ discard block |
||
140 | 140 | * Initialise the model, tie into the CodeIgniter superobject and |
141 | 141 | * try our best to guess the table name. |
142 | 142 | */ |
143 | - public function __construct(Database $db = null){ |
|
144 | - if(is_object($db)){ |
|
143 | + public function __construct(Database $db = null) { |
|
144 | + if (is_object($db)) { |
|
145 | 145 | $this->setDatabaseInstance($db); |
146 | 146 | } |
147 | - else{ |
|
147 | + else { |
|
148 | 148 | $obj = & get_instance(); |
149 | - if(isset($obj->database) && is_object($obj->database)){ |
|
149 | + if (isset($obj->database) && is_object($obj->database)) { |
|
150 | 150 | /** |
151 | 151 | * NOTE: Need use "clone" because some Model need have the personal instance of the database library |
152 | 152 | * to prevent duplication |
@@ -182,7 +182,7 @@ discard block |
||
182 | 182 | |
183 | 183 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
184 | 184 | { |
185 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
185 | + $this->_database->where($this->soft_delete_key, (bool) $this->_temporary_only_deleted); |
|
186 | 186 | } |
187 | 187 | |
188 | 188 | $this->_set_where($where); |
@@ -224,9 +224,9 @@ discard block |
||
224 | 224 | $this->trigger('before_get'); |
225 | 225 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
226 | 226 | { |
227 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
227 | + $this->_database->where($this->soft_delete_key, (bool) $this->_temporary_only_deleted); |
|
228 | 228 | } |
229 | - $type = $this->_temporary_return_type == 'array' ? 'array':false; |
|
229 | + $type = $this->_temporary_return_type == 'array' ? 'array' : false; |
|
230 | 230 | $result = $this->_database->from($this->_table)->getAll($type); |
231 | 231 | $this->_temporary_return_type = $this->return_type; |
232 | 232 | |
@@ -332,13 +332,13 @@ discard block |
||
332 | 332 | { |
333 | 333 | $args = func_get_args(); |
334 | 334 | $data = array(); |
335 | - if(count($args) == 2){ |
|
336 | - if(is_array($args[1])){ |
|
335 | + if (count($args) == 2) { |
|
336 | + if (is_array($args[1])) { |
|
337 | 337 | $data = array_pop($args); |
338 | 338 | } |
339 | 339 | } |
340 | - else if(count($args) == 3){ |
|
341 | - if(is_array($args[2])){ |
|
340 | + else if (count($args) == 3) { |
|
341 | + if (is_array($args[2])) { |
|
342 | 342 | $data = array_pop($args); |
343 | 343 | } |
344 | 344 | } |
@@ -376,7 +376,7 @@ discard block |
||
376 | 376 | $this->_database->where($this->primary_key, $id); |
377 | 377 | if ($this->soft_delete) |
378 | 378 | { |
379 | - $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
379 | + $result = $this->_database->from($this->_table)->update(array($this->soft_delete_key => TRUE)); |
|
380 | 380 | } |
381 | 381 | else |
382 | 382 | { |
@@ -397,7 +397,7 @@ discard block |
||
397 | 397 | $this->_set_where($where); |
398 | 398 | if ($this->soft_delete) |
399 | 399 | { |
400 | - $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
400 | + $result = $this->_database->from($this->_table)->update(array($this->soft_delete_key => TRUE)); |
|
401 | 401 | } |
402 | 402 | else |
403 | 403 | { |
@@ -416,7 +416,7 @@ discard block |
||
416 | 416 | $this->_database->in($this->primary_key, $primary_values); |
417 | 417 | if ($this->soft_delete) |
418 | 418 | { |
419 | - $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
419 | + $result = $this->_database->from($this->_table)->update(array($this->soft_delete_key => TRUE)); |
|
420 | 420 | } |
421 | 421 | else |
422 | 422 | { |
@@ -462,7 +462,7 @@ discard block |
||
462 | 462 | if (is_string($value)) |
463 | 463 | { |
464 | 464 | $relationship = $value; |
465 | - $options = array( 'primary_key' => $value . '_id', 'model' => $value . '_model' ); |
|
465 | + $options = array('primary_key' => $value . '_id', 'model' => $value . '_model'); |
|
466 | 466 | } |
467 | 467 | else |
468 | 468 | { |
@@ -472,10 +472,10 @@ discard block |
||
472 | 472 | |
473 | 473 | if (in_array($relationship, $this->_with)) |
474 | 474 | { |
475 | - if(is_object($this->loaderInstance)){ |
|
475 | + if (is_object($this->loaderInstance)) { |
|
476 | 476 | $this->loaderInstance->model($options['model'], $relationship . '_model'); |
477 | 477 | } |
478 | - else{ |
|
478 | + else { |
|
479 | 479 | Loader::model($options['model'], $relationship . '_model'); |
480 | 480 | } |
481 | 481 | if (is_object($row)) |
@@ -494,7 +494,7 @@ discard block |
||
494 | 494 | if (is_string($value)) |
495 | 495 | { |
496 | 496 | $relationship = $value; |
497 | - $options = array( 'primary_key' => $this->_table . '_id', 'model' => $value . '_model' ); |
|
497 | + $options = array('primary_key' => $this->_table . '_id', 'model' => $value . '_model'); |
|
498 | 498 | } |
499 | 499 | else |
500 | 500 | { |
@@ -504,10 +504,10 @@ discard block |
||
504 | 504 | |
505 | 505 | if (in_array($relationship, $this->_with)) |
506 | 506 | { |
507 | - if(is_object($this->loaderInstance)){ |
|
507 | + if (is_object($this->loaderInstance)) { |
|
508 | 508 | $this->loaderInstance->model($options['model'], $relationship . '_model'); |
509 | 509 | } |
510 | - else{ |
|
510 | + else { |
|
511 | 511 | Loader::model($options['model'], $relationship . '_model'); |
512 | 512 | } |
513 | 513 | if (is_object($row)) |
@@ -533,7 +533,7 @@ discard block |
||
533 | 533 | public function dropdown() |
534 | 534 | { |
535 | 535 | $args = func_get_args(); |
536 | - if(count($args) == 2) |
|
536 | + if (count($args) == 2) |
|
537 | 537 | { |
538 | 538 | list($key, $value) = $args; |
539 | 539 | } |
@@ -542,7 +542,7 @@ discard block |
||
542 | 542 | $key = $this->primary_key; |
543 | 543 | $value = $args[0]; |
544 | 544 | } |
545 | - $this->trigger('before_dropdown', array( $key, $value )); |
|
545 | + $this->trigger('before_dropdown', array($key, $value)); |
|
546 | 546 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
547 | 547 | { |
548 | 548 | $this->_database->where($this->soft_delete_key, FALSE); |
@@ -566,7 +566,7 @@ discard block |
||
566 | 566 | { |
567 | 567 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
568 | 568 | { |
569 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
569 | + $this->_database->where($this->soft_delete_key, (bool) $this->_temporary_only_deleted); |
|
570 | 570 | } |
571 | 571 | $where = func_get_args(); |
572 | 572 | $this->_set_where($where); |
@@ -581,7 +581,7 @@ discard block |
||
581 | 581 | { |
582 | 582 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
583 | 583 | { |
584 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
584 | + $this->_database->where($this->soft_delete_key, (bool) $this->_temporary_only_deleted); |
|
585 | 585 | } |
586 | 586 | $this->_database->from($this->_table)->getAll(); |
587 | 587 | return $this->_database->numRows(); |
@@ -590,8 +590,8 @@ discard block |
||
590 | 590 | /** |
591 | 591 | * Enabled cache temporary |
592 | 592 | */ |
593 | - public function cached($ttl = 0){ |
|
594 | - if($ttl > 0){ |
|
593 | + public function cached($ttl = 0) { |
|
594 | + if ($ttl > 0) { |
|
595 | 595 | $this->_database = $this->_database->cached($ttl); |
596 | 596 | } |
597 | 597 | return $this; |
@@ -745,13 +745,13 @@ discard block |
||
745 | 745 | { |
746 | 746 | if (is_object($row)) |
747 | 747 | { |
748 | - if(isset($row->$attr)){ |
|
748 | + if (isset($row->$attr)) { |
|
749 | 749 | unset($row->$attr); |
750 | 750 | } |
751 | 751 | } |
752 | 752 | else |
753 | 753 | { |
754 | - if(isset($row[$attr])){ |
|
754 | + if (isset($row[$attr])) { |
|
755 | 755 | unset($row[$attr]); |
756 | 756 | } |
757 | 757 | } |
@@ -763,7 +763,7 @@ discard block |
||
763 | 763 | * Return the database instance |
764 | 764 | * @return Database the database instance |
765 | 765 | */ |
766 | - public function getDatabaseInstance(){ |
|
766 | + public function getDatabaseInstance() { |
|
767 | 767 | return $this->_database; |
768 | 768 | } |
769 | 769 | |
@@ -771,9 +771,9 @@ discard block |
||
771 | 771 | * set the Database instance for future use |
772 | 772 | * @param Database $db the database object |
773 | 773 | */ |
774 | - public function setDatabaseInstance($db){ |
|
774 | + public function setDatabaseInstance($db) { |
|
775 | 775 | $this->_database = $db; |
776 | - if($this->dbCacheTime > 0){ |
|
776 | + if ($this->dbCacheTime > 0) { |
|
777 | 777 | $this->_database->setCache($this->dbCacheTime); |
778 | 778 | } |
779 | 779 | return $this; |
@@ -783,7 +783,7 @@ discard block |
||
783 | 783 | * Return the loader instance |
784 | 784 | * @return Loader the loader instance |
785 | 785 | */ |
786 | - public function getLoader(){ |
|
786 | + public function getLoader() { |
|
787 | 787 | return $this->loaderInstance; |
788 | 788 | } |
789 | 789 | |
@@ -791,7 +791,7 @@ discard block |
||
791 | 791 | * set the loader instance for future use |
792 | 792 | * @param Loader $loader the loader object |
793 | 793 | */ |
794 | - public function setLoader($loader){ |
|
794 | + public function setLoader($loader) { |
|
795 | 795 | $this->loaderInstance = $loader; |
796 | 796 | return $this; |
797 | 797 | } |
@@ -800,7 +800,7 @@ discard block |
||
800 | 800 | * Return the FormValidation instance |
801 | 801 | * @return FormValidation the form validation instance |
802 | 802 | */ |
803 | - public function getFormValidation(){ |
|
803 | + public function getFormValidation() { |
|
804 | 804 | return $this->formValidationInstance; |
805 | 805 | } |
806 | 806 | |
@@ -808,7 +808,7 @@ discard block |
||
808 | 808 | * set the form validation instance for future use |
809 | 809 | * @param FormValidation $fv the form validation object |
810 | 810 | */ |
811 | - public function setFormValidation($fv){ |
|
811 | + public function setFormValidation($fv) { |
|
812 | 812 | $this->formValidationInstance = $fv; |
813 | 813 | return $this; |
814 | 814 | } |
@@ -822,7 +822,7 @@ discard block |
||
822 | 822 | */ |
823 | 823 | public function order_by($criteria, $order = 'ASC') |
824 | 824 | { |
825 | - if ( is_array($criteria) ) |
|
825 | + if (is_array($criteria)) |
|
826 | 826 | { |
827 | 827 | foreach ($criteria as $key => $value) |
828 | 828 | { |
@@ -878,17 +878,17 @@ discard block |
||
878 | 878 | */ |
879 | 879 | protected function validate(array $data) |
880 | 880 | { |
881 | - if($this->skip_validation) |
|
881 | + if ($this->skip_validation) |
|
882 | 882 | { |
883 | 883 | return $data; |
884 | 884 | } |
885 | - if(!empty($this->validate)) |
|
885 | + if (!empty($this->validate)) |
|
886 | 886 | { |
887 | 887 | $fv = null; |
888 | - if(is_object($this->formValidationInstance)){ |
|
888 | + if (is_object($this->formValidationInstance)) { |
|
889 | 889 | $fv = $this->formValidationInstance; |
890 | 890 | } |
891 | - else{ |
|
891 | + else { |
|
892 | 892 | Loader::library('FormValidation'); |
893 | 893 | $fv = $this->formvalidation; |
894 | 894 | $this->setFormValidation($fv); |
@@ -943,7 +943,7 @@ discard block |
||
943 | 943 | { |
944 | 944 | $this->_database->where($params[0]); |
945 | 945 | } |
946 | - else if(count($params) == 2) |
|
946 | + else if (count($params) == 2) |
|
947 | 947 | { |
948 | 948 | if (is_array($params[1])) |
949 | 949 | { |
@@ -954,7 +954,7 @@ discard block |
||
954 | 954 | $this->_database->where($params[0], $params[1]); |
955 | 955 | } |
956 | 956 | } |
957 | - else if(count($params) == 3) |
|
957 | + else if (count($params) == 3) |
|
958 | 958 | { |
959 | 959 | $this->_database->where($params[0], $params[1], $params[2]); |
960 | 960 | } |
@@ -974,7 +974,7 @@ discard block |
||
974 | 974 | /** |
975 | 975 | Shortcut to controller |
976 | 976 | */ |
977 | - public function __get($key){ |
|
977 | + public function __get($key) { |
|
978 | 978 | return get_instance()->{$key}; |
979 | 979 | } |
980 | 980 |
@@ -143,8 +143,7 @@ discard block |
||
143 | 143 | public function __construct(Database $db = null){ |
144 | 144 | if(is_object($db)){ |
145 | 145 | $this->setDatabaseInstance($db); |
146 | - } |
|
147 | - else{ |
|
146 | + } else{ |
|
148 | 147 | $obj = & get_instance(); |
149 | 148 | if(isset($obj->database) && is_object($obj->database)){ |
150 | 149 | /** |
@@ -256,8 +255,7 @@ discard block |
||
256 | 255 | $insert_id = $this->_database->insertId(); |
257 | 256 | $this->trigger('after_create', $insert_id); |
258 | 257 | return $insert_id; |
259 | - } |
|
260 | - else |
|
258 | + } else |
|
261 | 259 | { |
262 | 260 | return FALSE; |
263 | 261 | } |
@@ -294,8 +292,7 @@ discard block |
||
294 | 292 | ->update($data, $escape); |
295 | 293 | $this->trigger('after_update', array($data, $result)); |
296 | 294 | return $result; |
297 | - } |
|
298 | - else |
|
295 | + } else |
|
299 | 296 | { |
300 | 297 | return FALSE; |
301 | 298 | } |
@@ -318,8 +315,7 @@ discard block |
||
318 | 315 | ->update($data, $escape); |
319 | 316 | $this->trigger('after_update', array($data, $result)); |
320 | 317 | return $result; |
321 | - } |
|
322 | - else |
|
318 | + } else |
|
323 | 319 | { |
324 | 320 | return FALSE; |
325 | 321 | } |
@@ -336,8 +332,7 @@ discard block |
||
336 | 332 | if(is_array($args[1])){ |
337 | 333 | $data = array_pop($args); |
338 | 334 | } |
339 | - } |
|
340 | - else if(count($args) == 3){ |
|
335 | + } else if(count($args) == 3){ |
|
341 | 336 | if(is_array($args[2])){ |
342 | 337 | $data = array_pop($args); |
343 | 338 | } |
@@ -349,8 +344,7 @@ discard block |
||
349 | 344 | $result = $this->_database->from($this->_table)->update($data); |
350 | 345 | $this->trigger('after_update', array($data, $result)); |
351 | 346 | return $result; |
352 | - } |
|
353 | - else |
|
347 | + } else |
|
354 | 348 | { |
355 | 349 | return FALSE; |
356 | 350 | } |
@@ -377,8 +371,7 @@ discard block |
||
377 | 371 | if ($this->soft_delete) |
378 | 372 | { |
379 | 373 | $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
380 | - } |
|
381 | - else |
|
374 | + } else |
|
382 | 375 | { |
383 | 376 | $result = $this->_database->from($this->_table)->delete(); |
384 | 377 | } |
@@ -398,8 +391,7 @@ discard block |
||
398 | 391 | if ($this->soft_delete) |
399 | 392 | { |
400 | 393 | $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
401 | - } |
|
402 | - else |
|
394 | + } else |
|
403 | 395 | { |
404 | 396 | $result = $this->_database->from($this->_table)->delete(); |
405 | 397 | } |
@@ -417,8 +409,7 @@ discard block |
||
417 | 409 | if ($this->soft_delete) |
418 | 410 | { |
419 | 411 | $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
420 | - } |
|
421 | - else |
|
412 | + } else |
|
422 | 413 | { |
423 | 414 | $result = $this->_database->from($this->_table)->delete(); |
424 | 415 | } |
@@ -463,8 +454,7 @@ discard block |
||
463 | 454 | { |
464 | 455 | $relationship = $value; |
465 | 456 | $options = array( 'primary_key' => $value . '_id', 'model' => $value . '_model' ); |
466 | - } |
|
467 | - else |
|
457 | + } else |
|
468 | 458 | { |
469 | 459 | $relationship = $key; |
470 | 460 | $options = $value; |
@@ -474,15 +464,13 @@ discard block |
||
474 | 464 | { |
475 | 465 | if(is_object($this->loaderInstance)){ |
476 | 466 | $this->loaderInstance->model($options['model'], $relationship . '_model'); |
477 | - } |
|
478 | - else{ |
|
467 | + } else{ |
|
479 | 468 | Loader::model($options['model'], $relationship . '_model'); |
480 | 469 | } |
481 | 470 | if (is_object($row)) |
482 | 471 | { |
483 | 472 | $row->{$relationship} = $this->{$relationship . '_model'}->get($row->{$options['primary_key']}); |
484 | - } |
|
485 | - else |
|
473 | + } else |
|
486 | 474 | { |
487 | 475 | $row[$relationship] = $this->{$relationship . '_model'}->get($row[$options['primary_key']]); |
488 | 476 | } |
@@ -495,8 +483,7 @@ discard block |
||
495 | 483 | { |
496 | 484 | $relationship = $value; |
497 | 485 | $options = array( 'primary_key' => $this->_table . '_id', 'model' => $value . '_model' ); |
498 | - } |
|
499 | - else |
|
486 | + } else |
|
500 | 487 | { |
501 | 488 | $relationship = $key; |
502 | 489 | $options = $value; |
@@ -506,15 +493,13 @@ discard block |
||
506 | 493 | { |
507 | 494 | if(is_object($this->loaderInstance)){ |
508 | 495 | $this->loaderInstance->model($options['model'], $relationship . '_model'); |
509 | - } |
|
510 | - else{ |
|
496 | + } else{ |
|
511 | 497 | Loader::model($options['model'], $relationship . '_model'); |
512 | 498 | } |
513 | 499 | if (is_object($row)) |
514 | 500 | { |
515 | 501 | $row->{$relationship} = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row->{$this->primary_key}); |
516 | - } |
|
517 | - else |
|
502 | + } else |
|
518 | 503 | { |
519 | 504 | $row[$relationship] = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row[$this->primary_key]); |
520 | 505 | } |
@@ -536,8 +521,7 @@ discard block |
||
536 | 521 | if(count($args) == 2) |
537 | 522 | { |
538 | 523 | list($key, $value) = $args; |
539 | - } |
|
540 | - else |
|
524 | + } else |
|
541 | 525 | { |
542 | 526 | $key = $this->primary_key; |
543 | 527 | $value = $args[0]; |
@@ -685,8 +669,7 @@ discard block |
||
685 | 669 | if (is_object($row)) |
686 | 670 | { |
687 | 671 | $row->created_at = date('Y-m-d H:i:s'); |
688 | - } |
|
689 | - else |
|
672 | + } else |
|
690 | 673 | { |
691 | 674 | $row['created_at'] = date('Y-m-d H:i:s'); |
692 | 675 | } |
@@ -699,8 +682,7 @@ discard block |
||
699 | 682 | if (is_object($row)) |
700 | 683 | { |
701 | 684 | $row->updated_at = date('Y-m-d H:i:s'); |
702 | - } |
|
703 | - else |
|
685 | + } else |
|
704 | 686 | { |
705 | 687 | $row['updated_at'] = date('Y-m-d H:i:s'); |
706 | 688 | } |
@@ -727,8 +709,7 @@ discard block |
||
727 | 709 | if (is_array($row)) |
728 | 710 | { |
729 | 711 | $row[$column] = unserialize($row[$column]); |
730 | - } |
|
731 | - else |
|
712 | + } else |
|
732 | 713 | { |
733 | 714 | $row->$column = unserialize($row->$column); |
734 | 715 | } |
@@ -748,8 +729,7 @@ discard block |
||
748 | 729 | if(isset($row->$attr)){ |
749 | 730 | unset($row->$attr); |
750 | 731 | } |
751 | - } |
|
752 | - else |
|
732 | + } else |
|
753 | 733 | { |
754 | 734 | if(isset($row[$attr])){ |
755 | 735 | unset($row[$attr]); |
@@ -828,8 +808,7 @@ discard block |
||
828 | 808 | { |
829 | 809 | $this->_database->orderBy($key, $value); |
830 | 810 | } |
831 | - } |
|
832 | - else |
|
811 | + } else |
|
833 | 812 | { |
834 | 813 | $this->_database->orderBy($criteria, $order); |
835 | 814 | } |
@@ -887,8 +866,7 @@ discard block |
||
887 | 866 | $fv = null; |
888 | 867 | if(is_object($this->formValidationInstance)){ |
889 | 868 | $fv = $this->formValidationInstance; |
890 | - } |
|
891 | - else{ |
|
869 | + } else{ |
|
892 | 870 | Loader::library('FormValidation'); |
893 | 871 | $fv = $this->formvalidation; |
894 | 872 | $this->setFormValidation($fv); |
@@ -900,13 +878,11 @@ discard block |
||
900 | 878 | if ($fv->run()) |
901 | 879 | { |
902 | 880 | return $data; |
903 | - } |
|
904 | - else |
|
881 | + } else |
|
905 | 882 | { |
906 | 883 | return FALSE; |
907 | 884 | } |
908 | - } |
|
909 | - else |
|
885 | + } else |
|
910 | 886 | { |
911 | 887 | return $data; |
912 | 888 | } |
@@ -925,46 +901,38 @@ discard block |
||
925 | 901 | if (is_array($filter)) |
926 | 902 | { |
927 | 903 | $this->_database->in($field, $filter); |
928 | - } |
|
929 | - else |
|
904 | + } else |
|
930 | 905 | { |
931 | 906 | if (is_int($field)) |
932 | 907 | { |
933 | 908 | $this->_database->where($filter); |
934 | - } |
|
935 | - else |
|
909 | + } else |
|
936 | 910 | { |
937 | 911 | $this->_database->where($field, $filter); |
938 | 912 | } |
939 | 913 | } |
940 | 914 | } |
941 | - } |
|
942 | - else if (count($params) == 1) |
|
915 | + } else if (count($params) == 1) |
|
943 | 916 | { |
944 | 917 | $this->_database->where($params[0]); |
945 | - } |
|
946 | - else if(count($params) == 2) |
|
918 | + } else if(count($params) == 2) |
|
947 | 919 | { |
948 | 920 | if (is_array($params[1])) |
949 | 921 | { |
950 | 922 | $this->_database->in($params[0], $params[1]); |
951 | - } |
|
952 | - else |
|
923 | + } else |
|
953 | 924 | { |
954 | 925 | $this->_database->where($params[0], $params[1]); |
955 | 926 | } |
956 | - } |
|
957 | - else if(count($params) == 3) |
|
927 | + } else if(count($params) == 3) |
|
958 | 928 | { |
959 | 929 | $this->_database->where($params[0], $params[1], $params[2]); |
960 | - } |
|
961 | - else |
|
930 | + } else |
|
962 | 931 | { |
963 | 932 | if (is_array($params[1])) |
964 | 933 | { |
965 | 934 | $this->_database->in($params[0], $params[1]); |
966 | - } |
|
967 | - else |
|
935 | + } else |
|
968 | 936 | { |
969 | 937 | $this->_database->where($params[0], $params[1]); |
970 | 938 | } |
@@ -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 Response{ |
28 | 28 | |
@@ -57,9 +57,9 @@ discard block |
||
57 | 57 | private $_currentUrlCacheKey = null; |
58 | 58 | |
59 | 59 | /** |
60 | - * Whether we can compress the output using Gzip |
|
61 | - * @var boolean |
|
62 | - */ |
|
60 | + * Whether we can compress the output using Gzip |
|
61 | + * @var boolean |
|
62 | + */ |
|
63 | 63 | private static $_canCompressOutput = false; |
64 | 64 | |
65 | 65 | /** |
@@ -236,8 +236,8 @@ discard block |
||
236 | 236 | } |
237 | 237 | |
238 | 238 | /** |
239 | - * Send the final page output to user |
|
240 | - */ |
|
239 | + * Send the final page output to user |
|
240 | + */ |
|
241 | 241 | public function renderFinalPage(){ |
242 | 242 | $logger = self::getLogger(); |
243 | 243 | $obj = & get_instance(); |
@@ -301,8 +301,8 @@ discard block |
||
301 | 301 | } |
302 | 302 | |
303 | 303 | /** |
304 | - * Send the final page output to user if is cached |
|
305 | - */ |
|
304 | + * Send the final page output to user if is cached |
|
305 | + */ |
|
306 | 306 | public function renderFinalPageFromCache(&$cache){ |
307 | 307 | $logger = self::getLogger(); |
308 | 308 | $url = $this->_currentUrl; |
@@ -362,9 +362,9 @@ discard block |
||
362 | 362 | } |
363 | 363 | |
364 | 364 | /** |
365 | - * Get the final page to be rendered |
|
366 | - * @return string |
|
367 | - */ |
|
365 | + * Get the final page to be rendered |
|
366 | + * @return string |
|
367 | + */ |
|
368 | 368 | public function getFinalPageRendered(){ |
369 | 369 | return $this->_pageRender; |
370 | 370 | } |
@@ -24,7 +24,7 @@ discard block |
||
24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | 25 | */ |
26 | 26 | |
27 | - class Response{ |
|
27 | + class Response { |
|
28 | 28 | |
29 | 29 | /** |
30 | 30 | * The list of request header to send with response |
@@ -65,9 +65,9 @@ discard block |
||
65 | 65 | /** |
66 | 66 | * Construct new response instance |
67 | 67 | */ |
68 | - public function __construct(){ |
|
69 | - $this->_currentUrl = (! empty($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '' ) |
|
70 | - . (! empty($_SERVER['QUERY_STRING']) ? ('?' . $_SERVER['QUERY_STRING']) : '' ); |
|
68 | + public function __construct() { |
|
69 | + $this->_currentUrl = (!empty($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '') |
|
70 | + . (!empty($_SERVER['QUERY_STRING']) ? ('?' . $_SERVER['QUERY_STRING']) : ''); |
|
71 | 71 | |
72 | 72 | $this->_currentUrlCacheKey = md5($this->_currentUrl); |
73 | 73 | |
@@ -82,9 +82,9 @@ discard block |
||
82 | 82 | * Get the logger singleton instance |
83 | 83 | * @return Log the logger instance |
84 | 84 | */ |
85 | - private static function getLogger(){ |
|
86 | - if(self::$logger == null){ |
|
87 | - self::$logger[0] =& class_loader('Log', 'classes'); |
|
85 | + private static function getLogger() { |
|
86 | + if (self::$logger == null) { |
|
87 | + self::$logger[0] = & class_loader('Log', 'classes'); |
|
88 | 88 | self::$logger[0]->setLogger('Library::Response'); |
89 | 89 | } |
90 | 90 | return self::$logger[0]; |
@@ -95,12 +95,12 @@ discard block |
||
95 | 95 | * @param integer $httpCode the HTTP status code |
96 | 96 | * @param array $headers the additional headers to add to the existing headers list |
97 | 97 | */ |
98 | - public static function sendHeaders($httpCode = 200, array $headers = array()){ |
|
98 | + public static function sendHeaders($httpCode = 200, array $headers = array()) { |
|
99 | 99 | set_http_status_header($httpCode); |
100 | 100 | self::setHeaders($headers); |
101 | - if(! headers_sent()){ |
|
102 | - foreach(self::getHeaders() as $key => $value){ |
|
103 | - header($key .': '.$value); |
|
101 | + if (!headers_sent()) { |
|
102 | + foreach (self::getHeaders() as $key => $value) { |
|
103 | + header($key . ': ' . $value); |
|
104 | 104 | } |
105 | 105 | } |
106 | 106 | } |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | * Get the list of the headers |
110 | 110 | * @return array the headers list |
111 | 111 | */ |
112 | - public static function getHeaders(){ |
|
112 | + public static function getHeaders() { |
|
113 | 113 | return self::$headers; |
114 | 114 | } |
115 | 115 | |
@@ -118,7 +118,7 @@ discard block |
||
118 | 118 | * @param string $name the header name |
119 | 119 | * @return string the header value |
120 | 120 | */ |
121 | - public static function getHeader($name){ |
|
121 | + public static function getHeader($name) { |
|
122 | 122 | return array_key_exists($name, self::$headers) ? self::$headers[$name] : null; |
123 | 123 | } |
124 | 124 | |
@@ -128,7 +128,7 @@ discard block |
||
128 | 128 | * @param string $name the header name |
129 | 129 | * @param string $value the header value to be set |
130 | 130 | */ |
131 | - public static function setHeader($name, $value){ |
|
131 | + public static function setHeader($name, $value) { |
|
132 | 132 | self::$headers[$name] = $value; |
133 | 133 | } |
134 | 134 | |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | * @param array $headers the list of the headers to set. |
138 | 138 | * Note: this will merge with the existing headers |
139 | 139 | */ |
140 | - public static function setHeaders(array $headers){ |
|
140 | + public static function setHeaders(array $headers) { |
|
141 | 141 | self::$headers = array_merge(self::getHeaders(), $headers); |
142 | 142 | } |
143 | 143 | |
@@ -145,17 +145,17 @@ discard block |
||
145 | 145 | * Redirect user in the specified page |
146 | 146 | * @param string $path the URL or URI to be redirect to |
147 | 147 | */ |
148 | - public static function redirect($path = ''){ |
|
148 | + public static function redirect($path = '') { |
|
149 | 149 | $logger = self::getLogger(); |
150 | 150 | $url = Url::site_url($path); |
151 | - $logger->info('Redirect to URL [' .$url. ']'); |
|
152 | - if(! headers_sent()){ |
|
153 | - header('Location: '.$url); |
|
151 | + $logger->info('Redirect to URL [' . $url . ']'); |
|
152 | + if (!headers_sent()) { |
|
153 | + header('Location: ' . $url); |
|
154 | 154 | exit; |
155 | 155 | } |
156 | - else{ |
|
156 | + else { |
|
157 | 157 | echo '<script> |
158 | - location.href = "'.$url.'"; |
|
158 | + location.href = "'.$url . '"; |
|
159 | 159 | </script>'; |
160 | 160 | } |
161 | 161 | } |
@@ -168,10 +168,10 @@ discard block |
||
168 | 168 | * @return void|string if $return is true will return the view content otherwise |
169 | 169 | * will display the view content. |
170 | 170 | */ |
171 | - public function render($view, $data = null, $return = false){ |
|
171 | + public function render($view, $data = null, $return = false) { |
|
172 | 172 | $logger = self::getLogger(); |
173 | 173 | //convert data to an array |
174 | - $data = ! is_array($data) ? (array) $data : $data; |
|
174 | + $data = !is_array($data) ? (array) $data : $data; |
|
175 | 175 | $view = str_ireplace('.php', '', $view); |
176 | 176 | $view = trim($view, '/\\'); |
177 | 177 | $viewFile = $view . '.php'; |
@@ -180,42 +180,42 @@ discard block |
||
180 | 180 | //super instance |
181 | 181 | $obj = & get_instance(); |
182 | 182 | |
183 | - if(Module::hasModule()){ |
|
183 | + if (Module::hasModule()) { |
|
184 | 184 | //check in module first |
185 | 185 | $logger->debug('Checking the view [' . $view . '] from module list ...'); |
186 | 186 | $mod = null; |
187 | 187 | //check if the request class contains module name |
188 | - if(strpos($view, '/') !== false){ |
|
188 | + if (strpos($view, '/') !== false) { |
|
189 | 189 | $viewPath = explode('/', $view); |
190 | - if(isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())){ |
|
190 | + if (isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())) { |
|
191 | 191 | $mod = $viewPath[0]; |
192 | 192 | array_shift($viewPath); |
193 | 193 | $view = implode('/', $viewPath); |
194 | 194 | $viewFile = $view . '.php'; |
195 | 195 | } |
196 | 196 | } |
197 | - if(! $mod && !empty($obj->moduleName)){ |
|
197 | + if (!$mod && !empty($obj->moduleName)) { |
|
198 | 198 | $mod = $obj->moduleName; |
199 | 199 | } |
200 | - if($mod){ |
|
200 | + if ($mod) { |
|
201 | 201 | $moduleViewPath = Module::findViewFullPath($view, $mod); |
202 | - if($moduleViewPath){ |
|
202 | + if ($moduleViewPath) { |
|
203 | 203 | $path = $moduleViewPath; |
204 | - $logger->info('Found view [' . $view . '] in module [' .$mod. '], the file path is [' .$moduleViewPath. '] we will used it'); |
|
204 | + $logger->info('Found view [' . $view . '] in module [' . $mod . '], the file path is [' . $moduleViewPath . '] we will used it'); |
|
205 | 205 | } |
206 | - else{ |
|
207 | - $logger->info('Cannot find view [' . $view . '] in module [' .$mod. '] using the default location'); |
|
206 | + else { |
|
207 | + $logger->info('Cannot find view [' . $view . '] in module [' . $mod . '] using the default location'); |
|
208 | 208 | } |
209 | 209 | } |
210 | - else{ |
|
210 | + else { |
|
211 | 211 | $logger->info('The current request does not use module using the default location.'); |
212 | 212 | } |
213 | 213 | } |
214 | 214 | $logger->info('The view file path to be loaded is [' . $path . ']'); |
215 | 215 | $found = false; |
216 | - if(file_exists($path)){ |
|
217 | - foreach(get_object_vars($obj) as $key => $value){ |
|
218 | - if(! isset($this->{$key})){ |
|
216 | + if (file_exists($path)) { |
|
217 | + foreach (get_object_vars($obj) as $key => $value) { |
|
218 | + if (!isset($this->{$key})) { |
|
219 | 219 | $this->{$key} = & $obj->{$key}; |
220 | 220 | } |
221 | 221 | } |
@@ -224,39 +224,39 @@ discard block |
||
224 | 224 | //need use require() instead of require_once because can load this view many time |
225 | 225 | require $path; |
226 | 226 | $content = ob_get_clean(); |
227 | - if($return){ |
|
227 | + if ($return) { |
|
228 | 228 | return $content; |
229 | 229 | } |
230 | 230 | $this->_pageRender .= $content; |
231 | 231 | $found = true; |
232 | 232 | } |
233 | - if(! $found){ |
|
234 | - show_error('Unable to find view [' .$view . ']'); |
|
233 | + if (!$found) { |
|
234 | + show_error('Unable to find view [' . $view . ']'); |
|
235 | 235 | } |
236 | 236 | } |
237 | 237 | |
238 | 238 | /** |
239 | 239 | * Send the final page output to user |
240 | 240 | */ |
241 | - public function renderFinalPage(){ |
|
241 | + public function renderFinalPage() { |
|
242 | 242 | $logger = self::getLogger(); |
243 | 243 | $obj = & get_instance(); |
244 | 244 | $cachePageStatus = get_config('cache_enable', false) && !empty($obj->view_cache_enable); |
245 | 245 | $dispatcher = $obj->eventdispatcher; |
246 | 246 | $content = $this->_pageRender; |
247 | - if(! $content){ |
|
247 | + if (!$content) { |
|
248 | 248 | $logger->warning('The final view content is empty.'); |
249 | 249 | return; |
250 | 250 | } |
251 | 251 | //dispatch |
252 | 252 | $event = $dispatcher->dispatch(new Event('FINAL_VIEW_READY', $content, true)); |
253 | - $content = ! empty($event->payload) ? $event->payload : null; |
|
254 | - if(empty($content)){ |
|
253 | + $content = !empty($event->payload) ? $event->payload : null; |
|
254 | + if (empty($content)) { |
|
255 | 255 | $logger->warning('The view content is empty after dispatch to Event Listeners.'); |
256 | 256 | } |
257 | 257 | |
258 | 258 | //check whether need save the page into cache. |
259 | - if($cachePageStatus){ |
|
259 | + if ($cachePageStatus) { |
|
260 | 260 | //current page URL |
261 | 261 | $url = $this->_currentUrl; |
262 | 262 | //Cache view Time to live in second |
@@ -271,14 +271,14 @@ discard block |
||
271 | 271 | |
272 | 272 | //get the cache information to prepare header to send to browser |
273 | 273 | $cacheInfo = $cacheInstance->getInfo($cacheKey); |
274 | - if($cacheInfo){ |
|
274 | + if ($cacheInfo) { |
|
275 | 275 | $lastModified = $cacheInfo['mtime']; |
276 | 276 | $expire = $cacheInfo['expire']; |
277 | 277 | $maxAge = $expire - time(); |
278 | 278 | self::setHeader('Pragma', 'public'); |
279 | 279 | self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
280 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
281 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
280 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire) . ' GMT'); |
|
281 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified) . ' GMT'); |
|
282 | 282 | } |
283 | 283 | } |
284 | 284 | |
@@ -289,10 +289,10 @@ discard block |
||
289 | 289 | $content = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
290 | 290 | |
291 | 291 | //compress the output if is available |
292 | - if (self::$_canCompressOutput){ |
|
292 | + if (self::$_canCompressOutput) { |
|
293 | 293 | ob_start('ob_gzhandler'); |
294 | 294 | } |
295 | - else{ |
|
295 | + else { |
|
296 | 296 | ob_start(); |
297 | 297 | } |
298 | 298 | self::sendHeaders(200); |
@@ -303,7 +303,7 @@ discard block |
||
303 | 303 | /** |
304 | 304 | * Send the final page output to user if is cached |
305 | 305 | */ |
306 | - public function renderFinalPageFromCache(&$cache){ |
|
306 | + public function renderFinalPageFromCache(&$cache) { |
|
307 | 307 | $logger = self::getLogger(); |
308 | 308 | $url = $this->_currentUrl; |
309 | 309 | //the current page cache key for identification |
@@ -312,25 +312,25 @@ discard block |
||
312 | 312 | $logger->debug('Checking if the page content for the URL [' . $url . '] is cached ...'); |
313 | 313 | //get the cache information to prepare header to send to browser |
314 | 314 | $cacheInfo = $cache->getInfo($pageCacheKey); |
315 | - if($cacheInfo){ |
|
315 | + if ($cacheInfo) { |
|
316 | 316 | $lastModified = $cacheInfo['mtime']; |
317 | 317 | $expire = $cacheInfo['expire']; |
318 | 318 | $maxAge = $expire - $_SERVER['REQUEST_TIME']; |
319 | 319 | self::setHeader('Pragma', 'public'); |
320 | 320 | self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
321 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
322 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
323 | - if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ |
|
321 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire) . ' GMT'); |
|
322 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified) . ' GMT'); |
|
323 | + if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { |
|
324 | 324 | $logger->info('The cache page content is not yet expire for the URL [' . $url . '] send 304 header to browser'); |
325 | 325 | self::sendHeaders(304); |
326 | 326 | return; |
327 | 327 | } |
328 | - else{ |
|
328 | + else { |
|
329 | 329 | $logger->info('The cache page content is expired or the browser don\'t send the HTTP_IF_MODIFIED_SINCE header for the URL [' . $url . '] send cache headers to tell the browser'); |
330 | 330 | self::sendHeaders(200); |
331 | 331 | //get the cache content |
332 | 332 | $content = $cache->get($pageCacheKey); |
333 | - if($content){ |
|
333 | + if ($content) { |
|
334 | 334 | $logger->info('The page content for the URL [' . $url . '] already cached just display it'); |
335 | 335 | //load benchmark class |
336 | 336 | $benchmark = & class_loader('Benchmark'); |
@@ -343,17 +343,17 @@ discard block |
||
343 | 343 | |
344 | 344 | ///display the final output |
345 | 345 | //compress the output if is available |
346 | - if (self::$_canCompressOutput){ |
|
346 | + if (self::$_canCompressOutput) { |
|
347 | 347 | ob_start('ob_gzhandler'); |
348 | 348 | } |
349 | - else{ |
|
349 | + else { |
|
350 | 350 | ob_start(); |
351 | 351 | } |
352 | 352 | echo $content; |
353 | 353 | ob_end_flush(); |
354 | 354 | return; |
355 | 355 | } |
356 | - else{ |
|
356 | + else { |
|
357 | 357 | $logger->info('The page cache content for the URL [' . $url . '] is not valid may be already expired'); |
358 | 358 | $cache->delete($pageCacheKey); |
359 | 359 | } |
@@ -365,7 +365,7 @@ discard block |
||
365 | 365 | * Get the final page to be rendered |
366 | 366 | * @return string |
367 | 367 | */ |
368 | - public function getFinalPageRendered(){ |
|
368 | + public function getFinalPageRendered() { |
|
369 | 369 | return $this->_pageRender; |
370 | 370 | } |
371 | 371 | |
@@ -373,14 +373,14 @@ discard block |
||
373 | 373 | * Send the HTTP 404 error if can not found the |
374 | 374 | * routing information for the current request |
375 | 375 | */ |
376 | - public static function send404(){ |
|
376 | + public static function send404() { |
|
377 | 377 | /********* for logs **************/ |
378 | 378 | //can't use $obj = & get_instance() here because the global super object will be available until |
379 | 379 | //the main controller is loaded even for Loader::library('xxxx'); |
380 | 380 | $logger = self::getLogger(); |
381 | - $request =& class_loader('Request', 'classes'); |
|
382 | - $userAgent =& class_loader('Browser'); |
|
383 | - $browser = $userAgent->getPlatform().', '.$userAgent->getBrowser().' '.$userAgent->getVersion(); |
|
381 | + $request = & class_loader('Request', 'classes'); |
|
382 | + $userAgent = & class_loader('Browser'); |
|
383 | + $browser = $userAgent->getPlatform() . ', ' . $userAgent->getBrowser() . ' ' . $userAgent->getVersion(); |
|
384 | 384 | |
385 | 385 | //here can't use Loader::functions just include the helper manually |
386 | 386 | require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
@@ -390,12 +390,12 @@ discard block |
||
390 | 390 | $logger->error($str); |
391 | 391 | /***********************************/ |
392 | 392 | $path = CORE_VIEWS_PATH . '404.php'; |
393 | - if(file_exists($path)){ |
|
393 | + if (file_exists($path)) { |
|
394 | 394 | //compress the output if is available |
395 | - if (self::$_canCompressOutput){ |
|
395 | + if (self::$_canCompressOutput) { |
|
396 | 396 | ob_start('ob_gzhandler'); |
397 | 397 | } |
398 | - else{ |
|
398 | + else { |
|
399 | 399 | ob_start(); |
400 | 400 | } |
401 | 401 | require_once $path; |
@@ -403,8 +403,8 @@ discard block |
||
403 | 403 | self::sendHeaders(404); |
404 | 404 | echo $output; |
405 | 405 | } |
406 | - else{ |
|
407 | - show_error('The 404 view [' .$path. '] does not exist'); |
|
406 | + else { |
|
407 | + show_error('The 404 view [' . $path . '] does not exist'); |
|
408 | 408 | } |
409 | 409 | } |
410 | 410 | |
@@ -412,14 +412,14 @@ discard block |
||
412 | 412 | * Display the error to user |
413 | 413 | * @param array $data the error information |
414 | 414 | */ |
415 | - public static function sendError(array $data = array()){ |
|
415 | + public static function sendError(array $data = array()) { |
|
416 | 416 | $path = CORE_VIEWS_PATH . 'errors.php'; |
417 | - if(file_exists($path)){ |
|
417 | + if (file_exists($path)) { |
|
418 | 418 | //compress the output if exists |
419 | - if (self::$_canCompressOutput){ |
|
419 | + if (self::$_canCompressOutput) { |
|
420 | 420 | ob_start('ob_gzhandler'); |
421 | 421 | } |
422 | - else{ |
|
422 | + else { |
|
423 | 423 | ob_start(); |
424 | 424 | } |
425 | 425 | extract($data); |
@@ -428,7 +428,7 @@ discard block |
||
428 | 428 | self::sendHeaders(503); |
429 | 429 | echo $output; |
430 | 430 | } |
431 | - else{ |
|
431 | + else { |
|
432 | 432 | //can't use show_error() at this time because some dependencies not yet loaded and to prevent loop |
433 | 433 | set_http_status_header(503); |
434 | 434 | echo 'The error view [' . $path . '] does not exist'; |
@@ -152,8 +152,7 @@ discard block |
||
152 | 152 | if(! headers_sent()){ |
153 | 153 | header('Location: '.$url); |
154 | 154 | exit; |
155 | - } |
|
156 | - else{ |
|
155 | + } else{ |
|
157 | 156 | echo '<script> |
158 | 157 | location.href = "'.$url.'"; |
159 | 158 | </script>'; |
@@ -202,12 +201,10 @@ discard block |
||
202 | 201 | if($moduleViewPath){ |
203 | 202 | $path = $moduleViewPath; |
204 | 203 | $logger->info('Found view [' . $view . '] in module [' .$mod. '], the file path is [' .$moduleViewPath. '] we will used it'); |
205 | - } |
|
206 | - else{ |
|
204 | + } else{ |
|
207 | 205 | $logger->info('Cannot find view [' . $view . '] in module [' .$mod. '] using the default location'); |
208 | 206 | } |
209 | - } |
|
210 | - else{ |
|
207 | + } else{ |
|
211 | 208 | $logger->info('The current request does not use module using the default location.'); |
212 | 209 | } |
213 | 210 | } |
@@ -291,8 +288,7 @@ discard block |
||
291 | 288 | //compress the output if is available |
292 | 289 | if (self::$_canCompressOutput){ |
293 | 290 | ob_start('ob_gzhandler'); |
294 | - } |
|
295 | - else{ |
|
291 | + } else{ |
|
296 | 292 | ob_start(); |
297 | 293 | } |
298 | 294 | self::sendHeaders(200); |
@@ -324,8 +320,7 @@ discard block |
||
324 | 320 | $logger->info('The cache page content is not yet expire for the URL [' . $url . '] send 304 header to browser'); |
325 | 321 | self::sendHeaders(304); |
326 | 322 | return; |
327 | - } |
|
328 | - else{ |
|
323 | + } else{ |
|
329 | 324 | $logger->info('The cache page content is expired or the browser don\'t send the HTTP_IF_MODIFIED_SINCE header for the URL [' . $url . '] send cache headers to tell the browser'); |
330 | 325 | self::sendHeaders(200); |
331 | 326 | //get the cache content |
@@ -345,15 +340,13 @@ discard block |
||
345 | 340 | //compress the output if is available |
346 | 341 | if (self::$_canCompressOutput){ |
347 | 342 | ob_start('ob_gzhandler'); |
348 | - } |
|
349 | - else{ |
|
343 | + } else{ |
|
350 | 344 | ob_start(); |
351 | 345 | } |
352 | 346 | echo $content; |
353 | 347 | ob_end_flush(); |
354 | 348 | return; |
355 | - } |
|
356 | - else{ |
|
349 | + } else{ |
|
357 | 350 | $logger->info('The page cache content for the URL [' . $url . '] is not valid may be already expired'); |
358 | 351 | $cache->delete($pageCacheKey); |
359 | 352 | } |
@@ -394,16 +387,14 @@ discard block |
||
394 | 387 | //compress the output if is available |
395 | 388 | if (self::$_canCompressOutput){ |
396 | 389 | ob_start('ob_gzhandler'); |
397 | - } |
|
398 | - else{ |
|
390 | + } else{ |
|
399 | 391 | ob_start(); |
400 | 392 | } |
401 | 393 | require_once $path; |
402 | 394 | $output = ob_get_clean(); |
403 | 395 | self::sendHeaders(404); |
404 | 396 | echo $output; |
405 | - } |
|
406 | - else{ |
|
397 | + } else{ |
|
407 | 398 | show_error('The 404 view [' .$path. '] does not exist'); |
408 | 399 | } |
409 | 400 | } |
@@ -418,8 +409,7 @@ discard block |
||
418 | 409 | //compress the output if exists |
419 | 410 | if (self::$_canCompressOutput){ |
420 | 411 | ob_start('ob_gzhandler'); |
421 | - } |
|
422 | - else{ |
|
412 | + } else{ |
|
423 | 413 | ob_start(); |
424 | 414 | } |
425 | 415 | extract($data); |
@@ -427,8 +417,7 @@ discard block |
||
427 | 417 | $output = ob_get_clean(); |
428 | 418 | self::sendHeaders(503); |
429 | 419 | echo $output; |
430 | - } |
|
431 | - else{ |
|
420 | + } else{ |
|
432 | 421 | //can't use show_error() at this time because some dependencies not yet loaded and to prevent loop |
433 | 422 | set_http_status_header(503); |
434 | 423 | echo 'The error view [' . $path . '] does not exist'; |
@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | * along with this program; if not, write to the Free Software |
24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | 25 | */ |
26 | - class Loader{ |
|
26 | + class Loader { |
|
27 | 27 | |
28 | 28 | /** |
29 | 29 | * List of loaded resources |
@@ -38,77 +38,77 @@ discard block |
||
38 | 38 | private static $logger; |
39 | 39 | |
40 | 40 | |
41 | - public function __construct(){ |
|
41 | + public function __construct() { |
|
42 | 42 | //add the resources already loaded during application bootstrap |
43 | 43 | //in the list to prevent duplicate or loading the resources again. |
44 | 44 | static::$loaded = class_loaded(); |
45 | 45 | |
46 | 46 | $autoloads = array(); |
47 | 47 | //loading of the resources in autoload.php configuration file |
48 | - if(file_exists(CONFIG_PATH . 'autoload.php')){ |
|
48 | + if (file_exists(CONFIG_PATH . 'autoload.php')) { |
|
49 | 49 | require_once CONFIG_PATH . 'autoload.php'; |
50 | - if(! empty($autoload) && is_array($autoload)){ |
|
50 | + if (!empty($autoload) && is_array($autoload)) { |
|
51 | 51 | $autoloads = $autoload; |
52 | 52 | unset($autoload); |
53 | 53 | } |
54 | 54 | } |
55 | 55 | //loading autoload configuration for modules |
56 | 56 | $modulesAutoloads = Module::getModulesAutoloadConfig(); |
57 | - if(! empty($modulesAutoloads) && is_array($modulesAutoloads)){ |
|
57 | + if (!empty($modulesAutoloads) && is_array($modulesAutoloads)) { |
|
58 | 58 | //libraries autoload |
59 | - if(! empty($modulesAutoloads['libraries']) && is_array($modulesAutoloads['libraries'])){ |
|
59 | + if (!empty($modulesAutoloads['libraries']) && is_array($modulesAutoloads['libraries'])) { |
|
60 | 60 | $autoloads['libraries'] = array_merge($autoloads['libraries'], $modulesAutoloads['libraries']); |
61 | 61 | } |
62 | 62 | //config autoload |
63 | - if(! empty($modulesAutoloads['config']) && is_array($modulesAutoloads['config'])){ |
|
63 | + if (!empty($modulesAutoloads['config']) && is_array($modulesAutoloads['config'])) { |
|
64 | 64 | $autoloads['config'] = array_merge($autoloads['config'], $modulesAutoloads['config']); |
65 | 65 | } |
66 | 66 | //models autoload |
67 | - if(! empty($modulesAutoloads['models']) && is_array($modulesAutoloads['models'])){ |
|
67 | + if (!empty($modulesAutoloads['models']) && is_array($modulesAutoloads['models'])) { |
|
68 | 68 | $autoloads['models'] = array_merge($autoloads['models'], $modulesAutoloads['models']); |
69 | 69 | } |
70 | 70 | //functions autoload |
71 | - if(! empty($modulesAutoloads['functions']) && is_array($modulesAutoloads['functions'])){ |
|
71 | + if (!empty($modulesAutoloads['functions']) && is_array($modulesAutoloads['functions'])) { |
|
72 | 72 | $autoloads['functions'] = array_merge($autoloads['functions'], $modulesAutoloads['functions']); |
73 | 73 | } |
74 | 74 | //languages autoload |
75 | - if(! empty($modulesAutoloads['languages']) && is_array($modulesAutoloads['languages'])){ |
|
75 | + if (!empty($modulesAutoloads['languages']) && is_array($modulesAutoloads['languages'])) { |
|
76 | 76 | $autoloads['languages'] = array_merge($autoloads['languages'], $modulesAutoloads['languages']); |
77 | 77 | } |
78 | 78 | } |
79 | 79 | |
80 | 80 | //config autoload |
81 | - if(! empty($autoloads['config']) && is_array($autoloads['config'])){ |
|
82 | - foreach($autoloads['config'] as $c){ |
|
81 | + if (!empty($autoloads['config']) && is_array($autoloads['config'])) { |
|
82 | + foreach ($autoloads['config'] as $c) { |
|
83 | 83 | $this->config($c); |
84 | 84 | } |
85 | 85 | } |
86 | 86 | |
87 | 87 | //languages autoload |
88 | - if(! empty($autoloads['languages']) && is_array($autoloads['languages'])){ |
|
89 | - foreach($autoloads['languages'] as $language){ |
|
88 | + if (!empty($autoloads['languages']) && is_array($autoloads['languages'])) { |
|
89 | + foreach ($autoloads['languages'] as $language) { |
|
90 | 90 | $this->lang($language); |
91 | 91 | } |
92 | 92 | } |
93 | 93 | |
94 | 94 | //libraries autoload |
95 | - if(! empty($autoloads['libraries']) && is_array($autoloads['libraries'])){ |
|
96 | - foreach($autoloads['libraries'] as $library){ |
|
95 | + if (!empty($autoloads['libraries']) && is_array($autoloads['libraries'])) { |
|
96 | + foreach ($autoloads['libraries'] as $library) { |
|
97 | 97 | $this->library($library); |
98 | 98 | } |
99 | 99 | } |
100 | 100 | |
101 | 101 | //models autoload |
102 | - if(! empty($autoloads['models']) && is_array($autoloads['models'])){ |
|
102 | + if (!empty($autoloads['models']) && is_array($autoloads['models'])) { |
|
103 | 103 | require_once CORE_CLASSES_MODEL_PATH . 'Model.php'; |
104 | - foreach($autoloads['models'] as $model){ |
|
104 | + foreach ($autoloads['models'] as $model) { |
|
105 | 105 | $this->model($model); |
106 | 106 | } |
107 | 107 | } |
108 | 108 | |
109 | 109 | //functions autoload |
110 | - if(! empty($autoloads['functions']) && is_array($autoloads['functions'])){ |
|
111 | - foreach($autoloads['functions'] as $function){ |
|
110 | + if (!empty($autoloads['functions']) && is_array($autoloads['functions'])) { |
|
111 | + foreach ($autoloads['functions'] as $function) { |
|
112 | 112 | $this->functions($function); |
113 | 113 | } |
114 | 114 | } |
@@ -119,9 +119,9 @@ discard block |
||
119 | 119 | * Get the logger singleton instance |
120 | 120 | * @return Log the logger instance |
121 | 121 | */ |
122 | - private static function getLogger(){ |
|
123 | - if(self::$logger == null){ |
|
124 | - self::$logger[0] =& class_loader('Log', 'classes'); |
|
122 | + private static function getLogger() { |
|
123 | + if (self::$logger == null) { |
|
124 | + self::$logger[0] = & class_loader('Log', 'classes'); |
|
125 | 125 | self::$logger[0]->setLogger('Library::Loader'); |
126 | 126 | } |
127 | 127 | return self::$logger[0]; |
@@ -135,25 +135,25 @@ discard block |
||
135 | 135 | * |
136 | 136 | * @return void |
137 | 137 | */ |
138 | - public static function model($class, $instance = null){ |
|
138 | + public static function model($class, $instance = null) { |
|
139 | 139 | $logger = static::getLogger(); |
140 | 140 | $class = str_ireplace('.php', '', $class); |
141 | 141 | $class = trim($class, '/\\'); |
142 | - $file = ucfirst($class).'.php'; |
|
142 | + $file = ucfirst($class) . '.php'; |
|
143 | 143 | $logger->debug('Loading model [' . $class . '] ...'); |
144 | - if(! $instance){ |
|
144 | + if (!$instance) { |
|
145 | 145 | //for module |
146 | - if(strpos($class, '/') !== false){ |
|
146 | + if (strpos($class, '/') !== false) { |
|
147 | 147 | $path = explode('/', $class); |
148 | - if(isset($path[1])){ |
|
148 | + if (isset($path[1])) { |
|
149 | 149 | $instance = strtolower($path[1]); |
150 | 150 | } |
151 | 151 | } |
152 | - else{ |
|
152 | + else { |
|
153 | 153 | $instance = strtolower($class); |
154 | 154 | } |
155 | 155 | } |
156 | - if(isset(static::$loaded[$instance])){ |
|
156 | + if (isset(static::$loaded[$instance])) { |
|
157 | 157 | $logger->info('Model [' . $class . '] already loaded no need to load it again, cost in performance'); |
158 | 158 | return; |
159 | 159 | } |
@@ -163,43 +163,43 @@ discard block |
||
163 | 163 | $searchModuleName = null; |
164 | 164 | $obj = & get_instance(); |
165 | 165 | //check if the request class contains module name |
166 | - if(strpos($class, '/') !== false){ |
|
166 | + if (strpos($class, '/') !== false) { |
|
167 | 167 | $path = explode('/', $class); |
168 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
168 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
169 | 169 | $searchModuleName = $path[0]; |
170 | 170 | $class = ucfirst($path[1]); |
171 | 171 | } |
172 | 172 | } |
173 | - else{ |
|
173 | + else { |
|
174 | 174 | $class = ucfirst($class); |
175 | 175 | } |
176 | 176 | |
177 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
177 | + if (!$searchModuleName && !empty($obj->moduleName)) { |
|
178 | 178 | $searchModuleName = $obj->moduleName; |
179 | 179 | } |
180 | 180 | $moduleModelFilePath = Module::findModelFullPath($class, $searchModuleName); |
181 | - if($moduleModelFilePath){ |
|
182 | - $logger->info('Found model [' . $class . '] from module [' .$searchModuleName. '], the file path is [' .$moduleModelFilePath. '] we will used it'); |
|
181 | + if ($moduleModelFilePath) { |
|
182 | + $logger->info('Found model [' . $class . '] from module [' . $searchModuleName . '], the file path is [' . $moduleModelFilePath . '] we will used it'); |
|
183 | 183 | $classFilePath = $moduleModelFilePath; |
184 | 184 | } |
185 | - else{ |
|
185 | + else { |
|
186 | 186 | $logger->info('Cannot find model [' . $class . '] from modules using the default location'); |
187 | 187 | } |
188 | 188 | $logger->info('The model file path to be loaded is [' . $classFilePath . ']'); |
189 | - if(file_exists($classFilePath)){ |
|
189 | + if (file_exists($classFilePath)) { |
|
190 | 190 | require_once $classFilePath; |
191 | - if(class_exists($class)){ |
|
191 | + if (class_exists($class)) { |
|
192 | 192 | $c = new $class(); |
193 | 193 | $obj = & get_instance(); |
194 | 194 | $obj->{$instance} = $c; |
195 | 195 | static::$loaded[$instance] = $class; |
196 | 196 | $logger->info('Model [' . $class . '] --> ' . $classFilePath . ' loaded successfully.'); |
197 | 197 | } |
198 | - else{ |
|
199 | - show_error('The file '.$classFilePath.' exists but does not contain the class ['. $class . ']'); |
|
198 | + else { |
|
199 | + show_error('The file ' . $classFilePath . ' exists but does not contain the class [' . $class . ']'); |
|
200 | 200 | } |
201 | 201 | } |
202 | - else{ |
|
202 | + else { |
|
203 | 203 | show_error('Unable to find the model [' . $class . ']'); |
204 | 204 | } |
205 | 205 | } |
@@ -214,31 +214,31 @@ discard block |
||
214 | 214 | * |
215 | 215 | * @return void |
216 | 216 | */ |
217 | - public static function library($class, $instance = null, array $params = array()){ |
|
217 | + public static function library($class, $instance = null, array $params = array()) { |
|
218 | 218 | $logger = static::getLogger(); |
219 | 219 | $class = str_ireplace('.php', '', $class); |
220 | 220 | $class = trim($class, '/\\'); |
221 | - $file = ucfirst($class) .'.php'; |
|
221 | + $file = ucfirst($class) . '.php'; |
|
222 | 222 | $logger->debug('Loading library [' . $class . '] ...'); |
223 | - if(! $instance){ |
|
223 | + if (!$instance) { |
|
224 | 224 | //for module |
225 | - if(strpos($class, '/') !== false){ |
|
225 | + if (strpos($class, '/') !== false) { |
|
226 | 226 | $path = explode('/', $class); |
227 | - if(isset($path[1])){ |
|
227 | + if (isset($path[1])) { |
|
228 | 228 | $instance = strtolower($path[1]); |
229 | 229 | } |
230 | 230 | } |
231 | - else{ |
|
231 | + else { |
|
232 | 232 | $instance = strtolower($class); |
233 | 233 | } |
234 | 234 | } |
235 | - if(isset(static::$loaded[$instance])){ |
|
235 | + if (isset(static::$loaded[$instance])) { |
|
236 | 236 | $logger->info('Library [' . $class . '] already loaded no need to load it again, cost in performance'); |
237 | 237 | return; |
238 | 238 | } |
239 | 239 | $obj = & get_instance(); |
240 | 240 | //TODO for Database library |
241 | - if(strtolower($class) == 'database'){ |
|
241 | + if (strtolower($class) == 'database') { |
|
242 | 242 | $logger->info('This is the Database library ...'); |
243 | 243 | $dbInstance = & class_loader('Database', 'classes', $params); |
244 | 244 | $obj->{$instance} = $dbInstance; |
@@ -248,44 +248,44 @@ discard block |
||
248 | 248 | } |
249 | 249 | $libraryFilePath = null; |
250 | 250 | $logger->debug('Check if this is a system library ...'); |
251 | - if(file_exists(CORE_LIBRARY_PATH . $file)){ |
|
251 | + if (file_exists(CORE_LIBRARY_PATH . $file)) { |
|
252 | 252 | $libraryFilePath = CORE_LIBRARY_PATH . $file; |
253 | 253 | $class = ucfirst($class); |
254 | 254 | $logger->info('This library is a system library'); |
255 | 255 | } |
256 | - else{ |
|
256 | + else { |
|
257 | 257 | $logger->info('This library is not a system library'); |
258 | 258 | //first check if this library is in the module |
259 | 259 | $logger->debug('Checking library [' . $class . '] from module list ...'); |
260 | 260 | $searchModuleName = null; |
261 | 261 | //check if the request class contains module name |
262 | - if(strpos($class, '/') !== false){ |
|
262 | + if (strpos($class, '/') !== false) { |
|
263 | 263 | $path = explode('/', $class); |
264 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
264 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
265 | 265 | $searchModuleName = $path[0]; |
266 | 266 | $class = ucfirst($path[1]); |
267 | 267 | } |
268 | 268 | } |
269 | - else{ |
|
269 | + else { |
|
270 | 270 | $class = ucfirst($class); |
271 | 271 | } |
272 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
272 | + if (!$searchModuleName && !empty($obj->moduleName)) { |
|
273 | 273 | $searchModuleName = $obj->moduleName; |
274 | 274 | } |
275 | 275 | $moduleLibraryPath = Module::findLibraryFullPath($class, $searchModuleName); |
276 | - if($moduleLibraryPath){ |
|
277 | - $logger->info('Found library [' . $class . '] from module [' .$searchModuleName. '], the file path is [' .$moduleLibraryPath. '] we will used it'); |
|
276 | + if ($moduleLibraryPath) { |
|
277 | + $logger->info('Found library [' . $class . '] from module [' . $searchModuleName . '], the file path is [' . $moduleLibraryPath . '] we will used it'); |
|
278 | 278 | $libraryFilePath = $moduleLibraryPath; |
279 | 279 | } |
280 | - else{ |
|
280 | + else { |
|
281 | 281 | $logger->info('Cannot find library [' . $class . '] from modules using the default location'); |
282 | 282 | } |
283 | 283 | } |
284 | - if(! $libraryFilePath){ |
|
284 | + if (!$libraryFilePath) { |
|
285 | 285 | $searchDir = array(LIBRARY_PATH); |
286 | - foreach($searchDir as $dir){ |
|
286 | + foreach ($searchDir as $dir) { |
|
287 | 287 | $filePath = $dir . $file; |
288 | - if(file_exists($filePath)){ |
|
288 | + if (file_exists($filePath)) { |
|
289 | 289 | $libraryFilePath = $filePath; |
290 | 290 | //is already found not to continue |
291 | 291 | break; |
@@ -293,20 +293,20 @@ discard block |
||
293 | 293 | } |
294 | 294 | } |
295 | 295 | $logger->info('The library file path to be loaded is [' . $libraryFilePath . ']'); |
296 | - if($libraryFilePath){ |
|
296 | + if ($libraryFilePath) { |
|
297 | 297 | require_once $libraryFilePath; |
298 | - if(class_exists($class)){ |
|
298 | + if (class_exists($class)) { |
|
299 | 299 | $c = $params ? new $class($params) : new $class(); |
300 | 300 | $obj = & get_instance(); |
301 | 301 | $obj->{$instance} = $c; |
302 | 302 | static::$loaded[$instance] = $class; |
303 | 303 | $logger->info('Library [' . $class . '] --> ' . $libraryFilePath . ' loaded successfully.'); |
304 | 304 | } |
305 | - else{ |
|
306 | - show_error('The file '.$libraryFilePath.' exists but does not contain the class '.$class); |
|
305 | + else { |
|
306 | + show_error('The file ' . $libraryFilePath . ' exists but does not contain the class ' . $class); |
|
307 | 307 | } |
308 | 308 | } |
309 | - else{ |
|
309 | + else { |
|
310 | 310 | show_error('Unable to find library class [' . $class . ']'); |
311 | 311 | } |
312 | 312 | } |
@@ -318,14 +318,14 @@ discard block |
||
318 | 318 | * |
319 | 319 | * @return void |
320 | 320 | */ |
321 | - public static function functions($function){ |
|
321 | + public static function functions($function) { |
|
322 | 322 | $logger = static::getLogger(); |
323 | 323 | $function = str_ireplace('.php', '', $function); |
324 | 324 | $function = trim($function, '/\\'); |
325 | 325 | $function = str_ireplace('function_', '', $function); |
326 | - $file = 'function_'.$function.'.php'; |
|
326 | + $file = 'function_' . $function . '.php'; |
|
327 | 327 | $logger->debug('Loading helper [' . $function . '] ...'); |
328 | - if(isset(static::$loaded['function_' . $function])){ |
|
328 | + if (isset(static::$loaded['function_' . $function])) { |
|
329 | 329 | $logger->info('Helper [' . $function . '] already loaded no need to load it again, cost in performance'); |
330 | 330 | return; |
331 | 331 | } |
@@ -335,30 +335,30 @@ discard block |
||
335 | 335 | $searchModuleName = null; |
336 | 336 | $obj = & get_instance(); |
337 | 337 | //check if the request class contains module name |
338 | - if(strpos($function, '/') !== false){ |
|
338 | + if (strpos($function, '/') !== false) { |
|
339 | 339 | $path = explode('/', $function); |
340 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
340 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
341 | 341 | $searchModuleName = $path[0]; |
342 | 342 | $function = 'function_' . $path[1] . '.php'; |
343 | - $file = $path[0] . DS . 'function_'.$function.'.php'; |
|
343 | + $file = $path[0] . DS . 'function_' . $function . '.php'; |
|
344 | 344 | } |
345 | 345 | } |
346 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
346 | + if (!$searchModuleName && !empty($obj->moduleName)) { |
|
347 | 347 | $searchModuleName = $obj->moduleName; |
348 | 348 | } |
349 | 349 | $moduleFunctionPath = Module::findFunctionFullPath($function, $searchModuleName); |
350 | - if($moduleFunctionPath){ |
|
351 | - $logger->info('Found helper [' . $function . '] from module [' .$searchModuleName. '], the file path is [' .$moduleFunctionPath. '] we will used it'); |
|
350 | + if ($moduleFunctionPath) { |
|
351 | + $logger->info('Found helper [' . $function . '] from module [' . $searchModuleName . '], the file path is [' . $moduleFunctionPath . '] we will used it'); |
|
352 | 352 | $functionFilePath = $moduleFunctionPath; |
353 | 353 | } |
354 | - else{ |
|
354 | + else { |
|
355 | 355 | $logger->info('Cannot find helper [' . $function . '] from modules using the default location'); |
356 | 356 | } |
357 | - if(! $functionFilePath){ |
|
357 | + if (!$functionFilePath) { |
|
358 | 358 | $searchDir = array(FUNCTIONS_PATH, CORE_FUNCTIONS_PATH); |
359 | - foreach($searchDir as $dir){ |
|
359 | + foreach ($searchDir as $dir) { |
|
360 | 360 | $filePath = $dir . $file; |
361 | - if(file_exists($filePath)){ |
|
361 | + if (file_exists($filePath)) { |
|
362 | 362 | $functionFilePath = $filePath; |
363 | 363 | //is already found not to continue |
364 | 364 | break; |
@@ -366,12 +366,12 @@ discard block |
||
366 | 366 | } |
367 | 367 | } |
368 | 368 | $logger->info('The helper file path to be loaded is [' . $functionFilePath . ']'); |
369 | - if($functionFilePath){ |
|
369 | + if ($functionFilePath) { |
|
370 | 370 | require_once $functionFilePath; |
371 | 371 | static::$loaded['function_' . $function] = $functionFilePath; |
372 | 372 | $logger->info('Helper [' . $function . '] --> ' . $functionFilePath . ' loaded successfully.'); |
373 | 373 | } |
374 | - else{ |
|
374 | + else { |
|
375 | 375 | show_error('Unable to find helper file [' . $file . ']'); |
376 | 376 | } |
377 | 377 | } |
@@ -383,14 +383,14 @@ discard block |
||
383 | 383 | * |
384 | 384 | * @return void |
385 | 385 | */ |
386 | - public static function config($filename){ |
|
386 | + public static function config($filename) { |
|
387 | 387 | $logger = static::getLogger(); |
388 | 388 | $filename = str_ireplace('.php', '', $filename); |
389 | 389 | $filename = trim($filename, '/\\'); |
390 | 390 | $filename = str_ireplace('config_', '', $filename); |
391 | - $file = 'config_'.$filename.'.php'; |
|
391 | + $file = 'config_' . $filename . '.php'; |
|
392 | 392 | $logger->debug('Loading configuration [' . $filename . '] ...'); |
393 | - if(isset(static::$loaded['config_' . $filename])){ |
|
393 | + if (isset(static::$loaded['config_' . $filename])) { |
|
394 | 394 | $logger->info('Configuration [' . $file . '] already loaded no need to load it again, cost in performance'); |
395 | 395 | return; |
396 | 396 | } |
@@ -400,33 +400,33 @@ discard block |
||
400 | 400 | $searchModuleName = null; |
401 | 401 | $obj = & get_instance(); |
402 | 402 | //check if the request class contains module name |
403 | - if(strpos($filename, '/') !== false){ |
|
403 | + if (strpos($filename, '/') !== false) { |
|
404 | 404 | $path = explode('/', $filename); |
405 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
405 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
406 | 406 | $searchModuleName = $path[0]; |
407 | 407 | $filename = $path[1] . '.php'; |
408 | 408 | } |
409 | 409 | } |
410 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
410 | + if (!$searchModuleName && !empty($obj->moduleName)) { |
|
411 | 411 | $searchModuleName = $obj->moduleName; |
412 | 412 | } |
413 | 413 | $moduleConfigPath = Module::findConfigFullPath($filename, $searchModuleName); |
414 | - if($moduleConfigPath){ |
|
415 | - $logger->info('Found config [' . $filename . '] from module [' .$searchModuleName. '], the file path is [' .$moduleConfigPath. '] we will used it'); |
|
414 | + if ($moduleConfigPath) { |
|
415 | + $logger->info('Found config [' . $filename . '] from module [' . $searchModuleName . '], the file path is [' . $moduleConfigPath . '] we will used it'); |
|
416 | 416 | $configFilePath = $moduleConfigPath; |
417 | 417 | } |
418 | - else{ |
|
418 | + else { |
|
419 | 419 | $logger->info('Cannot find config [' . $filename . '] from modules using the default location'); |
420 | 420 | } |
421 | 421 | $logger->info('The config file path to be loaded is [' . $configFilePath . ']'); |
422 | - if(file_exists($configFilePath)){ |
|
422 | + if (file_exists($configFilePath)) { |
|
423 | 423 | require_once $configFilePath; |
424 | - if(! empty($config) && is_array($config)){ |
|
424 | + if (!empty($config) && is_array($config)) { |
|
425 | 425 | Config::setAll($config); |
426 | 426 | } |
427 | 427 | } |
428 | - else{ |
|
429 | - show_error('Unable to find config file ['. $configFilePath . ']'); |
|
428 | + else { |
|
429 | + show_error('Unable to find config file [' . $configFilePath . ']'); |
|
430 | 430 | } |
431 | 431 | static::$loaded['config_' . $filename] = $configFilePath; |
432 | 432 | $logger->info('Configuration [' . $configFilePath . '] loaded successfully.'); |
@@ -442,14 +442,14 @@ discard block |
||
442 | 442 | * |
443 | 443 | * @return void |
444 | 444 | */ |
445 | - public static function lang($language){ |
|
445 | + public static function lang($language) { |
|
446 | 446 | $logger = static::getLogger(); |
447 | 447 | $language = str_ireplace('.php', '', $language); |
448 | 448 | $language = trim($language, '/\\'); |
449 | 449 | $language = str_ireplace('lang_', '', $language); |
450 | - $file = 'lang_'.$language.'.php'; |
|
450 | + $file = 'lang_' . $language . '.php'; |
|
451 | 451 | $logger->debug('Loading language [' . $language . '] ...'); |
452 | - if(isset(static::$loaded['lang_' . $language])){ |
|
452 | + if (isset(static::$loaded['lang_' . $language])) { |
|
453 | 453 | $logger->info('Language [' . $language . '] already loaded no need to load it again, cost in performance'); |
454 | 454 | return; |
455 | 455 | } |
@@ -459,7 +459,7 @@ discard block |
||
459 | 459 | $cfgKey = get_config('language_cookie_name'); |
460 | 460 | $objCookie = & class_loader('Cookie'); |
461 | 461 | $cookieLang = $objCookie->get($cfgKey); |
462 | - if($cookieLang){ |
|
462 | + if ($cookieLang) { |
|
463 | 463 | $appLang = $cookieLang; |
464 | 464 | } |
465 | 465 | $languageFilePath = null; |
@@ -468,30 +468,30 @@ discard block |
||
468 | 468 | $searchModuleName = null; |
469 | 469 | $obj = & get_instance(); |
470 | 470 | //check if the request class contains module name |
471 | - if(strpos($language, '/') !== false){ |
|
471 | + if (strpos($language, '/') !== false) { |
|
472 | 472 | $path = explode('/', $language); |
473 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
473 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
474 | 474 | $searchModuleName = $path[0]; |
475 | 475 | $language = 'lang_' . $path[1] . '.php'; |
476 | - $file = $path[0] . DS .$language; |
|
476 | + $file = $path[0] . DS . $language; |
|
477 | 477 | } |
478 | 478 | } |
479 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
479 | + if (!$searchModuleName && !empty($obj->moduleName)) { |
|
480 | 480 | $searchModuleName = $obj->moduleName; |
481 | 481 | } |
482 | 482 | $moduleLanguagePath = Module::findLanguageFullPath($language, $searchModuleName, $appLang); |
483 | - if($moduleLanguagePath){ |
|
484 | - $logger->info('Found language [' . $language . '] from module [' .$searchModuleName. '], the file path is [' .$moduleLanguagePath. '] we will used it'); |
|
483 | + if ($moduleLanguagePath) { |
|
484 | + $logger->info('Found language [' . $language . '] from module [' . $searchModuleName . '], the file path is [' . $moduleLanguagePath . '] we will used it'); |
|
485 | 485 | $languageFilePath = $moduleLanguagePath; |
486 | 486 | } |
487 | - else{ |
|
487 | + else { |
|
488 | 488 | $logger->info('Cannot find language [' . $language . '] from modules using the default location'); |
489 | 489 | } |
490 | - if(! $languageFilePath){ |
|
490 | + if (!$languageFilePath) { |
|
491 | 491 | $searchDir = array(APP_LANG_PATH, CORE_LANG_PATH); |
492 | - foreach($searchDir as $dir){ |
|
492 | + foreach ($searchDir as $dir) { |
|
493 | 493 | $filePath = $dir . $appLang . DS . $file; |
494 | - if(file_exists($filePath)){ |
|
494 | + if (file_exists($filePath)) { |
|
495 | 495 | $languageFilePath = $filePath; |
496 | 496 | //is already found not to continue |
497 | 497 | break; |
@@ -499,12 +499,12 @@ discard block |
||
499 | 499 | } |
500 | 500 | } |
501 | 501 | $logger->info('The language file path to be loaded is [' . $languageFilePath . ']'); |
502 | - if($languageFilePath){ |
|
502 | + if ($languageFilePath) { |
|
503 | 503 | require_once $languageFilePath; |
504 | - if(! empty($lang) && is_array($lang)){ |
|
505 | - $logger->info('Language file [' .$languageFilePath. '] contains the valid languages keys add them to language list'); |
|
504 | + if (!empty($lang) && is_array($lang)) { |
|
505 | + $logger->info('Language file [' . $languageFilePath . '] contains the valid languages keys add them to language list'); |
|
506 | 506 | //Note: may be here the class 'Lang' not yet loaded |
507 | - $langObj =& class_loader('Lang', 'classes'); |
|
507 | + $langObj = & class_loader('Lang', 'classes'); |
|
508 | 508 | $langObj->addLangMessages($lang); |
509 | 509 | //free the memory |
510 | 510 | unset($lang); |
@@ -512,7 +512,7 @@ discard block |
||
512 | 512 | static::$loaded['lang_' . $language] = $languageFilePath; |
513 | 513 | $logger->info('Language [' . $language . '] --> ' . $languageFilePath . ' loaded successfully.'); |
514 | 514 | } |
515 | - else{ |
|
515 | + else { |
|
516 | 516 | show_error('Unable to find language file [' . $file . ']'); |
517 | 517 | } |
518 | 518 | } |
@@ -148,8 +148,7 @@ discard block |
||
148 | 148 | if(isset($path[1])){ |
149 | 149 | $instance = strtolower($path[1]); |
150 | 150 | } |
151 | - } |
|
152 | - else{ |
|
151 | + } else{ |
|
153 | 152 | $instance = strtolower($class); |
154 | 153 | } |
155 | 154 | } |
@@ -169,8 +168,7 @@ discard block |
||
169 | 168 | $searchModuleName = $path[0]; |
170 | 169 | $class = ucfirst($path[1]); |
171 | 170 | } |
172 | - } |
|
173 | - else{ |
|
171 | + } else{ |
|
174 | 172 | $class = ucfirst($class); |
175 | 173 | } |
176 | 174 | |
@@ -181,8 +179,7 @@ discard block |
||
181 | 179 | if($moduleModelFilePath){ |
182 | 180 | $logger->info('Found model [' . $class . '] from module [' .$searchModuleName. '], the file path is [' .$moduleModelFilePath. '] we will used it'); |
183 | 181 | $classFilePath = $moduleModelFilePath; |
184 | - } |
|
185 | - else{ |
|
182 | + } else{ |
|
186 | 183 | $logger->info('Cannot find model [' . $class . '] from modules using the default location'); |
187 | 184 | } |
188 | 185 | $logger->info('The model file path to be loaded is [' . $classFilePath . ']'); |
@@ -194,12 +191,10 @@ discard block |
||
194 | 191 | $obj->{$instance} = $c; |
195 | 192 | static::$loaded[$instance] = $class; |
196 | 193 | $logger->info('Model [' . $class . '] --> ' . $classFilePath . ' loaded successfully.'); |
197 | - } |
|
198 | - else{ |
|
194 | + } else{ |
|
199 | 195 | show_error('The file '.$classFilePath.' exists but does not contain the class ['. $class . ']'); |
200 | 196 | } |
201 | - } |
|
202 | - else{ |
|
197 | + } else{ |
|
203 | 198 | show_error('Unable to find the model [' . $class . ']'); |
204 | 199 | } |
205 | 200 | } |
@@ -227,8 +222,7 @@ discard block |
||
227 | 222 | if(isset($path[1])){ |
228 | 223 | $instance = strtolower($path[1]); |
229 | 224 | } |
230 | - } |
|
231 | - else{ |
|
225 | + } else{ |
|
232 | 226 | $instance = strtolower($class); |
233 | 227 | } |
234 | 228 | } |
@@ -252,8 +246,7 @@ discard block |
||
252 | 246 | $libraryFilePath = CORE_LIBRARY_PATH . $file; |
253 | 247 | $class = ucfirst($class); |
254 | 248 | $logger->info('This library is a system library'); |
255 | - } |
|
256 | - else{ |
|
249 | + } else{ |
|
257 | 250 | $logger->info('This library is not a system library'); |
258 | 251 | //first check if this library is in the module |
259 | 252 | $logger->debug('Checking library [' . $class . '] from module list ...'); |
@@ -265,8 +258,7 @@ discard block |
||
265 | 258 | $searchModuleName = $path[0]; |
266 | 259 | $class = ucfirst($path[1]); |
267 | 260 | } |
268 | - } |
|
269 | - else{ |
|
261 | + } else{ |
|
270 | 262 | $class = ucfirst($class); |
271 | 263 | } |
272 | 264 | if(! $searchModuleName && !empty($obj->moduleName)){ |
@@ -276,8 +268,7 @@ discard block |
||
276 | 268 | if($moduleLibraryPath){ |
277 | 269 | $logger->info('Found library [' . $class . '] from module [' .$searchModuleName. '], the file path is [' .$moduleLibraryPath. '] we will used it'); |
278 | 270 | $libraryFilePath = $moduleLibraryPath; |
279 | - } |
|
280 | - else{ |
|
271 | + } else{ |
|
281 | 272 | $logger->info('Cannot find library [' . $class . '] from modules using the default location'); |
282 | 273 | } |
283 | 274 | } |
@@ -301,12 +292,10 @@ discard block |
||
301 | 292 | $obj->{$instance} = $c; |
302 | 293 | static::$loaded[$instance] = $class; |
303 | 294 | $logger->info('Library [' . $class . '] --> ' . $libraryFilePath . ' loaded successfully.'); |
304 | - } |
|
305 | - else{ |
|
295 | + } else{ |
|
306 | 296 | show_error('The file '.$libraryFilePath.' exists but does not contain the class '.$class); |
307 | 297 | } |
308 | - } |
|
309 | - else{ |
|
298 | + } else{ |
|
310 | 299 | show_error('Unable to find library class [' . $class . ']'); |
311 | 300 | } |
312 | 301 | } |
@@ -350,8 +339,7 @@ discard block |
||
350 | 339 | if($moduleFunctionPath){ |
351 | 340 | $logger->info('Found helper [' . $function . '] from module [' .$searchModuleName. '], the file path is [' .$moduleFunctionPath. '] we will used it'); |
352 | 341 | $functionFilePath = $moduleFunctionPath; |
353 | - } |
|
354 | - else{ |
|
342 | + } else{ |
|
355 | 343 | $logger->info('Cannot find helper [' . $function . '] from modules using the default location'); |
356 | 344 | } |
357 | 345 | if(! $functionFilePath){ |
@@ -370,8 +358,7 @@ discard block |
||
370 | 358 | require_once $functionFilePath; |
371 | 359 | static::$loaded['function_' . $function] = $functionFilePath; |
372 | 360 | $logger->info('Helper [' . $function . '] --> ' . $functionFilePath . ' loaded successfully.'); |
373 | - } |
|
374 | - else{ |
|
361 | + } else{ |
|
375 | 362 | show_error('Unable to find helper file [' . $file . ']'); |
376 | 363 | } |
377 | 364 | } |
@@ -414,8 +401,7 @@ discard block |
||
414 | 401 | if($moduleConfigPath){ |
415 | 402 | $logger->info('Found config [' . $filename . '] from module [' .$searchModuleName. '], the file path is [' .$moduleConfigPath. '] we will used it'); |
416 | 403 | $configFilePath = $moduleConfigPath; |
417 | - } |
|
418 | - else{ |
|
404 | + } else{ |
|
419 | 405 | $logger->info('Cannot find config [' . $filename . '] from modules using the default location'); |
420 | 406 | } |
421 | 407 | $logger->info('The config file path to be loaded is [' . $configFilePath . ']'); |
@@ -424,8 +410,7 @@ discard block |
||
424 | 410 | if(! empty($config) && is_array($config)){ |
425 | 411 | Config::setAll($config); |
426 | 412 | } |
427 | - } |
|
428 | - else{ |
|
413 | + } else{ |
|
429 | 414 | show_error('Unable to find config file ['. $configFilePath . ']'); |
430 | 415 | } |
431 | 416 | static::$loaded['config_' . $filename] = $configFilePath; |
@@ -483,8 +468,7 @@ discard block |
||
483 | 468 | if($moduleLanguagePath){ |
484 | 469 | $logger->info('Found language [' . $language . '] from module [' .$searchModuleName. '], the file path is [' .$moduleLanguagePath. '] we will used it'); |
485 | 470 | $languageFilePath = $moduleLanguagePath; |
486 | - } |
|
487 | - else{ |
|
471 | + } else{ |
|
488 | 472 | $logger->info('Cannot find language [' . $language . '] from modules using the default location'); |
489 | 473 | } |
490 | 474 | if(! $languageFilePath){ |
@@ -511,8 +495,7 @@ discard block |
||
511 | 495 | } |
512 | 496 | static::$loaded['lang_' . $language] = $languageFilePath; |
513 | 497 | $logger->info('Language [' . $language . '] --> ' . $languageFilePath . ' loaded successfully.'); |
514 | - } |
|
515 | - else{ |
|
498 | + } else{ |
|
516 | 499 | show_error('Unable to find language file [' . $file . ']'); |
517 | 500 | } |
518 | 501 | } |
@@ -92,36 +92,36 @@ discard block |
||
92 | 92 | /** |
93 | 93 | * Construct the new Router instance |
94 | 94 | */ |
95 | - public function __construct(){ |
|
96 | - $this->logger =& class_loader('Log', 'classes'); |
|
95 | + public function __construct() { |
|
96 | + $this->logger = & class_loader('Log', 'classes'); |
|
97 | 97 | $this->logger->setLogger('Library::Router'); |
98 | 98 | $routesPath = CONFIG_PATH . 'routes.php'; |
99 | 99 | $this->logger->debug('Loading of routes configuration file --> ' . $routesPath . ' ...'); |
100 | - if(file_exists($routesPath)){ |
|
101 | - $this->logger->info('Found routes configuration file --> ' . $routesPath. ' now load it'); |
|
100 | + if (file_exists($routesPath)) { |
|
101 | + $this->logger->info('Found routes configuration file --> ' . $routesPath . ' now load it'); |
|
102 | 102 | require_once $routesPath; |
103 | - if(! empty($route) && is_array($route)){ |
|
103 | + if (!empty($route) && is_array($route)) { |
|
104 | 104 | $this->routes = $route; |
105 | 105 | unset($route); |
106 | 106 | } |
107 | 107 | } |
108 | - else{ |
|
108 | + else { |
|
109 | 109 | show_error('Unable to find the routes configuration file [' . $routesPath . ']'); |
110 | 110 | } |
111 | 111 | |
112 | 112 | //loading routes for module |
113 | 113 | $this->logger->debug('Loading of modules routes ... '); |
114 | 114 | $modulesRoutes = Module::getModulesRoutes(); |
115 | - if($modulesRoutes && is_array($modulesRoutes)){ |
|
115 | + if ($modulesRoutes && is_array($modulesRoutes)) { |
|
116 | 116 | $this->routes = array_merge($this->routes, $modulesRoutes); |
117 | 117 | $this->logger->info('Routes for all modules loaded successfully'); |
118 | 118 | } |
119 | - else{ |
|
119 | + else { |
|
120 | 120 | $this->logger->info('No routes found for all modules skipping.'); |
121 | 121 | } |
122 | 122 | $this->logger->info('The routes configuration are listed below: ' . stringfy_vars($this->routes)); |
123 | 123 | |
124 | - foreach($this->routes as $pattern => $callback){ |
|
124 | + foreach ($this->routes as $pattern => $callback) { |
|
125 | 125 | $this->add($pattern, $callback); |
126 | 126 | } |
127 | 127 | |
@@ -129,14 +129,14 @@ discard block |
||
129 | 129 | $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; |
130 | 130 | $this->logger->debug('Check if URL suffix is enabled in the configuration'); |
131 | 131 | //remove url suffix from the request URI |
132 | - if($suffix = get_config('url_suffix')){ |
|
133 | - $this->logger->info('URL suffix is enabled in the configuration, the value is [' . $suffix . ']' ); |
|
132 | + if ($suffix = get_config('url_suffix')) { |
|
133 | + $this->logger->info('URL suffix is enabled in the configuration, the value is [' . $suffix . ']'); |
|
134 | 134 | $uri = str_ireplace($suffix, '', $uri); |
135 | 135 | } |
136 | - else{ |
|
136 | + else { |
|
137 | 137 | $this->logger->info('URL suffix is not enabled in the configuration'); |
138 | 138 | } |
139 | - if(strpos($uri, '?') !== false){ |
|
139 | + if (strpos($uri, '?') !== false) { |
|
140 | 140 | $uri = substr($uri, 0, strpos($uri, '?')); |
141 | 141 | } |
142 | 142 | $uri = trim($uri, $this->uriTrim); |
@@ -151,7 +151,7 @@ discard block |
||
151 | 151 | */ |
152 | 152 | public function add($uri, $callback) { |
153 | 153 | $uri = trim($uri, $this->uriTrim); |
154 | - if(in_array($uri, $this->pattern)){ |
|
154 | + if (in_array($uri, $this->pattern)) { |
|
155 | 155 | $this->logger->warning('The route [' . $uri . '] already added, may be adding again can have route conflict'); |
156 | 156 | } |
157 | 157 | $this->pattern[] = $uri; |
@@ -162,7 +162,7 @@ discard block |
||
162 | 162 | * Get the module name |
163 | 163 | * @return string |
164 | 164 | */ |
165 | - public function getModule(){ |
|
165 | + public function getModule() { |
|
166 | 166 | return $this->module; |
167 | 167 | } |
168 | 168 | |
@@ -170,7 +170,7 @@ discard block |
||
170 | 170 | * Get the controller name |
171 | 171 | * @return string |
172 | 172 | */ |
173 | - public function getController(){ |
|
173 | + public function getController() { |
|
174 | 174 | return $this->controller; |
175 | 175 | } |
176 | 176 | |
@@ -178,7 +178,7 @@ discard block |
||
178 | 178 | * Get the controller file path |
179 | 179 | * @return string |
180 | 180 | */ |
181 | - public function getControllerPath(){ |
|
181 | + public function getControllerPath() { |
|
182 | 182 | return $this->controllerPath; |
183 | 183 | } |
184 | 184 | |
@@ -186,7 +186,7 @@ discard block |
||
186 | 186 | * Get the controller method |
187 | 187 | * @return string |
188 | 188 | */ |
189 | - public function getMethod(){ |
|
189 | + public function getMethod() { |
|
190 | 190 | return $this->method; |
191 | 191 | } |
192 | 192 | |
@@ -194,7 +194,7 @@ discard block |
||
194 | 194 | * Get the request arguments |
195 | 195 | * @return array |
196 | 196 | */ |
197 | - public function getArgs(){ |
|
197 | + public function getArgs() { |
|
198 | 198 | return $this->args; |
199 | 199 | } |
200 | 200 | |
@@ -202,7 +202,7 @@ discard block |
||
202 | 202 | * Get the URL segments array |
203 | 203 | * @return array |
204 | 204 | */ |
205 | - public function getSegments(){ |
|
205 | + public function getSegments() { |
|
206 | 206 | return $this->segments; |
207 | 207 | } |
208 | 208 | |
@@ -211,27 +211,27 @@ discard block |
||
211 | 211 | * otherwise send 404 error. |
212 | 212 | */ |
213 | 213 | public function run() { |
214 | - $benchmark =& class_loader('Benchmark'); |
|
214 | + $benchmark = & class_loader('Benchmark'); |
|
215 | 215 | $benchmark->mark('ROUTING_PROCESS_START'); |
216 | 216 | $this->logger->debug('Routing process start ...'); |
217 | 217 | $segment = $this->segments; |
218 | 218 | $baseUrl = get_config('base_url'); |
219 | 219 | //check if the app is not in DOCUMENT_ROOT |
220 | - if(isset($segment[0]) && stripos($baseUrl, $segment[0]) != false){ |
|
220 | + if (isset($segment[0]) && stripos($baseUrl, $segment[0]) != false) { |
|
221 | 221 | array_shift($segment); |
222 | 222 | $this->segments = $segment; |
223 | 223 | } |
224 | 224 | $this->logger->debug('Check if the request URI contains the front controller'); |
225 | - if(isset($segment[0]) && $segment[0] == SELF){ |
|
225 | + if (isset($segment[0]) && $segment[0] == SELF) { |
|
226 | 226 | $this->logger->info('The request URI contains the front controller'); |
227 | 227 | array_shift($segment); |
228 | 228 | $this->segments = $segment; |
229 | 229 | } |
230 | - else{ |
|
230 | + else { |
|
231 | 231 | $this->logger->info('The request URI does not contain the front controller'); |
232 | 232 | } |
233 | 233 | $uri = implode('/', $segment); |
234 | - $this->logger->info('The final Request URI is [' . $uri . ']' ); |
|
234 | + $this->logger->info('The final Request URI is [' . $uri . ']'); |
|
235 | 235 | //generic routes |
236 | 236 | $pattern = array(':num', ':alpha', ':alnum', ':any'); |
237 | 237 | $replace = array('[0-9]+', '[a-zA-Z]+', '[a-zA-Z0-9]+', '.*'); |
@@ -245,20 +245,20 @@ discard block |
||
245 | 245 | array_shift($args); |
246 | 246 | //check if this contains an module |
247 | 247 | $moduleControllerMethod = explode('#', $this->callback[$index]); |
248 | - if(is_array($moduleControllerMethod) && count($moduleControllerMethod) >= 2){ |
|
249 | - $this->logger->info('The current request use the module [' .$moduleControllerMethod[0]. ']'); |
|
248 | + if (is_array($moduleControllerMethod) && count($moduleControllerMethod) >= 2) { |
|
249 | + $this->logger->info('The current request use the module [' . $moduleControllerMethod[0] . ']'); |
|
250 | 250 | $this->module = $moduleControllerMethod[0]; |
251 | 251 | $moduleControllerMethod = explode('@', $moduleControllerMethod[1]); |
252 | 252 | } |
253 | - else{ |
|
253 | + else { |
|
254 | 254 | $this->logger->info('The current request does not use the module'); |
255 | 255 | $moduleControllerMethod = explode('@', $this->callback[$index]); |
256 | 256 | } |
257 | - if(is_array($moduleControllerMethod)){ |
|
258 | - if(isset($moduleControllerMethod[0])){ |
|
257 | + if (is_array($moduleControllerMethod)) { |
|
258 | + if (isset($moduleControllerMethod[0])) { |
|
259 | 259 | $this->controller = $moduleControllerMethod[0]; |
260 | 260 | } |
261 | - if(isset($moduleControllerMethod[1])){ |
|
261 | + if (isset($moduleControllerMethod[1])) { |
|
262 | 262 | $this->method = $moduleControllerMethod[1]; |
263 | 263 | } |
264 | 264 | $this->args = $args; |
@@ -268,73 +268,73 @@ discard block |
||
268 | 268 | } |
269 | 269 | } |
270 | 270 | //first if the controller is not set and the module is set use the module name as the controller |
271 | - if(! $this->getController() && $this->getModule()){ |
|
271 | + if (!$this->getController() && $this->getModule()) { |
|
272 | 272 | $this->logger->info('After loop in predefined routes configuration, the module name is set but the controller is not set, so we will use module as the controller'); |
273 | 273 | $this->controller = $this->getModule(); |
274 | 274 | } |
275 | 275 | //if can not determine the module/controller/method via the defined routes configuration we will use |
276 | 276 | //the URL like http://domain.com/module/controller/method/arg1/arg2 |
277 | - if(! $this->getController()){ |
|
277 | + if (!$this->getController()) { |
|
278 | 278 | $this->logger->info('Cannot determine the routing information using the predefined routes configuration, will use the request URI parameters'); |
279 | 279 | $nbSegment = count($segment); |
280 | 280 | //if segment is null so means no need to perform |
281 | - if($nbSegment > 0){ |
|
281 | + if ($nbSegment > 0) { |
|
282 | 282 | //get the module list |
283 | 283 | $modules = Module::getModuleList(); |
284 | 284 | //first check if no module |
285 | - if(! $modules){ |
|
285 | + if (!$modules) { |
|
286 | 286 | $this->logger->info('No module was loaded will skip the module checking'); |
287 | 287 | //the application don't use module |
288 | 288 | //controller |
289 | - if(isset($segment[0])){ |
|
289 | + if (isset($segment[0])) { |
|
290 | 290 | $this->controller = $segment[0]; |
291 | 291 | array_shift($segment); |
292 | 292 | } |
293 | 293 | //method |
294 | - if(isset($segment[0])){ |
|
294 | + if (isset($segment[0])) { |
|
295 | 295 | $this->method = $segment[0]; |
296 | 296 | array_shift($segment); |
297 | 297 | } |
298 | 298 | //args |
299 | 299 | $this->args = $segment; |
300 | 300 | } |
301 | - else{ |
|
301 | + else { |
|
302 | 302 | $this->logger->info('The application contains a loaded module will check if the current request is found in the module list'); |
303 | - if(in_array($segment[0], $modules)){ |
|
303 | + if (in_array($segment[0], $modules)) { |
|
304 | 304 | $this->logger->info('Found, the current request use the module [' . $segment[0] . ']'); |
305 | 305 | $this->module = $segment[0]; |
306 | 306 | array_shift($segment); |
307 | 307 | //check if the second arg is the controller from module |
308 | - if(isset($segment[0])){ |
|
308 | + if (isset($segment[0])) { |
|
309 | 309 | $this->controller = $segment[0]; |
310 | 310 | //check if the request use the same module name and controller |
311 | 311 | $path = Module::findControllerFullPath(ucfirst($this->getController()), $this->getModule()); |
312 | - if(! $path){ |
|
312 | + if (!$path) { |
|
313 | 313 | $this->logger->info('The controller [' . $this->getController() . '] not found in the module, may be will use the module [' . $this->getModule() . '] as controller'); |
314 | 314 | $this->controller = $this->getModule(); |
315 | 315 | } |
316 | - else{ |
|
316 | + else { |
|
317 | 317 | $this->controllerPath = $path; |
318 | 318 | array_shift($segment); |
319 | 319 | } |
320 | 320 | } |
321 | 321 | //check for method |
322 | - if(isset($segment[0])){ |
|
322 | + if (isset($segment[0])) { |
|
323 | 323 | $this->method = $segment[0]; |
324 | 324 | array_shift($segment); |
325 | 325 | } |
326 | 326 | //the remaining is for args |
327 | 327 | $this->args = $segment; |
328 | 328 | } |
329 | - else{ |
|
329 | + else { |
|
330 | 330 | $this->logger->info('The current request information is not found in the module list'); |
331 | 331 | //controller |
332 | - if(isset($segment[0])){ |
|
332 | + if (isset($segment[0])) { |
|
333 | 333 | $this->controller = $segment[0]; |
334 | 334 | array_shift($segment); |
335 | 335 | } |
336 | 336 | //method |
337 | - if(isset($segment[0])){ |
|
337 | + if (isset($segment[0])) { |
|
338 | 338 | $this->method = $segment[0]; |
339 | 339 | array_shift($segment); |
340 | 340 | } |
@@ -344,18 +344,18 @@ discard block |
||
344 | 344 | } |
345 | 345 | } |
346 | 346 | } |
347 | - if(! $this->getController() && $this->getModule()){ |
|
347 | + if (!$this->getController() && $this->getModule()) { |
|
348 | 348 | $this->logger->info('After using the request URI the module name is set but the controller is not set so we will use module as the controller'); |
349 | 349 | $this->controller = $this->getModule(); |
350 | 350 | } |
351 | 351 | //did we set the controller, so set the controller path |
352 | - if($this->getController() && ! $this->getControllerPath()){ |
|
352 | + if ($this->getController() && !$this->getControllerPath()) { |
|
353 | 353 | $this->logger->debug('Setting the file path for the controller [' . $this->getController() . ']'); |
354 | 354 | //if it is the module controller |
355 | - if($this->getModule()){ |
|
355 | + if ($this->getModule()) { |
|
356 | 356 | $this->controllerPath = Module::findControllerFullPath(ucfirst($this->getController()), $this->getModule()); |
357 | 357 | } |
358 | - else{ |
|
358 | + else { |
|
359 | 359 | $this->controllerPath = APPS_CONTROLLER_PATH . ucfirst($this->getController()) . '.php'; |
360 | 360 | } |
361 | 361 | } |
@@ -365,20 +365,20 @@ discard block |
||
365 | 365 | $this->logger->debug('Loading controller [' . $controller . '], the file path is [' . $classFilePath . ']...'); |
366 | 366 | $benchmark->mark('ROUTING_PROCESS_END'); |
367 | 367 | $e404 = false; |
368 | - if(file_exists($classFilePath)){ |
|
368 | + if (file_exists($classFilePath)) { |
|
369 | 369 | require_once $classFilePath; |
370 | - if(! class_exists($controller, false)){ |
|
370 | + if (!class_exists($controller, false)) { |
|
371 | 371 | $e404 = true; |
372 | - $this->logger->info('The controller file [' .$classFilePath. '] exists but does not contain the class [' . $controller . ']'); |
|
372 | + $this->logger->info('The controller file [' . $classFilePath . '] exists but does not contain the class [' . $controller . ']'); |
|
373 | 373 | } |
374 | - else{ |
|
374 | + else { |
|
375 | 375 | $controllerInstance = new $controller(); |
376 | 376 | $controllerMethod = $this->getMethod(); |
377 | - if(! method_exists($controllerInstance, $controllerMethod)){ |
|
377 | + if (!method_exists($controllerInstance, $controllerMethod)) { |
|
378 | 378 | $e404 = true; |
379 | 379 | $this->logger->info('The controller [' . $controller . '] exist but does not contain the method [' . $controllerMethod . ']'); |
380 | 380 | } |
381 | - else{ |
|
381 | + else { |
|
382 | 382 | $this->logger->info('Routing data is set correctly now GO!'); |
383 | 383 | call_user_func_array(array($controllerInstance, $controllerMethod), $this->getArgs()); |
384 | 384 | $obj = & get_instance(); |
@@ -388,12 +388,12 @@ discard block |
||
388 | 388 | } |
389 | 389 | } |
390 | 390 | } |
391 | - else{ |
|
391 | + else { |
|
392 | 392 | $this->logger->info('The controller file path [' . $classFilePath . '] does not exist'); |
393 | 393 | $e404 = true; |
394 | 394 | } |
395 | - if($e404){ |
|
396 | - $response =& class_loader('Response', 'classes'); |
|
395 | + if ($e404) { |
|
396 | + $response = & class_loader('Response', 'classes'); |
|
397 | 397 | $response->send404(); |
398 | 398 | } |
399 | 399 | } |
@@ -104,8 +104,7 @@ discard block |
||
104 | 104 | $this->routes = $route; |
105 | 105 | unset($route); |
106 | 106 | } |
107 | - } |
|
108 | - else{ |
|
107 | + } else{ |
|
109 | 108 | show_error('Unable to find the routes configuration file [' . $routesPath . ']'); |
110 | 109 | } |
111 | 110 | |
@@ -115,8 +114,7 @@ discard block |
||
115 | 114 | if($modulesRoutes && is_array($modulesRoutes)){ |
116 | 115 | $this->routes = array_merge($this->routes, $modulesRoutes); |
117 | 116 | $this->logger->info('Routes for all modules loaded successfully'); |
118 | - } |
|
119 | - else{ |
|
117 | + } else{ |
|
120 | 118 | $this->logger->info('No routes found for all modules skipping.'); |
121 | 119 | } |
122 | 120 | $this->logger->info('The routes configuration are listed below: ' . stringfy_vars($this->routes)); |
@@ -132,8 +130,7 @@ discard block |
||
132 | 130 | if($suffix = get_config('url_suffix')){ |
133 | 131 | $this->logger->info('URL suffix is enabled in the configuration, the value is [' . $suffix . ']' ); |
134 | 132 | $uri = str_ireplace($suffix, '', $uri); |
135 | - } |
|
136 | - else{ |
|
133 | + } else{ |
|
137 | 134 | $this->logger->info('URL suffix is not enabled in the configuration'); |
138 | 135 | } |
139 | 136 | if(strpos($uri, '?') !== false){ |
@@ -226,8 +223,7 @@ discard block |
||
226 | 223 | $this->logger->info('The request URI contains the front controller'); |
227 | 224 | array_shift($segment); |
228 | 225 | $this->segments = $segment; |
229 | - } |
|
230 | - else{ |
|
226 | + } else{ |
|
231 | 227 | $this->logger->info('The request URI does not contain the front controller'); |
232 | 228 | } |
233 | 229 | $uri = implode('/', $segment); |
@@ -249,8 +245,7 @@ discard block |
||
249 | 245 | $this->logger->info('The current request use the module [' .$moduleControllerMethod[0]. ']'); |
250 | 246 | $this->module = $moduleControllerMethod[0]; |
251 | 247 | $moduleControllerMethod = explode('@', $moduleControllerMethod[1]); |
252 | - } |
|
253 | - else{ |
|
248 | + } else{ |
|
254 | 249 | $this->logger->info('The current request does not use the module'); |
255 | 250 | $moduleControllerMethod = explode('@', $this->callback[$index]); |
256 | 251 | } |
@@ -297,8 +292,7 @@ discard block |
||
297 | 292 | } |
298 | 293 | //args |
299 | 294 | $this->args = $segment; |
300 | - } |
|
301 | - else{ |
|
295 | + } else{ |
|
302 | 296 | $this->logger->info('The application contains a loaded module will check if the current request is found in the module list'); |
303 | 297 | if(in_array($segment[0], $modules)){ |
304 | 298 | $this->logger->info('Found, the current request use the module [' . $segment[0] . ']'); |
@@ -312,8 +306,7 @@ discard block |
||
312 | 306 | if(! $path){ |
313 | 307 | $this->logger->info('The controller [' . $this->getController() . '] not found in the module, may be will use the module [' . $this->getModule() . '] as controller'); |
314 | 308 | $this->controller = $this->getModule(); |
315 | - } |
|
316 | - else{ |
|
309 | + } else{ |
|
317 | 310 | $this->controllerPath = $path; |
318 | 311 | array_shift($segment); |
319 | 312 | } |
@@ -325,8 +318,7 @@ discard block |
||
325 | 318 | } |
326 | 319 | //the remaining is for args |
327 | 320 | $this->args = $segment; |
328 | - } |
|
329 | - else{ |
|
321 | + } else{ |
|
330 | 322 | $this->logger->info('The current request information is not found in the module list'); |
331 | 323 | //controller |
332 | 324 | if(isset($segment[0])){ |
@@ -354,8 +346,7 @@ discard block |
||
354 | 346 | //if it is the module controller |
355 | 347 | if($this->getModule()){ |
356 | 348 | $this->controllerPath = Module::findControllerFullPath(ucfirst($this->getController()), $this->getModule()); |
357 | - } |
|
358 | - else{ |
|
349 | + } else{ |
|
359 | 350 | $this->controllerPath = APPS_CONTROLLER_PATH . ucfirst($this->getController()) . '.php'; |
360 | 351 | } |
361 | 352 | } |
@@ -370,15 +361,13 @@ discard block |
||
370 | 361 | if(! class_exists($controller, false)){ |
371 | 362 | $e404 = true; |
372 | 363 | $this->logger->info('The controller file [' .$classFilePath. '] exists but does not contain the class [' . $controller . ']'); |
373 | - } |
|
374 | - else{ |
|
364 | + } else{ |
|
375 | 365 | $controllerInstance = new $controller(); |
376 | 366 | $controllerMethod = $this->getMethod(); |
377 | 367 | if(! method_exists($controllerInstance, $controllerMethod)){ |
378 | 368 | $e404 = true; |
379 | 369 | $this->logger->info('The controller [' . $controller . '] exist but does not contain the method [' . $controllerMethod . ']'); |
380 | - } |
|
381 | - else{ |
|
370 | + } else{ |
|
382 | 371 | $this->logger->info('Routing data is set correctly now GO!'); |
383 | 372 | call_user_func_array(array($controllerInstance, $controllerMethod), $this->getArgs()); |
384 | 373 | $obj = & get_instance(); |
@@ -387,8 +376,7 @@ discard block |
||
387 | 376 | $obj->response->renderFinalPage(); |
388 | 377 | } |
389 | 378 | } |
390 | - } |
|
391 | - else{ |
|
379 | + } else{ |
|
392 | 380 | $this->logger->info('The controller file path [' . $classFilePath . '] does not exist'); |
393 | 381 | $e404 = true; |
394 | 382 | } |
@@ -24,7 +24,7 @@ discard block |
||
24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | 25 | */ |
26 | 26 | |
27 | - class Log{ |
|
27 | + class Log { |
|
28 | 28 | |
29 | 29 | /** |
30 | 30 | * The defined constante for Log level |
@@ -52,14 +52,14 @@ discard block |
||
52 | 52 | /** |
53 | 53 | * Create new Log instance |
54 | 54 | */ |
55 | - public function __construct(){ |
|
55 | + public function __construct() { |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | /** |
59 | 59 | * Set the logger to identify each message in the log |
60 | 60 | * @param string $logger the logger name |
61 | 61 | */ |
62 | - public function setLogger($logger){ |
|
62 | + public function setLogger($logger) { |
|
63 | 63 | $this->logger = $logger; |
64 | 64 | } |
65 | 65 | |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | * @see Log::writeLog for more detail |
69 | 69 | * @param string $message the log message to save |
70 | 70 | */ |
71 | - public function fatal($message){ |
|
71 | + public function fatal($message) { |
|
72 | 72 | $this->writeLog($message, self::FATAL); |
73 | 73 | } |
74 | 74 | |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | * @see Log::writeLog for more detail |
78 | 78 | * @param string $message the log message to save |
79 | 79 | */ |
80 | - public function error($message){ |
|
80 | + public function error($message) { |
|
81 | 81 | $this->writeLog($message, self::ERROR); |
82 | 82 | } |
83 | 83 | |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | * @see Log::writeLog for more detail |
87 | 87 | * @param string $message the log message to save |
88 | 88 | */ |
89 | - public function warning($message){ |
|
89 | + public function warning($message) { |
|
90 | 90 | $this->writeLog($message, self::WARNING); |
91 | 91 | } |
92 | 92 | |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | * @see Log::writeLog for more detail |
96 | 96 | * @param string $message the log message to save |
97 | 97 | */ |
98 | - public function info($message){ |
|
98 | + public function info($message) { |
|
99 | 99 | $this->writeLog($message, self::INFO); |
100 | 100 | } |
101 | 101 | |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | * @see Log::writeLog for more detail |
105 | 105 | * @param string $message the log message to save |
106 | 106 | */ |
107 | - public function debug($message){ |
|
107 | + public function debug($message) { |
|
108 | 108 | $this->writeLog($message, self::DEBUG); |
109 | 109 | } |
110 | 110 | |
@@ -115,59 +115,59 @@ discard block |
||
115 | 115 | * @param integer|string $level the log level in integer or string format, if is string will convert into integer |
116 | 116 | * to allow check the log level threshold. |
117 | 117 | */ |
118 | - public function writeLog($message, $level = self::INFO){ |
|
118 | + public function writeLog($message, $level = self::INFO) { |
|
119 | 119 | $configLogLevel = get_config('log_level'); |
120 | - if(! $configLogLevel){ |
|
120 | + if (!$configLogLevel) { |
|
121 | 121 | //so means no need log just stop here |
122 | 122 | return; |
123 | 123 | } |
124 | 124 | //check config log level |
125 | - if(! self::isValidConfigLevel($configLogLevel)){ |
|
125 | + if (!self::isValidConfigLevel($configLogLevel)) { |
|
126 | 126 | //NOTE: here need put the show_error() "logging" to false to prevent loop |
127 | 127 | show_error('Invalid config log level [' . $configLogLevel . '], the value must be one of the following: ' . implode(', ', array_map('strtoupper', self::$validConfigLevel)), $title = 'Log Config Error', $logging = false); |
128 | 128 | } |
129 | 129 | |
130 | 130 | //check if config log_logger_name is set |
131 | - if($this->logger){ |
|
131 | + if ($this->logger) { |
|
132 | 132 | $configLoggerName = get_config('log_logger_name', ''); |
133 | - if($configLoggerName){ |
|
134 | - if (is_array($configLoggerName)){ |
|
133 | + if ($configLoggerName) { |
|
134 | + if (is_array($configLoggerName)) { |
|
135 | 135 | //for best comparaison put all string to lowercase |
136 | 136 | $configLoggerName = array_map('strtolower', $configLoggerName); |
137 | - if(! in_array(strtolower($this->logger), $configLoggerName)){ |
|
137 | + if (!in_array(strtolower($this->logger), $configLoggerName)) { |
|
138 | 138 | return; |
139 | 139 | } |
140 | 140 | } |
141 | - else if(strtolower($this->logger) !== strtolower($configLoggerName)){ |
|
141 | + else if (strtolower($this->logger) !== strtolower($configLoggerName)) { |
|
142 | 142 | return; |
143 | 143 | } |
144 | 144 | } |
145 | 145 | } |
146 | 146 | |
147 | 147 | //if $level is not an integer |
148 | - if(! is_numeric($level)){ |
|
148 | + if (!is_numeric($level)) { |
|
149 | 149 | $level = self::getLevelValue($level); |
150 | 150 | } |
151 | 151 | |
152 | 152 | //check if can logging regarding the log level config |
153 | 153 | $configLevel = self::getLevelValue($configLogLevel); |
154 | - if($configLevel > $level){ |
|
154 | + if ($configLevel > $level) { |
|
155 | 155 | //can't log |
156 | 156 | return; |
157 | 157 | } |
158 | 158 | |
159 | 159 | $logSavePath = get_config('log_save_path'); |
160 | - if(! $logSavePath){ |
|
160 | + if (!$logSavePath) { |
|
161 | 161 | $logSavePath = LOGS_PATH; |
162 | 162 | } |
163 | 163 | |
164 | - if(! is_dir($logSavePath) || !is_writable($logSavePath)){ |
|
164 | + if (!is_dir($logSavePath) || !is_writable($logSavePath)) { |
|
165 | 165 | //NOTE: here need put the show_error() "logging" to false to prevent loop |
166 | 166 | show_error('Error : the log dir does not exists or is not writable', $title = 'Log directory error', $logging = false); |
167 | 167 | } |
168 | 168 | |
169 | 169 | $path = $logSavePath . 'logs-' . date('Y-m-d') . '.log'; |
170 | - if(! file_exists($path)){ |
|
170 | + if (!file_exists($path)) { |
|
171 | 171 | touch($path); |
172 | 172 | } |
173 | 173 | //may be at this time helper user_agent not yet included |
@@ -189,7 +189,7 @@ discard block |
||
189 | 189 | |
190 | 190 | $str = $logDate . ' [' . str_pad($levelName, 7 /*warning len*/) . '] ' . ' [' . str_pad($ip, 15) . '] ' . $this->logger . ' : ' . $message . ' ' . '[' . $fileInfo['file'] . '::' . $fileInfo['line'] . ']' . "\n"; |
191 | 191 | $fp = fopen($path, 'a+'); |
192 | - if(is_resource($fp)){ |
|
192 | + if (is_resource($fp)) { |
|
193 | 193 | flock($fp, LOCK_EX); // exclusive lock, will get released when the file is closed |
194 | 194 | fwrite($fp, $str); |
195 | 195 | fclose($fp); |
@@ -203,7 +203,7 @@ discard block |
||
203 | 203 | * |
204 | 204 | * @return boolean true if the given log level is valid, false if not |
205 | 205 | */ |
206 | - private static function isValidConfigLevel($level){ |
|
206 | + private static function isValidConfigLevel($level) { |
|
207 | 207 | $level = strtolower($level); |
208 | 208 | return in_array($level, self::$validConfigLevel); |
209 | 209 | } |
@@ -213,27 +213,27 @@ discard block |
||
213 | 213 | * @param string $level the log level in string format |
214 | 214 | * @return int the log level in integer format using the predefinied constants |
215 | 215 | */ |
216 | - private static function getLevelValue($level){ |
|
216 | + private static function getLevelValue($level) { |
|
217 | 217 | $level = strtolower($level); |
218 | 218 | $value = self::NONE; |
219 | 219 | |
220 | 220 | //the default value is NONE, so means no need test for NONE |
221 | - if($level == 'fatal'){ |
|
221 | + if ($level == 'fatal') { |
|
222 | 222 | $value = self::FATAL; |
223 | 223 | } |
224 | - else if($level == 'error'){ |
|
224 | + else if ($level == 'error') { |
|
225 | 225 | $value = self::ERROR; |
226 | 226 | } |
227 | - else if($level == 'warning' || $level == 'warn'){ |
|
227 | + else if ($level == 'warning' || $level == 'warn') { |
|
228 | 228 | $value = self::WARNING; |
229 | 229 | } |
230 | - else if($level == 'info'){ |
|
230 | + else if ($level == 'info') { |
|
231 | 231 | $value = self::INFO; |
232 | 232 | } |
233 | - else if($level == 'debug'){ |
|
233 | + else if ($level == 'debug') { |
|
234 | 234 | $value = self::DEBUG; |
235 | 235 | } |
236 | - else if($level == 'all'){ |
|
236 | + else if ($level == 'all') { |
|
237 | 237 | $value = self::ALL; |
238 | 238 | } |
239 | 239 | return $value; |
@@ -244,23 +244,23 @@ discard block |
||
244 | 244 | * @param integer $level the log level in integer format |
245 | 245 | * @return string the log level in string format |
246 | 246 | */ |
247 | - private static function getLevelName($level){ |
|
247 | + private static function getLevelName($level) { |
|
248 | 248 | $value = ''; |
249 | 249 | |
250 | 250 | //the default value is NONE, so means no need test for NONE |
251 | - if($level == self::FATAL){ |
|
251 | + if ($level == self::FATAL) { |
|
252 | 252 | $value = 'FATAL'; |
253 | 253 | } |
254 | - else if($level == self::ERROR){ |
|
254 | + else if ($level == self::ERROR) { |
|
255 | 255 | $value = 'ERROR'; |
256 | 256 | } |
257 | - else if($level == self::WARNING){ |
|
257 | + else if ($level == self::WARNING) { |
|
258 | 258 | $value = 'WARNING'; |
259 | 259 | } |
260 | - else if($level == self::INFO){ |
|
260 | + else if ($level == self::INFO) { |
|
261 | 261 | $value = 'INFO'; |
262 | 262 | } |
263 | - else if($level == self::DEBUG){ |
|
263 | + else if ($level == self::DEBUG) { |
|
264 | 264 | $value = 'DEBUG'; |
265 | 265 | } |
266 | 266 | //no need for ALL |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | /** |
28 | 28 | * For application languages management |
29 | 29 | */ |
30 | - class Lang{ |
|
30 | + class Lang { |
|
31 | 31 | |
32 | 32 | /** |
33 | 33 | * The supported available language for this application. |
@@ -67,8 +67,8 @@ discard block |
||
67 | 67 | /** |
68 | 68 | * Construct new Lang instance |
69 | 69 | */ |
70 | - public function __construct(){ |
|
71 | - $this->logger =& class_loader('Log', 'classes'); |
|
70 | + public function __construct() { |
|
71 | + $this->logger = & class_loader('Log', 'classes'); |
|
72 | 72 | $this->logger->setLogger('Library::Lang'); |
73 | 73 | |
74 | 74 | $this->default = get_config('default_language', 'en'); |
@@ -76,8 +76,8 @@ discard block |
||
76 | 76 | |
77 | 77 | //add the supported languages ('key', 'display name') |
78 | 78 | $languages = get_config('languages', null); |
79 | - if(! empty($languages)){ |
|
80 | - foreach($languages as $key => $displayName){ |
|
79 | + if (!empty($languages)) { |
|
80 | + foreach ($languages as $key => $displayName) { |
|
81 | 81 | $this->addLang($key, $displayName); |
82 | 82 | } |
83 | 83 | } |
@@ -85,15 +85,15 @@ discard block |
||
85 | 85 | |
86 | 86 | //if the language exists in cookie use it |
87 | 87 | $cfgKey = get_config('language_cookie_name'); |
88 | - $this->logger->debug('Getting current language from cookie [' .$cfgKey. ']'); |
|
88 | + $this->logger->debug('Getting current language from cookie [' . $cfgKey . ']'); |
|
89 | 89 | $objCookie = & class_loader('Cookie'); |
90 | 90 | $cookieLang = $objCookie->get($cfgKey); |
91 | - if($cookieLang && $this->isValid($cookieLang)){ |
|
91 | + if ($cookieLang && $this->isValid($cookieLang)) { |
|
92 | 92 | $this->current = $cookieLang; |
93 | - $this->logger->info('Language from cookie [' .$cfgKey. '] is valid so we will set the language using the cookie value [' .$cookieLang. ']'); |
|
93 | + $this->logger->info('Language from cookie [' . $cfgKey . '] is valid so we will set the language using the cookie value [' . $cookieLang . ']'); |
|
94 | 94 | } |
95 | - else{ |
|
96 | - $this->logger->info('Language from cookie [' .$cfgKey. '] is not set, use the default value [' .$this->getDefault(). ']'); |
|
95 | + else { |
|
96 | + $this->logger->info('Language from cookie [' . $cfgKey . '] is not set, use the default value [' . $this->getDefault() . ']'); |
|
97 | 97 | $this->current = $this->getDefault(); |
98 | 98 | } |
99 | 99 | } |
@@ -103,7 +103,7 @@ discard block |
||
103 | 103 | * |
104 | 104 | * @return array the language message list |
105 | 105 | */ |
106 | - public function getAll(){ |
|
106 | + public function getAll() { |
|
107 | 107 | return $this->languages; |
108 | 108 | } |
109 | 109 | |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | * @param string $key the language key to identify |
114 | 114 | * @param string $value the language message value |
115 | 115 | */ |
116 | - public function set($key, $value){ |
|
116 | + public function set($key, $value) { |
|
117 | 117 | $this->languages[$key] = $value; |
118 | 118 | } |
119 | 119 | |
@@ -125,11 +125,11 @@ discard block |
||
125 | 125 | * |
126 | 126 | * @return string the language message value |
127 | 127 | */ |
128 | - public function get($key, $default = 'LANGUAGE_ERROR'){ |
|
129 | - if(isset($this->languages[$key])){ |
|
128 | + public function get($key, $default = 'LANGUAGE_ERROR') { |
|
129 | + if (isset($this->languages[$key])) { |
|
130 | 130 | return $this->languages[$key]; |
131 | 131 | } |
132 | - $this->logger->warning('Language key [' .$key. '] does not exist use the default value [' .$default. ']'); |
|
132 | + $this->logger->warning('Language key [' . $key . '] does not exist use the default value [' . $default . ']'); |
|
133 | 133 | return $default; |
134 | 134 | } |
135 | 135 | |
@@ -140,10 +140,10 @@ discard block |
||
140 | 140 | * |
141 | 141 | * @return boolean true if the language directory exists, false or not |
142 | 142 | */ |
143 | - public function isValid($language){ |
|
143 | + public function isValid($language) { |
|
144 | 144 | $searchDir = array(CORE_LANG_PATH, APP_LANG_PATH); |
145 | - foreach($searchDir as $dir){ |
|
146 | - if(file_exists($dir . $language) && is_dir($dir . $language)){ |
|
145 | + foreach ($searchDir as $dir) { |
|
146 | + if (file_exists($dir . $language) && is_dir($dir . $language)) { |
|
147 | 147 | return true; |
148 | 148 | } |
149 | 149 | } |
@@ -155,7 +155,7 @@ discard block |
||
155 | 155 | * |
156 | 156 | * @return string the default language |
157 | 157 | */ |
158 | - public function getDefault(){ |
|
158 | + public function getDefault() { |
|
159 | 159 | return $this->default; |
160 | 160 | } |
161 | 161 | |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | * |
165 | 165 | * @return string the current language |
166 | 166 | */ |
167 | - public function getCurrent(){ |
|
167 | + public function getCurrent() { |
|
168 | 168 | return $this->current; |
169 | 169 | } |
170 | 170 | |
@@ -174,14 +174,14 @@ discard block |
||
174 | 174 | * @param string $name the short language name like "en", "fr". |
175 | 175 | * @param string $description the human readable description of this language |
176 | 176 | */ |
177 | - public function addLang($name, $description){ |
|
178 | - if(isset($this->availables[$name])){ |
|
177 | + public function addLang($name, $description) { |
|
178 | + if (isset($this->availables[$name])) { |
|
179 | 179 | return; //already added cost in performance |
180 | 180 | } |
181 | - if($this->isValid($name)){ |
|
181 | + if ($this->isValid($name)) { |
|
182 | 182 | $this->availables[$name] = $description; |
183 | 183 | } |
184 | - else{ |
|
184 | + else { |
|
185 | 185 | show_error('The language [' . $name . '] is not valid or does not exists.'); |
186 | 186 | } |
187 | 187 | } |
@@ -191,7 +191,7 @@ discard block |
||
191 | 191 | * |
192 | 192 | * @return array the list of the application language |
193 | 193 | */ |
194 | - public function getSupported(){ |
|
194 | + public function getSupported() { |
|
195 | 195 | return $this->availables; |
196 | 196 | } |
197 | 197 | |
@@ -200,7 +200,7 @@ discard block |
||
200 | 200 | * |
201 | 201 | * @param array $langs the languages array of the messages to be added |
202 | 202 | */ |
203 | - public function addLangMessages(array $langs){ |
|
203 | + public function addLangMessages(array $langs) { |
|
204 | 204 | foreach ($langs as $key => $value) { |
205 | 205 | $this->set($key, $value); |
206 | 206 | } |