Example #1
0
 /**
  * Validate the form
  */
 private function validateForm()
 {
     if ($this->frm->isSubmitted()) {
         // cleanup the submitted fields, ignore fields that were added by hackers
         $this->frm->cleanupFields();
         // validate fields
         $file = $this->frm->getField('file');
         $this->frm->getField('title')->isFilled(BL::err('NameIsRequired'));
         $file->isFilled(BL::err('FieldIsRequired'));
         // validate the file
         if ($this->frm->getField('file')->isFilled()) {
             // file extension
             $this->frm->getField('file')->isAllowedExtension($this->allowedExtensions, BL::err('FileExtensionNotAllowed'));
         }
         // no errors?
         if ($this->frm->isCorrect()) {
             // build file record to insert
             $item['product_id'] = $this->product['id'];
             $item['title'] = $this->frm->getField('title')->getValue();
             // the file path
             $filePath = FRONTEND_FILES_PATH . '/' . $this->getModule() . '/' . $item['product_id'] . '/source';
             // create folders if needed
             $fs = new Filesystem();
             if (!$fs->exists($filePath)) {
                 $fs->mkdir($filePath);
             }
             // file provided?
             if ($file->isFilled()) {
                 // build the file name
                 $item['filename'] = time() . '.' . $file->getExtension();
                 // upload the file
                 $file->moveFile($filePath . '/' . $item['filename']);
             }
             $item['sequence'] = BackendCatalogModel::getMaximumFilesSequence($item['product_id']) + 1;
             // insert it
             $item['id'] = BackendCatalogModel::saveFile($item);
             // trigger event
             BackendModel::triggerEvent($this->getModule(), 'after_add_file', array('item' => $item));
             // everything is saved, so redirect to the overview
             $this->redirect(BackendModel::createURLForAction('media') . '&product_id=' . $item['product_id'] . '&report=added&var=' . urlencode($item['title']) . '&highlight=row-' . $item['id'] . '#tabFiles');
         }
     }
 }