Dotclear

source: plugins/simpleMenu/index.php @ 539:5d12c71d0399

Revision 539:5d12c71d0399, 15.6 KB checked in by Franck <carnet.franck.paul@…>, 14 years ago (diff)

Correction bug sur suppression

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

Sites map