aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaria Carolin Chabowski <laria@laria.me>2020-02-15 23:48:26 +0100
committerLaria Carolin Chabowski <laria@laria.me>2020-02-15 23:48:26 +0100
commit7f84e36fb2f6a380635bc773eae32aa42203422c (patch)
treeda87ac3d533d64aa7371b89ff61522c0710a3cc0
parentd24d5f2e21fd4bfb5c369e2932a4c62328ffa758 (diff)
downloadmicropoly-7f84e36fb2f6a380635bc773eae32aa42203422c.tar.gz
micropoly-7f84e36fb2f6a380635bc773eae32aa42203422c.tar.bz2
micropoly-7f84e36fb2f6a380635bc773eae32aa42203422c.zip
Improve attachments
- Allow multiple uploads at once - Make uploads deletable
-rw-r--r--assets/interactive.js19
-rw-r--r--assets/styles.css5
-rw-r--r--src/DbQuery.php5
-rw-r--r--src/Handlers/NoteHandler.php10
-rw-r--r--src/Models/Attachment.php2
-rw-r--r--templates/macros.twig37
6 files changed, 64 insertions, 14 deletions
diff --git a/assets/interactive.js b/assets/interactive.js
index 05b7556..404cc26 100644
--- a/assets/interactive.js
+++ b/assets/interactive.js
@@ -102,7 +102,7 @@ $(function () {
this.set_visible(false);
return;
}
-
+
this.get_suggestions(input_text, function (suggestions) {
console.log(suggestions);
suggestions = Array.prototype.slice.call(suggestions, 0);
@@ -289,5 +289,20 @@ $(function () {
$("button.confirm").on("click", function (ev) {
if (!window.confirm($(this).data("question")))
ev.preventDefault();
- })
+ });
+
+ function updateAttachmentDeletionClass(tr) {
+ tr = $(tr);
+ tr.toggleClass("delete", tr.find(".attachment-delete-checkbox")[0].checked);
+ }
+
+ function attachmentDeleteCheckbox(tr) {
+ $(tr).each(function () {
+ updateAttachmentDeletionClass(this);
+ }).find(".attachment-delete-checkbox").on("change", function () {
+ updateAttachmentDeletionClass($(this).closest("tr"));
+ });
+ }
+
+ attachmentDeleteCheckbox($(".attachments tbody tr"));
}); \ No newline at end of file
diff --git a/assets/styles.css b/assets/styles.css
index b9fca4d..3008b4e 100644
--- a/assets/styles.css
+++ b/assets/styles.css
@@ -229,4 +229,9 @@ input:placeholder-shown {
.autocomplete-root:not(.show-suggestions) ul {
display: none;
+}
+
+.attachments .delete {
+ font-style: italic;
+ text-decoration: line-through;
} \ No newline at end of file
diff --git a/src/DbQuery.php b/src/DbQuery.php
index 6fd32a1..dddb581 100644
--- a/src/DbQuery.php
+++ b/src/DbQuery.php
@@ -194,4 +194,9 @@ class DbQuery
return $out;
});
}
+
+ public function fetchValues(SQLite3 $db): array
+ {
+ return array_map(fn ($row) => $row[0], $this->fetchRows($db));
+ }
} \ No newline at end of file
diff --git a/src/Handlers/NoteHandler.php b/src/Handlers/NoteHandler.php
index 4ce3179..726b773 100644
--- a/src/Handlers/NoteHandler.php
+++ b/src/Handlers/NoteHandler.php
@@ -35,6 +35,16 @@ class NoteHandler implements Handler
$note->setTags($_POST["tag"]);
$note->save($db);
+ $deleteAttachments = $_POST['attachment_delete'] ?? [];
+ $deleteAttachments = array_filter($deleteAttachments, fn ($ok) => (bool)(int)$ok);
+ $deleteAttachments = array_keys($deleteAttachments);
+ $deleteAttachments = Attachment::byIds($db, $deleteAttachments);
+ $deleteAttachments = array_filter($deleteAttachments, fn (Attachment $att) => $att->getNoteId() === $note->getId());
+
+ /** @var Attachment $att */
+ foreach ($deleteAttachments as $att)
+ $att->delete($db, $env->attachmentsPath());
+
if (isset($_FILES['attachments']))
Attachment::createFromUploads($env->db(), $env->attachmentsPath(), $note, $_FILES['attachments']);
}
diff --git a/src/Models/Attachment.php b/src/Models/Attachment.php
index b627d33..bbd8fc4 100644
--- a/src/Models/Attachment.php
+++ b/src/Models/Attachment.php
@@ -70,7 +70,7 @@ class Attachment
LEFT JOIN note_attachments na
ON na.hash = a.hash
WHERE na.id IS NULL
- "))->fetchRows($db) as $hash) {
+ "))->fetchValues($db) as $hash) {
self::deleteFileByHash($attachmentPath, $hash);
}
diff --git a/templates/macros.twig b/templates/macros.twig
index 4e1b336..2c72334 100644
--- a/templates/macros.twig
+++ b/templates/macros.twig
@@ -39,17 +39,32 @@
</fieldset>
<div class="attachments-container">
<h2>Attachments</h2>
- <ul class="attachment-list">
- {% for att in note.attachments %}
- <li>
- <a href="{{ url("attachments/%s", att.id) }}">{{ att.fileName ? att.fileName : att.id }}</a>
- </li>
- {% endfor %}
- </ul>
-
- <ul class="attachment-inputs">
- <li><input type="file" name="attachments[]"></li>
- </ul>
+ <table class="attachments">
+ <thead>
+ <tr>
+ <th>Delete</th>
+ <th>File name</th>
+ </tr>
+ </thead>
+ <tbody>
+ {% for att in note.attachments %}
+ <tr class="existing">
+ <td>
+ <input type="checkbox" class="attachment-delete-checkbox" value="1" name="attachment_delete[{{ att.id }}]">
+ </td>
+ <td>
+ <a href="{{ url("attachments/%s", att.id) }}">{{ att.fileName ? att.fileName : att.id }}</a>
+ </td>
+ </tr>
+ {% endfor %}
+ <tr class="new">
+ <td></td>
+ <td>
+ <input type="file" multiple name="attachments[]">
+ </td>
+ </tr>
+ </tbody>
+ </table>
</div>
{% endmacro %}