[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_scheduler/src/Field/ -> CronField.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_scheduler
   6   *
   7   * @copyright   (C) 2021 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Scheduler\Administrator\Field;
  12  
  13  use Joomla\CMS\Form\Field\ListField;
  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   * Multi-select form field, supporting inputs of:
  23   * minutes, hours, days of week, days of month and months.
  24   *
  25   * @since  4.1.0
  26   */
  27  class CronField extends ListField
  28  {
  29      /**
  30       * The subtypes supported by this field type.
  31       *
  32       * @var string[]
  33       *
  34       * @since  4.1.0
  35       */
  36      private const SUBTYPES = [
  37          'minutes',
  38          'hours',
  39          'days_month',
  40          'months',
  41          'days_week',
  42      ];
  43  
  44      /**
  45       * Count of predefined options for each subtype
  46       *
  47       * @var int[][]
  48       *
  49       * @since  4.1.0
  50       */
  51      private const OPTIONS_RANGE = [
  52          'minutes'    => [0, 59],
  53          'hours'      => [0, 23],
  54          'days_week'  => [1, 7],
  55          'days_month' => [1, 31],
  56          'months'     => [1, 12],
  57      ];
  58  
  59      /**
  60       * Response labels for the 'month' and 'days_week' subtypes.
  61       * The labels are language constants translated when needed.
  62       *
  63       * @var string[][]
  64       * @since  4.1.0
  65       */
  66      private const PREPARED_RESPONSE_LABELS = [
  67          'months'    => [
  68              'JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE',
  69              'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER',
  70          ],
  71          'days_week' => [
  72              'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY',
  73              'FRIDAY', 'SATURDAY', 'SUNDAY',
  74          ],
  75      ];
  76  
  77      /**
  78       * The form field type.
  79       *
  80       * @var    string
  81       *
  82       * @since  4.1.0
  83       */
  84      protected $type = 'cronIntervals';
  85  
  86      /**
  87       * The subtype of the CronIntervals field
  88       *
  89       * @var string
  90       * @since  4.1.0
  91       */
  92      private $subtype;
  93  
  94      /**
  95       * If true, field options will include a wildcard
  96       *
  97       * @var boolean
  98       * @since  4.1.0
  99       */
 100      private $wildcard;
 101  
 102      /**
 103       * If true, field will only have numeric labels (for days_week and months)
 104       *
 105       * @var boolean
 106       * @since  4.1.0
 107       */
 108      private $onlyNumericLabels;
 109  
 110      /**
 111       * Override the parent method to set deal with subtypes.
 112       *
 113       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form
 114       *                                       field object.
 115       * @param   mixed              $value    The form field value to validate.
 116       * @param   string             $group    The field name group control value. This acts as an array container for
 117       *                                       the field. For example if the field has `name="foo"` and the group value is
 118       *                                       set to "bar" then the full field name would end up being "bar[foo]".
 119       *
 120       * @return  boolean  True on success.
 121       *
 122       * @since   4.1.0
 123       */
 124      public function setup(\SimpleXMLElement $element, $value, $group = null): bool
 125      {
 126          $parentResult = parent::setup($element, $value, $group);
 127  
 128          $subtype           = ((string) $element['subtype'] ?? '') ?: null;
 129          $wildcard          = ((string) $element['wildcard'] ?? '') === 'true';
 130          $onlyNumericLabels = ((string) $element['onlyNumericLabels']) === 'true';
 131  
 132          if (!($subtype && \in_array($subtype, self::SUBTYPES))) {
 133              return false;
 134          }
 135  
 136          $this->subtype           = $subtype;
 137          $this->wildcard          = $wildcard;
 138          $this->onlyNumericLabels = $onlyNumericLabels;
 139  
 140          return $parentResult;
 141      }
 142  
 143      /**
 144       * Method to get field options
 145       *
 146       * @return   array  Array of objects representing options in the options list
 147       *
 148       * @since  4.1.0
 149       */
 150      protected function getOptions(): array
 151      {
 152          $subtype = $this->subtype;
 153          $options = parent::getOptions();
 154  
 155          if (!\in_array($subtype, self::SUBTYPES)) {
 156              return $options;
 157          }
 158  
 159          if ($this->wildcard) {
 160              try {
 161                  $options[] = HTMLHelper::_('select.option', '*', '*');
 162              } catch (\InvalidArgumentException $e) {
 163              }
 164          }
 165  
 166          [$optionLower, $optionUpper] = self::OPTIONS_RANGE[$subtype];
 167  
 168          // If we need text labels, we translate them first
 169          if (\array_key_exists($subtype, self::PREPARED_RESPONSE_LABELS) && !$this->onlyNumericLabels) {
 170              $labels = array_map(
 171                  static function (string $string): string {
 172                      return Text::_($string);
 173                  },
 174                  self::PREPARED_RESPONSE_LABELS[$subtype]
 175              );
 176          } else {
 177              $labels = range(...self::OPTIONS_RANGE[$subtype]);
 178          }
 179  
 180          for ([$i, $l] = [$optionLower, 0]; $i <= $optionUpper; $i++, $l++) {
 181              try {
 182                  $options[] = HTMLHelper::_('select.option', (string) ($i), $labels[$l]);
 183              } catch (\InvalidArgumentException $e) {
 184              }
 185          }
 186  
 187          return $options;
 188      }
 189  }


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