<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: 			*/

// +----------------------------------------------------------------------+

// | PHP version 4/5                                                      |

// +----------------------------------------------------------------------+

// | Copyright (c) 2004-2010 ARMIA INC                                    |

// +----------------------------------------------------------------------+

// | This source file is a part of iScripts VistaCart                     |

// +----------------------------------------------------------------------+

// | Authors: Jipson<jipson.thomas@armiasystems.com>             		  |

// +----------------------------------------------------------------------+

class Category extends AppModel

{



   	 var $name = 'Category';

	



	 var $displayField = 'Category_cname';  



		 



	 var $belongsTo = array( 



			'Parentcategory' => 



				array('className' => 'Category', 



					  'foreignKey' => 'parent_id' 



			), 



		 ); 



	



	 var $hasMany = array( 



		'Childcategory' => 



				array('className' => 'Category', 



			  'foreignKey' => 'parent_id' 



				), 'Product'



		); 		



		





		



	##var $actsAs = array('Tree');



		#### Function to get leaf nodes...........



		function findleaf(){



			$Db = ConnectionManager::getDataSource($this->useDbConfig);



			$cttbl = $Db->fullTableName('categories');



			$dat	=	$this->query("select f.id as id,f.cname as cname from $cttbl f



Left join $cttbl s on f.id=s.parent_id



where  ISNULL(s.cname) AND f.id <> 1");



			$cat	=	array();



			foreach($dat as $el){



				$id		=	$el['f']['id'];



				$name	=	$el['f']['cname'];



				$cat[$id]	=	$name;



			}



			return $cat;



		}



		



		function getchilds($id,$ind=0){



			global $categ;



			global $ind;



			$Db = ConnectionManager::getDataSource($this->useDbConfig);



			$cttbl = $Db->fullTableName('categories');



			$dat	=	$this->query("select id from $cttbl where parent_id = ".$id);



			//print_r($dat);exit;



			$cttbl		=	str_replace('`','',$cttbl);



			foreach($dat as $el){



				$id					=	$el[$cttbl]['id'];



				$categ[$ind]["id"]	=	$id;



				$ind++;



				$this->getchilds($id,$ind);



			}



			return $categ;



		}



		



		function getFulltablename($tablename)



		{



		  $Db = ConnectionManager::getDataSource($this->useDbConfig);



		  $tblname = $Db->fullTableName($tablename);



		  return $tblname;



		}



		//Function to add custom value combinations for category



		/*



		function customValueCombination($catid,$combinations)



		{



			$Db = ConnectionManager::getDataSource($this->useDbConfig);



			$cttbl = $Db->fullTableName('customcombinations');			



		    $combinations=explode(',',$combinations);



			if(is_array($combinations) && !empty($combinations))



			{



				foreach($combinations as $key =>  $val)



				{



				  $combn_id=explode('->',$val);



				  $this->query("insert into $cttbl (category_id,customfield_id,customvalue_id) values ('".$catid."','".$combn_id[0]."','".$combn_id[1]."')");	



				}						



			}	



			return true;



		 } 



		 */

		 

		function checkAddDups($catname,$parentid){

   		$Db = ConnectionManager::getDataSource($this->useDbConfig);

		$cattbl = $Db->fullTableName('categories');	

		$res=$this->query("SELECT count(*)  as dups from $cattbl WHERE `cname` ='".$catname."' and `parent_id`='".$parentid."'");

		return $res[0][0]["dups"];

         }

        function checkeditDups($catname,$parentid,$editid){

   		$Db = ConnectionManager::getDataSource($this->useDbConfig);

		$cattbl = $Db->fullTableName('categories');	

		$res=$this->query("SELECT count(*)  as dups from $cattbl WHERE `cname` ='".$catname."' and `parent_id`='".$parentid."' and `id`<>".$editid );

		return $res[0][0]["dups"];

        }

}
?>