forked from evolution-cms/evolution
/
topicpath.class.inc.php
288 lines (252 loc) · 9.21 KB
/
topicpath.class.inc.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
<?php
$class_version = '1.0.2';
if(!isset($version) || $version !== $class_version) echo 'TopicPath version error';
class TopicPath
{
function TopicPath()
{
}
function getTopicPath()
{
global $modx;
if($modx->event->params) extract($modx->event->params);
if(!isset($theme)) $theme = 'string';
if(!isset($pathThruUnPub)) $pathThruUnPub = 1;
if(!isset($showInMenuOnly)) $showInMenuOnly = 1;
if(!isset($showCurrentTopic)) $showCurrentTopic = 1;
if(!isset($currentAsLink)) $currentAsLink = 0;
if(!isset($titleField)) $titleField = 'menutitle,pagetitle';
if(!isset($descField)) $descField = 'description,longtitle,pagetitle';
if(!isset($showTopicsAsLinks)) $showTopicsAsLinks = 1;
if(!isset($topicGap)) $topicGap = '...';
if(!isset($showHomeTopic)) $showHomeTopic = 1;
if(!isset($homeId)) $homeId = $modx->config['site_start'];
if(!isset($homeTopicTitle)) $homeTopicTitle = '';
if(!isset($homeTopicDesc)) $homeTopicDesc = '';
if(!isset($showTopicsAtHome)) $showTopicsAtHome = 0;
if(!isset($hideOn)) $hideOn = '';
if(!isset($hideUnder)) $hideUnder = '';
if(!isset($stopIds)) $stopIds = '';
if(!isset($ignoreIds)) $ignoreIds = '';
if(!isset($display)) $display = 100;
if(isset($templateSet)) $theme = $templateSet;
switch(strtolower($theme))
{
case 'list':
case 'li':
case 'defaultlist':
$tpl['outer'] = '<ul class="topicpath">[+topics+]</ul>';
$tpl['first_topic_outer'] = '<li class="first">[+topic+]</li>';
$tpl['last_topic_outer'] = '<li class="last">[+topic+]</li>';
$tpl['other_topic_outer'] = '<li>[+topic+]</li>';
$tpl['topic'] = '[+topic+]';
$tpl['separator'] = '';
break;
case 'raw':
case 'string':
case 'defaultstring':
case 'default':
$tpl['outer'] = '<div class="topicpath">[+topics+]</div>';
$tpl['first_topic_outer'] = '<span class="first">[+topic+]</span>';
$tpl['last_topic_outer'] = '<span class="last">[+topic+]</span>';
$tpl['other_topic_outer'] = '<span>[+topic+]</span>';
$tpl['topic'] = '[+topic+]';
$tpl['separator'] = ' » ';
break;
default:
}
if(isset($tplOuter)) $tpl['outer'] = $tplOuter;
if(isset($tplTopic)) $tpl['topic'] = $tplTopic;
if(isset($tplFirstTopicOuter)) $tpl['first_topic_outer'] = $tplFirstTopicOuter;
if(isset($tplLastTopicOuter)) $tpl['last_topic_outer'] = $tplLastTopicOuter;
if(isset($tplOtherTopicOuter)) $tpl['other_topic_outer'] = $tplOtherTopicOuter;
if(isset($tplSeparator)) $tpl['separator'] = $tplSeparator;
// Return blank if necessary: on home page
if ( !$showTopicsAtHome && $homeId == $modx->documentIdentifier )
{
return '';
}
// Return blank if necessary: specified pages
if ($hideOn || $hideUnder)
{
// Create array of hide pages
$hideOn = str_replace(' ','', $hideOn);
$hideOn = explode(',', $hideOn);
// Get more hide pages based on parents if needed
if ( $hideUnder )
{
$hiddenKids = array();
// Get child pages to hide
$hideKidsQuery = $modx->db->select('id',$modx->getFullTableName('site_content'),"parent IN ({$hideUnder})");
while ($hideKid = $modx->db->getRow($hideKidsQuery))
{
$hiddenKids[] = $hideKid['id'];
}
// Merge with hideOn pages
$hideOn = array_merge($hideOn,$hiddenKids);
}
if (in_array($modx->documentIdentifier,$hideOn))
{
return '';
}
}
// Initialize ------------------------------------------------------------------
// Put certain parameters in arrays
$stopIds = $this->convert_array($stopIds);
$titleField = $this->convert_array($titleField);
$descField = $this->convert_array($descField);
$ignoreIds = $this->convert_array($ignoreIds);
/* $topics
* Topic elements are: id, parent, pagetitle, longtitle, menutitle, description,
* published, hidemenu
*/
$topics = array();
$parent = &$modx->documentObject['parent'];
$output = '';
$display += ($showCurrentTopic) ? 1 : 0;
// Replace || in snippet parameters that accept them with =
$topicGap = str_replace('||','=',$topicGap);
// Curent topic ----------------------------------------------------------------
// Decide if current page is to be a topic
if ( $showCurrentTopic )
{
$topics[] = &$modx->documentObject;
}
// Intermediate topics ---------------------------------------------------------
// Iterate through parents till we hit root or a reason to stop
$loopSafety = 0;
while ( $parent && $parent!=$modx->config['site_start'] && $loopSafety < 1000 )
{
// Get next topic
$doc = $modx->getPageInfo($parent,0,'id,parent,pagetitle,longtitle,menutitle,description,published,hidemenu');
// Check for include conditions & add to topics
if ($doc['published']
&& (!$doc['hidemenu'] || !$showInMenuOnly)
&& !in_array($doc['id'],$ignoreIds))
{
// Add topic
$topics[] = $doc;
}
// Check stop conditions
if (
in_array($doc['id'],$stopIds) // Is one of the stop IDs
|| !$doc['parent'] // At root
|| ( !$doc['published'] && !$pathThruUnPub ) // Unpublished
)
{
break; // Halt making topics
}
$parent = $doc['parent']; // Reset parent
$loopSafety++; // Increment loop safety
}
// Home topic ------------------------------------------------------------------
$homeTopic = $modx->getPageInfo($homeId,0,'id,parent,pagetitle,longtitle,menutitle,description,published,hidemenu');
if($showHomeTopic && $homeId != $modx->documentIdentifier && !empty($homeTopic))
{
$topics[] = $homeTopic;
}
// Process each topic ----------------------------------------------------------
$pretplTopics = array();
foreach ($topics as $row )
{
// Skip if we've exceeded our topic limit but we're waiting to get to home
if (count($pretplTopics) > $display && $row['id'] != $homeId )
{
continue;
}
$text = '';
$title = '';
// Determine appropriate span/link text: home link specified
if ( $row['id'] == $homeId && $homeTopicTitle )
{
$text = $homeTopicTitle;
}
else
// Determine appropriate span/link text: home link not specified
{
for ($i = 0; !$text && $i < count($titleField); $i++)
{
if ( $row[$titleField[$i]] )
{
$text = $row[$titleField[$i]];
}
}
}
// Determine link/span class(es)
if ($row['id'] == $homeId ) $topicClass = 'home';
elseif($modx->documentIdentifier==$row['id']) $topicClass = 'current';
else $topicClass = 'each';
// Make link
if (
( $row['id'] != $modx->documentIdentifier && $showTopicsAsLinks ) ||
( $row['id'] == $modx->documentIdentifier && $currentAsLink )
)
{
// Determine appropriate title for link: home link specified
if ($row['id'] == $homeId && $homeTopicDesc )
{
$title = htmlspecialchars($homeTopicDesc);
}
else
// Determine appropriate title for link: home link not specified
{
for ($i = 0; !$title && $i < count($descField); $i++)
{
if ($row[$descField[$i]] )
{
$title = htmlspecialchars($row[$descField[$i]]);
}
}
}
$url = ($row['id'] == $modx->config['site_start']) ? $modx->config['base_url'] : $modx->makeUrl($row['id']);
$pretplTopics[] = '<a href="' . $url . '" class="' . $topicClass . '" title="' . $title . '">' . $text . '</a>';
}
else
// Make a span instead of a link
{
$pretplTopics[] = '<span class="'.$topicClass.'">'.$text.'</span>';
}
// If we have hit the topic limit
if ( count($pretplTopics) == $display )
{
if ( count($topics) > ($display + (($showHomeTopic) ? 1 : 0)) )
{
// Add gap
$pretplTopics[] = '<span class="hidden">' . $topicGap . '</span>';
}
// Stop here if we're not looking for the home topic
if ( !$showHomeTopic )
{
break;
}
}
}
// Put in correct order for output
$pretplTopics = array_reverse($pretplTopics);
// Insert topics into topic template
$processedTopics = array();
$c = 0;
$last = count($pretplTopics)-1;
foreach ( $pretplTopics as $pc )
{
$_ = str_replace('[+topic+]', $pc, $tpl['topic']);
if($c === 0) $_ = str_replace('[+topic+]', $_, $tpl['first_topic_outer']);
elseif($c === $last) $_ = str_replace('[+topic+]', $_, $tpl['last_topic_outer']);
else $_ = str_replace('[+topic+]', $_, $tpl['other_topic_outer']);
$processedTopics[] = $_;
$c++;
}
// Combine topics together into one string with separator
$processedTopics = implode($tpl['separator'],$processedTopics);
// Put topics into topic container template
$container = str_replace('[+topics+]',$processedTopics,$tpl['outer']);
// Return topics
return $container;
}
function convert_array($str)
{
if($str == '') return array();
$str = str_replace(' ','',$str);
return explode(',',$str);
}
}