<?php
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.model');
/**
 * Charts model. Database select operations.
 *
 * @version    1.0.0
 * @author 	   ARTIO s.r.o., http://www.artio.net
 * @package    Charts
 * @subpackage Models
 * @copyright  Copyright (C) 2009 ARTIO s.r.o.. All rights reserved.
 * @since      Version 1.0.0
 */
class ARTIOFusionChartsModelARTIOFusionCharts extends JModel
{
    /**
     * Charts data
     *
     * @access public
     * @var array
     */
    public $_data = null;
    /**
     * Total charts count
     *
     * @access public
     * @var int
     */
    public $_total = null;
    /**
     * Page listing object
     *
     * @access public
     * @var JPagination
     */
    public $_pagination = null;
    /**
     * Constructor. Seting maim params.
     *
     * @access public
     */
    function __construct ()
    {
        parent::__construct();
        $mainframe = &JFactory::getApplication();
        $option = JRequest::getString('option');
        $limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
        $limitstart = $mainframe->getUserStateFromRequest($option . '.limitstart', 'limitstart', 0, 'int');
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
        $this->setState('limit', $limit);
        $this->setState('limitstart', $limitstart);
    }
    /**
     * Get chart list by listing params.
     *
     * @access public
     * @return array
     */
    function getData ()
    {
        if (empty($this->_data)) {
            $query = $this->_buildQuery();
            $this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
        }
        return $this->_data;
    }
    /**
     * Get total charts count
     *
     * @access public
     * @return int
     */
    function getTotal ()
    {
        if (empty($this->_total)) {
            $query = $this->_buildQuery();
            $this->_total = $this->_getListCount($query);
        }
        return $this->_total;
    }
    /**
     * Get listing object with seting
     *
     * @access public
     * @return JPagination
     */
    function getPagination ()
    {
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit'));
        }
        return $this->_pagination;
    }
    /**
     * Build and return db query
     *
     * @access private
     * @return string
     */
    private function _buildQuery ()
    {
        $where = $this->_buildContentWhere();
        $orderby = $this->_buildContentOrderBy();
        $query = 'SELECT c.*, u.name AS editor FROM `#__artiofusioncharts` AS c ';
        $query .= 'LEFT JOIN #__users AS u ON c.checked_out = u.id ';
        $query .= $where.' '.$orderby;
        return $query;
    }
    /**
     * Get db query order options 
     *
     * @access private
     * @return string
     */
    private function _buildContentOrderBy ()
    {
        $mainframe = &JFactory::getApplication();
        $option = JRequest::getString('option');
        $filter_order = $mainframe->getUserStateFromRequest($option . 'filter_order', 'filter_order', 'id', 'word');
        $filter_order_Dir = $mainframe->getUserStateFromRequest($option . 'filter_order_Dir', 'filter_order_Dir', 'DESC', 'word');
        if ($filter_order && $filter_order_Dir) {
            return " ORDER BY `$filter_order` $filter_order_Dir ";
        }
        return " ORDER BY `id` DESC ";
    }
    /**
     * Get db query where options
     *
     * @access private
     * @return string
     */
    private function _buildContentWhere ()
    {
        $mainframe = &JFactory::getApplication();
        $option = JRequest::getString('option');
        $search = $mainframe->getUserStateFromRequest($option . 'search', 'search', '', 'string');
        $search = JString::strtolower($search);
        $category = $mainframe->getUserStateFromRequest($option . 'filter_categories', 'filter_categories', '', 'string');
        $type = $mainframe->getUserStateFromRequest($option . 'filter_types', 'filter_types', '', 'string');
        $where = array();
        if ($search) {
            $where[] = " LOWER(`title`) LIKE '%$search%' ";
        }
        if ($category) {
            $where[] = " category = '$category' ";
        }
        if ($type) {
            $where[] = " type = '$type' ";
        }
        $where = (count($where) ? ' WHERE ' . implode(' AND ', $where) : '');
        return $where;
    }
}
?>