public function editpostAction()
 {
     if ($datas = $this->getRequest()->getPost()) {
         try {
             $option_value = new Application_Model_Option_Value();
             $option_value->find($datas['value_id']);
             $datas = $datas['menus'];
             foreach ($datas as $product_id => $data) {
                 $product = new Catalog_Model_Product();
                 if ($id = $this->getRequest()->getParam('id')) {
                     $product->find($id);
                     if ($product->getValueId() != $option_value->getId()) {
                         throw new Exception($this->_('An error occurred while saving. Please try again later.'));
                     }
                 }
                 if (!$product->getId()) {
                     $product->setValueId($option_value->getId());
                 }
                 $pos_datas = array();
                 if (!empty($data['pos'])) {
                     foreach ($data['pos'] as $key => $pos_data) {
                         $pos_datas[$key] = $pos_data;
                     }
                 }
                 if (!empty($data['picture'])) {
                     if (substr($data['picture'], 0, 1) == '/') {
                         unset($data['picture']);
                     } else {
                         $illus_relative_path = $option_value->getImagePathTo();
                         $folder = Application_Model_Application::getBaseImagePath() . $illus_relative_path;
                         $file = Core_Model_Directory::getTmpDirectory(true) . '/' . $data['picture'];
                         if (!is_dir($folder)) {
                             mkdir($folder, 0777, true);
                         }
                         if (!copy($file, $folder . $data['picture'])) {
                             throw new exception($this->_('An error occurred while saving your picture. Please try againg later.'));
                         } else {
                             $data['picture'] = $illus_relative_path . $data['picture'];
                         }
                     }
                 }
                 $product->addData($data)->setType('menu');
                 $product->setPosDatas($pos_datas);
                 $product->save();
             }
             $html = array();
             if (!$product->getData('is_deleted')) {
                 $html['menu_id'] = $product->getId();
             }
             $html['success'] = 1;
             $html['success_message'] = $this->_('Set meal successfully saved.');
             $html['message_timeout'] = 2;
             $html['message_button'] = 0;
             $html['message_loader'] = 0;
         } catch (Exception $e) {
             $html = array('message' => $this->_('An error occurred while saving the set meal. Please try again later.'));
         }
         $this->getLayout()->setHtml(Zend_Json::encode($html));
     }
 }
 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 ($data = $this->getRequest()->getPost()) {
         $html = '';
         try {
             // 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']);
             // Instancie une nouvelle promotion
             $promotion = new Promotion_Model_Promotion();
             // Test si l'option des réductions spéciales est activée et payée
             if ($id = $this->getRequest()->getParam('id')) {
                 $promotion->find($id);
                 if ($promotion->getValueId() and $promotion->getValueId() != $option_value->getId()) {
                     throw new Exception('An error occurred while saving. Please try again later.');
                 }
             }
             //Vérifs champs
             if (empty($data['title']) || empty($data['description']) || empty($data['title'])) {
                 throw new Exception($this->_('An error occurred while saving your discount. Please fill in all fields'));
             }
             if (!isset($data['is_illimited']) && empty($data['end_at'])) {
                 throw new Exception($this->_('An error occurred while saving your discount. Please fill in all fields'));
             }
             if (!empty($data['end_at'])) {
                 $date_actuelle = new Zend_Date();
                 $date_modif = new Zend_Date($data['end_at'], 'y-MM-dd');
                 if ($date_modif < $date_actuelle) {
                     throw new Exception($this->_('Please select an end date greater than the current date.'));
                     die;
                 }
             }
             if (!empty($data['is_illimited'])) {
                 $data['end_at'] = null;
             }
             $data['force_validation'] = !empty($data['force_validation']);
             $data['is_unique'] = !empty($data['is_unique']);
             $data['owner'] = 1;
             if (isset($data['available_for']) and $data['available_for'] == 'all') {
                 $promotion->resetConditions();
             }
             if (!empty($data['picture'])) {
                 $filename = pathinfo($data['picture'], PATHINFO_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['picture'] = $relative_path . '/' . $filename;
                 }
             } else {
                 if (!empty($data['remove_cover'])) {
                     $data['picture'] = null;
                 }
             }
             $promotion->setData($data);
             $promotion->save();
             if (!empty($data['unlock_code'])) {
                 $dir_image = Core_Model_Directory::getBasePathTo("/images/application/" . $this->getApplication()->getId());
                 if (!is_dir($dir_image)) {
                     mkdir($dir_image, 0775, true);
                 }
                 if (!is_dir($dir_image . "/application")) {
                     mkdir($dir_image . "/application", 0775, true);
                 }
                 if (!is_dir($dir_image . "/application/qrpromotion")) {
                     mkdir($dir_image . "/application/qrpromotion", 0775, true);
                 }
                 $dir_image .= "/application/qrpromotion/";
                 $image_name = $promotion->getId() . "-qrpromotion_qrcode.png";
                 copy('http://api.qrserver.com/v1/create-qr-code/?color=000000&bgcolor=FFFFFF&data=sendback%3A' . $data["unlock_code"] . '&qzone=1&margin=0&size=200x200&ecc=L', $dir_image . $image_name);
             }
             $html = array('promotion_id' => $promotion->getId(), 'success_message' => $this->_('Discount successfully saved'), 'message_timeout' => 2, 'message_button' => 0, 'message_loader' => 0);
         } catch (Exception $e) {
             $html = array('message' => $e->getMessage(), 'url' => '/promotion/admin/list');
         }
         $this->getLayout()->setHtml(Zend_Json::encode($html));
     }
 }
 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'));
             }
             if (empty($data['title'])) {
                 throw new Exception($this->_('Folder title is required'));
             }
             // Récupère l'option_value en cours
             $option_value = new Application_Model_Option_Value();
             $option_value->find($data['value_id']);
             $folder = new Folder_Model_Folder();
             $category = new Folder_Model_Category();
             if (!empty($data['category_id'])) {
                 $category->find($data['category_id'], 'category_id');
             }
             if ($data['parent_id'] == 'null') {
                 unset($data['parent_id']);
                 //Assigne le nom de catégorie root à la feature
                 $option_value->setTabbarName($data['title'])->save();
             } else {
                 $data['pos'] = $category->getNextCategoryPosition($data['parent_id']);
             }
             if (!empty($data['file']) and is_file(Core_Model_Directory::getTmpDirectory(true) . '/' . $data['file'])) {
                 $file = pathinfo(Core_Model_Directory::getTmpDirectory() . '/' . $data['file']);
                 $filename = $file['basename'];
                 $relative_path = $option_value->getImagePathTo();
                 $dst_folder = Application_Model_Application::getBaseImagePath() . $relative_path;
                 $img_src = Core_Model_Directory::getTmpDirectory(true) . '/' . $filename;
                 $img_dst = $dst_folder . '/' . $filename;
                 if (!is_dir($dst_folder)) {
                     mkdir($dst_folder, 0777, true);
                 }
                 if (!@copy($img_src, $img_dst)) {
                     throw new exception($this->_('An error occurred while saving. Please try again later.'));
                 } else {
                     $data['picture'] = $relative_path . '/' . $filename;
                 }
             } else {
                 if (!empty($data['remove_picture'])) {
                     $data['picture'] = null;
                 }
             }
             $category->addData($data)->save();
             //Change root category
             if (!isset($data['parent_id'])) {
                 $folder->find($option_value->getId(), 'value_id');
                 $folder->setValueId($data['value_id'])->setRootCategoryId($category->getId())->save();
                 $parent_id = 'null';
             } else {
                 $parent_id = $data['parent_id'];
             }
             $html = array('success' => '1', 'success_message' => $this->_('Info successfully saved'), 'category_id' => $category->getId(), 'parent_id' => $parent_id, '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 images gallery. Please try again later."));
             }
             // Récupère l'option_value en cours
             $option_value = new Application_Model_Option_Value();
             $option_value->find($datas['value_id']);
             $isNew = true;
             $save = true;
             $image = new Media_Model_Gallery_Image();
             if (!empty($datas['id'])) {
                 $image->find($datas['id']);
                 $isNew = false;
                 $id = $datas['id'];
             } else {
                 $id = 'new';
                 $datas['value_id'] = $option_value->getId();
             }
             if ($image->getId() and $image->getValueId() != $option_value->getId()) {
                 throw new Exception("An error occurred while saving your images gallery. Please try again later.");
             }
             if (!empty($datas['param_instagram'])) {
                 $instagram = new Media_Model_Gallery_Image_Instagram();
                 $userId = $instagram->getUserId($datas['param_instagram']);
                 if (!$userId) {
                     throw new Exception($this->_("The entered name is not a valid Instagram user."));
                 }
                 $datas['type_id'] = 'instagram';
             } elseif (!empty($datas['param'])) {
                 $datas['type_id'] = 'picasa';
             } else {
                 $datas['type_id'] = 'custom';
             }
             $html = array('success' => 1, 'is_new' => (int) $isNew);
             if (empty($datas['type_id']) or $datas['type_id'] == 'picasa') {
                 $image->setTypeId('picasa');
                 $image->getTypeInstance()->setParam($datas['param']);
                 if (empty($datas['album_id'])) {
                     $albums = $image->getTypeInstance()->findAlbums();
                 }
                 if (!empty($albums)) {
                     $html['albums'] = $albums;
                     $save = false;
                 }
                 $datas['type'] = !empty($datas['album_id']) || !empty($albums) ? 'album' : 'search';
             }
             if ($save) {
                 $image->setData($datas)->save();
                 $html['id'] = (int) $image->getId();
                 $html['is_new'] = (int) $isNew;
                 $html['success_message'] = $this->_("Images gallery has been saved successfully");
                 $html['message_timeout'] = 2;
                 $html['message_button'] = 0;
                 $html['message_loader'] = 0;
                 if (isset($datas['images']['list_' . $id])) {
                     foreach ($datas['images']['list_' . $id] as $key => $info) {
                         $gallery = new Media_Model_Gallery_Image_Custom();
                         if (!empty($info['image_id'])) {
                             $gallery->find($info['image_id']);
                         }
                         if (!empty($info['delete'])) {
                             $gallery->delete();
                             continue;
                         }
                         if (!$gallery->getId()) {
                             $gallery->setGalleryId($image->getId());
                         }
                         if (!empty($info['path'])) {
                             $filename = $info['path'];
                             $img_src = Core_Model_Directory::getTmpDirectory(true) . '/' . $filename;
                             if (file_exists($img_src)) {
                                 $relative_path = $option_value->getImagePathTo();
                                 $folder = Application_Model_Application::getBaseImagePath() . $relative_path;
                                 $img_dst = $folder . '/' . $filename;
                                 if (!is_dir($folder)) {
                                     mkdir($folder, 0777, true);
                                 }
                                 if (!@rename($img_src, $img_dst)) {
                                     throw new Exception("An error occurred while saving your images gallery. Please try again later.");
                                 }
                                 $gallery->setUrl($relative_path . '/' . $filename);
                             }
                         }
                         $gallery->setTitle(!empty($info['title']) ? $info['title'] : null)->setDescription(!empty($info['description']) ? $info['description'] : null)->save();
                     }
                 }
             }
         } 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 ($data = $this->getRequest()->getPost()) {
         try {
             if (empty($data['value_id'])) {
                 throw new Exception($this->_('An error occurred while saving the product. Please try again later.'));
             }
             $option_value = new Application_Model_Option_Value();
             $option_value->find($data['value_id']);
             $html = array();
             $product = new Catalog_Model_Product();
             if (!empty($data['product_id'])) {
                 $product->find($data['product_id']);
             }
             $isNew = (bool) (!$product->getId());
             $isDeleted = !empty($data['is_deleted']);
             if ($product->getId() and $product->getValueId() != $option_value->getId()) {
                 throw new Exception($this->_('An error occurred while saving the product. Please try again later.'));
             }
             if (!$isDeleted) {
                 if (!isset($data['is_active'])) {
                     $data['is_active'] = 1;
                 }
                 $data['value_id'] = $option_value->getValueId();
                 $parent_id = $data['category_id'];
                 if (!empty($data['subcategory_id'])) {
                     $data['category_id'] = $data['subcategory_id'];
                 }
                 if (!empty($data['picture'])) {
                     if (!file_exists(Core_Model_Directory::getTmpDirectory(true) . "/" . $data['picture'])) {
                         unset($data['picture']);
                     } else {
                         $illus_relative_path = $option_value->getImagePathTo();
                         $folder = Application_Model_Application::getBaseImagePath() . $illus_relative_path;
                         $file = pathinfo(Core_Model_Directory::getBasePathTo($data['picture']));
                         $filename = $file['basename'];
                         $img_src = Core_Model_Directory::getTmpDirectory(true) . "/" . $data['picture'];
                         $img_dst = $folder . '/' . $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 againg later.'));
                         } else {
                             $data['picture'] = $illus_relative_path . '/' . $filename;
                         }
                     }
                 }
             }
             if (!$product->getId() and empty($data['is_multiple']) or $product->getId() and $product->getData('type') != 'format' and isset($data['option'])) {
                 unset($data['option']);
             }
             $product->addData($data);
             $product->save();
             $html = array('success' => 1);
             if (!$isDeleted) {
                 $product_id = $product->getId();
                 $product = new Catalog_Model_Product();
                 $product->find($product_id);
                 $html = array('success' => 1, 'product_id' => $product->getId(), 'parent_id' => $parent_id, 'category_id' => $data['category_id']);
                 $html['product_html'] = $this->getLayout()->addPartial('row', 'admin_view_default', 'catalog/application/edit/category/product.phtml')->setProduct($product)->setOptionValue($option_value)->toHtml();
             }
         } catch (Exception $e) {
             $html['message'] = $e->getMessage();
         }
         $this->getLayout()->setHtml(Zend_Json::encode($html));
     }
 }
 public function editpostAction()
 {
     if ($datas = $this->getRequest()->getPost()) {
         $html = '';
         try {
             $message = new Push_Model_Message();
             $message->setMessageTypeByOptionValue($this->getCurrentOptionValue()->getOptionId());
             $sendNow = false;
             $inputs = array('send_at', 'send_until');
             foreach ($inputs as $input) {
                 if (empty($datas[$input . '_a_specific_datetime'])) {
                     $datas[$input] = null;
                 } else {
                     if (empty($datas[$input])) {
                         throw new Exception($this->_('Please, enter a valid date'));
                     } else {
                         $date = new Zend_Date($datas[$input]);
                         $datas[$input] = $date->toString('y-MM-dd HH:mm:ss');
                     }
                 }
             }
             if (empty($datas['send_at'])) {
                 $sendNow = true;
                 $datas['send_at'] = Zend_Date::now()->toString('y-MM-dd HH:mm:ss');
             }
             if (!empty($datas['send_until']) and $datas['send_at'] > $datas['send_until']) {
                 throw new Exception($this->_("The duration limit must be higher than the sent date"));
             }
             // Récupère l'option_value en cours
             $option_value = new Application_Model_Option_Value();
             if (!empty($datas['file'])) {
                 $file = pathinfo($datas['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 {
                     $datas['cover'] = $relative_path . $filename;
                 }
             } else {
                 if (!empty($data['remove_cover'])) {
                     $data['cover'] = null;
                 }
             }
             if (empty($datas['action_value'])) {
                 $datas['action_value'] = null;
             } else {
                 if (!preg_match('/^[0-9]*$/', $datas['action_value'])) {
                     $url = "http://" . $datas['action_value'];
                     if (stripos($datas['action_value'], "http://") !== false || stripos($datas['action_value'], "https://") !== false) {
                         $url = $datas['action_value'];
                     }
                     $datas['action_value'] = file_get_contents("http://tinyurl.com/api-create.php?url=" . urlencode($url));
                 }
             }
             $datas['type_id'] = $message->getMessageType();
             $datas['app_id'] = $this->getApplication()->getId();
             $datas["send_to_all"] = $datas["topic_receiver"] ? 0 : 1;
             $message->setData($datas)->save();
             //PnTopics
             if ($datas["topic_receiver"]) {
                 $topic_data = explode(";", $datas["topic_receiver"]);
                 foreach ($topic_data as $id_topic) {
                     if ($id_topic != "") {
                         $category_message = new Topic_Model_Category_Message();
                         $category_message_data = array("category_id" => $id_topic, "message_id" => $message->getId());
                         $category_message->setData($category_message_data);
                         $category_message->save();
                     }
                 }
             }
             if ($message->getMessageType() == 1) {
                 if ($sendNow) {
                     $c = curl_init();
                     curl_setopt($c, CURLOPT_URL, $this->getUrl('push/message/send', array('message_id' => $message->getId())));
                     curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
                     // Follow the redirects (needed for mod_rewrite)
                     curl_setopt($c, CURLOPT_HEADER, false);
                     // Don't retrieve headers
                     curl_setopt($c, CURLOPT_NOBODY, true);
                     // Don't retrieve the body
                     curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
                     // Return from curl_exec rather than echoing
                     curl_setopt($c, CURLOPT_FRESH_CONNECT, true);
                     // Always ensure the connection is fresh
                     // Timeout super fast once connected, so it goes into async.
                     curl_setopt($c, CURLOPT_TIMEOUT, 10);
                     curl_exec($c);
                     curl_close($c);
                 }
             } else {
                 $message->updateStatus('delivered');
             }
             $html = array('success' => 1, 'message_timeout' => 2, 'message_button' => 0, 'message_loader' => 0);
             if ($sendNow) {
                 $html['success_message'] = $this->_('Your message has been saved successfully and will be sent in a few minutes');
             } else {
                 $html['success_message'] = $this->_('Your message has been saved successfully and will be sent at the entered date');
             }
         } 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));
     }
 }
 public function editpostAction()
 {
     if ($datas = $this->getRequest()->getPost()) {
         try {
             $isNew = false;
             $application = $this->getApplication();
             // Test s'il y a un value_id
             if (empty($datas['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($datas['value_id']);
             $html = array('success' => '1', 'success_message' => $this->_('Link has been successfully saved'), 'message_timeout' => 2, 'message_button' => 0, 'message_loader' => 0);
             // Prépare la weblink
             $weblink = $option_value->getObject();
             if (!$weblink->getId()) {
                 $weblink->setValueId($datas['value_id']);
             }
             // S'il y a une cover image
             if (!empty($datas['file'])) {
                 if (!empty($datas['file'])) {
                     $file = pathinfo($datas['file']);
                     $filename = $file['basename'];
                     $relative_path = $option_value->getImagePathTo("cover");
                     $folder = Application_Model_Application::getBaseImagePath() . $relative_path;
                     $img_src = Core_Model_Directory::getTmpDirectory(true) . '/' . $filename;
                     $img_dst = $folder . '/' . $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 againg later.'));
                     } else {
                         $weblink->setCover($relative_path . '/' . $filename);
                     }
                     if (empty($datas['link'])) {
                         $html['success_message'] = $this->_("The image has been successfully saved");
                     }
                 }
             } else {
                 if (!empty($datas['remove_cover'])) {
                     $weblink->setCover(null);
                     if (empty($datas['link'])) {
                         $html['success_message'] = $this->_("The image has been successfully deleted");
                     }
                 }
             }
             // Sauvegarde le weblink
             $weblink->save();
             if (!empty($datas['link'])) {
                 $link_datas = $datas['link'];
                 if (empty($link_datas['url']) or !Zend_Uri::check($link_datas['url'])) {
                     throw new Exception($this->_('Please enter a valid url'));
                 }
                 // Prépare le link
                 $link = new Weblink_Model_Weblink_Link();
                 if (!empty($link_datas['link_id'])) {
                     $link->find($link_datas['link_id']);
                 }
                 $is_deleted = !empty($link_datas["is_deleted"]);
                 $isNew = !$link->getId();
                 $link_datas['weblink_id'] = $weblink->getId();
                 // Test s'il y a un picto
                 if (!empty($link_datas['picto']) and file_exists(Core_Model_Directory::getTmpDirectory(true) . "/" . $link_datas['picto'])) {
                     $file = pathinfo(Core_Model_Directory::getTmpDirectory() . "/" . $link_datas['picto']);
                     $filename = $file['basename'];
                     $relative_path = $option_value->getImagePathTo("pictos");
                     $folder = Application_Model_Application::getBaseImagePath() . $relative_path;
                     $img_src = Core_Model_Directory::getTmpDirectory(true) . '/' . $filename;
                     $img_dst = $folder . '/' . $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 picto. Please try againg later."));
                     } else {
                         $link_datas['picto'] = $relative_path . '/' . $filename;
                     }
                 }
                 // Sauvegarde le link
                 $link->addData($link_datas)->save();
                 if ($is_deleted) {
                     $html['success_message'] = $this->_('Link has been successfully deleted');
                     $html['is_deleted'] = 1;
                 }
             }
             if ($isNew) {
                 $html['row_html'] = $this->getLayout()->addPartial('row_', 'admin_view_default', 'weblink/application/multi/edit/row.phtml')->setCurrentLink($link)->setCurrentOptionValue($option_value)->toHtml();
             }
         } catch (Exception $e) {
             $html = array('message' => $e->getMessage(), 'message_button' => 1, 'message_loader' => 1);
         }
         $this->getLayout()->setHtml(Zend_Json::encode($html));
     }
 }