Add "delete identity" button in settings
authorChristian Weiske <cweiske@cweiske.de>
Mon, 29 May 2017 15:20:19 +0000 (17:20 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Mon, 29 May 2017 15:20:19 +0000 (17:20 +0200)
init.php
settings.js [new file with mode: 0644]
settings.phtml

index 2fbabab..4c0f598 100644 (file)
--- a/init.php
+++ b/init.php
@@ -110,11 +110,23 @@ class Micropub extends Plugin implements IHandler
         }
 
         $accounts = PluginHost::getInstance()->get($this, 'accounts', []);
+        if (isset($_REQUEST['accordion'])
+            && $_REQUEST['accordion'] == 'micropub'
+        ) {
+            $accordionActive = 'selected="true"';
+        } else {
+            $accordionActive = '';
+        }
 
         //FIXME: default identity
         include __DIR__ . '/settings.phtml';
     }
 
+    public function get_prefs_js()
+    {
+        return file_get_contents(__DIR__ . '/settings.js');
+    }
+
     /**
      * CLI command
      */
@@ -148,6 +160,8 @@ class Micropub extends Plugin implements IHandler
             return $this->authreturnAction();
         } else if ($mode == 'post') {
             return $this->postAction();
+        } else if ($mode == 'deleteIdentity') {
+            return $this->deleteIdentityAction();
         } else {
             return $this->errorOut('Unsupported mode');
         }
@@ -158,17 +172,17 @@ class Micropub extends Plugin implements IHandler
         if (!isset($_POST['me'])) {
             return $this->errorOut('"me" parameter missing');
         }
-        $me = $_POST['me'];
+        $me = trim($_POST['me']);
 
         if (!isset($_POST['replyTo'])) {
             return $this->errorOut('"replyTo" parameter missing');
         }
-        $replyTo = $_POST['replyTo'];
+        $replyTo = trim($_POST['replyTo']);
 
         if (!isset($_POST['content'])) {
             return $this->errorOut('"content" parameter missing');
         }
-        $content = $_POST['content'];
+        $content = trim($_POST['content']);
 
         $accounts = PluginHost::getInstance()->get($this, 'accounts', []);
         if (!isset($accounts[$me])) {
@@ -346,7 +360,39 @@ class Micropub extends Plugin implements IHandler
         $host->set($this, 'accounts', $accounts);
 
         //all fine now.
-        header('Location: prefs.php');
+        //the accordion parameter will never work
+        // because fox has serious mental problems
+        // https://discourse.tt-rss.org/t/open-a-certain-accordion-in-preferences-by-url-parameter/234
+        header('Location: prefs.php?accordion=micropub');
+    }
+
+    /**
+     * Backend preferences action: Remove a given account
+     */
+    protected function deleteIdentityAction()
+    {
+        if (!isset($_POST['me'])) {
+            return $this->errorOut('"me" parameter missing');
+        }
+        $me = trim($_POST['me']);
+
+        $host = PluginHost::getInstance();
+        $accounts = $host->get($this, 'accounts', []);
+        if (!isset($accounts[$me])) {
+            return $this->errorOut('Unknown identity');
+        }
+
+        unset($accounts[$me]);
+        $host->set($this, 'accounts', $accounts);
+        header('Content-type: application/json');
+
+        echo json_encode(
+            [
+                'code'     => '200',
+                'message'  => 'Identity removed',
+            ]
+        );
+        exit();
     }
 
     /**
diff --git a/settings.js b/settings.js
new file mode 100644 (file)
index 0000000..40657a2
--- /dev/null
@@ -0,0 +1,25 @@
+/**
+ * Delete a given account
+ */
+function micropubDeleteIdentity(accountUrl)
+{
+    new Ajax.Request('backend.php', {
+        parameters: {
+            'op':     'pluginhandler',
+            'plugin': 'micropub',
+            'method': 'action',
+            'mode':   'deleteIdentity',
+            'me':     accountUrl,
+        },
+        onSuccess: function(transport) {
+            notify_info('Account removed');
+            var elems = dojo.query('tr[data-url="' + accountUrl + '"]');
+            if (elems.length > 0) {
+                elems.first().remove();
+            }
+        },
+        onFailure: function(transport) {
+            notify_error(transport.responseText);
+        }
+    });
+}
index a06f2ea..a22a00b 100644 (file)
@@ -1,4 +1,4 @@
-<div dojoType="dijit.layout.AccordionPane" title="Micropub">
+<div dojoType="dijit.layout.AccordionPane" title="Micropub" id="micropub" <?= $accordionActive ?>>
  <div>
   <h2>Registered identities</h2>
   <table width="100%">
@@ -6,17 +6,21 @@
     <tr class="title">
      <td width="5%">Default</td>
      <td>Identity URL</td>
+     <td width="5%"></td>
     </tr>
    </thead>
    <tbody>
     <?php foreach ($accounts as $accurl => $account) { ?>
-     <tr>
+     <tr data-url="<?= htmlspecialchars($accurl) ?>">
       <td align='center'>
        <input disabled='1' dojoType="dijit.form.CheckBox" type="checkbox"/>
       </td>
       <td>
        <a href="<?= htmlspecialchars($accurl) ?>"><?= htmlspecialchars($accurl) ?></a>
       </td>
+      <td>
+       <a href="#" onclick='javascript:micropubDeleteIdentity(<?= json_encode($accurl) ?>)'>Remove</a>
+      </td>
      </tr>
     <?php } ?>
    </tbody>