<?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_EnhancedsModelcustomvalues extends JModel
{
	var $_data;
	var $_total = null;
	var $_pagination = null;
	var $filter_order;
	var $filter_order_Dir;
	var $filter_category;
	var $filter_type;
	var $search;

	function __construct()
	{
		parent::__construct();
		global $mainframe, $option, $controller;
		$this->filter_order_Dir	= $mainframe->getUserStateFromRequest( $controller.$option.'.filter_order_Dir'	, 'filter_order_Dir', 	'ASC', 'word' );
		$this->filter_order		= $mainframe->getUserStateFromRequest( $controller.$option.'.filter_order'		, 'filter_order',		'ordering', 'cmd' );
		$this->filter_category	= $mainframe->getUserStateFromRequest( $controller.$option.'filter_category'	, $controller.'filter_category',	'',	'word' );
		$this->filter_type		= $mainframe->getUserStateFromRequest( $controller.$option.'filter_type'		, $controller.'filter_type',		'',	'word' );
		
		$this->search		= $mainframe->getUserStateFromRequest( "$controller$option.search", 'search', '', 'string' );
		$this->search		= JString::strtolower( $this->search );

		$limit		= $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
		$limitstart	= $mainframe->getUserStateFromRequest( $controller.$option.'.limitstart', 'limitstart', 0, 'int' );
		$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);
	}
	
	function _buildQuery()
	{
		$where = array();

		if ($this->search)
		{
			$where[] = 'LOWER(text) LIKE \'%'. $this->search. '%\'';
		}
		if ($this->filter_category)
		{
			$where[] = 'category = '. $this->_db->Quote($this->filter_category);
		}
		if ($this->filter_type)
		{
			$where[] = 'type = '. $this->_db->Quote($this->filter_type);
		}

		$where =( count($where) ) ? ' WHERE ' . implode( ' AND ', $where ) : '';
		$orderby = 'category ASC';

		if (($this->filter_order) && ($this->filter_order_Dir))
		{
			$orderby 	= ' ORDER BY '.' category ASC,'. $this->filter_order .' '. $this->filter_order_Dir;
		}

		$this->_query = ' SELECT *'
				. ' FROM #__contact_enhanced_cv'
				. $where
				. $orderby
				;
        	
		return $this->_query;	
	}
	

	function getData() 
	{
 		if (empty($this->_data))
 		{
			$query = $this->_buildQuery();
 	    	$this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
		}
		return $this->_data;
	}	

	function getList()
	{
		// table ordering
		$lists['order_Dir']	= $this->filter_order_Dir;
		$lists['order']		= $this->filter_order;

		// search filter
		$lists['search']	= $this->search;
		$lists['filter_category']	= $this->filter_category;
		$lists['filter_type']		= $this->filter_type;
		
		return $lists;
	}

	function getTotal()
	{
		// Load the content if it doesn't already exist
		if (empty($this->_total))
		{
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);	
		}
		
		return $this->_total;
	}

	function getPagination()
	{
		// Load the content if it doesn't already exist
		if (empty($this->_pagination))
		{
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
		}
		
		return $this->_pagination;
	}
	
	function getCategories()
	{
		$db =& JFactory::getDBO();
	
		$query = 'SELECT DISTINCT category AS value, category AS text'
					. ' FROM #__contact_enhanced_cv'
					. ' WHERE published = 1'
						. ' AND category <> '.$db->Quote('')
				. ' 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'
					. ' AND type <> '.$db->Quote('')
				. ' ORDER BY text ASC '
				;
		$db->setQuery( $query );
		$types = $db->loadObjectList();
		return $types;
	}
}// class
