2014-04-08 12:32:59 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @package Joomla.Site
|
|
|
|
* @subpackage com_weblinks
|
|
|
|
*
|
2015-02-18 00:35:48 +00:00
|
|
|
* @copyright Copyright (C) 2005 - 2015 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;
|
|
|
|
|
|
|
|
/**
|
2014-12-04 19:43:05 +00:00
|
|
|
* Weblinks Component Route Helper.
|
2014-04-08 12:32:59 +00:00
|
|
|
*
|
2014-12-04 19:43:05 +00:00
|
|
|
* @since 1.5
|
2014-04-08 12:32:59 +00:00
|
|
|
*/
|
|
|
|
abstract class WeblinksHelperRoute
|
|
|
|
{
|
|
|
|
protected static $lookup;
|
|
|
|
|
|
|
|
protected static $lang_lookup = array();
|
|
|
|
|
|
|
|
/**
|
2016-07-02 11:02:00 +00:00
|
|
|
* Get the route of the weblink
|
|
|
|
*
|
|
|
|
* @param integer $id Web link ID
|
|
|
|
* @param integer $catid Category ID
|
|
|
|
* @param string $language Language
|
2015-02-18 01:20:59 +00:00
|
|
|
*
|
|
|
|
* @return string
|
2014-04-08 12:32:59 +00:00
|
|
|
*/
|
|
|
|
public static function getWeblinkRoute($id, $catid, $language = 0)
|
|
|
|
{
|
2016-07-02 11:02:00 +00:00
|
|
|
$needles = array('weblink' => array((int) $id));
|
2014-04-08 12:32:59 +00:00
|
|
|
|
2015-02-18 01:20:59 +00:00
|
|
|
// Create the link
|
2016-07-02 11:02:00 +00:00
|
|
|
$link = 'index.php?option=com_weblinks&view=weblink&id=' . $id;
|
2014-04-08 12:32:59 +00:00
|
|
|
|
|
|
|
if ($catid > 1)
|
|
|
|
{
|
|
|
|
$categories = JCategories::getInstance('Weblinks');
|
|
|
|
$category = $categories->get($catid);
|
|
|
|
|
|
|
|
if ($category)
|
|
|
|
{
|
|
|
|
$needles['category'] = array_reverse($category->getPath());
|
|
|
|
$needles['categories'] = $needles['category'];
|
2016-08-01 17:47:09 +00:00
|
|
|
$link .= '&catid=' . $catid;
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($language && $language != "*" && JLanguageMultilang::isEnabled())
|
|
|
|
{
|
|
|
|
self::buildLanguageLookup();
|
|
|
|
|
|
|
|
if (isset(self::$lang_lookup[$language]))
|
|
|
|
{
|
|
|
|
$link .= '&lang=' . self::$lang_lookup[$language];
|
|
|
|
$needles['language'] = $language;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($item = self::_findItem($needles))
|
|
|
|
{
|
2016-08-01 17:47:09 +00:00
|
|
|
$link .= '&Itemid=' . $item;
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $link;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-02 11:02:00 +00:00
|
|
|
* Ge the form route
|
|
|
|
*
|
2015-02-18 01:20:59 +00:00
|
|
|
* @param integer $id The id of the weblink.
|
|
|
|
* @param string $return The return page variable.
|
|
|
|
*
|
|
|
|
* @return string
|
2014-04-08 12:32:59 +00:00
|
|
|
*/
|
|
|
|
public static function getFormRoute($id, $return = null)
|
|
|
|
{
|
|
|
|
// Create the link.
|
|
|
|
if ($id)
|
|
|
|
{
|
2016-07-02 11:02:00 +00:00
|
|
|
$link = 'index.php?option=com_weblinks&task=weblink.edit&w_id=' . $id;
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$link = 'index.php?option=com_weblinks&task=weblink.add&w_id=0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($return)
|
|
|
|
{
|
2016-07-02 11:02:00 +00:00
|
|
|
$link .= '&return=' . $return;
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $link;
|
|
|
|
}
|
|
|
|
|
2015-02-18 01:20:59 +00:00
|
|
|
/**
|
2016-07-02 11:02:00 +00:00
|
|
|
* Get the Category Route
|
|
|
|
*
|
2015-02-18 01:20:59 +00:00
|
|
|
* @param JCategoryNode|string|integer $catid JCategoryNode object or category ID
|
|
|
|
* @param integer $language Language code
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-04-08 12:32:59 +00:00
|
|
|
public static function getCategoryRoute($catid, $language = 0)
|
|
|
|
{
|
|
|
|
if ($catid instanceof JCategoryNode)
|
|
|
|
{
|
|
|
|
$id = $catid->id;
|
|
|
|
$category = $catid;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$id = (int) $catid;
|
|
|
|
$category = JCategories::getInstance('Weblinks')->get($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($id < 1 || !($category instanceof JCategoryNode))
|
|
|
|
{
|
|
|
|
$link = '';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$needles = array();
|
|
|
|
|
|
|
|
// Create the link
|
2016-07-02 11:02:00 +00:00
|
|
|
$link = 'index.php?option=com_weblinks&view=category&id=' . $id;
|
2014-04-08 12:32:59 +00:00
|
|
|
|
|
|
|
$catids = array_reverse($category->getPath());
|
|
|
|
$needles['category'] = $catids;
|
|
|
|
$needles['categories'] = $catids;
|
|
|
|
|
|
|
|
if ($language && $language != "*" && JLanguageMultilang::isEnabled())
|
|
|
|
{
|
|
|
|
self::buildLanguageLookup();
|
|
|
|
|
|
|
|
if (isset(self::$lang_lookup[$language]))
|
|
|
|
{
|
|
|
|
$link .= '&lang=' . self::$lang_lookup[$language];
|
|
|
|
$needles['language'] = $language;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($item = self::_findItem($needles))
|
|
|
|
{
|
2016-07-02 11:02:00 +00:00
|
|
|
$link .= '&Itemid=' . $item;
|
2014-04-08 12:32:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $link;
|
|
|
|
}
|
|
|
|
|
2015-02-18 01:20:59 +00:00
|
|
|
/**
|
2016-07-02 11:02:00 +00:00
|
|
|
* Do a language lookup
|
|
|
|
*
|
2016-08-01 17:47:09 +00:00
|
|
|
* @return void
|
2015-02-18 01:20:59 +00:00
|
|
|
*/
|
2014-04-08 12:32:59 +00:00
|
|
|
protected static function buildLanguageLookup()
|
|
|
|
{
|
|
|
|
if (count(self::$lang_lookup) == 0)
|
|
|
|
{
|
|
|
|
$db = JFactory::getDbo();
|
|
|
|
$query = $db->getQuery(true)
|
|
|
|
->select('a.sef AS sef')
|
|
|
|
->select('a.lang_code AS lang_code')
|
|
|
|
->from('#__languages AS a');
|
|
|
|
|
|
|
|
$db->setQuery($query);
|
|
|
|
$langs = $db->loadObjectList();
|
|
|
|
|
|
|
|
foreach ($langs as $lang)
|
|
|
|
{
|
|
|
|
self::$lang_lookup[$lang->lang_code] = $lang->sef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-02 11:02:00 +00:00
|
|
|
/**
|
|
|
|
* Find items per given $needles
|
|
|
|
*
|
2016-08-02 12:33:26 +00:00
|
|
|
* @param array $needles A given array of needles to find
|
2016-07-02 11:02:00 +00:00
|
|
|
*
|
2016-08-01 17:47:09 +00:00
|
|
|
* @return void
|
2016-07-02 11:02:00 +00:00
|
|
|
*/
|
2014-04-08 12:32:59 +00:00
|
|
|
protected static function _findItem($needles = null)
|
|
|
|
{
|
2015-02-18 01:20:59 +00:00
|
|
|
$app = JFactory::getApplication();
|
|
|
|
$menus = $app->getMenu('site');
|
|
|
|
$language = isset($needles['language']) ? $needles['language'] : '*';
|
2014-04-08 12:32:59 +00:00
|
|
|
|
|
|
|
// Prepare the reverse lookup array.
|
|
|
|
if (!isset(self::$lookup[$language]))
|
|
|
|
{
|
|
|
|
self::$lookup[$language] = array();
|
|
|
|
|
2016-08-01 17:47:09 +00:00
|
|
|
$component = JComponentHelper::getComponent('com_weblinks');
|
2014-04-08 12:32:59 +00:00
|
|
|
|
|
|
|
$attributes = array('component_id');
|
|
|
|
$values = array($component->id);
|
|
|
|
|
|
|
|
if ($language != '*')
|
|
|
|
{
|
|
|
|
$attributes[] = 'language';
|
|
|
|
$values[] = array($needles['language'], '*');
|
|
|
|
}
|
|
|
|
|
|
|
|
$items = $menus->getItems($attributes, $values);
|
|
|
|
|
|
|
|
if ($items)
|
|
|
|
{
|
|
|
|
foreach ($items as $item)
|
|
|
|
{
|
|
|
|
if (isset($item->query) && isset($item->query['view']))
|
|
|
|
{
|
|
|
|
$view = $item->query['view'];
|
2015-02-18 01:20:59 +00:00
|
|
|
|
2014-04-08 12:32:59 +00:00
|
|
|
if (!isset(self::$lookup[$language][$view]))
|
|
|
|
{
|
|
|
|
self::$lookup[$language][$view] = array();
|
|
|
|
}
|
2015-02-18 01:20:59 +00:00
|
|
|
|
2014-04-08 12:32:59 +00:00
|
|
|
if (isset($item->query['id']))
|
|
|
|
{
|
2016-08-01 17:47:09 +00:00
|
|
|
/**
|
|
|
|
* Here it will become a bit tricky
|
|
|
|
* language != * can override existing entries
|
|
|
|
* language == * cannot override existing entries
|
|
|
|
*/
|
2014-04-08 12:32:59 +00:00
|
|
|
if (!isset(self::$lookup[$language][$view][$item->query['id']]) || $item->language != '*')
|
|
|
|
{
|
|
|
|
self::$lookup[$language][$view][$item->query['id']] = $item->id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($needles)
|
|
|
|
{
|
|
|
|
foreach ($needles as $view => $ids)
|
|
|
|
{
|
|
|
|
if (isset(self::$lookup[$language][$view]))
|
|
|
|
{
|
|
|
|
foreach ($ids as $id)
|
|
|
|
{
|
|
|
|
if (isset(self::$lookup[$language][$view][(int) $id]))
|
|
|
|
{
|
|
|
|
return self::$lookup[$language][$view][(int) $id];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the active menuitem matches the requested language
|
|
|
|
$active = $menus->getActive();
|
2015-02-18 01:20:59 +00:00
|
|
|
|
2014-04-08 12:32:59 +00:00
|
|
|
if ($active && ($language == '*' || in_array($active->language, array('*', $language)) || !JLanguageMultilang::isEnabled()))
|
|
|
|
{
|
|
|
|
return $active->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not found, return language specific home link
|
|
|
|
$default = $menus->getDefault($language);
|
2015-02-18 01:20:59 +00:00
|
|
|
|
2014-04-08 12:32:59 +00:00
|
|
|
return !empty($default->id) ? $default->id : null;
|
|
|
|
}
|
|
|
|
}
|