Location: PHPKode > projects > HikaShop > front/views/product/view.html.php
<?php
/**
 * @package		HikaShop for Joomla!
 * @version		1.4.3
 * @author		hikashop.com
 * @copyright	(C) 2010 HIKARI SOFTWARE. All rights reserved.
 * @license		GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');
?>
<?php
class ProductViewProduct extends JView{
	var $type = 'main';
	var $ctrl= 'product';
	var $nameListing = 'PRODUCTS';
	var $nameForm = 'PRODUCTS';
	var $icon = 'product';
	var $module=false;
	function display($tpl = null,$params=array()){
		$this->paramBase = HIKASHOP_COMPONENT.'.'.$this->getName();
		$function = $this->getLayout();
		$this->params =& $params;
		if(method_exists($this,$function)) $this->$function();
		parent::display($tpl);
	}
	function listing(){
		$module = hikashop::get('helper.module');
		$module->initialize($this);
		$this->paramBase.='_'.$this->params->get('main_div_name');
		$database	=& JFactory::getDBO();
		$app =& JFactory::getApplication();
		$pageInfo = null;
		$filters = array('b.product_published=1');
		$category_selected='';
		$select='';
		$is_synchronized=false;
		$table='b';
		if($this->params->get('product_order')=='ordering'){
			$table='a';
		}
		if(!empty($this->module)){
			$pageInfo->filter->order->value =$table.'.'.$this->params->get('product_order');
			$pageInfo->search = '';
			$pageInfo->filter->order->dir = $this->params->get('order_dir');
			$synchro = $this->params->get('content_synchronize');
			if($synchro){
				if(JRequest::getString('option','')==HIKASHOP_COMPONENT){
					if(JRequest::getString('ctrl','category')=='product'){
						$ok = false;
						$product_synchronize = (int)$this->params->get('product_synchronize',0);
						if($product_synchronize){
							$product_id = hikashop::getCID('product_id');
							if(!empty($product_id)){
								if($product_synchronize==2){
									$filters[]='a.product_related_type=\'related\'';
									$filters[]='a.product_id='.$product_id;
									$select='SELECT DISTINCT b.*';
									$b = hikashop::table('product_related').' AS a';
									$a = ' LEFT JOIN '.hikashop::table('product').' AS b';
									$on = ' ON a.product_related_id=b.product_id';
									if($this->params->get('product_order')=='ordering'){
										$pageInfo->filter->order->value = '';
									}
								}else{
									$query = "SELECT category_id FROM ".hikashop::table('product_category').' WHERE product_id='.$product_id.' ORDER BY ordering';
									$database->setQuery($query);
									$pageInfo->filter->cid = $database->loadResultArray();
								}
								$ok = true;
							}
						}
						if(!$ok){
							$pageInfo->filter->cid = $this->params->get('selectparentlisting');
						}
					}elseif(JRequest::getString('ctrl','category')=='category'){
						$pageInfo->filter->cid = JRequest::getInt("cid",$this->params->get('selectparentlisting'));
						$is_synchronized=true;
					}else{
						$pageInfo->filter->cid = $this->params->get('selectparentlisting');
					}
				}else{
					$pageInfo->filter->cid = $this->params->get('selectparentlisting');
				}
			}else{
				$pageInfo->filter->cid = $this->params->get('selectparentlisting');
			}
			if(!empty($pageInfo->filter->cid) && !is_array($pageInfo->filter->cid)){
				$category_selected = '_'.$pageInfo->filter->cid;
				$this->paramBase.=$category_selected;
			}
			$pageInfo->filter->price_display_type = $this->params->get('price_display_type');
			if(JRequest::getVar('hikashop_front_end_main',0)){
				$pageInfo->limit->value = $app->getUserStateFromRequest( $this->paramBase.'.list_limit', 'limit_'.$this->params->get('main_div_name').$category_selected, $this->params->get('limit'), 'int' );
				$pageInfo->limit->start = $app->getUserStateFromRequest( $this->paramBase.'.limitstart', 'limitstart_'.$this->params->get('main_div_name').$category_selected, 0, 'int' );
			}else{
				$pageInfo->limit->value = $this->params->get('limit');
				$pageInfo->limit->start = 0;
			}
		}else{
			$pageInfo->filter->cid = JRequest::getInt("cid",$this->params->get('selectparentlisting'));
			$category_selected = '_'.$pageInfo->filter->cid;
			$this->paramBase.=$category_selected;
			$pageInfo->filter->order->value = $table.'.'.$this->params->get('product_order');
			$pageInfo->filter->order->value = $app->getUserStateFromRequest( $this->paramBase.".filter_order", 'filter_order_'.$this->params->get('main_div_name').$category_selected,	$pageInfo->filter->order->value,'cmd' );
			$pageInfo->filter->order->dir	= $app->getUserStateFromRequest( $this->paramBase.".filter_order_Dir", 'filter_order_Dir_'.$this->params->get('main_div_name').$category_selected,	$this->params->get('order_dir'),	'word' );
			$pageInfo->limit->value = $app->getUserStateFromRequest( $this->paramBase.'.list_limit', 'limit_'.$this->params->get('main_div_name').$category_selected, $this->params->get('limit'), 'int' );
			$pageInfo->limit->start = $app->getUserStateFromRequest( $this->paramBase.'.limitstart', 'limitstart_'.$this->params->get('main_div_name').$category_selected, 0, 'int' );
			$pageInfo->filter->price_display_type = $app->getUserStateFromRequest( $this->paramBase.'.price_display_type', 'price_display_type_'.$this->params->get('main_div_name').$category_selected, $this->params->get('price_display_type'), 'word' );
		}
		$this->assignRef('category_selected',$category_selected);
		if(empty($pageInfo->limit->value)){
			$pageInfo->limit->value=5;
		}
		$currencyClass = hikashop::get('class.currency');
		$this->assignRef('currencyHelper',$currencyClass);
		$pageInfo->currency_id = hikashop::getCurrency();
		$config =& hikashop::config();
		if($config->get('tax_zone_type','shipping')=='billing'){
			$pageInfo->zone_id = hikashop::getZone('billing');
		}else{
			$pageInfo->zone_id = hikashop::getZone('shipping');
		}
		$show_price_weight = (int)$config->get('show_price_weight',0);
		$this->params->set('show_price_weight',$show_price_weight);
		if(!empty($pageInfo->filter->cid)){
			$acl_filters = array('category_type=\'product\'');
			hikashop::addACLFilters($acl_filters,'category_access');
			if(!empty($acl_filters)){
				if(!is_array($pageInfo->filter->cid)){
					$pageInfo->filter->cid = array($pageInfo->filter->cid);
				}
				$acl_filters[]='category_id IN ('.implode(',',$pageInfo->filter->cid).')';
				$query = 'SELECT category_id FROM '.hikashop::table('category').' WHERE '.implode(' AND ',$acl_filters);
				$database->setQuery($query);
				$pageInfo->filter->cid = $database->loadResultArray();
			}
		}
		if(empty($pageInfo->filter->cid)){
			$query = 'SELECT category_id FROM '.hikashop::table('category').' WHERE category_type=\'product\' AND category_parent_id=0 LIMIT 1';
			$database->setQuery($query);
			$pageInfo->filter->cid = $database->loadResult();
		}
		$searchMap = array('b.product_name','b.product_description','b.product_id','b.product_code');
		$filters[]='b.product_type = '.$database->Quote($this->type);
		if(!empty($pageInfo->search)){
			$searchVal = '\'%'.$database->getEscaped($pageInfo->search,true).'%\'';
			$filters[] = implode(" LIKE $searchVal OR ",$searchMap)." LIKE $searchVal";
		}
		$order = '';
		if(!empty($pageInfo->filter->order->value)){
			$order = ' ORDER BY '.$pageInfo->filter->order->value.' '.$pageInfo->filter->order->dir;
		}
		if(!is_array($pageInfo->filter->cid)){
			$pageInfo->filter->cid=array((int)$pageInfo->filter->cid);
		}
		if(empty($select)){
			$parentCategories = implode(',',$pageInfo->filter->cid);
			if(!$this->params->get('filter_type')){
				$filters[]='a.category_id IN ('.$parentCategories.')';
			}else{
				$categoryClass = hikashop::get('class.category');
				$childs = $categoryClass->getChilds($pageInfo->filter->cid,true,array(),'',0,0);
				$filter = 'a.category_id IN (';
				foreach($childs as $child){
					$filter .= $child->category_id.',';
				}
				$filters[]=$filter.$parentCategories.')';
			}
			$b = hikashop::table('product_category').' AS a';
			$a = ' LEFT JOIN '.hikashop::table('product').' AS b';
			$on = ' ON a.product_id=b.product_id';
			$select='SELECT DISTINCT b.*';
		}
		if($this->params->get('add_to_cart')){
			$cart =& hikashop::get('helper.cart');
			$this->assignRef('cart',$cart);
			$catalogue = (int)$config->get('catalogue',0);
			$this->params->set('catalogue',$catalogue);
			$cart->cartCount(1);
			$cart->cartCount(1);
			$cart->getJS($this->init());
		}
		hikashop::addACLFilters($filters,'product_access','b');
		if($this->params->get('random')){
			$ids_query = ' FROM '.$b.$a.$on.' WHERE '.implode(' AND ',$filters);
			$database->setQuery('SELECT DISTINCT b.product_id '.$ids_query);
			$possibilities = $database->loadResultArray();
			$filters=array('b.product_id = 0');
			$order = '';
			if($this->params->get('filter_type')){
				$select='SELECT DISTINCT b.*';
				$a = hikashop::table('product').' AS b';
				$b = '';
				$on = '';
			}
			if(count($possibilities)){
				$items = (int)$pageInfo->limit->value;
				$idsSelected = array();
				while($items>0 && !empty($possibilities)){
					$start = rand(0,count($possibilities)-1);
					$idsSelected[]=$possibilities[$start];
					unset($possibilities[$start]);
					$possibilities=array_values($possibilities);
					$items--;
				}
				if(!empty($idsSelected)){
					$filters=array('b.product_id IN ( '.implode(',',$idsSelected).')');
				}
			}
		}
		$query = ' FROM '.$b.$a.$on.' WHERE '.implode(' AND ',$filters).$order;
		$database->setQuery($select.$query,(int)$pageInfo->limit->start,(int)$pageInfo->limit->value);
		$rows = $database->loadObjectList();
		if(!empty($rows)){
			$ids = array();
			foreach($rows as $key => $row){
				$ids[]=$row->product_id;
				$rows[$key]->alias = JFilterOutput::stringURLSafe($row->product_name);
			}
			$queryImage = 'SELECT * FROM '.hikashop::table('file').' WHERE file_ref_id IN ('.implode(',',$ids).') AND file_type=\'product\' ORDER BY file_id ASC';
			$database->setQuery($queryImage);
			$images = $database->loadObjectList();
			foreach($images as $image){
				foreach($rows as $k=>$row){
					if($row->product_id==$image->file_ref_id){
						if(!isset($row->file_ref_id)){
							foreach(get_object_vars($image) as $key => $name){
								$rows[$k]->$key = $name;
							}
						}
						break;
					}
				}
			}
			$currencyClass->getListingPrices($rows,$pageInfo->zone_id,$pageInfo->currency_id,$pageInfo->filter->price_display_type);
		}		
		$database->setQuery('SELECT count( DISTINCT b.product_id)'.$query);
		$pageInfo->elements->total = $database->loadResult();
		$pageInfo->elements->page = count($rows);
		$toggleClass = hikashop::get('helper.toggle');
		$this->assignRef('toggleClass',$toggleClass);
		$this->assignRef('rows',$rows);
		$this->assignRef('pageInfo',$pageInfo);
		$this->assignRef('modules',$this->modules);
		$image=hikashop::get('helper.image');
		$this->assignRef('image',$image);
		global $Itemid;
		$menus	= &JSite::getMenu();
		$menu	= $menus->getActive();
		if(empty($menu)){
			if(!empty($Itemid)){
				$menus->setActive($Itemid);
				$menu	= $menus->getItem($Itemid);
			}
		}
		$url_itemid = '';
		if(!empty($Itemid)){
			$url_itemid = '&Itemid='.(int)$Itemid;
		}
		$category_pathway = '&category_pathway=0';
		$pagination = hikashop::get('helper.pagination', $pageInfo->elements->total, $pageInfo->limit->start, $pageInfo->limit->value);
		$pagination->hikaSuffix = '_'.$this->params->get('main_div_name').$category_selected;
		$this->assignRef('pagination',$pagination);
		if(empty($this->module)){
			$class = hikashop::get('class.category');
			$element = $class->get(reset($pageInfo->filter->cid),true);
			$this->assignRef('element',$element);
			$page_title = $this->params->get('page_title');
			$document	=& JFactory::getDocument();
			if(empty($page_title)){
				@$this->params->set('page_title',$element->category_name);
				$document->setTitle( $element->category_name );
			}else{
				$document->setTitle( $page_title );
			}
			if(!$this->params->get('random')){
				$this->params->set('show_limit',1);
			}
			if(empty($menu)){
				$pathway =& $app->getPathway();
				$category_pathway = '&category_pathway='.JRequest::getVar('menu_main_category');
				$categories = $class->getParents(reset($pageInfo->filter->cid));
				$one = true;
				foreach($categories as $category){
					if($one) $one = false;
					else $pathway->addItem($category->category_name,hikashop::completeLink('category&task=listing&cid='.(int)$category->category_id.'&name='.strtolower(preg_replace('#[^a-z0-9-_]#i','',$category->category_name))));
				}
			}
		}else{
			$main = JRequest::getVar('hikashop_front_end_main',0);
			if($main){
				if( !$this->params->get('random')){
					$this->params->set('show_limit',1);
				}
			}elseif(!$is_synchronized){
				$url_itemid='';
				$module_item_id = $this->params->get('itemid');
				$url_itemid='&Itemid='.(int)$module_item_id;
			}
			if(!empty($menu) && strpos($menu->link,'option='.HIKASHOP_COMPONENT)!==false && (strpos($menu->link,'view=category')!==false || strpos($menu->link,'view=')===false) && !JRequest::getInt('no_cid',0)){
				$category_pathway = '&category_pathway='.reset($pageInfo->filter->cid);
			}
		}
		$this->assignRef('itemid',$url_itemid);
		$this->assignRef('category_pathway',$category_pathway);
	}
	function show(){
		$app =& JFactory::getApplication();
		$product_id = (int)hikashop::getCID('product_id');
		global $Itemid;
		$menus	= &JSite::getMenu();
		$menu	= $menus->getActive();
		if(empty($menu)){
			if(!empty($Itemid)){
				$menus->setActive($Itemid);
				$menu	= $menus->getItem($Itemid);
			}
		}
		if(empty($product_id)){
			if (is_object( $menu )) {
				jimport('joomla.html.parameter');
				$category_params = new JParameter( $menu->params );
				$product_id = $category_params->get('product_id');
			}
		}
		if(empty($product_id)){
			return;
		}
		$filters=array('a.product_id='.$product_id);
		hikashop::addACLFilters($filters,'product_access','a');
		$query = 'SELECT a.*,b.product_category_id, b.category_id, b.ordering FROM '.hikashop::table('product').' AS a LEFT JOIN '.hikashop::table('product_category').' AS b ON a.product_id = b.product_id WHERE '.implode(' AND ',$filters). ' LIMIT 1';
		$database =& JFactory::getDBO();
		$database->setQuery($query);
		$element = $database->loadObject();
		if(empty($element)){
			return;
		}
		$config =& hikashop::config();
		$this->assignRef('config',$config);
		$this->modules = $config->get('product_show_modules',''); 
		$module = hikashop::get('helper.module');
		$this->modules=$module->setModuleData($this->modules);
		$currencyClass = hikashop::get('class.currency');
		$default_params = $config->get('default_params');
		$empty='';
		jimport('joomla.html.parameter');
		$params = new JParameter($empty);
		foreach($default_params as $k => $param){
			$params->set($k,$param);
		}
		$main_currency = (int)$config->get('main_currency',1);
		$params->set('main_currency',$main_currency);
		$discount_before_tax = (int)$config->get('discount_before_tax',0);
		$params->set('discount_before_tax',$discount_before_tax);
		$catalogue = (int)$config->get('catalogue',0);
		$params->set('catalogue',$catalogue);
		$show_price_weight = (int)$config->get('show_price_weight',0);
		$params->set('show_price_weight',$show_price_weight);
		$currency_id = hikashop::getCurrency();
		if($config->get('tax_zone_type','shipping')=='billing'){
			$zone_id = hikashop::getZone('billing');
		}else{
			$zone_id = hikashop::getZone('shipping');
		}
		$params->set('characteristic_display',$config->get('characteristic_display','table'));
		$params->set('characteristic_display_text',$config->get('characteristic_display_text',1));
		$params->set('show_quantity_field',$config->get('show_quantity_field',1));
		$this->assignRef('params',$params);
		$cart = hikashop::get('helper.cart');
		$this->assignRef('cart',$cart);
		$this->selected_variant_id=0;
		if(!empty($element)){
			if($element->product_type=='variant'){
				$this->selected_variant_id = $product_id;
				$filters=array('a.product_id='.$element->product_parent_id);
				hikashop::addACLFilters($filters,'product_access','a');
				$query = 'SELECT a.*,b.* FROM '.hikashop::table('product').' AS a LEFT JOIN '.hikashop::table('product_category').' AS b ON a.product_id = b.product_id WHERE '.implode(' AND ',$filters). ' LIMIT 1';
				$database =& JFactory::getDBO();
				$database->setQuery($query);
				$element = $database->loadObject();
				if(empty($element)){
					return;
				}
				$product_id = $element->product_id;
			}
			$element->alias = JFilterOutput::stringURLSafe($element->product_name);
			$filters=array('a.product_id ='.$product_id,'a.product_related_type=\'options\'','b.product_published=1');
			hikashop::addACLFilters($filters,'product_access','b');
			$query = 'SELECT b.* FROM '.hikashop::table('product_related').' AS a LEFT JOIN '.hikashop::table('product').' AS b ON a.product_related_id	= b.product_id WHERE '.implode(' AND ',$filters);
			$database->setQuery($query);
			$element->options = $database->loadObjectList('product_id');
			$ids = array($product_id);
			if(!empty($element->options)){
				foreach($element->options as $option){
					$ids[]=$option->product_id;
				}
			}
			$filters=array('product_parent_id IN ('.implode(',',$ids).')','product_published=1');
			hikashop::addACLFilters($filters,'product_access');
			$query = 'SELECT * FROM '.hikashop::table('product').' WHERE '.implode(' AND ',$filters);
			$database->setQuery($query);
			$variants = $database->loadObjectList();
			if(!empty($variants)){
				foreach($variants as $variant){
					$ids[]=$variant->product_id;
					if($variant->product_parent_id==$product_id){
						$element->variants[$variant->product_id]=$variant;
					}
					if(!empty($element->options)){
						foreach($element->options as $k => $option){
							if($variant->product_parent_id==$option->product_id){
								$element->options[$k]->variants[$variant->product_id] = $variant;
								break;
							}
						}
					}
				}
			}
			$config =& hikashop::config();
			if($config->get('characteristics_values_sorting')=='old'){
				$order = 'characteristic_id ASC';
			}else{
				$order = 'characteristic_value ASC';
			}
			$query = 'SELECT a.*,b.* FROM '.hikashop::table('variant').' AS a LEFT JOIN '.hikashop::table('characteristic').' AS b ON a.variant_characteristic_id=b.characteristic_id WHERE a.variant_product_id IN ('.implode(',',$ids).') ORDER BY a.ordering ASC,b.'.$order;
			$database->setQuery($query);
			$characteristics = $database->loadObjectList();
			if(!empty($characteristics)){
				$mainCharacteristics = array();
				foreach($characteristics as $characteristic){
					if($product_id==$characteristic->variant_product_id){
						$mainCharacteristics[$product_id][$characteristic->characteristic_parent_id][$characteristic->characteristic_id]=$characteristic;
					}
					if(!empty($element->options)){
						foreach($element->options as $k => $option){
							if($option->product_id==$characteristic->variant_product_id){
								$mainCharacteristics[$option->product_id][$characteristic->characteristic_parent_id][$characteristic->characteristic_id]=$characteristic;
							}
						}
					}
				}
				if(!empty($element->variants)){
					$this->addCharacteristics($element,$mainCharacteristics,$characteristics);
				}
				if(!empty($element->options)){
					foreach($element->options as $k => $option){
						if(!empty($option->variants)){
							$this->addCharacteristics($element->options[$k],$mainCharacteristics,$characteristics);
						}
					}
				}
			}
			$query = 'SELECT * FROM '.hikashop::table('file').' WHERE file_ref_id IN ('.implode(',',$ids).') AND file_type = \'product\' ORDER BY file_id ASC';
			$database->setQuery($query);
			$images = $database->loadObjectList();
			if(!empty($images)){
				$productClass = hikashop::get('class.product');
				$productClass->addImages($element,$images);
			}
			$currencyClass->getPrices($element,$ids,$currency_id,$main_currency,$zone_id,$discount_before_tax);
			$this->checkVariants($element);
			if(!empty($element->options)){
				foreach($element->options as $k => $option){
					$this->checkVariants($element->options[$k]);
				}
			}
			$this->setDefault($element);
			if(!empty($element->options)){
				foreach($element->options as $k => $option){
					$this->setDefault($element->options[$k]);
				}
			}
			$this->assignRef('element',$element);
			$fieldsClass = hikashop::get('class.field');
			$fields = $fieldsClass->getFields('frontcomp',$element,'product','checkout&task=state');
			$this->assignRef('fieldsClass',$fieldsClass);
			$this->assignRef('fields',$fields);
			$itemFields = $fieldsClass->getFields('frontcomp',$element,'item','checkout&task=state');
			$this->assignRef('itemFields',$itemFields);
			$doc =& JFactory::getDocument();
			$product_name = $this->element->product_name;
			$product_description = $element->product_meta_description;
			$product_keywords = $element->product_keywords;
			if(!empty($this->element->main)){
				$product_name = $this->element->main->product_name;
				$product_description = $this->element->main->product_meta_description;
				$product_keywords = $this->element->main->product_keywords;
			}
			if(!empty($product_keywords)){
				$doc->setMetadata('keywords', $product_keywords);
			}
			if(!empty($product_description)){
				$doc->setMetadata('description', $product_description);
			}
			$parent = 0;
			if(empty($menu) || !(strpos($menu->link,'option='.HIKASHOP_COMPONENT)!==false && strpos($menu->link,'view=product')!==false && strpos($menu->link,'layout=show')!==false)){
				$pathway =& $app->getPathway();
				$category_pathway = JRequest::getInt('category_pathway',0);
				$url_itemid='';
				if(!empty($Itemid)){
					$url_itemid='&Itemid='.$Itemid;
				}
				if($category_pathway){
					$class = hikashop::get('class.category');
					if(!empty($menu->id)){
						$menuClass = hikashop::get('class.menus');
						$menuData = $menuClass->get($menu->id);
						if(!empty($menuData->hikashop_params['selectparentlisting'])){
							$parent = $menuData->hikashop_params['selectparentlisting'];
						}
					}
					$categories = $class->getParents($category_pathway,$parent);
					$one = true;
					foreach($categories as $category){
						if($one){
							$one = false;
						}
						else{
							$pathway->addItem($category->category_name,hikashop::completeLink('category&task=listing&cid='.(int)$category->category_id.'&name='.strtolower(preg_replace('#[^a-z0-9-_]#i','',$category->category_name)).$url_itemid));
						}
					}
				}
				$pathway->addItem($product_name,hikashop::completeLink('product&task=show&cid='.(int)$element->product_id.'&name='.$element->alias.'&category_pathway='.$category_pathway.$url_itemid));
			}
			$image = hikashop::get('helper.image');
			$this->assignRef('image',$image);
			$this->assignRef('currencyHelper',$currencyClass);
			$characteristic = hikashop::get('type.characteristic');
			$this->assignRef('characteristic',$characteristic);
			$document	=& JFactory::getDocument();
			$document->setTitle( $product_name );
		}	
		$cart->getJS($this->init());
	}
	function addCharacteristics(&$element,&$mainCharacteristics,&$characteristics){
		$element->characteristics = $mainCharacteristics[$element->product_id][0];
		foreach($element->characteristics as $k => $characteristic){
			$element->characteristics[$k]->default=end($mainCharacteristics[$element->product_id][$k]);
		}
		if(!empty($element->variants)){
			foreach($characteristics as $characteristic){
				foreach($element->variants as $k => $variant){ 
					if($variant->product_id==$characteristic->variant_product_id){
						$element->variants[$k]->characteristics[$characteristic->characteristic_parent_id]=$characteristic;
						$element->characteristics[$characteristic->characteristic_parent_id]->values[$characteristic->characteristic_id]=$characteristic;
						if($this->selected_variant_id && $variant->product_id==$this->selected_variant_id){
							$element->characteristics[$characteristic->characteristic_parent_id]->default=$characteristic;
						}
					}
				}
			}
			if(isset($_REQUEST['hikashop_product_characteristic'])){
				if(is_array($_REQUEST['hikashop_product_characteristic'])){
					JArrayHelper::toInteger($_REQUEST['hikashop_product_characteristic']);
					$chars = $_REQUEST['hikashop_product_characteristic'];
				}else{
					$chars = JRequest::getCmd('hikashop_product_characteristic','');
					$chars = explode('_',$chars);
				}
				if(!empty($chars)){	
					foreach($element->variants as $k => $variant){
						$choosed = true;
						foreach($variant->characteristics as $characteristic){
							$ok = false;
							foreach($chars as $k => $char){
								if(!empty($char)){
									if($characteristic->characteristic_id==$char){
										$ok = true;
										break;
									}
								}
							}
							if(!$ok){
								$choosed=false;
							}else{
								$element->characteristics[$characteristic->characteristic_parent_id]->default=$characteristic;
							}
						}
						if($choosed){
							break;
						}
					}
				}
			}
			foreach($element->variants as $k => $variant){ 
				$temp=array();
				foreach($element->characteristics as $k2 => $characteristic2){
					if(!empty($variant->characteristics)){
						foreach($variant->characteristics as $k3 => $characteristic3){
							if($k2==$k3){
								$temp[$k3]=$characteristic3;
								break;
							}
						}
					}
				}
				$element->variants[$k]->characteristics=$temp;
			}
		}
	}
	
	function setDefault(&$element){
		if(!empty($element->characteristics)&&!empty($element->variants)){
			$match = false;
			foreach($element->variants as $k => $variant){
				$default = true;
				foreach($element->characteristics as $characteristic){
					$found=false;
					foreach($variant->characteristics as $k => $characteristic2){
						if($characteristic2->characteristic_id==$characteristic->default->characteristic_id){
							$found=true;
							break;
						}
					}
					if(!$found){
						$default = false;
						break;
					}
				}
				if($default){
					foreach(get_object_vars($variant) as $field=>$value){
						$element->main->$field=@$element->$field;
						$element->$field = $value;
					}
					$match = true;
					break;
				}
			}
			if(!$match){
				$variant = reset($element->variants);
				foreach(get_object_vars($variant) as $field=>$value){
					$element->main->$field=@$element->$field;
					$element->$field = $value;
				}
			}
		}
	}
	function checkVariants(&$element){
		if(!empty($element->characteristics)){
			$mapping = array();
			foreach($element->characteristics as $characteristic){
				$tempmapping = array();
				if(!empty($characteristic->values)){
					foreach($characteristic->values as $k => $value){
						if(empty($mapping)){
							$tempmapping[]=array($characteristic->characteristic_id=>$k);
						}else{
							foreach($mapping as $val){
								$val[$characteristic->characteristic_id]=$k;
								$tempmapping[]=$val;
							}
						}
					}
				}
				$mapping = $tempmapping;
			}
			if(empty($element->variants)){
				$element->variants = array();
			}
			$productClass = hikashop::get('class.product');
			foreach($mapping as $map){
				$found=false;
				foreach($element->variants as $k2 => $variant){
					$ok = true;
					foreach($map as $k => $id){
						if(empty($variant->characteristics[$k]->characteristic_id) || $variant->characteristics[$k]->characteristic_id != $id){
							$ok = false;
							break;
						}
					}
					if($ok){
						$found = true;
						$productClass->checkVariant($element->variants[$k2],$element,$map);
						break;
					}
				}
				if(!$found){
					$new = new stdClass;
					$productClass->checkVariant($new,$element,$map);
					$element->variants[$new->map]=$new;
				}
			}
		}
	}
	function _getCheckoutURL(){
		global $Itemid;
		$url_itemid='';
		if(!empty($Itemid)){
			$url_itemid='&Itemid='.$Itemid;
		}
		return hikashop::completeLink('checkout'.$url_itemid,false,true);
	}
	function init($cart=false){
		$config =& hikashop::config();
		$url = $config->get('redirect_url_after_add_cart','stay_if_cart');
		switch($url){
			case 'checkout':
				$url = $this->_getCheckoutURL();
				break;
			case 'stay_if_cart':
				$url='';
				if(!$cart){
					$url = $this->_getCheckoutURL();
					break;
				}
			case 'ask_user':
			case 'stay':
				$url='';
			case '':
			default:
				if(empty($url)){
					$url = hikashop::currentURL();
				}
				break;
		}
		return urlencode($url);
	}
	function cart(){
		$module = hikashop::get('helper.module');
		$module->initialize($this);
		$app =& JFactory::getApplication();
		$database	=& JFactory::getDBO();
		$config =& hikashop::config();
		$currencyClass = hikashop::get('class.currency');
		$class = hikashop::get('class.cart');
		$productClass = hikashop::get('class.product');
		$main_currency = (int)$config->get('main_currency',1);
		$currency_id = hikashop::getCurrency();
		if($config->get('tax_zone_type','shipping')=='billing'){
			$zone_id = hikashop::getZone('billing');
		}else{
			$zone_id = hikashop::getZone('shipping');
		}
		$discount_before_tax = (int)$config->get('discount_before_tax',0);
		$rows = $class->get();
		$total = null;
		if(!empty($rows)){
			$variants = false;
			foreach($rows as $k => $row){
				if($row->product_type=='variant'){
					$variants = true;
					foreach($rows as $k2 => $row2){
						if($row->product_parent_id==$row2->product_id){
							$rows[$k2]->variants[]=&$rows[$k];
						}
					}
				}
			}
			$ids = array_keys($rows);
			if($variants){
				$this->selected_variant_id = 0;
				$query = 'SELECT a.*,b.* FROM '.hikashop::table('variant').' AS a LEFT JOIN '.hikashop::table('characteristic').' AS b ON a.variant_characteristic_id=b.characteristic_id WHERE a.variant_product_id IN ('.implode(',',$ids).') ORDER BY a.ordering,b.characteristic_value';
				$database->setQuery($query);
				$characteristics = $database->loadObjectList();
				if(!empty($characteristics)){
					foreach($rows as $k => $row){
						$element =& $rows[$k];
						$product_id=$row->product_id;
						if($row->product_type=='variant'){
							continue;
						}
						$mainCharacteristics = array();
						foreach($characteristics as $characteristic){
							if($product_id==$characteristic->variant_product_id){
								$mainCharacteristics[$product_id][$characteristic->characteristic_parent_id][$characteristic->characteristic_id]=$characteristic;
							}
							if(!empty($element->options)){
								foreach($element->options as $k => $option){
									if($option->product_id==$characteristic->variant_product_id){
										$mainCharacteristics[$option->product_id][$characteristic->characteristic_parent_id][$characteristic->characteristic_id]=$characteristic;
									}
								}
							}
						}
						if(!empty($element->variants)){
							$this->addCharacteristics($element,$mainCharacteristics,$characteristics);
						}
						if(!empty($element->options)){
							foreach($element->options as $k => $option){
								if(!empty($option->variants)){
									$this->addCharacteristics($element->options[$k],$mainCharacteristics,$characteristics);
								}
							}
						}
					}
				}
			}
			$currencyClass->getPrices($rows,$ids,$currency_id,$main_currency,$zone_id,$discount_before_tax);
			foreach($rows as $k => $row){
				if(!empty($row->variants)){
					foreach($row->variants as $k2 => $variant){
						$productClass->checkVariant($rows[$k]->variants[$k2],$row);
					}
				}
			}
			foreach($rows as $k => $row){
				$currencyClass->quantityPrices($rows[$k]->prices,@$row->cart_product_quantity,$main_currency);
			}
			$total=null;
			$currencyClass->calculateTotal($rows,$total,$currency_id);
		}
		$this->assignRef('total',$total);
		$this->assignRef('rows',$rows);
		$cart=hikashop::get('helper.cart');
		$this->assignRef('cart',$cart);
		$this->assignRef('currencyHelper',$currencyClass);
		$cart->cartCount(true);
		$cart->getJS($this->init(true));
	}
}
Return current item: HikaShop