Example #1
0
 /**
  * generate form for table
  * @param $table string table
  * @param $action string action
  * @param string $method method form (default = POST)
  * @return \SwagFramework\Form\Form
  * @throws TableNotFoundDatabaseException
  */
 public static function generate($table, $action, $method = 'POST')
 {
     $sql = 'SHOW FIELDS ' . 'FROM ' . $table;
     $res = DatabaseProvider::connection()->query($sql);
     if (empty($res)) {
         throw new TableNotFoundDatabaseException($table);
     }
     $form = new Form($action, $method);
     $form->setClass('pure-form pure-form-aligned centered');
     foreach ($res as $value) {
         if (self::getType($value['Type']) == 'text') {
             $field = self::getTextArea($value);
         } elseif (self::getType($value['Type']) == 'datetime') {
             $field = self::getInput($value);
             $field->addAttribute('class', 'datepicker');
             $field->addAttribute('placeholder', '01/01/2014');
         } else {
             $field = self::getInput($value);
         }
         $form->addField($field);
     }
     $submit = new InputField('submit');
     $submit->addAttribute('type', 'submit');
     $submit->addAttribute('value', 'Envoyer');
     $submit->addAttribute('class', 'pure-button pure-button-primary');
     $form->addField($submit);
     return $form;
 }
 public function modify()
 {
     if (!Authentication::getInstance()->isAuthenticated() && !Authentication::getInstance()->getOptionOr('accessLevel', 0)) {
         throw new NotAuthenticatedException();
     }
     $id = (int) $this->getParams()[0];
     $event = $this->eventModel->get($id);
     if (empty($event)) {
         throw new EventNotFoundException($id);
     }
     if ($event['user'] != Authentication::getInstance()->getUserId() && !Authentication::getInstance()->getOptionOr('accessLevel', 0)) {
         throw new NotYourEventException($id);
     }
     $form = FormHelper::generate('event', '/event/modify');
     $form->getField('id')->addAttribute('value', $event['id']);
     $form->getField('name')->addAttribute('value', $event['name']);
     $form->getField('image')->addAttribute('value', $event['image']);
     $form->getField('description')->setContent($event['description']);
     $form->getField('address')->setContent($event['address']);
     $eventtime = new \DateTime();
     $eventtime = $eventtime->createFromFormat('Y-m-d H:i:s', $event['eventtime']);
     $form->getField('eventtime')->addAttribute('value', $eventtime->format('d/m/Y'));
     $form->getField('money')->addAttribute('value', $event['money']);
     $form->getField('personsmax')->addAttribute('value', $event['personsmax']);
     $html = $form->getFormHTML(['name' => 'Nom de l\'évènement', 'description' => 'Description', 'address' => 'Adresse', 'eventtime' => 'Date de l\'évènement', 'money' => 'Prix', 'personsmax' => 'Nombre maximum de participants', 'image' => 'Image']);
     $formDelete = new Form('/event/delete');
     $formDelete->setClass('pure-form centered');
     $formDelete->addField(new InputField('id', ['type' => 'hidden', 'value' => $id]));
     $formDelete->addField(new InputField('submit', ['type' => 'submit', 'value' => 'Supprimer', 'class' => 'pure-button button-error']));
     $this->getView()->render('event/modify', ['form' => $html, 'formDelete' => $formDelete->getFormHTML([])]);
 }