Add HTML Service

This commit is contained in:
Tuan Pham Ngoc 2021-06-20 18:25:20 +07:00
parent 08fe10e0ef
commit 0c4a9ce8b5
3 changed files with 140 additions and 1 deletions

View File

@ -16,6 +16,7 @@ use Joomla\CMS\Extension\Service\Provider\CategoryFactory;
use Joomla\CMS\Extension\Service\Provider\ComponentDispatcherFactory;
use Joomla\CMS\Extension\Service\Provider\MVCFactory;
use Joomla\CMS\Extension\Service\Provider\RouterFactory;
use Joomla\CMS\HTML\Registry;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\Component\Weblinks\Administrator\Extension\WeblinksComponent;
use Joomla\DI\Container;
@ -47,6 +48,7 @@ return new class implements ServiceProviderInterface
ComponentInterface::class,
function (Container $container) {
$component = new WeblinksComponent($container->get(ComponentDispatcherFactoryInterface::class));
$component->setRegistry($container->get(Registry::class));
$component->setMVCFactory($container->get(MVCFactoryInterface::class));
$component->setCategoryFactory($container->get(CategoryFactoryInterface::class));
$component->setRouterFactory($container->get(RouterFactoryInterface::class));

View File

@ -11,15 +11,20 @@ namespace Joomla\Component\Weblinks\Administrator\Extension;
defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Application\SiteApplication;
use Joomla\CMS\Association\AssociationServiceInterface;
use Joomla\CMS\Association\AssociationServiceTrait;
use Joomla\CMS\Categories\CategoryServiceInterface;
use Joomla\CMS\Categories\CategoryServiceTrait;
use Joomla\CMS\Component\Router\RouterServiceInterface;
use Joomla\CMS\Component\Router\RouterServiceTrait;
use Joomla\CMS\Extension\BootableExtensionInterface;
use Joomla\CMS\Extension\MVCComponent;
use Joomla\CMS\HTML\HTMLRegistryAwareTrait;
use Joomla\CMS\Tag\TagServiceInterface;
use Joomla\CMS\Tag\TagServiceTrait;
use Joomla\Component\Weblinks\Administrator\Service\HTML\AdministratorService;
use Psr\Container\ContainerInterface;
/**
* Component class for com_contact
@ -27,10 +32,11 @@ use Joomla\CMS\Tag\TagServiceTrait;
* @since 4.0.0
*/
class WeblinksComponent extends MVCComponent implements CategoryServiceInterface, AssociationServiceInterface,
TagServiceInterface, RouterServiceInterface
TagServiceInterface, RouterServiceInterface, BootableExtensionInterface
{
use CategoryServiceTrait;
use AssociationServiceTrait;
use HTMLRegistryAwareTrait;
use RouterServiceTrait;
use CategoryServiceTrait, TagServiceTrait
{
@ -38,6 +44,24 @@ class WeblinksComponent extends MVCComponent implements CategoryServiceInterface
CategoryServiceTrait::getStateColumnForSection insteadof TagServiceTrait;
}
/**
* Booting the extension. This is the function to set up the environment of the extension like
* registering new class loaders, etc.
*
* If required, some initial set up can be done from services of the container, eg.
* registering HTML services.
*
* @param ContainerInterface $container The container
*
* @return void
*
* @since 4.0.0
*/
public function boot(ContainerInterface $container)
{
$this->getRegistry()->register('weblinksadministrator', new AdministratorService);
}
/**
* Returns the table for the count items functions for the given section.
*

View File

@ -0,0 +1,113 @@
<?php
/**
* @package Joomla.Administrator
* @subpackage com_contact
*
* @copyright (C) 2009 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Weblinks\Administrator\Service\HTML;
\defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Associations;
use Joomla\CMS\Language\LanguageHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Layout\LayoutHelper;
use Joomla\CMS\Router\Route;
use Joomla\Database\ParameterType;
/**
* Weblinks HTML helper class.
*
* @since 1.6
*/
class AdministratorService
{
/**
* Get the associated language flags
*
* @param integer $weblinkid The item id to search associations
*
* @return string The language HTML
*
* @throws \Exception
*/
public function association($weblinkid)
{
// Defaults
$html = '';
// Get the associations
if ($associations = Associations::getAssociations('com_weblinks', '#__weblinks', 'com_weblinks.item', $weblinkid))
{
foreach ($associations as $tag => $associated)
{
$associations[$tag] = (int) $associated->id;
}
// Get the associated contact items
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select(
[
$db->quoteName('c.id'),
$db->quoteName('c.title', 'title'),
$db->quoteName('l.sef', 'lang_sef'),
$db->quoteName('lang_code'),
$db->quoteName('cat.title', 'category_title'),
$db->quoteName('l.image'),
$db->quoteName('l.title', 'language_title'),
]
)
->from($db->quoteName('#__weblinks', 'c'))
->join('LEFT', $db->quoteName('#__categories', 'cat'), $db->quoteName('cat.id') . ' = ' . $db->quoteName('c.catid'))
->join('LEFT', $db->quoteName('#__languages', 'l'), $db->quoteName('c.language') . ' = ' . $db->quoteName('l.lang_code'))
->whereIn($db->quoteName('c.id'), array_values($associations))
->where($db->quoteName('c.id') . ' != :id')
->bind(':id', $weblinkid, ParameterType::INTEGER);
$db->setQuery($query);
try
{
$items = $db->loadObjectList('id');
}
catch (\RuntimeException $e)
{
throw new \Exception($e->getMessage(), 500, $e);
}
if ($items)
{
$languages = LanguageHelper::getContentLanguages(array(0, 1));
$content_languages = array_column($languages, 'lang_code');
foreach ($items as &$item)
{
if (in_array($item->lang_code, $content_languages))
{
$text = $item->lang_code;
$url = Route::_('index.php?option=com_weblinks&task=weblink.edit&id=' . (int) $item->id);
$tooltip = '<strong>' . htmlspecialchars($item->language_title, ENT_QUOTES, 'UTF-8') . '</strong><br>'
. htmlspecialchars($item->title, ENT_QUOTES, 'UTF-8') . '<br>' . Text::sprintf('JCATEGORY_SPRINTF', $item->category_title);
$classes = 'badge bg-secondary';
$item->link = '<a href="' . $url . '" class="' . $classes . '">' . $text . '</a>'
. '<div role="tooltip" id="tip-' . (int) $weblinkid . '-' . (int) $item->id . '">' . $tooltip . '</div>';
}
else
{
// Display warning if Content Language is trashed or deleted
Factory::getApplication()->enqueueMessage(Text::sprintf('JGLOBAL_ASSOCIATIONS_CONTENTLANGUAGE_WARNING', $item->lang_code), 'warning');
}
}
}
$html = LayoutHelper::render('joomla.content.associations', $items);
}
return $html;
}
}