-
Notifications
You must be signed in to change notification settings - Fork 0
/
crumbz.class.php
211 lines (189 loc) · 7.48 KB
/
crumbz.class.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
<?php
/**
* File crumbz.class.php (requires MODx Revolution 2.x)
* Created on Jul 02, 2010
* Project shawn_wilkerson
* @package crumbz
* @version 1.0
* @category navigation
* @author W. Shawn Wilkerson
* @link http://www.shawnWilkerson.com
* @copyright Copyright (c) 2010, W. Shawn Wilkerson. All rights reserved.
* @license GPL
*
*
* This is the main file to access the breadcrumb class.
*
*/
if (!class_exists(Crumbz)) {
class Crumbz {
function __construct(modX & $modx, array $config = array()) {
/*
* Import $modx reference
*/
$this->modx = & $modx;
/*
* establish site start
*/
$this->siteStartID = $this->modx->getOption('site_start');
/*
* Establish current resource ID
*/
$this->docID = $this->modx->resource->get('id');
/*
* Establish base settings -- merging incoming overrides
*/
$this->_config = array_merge(array(
'id' => $this->docID,
'depth' => 10,
'format' => 'bar',
'seperator' => ' » ',
'textOnly' => false,
'removeSiteHome' => false,
'removeLastID' => false,
'lastChildAsLink' => false,
'useLongTitle' => false,
'useSeperatorWithList' => false,
'reverse' => false,
), $config);
/*
* establish parents of user provided document
*/
$this->parents = $this->modx->getParentIds($this->_config['id'], $this->_config['depth']);
/*
* Reverse array to top-down order
*/
$this->parents = array_reverse($this->parents);
}
/**
* clear resources
*/
function __destruct() {
unset ($this->modx, $this->_config, $this->parents, $this->setHome, $this->lastCrumb, $this->getNavigation, $this->links, $this->setFormat);
}
/**
* Processes the request
* @return html formated string of navigation links
*/
public function run() {
$this->setHome();
$this->setLastCrumb();
$this->parents = array_unique($this->parents);
$this->setdirection();
$this->links = $this->getNavigation();
return $this->setFormat();
}
/**
* Gets all the resource id's in the current path or of docID provided
* @return navigation links for current page
*/
public function getNavigation() {
$useText = ($this->_config['useLongTitle'] == true) ? 'longtitle' : 'pagetitle';
foreach ($this->parents as $resourceID)
{
$obj = $this->modx->getObject('modDocument', array(
'id' => $resourceID
));
/**
* Only process objects
*/
if (is_object($obj)) {
/*
* Stop processing on deleted documents as the tree will also be deleted
*/
if ($obj->get('deleted') == false) {
/*
* Show only published documents and those not hidden from menus
*/
if (($obj->get('published') == true) && ($obj->get('hidemenu') == false)) {
/*
* Capture text to be used for link / text
*/
$linkText = $obj->get($useText);
/*
* Match the title text to the respective linkText.
* pageTitle gets longTitle as Title="" text
* longTitle gets description as Title="" text
*/
$linkTitle = ($this->_config['useLongTitle'] == true) ? $obj->get('description')
: $obj->get('longtitle');
$o[] = ($this->_config['textOnly'] == true) ? $linkText
: '<a href="' . $this->modx->makeUrl($resourceID) . '" title="' . $linkTitle . '">' . $linkText . '</a>';
}
}
}
}
/*
* Replace the last link with straight text
*/
if ($this->_config['lastChildAsLink'] == false) {
array_pop($o);
$o[] = $linkText;
}
/*
* clear resources
*/
unset ($useText, $resourceID, $obj, $linkText, $linkTitle);
return $o;
}
/**
* Reverse the array order based on the reverse parameter: removes the array key.
*/
public function setDirection() {
if ($this->_config['reverse'] == true) {
$this->parents = array_reverse($this->parents);
}
}
/**
* Sets the format of the breadcrumbs to be returned.
* @return string of html content.
*/
public function setFormat() {
$c = 0;
$o = '';
$numLinks = count($this->links);
foreach ($this->links as $location)
{
switch ($this->_config['format'])
{
case 'list' :
$o .= '<li>' . $location;
$o .= ($this->_config['useSeperatorWithList'] && ($numLinks !== ++$c)) ? $this->_config['seperator'] : '';
$o .= '</li>';
break;
case 'vertical' :
$o .= $location . '<br />';
break;
case 'bar' :
default :
$o .= $location;
$o .= ($numLinks === ++$c) ? '' : $this->_config['seperator'];
break;
}
}
return $o;
}
/**
* Establishes the current site home and adjusts the $this->parents array to the site_start
* Based on the removeSiteHome parameter: removes the array key.
* @return int | empty .
*/
public function setHome() {
$this->parents[0] = $this->siteStartID;
if ($this->_config['removeSiteHome'] == true) {
unset($this->parents[0]);
$this->parents = array_values($this->parents);
}
}
/**
* Based on the removeLastChild parameter
* @returns array filtered array key of $this->parents removing duplicates
*/
public function setLastCrumb() {
if (($this->_config['id'] != $this->siteStartID) && ($this->_config['removeLastID'] == false)) {
$this->parents[] = $this->_config['id'];
}
}
}
}
?>