Current File : /home/karenpetzb/application/models/utils/Caddy.php
<?php
class Caddy {
	var $items;
	var $itemsCaddyType;

	function Caddy() {
		$this->items = array();
		$this->itemsCaddyType = array();
	}
	
	private function getStorage() {
		$registry = Zend_Registry::getInstance();
		$setting = $registry->get('setting');
		return new Zend_Auth_Storage_Session($setting->session_storage);
	}
	
	function getTotalSize() {
		$result = 0;
        
		if (isset($this->items) && !empty($this->items)) { $result += sizeof($this->items); }
		
		$auth = Zend_Auth::getInstance();
		$auth->setStorage($this->getStorage());
		$storage = $auth->getStorage()->read(); 
		if ($auth->hasIdentity() && isset($storage['user']) && $storage['user']['iscaddytype'] == 'Y') {
			if (isset($this->itemsCaddyType) && !empty($this->itemsCaddyType)) { $result += sizeof($this->itemsCaddyType); }
		} 
		return $result;
	}

	function cleanUp() {
		$this->items = array();
		$this->itemsCaddyType = array();
	}

	function addItemToCaddy($idChild, $qteChild, $isAccessoire, $selectedOption){
		$currentItem = false;  
		foreach($this->items as $key => $item) { 
			if ($item->idChild == $idChild && $item->selectedOption == $selectedOption) {
				$item->qteChild = $qteChild; 
				$currentItem = true;  
			} 
		}    
		if ($currentItem == false) {
			$item = new Item();
			$item->getItemInfo($idChild, $qteChild, $isAccessoire, $selectedOption);
			array_push($this->items, $item);
		} 
	}
	function addItemToCaddyType($idCaddyType, $qte, $selectedOption){
		$currentItem = false;
		foreach($this->itemsCaddyType as $item) {
			if ($item->id == $idCaddyType && $item->selectedOption == $selectedOption) {
				$item->qte = $qte;
				$currentItem = true;
				break;
			}
		}
		if ($currentItem == false) {
			$itemCaddyType = new ItemCaddyType();
			$itemCaddyType->getItemInfo($idCaddyType, $qte, $selectedOption);
			array_push($this->itemsCaddyType, $itemCaddyType);
		}
	}
	function delItemToCaddyType($idCaddyType, $selectedOption){
		$currentKey = -1;
		foreach($this->itemsCaddyType as $key => $item) {
			if ($item->id == $idCaddyType && $item->selectedOption == $selectedOption) {
				$currentKey = $key; 
				break;
			}
		}
		if ($currentKey != -1) { unset($this->itemsCaddyType[$currentKey]); }
		if (!is_array($this->itemsCaddyType)) {  $this->itemsCaddyType = array(); }
		$tmp = $this->itemsCaddyType;
		$this->itemsCaddyType = array_values($tmp); 
	}

	function delItemToCaddy($idChild, $selectedOption){
		$currentKey = -1; 
		foreach($this->items as $key => $item) {
			if ($item->idChild == $idChild && $item->selectedOption == $selectedOption) {
				$currentKey = $key;
				break;
			}
		} 
		if ($currentKey != -1) { unset($this->items[$currentKey]); }
		if (!is_array($this->items)) {  $this->items = array(); }
		$tmp = $this->items;
		$this->items = array_values($tmp);
	}
	
	function delItemToCaddyByKey($currentKey){  
		if ($currentKey != -1) { unset($this->items[$currentKey]); }
		if (!is_array($this->items)) {  $this->items = array(); }
		$tmp = $this->items;
		$this->items = array_values($tmp);
	}

	function getQuantity($idChild) {
		if (!empty($this->items)) {
			foreach($this->items as $item) {
				if ($item->idChild == $idChild) {
					return $item->qteChild;
				}
			}
		}
		return 0;
	}

	function getItemById($idChild) {
		foreach($this->items as $item) {
			if ($item->idChild == $idChild) {
				return $item;
			}
		}
		return null;
	}

	function getItemByIdAndOption($idChild, $selectedOption) {
		foreach($this->items as $item) {
			if ($item->idChild == $idChild && $item->selectedOption == $selectedOption) {
				return $item;
			}
		}
		return null;
	}

	function getItemCaddyTypeByIdChild($idChild) {
		foreach($this->itemsCaddyType as $item) {
			if ($item->idChild == $idChild) {
				return $item;
			}
		}
		return null;
	}

	function getItemCaddyTypeByIdChildAndOption($idChild, $selectedOption) {
		foreach($this->itemsCaddyType as $item) {
			if ($item->idChild == $idChild && $item->selectedOption == $selectedOption) {
				return $item;
			}
		}
		return null;
	}

	function getCaddyTemp($userId) {

		$this->items = array();
		$caddyTemp = new CaddyTemp();
		$resultCaddy = $caddyTemp->findCaddyByUser($userId);

		if (isset($resultCaddy) && !empty($resultCaddy)) {
			foreach ($resultCaddy AS $row) {
				$item = new Item();

				$item->idChild = $row['CHILDID'];
				$item->qteChild = $row['CHILDQUANTITY'];
				$item->idProduit = $row['PRODUCTID'];
				$item->nom = $row['PRODUCTNOM'];
				$item->descshort = $row['DESCSHORT'];
				$item->navnom = $row['NAVNOM'];
				$item->url = $row['URL'];
				$item->designation = $row['DESIGNATION'];
				$item->isAccessoire = $row['isACCESSOIRE'];
				$item->selectedOption = $row['SELECTEDOPTION'];
				$item->stock = 1;

				array_push($this->items, $item);
			}
		}
	}

	function showCaddy() {
		foreach($this->items as $item) {
			print $item->toString()."<br/>";
		}
	}
}

?>