2014-04-08 12:32:59 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2017-05-16 06:03:22 +00:00
|
|
|
* @package Joomla.Site
|
|
|
|
* @subpackage com_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
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined('_JEXEC') or die;
|
|
|
|
|
2016-06-25 21:23:16 +00:00
|
|
|
use Joomla\Utilities\ArrayHelper;
|
2017-06-10 06:33:20 +00:00
|
|
|
use Joomla\Registry\Registry;
|
2016-06-25 21:23:16 +00:00
|
|
|
|
2014-04-08 12:32:59 +00:00
|
|
|
JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/tables');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Weblinks Component Model for a Weblink record
|
|
|
|
*
|
2014-12-04 19:43:05 +00:00
|
|
|
* @since 1.5
|
2014-04-08 12:32:59 +00:00
|
|
|
*/
|
|
|
|
class WeblinksModelWeblink extends JModelItem
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Model context string.
|
|
|
|
*
|
2015-02-18 01:20:59 +00:00
|
|
|
* @var string
|
2014-04-08 12:32:59 +00:00
|
|
|
*/
|
|
|
|
protected $_context = 'com_weblinks.weblink';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to auto-populate the model state.
|
|
|
|
*
|
|
|
|
* Note. Calling getState in this method will result in recursion.
|
|
|
|
*
|
2016-07-08 18:55:07 +00:00
|
|
|
* @return void
|
|
|
|
*
|
2014-04-08 12:32:59 +00:00
|
|
|
* @since 1.6
|
|
|
|
*/
|
|
|
|
protected function populateState()
|
|
|
|
{
|
2017-06-10 06:33:20 +00:00
|
|
|
$app = JFactory::getApplication('site');
|
2014-04-08 12:32:59 +00:00
|
|
|
|
|
|
|
// Load the object state.
|
2017-06-10 06:33:20 +00:00
|
|
|
$pk = $app->input->getInt('id');
|
|
|
|
$this->setState('weblink.id', $pk);
|
2014-04-08 12:32:59 +00:00
|
|
|
|
|
|
|
// Load the parameters.
|
2017-06-10 06:33:20 +00:00
|
|
|
$params = $app->getParams();
|
2014-04-08 12:32:59 +00:00
|
|
|
$this->setState('params', $params);
|
2017-06-10 06:33:20 +00:00
|
|
|
|
|
|
|
$user = JFactory::getUser();
|
|
|
|
|
|
|
|
if ((!$user->authorise('core.edit.state', 'com_weblinks')) && (!$user->authorise('core.edit', 'com_weblinks')))
|
|
|
|
{
|
|
|
|
$this->setState('filter.published', 1);
|
|
|
|
$this->setState('filter.archived', 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->setState('filter.language', JLanguageMultilang::isEnabled());
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to get an object.
|
|
|
|
*
|
2019-08-12 23:15:35 +00:00
|
|
|
* @param integer $pk The id of the object to get.
|
2014-04-08 12:32:59 +00:00
|
|
|
*
|
|
|
|
* @return mixed Object on success, false on failure.
|
|
|
|
*/
|
2017-06-10 06:33:20 +00:00
|
|
|
public function getItem($pk = null)
|
2014-04-08 12:32:59 +00:00
|
|
|
{
|
2017-06-10 06:33:20 +00:00
|
|
|
$user = JFactory::getUser();
|
|
|
|
|
|
|
|
$pk = (!empty($pk)) ? $pk : (int) $this->getState('weblink.id');
|
|
|
|
|
2014-04-08 12:32:59 +00:00
|
|
|
if ($this->_item === null)
|
|
|
|
{
|
2017-06-10 06:33:20 +00:00
|
|
|
$this->_item = array();
|
|
|
|
}
|
2014-04-08 12:32:59 +00:00
|
|
|
|
2017-06-10 06:33:20 +00:00
|
|
|
if (!isset($this->_item[$pk]))
|
|
|
|
{
|
|
|
|
try
|
2014-04-08 12:32:59 +00:00
|
|
|
{
|
2017-06-10 06:33:20 +00:00
|
|
|
$db = $this->getDbo();
|
|
|
|
$query = $db->getQuery(true)
|
|
|
|
->select($this->getState('item.select', 'a.*'))
|
|
|
|
->from('#__weblinks AS a')
|
|
|
|
->where('a.id = ' . (int) $pk);
|
|
|
|
|
|
|
|
// Join on category table.
|
|
|
|
$query->select('c.title AS category_title, c.alias AS category_alias, c.access AS category_access')
|
|
|
|
->innerJoin('#__categories AS c on c.id = a.catid')
|
|
|
|
->where('c.published > 0');
|
|
|
|
|
|
|
|
// Join on user table.
|
|
|
|
$query->select('u.name AS author')
|
|
|
|
->join('LEFT', '#__users AS u on u.id = a.created_by');
|
|
|
|
|
|
|
|
// Filter by language
|
|
|
|
if ($this->getState('filter.language'))
|
|
|
|
{
|
|
|
|
$query->where('a.language in (' . $db->quote(JFactory::getLanguage()->getTag()) . ',' . $db->quote('*') . ')');
|
|
|
|
}
|
2014-04-08 12:32:59 +00:00
|
|
|
|
2017-06-10 06:33:20 +00:00
|
|
|
// Join over the categories to get parent category titles
|
|
|
|
$query->select('parent.title as parent_title, parent.id as parent_id, parent.path as parent_route, parent.alias as parent_alias')
|
|
|
|
->join('LEFT', '#__categories as parent ON parent.id = c.parent_id');
|
2014-04-08 12:32:59 +00:00
|
|
|
|
2017-06-10 06:33:20 +00:00
|
|
|
if ((!$user->authorise('core.edit.state', 'com_weblinks')) && (!$user->authorise('core.edit', 'com_weblinks')))
|
|
|
|
{
|
|
|
|
// Filter by start and end dates.
|
|
|
|
$nullDate = $db->quote($db->getNullDate());
|
|
|
|
$date = JFactory::getDate();
|
|
|
|
|
|
|
|
$nowDate = $db->quote($date->toSql());
|
|
|
|
|
|
|
|
$query->where('(a.publish_up = ' . $nullDate . ' OR a.publish_up <= ' . $nowDate . ')')
|
|
|
|
->where('(a.publish_down = ' . $nullDate . ' OR a.publish_down >= ' . $nowDate . ')');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter by published state.
|
|
|
|
$published = $this->getState('filter.published');
|
|
|
|
$archived = $this->getState('filter.archived');
|
|
|
|
|
|
|
|
if (is_numeric($published))
|
|
|
|
{
|
|
|
|
$query->where('(a.state = ' . (int) $published . ' OR a.state =' . (int) $archived . ')');
|
|
|
|
}
|
|
|
|
|
|
|
|
$db->setQuery($query);
|
|
|
|
|
|
|
|
$data = $db->loadObject();
|
|
|
|
|
|
|
|
if (empty($data))
|
|
|
|
{
|
|
|
|
JError::raiseError(404, JText::_('COM_WEBLINKS_ERROR_WEBLINK_NOT_FOUND'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for published state if filter set.
|
|
|
|
if ((is_numeric($published) || is_numeric($archived)) && (($data->state != $published) && ($data->state != $archived)))
|
|
|
|
{
|
|
|
|
JError::raiseError(404, JText::_('COM_WEBLINKS_ERROR_WEBLINK_NOT_FOUND'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert parameter fields to objects.
|
|
|
|
$data->params = new Registry($data->params);
|
|
|
|
$data->metadata = new Registry($data->metadata);
|
|
|
|
|
|
|
|
// Compute access permissions.
|
|
|
|
if ($access = $this->getState('filter.access'))
|
|
|
|
{
|
|
|
|
// If the access filter has been set, we already know this user can view.
|
|
|
|
$data->params->set('access-view', true);
|
|
|
|
}
|
|
|
|
else
|
2014-04-08 12:32:59 +00:00
|
|
|
{
|
2017-06-10 06:33:20 +00:00
|
|
|
// If no access filter is set, the layout takes some responsibility for display of limited information.
|
|
|
|
$groups = $user->getAuthorisedViewLevels();
|
|
|
|
$data->params->set('access-view', in_array($data->access, $groups) && in_array($data->category_access, $groups));
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
|
2017-06-10 06:33:20 +00:00
|
|
|
$this->_item[$pk] = $data;
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
2017-06-10 06:33:20 +00:00
|
|
|
catch (Exception $e)
|
2014-04-08 12:32:59 +00:00
|
|
|
{
|
2017-06-10 06:33:20 +00:00
|
|
|
$this->setError($e);
|
|
|
|
$this->_item[$pk] = false;
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-10 06:33:20 +00:00
|
|
|
return $this->_item[$pk];
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a reference to the a Table object, always creating it.
|
|
|
|
*
|
2016-07-08 18:55:07 +00:00
|
|
|
* @param string $type The table type to instantiate
|
|
|
|
* @param string $prefix A prefix for the table class name. Optional.
|
|
|
|
* @param array $config Configuration array for model. Optional.
|
2015-02-18 01:20:59 +00:00
|
|
|
*
|
2016-06-25 21:23:16 +00:00
|
|
|
* @return JTable A database object
|
2015-02-18 01:20:59 +00:00
|
|
|
*
|
2016-06-25 21:23:16 +00:00
|
|
|
* @since 1.6
|
2014-04-08 12:32:59 +00:00
|
|
|
*/
|
|
|
|
public function getTable($type = 'Weblink', $prefix = 'WeblinksTable', $config = array())
|
|
|
|
{
|
|
|
|
return JTable::getInstance($type, $prefix, $config);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to increment the hit counter for the weblink
|
|
|
|
*
|
2019-08-12 23:15:35 +00:00
|
|
|
* @param integer $pk Optional ID of the weblink.
|
2014-04-08 12:32:59 +00:00
|
|
|
*
|
|
|
|
* @return boolean True on success
|
|
|
|
*/
|
2017-06-10 06:33:20 +00:00
|
|
|
public function hit($pk = null)
|
2014-04-08 12:32:59 +00:00
|
|
|
{
|
2017-06-10 06:33:20 +00:00
|
|
|
if (empty($pk))
|
2014-04-08 12:32:59 +00:00
|
|
|
{
|
2017-06-10 06:33:20 +00:00
|
|
|
$pk = $this->getState('weblink.id');
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
|
2017-06-10 06:33:20 +00:00
|
|
|
return $this->getTable('Weblink', 'WeblinksTable')->hit($pk);
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
}
|