Example #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('TitleIsRequired'));
         $fields['description']->isFilled(Language::err('FieldIsRequired'));
         $fields['author_name']->isFilled(Language::err('FieldIsRequired'));
         $fields['author_url']->isFilled(Language::err('FieldIsRequired'));
         $fields['author_email']->isFilled(Language::err('FieldIsRequired'));
         // cleanup the modulename
         $title = preg_replace('/[^A-Za-z ]/', '', $fields['title']->getValue());
         // check if there is already a module with this name
         if (BackendExtensionsModel::existsModule($title)) {
             $fields['title']->addError(Language::err('DuplicateModuleName'));
         }
         if ($this->frm->isCorrect()) {
             $this->record['title'] = $title;
             $this->record['description'] = trim($fields['description']->getValue());
             $this->record['author_name'] = $fields['author_name']->getValue();
             $this->record['author_url'] = $fields['author_url']->getValue();
             $this->record['author_email'] = $fields['author_email']->getValue();
             $this->record['camel_case_name'] = BackendModuleMakerHelper::buildCamelCasedName($title);
             $this->record['underscored_name'] = BackendModuleMakerHelper::buildUnderscoredName($title);
             \SpoonSession::set('module', $this->record);
             $this->redirect(Model::createURLForAction('AddStep2'));
         }
     }
 }
 /**
  * Execute the action.
  */
 public function execute()
 {
     parent::execute();
     $this->loadData();
     if ($this->getParameter('module') !== NULL) {
         $module = $this->getParameter('module');
         $files = array();
         // frontend files
         $frontendDir = FRONTEND_PATH . "/Modules/" . $module . "/";
         // check if dir exist
         if (file_exists($frontendDir)) {
             $dir = new \RecursiveDirectoryIterator($frontendDir);
             foreach (new \RecursiveIteratorIterator($dir) as $filename => $file) {
                 $files[] = str_replace(PATH_WWW . '/', '', $filename);
             }
         }
         // backend files
         $backendDir = BACKEND_PATH . "/Modules/" . $module . "/";
         // check if dir exist
         if (file_exists($backendDir)) {
             $dir = new \RecursiveDirectoryIterator($backendDir);
             foreach (new \RecursiveIteratorIterator($dir) as $filename => $file) {
                 $files[] = str_replace(PATH_WWW . '/', '', $filename);
             }
         }
         // we found some files
         if (!empty($files)) {
             // create zip
             if (BackendModuleMakerHelper::createZip($files, PATH_WWW . '/' . $module . '.zip')) {
                 // download zip
                 header('Content-Type: application/zip');
                 header('Content-disposition: attachment; filename=' . $module . '.zip');
                 header('Content-Length: ' . filesize(PATH_WWW . '/' . $module . '.zip'));
                 readfile(PATH_WWW . '/' . $module . '.zip');
                 // delete temp file
                 \Spoonfile::delete(PATH_WWW . '/' . $module . '.zip');
                 exit;
             }
         } else {
             $this->redirect(Model::createURLForAction('create_zip') . '&error=non-existing');
         }
     } else {
         $this->loadDataGridInstalled();
         $this->loadDataGridInstallable();
         $this->parse();
         $this->display();
     }
 }
 /**
  * Validate the form
  */
 protected function validateForm()
 {
     if ($this->frm->isSubmitted()) {
         $this->frm->cleanupFields();
         // validation
         $fields = $this->frm->getFields();
         $fields['label']->isFilled(Language::err('FieldIsRequired'));
         // get existing fields
         $this->record = \SpoonSession::get('module');
         if (array_key_exists('fields', $this->record)) {
             foreach ($this->record['fields'] as $field) {
                 // check if we already have a type with the same label
                 if (strtolower($field['label']) == strtolower($fields['label']->getValue())) {
                     $fields['label']->addError(Language::err('LabelAlreadyExist'));
                     break;
                 }
             }
         }
         // for certain types, the options field is required
         $type = $fields['type']->getValue();
         if ($type == 'dropdown' || $type == 'multicheckbox' || $type == 'radiobutton') {
             $fields['tags']->isFilled(Language::err('FieldIsRequired'));
             // check if the default field is one of the options
             if ($fields['default']->isFilled()) {
                 $options = explode(',', $fields['tags']->getValue());
                 if (!in_array($fields['default']->getValue(), $options)) {
                     $fields['default']->addError(Language::err('DefaultShouldBeAnOption'));
                 }
             } elseif ($type == 'radiobutton') {
                 $fields['default']->addError(Language::err('FieldIsRequired'));
             }
         }
         // if the type is images, the options should be in the form 200x200 seperated by a comma
         if ($type == 'image') {
             $fields['tags']->isFilled(Language::err('FieldIsRequired'));
             $tags = explode(',', $fields['tags']->getValue());
             // loop all tags and check on format, example (400x400)
             foreach ($tags as $tag) {
                 if (!preg_match('\'([1-9][0-9]*x[1-9][0-9]*|x[1-9][0-9]*|[1-9][0-9]*x)\'', $tag)) {
                     $fields['tags']->addError(Language::err('ImageSizeNotWellFormed'));
                     break;
                 }
             }
         }
         // check if the default value is valid
         if ($fields['default']->isFilled()) {
             // get default value
             $defaultValue = $fields['default']->getValue();
             // check the default values
             if ($type == 'text' || $type == 'password' || $type == 'file' || $type == 'image') {
                 if (strlen($defaultValue) > 255) {
                     $fields['default']->addError(Language::err('Max255Characters'));
                 }
             } elseif ($type == 'number') {
                 if (!is_numeric($defaultValue)) {
                     $fields['default']->addError(Language::err('FieldIsNotNumeric'));
                 }
             } elseif ($type == 'datetime') {
                 if (!BackendModuleMakerHelper::isValidDateTime($defaultValue)) {
                     $fields['default']->addError(Language::err('FieldIsNotAValidDateTime'));
                 }
             } elseif ($type == 'checkbox') {
                 if (strtoupper($defaultValue) != 'Y' && strtoupper($defaultValue) != 'N') {
                     $fields['default']->addError(Language::err('MustBeAYOrAN'));
                 }
             }
         }
         if ($this->frm->isCorrect()) {
             // create the item
             $item['label'] = strtolower($fields['label']->getValue());
             $item['type'] = $type;
             $item['options'] = $fields['tags']->getValue();
             $item['required'] = $fields['required']->isChecked();
             $item['default'] = $fields['default']->getValue();
             $item['camel_cased_label'] = BackendModuleMakerHelper::buildCamelCasedName($item['label']);
             $item['underscored_label'] = BackendModuleMakerHelper::buildUnderscoredName($item['label']);
             $item['lower_ccased_label'] = BackendModuleMakerHelper::buildLowerCamelCasedName($item['label']);
             $item['meta'] = false;
             $item['searchable'] = false;
             if ($item['type'] == 'image' && $fields['caption']->isChecked()) {
                 $item['type'] = 'image_caption';
             }
             // generate the SQL for the field
             $item['sql'] = $this->generateSQL($item);
             // if the record has no fields key yet, add it
             if (!array_key_exists('fields', $this->record)) {
                 $this->record['fields'] = array();
             }
             // add the item to the fields array of the record
             $this->record['fields'][] = $item;
             // save
             \SpoonSession::set('module', $this->record);
             $this->redirect(Model::createURLForAction('AddStep2'));
         }
     }
 }