-
Notifications
You must be signed in to change notification settings - Fork 0
/
layout.php
164 lines (132 loc) · 3.83 KB
/
layout.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
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Simple CodeIgniter Layout Class
*
* This class enables use of simple layouts in CodeIgniter
*
* @category Libraries
* @author Kiril "NoNameZ" Calkin
* @link https://github.com/nonamez/simple_ci_layout
*/
class Layout{
private $_views;
private $_views_data;
private $_custom_views_data;
private $_CI;
function __construct()
{
$this->_CI =& get_instance();
$this->_views = array(
'head' => 'head',
'before' => array(),
'after' => array(),
'bottom' => 'bottom'
);
$this->_views_data = array(
'head' => array(),
'before' => array(),
'after' => array(),
'bottom' => array()
);
$this->_custom_views_data = array();
}
public function load($views = FALSE, $view_data = NULL, $head_data = FALSE, $bottom_data = FALSE)
{
if ($head_data)
$this->setHeadData($head_data);
if ($bottom_data)
$this->setBottomData($bottom_data);
if ($this->_views['head'])
$this->_showView($this->_views['head'], $this->_views_data['head']);
if (count($this->_views['before']) > 0)
foreach ($this->_views['before'] as $view)
$this->_showView($view, $this->_views_data['before']);
if (is_array($views)) {
foreach ($views as $view)
$this->_showView($view, $view_data);
}
else
$this->_showView($views, $view_data);
if (count($this->_views['after']) > 0)
foreach ($this->_views['after'] as $view)
$this->_showView($view, $this->_views_data['after']);
if ($this->_views['bottom'])
$this->_showView($this->_views['bottom'], $this->_views_data['bottom']);
}
public function setHeadData($data = FALSE)
{
if (is_array($data))
$this->_views_data['head'] = array_merge($this->_views_data['head'], $data);
}
public function setBottomData($data = FALSE)
{
if (is_array($data))
$this->_views_data['bottom'] = array_merge($this->_views_data['bottom'], $data);
}
public function setGlobalData($data = FALSE)
{
if (is_array($data)) {
$this->setHeadData($data);
$this->setBottomData($data);
foreach ($this->_views_data['before'] as &$value)
$value = array_merge($value, $data);
foreach ($this->_views_data['after'] as &$value)
$value = array_merge($value, $data);
}
}
public function setCustomData($view = FALSE, $data = FALSE)
{
if ($view && is_array($data))
$this->_custom_views_data[$view] = $data;
}
public function setDocumentHead($head = FALSE)
{
if ($head)
$this->_views['head'] = (string)$head;
}
public function setDocumentBottom($bottom = FALSE)
{
if ($bottom)
$this->_views['bottom'] = (string)$bottom;
}
public function setBeforeContent($before_content = FALSE)
{
if ($before_content)
$this->_setView('before', (array)$before_content);
}
public function setAfterContent($after_content = FALSE)
{
if ($after_content)
$this->_setView('after', (array)$after_content);
}
private function _setView($condition, $views)
{
// iteration over array of views
foreach ($views as $key => $view_data) {
// checking if the view passed with or without data
if (is_array($view_data))
$view = $key;
else {
$view = $view_data;
$view_data = FALSE;
}
if (!array_key_exists($view, $this->_views[$condition]))
// adding new view
array_push($this->_views[$condition], $view);
// adding view data if exists
if ($view_data) {
// creating view data array if not exists
if (!isset($this->_views_data[$condition][$view]))
$this->_views_data[$condition][$view] = array();
$this->_views_data[$condition][$view] = array_merge($this->_views_data[$condition][$view], (array)$view_data);
}
}
}
private function _showView($view, $view_data)
{
if (isset($this->_custom_views_data[$view]))
$view_data = array_merge($view_data, $this->_custom_views_data[$view]);
$this->_CI->load->view($view, $view_data);
}
}
?>