30
1
mirror of https://github.com/joomla-extensions/weblinks.git synced 2024-09-27 20:49:05 +00:00

Correcting weblink frontend model to adapt to the new display

This commit is contained in:
Jean-Marie Simonet 2017-06-10 08:33:20 +02:00
parent 58fa74a1a2
commit 725464800f

View File

@ -10,6 +10,7 @@
defined('_JEXEC') or die; defined('_JEXEC') or die;
use Joomla\Utilities\ArrayHelper; use Joomla\Utilities\ArrayHelper;
use Joomla\Registry\Registry;
JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/tables'); JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/tables');
@ -38,15 +39,25 @@ class WeblinksModelWeblink extends JModelItem
*/ */
protected function populateState() protected function populateState()
{ {
$app = JFactory::getApplication(); $app = JFactory::getApplication('site');
$params = $app->getParams();
// Load the object state. // Load the object state.
$id = $app->input->getInt('id'); $pk = $app->input->getInt('id');
$this->setState('weblink.id', $id); $this->setState('weblink.id', $pk);
// Load the parameters. // Load the parameters.
$params = $app->getParams();
$this->setState('params', $params); $this->setState('params', $params);
$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());
} }
/** /**
@ -56,43 +67,109 @@ class WeblinksModelWeblink extends JModelItem
* *
* @return mixed Object on success, false on failure. * @return mixed Object on success, false on failure.
*/ */
public function getItem($id = null) public function getItem($pk = null)
{ {
$user = JFactory::getUser();
$pk = (!empty($pk)) ? $pk : (int) $this->getState('weblink.id');
if ($this->_item === null) if ($this->_item === null)
{ {
$this->_item = false; $this->_item = array();
if (empty($id))
{
$id = $this->getState('weblink.id');
} }
// Get a level row instance. if (!isset($this->_item[$pk]))
$table = JTable::getInstance('Weblink', 'WeblinksTable'); {
try
{
$db = $this->getDbo();
$query = $db->getQuery(true)
->select($this->getState('item.select', 'a.*'))
->from('#__weblinks AS a')
->where('a.id = ' . (int) $pk);
// Attempt to load the row. // Join on category table.
if ($table->load($id)) $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'))
{ {
// Check published state. $query->where('a.language in (' . $db->quote(JFactory::getLanguage()->getTag()) . ',' . $db->quote('*') . ')');
if ($published = $this->getState('filter.published')) }
// 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');
if ((!$user->authorise('core.edit.state', 'com_weblinks')) && (!$user->authorise('core.edit', 'com_weblinks')))
{ {
if ($table->state != $published) // 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))
{ {
return $this->_item; $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
{
// 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));
}
$this->_item[$pk] = $data;
}
catch (Exception $e)
{
$this->setError($e);
$this->_item[$pk] = false;
} }
} }
// Convert the JTable to a clean JObject. return $this->_item[$pk];
$properties = $table->getProperties(1);
$this->_item = ArrayHelper::toObject($properties, 'JObject');
}
elseif ($error = $table->getError())
{
$this->setError($error);
}
}
return $this->_item;
} }
/** /**
@ -118,13 +195,13 @@ class WeblinksModelWeblink extends JModelItem
* *
* @return boolean True on success * @return boolean True on success
*/ */
public function hit($id = null) public function hit($pk = null)
{ {
if (empty($id)) if (empty($pk))
{ {
$id = $this->getState('weblink.id'); $pk = $this->getState('weblink.id');
} }
return $this->getTable('Weblink', 'WeblinksTable')->hit($id); return $this->getTable('Weblink', 'WeblinksTable')->hit($pk);
} }
} }