From 3da71de4414788401ee5d4642c8c6ad2912de047 Mon Sep 17 00:00:00 2001 From: Jared Hancock <jared@osticket.com> Date: Wed, 20 Aug 2014 18:43:08 -0500 Subject: [PATCH] forms: Use file upload widget for canned response attachments and notes --- include/class.forms.php | 19 ++++++++++++------- include/staff/ticket-view.inc.php | 9 +++------ js/filedrop.field.js | 7 ++++--- scp/js/scp.js | 11 +++-------- scp/tickets.php | 12 ++++++++++-- 5 files changed, 32 insertions(+), 26 deletions(-) diff --git a/include/class.forms.php b/include/class.forms.php index 3de1c8292..af724db32 100644 --- a/include/class.forms.php +++ b/include/class.forms.php @@ -417,7 +417,7 @@ class FormField { return substr(md5( session_id() . '-field-id-'.$this->get('id')), -16); else - return $this->get('id'); + return $this->get('name') ?: $this->get('id'); } function setForm($form) { @@ -1263,13 +1263,17 @@ class FileUploadField extends FormField { function loadSystemDefaultConfig() { global $cfg; - $this->_config = array( + return array( 'max' => $cfg->getStaffMaxFileUploads(), 'size' => $cfg->getMaxFileSize(), 'extensions' => $cfg->getAllowedFileTypes(), ); } + function getConfiguration() { + return parent::getConfiguration() ?: $this->loadSystemDefaultConfig(); + } + function upload() { $config = $this->getConfiguration(); @@ -1722,6 +1726,7 @@ class FileUploadWidget extends Widget { function render($how) { $config = $this->field->getConfiguration(); $name = $this->field->getFormName(); + $id = substr(md5(spl_object_hash($this)), 10); $attachments = $this->field->getFiles(); $files = array(); foreach ($this->value ?: array() as $id) { @@ -1742,18 +1747,18 @@ class FileUploadWidget extends Widget { ); } } - ?><div id="filedrop-<?php echo $name; + ?><div id="<?php echo $id; ?>" class="filedrop"><div class="files"></div> <div class="dropzone"><i class="icon-upload"></i> Drop files here or <a href="#" class="manual">choose them</a></div></div> - <input type="file" id="file-<?php echo $name; ?>" style="display:none;"/> + <input type="file" multiple id="file-<?php echo $id; ?>" style="display:none;"/> <script type="text/javascript"> - $(function(){$('#filedrop-<?php echo $name; ?> .dropzone').filedropbox({ + $(function(){$('#<?php echo $id; ?> .dropzone').filedropbox({ url: 'ajax.php/form/upload/<?php echo $this->field->get('id') ?>', - link: $('#filedrop-<?php echo $name; ?>').find('a.manual'), + link: $('#<?php echo $id; ?>').find('a.manual'), paramname: 'upload[]', - fallback_id: 'file-<?php echo $name; ?>', + fallback_id: 'file-<?php echo $id; ?>', allowedfileextensions: '<?php echo $config['extensions']; ?>'.split(/,\s*/), maxfiles: <?php echo $config['max'] ?: 20; ?>, diff --git a/include/staff/ticket-view.inc.php b/include/staff/ticket-view.inc.php index 442ca0882..32c7d7e6d 100644 --- a/include/staff/ticket-view.inc.php +++ b/include/staff/ticket-view.inc.php @@ -695,7 +695,6 @@ print $response_form->getField('attachments')->render(); class="richtext ifhtml draft draft-delete"><?php echo $info['note']; ?></textarea> <span class="error"><?php echo $errors['note']; ?></span> - <br> </td> </tr> <?php @@ -705,11 +704,9 @@ print $response_form->getField('attachments')->render(); <label for="attachment"><?php echo __('Attachments');?>:</label> </td> <td class="attachments"> - <div class="uploads"> - </div> - <div class="file_input"> - <input type="file" class="multifile" name="attachments[]" size="30" value="" /> - </div> +<?php +print $note_form->getField('attachments')->render(); +?> </td> </tr> <?php diff --git a/js/filedrop.field.js b/js/filedrop.field.js index 5b0249527..79983c7f1 100644 --- a/js/filedrop.field.js +++ b/js/filedrop.field.js @@ -11,7 +11,7 @@ progressUpdated: $.proxy(this.progressUpdated, this), speedUpdated: $.proxy(this.speedUpdated, this), dragOver: $.proxy(this.dragOver, this), - drop: $.proxy(this.dragLeave, this), + drop: $.proxy(this.drop, this), error: $.proxy(this.handleError, this) }; @@ -77,14 +77,14 @@ size /= 1024; suffix = sizes.shift(); } - return size.toPrecision(3) + suffix + 'B'; + return (suffix ? size.toPrecision(3) + suffix : size) + 'B'; }, addNode: function(file) { var filenode = $('<div class="file"></div>') .append($('<div class="filetype"></div>').addClass()) .append($('<div class="filename"></div>').text(file.name) .append($('<span class="filesize"></span>').text( - this.fileSize(file.size) + this.fileSize(parseInt(file.size)) )).append($('<div class="upload-rate pull-right"></div>')) ).append($('<div class="progress"></div>') .append($('<div class="progress-bar"></div>')) @@ -210,6 +210,7 @@ (opts.link || this).on('click', function(e){ $('#' + opts.fallback_id).trigger(e); + return false; }); $('#' + opts.fallback_id).change(function(e) { diff --git a/scp/js/scp.js b/scp/js/scp.js index dcd8b80f5..a4ca3146d 100644 --- a/scp/js/scp.js +++ b/scp/js/scp.js @@ -230,15 +230,10 @@ var scp_prep = function() { } //Canned attachments. if(canned.files && $('.canned_attachments',fObj).length) { + var fdb = $('#filedrop-attach .dropzone').data('dropbox'); $.each(canned.files,function(i, j) { - if(!$('.canned_attachments #f'+j.id,fObj).length) { - var file='<span><label><input type="checkbox" name="cannedattachments[]" value="' + j.id+'" id="f'+j.id+'" checked="checked">'; - file+= ' '+ j.name + '</label>'; - file+= ' (<a target="_blank" class="no-pjax" href="file.php?h=' + j.key + j.hash + '">view</a>) </span>'; - $('.canned_attachments', fObj).append(file); - } - - }); + fdb.addNode(j); + }); } } }) diff --git a/scp/tickets.php b/scp/tickets.php index 8815c3209..944a12e31 100644 --- a/scp/tickets.php +++ b/scp/tickets.php @@ -43,6 +43,9 @@ if ($_REQUEST['uid']) $response_form = new Form(array( 'attachments' => new FileUploadField(array('id'=>'attach')) )); +$note_form = new Form(array( + 'attachments' => new FileUploadField(array('id'=>'attach')) +)); //At this stage we know the access status. we can process the post. if($_POST && !$errors): @@ -179,13 +182,18 @@ if($_POST && !$errors): break; case 'postnote': /* Post Internal Note */ $vars = $_POST; - if($_FILES['attachments']) - $vars['files'] = AttachmentFile::format($_FILES['attachments']); + $attachments = $response_form->getField('attachments')->getClean(); + $vars['cannedattachments'] = array_merge( + $vars['cannedattachments'] ?: array(), $attachments); $wasOpen = ($ticket->isOpen()); if(($note=$ticket->postNote($vars, $errors, $thisstaff))) { $msg=__('Internal note posted successfully'); + // Clear attachment list + $note_form->setSource(array()); + $note_form->getField('attachments')->reset(); + if($wasOpen && $ticket->isClosed()) $ticket = null; //Going back to main listing. else -- GitLab