Dotclear

source: plugins/aboutConfig/index.php @ 3160:80348e1ba097

Revision 3160:80348e1ba097, 6.5 KB checked in by franck <carnet.franck.paul@…>, 9 years ago (diff)

Update about:config plugin to cope with array settings, addresses #1833

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
6# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
7# Licensed under the GPL version 2.0 license.
8# See LICENSE file or
9# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10#
11# -- END LICENSE BLOCK -----------------------------------------
12if (!defined('DC_CONTEXT_ADMIN')) { return; }
13
14# Local navigation
15if (!empty($_POST['gs_nav'])) {
16     http::redirect($p_url.$_POST['gs_nav']);
17     exit;
18}
19if (!empty($_POST['ls_nav'])) {
20     http::redirect($p_url.$_POST['ls_nav']);
21     exit;
22}
23
24# Local settings update
25if (!empty($_POST['s']) && is_array($_POST['s']))
26{
27     try
28     {
29          foreach ($_POST['s'] as $ns => $s) {
30               $core->blog->settings->addNamespace($ns);
31               foreach ($s as $k => $v) {
32                    if ($_POST['s_type'][$ns][$k] == 'array') {
33                         $v = json_decode($v,true);
34                    }
35                    $core->blog->settings->$ns->put($k,$v);
36               }
37               $core->blog->triggerBlog();
38          }
39
40          dcPage::addSuccessNotice(__('Configuration successfully updated'));
41          http::redirect($p_url);
42     }
43     catch (Exception $e)
44     {
45          $core->error->add($e->getMessage());
46     }
47}
48
49# Global settings update
50if (!empty($_POST['gs']) && is_array($_POST['gs']))
51{
52     try
53     {
54          foreach ($_POST['gs'] as $ns => $s) {
55               $core->blog->settings->addNamespace($ns);
56               foreach ($s as $k => $v) {
57                    if ($_POST['gs_type'][$ns][$k] == 'array') {
58                         $v = json_decode($v,true);
59                    }
60                    $core->blog->settings->$ns->put($k,$v,null,null,true,true);
61               }
62               $core->blog->triggerBlog();
63          }
64
65          dcPage::addSuccessNotice(__('Configuration successfully updated'));
66          http::redirect($p_url.'&part=global');
67     }
68     catch (Exception $e)
69     {
70          $core->error->add($e->getMessage());
71     }
72}
73
74$part = !empty($_GET['part']) && $_GET['part'] == 'global' ? 'global' : 'local';
75
76function settingLine($id,$s,$ns,$field_name,$strong_label)
77{
78     if ($s['type'] == 'boolean') {
79          $field = form::combo(array($field_name.'['.$ns.']['.$id.']',$field_name.'_'.$ns.'_'.$id),
80          array(__('yes') => 1, __('no') => 0),$s['value'] ? 1 : 0);
81     } else {
82          if ($s['type'] == 'array') {
83               $field = form::field(array($field_name.'['.$ns.']['.$id.']',$field_name.'_'.$ns.'_'.$id),40,null,
84               html::escapeHTML(json_encode($s['value'])));
85          } else {
86               $field = form::field(array($field_name.'['.$ns.']['.$id.']',$field_name.'_'.$ns.'_'.$id),40,null,
87               html::escapeHTML($s['value']));
88          }
89     }
90     $type = form::hidden(array($field_name.'_type'.'['.$ns.']['.$id.']',$field_name.'_'.$ns.'_'.$id.'_type'),
91          html::escapeHTML($s['type']));
92
93     $slabel = $strong_label ? '<strong>%s</strong>' : '%s';
94
95     return
96     '<tr class="line">'.
97     '<td scope="row"><label for="'.$field_name.'_'.$ns.'_'.$id.'">'.sprintf($slabel,html::escapeHTML($id)).'</label></td>'.
98     '<td>'.$field.'</td>'.
99     '<td>'.$s['type'].$type.'</td>'.
100     '<td>'.html::escapeHTML($s['label']).'</td>'.
101     '</tr>';
102}
103?>
104<html>
105<head>
106  <title>about:config</title>
107  <?php echo dcPage::jsPageTabs($part); ?>
108  <script type="text/javascript">
109     //<![CDATA[
110     $(function() {
111          $("#gs_submit").hide();
112          $("#ls_submit").hide();
113          $("#gs_nav").change(function() {
114               window.location = $("#gs_nav option:selected").val();
115          })
116          $("#ls_nav").change(function() {
117               window.location = $("#ls_nav option:selected").val();
118          })
119     });
120     //]]>
121     </script>
122</head>
123
124<body>
125<?php
126echo dcPage::breadcrumb(
127     array(
128          __('System') => '',
129          html::escapeHTML($core->blog->name) => '',
130          __('about:config') => ''
131     )).
132     dcPage::notices();
133?>
134
135<div id="local" class="multi-part" title="<?php echo sprintf(__('Settings for %s'),html::escapeHTML($core->blog->name)); ?>">
136<h3 class="out-of-screen-if-js"><?php echo sprintf(__('Settings for %s'),html::escapeHTML($core->blog->name)); ?></h3>
137
138<?php
139$table_header = '<div class="table-outer"><table class="settings" id="%s"><caption class="as_h3">%s</caption>'.
140'<thead>'.
141'<tr>'."\n".
142'  <th class="nowrap">'.__('Setting ID').'</th>'."\n".
143'  <th>'.__('Value').'</th>'."\n".
144'  <th>'.__('Type').'</th>'."\n".
145'  <th class="maximalx">'.__('Description').'</th>'."\n".
146'</tr>'."\n".
147'</thead>'."\n".
148'<tbody>';
149$table_footer = '</tbody></table></div>';
150
151$settings = array();
152foreach ($core->blog->settings->dumpNamespaces() as $ns => $namespace) {
153     foreach ($namespace->dumpSettings() as $k => $v) {
154          $settings[$ns][$k] = $v;
155     }
156}
157ksort($settings);
158if (count($settings) > 0) {
159     $ns_combo = array();
160     foreach ($settings as $ns => $s) {
161          $ns_combo[$ns] = '#l_'.$ns;
162     }
163     echo
164          '<form action="'.$core->adminurl->get('admin.plugin').'" method="post">'.
165          '<p class="anchor-nav">'.
166          '<label for="ls_nav" class="classic">'.__('Goto:').'</label> '.form::combo('ls_nav',$ns_combo).
167          ' <input type="submit" value="'.__('Ok').'" id="ls_submit" />'.
168          '<input type="hidden" name="p" value="aboutConfig" />'.
169          $core->formNonce().'</p></form>';
170}
171?>
172
173<form action="<?php echo $core->adminurl->get('admin.plugin'); ?>" method="post">
174
175<?php
176foreach ($settings as $ns => $s)
177{
178     ksort($s);
179     echo sprintf($table_header,'l_'.$ns,$ns);
180     foreach ($s as $k => $v)
181     {
182          echo settingLine($k,$v,$ns,'s',!$v['global']);
183     }
184     echo $table_footer;
185}
186?>
187
188<p><input type="submit" value="<?php echo __('Save'); ?>" />
189<input type="hidden" name="p" value="aboutConfig" />
190<?php echo $core->formNonce(); ?></p>
191</form>
192</div>
193
194<div id="global" class="multi-part" title="<?php echo __('Global settings'); ?>">
195<h3 class="out-of-screen-if-js"><?php echo __('Global settings'); ?></h3>
196
197<?php
198$settings = array();
199
200foreach ($core->blog->settings->dumpNamespaces() as $ns => $namespace) {
201     foreach ($namespace->dumpGlobalSettings() as $k => $v) {
202          $settings[$ns][$k] = $v;
203     }
204}
205
206ksort($settings);
207
208if (count($settings) > 0) {
209     $ns_combo = array();
210     foreach ($settings as $ns => $s) {
211          $ns_combo[$ns] = '#g_'.$ns;
212     }
213     echo
214          '<form action="'.$core->adminurl->get('admin.plugin').'" method="post">'.
215          '<p class="anchor-nav">'.
216          '<label for="gs_nav" class="classic">'.__('Goto:').'</label> '.form::combo('gs_nav',$ns_combo).' '.
217          '<input type="submit" value="'.__('Ok').'" id="gs_submit" />'.
218          '<input type="hidden" name="p" value="aboutConfig" />'.
219          $core->formNonce().'</p></form>';
220}
221?>
222
223<form action="<?php echo $core->adminurl->get('admin.plugin'); ?>" method="post">
224
225<?php
226foreach ($settings as $ns => $s)
227{
228     ksort($s);
229     echo sprintf($table_header,'g_'.$ns,$ns);
230     foreach ($s as $k => $v)
231     {
232          echo settingLine($k,$v,$ns,'gs',false);
233     }
234     echo $table_footer;
235}
236?>
237
238<p><input type="submit" value="<?php echo __('Save'); ?>" />
239<input type="hidden" name="p" value="aboutConfig" />
240<?php echo $core->formNonce(); ?></p>
241</form>
242</div>
243
244<?php dcPage::helpBlock('aboutConfig'); ?>
245
246</body>
247</html>
Note: See TracBrowser for help on using the repository browser.

Sites map