Allow setting a default account
authorChristian Weiske <cweiske@cweiske.de>
Tue, 30 May 2017 16:14:18 +0000 (18:14 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Tue, 30 May 2017 16:14:18 +0000 (18:14 +0200)
commentform.phtml
init.php
settings.js
settings.phtml

index d09e49f360c2af67bd2f9716caf6303d4f0afcca..636fa51252a7094eeac86f2f27aecbf7fa2214b9 100644 (file)
@@ -38,7 +38,7 @@
    <?php print_hidden('me', htmlspecialchars(reset($accounts))); ?>
   <?php } else { ?>
    <label for="micropub-me">Identity:</label>
-   <?php print_select('micropub-me', null, $accounts,
+   <?php print_select('micropub-me', $defaultAccount, $accountUrls,
      'dojoType="dijit.form.Select" size="1"', 'me'); ?>
    <br/>
   <?php } ?>
index 4c0f598aae9b061ba1bf9a48d3b1a19b821645ac..7fced8e926b1f43394faef56f2b4683642359b15 100644 (file)
--- a/init.php
+++ b/init.php
@@ -83,10 +83,19 @@ class Micropub extends Plugin implements IHandler
             . '?reply=' . urlencode($article['link']);
         // did I tell you I hate dojo/dijit?
 
-        $accounts = array_keys(PluginHost::getInstance()->get($this, 'accounts', []));
+        $accounts = PluginHost::getInstance()->get($this, 'accounts', []);
         if (!count($accounts)) {
             return $article;
         }
+
+        $accountUrls = array_keys($accounts);
+        $defaultAccount = null;
+        foreach ($accounts as $url => $account) {
+            if ($account['default']) {
+                $defaultAccount = $url;
+            }
+        }
+
         ob_start();
         include __DIR__ . '/commentform.phtml';
         $html = ob_get_clean();
@@ -118,6 +127,13 @@ class Micropub extends Plugin implements IHandler
             $accordionActive = '';
         }
 
+        foreach ($accounts as $url => $account) {
+            $accounts[$url]['checked'] = '';
+            if ($account['default']) {
+                $accounts[$url]['checked'] = 'checked="checked"';
+            }
+        }
+
         //FIXME: default identity
         include __DIR__ . '/settings.phtml';
     }
@@ -162,6 +178,8 @@ class Micropub extends Plugin implements IHandler
             return $this->postAction();
         } else if ($mode == 'deleteIdentity') {
             return $this->deleteIdentityAction();
+        } else if ($mode == 'setDefaultIdentity') {
+            return $this->setDefaultIdentityAction();
         } else {
             return $this->errorOut('Unsupported mode');
         }
@@ -395,6 +413,37 @@ class Micropub extends Plugin implements IHandler
         exit();
     }
 
+    /**
+     * Backend preferences action: Make a given account the default
+     */
+    protected function setDefaultIdentityAction()
+    {
+        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');
+        }
+
+        foreach ($accounts as $url => $data) {
+            $accounts[$url]['default'] = ($url == $me);
+        }
+        $host->set($this, 'accounts', $accounts);
+        header('Content-type: application/json');
+
+        echo json_encode(
+            [
+                'code'     => '200',
+                'message'  => 'Default account set',
+            ]
+        );
+        exit();
+    }
+
     /**
      * Send an error message.
      * Automatically in the correct format (plain text or json)
index 40657a20a03052c7338dbe1119b3268bb731bb7f..1f53d77eec9c8100a3c30cafe411dcabcc19cb2b 100644 (file)
@@ -23,3 +23,29 @@ function micropubDeleteIdentity(accountUrl)
         }
     });
 }
+
+/**
+ * Mark account as the default account
+ */
+function micropubMakeDefault(accountUrl, checkbox)
+{
+    if (!checkbox.checked) {
+        return;
+    }
+
+    new Ajax.Request('backend.php', {
+        parameters: {
+            'op':     'pluginhandler',
+            'plugin': 'micropub',
+            'method': 'action',
+            'mode':   'setDefaultIdentity',
+            'me':     accountUrl,
+        },
+        onSuccess: function(transport) {
+            notify_info('Default account changed');
+        },
+        onFailure: function(transport) {
+            notify_error(transport.responseText);
+        }
+    });
+}
index a22a00bfb349bad77842daa3c7d2c2e4d5dbbd13..97e3b8fdf52418df89228ce162aa4513dccf06c6 100644 (file)
     <?php foreach ($accounts as $accurl => $account) { ?>
      <tr data-url="<?= htmlspecialchars($accurl) ?>">
       <td align='center'>
-       <input disabled='1' dojoType="dijit.form.CheckBox" type="checkbox"/>
+       <input type="radio" href="#" dojoType="dijit.form.RadioButton"
+              name="micropubDefaultIdentity"
+              value="<?= htmlspecialchars($accurl) ?>"
+              <?= $account['checked'] ?>
+              onchange='micropubMakeDefault(<?= json_encode($accurl) ?>, this)'
+          />
       </td>
       <td>
        <a href="<?= htmlspecialchars($accurl) ?>"><?= htmlspecialchars($accurl) ?></a>