Dotclear

source: plugins/simpleMenu/index.php @ 1107:3943962d69b8

Revision 1107:3943962d69b8, 16.9 KB checked in by JcDenis, 12 years ago (diff)

merge with default branch in 2.5-RC

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

Sites map