1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* The view class. |
5
|
|
|
* |
6
|
|
|
* Responsible for rendering files as HTML, encode JSON, with some helper methods |
7
|
|
|
* |
8
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
9
|
|
|
* @author Omar El Gabry <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
class View { |
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* controller object that instantiated view object |
16
|
|
|
* |
17
|
|
|
* @var object |
18
|
|
|
*/ |
19
|
|
|
public $controller; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Constructor |
23
|
|
|
* |
24
|
|
|
* @param Controller $controller |
25
|
|
|
*/ |
26
|
|
|
public function __construct(Controller $controller){ |
27
|
|
|
|
28
|
|
|
$this->controller = $controller; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Magic __get method |
33
|
|
|
* |
34
|
|
|
* @param $proporty |
35
|
|
|
*/ |
36
|
|
|
public function __get($property) |
37
|
|
|
{ |
38
|
|
|
if(property_exists(get_class($this->controller), $property)) |
39
|
|
|
{ |
40
|
|
|
return (array_key_exists($property, get_object_vars($this->controller)) ? $this->controller->{$property} : $this->controller::$$property); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
/** |
44
|
|
|
* Renders and returns output for the given file with its array of data. |
45
|
|
|
* |
46
|
|
|
* @param string $filePath |
47
|
|
|
* @param array $data |
48
|
|
|
* @return string Rendered output |
49
|
|
|
* |
50
|
|
|
*/ |
51
|
|
|
public function render($filePath, $data = null){ |
52
|
|
|
|
53
|
|
|
if(!empty($data)) { |
54
|
|
|
extract($data); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Using include Vs require is better, |
58
|
|
|
// because we may want to include a file more than once. |
59
|
|
|
ob_start(); |
60
|
|
|
include $filePath . "" ; |
61
|
|
|
$renderedFile = ob_get_clean(); |
62
|
|
|
|
63
|
|
|
$this->controller->response->setContent($renderedFile); |
64
|
|
|
return $renderedFile; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Renders and returns output with header and footer for the given file with its array of data. |
69
|
|
|
* |
70
|
|
|
* @param string $layoutDir |
71
|
|
|
* @param string $filePath |
72
|
|
|
* @param array $data |
73
|
|
|
* @return string Rendered output |
74
|
|
|
*/ |
75
|
|
|
public function renderWithLayouts($layoutDir, $filePath, $data = null){ |
76
|
|
|
|
77
|
|
|
if(!empty($data)) { |
78
|
|
|
extract($data); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// you can use require_once() immediately without ob_start() & ob_get_clean() |
|
|
|
|
82
|
|
|
// or use ob_start() & ob_get_clean() then return $renderedFile then echo, |
|
|
|
|
83
|
|
|
// but, using ob_start() & ob_get_clean() is a handy way, especially for ajax response. |
84
|
|
|
ob_start(); |
85
|
|
|
require_once $layoutDir . "header.php"; |
86
|
|
|
require_once $filePath . "" ; |
87
|
|
|
require_once $layoutDir . "footer.php"; |
88
|
|
|
$renderedFile = ob_get_clean(); |
89
|
|
|
|
90
|
|
|
$this->controller->response->setContent($renderedFile); |
91
|
|
|
return $renderedFile; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Render a JSON view. |
96
|
|
|
* |
97
|
|
|
* @param array $data |
98
|
|
|
* @return string Rendered output |
99
|
|
|
* |
100
|
|
|
*/ |
101
|
|
|
public function renderJson($data){ |
102
|
|
|
|
103
|
|
|
$jsonData = $this->jsonEncode($data); |
104
|
|
|
|
105
|
|
|
$this->controller->response->type('application/json')->setContent($jsonData); |
106
|
|
|
return $jsonData; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Renders errors |
111
|
|
|
* A json respond will be sent in case of ajax call |
112
|
|
|
* |
113
|
|
|
* @param array $errors |
114
|
|
|
* @return mixed Rendered output |
115
|
|
|
*/ |
116
|
|
View Code Duplication |
public function renderErrors($errors){ |
|
|
|
|
117
|
|
|
|
118
|
|
|
$html = $this->render(Config::get('VIEWS_PATH') . 'alerts/errors.php', ["errors" => $errors]); |
119
|
|
|
|
120
|
|
|
if($this->controller->request->isAjax()){ |
121
|
|
|
return $this->renderJson(array("error" => $html)); |
122
|
|
|
}else{ |
123
|
|
|
$this->controller->response->setContent($html); |
124
|
|
|
return $html; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Renders success message |
130
|
|
|
* A json respond will be sent in case of ajax call |
131
|
|
|
* |
132
|
|
|
* @param string $message |
133
|
|
|
* @return mixed Rendered output |
134
|
|
|
*/ |
135
|
|
View Code Duplication |
public function renderSuccess($message){ |
|
|
|
|
136
|
|
|
|
137
|
|
|
$html = $this->render(Config::get('VIEWS_PATH') . 'alerts/success.php', array("success" => $message)); |
138
|
|
|
|
139
|
|
|
if($this->controller->request->isAjax()){ |
140
|
|
|
return $this->renderJson(array("success" => $html)); |
141
|
|
|
}else{ |
142
|
|
|
$this->controller->response->setContent($html); |
143
|
|
|
return $html; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** *********************************************** **/ |
148
|
|
|
/** ************** JSON View ************** **/ |
149
|
|
|
/** *********************************************** **/ |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Serialize array to JSON and used for the response |
153
|
|
|
* |
154
|
|
|
* @param array $data |
155
|
|
|
* @return string Rendered output |
156
|
|
|
* |
157
|
|
|
*/ |
158
|
|
|
public function jsonEncode($data){ |
159
|
|
|
return json_encode($data); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** *********************************************** **/ |
163
|
|
|
/** ************** Text Helper ************** **/ |
164
|
|
|
/** *********************************************** **/ |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Cuts a string to the length of $length and replaces the last characters |
168
|
|
|
* with the ellipsis => '...' if the text is longer than length. |
169
|
|
|
* |
170
|
|
|
* @param string $str |
171
|
|
|
* @param string $len |
172
|
|
|
* @return string the truncated string |
173
|
|
|
*/ |
174
|
|
|
public function truncate($str, $len){ |
175
|
|
|
|
176
|
|
|
if(empty($str)) { |
177
|
|
|
return ""; |
178
|
|
|
}else if(mb_strlen($str, 'UTF-8') > $len){ |
179
|
|
|
return mb_substr($str, 0, $len, "UTF-8") . " ..."; |
180
|
|
|
}else{ |
181
|
|
|
return mb_substr($str, 0, $len, "UTF-8"); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* formats timestamp string coming from the database to "Month Day, Year" |
187
|
|
|
* |
188
|
|
|
* @param string $timestamp MySQL TIMESTAMP |
189
|
|
|
* @return string Date after formatting. |
190
|
|
|
*/ |
191
|
|
|
public function timestamp($timestamp){ |
192
|
|
|
|
193
|
|
|
$unixTime = strtotime($timestamp); |
194
|
|
|
$date = date("F j, Y", $unixTime); |
195
|
|
|
|
196
|
|
|
// What if date() failed to format? It will return false. |
197
|
|
|
return (empty($date))? "": $date; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* formats Unix timestamp string to "Month Day, Year" |
202
|
|
|
* |
203
|
|
|
* @param integer $unixtime Unix timestamp |
204
|
|
|
* @return string Date after formatting. |
205
|
|
|
*/ |
206
|
|
|
public function unixtime($unixtime){ |
207
|
|
|
|
208
|
|
|
$date = date("F j, Y", intval($unixtime)); |
209
|
|
|
return (empty($date))? "": $date; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* formats Unix timestamp to be used in Date Picker in form of: "day/month/year" |
214
|
|
|
* |
215
|
|
|
* @param integer $unixtime Unix timestamp |
216
|
|
|
* @return string Date after formatting. |
217
|
|
|
*/ |
218
|
|
|
public function datePicker($unixtime){ |
219
|
|
|
|
220
|
|
|
$date = date("d/m/Y", (int)$unixtime); |
221
|
|
|
return (empty($date))? "": $date; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Surround the links in a context with anchor tags. |
226
|
|
|
* |
227
|
|
|
* @param string $str |
228
|
|
|
* @return string |
229
|
|
|
* @see http://stackoverflow.com/questions/5341168/best-way-to-make-links-clickable-in-block-of-text |
230
|
|
|
*/ |
231
|
|
|
public function autoLinks($str){ |
232
|
|
|
return preg_replace('!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9@:%_+.~#?&;//=]+)!i', '<a href="$1" target="_blank">$1</a>', $str); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Converts characters to HTML entities |
237
|
|
|
* This is important to avoid XSS attacks, and attempts to inject malicious code in your page. |
238
|
|
|
* |
239
|
|
|
* @param string $str The string. |
240
|
|
|
* @return string |
241
|
|
|
*/ |
242
|
|
|
public function encodeHTML($str){ |
243
|
|
|
return htmlentities($str, ENT_QUOTES, 'UTF-8'); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* It's same as encodeHTML(), But, also use nl2br() function in PHP |
248
|
|
|
* |
249
|
|
|
* @param string The string. |
250
|
|
|
* @return string The string after converting characters and inserting br tags. |
251
|
|
|
*/ |
252
|
|
|
public function encodeHTMLWithBR($str){ |
253
|
|
|
return nl2br(htmlentities($str, ENT_QUOTES, 'UTF-8')); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
} |
257
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.