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