예제 #1
0
 /**
  * Organise les champs
  */
 public function sortfieldsAction()
 {
     if ($rows = $this->getRequest()->getParam('field')) {
         $html = array();
         try {
             if (!$this->getCurrentOptionValue()) {
                 throw new Exception($this->_("An error occurred while saving. Please try again later."));
             }
             $field = new Form_Model_Field();
             $fields = $field->findBySectionId($this->getRequest()->getParam('section_id'));
             $fields_id = array();
             foreach ($fields as $currField) {
                 $fields_id[] = $currField->getId();
             }
             foreach ($rows as $key => $row) {
                 if (!in_array($row, $fields_id)) {
                     throw new Exception($this->_('An error occurred while saving. A field can\'t be identified.'));
                 }
             }
             $field->updatePosition($rows);
             $html = array('success' => 1);
         } catch (Exception $e) {
             $html = array('message' => $e->getMessage());
         }
         $this->getLayout()->setHtml(Zend_Json::encode($html));
     }
 }
예제 #2
0
 public function findFields()
 {
     $field = new Form_Model_Field();
     $fields = $field->findAll(array('section_id' => $this->getId()));
     foreach ($fields as $field) {
         $this->addField($field);
     }
     return $this;
 }
예제 #3
0
 /**
  * Sauvegarde
  */
 public function postAction()
 {
     if ($datas = $this->getRequest()->getPost()) {
         try {
             $errors = '';
             // Recherche des sections
             $section = new Form_Model_Section();
             $sections = $section->findByValueId($this->getCurrentOptionValue()->getId());
             $field = new Form_Model_Field();
             // Validator date
             $validator = new Zend_Validate_Date(array('format' => 'yyyy-mm-dd'));
             foreach ($sections as $k => $section) {
                 // Recherche les fields de la section
                 $section->findFields($section->getId());
                 // Boucle sur les fields
                 foreach ($section->getFields() as $key => $field) {
                     if ($field->isRequired() == 1 && $datas['field_' . $k . '_' . $key] == '') {
                         $errors .= $this->_('<strong>%s</strong> must be filled<br />', $field->getName());
                     }
                     if ($field->getType() == 'email' && !Zend_Validate::is($datas['field_' . $k . '_' . $key], 'EmailAddress')) {
                         $errors .= $this->_('<strong>%s</strong> must be a valid email address<br />', $field->getName());
                     }
                     if ($field->getType() == 'nombre' && !Zend_Validate::is($datas['field_' . $k . '_' . $key], 'Digits')) {
                         $errors .= $this->_('<strong>%s</strong> must be a numerical value<br />', $field->getName());
                     }
                     if ($field->getType() == 'date' && !$validator->isValid($datas['field_' . $k . '_' . $key])) {
                         $errors .= $this->_('<strong>%s</strong> must be a valid date<br />', $field->getName());
                     }
                     $datasChanged['field_' . $k . '_' . $key] = array('name' => $field->getName(), 'value' => $datas['field_' . $k . '_' . $key]);
                 }
             }
             if (empty($errors)) {
                 $form = $this->getCurrentOptionValue()->getObject();
                 $layout = $this->getLayout()->loadEmail('form', 'send_email');
                 $layout->getPartial('content_email')->setDatas($datasChanged);
                 $content = $layout->render();
                 $mail = new Zend_Mail('UTF-8');
                 $mail->setBodyHtml($content);
                 $mail->setFrom($form->getEmail(), $this->getApplication()->getName());
                 $mail->addTo($form->getEmail(), $this->_('Your app\'s form'));
                 $mail->setSubject($this->_('Your app\'s form'));
                 $mail->send();
                 $html = array('success' => 1);
             } else {
                 $html = array('error' => 1, 'message' => $errors);
             }
         } catch (Exception $e) {
             $html = array('error' => 1, 'message' => $e->getMessage());
         }
         $this->_sendHtml($html);
     }
 }
예제 #4
0
 public function getTypes()
 {
     if (empty(self::$_types)) {
         self::$_types = array(new Core_Model_Default(array('type' => 'texte', 'label' => $this->_('Text'), 'icon' => '<i class="icon-file-text-alt"></i>')), new Core_Model_Default(array('type' => 'textarea', 'label' => $this->_('Multiline text'), 'icon' => '<i class="icon-file-text"></i>')), new Core_Model_Default(array('type' => 'email', 'label' => $this->_('Email'), 'icon' => '@')), new Core_Model_Default(array('type' => 'nombre', 'label' => $this->_('Number'), 'icon' => '123')), new Core_Model_Default(array('type' => 'date', 'label' => $this->_('Date/Hour'), 'icon' => '<i class="icon-time"></i>')), new Core_Model_Default(array('type' => 'geoloc', 'label' => $this->_('Geolocation'), 'icon' => '<i class="icon-map-marker"></i>')), new Core_Model_Default(array('type' => 'checkbox', 'label' => $this->_('Checkbox'), 'icon' => '<i class="icon-check-sign"></i>')), new Core_Model_Default(array('type' => 'radio', 'label' => $this->_('Radio'), 'icon' => '<i class="icon-circle-blank"></i>')), new Core_Model_Default(array('type' => 'select', 'label' => $this->_('Drop down'), 'icon' => '<i class="icon-collapse"></i>')), new Core_Model_Default(array('type' => 'image', 'label' => $this->_('Image'), 'icon' => '<i class="icon-picture"></i>')));
     }
     return self::$_types;
 }
예제 #5
0
 public function copyTo($option)
 {
     $old_value_id = $this->getValueId();
     $this->setId(null)->setValueId($option->getId())->save();
     $section = new Form_Model_Section();
     $sections = $section->findAll(array('value_id' => $old_value_id));
     foreach ($sections as $section) {
         $old_section_id = $section->getId();
         $section->setId(null)->setValueId($option->getId())->save();
         $field = new Form_Model_Field();
         $fields = $field->findAll(array('section_id' => $old_section_id));
         foreach ($fields as $field) {
             $field->setId(null)->setSectionId($section->getId())->save();
         }
     }
     return $this;
 }
예제 #6
0
 /**
  * Sauvegarde
  */
 public function postAction()
 {
     if ($data = Zend_Json::decode($this->getRequest()->getRawBody())) {
         try {
             $data = $data["form"];
             $data_image = array();
             $errors = '';
             // Recherche des sections
             $section = new Form_Model_Section();
             $sections = $section->findByValueId($this->getCurrentOptionValue()->getId());
             $field = new Form_Model_Field();
             // Date Validator
             //                $validator = new Zend_Validate_Date(array('format' => 'yyyy-mm-ddTHH:mm'));
             $dataChanged = array();
             foreach ($sections as $k => $section) {
                 // Load the fields
                 $section->findFields($section->getId());
                 // Browse the fields
                 foreach ($section->getFields() as $field) {
                     // If the field has options
                     if ($field->hasOptions()) {
                         // If the data is not empty
                         if (isset($data[$field->getId()])) {
                             // if all checkbox = false
                             $empty_checkbox = false;
                             if (is_array($data[$field->getId()])) {
                                 if (count($data[$field->getId()]) <= count(array_keys($data[$field->getId()], false))) {
                                     $empty_checkbox = true;
                                 }
                             }
                             if (!$empty_checkbox) {
                                 // Browse the field's options
                                 foreach ($field->getOptions() as $option) {
                                     // If it's a multiselect option and there's at least one selected option, store its value
                                     if (is_array($data[$field->getId()])) {
                                         // If the key exists,
                                         if (array_key_exists($option["id"], $data[$field->getId()])) {
                                             //                                            $data[$field->getId()][$option["id"]] = $option["name"];
                                             if ($data[$field->getId()][$option["id"]]) {
                                                 $dataChanged[$field->getName()][$option["id"]] = $option["name"];
                                             }
                                         }
                                         // If the current option has been posted, store its value
                                     } else {
                                         if ($option["id"] == $data[$field->getId()]) {
                                             //                                        $data[$field->getId()] = $option["name"];
                                             $dataChanged[$field->getName()] = $option["name"];
                                         }
                                     }
                                 }
                             } else {
                                 if ($field->isRequired()) {
                                     $errors .= $this->_('<strong>%s</strong> must be filled<br />', $field->getName());
                                 }
                             }
                             // If the field is empty and required, add an error
                         } else {
                             if ($field->isRequired()) {
                                 $errors .= $this->_('<strong>%s</strong> must be filled<br />', $field->getName());
                             }
                         }
                     } else {
                         // If the field is required
                         if ($field->isRequired()) {
                             // Add an error based on its type (and if it's empty)
                             switch ($field->getType()) {
                                 case "email":
                                     if (empty($data[$field->getId()]) or !Zend_Validate::is($data[$field->getId()], 'EmailAddress')) {
                                         $errors .= $this->_('<strong>%s</strong> must be a valid email address<br />', $field->getName());
                                     }
                                     break;
                                 case "nombre":
                                     if (!isset($data[$field->getId()]) or !Zend_Validate::is($data[$field->getId()], 'Digits')) {
                                         $errors .= $this->_('<strong>%s</strong> must be a numerical value<br />', $field->getName());
                                     }
                                     break;
                                 case "date":
                                     if (!isset($data[$field->getId()])) {
                                         $errors .= $this->_('<strong>%s</strong> must be a valid date (e.g. dd/mm/yyyy)<br />', $field->getName());
                                     }
                                     break;
                                 default:
                                     if (empty($data[$field->getId()])) {
                                         $errors .= $this->_('<strong>%s</strong> must be filled<br />', $field->getName());
                                     }
                                     break;
                             }
                         }
                         // If not empty, store its value
                         if (!empty($data[$field->getId()])) {
                             // If the field is an image
                             if ($field->getType() == "image") {
                                 $image = $data[$field->getId()];
                                 if (!preg_match("@^data:image/([^;]+);@", $image, $matches)) {
                                     throw new Exception($this->_("Unrecognized image format"));
                                 }
                                 $extension = $matches[1];
                                 $fileName = uniqid() . '.' . $extension;
                                 $relativePath = $this->getCurrentOptionValue()->getImagePathTo();
                                 $fullPath = Application_Model_Application::getBaseImagePath() . $relativePath;
                                 if (!is_dir($fullPath)) {
                                     mkdir($fullPath, 0777, true);
                                 }
                                 $filePath = $fullPath . '/' . $fileName;
                                 $contents = file_get_contents($image);
                                 if ($contents === FALSE) {
                                     throw new Exception($this->_("No uploaded image"));
                                 }
                                 $res = @file_put_contents($filePath, $contents);
                                 if ($res === FALSE) {
                                     throw new Exception('Unable to save image');
                                 }
                                 list($width, $height) = getimagesize($fullPath . DS . $fileName);
                                 $max_height = $max_width = 600;
                                 $image_name = uniqid($max_height);
                                 if ($height > $width) {
                                     $image_width = $max_height * $width / $height;
                                     $image_height = $max_height;
                                 } else {
                                     $image_width = $max_width;
                                     $image_height = $max_width * $height / $width;
                                 }
                                 $newIcon = new Core_Model_Lib_Image();
                                 $newIcon->setId($image_name)->setPath($fullPath . DS . $fileName)->setWidth($image_width)->setHeight($image_height)->crop();
                                 $image_url = $this->getRequest()->getBaseUrl() . $newIcon->getUrl();
                                 $dataChanged[$field->getName()] = '<br/><img width="' . $image_width . '" height="' . $image_height . '" src="' . $image_url . '" alt="' . $field->getName() . '" />';
                             } else {
                                 $dataChanged[$field->getName()] = $data[$field->getId()];
                             }
                         }
                     }
                 }
             }
             if (empty($errors)) {
                 $form = $this->getCurrentOptionValue()->getObject();
                 $layout = $this->getLayout()->loadEmail('form', 'send_email');
                 $layout->getPartial('content_email')->setFields($dataChanged);
                 $content = $layout->render();
                 $mail = new Zend_Mail('UTF-8');
                 $mail->setBodyHtml($content);
                 $mail->setFrom($form->getEmail(), $this->getApplication()->getName());
                 $mail->addTo($form->getEmail(), $this->_('Your app\'s form'));
                 $mail->setSubject($this->_('Your app\'s form'));
                 $mail->send();
                 $html = array("success" => 1, "message" => $this->_("The form has been sent successfully"));
             } else {
                 $html = array('error' => 1, 'message' => $errors);
             }
         } catch (Exception $e) {
             $html = array('error' => 1, 'message' => $e->getMessage());
         }
         $this->_sendHtml($html);
     }
 }