1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
---|
3 | # This file is part of multiToc, a plugin for Dotclear. |
---|
4 | # |
---|
5 | # Copyright (c) 2009-2010 Tomtom and contributors |
---|
6 | # http://blog.zenstyle.fr/ |
---|
7 | # |
---|
8 | # Licensed under the GPL version 2.0 license. |
---|
9 | # A copy of this license is available in LICENSE file or at |
---|
10 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
11 | # -- END LICENSE BLOCK ------------------------------------ |
---|
12 | |
---|
13 | class multiTocPost |
---|
14 | { |
---|
15 | protected $p_h = '/<h([1-6])>(.*)<\/h\\1>/'; |
---|
16 | protected $p_t = '/<p>::TOC::<\/p>/'; |
---|
17 | protected $p_r_a = '<a href="%1$s#%2$s">%3$s</a>'; |
---|
18 | protected $p_r_h = '<h%1$s%2$s>%3$s</h%1$s>'; |
---|
19 | protected $p_r_t = '<div class="post-toc">%s</div>'; |
---|
20 | protected $tree = array(); |
---|
21 | protected $num = array(); |
---|
22 | protected $count = false; |
---|
23 | protected $rs; |
---|
24 | |
---|
25 | public function __construct($rs) |
---|
26 | { |
---|
27 | $s = unserialize($rs->core->blog->settings->multiToc->multitoc_settings); |
---|
28 | |
---|
29 | $this->rs = $rs; |
---|
30 | $this->count = $s['post']['numbering']; |
---|
31 | $this->getTree(); |
---|
32 | } |
---|
33 | |
---|
34 | public function process($c) |
---|
35 | { |
---|
36 | if (preg_match($this->p_t,$c)) { |
---|
37 | $c = preg_replace($this->p_t,sprintf($this->p_r_t,$this->getToc()),$c); |
---|
38 | } |
---|
39 | |
---|
40 | $c = preg_replace_callback($this->p_h,array($this,'replaceTitles'),$c); |
---|
41 | |
---|
42 | return $c; |
---|
43 | } |
---|
44 | |
---|
45 | protected function getTree() |
---|
46 | { |
---|
47 | preg_match_all($this->p_h,$this->rs->post_excerpt_xhtml.$this->rs->post_content_xhtml,$matches); |
---|
48 | |
---|
49 | $levels = $matches[1]; |
---|
50 | $titles = $matches[2]; |
---|
51 | $offset = min($levels); |
---|
52 | $key = array('','','','',''); |
---|
53 | $count = array(0,0,0,0,0,0); |
---|
54 | |
---|
55 | foreach ($levels as $k => $v) { |
---|
56 | $title = $titles[$k]; |
---|
57 | |
---|
58 | if ($title === 'Notes') { continue; } |
---|
59 | |
---|
60 | $dim = $v - $offset; |
---|
61 | |
---|
62 | switch ($dim) { |
---|
63 | case 0: |
---|
64 | $key[0] = $title; |
---|
65 | $this->tree[$title] = null; |
---|
66 | $this->num[$title] = count($this->tree); |
---|
67 | break; |
---|
68 | case 1: |
---|
69 | $key[1] = $title; |
---|
70 | $this->tree[$key[0]][$title] = null; |
---|
71 | $this->num[$title] = |
---|
72 | count($this->tree).'.'. |
---|
73 | count($this->tree[$key[0]]); |
---|
74 | break; |
---|
75 | case 2: |
---|
76 | $key[2] = $title; |
---|
77 | $this->tree[$key[0]][$key[1]][$title] = null; |
---|
78 | $this->num[$title] = |
---|
79 | count($this->tree).'.'. |
---|
80 | count($this->tree[$key[0]]).'.'. |
---|
81 | count($this->tree[$key[0]][$key[1]]); |
---|
82 | break; |
---|
83 | case 3: |
---|
84 | $key[3] = $title; |
---|
85 | $this->tree[$key[0]][$key[1]][$key[2]][$title] = null; |
---|
86 | $this->num[$title] = |
---|
87 | count($this->tree).'.'. |
---|
88 | count($this->tree[$key[0]]).'.'. |
---|
89 | count($this->tree[$key[0]][$key[1]]).'.'. |
---|
90 | count($this->tree[$key[0]][$key[1]][$key[2]]); |
---|
91 | break; |
---|
92 | case 4: |
---|
93 | $key[4] = $title; |
---|
94 | $this->tree[$key[0]][$key[1]][$key[2]][$key[3]][$title] = null; |
---|
95 | $this->num[$title] = |
---|
96 | count($this->tree).'.'. |
---|
97 | count($this->tree[$key[0]]).'.'. |
---|
98 | count($this->tree[$key[0]][$key[1]]).'.'. |
---|
99 | count($this->tree[$key[0]][$key[1]][$key[2]]).'.'. |
---|
100 | count($this->tree[$key[0]][$key[1]][$key[2]][$key[3]]); |
---|
101 | break; |
---|
102 | case 5: |
---|
103 | $this->tree[$key[0]][$key[1]][$key[2]][$key[3]][$key[4]][$title] = null; |
---|
104 | $this->num[$title] = |
---|
105 | count($this->tree).'.'. |
---|
106 | count($this->tree[$key[0]]).'.'. |
---|
107 | count($this->tree[$key[0]][$key[1]]).'.'. |
---|
108 | count($this->tree[$key[0]][$key[1]][$key[2]]).'.'. |
---|
109 | count($this->tree[$key[0]][$key[1]][$key[2]][$key[3]]).'.'. |
---|
110 | count($this->tree[$key[0]][$key[1]][$key[2]][$key[3]][$key[4]]); |
---|
111 | break; |
---|
112 | } |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | protected function getToc($tree = null) |
---|
117 | { |
---|
118 | $res = array(); |
---|
119 | |
---|
120 | if (is_null($tree)) { |
---|
121 | $tree = $this->tree; |
---|
122 | } |
---|
123 | |
---|
124 | foreach ($tree as $title => $child) |
---|
125 | { |
---|
126 | $url = $this->rs->getURL().'#'.text::tidyURL($title); |
---|
127 | if ($this->count) { |
---|
128 | if (array_key_exists($title,$this->num)) { |
---|
129 | $title = sprintf('%s: %s',$this->num[$title],$title); |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | $link = sprintf('<a href="%1$s">%2$s</a>',$url,$title); |
---|
134 | |
---|
135 | if (is_array($child)) { |
---|
136 | $link .= $this->getToc($child); |
---|
137 | } |
---|
138 | array_push($res,sprintf('<li>%s</li>',$link)); |
---|
139 | } |
---|
140 | |
---|
141 | return sprintf('<ul>%s</ul>',implode("\n",$res)); |
---|
142 | } |
---|
143 | |
---|
144 | protected function replaceTitles($matches) |
---|
145 | { |
---|
146 | $num = array_key_exists($matches[2],$this->num) ? $this->num[$matches[2]] : ''; |
---|
147 | |
---|
148 | return sprintf( |
---|
149 | '<h%1$s%2$s>%3$s</h%1$s>',$matches[1], |
---|
150 | ' id="'.text::tidyURL($matches[2]).'"', |
---|
151 | sprintf( |
---|
152 | (array_key_exists($matches[2],$this->num) && $this->count ? '%2$s: %1$s' : '%1$s'), |
---|
153 | $matches[2],$num |
---|
154 | ) |
---|
155 | ); |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | class multiTocUi |
---|
160 | { |
---|
161 | public static function form($type = 'cat') |
---|
162 | { |
---|
163 | global $core; |
---|
164 | |
---|
165 | $order_entry_data = array( |
---|
166 | __('Title up') => 'post_title asc', |
---|
167 | __('Title down') => 'post_title desc', |
---|
168 | __('Date up') => 'post_dt asc', |
---|
169 | __('Date down') => 'post_dt desc', |
---|
170 | __('Author up') => 'user_id asc', |
---|
171 | __('Author down') => 'user_id desc', |
---|
172 | __('Comments number up') => 'nb_comment asc', |
---|
173 | __('Comments number down') => 'nb_comment desc', |
---|
174 | __('Trackbacks number up') => 'nb_trackback asc', |
---|
175 | __('Trackbacks number down') => 'nb_trackback desc' |
---|
176 | ); |
---|
177 | |
---|
178 | switch($type) |
---|
179 | { |
---|
180 | case 'tag': |
---|
181 | $legend = __('TOC by tags'); |
---|
182 | $enable = __('Enable TOC by tags'); |
---|
183 | $order_group = __('Order of tags'); |
---|
184 | $order_entry = __('Order of entries'); |
---|
185 | $order_group_data = array( |
---|
186 | __('Name up') => 'asc', |
---|
187 | __('Name down') => 'desc', |
---|
188 | ); |
---|
189 | break; |
---|
190 | case 'alpha': |
---|
191 | $legend = __('TOC by alpha list'); |
---|
192 | $enable = __('Enable TOC by alpha list'); |
---|
193 | $order_group = __('Order of alpha list'); |
---|
194 | $order_entry = __('Order of entries'); |
---|
195 | $order_group_data = array( |
---|
196 | __('Alpha up') => 'post_letter asc', |
---|
197 | __('Alpha down') => 'post_letter desc', |
---|
198 | ); |
---|
199 | break; |
---|
200 | case 'post': |
---|
201 | $legend = __('Post TOC'); |
---|
202 | $enable = __('Enable post TOC'); |
---|
203 | $numbering = __('Auto numbering'); |
---|
204 | break; |
---|
205 | default: |
---|
206 | $legend = __('TOC by category'); |
---|
207 | $enable = __('Enable TOC by category'); |
---|
208 | $order_group = __('Order of categories'); |
---|
209 | $order_entry = __('Order of entries'); |
---|
210 | $order_group_data = array( |
---|
211 | __('No option') => '', |
---|
212 | ); |
---|
213 | break; |
---|
214 | } |
---|
215 | |
---|
216 | if ($type !== 'post') { |
---|
217 | $res = |
---|
218 | '<fieldset>'. |
---|
219 | '<legend>'.$legend.'</legend>'. |
---|
220 | '<div class="two-cols"><div class="col">'. |
---|
221 | '<p><label for="'.$type.'_enable" class="classic">'. |
---|
222 | form::checkbox($type.'_enable',1,multiTocUi::getSetting($type,'enable')).$enable. |
---|
223 | '</label></p>'. |
---|
224 | '<p><label for="'.$type.'_display_nb_entry" class="classic">'. |
---|
225 | form::checkbox($type.'_display_nb_entry',1,multiTocUi::getSetting($type,'display_nb_entry')). |
---|
226 | __('Display entry number of each group'). |
---|
227 | '</label></p>'. |
---|
228 | '<p><label for="'.$type.'_display_date" class="classic">'. |
---|
229 | form::checkbox($type.'_display_date',1,multiTocUi::getSetting($type,'display_date')). |
---|
230 | __('Display date'). |
---|
231 | '</label></p>'. |
---|
232 | '<p><label for="'.$type.'_display_author" class="classic">'. |
---|
233 | form::checkbox($type.'_display_author',1,multiTocUi::getSetting($type,'display_author')). |
---|
234 | __('Display author'). |
---|
235 | '</label></p>'. |
---|
236 | '<p><label for="'.$type.'_display_cat" class="classic">'. |
---|
237 | form::checkbox($type.'_display_cat',1,multiTocUi::getSetting($type,'display_cat')). |
---|
238 | __('Display category'). |
---|
239 | '</label></p>'. |
---|
240 | '<p><label for="'.$type.'_display_nb_com" class="classic">'. |
---|
241 | form::checkbox($type.'_display_nb_com',1,multiTocUi::getSetting($type,'display_nb_com')). |
---|
242 | __('Display comment number'). |
---|
243 | '</label></p>'. |
---|
244 | '<p><label for="'.$type.'_display_nb_tb" class="classic">'. |
---|
245 | form::checkbox($type.'_display_nb_tb',1,multiTocUi::getSetting($type,'display_nb_tb')). |
---|
246 | __('Display trackback number'). |
---|
247 | '</label></p>'. |
---|
248 | '<p><label for="'.$type.'_display_tag" class="classic">'. |
---|
249 | form::checkbox($type.'_display_tag',1,multiTocUi::getSetting($type,'display_tag')). |
---|
250 | __('Display tags'). |
---|
251 | '</label></p>'. |
---|
252 | '</div><div class="col">'. |
---|
253 | '<p><label for="'.$type.'_order_group">'. |
---|
254 | $order_group. |
---|
255 | form::combo(array($type.'_order_group'),$order_group_data,multiTocUi::getSetting($type,'order_group')). |
---|
256 | '</label></p>'. |
---|
257 | '<p><label for="'.$type.'_order_entry">'. |
---|
258 | $order_entry. |
---|
259 | form::combo(array($type.'_order_entry'),$order_entry_data,multiTocUi::getSetting($type,'order_entry')). |
---|
260 | '</label></p>'. |
---|
261 | '<p><label for="'.$type.'_format_date">'. |
---|
262 | __('Format date'). |
---|
263 | form::field($type.'_format_date',40,255,multiTocUi::getSetting($type,'format_date')). |
---|
264 | '</label></p>'. |
---|
265 | '</div></div>'. |
---|
266 | '</fieldset>' ; |
---|
267 | } |
---|
268 | else { |
---|
269 | $res = |
---|
270 | '<fieldset>'. |
---|
271 | '<legend>'.$legend.'</legend>'. |
---|
272 | '<p><label for="'.$type.'_enable" class="classic">'. |
---|
273 | form::checkbox($type.'_enable',1,multiTocUi::getSetting($type,'enable'),null,null,!$core->plugins->moduleExists('stacker')).$enable. |
---|
274 | '</label></p>'. |
---|
275 | '<p><label for="'.$type.'_numbering" class="classic">'. |
---|
276 | form::checkbox($type.'_numbering',1,multiTocUi::getSetting($type,'numbering'),null,null,!$core->plugins->moduleExists('stacker')).$numbering. |
---|
277 | '</label></p>'. |
---|
278 | '<p class="form-note">'.__('Those options require stacker plugin').'</p>'. |
---|
279 | '</fieldset>'; |
---|
280 | |
---|
281 | } |
---|
282 | |
---|
283 | return $res; |
---|
284 | } |
---|
285 | |
---|
286 | public static function getSetting($type,$value) |
---|
287 | { |
---|
288 | global $core; |
---|
289 | |
---|
290 | $settings = unserialize($core->blog->settings->multiToc->multitoc_settings); |
---|
291 | |
---|
292 | return isset($settings[$type][$value]) ? $settings[$type][$value] : ''; |
---|
293 | } |
---|
294 | } |
---|
295 | |
---|
296 | ?> |
---|