1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the NINEJKH/php-tpl library. |
5
|
|
|
* |
6
|
|
|
* (c) 9JKH (Pty) Ltd. <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE file |
9
|
|
|
* that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace NINEJKH\Tpl; |
13
|
|
|
|
14
|
|
|
use Detection\MobileDetect; |
15
|
|
|
|
16
|
|
|
class Helpers |
17
|
|
|
{ |
18
|
|
|
protected static $mobileDetect; |
19
|
|
|
|
20
|
|
|
public static function mobileDetect() |
21
|
|
|
{ |
22
|
|
|
if (self::$mobileDetect === null) { |
23
|
|
|
self::$mobileDetect = new MobileDetect; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
return self::$mobileDetect; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public static function active($menu, $return = false) |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
if (empty($_SERVER['REQUEST_URI'])) { |
32
|
|
|
return; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// normalise request-uri |
36
|
|
|
$request_uri = mb_strtolower(urldecode($_SERVER['REQUEST_URI'])); |
37
|
|
|
if (($pos = strpos($request_uri, '?')) !== false) { |
38
|
|
|
$request_uri = substr($request_uri, 0, $pos); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (preg_match('~' . $menu . '~i', $request_uri)) { |
42
|
|
|
if (!$return) { |
43
|
|
|
echo ' active'; |
44
|
|
|
} else { |
45
|
|
|
return true; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ($return) { |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public static function checked($key, $default) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
// if POST then use it |
57
|
|
|
if (static::posted()) { |
58
|
|
|
if (!empty($_POST[$key])) { |
59
|
|
|
echo ' checked'; |
60
|
|
|
return; |
61
|
|
|
} else { |
62
|
|
|
echo ''; |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// fallback |
68
|
|
|
if ($default) { |
69
|
|
|
echo ' checked'; |
70
|
|
|
return; |
71
|
|
|
} else { |
72
|
|
|
echo ''; |
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public static function selected($key, $value, $default) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
if (static::posted()) { |
80
|
|
|
if (!empty($_POST[$key]) && $_POST[$key] == $value) { |
81
|
|
|
echo ' selected'; |
82
|
|
|
return; |
83
|
|
|
} else { |
84
|
|
|
echo ''; |
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($default == $value) { |
90
|
|
|
echo ' selected'; |
91
|
|
|
return; |
92
|
|
|
} else { |
93
|
|
|
echo ''; |
94
|
|
|
return; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public static function posted() |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
return (!empty($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public static function caseTitle($string) |
104
|
|
|
{ |
105
|
|
|
return mb_convert_case($string, MB_CASE_TITLE, 'UTF-8'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public static function truncate($string, $max_length) |
109
|
|
|
{ |
110
|
|
|
if (mb_strlen($string, 'UTF-8') <= $max_length) { |
111
|
|
|
return $string; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$string = mb_substr($string, 0, ($max_length - 3), 'UTF-8'); |
115
|
|
|
$string .= '...'; |
116
|
|
|
return $string; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public static function showAds() |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
if (($_SERVER['REMOTE_ADDR'] === '::1' || $_SERVER['REMOTE_ADDR'] === '127.0.0.1') && empty($_GET['force_ads'])) { |
122
|
|
|
return false; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return true; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public static function isDev() |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
if ($_SERVER['REMOTE_ADDR'] === '::1' || $_SERVER['REMOTE_ADDR'] === '127.0.0.1' || !empty($_GET['force_dev'])) { |
131
|
|
|
return true; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return false; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public static function isMobile() |
138
|
|
|
{ |
139
|
|
|
if (self::mobileDetect()->isMobile()) { |
140
|
|
|
return true; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return false; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public static function isTablet() |
147
|
|
|
{ |
148
|
|
|
if (self::mobileDetect()->isTablet()) { |
149
|
|
|
return true; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return false; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: