From 91041a5d46610433cbbe49ed17167c3b204bc729 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Wed, 20 Aug 2014 22:58:39 -0500
Subject: [PATCH] forms: Use new upload widget for FAQ articles

---
 include/class.faq.php                |  7 +++----
 include/staff/cannedresponse.inc.php |  8 ++++----
 include/staff/faq.inc.php            | 28 ++++++++++++----------------
 scp/canned.php                       | 10 +++++-----
 scp/faq.php                          |  9 +++++++++
 5 files changed, 33 insertions(+), 29 deletions(-)

diff --git a/include/class.faq.php b/include/class.faq.php
index 78d221c21..c2a195891 100644
--- a/include/class.faq.php
+++ b/include/class.faq.php
@@ -171,7 +171,7 @@ class FAQ {
         $this->updateTopics($vars['topics']);
 
         //Delete removed attachments.
-        $keepers = $vars['files']?$vars['files']:array();
+        $keepers = $vars['files'];
         if(($attachments = $this->attachments->getSeparates())) {
             foreach($attachments as $file) {
                 if($file['id'] && !in_array($file['id'], $keepers))
@@ -179,9 +179,8 @@ class FAQ {
             }
         }
 
-        //Upload new attachments IF any.
-        if($_FILES['attachments'] && ($files=AttachmentFile::format($_FILES['attachments'])))
-            $this->attachments->upload($files);
+        // Upload new attachments IF any.
+        $this->attachments->upload($keepers);
 
         // Inline images (attached to the draft)
         $this->attachments->deleteInlines();
diff --git a/include/staff/cannedresponse.inc.php b/include/staff/cannedresponse.inc.php
index 0b6a81ccd..6be5a63b1 100644
--- a/include/staff/cannedresponse.inc.php
+++ b/include/staff/cannedresponse.inc.php
@@ -92,14 +92,14 @@ $info=Format::htmlchars(($errors && $_POST)?$_POST:$info);
                 <div class="error"><?php echo $errors['files']; ?></div>
                 </div>
                 <?php
-                if($canned && ($files=$canned->attachments->getSeparates())) {
-                    $attachments = $canned_form->getField('attachments');
+                $attachments = $canned_form->getField('attachments');
+                if ($canned && ($files=$canned->attachments->getSeparates())) {
                     $ids = array();
                     foreach ($files as $f)
                         $ids[] = $f['id'];
                     $attachments->value = $ids;
-                    print $attachments->render();
-                } ?>
+                }
+                print $attachments->render(); ?>
                 <br/>
             </td>
         </tr>
diff --git a/include/staff/faq.inc.php b/include/staff/faq.inc.php
index e3f8dcfc9..4fcea8138 100644
--- a/include/staff/faq.inc.php
+++ b/include/staff/faq.inc.php
@@ -96,24 +96,20 @@ $info=Format::htmlchars(($errors && $_POST)?$_POST:$info);
         </tr>
         <tr>
             <td colspan=2>
-                <div><b><?php echo __('Attachments');?></b> (<?php echo __('optional');?>) <font class="error">&nbsp;<?php echo $errors['files']; ?></font></div>
+                <div><h3><?php echo __('Attachments');?>
+                    <span class="faded">(<?php echo __('optional');?>)</span></h3>
+                    <div class="error"><?php echo $errors['files']; ?></div>
+                </div>
                 <?php
-                if($faq && ($files=$faq->attachments->getSeparates())) {
-                    echo '<div class="faq_attachments"><span class="faded">'.__('Uncheck to delete the attachment on submit').'</span><br>';
-                    foreach($files as $file) {
-                        $hash=$file['key'].md5($file['id'].session_id().strtolower($file['key']));
-                        echo sprintf('<label><input type="checkbox" name="files[]" id="f%d" value="%d" checked="checked">
-                                      <a href="file.php?h=%s">%s</a>&nbsp;&nbsp;</label>&nbsp;',
-                                      $file['id'], $file['id'], $hash, $file['name']);
-                    }
-                    echo '</div><br>';
+                $attachments = $faq_form->getField('attachments');
+                if ($faq && ($files=$faq->attachments->getSeparates())) {
+                    $ids = array();
+                    foreach ($files as $f)
+                        $ids[] = $f['id'];
+                    $attachments->value = $ids;
                 }
-                ?>
-                <div class="faded"><?php echo __('Select files to upload.');?></div>
-                <div class="uploads"></div>
-                <div class="file_input">
-                    <input type="file" class="multifile" name="attachments[]" size="30" value="" />
-                </div>
+                print $attachments->render(); ?>
+                <br/>
             </td>
         </tr>
         <?php
diff --git a/scp/canned.php b/scp/canned.php
index 57a51fb0f..4a5447525 100644
--- a/scp/canned.php
+++ b/scp/canned.php
@@ -45,7 +45,7 @@ if($_POST && $thisstaff->canManageCannedResponses()) {
                     __('this canned response'));
                 //Delete removed attachments.
                 //XXX: files[] shouldn't be changed under any circumstances.
-                $keepers = $_POST['files']?$_POST['files']:array();
+                $keepers = $canned_form->getField('attachments')->getClean();
                 $attachments = $canned->attachments->getSeparates(); //current list of attachments.
                 foreach($attachments as $k=>$file) {
                     if($file['id'] && !in_array($file['id'], $keepers)) {
@@ -53,8 +53,7 @@ if($_POST && $thisstaff->canManageCannedResponses()) {
                     }
                 }
                 //Upload NEW attachments IF ANY - TODO: validate attachment types??
-                $attachments = $canned_form->getField('attachments')->getClean();
-                if ($attachments)
+                if ($keepers)
                     $canned->attachments->upload($attachments);
 
                 // Attach inline attachments from the editor
@@ -85,8 +84,9 @@ if($_POST && $thisstaff->canManageCannedResponses()) {
                 $msg=sprintf(__('Successfully added %s'), Format::htmlchars($_POST['title']));
                 $_REQUEST['a']=null;
                 //Upload attachments
-                if($_FILES['attachments'] && ($c=Canned::lookup($id)) && ($files=AttachmentFile::format($_FILES['attachments'])))
-                    $c->attachments->upload($files);
+                $keepers = $canned_form->getField('attachments')->getClean();
+                if ($keepers && ($c=Canned::lookup($id)))
+                    $c->attachments->upload($keepers);
 
                 // Attach inline attachments from the editor
                 if (isset($_POST['draft_id'])
diff --git a/scp/faq.php b/scp/faq.php
index 47c61203d..2a3c2e951 100644
--- a/scp/faq.php
+++ b/scp/faq.php
@@ -23,8 +23,16 @@ if($_REQUEST['id'] && !($faq=FAQ::lookup($_REQUEST['id'])))
 if($_REQUEST['cid'] && !$faq && !($category=Category::lookup($_REQUEST['cid'])))
     $errors['err']=sprintf(__('%s: Unknown or invalid'), __('FAQ category'));
 
+$faq_form = new Form(array(
+    'attachments' => new FileUploadField(array('id'=>'attach',
+        'configuration'=>array('extensions'=>false,
+            'size'=>$cfg->getMaxFileSize())
+   )),
+));
+
 if($_POST):
     $errors=array();
+    $_POST['files'] = $faq_form->getField('attachments')->getClean();
     switch(strtolower($_POST['do'])) {
         case 'create':
         case 'add':
@@ -108,5 +116,6 @@ $ost->addExtraHeader('<meta name="tip-namespace" content="' . $tip_namespace . '
     "$('#content').data('tipNamespace', '".$tip_namespace."');");
 require_once(STAFFINC_DIR.'header.inc.php');
 require_once(STAFFINC_DIR.$inc);
+print $faq_form->getMedia();
 require_once(STAFFINC_DIR.'footer.inc.php');
 ?>
-- 
GitLab