-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBreadcrumbs.php
237 lines (212 loc) · 6.36 KB
/
Breadcrumbs.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
<?php
/**
* @author Se#
* @version 0.0.2
* @description Realize auto-breadcrumbs
*/
class Evil_Breadcrumbs extends Zend_Controller_Plugin_Abstract
{
/**
* @description contain all paths in ->pages
* @var Zend_Session_Namespace
* @author Se#
* @version 0.0.1
*/
protected static $_pages = null;
/**
* @description contain breadcrumbs configuration
* @var array
* @author Se#
* @version 0.0.1
*/
protected static $_config = array();
/**
* @description add page to the paths if it is not in paths yet
* @param Zend_Controller_Request_Abstract $request
* @return void
* @author Se#
* @version 0.0.2
*/
public function routeShutdown(Zend_Controller_Request_Abstract $request)
{
$this->addPage($request->getControllerName(),$request->getActionName());
}
/**
* @description return self config
* @static
* @return array
* @author Se#
* @version 0.0.1
*/
public static function getConfig()
{
return self::$_config;
}
/**
* @description return self pages-object
* @static
* @return null|Zend_Session_Namespace
* @author Se#
* @version 0.0.1
*/
public static function getPages()
{
return self::$_pages;
}
/**
* @description define is the current controller appendix
* @param string $controller
* @return bool
* @author Se#
* @version 0.0.1
*/
public function isAppendix($controller)
{
$cfg = self::$_config;
if(isset($cfg['controller'][$controller])
&& is_array($cfg['controller'][$controller])
&& isset($cfg['controller'][$controller]['appendTo'])
)
return $cfg['controller'][$controller]['appendTo'];
return false;
}
/**
* @description add page to the paths
* @param string $controller
* @param string $action
* @param bool $get
* @return bool
* @author Se#
* @version 0.0.1
*/
public function addPage($controller, $action, $get = false)
{
if(!self::$_pages)
$this->initialize();
// If exist controller in pages
if(isset(self::$_pages->pages[$controller]))
{// if action already exist
if(isset(self::$_pages->registry[$controller.$action]))
return true;
else
{
list($controllerLabel, $actionLabel) = $this->getLabel($controller, $action);
self::$_pages->pages[$controller]['pages'][] = array(
'controller' => $controller,
'action' => $action,
'label' => $actionLabel
);
self::$_pages->registry[$controller.$action] = $actionLabel;
}
}
else
{
list($label, $actionLabel) = $this->getLabel($controller, $action);
self::$_pages->registry[$controller.$action] = $actionLabel;
self::$_pages->pages[$controller] = array(
'controller' => $controller,
'action' => 'index',
'label' => $label,
'pages' => $this->_pagesConfig($controller, $action, $actionLabel)
);
}
}
/**
* @description define is sub-pages needed
* @param string $controller
* @param string $action
* @param string $actionLabel
* @return array
* @author Se#
* @version 0.0.1
*/
protected function _pagesConfig($controller, $action, $actionLabel)
{
$pagesConfig = array(
array(
'controller' => $controller,
'action' => $action,
'label' => $actionLabel,
'pages' => array()
));
if('index' == $action)
$pagesConfig = array();
return $pagesConfig;
}
/**
* @description get label for current controller and action
* @param string $controller
* @param string $action
* @return array
* @author Se#
* @version 0.0.1
*/
public function getLabel($controller, $action)
{
$actionLabel = '';
if(isset(self::$_config['controller'][$controller]))
{
if(is_array(self::$_config['controller'][$controller]))
{
if(isset(self::$_config['controller'][$controller]['label']))
$label = self::$_config['controller'][$controller]['label'];
else
$label = ucfirst($controller);
if(isset(self::$_config['controller'][$controller][$action]))
$actionLabel = _(self::$_config['controller'][$controller][$action]);
}
else
$label = self::$_config['controller'][$controller];
}
else
$label = ucfirst($controller);
if(empty($actionLabel))
{
if(isset(self::$_config['action'][$action]))
$actionLabel = self::$_config['action'][$action];
else
$actionLabel = ucfirst($action);
}
return array($label, $actionLabel);
}
/**
* @description initialize self pages-object and config
* @param bool $pages
* @param bool $config
* @return void
* @author Se#
* @version 0.0.2
*/
public function initialize($pages = true, $config = true)
{
if($pages)
{
self::$_pages = new Zend_Session_Namespace('evil-pages');
self::$_pages->pages = array();
}
if($config)
{
if(is_file(APPLICATION_PATH . '/configs/breadcrumbs.json'))
self::$_config = json_decode(file_get_contents(APPLICATION_PATH . '/configs/breadcrumbs.json'), true);
else
self::$_config = json_decode(file_get_contents(__DIR__ . '/Breadcrumbs/default.json'), true);
}
}
/**
* @description return breadcrumbs
* @return null|Zend_Navigation
* @author Se#
* @version 0.0.2
*/
public function breadcrumbs()
{
if(!empty(self::$_pages))
{
$pages = self::$_pages->pages;
// Ñîçäàåì íîâûé êîíòåéíåð íà îñíîâå íàøåé ñòðóêòóðû
$container = new Zend_Navigation($pages);
return $container;
}
return null;
}
}