1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
<?php
/**
* MINZ - Copyright 2011 Marien Fressinaud
* Sous licence AGPL3 <http://www.gnu.org/licenses/>
*/
/**
* La classe View représente la vue de l'application
*/
class Minz_View {
const VIEWS_PATH_NAME = '/views';
const LAYOUT_PATH_NAME = '/layout/';
const LAYOUT_DEFAULT = 'layout';
private $view_filename = '';
private $layout_filename = '';
private static $base_pathnames = array(APP_PATH);
private static $title = '';
private static $styles = array ();
private static $scripts = array ();
private static $params = array ();
/**
* Constructeur
* Détermine si on utilise un layout ou non
*/
public function __construct() {
$this->_layout(self::LAYOUT_DEFAULT);
$conf = Minz_Configuration::get('system');
self::$title = $conf->title;
}
/**
* [deprecated] Change the view file based on controller and action.
*/
public function change_view($controller_name, $action_name) {
Minz_Log::warning('Minz_View::change_view is deprecated, it will be removed in a future version. Please use Minz_View::_path instead.');
$this->_path($controller_name. '/' . $action_name . '.phtml');
}
/**
* Change the view file based on a pathname relative to VIEWS_PATH_NAME.
*
* @param string $path the new path
*/
public function _path($path) {
$this->view_filename = self::VIEWS_PATH_NAME . '/' . $path;
}
/**
* Add a base pathname to search views.
*
* New pathnames will be added at the beginning of the list.
*
* @param $base_pathname the new base pathname.
*/
public static function addBasePathname($base_pathname) {
array_unshift(self::$base_pathnames, $base_pathname);
}
/**
* Construit la vue
*/
public function build () {
if ($this->layout_filename !== '') {
$this->buildLayout ();
} else {
$this->render ();
}
}
/**
* Include a view file.
*
* The file is searched inside list of $base_pathnames.
*
* @param $filename the name of the file to include.
* @return true if the file has been included, false else.
*/
private function includeFile($filename) {
// We search the filename in the list of base pathnames. Only the first view
// found is considered.
foreach (self::$base_pathnames as $base) {
$absolute_filename = $base . $filename;
if (file_exists($absolute_filename)) {
include $absolute_filename;
return true;
}
}
return false;
}
/**
* Construit le layout
*/
public function buildLayout () {
header('Content-Type: text/html; charset=UTF-8');
if (!$this->includeFile($this->layout_filename)) {
Minz_Log::notice('File not found: `' . $this->layout_filename . '`');
}
}
/**
* Affiche la Vue en elle-même
*/
public function render () {
if (!$this->includeFile($this->view_filename)) {
Minz_Log::notice('File not found: `' . $this->view_filename . '`');
}
}
/**
* Ajoute un élément du layout
* @param $part l'élément partial à ajouter
*/
public function partial ($part) {
$fic_partial = self::LAYOUT_PATH_NAME . '/' . $part . '.phtml';
if (!$this->includeFile($fic_partial)) {
Minz_Log::warning('File not found: `' . $fic_partial . '`');
}
}
/**
* Affiche un élément graphique situé dans APP./views/helpers/
* @param $helper l'élément à afficher
*/
public function renderHelper ($helper) {
$fic_helper = '/views/helpers/' . $helper . '.phtml';
if (!$this->includeFile($fic_helper)) {
Minz_Log::warning('File not found: `' . $fic_helper . '`');
}
}
/**
* Retourne renderHelper() dans une chaîne
* @param $helper l'élément à traîter
*/
public function helperToString($helper) {
ob_start();
$this->renderHelper($helper);
return ob_get_clean();
}
/**
* Choose the current view layout.
* @param $layout the layout name to use, false to use no layouts.
*/
public function _layout($layout) {
if ($layout) {
$this->layout_filename = self::LAYOUT_PATH_NAME . $layout . '.phtml';
} else {
$this->layout_filename = '';
}
}
/**
* [deprecated] Choose if we want to use the layout or not.
* Please use the `_layout` function instead.
* @param $use true if we want to use the layout, false else
*/
public function _useLayout ($use) {
Minz_Log::warning('Minz_View::_useLayout is deprecated, it will be removed in a future version. Please use Minz_View::_layout instead.');
if ($use) {
$this->_layout(self::LAYOUT_DEFAULT);
} else {
$this->_layout(false);
}
}
/**
* Gestion du titre
*/
public static function title () {
return self::$title;
}
public static function headTitle () {
return '<title>' . self::$title . '</title>' . "\n";
}
public static function _title ($title) {
self::$title = $title;
}
public static function prependTitle ($title) {
self::$title = $title . self::$title;
}
public static function appendTitle ($title) {
self::$title = self::$title . $title;
}
/**
* Gestion des feuilles de style
*/
public static function headStyle () {
$styles = '';
foreach(self::$styles as $style) {
$cond = $style['cond'];
if ($cond) {
$styles .= '<!--[if ' . $cond . ']>';
}
$styles .= '<link rel="stylesheet" ' .
($style['media'] === 'all' ? '' : 'media="' . $style['media'] . '" ') .
'href="' . $style['url'] . '" />';
if ($cond) {
$styles .= '<![endif]-->';
}
$styles .= "\n";
}
return $styles;
}
public static function prependStyle ($url, $media = 'all', $cond = false) {
array_unshift (self::$styles, array (
'url' => $url,
'media' => $media,
'cond' => $cond
));
}
public static function appendStyle ($url, $media = 'all', $cond = false) {
self::$styles[] = array (
'url' => $url,
'media' => $media,
'cond' => $cond
);
}
/**
* Gestion des scripts JS
*/
public static function headScript () {
$scripts = '';
foreach (self::$scripts as $script) {
$cond = $script['cond'];
if ($cond) {
$scripts .= '<!--[if ' . $cond . ']>';
}
$scripts .= '<script src="' . $script['url'] . '"';
if ($script['defer']) {
$scripts .= ' defer="defer"';
}
if ($script['async']) {
$scripts .= ' async="async"';
}
$scripts .= '></script>';
if ($cond) {
$scripts .= '<![endif]-->';
}
$scripts .= "\n";
}
return $scripts;
}
public static function prependScript ($url, $cond = false, $defer = true, $async = true) {
array_unshift(self::$scripts, array (
'url' => $url,
'cond' => $cond,
'defer' => $defer,
'async' => $async,
));
}
public static function appendScript ($url, $cond = false, $defer = true, $async = true) {
self::$scripts[] = array (
'url' => $url,
'cond' => $cond,
'defer' => $defer,
'async' => $async,
);
}
/**
* Gestion des paramètres ajoutés à la vue
*/
public static function _param ($key, $value) {
self::$params[$key] = $value;
}
public function attributeParams () {
foreach (Minz_View::$params as $key => $value) {
$this->$key = $value;
}
}
}
|