Dotclear

source: plugins/simpleMenu/index.php @ 3159:a7553434ee4c

Revision 3159:a7553434ee4c, 18.1 KB checked in by franck <carnet.franck.paul@…>, 9 years ago (diff)

Use new setting type 'array' for some settings, addresses #1833

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

Sites map