2014-04-08 12:32:59 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-12-01 07:21:47 +00:00
|
|
|
* @package Joomla.Administrator
|
|
|
|
* @subpackage Weblinks
|
2014-04-08 12:32:59 +00:00
|
|
|
*
|
2017-02-13 16:20:35 +00:00
|
|
|
* @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
|
2014-04-08 12:32:59 +00:00
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2018-02-19 09:44:55 +00:00
|
|
|
namespace Joomla\Component\Weblinks\Administrator\Model;
|
|
|
|
|
2014-04-08 12:32:59 +00:00
|
|
|
defined('_JEXEC') or die;
|
|
|
|
|
2021-06-19 14:17:09 +00:00
|
|
|
use Joomla\CMS\Component\ComponentHelper;
|
|
|
|
use Joomla\CMS\Factory;
|
2021-07-16 16:02:40 +00:00
|
|
|
use Joomla\CMS\Language\Associations;
|
2018-02-19 09:44:55 +00:00
|
|
|
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
|
2021-06-19 14:17:09 +00:00
|
|
|
use Joomla\CMS\MVC\Model\ListModel;
|
2021-07-16 16:02:40 +00:00
|
|
|
use Joomla\Database\ParameterType;
|
2018-02-19 09:44:55 +00:00
|
|
|
|
2014-04-08 12:32:59 +00:00
|
|
|
/**
|
|
|
|
* Methods supporting a list of weblink records.
|
|
|
|
*
|
2014-12-04 19:43:05 +00:00
|
|
|
* @since 1.6
|
2014-04-08 12:32:59 +00:00
|
|
|
*/
|
2018-02-19 09:44:55 +00:00
|
|
|
class WeblinksModel extends ListModel
|
2014-04-08 12:32:59 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2021-06-19 14:17:09 +00:00
|
|
|
* @param array $config An optional associative array of configuration settings.
|
2018-02-19 09:44:55 +00:00
|
|
|
* @param MVCFactoryInterface $factory The factory.
|
2015-02-18 01:20:59 +00:00
|
|
|
*
|
|
|
|
* @see JControllerLegacy
|
2014-04-08 12:32:59 +00:00
|
|
|
* @since 1.6
|
|
|
|
*/
|
2018-02-19 09:44:55 +00:00
|
|
|
public function __construct($config = array(), MVCFactoryInterface $factory = null)
|
2014-04-08 12:32:59 +00:00
|
|
|
{
|
|
|
|
if (empty($config['filter_fields']))
|
|
|
|
{
|
|
|
|
$config['filter_fields'] = array(
|
|
|
|
'id', 'a.id',
|
|
|
|
'title', 'a.title',
|
|
|
|
'alias', 'a.alias',
|
|
|
|
'checked_out', 'a.checked_out',
|
|
|
|
'checked_out_time', 'a.checked_out_time',
|
2016-06-28 09:45:18 +00:00
|
|
|
'catid', 'a.catid', 'category_id',
|
|
|
|
'c.title', 'category_title',
|
|
|
|
'state', 'a.state', 'published',
|
|
|
|
'access', 'a.access',
|
|
|
|
'ag.title', 'access_level',
|
2014-04-08 12:32:59 +00:00
|
|
|
'created', 'a.created',
|
|
|
|
'created_by', 'a.created_by',
|
|
|
|
'ordering', 'a.ordering',
|
|
|
|
'featured', 'a.featured',
|
|
|
|
'language', 'a.language',
|
2016-06-28 09:45:18 +00:00
|
|
|
'l.title', 'language_title',
|
2014-04-08 12:32:59 +00:00
|
|
|
'hits', 'a.hits',
|
|
|
|
'publish_up', 'a.publish_up',
|
|
|
|
'publish_down', 'a.publish_down',
|
|
|
|
'url', 'a.url',
|
2016-06-28 09:45:18 +00:00
|
|
|
'tag',
|
|
|
|
'level', 'c.level',
|
2014-04-08 12:32:59 +00:00
|
|
|
);
|
2017-05-16 06:03:22 +00:00
|
|
|
|
2021-07-17 04:45:13 +00:00
|
|
|
$assoc = Associations::isEnabled();
|
2017-05-21 16:59:44 +00:00
|
|
|
|
2017-05-16 06:03:22 +00:00
|
|
|
if ($assoc)
|
|
|
|
{
|
|
|
|
$config['filter_fields'][] = 'association';
|
|
|
|
}
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 09:44:55 +00:00
|
|
|
parent::__construct($config, $factory);
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to auto-populate the model state.
|
|
|
|
*
|
2016-06-28 21:05:31 +00:00
|
|
|
* @param string $ordering An optional ordering field.
|
|
|
|
* @param string $direction An optional direction (asc|desc).
|
|
|
|
*
|
2015-02-18 01:20:59 +00:00
|
|
|
* @return void
|
2014-04-08 12:32:59 +00:00
|
|
|
*
|
2015-02-18 01:20:59 +00:00
|
|
|
* @note Calling getState in this method will result in recursion.
|
2014-04-08 12:32:59 +00:00
|
|
|
* @since 1.6
|
|
|
|
*/
|
2016-06-28 09:45:18 +00:00
|
|
|
protected function populateState($ordering = 'a.title', $direction = 'asc')
|
2014-04-08 12:32:59 +00:00
|
|
|
{
|
2021-06-19 14:17:09 +00:00
|
|
|
$app = Factory::getApplication();
|
2017-05-16 06:03:22 +00:00
|
|
|
|
|
|
|
$forcedLanguage = $app->input->get('forcedLanguage', '', 'cmd');
|
|
|
|
|
|
|
|
// Adjust the context to support modal layouts.
|
|
|
|
if ($layout = $app->input->get('layout'))
|
|
|
|
{
|
|
|
|
$this->context .= '.' . $layout;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust the context to support forced languages.
|
|
|
|
if ($forcedLanguage)
|
|
|
|
{
|
|
|
|
$this->context .= '.' . $forcedLanguage;
|
|
|
|
}
|
|
|
|
|
2014-04-08 12:32:59 +00:00
|
|
|
// Load the parameters.
|
2021-06-19 14:17:09 +00:00
|
|
|
$params = ComponentHelper::getParams('com_weblinks');
|
2014-04-08 12:32:59 +00:00
|
|
|
$this->setState('params', $params);
|
|
|
|
|
2017-05-16 06:03:22 +00:00
|
|
|
// Force a language.
|
|
|
|
if (!empty($forcedLanguage))
|
|
|
|
{
|
|
|
|
$this->setState('filter.language', $forcedLanguage);
|
|
|
|
}
|
|
|
|
|
2014-04-08 12:32:59 +00:00
|
|
|
// List state information.
|
2016-06-28 09:45:18 +00:00
|
|
|
parent::populateState($ordering, $direction);
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to get a store id based on model configuration state.
|
|
|
|
*
|
|
|
|
* This is necessary because the model is used by the component and
|
|
|
|
* different modules that might need different sets of data or different
|
|
|
|
* ordering requirements.
|
|
|
|
*
|
2015-02-18 01:20:59 +00:00
|
|
|
* @param string $id A prefix for the store id.
|
|
|
|
*
|
2014-04-08 12:32:59 +00:00
|
|
|
* @return string A store id.
|
2015-02-18 01:20:59 +00:00
|
|
|
*
|
2014-04-08 12:32:59 +00:00
|
|
|
* @since 1.6
|
|
|
|
*/
|
|
|
|
protected function getStoreId($id = '')
|
|
|
|
{
|
|
|
|
// Compile the store id.
|
|
|
|
$id .= ':' . $this->getState('filter.search');
|
|
|
|
$id .= ':' . $this->getState('filter.access');
|
2016-06-28 09:45:18 +00:00
|
|
|
$id .= ':' . $this->getState('filter.published');
|
2014-04-08 12:32:59 +00:00
|
|
|
$id .= ':' . $this->getState('filter.category_id');
|
|
|
|
$id .= ':' . $this->getState('filter.language');
|
2016-06-28 09:45:18 +00:00
|
|
|
$id .= ':' . $this->getState('filter.tag');
|
|
|
|
$id .= ':' . $this->getState('filter.level');
|
2014-04-08 12:32:59 +00:00
|
|
|
|
|
|
|
return parent::getStoreId($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build an SQL query to load the list data.
|
|
|
|
*
|
2021-06-19 14:17:09 +00:00
|
|
|
* @return \JDatabaseQuery
|
2015-02-18 01:20:59 +00:00
|
|
|
*
|
2014-04-08 12:32:59 +00:00
|
|
|
* @since 1.6
|
|
|
|
*/
|
|
|
|
protected function getListQuery()
|
|
|
|
{
|
|
|
|
// Create a new query object.
|
2021-06-19 14:17:09 +00:00
|
|
|
$db = $this->getDbo();
|
2014-04-08 12:32:59 +00:00
|
|
|
$query = $db->getQuery(true);
|
2021-07-17 04:49:07 +00:00
|
|
|
$user = Factory::getApplication()->getIdentity();
|
2014-04-08 12:32:59 +00:00
|
|
|
|
|
|
|
// Select the required fields from the table.
|
|
|
|
$query->select(
|
|
|
|
$this->getState(
|
|
|
|
'list.select',
|
2017-05-16 06:03:22 +00:00
|
|
|
'a.id, a.title, a.alias, a.checked_out, a.checked_out_time, a.catid, a.created, a.created_by, ' .
|
2015-02-18 01:20:59 +00:00
|
|
|
'a.hits, a.state, a.access, a.ordering, a.language, a.publish_up, a.publish_down'
|
2014-04-08 12:32:59 +00:00
|
|
|
)
|
|
|
|
);
|
2016-06-28 09:45:18 +00:00
|
|
|
$query->from($db->quoteName('#__weblinks', 'a'));
|
2014-04-08 12:32:59 +00:00
|
|
|
|
|
|
|
// Join over the language
|
2016-06-28 09:45:18 +00:00
|
|
|
$query->select($db->quoteName('l.title', 'language_title'))
|
|
|
|
->select($db->quoteName('l.image', 'language_image'))
|
|
|
|
->join('LEFT', $db->quoteName('#__languages', 'l') . ' ON ' . $db->qn('l.lang_code') . ' = ' . $db->qn('a.language'));
|
2014-04-08 12:32:59 +00:00
|
|
|
|
|
|
|
// Join over the users for the checked out user.
|
2016-06-28 09:45:18 +00:00
|
|
|
$query->select($db->quoteName('uc.name', 'editor'))
|
|
|
|
->join('LEFT', $db->quoteName('#__users', 'uc') . ' ON ' . $db->qn('uc.id') . ' = ' . $db->qn('a.checked_out'));
|
2014-04-08 12:32:59 +00:00
|
|
|
|
|
|
|
// Join over the asset groups.
|
2016-06-28 09:45:18 +00:00
|
|
|
$query->select($db->quoteName('ag.title', 'access_level'))
|
|
|
|
->join('LEFT', $db->quoteName('#__viewlevels', 'ag') . ' ON ' . $db->qn('ag.id') . ' = ' . $db->qn('a.access'));
|
2014-04-08 12:32:59 +00:00
|
|
|
|
|
|
|
// Join over the categories.
|
|
|
|
$query->select('c.title AS category_title')
|
2016-06-28 09:45:18 +00:00
|
|
|
->join('LEFT', $db->quoteName('#__categories', 'c') . ' ON ' . $db->qn('c.id') . ' = ' . $db->qn('a.catid'));
|
2014-04-08 12:32:59 +00:00
|
|
|
|
2017-05-16 06:03:22 +00:00
|
|
|
// Join over the associations.
|
2021-07-16 16:02:40 +00:00
|
|
|
$assoc = Associations::isEnabled();
|
2017-05-16 06:03:22 +00:00
|
|
|
|
|
|
|
if ($assoc)
|
|
|
|
{
|
|
|
|
$query->select('COUNT(asso2.id)>1 AS association')
|
|
|
|
->join('LEFT', $db->quoteName('#__associations', 'asso') . ' ON asso.id = a.id AND asso.context = ' . $db->quote('com_weblinks.item'))
|
|
|
|
->join('LEFT', $db->quoteName('#__associations', 'asso2') . ' ON asso2.key = asso.key')
|
|
|
|
->group('a.id, l.title, l.image, uc.name, ag.title, c.title');
|
|
|
|
}
|
|
|
|
|
2014-04-08 12:32:59 +00:00
|
|
|
// Filter by access level.
|
|
|
|
if ($access = $this->getState('filter.access'))
|
|
|
|
{
|
2021-07-16 16:02:40 +00:00
|
|
|
$query->where($db->quoteName('a.access') . ' = :access')
|
|
|
|
->bind(':access', $access, ParameterType::INTEGER);
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implement View Level Access
|
|
|
|
if (!$user->authorise('core.admin'))
|
|
|
|
{
|
2021-07-16 16:02:40 +00:00
|
|
|
$query->whereIn($db->quoteName('a.access'), $user->getAuthorisedViewLevels());
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filter by published state
|
2016-06-28 09:45:18 +00:00
|
|
|
$published = $this->getState('filter.published');
|
2015-02-18 01:20:59 +00:00
|
|
|
|
2014-04-08 12:32:59 +00:00
|
|
|
if (is_numeric($published))
|
|
|
|
{
|
2021-07-16 16:02:40 +00:00
|
|
|
$query->where($db->quoteName('a.state') . ' = :state')
|
|
|
|
->bind(':state', $published, ParameterType::INTEGER);
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
elseif ($published === '')
|
|
|
|
{
|
2021-07-16 16:02:40 +00:00
|
|
|
$query->whereIn($db->quoteName('a.state'), [0, 1]);
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filter by category.
|
|
|
|
$categoryId = $this->getState('filter.category_id');
|
2015-02-18 01:20:59 +00:00
|
|
|
|
2014-04-08 12:32:59 +00:00
|
|
|
if (is_numeric($categoryId))
|
|
|
|
{
|
2021-07-16 16:02:40 +00:00
|
|
|
$query->where($db->quoteName('a.catid') . ' = :catid')
|
|
|
|
->bind(':catid', $categoryId, ParameterType::INTEGER);
|
2016-06-28 09:45:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filter on the level.
|
|
|
|
if ($level = $this->getState('filter.level'))
|
|
|
|
{
|
2021-07-16 16:02:40 +00:00
|
|
|
$query->where($db->quoteName('c.level') . ' <= :level')
|
|
|
|
->bind(':level', $level, ParameterType::INTEGER);
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filter by search in title
|
|
|
|
$search = $this->getState('filter.search');
|
2015-02-18 01:20:59 +00:00
|
|
|
|
2014-04-08 12:32:59 +00:00
|
|
|
if (!empty($search))
|
|
|
|
{
|
|
|
|
if (stripos($search, 'id:') === 0)
|
|
|
|
{
|
2021-07-16 16:02:40 +00:00
|
|
|
$query->where($db->quoteName('a.id') . ' = :id')
|
|
|
|
->bind(':id', substr($search, 3), ParameterType::INTEGER);
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-16 16:02:40 +00:00
|
|
|
$search = '%' . str_replace(' ', '%', $db->escape(trim($search), true) . '%');
|
2021-07-17 10:40:29 +00:00
|
|
|
$query->where('(' . $db->quoteName('a.title') . ' LIKE :title OR ' . $db->quoteName('a.alias') . ' LIKE :alias)')
|
|
|
|
->bind(':title', $search)
|
|
|
|
->bind(':alias', $search);
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter on the language.
|
|
|
|
if ($language = $this->getState('filter.language'))
|
|
|
|
{
|
2021-07-16 16:02:40 +00:00
|
|
|
$query->where($db->quoteName('a.language') . ' = :language')
|
|
|
|
->bind(':language', $language);
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$tagId = $this->getState('filter.tag');
|
2015-02-18 01:20:59 +00:00
|
|
|
|
2014-04-08 12:32:59 +00:00
|
|
|
// Filter by a single tag.
|
|
|
|
if (is_numeric($tagId))
|
|
|
|
{
|
2021-07-16 16:02:40 +00:00
|
|
|
$query->where($db->quoteName('tagmap.tag_id') . ' = :tagId')
|
|
|
|
->bind(':tagId', $tagId, ParameterType::INTEGER)
|
2014-04-08 12:32:59 +00:00
|
|
|
->join(
|
|
|
|
'LEFT', $db->quoteName('#__contentitem_tag_map', 'tagmap')
|
|
|
|
. ' ON ' . $db->quoteName('tagmap.content_item_id') . ' = ' . $db->quoteName('a.id')
|
|
|
|
. ' AND ' . $db->quoteName('tagmap.type_alias') . ' = ' . $db->quote('com_weblinks.weblink')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the list ordering clause.
|
2016-06-28 09:45:18 +00:00
|
|
|
$orderCol = $this->state->get('list.ordering', 'a.title');
|
|
|
|
$orderDirn = $this->state->get('list.direction', 'ASC');
|
2015-02-18 01:20:59 +00:00
|
|
|
|
2014-04-08 12:32:59 +00:00
|
|
|
if ($orderCol == 'a.ordering' || $orderCol == 'category_title')
|
|
|
|
{
|
|
|
|
$orderCol = 'c.title ' . $orderDirn . ', a.ordering';
|
|
|
|
}
|
2015-02-18 01:20:59 +00:00
|
|
|
|
2014-04-08 12:32:59 +00:00
|
|
|
$query->order($db->escape($orderCol . ' ' . $orderDirn));
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
}
|