Dotclear

source: plugins/simpleMenu/index.php @ 573:1054911a40b7

Revision 573:1054911a40b7, 17.9 KB checked in by Franck <carnet.franck.paul@…>, 14 years ago (diff)

Déplacement du bouton "Ajout d'un item de menu" au dessus de la liste des items de menu existants

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                         $core->blog->triggerBlog();
232                   
233                         // All done successfully, return to menu items list
234                         http::redirect($p_url.'&added=1');
235                    } else {
236                         throw new Exception(__('Label and URL of menu item are mandatory.'));
237                    }
238               }
239               catch (Exception $e) {
240                    $core->error->add($e->getMessage());
241               }
242               break;
243     }
244} else {
245     
246     # Remove selected menu items
247     if (!empty($_POST['removeaction']))
248     {
249          try {
250               if (!empty($_POST['items_selected'])) {
251                    foreach ($_POST['items_selected'] as $k => $v) {
252                         $menu[$v]['label'] = '';
253                    }
254                    $newmenu = array();
255                    foreach ($menu as $k => $v) {
256                         if ($v['label']) {
257                              $newmenu[] = array(
258                                   'label' => $v['label'],
259                                   'descr' => $v['descr'],
260                                   'url' => $v['url']);
261                         }
262                    }
263                    $menu = $newmenu;
264                    // Save menu in blog settings
265                    $core->blog->settings->system->put('simpleMenu',serialize($menu));
266                    $core->blog->triggerBlog();
267                   
268                    // All done successfully, return to menu items list
269                    http::redirect($p_url.'&removed=1');
270               } else {
271                    throw new Exception(__('No menu items selected.'));
272               }
273          }
274          catch (Exception $e) {
275               $core->error->add($e->getMessage());
276          }
277     }
278
279     # Update menu items
280     if (!empty($_POST['updateaction']))
281     {
282          try {
283               foreach ($_POST['items_label'] as $k => $v) {
284                    if (!$v) throw new Exception(__('Label is mandatory.'));
285               }
286               foreach ($_POST['items_url'] as $k => $v) {
287                    if (!$v) throw new Exception(__('URL is mandatory.'));
288               }
289               $newmenu = array();
290               for ($i = 0; $i < count($_POST['items_label']); $i++)
291               {
292                    $newmenu[] = array(
293                         'label' => $_POST['items_label'][$i],
294                         'descr' => $_POST['items_descr'][$i],
295                         'url' => $_POST['items_url'][$i]);
296               }
297               $menu = $newmenu;
298               // Save menu in blog settings
299               $core->blog->settings->system->put('simpleMenu',serialize($menu));
300               $core->blog->triggerBlog();
301
302               // All done successfully, return to menu items list
303               http::redirect($p_url.'&updated=1');
304          }
305          catch (Exception $e) {
306               $core->error->add($e->getMessage());
307          }
308     }
309     
310     # Order menu items
311     $order = array();
312     if (empty($_POST['im_order']) && !empty($_POST['order'])) {
313          $order = $_POST['order'];
314          asort($order);
315          $order = array_keys($order);
316     } elseif (!empty($_POST['im_order'])) {
317          $order = $_POST['im_order'];
318          if (substr($order,-1) == ',') {
319               $order = substr($order,0,strlen($order)-1);
320          }
321          $order = explode(',',$order);
322     }
323
324     if (!empty($_POST['updateaction']) && !empty($order))
325     {
326          try {
327               $newmenu = array();
328               foreach ($order as $i => $k) {
329                    $newmenu[] = array(
330                         'label' => $menu[$k]['label'],
331                         'descr' => $menu[$k]['descr'],
332                         'url' => $menu[$k]['url']);
333               }
334               $menu = $newmenu;
335               // Save menu in blog settings
336               $core->blog->settings->system->put('simpleMenu',serialize($menu));
337               $core->blog->triggerBlog();
338
339               // All done successfully, return to menu items list
340               http::redirect($p_url.'&neworder=1');
341          } 
342          catch (Exception $e) {
343               $core->error->add($e->getMessage());
344          }
345     }
346     
347}
348
349# Display
350?>
351
352<html>
353<head>
354     <title><?php echo $page_title; ?></title>
355     <?php
356          echo
357               dcPage::jsToolMan().
358               # --BEHAVIOR-- adminPageHeaders
359               $core->callBehavior('adminPageHeaders');
360     ?>
361     <?php 
362          $core->auth->user_prefs->addWorkspace('accessibility'); 
363          $user_dm_nodragdrop = $core->auth->user_prefs->accessibility->nodragdrop;
364     ?>
365     <?php if (!$user_dm_nodragdrop) : ?>
366     <script type="text/javascript">
367     //<![CDATA[
368
369     var dragsort = ToolMan.dragsort();
370     $(function() {
371          dragsort.makeTableSortable($("#menuitemslist").get(0),
372          dotclear.sortable.setHandle,dotclear.sortable.saveOrder);
373
374          $('.checkboxes-helpers').each(function() {
375               dotclear.checkboxesHelpers(this);
376          });
377     });
378
379     dotclear.sortable = {
380          setHandle: function(item) {
381               var handle = $(item).find('td.handle').get(0);
382               while (handle.firstChild) {
383                    handle.removeChild(handle.firstChild);
384               }
385
386               item.toolManDragGroup.setHandle(handle);
387               handle.className = handle.className+' handler';
388          },
389
390          saveOrder: function(item) {
391               var group = item.toolManDragGroup;
392               var order = document.getElementById('im_order');
393               group.register('dragend', function() {
394                    order.value = '';
395                    items = item.parentNode.getElementsByTagName('tr');
396
397                    for (var i=0; i<items.length; i++) {
398                         order.value += items[i].id.substr(2)+',';
399                    }
400               });
401          }
402     };
403     //]]>
404     </script>
405     <?php endif; ?>
406     <!--
407     <link rel="stylesheet" type="text/css" href="index.php?pf=simpleMenu/style.css" />
408     -->
409</head>
410
411<body>
412
413<?php
414
415if (!empty($_GET['added'])) {
416     echo '<p class="message">'.__('Menu item has been successfully added.').'</p>';
417}
418if (!empty($_GET['removed'])) {
419     echo '<p class="message">'.__('Menu items have been successfully removed.').'</p>';
420}
421if (!empty($_GET['neworder'])) {
422     echo '<p class="message">'.__('Menu items have been successfully updated.').'</p>';
423}
424if (!empty($_GET['updated'])) {
425     echo '<p class="message">'.__('Menu items have been successfully updated.').'</p>';
426}
427
428if ($step) 
429{
430     // Formulaire d'ajout d'un item
431     echo '<h2>'.html::escapeHTML($core->blog->name).' &rsaquo; <a href="'.$p_url.'">'.$page_title.'</a> &rsaquo; <span class="page-title">'.__('Add item').'</span></h2>';
432     
433     switch ($step) {
434          case 1:
435               // Selection du type d'item
436               echo '<form id="additem" action="'.$p_url.'&add=2" method="post">';
437               echo '<fieldset><legend>'.__('Select type').'</legend>';
438               echo '<p class="field"><label for"item_type" class="classic">'.__('Type of item menu:').'</label>'.form::combo('item_type',$items_combo,'').'</p>';
439               echo '<p>'.$core->formNonce().'<input type="submit" name="appendaction" value="'.__('Continue…').'" />'.'</p>';
440               echo '</fieldset>';
441               echo '</form>';
442               break;
443          case 2:
444               if ($items[$item_type][1]) {
445                    // Choix à faire
446                    echo '<form id="additem" action="'.$p_url.'&add=3" method="post">';
447                    echo '<fieldset><legend>'.$item_type_label.'</legend>';
448                    switch ($item_type) {
449                         case 'lang':
450                              echo '<p class="field"><label for"item_select" class="classic">'.__('Select language:').'</label>'.
451                                   form::combo('item_select',$langs_combo,'');
452                              break;
453                         case 'category':
454                              echo '<p class="field"><label for"item_select" class="classic">'.__('Select category:').'</label>'.
455                                   form::combo('item_select',$categories_combo,'');
456                              break;
457                         case 'archive':
458                              echo '<p class="field"><label for"item_select" class="classic">'.__('Select month (if necessary):').'</label>'.
459                                   form::combo('item_select',$months_combo,'');
460                              break;
461                         case 'pages':
462                              echo '<p class="field"><label for"item_select" class="classic">'.__('Select page:').'</label>'.
463                                   form::combo('item_select',$pages_combo,'');
464                              break;
465                         case 'tags':
466                              echo '<p class="field"><label for"item_select" class="classic">'.__('Select tag (if necessary):').'</label>'.
467                                   form::combo('item_select',$tags_combo,'');
468                              break;
469                         default:
470                              echo
471                                   # --BEHAVIOR-- adminSimpleMenuSelect
472                                   # Optional step once $item_type known : should provide a field using 'item_select' as id
473                                   $core->callBehavior('adminSimpleMenuSelect',$item_type,'item_select');
474                    }
475                    echo form::hidden('item_type',$item_type);
476                    echo '<p>'.$core->formNonce().'<input type="submit" name="appendaction" value="'.__('Continue…').'" /></p>';
477                    echo '</fieldset>';
478                    echo '</form>';
479                    break;
480               }
481          case 3:
482               // Libellé et description
483               echo '<form id="additem" action="'.$p_url.'&add=4" method="post">';
484               echo '<fieldset><legend>'.$item_type_label.($item_select_label != '' ? ' ('.$item_select_label.')' : '').'</legend>';
485               echo '<p class="field"><label for"item_label" class="classic required"><abbr title="'.__('Required field').'">*</abbr> '.
486                    __('Label of item menu:').'</label>'.form::field('item_label',20,255,$item_label).'</p>';
487               echo '<p class="field"><label for"item_descr" class="classic">'.
488                    __('Description of item menu:').'</label>'.form::field('item_descr',30,255,$item_descr).'</p>';
489               echo '<p class="field"><label for"item_url" class="classic required"><abbr title="'.__('Required field').'">*</abbr> '.
490                    __('URL of item menu:').'</label>'.form::field('item_url',40,255,$item_url).'</p>';
491               echo form::hidden('item_type',$item_type).form::hidden('item_select',$item_select);
492               echo '<p>'.$core->formNonce().'<input type="submit" name="appendaction" value="'.__('Add this item').'" /></p>';
493               echo '</fieldset>';
494               echo '</form>';
495               break;
496     }
497}
498
499// Liste des items
500if (!$step) {
501     echo '<h2>'.html::escapeHTML($core->blog->name).' &rsaquo; <span class="page-title">'.$page_title.'</span></h2>';
502}
503
504if (!$step) {
505     echo '<form id="menuitemsappend" action="'.$p_url.'&add=1" method="post">';
506     echo '<p>'.$core->formNonce().'<input class="add" type="submit" name="appendaction" value="'.__('Add an item').'" /></p>';
507     echo '</form>';
508}
509
510if (count($menu)) {
511     if (!$step) {
512          echo '<form id="menuitems" action="'.$p_url.'" method="post">';
513     }
514     // Entête table
515     echo 
516          '<table class="maximal dragable">'.
517          '<caption>'.__('Menu items list').'</caption>'.
518          '<thead>'.
519          '<tr>';
520     if (!$step) {
521          echo '<th scope="col"></th>';
522          echo '<th scope="col"></th>';
523     }
524     echo
525          '<th scope="col">'.__('Label').'</th>'.
526          '<th scope="col">'.__('Description').'</th>'.
527          '<th scope="col">'.__('URL').'</th>'.
528          '</tr>'.
529          '</thead>'.
530          '<tbody id="menuitemslist">';
531     $count = 0;
532     foreach ($menu as $i => $m) {
533          echo '<tr class="line" id="l_'.$i.'">';
534          if (!$step) {
535               $count++;
536               echo '<td class="handle minimal">'.form::field(array('order['.$i.']'),2,3,$count,'position','',false,'title="'.sprintf(__('position of %s'),__($m['label'])).'"').
537                    form::hidden(array('dynorder[]','dynorder-'.$i),$i).'</td>';
538               echo '<td class="minimal">'.form::checkbox(array('items_selected[]','ims-'.$i),$i).'</td>';
539               echo '<td class="nowrap" scope="row">'.form::field(array('items_label[]','iml-'.$i),20,255,__($m['label'])).'</td>';
540               echo '<td class="nowrap">'.form::field(array('items_descr[]','imd-'.$i),30,255,__($m['descr'])).'</td>';
541               echo '<td class="nowrap">'.form::field(array('items_url[]','imu-'.$i),40,255,$m['url']).'</td>';
542          } else {
543               echo '<td class="nowrap" scope="row">'.__($m['label']).'</td>';
544               echo '<td class="nowrap">'.__($m['descr']).'</td>';
545               echo '<td class="nowrap">'.$m['url'].'</td>';
546          }
547          echo '</tr>';
548     }
549     echo '</tbody>'.
550          '</table>';
551     if (!$step) {
552          echo '<div class="two-cols">';
553          echo '<p class="col">'.form::hidden('im_order','').$core->formNonce();
554          echo '<input type="submit" name="updateaction" value="'.__('Update menu').'" />'.'</p>';
555          echo '<p class="col right">'.'<input type="submit" class="delete" name="removeaction" '.
556                    'value="'.__('Delete selected menu items').'" '.
557                    'onclick="return window.confirm(\''.html::escapeJS(__('Are you sure you want to remove selected menu items?')).'\');" />'.
558               '</p>';
559          echo '</div>';
560          echo '</form>';
561     }
562} else {
563     echo
564          '<p>'.__('Currently no menu items').'</p>';
565}
566
567dcPage::helpBlock('simpleMenu');
568?>
569
570</body>
571</html>
Note: See TracBrowser for help on using the repository browser.

Sites map