-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbreadcrumb.php
300 lines (261 loc) · 6.28 KB
/
breadcrumb.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
<?php
/**
* Created by mjsilva | mail@manueljoaosilva.com
*
* Date: 11-10-2011
* Time: 23:57
*
* @USAGE:
* \Breadcrumb::add_crumb( string [title] , string [link], bool [is home]);
* +-----------------------------------------------------------------------------------+
* + Title | The title of the Crumb it's the anchor text. This got to be unique. +
* +-----------------------------------------------------------------------------------+
* + Link | The Link of the current page, if not provided we'll use \Uri::current() +
* +-----------------------------------------------------------------------------------+
* + is_home | If it's set to true crumbs will be reset and this will be the first. +
* +-----------------------------------------------------------------------------------+
*/
class Breadcrumb {
private static $crumbs = array();
private static $session_name = "breadcrumb";
private static $template = array(
'wrapper_start' => '<ul class="breadcrumb">',
'wrapper_end' => ' </ul>',
'crumb_start' => '<li>',
'crumb_start_active' => '<li class="active">',
'crumb_end' => '</li>',
'divider' => '<span class="divider">/</span>'
);
/**
* Init
*
* Loads in the config and sets the variables
*
* @access public
* @return void
*/
public static function _init()
{
$config = \Config::get('pagination', array());
static::set_config($config);
}
/**
* Set Config
*
* Sets the configuration for BreadCrumb
*
* @access public
* @param array $config The configuration array
* @return void
*/
public static function set_config(array $config)
{
foreach ( $config as $key => $value )
{
if ( $key == 'template' )
{
static::$template = array_merge(static::$template, $config['template']);
continue;
}
static::$
{$key} = $value;
}
static::initialize();
}
/**
* Adds a new crumb to crumbs static property
*
* @param string $title Crumb title
* @param string $link Relative Crumb link
* @return void
*/
public static function add_crumb($title, $link = "", $is_home = false)
{
// trim the bastard
$title = trim($title);
// if link is empty user the current
$link = (empty($link)) ? \Uri::current() : $link;
if ( $is_home )
{
static::set_home_crumb($title, $link);
return true;
}
// set all crumbs inactive
// we will re-set this in a sec
static::set_crumbs_inactive();
// check if the crumb already exists
// in the crumbs array
if ( static::crumb_exists($title) )
{
// it exists, lets rewind to it
static::crumbs_rewind($title);
// set this crumb active
static::set_crumb_active($title);
}
else
{
// it's a new one, lets push it in the array
array_push(static::$crumbs, array("title" => $title, "link" => $link, "active" => true));
}
// save the stuff we did
static::save_crumbs();
}
/**
* Tries to set crumbs static property
*
* @return void
*/
private static function initialize()
{
if ( !empty(static::$crumbs) ) return true;
$session_crumbs = static::get_session_crumbs();
if ( !empty($session_crumbs) )
{
static::$crumbs = $session_crumbs;
}
}
/**
* Get crumbs from session
*
* @return array
*/
private static function get_session_crumbs()
{
return \Session::get(static::$session_name);
}
/**
* Save crumbs in session
*
* @return void
*/
private static function save_crumbs()
{
\Session::set(static::$session_name, static::$crumbs);
}
/**
* Set home crumb and destroy the rest
*
* @param $title
* @param $link
* @return void
*/
private static function set_home_crumb($title, $link)
{
static::reset();
array_push(static::$crumbs, array("title" => $title, "link" => $link, "active" => true));
// save the stuff we did
static::save_crumbs();
}
/**
* Check whether crumb already exists
*
* @static
* @param string $title Crumb title
* @return bool
*/
private static function crumb_exists($title)
{
foreach ( static::$crumbs as $crumb )
{
if ( $title === $crumb["title"] ) return true;
}
return false;
}
/**
* Rewinds crumbs array to the specific title
*
* @static
* @param string $title Crumb title
* @return bool
*/
private static function crumbs_rewind($title)
{
// While we still have crumbs remove the last visited
// until we've reached the one we are currently in
while ( count(static::$crumbs) > 0 )
{
end(static::$crumbs);
// if we are in the current one stop removing them
if ( $title === static::$crumbs[key(static::$crumbs)]["title"] ) break;
// we are still not there continue removing
unset(static::$crumbs[key(static::$crumbs)]);
}
}
/**
* Set all crumbs inactive
*
* @return void
*/
private static function set_crumbs_inactive()
{
foreach ( static::$crumbs as $key => $crumb )
{
static::$crumbs[$key]["active"] = false;
}
}
/**
* Set a specific crumb as active
*
* @param string $title Crumb title
* @return void
*/
private static function set_crumb_active($title)
{
foreach ( static::$crumbs as $key => $crumb )
{
if ( $title === $crumb["title"] )
{
static::$crumbs[$key]["active"] = true;
return true;
}
}
}
/**
* Create Html structure for Crumbs
*
* @return string The html
*/
public static function create_links()
{
if ( empty(static::$crumbs) )
{
return '';
}
$breadcrumb = static::$template["wrapper_start"];
$breadcrumb .= static::crumb_links();
$breadcrumb .= static::$template['wrapper_end'];
return $breadcrumb;
}
/**
* Loop trough the crumbs and build the links
*
* @return string
*/
private static function crumb_links()
{
if ( empty(static::$crumbs) )
{
return '';
}
$breadcrumb = '';
foreach ( static::$crumbs as $k => $crumb )
{
$is_last = ($k === count(static::$crumbs) - 1);
$breadcrumb .= ($is_last) ? static::$template["crumb_start_active"] : static::$template["crumb_start"];
$breadcrumb .= ($is_last) ? $crumb["title"] : \Html::anchor($crumb["link"], $crumb["title"]);
$breadcrumb .= ($is_last) ? "" : static::$template["divider"];
$breadcrumb .= static::$template["crumb_end"];
}
return $breadcrumb;
}
/**
* Resets the breadcrumb to empty state
*
* @return void
*/
public static function reset()
{
static::$crumbs = array();
\Session::delete(static::$session_name);
}
}