Add File Upload field
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 25 Feb 2015 21:16:35 +0000 (16:16 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 25 Feb 2015 21:16:35 +0000 (16:16 -0500)
A lot of work just to add one field

Toolkit/Members/Admin/EditCourses.php
Toolkit/Members/EditCourses.php
Toolkit/Members/templates/addCourse.tpl
Toolkit/Members/templates/editCourse.tpl

index 520a384..8777baf 100644 (file)
@@ -340,6 +340,11 @@ class AddAdminCourse extends Toolkit_FormBuilder
         'image/gif',
         'image/png',
     );
+    protected $fileMimeTypes = array(
+        'application/pdf',
+        'application/msword',
+        'application/force-download'
+    );
 
     //    }}}
     //    {{{ __construct()
@@ -453,6 +458,29 @@ class AddAdminCourse extends Toolkit_FormBuilder
             'name'    => 'image',
             'display' => 'Upload a Course Photo / Image',
         );
+        $e[] = array(
+            'type'    => 'checkbox',
+            'req'     => false,
+            'name'    => 'remove_file_rmv',
+            'display' => 'Remove File',
+        );
+        $e[] = array(
+            'type'    => 'static',
+            'req'     => false,
+            'name'    => 'curr_file',
+            'display' => 'Current File',
+        );
+        $e[] = array(
+            'type'    => 'hidden',
+            'req'     => false,
+            'name'    => 'curr_file_rmv',
+        );
+        $e[] = array(
+            'type'    => 'file',
+            'req'     => false,
+            'name'    => 'file',
+            'display' => 'Upload a Course File',
+        );
         $e[] = array(
             'type' => 'header',
             'req' => false,
@@ -541,7 +569,6 @@ class AddAdminCourse extends Toolkit_FormBuilder
     {
         $r = array();
 
-        $checkDate = create_function('$d', '$d = implode("-", $d); return Validate::date($d, array("format" => "%n-%j-%Y"));');
         $r[] = array(
             'element'    => 'image',
             'message'    => 'ERROR: Incorrect File Type (.gif, .png, .jpg) only!',
@@ -551,6 +578,15 @@ class AddAdminCourse extends Toolkit_FormBuilder
             'reset'      => false,
             'force'      => false
         );
+        $r[] = array(
+            'element'    => 'file_rmv',
+            'message'    => 'ERROR: Incorrect File Type (.pdf, .doc) only!',
+            'type'       => 'mimetype',
+            'format'     => $this->fileMimeTypes,
+            'validation' => $this->validationType,
+            'reset'      => false,
+            'force'      => false
+        );
 
         $this->setupRules($r);
     }
@@ -614,6 +650,11 @@ class AddAdminCourse extends Toolkit_FormBuilder
 
     //  }}}
 
+    protected function deleteFile($file)
+    {
+        $fs = new Toolkit_FileServer_FileAdapter();
+        return $fs->delete($file);
+    }
     //    {{{ insertData()
 
     /**
@@ -671,12 +712,17 @@ class AddAdminCourse extends Toolkit_FormBuilder
 
         $values['pos']       = $courses->getListSize() + 1;
         $values['image'] = $e->getValue('curr_image_rmv');
+        $e =& $this->getElement('curr_file_rmv');
+        $values['file'] = $e->getValue('curr_file_rmv');
         $values['member_id'] = $_GET['id'];
         $values['pending']   = 0;
         unset($values['MAX_FILE_SIZE'],
               $values['curr_image_rmv'],
+              $values['curr_file_rmv'],
               $values['remove_img_rmv'],
               $values['add_rmv']);
+        //echo '<pre>'.print_r($values, true).'</pre>';
+        //exit;
 
         $this->tableMetaData = Toolkit_Common::getTableMetaData(
             $this->dbh,
@@ -735,6 +781,12 @@ class AddAdminCourse extends Toolkit_FormBuilder
     }
 
     //  }}}
+    function validNewFile(array $newFile)
+    {
+        return (   is_numeric($newFile['size'])
+                && $newFile['size'] > 0
+                && in_array($newFile['type'], $this->fileMimeTypes));
+    }
     //  {{{ removeOldImage()
 
     /**
@@ -798,6 +850,34 @@ class AddAdminCourse extends Toolkit_FormBuilder
     }
 
     //  }}}
+    protected function syncCurrFile()
+    {
+        $is = new Toolkit_Image_Server();
+
+        $delFile = $this->getSubmitValue('remove_file_rmv');
+        $oldFile = $this->getSubmitValue('curr_file_rmv');
+        $newFile = $this->getSubmitValue('file');
+
+        if ($delFile && $oldFile) {
+            $this->removeOldFile($is, $oldFile);
+            unset($oldFile);
+        } elseif ($oldFile && $this->validNewFile($newFile)) {
+            $this->removeOldFile($is, $oldFile);
+            unset($oldFile);
+        }
+
+        if ($this->validNewFile($newFile)) {
+            $fileData = $this->uploadFile('file');
+            $file = ($fileData['name']) ? $fileData['name']: null;
+        } else {
+            $file = $oldFile;
+        }
+
+        if ($file) {
+            $this->updateFileElements($file);
+            $this->showCurrFile = true;
+        }
+    }
     //    {{{ toHtml()
 
     /**
@@ -823,6 +903,7 @@ class AddAdminCourse extends Toolkit_FormBuilder
         //  their uploaded image in the form
         if ($this->isSubmitted()) {
             $this->syncCurrImage();
+            $this->syncCurrFile();
         }
 
         $this->setupRenderers($tEngine);
@@ -841,6 +922,20 @@ class AddAdminCourse extends Toolkit_FormBuilder
 
     //  {{{ updatePhotoElements()
 
+    public function updateFileElements($file)
+    {
+        //  Get the dimensions of the image
+        $s = MEMBER_ORIGINAL . $file;
+
+        //  Set the image to show in the element
+        $e =& $this->getElement('curr_file');
+        $e->setText('<a href="'.$s.'"></a>');
+
+        //  updated the hidden elements value to make sure it
+        //  holds the most up-to-date image name
+        $e =& $this->getElement('curr_file_rmv');
+        $e->setValue($file);
+    }
     /**
      * Short description for function
      *
@@ -873,6 +968,15 @@ class AddAdminCourse extends Toolkit_FormBuilder
     }
 
     //  }}}
+    protected function uploadFile($field)
+    {
+        $fs = new Toolkit_FileServer_FileAdapter();
+        try {
+            return $fs->upload($field);
+        } catch (Toolkit_FileServer_Exception $e) {
+            return;
+        }
+    }
     //  {{{ uploadImage()
 
     /**
@@ -976,6 +1080,11 @@ class EditAdminCourse extends Toolkit_FormBuilder
         'image/gif',
         'image/png',
     );
+    protected $fileMimeTypes = array(
+        'application/pdf',
+        'application/msword',
+        'application/force-download'
+    );
 
     //    }}}
     //    {{{ __construct()
@@ -1066,7 +1175,12 @@ class EditAdminCourse extends Toolkit_FormBuilder
         $defaults['curr_image_rmv'] = $defaults['image'];
         $img = '<img src="%s">';
         $defaults['curr_image'] = sprintf($img, MEMBER_PHOTOS . $defaults['image']);
-        $this->showCurrImg = $defaults['image'];
+        $this->showCurrImg  = $defaults['image'];
+
+        $defaults['curr_file_rmv'] = $defaults['file'];
+        $file = '<a href="%s">File</a>';
+        $defaults['curr_file'] = sprintf($file, MEMBER_ORIGINAL . $defaults['file']);
+        $this->showCurrFile = $defaults['file'];
         $this->setupDefaults($defaults);
     }
 
@@ -1123,6 +1237,29 @@ class EditAdminCourse extends Toolkit_FormBuilder
             'name'    => 'image',
             'display' => 'Upload a Course Photo / Image',
         );
+        $e[] = array(
+            'type'    => 'checkbox',
+            'req'     => false,
+            'name'    => 'remove_file_rmv',
+            'display' => 'Remove File',
+        );
+        $e[] = array(
+            'type'    => 'static',
+            'req'     => false,
+            'name'    => 'curr_file',
+            'display' => 'Current File',
+        );
+        $e[] = array(
+            'type'    => 'hidden',
+            'req'     => false,
+            'name'    => 'curr_file_rmv',
+        );
+        $e[] = array(
+            'type'    => 'file',
+            'req'     => false,
+            'name'    => 'file',
+            'display' => 'Upload a Course File',
+        );
         $e[] = array(
             'type'    => 'text',
             'req'     => false,
@@ -1211,30 +1348,20 @@ class EditAdminCourse extends Toolkit_FormBuilder
     {
         $r = array();
 
-        $checkDate = create_function('$d', '$d = implode("-", $d); return Validate::date($d, array("format" => "%n-%j-%Y"));');
         $r[] = array(
-            'element'    => 'sdate',
-            'message'    => 'ERROR: Invalid Date!',
-            'type'       => 'callback',
-            'format'     => $checkDate,
-            'validation' => $this->validationType,
-            'reset'      => false,
-            'force'      => false
-        );
-        $r[] = array(
-            'element'    => 'edate',
-            'message'    => 'ERROR: Invalid Date!',
-            'type'       => 'callback',
-            'format'     => $checkDate,
+            'element'    => 'image',
+            'message'    => 'ERROR: Incorrect File Type (.gif, .png, .jpg) only!',
+            'type'       => 'mimetype',
+            'format'     => $this->mimeTypes,
             'validation' => $this->validationType,
             'reset'      => false,
             'force'      => false
         );
         $r[] = array(
-            'element'    => 'image',
-            'message'    => 'ERROR: Incorrect File Type (.gif, .png, .jpg) only!',
+            'element'    => 'file_rmv',
+            'message'    => 'ERROR: Incorrect File Type (.pdf, .doc) only!',
             'type'       => 'mimetype',
-            'format'     => $this->mimeTypes,
+            'format'     => $this->fileMimeTypes,
             'validation' => $this->validationType,
             'reset'      => false,
             'force'      => false
@@ -1304,6 +1431,11 @@ class EditAdminCourse extends Toolkit_FormBuilder
 
     //  }}}
 
+    protected function deleteFile($file)
+    {
+        $fs = new Toolkit_FileServer_FileAdapter();
+        return $fs->delete($file);
+    }
     //    {{{ processData()
 
     /**
@@ -1320,12 +1452,22 @@ class EditAdminCourse extends Toolkit_FormBuilder
         $cache->remove("Member-{$_GET['id']}", 'Profile');
 
         $e =& $this->getElement('curr_image_rmv');
-
         $values['image'] = $e->getValue('curr_image_rmv');
-        unset($values['MAX_FILE_SIZE'],
-              $values['curr_image_rmv'],
-              $values['remove_img_rmv'],
-              $values['add_rmv']);
+
+        $e =& $this->getElement('curr_file_rmv');
+        $values['file'] = $e->getValue('curr_file_rmv');
+
+        unset(
+            $values['MAX_FILE_SIZE'],
+            $values['curr_image_rmv'],
+            $values['remove_img_rmv'],
+            $values['curr_file_rmv'],
+            $values['remove_file_rmv'],
+            $values['add_rmv']
+        );
+
+        //echo '<pre>'.print_r($values, true).'</pre>';
+        //exit;
 
         $this->tableMetaData = Toolkit_Common::getTableMetaData(
             $this->dbh,
@@ -1355,10 +1497,11 @@ class EditAdminCourse extends Toolkit_FormBuilder
         $renderer = new HTML_QuickForm_Renderer_ObjectFlexy($tEngine);
 
         $this->accept($renderer);
-        $this->view              = new stdClass();
-        $this->view->courseId    = $this->courseId;
-        $this->view->showCurrImg = $this->showCurrImg;
-        $this->view->form        = $renderer->toObject();
+        $this->view               = new stdClass();
+        $this->view->courseId     = $this->courseId;
+        $this->view->showCurrImg  = $this->showCurrImg;
+        $this->view->showCurrFile = $this->showCurrFile;
+        $this->view->form         = $renderer->toObject();
         $tEngine->compile($this->formTemplate);
     }
 
@@ -1384,6 +1527,12 @@ class EditAdminCourse extends Toolkit_FormBuilder
     }
 
     //  }}}
+    function validNewFile(array $newFile)
+    {
+        return (   is_numeric($newFile['size'])
+                && $newFile['size'] > 0
+                && in_array($newFile['type'], $this->fileMimeTypes));
+    }
     //  {{{ removeOldImage()
 
     /**
@@ -1408,6 +1557,15 @@ class EditAdminCourse extends Toolkit_FormBuilder
     }
 
     //  }}}
+    function removeOldFile(Toolkit_Image_Server $is, $oldFile)
+    {
+        $this->deleteFile($oldFile);
+        if ($this->elementExists('curr_file_rmv')) {
+            $e =& $this->getElement('curr_file_rmv');
+            $e->setValue(null);
+            $this->_submitValues['curr_file_rmv'] = null;
+        }
+    }
     //  {{{ syncCurrImage()
 
     /**
@@ -1448,6 +1606,42 @@ class EditAdminCourse extends Toolkit_FormBuilder
 
     //  }}}
 
+    /**
+     * Short description for function
+     *
+     * Long description (if any) ...
+     *
+     * @return void
+     * @access protected
+     */
+    protected function syncCurrFile()
+    {
+        $is = new Toolkit_Image_Server();
+
+        $delFile = $this->getSubmitValue('remove_file_rmv');
+        $oldFile = $this->getSubmitValue('curr_file_rmv');
+        $newFile = $this->getSubmitValue('file');
+
+        if ($delFile && $oldFile) {
+            $this->removeOldFile($is, $oldFile);
+            unset($oldFile);
+        } elseif ($oldFile && $this->validNewFile($newFile)) {
+            $this->removeOldFile($is, $oldFile);
+            unset($oldFile);
+        }
+
+        if ($this->validNewFile($newFile)) {
+            $fileData = $this->uploadFile('file');
+            $file = ($fileData['name']) ? $fileData['name']: null;
+        } else {
+            $file = $oldFile;
+        }
+
+        if ($file) {
+            $this->updateFileElements($file);
+            $this->showCurrFile = true;
+        }
+    }
     //    {{{ toHtml()
 
     /**
@@ -1473,6 +1667,7 @@ class EditAdminCourse extends Toolkit_FormBuilder
         //  their uploaded image in the form
         if ($this->isSubmitted()) {
             $this->syncCurrImage();
+            $this->syncCurrFile();
         }
 
         $this->setupRenderers($tEngine);
@@ -1588,6 +1783,20 @@ class EditAdminCourse extends Toolkit_FormBuilder
     }
 
     //  }}}
+    public function updateFileElements($file)
+    {
+        //  Get the dimensions of the image
+        $s = MEMBER_ORIGINAL . $file;
+
+        //  Set the image to show in the element
+        $e =& $this->getElement('curr_file');
+        $e->setText('<a href="'.$s.'"></a>');
+
+        //  updated the hidden elements value to make sure it
+        //  holds the most up-to-date image name
+        $e =& $this->getElement('curr_file_rmv');
+        $e->setValue($file);
+    }
     //  {{{ uploadImage()
 
     /**
@@ -1607,5 +1816,14 @@ class EditAdminCourse extends Toolkit_FormBuilder
     }
 
     //  }}}
+    protected function uploadFile($field)
+    {
+        $fs = new Toolkit_FileServer_FileAdapter();
+        try {
+            return $fs->upload($field);
+        } catch (Toolkit_FileServer_Exception $e) {
+            return;
+        }
+    }
 }
 ?>
index 4894730..10eab78 100644 (file)
@@ -1641,6 +1641,7 @@ class EditCourse extends Toolkit_FormBuilder
 
         $this->accept($renderer);
         $this->view              = new StdClass;
+        $this->view->courseId    = $this->courseId;
         $this->view->showCurrImg = $this->showCurrImg;
         $this->view->form        = $renderer->toObject();
         $this->template->compile($this->formTemplate);
index dbb7df1..748e9b9 100644 (file)
             {form.holes9.html:h}
 
         </div>
+        <div class="coursePhoto">
+            <div flexy:if="showCurrFile">
+                <label>
+                    {form.remove_file_rmv.html:h}
+                    {form.remove_file_rmv.label:h}
+                </label>
+
+                <i>{form.curr_file.label:h}</i>
+                {form.curr_file.html:h}
+            </div>
+            <i>{form.file.label:h}</i>
+            <div flexy:if="form.file.error" class="req">
+                {form.file.error:h}
+            </div>
+            {form.file.html:h}
+        </div>
         <div class="submitArea"> {form.add_rmv.html:h} </div>
     </form>
 </div>
index 2f505df..569a84e 100644 (file)
             {form.holes9.html:h}
 
         </div>
+        <div class="coursePhoto">
+            <div flexy:if="showCurrFile">
+                <label>
+                    {form.remove_file_rmv.html:h}
+                    {form.remove_file_rmv.label:h}
+                </label>
+
+                <i>{form.curr_file.label:h}</i>
+                {form.curr_file.html:h}
+            </div>
+            <i>{form.file.label:h}</i>
+            <div flexy:if="form.file.error" class="req">
+                {form.file.error:h}
+            </div>
+            {form.file.html:h}
+        </div>
         <div class="submitArea">
             {form.add_rmv.html:h}
             {form.remove_rmv.html:h}