[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Button/ -> FeaturedButton.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Button;
  11  
  12  use Joomla\CMS\Date\Date;
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\HTML\HTMLHelper;
  15  use Joomla\CMS\Language\Text;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('_JEXEC') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * The FeaturedButton class.
  23   *
  24   * @since  4.0.0
  25   */
  26  class FeaturedButton extends ActionButton
  27  {
  28      /**
  29       * Configure this object.
  30       *
  31       * @return  void
  32       *
  33       * @since  4.0.0
  34       */
  35      protected function preprocess()
  36      {
  37          $this->addState(
  38              0,
  39              'featured',
  40              'icon-unfeatured',
  41              Text::_('JGLOBAL_TOGGLE_FEATURED'),
  42              ['tip_title' => Text::_('JUNFEATURED')]
  43          );
  44          $this->addState(
  45              1,
  46              'unfeatured',
  47              'icon-color-featured icon-star',
  48              Text::_('JGLOBAL_TOGGLE_FEATURED'),
  49              ['tip_title' => Text::_('JFEATURED')]
  50          );
  51      }
  52  
  53      /**
  54       * Render action button by item value.
  55       *
  56       * @param   integer|null  $value         Current value of this item.
  57       * @param   integer|null  $row           The row number of this item.
  58       * @param   array         $options       The options to override group options.
  59       * @param   string|Date   $featuredUp    The date which item featured up.
  60       * @param   string|Date   $featuredDown  The date which item featured down.
  61       *
  62       * @return  string  Rendered HTML.
  63       *
  64       * @since  4.0.0
  65       */
  66      public function render(?int $value = null, ?int $row = null, array $options = [], $featuredUp = null, $featuredDown = null): string
  67      {
  68          if ($featuredUp || $featuredDown) {
  69              $bakState = $this->getState($value);
  70              $default  = $this->getState($value) ?? $this->unknownState;
  71  
  72              $nowDate  = Factory::getDate()->toUnix();
  73  
  74              $tz       = Factory::getUser()->getTimezone();
  75  
  76              if (!is_null($featuredUp)) {
  77                  $featuredUp = Factory::getDate($featuredUp, 'UTC')->setTimezone($tz);
  78              }
  79  
  80              if (!is_null($featuredDown)) {
  81                  $featuredDown = Factory::getDate($featuredDown, 'UTC')->setTimezone($tz);
  82              }
  83  
  84              // Add tips and special titles
  85              // Create special titles for featured items
  86              if ($value === 1) {
  87                  // Create tip text, only we have featured up or down settings
  88                  $tips = [];
  89  
  90                  if ($featuredUp) {
  91                      $tips[] = Text::sprintf('JLIB_HTML_FEATURED_STARTED', HTMLHelper::_('date', $featuredUp, Text::_('DATE_FORMAT_LC5'), 'UTC'));
  92                  }
  93  
  94                  if ($featuredDown) {
  95                      $tips[] = Text::sprintf('JLIB_HTML_FEATURED_FINISHED', HTMLHelper::_('date', $featuredDown, Text::_('DATE_FORMAT_LC5'), 'UTC'));
  96                  }
  97  
  98                  $tip = empty($tips) ? false : implode('<br>', $tips);
  99  
 100                  $default['title'] = $tip;
 101  
 102                  $options['tip_title'] = Text::_('JLIB_HTML_FEATURED_ITEM');
 103  
 104                  if ($featuredUp && $nowDate < $featuredUp->toUnix()) {
 105                      $options['tip_title'] = Text::_('JLIB_HTML_FEATURED_PENDING_ITEM');
 106                      $default['icon'] = 'pending';
 107                  }
 108  
 109                  if ($featuredDown && $nowDate > $featuredDown->toUnix()) {
 110                      $options['tip_title'] = Text::_('JLIB_HTML_FEATURED_EXPIRED_ITEM');
 111                      $default['icon'] = 'expired';
 112                  }
 113              }
 114  
 115              $this->states[$value] = $default;
 116  
 117              $html = parent::render($value, $row, $options);
 118  
 119              $this->states[$value] = $bakState;
 120  
 121              return $html;
 122          }
 123  
 124          return parent::render($value, $row, $options);
 125      }
 126  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer