@@ -34,27 +34,31 @@ |
||
| 34 | 34 | |
| 35 | 35 | public function filters($filter_name=null, $value=null) |
| 36 | 36 | { |
| 37 | - if(is_null($filter_name)) |
|
| 38 | - return $_SESSION[$this->index_filter]; |
|
| 37 | + if(is_null($filter_name)) { |
|
| 38 | + return $_SESSION[$this->index_filter]; |
|
| 39 | + } |
|
| 39 | 40 | |
| 40 | - if(!is_null($value)) |
|
| 41 | - $_SESSION[$this->index_filter][$filter_name] = $value; |
|
| 41 | + if(!is_null($value)) { |
|
| 42 | + $_SESSION[$this->index_filter][$filter_name] = $value; |
|
| 43 | + } |
|
| 42 | 44 | |
| 43 | 45 | return $_SESSION[$this->index_filter][$filter_name] ?? null; |
| 44 | 46 | } |
| 45 | 47 | |
| 46 | 48 | public function reset_filters($filter_name=null) |
| 47 | 49 | { |
| 48 | - if(is_null($filter_name)) |
|
| 49 | - $_SESSION[$this->index_filter]=[]; |
|
| 50 | - else |
|
| 51 | - unset($_SESSION[$this->index_filter][$filter_name]); |
|
| 50 | + if(is_null($filter_name)) { |
|
| 51 | + $_SESSION[$this->index_filter]=[]; |
|
| 52 | + } else { |
|
| 53 | + unset($_SESSION[$this->index_filter][$filter_name]); |
|
| 54 | + } |
|
| 52 | 55 | } |
| 53 | 56 | |
| 54 | 57 | public function operator_id($setter = null) |
| 55 | 58 | { |
| 56 | - if(!is_null($setter)) |
|
| 57 | - $_SESSION[$this->index_operator] = ['id' => $setter, 'set_on' => time()]; |
|
| 59 | + if(!is_null($setter)) { |
|
| 60 | + $_SESSION[$this->index_operator] = ['id' => $setter, 'set_on' => time()]; |
|
| 61 | + } |
|
| 58 | 62 | |
| 59 | 63 | return $_SESSION[$this->index_operator]['id'] ?? null; |
| 60 | 64 | } |
@@ -17,8 +17,10 @@ discard block |
||
| 17 | 17 | |
| 18 | 18 | public function load_operator($id = null) |
| 19 | 19 | { |
| 20 | - if(!is_null($operator_id = $id ?? $this->get('operator_id'))) // extraction failed but we have an fk |
|
| 20 | + if(!is_null($operator_id = $id ?? $this->get('operator_id'))) { |
|
| 21 | + // extraction failed but we have an fk |
|
| 21 | 22 | $this->operator = Operator::exists($operator_id); |
| 23 | + } |
|
| 22 | 24 | } |
| 23 | 25 | |
| 24 | 26 | public function operator() : ?OperatorInterface |
@@ -28,9 +30,10 @@ discard block |
||
| 28 | 30 | $extract_attempt = $this->extract(new Operator(), true); |
| 29 | 31 | if(!is_null($extract_attempt)) |
| 30 | 32 | { |
| 31 | - foreach(['permission_names', 'permission_ids'] as $permission_marker) |
|
| 32 | - if(property_exists($this, $permission_marker)) |
|
| 33 | + foreach(['permission_names', 'permission_ids'] as $permission_marker) { |
|
| 34 | + if(property_exists($this, $permission_marker)) |
|
| 33 | 35 | $extract_attempt->set($permission_marker, $this->$permission_marker); |
| 36 | + } |
|
| 34 | 37 | |
| 35 | 38 | $this->operator = $extract_attempt; |
| 36 | 39 | } |
@@ -55,14 +58,17 @@ discard block |
||
| 55 | 58 | |
| 56 | 59 | $Query->select_also(['operator.name as operator_name', 'operator.active as operator_active']); |
| 57 | 60 | |
| 58 | - if(isset($filters['username'])) |
|
| 59 | - $Query->aw_eq('username', $filters['username'], 'operator'); |
|
| 61 | + if(isset($filters['username'])) { |
|
| 62 | + $Query->aw_eq('username', $filters['username'], 'operator'); |
|
| 63 | + } |
|
| 60 | 64 | |
| 61 | - if(isset($filters['email'])) |
|
| 62 | - $Query->aw_eq('email', $filters['email'], 'operator'); |
|
| 65 | + if(isset($filters['email'])) { |
|
| 66 | + $Query->aw_eq('email', $filters['email'], 'operator'); |
|
| 67 | + } |
|
| 63 | 68 | |
| 64 | - if(isset($filters['active'])) |
|
| 65 | - $Query->aw_eq('active', $filters['active'], 'operator'); |
|
| 69 | + if(isset($filters['active'])) { |
|
| 70 | + $Query->aw_eq('active', $filters['active'], 'operator'); |
|
| 71 | + } |
|
| 66 | 72 | |
| 67 | 73 | return $Query; |
| 68 | 74 | } |
@@ -7,18 +7,18 @@ discard block |
||
| 7 | 7 | { |
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | - * An associative array where the key is a namespace prefix and the value |
|
| 11 | - * is an array of base directories for classes in that namespace. |
|
| 12 | - * |
|
| 13 | - * @var array |
|
| 14 | - */ |
|
| 10 | + * An associative array where the key is a namespace prefix and the value |
|
| 11 | + * is an array of base directories for classes in that namespace. |
|
| 12 | + * |
|
| 13 | + * @var array |
|
| 14 | + */ |
|
| 15 | 15 | protected $prefixes = []; |
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | - * Register loader with SPL autoloader stack. |
|
| 19 | - * |
|
| 20 | - * @return void |
|
| 21 | - */ |
|
| 18 | + * Register loader with SPL autoloader stack. |
|
| 19 | + * |
|
| 20 | + * @return void |
|
| 21 | + */ |
|
| 22 | 22 | |
| 23 | 23 | public function register() |
| 24 | 24 | { |
@@ -26,28 +26,28 @@ discard block |
||
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | - * Adds a base directory for a namespace prefix. |
|
| 30 | - * |
|
| 31 | - * @param string $prefix The namespace prefix. |
|
| 32 | - * @param string $base_dir A base directory for class files in the |
|
| 33 | - * namespace. |
|
| 34 | - * @param bool $prepend If true, prepend the base directory to the stack |
|
| 35 | - * instead of appending it; this causes it to be searched first rather |
|
| 36 | - * than last. |
|
| 37 | - * @return void |
|
| 38 | - */ |
|
| 29 | + * Adds a base directory for a namespace prefix. |
|
| 30 | + * |
|
| 31 | + * @param string $prefix The namespace prefix. |
|
| 32 | + * @param string $base_dir A base directory for class files in the |
|
| 33 | + * namespace. |
|
| 34 | + * @param bool $prepend If true, prepend the base directory to the stack |
|
| 35 | + * instead of appending it; this causes it to be searched first rather |
|
| 36 | + * than last. |
|
| 37 | + * @return void |
|
| 38 | + */ |
|
| 39 | 39 | public function addNamespaceTree($base_dir, $prepend = false) |
| 40 | 40 | { |
| 41 | - $dir_content = FileSystem::preg_scandir($base_dir, '/^[A-Z]{1}[A-Za-z]+(?!\.class.\.php)$/'); |
|
| 41 | + $dir_content = FileSystem::preg_scandir($base_dir, '/^[A-Z]{1}[A-Za-z]+(?!\.class.\.php)$/'); |
|
| 42 | 42 | |
| 43 | - foreach($dir_content as $res) |
|
| 44 | - { |
|
| 45 | - if(is_dir($fullpath = $base_dir.'/'.$res)) |
|
| 46 | - { |
|
| 47 | - $this->addNamespace('HexMakina\kadro\\'.$res, $fullpath); |
|
| 43 | + foreach($dir_content as $res) |
|
| 44 | + { |
|
| 45 | + if(is_dir($fullpath = $base_dir.'/'.$res)) |
|
| 46 | + { |
|
| 47 | + $this->addNamespace('HexMakina\kadro\\'.$res, $fullpath); |
|
| 48 | 48 | $this->addNamespaceTree($fullpath); |
| 49 | - } |
|
| 50 | - } |
|
| 49 | + } |
|
| 50 | + } |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | public function addNamespace($prefix, $base_dir, $prepend = false) |
@@ -73,12 +73,12 @@ discard block |
||
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | /** |
| 76 | - * Loads the class file for a given class name. |
|
| 77 | - * |
|
| 78 | - * @param string $class The fully-qualified class name. |
|
| 79 | - * @return mixed The mapped file name on success, or boolean false on |
|
| 80 | - * failure. |
|
| 81 | - */ |
|
| 76 | + * Loads the class file for a given class name. |
|
| 77 | + * |
|
| 78 | + * @param string $class The fully-qualified class name. |
|
| 79 | + * @return mixed The mapped file name on success, or boolean false on |
|
| 80 | + * failure. |
|
| 81 | + */ |
|
| 82 | 82 | public function loadClass($class) |
| 83 | 83 | { |
| 84 | 84 | // var_dump(__FUNCTION__."($class)"); |
@@ -110,13 +110,13 @@ discard block |
||
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | /** |
| 113 | - * Load the mapped file for a namespace prefix and relative class. |
|
| 114 | - * |
|
| 115 | - * @param string $prefix The namespace prefix. |
|
| 116 | - * @param string $relative_class The relative class name. |
|
| 117 | - * @return mixed Boolean false if no mapped file can be loaded, or the |
|
| 118 | - * name of the mapped file that was loaded. |
|
| 119 | - */ |
|
| 113 | + * Load the mapped file for a namespace prefix and relative class. |
|
| 114 | + * |
|
| 115 | + * @param string $prefix The namespace prefix. |
|
| 116 | + * @param string $relative_class The relative class name. |
|
| 117 | + * @return mixed Boolean false if no mapped file can be loaded, or the |
|
| 118 | + * name of the mapped file that was loaded. |
|
| 119 | + */ |
|
| 120 | 120 | protected function loadMappedFile($prefix, $relative_class) |
| 121 | 121 | { |
| 122 | 122 | // are there any base directories for this namespace prefix? |
@@ -147,11 +147,11 @@ discard block |
||
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | /** |
| 150 | - * If a file exists, require it from the file system. |
|
| 151 | - * |
|
| 152 | - * @param string $file The file to require. |
|
| 153 | - * @return bool True if the file exists, false if not. |
|
| 154 | - */ |
|
| 150 | + * If a file exists, require it from the file system. |
|
| 151 | + * |
|
| 152 | + * @param string $file The file to require. |
|
| 153 | + * @return bool True if the file exists, false if not. |
|
| 154 | + */ |
|
| 155 | 155 | protected function requireFile($file) |
| 156 | 156 | { |
| 157 | 157 | // var_dump(__FUNCTION__."($file)"); |
@@ -192,8 +192,8 @@ |
||
| 192 | 192 | |
| 193 | 193 | public function hop_url($url) |
| 194 | 194 | { |
| 195 | - header('Cache-Control: no-cache, must-revalidate'); |
|
| 196 | - header('Expires: Mon, 01 Jan 1970 00:00:00 GMT'); |
|
| 195 | + header('Cache-Control: no-cache, must-revalidate'); |
|
| 196 | + header('Expires: Mon, 01 Jan 1970 00:00:00 GMT'); |
|
| 197 | 197 | header('Location: '.$url); |
| 198 | 198 | exit(); |
| 199 | 199 | } |
@@ -17,8 +17,9 @@ discard block |
||
| 17 | 17 | //----------------------------------------------------------- INITIALISATION |
| 18 | 18 | public function __construct($settings) |
| 19 | 19 | { |
| 20 | - if(!isset($settings['route_home'])) |
|
| 21 | - throw new RouterException('ROUTE_HOME_UNDEFINED'); |
|
| 20 | + if(!isset($settings['route_home'])) { |
|
| 21 | + throw new RouterException('ROUTE_HOME_UNDEFINED'); |
|
| 22 | + } |
|
| 22 | 23 | |
| 23 | 24 | parent::__construct(); |
| 24 | 25 | |
@@ -44,13 +45,15 @@ discard block |
||
| 44 | 45 | { |
| 45 | 46 | $this->match = parent::match($requestUrl, $requestMethod); |
| 46 | 47 | |
| 47 | - if($this->match === false) |
|
| 48 | - throw new RouterException('ROUTE_MATCH_FALSE'); |
|
| 48 | + if($this->match === false) { |
|
| 49 | + throw new RouterException('ROUTE_MATCH_FALSE'); |
|
| 50 | + } |
|
| 49 | 51 | |
| 50 | 52 | $res = explode('::', self::target()); |
| 51 | 53 | |
| 52 | - if($res === false || !isset($res[1]) || isset($ret[2])) |
|
| 53 | - throw new RouterException('INVALID_TARGET'); |
|
| 54 | + if($res === false || !isset($res[1]) || isset($ret[2])) { |
|
| 55 | + throw new RouterException('INVALID_TARGET'); |
|
| 56 | + } |
|
| 54 | 57 | |
| 55 | 58 | // if($this->match['name'] === 'akadok_controller_method') |
| 56 | 59 | // $res = [ucfirst(self::params('controller')).'Controller', ucfirst(self::params('method'))]; |
@@ -60,12 +63,14 @@ discard block |
||
| 60 | 63 | $target_method = $res[1]; |
| 61 | 64 | $found = false; |
| 62 | 65 | |
| 63 | - foreach($this->controller_namespaces as $controller_ns) |
|
| 64 | - if($found = class_exists($controller_class_name = "$controller_ns$target_controller")) |
|
| 66 | + foreach($this->controller_namespaces as $controller_ns) { |
|
| 67 | + if($found = class_exists($controller_class_name = "$controller_ns$target_controller")) |
|
| 65 | 68 | break; |
| 69 | + } |
|
| 66 | 70 | |
| 67 | - if($found === false) |
|
| 68 | - throw new RouterException('INVALID_CONTROLLER_NAME'); |
|
| 71 | + if($found === false) { |
|
| 72 | + throw new RouterException('INVALID_CONTROLLER_NAME'); |
|
| 73 | + } |
|
| 69 | 74 | |
| 70 | 75 | $this->match['target_controller'] = $controller_class_name; |
| 71 | 76 | $this->match['target_method'] = $target_method; |
@@ -87,16 +92,19 @@ discard block |
||
| 87 | 92 | { |
| 88 | 93 | |
| 89 | 94 | // $key is null, returns $dat_ass or empty array |
| 90 | - if(is_null($key)) |
|
| 91 | - return $dat_ass ?? []; |
|
| 95 | + if(is_null($key)) { |
|
| 96 | + return $dat_ass ?? []; |
|
| 97 | + } |
|
| 92 | 98 | |
| 93 | 99 | // $dat_ass[$key] not set, returns null |
| 94 | - if(!isset($dat_ass[$key])) |
|
| 95 | - return null; |
|
| 100 | + if(!isset($dat_ass[$key])) { |
|
| 101 | + return null; |
|
| 102 | + } |
|
| 96 | 103 | |
| 97 | 104 | // $dat_ass[$key] is a string, returns decoded value |
| 98 | - if(is_string($dat_ass[$key])) |
|
| 99 | - return urldecode($dat_ass[$key]); |
|
| 105 | + if(is_string($dat_ass[$key])) { |
|
| 106 | + return urldecode($dat_ass[$key]); |
|
| 107 | + } |
|
| 100 | 108 | |
| 101 | 109 | // $dat_ass[$key] is not a string, return match[$key] |
| 102 | 110 | return $dat_ass[$key]; |
@@ -146,7 +154,7 @@ discard block |
||
| 146 | 154 | { |
| 147 | 155 | try{ |
| 148 | 156 | $url = $this->generate($route, $route_params); |
| 149 | - }catch(\Exception $e){ |
|
| 157 | + } catch(\Exception $e){ |
|
| 150 | 158 | $url = $this->prehop(self::ROUTE_HOME_NAME); |
| 151 | 159 | } |
| 152 | 160 | |
@@ -169,12 +177,13 @@ discard block |
||
| 169 | 177 | { |
| 170 | 178 | $url = null; |
| 171 | 179 | |
| 172 | - if(is_null($route)) |
|
| 173 | - $url = $this->prehop(self::ROUTE_HOME_NAME, $route_params); |
|
| 174 | - elseif(is_string($route) && $this->route_exists($route)) |
|
| 175 | - $url = $this->prehop($route, $route_params); |
|
| 176 | - else |
|
| 177 | - $url = $route; |
|
| 180 | + if(is_null($route)) { |
|
| 181 | + $url = $this->prehop(self::ROUTE_HOME_NAME, $route_params); |
|
| 182 | + } elseif(is_string($route) && $this->route_exists($route)) { |
|
| 183 | + $url = $this->prehop($route, $route_params); |
|
| 184 | + } else { |
|
| 185 | + $url = $route; |
|
| 186 | + } |
|
| 178 | 187 | |
| 179 | 188 | $this->hop_url($url); |
| 180 | 189 | } |
@@ -182,8 +191,9 @@ discard block |
||
| 182 | 191 | // hops back to previous page (referer()), or home if no referer |
| 183 | 192 | public function hop_back() |
| 184 | 193 | { |
| 185 | - if(!is_null($back = $this->referer())) |
|
| 186 | - $this->hop_url($back); |
|
| 194 | + if(!is_null($back = $this->referer())) { |
|
| 195 | + $this->hop_url($back); |
|
| 196 | + } |
|
| 187 | 197 | |
| 188 | 198 | $this->hop(); |
| 189 | 199 | } |
@@ -200,16 +210,18 @@ discard block |
||
| 200 | 210 | // returns null if same as current URL (prevents endless redirection loop) |
| 201 | 211 | public function referer() |
| 202 | 212 | { |
| 203 | - if(isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] != $this->web_host() .$_SERVER['REQUEST_URI']) |
|
| 204 | - return $_SERVER['HTTP_REFERER']; |
|
| 213 | + if(isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] != $this->web_host() .$_SERVER['REQUEST_URI']) { |
|
| 214 | + return $_SERVER['HTTP_REFERER']; |
|
| 215 | + } |
|
| 205 | 216 | |
| 206 | 217 | return null; |
| 207 | 218 | } |
| 208 | 219 | |
| 209 | 220 | public function send_file($file_path) |
| 210 | 221 | { |
| 211 | - if(!file_exists($file_path)) |
|
| 212 | - throw new RouterException('SENDING_NON_EXISTING_FILE'); |
|
| 222 | + if(!file_exists($file_path)) { |
|
| 223 | + throw new RouterException('SENDING_NON_EXISTING_FILE'); |
|
| 224 | + } |
|
| 213 | 225 | |
| 214 | 226 | $file_name = basename($file_path); |
| 215 | 227 | |
@@ -2,266 +2,266 @@ |
||
| 2 | 2 | |
| 3 | 3 | class AltoRouter { |
| 4 | 4 | |
| 5 | - /** |
|
| 6 | - * @var array Array of all routes (incl. named routes). |
|
| 7 | - */ |
|
| 8 | - protected $routes = []; |
|
| 9 | - |
|
| 10 | - /** |
|
| 11 | - * @var array Array of all named routes. |
|
| 12 | - */ |
|
| 13 | - protected $namedRoutes = []; |
|
| 14 | - |
|
| 15 | - /** |
|
| 16 | - * @var string Can be used to ignore leading part of the Request URL (if main file lives in subdirectory of host) |
|
| 17 | - */ |
|
| 18 | - protected $basePath = ''; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * @var array Array of default match types (regex helpers) |
|
| 22 | - */ |
|
| 23 | - protected $matchTypes = array( |
|
| 24 | - 'i' => '[0-9]++', |
|
| 25 | - 'a' => '[0-9A-Za-z]++', |
|
| 26 | - 'h' => '[0-9A-Fa-f]++', |
|
| 27 | - '*' => '.+?', |
|
| 28 | - '**' => '.++', |
|
| 29 | - '' => '[^/\.]++' |
|
| 30 | - ); |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Create router in one call from config. |
|
| 34 | - * |
|
| 35 | - * @param array $routes |
|
| 36 | - * @param string $basePath |
|
| 37 | - * @param array $matchTypes |
|
| 38 | - */ |
|
| 39 | - public function __construct( $routes = array(), $basePath = '', $matchTypes = array() ) { |
|
| 40 | - $this->addRoutes($routes); |
|
| 41 | - $this->setBasePath($basePath); |
|
| 42 | - $this->addMatchTypes($matchTypes); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Retrieves all routes. |
|
| 47 | - * Useful if you want to process or display routes. |
|
| 48 | - * @return array All routes. |
|
| 49 | - */ |
|
| 50 | - public function getRoutes() { |
|
| 51 | - return $this->routes; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Add multiple routes at once from array in the following format: |
|
| 56 | - * |
|
| 57 | - * $routes = array( |
|
| 58 | - * array($method, $route, $target, $name) |
|
| 59 | - * ); |
|
| 60 | - * |
|
| 61 | - * @param array $routes |
|
| 62 | - * @return void |
|
| 63 | - * @author Koen Punt |
|
| 64 | - * @throws Exception |
|
| 65 | - */ |
|
| 66 | - public function addRoutes($routes){ |
|
| 67 | - if(!is_array($routes) && !$routes instanceof Traversable) { |
|
| 68 | - throw new \Exception('Routes should be an array or an instance of Traversable'); |
|
| 69 | - } |
|
| 70 | - foreach($routes as $route) { |
|
| 71 | - call_user_func_array(array($this, 'map'), $route); |
|
| 72 | - } |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Set the base path. |
|
| 77 | - * Useful if you are running your application from a subdirectory. |
|
| 78 | - */ |
|
| 79 | - public function setBasePath($basePath) { |
|
| 80 | - $this->basePath = $basePath; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Add named match types. It uses array_merge so keys can be overwritten. |
|
| 85 | - * |
|
| 86 | - * @param array $matchTypes The key is the name and the value is the regex. |
|
| 87 | - */ |
|
| 88 | - public function addMatchTypes($matchTypes) { |
|
| 89 | - $this->matchTypes = array_merge($this->matchTypes, $matchTypes); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Map a route to a target |
|
| 94 | - * |
|
| 95 | - * @param string $method One of 5 HTTP Methods, or a pipe-separated list of multiple HTTP Methods (GET|POST|PATCH|PUT|DELETE) |
|
| 96 | - * @param string $route The route regex, custom regex must start with an @. You can use multiple pre-set regex filters, like [i:id] |
|
| 97 | - * @param mixed $target The target where this route should point to. Can be anything. |
|
| 98 | - * @param string $name Optional name of this route. Supply if you want to reverse route this url in your application. |
|
| 99 | - * @throws Exception |
|
| 100 | - */ |
|
| 101 | - public function map($method, $route, $target, $name = null) { |
|
| 102 | - |
|
| 103 | - $this->routes[] = array($method, $route, $target, $name); |
|
| 104 | - |
|
| 105 | - if($name) { |
|
| 106 | - if(isset($this->namedRoutes[$name])) { |
|
| 107 | - throw new \Exception("Can not redeclare route '{$name}'"); |
|
| 108 | - } else { |
|
| 109 | - $this->namedRoutes[$name] = $route; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - return; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Reversed routing |
|
| 119 | - * |
|
| 120 | - * Generate the URL for a named route. Replace regexes with supplied parameters |
|
| 121 | - * |
|
| 122 | - * @param string $routeName The name of the route. |
|
| 123 | - * @param array @params Associative array of parameters to replace placeholders with. |
|
| 124 | - * @return string The URL of the route with named parameters in place. |
|
| 125 | - * @throws Exception |
|
| 126 | - */ |
|
| 127 | - public function generate($routeName, array $params = array()) { |
|
| 128 | - |
|
| 129 | - // Check if named route exists |
|
| 130 | - if(!isset($this->namedRoutes[$routeName])) { |
|
| 131 | - throw new \Exception("Route '{$routeName}' does not exist."); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - // Replace named parameters |
|
| 135 | - $route = $this->namedRoutes[$routeName]; |
|
| 136 | - |
|
| 137 | - // prepend base path to route url again |
|
| 138 | - $url = $this->basePath . $route; |
|
| 139 | - |
|
| 140 | - if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) { |
|
| 141 | - |
|
| 142 | - foreach($matches as $match) { |
|
| 143 | - list($block, $pre, $type, $param, $optional) = $match; |
|
| 144 | - |
|
| 145 | - if ($pre) { |
|
| 146 | - $block = substr($block, 1); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - if(isset($params[$param])) { |
|
| 150 | - $url = str_replace($block, $params[$param], $url); |
|
| 151 | - } elseif ($optional) { |
|
| 152 | - $url = str_replace($pre . $block, '', $url); |
|
| 153 | - } |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - return $url; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Match a given Request Url against stored routes |
|
| 164 | - * @param string $requestUrl |
|
| 165 | - * @param string $requestMethod |
|
| 166 | - * @return array|boolean Array with route information on success, false on failure (no match). |
|
| 167 | - */ |
|
| 168 | - public function match($requestUrl = null, $requestMethod = null) { |
|
| 169 | - |
|
| 170 | - $params = []; |
|
| 171 | - $match = false; |
|
| 172 | - |
|
| 173 | - // set Request Url if it isn't passed as parameter |
|
| 174 | - if($requestUrl === null) { |
|
| 175 | - $requestUrl = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/'; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - // strip base path from request url |
|
| 179 | - $requestUrl = substr($requestUrl, strlen($this->basePath)); |
|
| 5 | + /** |
|
| 6 | + * @var array Array of all routes (incl. named routes). |
|
| 7 | + */ |
|
| 8 | + protected $routes = []; |
|
| 9 | + |
|
| 10 | + /** |
|
| 11 | + * @var array Array of all named routes. |
|
| 12 | + */ |
|
| 13 | + protected $namedRoutes = []; |
|
| 14 | + |
|
| 15 | + /** |
|
| 16 | + * @var string Can be used to ignore leading part of the Request URL (if main file lives in subdirectory of host) |
|
| 17 | + */ |
|
| 18 | + protected $basePath = ''; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * @var array Array of default match types (regex helpers) |
|
| 22 | + */ |
|
| 23 | + protected $matchTypes = array( |
|
| 24 | + 'i' => '[0-9]++', |
|
| 25 | + 'a' => '[0-9A-Za-z]++', |
|
| 26 | + 'h' => '[0-9A-Fa-f]++', |
|
| 27 | + '*' => '.+?', |
|
| 28 | + '**' => '.++', |
|
| 29 | + '' => '[^/\.]++' |
|
| 30 | + ); |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Create router in one call from config. |
|
| 34 | + * |
|
| 35 | + * @param array $routes |
|
| 36 | + * @param string $basePath |
|
| 37 | + * @param array $matchTypes |
|
| 38 | + */ |
|
| 39 | + public function __construct( $routes = array(), $basePath = '', $matchTypes = array() ) { |
|
| 40 | + $this->addRoutes($routes); |
|
| 41 | + $this->setBasePath($basePath); |
|
| 42 | + $this->addMatchTypes($matchTypes); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Retrieves all routes. |
|
| 47 | + * Useful if you want to process or display routes. |
|
| 48 | + * @return array All routes. |
|
| 49 | + */ |
|
| 50 | + public function getRoutes() { |
|
| 51 | + return $this->routes; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Add multiple routes at once from array in the following format: |
|
| 56 | + * |
|
| 57 | + * $routes = array( |
|
| 58 | + * array($method, $route, $target, $name) |
|
| 59 | + * ); |
|
| 60 | + * |
|
| 61 | + * @param array $routes |
|
| 62 | + * @return void |
|
| 63 | + * @author Koen Punt |
|
| 64 | + * @throws Exception |
|
| 65 | + */ |
|
| 66 | + public function addRoutes($routes){ |
|
| 67 | + if(!is_array($routes) && !$routes instanceof Traversable) { |
|
| 68 | + throw new \Exception('Routes should be an array or an instance of Traversable'); |
|
| 69 | + } |
|
| 70 | + foreach($routes as $route) { |
|
| 71 | + call_user_func_array(array($this, 'map'), $route); |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Set the base path. |
|
| 77 | + * Useful if you are running your application from a subdirectory. |
|
| 78 | + */ |
|
| 79 | + public function setBasePath($basePath) { |
|
| 80 | + $this->basePath = $basePath; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Add named match types. It uses array_merge so keys can be overwritten. |
|
| 85 | + * |
|
| 86 | + * @param array $matchTypes The key is the name and the value is the regex. |
|
| 87 | + */ |
|
| 88 | + public function addMatchTypes($matchTypes) { |
|
| 89 | + $this->matchTypes = array_merge($this->matchTypes, $matchTypes); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Map a route to a target |
|
| 94 | + * |
|
| 95 | + * @param string $method One of 5 HTTP Methods, or a pipe-separated list of multiple HTTP Methods (GET|POST|PATCH|PUT|DELETE) |
|
| 96 | + * @param string $route The route regex, custom regex must start with an @. You can use multiple pre-set regex filters, like [i:id] |
|
| 97 | + * @param mixed $target The target where this route should point to. Can be anything. |
|
| 98 | + * @param string $name Optional name of this route. Supply if you want to reverse route this url in your application. |
|
| 99 | + * @throws Exception |
|
| 100 | + */ |
|
| 101 | + public function map($method, $route, $target, $name = null) { |
|
| 102 | + |
|
| 103 | + $this->routes[] = array($method, $route, $target, $name); |
|
| 104 | + |
|
| 105 | + if($name) { |
|
| 106 | + if(isset($this->namedRoutes[$name])) { |
|
| 107 | + throw new \Exception("Can not redeclare route '{$name}'"); |
|
| 108 | + } else { |
|
| 109 | + $this->namedRoutes[$name] = $route; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + return; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Reversed routing |
|
| 119 | + * |
|
| 120 | + * Generate the URL for a named route. Replace regexes with supplied parameters |
|
| 121 | + * |
|
| 122 | + * @param string $routeName The name of the route. |
|
| 123 | + * @param array @params Associative array of parameters to replace placeholders with. |
|
| 124 | + * @return string The URL of the route with named parameters in place. |
|
| 125 | + * @throws Exception |
|
| 126 | + */ |
|
| 127 | + public function generate($routeName, array $params = array()) { |
|
| 128 | + |
|
| 129 | + // Check if named route exists |
|
| 130 | + if(!isset($this->namedRoutes[$routeName])) { |
|
| 131 | + throw new \Exception("Route '{$routeName}' does not exist."); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + // Replace named parameters |
|
| 135 | + $route = $this->namedRoutes[$routeName]; |
|
| 136 | + |
|
| 137 | + // prepend base path to route url again |
|
| 138 | + $url = $this->basePath . $route; |
|
| 139 | + |
|
| 140 | + if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) { |
|
| 141 | + |
|
| 142 | + foreach($matches as $match) { |
|
| 143 | + list($block, $pre, $type, $param, $optional) = $match; |
|
| 144 | + |
|
| 145 | + if ($pre) { |
|
| 146 | + $block = substr($block, 1); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + if(isset($params[$param])) { |
|
| 150 | + $url = str_replace($block, $params[$param], $url); |
|
| 151 | + } elseif ($optional) { |
|
| 152 | + $url = str_replace($pre . $block, '', $url); |
|
| 153 | + } |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + return $url; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Match a given Request Url against stored routes |
|
| 164 | + * @param string $requestUrl |
|
| 165 | + * @param string $requestMethod |
|
| 166 | + * @return array|boolean Array with route information on success, false on failure (no match). |
|
| 167 | + */ |
|
| 168 | + public function match($requestUrl = null, $requestMethod = null) { |
|
| 169 | + |
|
| 170 | + $params = []; |
|
| 171 | + $match = false; |
|
| 172 | + |
|
| 173 | + // set Request Url if it isn't passed as parameter |
|
| 174 | + if($requestUrl === null) { |
|
| 175 | + $requestUrl = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/'; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + // strip base path from request url |
|
| 179 | + $requestUrl = substr($requestUrl, strlen($this->basePath)); |
|
| 180 | 180 | |
| 181 | - // Strip query string (?a=b) from Request Url |
|
| 182 | - if (($strpos = strpos($requestUrl, '?')) !== false) { |
|
| 183 | - $requestUrl = substr($requestUrl, 0, $strpos); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - // set Request Method if it isn't passed as a parameter |
|
| 187 | - if($requestMethod === null) { |
|
| 188 | - $requestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET'; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - foreach($this->routes as $handler) { |
|
| 192 | - list($methods, $route, $target, $name) = $handler; |
|
| 193 | - $method_match = (stripos($methods, $requestMethod) !== false); |
|
| 194 | - |
|
| 195 | - // Method did not match, continue to next route. |
|
| 196 | - if (!$method_match) continue; |
|
| 197 | - |
|
| 198 | - if ($route === '*') { |
|
| 199 | - // * wildcard (matches all) |
|
| 200 | - $match = true; |
|
| 201 | - } elseif (isset($route[0]) && $route[0] === '@') { |
|
| 202 | - // @ regex delimiter |
|
| 203 | - $pattern = '`' . substr($route, 1) . '`u'; |
|
| 204 | - $match = preg_match($pattern, $requestUrl, $params) === 1; |
|
| 205 | - } elseif (($position = strpos($route, '[')) === false) { |
|
| 206 | - // No params in url, do string comparison |
|
| 207 | - $match = strcmp($requestUrl, $route) === 0; |
|
| 208 | - } else { |
|
| 209 | - // Compare longest non-param string with url |
|
| 210 | - if (strncmp($requestUrl, $route, $position) !== 0) { |
|
| 211 | - continue; |
|
| 212 | - } |
|
| 213 | - $regex = $this->compileRoute($route); |
|
| 214 | - $match = preg_match($regex, $requestUrl, $params) === 1; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - if ($match) { |
|
| 218 | - |
|
| 219 | - if ($params) { |
|
| 220 | - foreach($params as $key => $value) { |
|
| 221 | - if(is_numeric($key)) unset($params[$key]); |
|
| 222 | - } |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - return array( |
|
| 226 | - 'target' => $target, |
|
| 227 | - 'params' => $params, |
|
| 228 | - 'name' => $name |
|
| 229 | - ); |
|
| 230 | - } |
|
| 231 | - } |
|
| 232 | - return false; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * Compile the regex for a given route (EXPENSIVE) |
|
| 237 | - */ |
|
| 238 | - private function compileRoute($route) { |
|
| 239 | - if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) { |
|
| 240 | - |
|
| 241 | - $matchTypes = $this->matchTypes; |
|
| 242 | - foreach($matches as $match) { |
|
| 243 | - list($block, $pre, $type, $param, $optional) = $match; |
|
| 244 | - |
|
| 245 | - if (isset($matchTypes[$type])) { |
|
| 246 | - $type = $matchTypes[$type]; |
|
| 247 | - } |
|
| 248 | - if ($pre === '.') { |
|
| 249 | - $pre = '\.'; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - //Older versions of PCRE require the 'P' in (?P<named>) |
|
| 253 | - $pattern = '(?:' |
|
| 254 | - . ($pre !== '' ? $pre : null) |
|
| 255 | - . '(' |
|
| 256 | - . ($param !== '' ? "?P<$param>" : null) |
|
| 257 | - . $type |
|
| 258 | - . '))' |
|
| 259 | - . ($optional !== '' ? '?' : null); |
|
| 260 | - |
|
| 261 | - $route = str_replace($block, $pattern, $route); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - } |
|
| 265 | - return "`^$route$`u"; |
|
| 266 | - } |
|
| 181 | + // Strip query string (?a=b) from Request Url |
|
| 182 | + if (($strpos = strpos($requestUrl, '?')) !== false) { |
|
| 183 | + $requestUrl = substr($requestUrl, 0, $strpos); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + // set Request Method if it isn't passed as a parameter |
|
| 187 | + if($requestMethod === null) { |
|
| 188 | + $requestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET'; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + foreach($this->routes as $handler) { |
|
| 192 | + list($methods, $route, $target, $name) = $handler; |
|
| 193 | + $method_match = (stripos($methods, $requestMethod) !== false); |
|
| 194 | + |
|
| 195 | + // Method did not match, continue to next route. |
|
| 196 | + if (!$method_match) continue; |
|
| 197 | + |
|
| 198 | + if ($route === '*') { |
|
| 199 | + // * wildcard (matches all) |
|
| 200 | + $match = true; |
|
| 201 | + } elseif (isset($route[0]) && $route[0] === '@') { |
|
| 202 | + // @ regex delimiter |
|
| 203 | + $pattern = '`' . substr($route, 1) . '`u'; |
|
| 204 | + $match = preg_match($pattern, $requestUrl, $params) === 1; |
|
| 205 | + } elseif (($position = strpos($route, '[')) === false) { |
|
| 206 | + // No params in url, do string comparison |
|
| 207 | + $match = strcmp($requestUrl, $route) === 0; |
|
| 208 | + } else { |
|
| 209 | + // Compare longest non-param string with url |
|
| 210 | + if (strncmp($requestUrl, $route, $position) !== 0) { |
|
| 211 | + continue; |
|
| 212 | + } |
|
| 213 | + $regex = $this->compileRoute($route); |
|
| 214 | + $match = preg_match($regex, $requestUrl, $params) === 1; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + if ($match) { |
|
| 218 | + |
|
| 219 | + if ($params) { |
|
| 220 | + foreach($params as $key => $value) { |
|
| 221 | + if(is_numeric($key)) unset($params[$key]); |
|
| 222 | + } |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + return array( |
|
| 226 | + 'target' => $target, |
|
| 227 | + 'params' => $params, |
|
| 228 | + 'name' => $name |
|
| 229 | + ); |
|
| 230 | + } |
|
| 231 | + } |
|
| 232 | + return false; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * Compile the regex for a given route (EXPENSIVE) |
|
| 237 | + */ |
|
| 238 | + private function compileRoute($route) { |
|
| 239 | + if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) { |
|
| 240 | + |
|
| 241 | + $matchTypes = $this->matchTypes; |
|
| 242 | + foreach($matches as $match) { |
|
| 243 | + list($block, $pre, $type, $param, $optional) = $match; |
|
| 244 | + |
|
| 245 | + if (isset($matchTypes[$type])) { |
|
| 246 | + $type = $matchTypes[$type]; |
|
| 247 | + } |
|
| 248 | + if ($pre === '.') { |
|
| 249 | + $pre = '\.'; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + //Older versions of PCRE require the 'P' in (?P<named>) |
|
| 253 | + $pattern = '(?:' |
|
| 254 | + . ($pre !== '' ? $pre : null) |
|
| 255 | + . '(' |
|
| 256 | + . ($param !== '' ? "?P<$param>" : null) |
|
| 257 | + . $type |
|
| 258 | + . '))' |
|
| 259 | + . ($optional !== '' ? '?' : null); |
|
| 260 | + |
|
| 261 | + $route = str_replace($block, $pattern, $route); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + } |
|
| 265 | + return "`^$route$`u"; |
|
| 266 | + } |
|
| 267 | 267 | } |
@@ -193,7 +193,9 @@ discard block |
||
| 193 | 193 | $method_match = (stripos($methods, $requestMethod) !== false); |
| 194 | 194 | |
| 195 | 195 | // Method did not match, continue to next route. |
| 196 | - if (!$method_match) continue; |
|
| 196 | + if (!$method_match) { |
|
| 197 | + continue; |
|
| 198 | + } |
|
| 197 | 199 | |
| 198 | 200 | if ($route === '*') { |
| 199 | 201 | // * wildcard (matches all) |
@@ -218,7 +220,9 @@ discard block |
||
| 218 | 220 | |
| 219 | 221 | if ($params) { |
| 220 | 222 | foreach($params as $key => $value) { |
| 221 | - if(is_numeric($key)) unset($params[$key]); |
|
| 223 | + if(is_numeric($key)) { |
|
| 224 | + unset($params[$key]); |
|
| 225 | + } |
|
| 222 | 226 | } |
| 223 | 227 | } |
| 224 | 228 | |
@@ -7,8 +7,8 @@ discard block |
||
| 7 | 7 | { |
| 8 | 8 | protected static function prepare_pagination($records_total) |
| 9 | 9 | { |
| 10 | - global $settings; |
|
| 11 | - global $smarty; |
|
| 10 | + global $settings; |
|
| 11 | + global $smarty; |
|
| 12 | 12 | |
| 13 | 13 | $pages_max_on_each_side = 3; |
| 14 | 14 | $pages_max_displayed = $pages_max_on_each_side*2+1; |
@@ -35,12 +35,12 @@ discard block |
||
| 35 | 35 | if($pages_last > $pages_total) // $pages_max_displayed greater than the total of pages |
| 36 | 36 | $pages_last = $pages_total; |
| 37 | 37 | |
| 38 | - $this->viewport("pages_total", $pages_total); |
|
| 39 | - $this->viewport("pages_first", $pages_first); |
|
| 38 | + $this->viewport("pages_total", $pages_total); |
|
| 39 | + $this->viewport("pages_first", $pages_first); |
|
| 40 | 40 | $this->viewport("pages_previous", $pages_current <= 1 ? $pages_total : $pages_current - 1); |
| 41 | - $this->viewport("pages_current", $pages_current); |
|
| 42 | - $this->viewport("pages_next", $pages_current >= $pages_total ? 1 : $pages_current + 1); |
|
| 43 | - $this->viewport("pages_last", $pages_last); |
|
| 41 | + $this->viewport("pages_current", $pages_current); |
|
| 42 | + $this->viewport("pages_next", $pages_current >= $pages_total ? 1 : $pages_current + 1); |
|
| 43 | + $this->viewport("pages_last", $pages_last); |
|
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | } |
@@ -15,25 +15,31 @@ |
||
| 15 | 15 | |
| 16 | 16 | // are we paginating ? |
| 17 | 17 | |
| 18 | - if(is_null($this->box('StateAgent')->filters('results_per_page'))) |
|
| 19 | - return; |
|
| 18 | + if(is_null($this->box('StateAgent')->filters('results_per_page'))) { |
|
| 19 | + return; |
|
| 20 | + } |
|
| 20 | 21 | |
| 21 | 22 | $pages_range = intval($this->box('StateAgent')->filters('results_per_page')); |
| 22 | 23 | $pages_total = $pages_range > 0 ? intval(ceil($records_total / $pages_range)) : 1; |
| 23 | 24 | $pages_current = intval($this->box('StateAgent')->filters('page')); |
| 24 | 25 | |
| 25 | - if($pages_current >= $pages_total) |
|
| 26 | - $pages_current = $pages_total; |
|
| 26 | + if($pages_current >= $pages_total) { |
|
| 27 | + $pages_current = $pages_total; |
|
| 28 | + } |
|
| 27 | 29 | |
| 28 | 30 | $pages_first = ($pages_current <= $pages_max_on_each_side)? 1 : $pages_current - $pages_max_on_each_side; |
| 29 | 31 | |
| 30 | 32 | $pages_last = $pages_current + $pages_max_on_each_side; |
| 31 | 33 | |
| 32 | - if($pages_last < $pages_max_displayed) // add the missing pages to fullfill $pages_max_displayed |
|
| 34 | + if($pages_last < $pages_max_displayed) { |
|
| 35 | + // add the missing pages to fullfill $pages_max_displayed |
|
| 33 | 36 | $pages_last += $pages_max_displayed-$pages_last; |
| 37 | + } |
|
| 34 | 38 | |
| 35 | - if($pages_last > $pages_total) // $pages_max_displayed greater than the total of pages |
|
| 39 | + if($pages_last > $pages_total) { |
|
| 40 | + // $pages_max_displayed greater than the total of pages |
|
| 36 | 41 | $pages_last = $pages_total; |
| 42 | + } |
|
| 37 | 43 | |
| 38 | 44 | $this->viewport("pages_total", $pages_total); |
| 39 | 45 | $this->viewport("pages_first", $pages_first); |
@@ -107,18 +107,18 @@ discard block |
||
| 107 | 107 | $custom_template = null; |
| 108 | 108 | |
| 109 | 109 | if(method_exists($this, 'prepare')) |
| 110 | - $this->prepare(); |
|
| 110 | + $this->prepare(); |
|
| 111 | 111 | |
| 112 | - if(method_exists($this, $method = $this->router()->target_method())) |
|
| 112 | + if(method_exists($this, $method = $this->router()->target_method())) |
|
| 113 | 113 | { |
| 114 | 114 | $custom_template = $this->$method(); |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | if(method_exists($this, 'conclude')) |
| 118 | - $this->conclude(); |
|
| 118 | + $this->conclude(); |
|
| 119 | 119 | |
| 120 | 120 | if(method_exists($this, 'display')) |
| 121 | - $template = $this->display($custom_template); |
|
| 121 | + $template = $this->display($custom_template); |
|
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | public function conclude(){} |
@@ -143,16 +143,16 @@ discard block |
||
| 143 | 143 | |
| 144 | 144 | $template = $this->find_template($smarty, $custom_template); // throws Exception if nothing found |
| 145 | 145 | |
| 146 | - $this->viewport('controller', $this); |
|
| 146 | + $this->viewport('controller', $this); |
|
| 147 | 147 | |
| 148 | 148 | $this->viewport('user_messages', $this->logger()->get_user_report()); |
| 149 | 149 | |
| 150 | 150 | |
| 151 | - $this->viewport('file_root', $this->router()->file_root()); |
|
| 152 | - $this->viewport('view_path', $this->router()->file_root() . $this->box('settings.smarty.template_path').'app/'); |
|
| 153 | - $this->viewport('web_root', $this->router()->web_root()); |
|
| 154 | - $this->viewport('view_url', $this->router()->web_root() . $this->box('settings.smarty.template_path')); |
|
| 155 | - $this->viewport('images_url', $this->router()->web_root() . $this->box('settings.smarty.template_path') . 'images/'); |
|
| 151 | + $this->viewport('file_root', $this->router()->file_root()); |
|
| 152 | + $this->viewport('view_path', $this->router()->file_root() . $this->box('settings.smarty.template_path').'app/'); |
|
| 153 | + $this->viewport('web_root', $this->router()->web_root()); |
|
| 154 | + $this->viewport('view_url', $this->router()->web_root() . $this->box('settings.smarty.template_path')); |
|
| 155 | + $this->viewport('images_url', $this->router()->web_root() . $this->box('settings.smarty.template_path') . 'images/'); |
|
| 156 | 156 | |
| 157 | 157 | foreach($this->viewport() as $template_var_name => $value) |
| 158 | 158 | $smarty->assign($template_var_name, $value); |
@@ -227,12 +227,12 @@ discard block |
||
| 227 | 227 | * |
| 228 | 228 | */ |
| 229 | 229 | public function route_back($route_name=null, $route_params=[]) : string |
| 230 | - { |
|
| 230 | + { |
|
| 231 | 231 | if(is_null($route_name)) |
| 232 | - return $this->route_back ?? $this->router()->prehop(RouterInterface::ROUTE_HOME_NAME); |
|
| 232 | + return $this->route_back ?? $this->router()->prehop(RouterInterface::ROUTE_HOME_NAME); |
|
| 233 | 233 | |
| 234 | 234 | return $this->route_back = $this->route_factory($route_name, $route_params); |
| 235 | - } |
|
| 235 | + } |
|
| 236 | 236 | |
| 237 | 237 | public function route_factory($route_name=null, $route_params=[]) : string |
| 238 | 238 | { |
@@ -31,8 +31,9 @@ discard block |
||
| 31 | 31 | // shortcut for (un)boxing |
| 32 | 32 | public function box($key, $instance=null) |
| 33 | 33 | { |
| 34 | - if(!is_null($instance)) |
|
| 35 | - $this->container->register($key, $instance); |
|
| 34 | + if(!is_null($instance)) { |
|
| 35 | + $this->container->register($key, $instance); |
|
| 36 | + } |
|
| 36 | 37 | |
| 37 | 38 | // dd($this->container->get($key)); |
| 38 | 39 | return $this->container->get($key); |
@@ -64,15 +65,18 @@ discard block |
||
| 64 | 65 | public function authorize($permission=null) |
| 65 | 66 | { |
| 66 | 67 | // if(!$this->requires_operator() || is_null($permission)) |
| 67 | - if(!$this->requires_operator()) |
|
| 68 | - return true; |
|
| 68 | + if(!$this->requires_operator()) { |
|
| 69 | + return true; |
|
| 70 | + } |
|
| 69 | 71 | |
| 70 | 72 | $operator = $this->operator(); |
| 71 | - if(is_null($operator) || $operator->is_new() || !$operator->is_active()) |
|
| 72 | - throw new AccessRefusedException(); |
|
| 73 | + if(is_null($operator) || $operator->is_new() || !$operator->is_active()) { |
|
| 74 | + throw new AccessRefusedException(); |
|
| 75 | + } |
|
| 73 | 76 | |
| 74 | - if(!is_null($permission) && !$operator->has_permission($permission)) |
|
| 75 | - throw new AccessRefusedException(); |
|
| 77 | + if(!is_null($permission) && !$operator->has_permission($permission)) { |
|
| 78 | + throw new AccessRefusedException(); |
|
| 79 | + } |
|
| 76 | 80 | |
| 77 | 81 | return true; |
| 78 | 82 | } |
@@ -80,22 +84,26 @@ discard block |
||
| 80 | 84 | public function viewport($key=null, $value=null, $coercion=false) |
| 81 | 85 | { |
| 82 | 86 | // no key, returns all |
| 83 | - if(is_null($key)) |
|
| 84 | - return $this->template_variables; |
|
| 87 | + if(is_null($key)) { |
|
| 88 | + return $this->template_variables; |
|
| 89 | + } |
|
| 85 | 90 | |
| 86 | 91 | // got key, got null value, returns $[$key] |
| 87 | 92 | if(is_null($value)) |
| 88 | 93 | { |
| 89 | - if($coercion === true) // break rule 1 ? |
|
| 94 | + if($coercion === true) { |
|
| 95 | + // break rule 1 ? |
|
| 90 | 96 | $this->template_variables[$key] = null; |
| 97 | + } |
|
| 91 | 98 | |
| 92 | 99 | return $this->template_variables[$key] ?? null; |
| 93 | 100 | } |
| 94 | 101 | |
| 95 | 102 | // got key, got value |
| 96 | 103 | // sets or coerces $[$key]=$value and returns $[$key] |
| 97 | - if(!isset($this->template_variables[$key]) || $coercion === true) |
|
| 98 | - $this->template_variables[$key] = $value; |
|
| 104 | + if(!isset($this->template_variables[$key]) || $coercion === true) { |
|
| 105 | + $this->template_variables[$key] = $value; |
|
| 106 | + } |
|
| 99 | 107 | |
| 100 | 108 | return $this->template_variables[$key] ?? null; |
| 101 | 109 | } |
@@ -106,19 +114,22 @@ discard block |
||
| 106 | 114 | |
| 107 | 115 | $custom_template = null; |
| 108 | 116 | |
| 109 | - if(method_exists($this, 'prepare')) |
|
| 110 | - $this->prepare(); |
|
| 117 | + if(method_exists($this, 'prepare')) { |
|
| 118 | + $this->prepare(); |
|
| 119 | + } |
|
| 111 | 120 | |
| 112 | 121 | if(method_exists($this, $method = $this->router()->target_method())) |
| 113 | 122 | { |
| 114 | 123 | $custom_template = $this->$method(); |
| 115 | 124 | } |
| 116 | 125 | |
| 117 | - if(method_exists($this, 'conclude')) |
|
| 118 | - $this->conclude(); |
|
| 126 | + if(method_exists($this, 'conclude')) { |
|
| 127 | + $this->conclude(); |
|
| 128 | + } |
|
| 119 | 129 | |
| 120 | - if(method_exists($this, 'display')) |
|
| 121 | - $template = $this->display($custom_template); |
|
| 130 | + if(method_exists($this, 'display')) { |
|
| 131 | + $template = $this->display($custom_template); |
|
| 132 | + } |
|
| 122 | 133 | } |
| 123 | 134 | |
| 124 | 135 | public function conclude(){} |
@@ -133,8 +144,10 @@ discard block |
||
| 133 | 144 | array_walk_recursive($_GET, function(&$value){$value = trim($value);}); |
| 134 | 145 | array_walk_recursive($_REQUEST, function(&$value){$value = trim($value);}); |
| 135 | 146 | |
| 136 | - if($this->router()->submits()) |
|
| 137 | - array_walk_recursive($_POST, function(&$value){$value = trim($value);}); |
|
| 147 | + if($this->router()->submits()) { |
|
| 148 | + array_walk_recursive($_POST, function(&$value){$value = trim($value); |
|
| 149 | + } |
|
| 150 | + }); |
|
| 138 | 151 | } |
| 139 | 152 | |
| 140 | 153 | public function display($custom_template = null, $standalone=false) |
@@ -154,14 +167,14 @@ discard block |
||
| 154 | 167 | $this->viewport('view_url', $this->router()->web_root() . $this->box('settings.smarty.template_path')); |
| 155 | 168 | $this->viewport('images_url', $this->router()->web_root() . $this->box('settings.smarty.template_path') . 'images/'); |
| 156 | 169 | |
| 157 | - foreach($this->viewport() as $template_var_name => $value) |
|
| 158 | - $smarty->assign($template_var_name, $value); |
|
| 170 | + foreach($this->viewport() as $template_var_name => $value) { |
|
| 171 | + $smarty->assign($template_var_name, $value); |
|
| 172 | + } |
|
| 159 | 173 | |
| 160 | 174 | if($standalone === false) |
| 161 | 175 | { |
| 162 | 176 | $smarty->display(sprintf('%s|%s', $this->box('settings.smarty.template_inclusion_path'), $template)); |
| 163 | - } |
|
| 164 | - else |
|
| 177 | + } else |
|
| 165 | 178 | { |
| 166 | 179 | $smarty->display($template); |
| 167 | 180 | } |
@@ -205,8 +218,9 @@ discard block |
||
| 205 | 218 | |
| 206 | 219 | while(!is_null($tpl_path = array_shift($templates))) |
| 207 | 220 | { |
| 208 | - if($smarty->templateExists($tpl_path)) |
|
| 209 | - return $tpl_path; |
|
| 221 | + if($smarty->templateExists($tpl_path)) { |
|
| 222 | + return $tpl_path; |
|
| 223 | + } |
|
| 210 | 224 | } |
| 211 | 225 | |
| 212 | 226 | throw new \Exception('KADRO_ERR_NO_TEMPLATE_TO_DISPLAY'); |
@@ -228,8 +242,9 @@ discard block |
||
| 228 | 242 | */ |
| 229 | 243 | public function route_back($route_name=null, $route_params=[]) : string |
| 230 | 244 | { |
| 231 | - if(is_null($route_name)) |
|
| 232 | - return $this->route_back ?? $this->router()->prehop(RouterInterface::ROUTE_HOME_NAME); |
|
| 245 | + if(is_null($route_name)) { |
|
| 246 | + return $this->route_back ?? $this->router()->prehop(RouterInterface::ROUTE_HOME_NAME); |
|
| 247 | + } |
|
| 233 | 248 | |
| 234 | 249 | return $this->route_back = $this->route_factory($route_name, $route_params); |
| 235 | 250 | } |
@@ -240,10 +255,11 @@ discard block |
||
| 240 | 255 | |
| 241 | 256 | if(is_string($route_name) && !empty($route_name)) |
| 242 | 257 | { |
| 243 | - if($this->router()->route_exists($route_name)) |
|
| 244 | - $route = $this->router()->prehop($route_name, $route_params); |
|
| 245 | - else |
|
| 246 | - $route = $route_name; |
|
| 258 | + if($this->router()->route_exists($route_name)) { |
|
| 259 | + $route = $this->router()->prehop($route_name, $route_params); |
|
| 260 | + } else { |
|
| 261 | + $route = $route_name; |
|
| 262 | + } |
|
| 247 | 263 | |
| 248 | 264 | return $route; |
| 249 | 265 | } |
@@ -9,11 +9,13 @@ discard block |
||
| 9 | 9 | { |
| 10 | 10 | private static function compute_field_value($model,$field_name) |
| 11 | 11 | { |
| 12 | - if(method_exists($model,$field_name)) |
|
| 13 | - return $model->$field_name(); |
|
| 12 | + if(method_exists($model,$field_name)) { |
|
| 13 | + return $model->$field_name(); |
|
| 14 | + } |
|
| 14 | 15 | |
| 15 | - if(property_exists($model,$field_name)) |
|
| 16 | - return $model->$field_name; |
|
| 16 | + if(property_exists($model,$field_name)) { |
|
| 17 | + return $model->$field_name; |
|
| 18 | + } |
|
| 17 | 19 | |
| 18 | 20 | return ''; |
| 19 | 21 | } |
@@ -51,74 +53,66 @@ discard block |
||
| 51 | 53 | |
| 52 | 54 | $table = get_class($model)::table(); |
| 53 | 55 | |
| 54 | - if(is_null($table->column($field_name))) |
|
| 55 | - return Form::input($field_name,$field_value,$attributes,$errors); |
|
| 56 | + if(is_null($table->column($field_name))) { |
|
| 57 | + return Form::input($field_name,$field_value,$attributes,$errors); |
|
| 58 | + } |
|
| 56 | 59 | |
| 57 | 60 | $ret = ''; |
| 58 | 61 | |
| 59 | 62 | $field = $table->column($field_name); |
| 60 | 63 | |
| 61 | 64 | |
| 62 | - if(isset($attributes['required']) && $attributes['required'] === false) |
|
| 63 | - unset($attributes['required']); |
|
| 64 | - |
|
| 65 | - elseif(!$field->is_nullable()) |
|
| 66 | - $attributes[] = 'required'; |
|
| 65 | + if(isset($attributes['required']) && $attributes['required'] === false) { |
|
| 66 | + unset($attributes['required']); |
|
| 67 | + } elseif(!$field->is_nullable()) { |
|
| 68 | + $attributes[] = 'required'; |
|
| 69 | + } |
|
| 67 | 70 | |
| 68 | 71 | if($field->is_auto_incremented()) |
| 69 | 72 | { |
| 70 | 73 | $ret .= Form::hidden($field->name(),$field_value); |
| 71 | - } |
|
| 72 | - elseif($field->type()->is_boolean()) |
|
| 74 | + } elseif($field->type()->is_boolean()) |
|
| 73 | 75 | { |
| 74 | 76 | $option_list = $attributes['values'] ?? [0 => 0, 1 => 1]; |
| 75 | 77 | $ret .= Form::select($field->name(), $option_list ,$field_value, $attributes); // |
| 76 | - } |
|
| 77 | - elseif($field->type()->is_integer()) |
|
| 78 | + } elseif($field->type()->is_integer()) |
|
| 78 | 79 | { |
| 79 | 80 | $ret .= Form::input($field->name(),$field_value,$attributes,$errors); |
| 80 | - } |
|
| 81 | - elseif($field->type()->is_year()) |
|
| 81 | + } elseif($field->type()->is_year()) |
|
| 82 | 82 | { |
| 83 | 83 | $attributes['size'] = $attributes['maxlength'] = 4; |
| 84 | 84 | $ret .= Form::input($field->name(),$field_value,$attributes,$errors); |
| 85 | - } |
|
| 86 | - elseif($field->type()->is_date()) |
|
| 85 | + } elseif($field->type()->is_date()) |
|
| 87 | 86 | { |
| 88 | 87 | $ret .= Form::date($field->name(),$field_value,$attributes,$errors); |
| 89 | - } |
|
| 90 | - elseif($field->type()->is_time()) |
|
| 88 | + } elseif($field->type()->is_time()) |
|
| 91 | 89 | { |
| 92 | 90 | $ret .= Form::time($field->name(),$field_value,$attributes,$errors); |
| 93 | - } |
|
| 94 | - elseif($field->type()->is_datetime()) |
|
| 91 | + } elseif($field->type()->is_datetime()) |
|
| 95 | 92 | { |
| 96 | 93 | $ret .= Form::datetime($field->name(),$field_value,$attributes,$errors); |
| 97 | - } |
|
| 98 | - elseif($field->type()->is_text()) |
|
| 94 | + } elseif($field->type()->is_text()) |
|
| 99 | 95 | { |
| 100 | 96 | $ret .= Form::textarea($field->name(),$field_value,$attributes,$errors); |
| 101 | - } |
|
| 102 | - elseif($field->type()->is_enum()) |
|
| 97 | + } elseif($field->type()->is_enum()) |
|
| 103 | 98 | { |
| 104 | 99 | |
| 105 | 100 | $enum_values = []; |
| 106 | - foreach($field->type()->enum_values() as $e_val) |
|
| 107 | - $enum_values[$e_val] = $e_val; |
|
| 101 | + foreach($field->type()->enum_values() as $e_val) { |
|
| 102 | + $enum_values[$e_val] = $e_val; |
|
| 103 | + } |
|
| 108 | 104 | |
| 109 | 105 | $selected = $attributes['value'] ?? $field_value ?? ''; |
| 110 | 106 | // foreach($field->) |
| 111 | 107 | $ret .= Form::select($field->name(), $enum_values, $selected, $attributes); // |
| 112 | 108 | |
| 113 | 109 | // throw new \Exception('ENUM IS NOT HANDLED BY TableToFom'); |
| 114 | - } |
|
| 115 | - elseif($field->type()->is_string()) |
|
| 110 | + } elseif($field->type()->is_string()) |
|
| 116 | 111 | { |
| 117 | 112 | $max_length = $field->type()->length(); |
| 118 | 113 | $attributes['size'] = $attributes['maxlength'] = $max_length; |
| 119 | 114 | $ret .= Form::input($field->name(),$field_value,$attributes,$errors); |
| 120 | - } |
|
| 121 | - else |
|
| 115 | + } else |
|
| 122 | 116 | { |
| 123 | 117 | $ret .= Form::input($field->name(),$field_value,$attributes,$errors); |
| 124 | 118 | } |
@@ -130,8 +124,9 @@ discard block |
||
| 130 | 124 | public static function field_with_label($model,$field_name,$attributes=[],$errors=[]) : string |
| 131 | 125 | { |
| 132 | 126 | $field_attributes = $attributes; |
| 133 | - if(isset($attributes['label'])) |
|
| 134 | - unset($field_attributes['label']); |
|
| 127 | + if(isset($attributes['label'])) { |
|
| 128 | + unset($field_attributes['label']); |
|
| 129 | + } |
|
| 135 | 130 | |
| 136 | 131 | return sprintf('%s %s', self::label($model,$field_name,$attributes,$errors), self::field($model,$field_name,$field_attributes,$errors)); |
| 137 | 132 | } |
@@ -148,14 +143,15 @@ discard block |
||
| 148 | 143 | $form_field = ''; |
| 149 | 144 | if($column->is_auto_incremented()) |
| 150 | 145 | { |
| 151 | - if(!$model->is_new()) |
|
| 152 | - $form_field = self::field($model,$column_name); |
|
| 153 | - } |
|
| 154 | - else |
|
| 146 | + if(!$model->is_new()) { |
|
| 147 | + $form_field = self::field($model,$column_name); |
|
| 148 | + } |
|
| 149 | + } else |
|
| 155 | 150 | { |
| 156 | 151 | $form_field = self::field_with_label($model,$column_name); |
| 157 | - if(!is_null($group_class)) |
|
| 158 | - $form_field = new Element('div',$form_field, ['class' => $group_class]); |
|
| 152 | + if(!is_null($group_class)) { |
|
| 153 | + $form_field = new Element('div',$form_field, ['class' => $group_class]); |
|
| 154 | + } |
|
| 159 | 155 | } |
| 160 | 156 | $ret .= PHP_EOL.$form_field; |
| 161 | 157 | } |
@@ -31,10 +31,10 @@ discard block |
||
| 31 | 31 | return $this->iso_name().' ('.$this->Part3.')'; |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | - public function traceable() : bool |
|
| 35 | - { |
|
| 36 | - return false; |
|
| 37 | - } |
|
| 34 | + public function traceable() : bool |
|
| 35 | + { |
|
| 36 | + return false; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | 39 | public function iso_name() |
| 40 | 40 | { |
@@ -102,9 +102,9 @@ discard block |
||
| 102 | 102 | public static function search_language($term, $authority=null) |
| 103 | 103 | { |
| 104 | 104 | $rows = self::query_retrieve(['term' => $term, 'requires_authority' => $authority])->ret_ass(); |
| 105 | - $ret = []; |
|
| 106 | - foreach($rows as $row) |
|
| 107 | - $ret[$row[self::ISO_3]] = $row[self::ISO_NAME]; |
|
| 105 | + $ret = []; |
|
| 106 | + foreach($rows as $row) |
|
| 107 | + $ret[$row[self::ISO_3]] = $row[self::ISO_NAME]; |
|
| 108 | 108 | |
| 109 | 109 | return $ret; |
| 110 | 110 | } |
@@ -103,8 +103,9 @@ discard block |
||
| 103 | 103 | { |
| 104 | 104 | $rows = self::query_retrieve(['term' => $term, 'requires_authority' => $authority])->ret_ass(); |
| 105 | 105 | $ret = []; |
| 106 | - foreach($rows as $row) |
|
| 107 | - $ret[$row[self::ISO_3]] = $row[self::ISO_NAME]; |
|
| 106 | + foreach($rows as $row) { |
|
| 107 | + $ret[$row[self::ISO_3]] = $row[self::ISO_NAME]; |
|
| 108 | + } |
|
| 108 | 109 | |
| 109 | 110 | return $ret; |
| 110 | 111 | } |
@@ -115,8 +116,10 @@ discard block |
||
| 115 | 116 | $Query->aw_eq(self::ISO_3, $code); |
| 116 | 117 | $rows = $Query->ret_col(); |
| 117 | 118 | |
| 118 | - if(isset($rows[0])) // only 1 result |
|
| 119 | + if(isset($rows[0])) { |
|
| 120 | + // only 1 result |
|
| 119 | 121 | return current($rows); |
| 122 | + } |
|
| 120 | 123 | |
| 121 | 124 | return null; // no results |
| 122 | 125 | } |