Update for member only edit courses
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 26 Feb 2015 15:41:12 +0000 (10:41 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 26 Feb 2015 15:41:12 +0000 (10:41 -0500)
Get the file upload to work.

Toolkit/Members/EditCourses.php

index 10eab78..5dece66 100644 (file)
@@ -302,6 +302,11 @@ class AddCourse extends Toolkit_FormBuilder
         'image/gif',
         'image/png',
     );
+    protected $fileMimeTypes = array(
+        'application/pdf',
+        'application/msword',
+        'application/force-download'
+    );
 
     //    }}}
     //    {{{ __construct()
@@ -420,6 +425,36 @@ class AddCourse 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,
+            'name' => 'GolfingHdr',
+            'display' => 'Golf Course Information',
+            //'col2' => true
+        );
         $e[] = array(
             'type'    => 'text',
             'req'     => false,
@@ -501,7 +536,6 @@ class AddCourse 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!',
@@ -511,6 +545,15 @@ class AddCourse 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);
     }
@@ -796,9 +839,12 @@ class AddCourse extends Toolkit_FormBuilder
         $courses->createMemberList();
         $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'] = $GLOBALS['memberAuth']->getAuthData('member_id');
         unset($values['MAX_FILE_SIZE'],
               $values['curr_image_rmv'],
+              $values['curr_file_rmv'],
               $values['remove_img_rmv'],
               $values['add_rmv']);
 
@@ -862,6 +908,12 @@ class AddCourse extends Toolkit_FormBuilder
     }
 
     //  }}}
+    function validNewFile(array $newFile)
+    {
+        return (   is_numeric($newFile['size'])
+                && $newFile['size'] > 0
+                && in_array($newFile['type'], $this->fileMimeTypes));
+    }
     //  {{{ removeOldImage()
 
 
@@ -927,6 +979,34 @@ class AddCourse 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()
 
     /**
@@ -950,6 +1030,7 @@ class AddCourse extends Toolkit_FormBuilder
         //  their uploaded image in the form
         if ($this->isSubmitted()) {
             $this->syncCurrImage();
+            $this->syncCurrFile();
         }
 
         $this->setupRenderers();
@@ -968,7 +1049,20 @@ class AddCourse 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
      *
@@ -1001,6 +1095,15 @@ class AddCourse extends Toolkit_FormBuilder
     }
 
     //  }}}
+    protected function uploadFile($field)
+    {
+        $fs = new Toolkit_FileServer_FileAdapter();
+        try {
+            return $fs->upload($field);
+        } catch (Toolkit_FileServer_Exception $e) {
+            return;
+        }
+    }
     //  {{{ uploadImage()
 
 
@@ -1107,6 +1210,11 @@ class EditCourse extends Toolkit_FormBuilder
         'image/gif',
         'image/png',
     );
+    protected $fileMimeTypes = array(
+        'application/pdf',
+        'application/msword',
+        'application/force-download'
+    );
 
     //    }}}
     //    {{{ __construct()
@@ -1219,6 +1327,10 @@ class EditCourse extends Toolkit_FormBuilder
         }
 
         $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);
     }
 
@@ -1269,6 +1381,35 @@ class EditCourse extends Toolkit_FormBuilder
             'req'     => false,
             'name'    => 'curr_image_rmv',
         );
+        $e[] = array(
+            'type'    => 'file',
+            'req'     => false,
+            '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,
@@ -1326,12 +1467,6 @@ class EditCourse extends Toolkit_FormBuilder
             'display' => '9 Holes',
             'opts'    => array('class' => 'text')
         );
-        $e[] = array(
-            'type'    => 'file',
-            'req'     => false,
-            'name'    => 'image',
-            'display' => 'Upload a Course Photo / Image',
-        );
         $e[] = array(
             'type'    => 'submit',
             'req'     => false,
@@ -1363,7 +1498,6 @@ class EditCourse 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!',
@@ -1373,6 +1507,15 @@ class EditCourse 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);
     }
@@ -1439,6 +1582,12 @@ class EditCourse extends Toolkit_FormBuilder
 
     //  }}}
 
+    protected function deleteFile($file)
+    {
+        $fs = new Toolkit_FileServer_FileAdapter();
+        return $fs->delete($file);
+    }
+
     //    {{{    _createPendingData()
 
     /**
@@ -1600,9 +1749,15 @@ class EditCourse extends Toolkit_FormBuilder
         $e =& $this->getElement('curr_image_rmv');
 
         $values['image'] = $e->getValue('curr_image_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']);
 
         $this->tableMetaData = Toolkit_Common::getTableMetaData(
@@ -1643,6 +1798,7 @@ class EditCourse extends Toolkit_FormBuilder
         $this->view              = new StdClass;
         $this->view->courseId    = $this->courseId;
         $this->view->showCurrImg = $this->showCurrImg;
+        $this->view->showCurrFile = $this->showCurrFile;
         $this->view->form        = $renderer->toObject();
         $this->template->compile($this->formTemplate);
     }
@@ -1670,7 +1826,12 @@ class EditCourse extends Toolkit_FormBuilder
     }
 
     //  }}}
-    //  {{{ removeOldImage()
+    function validNewFile(array $newFile)
+    {
+        return (   is_numeric($newFile['size'])
+                && $newFile['size'] > 0
+                && in_array($newFile['type'], $this->fileMimeTypes));
+    }
 
 
     /**
@@ -1695,7 +1856,15 @@ class EditCourse extends Toolkit_FormBuilder
     }
 
     //  }}}
-    //  {{{ syncCurrImage()
+    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;
+        }
+    }
 
 
     /**
@@ -1736,6 +1905,42 @@ class EditCourse 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()
 
     /**
@@ -1759,6 +1964,7 @@ class EditCourse extends Toolkit_FormBuilder
         //  their uploaded image in the form
         if ($this->isSubmitted()) {
             $this->syncCurrImage();
+            $this->syncCurrFile();
         }
 
         $this->setupRenderers();
@@ -1913,6 +2119,20 @@ class EditCourse 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()
 
 
@@ -1933,5 +2153,14 @@ class EditCourse extends Toolkit_FormBuilder
     }
 
     //  }}}
+    protected function uploadFile($field)
+    {
+        $fs = new Toolkit_FileServer_FileAdapter();
+        try {
+            return $fs->upload($field);
+        } catch (Toolkit_FileServer_Exception $e) {
+            return;
+        }
+    }
 }
 ?>