Current File : /home/karenpetzb/application/modules/default/controllers/ProduitspromotionController.php
<?php

class ProduitsPromotionController extends Modules_Default_Controllers_MainController
{

	private $myCaddy;

	public function init()
	{
		$this->view->baseUrl = $this->getBaseUrl();
		$this->checkMaintenance();

	}
    
    private function computeUrlNavigationParent($row) {
        return $row['NAVNOM_URLPARENTS']."/".$row['CATNAVNOM'];
    }
    
	public function indexAction()
	{
		$params = $this->getRequest()->getParams();
		$params['c'] = 1;
		$params['page'] = 'nos-promotions';
		$this->getRequest()->setParams($params);
		$this->_forward('/categorie');
	}
	private function computeProductListToShow($list, $user) {
		try {
			$listProducts = array();
			$metakeywords = '';

			$isAllPromos = false;
			$currentPromoAll = array();
			$currentPromo = array();
			$listUserCodeInternIdBrendFound = array();
			$listUserCodeInternIdBrends = array();
			$listIdBrendFound = array();
			$listUserIdBrendFound = array();
			$listUserIdBrends = array();
			$listIdBrends = array();
			$promoCalculator = new PromoCalculator();
			$promoProduct = new PromoProduct();
			$promoUser = new PromoUser();
			$product= new Product();
 
			if (!empty($list)) {
				$idCategory = $list[0]['CATID'];
				$myPromo = $promoProduct->getRemiseByProductCategory($idCategory);
				if ($this->isPromoActive($myPromo)) { $isAllPromos = true; $currentPromoAll = $myPromo; }
			}
			$productChildQte = new ProductChildQte();

			foreach ($list AS $row) {
				$currentProduct = array();
				$currentProduct['ID'] = $row['ID'];
				$currentProduct['NOM'] = $row['NOM'];
				//$currentProduct['DESCSHORT'] = $this->cutString($row['DESCSHORT'], 80);
				$currentProduct['DESCSHORT'] = $row['DESCSHORT'];
				$currentProduct['URL'] = $row['URL'];
				$currentProduct['BREND'] = $row['BREND'];
				$currentProduct['isSHOWBREND'] = $row['isSHOWBREND'];
				$currentProduct['BRENDURL'] = $row['BRENDURL'];
				$currentProduct['NBREFERENCE'] = $row['NBREFERENCE'];
				$currentProduct['isDEVISPRODUCT'] = $row['isDEVISPRODUCT'];
                $currentProduct['NAVNOM_URLPARENTS'] = $this->computeUrlNavigationParent($row);

				if (isset($row['NAVNOM'])) {
					$navnom = $row['NAVNOM'];
				} else {
					$navnom = $row['PRODNAVNOM'];
				}

				$currentProduct['NAVNOM'] = $this->verifyNavigationString($navnom, $row['NOM'], '');
                

				$currentProduct['NAVTITRE'] = $row['NAVTITRE'];
				$currentProduct['NAVDESC'] = $row['NAVDESC'];

				$isFirst = false;

				if (empty($metakeywords)) {
					$metakeywords .= $row['KEYWORDS_PROD'];
				} else {
					$metakeywords .= ','.$row['KEYWORDS_PROD'];
				}

				$currentProduct['isPROMO'] = $row['isPROMO'];
				$currentProduct['PRIX'] = $row['PRIX'];

				/*
				 * PRIX DEGRESSIF
				 */
				$currentProduct['isPRIXDEGRESSIF'] = $productChildQte->isPrixDegressifByProductID($row['ID']);
					
				/*
				 * CALCUL PROMO
				 */
				$idBrend = $row['BRENDID'];

				if (!in_array($idBrend, $listIdBrends, true)) {
					$myPromo = $promoProduct->getRemiseByProductBrend($idBrend);
					array_push($listIdBrends, $idBrend);
					if ($this->isPromoActive($myPromo)) { array_push($listIdBrendFound, array('ID' => $idBrend, 'PROMO' => $myPromo)); }
				}
				if ($row['isPROMO'] == 1) {
					$isRemise = false;
					if (in_array($idBrend, $listIdBrends, true)) {
						foreach ($listIdBrendFound as $brendPromos) {
							if ($brendPromos['ID'] == $idBrend) {
								$currentProduct['isPROMO'] = 0;
								$currentPromo = $brendPromos['PROMO'];
								if ($currentPromo['REMISEEURO'] > 0) {
									$currentProduct['PRIX'] = $promoCalculator->calculEuro($row['PRIX'], $currentPromo['REMISEEURO']);
									$isRemise = true;
								}
								if ($currentPromo['REMISEPOUR'] > 0) {
									$currentProduct['PRIX'] = $promoCalculator->calculPour($row['PRIX'], $currentPromo['REMISEPOUR']);
									$isRemise = true;
								}
								break;
							}
						}
					}
					if ($isAllPromos && !empty($currentPromoAll) && !$isRemise ) {
						$currentProduct['isPROMO'] = 0;
						if ($currentPromoAll['REMISEEURO'] > 0) {
							$currentProduct['PRIX'] = $promoCalculator->calculEuro($row['PRIX'], $currentPromoAll['REMISEEURO']);
						}
						if ($currentPromoAll['REMISEPOUR'] > 0) {
							$currentProduct['PRIX'] = $promoCalculator->calculPour($row['PRIX'], $currentPromoAll['REMISEPOUR']);
						}
					}
				}
				if (isset($user) && !empty($user)) {
					$userId = $user['id'];
					$codeIntern = $user['cintern'];
					$isRemise = false;

					if (!empty($codeIntern)) {
						if (!in_array($idBrend.'-'.$codeIntern, $listUserCodeInternIdBrends, true)) {
							$myPromo = $promoUser->getRemiseByCodeInternMarque($idBrend, $codeIntern);
							array_push($listUserCodeInternIdBrends, $idBrend.'-'.$codeIntern);
							if ($this->isPromoActive($myPromo)) { array_push($listUserCodeInternIdBrendFound, array('ID' => $idBrend, 'CODE' => $codeIntern, 'PROMO' => $myPromo)); }
						}
						if (in_array($idBrend.'-'.$codeIntern, $listUserCodeInternIdBrends, true)) {
							foreach ($listUserCodeInternIdBrendFound as $brendPromos) {
								if ($brendPromos['ID'] == $idBrend && $brendPromos['CODE'] == $codeIntern ) {
									$currentProduct['isPROMO'] = 0;
									$currentPromo = $brendPromos['PROMO'];
									if ($currentPromo['REMISEEURO'] > 0) {
										$currentProduct['PRIX'] = $promoCalculator->calculEuro($row['PRIX'], $currentPromo['REMISEEURO']);
										$isRemise = true;
									}
									if ($currentPromo['REMISEPOUR'] > 0) {
										$currentProduct['PRIX'] = $promoCalculator->calculPour($row['PRIX'], $currentPromo['REMISEPOUR']);
										$isRemise = true;
									}
									break;
								}
							}
						}
					}
					if (!$isRemise) {
						if (!in_array($idBrend, $listUserIdBrends, true)) {
							$myPromo = $promoUser->getRemiseByMarque($idBrend, $userId);
							array_push($listUserIdBrends, $idBrend);
							if ($this->isPromoActive($myPromo)) { array_push($listUserIdBrendFound, array('ID' => $idBrend, 'PROMO' => $myPromo)); }
						}
						if (in_array($idBrend, $listUserIdBrends, true)) {
							foreach ($listUserIdBrendFound as $brendPromos) {
								if ($brendPromos['ID'] == $idBrend) {
									$currentProduct['isPROMO'] = 0;
									$currentPromo = $brendPromos['PROMO'];
									if ($currentPromo['REMISEEURO'] > 0) {
										$currentProduct['PRIX'] = $promoCalculator->calculEuro($row['PRIX'], $currentPromo['REMISEEURO']);
										$isRemise = true;
									}
									if ($currentPromo['REMISEPOUR'] > 0) {
										$currentProduct['PRIX'] = $promoCalculator->calculPour($row['PRIX'], $currentPromo['REMISEPOUR']);
										$isRemise = true;
									}
									break;
								}
							}
						}
					}
				}


				if ($currentProduct['PRIX'] <= 0 ) { $currentProduct['PRIX'] = $row['PRIX']; }
				
				$currentProduct['PRIXLOWEST'] = $product->calculateLowestPrice($currentProduct['ID']);
				
				array_push($listProducts, $currentProduct);
			}
			return $result = array( 'PRODUCTS' => $listProducts, 'META_KEY' =>  $metakeywords);
		} catch (Zend_Exception $e) {
			$this->log('Erreur computeProductListToShow : '.$e->getMessage(),'err');
		}
		return $result = array( 'PRODUCTS' => array(), 'META_KEY' =>  '');
	}
	public function categorieAction()
	{
		$userNamespace = $this->getSession();
		$category = new Category2();
		$product = new Product();

		$auth = Zend_Auth::getInstance();
		$auth->setStorage($this->getSessionStorage());
		$storage = $auth->getStorage()->read();
		$currentUser = array();
		if ($auth->hasIdentity() && isset($storage['user'])) {
			$currentUser = $storage['user'];
		}
		if ($this->getRequest()->getParam('c')) {
			$id = (int)$this->getRequest()->getParam('c');

			if ($id>0) {

				try {
					$this->checkTypeOfPromo($id);
					$listCat = array();
					$listCat = $category->getTreeCatsOf($id);
					if (!empty($listCat)) {
						$this->view->listCat = $listCat;

						$this->view->isPromoPage = true;
						$breadcrumb = $category->getTreeInLineOf($id);
						$this->view->breadcrumb = $breadcrumb;
						$this->view->actualDesign = $this->showDesign(0, 0);

						if (empty($listCat['NAVTITRENOM'])) {
							$listCat['NAVTITRENOM'] = $listCat['NOM'];
						}
						if (empty($listCat['NAVDESCRIPTION'])) {
							$listCat['NAVDESCRIPTION'] = $listCat['DESCRIPTION'];
						}

						$filter = new Zend_Filter();
						$filter	->addFilter(new Zend_Filter_StripTags())->addFilter(new Zend_Filter_StringTrim());

						$this->view->metadescription = $listCat['NAVDESCRIPTION'];
						$this->view->title = $listCat['NAVTITRENOM'];
						$isTri_metaKeyword = false;

						$annoncesleft = new AnnonceLeft();
						$this->view->listads = $annoncesleft->getAnnoncesByShow($id);

						if (!$listCat['SUBS']) {
							$triSql = 'NOM ASC';
							//List of products
							$productList = $product->getProductsByIdCategoryForPromotion($id, $triSql);


							$this->view->orderNameProduct = $userNamespace->objOrderNameProduct;
							$resultProduct = $this->computeProductListToShow($productList, $currentUser);
							$this->view->listAllProducts = $resultProduct['PRODUCTS'];

                            if (!empty($listCat['KEYWORDS'])) {
                                $this->view->metakeywords = $listCat['KEYWORDS'];
                            } else {                                
                                $this->view->metakeywords = $resultProduct['META_KEY'];
                            }
							
							//Show product page
							$this->render('nosproduits');
						} else {
							$metakeywords = $listCat['KEYWORDS'];

                            if (empty($metakeywords)) {
							    for ($index = 0; $index < sizeOf($listCat['SUBS']); $index++) {
								    if (empty($metakeywords)) {
									    $metakeywords .= $listCat['SUBS'][$index]['KEYWORDS'];
								    } else {
									    $metakeywords .= ','.$listCat['SUBS'][$index]['KEYWORDS'];
								    }
							    }
                            }
							$this->view->metakeywords = $metakeywords;
						}
					} else {
						$this->log('Impossible de trouver la categorie : '.$this->getRequest()->getRequestUri(), 'warn');
						$this->_redirect('/');
					}

				} catch (Zend_Exception $e) {
					$this->log('Erreur pour trouver : '.$this->getRequest()->getRequestUri()." ".$e->getMessage(),'err');
					$this->_redirect('/');
				}
			} else {
				$this->_redirect('/');
			}
		}  else {
			$this->_redirect('/');
		}
	}

	private function checkTypeOfPromo($id) {
		$category = new Category2();
		$firstCategory = $category->getParentFirstOf($id);
		if ($firstCategory != null) {
			$currentId = $firstCategory["ID"];
			if ($currentId == 2) { $this->view->prefixPromoClass = "eco-"; }
			if ($currentId == 1) { $this->view->prefixPromoClass = ""; }
		}
	}

	private function isPromoActive($myPromo) {
		if ($myPromo) {
			if ($myPromo['REMISEEURO'] > 0) { return true; }
			if ($myPromo['REMISEPOUR'] > 0) { return true; }
		}
		return false;
	}
}
?>