| 1 | <?php |
|---|
| 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 | |
|---|
| 12 | if (!defined('DC_CONTEXT_ADMIN')) {return;} |
|---|
| 13 | |
|---|
| 14 | include dirname(__FILE__) . '/_default_widgets.php'; |
|---|
| 15 | |
|---|
| 16 | # Loading navigation, extra widgets and custom widgets |
|---|
| 17 | $widgets_nav = null; |
|---|
| 18 | if ($core->blog->settings->widgets->widgets_nav) { |
|---|
| 19 | $widgets_nav = dcWidgets::load($core->blog->settings->widgets->widgets_nav); |
|---|
| 20 | } |
|---|
| 21 | $widgets_extra = null; |
|---|
| 22 | if ($core->blog->settings->widgets->widgets_extra) { |
|---|
| 23 | $widgets_extra = dcWidgets::load($core->blog->settings->widgets->widgets_extra); |
|---|
| 24 | } |
|---|
| 25 | $widgets_custom = null; |
|---|
| 26 | if ($core->blog->settings->widgets->widgets_custom) { |
|---|
| 27 | $widgets_custom = dcWidgets::load($core->blog->settings->widgets->widgets_custom); |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | $append_combo = [ |
|---|
| 31 | '-' => 0, |
|---|
| 32 | __('navigation') => 'nav', |
|---|
| 33 | __('extra') => 'extra', |
|---|
| 34 | __('custom') => 'custom' |
|---|
| 35 | ]; |
|---|
| 36 | |
|---|
| 37 | function literalNullString($v) |
|---|
| 38 | { |
|---|
| 39 | if ($v == '') { |
|---|
| 40 | return '<' . __('empty string') . '>'; |
|---|
| 41 | } |
|---|
| 42 | return $v; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | # Adding widgets to sidebars |
|---|
| 46 | if (!empty($_POST['append']) && is_array($_POST['addw'])) { |
|---|
| 47 | # Filter selection |
|---|
| 48 | $addw = []; |
|---|
| 49 | foreach ($_POST['addw'] as $k => $v) { |
|---|
| 50 | if (($v == 'extra' || $v == 'nav' || $v == 'custom') && $__widgets->{$k} !== null) { |
|---|
| 51 | $addw[$k] = $v; |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 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 | } |
|---|
| 61 | |
|---|
| 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 | } |
|---|
| 73 | |
|---|
| 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 | } |
|---|
| 89 | |
|---|
| 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 | } |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | # Removing ? |
|---|
| 104 | $removing = false; |
|---|
| 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 | } |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | # Move ? |
|---|
| 117 | $move = false; |
|---|
| 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 | } |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | # Update sidebars |
|---|
| 144 | if (!empty($_POST['wup']) || $removing || $move) { |
|---|
| 145 | if (!isset($_POST['w']) || !is_array($_POST['w'])) { |
|---|
| 146 | $_POST['w'] = []; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 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 | } |
|---|
| 160 | |
|---|
| 161 | if (!isset($_POST['w']['nav'])) { |
|---|
| 162 | $_POST['w']['nav'] = []; |
|---|
| 163 | } |
|---|
| 164 | if (!isset($_POST['w']['extra'])) { |
|---|
| 165 | $_POST['w']['extra'] = []; |
|---|
| 166 | } |
|---|
| 167 | if (!isset($_POST['w']['custom'])) { |
|---|
| 168 | $_POST['w']['custom'] = []; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 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); |
|---|
| 174 | |
|---|
| 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(); |
|---|
| 180 | |
|---|
| 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(); |
|---|
| 194 | |
|---|
| 195 | dcPage::addSuccessNotice(__('Sidebars have been resetting.')); |
|---|
| 196 | http::redirect($p_url); |
|---|
| 197 | } catch (Exception $e) { |
|---|
| 198 | $core->error->add($e->getMessage()); |
|---|
| 199 | } |
|---|
| 200 | } |
|---|
| 201 | ?> |
|---|
| 202 | <html> |
|---|
| 203 | <head> |
|---|
| 204 | <title><?php echo __('Widgets'); ?></title> |
|---|
| 205 | <?php |
|---|
| 206 | $widget_editor = $core->auth->getOption('editor'); |
|---|
| 207 | $rte_flag = true; |
|---|
| 208 | $rte_flags = @$core->auth->user_prefs->interface->rte_flags; |
|---|
| 209 | if (is_array($rte_flags) && in_array('widgets_text', $rte_flags)) { |
|---|
| 210 | $rte_flag = $rte_flags['widgets_text']; |
|---|
| 211 | } |
|---|
| 212 | echo dcPage::cssLoad(dcPage::getPF('widgets/style.css')) . |
|---|
| 213 | dcPage::jsLoad('js/jquery/jquery-ui.custom.js') . |
|---|
| 214 | dcPage::jsLoad('js/jquery/jquery.ui.touch-punch.js') . |
|---|
| 215 | dcPage::jsJson('widgets', [ |
|---|
| 216 | 'widget_noeditor' => ($rte_flag ? 0 : 1), |
|---|
| 217 | 'msg' => ['confirm_widgets_reset' => __('Are you sure you want to reset sidebars?')] |
|---|
| 218 | ]) . |
|---|
| 219 | dcPage::jsLoad(dcPage::getPF('widgets/js/widgets.js')); |
|---|
| 220 | |
|---|
| 221 | $core->auth->user_prefs->addWorkspace('accessibility'); |
|---|
| 222 | $user_dm_nodragdrop = $core->auth->user_prefs->accessibility->nodragdrop; |
|---|
| 223 | if (!$user_dm_nodragdrop) { |
|---|
| 224 | echo dcPage::jsLoad(dcPage::getPF('widgets/js/dragdrop.js')); |
|---|
| 225 | } |
|---|
| 226 | if ($rte_flag) { |
|---|
| 227 | echo $core->callBehavior('adminPostEditor', $widget_editor['xhtml'], 'widget', |
|---|
| 228 | ['#sidebarsWidgets textarea:not(.noeditor)'], 'xhtml'); |
|---|
| 229 | } |
|---|
| 230 | echo (dcPage::jsConfirmClose('sidebarsWidgets')); |
|---|
| 231 | ?> |
|---|
| 232 | </head> |
|---|
| 233 | <body> |
|---|
| 234 | <?php |
|---|
| 235 | echo dcPage::breadcrumb( |
|---|
| 236 | [ |
|---|
| 237 | html::escapeHTML($core->blog->name) => '', |
|---|
| 238 | __('Widgets') => '' |
|---|
| 239 | ]) . |
|---|
| 240 | dcPage::notices(); |
|---|
| 241 | |
|---|
| 242 | # All widgets |
|---|
| 243 | echo |
|---|
| 244 | '<form id="listWidgets" action="' . $p_url . '" method="post" class="widgets">' . |
|---|
| 245 | '<h3>' . __('Available widgets') . '</h3>' . |
|---|
| 246 | '<p>' . __('Drag widgets from this list to one of the sidebars, for add.') . '</p>' . |
|---|
| 247 | '<ul id="widgets-ref">'; |
|---|
| 248 | |
|---|
| 249 | $j = 0; |
|---|
| 250 | foreach ($__widgets->elements(true) as $w) { |
|---|
| 251 | echo |
|---|
| 252 | '<li>' . form::hidden(['w[void][0][id]'], html::escapeHTML($w->id())) . |
|---|
| 253 | '<p class="widget-name">' . form::number(['w[void][0][order]'], [ |
|---|
| 254 | 'default' => 0, |
|---|
| 255 | 'class' => 'hide', |
|---|
| 256 | 'extra_html' => 'title="' . __('order') . '"' |
|---|
| 257 | ]) . |
|---|
| 258 | ' ' . $w->name() . |
|---|
| 259 | ($w->desc() != '' ? ' <span class="form-note">' . __($w->desc()) . '</span>' : '') . '</p>' . |
|---|
| 260 | '<p class="manual-move remove-if-drag"><label class="classic">' . __('Append to:') . '</label> ' . |
|---|
| 261 | form::combo(['addw[' . $w->id() . ']'], $append_combo) . |
|---|
| 262 | '<input type="submit" name="append[' . $w->id() . ']" value="' . __('Add') . '" /></p>' . |
|---|
| 263 | '<div class="widgetSettings hidden-if-drag">' . $w->formSettings('w[void][0]', $j) . '</div>' . |
|---|
| 264 | '</li>'; |
|---|
| 265 | $j++; |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | echo |
|---|
| 269 | '</ul>' . |
|---|
| 270 | '<p>' . $core->formNonce() . '</p>' . |
|---|
| 271 | '<p class="remove-if-drag"><input type="submit" name="append" value="' . __('Add widgets to sidebars') . '" /></p>' . |
|---|
| 272 | '</form>'; |
|---|
| 273 | |
|---|
| 274 | echo '<form id="sidebarsWidgets" action="' . $p_url . '" method="post">'; |
|---|
| 275 | # Nav sidebar |
|---|
| 276 | echo |
|---|
| 277 | '<div id="sidebarNav" class="widgets fieldset">' . |
|---|
| 278 | sidebarWidgets('dndnav', __('Navigation sidebar'), $widgets_nav, 'nav', $__default_widgets['nav'], $j); |
|---|
| 279 | echo '</div>'; |
|---|
| 280 | |
|---|
| 281 | # Extra sidebar |
|---|
| 282 | echo |
|---|
| 283 | '<div id="sidebarExtra" class="widgets fieldset">' . |
|---|
| 284 | sidebarWidgets('dndextra', __('Extra sidebar'), $widgets_extra, 'extra', $__default_widgets['extra'], $j); |
|---|
| 285 | echo '</div>'; |
|---|
| 286 | |
|---|
| 287 | # Custom sidebar |
|---|
| 288 | echo |
|---|
| 289 | '<div id="sidebarCustom" class="widgets fieldset">' . |
|---|
| 290 | sidebarWidgets('dndcustom', __('Custom sidebar'), $widgets_custom, 'custom', $__default_widgets['custom'], $j); |
|---|
| 291 | echo '</div>'; |
|---|
| 292 | |
|---|
| 293 | echo |
|---|
| 294 | '<p id="sidebarsControl">' . |
|---|
| 295 | $core->formNonce() . |
|---|
| 296 | '<input type="submit" name="wup" value="' . __('Update sidebars') . '" /> ' . |
|---|
| 297 | '<input type="submit" class="reset" name="wreset" value="' . __('Reset sidebars') . '" /></p>' . |
|---|
| 298 | '</form>'; |
|---|
| 299 | |
|---|
| 300 | $widget_elements = new stdClass; |
|---|
| 301 | $widget_elements->content = '<dl>'; |
|---|
| 302 | foreach ($__widgets->elements() as $w) { |
|---|
| 303 | $widget_elements->content .= |
|---|
| 304 | '<dt><strong>' . html::escapeHTML($w->name()) . '</strong> (' . |
|---|
| 305 | __('Widget ID:') . ' <strong>' . html::escapeHTML($w->id()) . '</strong>)' . |
|---|
| 306 | ($w->desc() != '' ? ' <span class="form-note">' . __($w->desc()) . '</span>' : '') . '</dt>' . |
|---|
| 307 | '<dd>'; |
|---|
| 308 | |
|---|
| 309 | $w_settings = $w->settings(); |
|---|
| 310 | if (empty($w_settings)) { |
|---|
| 311 | $widget_elements->content .= '<p>' . __('No setting for this widget') . '</p>'; |
|---|
| 312 | } else { |
|---|
| 313 | $widget_elements->content .= '<ul>'; |
|---|
| 314 | foreach ($w->settings() as $n => $s) { |
|---|
| 315 | switch ($s['type']) { |
|---|
| 316 | case 'check': |
|---|
| 317 | $s_type = __('boolean') . ", " . __('possible values:') . " <code>0</code> " . __('or') . " <code>1</code>"; |
|---|
| 318 | break; |
|---|
| 319 | case 'combo': |
|---|
| 320 | $s['options'] = array_map("literalNullString", $s['options']); |
|---|
| 321 | $s_type = __('listitem') . ", " . __('possible values:') . " <code>" . implode('</code>, <code>', $s['options']) . "</code>"; |
|---|
| 322 | break; |
|---|
| 323 | case 'text': |
|---|
| 324 | case 'textarea': |
|---|
| 325 | default: |
|---|
| 326 | $s_type = __('string'); |
|---|
| 327 | break; |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | $widget_elements->content .= |
|---|
| 331 | '<li>' . |
|---|
| 332 | __('Setting name:') . ' <strong>' . html::escapeHTML($n) . '</strong>' . |
|---|
| 333 | ' (' . $s_type . ')' . |
|---|
| 334 | '</li>'; |
|---|
| 335 | } |
|---|
| 336 | $widget_elements->content .= '</ul>'; |
|---|
| 337 | } |
|---|
| 338 | $widget_elements->content .= '</dd>'; |
|---|
| 339 | } |
|---|
| 340 | $widget_elements->content .= '</dl></div>'; |
|---|
| 341 | |
|---|
| 342 | dcPage::helpBlock('widgets', $widget_elements); |
|---|
| 343 | |
|---|
| 344 | function sidebarWidgets($id, $title, $widgets, $pr, $default_widgets, &$j) |
|---|
| 345 | { |
|---|
| 346 | $res = '<h3>' . $title . '</h3>'; |
|---|
| 347 | |
|---|
| 348 | if (!($widgets instanceof dcWidgets)) { |
|---|
| 349 | $widgets = $default_widgets; |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | $res .= '<ul id="' . $id . '" class="connected">'; |
|---|
| 353 | |
|---|
| 354 | $res .= '<li class="empty-widgets" ' . (!$widgets->isEmpty() ? 'style="display: none;"' : '') . '>' . __('No widget as far.') . '</li>'; |
|---|
| 355 | |
|---|
| 356 | $i = 0; |
|---|
| 357 | foreach ($widgets->elements() as $w) { |
|---|
| 358 | $upDisabled = $i == 0 ? ' disabled" src="images/disabled_' : '" src="images/'; |
|---|
| 359 | $downDisabled = $i == count($widgets->elements()) - 1 ? ' disabled" src="images/disabled_' : '" src="images/'; |
|---|
| 360 | $altUp = $i == 0 ? ' alt=""' : ' alt="' . __('Up the widget') . '"'; |
|---|
| 361 | $altDown = $i == count($widgets->elements()) - 1 ? ' alt=""' : ' alt="' . __('Down the widget') . '"'; |
|---|
| 362 | |
|---|
| 363 | $iname = 'w[' . $pr . '][' . $i . ']'; |
|---|
| 364 | |
|---|
| 365 | $res .= |
|---|
| 366 | '<li>' . form::hidden([$iname . '[id]'], html::escapeHTML($w->id())) . |
|---|
| 367 | '<p class="widget-name">' . form::number([$iname . '[order]'], [ |
|---|
| 368 | 'default' => $i, |
|---|
| 369 | 'class' => 'hidden', |
|---|
| 370 | 'extra_html' => 'title="' . __('order') . '"' |
|---|
| 371 | ]) . |
|---|
| 372 | ' ' . $w->name() . |
|---|
| 373 | ($w->desc() != '' ? ' <span class="form-note">' . __($w->desc()) . '</span>' : '') . |
|---|
| 374 | '<span class="toolsWidget remove-if-drag">' . |
|---|
| 375 | '<input type="image" class="upWidget' . $upDisabled . 'up.png" name="' . $iname . '[_up]" value="' . __('Up the widget') . '"' . $altUp . ' /> ' . |
|---|
| 376 | '<input type="image" class="downWidget' . $downDisabled . 'down.png" name="' . $iname . '[_down]" value="' . __('Down the widget') . '"' . $altDown . ' /> ' . ' ' . |
|---|
| 377 | '<input type="image" class="removeWidget" src="images/trash.png" name="' . $iname . '[_rem]" value="' . __('Remove widget') . '" alt="' . __('Remove the widget') . '" />' . |
|---|
| 378 | '</span>' . |
|---|
| 379 | '<br class="clear"/></p>' . |
|---|
| 380 | '<div class="widgetSettings hidden-if-drag">' . $w->formSettings($iname, $j) . '</div>' . |
|---|
| 381 | '</li>'; |
|---|
| 382 | |
|---|
| 383 | $i++; |
|---|
| 384 | $j++; |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| 387 | $res .= '</ul>'; |
|---|
| 388 | |
|---|
| 389 | $res .= '<ul class="sortable-delete"' . ($i > 0 ? '' : ' style="display: none;"') . '><li class="sortable-delete-placeholder">' . |
|---|
| 390 | __('Drag widgets here to remove.') . '</li></ul>'; |
|---|
| 391 | |
|---|
| 392 | return $res; |
|---|
| 393 | } |
|---|
| 394 | ?> |
|---|
| 395 | </body> |
|---|
| 396 | </html> |
|---|