From fb7685fb53589ebe099201297c0e8dbf20d89c59 Mon Sep 17 00:00:00 2001
From: Andreas Gohr <andi@splitbrain.org>
Date: Sat, 16 May 2015 19:52:26 +0200
Subject: [PATCH] more improvements

---
 lib/plugins/styler/action.php | 50 +++++++++++++++++++++--------------
 lib/plugins/styler/admin.php  | 16 ++++++-----
 lib/plugins/styler/script.js  | 11 ++++----
 3 files changed, 46 insertions(+), 31 deletions(-)

diff --git a/lib/plugins/styler/action.php b/lib/plugins/styler/action.php
index e2385e739..88e2d0912 100644
--- a/lib/plugins/styler/action.php
+++ b/lib/plugins/styler/action.php
@@ -9,6 +9,14 @@
 // must be run within Dokuwiki
 if(!defined('DOKU_INC')) die();
 
+/**
+ * Class action_plugin_styler
+ *
+ * This handles all the save actions and loading the interface
+ *
+ * All this usually would be done within an admin plugin, but we want to have this available outside
+ * the admin interface using our floating dialog.
+ */
 class action_plugin_styler extends DokuWiki_Action_Plugin {
 
     /**
@@ -18,10 +26,8 @@ class action_plugin_styler extends DokuWiki_Action_Plugin {
      * @return void
      */
     public function register(Doku_Event_Handler $controller) {
-
         $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax');
         $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_action');
-
     }
 
     /**
@@ -33,6 +39,8 @@ class action_plugin_styler extends DokuWiki_Action_Plugin {
      * @return void
      */
     public function handle_action(Doku_Event &$event, $param) {
+        if(!auth_isadmin()) return;
+
         $event->data = act_clean($event->data);
         if($event->data === 'styler_plugin_preview') {
             $event->data = 'show';
@@ -49,6 +57,26 @@ class action_plugin_styler extends DokuWiki_Action_Plugin {
         }
     }
 
+    /**
+     * [Custom event handler which performs action]
+     *
+     * @param Doku_Event $event  event object by reference
+     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
+     *                           handler was registered]
+     * @return void
+     */
+
+    public function handle_ajax(Doku_Event &$event, $param) {
+        if(!auth_isadmin()) return;
+        if($event->data != 'plugin_styler') return;
+        $event->preventDefault();
+        $event->stopPropagation();
+
+        /** @var admin_plugin_styler $hlp */
+        $hlp = plugin_load('admin', 'styler');
+        $hlp->form();
+    }
+
     /**
      * saves the preview.ini
      */
@@ -119,24 +147,6 @@ class action_plugin_styler extends DokuWiki_Action_Plugin {
         io_saveFile($ini, "$old\n\n$new");
     }
 
-    /**
-     * [Custom event handler which performs action]
-     *
-     * @param Doku_Event $event  event object by reference
-     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
-     *                           handler was registered]
-     * @return void
-     */
-
-    public function handle_ajax(Doku_Event &$event, $param) {
-        if($event->data != 'plugin_styler') return;
-        $event->preventDefault();
-        $event->stopPropagation();
-
-        /** @var admin_plugin_styler $hlp */
-        $hlp = plugin_load('admin', 'styler');
-        $hlp->html();
-    }
 
 }
 
diff --git a/lib/plugins/styler/admin.php b/lib/plugins/styler/admin.php
index 323bf61ec..4be2153ab 100644
--- a/lib/plugins/styler/admin.php
+++ b/lib/plugins/styler/admin.php
@@ -29,13 +29,21 @@ class admin_plugin_styler extends DokuWiki_Admin_Plugin {
      * Should carry out any processing required by the plugin.
      */
     public function handle() {
-        set_doku_pref('styler_plugin', 1);
     }
 
     /**
      * Render HTML output, e.g. helpful text and a form
      */
     public function html() {
+        echo '<div id="plugin__styler">';
+        $this->form();
+        echo '</div>';
+    }
+
+    /**
+     * Create the actual editing form
+     */
+    public function form() {
         global $conf;
         $tpl = $conf['template'];
         define('SIMPLE_TEST',1); // hack, ideally certain functions should be moved out of css.php
@@ -50,7 +58,7 @@ class admin_plugin_styler extends DokuWiki_Admin_Plugin {
         } else {
             echo '<p>Intro blah... for the currently active template ("'.$tpl.'")... not all variables preview...</p>';
 
-            echo '<form class="styler" id="plugin__styler" method="post">';
+            echo '<form class="styler" method="post">';
             echo '<h2>Template variables</h2>';
             echo '<table>';
             foreach($replacements as $key => $value){
@@ -66,12 +74,8 @@ class admin_plugin_styler extends DokuWiki_Admin_Plugin {
             echo '<input type="submit" name="do[styler_plugin_save]" value="save">';
             echo '</form>';
         }
-
-
-
     }
 
-
 }
 
 // vim:ts=4:sw=4:et:
\ No newline at end of file
diff --git a/lib/plugins/styler/script.js b/lib/plugins/styler/script.js
index d09a8b8da..b3cadfd5e 100644
--- a/lib/plugins/styler/script.js
+++ b/lib/plugins/styler/script.js
@@ -1,10 +1,12 @@
 jQuery(function () {
+    // user openend the admin page, set cookie and redirect
+    if(jQuery('#plugin__styler').length) {
+        DokuCookie.setValue('styler_plugin', 1);
+        document.location.href = DOKU_BASE;
+    }
 
-
+    // The Styler Dialog is currently enabled, display it here and apply the preview styles
     if (DokuCookie.getValue('styler_plugin') == 1) {
-
-
-
         // load dialog
         var $dialog = jQuery(document.createElement('div'));
         jQuery('body').append($dialog);
@@ -35,6 +37,5 @@ jQuery(function () {
                 });
             }
         );
-
     }
 });
\ No newline at end of file
-- 
GitLab