[0] | 1 | <?php |
---|
[3731] | 2 | /** |
---|
| 3 | * @brief widgets, a plugin for Dotclear 2 |
---|
| 4 | * |
---|
| 5 | * @package Dotclear |
---|
| 6 | * @subpackage Plugins |
---|
| 7 | * |
---|
| 8 | * @copyright Olivier Meunier & Association Dotclear |
---|
| 9 | * @copyright GPL-2.0-only |
---|
| 10 | */ |
---|
| 11 | |
---|
[3709] | 12 | if (!defined('DC_CONTEXT_ADMIN')) {return;} |
---|
[0] | 13 | |
---|
[3709] | 14 | include dirname(__FILE__) . '/_default_widgets.php'; |
---|
[2403] | 15 | |
---|
[665] | 16 | # Loading navigation, extra widgets and custom widgets |
---|
[0] | 17 | $widgets_nav = null; |
---|
| 18 | if ($core->blog->settings->widgets->widgets_nav) { |
---|
[3709] | 19 | $widgets_nav = dcWidgets::load($core->blog->settings->widgets->widgets_nav); |
---|
[0] | 20 | } |
---|
| 21 | $widgets_extra = null; |
---|
| 22 | if ($core->blog->settings->widgets->widgets_extra) { |
---|
[3709] | 23 | $widgets_extra = dcWidgets::load($core->blog->settings->widgets->widgets_extra); |
---|
[0] | 24 | } |
---|
[665] | 25 | $widgets_custom = null; |
---|
| 26 | if ($core->blog->settings->widgets->widgets_custom) { |
---|
[3709] | 27 | $widgets_custom = dcWidgets::load($core->blog->settings->widgets->widgets_custom); |
---|
[665] | 28 | } |
---|
[0] | 29 | |
---|
[3874] | 30 | $append_combo = [ |
---|
[3709] | 31 | '-' => 0, |
---|
| 32 | __('navigation') => 'nav', |
---|
| 33 | __('extra') => 'extra', |
---|
| 34 | __('custom') => 'custom' |
---|
[3874] | 35 | ]; |
---|
[0] | 36 | |
---|
[902] | 37 | function literalNullString($v) |
---|
| 38 | { |
---|
[3709] | 39 | if ($v == '') { |
---|
| 40 | return '<' . __('empty string') . '>'; |
---|
| 41 | } |
---|
| 42 | return $v; |
---|
[902] | 43 | } |
---|
| 44 | |
---|
[0] | 45 | # Adding widgets to sidebars |
---|
[3709] | 46 | if (!empty($_POST['append']) && is_array($_POST['addw'])) { |
---|
| 47 | # Filter selection |
---|
[3874] | 48 | $addw = []; |
---|
[3709] | 49 | foreach ($_POST['addw'] as $k => $v) { |
---|
| 50 | if (($v == 'extra' || $v == 'nav' || $v == 'custom') && $__widgets->{$k} !== null) { |
---|
| 51 | $addw[$k] = $v; |
---|
| 52 | } |
---|
| 53 | } |
---|
[2509] | 54 | |
---|
[3709] | 55 | # Append 1 widget |
---|
| 56 | $wid = false; |
---|
| 57 | if (gettype($_POST['append']) == 'array' && count($_POST['append']) == 1) { |
---|
| 58 | $wid = array_keys($_POST['append']); |
---|
| 59 | $wid = $wid[0]; |
---|
| 60 | } |
---|
[2509] | 61 | |
---|
[3709] | 62 | # Append widgets |
---|
| 63 | if (!empty($addw)) { |
---|
| 64 | if (!($widgets_nav instanceof dcWidgets)) { |
---|
| 65 | $widgets_nav = new dcWidgets; |
---|
| 66 | } |
---|
| 67 | if (!($widgets_extra instanceof dcWidgets)) { |
---|
| 68 | $widgets_extra = new dcWidgets(); |
---|
| 69 | } |
---|
| 70 | if (!($widgets_custom instanceof dcWidgets)) { |
---|
| 71 | $widgets_custom = new dcWidgets(); |
---|
| 72 | } |
---|
[2509] | 73 | |
---|
[3709] | 74 | foreach ($addw as $k => $v) { |
---|
| 75 | if (!$wid || $wid == $k) { |
---|
| 76 | switch ($v) { |
---|
| 77 | case 'nav': |
---|
| 78 | $widgets_nav->append($__widgets->{$k}); |
---|
| 79 | break; |
---|
| 80 | case 'extra': |
---|
| 81 | $widgets_extra->append($__widgets->{$k}); |
---|
| 82 | break; |
---|
| 83 | case 'custom': |
---|
| 84 | $widgets_custom->append($__widgets->{$k}); |
---|
| 85 | break; |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | } |
---|
[2509] | 89 | |
---|
[3709] | 90 | try { |
---|
| 91 | $core->blog->settings->addNamespace('widgets'); |
---|
| 92 | $core->blog->settings->widgets->put('widgets_nav', $widgets_nav->store()); |
---|
| 93 | $core->blog->settings->widgets->put('widgets_extra', $widgets_extra->store()); |
---|
| 94 | $core->blog->settings->widgets->put('widgets_custom', $widgets_custom->store()); |
---|
| 95 | $core->blog->triggerBlog(); |
---|
| 96 | http::redirect($p_url); |
---|
| 97 | } catch (Exception $e) { |
---|
| 98 | $core->error->add($e->getMessage()); |
---|
| 99 | } |
---|
| 100 | } |
---|
[0] | 101 | } |
---|
| 102 | |
---|
[2400] | 103 | # Removing ? |
---|
| 104 | $removing = false; |
---|
[3709] | 105 | if (isset($_POST['w']) && is_array($_POST['w'])) { |
---|
| 106 | foreach ($_POST['w'] as $nsid => $nsw) { |
---|
| 107 | foreach ($nsw as $i => $v) { |
---|
| 108 | if (!empty($v['_rem'])) { |
---|
| 109 | $removing = true; |
---|
| 110 | break 2; |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | } |
---|
[2400] | 114 | } |
---|
| 115 | |
---|
[2401] | 116 | # Move ? |
---|
| 117 | $move = false; |
---|
[3709] | 118 | if (isset($_POST['w']) && is_array($_POST['w'])) { |
---|
| 119 | foreach ($_POST['w'] as $nsid => $nsw) { |
---|
| 120 | foreach ($nsw as $i => $v) { |
---|
| 121 | if (!empty($v['_down'])) { |
---|
| 122 | $oldorder = $_POST['w'][$nsid][$i]['order']; |
---|
| 123 | $neworder = $oldorder + 1; |
---|
| 124 | if (isset($_POST['w'][$nsid][$neworder])) { |
---|
| 125 | $_POST['w'][$nsid][$i]['order'] = $neworder; |
---|
| 126 | $_POST['w'][$nsid][$neworder]['order'] = $oldorder; |
---|
| 127 | $move = true; |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | if (!empty($v['_up'])) { |
---|
| 131 | $oldorder = $_POST['w'][$nsid][$i]['order']; |
---|
| 132 | $neworder = $oldorder - 1; |
---|
| 133 | if (isset($_POST['w'][$nsid][$neworder])) { |
---|
| 134 | $_POST['w'][$nsid][$i]['order'] = $neworder; |
---|
| 135 | $_POST['w'][$nsid][$neworder]['order'] = $oldorder; |
---|
| 136 | $move = true; |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | } |
---|
[2401] | 141 | } |
---|
| 142 | |
---|
[0] | 143 | # Update sidebars |
---|
[3709] | 144 | if (!empty($_POST['wup']) || $removing || $move) { |
---|
| 145 | if (!isset($_POST['w']) || !is_array($_POST['w'])) { |
---|
[3874] | 146 | $_POST['w'] = []; |
---|
[3709] | 147 | } |
---|
[2509] | 148 | |
---|
[3709] | 149 | try |
---|
| 150 | { |
---|
| 151 | # Removing mark as _rem widgets |
---|
| 152 | foreach ($_POST['w'] as $nsid => $nsw) { |
---|
| 153 | foreach ($nsw as $i => $v) { |
---|
| 154 | if (!empty($v['_rem'])) { |
---|
| 155 | unset($_POST['w'][$nsid][$i]); |
---|
| 156 | continue; |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | } |
---|
[2509] | 160 | |
---|
[3709] | 161 | if (!isset($_POST['w']['nav'])) { |
---|
[3874] | 162 | $_POST['w']['nav'] = []; |
---|
[3709] | 163 | } |
---|
| 164 | if (!isset($_POST['w']['extra'])) { |
---|
[3874] | 165 | $_POST['w']['extra'] = []; |
---|
[3709] | 166 | } |
---|
| 167 | if (!isset($_POST['w']['custom'])) { |
---|
[3874] | 168 | $_POST['w']['custom'] = []; |
---|
[3709] | 169 | } |
---|
[2509] | 170 | |
---|
[3709] | 171 | $widgets_nav = dcWidgets::loadArray($_POST['w']['nav'], $__widgets); |
---|
| 172 | $widgets_extra = dcWidgets::loadArray($_POST['w']['extra'], $__widgets); |
---|
| 173 | $widgets_custom = dcWidgets::loadArray($_POST['w']['custom'], $__widgets); |
---|
[2509] | 174 | |
---|
[3709] | 175 | $core->blog->settings->addNamespace('widgets'); |
---|
| 176 | $core->blog->settings->widgets->put('widgets_nav', $widgets_nav->store()); |
---|
| 177 | $core->blog->settings->widgets->put('widgets_extra', $widgets_extra->store()); |
---|
| 178 | $core->blog->settings->widgets->put('widgets_custom', $widgets_custom->store()); |
---|
| 179 | $core->blog->triggerBlog(); |
---|
[2509] | 180 | |
---|
[3709] | 181 | dcPage::addSuccessNotice(__('Sidebars and their widgets have been saved.')); |
---|
| 182 | http::redirect($p_url); |
---|
| 183 | } catch (Exception $e) { |
---|
| 184 | $core->error->add($e->getMessage()); |
---|
| 185 | } |
---|
| 186 | } elseif (!empty($_POST['wreset'])) { |
---|
| 187 | try |
---|
| 188 | { |
---|
| 189 | $core->blog->settings->addNamespace('widgets'); |
---|
| 190 | $core->blog->settings->widgets->put('widgets_nav', ''); |
---|
| 191 | $core->blog->settings->widgets->put('widgets_extra', ''); |
---|
| 192 | $core->blog->settings->widgets->put('widgets_custom', ''); |
---|
| 193 | $core->blog->triggerBlog(); |
---|
[2509] | 194 | |
---|
[3709] | 195 | dcPage::addSuccessNotice(__('Sidebars have been resetting.')); |
---|
| 196 | http::redirect($p_url); |
---|
| 197 | } catch (Exception $e) { |
---|
| 198 | $core->error->add($e->getMessage()); |
---|
| 199 | } |
---|
[0] | 200 | } |
---|
[2403] | 201 | ?> |
---|
[0] | 202 | <html> |
---|
| 203 | <head> |
---|
| 204 | <title><?php echo __('Widgets'); ?></title> |
---|
[3709] | 205 | <?php |
---|
| 206 | echo dcPage::cssLoad(dcPage::getPF('widgets/style.css')) . |
---|
[3725] | 207 | dcPage::jsLoad('js/jquery/jquery-ui.custom.js') . |
---|
| 208 | dcPage::jsLoad('js/jquery/jquery.ui.touch-punch.js') . |
---|
| 209 | dcPage::jsLoad(dcPage::getPF('widgets/js/widgets.js')); |
---|
[3709] | 210 | |
---|
| 211 | $core->auth->user_prefs->addWorkspace('accessibility'); |
---|
| 212 | $user_dm_nodragdrop = $core->auth->user_prefs->accessibility->nodragdrop; |
---|
| 213 | if (!$user_dm_nodragdrop) { |
---|
| 214 | echo dcPage::jsLoad(dcPage::getPF('widgets/js/dragdrop.js')); |
---|
| 215 | } |
---|
[3874] | 216 | echo dcPage::jsVars(['dotclear.msg.confirm_widgets_reset' => __('Are you sure you want to reset sidebars?')]); |
---|
[3709] | 217 | |
---|
| 218 | $widget_editor = $core->auth->getOption('editor'); |
---|
| 219 | $rte_flag = true; |
---|
| 220 | $rte_flags = @$core->auth->user_prefs->interface->rte_flags; |
---|
| 221 | if (is_array($rte_flags) && in_array('widgets_text', $rte_flags)) { |
---|
| 222 | $rte_flag = $rte_flags['widgets_text']; |
---|
| 223 | } |
---|
| 224 | if ($rte_flag) { |
---|
[3874] | 225 | echo dcPage::jsVars(['dotclear.widget_noeditor' => 0]); |
---|
[3825] | 226 | echo $core->callBehavior('adminPostEditor', $widget_editor['xhtml'], 'widget', |
---|
[3874] | 227 | ['#sidebarsWidgets textarea:not(.noeditor)'], 'xhtml'); |
---|
[3826] | 228 | } else { |
---|
[3874] | 229 | echo dcPage::jsVars(['dotclear.widget_noeditor' => 1]); |
---|
[3709] | 230 | } |
---|
| 231 | echo (dcPage::jsConfirmClose('sidebarsWidgets')); |
---|
| 232 | ?> |
---|
[0] | 233 | </head> |
---|
| 234 | <body> |
---|
[2403] | 235 | <?php |
---|
[1358] | 236 | echo dcPage::breadcrumb( |
---|
[3874] | 237 | [ |
---|
[3709] | 238 | html::escapeHTML($core->blog->name) => '', |
---|
| 239 | __('Widgets') => '' |
---|
[3874] | 240 | ]) . |
---|
[3709] | 241 | dcPage::notices(); |
---|
[0] | 242 | |
---|
| 243 | # All widgets |
---|
| 244 | echo |
---|
[3709] | 245 | '<form id="listWidgets" action="' . $p_url . '" method="post" class="widgets">' . |
---|
| 246 | '<h3>' . __('Available widgets') . '</h3>' . |
---|
| 247 | '<p>' . __('Drag widgets from this list to one of the sidebars, for add.') . '</p>' . |
---|
| 248 | '<ul id="widgets-ref">'; |
---|
[0] | 249 | |
---|
[117] | 250 | $j = 0; |
---|
[0] | 251 | foreach ($__widgets->elements(true) as $w) { |
---|
[3709] | 252 | echo |
---|
[3874] | 253 | '<li>' . form::hidden(['w[void][0][id]'], html::escapeHTML($w->id())) . |
---|
| 254 | '<p class="widget-name">' . form::number(['w[void][0][order]'], [ |
---|
[3725] | 255 | 'default' => 0, |
---|
| 256 | 'class' => 'hide', |
---|
| 257 | 'extra_html' => 'title="' . __('order') . '"' |
---|
[3874] | 258 | ]) . |
---|
[3725] | 259 | ' ' . $w->name() . |
---|
[3709] | 260 | ($w->desc() != '' ? ' <span class="form-note">' . __($w->desc()) . '</span>' : '') . '</p>' . |
---|
| 261 | '<p class="manual-move remove-if-drag"><label class="classic">' . __('Append to:') . '</label> ' . |
---|
[3874] | 262 | form::combo(['addw[' . $w->id() . ']'], $append_combo) . |
---|
[3709] | 263 | '<input type="submit" name="append[' . $w->id() . ']" value="' . __('Add') . '" /></p>' . |
---|
| 264 | '<div class="widgetSettings hidden-if-drag">' . $w->formSettings('w[void][0]', $j) . '</div>' . |
---|
| 265 | '</li>'; |
---|
| 266 | $j++; |
---|
[0] | 267 | } |
---|
| 268 | |
---|
| 269 | echo |
---|
[3709] | 270 | '</ul>' . |
---|
| 271 | '<p>' . $core->formNonce() . '</p>' . |
---|
| 272 | '<p class="remove-if-drag"><input type="submit" name="append" value="' . __('Add widgets to sidebars') . '" /></p>' . |
---|
| 273 | '</form>'; |
---|
[0] | 274 | |
---|
[3709] | 275 | echo '<form id="sidebarsWidgets" action="' . $p_url . '" method="post">'; |
---|
[0] | 276 | # Nav sidebar |
---|
| 277 | echo |
---|
[3709] | 278 | '<div id="sidebarNav" class="widgets fieldset">' . |
---|
| 279 | sidebarWidgets('dndnav', __('Navigation sidebar'), $widgets_nav, 'nav', $__default_widgets['nav'], $j); |
---|
[1669] | 280 | echo '</div>'; |
---|
[0] | 281 | |
---|
| 282 | # Extra sidebar |
---|
| 283 | echo |
---|
[3709] | 284 | '<div id="sidebarExtra" class="widgets fieldset">' . |
---|
| 285 | sidebarWidgets('dndextra', __('Extra sidebar'), $widgets_extra, 'extra', $__default_widgets['extra'], $j); |
---|
[1669] | 286 | echo '</div>'; |
---|
[0] | 287 | |
---|
[665] | 288 | # Custom sidebar |
---|
| 289 | echo |
---|
[3709] | 290 | '<div id="sidebarCustom" class="widgets fieldset">' . |
---|
| 291 | sidebarWidgets('dndcustom', __('Custom sidebar'), $widgets_custom, 'custom', $__default_widgets['custom'], $j); |
---|
[1669] | 292 | echo '</div>'; |
---|
[665] | 293 | |
---|
[0] | 294 | echo |
---|
[3709] | 295 | '<p id="sidebarsControl">' . |
---|
| 296 | $core->formNonce() . |
---|
| 297 | '<input type="submit" name="wup" value="' . __('Update sidebars') . '" /> ' . |
---|
| 298 | '<input type="submit" class="reset" name="wreset" value="' . __('Reset sidebars') . '" /></p>' . |
---|
| 299 | '</form>'; |
---|
[0] | 300 | |
---|
[3709] | 301 | $widget_elements = new stdClass; |
---|
[2369] | 302 | $widget_elements->content = '<dl>'; |
---|
[3709] | 303 | foreach ($__widgets->elements() as $w) { |
---|
| 304 | $widget_elements->content .= |
---|
| 305 | '<dt><strong>' . html::escapeHTML($w->name()) . '</strong> (' . |
---|
| 306 | __('Widget ID:') . ' <strong>' . html::escapeHTML($w->id()) . '</strong>)' . |
---|
| 307 | ($w->desc() != '' ? ' <span class="form-note">' . __($w->desc()) . '</span>' : '') . '</dt>' . |
---|
| 308 | '<dd>'; |
---|
[2509] | 309 | |
---|
[3709] | 310 | $w_settings = $w->settings(); |
---|
| 311 | if (empty($w_settings)) { |
---|
| 312 | $widget_elements->content .= '<p>' . __('No setting for this widget') . '</p>'; |
---|
| 313 | } else { |
---|
| 314 | $widget_elements->content .= '<ul>'; |
---|
| 315 | foreach ($w->settings() as $n => $s) { |
---|
| 316 | switch ($s['type']) { |
---|
| 317 | case 'check': |
---|
| 318 | $s_type = __('boolean') . ", " . __('possible values:') . " <code>0</code> " . __('or') . " <code>1</code>"; |
---|
| 319 | break; |
---|
| 320 | case 'combo': |
---|
| 321 | $s['options'] = array_map("literalNullString", $s['options']); |
---|
| 322 | $s_type = __('listitem') . ", " . __('possible values:') . " <code>" . implode('</code>, <code>', $s['options']) . "</code>"; |
---|
| 323 | break; |
---|
| 324 | case 'text': |
---|
| 325 | case 'textarea': |
---|
| 326 | default: |
---|
| 327 | $s_type = __('string'); |
---|
| 328 | break; |
---|
| 329 | } |
---|
[2509] | 330 | |
---|
[3709] | 331 | $widget_elements->content .= |
---|
| 332 | '<li>' . |
---|
| 333 | __('Setting name:') . ' <strong>' . html::escapeHTML($n) . '</strong>' . |
---|
| 334 | ' (' . $s_type . ')' . |
---|
| 335 | '</li>'; |
---|
| 336 | } |
---|
| 337 | $widget_elements->content .= '</ul>'; |
---|
| 338 | } |
---|
| 339 | $widget_elements->content .= '</dd>'; |
---|
[0] | 340 | } |
---|
| 341 | $widget_elements->content .= '</dl></div>'; |
---|
| 342 | |
---|
[3709] | 343 | dcPage::helpBlock('widgets', $widget_elements); |
---|
[0] | 344 | |
---|
[3709] | 345 | function sidebarWidgets($id, $title, $widgets, $pr, $default_widgets, &$j) |
---|
[0] | 346 | { |
---|
[3709] | 347 | $res = '<h3>' . $title . '</h3>'; |
---|
[2509] | 348 | |
---|
[3709] | 349 | if (!($widgets instanceof dcWidgets)) { |
---|
| 350 | $widgets = $default_widgets; |
---|
| 351 | } |
---|
[2509] | 352 | |
---|
[3709] | 353 | $res .= '<ul id="' . $id . '" class="connected">'; |
---|
[2509] | 354 | |
---|
[3709] | 355 | $res .= '<li class="empty-widgets" ' . (!$widgets->isEmpty() ? 'style="display: none;"' : '') . '>' . __('No widget as far.') . '</li>'; |
---|
[2509] | 356 | |
---|
[3709] | 357 | $i = 0; |
---|
| 358 | foreach ($widgets->elements() as $w) { |
---|
| 359 | $upDisabled = $i == 0 ? ' disabled" src="images/disabled_' : '" src="images/'; |
---|
| 360 | $downDisabled = $i == count($widgets->elements()) - 1 ? ' disabled" src="images/disabled_' : '" src="images/'; |
---|
| 361 | $altUp = $i == 0 ? ' alt=""' : ' alt="' . __('Up the widget') . '"'; |
---|
| 362 | $altDown = $i == count($widgets->elements()) - 1 ? ' alt=""' : ' alt="' . __('Down the widget') . '"'; |
---|
[2778] | 363 | |
---|
[3709] | 364 | $iname = 'w[' . $pr . '][' . $i . ']'; |
---|
[2509] | 365 | |
---|
[3709] | 366 | $res .= |
---|
[3874] | 367 | '<li>' . form::hidden([$iname . '[id]'], html::escapeHTML($w->id())) . |
---|
| 368 | '<p class="widget-name">' . form::number([$iname . '[order]'], [ |
---|
[3725] | 369 | 'default' => $i, |
---|
| 370 | 'class' => 'hidden', |
---|
| 371 | 'extra_html' => 'title="' . __('order') . '"' |
---|
[3874] | 372 | ]) . |
---|
[3709] | 373 | ' ' . $w->name() . |
---|
| 374 | ($w->desc() != '' ? ' <span class="form-note">' . __($w->desc()) . '</span>' : '') . |
---|
| 375 | '<span class="toolsWidget remove-if-drag">' . |
---|
| 376 | '<input type="image" class="upWidget' . $upDisabled . 'up.png" name="' . $iname . '[_up]" value="' . __('Up the widget') . '"' . $altUp . ' /> ' . |
---|
| 377 | '<input type="image" class="downWidget' . $downDisabled . 'down.png" name="' . $iname . '[_down]" value="' . __('Down the widget') . '"' . $altDown . ' /> ' . ' ' . |
---|
| 378 | '<input type="image" class="removeWidget" src="images/trash.png" name="' . $iname . '[_rem]" value="' . __('Remove widget') . '" alt="' . __('Remove the widget') . '" />' . |
---|
| 379 | '</span>' . |
---|
| 380 | '<br class="clear"/></p>' . |
---|
| 381 | '<div class="widgetSettings hidden-if-drag">' . $w->formSettings($iname, $j) . '</div>' . |
---|
| 382 | '</li>'; |
---|
[2509] | 383 | |
---|
[3709] | 384 | $i++; |
---|
| 385 | $j++; |
---|
| 386 | } |
---|
[2509] | 387 | |
---|
[3709] | 388 | $res .= '</ul>'; |
---|
[2509] | 389 | |
---|
[3709] | 390 | $res .= '<ul class="sortable-delete"' . ($i > 0 ? '' : ' style="display: none;"') . '><li class="sortable-delete-placeholder">' . |
---|
| 391 | __('Drag widgets here to remove.') . '</li></ul>'; |
---|
[2509] | 392 | |
---|
[3709] | 393 | return $res; |
---|
[0] | 394 | } |
---|
| 395 | ?> |
---|
| 396 | </body> |
---|
[2509] | 397 | </html> |
---|