Dotclear

source: plugins/simpleMenu/index.php @ 556:391a00fa22a6

Revision 556:391a00fa22a6, 17.7 KB checked in by Franck <carnet.franck.paul@…>, 14 years ago (diff)

Commentaires et behaviours supplémentaires

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
6# Copyright (c) 2003-2011 Olivier Meunier & Association Dotclear
7# Licensed under the GPL version 2.0 license.
8# See LICENSE file or
9# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10#
11# -- END LICENSE BLOCK -----------------------------------------
12if (!defined('DC_CONTEXT_ADMIN')) { return; }
13
14dcPage::check('admin');
15
16$page_title = __('Simple menu');
17
18# Url de base
19$p_url = 'plugin.php?p=simpleMenu';
20
21# Url du blog
22$blog_url = html::stripHostURL($core->blog->url);
23
24# Liste des catégories
25$categories_combo = array();
26$categories_label = array();
27try {
28     $rs = $core->blog->getCategories(array('post_type'=>'post'));
29     while ($rs->fetch()) {
30          $categories_combo[] = new formSelectOption(
31               str_repeat('&nbsp;&nbsp;',$rs->level-1).($rs->level-1 == 0 ? '' : '&bull; ').html::escapeHTML($rs->cat_title),
32               $rs->cat_url
33          );
34          $categories_label[$rs->cat_url] = html::escapeHTML($rs->cat_title);
35     }
36} catch (Exception $e) { }
37
38# Liste des langues utilisées
39$langs_combo = array();
40try {
41     $rs = $core->blog->getLangs(array('order'=>'asc'));
42     if ($rs->count() > 1)
43     {
44          $all_langs = l10n::getISOcodes(0,1);
45          while ($rs->fetch()) {
46               $lang_name = isset($all_langs[$rs->post_lang]) ? $all_langs[$rs->post_lang] : $rs->post_lang;
47               $langs_combo[$lang_name] = $rs->post_lang;
48          }
49          unset($all_langs);
50     }
51     unset($rs);
52} catch (Exception $e) { }
53
54# Liste des mois d'archive
55$months_combo = array();
56try {
57     $rs = $core->blog->getDates(array('type'=>'month'));
58     $months_combo[__('All months')] = '-';
59     $first_year = $last_year = 0;
60     while ($rs->fetch()) {
61          $months_combo[dt::str('%B %Y',$rs->ts())] = $rs->year().$rs->month();
62          if (($first_year == 0) || ($rs->year() < $first_year)) $first_year = $rs->year();
63          if (($last_year == 0) || ($rs->year() > $last_year)) $last_year = $rs->year();
64     }
65     unset($rs);
66} catch (Exception $e) { }
67
68# Liste des pages -- Doit être pris en charge plus tard par le plugin ?
69$pages_combo = array();
70try {
71     $rs = $core->blog->getPosts(array('post_type'=>'page'));
72     while ($rs->fetch()) {
73          $pages_combo[$rs->post_title] = $rs->getURL();
74     }
75     unset($rs);
76} catch (Exception $e) { }
77
78# Liste des tags -- Doit être pris en charge plus tard par le plugin ?
79$tags_combo = array();
80try {
81     $rs = $core->meta->getMetadata(array('meta_type' => 'tag'));
82     $tags_combo[__('All tags')] = '-';
83     while ($rs->fetch()) {
84          $tags_combo[$rs->meta_id] = $rs->meta_id;
85     }
86     unset($rs);
87} catch (Exception $e) { }
88
89# Liste des types d'item de menu
90$items = array('home' => array(__('Home'),false));
91
92if (count($langs_combo) > 1) {
93     $items['lang'] = array(__('Language'),true);
94}
95if (count($categories_combo)) {
96     $items['category'] = array(__('Category'),true);
97}
98if (count($months_combo) > 1) {
99     $items['archive'] = array(__('Archive'),true);
100}
101if ($core->plugins->moduleExists('pages')) {
102     if(count($pages_combo))
103          $items['pages'] = array(__('Page'),true);
104}
105if ($core->plugins->moduleExists('tags')) {
106     if (count($tags_combo) > 1)
107          $items['tags'] = array(__('Tags'),true);
108}
109
110# --BEHAVIOR-- adminSimpleMenuAddType
111# Should add an item to $items as an array(<label>,<optional step (true or false)>)
112$core->callBehavior('adminSimpleMenuAddType',$items);
113
114$items['special'] = array(__('User defined'),false);
115
116$items_combo = array();
117foreach ($items as $k => $v) {
118     $items_combo[$v[0]] = $k;
119}
120
121# Lecture menu existant
122$menu = $core->blog->settings->system->get('simpleMenu');
123$menu = @unserialize($menu);
124if (!is_array($menu)) {
125     $menu = array();
126}
127
128# Récupération paramètres postés
129$item_type = isset($_POST['item_type']) ? $_POST['item_type'] : '';
130$item_select = isset($_POST['item_select']) ? $_POST['item_select'] : '';
131$item_label = isset($_POST['item_label']) ? $_POST['item_label'] : '';
132$item_descr = isset($_POST['item_descr']) ? $_POST['item_descr'] : '';
133$item_url = isset($_POST['item_url']) ? $_POST['item_url'] : '';
134
135# Traitement
136$step = (!empty($_GET['add']) ? (integer) $_GET['add'] : 0);
137if (($step > 4) || ($step < 0)) $step = 0;
138if ($step) {
139
140     # Récupération libellés des choix
141     $item_type_label = isset($items[$item_type]) ? $items[$item_type][0] : '';
142
143     switch ($step) {
144          case 1:
145               // First step, menu item type to be selected
146               $item_type = $item_select = '';
147               break;
148          case 2:
149               if ($items[$item_type][1]) {
150                    // Second step (optional), menu item sub-type to be selected
151                    $item_select = '';
152                    break;
153               }
154          case 3:
155               // Third step, menu item attributes to be changed or completed if necessary
156               $item_select_label = '';
157               $item_label = __('Label');
158               $item_descr = __('Description');
159               $item_url = $blog_url;
160               switch ($item_type) {
161                    case 'home':
162                         $item_label = __('Home');
163                         $item_descr = __('Recent posts');
164                         break;
165                    case 'lang':
166                         $item_select_label = array_search($item_select,$langs_combo);
167                         $item_label = $item_select_label;
168                         $item_descr = sprintf(__('Switch to %s language'),$item_select_label);
169                         $item_url .= $core->url->getBase('lang').$item_select;
170                         break;
171                    case 'category':
172                         $item_select_label = $categories_label[$item_select];
173                         $item_label = $item_select_label;
174                         $item_descr = __('Recent Posts from this category');
175                         $item_url .= $core->url->getBase('category').'/'.$item_select;
176                         break;
177                    case 'archive':
178                         $item_select_label = array_search($item_select,$months_combo);
179                         if ($item_select == '-') {
180                              $item_label = __('Archives');
181                              $item_descr = $first_year.($first_year != $last_year ? ' - '.$last_year : '');
182                              $item_url .= $core->url->getBase('archive');
183                         } else {
184                              $item_label = $item_select_label;
185                              $item_descr = sprintf(__('Posts from %s'),$item_select_label);
186                              $item_url .= $core->url->getBase('archive').'/'.substr($item_select,0,4).'/'.substr($item_select,-2);
187                         }
188                         break;
189                    case 'pages':
190                         $item_select_label = array_search($item_select,$pages_combo);
191                         $item_label = $item_select_label;
192                         $item_descr = '';
193                         $item_url = html::stripHostURL($item_select);
194                         break;
195                    case 'tags':
196                         $item_select_label = array_search($item_select,$tags_combo);
197                         if ($item_select == '-') {
198                              $item_label = __('All tags');
199                              $item_descr = '';
200                              $item_url .= $core->url->getBase('tags');
201                         } else {
202                              $item_label = $item_select_label;
203                              $item_descr = sprintf(__('Recent posts for %s tag'),$item_select_label);
204                              $item_url .= $core->url->getBase('tag').'/'.$item_select;
205                         }
206                         break;
207                    case 'special':
208                         break;
209                    default:
210                         # --BEHAVIOR-- adminSimpleMenuBeforeEdit
211                         # Should modify if necessary $item_label, $item_descr and $item_url
212                         # Should set if necessary $item_select_label (displayed on further admin step only)
213                         $core->callBehavior('adminSimpleMenuBeforeEdit',$item_type,$item_select,
214                              $item_label,$item_descr,$item_url,$item_select_label);
215                         break;
216               }
217               break;
218          case 4:
219               // Fourth step, menu item to be added
220               try {
221                    if (($item_label != '') && ($item_url != '')) 
222                    {
223                         // Add new item menu in menu array
224                         $menu[] = array(
225                              'label' => $item_label,
226                              'descr' => $item_descr,
227                              'url' => $item_url
228                         );
229                         // Save menu in blog settings
230                         $core->blog->settings->system->put('simpleMenu',serialize($menu));
231                   
232                         // All done successfully, return to menu items list
233                         http::redirect($p_url.'&added=1');
234                    } else {
235                         throw new Exception(__('Label and URL of menu item are mandatory.'));
236                    }
237               }
238               catch (Exception $e) {
239                    $core->error->add($e->getMessage());
240               }
241               break;
242     }
243} else {
244     
245     # Remove selected menu items
246     if (!empty($_POST['removeaction']))
247     {
248          try {
249               if (!empty($_POST['items_selected'])) {
250                    foreach ($_POST['items_selected'] as $k => $v) {
251                         $menu[$v]['label'] = '';
252                    }
253                    $newmenu = array();
254                    foreach ($menu as $k => $v) {
255                         if ($v['label']) {
256                              $newmenu[] = array(
257                                   'label' => $v['label'],
258                                   'descr' => $v['descr'],
259                                   'url' => $v['url']);
260                         }
261                    }
262                    $menu = $newmenu;
263                    // Save menu in blog settings
264                    $core->blog->settings->system->put('simpleMenu',serialize($menu));
265                   
266                    // All done successfully, return to menu items list
267                    http::redirect($p_url.'&removed=1');
268               } else {
269                    throw new Exception(__('No menu items selected.'));
270               }
271          }
272          catch (Exception $e) {
273               $core->error->add($e->getMessage());
274          }
275     }
276
277     # Update menu items
278     if (!empty($_POST['updateaction']))
279     {
280          try {
281               foreach ($_POST['items_label'] as $k => $v) {
282                    if (!$v) throw new Exception(__('Label is mandatory.'));
283               }
284               foreach ($_POST['items_url'] as $k => $v) {
285                    if (!$v) throw new Exception(__('URL is mandatory.'));
286               }
287               $newmenu = array();
288               for ($i = 0; $i < count($_POST['items_label']); $i++)
289               {
290                    $newmenu[] = array(
291                         'label' => $_POST['items_label'][$i],
292                         'descr' => $_POST['items_descr'][$i],
293                         'url' => $_POST['items_url'][$i]);
294               }
295               $menu = $newmenu;
296               // Save menu in blog settings
297               $core->blog->settings->system->put('simpleMenu',serialize($menu));
298
299               // All done successfully, return to menu items list
300               http::redirect($p_url.'&updated=1');
301          }
302          catch (Exception $e) {
303               $core->error->add($e->getMessage());
304          }
305     }
306     
307     # Order menu items
308     $order = array();
309     if (empty($_POST['im_order']) && !empty($_POST['order'])) {
310          $order = $_POST['order'];
311          asort($order);
312          $order = array_keys($order);
313     } elseif (!empty($_POST['im_order'])) {
314          $order = $_POST['im_order'];
315          if (substr($order,-1) == ',') {
316               $order = substr($order,0,strlen($order)-1);
317          }
318          $order = explode(',',$order);
319     }
320
321     if (!empty($_POST['updateaction']) && !empty($order))
322     {
323          try {
324               $newmenu = array();
325               foreach ($order as $i => $k) {
326                    $newmenu[] = array(
327                         'label' => $menu[$k]['label'],
328                         'descr' => $menu[$k]['descr'],
329                         'url' => $menu[$k]['url']);
330               }
331               $menu = $newmenu;
332               // Save menu in blog settings
333               $core->blog->settings->system->put('simpleMenu',serialize($menu));
334
335               // All done successfully, return to menu items list
336               http::redirect($p_url.'&neworder=1');
337          } 
338          catch (Exception $e) {
339               $core->error->add($e->getMessage());
340          }
341     }
342     
343}
344
345# Display
346?>
347
348<html>
349<head>
350     <title><?php echo $page_title; ?></title>
351     <?php
352          echo
353               dcPage::jsToolMan().
354               # --BEHAVIOR-- adminPageHeaders
355               $core->callBehavior('adminPageHeaders');
356     ?>
357     <?php 
358          $core->auth->user_prefs->addWorkspace('accessibility'); 
359          $user_dm_nodragdrop = $core->auth->user_prefs->accessibility->nodragdrop;
360     ?>
361     <?php if (!$user_dm_nodragdrop) : ?>
362     <script type="text/javascript">
363     //<![CDATA[
364
365     var dragsort = ToolMan.dragsort();
366     $(function() {
367          dragsort.makeTableSortable($("#menuitemslist").get(0),
368          dotclear.sortable.setHandle,dotclear.sortable.saveOrder);
369
370          $('.checkboxes-helpers').each(function() {
371               dotclear.checkboxesHelpers(this);
372          });
373     });
374
375     dotclear.sortable = {
376          setHandle: function(item) {
377               var handle = $(item).find('td.handle').get(0);
378               while (handle.firstChild) {
379                    handle.removeChild(handle.firstChild);
380               }
381
382               item.toolManDragGroup.setHandle(handle);
383               handle.className = handle.className+' handler';
384          },
385
386          saveOrder: function(item) {
387               var group = item.toolManDragGroup;
388               var order = document.getElementById('im_order');
389               group.register('dragend', function() {
390                    order.value = '';
391                    items = item.parentNode.getElementsByTagName('tr');
392
393                    for (var i=0; i<items.length; i++) {
394                         order.value += items[i].id.substr(2)+',';
395                    }
396               });
397          }
398     };
399     //]]>
400     </script>
401     <?php endif; ?>
402     <!--
403     <link rel="stylesheet" type="text/css" href="index.php?pf=simpleMenu/style.css" />
404     -->
405</head>
406
407<body>
408
409<?php
410
411if (!empty($_GET['added'])) {
412     echo '<p class="message">'.__('Menu item has been successfully added.').'</p>';
413}
414if (!empty($_GET['removed'])) {
415     echo '<p class="message">'.__('Menu items have been successfully removed.').'</p>';
416}
417if (!empty($_GET['neworder'])) {
418     echo '<p class="message">'.__('Menu items have been successfully updated.').'</p>';
419}
420if (!empty($_GET['updated'])) {
421     echo '<p class="message">'.__('Menu items have been successfully updated.').'</p>';
422}
423
424if ($step) 
425{
426     // Formulaire d'ajout d'un item
427     echo '<h2>'.html::escapeHTML($core->blog->name).' &rsaquo; <a href="'.$p_url.'">'.$page_title.'</a> &rsaquo; <span class="page-title">'.__('Add item').'</span></h2>';
428     
429     switch ($step) {
430          case 1:
431               // Selection du type d'item
432               echo '<form id="additem" action="'.$p_url.'&add=2" method="post">';
433               echo '<fieldset><legend>'.__('Select type').'</legend>';
434               echo '<p class="field"><label for"item_type" class="classic">'.__('Type of item menu:').'</label>'.form::combo('item_type',$items_combo,'').'</p>';
435               echo '<p>'.$core->formNonce().'<input type="submit" name="appendaction" value="'.__('Continue…').'" />'.'</p>';
436               echo '</fieldset>';
437               echo '</form>';
438               break;
439          case 2:
440               if ($items[$item_type][1]) {
441                    // Choix à faire
442                    echo '<form id="additem" action="'.$p_url.'&add=3" method="post">';
443                    echo '<fieldset><legend>'.$item_type_label.'</legend>';
444                    switch ($item_type) {
445                         case 'lang':
446                              echo '<p class="field"><label for"item_select" class="classic">'.__('Select language:').'</label>'.
447                                   form::combo('item_select',$langs_combo,'');
448                              break;
449                         case 'category':
450                              echo '<p class="field"><label for"item_select" class="classic">'.__('Select category:').'</label>'.
451                                   form::combo('item_select',$categories_combo,'');
452                              break;
453                         case 'archive':
454                              echo '<p class="field"><label for"item_select" class="classic">'.__('Select month (if necessary):').'</label>'.
455                                   form::combo('item_select',$months_combo,'');
456                              break;
457                         case 'pages':
458                              echo '<p class="field"><label for"item_select" class="classic">'.__('Select page:').'</label>'.
459                                   form::combo('item_select',$pages_combo,'');
460                              break;
461                         case 'tags':
462                              echo '<p class="field"><label for"item_select" class="classic">'.__('Select tag (if necessary):').'</label>'.
463                                   form::combo('item_select',$tags_combo,'');
464                              break;
465                         default:
466                              echo
467                                   # --BEHAVIOR-- adminSimpleMenuSelect
468                                   # Optional step once $item_type known : should provide a field using 'item_select' as id
469                                   $core->callBehavior('adminSimpleMenuSelect',$item_type,'item_select');
470                    }
471                    echo form::hidden('item_type',$item_type);
472                    echo '<p>'.$core->formNonce().'<input type="submit" name="appendaction" value="'.__('Continue…').'" /></p>';
473                    echo '</fieldset>';
474                    echo '</form>';
475                    break;
476               }
477          case 3:
478               // Libellé et description
479               echo '<form id="additem" action="'.$p_url.'&add=4" method="post">';
480               echo '<fieldset><legend>'.$item_type_label.($item_select_label != '' ? ' ('.$item_select_label.')' : '').'</legend>';
481               echo '<p class="field"><label for"item_label" class="classic required"><abbr title="'.__('Required field').'">*</abbr> '.
482                    __('Label of item menu:').'</label>'.form::field('item_label',20,255,$item_label).'</p>';
483               echo '<p class="field"><label for"item_descr" class="classic">'.
484                    __('Description of item menu:').'</label>'.form::field('item_descr',30,255,$item_descr).'</p>';
485               echo '<p class="field"><label for"item_url" class="classic required"><abbr title="'.__('Required field').'">*</abbr> '.
486                    __('URL of item menu:').'</label>'.form::field('item_url',40,255,$item_url).'</p>';
487               echo form::hidden('item_type',$item_type).form::hidden('item_select',$item_select);
488               echo '<p>'.$core->formNonce().'<input type="submit" name="appendaction" value="'.__('Add this item').'" /></p>';
489               echo '</fieldset>';
490               echo '</form>';
491               break;
492     }
493}
494
495// Liste des items
496if (!$step) {
497     echo '<h2>'.html::escapeHTML($core->blog->name).' &rsaquo; <span class="page-title">'.$page_title.'</span></h2>';
498}
499     
500if (count($menu)) {
501     if (!$step) {
502          echo '<form id="menuitems" action="'.$p_url.'" method="post">';
503     }
504     // Entête table
505     echo 
506          '<table class="maximal dragable">'.
507          '<caption>'.__('Menu items list').'</caption>'.
508          '<thead>'.
509          '<tr>';
510     if (!$step) {
511          echo '<th scope="col"></th>';
512          echo '<th scope="col"></th>';
513     }
514     echo
515          '<th scope="col">'.__('Label').'</th>'.
516          '<th scope="col">'.__('Description').'</th>'.
517          '<th scope="col">'.__('URL').'</th>'.
518          '</tr>'.
519          '</thead>'.
520          '<tbody id="menuitemslist">';
521     $count = 0;
522     foreach ($menu as $i => $m) {
523          echo '<tr class="line" id="l_'.$i.'">';
524          if (!$step) {
525               $count++;
526               echo '<td class="handle minimal">'.form::field(array('order['.$i.']'),2,3,$count,'position','',false,'title="'.sprintf(__('position of %s'),__($m['label'])).'"').
527                    form::hidden(array('dynorder[]','dynorder-'.$i),$i).'</td>';
528               echo '<td class="minimal">'.form::checkbox(array('items_selected[]','ims-'.$i),$i).'</td>';
529               echo '<td class="nowrap" scope="row">'.form::field(array('items_label[]','iml-'.$i),20,255,__($m['label'])).'</td>';
530               echo '<td class="nowrap">'.form::field(array('items_descr[]','imd-'.$i),30,255,__($m['descr'])).'</td>';
531               echo '<td class="nowrap">'.form::field(array('items_url[]','imu-'.$i),40,255,$m['url']).'</td>';
532          } else {
533               echo '<td class="nowrap" scope="row">'.__($m['label']).'</td>';
534               echo '<td class="nowrap">'.__($m['descr']).'</td>';
535               echo '<td class="nowrap">'.$m['url'].'</td>';
536          }
537          echo '</tr>';
538     }
539     echo '</tbody>'.
540          '</table>';
541     if (!$step) {
542          echo '<div class="two-cols">';
543          echo '<p class="col">'.form::hidden('im_order','').$core->formNonce();
544          echo '<input type="submit" name="updateaction" value="'.__('Update menu').'" />'.'</p>';
545          echo '<p class="col right">'.'<input type="submit" class="delete" name="removeaction" '.
546                    'value="'.__('Delete selected menu items').'" '.
547                    'onclick="return window.confirm(\''.html::escapeJS(__('Are you sure you want to remove selected menu items?')).'\');" />'.
548               '</p>';
549          echo '</div>';
550          echo '</form>';
551     }
552} else {
553     echo
554          '<p>'.__('Currently no menu items').'</p>';
555}
556
557if (!$step) {
558     echo '<form id="menuitems" action="'.$p_url.'&add=1" method="post">';
559     echo '<p>'.$core->formNonce().'<input class="add" type="submit" name="appendaction" value="'.__('Add an item').'" /></p>';
560     echo '</form>';
561}
562
563dcPage::helpBlock('simpleMenu');
564?>
565
566</body>
567</html>
Note: See TracBrowser for help on using the repository browser.

Sites map