From 373bba0a3525e1cc53707e36716fd9ece345fcf7 Mon Sep 17 00:00:00 2001 From: Jared Hancock Date: Tue, 3 Jan 2017 14:17:02 -0600 Subject: [PATCH] thread: Allow edits to the attachments of thread entry This allows administrators with access to a thread and staff members access to edit the attachments of a thread entry item. Non administrators can only edit the attachments if they own the thread entry. This is helpful if the incorrect attachments were submitted with a posting, or if the file names should be edited, or if additional attachments should have been added with the posting. Edits to responses will not be resent to the end user. Instead, the posting should be edited afterwards and then resent. Perhaps a note could be added to the amended responses to indicate that the attachments were modified. --- thread-action-attachments/plugin.php | 13 ++ thread-action-attachments/tea_attachments.php | 163 ++++++++++++++++++ .../templates/attachment.tmpl.php | 25 +++ .../thread-entry-attachments.tmpl.php | 44 +++++ 4 files changed, 245 insertions(+) create mode 100644 thread-action-attachments/plugin.php create mode 100644 thread-action-attachments/tea_attachments.php create mode 100644 thread-action-attachments/templates/attachment.tmpl.php create mode 100644 thread-action-attachments/templates/thread-entry-attachments.tmpl.php diff --git a/thread-action-attachments/plugin.php b/thread-action-attachments/plugin.php new file mode 100644 index 0000000..cd910cb --- /dev/null +++ b/thread-action-attachments/plugin.php @@ -0,0 +1,13 @@ + 'thread-entry:attachments', # notrans + 'version' => '0.1', + 'name' => /* trans */ 'Attachment management plugin for thread entries', + 'author' => 'Jared Hancock', + 'description' => /* trans */ 'Allows administrators to add, remove, and rename attachments to thread entries', + 'url' => 'http://www.osticket.com/plugins/thread-entry/attachments', + 'plugin' => 'tea_attachments.php:TEA_AttachmentsPlugin' +); + +?> diff --git a/thread-action-attachments/tea_attachments.php b/thread-action-attachments/tea_attachments.php new file mode 100644 index 0000000..829baee --- /dev/null +++ b/thread-action-attachments/tea_attachments.php @@ -0,0 +1,163 @@ +entry->attachments); + } + + function isEnabled() { + global $thisstaff; + + // Only an administrator with access to the thread, or the owner of the + // thread item can perform this action + $T = $this->entry->getThread()->getObject(); + return $T->checkStaffPerm($thisstaff) + && ($thisstaff->isAdmin() + || $thisstaff->staff_id == $this->entry->staff_id + ); + } + + function getJsStub() { + return sprintf(<<getAjaxUrl()); + } + + function trigger() { + switch ($_SERVER['REQUEST_METHOD']) { + case 'GET': + return $this->trigger__get(); + case 'POST': + return $this->trigger__post(); + } + } + + protected function getNewAttachmentsForm() { + if (!isset($this->__form)) { + $this->__form = new SimpleForm(array( + 'new_uploads' => new FileUploadField(array( + 'id' => 'attach', + 'name'=>'attach:thread-entry', + )), + ), $_POST); + } + return $this->__form; + } + + protected function trigger__get($errors=array()) { + global $cfg, $thisstaff; + + $poster = $this->entry->getStaff(); + $action = str_replace('ajax.php/','#', $this->getAjaxUrl()); + $new_attachments = $this->getNewAttachmentsForm(); + + include 'templates/thread-entry-attachments.tmpl.php'; + } + + protected function trigger__post() { + global $thisstaff; + + // Sanity check first + $errors = array('attachment' => array()); + $attachments = array(); + foreach ($_POST['attachment-id'] as $i=>$id) { + if (isset($_POST['attachment-keep'][$id])) { + if (!($name = trim($_POST['attachment-name'][$id]))) { + $errors['attachment'][$id] + = __('File name is required'); + } + else { + $attachments[$id] = trim(Format::striptags( + $_POST['attachment-name'][$id] + )); + } + } + } + + // Add new attachments + $new_attachments = $this->getNewAttachmentsForm(); + $clean = $new_attachments->getField('new_uploads')->getClean(); + if ($clean) { + // XXX: Arrgh. ThreadEntry::normalizeFileInfo is protected... + $files = array(); + foreach ($clean as $name=>$id) { + $file = AttachmentFile::lookup($id); + $files[] = array( + 'id' => $id, + 'key' => $file->key, + // ThreadEntry::createAttachment checks if name differs + 'name' => $name, + 'file' => $file, + 'inline' => false, + ); + } + + if ($new = $this->entry->createAttachments($files)) { + // Keep these new ones + foreach ($new as $attach) + $attachments[$attach->id] = $attach->getFilename(); + } + else { + $errors['attachment'][0] = true; + Messages::error(__('Unable to save new attachments')); + } + } + + if ($errors['attachment']) { + return $this->trigger__get($errors); + } + + foreach ($this->entry->attachments as $attach) { + $id = $attach->id; + if (!isset($attachments[$id])) { + $attach->delete(); + } + elseif ($attach->getFilename() != $attachments[$id]) { + // If the file was renamed to be the same as the original file + // name, just remove the edited name + if ($attach->file->getName() == $attachments[$id]) { + $attach->name = null; + } + else { + $attach->name = $attachments[$id]; + } + $attach->save(); + } + } + + // Re-render the thread entry with the new attachment info + $entry = $this->entry; + ob_start(); + include STAFFINC_DIR . 'templates/thread-entry.tmpl.php'; + $content = ob_get_clean(); + + Http::response('201', JsonDataEncoder::encode(array( + 'thread_id' => $this->entry->id, + 'entry' => $content, + ))); + } +} + +class TEA_AttachmentsPlugin +extends Plugin { + function bootstrap() { + ThreadEntry::registerAction(/* trans */ 'Manage', 'TEA_ManageAttachments'); + } +} diff --git a/thread-action-attachments/templates/attachment.tmpl.php b/thread-action-attachments/templates/attachment.tmpl.php new file mode 100644 index 0000000..7ab7146 --- /dev/null +++ b/thread-action-attachments/templates/attachment.tmpl.php @@ -0,0 +1,25 @@ +
+ + +id] as $err) { ?> +
+ +
+
diff --git a/thread-action-attachments/templates/thread-entry-attachments.tmpl.php b/thread-action-attachments/templates/thread-entry-attachments.tmpl.php new file mode 100644 index 0000000..90cb0bd --- /dev/null +++ b/thread-action-attachments/templates/thread-entry-attachments.tmpl.php @@ -0,0 +1,44 @@ +

+ +
+
+ +
+
+ + +
+ + +
+
+ entry->getAttachments() as $attach) { + include 'attachment.tmpl.php'; + } + ?> +
+ +
+ asTable(); + ?> +
+ +
+

+ + + + + + + +

+
+
+