Exemplo n.º 1
0
 /**
  * Validate the form
  */
 protected function validateForm()
 {
     if ($this->frm->isSubmitted()) {
         $this->frm->cleanupFields();
         // validation
         $fields = $this->frm->getFields();
         $fields['title']->isFilled(Language::err('FieldIsRequired'));
         // validate redirect
         $redirectValue = $this->frm->getField('redirect')->getValue();
         if ($redirectValue == 'internal') {
             $this->frm->getField('internal_redirect')->isFilled(Language::err('FieldIsRequired'));
         }
         if ($redirectValue == 'external') {
             $this->frm->getField('external_redirect')->isURL(Language::err('InvalidURL'));
         }
         // validate meta
         $this->meta->validate();
         if ($this->frm->isCorrect()) {
             // build the item
             $item['language'] = Language::getWorkingLanguage();
             $item['title'] = $fields['title']->getValue();
             $item['text'] = $fields['text']->getValue();
             //$item['link'] = $fields['link']->getValue();
             $item['linktext'] = $fields['linktext']->getValue();
             if ($redirectValue == 'internal') {
                 $item['page_id'] = $this->frm->getField('internal_redirect')->getValue();
             }
             if ($redirectValue == 'external') {
                 $item['link'] = $this->frm->getField('external_redirect')->getValue();
             }
             // the image path
             $imagePath = FRONTEND_FILES_PATH . '/' . $this->getModule() . '/image';
             // create folders if needed
             if (!\SpoonDirectory::exists($imagePath . '/800x')) {
                 \SpoonDirectory::create($imagePath . '/800x');
             }
             if (!\SpoonDirectory::exists($imagePath . '/x800')) {
                 \SpoonDirectory::create($imagePath . '/x800');
             }
             if (!\SpoonDirectory::exists($imagePath . '/450x250')) {
                 \SpoonDirectory::create($imagePath . '/450x250');
             }
             if (!\SpoonDirectory::exists($imagePath . '/600x300')) {
                 \SpoonDirectory::create($imagePath . '/600x300');
             }
             if (!\SpoonDirectory::exists($imagePath . '/128x128')) {
                 \SpoonDirectory::create($imagePath . '/128x128');
             }
             if (!\SpoonDirectory::exists($imagePath . '/source')) {
                 \SpoonDirectory::create($imagePath . '/source');
             }
             // image provided?
             if ($fields['image']->isFilled()) {
                 // build the image name
                 $item['image'] = $this->meta->getUrl() . '.' . $fields['image']->getExtension();
                 // upload the image & generate thumbnails
                 $fields['image']->generateThumbnails($imagePath, $item['image']);
             }
             $item['hidden'] = $fields['hidden']->getValue();
             $item['sequence'] = BackendBlocksModel::getMaximumSequence() + 1;
             $item['category_id'] = $this->frm->getField('category_id')->getValue();
             $item['meta_id'] = $this->meta->save();
             // insert it
             $item['id'] = BackendBlocksModel::insert($item);
             Model::triggerEvent($this->getModule(), 'after_add', $item);
             $this->redirect(Model::createURLForAction('Index') . '&report=added&highlight=row-' . $item['id']);
         }
     }
 }