public function editpostAction()
 {
     if ($datas = $this->getRequest()->getPost()) {
         try {
             // Flag utilisé pour le retour HTML
             $isSection = false;
             // Récupère le formulaire
             $form = $this->getCurrentOptionValue()->getObject();
             // Init HTML
             $html = array();
             // On récupère le value_id
             $datas['value_id'] = $this->getCurrentOptionValue()->getId();
             // Cas de la sauvegarde du mail
             if (isset($datas['email'])) {
                 $error = false;
                 if (empty($datas['email']) or !Zend_Validate::is($datas['email'], 'EmailAddress')) {
                     $html = array('error' => 1, 'message' => $this->_('<strong>%s</strong> must be a valid email address<br />', $this->_('Recipient email')));
                     $error = true;
                 } else {
                     if (!$form->getId()) {
                         $form->setValueId($datas['value_id']);
                     }
                     $form->setEmail($datas['email'])->save();
                     unset($datas['email']);
                 }
             } else {
                 // Init du model Section
                 $section = new Form_Model_Section();
                 if (!empty($datas['section_id'])) {
                     $section->find($datas['section_id']);
                     if ($section->getId() and $section->getValueId() != $this->getCurrentOptionValue()->getId()) {
                         throw new Exception($this->_('An error occurred while saving. Please try again later.'));
                     }
                 }
                 // On sauvegarde
                 $section->addData($datas)->save();
                 $isSection = true;
             }
             if (!$error) {
                 $html = array('success' => 1, 'success_message' => $this->_('Info successfully saved'), 'message_timeout' => 2, 'message_button' => 0, 'message_loader' => 0);
                 if ($isSection) {
                     // Construit le html
                     $html['section_id'] = $section->getId();
                     $html['is_deleted'] = $section->getIsDeleted() ? 1 : 0;
                     $html['section_html'] = $this->getLayout()->addPartial('row', 'admin_view_default', 'form/application/edit/section.phtml')->setSection($section)->setOptionValue($this->getCurrentOptionValue())->toHtml();
                 }
             }
         } catch (Exception $e) {
             // Erreur
             if (!isset($datas['is_deleted'])) {
                 $html['message'] = $e->getMessage();
             }
         }
         // Envoi la réponse
         $this->getLayout()->setHtml(Zend_Json::encode($html));
     }
     return $this;
 }
Beispiel #2
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;
 }
 /**
  * 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);
     }
 }
 /**
  * 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);
     }
 }