Dotclear

source: plugins/simpleMenu/index.php @ 3703:53c8bef8608a

Revision 3703:53c8bef8608a, 24.4 KB checked in by franck <carnet.franck.paul@…>, 8 years ago (diff)

Use array form of optionnal parameters for form::combo(), code formatting (PSR-2)

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

Sites map