public function editpostAction()
 {
     if ($data = $this->getRequest()->getPost()) {
         try {
             $application = $this->getApplication();
             // Test s'il y a un value_id
             if (empty($data['value_id'])) {
                 throw new Exception($this->_('An error occurred while saving. Please try again later.'));
             }
             // Récupère l'option_value en cours
             $option_value = new Application_Model_Option_Value();
             $option_value->find($data['value_id']);
             $html = '';
             $contact = new Contact_Model_Contact();
             $contact->find($option_value->getId(), 'value_id');
             if (!empty($data['file'])) {
                 $file = pathinfo($data['file']);
                 $filename = $file['basename'];
                 $relative_path = $option_value->getImagePathTo();
                 $folder = Application_Model_Application::getBaseImagePath() . $relative_path;
                 $img_dst = $folder . '/' . $filename;
                 $img_src = Core_Model_Directory::getTmpDirectory(true) . '/' . $filename;
                 if (!is_dir($folder)) {
                     mkdir($folder, 0777, true);
                 }
                 if (!@copy($img_src, $img_dst)) {
                     throw new exception($this->_('An error occurred while saving your picture. Please try again later.'));
                 } else {
                     $data['cover'] = $relative_path . '/' . $filename;
                 }
             } else {
                 if (!empty($data['remove_cover'])) {
                     $data['cover'] = null;
                 }
             }
             $contact->setData($data);
             if ($contact->getStreet() and $contact->getPostcode() and $contact->getCity()) {
                 $latlon = Siberian_Google_Geocoding::getLatLng(array("street" => $contact->getStreet(), "postcode" => $contact->getPostcode(), "city" => $contact->getCity()));
                 if (!empty($latlon[0]) && !empty($latlon[1])) {
                     $contact->setLatitude($latlon[0])->setLongitude($latlon[1]);
                 }
             } else {
                 $contact->setLatitude(null)->setLongitude(null);
             }
             $contact->save();
             $html = array('success' => '1', 'success_message' => $this->_('Info successfully saved'), 'message_timeout' => 2, 'message_button' => 0, 'message_loader' => 0);
         } catch (Exception $e) {
             $html = array('message' => $e->getMessage(), 'message_button' => 1, 'message_loader' => 1);
         }
         $this->getLayout()->setHtml(Zend_Json::encode($html));
     }
 }
 public function editpostAction()
 {
     if ($datas = $this->getRequest()->getPost()) {
         $html = '';
         try {
             // Test s'il y a un value_id
             if (empty($datas['value_id'])) {
                 throw new Exception($this->_('An error occurred while saving your page.'));
             }
             // Récupère l'option_value en cours
             $option_value = new Application_Model_Option_Value();
             $option_value->find($datas['value_id']);
             if (!$option_value->getId()) {
                 throw new Exception($this->_('An error occurred while saving your page.'));
             }
             $page = new Cms_Model_Application_Page();
             if (!empty($datas["page_id"])) {
                 if ($datas["page_id"] == "new") {
                     unset($datas["page_id"]);
                 }
                 $page->find($datas["page_id"]);
                 if ($page->getId() and $page->getValueId() != $option_value->getId()) {
                     throw new Exception($this->_('An error occurred while saving your page.'));
                 }
             }
             if (empty($datas["picture"])) {
                 $datas["picture"] = null;
             } else {
                 if (file_exists(Core_Model_Directory::getTmpDirectory(true) . "/" . $datas["picture"])) {
                     $application = $this->getApplication();
                     $relative_path = $option_value->getImagePathTo();
                     $folder = Application_Model_Application::getBaseImagePath() . $relative_path;
                     $path = Application_Model_Application::getBaseImagePath() . $relative_path;
                     $file = Core_Model_Directory::getTmpDirectory(true) . '/' . $datas['picture'];
                     if (!is_dir($path)) {
                         mkdir($path, 0777, true);
                     }
                     if (!copy($file, $folder . $datas['picture'])) {
                         throw new exception($this->_('An error occurred while saving. Please try again later.'));
                     } else {
                         $datas['picture'] = $relative_path . $datas['picture'];
                     }
                 }
             }
             // Traitement des images des blocks
             $blocks = !empty($datas['block']) && is_array($datas['block']) ? $datas['block'] : array();
             $image_path = $option_value->getImagePathTo() . '/';
             $base_image_path = $this->getApplication()->getBaseImagePath() . $image_path;
             if (!is_dir($base_image_path)) {
                 mkdir($base_image_path, 0777, true);
             }
             foreach ($blocks as $k => $block) {
                 if ($block["type"] == "image" && !empty($block['image_url'])) {
                     foreach ($block['image_url'] as $index => $image_url) {
                         //déjà enregistrée
                         if (substr($image_url, 0, 1) != '/') {
                             if (!empty($image_url) and file_exists(Core_Model_Directory::getTmpDirectory(true) . '/' . $image_url)) {
                                 rename(Core_Model_Directory::getTmpDirectory(true) . '/' . $image_url, $base_image_path . $image_url);
                                 $blocks[$k]['image_url'][$index] = $image_path . $image_url;
                             }
                         } else {
                             //                                $img = explode('/', $image_url);
                             //                                $img = $img[count($img) - 1];
                             //                                $blocks[$k]['image_url'][$index] = $image_path . $img;
                             $blocks[$k]['image_url'][$index] = $image_url;
                         }
                     }
                     foreach ($block['image_fullsize_url'] as $index => $image_url) {
                         //déjà enregistrée
                         if (substr($image_url, 0, 1) != '/') {
                             if (!empty($image_url) and file_exists(Core_Model_Directory::getTmpDirectory(true) . '/' . $image_url)) {
                                 rename(Core_Model_Directory::getTmpDirectory(true) . '/' . $image_url, $base_image_path . $image_url);
                                 $blocks[$k]['image_fullsize_url'][$index] = $image_path . $image_url;
                             }
                         } else {
                             //                                $img = explode('/', $image_url);
                             //                                $img = $img[count($img) - 1];
                             //                                $blocks[$k]['image_fullsize_url'][$index] = $image_path . $img;
                             $blocks[$k]['image_fullsize_url'][$index] = $image_url;
                         }
                     }
                 }
                 if (($block["type"] == "text" || $block["type"] == "video") && !empty($block['image'])) {
                     //déjà enregistrée
                     if (substr($block['image'], 0, 1) != '/') {
                         if (!empty($block['image']) and file_exists(Core_Model_Directory::getTmpDirectory(true) . '/' . $block['image'])) {
                             rename(Core_Model_Directory::getTmpDirectory(true) . '/' . $block['image'], $base_image_path . $block['image']);
                             $blocks[$k]['image'] = $image_path . $block['image'];
                         }
                     } else {
                         //                            $img = explode('/', $block['image']);
                         //                            $img = $img[count($img) - 1];
                         //                            $blocks[$k]['image'] = $image_path . $img;
                         $blocks[$k]['image'] = $block['image'];
                     }
                 }
                 if ($block["type"] == "text") {
                     $blocks[$k]["content"] = stripslashes($block["content"]);
                 }
                 if ($block["type"] == "address" and (empty($block["latitude"]) or empty($block["longitude"]))) {
                     $latlon = Siberian_Google_Geocoding::getLatLng(array("address" => $block["address"]));
                     if (!empty($latlon[0]) and !empty($latlon[1])) {
                         $blocks[$k]["latitude"] = $latlon[0];
                         $blocks[$k]["longitude"] = $latlon[1];
                     }
                 }
             }
             $datas['block'] = $blocks;
             // Sauvegarde
             $page->setData($datas)->save();
             $html = array('success' => 1, 'success_message' => $this->_('Page successfully saved'), 'message_timeout' => 2, 'message_button' => 0, 'message_loader' => 0);
         } catch (Exception $e) {
             $html = array('message' => $e->getMessage(), 'message_button' => 1, 'message_loader' => 1);
         }
         $this->getLayout()->setHtml(Zend_Json::encode($html));
     }
 }