primo commit
This commit is contained in:
		
							
								
								
									
										189
									
								
								administrator/components/com_scheduler/src/Field/CronField.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										189
									
								
								administrator/components/com_scheduler/src/Field/CronField.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,189 @@ | ||||
| <?php | ||||
|  | ||||
| /** | ||||
|  * @package     Joomla.Administrator | ||||
|  * @subpackage  com_scheduler | ||||
|  * | ||||
|  * @copyright   (C) 2021 Open Source Matters, Inc. <https://www.joomla.org> | ||||
|  * @license     GNU General Public License version 2 or later; see LICENSE.txt | ||||
|  */ | ||||
|  | ||||
| namespace Joomla\Component\Scheduler\Administrator\Field; | ||||
|  | ||||
| use Joomla\CMS\Form\Field\ListField; | ||||
| use Joomla\CMS\HTML\HTMLHelper; | ||||
| use Joomla\CMS\Language\Text; | ||||
|  | ||||
| // phpcs:disable PSR1.Files.SideEffects | ||||
| \defined('_JEXEC') or die; | ||||
| // phpcs:enable PSR1.Files.SideEffects | ||||
|  | ||||
| /** | ||||
|  * Multi-select form field, supporting inputs of: | ||||
|  * minutes, hours, days of week, days of month and months. | ||||
|  * | ||||
|  * @since  4.1.0 | ||||
|  */ | ||||
| class CronField extends ListField | ||||
| { | ||||
|     /** | ||||
|      * The subtypes supported by this field type. | ||||
|      * | ||||
|      * @var string[] | ||||
|      * | ||||
|      * @since  4.1.0 | ||||
|      */ | ||||
|     private const SUBTYPES = [ | ||||
|         'minutes', | ||||
|         'hours', | ||||
|         'days_month', | ||||
|         'months', | ||||
|         'days_week', | ||||
|     ]; | ||||
|  | ||||
|     /** | ||||
|      * Count of predefined options for each subtype | ||||
|      * | ||||
|      * @var int[][] | ||||
|      * | ||||
|      * @since  4.1.0 | ||||
|      */ | ||||
|     private const OPTIONS_RANGE = [ | ||||
|         'minutes'    => [0, 59], | ||||
|         'hours'      => [0, 23], | ||||
|         'days_week'  => [1, 7], | ||||
|         'days_month' => [1, 31], | ||||
|         'months'     => [1, 12], | ||||
|     ]; | ||||
|  | ||||
|     /** | ||||
|      * Response labels for the 'month' and 'days_week' subtypes. | ||||
|      * The labels are language constants translated when needed. | ||||
|      * | ||||
|      * @var string[][] | ||||
|      * @since  4.1.0 | ||||
|      */ | ||||
|     private const PREPARED_RESPONSE_LABELS = [ | ||||
|         'months' => [ | ||||
|             'JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', | ||||
|             'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER', | ||||
|         ], | ||||
|         'days_week' => [ | ||||
|             'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', | ||||
|             'FRIDAY', 'SATURDAY', 'SUNDAY', | ||||
|         ], | ||||
|     ]; | ||||
|  | ||||
|     /** | ||||
|      * The form field type. | ||||
|      * | ||||
|      * @var    string | ||||
|      * | ||||
|      * @since  4.1.0 | ||||
|      */ | ||||
|     protected $type = 'cronIntervals'; | ||||
|  | ||||
|     /** | ||||
|      * The subtype of the CronIntervals field | ||||
|      * | ||||
|      * @var string | ||||
|      * @since  4.1.0 | ||||
|      */ | ||||
|     private $subtype; | ||||
|  | ||||
|     /** | ||||
|      * If true, field options will include a wildcard | ||||
|      * | ||||
|      * @var boolean | ||||
|      * @since  4.1.0 | ||||
|      */ | ||||
|     private $wildcard; | ||||
|  | ||||
|     /** | ||||
|      * If true, field will only have numeric labels (for days_week and months) | ||||
|      * | ||||
|      * @var boolean | ||||
|      * @since  4.1.0 | ||||
|      */ | ||||
|     private $onlyNumericLabels; | ||||
|  | ||||
|     /** | ||||
|      * Override the parent method to set deal with subtypes. | ||||
|      * | ||||
|      * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form | ||||
|      *                                       field object. | ||||
|      * @param   mixed              $value    The form field value to validate. | ||||
|      * @param   string             $group    The field name group control value. This acts as an array container for | ||||
|      *                                       the field. For example if the field has `name="foo"` and the group value is | ||||
|      *                                       set to "bar" then the full field name would end up being "bar[foo]". | ||||
|      * | ||||
|      * @return  boolean  True on success. | ||||
|      * | ||||
|      * @since   4.1.0 | ||||
|      */ | ||||
|     public function setup(\SimpleXMLElement $element, $value, $group = null): bool | ||||
|     { | ||||
|         $parentResult = parent::setup($element, $value, $group); | ||||
|  | ||||
|         $subtype           = ((string) $element['subtype'] ?? '') ?: null; | ||||
|         $wildcard          = ((string) $element['wildcard'] ?? '') === 'true'; | ||||
|         $onlyNumericLabels = ((string) $element['onlyNumericLabels']) === 'true'; | ||||
|  | ||||
|         if (!($subtype && \in_array($subtype, self::SUBTYPES))) { | ||||
|             return false; | ||||
|         } | ||||
|  | ||||
|         $this->subtype           = $subtype; | ||||
|         $this->wildcard          = $wildcard; | ||||
|         $this->onlyNumericLabels = $onlyNumericLabels; | ||||
|  | ||||
|         return $parentResult; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Method to get field options | ||||
|      * | ||||
|      * @return   array  Array of objects representing options in the options list | ||||
|      * | ||||
|      * @since  4.1.0 | ||||
|      */ | ||||
|     protected function getOptions(): array | ||||
|     { | ||||
|         $subtype = $this->subtype; | ||||
|         $options = parent::getOptions(); | ||||
|  | ||||
|         if (!\in_array($subtype, self::SUBTYPES)) { | ||||
|             return $options; | ||||
|         } | ||||
|  | ||||
|         if ($this->wildcard) { | ||||
|             try { | ||||
|                 $options[] = HTMLHelper::_('select.option', '*', '*'); | ||||
|             } catch (\InvalidArgumentException $e) { | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         [$optionLower, $optionUpper] = self::OPTIONS_RANGE[$subtype]; | ||||
|  | ||||
|         // If we need text labels, we translate them first | ||||
|         if (\array_key_exists($subtype, self::PREPARED_RESPONSE_LABELS) && !$this->onlyNumericLabels) { | ||||
|             $labels = array_map( | ||||
|                 static function (string $string): string { | ||||
|                     return Text::_($string); | ||||
|                 }, | ||||
|                 self::PREPARED_RESPONSE_LABELS[$subtype] | ||||
|             ); | ||||
|         } else { | ||||
|             $labels = range(...self::OPTIONS_RANGE[$subtype]); | ||||
|         } | ||||
|  | ||||
|         for ([$i, $l] = [$optionLower, 0]; $i <= $optionUpper; $i++, $l++) { | ||||
|             try { | ||||
|                 $options[] = HTMLHelper::_('select.option', (string) ($i), $labels[$l]); | ||||
|             } catch (\InvalidArgumentException $e) { | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         return $options; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,48 @@ | ||||
| <?php | ||||
|  | ||||
| /** | ||||
|  * @package     Joomla.Administrator | ||||
|  * @subpackage  com_scheduler | ||||
|  * | ||||
|  * @copyright   (C) 2021 Open Source Matters, Inc. <https://www.joomla.org> | ||||
|  * @license     GNU General Public License version 2 or later; see LICENSE.txt | ||||
|  */ | ||||
|  | ||||
| namespace Joomla\Component\Scheduler\Administrator\Field; | ||||
|  | ||||
| use Joomla\CMS\Form\Field\PredefinedlistField; | ||||
|  | ||||
| // phpcs:disable PSR1.Files.SideEffects | ||||
| \defined('_JEXEC') or die; | ||||
| // phpcs:enable PSR1.Files.SideEffects | ||||
|  | ||||
| /** | ||||
|  * A select list containing valid Cron interval types. | ||||
|  * | ||||
|  * @since  4.1.0 | ||||
|  */ | ||||
| class ExecutionRuleField extends PredefinedlistField | ||||
| { | ||||
|     /** | ||||
|      * The form field type. | ||||
|      * | ||||
|      * @var    string | ||||
|      * @since  4.1.0 | ||||
|      */ | ||||
|     protected $type = 'ExecutionRule'; | ||||
|  | ||||
|     /** | ||||
|      * Available execution rules. | ||||
|      * | ||||
|      * @var string[] | ||||
|      * @since  4.1.0 | ||||
|      */ | ||||
|     protected $predefinedOptions = [ | ||||
|         'interval-minutes' => 'COM_SCHEDULER_EXECUTION_INTERVAL_MINUTES', | ||||
|         'interval-hours'   => 'COM_SCHEDULER_EXECUTION_INTERVAL_HOURS', | ||||
|         'interval-days'    => 'COM_SCHEDULER_EXECUTION_INTERVAL_DAYS', | ||||
|         'interval-months'  => 'COM_SCHEDULER_EXECUTION_INTERVAL_MONTHS', | ||||
|         'cron-expression'  => 'COM_SCHEDULER_EXECUTION_CRON_EXPRESSION', | ||||
|         'manual'           => 'COM_SCHEDULER_OPTION_EXECUTION_MANUAL_LABEL', | ||||
|     ]; | ||||
| } | ||||
| @ -0,0 +1,99 @@ | ||||
| <?php | ||||
|  | ||||
| /** | ||||
|  * @package     Joomla.Administrator | ||||
|  * @subpackage  com_scheduler | ||||
|  * | ||||
|  * @copyright   (C) 2021 Open Source Matters, Inc. <https://www.joomla.org> | ||||
|  * @license     GNU General Public License version 2 or later; see LICENSE.txt | ||||
|  */ | ||||
|  | ||||
| namespace Joomla\Component\Scheduler\Administrator\Field; | ||||
|  | ||||
| use Joomla\CMS\Form\Field\NumberField; | ||||
| use Joomla\CMS\Form\FormField; | ||||
|  | ||||
| // phpcs:disable PSR1.Files.SideEffects | ||||
| \defined('_JEXEC') or die; | ||||
| // phpcs:enable PSR1.Files.SideEffects | ||||
|  | ||||
| /** | ||||
|  * Select style field for interval(s) in minutes, hours, days and months. | ||||
|  * | ||||
|  * @since  4.1.0 | ||||
|  */ | ||||
| class IntervalField extends NumberField | ||||
| { | ||||
|     /** | ||||
|      * The form field type. | ||||
|      * | ||||
|      * @var    string | ||||
|      * @since  4.1.0 | ||||
|      */ | ||||
|     protected $type = 'Intervals'; | ||||
|  | ||||
|     /** | ||||
|      * The subtypes supported by this field type => [minVal, maxVal] | ||||
|      * | ||||
|      * @var string[] | ||||
|      * @since  4.1.0 | ||||
|      */ | ||||
|     private const SUBTYPES = [ | ||||
|         'minutes' => [1, 59], | ||||
|         'hours'   => [1, 23], | ||||
|         'days'    => [1, 30], | ||||
|         'months'  => [1, 12], | ||||
|     ]; | ||||
|  | ||||
|     /** | ||||
|      * The allowable maximum value of the field. | ||||
|      * | ||||
|      * @var    float | ||||
|      * @since  4.1.0 | ||||
|      */ | ||||
|     protected $max; | ||||
|  | ||||
|     /** | ||||
|      * The allowable minimum value of the field. | ||||
|      * | ||||
|      * @var    float | ||||
|      * @since  4.1.0 | ||||
|      */ | ||||
|     protected $min; | ||||
|  | ||||
|     /** | ||||
|      * The step by which value of the field increased or decreased. | ||||
|      * | ||||
|      * @var    float | ||||
|      * @since  4.1.0 | ||||
|      */ | ||||
|     protected $step = 1; | ||||
|  | ||||
|     /** | ||||
|      * Override the parent method to set deal with subtypes. | ||||
|      * | ||||
|      * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form | ||||
|      *                                       field object. | ||||
|      * @param   mixed              $value    The form field value to validate. | ||||
|      * @param   string             $group    The field name group control value. This acts as an array container for | ||||
|      *                                       the field. For example if the field has `name="foo"` and the group value is | ||||
|      *                                       set to "bar" then the full field name would end up being "bar[foo]". | ||||
|      * | ||||
|      * @return  boolean  True on success. | ||||
|      * | ||||
|      * @since   4.1.0 | ||||
|      */ | ||||
|     public function setup(\SimpleXMLElement $element, $value, $group = null): bool | ||||
|     { | ||||
|         $parentResult = FormField::setup($element, $value, $group); | ||||
|         $subtype      = ((string) $element['subtype'] ?? '') ?: null; | ||||
|  | ||||
|         if (empty($subtype) || !\array_key_exists($subtype, self::SUBTYPES)) { | ||||
|             return false; | ||||
|         } | ||||
|  | ||||
|         [$this->min, $this->max] = self::SUBTYPES[$subtype]; | ||||
|  | ||||
|         return $parentResult; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,46 @@ | ||||
| <?php | ||||
|  | ||||
| /** | ||||
|  * @package     Joomla.Administrator | ||||
|  * @subpackage  com_scheduler | ||||
|  * | ||||
|  * @copyright   (C) 2021 Open Source Matters, Inc. <https://www.joomla.org> | ||||
|  * @license     GNU General Public License version 2 or later; see LICENSE.txt | ||||
|  */ | ||||
|  | ||||
| namespace Joomla\Component\Scheduler\Administrator\Field; | ||||
|  | ||||
| use Joomla\CMS\Form\Field\PredefinedlistField; | ||||
|  | ||||
| // phpcs:disable PSR1.Files.SideEffects | ||||
| \defined('_JEXEC') or die; | ||||
| // phpcs:enable PSR1.Files.SideEffects | ||||
|  | ||||
| /** | ||||
|  * A predefined list field with all possible states for a com_scheduler entry. | ||||
|  * | ||||
|  * @since  4.1.0 | ||||
|  */ | ||||
| class TaskStateField extends PredefinedlistField | ||||
| { | ||||
|     /** | ||||
|      * The form field type. | ||||
|      * | ||||
|      * @var    string | ||||
|      * @since  4.1.0 | ||||
|      */ | ||||
|     public $type = 'taskState'; | ||||
|  | ||||
|     /** | ||||
|      * Available states | ||||
|      * | ||||
|      * @var  string[] | ||||
|      * @since  4.1.0 | ||||
|      */ | ||||
|     protected $predefinedOptions = [ | ||||
|         -2  => 'JTRASHED', | ||||
|         0   => 'JDISABLED', | ||||
|         1   => 'JENABLED', | ||||
|         '*' => 'JALL', | ||||
|     ]; | ||||
| } | ||||
| @ -0,0 +1,70 @@ | ||||
| <?php | ||||
|  | ||||
| /** | ||||
|  * @package     Joomla.Administrator | ||||
|  * @subpackage  com_scheduler | ||||
|  * | ||||
|  * @copyright   (C) 2021 Open Source Matters, Inc. <https://www.joomla.org> | ||||
|  * @license     GNU General Public License version 2 or later; see LICENSE.txt | ||||
|  */ | ||||
|  | ||||
| namespace Joomla\Component\Scheduler\Administrator\Field; | ||||
|  | ||||
| use Joomla\CMS\Form\Field\ListField; | ||||
| use Joomla\CMS\HTML\HTMLHelper; | ||||
| use Joomla\Component\Scheduler\Administrator\Helper\SchedulerHelper; | ||||
| use Joomla\Component\Scheduler\Administrator\Task\TaskOption; | ||||
| use Joomla\Utilities\ArrayHelper; | ||||
|  | ||||
| // phpcs:disable PSR1.Files.SideEffects | ||||
| \defined('_JEXEC') or die; | ||||
| // phpcs:enable PSR1.Files.SideEffects | ||||
|  | ||||
| /** | ||||
|  * A list field with all available task routines. | ||||
|  * | ||||
|  * @since  4.1.0 | ||||
|  */ | ||||
| class TaskTypeField extends ListField | ||||
| { | ||||
|     /** | ||||
|      * The form field type. | ||||
|      * | ||||
|      * @var    string | ||||
|      * @since  4.1.0 | ||||
|      */ | ||||
|     protected $type = 'taskType'; | ||||
|  | ||||
|     /** | ||||
|      * Method to get field options | ||||
|      * | ||||
|      * @return array | ||||
|      * | ||||
|      * @since  4.1.0 | ||||
|      * @throws \Exception | ||||
|      */ | ||||
|     protected function getOptions(): array | ||||
|     { | ||||
|         $options = parent::getOptions(); | ||||
|  | ||||
|         // Get all available task types and sort by title | ||||
|         $types = ArrayHelper::sortObjects( | ||||
|             SchedulerHelper::getTaskOptions()->options, | ||||
|             'title', | ||||
|             1 | ||||
|         ); | ||||
|  | ||||
|         // Closure to add a TaskOption as a <select> option in $options: array | ||||
|         $addTypeAsOption = function (TaskOption $type) use (&$options) { | ||||
|             try { | ||||
|                 $options[] = HTMLHelper::_('select.option', $type->id, $type->title); | ||||
|             } catch (\InvalidArgumentException $e) { | ||||
|             } | ||||
|         }; | ||||
|  | ||||
|         // Call $addTypeAsOption on each type | ||||
|         array_map($addTypeAsOption, $types); | ||||
|  | ||||
|         return $options; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,54 @@ | ||||
| <?php | ||||
|  | ||||
| /** | ||||
|  * @package     Joomla.Administrator | ||||
|  * @subpackage  com_scheduler | ||||
|  * | ||||
|  * @copyright   (C) 2021 Open Source Matters, Inc. <https://www.joomla.org> | ||||
|  * @license     GNU General Public License version 2 or later; see LICENSE.txt | ||||
|  */ | ||||
|  | ||||
| namespace Joomla\Component\Scheduler\Administrator\Field; | ||||
|  | ||||
| use Joomla\CMS\Form\Field\TextField; | ||||
|  | ||||
| // phpcs:disable PSR1.Files.SideEffects | ||||
| \defined('_JEXEC') or die; | ||||
| // phpcs:enable PSR1.Files.SideEffects | ||||
|  | ||||
| /** | ||||
|  * Field to override the text field layout to add a copy-text button, used in the com_scheduler | ||||
|  * configuration form. | ||||
|  * This field class is only needed because the layout file is in a non-global directory, so this should | ||||
|  * be made redundant and removed if/once the layout is shifted to `JPATH_SITE/layout/` | ||||
|  * | ||||
|  * @since 4.1.0 | ||||
|  */ | ||||
| class WebcronLinkField extends TextField | ||||
| { | ||||
|     /** | ||||
|      * We use a custom layout that allows for the link to be copied. | ||||
|      * | ||||
|      * @var  string | ||||
|      * @since  4.1.0 | ||||
|      */ | ||||
|     protected $layout = 'form.field.webcron_link'; | ||||
|  | ||||
|     /** | ||||
|      * Override layout paths. | ||||
|      * | ||||
|      * @inheritDoc | ||||
|      * @return string[] | ||||
|      * | ||||
|      * @since  4.1.0 | ||||
|      */ | ||||
|     protected function getLayoutPaths(): array | ||||
|     { | ||||
|         $s = DIRECTORY_SEPARATOR; | ||||
|  | ||||
|         return array_merge( | ||||
|             [JPATH_ADMINISTRATOR . "{$s}/components{$s}com_scheduler{$s}layouts{$s}"], | ||||
|             parent::getLayoutPaths() | ||||
|         ); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user