<?php

class product_collection{
	
	/**
	 * get product collection categories 
	 * @example for select    : getProductCollectionCategories(false, false, false, true, 0, false, false, false)
	 * 			for left menu : getProductCollectionCategories(false, true, true, true, 'session id', false, true, true)
	 * @param boolean $description column
	 * @param boolean $required where condition
	 * @param boolean $hasProduct join statemant
	 * @param boolean $ordering order by pozition 
	 * @param string|integer $user_id if user_id is diferent that 0 than join product_collection_configs
	 * @param boolean $getActive column
	 * @param boolean $active  where condition
	 * @param boolean $hasProductName if user_id is diferent that 0 and $hasProductName is true than query product_collection_categories.name
	 * @return array|NULL
	 */
	function getProductCollectionCategories($description=true, $required=true, $hasProduct=true, $ordering=true, $user_id=0, $getActive=false, $active=false, $hasProductName=false){
		global $dbi;
		global $ws;
		global $prop;
		
		
		//$prop=new property;

		if ($description) $paramlist = ", p_c_cat.description";
		if ($active) $condiction  = " where p_c_cat.inactive=0";
		/**
		 * helyette
		 * if ($required) $condiction = "where p_c_cat.required=0";
		 */
		if ($required){
			$paramlist .= ", p_c_cat.required";
			
			if ($user_id!==0){
				$not_required_categories = $this->getNotRequiredCategories($user_id);
			}
		}
		
		if ($getActive) $paramlist .= ", p_c_cat.inactive";
		if ($ordering) $order = " order by poz";
		if ($hasProduct and $user_id!==0)
		{
			$sql="select p_c_cat.product_collection_categories_id, p_c_cat.name, mode ".$paramlist." 
			from product_collection_categories p_c_cat
			left join product_collection_configs p_c_con on 
				p_c_con.product_collection_configs_id=p_c_cat.product_collection_categories_id and
				p_c_con.session_id='$user_id'
			".$condiction.$order;
		}else
		{ 
			$sql="select product_collection_categories_id, name, mode, main_pic ".$paramlist." 
			from product_collection_categories p_c_cat
			".$condiction.$order;
		} 

		$res=sql_query($sql,$dbi);

		while ($data = sql_fetch_array($res,$dbi))
		{
			
			if ($hasProduct)
				{
					if ($data['mode']){
						// a product_collection_direct_webshop táblát nézi meg és hogy van-e benne termék
						$sql="select webshop_category_id from product_collection_direct_webshop where product_collection_categories_id = ".$data['product_collection_categories_id'];
						$res1=sql_query($sql,$dbi);
						
						$ws->count_products=0;
						$ws->ids="";
						while ($data2 = sql_fetch_array($res1,$dbi)){
	
							$ws->collectProductIds($data2['webshop_category_id'], true, true);
						}
						$hasProductInCategory = $ws->count_products;
					}else{
						// a product_collection táblát nézi meg és hogy van-e benne termék
						$sql="select count(*) from product_collection 
							where product_collection_categories_id=".$data['product_collection_categories_id']."  and active=0";
						$res1=sql_query($sql,$dbi);
						list($hasProductInCategory)=sql_fetch_array($res1,$dbi);
					}
				}
			if (!$hasProduct or $hasProductInCategory>0)
				{
					$temp = array();
					$temp['main_pic'] = $data['main_pic']; 
					
					$sql="select webshop_product_id from product_collection_configs p_c_c
						where p_c_c.product_collection_categories_id=".$data['product_collection_categories_id']." and session_id='$user_id'";
					
					$res1=sql_query($sql,$dbi);
					while(list($webshop_product_id, $product_collection_id)=sql_fetch_array($res1,$dbi))
					{
						$product = $ws->getProductDetails($webshop_product_id);
						$prod_array = array(id=>$webshop_product_id, name=>$product["name"]);
						$temp['product'][] = $prod_array;
					}
					
					if (count($temp['product'])>0) 
					{
						$level=7;	
					} else 
					{
						if ($data['required'])
								$level=6;
							else 
								$level=5;
					}
					
					if ($i==0)
					{
						$temp['first'] = 1;
						$i++;
					}
					$temp['id'] = $data['product_collection_categories_id'];
					$temp['name'] = $data['name'];
					$temp['mode'] = $data['mode'];
					
					/**
					 * csak akkor lehet felülbirálni a required oszlopot, ha belépett userről van szó és
					 * a getNotRequiredCategories fv tömbbel tért vissza
					 */
					if ($required && $user_id!==0 && is_array($not_required_categories)){
						$not_required_cat=false;
						
						foreach ($not_required_categories as $cat)
							if ($cat==$data['product_collection_categories_id'])
								$not_required_cat=true;
								
						if ($not_required_cat==true){	
							$temp['required']=0;
							$level=6;
						}else
							$temp['required'] = $data['required'];
					} elseif ($required){
						$temp['required'] = $data['required'];
					}
					
					
					if ($description)
						$temp['description'] = $data['description'];
						
					$temp['level'] = $level;
					$temp['product_added'] = $data['product_added'];
					
					if ($getActive)
						$temp['active'] = $data['inactive'];	
					$return[] = $temp;
				}
		}
		
		if (is_array($return))
		{
			array_splice($return, count($return)-1);
			$temp['last']=1;
			$return[]=$temp;
		}
		return $return;		
	}
	
	
	/**
	 * get product collection categories using webshop products
	 * @example for select    : getProductCollectionCategories(false, false, false, true, 0, false, false, false)
	 * 			for left menu : getProductCollectionCategories(false, true, true, true, 'session id', false, true, true)
	 * @param boolean $description column
	 * @param boolean $required where condition
	 * @param boolean $hasProduct join statemant
	 * @param boolean $ordering order by pozition 
	 * @param string|integer $user_id if user_id is diferent that 0 than join product_collection_configs
	 * @param boolean $getActive column
	 * @param boolean $active  where condition
	 * @param boolean $hasProductName if user_id is diferent that 0 and $hasProductName is true than query product_collection_categories.name
	 * @return array|NULL
	 */
	function getProductCollectionCategoriesDirectWebshop_all($description=true, $required=true, $hasProduct=true, $ordering=true, $user_id=0, $getActive=false, $active=false, $hasProductName=false){
		global $dbi;
		global $ws;
		
		$prop=new property;

		if ($description) $paramlist = ", p_c_cat.description";
		if ($active) $condiction  = " where p_c_cat.inactive=0";
		/**
		 * helyette
		 * if ($required) $condiction = "where p_c_cat.required=0";
		 */
		if ($required){
			$paramlist .= ", p_c_cat.required";
			
			if ($user_id!==0){
				$not_required_categories = $this->getNotRequiredCategoriesDirectWebshop($user_id);
			}
		}
		
		if ($getActive) $paramlist .= ", p_c_cat.inactive";
		if ($ordering) $order = " order by poz";
		if ($hasProduct and $user_id!==0)
		{
			$sql="select p_c_cat.product_collection_categories_id, p_c_cat.name ".$paramlist." 
			from product_collection_categories p_c_cat
			left join product_collection_configs p_c_con on 
				p_c_con.product_collection_configs_id=p_c_cat.product_collection_categories_id and
				p_c_con.session_id='$user_id'
			".$condiction.$order;
		}else
		{ 
			$sql="select product_collection_categories_id, name".$paramlist." 
			from product_collection_categories p_c_cat
			".$condiction.$order;
		} 

		$res=sql_query($sql,$dbi);

		while ($data = sql_fetch_array($res,$dbi))
		{
			
			if ($hasProduct)
				{
					// a product_collection_direct_webshop táblát nézi meg és hogy van-e benne termék
					$sql="select webshop_category_id from product_collection_direct_webshop where product_collection_categories_id = ".$data['product_collection_categories_id'];
					$res1=sql_query($sql,$dbi);
					
					$ws->count_products=0;
					$ws->ids="";
					while ($data2 = sql_fetch_array($res1,$dbi)){

						$ws->collectProductIds($data2['webshop_category_id'], true, true);
					}
					$hasProductInCategory_1 = $ws->count_products;

					// a product_collection táblát nézi meg és hogy van-e benne termék
					$sql="select count(*) from product_collection 
						where product_collection_categories_id=".$data['product_collection_categories_id']."  and active=0";
					$res1=sql_query($sql,$dbi);
					list($hasProductInCategory)=sql_fetch_array($res1,$dbi);
					
					// azért adjuk össze, mert a kettőnek az összege kell nagyobb mint 0 legyen
					$hasProductInCategory+=$hasProductInCategory_1;
				}
			if (!$hasProduct or $hasProductInCategory>0)
				{
					$temp = array();

					$sql="select webshop_product_id from product_collection_configs p_c_c
						where p_c_c.product_collection_categories_id=".$data['product_collection_categories_id']." and session_id='$user_id'";
	
					$res1=sql_query($sql,$dbi);
					while(list($webshop_product_id)=sql_fetch_array($res1,$dbi))
					{
						$product = $ws->getProductDetails($webshop_product_id);
						$prod_array = array(id=>$webshop_product_id, name=>$product["name"]);
						$temp['product'][] = $prod_array;
					}
					
					if (count($temp['product'])>0) 
					{
						$level=7;	
					} else 
					{
						if ($data['required'])
								$level=6;
							else 
								$level=5;
					}
					
					if ($i==0)
					{
						$temp['first'] = 1;
						$i++;
					}
					$temp['id'] = $data['product_collection_categories_id'];
					$temp['name'] = $data['name'];
					
					/**
					 * csak akkor lehet felülbirálni a required oszlopot, ha belépett userről van szó és
					 * a getNotRequiredCategories fv tömbbel tért vissza
					 */
					if ($required && $user_id!==0 && is_array($not_required_categories)){
						$not_required_cat=false;
						
						foreach ($not_required_categories as $cat)
							if ($cat==$data['product_collection_categories_id'])
								$not_required_cat=true;
								
						if ($not_required_cat==true){
							$temp['modified_required']=1;	
							$temp['required']=0;
							$level=6;
						}else
							$temp['required'] = $data['required'];
					} elseif ($required){
						$temp['required'] = $data['required'];
					}
					
					
					if ($description)
						$temp['description'] = $data['description'];
						
					$temp['level'] = $level;
					$temp['product_added'] = $data['product_added'];
					
					if ($getActive)
						$temp['active'] = $data['inactive'];	
					$return[] = $temp;
				}
		}
		
		if (is_array($return))
		{
			array_splice($return, count($return)-1);
			$temp['last']=1;
			$return[]=$temp;
		}
		
		return $return;		
	}	
	
	/**
	 * a konfigurációhoz adott termékek tulajdonságok alapján megállapitja, hogy mindegyik kategória szükséges-e
	 * az alapkonfigurációhoz
	 * 
	 * Müködése: - legyüjti a konfigurációhoz adott termékek azonositóját
	 *           - legyüjti a termék tulajdonságait
	 *           - megnézi van-e eggyezés a product_collection_requirements táblában
	 *
	 * @return array|NULL
	 */
	function getNotRequiredCategories($user_id){
		global $dbi;
		
		$sql = "select pc_req.product_collection_categories_id from product_collection_requirements pc_req
			left join property_value pv on pv.property_id=pc_req.property_id
			left join product_collection pc on pc.webshop_product_id=pv.target_id
			left join product_collection_configs pc_conf on pc_conf.webshop_product_id=pc.product_collection_id where pc_conf.session_id='$user_id'";

		$res=sql_query($sql,$dbi);
		while(list($id)=sql_fetch_array($res,$dbi)){
			$ret[] = $id;
		}
		return $ret;
	}
	
	
	/**
	 * a konfigurációhoz adott termékek tulajdonságok alapján megállapitja, hogy mindegyik kategória szükséges-e
	 * az alapkonfigurációhoz
	 * 
	 * Müködése: - legyüjti a konfigurációhoz adott termékek azonositóját
	 *           - legyüjti a termék tulajdonságait
	 *           - megnézi van-e eggyezés a product_collection_requirements táblában
	 *
	 * @return array|NULL
	 */
	function getNotRequiredCategoriesDirectWebshop($user_id){
		global $dbi;
		
		$sql = "select pc_req.product_collection_categories_id from product_collection_requirements pc_req
			left join property_value pv on pv.property_id=pc_req.property_id
			left join product_collection_configs pc_conf on pc_conf.webshop_product_id=pv.target_id where pc_conf.session_id='$user_id'";
		$res=sql_query($sql,$dbi);
		while(list($id)=sql_fetch_array($res,$dbi)){
			$ret[] = $id;
		}
		return $ret;
	}	
	
	/**
	 * getting the selected category
	 *
	 * @param integer|string $category_id category id or first string for the first active and not empty category
	 * @return array|NULL
	 */
	function getActualCategory($category_id){
		global $dbi;

		if ($category_id=="first") 
				$sql = "select p_c_c.product_collection_categories_id, name, description,
					(
						select count(*) as products_nr1
						from product_collection p_c 
						where p_c_c.product_collection_categories_id=p_c.product_collection_categories_id and
						p_c.active=0
					) as products_nr	
				from product_collection_categories p_c_c
				where inactive=0 
				having products_nr>0
				order by poz limit 1";
			else
				$sql = "select p_c_c.product_collection_categories_id, name, description,
					(
						select count(*) as products_nr1
						from product_collection p_c 
						where p_c_c.product_collection_categories_id=p_c.product_collection_categories_id and
						p_c.active=0
					) as products_nr	
				from product_collection_categories p_c_c
				where inactive=0 and p_c_c.product_collection_categories_id=$category_id
				having products_nr>0";

		$res=sql_query($sql,$dbi);
		if(list($id, $name, $description)=sql_fetch_array($res,$dbi)){
			$ret = array(id=>$id,name=>$name, description=>$description);
		}		
		return $ret;
	}
	

function getSeoName($n){

	global $SITEGLOBALS;
	return str_replace($SITEGLOBALS[REPLACE_CHARS][mit],$SITEGLOBALS[REPLACE_CHARS][mire],$n);

}	
	
	
	/**
	 * getting the selected category
	 *
	 * @param integer|string $category_id category id or first string for the first active and not empty category
	 * @return array|NULL
	 */
	function getActualCategoryDirectWebshop($category_id){
		global $dbi;
		global $ws;
		
		if ($category_id=="first") 
				$sql = "select p_c_c.product_collection_categories_id, name, description, mode
					from product_collection_categories p_c_c
					where inactive=0 
					order by poz limit 1";
			else
				$sql = "select p_c_c.product_collection_categories_id, name, description, mode
					from product_collection_categories p_c_c
					where inactive=0 and p_c_c.product_collection_categories_id=$category_id";

		$res=sql_query($sql,$dbi);
		if(list($id, $name, $description, $mode)=sql_fetch_array($res,$dbi)){
			/*$ws->count_products=0;
			$ws->ids="";
			$ws->collectProductIds($id, true, true);
			if ($ws->count_products>0)*/
				$ret = array(id=>$id,name=>$name, description=>$description, mode=>$mode);
		}		
		return $ret;
	}	
	
	/**
	 * save new product collection categorie
	 *
	 * @param array $post data
	 */
	function saveProductCollectionCategories(){
		global $dbi;
	
		$sql="insert into product_collection_categories
		(product_collection_categories_id, name, description, poz, required) 
		values(
			'', 
			'Új kategória', 
			'', 
			(SELECT if( count( * ) !=0, max( poz ) +1, 1 ) AS max_order from product_collection_categories p_c_c),
			0)";
		sql_query($sql,$dbi);
		
		$sql="select product_collection_categories_id from product_collection_categories order by poz desc limit 1";
		$res=sql_query($sql,$dbi);
		list($lsci)=sql_fetch_array($res,$dbi);
		$this->lastsavedcategoryid = $lsci; 	
	}	
	
	/**
	 * get last saved category id
	 *
	 * @return integer|NULL
	 */
	function getLastSavedCategoryId()
	{
		return $this->lastsavedcategoryid;
	}
	
	/**
	 * rename product collection categories
	 *
	 * @param integer $category_id category id
	 * @param string $new_category_name new category name
	 */
	function renameProductCollectionCategories($category_id, $new_category_name){
		global $dbi;
	
		$sql="update product_collection_categories
			set name='".$new_category_name."'
			where product_collection_categories_id=".$category_id;
		sql_query($sql,$dbi);
	}	

	function renamePreconfig($webshop_id, $new_pc_name){
		global $dbi;
	
		$sql="update webshop_product
			set name='".$new_pc_name."'
			where id=".$webshop_id;
		sql_query($sql,$dbi);
	}		
	
	
	/**
	 * delete product collection category and products from the category
	 * delete all products from category
	 *
	 * @param integer $category_id product_collection_categories_id
	 */
	function deleteProductCollectionCategories($category_id){
		global $dbi;
	
		/**
		 * deleting category
		 */
		$sql="delete from product_collection_categories where product_collection_categories_id=".$category_id;
		sql_query($sql,$dbi);

		/**
		 * deleting all products from category
		 */
		$sql="delete from product_collection where product_collection_categories_id=".$category_id;
		sql_query($sql,$dbi);
	}	
	
	
	/**
	 * delete product collection category and products from the category
	 * delete all products from category
	 *
	 * @param integer $category_id product_collection_categories_id
	 */
	function deleteProductCollectionCategoriesDirectWebshop($category_id){
		global $dbi;
	
		/**
		 * deleting category
		 */
		$sql="delete from product_collection_categories where product_collection_categories_id=".$category_id;
		sql_query($sql,$dbi);

		/**
		 * deleting all products from category
		 */
		$sql="delete from product_collection_direct_webshop where product_collection_categories_id=".$category_id;
		sql_query($sql,$dbi);
	}	
	
	/**
	 * list all products from one categorty
	 * 
	 * @param integer $category_id categorty id
	 * @param boolean $active list all active products
	 * @param boolean $ordering put in order
	 * @param boolean $webshopdata get the webshop data: webshop product name, webshop category id, category chain, main image, property
	 * @param boolean $getActive get active column
	 * @param boolean $getSelected get selected column
	 * @param boolean $onlySelected where condition for selected products
	 * @param boolean $user_id session id
	 * @param boolean $group_ids for property list (azok a halmazokat kell átadni neki amelyek inkompatibilitást eredményeznek)
	 */ 
	function getProducts($category_id, $active=true, $ordering=true, $webshopdata=false, $getActive=false, $getSelected=false, $onlySelected=false, $user_id=0, $group_ids=0, $compatible_group_ids=0, $split=0)
	{
		global $dbi; 
		global $ws;
		global $prop;
		
		if(!$ws) $ws=new webshop();
		
		$split=1;
		if ($split>0)
		{
			$ws->split=10;
			if(!($o=$_GET['o'])) $o=0;
			$o_item=$_GET['o']*$ws->split;
			$limit=" limit $o_item,$ws->split ";
		}
		
		if ($active) $sql_active = " and active=0";
		if ($getActive) $cols = ", pc.active";
		if ($getSelected and $user_id!==0)
			{ 
				$cols .= ", product_collection_configs_id";
				$join = " left join product_collection_configs pc_conf ON 
								pc.webshop_product_id = pc_conf.webshop_product_id and 
								pc_conf.session_id='".$user_id."'";
			}
		if ($onlySelected)
			{
				$join = " inner join product_collection_configs pc_conf ON pc.webshop_product_id = pc_conf.webshop_product_id";
			}
			
	    /**
		 * hogyha első kategóriából szeretnénk lekérdezni a termékeket
		 */
		if ($category_id=="first") $category_id = "(
			select p_c_c.product_collection_categories_id
			from product_collection_categories p_c_c
			where inactive=0 order by poz limit 1
			)";
		if ($category_id=="order") $category_id = "pc.product_collection_categories_id and session_id='".$user_id."'";
		
		
		if ($ordering)
			{ 
				$order = " order by poz";
				switch ($_GET['name'])
				{
					case 'up':$order = " order by name";
						break;
					case 'dn':$order = " order by name desc";
						break;
				}
				
				switch ($_GET['price'])
				{
					case 'up':$order = " order by price";
						break;
					case 'dn':$order = " order by price desc";
						break;
				}				
				
			}
		
		/**
		 * hogyha a webshop beli névre vagyunk kiváncsiak
		 */
		if ($webshopdata) 
			{
				
				$cols .= ",p.name, w_c_conn.category_id as webshop_category_id";
				$join .= " left join webshop_product p on p.id=pc.webshop_product_id";
				$join .= " left join webshop_product_conn w_c_conn on w_c_conn.product_id=pc.webshop_product_id";
				$join .= " left join webshop_product_price ws_p on ws_p.product_id=pc.webshop_product_id";
				if (is_numeric($ws->price_type))
					$cond .= " and type_id=".$ws->price_type;
			}
		if ($order)	
		$sql="select 
				distinct product_collection_id, 
				pc.webshop_product_id
				".$cols." 
			from 
				product_collection pc
			$join 
			where 
				pc.product_collection_categories_id=".$category_id.$cond.
				$sql_active.
			$order;

		$res=sql_query($sql,$dbi);
		$items=sql_num_rows($res,$dbi);
		$sql.=$limit;
		$res=sql_query($sql,$dbi);
		if ($ws->split>0)
			{
				$pages=ceil($items/$ws->split);
	
				if(!($o=$_GET['o'])) $o=0;
				$o_item=$_GET['o']*$ws->split;
				$ws->makePages($pages,$items,$o);
				
				$this->pages = $ws->pages;
			}
		$items=sql_num_rows($res,$dbi);	
		while($data=sql_fetch_array($res,$dbi))
		{
			$temp = array();
			if ($webshopdata)
			{
				
				if ($i==0)
				{
					$temp['first'] = 1;
					$i++;
				}
				//$ws = new webshop;
				$cat_chain=$ws->getCategoryChain($data['webshop_category_id'], true);
				
				$temp['cat_chain'] = $cat_chain;
				
				$main_image=$ws->getProductMainImage($data['webshop_product_id']);
				$temp['main_image'] = $main_image;
				
				$price = $ws->getProductPrice($data['webshop_product_id']);
				$temp['price'] = $price;
				$prop=new property;
				$prop->list="";
				$prop->getTree('flat',0,'p',$data['webshop_product_id']);
				//var_dump($prop->list);
				$activeprops = array();
				if (is_array($prop->list))
					foreach ($prop->list as $allProps)
					{
						if ($allProps['value'])
							$activeprops[] = $allProps['id'];
					}
				$prop_id = array();
				if (is_array($activeprops))
					{
						foreach ($activeprops as $prop){
							$prop_id[] = $prop;
						}
						$prop_id = array_unique($prop_id); 
					} else
					{
						$prop_id = $activeprops;
					}
				if (is_array($prop_id))
					{ 
						$prop_id_impl = implode(",", $prop_id);
					} else 
					{
						$prop_id_impl = $prop_id;
					}

				$incompatible=0;		
				$compatible=0;
				if ($prop_id_impl!=0)
				{
					$sql="select distinct product_collection_compatible_id from product_collection_compatible where property_id in (".$prop_id_impl.")";
					$res2=sql_query($sql,$dbi);
					//echo $sql."<br>";
					while(list($group_id)=sql_fetch_array($res2,$dbi)){
						$gr += $group_id." ";//echo "<br>group_ids:<br>";
						if (is_array($group_ids))
						{
							if (in_array($group_id, $group_ids)) $incompatible=1;
						} else
						{
							if ($group_id==$group_ids) $incompatible=1;
						}
					}

					$sql="select distinct product_collection_compatible_id from product_collection_compatible where property_id in (".$prop_id_impl.")";
					$res2=sql_query($sql,$dbi);
					while(list($group_id)=sql_fetch_array($res2,$dbi)){
						$gr += $group_id." ";//echo "<br>group_ids:<br>";
						if (is_array($compatible_group_ids))
						{
							if (in_array($group_id, $compatible_group_ids)) $compatible=1;
						} else
						{
							if ($group_id==$compatible_group_ids) $compatible=1;
						}
					}
				}
			}
			
			$temp['incompatible'] = $incompatible;	
			$temp['compatible'] = $compatible;	
			
			if ($getSelected)
			{
				$temp['in_config'] = $data['product_collection_configs_id'];	
			}
			
			
			$temp['p_c_id'] = $data['product_collection_id'];
			$temp['w_p_id'] = $data['webshop_product_id'];
			
			if ($getActive)
				$temp['active'] = $data['active'];
				
			$temp['name'] = $data['name'];
			
			$return[] = $temp;
			 
		}

		if ($webshopdata and is_array($return))
			{
				array_splice($return, count($return)-1);
				$temp['last']=1;
				$return[]=$temp;
			}
			
		$pages=ceil($items/$ws->split);
		$ws->makePages($pages,$items,$o);
		return $return;		
	}
	
	
	/**
	 * list all products from one categorty
	 * 
	 * @param integer $category_id categorty id
	 * @param boolean $active list all active products
	 * @param boolean $ordering put in order
	 * @param boolean $webshopdata get the webshop data: webshop product name, webshop category id, category chain, main image, property
	 * @param boolean $getActive get active column
	 * @param boolean $getSelected get selected column
	 * @param boolean $onlySelected where condition for selected products
	 * @param boolean $user_id session id
	 * @param boolean $group_ids for property list (azok a halmazokat kell átadni neki amelyek inkompatibilitást eredményeznek)
	 */ 
	function getProductsOrder($category_id, $active=true, $ordering=true, $webshopdata=false, $getActive=false, $getSelected=false, $onlySelected=false, $user_id=0, $group_ids=0, $compatible_group_ids=0, $split=0)
	{
		global $dbi; 
		global $ws;
		global $prop;
		
		if(!$ws) $ws=new webshop();
		
		//if ($active) $sql_active = " and active=0";
		//if ($getActive) $cols = ", pc.active";
		if ($getSelected and $user_id!=0)
			{ 
				$cols .= ", product_collection_configs_id";
				/*$join = " left join product_collection_configs pc_conf ON 
								pc.webshop_product_id = pc_conf.webshop_product_id and 
								pc_conf.session_id='".$user_id."'";*/
			}
		/*if ($onlySelected)
			{
				$join = " inner join product_collection_configs pc_conf ON pc.webshop_product_id = pc_conf.webshop_product_id";
			}*/
			
	    /**
		 * hogyha első kategóriából szeretnénk lekérdezni a termékeket
		 */
		if ($category_id=="first") $category_id = "(
			select p_c_c.product_collection_categories_id
			from product_collection_categories p_c_c
			where inactive=0 order by poz limit 1
			)";
		if ($category_id=="order") $category_id = " session_id='".$user_id."'";
		
		
		if ($ordering)
			{ 
				$order = " order by name";
				switch ($_GET['name'])
				{
					case 'up':$order = " order by name";
						break;
					case 'dn':$order = " order by name desc";
						break;
				}
				
				switch ($_GET['price'])
				{
					case 'up':$order = " order by price";
						break;
					case 'dn':$order = " order by price desc";
						break;
				}				
				
			}
		
		/**
		 * hogyha a webshop beli névre vagyunk kiváncsiak
		 */
		if ($webshopdata) 
			{
				
				$cols .= ",p.name, w_c_conn.category_id as webshop_category_id";
				$join .= " left join webshop_product p on p.id=pc_conf.webshop_product_id";
				$join .= " left join webshop_product_conn w_c_conn on w_c_conn.product_id=pc_conf.webshop_product_id";
				$join .= " left join webshop_product_price ws_p on ws_p.product_id=pc_conf.webshop_product_id";
				if (is_numeric($ws->price_type))
					$cond .= " and type_id=".$ws->price_type;
			}	
		$sql="select  
				distinct pc_conf.webshop_product_id
				".$cols." 
			from 
				product_collection_configs pc_conf
			$join 
			where ".$category_id.$cond.
				$sql_active.$order;

		$res=sql_query($sql,$dbi);
		$items=sql_num_rows($res,$dbi);
		$sql.=$limit;
		$res=sql_query($sql,$dbi);
		if ($ws->split>0)
			{
				$pages=ceil($items/$ws->split);
	
				if(!($o=$_GET['o'])) $o=0;
				$o_item=$_GET['o']*$ws->split;
				$ws->makePages($pages,$items,$o);
				
				$this->pages = $ws->pages;
			}
		$items=sql_num_rows($res,$dbi);	
		while($data=sql_fetch_array($res,$dbi))
		{
			$temp = array();
			if ($webshopdata)
			{
				
				if ($i==0)
				{
					$temp['first'] = 1;
					$i++;
				}
				//$ws = new webshop;
				$cat_chain=$ws->getCategoryChain($data['webshop_category_id'], true);
				
				$temp['cat_chain'] = $cat_chain;
				
				$main_image=$ws->getProductMainImage($data['webshop_product_id']);
				$temp['main_image'] = $main_image;
				
				$price = $ws->getProductPrice($data['webshop_product_id']);
				$temp['price'] = $price;
				$prop=new property;
				$prop->list="";
				$prop->getTree('flat',0,'p',$data['webshop_product_id']);
				//var_dump($prop->list);
				$activeprops = array();
				if (is_array($prop->list))
					foreach ($prop->list as $allProps)
					{
						if ($allProps['value'])
							$activeprops[] = $allProps['id'];
					}
				$prop_id = array();
				if (is_array($activeprops))
					{
						foreach ($activeprops as $prop){
							$prop_id[] = $prop;
						}
						$prop_id = array_unique($prop_id); 
					} else
					{
						$prop_id = $activeprops;
					}
				if (is_array($prop_id))
					{ 
						$prop_id_impl = implode(",", $prop_id);
					} else 
					{
						$prop_id_impl = $prop_id;
					}

				$incompatible=0;		
				$compatible=0;
				if ($prop_id_impl!=0)
				{
					$sql="select distinct product_collection_compatible_id from product_collection_compatible where property_id in (".$prop_id_impl.")";
					$res2=sql_query($sql,$dbi);
					//echo $sql."<br>";
					while(list($group_id)=sql_fetch_array($res2,$dbi)){
						$gr += $group_id." ";//echo "<br>group_ids:<br>";
						if (is_array($group_ids))
						{
							if (in_array($group_id, $group_ids)) $incompatible=1;
						} else
						{
							if ($group_id==$group_ids) $incompatible=1;
						}
					}

					$sql="select distinct product_collection_compatible_id from product_collection_compatible where property_id in (".$prop_id_impl.")";
					$res2=sql_query($sql,$dbi);
					while(list($group_id)=sql_fetch_array($res2,$dbi)){
						$gr += $group_id." ";//echo "<br>group_ids:<br>";
						if (is_array($compatible_group_ids))
						{
							if (in_array($group_id, $compatible_group_ids)) $compatible=1;
						} else
						{
							if ($group_id==$compatible_group_ids) $compatible=1;
						}
					}
				}
			}
			
			$temp['incompatible'] = $incompatible;	
			$temp['compatible'] = $compatible;	
			
			if ($getSelected)
			{
				$temp['in_config'] = $data['product_collection_configs_id'];	
			}
			
			
			$temp['p_c_id'] = $data['product_collection_id'];
			$temp['w_p_id'] = $data['webshop_product_id'];
			
			if ($getActive)
				$temp['active'] = $data['active'];
				
			$temp['name'] = $data['name'];
			
			$return[] = $temp;
			 
		}

		if ($webshopdata and is_array($return))
			{
				array_splice($return, count($return)-1);
				$temp['last']=1;
				$return[]=$temp;
			}
			
		$pages=ceil($items/$ws->split);
		$ws->makePages($pages,$items,$o);
		return $return;		
	}	
	
	
	/**
	 * list all products from one categorty
	 * 
	 * @param integer $category_id categorty id
	 * @param boolean $active list all active products
	 * @param boolean $ordering put in order
	 * @param boolean $webshopdata get the webshop data: webshop product name, webshop category id, category chain, main image, property
	 * @param boolean $getActive get active column
	 * @param boolean $getSelected get selected column
	 * @param boolean $onlySelected where condition for selected products
	 * @param boolean $user_id session id
	 * @param boolean $group_ids for property list (azok a halmazokat kell átadni neki amelyek inkompatibilitást eredményeznek)
	 */
	function getProductsDirectWebshop_all($category_id, $active=true, $ordering=true, $webshopdata=false, $getActive=false, $getSelected=false, $onlySelected=false, $user_id=0, $group_ids=0, $compatible_group_ids=0, $split=0)
	{
		global $dbi; 
		global $ws;
		global $prop;
		
		if(!$ws) $ws=new webshop();
		
		$split=1;
		if ($split>0)
		{
			$ws->split=10;
			if(!($o=$_GET['o'])) $o=0;
			$o_item=$o*$ws->split;
			$limit=" limit $o_item,$ws->split ";
		}
		

		if ($active) $sql_active = " and active=0";
		
		 /**
		 * hogyha első kategóriából szeretnénk lekérdezni a termékeket
		 */
		if ($category_id=="first") $category_id = "(
			select p_c_c.product_collection_categories_id
			from product_collection_categories p_c_c
			where inactive=0 order by poz limit 1
			)";
		if ($category_id=="order") $category_id = "pc.product_collection_categories_id";
		
		
		// lekérdezem a webshop_categoryt
		$sql="select webshop_category_id from product_collection_direct_webshop pc where 
				pc.product_collection_categories_id=".$category_id;
		$res=sql_query($sql,$dbi);

		$ws->count_products=0;
		$ws->ids="";		
		//bejárja az összes webshop kategóriát, amely a product collection kategóriában van
		while($data=sql_fetch_array($res,$dbi))
		{
			//$cat_chain=$ws->getCategoryChain($data['webshop_category_id']);
			$ws->collectProductIds($data['webshop_category_id'], true, true);
		}
		/*$sql="select webshop_product_id from product_collection pc where 
				pc.product_collection_categories_id=".$category_id.$sql_active;
		
		$res1=sql_query($sql,$dbi);
		while($data1=sql_fetch_array($res1,$dbi)){
			$ws->ids[]=$data1['webshop_product_id'];
		}
		$ws->ids = array_unique($ws->ids);*/

			//var_dump($ws->ids);die;
			//bejárja az összes terméket
			if (is_array($ws->ids))
			foreach ($ws->ids as $product){
				//echo $product." ";
				$cols="";
				if ($getSelected and $user_id!==0)
				{ 
					$cols .= ", product_collection_configs_id";
					$join = " left join product_collection_configs pc_conf ON 
									wp.id = pc_conf.webshop_product_id and 
									pc_conf.session_id='".$user_id."'";
				}
				
				if ($onlySelected)
				{
					$join = " inner join product_collection_configs pc_conf ON wp.id = pc_conf.webshop_product_id";
				}
				
				$sql="select distinct wp.id as webshop_product_id, name$cols from webshop_product wp 
						left join webshop_product_price wp_price on wp_price.product_id=wp.id 
						$join
						where price>0 and wp.id=".$product;
				
				$res=sql_query($sql,$dbi);
				while($data2=sql_fetch_array($res,$dbi))
				{
					$temp = array();
					$temp['name'] = $data2['name'];
					$temp['seoname'] = $this->getSeoName($data2['name']);
					$temp['w_p_id']	= $data2['webshop_product_id'];
					$temp['p_c_id']	= $data2['webshop_product_id'];
					
					if ($getSelected)
					{
						$temp['in_config'] = $data2['product_collection_configs_id'];	
					}
					
					$temp['cat_chain'] = $cat_chain;
					
					$main_image=$ws->getProductMainImage($data2['webshop_product_id']);
					$temp['main_image'] = $main_image;
					
					$price = $ws->getProductPrice($data2['webshop_product_id']);
					$temp['price'] = $price;
					$prop=new property;
					$prop->list="";
					$prop->getTree('flat',0,'p',$data2['webshop_product_id']);
					//var_dump($prop->list);
					$activeprops = array();
					if (is_array($prop->list))
						foreach ($prop->list as $allProps)
						{
							if ($allProps['value'])
								$activeprops[] = $allProps['id'];
						}
					$prop_id = array();
					if (is_array($activeprops))
						{
							foreach ($activeprops as $prop){
								$prop_id[] = $prop;
							}
							$prop_id = array_unique($prop_id); 
						} else
						{
							$prop_id = $activeprops;
						}				
					
					if (is_array($prop_id))
						{ 
							$prop_id_impl = implode(",", $prop_id);
						} else 
						{
							$prop_id_impl = $prop_id;
						}
	
					$incompatible=0;		
					$compatible=0;
					if ($prop_id_impl!=0)
					{
						$sql="select distinct product_collection_compatible_id from product_collection_compatible where property_id in (".$prop_id_impl.")";
						$res2=sql_query($sql,$dbi);
						//echo $sql."<br>";
						while(list($group_id)=sql_fetch_array($res2,$dbi)){
							$gr += $group_id." ";//echo "<br>group_ids:<br>";
							if (is_array($group_ids))
							{
								if (in_array($group_id, $group_ids)) $incompatible=1;
							} else
							{
								if ($group_id==$group_ids) $incompatible=1;
							}
						}
	
						$sql="select distinct product_collection_compatible_id from product_collection_compatible where property_id in (".$prop_id_impl.")";
						$res2=sql_query($sql,$dbi);
						while(list($group_id)=sql_fetch_array($res2,$dbi)){
							$gr += $group_id." ";//echo "<br>group_ids:<br>";
							if (is_array($compatible_group_ids))
							{
								if (in_array($group_id, $compatible_group_ids)) $compatible=1;
							} else
							{
								if ($group_id==$compatible_group_ids) $compatible=1;
							}
						}
					}
					
					$temp['incompatible'] = $incompatible;	
					$temp['compatible'] = $compatible;	
			
					$return[] = $temp;	
				}
				
						
			}
		
		if ($webshopdata and is_array($return))
			{
				array_splice($return, count($return)-1);
				$temp['last']=1;
				$return[]=$temp;
			}
			
		$pages=ceil($items/$ws->split);
		$ws->makePages($pages,$items,$o);
		//echo "<pre>";print_r($return);
		return $return;		
	}	

	
	function getProductsJson($category_id, $active=true, $ordering=true, $webshopdata=false, $getActive=false, $getSelected=false, $onlySelected=false, $user_id=0, $group_ids=0, $compatible_group_ids=0, $split=0)
	{
		global $dbi; 
		global $ws;
		global $prop;
		global $lang;
		
		$split=1;
		if ($split>0)
		{
			$ws->split=10;
			if(!($o=$_GET['o'])) $o=0;
			$o_item=$_GET['o']*$ws->split;
			$limit=" limit $o_item,$ws->split ";
		}
		
		if ($active) $sql_active = " and active=0";
		if ($getActive) $cols = ", pc.active";
		if ($getSelected and $user_id!=0)
			{ 
				$cols .= ", product_collection_configs_id";
				$join = " left join product_collection_configs pc_conf ON 
								pc.product_collection_id = pc_conf.product_id and 
								pc_conf.session_id='".$user_id."'";
			}
		if ($onlySelected)
			{
				$join = " inner join product_collection_configs pc_conf ON pc.product_collection_id = pc_conf.product_id";
			}
			
	    /**
		 * hogyha első kategóriából szeretnénk lekérdezni a termékeket
		 */
		if ($category_id=="first") $category_id = "(
			select p_c_c.product_collection_categories_id
			from product_collection_categories p_c_c
			where inactive=0 order by poz limit 1
			)";
		if ($category_id=="order") $category_id = "pc.product_collection_categories_id and session_id='".$user_id."'";
		
		
		if ($ordering)
			{ 
				$order = " order by poz";
				switch ($_GET['name'])
				{
					case 'up':$order = " order by name";
						break;
					case 'dn':$order = " order by name desc";
						break;
				}
				
				switch ($_GET['price'])
				{
					case 'up':$order = " order by price";
						break;
					case 'dn':$order = " order by price desc";
						break;
				}				
				
			}
		
		/**
		 * hogyha a webshop beli névre vagyunk kiváncsiak
		 */
		if ($webshopdata) 
			{
				
				$cols .= ",p.name, w_c_conn.category_id as webshop_category_id";
				$join .= " left join webshop_product p on p.id=pc.webshop_product_id";
				$join .= " left join webshop_product_conn w_c_conn on w_c_conn.product_id=pc.webshop_product_id";
				$join .= " left join webshop_product_price ws_p on ws_p.product_id=pc.webshop_product_id";
				if (is_numeric($ws->price_type))
					$cond .= " and type_id=".$ws->price_type;
			}
			
		$sql="select 
				distinct product_collection_id, 
				webshop_product_id
				".$cols." 
			from 
				product_collection pc
			$join 
			where 
				pc.product_collection_categories_id=".$category_id.$cond.
				$sql_active.
			$order;

		$res=sql_query($sql,$dbi);
		$items=sql_num_rows($res,$dbi);
		$sql.=$limit;
		$res=sql_query($sql,$dbi);
		if ($ws->split>0)
			{
				$pages=ceil($items/$ws->split);
	
				if(!($o=$_GET['o'])) $o=0;
				$o_item=$_GET['o']*$ws->split;
				$ws->makePages($pages,$items,$o);
				
				$this->pages = $ws->pages;
			}
		$items=sql_num_rows($res,$dbi);	
		while($data=sql_fetch_array($res,$dbi))
		{
			$temp = array();
			if ($webshopdata)
			{
				if ($data['product_collection_configs_id']>0)
					$temp['in_config'] = "true"; else $temp['in_config'] = "false";
				$temp['id'] = $data['product_collection_id'];
				$temp['name'] = "<b>".$data['name']."</b><br>";

				$ws = new webshop;
				$cat_chain=$ws->getCategoryChain($data['webshop_category_id']);

				$cat_count = count($cat_chain);
				$i=0;
				foreach ($cat_chain as $c)
				{
					if ($cat_count-1!=$i) "<b>".$temp['name'].=$lang[webshop_category][$c]."&#8594</b>";
						else $temp['name'].=$lang[webshop_category][$c];
					$i++;
				}
				$temp['name'].="</b>";

				
				$main_image=$ws->getProductMainImage($data['webshop_product_id']);
				$temp['main_image'] = "<img src='Image/products/".$main_image['thumb']."'>";
				
				$price = $ws->getProductPrice($data['webshop_product_id']);
							
				if ($auth->type==7)
						$temp['price'] = $price['netto']['text']; 
					else
						$temp['price'] = $price['brutto']['text']; 
						
				$temp['adatlap'] = "<a target='_blank' href='index.php?page=webshop_product_details&id=".$data['webshop_product_id']."'<img src='Image/site/ikons_normal/edit.gif'></a>";
				$prop=new property;
				$prop->list="";
				$prop->getTree('flat',0,'p',$data['webshop_product_id']);
				//var_dump($prop->list);
				$activeprops = array();
				if (is_array($prop->list))
					foreach ($prop->list as $allProps)
					{
						if ($allProps['value'])
							$activeprops[] = $allProps['id'];
					}
				$prop_id = array();
				if (is_array($activeprops))
					{
						foreach ($activeprops as $prop){
							$prop_id[] = $prop;
						}
						$prop_id = array_unique($prop_id); 
					} else
					{
						$prop_id = $activeprops;
					}				
				
				if (is_array($prop_id))
					{ 
						$prop_id_impl = implode(",", $prop_id);
					} else 
					{
						$prop_id_impl = $prop_id;
					}

				$incompatible=0;		
				$compatible=0;
				if ($prop_id_impl!=0)
				{
					$sql="select distinct product_collection_compatible_id from product_collection_compatible where property_id in (".$prop_id_impl.")";
					$res2=sql_query($sql,$dbi);
					//echo $sql."<br>";
					while(list($group_id)=sql_fetch_array($res2,$dbi)){
						$gr += $group_id." ";//echo "<br>group_ids:<br>";
						if (is_array($group_ids))
						{
							if (in_array($group_id, $group_ids)) $incompatible=1;
						} else
						{
							if ($group_id==$group_ids) $incompatible=1;
						}
					}

					$sql="select distinct product_collection_compatible_id from product_collection_compatible where property_id in (".$prop_id_impl.")";
					$res2=sql_query($sql,$dbi);
					while(list($group_id)=sql_fetch_array($res2,$dbi)){
						$gr += $group_id." ";//echo "<br>group_ids:<br>";
						if (is_array($compatible_group_ids))
						{
							if (in_array($group_id, $compatible_group_ids)) $compatible=1;
						} else
						{
							if ($group_id==$compatible_group_ids) $compatible=1;
						}
					}
				}
			}
			if ($compatible) $temp['name']="<font color='green'>".$temp['name']."</font>";
			if ($incompatible) $temp['name']="<font color='red'>".$temp['name']."</font>";

			/*if ($getSelected)
			{
				$temp['in_config'] = $data['product_collection_configs_id'];	
			}*/
			
			
			$temp['p_c_id'] = $data['product_collection_id'];
			$temp['w_p_id'] = $data['webshop_product_id'];
			
			if ($getActive)
				$temp['active'] = $data['active'];
				
			
			$return[] = $temp;
			 
		}

		/*if ($webshopdata and is_array($return))
			{
				array_splice($return, count($return)-1);
				$temp['last']=1;
				$return[]=$temp;
			}
			*/
		$pages=ceil($items/$ws->split);
		$ws->makePages($pages,$items,$o);
		/*echo "<pre>";
		var_dump($return);*/
		return $return;		
	}	
	
	/**
	 * add product to category
	 *
	 * @param integer $category_id category id
	 * @param integer $product_id product id
	 */	
	function addProduct($category_id, $product_id)
	{
		global $dbi;
	
		$sql="insert into product_collection
		(webshop_product_id, product_collection_categories_id, poz) 
		values(
			".$product_id.", 
			".$category_id.", (SELECT if( count( * ) !=0, max( poz ) +1, 1 ) AS max_order from product_collection p_c where product_collection_categories_id=$category_id))";
		sql_query($sql,$dbi);		
	}
	
	/**
	 * add webshop category to product collection category
	 *
	 * @param integer $category_id category id
	 * @param integer $product_id product id
	 */	
	function addCategory($category_id, $pc_category_id)
	{
		global $dbi;
	
		$sql="insert into product_collection_direct_webshop
		(webshop_category_id , product_collection_categories_id) 
		values(
			".$category_id.", 
			".$pc_category_id.")";
		sql_query($sql,$dbi);		
	}	
	
	/**
	 * delete product from product_collection
	 *
	 * @param integer $product_id product id
	 */
	function deleteProduct($product_id)
	{
		global $dbi;
	
		$sql="delete from product_collection where 
					product_collection_id=".$product_id;
		sql_query($sql,$dbi);		
	}
	
	
	/**
	 * delete webshop category from product_collection
	 *
	 * @param integer $webshop_id product id
	 */
	function deleteWebshopCategoryFromProductCollection($product_collection_direct_webshop, $product_collection_categories_id)
	{
		global $dbi;
	
		$sql="delete from product_collection_direct_webshop where 
					webshop_category_id=".$product_collection_direct_webshop." and product_collection_categories_id=".$product_collection_categories_id;
		sql_query($sql,$dbi);		
	}	
	
	/**
	  * switch product order
	  * 
	  * @param integer $category_id category id
	  * @param integer $id1 image id
	  * @param string $direction direction of switching
	  */
	function switchProduct($category_id, $id1, $direction) {
		global $dbi;
		if($direction=='up') {
			$rel='<'; $ord='desc'; 
		}else{
			$rel='>'; $ord='asc'; 
		}
		
		$sql="select poz from product_collection where product_collection_categories_id=$category_id and product_collection_id=$id1";
		$res=sql_query($sql,$dbi);
		list($poz1)=sql_fetch_array($res,$dbi);
	
		$sql="select product_collection_id,poz from product_collection where product_collection_categories_id=$category_id and poz$rel$poz1 order by poz $ord limit 1";
		$res=sql_query($sql,$dbi);
		list($id2,$poz2)=sql_fetch_array($res,$dbi);
	
		$sql="update product_collection set poz=$poz2 where product_collection_categories_id=$category_id and product_collection_id=$id1";
		sql_query($sql,$dbi);
		$sql="update product_collection set poz=$poz1 where product_collection_categories_id=$category_id and product_collection_id=$id2";
		sql_query($sql,$dbi);
	}	
	
	/**
	  * switch category order
	  * 
	  * @param integer $category_id category id
	  * @param string $direction direction of switching
	  */
	function switchCategory($category_id, $direction) {
		global $dbi;
		if($direction=='up') {
			$rel='<'; $ord='desc'; 
		}else{
			$rel='>'; $ord='asc'; 
		}
		
		$sql="select poz from product_collection_categories where product_collection_categories_id=$category_id";
		$res=sql_query($sql,$dbi);
		list($poz1)=sql_fetch_array($res,$dbi);
	
		$sql="select product_collection_categories_id,poz from product_collection_categories where poz$rel$poz1 order by poz $ord limit 1";
		$res=sql_query($sql,$dbi);
		list($id2,$poz2)=sql_fetch_array($res,$dbi);
	
		$sql="update product_collection_categories set poz=$poz2 where product_collection_categories_id=$category_id";
		sql_query($sql,$dbi);
		$sql="update product_collection_categories set poz=$poz1 where product_collection_categories_id=$id2";
		sql_query($sql,$dbi);
	}	

	/**
	 * recursive function for querying next category in the collection
	 * don't query inactive or empty category
	 * use the order parameter from database
	 *
	 * @param  $category_id category id
	 */
	function getNextCategory($category_id){
		global $dbi;
		global $ws;
	
		$sql="select product_collection_categories_id from product_collection_categories
			where  poz > 
				(select poz from product_collection_categories where 
					product_collection_categories_id=$category_id 
				) and inactive=0 order by poz";
		$res=sql_query($sql,$dbi);
		while(list($id)=sql_fetch_array($res,$dbi)){
			$sql = "select product_collection_id from product_collection where product_collection_categories_id=".$id;
			$res1=sql_query($sql,$dbi);
			while(list($id1)=sql_fetch_array($res1,$dbi)){
				
				if ($id1!=NULL)$i++;
				//if ($id1!=NULL) return $id;
			}

			$sql="select webshop_category_id from product_collection_direct_webshop where product_collection_categories_id = ".$id;
			//echo $sql;
			$res1=sql_query($sql,$dbi);
						
			$ws->count_products=0;
			$ws->ids="";
			while ($data2 = sql_fetch_array($res1,$dbi)){
	
				$ws->collectProductIds($data2['webshop_category_id'], true, true);
			}
			//echo $ws->count_products;die;
			$i+=$ws->count_products;
			if ($i>0) return $id;
			
		}	
	}	

	/**
	 * set product active
	 *
	 * @param integer $product_id product id
	 */
	function setProductActive($product_id){
		global $dbi;
	
		$sql="update product_collection set active=not active where product_collection_id=".$product_id;
		sql_query($sql,$dbi);
				
	}	

	/**
	 * set category desctiption
	 *
	 * @param integer $category_id category id
	 * @param string $description desctiption
	 */
	function setCategoryDescription($category_id, $description){
		global $dbi;
	
		$sql="update product_collection_categories set description='".$description."' where product_collection_categories_id=".$category_id;
		sql_query($sql,$dbi);
	}	
	
	/**
	 * set active category
	 *
	 * @param integer $category_id category id
	 */
	function setActiveCategory($category_id){
		global $dbi;
		
		$sql="update product_collection_categories set inactive=not inactive where product_collection_categories_id=".$category_id;
		sql_query($sql,$dbi);
	}	
	
	/**
	 * set mode category
	 *
	 * @param integer $category_id category id
	 */
	function setModeCategory($category_id){
		global $dbi;
		
		$sql="update product_collection_categories set mode=not mode where product_collection_categories_id=".$category_id;
		sql_query($sql,$dbi);
	}		
	
	/**
	 * set required category
	 *
	 * @param integer $category_id category id
	 */
	function setRequiredCategory($category_id){
		global $dbi;
		
		$sql="update product_collection_categories set required=not required where product_collection_categories_id=".$category_id;
		sql_query($sql,$dbi);
	}		

	/**
	 * get config price
	 *
	 * @param string $session_id session id
	 * @return integer
	 */
	function getConfigPrice($session_id){
		global $dbi;
		global $ws;
		
		if (!$ws) $ws = new webshop;
		
		$sql="select webshop_product_id from product_collection_configs pc_conf
		right join product_collection pc on pc.product_collection_id=pc_conf.product_id
		where session_id='".$session_id."'";
		$res=sql_query($sql,$dbi);

		while(list($product_id)=sql_fetch_array($res,$dbi)){
			$price = $ws->getProductPrice($product_id);
			$brutto += $price['brutto']['value'];
			$netto += $price['netto']['value'];
		}
		$pr = array(brutto=>number_format($brutto,0,'.',' '), netto=>number_format($netto,0,'.',' '));
		return $pr;		
	}	
	
	/**
	 * get config price
	 *
	 * @param string $session_id session id
	 * @return integer
	 */
	function getConfigPriceDirectWebshop_all($session_id){
		global $dbi;
		global $ws;
		
		if (!$ws) $ws = new webshop;
		
		$sql="select pc_conf.webshop_product_id from product_collection_configs pc_conf
		where session_id='".$session_id."'";
		$res=sql_query($sql,$dbi);

		while(list($product_id)=sql_fetch_array($res,$dbi)){
			$price = $ws->getProductPrice($product_id);
			$brutto += $price['brutto']['value'];
			$netto += $price['netto']['value'];
		}
		$pr = array(brutto=>number_format($brutto,0,'.',' '), netto=>number_format($netto,0,'.',' '));
		return $pr;		
	}	
	
	/**
	 * get config price
	 *
	 * @param integer $config_id config id
	 * @return integer
	 */	
	function getPreconfigPrice($config_id, $toWebshop=false, $allprices=false){
		global $dbi;
		global $ws;
		
		if(!$ws) {
			global $auth;
			$ws=new webshop($auth->user_data[country_code],$auth->user_data[type]);
		}
		
		$format=$toWebshop?'text':'value';
		
		$sql="select webshop_product_id from product_collection_preconfigs where config_id=".$config_id;
		$res=sql_query($sql,$dbi);

		while(list($product_id)=sql_fetch_array($res,$dbi)){
			$price = $allprices ? $ws->getProductAllPrices($product_id) : $ws->getProductPrice($product_id);
			
			if($allprices) {
				foreach($price as $t=>$p) { // ártípusok
					$sum_net[$t] += $p['netto']['value']; 
					$sum_bru[$t] += $p['brutto']['value']; 
				}
			}else{
				$sum_net += $price['netto']['value'];
				$sum_bru += $price['brutto']['value'];
			}
		}
		if($allprices) {
			if (is_array($price))
				foreach($price as $t=>$p) { // ártípusok
					$sum[$t][netto]  = $toWebshop ? array(value=>$sum_net[$t], text=>$ws->valueToPrice($sum_net[$t])) : $sum_net[$t]; 
					$sum[$t][brutto] = $toWebshop ? array(value=>$sum_bru[$t], text=>$ws->valueToPrice($sum_bru[$t])) : $sum_bru[$t]; 
				}
		}else{
			if($toWebshop) {
				$sum_net = array(value=>$sum_net, text=>$ws->valueToPrice($sum_net));
				$sum_bru = array(value=>$sum_bru, text=>$ws->valueToPrice($sum_bru));
			}
			$sum = array(netto=>$sum_net, brutto=>$sum_bru);
		}
		return $sum; 		
	}		
	
	/**
	 * get config property groups
	 * @param string $session_id session id 
	 * - a félkész konfigurációban lévő termékek webshop_id-jának a lekérdezése
	 * - a webshop_id alapján az eggyes termékhez tartozó property_id lekérdezése
	 * - a megduplázódott tulajdonságok kiszürése 
	 * - a tulajdonságok apjának a lekérdezése
	 * - az apák gyerekeinek a lekérdezése
	 * - azok a gyerekek maradnak a listában, akik nem voltam lekérdezett tulajdonságok listájában
	 * - azokat a halmazokat kérdezze le, amelyek tartalmazzák ezeket a tulajdonságokat
	 * - a megduplázott halmazok kiszürése 
	 * @return array
	 */
	function getConfigPropertyGroups($session_id){
		global $dbi;
	
		$sql="select pc_conf.webshop_product_id as wc from product_collection_configs pc_conf
		where session_id='".$session_id."'";
		$res=sql_query($sql,$dbi);
		while(list($product_id)=sql_fetch_array($res,$dbi)){
			$prop=new property;
			$prop->getTree('flat',0,'p',$product_id);
			/*echo "<pre>";
			var_dump($prop->list);*/
			if (is_array($prop->list))
				foreach ($prop->list as $allProps)
				{
					if ($allProps['value'])
						$activeprops[] = $allProps['id'];
				}
			
			if (is_array($activeprops))
				{
					foreach ($activeprops as $prop){
						$prop_id[] = $prop;
					}
					$prop_id = array_unique($prop_id); 
				} else
				{
					$prop_id = $activeprops;
				}
		}
		
		if (is_array($prop_id))
		{
			$prop_id = implode(',', $prop_id);
			$this->prop_id = $prop_id;	
			//echo "ezek a hozzáadott termékek tulajondásgai".$prop_id."<br>";
			//most a tulajdonságok apjának a lekérdezése
			$sql="select distinct main from property_conn where sub in (".$prop_id.")";
			$res=sql_query($sql,$dbi);
			while(list($main_id)=sql_fetch_array($res,$dbi)){
				$mains[] = $main_id; 
			}	
		}
		

		if (is_array($mains))
		{
			$mains = implode(',', $mains);
		
			//echo "ezek a tulajondásgok apai ".$mains."<br>";	
			//az apák gyerekeinek a lekérdezése
			//azok a gyerekek maradnak a listában, akik nem voltam lekérdezett tulajdonságok listájában
			$sql="select distinct sub from property_conn where main in (".$mains.") and sub not in (".$prop_id.")";
			$res=sql_query($sql,$dbi);
			while(list($sub_id)=sql_fetch_array($res,$dbi)){
				$subs[] = $sub_id; 
			}
		}	

		if (is_array($subs))
		{
			$subs = implode(',', $subs);
		
			//echo "ezek az apák gyerekei ".$subs."<br>";				
			//azokat a halmazokat kérdezze le, amelyek tartalmazzák ezeket a tulajdonságokat
			// a megduplázott halmazok kiszürése 
			$sql="select distinct product_collection_compatible_id from product_collection_compatible where property_id in (".$subs.")";
			$res=sql_query($sql,$dbi);
			while(list($groups)=sql_fetch_array($res,$dbi)){
				$return[] = $groups; 
			}		
		}
		
		//var_dump($return);
		return $return;		
	}	

	/**
	 * get groups
	 *
	 * @param string $user_id session id
	 * @return array
	 */
	function getGroups($user_id){
		if (is_string($this->prop_id))
		{
			global $dbi;
			global $prop;
	
			$sql="select product_collection_compatible_id from product_collection_compatible pc_comp
			left join property prop on prop.id=pc_comp.property_id
			where property_id in (".$this->prop_id.")";
			
			$res=sql_query($sql,$dbi);
	
			while(list($group_id)=sql_fetch_array($res,$dbi)){
				$return[] = $group_id;
			}
			return $return;
		}
	}	
	
	/**
	 * get config compatible groups for admin menu
	 *
	 * @return array
	 */
	function getConfigCompatibleGroups(){
		global $dbi;
		global $prop;
	
		$sql="select product_collection_compatible_id, property_id, name from product_collection_compatible pc_comp
		left join property prop on prop.id=pc_comp.property_id
		order by product_collection_compatible_id";
		$res=sql_query($sql,$dbi);

		while(list($group_id, $property_id, $property_name)=sql_fetch_array($res,$dbi)){
			$prop->getParentNamesChain($property_id, true);
			$prop->parents_name = substr($prop->parents_name, 0, -2);
			$return[] = array(id=>$group_id,property_id=>$property_id, property_name=>$property_name, parents_name=>$prop->parents_name);
		}
		return $return;		
	}	
	
	
	/**
	 * get propertys with category for admin menu
	 *
	 * @return array
	 */
	function getPropertyRequirementsToCategory(){
		global $dbi;
		global $prop;
	
		$sql="select pc_req.product_collection_categories_id, pc_cat.name as catname, pc_req.property_id, prop.name from product_collection_requirements pc_req
		left join property prop on prop.id=pc_req.property_id
		left join product_collection_categories pc_cat on pc_cat.product_collection_categories_id = pc_req.product_collection_categories_id
		order by pc_cat.name";
		$res=sql_query($sql,$dbi);

		while(list($category_id, $category_name, $property_id, $property_name)=sql_fetch_array($res,$dbi)){
			$prop->getParentNamesChain($property_id, true);
			$prop->parents_name = substr($prop->parents_name, 0, -2);
			$return[] = array(id=>$category_id, name=>$category_name, property_id=>$property_id, property_name=>$property_name, parents_name=>$prop->parents_name);
		}
		return $return;		
	}

	function getPropertySettings(){
		global $dbi;
		
		$sql="select name from product_collection_property_settings
			left join property on id=property_id";
		$res=sql_query($sql,$dbi);

		while(list($name)=sql_fetch_array($res,$dbi)){
			$return[]=$name;
		}
		return $return;	
	}

	
	//---------------------Preconfig--------------------------------------------------------------
	/**
	 * save new preconfig category
	 *
	 */
	function savePreconfigCategories($post_name=''){
		global $dbi;
		global $ws;

		$id = $ws->getLastProductId();
		$id++;
		$ret=true; 
		while($ret){
			if(!($name=$post_name)) $name = "Új gép".$i;
			$post_name='';
			$data = array(id=>$id, name=>$name, collection=>1);
			$ret = $ws->saveNewProduct($data);
			$i++;
		}
		return $id;
	}
	

	/**
	 * save new preconfig category from product collection
	 *
	 */
	function savePreconfigCategoriesFromProductCollection($user_id){
		global $dbi;
		global $ws;
	
		$id = $ws->getLastProductId();
		$id++; 

		$ret = true;
		while ($ret!=NULL){
			$name = "Új gép".$i;
			$data = array(id=>$id, name=>$name, collection=>1);
			$ret = $ws->saveNewProduct($data);
			$i++;
		}

		$this->addConfigToPreconfig($id, $user_id);
	}	
	
	
	/**
	 * get preconfig categories 
	 * @param boolean $price price
	 * @return array
	 */
	function getPreconfigCategories($price=false){
		global $dbi;
		global $ws;
		
		$sql="select id,name,inactive from webshop_product where collection=1";
		$res=sql_query($sql,$dbi);

		while (list($id, $name, $inactive) = sql_fetch_array($res,$dbi))
		{
			if ($price)
			{
				$sql="select webshop_product_id from 
				product_collection_preconfigs where config_id=".$id;

				$res1=sql_query($sql,$dbi);
				list($webshop_product_id) = sql_fetch_array($res1,$dbi);
				if ($webshop_product_id)
				{
					$price = $ws->getProductPrice($webshop_product_id);
					$return[] = array(id=>$id, name=>$name, price=>$price, inactive=>$inactive);
				} else 
				{
					$return[] = array(id=>$id, name=>$name, inactive=>$inactive);
				}
			}else
			{
				$return[] = array(id=>$id, name=>$name, inactive=>$inactive);
			}
		}
		
		return $return;		
	}

	/**
	 * get preconfig products
	 *
	 * @param integer $category_id
	 * @param boolean $active
	 * @param boolean $ordering
	 * @param boolean $webshopdata
	 * @param boolean $getActive
	 * @param boolean $getSelected
	 * @param boolean $onlySelected
	 * @return array
	 */
	function getPreconfigProducts($category_id, $active=true, $ordering=true, $webshopdata=false, $getActive=false, $getSelected=false, $onlySelected=false)
	{
		global $dbi; 
		global $ws;
		
		if (!$ws) $ws = new webshop;
		
		//if ($active) $sql_active = " and active=0";
		//if ($getActive) $cols = ", pc.active";
		if ($getSelected)
			{ 
				$cols .= ", product_collection_configs_id";
				$join = " left join product_collection_configs pc_conf ON pc_pre.webshop_product_id  = pc_conf.webshop_product_id ";
			}
		if ($onlySelected)
			{ 
				$join = " inner join product_collection_configs pc_conf ON pc_pre.webshop_product_id  = pc_conf.webshop_product_id ";
			}
		
		/**
		 * hogyha első kategóriából szeretnénk lekérdezni a termékeket
		 */
		if ($category_id=="first") $category_id = "(
			select p_c_c.product_collection_categories_id
			from product_collection_categories p_c_c
			where inactive=0 order by poz limit 1
			)";
		if ($category_id=="order") $category_id = "pc.product_collection_categories_id";
		
		
		/*if ($ordering)
			{ 
				$order = " order by poz";
				switch ($_GET['name'])
				{
					case 'up':$order = " order by name";
						break;
					case 'dn':$order = " order by name desc";
						break;
				}
				
				switch ($_GET['price'])
				{
					case 'up':$order = " order by price";
						break;
					case 'dn':$order = " order by price desc";
						break;
				}				
				
			}*/
		
		/**
		 * hogyha a webshop beli névre vagyunk kiváncsiak
		 */
		if ($webshopdata) 
			{
				
				$cols .= ",p.name, w_c_conn.category_id as webshop_category_id";
				$join .= " left join webshop_product p on p.id=pc_pre.webshop_product_id";
				$join .= " left join webshop_product_conn w_c_conn on w_c_conn.product_id=pc_pre.webshop_product_id";
				$join .= " left join webshop_product_price ws_p on ws_p.product_id=pc_pre.webshop_product_id";
				if (is_numeric($ws->price_type))
					$cond = " and type_id=".$ws->price_type;
			}

		$sql="select 
				distinct pc_pre.webshop_product_id,
				pc_cat.name as cat_name,
				pc_pre.product_collection_categories_id
				".$cols." 
			from 
				product_collection_preconfigs pc_pre
			left join product_collection_categories pc_cat on pc_cat.product_collection_categories_id=pc_pre.product_collection_categories_id
			$join 
			where 
				pc_pre.config_id=".$category_id.$cond.
				$sql_active.
			$order;

		$res=sql_query($sql,$dbi);
		$items=sql_num_rows($res,$dbi);
		$pages=ceil($items/$ws->split);

		if(!($o=$_GET['o'])) $o=0;
		$o_item=$_GET['o']*$ws->split;
		$ws->makePages($pages,$items,$o);
		$this->pages = $ws->pages;
		while($data=sql_fetch_array($res,$dbi))
		{
			$temp = array();
			if ($webshopdata)
			{
				
				if ($i==0)
				{
					$temp['first'] = 1;
					$i++;
				}

				$cat_chain=$ws->getCategoryChain($data['webshop_category_id'], true);
				$temp['cat_chain'] = $cat_chain;
				
				$main_image=$ws->getProductMainImage($data['webshop_product_id']);
				$temp['main_image'] = $main_image;
				
				$price = $ws->getProductPrice($data['webshop_product_id']);
				$temp['price'] = $price;
			}
			
			if ($getSelected)
			{
				$temp['in_config'] = $data['product_collection_configs_id'];	
			}
			
			$temp['p_cat_id'] = $data['product_collection_categories_id'];
			$temp['w_p_id'] = $data['webshop_product_id'];
			
			$temp['cat_name'] = $data['cat_name'];
			
			if ($getActive)
				$temp['active'] = $data['active'];
				
			$temp['name'] = $data['name'];
			
			$return[] = $temp;
			 
		}
		if ($webshopdata and is_array($return))
			{
				array_splice($return, count($return)-1);
				$temp['last']=1;
				$return[]=$temp;
			}
		return $return;		
	}
	
	/**
	 * add preconfig to config
	 *
	 * @param integer $preconfig_id preconfig id
	 * @param string $user_id session id
	 */
	function addPreconfigToConfig($preconfig_id, $user_id=0){
		global $dbi;

		if (is_string($user_id))
		{
			$sql="delete from product_collection_configs where session_id='".$user_id."'";
			$res=sql_query($sql,$dbi);

			$sql="select pre.webshop_product_id, pre.product_collection_categories_id from product_collection_preconfigs pre
				where config_id=".$preconfig_id;
			$res=sql_query($sql,$dbi);

			while(list($webshop_product_id, $product_collection_categories_id)=sql_fetch_array($res,$dbi)){
				$sql="insert into product_collection_configs (session_id, webshop_product_id, product_collection_categories_id)
					values('$user_id', $webshop_product_id, $product_collection_categories_id)";

				sql_query($sql,$dbi);				
			}
		}
	}
	
	
	function deletePreconfig($preconfig_id){
		global $dbi;

		$sql="delete from webshop_product where id=".$preconfig_id;
		$res=sql_query($sql,$dbi);
		
		$sql="delete from product_collection_preconfigs where config_id=".$preconfig_id;
		$res=sql_query($sql,$dbi);
	}
	
	
	/**
	 * add config to preconfig
	 *
	 * @param integer $preconfig_id preconfig id
	 * @param string $user_id session id
	 */
	function addConfigToPreconfig($preconfig_id, $user_id=0){
		global $dbi;

		if (is_numeric($preconfig_id) and is_string($user_id))
		{
			$sql="select conf.webshop_product_id , conf.product_collection_categories_id from product_collection_configs conf where session_id='".$user_id."'";
			$res=sql_query($sql,$dbi);
			
			while(list($product_id, $product_collection_categories_id)=sql_fetch_array($res,$dbi)){
				$sql="insert into product_collection_preconfigs (config_id, webshop_product_id, product_collection_categories_id)
					values($preconfig_id, $product_id, $product_collection_categories_id)";
				sql_query($sql,$dbi);				
			}
			
			$sql="delete from product_collection_configs where session_id='".$user_id."'";
			$res=sql_query($sql,$dbi);

		}
	}

	
	function saveCategoryMainPic($category_id){
		global $dbi;
		
		if (is_numeric($category_id)){
			$sql = "update product_collection_categories set main_pic=1 where product_collection_categories_id=".$category_id;
			sql_query($sql,$dbi);
			
			$sql = "update product_collection_categories set main_pic=0 where product_collection_categories_id!=".$category_id;
			sql_query($sql,$dbi);
		}
	}
}
?>