CoreFiles by mySites.guru
FeaturedButton.php121 lines, 3,228 bytes, from Joomla_4.0.0-beta7-Beta-Full_Package.zip
Raw History Compare with 4.0.0-beta6
md5
4356769a55e1495cba2cc9f10a25acb4
sha1
335eca9f3c40dcd5e91bbb65c631181d7cdb5dde
sha256
ac9def1b80eee3d74af5d2c4274ab2ef9036b8608f1fa2541904441955d24a23
<?php
/**
 * Joomla! Content Management System
 *
 * @copyright  (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 */

namespace Joomla\CMS\Button;

use Joomla\CMS\Date\Date;
use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;

/**
 * The FeaturedButton class.
 *
 * @since  4.0.0
 */
class FeaturedButton extends ActionButton
{
	/**
	 * Configure this object.
	 *
	 * @return  void
	 *
	 * @since  4.0.0
	 */
	protected function preprocess()
	{
		$this->addState(0, 'featured', 'icon-color-unfeatured icon-star',
			Text::_('JGLOBAL_TOGGLE_FEATURED'), ['tip_title' => Text::_('JUNFEATURED')]
		);
		$this->addState(1, 'unfeatured', 'icon-color-featured icon-star',
			Text::_('JGLOBAL_TOGGLE_FEATURED'), ['tip_title' => Text::_('JFEATURED')]
		);
	}

	/**
	 * Render action button by item value.
	 *
	 * @param   integer|null  $value         Current value of this item.
	 * @param   integer|null  $row           The row number of this item.
	 * @param   array         $options       The options to override group options.
	 * @param   string|Date   $featuredUp    The date which item featured up.
	 * @param   string|Date   $featuredDown  The date which item featured down.
	 *
	 * @return  string  Rendered HTML.
	 *
	 * @since  4.0.0
	 */
	public function render(?int $value = null, ?int $row = null, array $options = [], $featuredUp = null, $featuredDown = null): string
	{
		if ($featuredUp || $featuredDown)
		{
			$bakState = $this->getState($value);
			$default  = $this->getState($value) ?? $this->unknownState;

			$nowDate  = Factory::getDate()->toUnix();

			$tz       = Factory::getUser()->getTimezone();

			if (!is_null($featuredUp))
			{
				$featuredUp = Factory::getDate($featuredUp, 'UTC')->setTimeZone($tz);
			}

			if (!is_null($featuredDown))
			{
				$featuredDown = Factory::getDate($featuredDown, 'UTC')->setTimeZone($tz);
			}

			// Add tips and special titles
			// Create special titles for featured items
			if ($value === 1)
			{
				// Create tip text, only we have featured up or down settings
				$tips = [];

				if ($featuredUp)
				{
					$tips[] = Text::sprintf('JLIB_HTML_FEATURED_STARTED', HTMLHelper::_('date', $featuredUp, Text::_('DATE_FORMAT_LC5'), 'UTC'));
				}

				if ($featuredDown)
				{
					$tips[] = Text::sprintf('JLIB_HTML_FEATURED_FINISHED', HTMLHelper::_('date', $featuredDown, Text::_('DATE_FORMAT_LC5'), 'UTC'));
				}

				$tip = empty($tips) ? false : implode('<br>', $tips);

				$default['title'] = $tip;

				$options['tip_title'] = Text::_('JLIB_HTML_FEATURED_ITEM');

				if ($featuredUp && $nowDate < $featuredUp->toUnix())
				{
					$options['tip_title'] = Text::_('JLIB_HTML_FEATURED_PENDING_ITEM');
					$default['icon'] = 'pending';
				}

				if ($featuredDown && $nowDate > $featuredDown->toUnix())
				{
					$options['tip_title'] = Text::_('JLIB_HTML_FEATURED_EXPIRED_ITEM');
					$default['icon'] = 'expired';
				}
			}

			$this->states[$value] = $default;

			$html = parent::render($value, $row, $options);

			$this->states[$value] = $bakState;

			return $html;
		}

		return parent::render($value, $row, $options);
	}
}

The same content is in 2 releases, Joomla 4.0.0-beta6 to 4.0.0-beta7. See them all, or every version of this file.