<?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 Customfieldvalue extends AppModel

{

   var $name = 'Customfieldvalue';   

 var $belongsTo = 'Customfield'; 

   function delcusnam($nam){

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

		$cftbl = $Db->fullTableName('customfields');

		$pcfttbl = $Db->fullTableName('productcustomfields');

		$this->query("DELETE c.*, p.* FROM $cftbl c LEFT JOIN $pcfttbl p ON c.id = p.customfield_id WHERE c.custom_name = '$nam'");

		return  true;

   }

   function delcusfieldval($delid){

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

		$cfvtbl = $Db->fullTableName('customfieldvalues');

		$this->query("DELETE from $cfvtbl WHERE `id` =".$delid);		

		return  true;

   }

      function updatecusfieldval($customvalue,$editid){

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

		$cfvtbl = $Db->fullTableName('customfieldvalues');	

		$this->query("UPDATE $cfvtbl SET `custom_value` = '".$customvalue."' WHERE `id` =".$editid);

		return  true;

   }

    function checkAddDups($customvalue,$customid){

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

		$cfvtbl = $Db->fullTableName('customfieldvalues');	
                
		$res=$this->query("SELECT count(*)  as dups from $cfvtbl WHERE `custom_value` ='".$customvalue."' and `customfield_id`='".$customid."'");

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

   }

   function checkeditDups($customvalue,$customid,$editid){

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

		$cfvtbl = $Db->fullTableName('customfieldvalues');	
                
		$res=$this->query("SELECT count(*)  as dups from $cfvtbl WHERE `custom_value` ='".$customvalue."' and `customfield_id`='".$customid."' and `id`<>".$editid );

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

   }

   function checkChildExits($id)

   {

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

		$cattbl = $Db->fullTableName('categories');	
                $cattbl1 = str_replace('`' ,'',$cattbl);
               

		$combn=$this->query("SELECT custom_combination  from $cattbl");

		$combn_array=array();

		foreach($combn as $val)

		{

		     if(isset($val[$cattbl1]["custom_combination"]))

			 $split=explode(",",$val[$cattbl1]["custom_combination"]);

			 if(isset($split) && !empty($split))

			 {

			   foreach($split as $splitval)

			   $combn_array[]=$splitval;		

			 }  	 

		}			
                
		if(in_array($id,$combn_array))

		return 1;

		else

		return 0;		

   }

}

?>