<?php
/**
 * Adodis Blog package
 * @author J-Extension <contact@jextn.com>
 * @link http://www.jextn.com
 * @copyright (C) 2012 - 2013 J-Extension
 * @license GNU/GPL, see LICENSE.php for full license.
**/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

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

class AdodisBlogModelComments extends JModelList
{
	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
				'id', 'ct.id',
				'title', 'bg.title', 'blog_title',
				'comment', 'ct.comment',
				'name', 'ur.name', 'user_name',
				'ip_address', 'ct.ip_address',
				'created_date', 'ct.created_date',
				'published', 'ct.published',
				'language', 'ct.language',
			);
		}

		parent::__construct($config);
	}

	//Method to auto-populate the model state.
	protected function populateState($ordering = null, $direction = null)
	{
		// Initialise variables.
		$app		= JFactory::getApplication();

		// Adjust the context to support modal layouts.
		if ($layout = JRequest::getVar('layout')) {
			$this->context .= '.'.$layout;
		}

		$search 	= $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
		$this->setState('filter.search', $search);

		$published	= $this->getUserStateFromRequest($this->context.'.filter.published', 'filter_published', '');
		$this->setState('filter.published', $published);

		$users		= $this->getUserStateFromRequest($this->context.'.filter.user', 'filter_user', '');
		$this->setState('filter.user', $users);

		$access		= $this->getUserStateFromRequest($this->context.'.filter.access', 'filter_access', 0, 'int');
		$this->setState('filter.access', $access);

		$language	= $this->getUserStateFromRequest($this->context.'.filter.language', 'filter_language', '');
		$this->setState('filter.language', $language);

		// List state information.
			parent::populateState('ct.id', 'asc');
	}

	/**
	 * Method to get a store id based on model configuration state.
	 */
	protected function getStoreId($id = '')
{
		// Compile the store id.
			$id	.= ':'.$this->getState('filter.search');
			$id	.= ':'.$this->getState('filter.published');
			$id	.= ':'.$this->getState('filter.user');
			$id	.= ':'.$this->getState('filter.access');
			$id	.= ':'.$this->getState('filter.language');

		return parent::getStoreId($id);
	}

	/**
	 * Build an SQL query to load the list data.
	 */
	protected function getListQuery()
	{
		// Create a new query object.
		$db				= $this->getDbo();
		$query			= $db->getQuery(true);

		// Select the required fields from the table.
		$query->select( $this->getState(
			'list.select',
			'ct.* '
		) );
		$query->from( '#__adodisblog_comments as ct' );

		// Join over the Blogs.
		$query->select( 'bg.title AS blog_title' );
		$query->join( 'LEFT', '#__adodisblog_blogs AS bg ON bg.id = ct.blog_id' );

		// Join over the Users.
		$query->select( 'ur.name AS user_name' );
		$query->join( 'LEFT', '#__adodisblog_users AS ur ON ur.user_id = ct.user_id' );

		// Join over the language
		$query->select( 'l.title AS language_title' );
		$query->join( 'LEFT', '`#__languages` AS l ON l.lang_code = ct.language' );

		// Filter by published state
		$published		= $this->getState('filter.published');
		if (is_numeric($published)) {
			$query->where('ct.published = ' . (int) $published);
		} else if ($published === '') {
			$query->where('(ct.published = 0 OR ct.published = 1)');
		}

		// Filter by User
		$users			= $this->getState('filter.user');
		if ($users) {
			$users 		= $db->Quote('%'.$db->getEscaped($users, true).'%');
			$query->where('(ur.name LIKE '.$users.')');
		}

		// Filter by search in title & microblog.
		$search			= $this->getState('filter.search');
		if (!empty($search)) {
			if (stripos($search, 'id:') === 0) {
				$query->where('ct.id = '.(int) substr($search, 3));
			} else {
				$search = $db->Quote('%'.$db->getEscaped($search, true).'%');
				$query->where('(bg.title LIKE '.$search.' OR ct.comment LIKE '.$search.')');
			}
		}

		// Filter by access level.
		if ($access = $this->getState('filter.access')) {
			$query->where('ct.access = ' . (int) $access);
		}

		// Filter on the language.
		if ($language = $this->getState('filter.language')) {
			$query->where('ct.language = '.$db->quote($language));
		}

		// Add the list ordering clause.
		$orderCol		= $this->state->get('list.ordering');
		$orderDirn		= $this->state->get('list.direction');

		$query->order($db->getEscaped($orderCol.' '.$orderDirn));

		return $query;
	}

	function filterUser($active = NULL)
	{
		$options		= array();
		$db				= JFactory::getDbo();
		$query			= $db->getQuery(true);

		$query->select('name AS name');
		$query->from('#__adodisblog_users');
		$query->order('name');
		$db->setQuery($query);
		$users			= $db->loadObjectList();

		$options[]      = JHTML::_('select.option','', "--".JText::_( 'COM_ADODISBLOG_SELECT_USER' )."--");
		foreach ( $users as $key=>$value ) {
			$options[]	= JHTML::_('select.option',  $value->name);
		}

		$users 			= JHTML::_('select.genericlist',  $options, 'filter_user', 'class="inputbox" size="1" onchange="document.adminForm.submit( );"', 'value', 'text',$active);

		return $users;
	}
}
?>
