1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
<?php
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
/**
* Class to create a date textbox and calendar button.
*/
class CDateSelector extends CTag {
/**
* Default CSS class name for HTML root element.
*/
const ZBX_STYLE_CLASS = 'calendar-control';
/**
* Input name.
*
* @var string
*/
private $name = '';
/**
* Default date format.
*
* @var string
*/
private $date_format = ZBX_FULL_DATE_TIME;
/**
* Set aria-required to textbox.
*
* @var bool
*/
private $is_required = false;
/**
* Placeholder for date textbox field.
*
* @var string
*/
private $placeholder = null;
/**
* Date and time set from view. Absolute (Y-m-d H:i:s) or relative time (now+1d, now/M ...).
*
* @var string
*/
private $value = null;
/**
* Readonly state of HTML element.
*
* @var bool
*/
private $readonly = false;
/**
* Enabled or disabled state of HTML element.
*
* @var bool
*/
private $enabled = true;
/**
* Maxlength attribute of the input field.
*
* @var int
*/
private $maxlength = 255;
/**
* Create array with all inputs required for date selection and calendar.
*
* @param string $name Textbox field name and calendar name prefix.
* @param string $value Date and time set from view. Absolute (Y-m-d H:i:s) or relative time (now+1d, now/M ...).
*
* @return CDateSelector
*/
public function __construct(string $name = 'calendar', $value = null) {
parent::__construct('div', true);
$this->name = $name;
$this->value = $value;
$this->addClass(static::ZBX_STYLE_CLASS);
}
/**
* Set or reset element 'aria-required' attribute to textbox (not the container).
*
* @param bool $is_required True to set field as required or false if field is not required.
*
* @return CDateSelector
*/
public function setAriaRequired($is_required = false) {
$this->is_required = $is_required;
return $this;
}
/**
* Set date format which calendar will return upon selection.
*
* @param string $format Date and time format. Usually Y-m-d H:i:s or Y-m-d H:i
*
* @return CDateSelector
*/
public function setDateFormat($format) {
$this->date_format = $format;
return $this;
}
/**
* Add placeholder to date textbox field.
*
* @param string|null $text Placeholder text for date textbox field.
*
* @return CDateSelector
*/
public function setPlaceholder($text) {
$this->placeholder = $text;
return $this;
}
/**
* Enable or disable readonly mode for the element.
*
* @param bool $value
*
* @return CDateSelector
*/
public function setReadonly(bool $value): self {
$this->readonly = $value;
return $this;
}
/**
* Set enabled or disabled state to field.
*
* @param bool $enabled Field state.
*
* @return CDateSelector
*/
public function setEnabled($enabled) {
$this->enabled = $enabled;
return $this;
}
/**
* Set non-default maxlength attribute to the input field.
*
* @param int $maxlength
*
* @return CDateSelector
*/
public function setMaxLength(int $maxlength) {
$this->maxlength = $maxlength;
return $this;
}
/**
* Gets string representation of date textbox and calendar button.
*
* @param bool $destroy
*
* @return string
*/
public function toString($destroy = true) {
$this
->addItem(
(new CTextBox($this->name, $this->value))
->setId(zbx_formatDomId($this->name))
->setAttribute('placeholder', $this->placeholder)
->setAttribute('maxlength', $this->maxlength)
->setAriaRequired($this->is_required)
->setEnabled($this->enabled)
->setReadonly($this->readonly)
)
->addItem((new CButton($this->name.'_calendar'))
->addClass(ZBX_STYLE_BTN_ICON)
->addClass(ZBX_ICON_CALENDAR)
->setEnabled($this->enabled && !$this->readonly)
->onClick('toggleCalendar(this, "'.zbx_formatDomId($this->name).'", "'.$this->date_format.'");'));
return parent::toString($destroy);
}
}
|