Current File : /home/karenpetzb/application/modules/backoffice/controllers/AnnoncegalleryController.php
<?php
class Backoffice_AnnoncegalleryController extends Modules_Backoffice_Controllers_MainController
{

	public function init()
	{
		$this->view->title = "Administration";
		$this->view->currentMenu = "AnnonceGallery";
		$this->isConnectedWithRole('isCategory');
	}
	public function indexAction()
	{
		$this->_forward('/list');
	}

    public function ajaxshowallpictureAction() {
		$annonces = new AnnonceGallery();
		$result = $annonces->getAllAnnonces();
		$this->view->listAnnonceGallery = $result;

		$layout = Zend_Layout::getMvcInstance();
		$layout->disableLayout();
		$this->render('ajaxshowallpicture');
    }
    

	function editpositionAction() {
		
		if ($this->getRequest()->isPost()) {

			//get the form params
			$params = $this->_request->getPost();
			
			$id = 0;
			$position = 0;
			$category = 0;
			if (!empty($params['id'])) { $id = (int)$params['id']; }
			if (!empty($params['position'])) { $position = (int)$params['position']; }
			if (!empty($params['category'])) { $category = (int)$params['category']; }

			if($id > 0 && $category > 0) {
				try {
					$annonce = new AnnonceGallery();
					$dataAnnonce = array (
						'POSITION' => $position
					);
					$annonce->update($dataAnnonce,'ID = '.$id); 

					$annonceList = $annonce->select()
											->where("ID <> ".$id." AND ID_CATS LIKE '%:".$category.":%'")
											->order('POSITION ASC')
											->query()->fetchAll();
					 
					$count = 0;
					foreach($annonceList as $row)
					{
						if ($count == $position) {
							$count++;
						}
						$dataAnnonce['POSITION'] = $count;
						$annonce->update($dataAnnonce,'ID = '.$row['ID']); 
						$count++;
					}
					 $this->view->messageSuccess = "Les positions ont �t� mises � jour";
				} catch (Zend_Exception $e) {
					$this->log($e->getMessage(),'err'); 
					$this->view->messageError = "La mise � jour n'a pas �t� r�alis�e";
				} 
			} 
		}

		$layout = Zend_Layout::getMvcInstance();
		$layout->disableLayout();
		$this->render('message');
    }

	public function editAction()
	{
			
		$this->view->titlePage = "Modifier une image";
		$this->view->messageSuccess = "";
		$this->view->messageError = "";
			
		//filtres pour changer les chaines
		$filter = new Zend_Filter();
		$filter	->addFilter(new Zend_Filter_StripTags())
		->addFilter(new Zend_Filter_StringTrim());

		//valideurs pour les chaines
		$validator = new Zend_Validate();
		$validator -> addValidator(new Zend_Validate_NotEmpty())
		-> addValidator(new Zend_Validate_StringLength(3));

		$annonce = new AnnonceGallery();
		if ($this->getRequest()->getParam('id')) {
			$id = (int)$this->getRequest()->getParam('id');
			if ($id>0) {
				$this->view->populateFormAnnonceGallery = $annonce->fetchRow('ID = '.$id);
			}
		}
		if ($this->getRequest()->isPost()) {

			//get the form params
			$params = $this->_request->getPost();

			//Refractor the params
			$dataAnnonce = array (
			 	'ID' => (int)$params['id'],
			 	'NOM' => $filter->filter($params['nom']),
			 	'CONTENT' => $params['content'],
			 	'CONTENT_SHORT' => $filter->filter($params['content_short']),
			 	'CONTENT_LONG' => $params['content_long'],
			 	'POSITION' => (int) $filter->filter($params['position']),
			 	'isSHOW' => $params['isshow'],
				'ID_CATS' => ''
			);
					
			if (isset($params['categories'])) {
				foreach($params['categories'] as $value)
				{
					$dataAnnonce['ID_CATS'] .= ":".$value.":";
				}
			} 
			try {
				$id = (int)$params['id'];
				if ( $id > 0) {
									
					$annonce->update($dataAnnonce,'ID = '.$id);
					$this->view->messageSuccess = "L'image a �t� modifi�e";
                                 
					$this->uploadNewPicsAndSave($id);

					$this->view->populateFormAnnonceGallery = $annonce->fetchRow('ID = '.$id);
					$this->log("L'image a �t� modifi�e ".$id,'info');
				}
			} catch (Zend_Exception $e) {
				$this->log($e->getMessage(),'err');
				$this->view->populateFormAnnonceGallery = $dataAnnonce;
			} 

		}
	}
	 
    function uploadNewPicsAndSave($id) {
        $url = $this->uploadNewPics($id, 'picture');
		if(!empty($url)) {
			try {
				$annonce = new AnnonceGallery();
				$data = array (
					'URL' => $url
				);
				$annonce->update($data,'ID = '.$id);

				return true;
			} catch (Exception $e) {
				$this->view->messageError = $e->getMessage();
				return false;
			}
		}
		return false;
	}
    function uploadNewPics($idCat, $field) {
        if(!empty($_FILES[$field]) && !empty($_FILES[$field]['name'])) {
			$nomOrigine = $_FILES[$field]['name'];
			$elementsChemin = pathinfo($nomOrigine);
			$extensionFichier = strtolower($elementsChemin['extension']);
			$extensionsAutorisees = array("jpg", "jpeg",  "gif", "png");
			if (!(in_array($extensionFichier, $extensionsAutorisees))) {
				$this->view->messageError = "Le fichier n'a pas l'extension attendue";
			} else {
				// Copie dans le repertoire du script avec un nom
				$repertoireDestination = 'items/gallery/'.$idCat.'/';
				$this->checkDirectoryExist($repertoireDestination);
				
                $nomDestination = $this->generateNavigationString($elementsChemin['filename']).".".$extensionFichier;
				$fileoriginal = $repertoireDestination.$nomDestination;
                
				if (move_uploaded_file($_FILES[$field]["tmp_name"],$fileoriginal)) {
                        //Resize picture
                        $fileResizedName = $repertoireDestination.$this->generateNavigationString($elementsChemin['filename']);
                        
                        //Admin
                        Utils_Tool::smart_resize_image($fileoriginal , null, 100 , null , true , $fileResizedName."_100.".$extensionFichier , false , false ,80 ); 
                                
                        if (isset($this->FeatureSiteThemeCms) && !empty($this->FeatureSiteThemeCms)) { 
		                    foreach ($this->FeatureSiteThemeCms as $row) { 
                                if (isset($row['WIDTH_THUMB']) && $row['WIDTH_THUMB'] > 0) {
                                    Utils_Tool::smart_resize_image($fileoriginal , null, $row['WIDTH_THUMB'] , null , true , $fileResizedName."_".$row['WIDTH_THUMB'].".".$extensionFichier , false , false ,80 ); 
                                }
                            } 
	                    }

					return $repertoireDestination.$nomDestination;
				} else {
					$this->view->messageError = "Le fichier n'a pas �t� upload�";
				}
			}
		}
		return "";
    }

	public function addAction()
	{
			
		$this->view->titlePage = "Ajouter une image";
		$this->view->messageSuccess = "";
		$this->view->messageError = "";

		if ($this->_request->isPost()) {

			//filtres pour changer les chaines
			$filter = new Zend_Filter();
			$filter	->addFilter(new Zend_Filter_StripTags())
			->addFilter(new Zend_Filter_StringTrim());

			//valideurs pour les chaines
			$validator = new Zend_Validate();
			$validator -> addValidator(new Zend_Validate_NotEmpty())
			-> addValidator(new Zend_Validate_StringLength(3));


			//get the form params
			$params = $this->_request->getPost();

			//Refractor the params
			$dataAnnonce = array (
			 		'NOM' => $filter->filter($params['nom']),
			 		'CONTENT' => $params['content'],
			 		'CONTENT_SHORT' => $filter->filter($params['content_short']),
			 		'CONTENT_LONG' => $params['content_long'],
			 		'POSITION' => (int) $filter->filter($params['position']),
			 		'isSHOW' => $params['isshow'],
					'ID_CATS' => ''
			);
					
			if (isset($params['categories'])) {
				foreach($params['categories'] as $value)
				{
					$dataAnnonce['ID_CATS'] .= ":".$value.":";
				}
			}
			if(!empty($_FILES['picture']) && !empty($_FILES['picture']['name'])) {
					 
				try {
					$annonce = new AnnonceGallery();
					$annonce->insert($dataAnnonce);

					$lastid = $annonce->getAdapter()->lastInsertId();                             
					$this->uploadNewPicsAndSave($lastid);
								
					$this->view->messageSuccess = "L'image a �t� ajout�e";
								
					$this->log("L'image a �t� ajout�e",'info');
				} catch (Zend_Exception $e) {
					$this->log($e->getMessage(),'err');
					$this->view->populateFormAnnonceGallery = $dataAnnonce;
				} 
			} else {
				$this->view->messageError = "L'image est obligatoire";
			}
		}
	}
	
	

	public function listAction()
	{
		$this->view->titlePage = "Gestion des images"; 
			
		//Appel model pour listing
		$annonces = new AnnonceGallery();
		$result = $annonces->getAllAnnonces();
         
		$this->view->listAnnonceGallery = $result;
	}



	public function delAction() {

		$this->view->messageSuccess = "";
		$this->view->messageError = "";

		if($this->_request->getParam('id')) {
			$id = (int)$this->_request->getParam('id');
			if ($id > 0) {
				try {
					$annonce = new AnnonceGallery();

					$annonce->delete('ID = '.$id);
                    
                    $this->delete_directory('items/gallery/'.$id);

					$this->view->messageSuccess = "L'image a �t� supprim�e";

					$this->log("L'image a �t� supprim�e",'info');
				} catch (Zend_Exception $e) {
					$this->log($e->getMessage(),'err');
					$this->view->messageError = $e->getMessage();
				}
			}
		}
		$this->_forward('/list');
	}

}
?>