Your IP : 216.73.216.41


Current Path : /home/purehotels/public_html/administrator/components/com_easyfolderlistingpro/models/
Upload File :
Current File : /home/purehotels/public_html/administrator/components/com_easyfolderlistingpro/models/logs.php

<?php
/**
* @version		3.2
* @author		Michael A. Gilkes (michael@valorapps.com)
* @copyright	Michael Albert Gilkes
* @license		GNU/GPLv3

Easy Folder Listing Pro Component for Joomla!
Copyright (C) 2012-2016 Michael Albert Gilkes (Valor Apps)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

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


/**
 * EasyFolderListingPro Logs Model
 */
class EasyFolderListingProModelLogs extends JModelList
{
	/**
	 * The event to trigger after deleting the data.
	 */
	protected $event_after_delete = 'onContentAfterDelete';

	/**
	 * The event to trigger before deleting the data.
	 */
	protected $event_before_delete = 'onContentBeforeDelete';
	
	public function __construct($config = array())
	{
		/* Add all fields that may be filtered and sorted */
		if (empty($config['filter_fields']))
		{
			$config['filter_fields'] = array(
				'id', 'a.id',
				'filepath', 'a.filepath',
				'user_id_fk', 'a.user_id_fk', 'username', 'name',
				'dl_or_pv', 'a.dl_or_pv',
				'ip_address', 'a.ip_address',
				'access_time', 'a.access_time',
			);
		}

		parent::__construct($config);
	}
	
	protected function getStoreId($id = '')
	{
		// Compile the store id.
		$id	.= ':'.$this->getState('filter.search');
		$id	.= ':'.$this->getState('filter.dl_or_pv');
		$id	.= ':'.$this->getState('filter.username');

		return parent::getStoreId($id);
	}
	
	public function getTable($type = 'Log', $prefix = 'EasyFolderListingProTable', $config = array())
	{
		return JTable::getInstance($type, $prefix, $config);
	}
	
	protected function populateState($ordering = null, $direction = null)
	{
		$app = JFactory::getApplication('administrator');

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

		$dl_or_pv = $this->getUserStateFromRequest($this->context.'.filter.dl_or_pv', 'filter_dl_or_pv', '');
		$this->setState('filter.dl_or_pv', $dl_or_pv);

		$username = $this->getUserStateFromRequest($this->context.'.filter.username', 'filter_username', '');
		$this->setState('filter.username', $username);

		//List state information.
		parent::populateState('a.id', 'asc');
	}
	
	function getListQuery()
	{
		//Create a new query object.
		$db		= $this->getDbo();
		$query	= $db->getQuery(true);
		$user	= JFactory::getUser();

		// Select the required fields from the table.
		$query->select(
			$this->getState(
				'list.select',
				'a.id AS id, '.
				'a.filepath AS filepath, '.
				'a.user_id_fk AS user_id_fk, '.
				'a.dl_or_pv AS dl_or_pv, '.
				'a.ip_address AS ip_address, '.
				'a.access_time AS access_time'
			)
		);
		$query->from('#__eflp_logs AS a');

		//Join over the users for the checked out user.
		$query->select(
			'uc.username AS username, '.
			'uc.name AS name'
		);
		$query->join('LEFT', '#__users AS uc ON uc.id=a.user_id_fk');

		//Filter by published state
		$dl_or_pv = $this->getState('filter.dl_or_pv');
		if (is_numeric($dl_or_pv))
		{
			$query->where('a.dl_or_pv = ' . (int) $dl_or_pv);
		}
		elseif ($dl_or_pv === '')
		{
			$query->where('(a.dl_or_pv IN (0, 1))');
		}

		// Filter by search in title
		$search = $this->getState('filter.search');
		if (!empty($search))
		{
			if (stripos($search, 'id:') === 0)
			{
				$query->where('a.id = '.(int) substr($search, 3));
			}
			elseif (stripos($search, 'ip:') === 0)
			{
				$query->where('a.ip_address = '.(int) substr($search, 3));
			}
			else
			{
				$search = $db->Quote('%'.$db->escape($search, true).'%');
				$query->where('(a.filepath LIKE '.$search.' OR uc.username LIKE '.$search.' OR uc.name LIKE '.$search.')');
			}
		}

		// Add the list ordering clause.
		$orderCol = $this->state->get('list.ordering', 'a.id');
		$orderDirn = $this->state->get('list.direction', 'asc');
		
		//sqlsrv change: since you can't order by an alias in sqlsvr
		if ($orderCol == 'username')
		{
			$orderCol = 'uc.username';
		}
		elseif ($orderCol == 'name')
		{
			$orderCol = 'uc.name';
		}
		$query->order($db->escape($orderCol . ' ' . $orderDirn));

		return $query;
	}
	
	/**
	 * Method to test whether a record can be deleted.
	 *
	 * @param   object  $record  A record object.
	 *
	 * @return  boolean  True if allowed to delete the record. Defaults to the permission for the component.
	 *
	 * @since   2.5
	 */
	protected function canDelete($record)
	{
		$user = JFactory::getUser();
		return $user->authorise('core.delete', $this->option);
	}

	/**
	 * Method to test whether a record can be deleted.
	 *
	 * @param   object  $record  A record object.
	 *
	 * @return  boolean  True if allowed to change the state of the record. Defaults to the permission for the component.
	 *
	 * @since   2.5
	 */
	protected function canEditState($record)
	{
		$user = JFactory::getUser();
		return $user->authorise('core.edit.state', $this->option);
	}

	/**
	 * Method to delete one or more records.
	 *
	 * @param   array  &$pks  An array of record primary keys.
	 *
	 * @return  boolean  True if successful, false if an error occurs.
	 */
	public function delete(&$pks)
	{
		// Initialise variables.
		$dispatcher = JDispatcher::getInstance();
		$pks = (array) $pks;
		$table = $this->getTable();

		// Include the content plugins for the on delete events.
		JPluginHelper::importPlugin('content');

		// Iterate the items to delete each one.
		foreach ($pks as $i => $pk)
		{
			if ($table->load($pk))
			{
				if ($this->canDelete($table))
				{
					$context = $this->option . '.' . $this->name;

					// Trigger the onContentBeforeDelete event.
					$result = $dispatcher->trigger($this->event_before_delete, array($context, $table));
					if (in_array(false, $result, true))
					{
						$this->setError($table->getError());
						return false;
					}

					if (!$table->delete($pk))
					{
						$this->setError($table->getError());
						return false;
					}

					// Trigger the onContentAfterDelete event.
					$dispatcher->trigger($this->event_after_delete, array($context, $table));
				}
				else
				{
					// Prune items that you can't change.
					unset($pks[$i]);
					$error = $this->getError();
					if ($error)
					{
						$this->setError($error);
					}
					else
					{
						$this->setError(JText::_('JLIB_APPLICATION_ERROR_DELETE_NOT_PERMITTED'));
					}
				}
			}
			else
			{
				$this->setError($table->getError());
				return false;
			}
		}

		// Clear the component's cache
		$this->cleanCache();

		return true;
	}

	/**
	 * Method to purge the logs, deleting all log entries.
	 */
	public function purge()
	{
		$db = $this->getDbo();

		// Truncate the links table.
		$db->truncateTable('#__eflp_logs');

		return true;
	}
}