<?php
/**
 * @version        1.9.7
 * @package        Joomla
 * @subpackage     Edocman
 * @author         Tuan Pham Ngoc
 * @copyright      Copyright (C) 2011 - 2024 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */

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

class EDocmanModelDownloadlogs extends OSModelList
{
	/**
	 * Constructor method, initialize config data for the model and set default model state
	 *
	 * @param array $config
	 */
	public function __construct($config = array())
	{
		$config['search_fields'] = array('b.title', 'c.username');
		$config['table']         = '#__edocman_statistics';
		parent::__construct($config);

		$this->state->setDefault('filter_order_Dir', 'DESC');
		$this->state->insert('start_date','string','')
					->insert('end_date','string','');
	}

	/**
	 * Builds SELECT columns list for the query
	 *
	 * @param JDatabaseQuery $query
	 *
	 * @return $this
	 */
	protected function _buildQueryColumns(JDatabaseQuery $query)
	{
		$query->select('tbl.*, b.title, c.username AS downloader_username, c.email AS downloader_email');

		return $this;
	}

	/**
	 * Builds JOIN clauses for the query
	 *
	 * @param JDatabaseQuery $query
	 *
	 * @return $this
	 */
	protected function _buildQueryJoins(JDatabaseQuery $query)
	{
		$query->join('LEFT', '#__edocman_documents AS b ON tbl.document_id=b.id')
			->join('LEFT', '#__users AS c ON tbl.user_id=c.id');

		return $this;
	}

	protected function _buildQueryGroup(JDatabaseQuery $query)
	{
		$query->group('tbl.download_time, tbl.document_id');

		return $this;
	}

	/**
	 * Builds WHERE clauses for the query
	 *
	 * @param JDatabaseQuery $query
	 *
	 * @return $this
	 */
	protected function _buildQueryWhere(JDatabaseQuery $query)
	{
		parent::_buildQueryWhere($query);
		if($this->state->start_date != "" && $this->state->start_date != "0000-00-00 00:00:00")
		{
			$query->where('tbl.download_time >= "'.$this->state->start_date. '"');
		}

		if($this->state->end_date != "" && $this->state->end_date != "0000-00-00 00:00:00")
		{
			$query->where('tbl.download_time <= "'.$this->state->end_date. '"');
		}
		//echo($query->__toString());die();
		return $this;
	}
}
