nieuws: apply tag changes to article links in edit mode
authorMischa POSLAWSKY <perl@shiar.org>
Mon, 17 Sep 2018 15:12:17 +0000 (17:12 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Mon, 17 Sep 2018 19:01:37 +0000 (21:01 +0200)
edit/nieuws/tag/index.php [new file with mode: 0644]
nieuws/edit.js
nieuws/index.php

diff --git a/edit/nieuws/tag/index.php b/edit/nieuws/tag/index.php
new file mode 100644 (file)
index 0000000..d4edb71
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+ob_clean();
+
+if (empty($User['admin']))
+       abort("geen beheersrechten", '401 unauthorised');
+
+if (!$_POST)
+       abort("niets te doen", '405 post error');
+if (!$Args)
+       abort("pagina niet opgegeven", '409 input error');
+
+$pagename = ltrim($Args, '/').'.html';
+@list ($category, $year, $article) = explode('/', $pagename);
+if (empty($article) or !file_exists($pagename))
+       abort("artikel onbekend: $pagename", '404 page error');
+
+if (!isset($_REQUEST['value']))
+       abort("ongeldige tagwaarde {$_REQUEST['value']}", '403 missing value');
+
+if (!isset($_REQUEST['tag']))
+       abort("geen tag ontvangen", '403 input error');
+
+$target = "$category/.tags/{$_REQUEST['tag']}";
+if (!file_exists($target))
+       abort("tag onbekend: $target", '404 tag error');
+
+$target .= "/$year-$article";
+if (is_link($target) or file_exists($target)) {
+       if (!unlink($target))
+               abort("kon bestaande link niet weghalen op $target", '500 unlink error');
+}
+
+$pagelink = "../../../$pagename";
+if ($_REQUEST['value'] and !symlink($pagelink, $target))
+       abort("fout bij link aanmaken op $target", '500 link error');
+
+abort("Tag {$_REQUEST['tag']} aangepast");
+
index 0061b025a315b61608a034178177ead4ea1a1236..947de7b6a3b22001a3a365d9a484a577d2b17e92 100644 (file)
@@ -16,4 +16,33 @@ document.addEventListener('DOMContentLoaded', () => {
                };
                overview.appendChild(editlink);
        }
+
+       var editlink = document.querySelector('a[href="#edit"]');
+       if (!editlink) return;
+       editlink.addEventListener('click', () => {
+               document.querySelectorAll('.tags input').forEach(tagoption => {
+                       tagoption.removeAttribute('disabled');
+                       tagoption.addEventListener('change', () => {
+                               let editpost = '/edit/nieuws/tag' + window.location.pathname;
+                               let params = new URLSearchParams;
+                               params.append('tag', tagoption.value);
+                               params.append('value', tagoption.checked ? 1 : 0);
+                               fetch(editpost, {
+                                       method: 'POST',
+                                       body: params,
+                                       credentials: 'same-origin',
+                               })
+                               .then(res => {
+                                       if (res.status != 200) {
+                                               return res.text().then(body => {
+                                                       throw `foutcode ${res.status}: ${body}`;
+                                               });
+                                       }
+                               })
+                               .catch(error => {
+                                       alert(`Tag aanpassen mislukt: ${error}`);
+                               });
+                       });
+               });
+       });
 });
index 1a520112da6663f1805f602829469f6c65a1ee99..ad85011716c9825248995d28d4573d12ac67a5c8 100644 (file)
@@ -24,14 +24,14 @@ if ($page and !is_numeric($page)) {
                        $tagtarget = is_writable($tagpath);
                        $taglist[] = sprintf(
                                '<input type="checkbox" name="%s" value="%s" id="%s"%s%s />' .
-                               '<label for="%2$s"> %s</label>',
+                               '<label for="%3$s"> %s</label>',
                                "tags[$tagname]", $tagname, "tag-$tagname",
                                $tagvalue ? ' checked' : '',
-                               ' onclick="return false"',
+                               ' disabled',
                                ucfirst($tagname)
                        );
                }
-               printf("<p><strong>Tags:</strong> %s</p>\n",
+               printf('<p class="tags"><strong>Tags:</strong> %s</p>'."\n",
                        implode("\n\t", $taglist)
                );
        }