<?php
/**
 * @version $Id$
 * @package    Contact_Enhanced
 * @subpackage Models
 * @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.
 */

//--No direct access
defined('_JEXEC') or die('Direct Access NOT Allowed. Please try to access the main page');

jimport('joomla.application.component.model');

class Contact_EnhancedsModelcustomvalue extends JModel
{
	function __construct()
	{
		parent::__construct();
	}

	function store($data)
	{	
		$row =& $this->getTable('customvalue');

		if (!$row->bind($data)) {
			return false;
		}

		if (!$row->check()) {
			return false;
		}

		if (!$row->store()) {
			return false;
		}
		if($row->id){
			return $row->id;
		}else{
			return $row->_db->insertid();
		}
	}	
	
	function delete()
	{
		$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );
		$row =& $this->getTable('customvalue');
  
		if (count( $cids )) {
			foreach($cids as $cid) {
				if (!$row->delete( $cid )) {
					$this->setError( $row->getErrorMsg() );
					return false;
				}
			}
		}
		return true;
	}

	function getData()
	{
		$id = JRequest::getVar('cid');
		$row =& $this->getTable('customvalue');
		$row->load($id[0]);
		return $row;
	}
	function getCategories()
	{
		$db =& JFactory::getDBO();
	
		$query = '(SELECT id AS value, title AS text'
					. ' FROM #__categories'
					. ' WHERE section = '.$db->Quote('com_contact_enhanced_details')
					. ' AND published = 1)'
				. ' UNION '
				.' (SELECT DISTINCT category AS value, category AS text'
					. ' FROM #__contact_enhanced_cv'
					. ' WHERE published = 1)'
				. ' ORDER BY text ASC '
				;
		$db->setQuery( $query );
		$categories = $db->loadObjectList();
		return $categories;
	}
	
	function getTypes()
	{
		$db =& JFactory::getDBO();
	
		$query = 'SELECT DISTINCT type AS value, type AS text'
					. ' FROM #__contact_enhanced_cv'
					. ' WHERE published = 1'
				. ' ORDER BY text ASC '
				;
		$db->setQuery( $query );
		$types = $db->loadObjectList();
		return $types;
	}
}
