<?php
/**
 * Content Templater List Model
 *
 * @package     Content Templater
 * @version     1.5.4
 *
 * @author      Peter van Westen <peter@nonumber.nl>
 * @link        http://www.nonumber.nl
 * @copyright   Copyright (C) 2010 NoNumber! All Rights Reserved
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

// Import MODEL object class
jimport( 'joomla.application.component.model' );
// Include Item Model object class
require_once JPATH_ADMINISTRATOR.DS.'components'.DS.'com_contenttemplater'.DS.'models'.DS.'item.php';

/**
 * Content Templater List Model
 */
class ContentTemplaterModelList extends JModel
{

	/**
	 * List data
	 *
	 * @var array
	 */
	var $_data = array();
	/**
	 * Category total
	 *
	 * @var integer
	 */
	var $_total = null;

	/**
	 * Pagination object
	 *
	 * @var object
	 */
	var $_pagination = null;

	/**
	 * table_prefix - table prefix for all component table
	 *
	 * @var string
	 */
	var $_table_prefix = null;

	/**
	 * Custom Constructor
	 */
	function __construct()
	{
		parent::__construct();

		$mainframe =& JFactory::getApplication();

		// initialize class property
		$this->_table_prefix = '#__';

		// Get the pagination request variables
		$limit		= $mainframe->getUserStateFromRequest( 'list.list.limit', 'limit', $mainframe->getCfg( 'list_limit' ), 0 );
		$limitstart = $mainframe->getUserStateFromRequest( 'list.list.limitstart', 'limitstart', 0 );

		$this->setState( 'limit', $limit );
		$this->setState( 'limitstart', $limitstart );
		$this->setState( 'state', '' );
		$this->setState( 'search', '' );
	}

	/**
	 * Method to get a contenttemplater data
	 *
	 * this method is called from the owner VIEW by VIEW->get( 'Data' );
	 * - get list of all contenttemplater for the current data page
	 * - pagination is spec. by variables limitstart,limit
	 * - ordering of list is build in _buildContentOrderBy
	 */
	function getData()
	{
		// Lets load the content if it doesn't already exist
		if ( empty( $this->_data ) ) {
			$query = $this->_buildQuery();
			$this->_data = $this->_getList( $query, $this->getState( 'limitstart' ), $this->getState( 'limit' ) );
		}
	}

	/**
	 * Method to get all params data
	 *
	 * this method is called from the owner VIEW by VIEW->get( 'Data' );
	 * - get list of all contenttemplater for the current data page
	 * - pagination is spec. by variables limitstart,limit
	 * - ordering of list is build in _buildContentOrderBy
	 */
	function getAllParams()
	{
		$this->getData();

		$data_array = array();
		foreach( $this->_data as $dat ) {
			$item = new ContentTemplaterModelItem();
			$item->_id = $dat->id;
			$item->getData( 0, $dat );
			$data_array[] = $item->_data;
		}

		return $data_array;
	}

	/**
	 * Method to get all params data of published items only
	 *
	 * this method is called from the owner VIEW by VIEW->get( 'Data' );
	 * - get list of all contenttemplater for the current data page
	 * - pagination is spec. by variables limitstart,limit
	 * - ordering of list is build in _buildContentOrderBy
	 */
	function getAllPublishedParams()
	{
		$this->setState( 'state', 'P' );
		return $this->getAllParams();
	}

	/**
	 * Method to get the total number of contenttemplater items
	 *
	 * @access public
	 * @return integer
	 */
	function getTotal()
	{
		// Lets load the content if it doesn't already exist
		if ( empty( $this->_total ) ) {
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount( $query );
		}

		return $this->_total;
	}

	/**
	 * Method to get a pagination object for the contenttemplater
	 *
	 * @access public
	 * @return integer
	 */
	function getPagination()
	{
		// Lets 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 _buildQuery()
	{
		$where = array();
		$state = $this->getState( 'state' );
		if ( $state == 'P' ) {
			$where[] = ' published != 0';
		} else if ( $state == 'U' ) {
			$where[] = ' published = 0';
		}
		$search = $this->getState( 'search' );
		if ( $search ) {
			$where[] = " (
				LOWER( name ) LIKE ".$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false )."
				OR LOWER( description ) LIKE ".$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false )."
			) ";
		}

		$query = ' SELECT * FROM '.$this->_table_prefix.'contenttemplater';
		if ( !empty( $where ) ) {
			$query .= ' WHERE '.implode( ' AND ', $where );
		}
		$query .= $this->_buildContentOrderBy();

		return $query;
	}

	function _buildContentOrderBy()
	{
		$mainframe =& JFactory::getApplication();
		$option	= JRequest::getCmd( 'option' );

		$orderby = 'ordering, id';
		if ( $option == 'com_contenttemplater' ) {
			// give me ordering from request
			$filter_order		= $mainframe->getUserStateFromRequest( 'contenttemplater.list.filter_order',		'filter_order',		'ordering' );
			$filter_order_Dir	= $mainframe->getUserStateFromRequest( 'contenttemplater.list.filter_order_Dir',	'filter_order_Dir',	'' );
			$orderby = '`'.$filter_order.'` '.$filter_order_Dir.', '.$orderby;
		}

		// all countries are in the same category (no category)
		$orderby	= ' ORDER BY '.$orderby;

		return $orderby;
	}

}