<?php
/**
 * @version $Id$
 * @package    Contact_Enhanced
 * @author     Douglas Machado {@link http://ideal.fok.com.br}
 * @author     Created on 28-Jul-09
 * @license		GNU/GPL, see license.txt
 * Contact Enhanced  is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * @license		GNU/GPL, see license.txt
 * Contact Enhanced  is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses. */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );


/**
* @package		Contact_Enhanced
 * @subpackage	Contact
 */
class TableCustomfield extends JTable
{
	/** @var int Primary key */
	var $id 				= null;
	/** @var string */
	var $name 				= null;
	/** @var int */
	var $published 			= null;
	/** @var int */
	var $required	 		= null;
	/** @var int */
	var $ordering 			= null;
	/** @var int */
	var $catid	 			= null;
	/** @var string */
	var $type	 			= null;
	/** @var string */
	var $value	 			= null;
	/** @var string */
	var $attributes 		= null;
	/** @var string */
	var $tooltip	 		= null;
	/** @var bool */
	var $iscore	 		= null;
	/** @var string */
	var $params	 		= null;
	

	/**
	* @param database A database connector object
	*/
	function __construct(&$db)
	{
		parent::__construct( '#__contact_enhanced_cf', 'id', $db );
	}
	
	function canInsert(){
		$denyMultiple	= array('subject','email','name','username');
		
		if( !in_array($this->type, $denyMultiple) ){
			return true;
		}
		
		$where	= array();
		if($this->id){
			$where[]= 'id <> '.$this->_db->Quote($this->id);
		}

		$where[]= 'type		= '.$this->_db->Quote($this->type);
		$where[]= '( catid	= '.$this->_db->Quote($this->catid) .' OR catid	= '.$this->_db->Quote('0'). ')';
		
		$query	= 'SELECT * FROM '.$this->_tbl
				 . (count( $where ) ? ' WHERE '. implode( ' AND ', $where ) : '')
				 ;
		$this->_db->setQuery($query);
		// echo $this->_db->getQuery($query); exit;
		if( count($this->_db->loadResult()) > 0 ){
			return false;
		}else{
			return true;
		}
		
	}
}
