-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.php
497 lines (440 loc) · 11.4 KB
/
Controller.php
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<?php if ( ! defined('DENY_ACCESS')) exit('403: No direct file access allowed');
/**
* A Bright CMS
*
* Open source, lightweight, web application framework and content management
* system in PHP.
*
* @package A Bright CMS
* @author Gabriel Liwerant
*/
/**
* Controller Class
*
* Base controller for the entire application.
*
* Holds instance of model and view.
*
* @subpackage core
* @author Gabriel Liwerant
*/
class Controller
{
/**
* Holds an instance of the base model
*
* @var object $_model
*/
protected $_model;
/**
* Holds an instance of the base view
*
* @var object $_view
*/
protected $_view;
/**
* Upon construction we store the model object and the view object.
*
* @param object $model Model object we store and use in the controller
* @param object $view View object we store and use in the controller
*/
public function __construct($model, $view)
{
$this
->_setModel($model)
->_setView($view);
}
/**
* Model setter
*
* @param object $model
*
* @return object Controller
*/
protected function _setModel($model)
{
$this->_model = $model;
return $this;
}
/**
* View setter
*
* @param object $view
*
* @return object Controller
*/
protected function _setView($view)
{
$this->_view = $view;
return $this;
}
/**
* Generic setter for view properties
*
* @param string $property Name of property to set in view
* @param string $data
*
* @return object Controller
*/
protected function _setViewProperty($property, $data)
{
$this->_view->$property = $data;
return $this;
}
/**
* Set the view properties for the rendering of the basic HTML head
* information.
*
* @param string array $head_data HTML document data for head section
*
* @return object Controller
*/
private function _setHeadDoc($head_data)
{
foreach($head_data as $key => $value)
{
$this->_setViewProperty($key, $value);
}
return $this;
}
/**
* Set the view property for the rendering of the head meta tags.
*
* @param string array $head_meta Meta types and values for building
*
* @return object Controller
*/
private function _setHeadMeta($head_meta)
{
$property_data = null;
foreach ($head_meta as $content_type => $content_data)
{
foreach($content_data as $type => $value)
{
$meta_content = $content_type . '=' . $type;
$property_data .= $this->_view->buildHeadMeta($meta_content, $value);
}
}
return $this->_setViewProperty('meta', $property_data);
}
/**
* Set the view property for the head CSS link tags.
*
* @param array $include_data Data with CSS file names and other info
* @param string $cache_buster Optional random string to force re-caching
*
* @return object Controller
*/
private function _setHeadIncludesCss($include_data, $cache_buster)
{
$property_data = null;
foreach ($include_data as $name => $css_data)
{
$property_data .= $this->_view->buildHeadCss($name, $css_data, $cache_buster);
}
return $this->_setViewProperty('css', $property_data);
}
/**
* Set a view property for JavaScript script tags.
*
* @param string $property Name of property to set in view
* @param array $include_data JS file names and other info
* @param string $cache_buster Optional random string to force re-caching
*
* @return object Controller
*/
private function _setJs($property, $include_data, $cache_buster)
{
$property_data = null;
foreach ($include_data as $name => $js_data)
{
$property_data .= $this->_view->buildJs($js_data, $cache_buster);
}
return $this->_setViewProperty($property, $property_data);
}
/**
* Set view property for head links with built HTML.
*
* @param array $link_data
* @param string $cache_buster
*
* @return object Controller
*/
protected function _setHeadIncludesLink($link_data, $cache_buster)
{
$links = null;
foreach ($link_data as $property_name => $data)
{
if (isset($data['attributes']))
{
$attribute_data = $data['attributes'];
}
else
{
$attribute_data = null;
}
$links .= $this->_view->buildHeadLink($data['rel'], $data['href'], (boolean)$data['is_image'], $attribute_data, $cache_buster);
}
return $this->_setViewProperty('head_links', $links);
}
/**
* Setter for the title page view property
*
* @param string $sub_title
* @param string|void $separator Optional separator subpage
*
* @return object Controller
*/
protected function _setTitleSubPage($submenu, $sub_title, $separator = null)
{
if ( ! empty($sub_title))
{
$built_sub_title = $this->_view->buildTitleSubpage($submenu[$sub_title], $separator);
return $this->_setViewProperty('title_subpage', $built_sub_title);
}
else
{
return $this;
}
}
/**
* Set the view property for the rendering of the navigation.
*
* @param string $nav_property
* @param array $nav_data Name and other information for the nav
* @param string|void $separator Optional separator in HTML for nav items
*
* @return object Controller
*/
protected function _setNav($nav_property, $nav_data, $separator = null)
{
$property_data = null;
$i = 1;
foreach ($nav_data as $nav => $data)
{
if ($i === 1)
{
$list_class = 'first';
}
elseif ($i === count($nav_data))
{
$list_class = 'last';
$separator = null;
}
else
{
$list_class= null;
}
if ((boolean)$data['is_anchor'])
{
$nav_item = $this->_view->buildAnchorTag(
$data['text'],
$data['path'],
$data['is_internal'],
$data['target'],
$data['title'],
$data['class'],
$data['id']
);
}
else
{
$nav_item = $data['text'];
}
$property_data .= $this->_view->buildNav($nav_item, $list_class, $separator);
$i++;
}
return $this->_setViewProperty($nav_property, $property_data);
}
/**
* Set sub navigation into view property.
*
* Finds all the content files corresponding to the sub section, loop
* through them to prepare the necessary data for the navigation, and then
* build the navigation items for display.
*
* @param string $sub_directory Directory where the files are
* @param string $api_path Used to build href
* @param string $sub_section_title Current section title to compare
*
* @return object Controller
*/
protected function _setSubNav($sub_directory, $api_path, $sub_section_title)
{
$dir = $this->_model->getStorageTypePath(STORAGE_TYPE) . $sub_directory;
$this->_model->setFilesFromDirectoryIntoStorage($dir, STORAGE_TYPE);
$dir_files_arr = $this->_model->getStorageFilesFromDirectory($dir, STORAGE_TYPE);
$nav_arr_to_build = $this->_model->getSortedSubNavArray($dir_files_arr, $api_path);
$this->_view->sub_nav = null;
foreach ($nav_arr_to_build as $nav)
{
if ($nav['nav'] === $sub_section_title)
{
$class = 'active';
}
else
{
$class = null;
}
$nav_item = $this->_view->buildAnchorTag($nav['nav'], $nav['partial_href'], true, '_self', null, $class);
$this->_view->sub_nav .= $this->_view->buildNav($nav_item, null);
}
return $this;
}
/**
* Set the view property for the rendering of the logo.
*
* @param string $property_name
* @param array $logo_data
*
* @return object Controller
*/
protected function _setLogo($property_name, $logo_data)
{
$logo = $this->_view->buildBrandingLogo(
$logo_data['src'],
$logo_data['alt'],
$logo_data['id']
);
if ((boolean)$logo_data['is_anchor'])
{
$property_data = $this->_view->buildAnchorTag(
$logo,
$logo_data['path'],
(boolean)$logo_data['is_internal'],
$logo_data['target'],
$logo_data['title'],
$logo_data['class'],
$logo_data['id']
);
}
else
{
$property_data = $logo;
}
return $this->_setViewProperty($property_name, $property_data);
}
/**
* Set the view property for the rendering of the fine print.
*
* @param array $fine_print_data Name and other information for footer nav
* @param string|void $separator Optional separator in HTML for nav items
*
* @return object Controller
*/
protected function _setFinePrint($fine_print_data, $separator = null)
{
$property_data = null;
$i = 1;
foreach ($fine_print_data as $nav => $data)
{
if ($nav === 'copyright')
{
$property_data .= $this->_view->buildCopyright(
$data,
$separator,
(boolean)$data['show_current_date']
);
}
else
{
// If we use a separator, make the last one null
if ($i === count($fine_print_data))
{
$separator = null;
}
$property_data .= $this->_view->buildNav($data, null, $separator);
}
$i++;
}
return $this->_setViewProperty('fine_print', $property_data);
}
/**
* Sets the view property for a link list column.
*
* @param array $link_data
* @param integer $max_columns Maximum number of link columns to make
*
* @return object Controller
*/
protected function _setLinkListColumn($link_data, $max_columns)
{
$property_data = null;
$i = 0;
foreach ($link_data as $list_name => $list_data )
{
$i++;
if ($i <= $max_columns)
{
$property_data .= $this->_view->buildLinkListColumn($list_name, $list_data);
}
}
return $this->_setViewProperty('link_section', $property_data);
}
/**
* Allows us to force re-caching on included files like CSS and JS.
*
* @param boolean $is_mode_cache_busting
* @param string|void $preexisting_value Preexisting re-cache value to use
*
* @return string The re-cache string to append to files
*/
protected function _cacheBuster($is_mode_cache_busting, $preexisting_value = null)
{
if ($is_mode_cache_busting)
{
if (empty($preexisting_value))
{
$cache_buster = $this->_model
->setKeyGenerator()
->createStandardKeyFromKeyGenerator('10', array('digital'));
}
else
{
$cache_buster = $preexisting_value;
}
}
else
{
$cache_buster = null;
}
return '?' . $cache_buster;
}
/**
* Call any methods necessary to build out basic page elements and set them
* as view properties for viewing.
*
* @param array $data From storage to build out view properties
* @param string $child_class_name
* @param string $cache_buster Allows us to force re-caching
*
* @return object Controller
*/
protected function _pageBuilder($data, $child_class_name, $cache_buster)
{
$this
->_setHeadDoc($data['head']['head_doc'])
->_setHeadMeta($data['head']['head_meta'])
->_setHeadIncludesCss($data['head']['head_includes']['head_css'], $cache_buster)
->_setJs('head_js', $data['head']['head_includes']['head_js'], $cache_buster)
->_setJs('footer_js', $data['footer']['footer_js'], $cache_buster);
return $this;
}
/**
* Loads the views for our template from stored JSON data.
*
* Call the page builder here to build out basic page elements into view
* properties. Then call the view to render the page along with the basic
* template pages.
*
* @param string $page_name Name of the page we load as the view
*/
public function render($page_name)
{
$this
->_setViewProperty('page', $page_name)
->_view->renderPage($page_name);
}
}
// End of Controller Class
/* EOF system/core/Controller.php */