jespernyhlen /
weather-module
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Get value from GET variable or return default value. |
||
| 4 | * |
||
| 5 | * @param string $key to look for |
||
| 6 | * @param mixed $default value to set if key does not exists |
||
| 7 | * |
||
| 8 | * @return mixed value from GET or the default value |
||
| 9 | */ |
||
| 10 | function getGet($key, $default = null) |
||
| 11 | { |
||
| 12 | return isset($_GET[$key]) |
||
| 13 | ? $_GET[$key] |
||
| 14 | : $default; |
||
| 15 | } |
||
| 16 | |||
| 17 | |||
| 18 | |||
| 19 | // /** |
||
| 20 | // * Get value from POST variable or return default value. |
||
| 21 | // * |
||
| 22 | // * @param string $key to look for |
||
| 23 | // * @param mixed $default value to set if key does not exists |
||
| 24 | // * |
||
| 25 | // * @return mixed value from POST or the default value |
||
| 26 | // */ |
||
| 27 | // function getPost($key, $default = null) |
||
| 28 | // { |
||
| 29 | // return isset($_POST[$key]) |
||
| 30 | // ? $_POST[$key] |
||
| 31 | // : $default; |
||
| 32 | // } |
||
| 33 | |||
| 34 | |||
| 35 | |||
| 36 | /** |
||
| 37 | * Get value from POST variable or return default value. |
||
| 38 | * |
||
| 39 | * @param mixed $key to look for, or value array |
||
| 40 | * @param mixed $default value to set if key does not exists |
||
| 41 | * |
||
| 42 | * @return mixed value from POST or the default value |
||
| 43 | */ |
||
| 44 | function getPost($key, $default = null) |
||
| 45 | { |
||
| 46 | if (is_array($key)) { |
||
| 47 | // $key = array_flip($key); |
||
| 48 | // return array_replace($key, array_intersect_key($_POST, $key)); |
||
| 49 | foreach ($key as $val) { |
||
| 50 | $post[$val] = getPost($val); |
||
| 51 | } |
||
| 52 | return $post; |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 53 | } |
||
| 54 | |||
| 55 | return isset($_POST[$key]) |
||
| 56 | ? $_POST[$key] |
||
| 57 | : $default; |
||
| 58 | } |
||
| 59 | |||
| 60 | |||
| 61 | /** |
||
| 62 | * Check if key is set in POST. |
||
| 63 | * |
||
| 64 | * @param mixed $key to look for |
||
| 65 | * |
||
| 66 | * @return boolean true if key is set, otherwise false |
||
| 67 | */ |
||
| 68 | function hasKeyPost($key) |
||
| 69 | { |
||
| 70 | return array_key_exists($key, $_POST); |
||
| 71 | } |
||
| 72 | |||
| 73 | |||
| 74 | |||
| 75 | /** |
||
| 76 | * Sanitize value for output in view. |
||
| 77 | * |
||
| 78 | * @param string $value to sanitize |
||
| 79 | * |
||
| 80 | * @return string beeing sanitized |
||
| 81 | */ |
||
| 82 | function esc($value) |
||
| 83 | { |
||
| 84 | return htmlentities($value); |
||
| 85 | } |
||
| 86 | |||
| 87 | |||
| 88 | |||
| 89 | /** |
||
| 90 | * Function to create links for sorting. |
||
| 91 | * |
||
| 92 | * @param string $column the name of the database column to sort by |
||
| 93 | * @param string $route prepend this to the anchor href |
||
| 94 | * |
||
| 95 | * @return string with links to order by column. |
||
| 96 | */ |
||
| 97 | function orderby($column, $route) |
||
| 98 | { |
||
| 99 | return <<<EOD |
||
| 100 | <span class="orderby"> |
||
| 101 | <a href="{$route}orderby={$column}&order=asc">↓</a> |
||
| 102 | <a href="{$route}orderby={$column}&order=desc">↑</a> |
||
| 103 | </span> |
||
| 104 | EOD; |
||
| 105 | } |
||
| 106 | |||
| 107 | |||
| 108 | |||
| 109 | /** |
||
| 110 | * Function to create links for sorting and keeping the original querystring. |
||
| 111 | * |
||
| 112 | * @param string $column the name of the database column to sort by |
||
| 113 | * @param string $route prepend this to the anchor href |
||
| 114 | * |
||
| 115 | * @return string with links to order by column. |
||
| 116 | */ |
||
| 117 | function orderby2($column, $route) |
||
| 118 | { |
||
| 119 | $asc = mergeQueryString(["orderby" => $column, "order" => "asc"], $route); |
||
| 120 | $desc = mergeQueryString(["orderby" => $column, "order" => "desc"], $route); |
||
| 121 | |||
| 122 | return <<<EOD |
||
| 123 | <span class="orderby"> |
||
| 124 | <a href="$asc">↓</a> |
||
| 125 | <a href="$desc">↑</a> |
||
| 126 | </span> |
||
| 127 | EOD; |
||
| 128 | } |
||
| 129 | |||
| 130 | |||
| 131 | |||
| 132 | /** |
||
| 133 | * Use current querystring as base, extract it to an array, merge it |
||
| 134 | * with incoming $options and recreate the querystring using the |
||
| 135 | * resulting array. |
||
| 136 | * |
||
| 137 | * @param array $options to merge into exitins querystring |
||
| 138 | * @param string $prepend to the resulting query string |
||
| 139 | * |
||
| 140 | * @return string as an url with the updated query string. |
||
| 141 | */ |
||
| 142 | function mergeQueryString($options, $prepend = "?") |
||
| 143 | { |
||
| 144 | // Parse querystring into array |
||
| 145 | $query = []; |
||
| 146 | parse_str($_SERVER["QUERY_STRING"], $query); |
||
| 147 | |||
| 148 | // Merge query string with new options |
||
| 149 | $query = array_merge($query, $options); |
||
| 150 | |||
| 151 | // Build and return the modified querystring as url |
||
| 152 | return $prepend . http_build_query($query); |
||
| 153 | } |
||
| 154 | |||
| 155 | |||
| 156 | |||
| 157 | /** |
||
| 158 | * Create a slug of a string, to be used as url. |
||
| 159 | * |
||
| 160 | * @param string $str the string to format as slug. |
||
| 161 | * |
||
| 162 | * @return str the formatted slug. |
||
| 163 | */ |
||
| 164 | function slugify($str) |
||
| 165 | { |
||
| 166 | $str = mb_strtolower(trim($str)); |
||
| 167 | $str = str_replace(['å','ä'], 'a', $str); |
||
| 168 | $str = str_replace('ö', 'o', $str); |
||
| 169 | $str = preg_replace('/[^a-z0-9-]/', '-', $str); |
||
| 170 | $str = trim(preg_replace('/-+/', '-', $str), '-'); |
||
| 171 | return $str; |
||
| 172 | } |
||
| 173 |