Dotclear

source: plugins/simpleMenu/index.php @ 2820:208141170abc

Revision 2820:208141170abc, 18.2 KB checked in by franck <carnet.franck.paul@…>, 11 years ago (diff)

registercopy for plugin is not necessary as it has been already done on module loading (by the core)

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

Sites map