Dotclear

source: plugins/simpleMenu/index.php @ 683:b32debb0479b

Revision 683:b32debb0479b, 18.0 KB checked in by Franck <carnet.franck.paul@…>, 14 years ago (diff)

Remerge themes dans default

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

Sites map