<?php defined('_JEXEC') or die();
/**
 * @version		1.1.1
 * @package		Read More, Basic
 * @copyright	2009 Rockford Solutions, Inc. All rights reserved.
 * @license		GNU/GPL
 */
 
jimport('joomla.application.component.model');
jimport('joomla.html.pagination');

/**
 * Emails Model
 *
 * @package		Read More, Basic
 */
class ReadmoreModelEmails extends JModel {
	/**
	 * Database recordset data
	 *
	 * @access	protected
	 * @var		array
	 */
	protected $_data = null;
	
	/**
	 * Database recordset size
	 *
	 * @access	protected
	 * @var		int
	 */
	protected $_total = 0;
	
	/**
	 * Database recordset pagination object
	 *
	 * @access	protected
	 * @var		object
	 */
	protected $_pagination = null;
	
	/**
	 * Constructor
	 */
	public function __construct() {
		parent::__construct();
		
		global $mainframe, $option;
		
		$limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart = JRequest::getVar('limitstart', 0, '', 'int');
		$limitstart = $limit != 0 ? floor($limitstart / $limit) * $limit : 0;
		
		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);
	}
	
	/**
	 * Returns a database query based on search/sort criteria
	 *
	 * @access	protected
	 * @return	string	Database query
	 */
	protected function _buildQuery() {
		global $mainframe, $option;
		
		$filter_search     = $mainframe->getUserStateFromRequest('filter_search',     'filter_search',     '',      'string');
		$filter_search_col = $mainframe->getUserStateFromRequest('filter_search_col', 'filter_search_col', 'email', 'cmd');
		$filter_order      = $mainframe->getUserStateFromRequest('filter_order',      'filter_order',      'id',    'cmd');
		$filter_order_Dir  = $mainframe->getUserStateFromRequest('filter_order_Dir',  'filter_order_Dir',  'asc',   'word');
		
		$this->setState('filter_search',     $filter_search);
		$this->setState('filter_search_col', $filter_search_col);
		$this->setState('filter_order',      $filter_order);
		$this->setState('filter_order_Dir',  $filter_order_Dir);
		
		$query = 'SELECT * FROM `#__readmore_email` '
		       . 'WHERE `' . $filter_search_col . '` LIKE "%' . $filter_search . '%" '
		       . 'ORDER BY `' . $filter_order . '` ' . $filter_order_Dir;
		
		return $query;
	}
	
	/**
	 * Returns database recordset data, performing database query if necessary
	 *
	 * @return	array	Recordset data
	 */
	public function getData() {
		if (empty($this->_data)) {
			$query = $this->_buildQuery();
			$this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
		}

		return $this->_data;
	}
	
	/**
	 * Returns database recordset size, performing database query if necessary
	 *
	 * @return	int 	Recordset size
	 */
	public function getTotal() {
		if (empty($this->_total)) {
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
		}
		
		return $this->_total;
	}
	
	/**
	 * Returns database recordset pagination object
	 *
	 * @return	object	recordset pagination object
	 */
	public function getPagination() {
		if (empty($this->_pagination)) {
			$this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit'));
		}
		
		return $this->_pagination;
	}
}
