* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Associations\Administrator\Helper;
use Joomla\CMS\Association\AssociationExtensionInterface;
use Joomla\CMS\Association\AssociationServiceInterface;
use Joomla\CMS\Factory;
use Joomla\CMS\Helper\ContentHelper;
use Joomla\CMS\Language\LanguageHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Layout\LayoutHelper;
use Joomla\CMS\Router\Route;
use Joomla\Database\ParameterType;
use Joomla\Registry\Registry;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* Associations component helper.
*
* @since 3.7.0
*/
class AssociationsHelper extends ContentHelper
{
/**
* Array of Registry objects of extensions
*
* @var array
* @since 3.7.0
*/
public static $extensionsSupport = null;
/**
* List of extensions name with support
*
* @var array
* @since 3.7.0
*/
public static $supportedExtensionsList = array();
/**
* Get the associated items for an item
*
* @param string $extensionName The extension name with com_
* @param string $typeName The item type
* @param int $itemId The id of item for which we need the associated items
*
* @return array
*
* @since 3.7.0
*/
public static function getAssociationList($extensionName, $typeName, $itemId)
{
if (!self::hasSupport($extensionName)) {
return array();
}
// Get the extension specific helper method
$helper = self::getExtensionHelper($extensionName);
return $helper->getAssociationList($typeName, $itemId);
}
/**
* Get the the instance of the extension helper class
*
* @param string $extensionName The extension name with com_
*
* @return \Joomla\CMS\Association\AssociationExtensionHelper|null
*
* @since 3.7.0
*/
public static function getExtensionHelper($extensionName)
{
if (!self::hasSupport($extensionName)) {
return null;
}
$support = self::$extensionsSupport[$extensionName];
return $support->get('helper');
}
/**
* Get item information
*
* @param string $extensionName The extension name with com_
* @param string $typeName The item type
* @param int $itemId The id of item for which we need the associated items
*
* @return \Joomla\CMS\Table\Table|null
*
* @since 3.7.0
*/
public static function getItem($extensionName, $typeName, $itemId)
{
if (!self::hasSupport($extensionName)) {
return null;
}
// Get the extension specific helper method
$helper = self::getExtensionHelper($extensionName);
return $helper->getItem($typeName, $itemId);
}
/**
* Check if extension supports associations
*
* @param string $extensionName The extension name with com_
*
* @return boolean
*
* @since 3.7.0
*/
public static function hasSupport($extensionName)
{
if (\is_null(self::$extensionsSupport)) {
self::getSupportedExtensions();
}
return \in_array($extensionName, self::$supportedExtensionsList);
}
/**
* Loads the helper for the given class.
*
* @param string $extensionName The extension name with com_
*
* @return AssociationExtensionInterface|null
*
* @since 4.0.0
*/
private static function loadHelper($extensionName)
{
$component = Factory::getApplication()->bootComponent($extensionName);
if ($component instanceof AssociationServiceInterface) {
return $component->getAssociationsExtension();
}
// Check if associations helper exists
if (!file_exists(JPATH_ADMINISTRATOR . '/components/' . $extensionName . '/helpers/associations.php')) {
return null;
}
require_once JPATH_ADMINISTRATOR . '/components/' . $extensionName . '/helpers/associations.php';
$componentAssociationsHelperClassName = self::getExtensionHelperClassName($extensionName);
if (!class_exists($componentAssociationsHelperClassName, false)) {
return null;
}
// Create an instance of the helper class
return new $componentAssociationsHelperClassName();
}
/**
* Get the extension specific helper class name
*
* @param string $extensionName The extension name with com_
*
* @return string
*
* @since 3.7.0
*/
private static function getExtensionHelperClassName($extensionName)
{
$realName = self::getExtensionRealName($extensionName);
return ucfirst($realName) . 'AssociationsHelper';
}
/**
* Get the real extension name. This means without com_
*
* @param string $extensionName The extension name with com_
*
* @return string
*
* @since 3.7.0
*/
private static function getExtensionRealName($extensionName)
{
return strpos($extensionName, 'com_') === false ? $extensionName : substr($extensionName, 4);
}
/**
* Get the associated language edit links Html.
*
* @param string $extensionName Extension Name
* @param string $typeName ItemType
* @param integer $itemId Item id.
* @param string $itemLanguage Item language code.
* @param boolean $addLink True for adding edit links. False for just text.
* @param boolean $assocLanguages True for showing non associated content languages. False only languages with associations.
*
* @return string The language HTML
*
* @since 3.7.0
*/
public static function getAssociationHtmlList($extensionName, $typeName, $itemId, $itemLanguage, $addLink = true, $assocLanguages = true)
{
// Get the associations list for this item.
$items = self::getAssociationList($extensionName, $typeName, $itemId);
$titleFieldName = self::getTypeFieldName($extensionName, $typeName, 'title');
// Get all content languages.
$languages = LanguageHelper::getContentLanguages(array(0, 1), false);
$content_languages = array_column($languages, 'lang_code');
// Display warning if Content Language is trashed or deleted
foreach ($items as $item) {
if (!\in_array($item['language'], $content_languages)) {
Factory::getApplication()->enqueueMessage(Text::sprintf('JGLOBAL_ASSOCIATIONS_CONTENTLANGUAGE_WARNING', $item['language']), 'warning');
}
}
$canEditReference = self::allowEdit($extensionName, $typeName, $itemId);
$canCreate = self::allowAdd($extensionName, $typeName);
// Create associated items list.
foreach ($languages as $langCode => $language) {
// Don't do for the reference language.
if ($langCode == $itemLanguage) {
continue;
}
// Don't show languages with associations, if we don't want to show them.
if ($assocLanguages && isset($items[$langCode])) {
unset($items[$langCode]);
continue;
}
// Don't show languages without associations, if we don't want to show them.
if (!$assocLanguages && !isset($items[$langCode])) {
continue;
}
// Get html parameters.
if (isset($items[$langCode])) {
$title = $items[$langCode][$titleFieldName];
$additional = '';
if (isset($items[$langCode]['catid'])) {
$db = Factory::getDbo();
// Get the category name
$query = $db->getQuery(true)
->select($db->quoteName('title'))
->from($db->quoteName('#__categories'))
->where($db->quoteName('id') . ' = :id')
->bind(':id', $items[$langCode]['catid'], ParameterType::INTEGER);
$db->setQuery($query);
$categoryTitle = $db->loadResult();
$additional = '' . Text::sprintf('JCATEGORY_SPRINTF', $categoryTitle) . '
';
} elseif (isset($items[$langCode]['menutype'])) {
$db = Factory::getDbo();
// Get the menutype name
$query = $db->getQuery(true)
->select($db->quoteName('title'))
->from($db->quoteName('#__menu_types'))
->where($db->quoteName('menutype') . ' = :menutype')
->bind(':menutype', $items[$langCode]['menutype']);
$db->setQuery($query);
$menutypeTitle = $db->loadResult();
$additional = '' . Text::sprintf('COM_MENUS_MENU_SPRINTF', $menutypeTitle) . '
';
}
$labelClass = 'bg-secondary';
$target = $langCode . ':' . $items[$langCode]['id'] . ':edit';
$allow = $canEditReference
&& self::allowEdit($extensionName, $typeName, $items[$langCode]['id'])
&& self::canCheckinItem($extensionName, $typeName, $items[$langCode]['id']);
$additional .= $addLink && $allow ? Text::_('COM_ASSOCIATIONS_EDIT_ASSOCIATION') : '';
} else {
$items[$langCode] = array();
$title = Text::_('COM_ASSOCIATIONS_NO_ASSOCIATION');
$additional = $addLink ? Text::_('COM_ASSOCIATIONS_ADD_NEW_ASSOCIATION') : '';
$labelClass = 'bg-warning text-dark';
$target = $langCode . ':0:add';
$allow = $canCreate;
}
// Generate item Html.
$options = array(
'option' => 'com_associations',
'view' => 'association',
'layout' => 'edit',
'itemtype' => $extensionName . '.' . $typeName,
'task' => 'association.edit',
'id' => $itemId,
'target' => $target,
);
$url = Route::_('index.php?' . http_build_query($options));
$url = $allow && $addLink ? $url : '';
$text = $language->lang_code;
$tooltip = '' . htmlspecialchars($language->title, ENT_QUOTES, 'UTF-8') . '
'
. htmlspecialchars($title, ENT_QUOTES, 'UTF-8') . '
' . $additional;
$classes = 'badge ' . $labelClass;
$items[$langCode]['link'] = '' . $text . ''
. '