Current File : /home/karenpetzb/application/modules/backoffice/controllers/CategoryController.php
<?php

class Backoffice_CategoryController extends Modules_Backoffice_Controllers_MainController
{

	function init()
	{
		$this->view->title = "Administration";
		$this->view->currentMenu = "Category";

		$this->isConnectedWithRole('isCategory'); 
	}
	function indexAction()
	{
		$this->_forward('/list');

	}
    
	function listAction()
	{
		$this->view->titlePage = "Gestion des cat�gories";
			
		//Gestion des tris
		$table = 'NOM';
		$tri = 'ASC';
	}

	function delAction() {

		$this->view->messageSuccess = "";
		$this->view->messageError = "";
		if($this->_request->getParam('id')) {
			$id = (int)$this->_request->getParam('id');
			if ($id > 0) {
				try {
					$category = new Category();
					if ($category->fetchRow('IDPARENT = '.$id)) {
						$this->view->messageError = 'Cette cat�gorie est parente';
					} else {
						$product = new Product();
						$select = $product->fetchRow('IDCATEGORY = '.$id);
						if ($select) {
							$this->view->messageError = 'Cette cat�gorie est utilis�e par le produit : <b>'.$select->NOM."<b/>";
						} else {

							$category->delete('ID = '.$id);

							$this->delete_directory('items/category/'.$id);
							$this->delete_directory('items/product/'.$id);

							$this->view->messageSuccess = "La cat�gorie a et� supprim�e";
							$this->log("La cat�gorie a et� supprim�e",'info');

						}
					}
				} catch (Zend_Exception $e) {
					$this->view->messageError = $e->getMessage();
					$this->log($e->getMessage(),'err');
				}
			}
		}
		$this->_forward('/list');

	}

	function editAction() {
		$this->view->titlePage = "Modifier une cat�gorie";
		//populate form
		if ($this->_request->getParam('id')) { $id = (int)$this->_request->getParam('id'); }
		if ($this->_request->getParam('idCat')) { $id = (int)$this->_request->getParam('idCat'); }

		if ($id > 0) {
			$category = new Category();
			$row = $category->fetchRow('ID = '.$id);
			if ($row) {
				$this->view->populateForm = $row->toArray();
 
				$listCat = array();
				$listCat = $category->getTreeCatsOf($id);
				if (!empty($listCat) && !$listCat['SUBS']) {
					$categoryTypeTri = new CategoryTypeTri();
					$this->view->listNavigationTri = $categoryTypeTri->getListTriByCategoryInOneRow($id);
				}

                
		        if ($this->isSiteGallery) {
		            $annonces = new AnnonceCms();
		            $this->view->listAnnoncecms = $annonces->getAllAnnoncesByCategory($id);
                     
		            $annonces = new AnnonceGallery();
		            $this->view->listAnnonceGallery = $annonces->getAllAnnoncesByCategory($id);

		        }

			} else { $this->_forward('/list'); }
		} else {
			$this->_forward('/list');
		}
	}

	function editmigrateAction()
	{ 
		$this->view->titlePage = "Modifier une cat�gorie";
        if ($this->_request->isPost()) {
            //get the form params
			$params = $this->_request->getPost();
             
			try {

				$id = (int)$params['id'];
				$idReceiver = (int)$params['idcategory'];
                    
                if ($id > 0 && $idReceiver > 0) { 
                    $category = new Category();  
                    if ($category->canBeMigrate($id)) { 
                            //Migration des produits
                            $dataProduct = array (
			 		            'IDCATEGORY' => $idReceiver
			                ); 
                            $product = new Product();  
				            $product->update($dataProduct, 'IDCATEGORY = '.$id);
                            
                            $dataCategory = array (
			 		            'isACTIVE' => false
			                ); 
				            $category->update($dataCategory, 'ID = '.$id);
                            
				            $this->view->messageSuccess .= "La cat�gorie a �t� migr�e, la redirection est effectu�e";
                    
				            $this->log("La cat�gorie a �t� migr�e de ".$id." vers ".$idReceiver,'info'); 
                    } else {
					    $this->view->messageError .= "La cat�gorie est parente";
                    }

                } else {                    
					$this->view->messageError .= "V�rifier les param�tres";
                }
 
			} catch (Zend_Exception $e) {
				$this->log($e->getMessage(),'err');
				$this->view->messageError = "Impossible de migrer la cat�gorie".$e->getMessage();
			} 
        }
		$this->_forward('/edit');
	}
    
	function editcategoryAction()
	{
			
		$this->view->titlePage = "Modifier une cat�gorie";
		$id = 0;
		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());


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

			$docChoice = '';
			if (isset($params['docChoice'])) {
				$docChoice = $filter->filter($params['docChoice']);
			}
			
			$urlRedirect = '';
			if (isset($params['urlredirect'])) {
				$urlRedirect = $filter->filter($params['urlredirect']);
			}
			//Refractor the params
			$data = array (
			 		'NOM' => $filter->filter($params['nom']),
					'IDPARENT' => $filter->filter($params['idparent']),
			 		'DESCRIPTION' => $params['desc'], 
			 		'DESCRIPTIONLONG' => $params['desclong'], 
			 		'DESCRIPTIONSHORT' => $params['descshort'], 
			 		'URLREDIRECT' => $urlRedirect, 
			 		'CHOICEDOC' => $docChoice
			);
             
			if (isset($params['position'])) {
				$data['POSITION'] = (int)$params['position'];
			}



			if ($validator->isValid($data['NOM'])) {

				try {

					$id = $params['id'];

					$category = new Category();
					$category->update($data, 'ID = '.$id);

					$this->view->messageSuccess = "La cat�gorie a �t� modifi�e";
					if($this->uploadNewPicsAndSave($id)) {
						$this->view->messageSuccess .= " , l'image aussi";
					}
					if($this->uploadNewPicsSlideAndSave($id)) {
						$this->view->messageSuccess .= " , l'image du slide aussi";
					}

					if($this->uploadNewChoicePics($id)) {
						$this->view->messageSuccess .= " , le comment choisir aussi";
					}
                    
                    $nbSubs = $category->updateTreeInLineAsPathOfChilds($id, true);

					$this->view->messageSuccess .= " , ".$nbSubs." urls d'acc�s modifi�es.";

					$this->log("La cat�gorie a �t� modifi�e : ".$id,'info');

				} catch (Zend_Exception $e) {
					$this->log($e->getMessage(),'err');
					$this->view->messageError = "La cat�gorie existe d�j�";
				}
			} else {
				foreach ($validator->getErrors() as $errorCode) {
					$this->view->messageError =  $this->getErrorValidator($errorCode);
				}
			}

		}
		$this->_forward('/edit');
	}

	function customnavnomeditAction()
	{
		$this->view->titlePage = "Modifier une cat�gorie";
		$id = 0;
		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());

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

			$navnom = $this->verifyNavigationString($filter->filter($params['navnom']), $params['nom'], '');
            
			$keywords = "";
			if (isset($params['keywords']) && !empty($params['keywords'])) {
				$keywords = $params['keywords'];
			} else {
				$navnomStrip =  explode("-", $navnom);
				for ($index = 0; $index < sizeof($navnomStrip); $index++) {
					$word = $navnomStrip[$index];
					if (empty($keywords)) {
						if (strlen($word) > 3){ $keywords .= $word; }
					} else {
						if (strlen($word) > 3){ $keywords .= ",".$word; }
					}
				}
			}

			//Refractor the params
			$data = array (
					'NAVTITRENOM' => $filter->filter($params['navtitrenom']),
			 		'NAVNOM' => $navnom,
					'KEYWORDS' => $keywords,
					'NAVDESCRIPTION' => $filter->filter($params['navdescription'])
			);
            
			if ($validator->isValid($data['NAVNOM']) &&
			$validator->isValid($data['KEYWORDS'])) {
				if (empty($data['NAVTITRENOM'])) {
					$data['NAVTITRENOM'] = $filter->filter($params['nom']);
				}
				if (empty($data['NAVDESCRIPTION'])) {
					$data['NAVDESCRIPTION'] = "";
				}
				try {
					$id = $params['id'];

                    $category = new Category();
                    
                    if (isset($params['urlaccess']) && $params['idparent'] == 0) {
                        $data['NAVNOM_URLPARENTS'] = $this->verifyNavigationString('',$filter->filter($params['urlaccess']), '');
                    }
                    
                    if (empty($data['NAVNOM_URLPARENTS'])) {
                        $data['NAVNOM_URLPARENTS'] = $category->getTreeInLineAsPathOf($id);
                        if (empty($data['NAVNOM_URLPARENTS'])) {
                            $data['NAVNOM_URLPARENTS'] = $navnom;
                        }
                    }
                    
					$category->update($data, 'ID = '.$id);
                    
                    $nbSubs = $category->updateTreeInLineAsPathOfChilds($id, false);

					$this->view->messageSuccess = "La cat�gorie a �t� modifi�e, ".$nbSubs." urls d'acc�s modifi�es.";

				} catch (Zend_Exception $e) {
					$this->log($e->getMessage(),'err');
					$this->view->messageError = "La cat�gorie existe d�j�";
				}
			} else {
				foreach ($validator->getErrors() as $errorCode) {
					$this->view->messageError .=  $this->getErrorValidator($errorCode);
				}
			}

		}
		$this->_forward('/edit');
	}
    
	function editthemeAction()
	{
		$this->view->titlePage = "Modifier le th�me";
		if ($this->_request->isPost()) {
			//get the form params
			$params = $this->_request->getPost();

			//Refractor the params
			$data = array (
			 		'ID_THEME' => $params['idtheme']
			);

			try {

				$id = (int)$params['id'];

				$category = new Category();
				$category->update($data, 'ID = '.$id);
                 $this->view->messageSuccess = "Le th�me a �t� modifi�"; 
			} catch (Zend_Exception $e) {
					$this->log($e->getMessage(),'err');
			}
		}
		$this->_forward('/edit');
	}
	function editactiveAction()
	{
		$this->view->titlePage = "Modifier une cat�gorie";
		if ($this->_request->isPost()) {
			//get the form params
			$params = $this->_request->getPost();

			//Refractor the params
			$data = array (
			 		'isACTIVE' => $params['active']
			);

			try {

				$id = (int)$params['id'];

				$category = new Category();
				$category->update($data, 'ID = '.$id);

				//inversement des bool
				if ($data['isACTIVE'] == 1) {
					$data['isACTIVE'] = 0;
				} else {
					$data['isACTIVE'] = 1;
				}
				$dataProduct = array(
						'isACTIVE' => $data['isACTIVE']
				);
				$produit = new Product();
				$nb = $produit->updateByCategory($id, $dataProduct);
				$this->view->messageSuccess = "La cat�gorie a �t� modifi�e";
			} catch (Zend_Exception $e) {
					$this->log($e->getMessage(),'err');
			}
		}
		$this->_forward('/edit');
	}

	function addAction()
	{
			
		$this->view->titlePage = "Ajouter une cat�gorie";
		$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());

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

			$navnom = $this->verifyNavigationString($filter->filter($params['navnom']), $params['nom'], '');

			$keywords = "";
			if (isset($params['keywords']) && !empty($params['keywords'])) {
				$keywords = $params['keywords'];
			} else {

				$navnomStrip =  explode("-", $navnom);
				for ($index = 0; $index < sizeof($navnomStrip); $index++) {
					$word = $navnomStrip[$index];
					if (empty($keywords)) {
						if (strlen($word) > 3){ $keywords .= $word; }
					} else {
						if (strlen($word) > 3){ $keywords .= ",".$word; }
					}
				}
			}

			$data = array (
			 		'NOM' => $filter->filter($params['nom']),
					'NAVTITRENOM' => $filter->filter($params['nom']),
			 		'DESCRIPTION' => $filter->filter($params['desc']),
			 		'NAVDESCRIPTION' => $filter->filter($params['desc']),
			 		'NAVNOM' => $navnom,
			 		'KEYWORDS' => $keywords,
			 		'IDPARENT' => $filter->filter($params['idparent'])
			);
             
			if ($validator->isValid($data['NOM']) &&
			$validator->isValid($data['NAVNOM'])
			) {
				try {
					$category = new Category();
                    $data['NAVNOM_URLPARENTS'] = $category->getTreeInLineAsPathOf($data['IDPARENT']);                    
                    if (empty($data['NAVNOM_URLPARENTS'])) {
                        $data['NAVNOM_URLPARENTS'] = $navnom;
                    }
					$category->insert($data);

					//create items folder for pics
					$lastid = $category->getAdapter()->lastInsertId();

					if ($lastid > 0) { 
                        $this->checkDirectoryExist("items/category/".$lastid);
                        $this->checkDirectoryExist("items/product/".$lastid);

						$this->view->messageSuccess = "La cat�gorie a �t� ajout�e";
						if($this->uploadNewPicsAndSave($lastid)) {
							$this->view->messageSuccess .= " , l'image a �t� upload�e";
						}

						$this->log("La cat�gorie a �t� ajout�e",'info');
						//Listing Categories
						$category = new Category();
						$this->view->listallcategories = $category->getAllCategoriesByID(0);
                        
                        $this->_request->setParam('id', $lastid);
		                $this->_forward('/edit'); 

					}
				} catch (Zend_Exception $e) {
					$this->view->messageError = "La cat�gorie existe d�j�";
					$this->log($e->getMessage(),'err');
					$this->view->populateForm = $data;
				}
			} else {
				foreach ($validator->getErrors() as $errorCode) {
					$this->view->messageError .=  $this->getErrorValidator($errorCode);
				}
				$this->view->populateForm = $data;
			}
		}
	}
     
	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/category/'.$idCat.'/';
				$this->checkDirectoryExist($repertoireDestination);
				
                $nomDestination = $this->generateNavigationString($elementsChemin['filename']).".".$extensionFichier;
				$fileoriginal = $repertoireDestination.$nomDestination;
                
				if (move_uploaded_file($_FILES[$field]["tmp_name"],$fileoriginal)) {
                        //Resize picture
                        $info = getimagesize($fileoriginal) ;
                        list($width_old, $height_old) = $info;
                        $maxwidth = $this->FeaturePictureResizeWidth;
                        $maxheight = $this->FeaturePictureResizeHeight;
                            
                        if ($width_old > $maxwidth || $height_old > $maxheight) {     
                            Utils_Tool::smart_resize_image($fileoriginal , null, $maxwidth , $maxheight , true , $fileoriginal , true , false ,50 );
                        }  
                        
					return $repertoireDestination.$nomDestination;
				} else {
					$this->view->messageError = "Le fichier n'a pas �t� upload�";
				}
			}
		}
		return "";
    }
	function uploadNewPicsAndSave($idCat) {
        $url = $this->uploadNewPics($idCat, 'picCat');
		if(!empty($url)) {
			try {
				$category = new Category();
				$data = array (
					'URL' => $url
				);
				$category->update($data,'ID = '.$idCat);

				return true;
			} catch (Exception $e) {
				$this->view->messageError = $e->getMessage();
				return false;
			}
		}
		return false;
	}
	function uploadNewPicsSlideAndSave($idCat) {
        $url = $this->uploadNewPics($idCat, 'picSlideCat');
		if(!empty($url)) {
			try {
				$category = new Category();
				$data = array (
					'URL_SLIDE' => $url
				);
				$category->update($data,'ID = '.$idCat);

				return true;
			} catch (Exception $e) {
				$this->view->messageError = $e->getMessage();
				return false;
			}
		}
		return false;
	}

	function uploadNewChoicePics($idCat) {
		if(!empty($_FILES['picChoice']) && !empty($_FILES['picChoice']['name'])) {
			$nomOrigine = $_FILES['picChoice']['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
				// incluant l'heure a la seconde pres
				$repertoireDestination = 'items/category_choice/'.$idCat.'/';
				$this->checkDirectoryExist($repertoireDestination);
				
				$date = new Zend_Date();
					
				//$nomDestination = $date->toString('dd-MM-YYYY_HH-mm-ss').".".$extensionFichier;
                $nomDestination = $this->generateNavigationString($elementsChemin['filename']).".".$extensionFichier;
					
				if (move_uploaded_file($_FILES["picChoice"]["tmp_name"],$repertoireDestination.$nomDestination)) {
					try {
						$category = new Category();
						$data = array (
					 		'CHOICEURL' => $repertoireDestination.$nomDestination
						);
						$category->update($data,'ID = '.$idCat);

						return true;
					} catch (Exception $e) {
						$this->view->messageError = $e->getMessage();
						return false;
					}
				} else {
					$this->view->messageError = "Le fichier n'a pas �t� upload�";
				}
			}
		}

		return false;
	}
	function setpictureAction() {

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

		if($this->_request->isPost()) {
			$params = $this->_request->getPost();
			try {
				$category = new Category();
                if (isset($params['typeimage']) && $params['typeimage'] == 1) {
				    $data = array (
				 		    'URL_SLIDE' => $params['picture']
				    );
                } else {
				    $data = array (
				 		    'URL' => $params['picture']
				    );
                }
				$category->update($data,'ID = '.$params['idCat']);
				$this->view->messageSuccess = "L'image a �t� modifi�e";
			} catch (Zend_Exception $e) {
					$this->log($e->getMessage(),'err');
				$this->view->messageError = $e->getMessage();
			}
		}
		$this->_forward('/edit');

	}

	function erasepictureAction() {

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

		if($this->_request->isPost()) {
			$params = $this->_request->getPost();

			try {
				$category = new Category();

				unlink($params['picture']);
				$data = array (
			 		'URL' => ''
			 		);
			 		$category->update($data,"ID = ".$params['idCat']." AND URL = '".$params['picture']."'");
			 		$this->view->messageSuccess = "L'image a �t� supprim�e d�finitivement";
			} catch (Zend_Exception $e) {
					$this->log($e->getMessage(),'err');
				$this->view->messageError = $e->getMessage();
			}
		}
		$this->_forward('/edit');

	}


	function setpicturechoiceAction() {

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

		if($this->_request->isPost()) {
			$params = $this->_request->getPost();
			try {
				$category = new Category();
				$data = array (
				 	'CHOICEURL' => $params['picture']
				);
				$category->update($data,'ID = '.$params['idCat']);
				$this->view->messageSuccess = "L'image a �t� modifi�e";
			} catch (Zend_Exception $e) {
					$this->log($e->getMessage(),'err');
				$this->view->messageError = $e->getMessage();
			}
		}
		$this->_forward('/edit');

	}

	function erasepicturechoiceAction() {

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

		if($this->_request->isPost()) {
			$params = $this->_request->getPost();

			try {
				$category = new Category();
				unlink($params['picture']);
				$data = array (
			 		'CHOICEURL' => ''
			 		);
			 		$category->update($data,"ID = ".$params['idCat']." AND CHOICEURL = '".$params['picture']."'");
			 		$this->view->messageSuccess = "L'image a �t� supprim�e d�finitivement";
			} catch (Zend_Exception $e) {
					$this->log($e->getMessage(),'err');
				$this->view->messageError = $e->getMessage();
			}
		}
		$this->_forward('/edit');

	}
	
	
	function deletegalleryAction() {

		$this->view->messageSuccess = "";
		$this->view->messageError = "";
		if($this->_request->getParam('id') && $this->_request->getParam('idgallery')) {
			$id = (int)$this->_request->getParam('id');
			$idgallery = (int)$this->_request->getParam('idgallery');
			if ($id > 0 && $idgallery > 0) {
				try {
					
					$annonce = new AnnonceGallery();
					$dataAnnonceCurrent = $annonce->fetchRow('ID = '.$idgallery);
					
					$dataAnnonce = array ( 
						'ID_CATS' => str_replace(":".$id.":", "", $dataAnnonceCurrent['ID_CATS'])
					);
				 
					$annonce->update($dataAnnonce,'ID = '.$idgallery); 
			 		$this->view->messageSuccess = "L'image a �t� supprim�e de la cat�gorie";
					$this->log("L'image ".$idgallery." a �t� supprim�e de la cat�gorie".$id,'info');
					
				} catch (Zend_Exception $e) {
					$this->view->messageError = $e->getMessage();
					$this->log($e->getMessage(),'err');
				}
			}
		}
		$this->_forward('/edit'); 
	}
	
	function deletecmsAction() {

		$this->view->messageSuccess = "";
		$this->view->messageError = "";
		if($this->_request->getParam('id') && $this->_request->getParam('idarticle')) {
			$id = (int)$this->_request->getParam('id');
			$idarticle = (int)$this->_request->getParam('idarticle');
			if ($id > 0 && $idarticle > 0) {
				try {
					
					$annonce = new AnnonceCms();
					$dataAnnonceCurrent = $annonce->fetchRow('ID = '.$idarticle);
					
					$dataAnnonce = array ( 
						'ID_CATS' => str_replace(":".$id.":", "", $dataAnnonceCurrent['ID_CATS'])
					);
				 
			 		$annonce->update($dataAnnonce,'ID = '.$idarticle); 
			 		$this->view->messageSuccess = "L'article a �t� supprim� de la cat�gorie";
					$this->log("L'article ".$idarticle." a �t� supprim� de la cat�gorie".$id,'info');
					
				} catch (Zend_Exception $e) {
					$this->view->messageError = $e->getMessage();
					$this->log($e->getMessage(),'err');
				}
			}
		}
		$this->_forward('/edit');
	}
	
	
	function addmultiplegalleryAction() {
		$this->view->messageSuccess = "";
		$this->view->messageError = "";
		  
		if($this->_request->isPost()) {
			$params = $this->_request->getPost();
			  
			$id = (int)$this->_request->getParam('idCat');
			$annonce = new AnnonceGallery();
			
			 if ($id > 0) { 
				$dataAnnonce = array (
						'NOM' => "",
						'CONTENT' => "",
						'CONTENT_SHORT' => "",
						'CONTENT_LONG' => "",
						'POSITION' => 0,
						'isSHOW' => 0,
						'ID_CATS' => ":".$id.":"
				); 
				try { 
					$annonce->insert($dataAnnonce);
					$lastid = $annonce->getAdapter()->lastInsertId();                               
					$result = $this->uploadHandlerFileUpload($lastid); 
					
					if (isset($result['success']) && $result['success'] == true) {

						$this->log("L'image a �t� ajout�e ".$lastid ,'info'); 
						
					} else {
						 
						$annonce->delete('ID = '.$lastid);						
						$this->delete_directory('items/gallery/'.$lastid);
						$this->log("L'image a �t� ajout�e puis supprim�e : ".$result['error'],'err'); 
					}	
								 
					echo json_encode($result);  
					
				} catch (Zend_Exception $e) { 
					$this->log($e->getMessage(),'err');
				}
			} 
		}
		 
		$layout = Zend_Layout::getMvcInstance();
		$layout->disableLayout(); 
		$this->render('ajaxvalue'); 
	} 
	
	function uploadHandlerFileUpload($id) { 
		$uploader = new UploadHandler();
 
		$uploader->allowedExtensions = array("jpg", "jpeg",  "gif", "png"); 
		$uploader->sizeLimit = null; 
		$uploader->inputName = "qqfile";
		
		$repertoireGallery = 'items/gallery/';
		$_REQUEST['qquuid'] = $id; 
		
		$currentName = '';
		 if (isset($_REQUEST['qqfilename'])) {
			 $currentName = $_REQUEST['qqfilename'];
		} else if (isset($_FILES[$uploader->inputName])) {
            $currentName = $_FILES[$uploader->inputName]['name'];
		}
		 
		$elementsChemin = pathinfo($currentName);
		$extensionFichier = strtolower($elementsChemin['extension']); 
		$nomDestination = $this->generateNavigationString($elementsChemin['filename']).".".$extensionFichier; 
		 
		// If you want to use the chunking/resume feature, specify the folder to temporarily save parts.
		$this->checkDirectoryExist($repertoireGallery."chunks");
		$uploader->chunksFolder = $repertoireGallery."chunks";
  

		// Call handleUpload() with the name of the folder, relative to PHP's getcwd()
		$result = $uploader->handleUpload($repertoireGallery, $nomDestination);
 
		if (isset($result['success']) && $result['success'] == true) {
			$repertoireDestination = $repertoireGallery.$id."/";
			$fileoriginal = $repertoireDestination.$nomDestination;	 
			
			//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 ); 
					}
				} 
			}
			
			//Save url
			$url = $repertoireDestination.$result["uploadName"];
			if(!empty($url)) {
				try { 
					$annonce = new AnnonceGallery();
					$data = array ( 'URL' => $url, 'NOM' => $elementsChemin['filename'] );
					$annonce->update($data,'ID = '.$id);  
				} catch (Exception $e) {
					$this->log($e->getMessage(),'err');
				}
			} 
		}

		return $result;
	}  
	
}
?>