From 554dde6c03f0805a76eeb7d80818a29c1722e25a Mon Sep 17 00:00:00 2001 From: Tuan Pham Ngoc Date: Sun, 20 Jun 2021 20:21:32 +0700 Subject: [PATCH] Add icon service --- .../src/Extension/WeblinksComponent.php | 2 + .../com_weblinks/src/Service/HTML/Icon.php | 172 ++++++++++++++++++ .../tmpl/category/default_items.php | 2 +- 3 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 src/administrator/components/com_weblinks/src/Service/HTML/Icon.php diff --git a/src/administrator/components/com_weblinks/src/Extension/WeblinksComponent.php b/src/administrator/components/com_weblinks/src/Extension/WeblinksComponent.php index ffa7d65..b39574c 100644 --- a/src/administrator/components/com_weblinks/src/Extension/WeblinksComponent.php +++ b/src/administrator/components/com_weblinks/src/Extension/WeblinksComponent.php @@ -24,6 +24,7 @@ use Joomla\CMS\HTML\HTMLRegistryAwareTrait; use Joomla\CMS\Tag\TagServiceInterface; use Joomla\CMS\Tag\TagServiceTrait; use Joomla\Component\Weblinks\Administrator\Service\HTML\AdministratorService; +use Joomla\Component\Weblinks\Administrator\Service\HTML\Icon; use Psr\Container\ContainerInterface; /** @@ -60,6 +61,7 @@ class WeblinksComponent extends MVCComponent implements CategoryServiceInterface public function boot(ContainerInterface $container) { $this->getRegistry()->register('weblinksadministrator', new AdministratorService); + $this->getRegistry()->register('weblinkicon', new Icon($container->get(SiteApplication::class))); } /** diff --git a/src/administrator/components/com_weblinks/src/Service/HTML/Icon.php b/src/administrator/components/com_weblinks/src/Service/HTML/Icon.php new file mode 100644 index 0000000..6f003db --- /dev/null +++ b/src/administrator/components/com_weblinks/src/Service/HTML/Icon.php @@ -0,0 +1,172 @@ + + * @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\Application\CMSApplication; +use Joomla\CMS\Factory; +use Joomla\CMS\HTML\HTMLHelper; +use Joomla\CMS\Language\Text; +use Joomla\CMS\Layout\LayoutHelper; +use Joomla\CMS\Router\Route; +use Joomla\CMS\Uri\Uri; +use Joomla\Component\Weblinks\Site\Helper\RouteHelper; +use Joomla\Registry\Registry; + +/** + * Content Component HTML Helper + * + * @since 4.0.0 + */ +class Icon +{ + /** + * The application + * + * @var CMSApplication + * + * @since 4.0.0 + */ + private $application; + + /** + * Service constructor + * + * @param CMSApplication $application The application + * + * @since 4.0.0 + */ + public function __construct(CMSApplication $application) + { + $this->application = $application; + } + + /** + * Method to generate a link to the create item page for the given category + * + * @param object $category The category information + * @param Registry $params The item parameters + * @param array $attribs Optional attributes for the link + * + * @return string The HTML markup for the create item link + * + * @since 4.0.0 + */ + public static function create($category, $params, $attribs = array()) + { + $uri = Uri::getInstance(); + + $url = 'index.php?option=com_weblinks&task=weblink.add&return=' . base64_encode($uri) . '&w_id=0&catid=' . $category->id; + + $text = LayoutHelper::render('joomla.content.icons.create', array('params' => $params, 'legacy' => false)); + + // Add the button classes to the attribs array + if (isset($attribs['class'])) + { + $attribs['class'] .= ' btn btn-primary'; + } + else + { + $attribs['class'] = 'btn btn-primary'; + } + + $button = HTMLHelper::_('link', Route::_($url), $text, $attribs); + + $output = '' . $button . ''; + + return $output; + } + + /** + * Display an edit icon for the weblink. + * + * This icon will not display in a popup window, nor if the weblink is trashed. + * Edit access checks must be performed in the calling code. + * + * @param object $weblink The weblink information + * @param Registry $params The item parameters + * @param array $attribs Optional attributes for the link + * @param boolean $legacy True to use legacy images, false to use icomoon based graphic + * + * @return string The HTML for the contact edit icon. + * + * @since 4.0.0 + */ + public static function edit($weblink, $params, $attribs = array(), $legacy = false) + { + $user = Factory::getApplication()->getIdentity(); + $uri = Uri::getInstance(); + + // Ignore if in a popup window. + if ($params && $params->get('popup')) + { + return ''; + } + + // Ignore if the state is negative (trashed). + if ($weblink->state < 0) + { + return ''; + } + + // Show checked_out icon if the contact is checked out by a different user + if (property_exists($weblink, 'checked_out') + && property_exists($weblink, 'checked_out_time') + && $weblink->checked_out + && $weblink->checked_out !== $user->get('id')) + { + $checkoutUser = Factory::getUser($weblink->checked_out); + $date = HTMLHelper::_('date', $weblink->checked_out_time); + $tooltip = Text::sprintf('COM_WEBLINKS_CHECKED_OUT_BY', $checkoutUser->name) + . '
' . $date; + + $text = LayoutHelper::render('joomla.content.icons.edit_lock', array('contact' => $weblink, 'tooltip' => $tooltip, 'legacy' => $legacy)); + + $attribs['aria-describedby'] = 'editweblink-' . (int) $weblink->id; + $output = HTMLHelper::_('link', '#', $text, $attribs); + + return $output; + } + + $weblinkUrl = RouteHelper::getWeblinkRoute($weblink->slug, $weblink->catid, $weblink->language); + $url = $weblinkUrl . '&task=weblink.edit&id=' . $weblink->id . '&return=' . base64_encode($uri); + + if ((int) $weblink->state === 0) + { + $tooltip = Text::_('COM_WEBLINKS_EDIT_UNPUBLISHED_WEBLINK'); + } + else + { + $tooltip = Text::_('COM_WEBLINKS_EDIT_PUBLISHED_WEBLINK'); + } + + $nowDate = strtotime(Factory::getDate()); + $icon = $weblink->state ? 'edit' : 'eye-slash'; + + if (($weblink->publish_up !== null && strtotime($weblink->publish_up) > $nowDate) + || ($weblink->publish_down !== null && strtotime($weblink->publish_down) < $nowDate + && $weblink->publish_down !== Factory::getDbo()->getNullDate())) + { + $icon = 'eye-slash'; + } + + $aria_described = 'editweblink-' . (int) $weblink->id; + + $text = ''; + $text .= Text::_('JGLOBAL_EDIT'); + $text .= ''; + + $attribs['aria-describedby'] = $aria_described; + $output = HTMLHelper::_('link', Route::_($url), $text, $attribs); + + return $output; + } +} diff --git a/src/components/com_weblinks/tmpl/category/default_items.php b/src/components/com_weblinks/tmpl/category/default_items.php index d2b3b9e..5b1d9e1 100644 --- a/src/components/com_weblinks/tmpl/category/default_items.php +++ b/src/components/com_weblinks/tmpl/category/default_items.php @@ -69,7 +69,7 @@ $listDirn = $this->escape($this->state->get('list.direction')); - +