Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
timelimiter.php
1<?php
2
3
5
13
14Loc::loadMessages(__FILE__);
15class TimeLimiter implements iLimiter
16{
17 public const DEFAULT_SENDING_START = '09:00';
18 public const DEFAULT_SENDING_END = '18:00';
22 private $letter;
23 private $parameters;
24
25 public function withLetter($letter)
26 {
27 $this->letter = $letter;
28 return $this;
29 }
30
35 public static function create()
36 {
37 return new static();
38 }
39
43 public function getLimit()
44 {
45 return 1;
46 }
47
51 public function getCurrent()
52 {
53 if (!$this->letter)
54 {
55 return 0;
56 }
57
58 $sendingStart = $this->letter->getConfiguration()->get('SENDING_START');
59 $sendingEnd = $this->letter->getConfiguration()->get('SENDING_END');
60 $sendingTimeEnabled = $this->letter->getConfiguration()->get('SENDING_TIME');
61
62 if (!$sendingEnd || !$sendingStart || $sendingTimeEnabled !== 'Y')
63 {
64 return 0;
65 }
66 $checkTime = strtotime($sendingStart);
67 $sendingStart = strtotime($sendingStart);
68 $sendingEnd = strtotime($sendingEnd);
69 $currentTime = strtotime((new DateTime())->format("H:i:s"));
70
71 $sendingStart = $sendingStart > $sendingEnd ? $sendingEnd : $sendingStart;
72 $sendingEnd = $checkTime > $sendingEnd ? $checkTime : $sendingEnd;
73
74 $this->setParameter('sendingStart', $sendingStart);
75 $this->setParameter('sendingEnd', $sendingEnd);
76 $this->setParameter('currentTime', $currentTime);
77
78 if ($currentTime > $sendingStart && $currentTime < $sendingEnd)
79 {
80 return 0;
81 }
82
83 return 1;
84 }
85
89 public function getUnitName()
90 {
91 return '';
92 }
93
97 public function getUnit()
98 {
99 return '';
100 }
101
105 public function getCaption()
106 {
107 return '';
108 }
109
113 public function getParameter($name)
114 {
115 return $this->parameters[$name] ?? null;
116 }
117
121 public function setParameter($name, $value)
122 {
123 $this->parameters[$name] = $value;
124 }
125
129 public function isHidden()
130 {
131 return true;
132 }
133
138 public static function prepareMessageConfiguration($configuration)
139 {
140 $configuration->addOption(new Message\ConfigurationOption([
142 'code' => 'SENDING_TIME',
144 'name' => Loc::getMessage('SENDER_INTEGRATION_MESSAGE_CONFIG_SENDING_TIME'),
145 'show_in_list' => false,
146 'required' => false,
147 'value' => Option::get('sender', 'sending_time')
148 ]));
149
150 $configuration->addOption(new Message\ConfigurationOption([
152 'code' => 'SENDING_START',
153 'name' => Loc::getMessage('SENDER_INTEGRATION_MESSAGE_CONFIG_SENDING_START'),
155 'show_in_list' => false,
156 'required' => false,
157 'value' => Option::get('sender', 'sending_start', TimeLimiter::DEFAULT_SENDING_START)
158 ]));
159
160 $configuration->addOption(new Message\ConfigurationOption([
162 'code' => 'SENDING_END',
163 'name' => Loc::getMessage('SENDER_INTEGRATION_MESSAGE_CONFIG_SENDING_END'),
165 'show_in_list' => false,
166 'required' => false,
167 'value' => Option::get('sender', 'sending_end', TimeLimiter::DEFAULT_SENDING_END)
168 ]));
169 }
170
175 public static function prepareMessageConfigurationView($configuration)
176 {
177
178 $sendingStart = $configuration->getOption('SENDING_START');
179 $sendingEnd = $configuration->getOption('SENDING_END');
180 $checkbox = $configuration->getOption('SENDING_TIME');
181
182 $view = function($input, $checkbox)
183 {
184 $prefix = 'CONFIGURATION_';
185 $inputCode = htmlspecialcharsbx($prefix.$input->getCode());
186 $checkboxCode = htmlspecialcharsbx($prefix.$checkbox->getCode());
187 ob_start();
188 Extension::load("sender.secret_block");
189 $inputHtml = "<select
190 id=\"$inputCode\"
191 name=\"$inputCode\"
192 value='".$input->getValue()."'
193 class=\"bx-sender-form-control bx-sender-message-editor-field-select\">";
194 for ($hour = 0; $hour < 24; $hour++)
195 {
196 foreach ([0, 30] as $minute)
197 {
198 $time = strtotime(sprintf("%02d:%02d", $hour, $minute));
199 $formatted = (new \DateTime())
200 ->setTimestamp($time)
201 ->format(Context::getCurrent()
202 ->getCulture()
203 ->getShortTimeFormat()
204 );
205
206 $inputHtml .= "<option value='{$formatted}'";
207 $inputHtml .= $time === strtotime($input->getValue()) ? "selected" : "";
208 $inputHtml .= ">{$formatted}</option>";
209 }
210 }
211
212 $inputHtml .= '</select>';
213
214 echo $inputHtml;
215 $params = \Bitrix\Main\Web\Json::encode(
216 [
217 'elementId' => $inputCode,
218 'conditionElementId' => $checkboxCode
219 ]
220 );
221
222 echo "<script>new BX.Sender.SecretBlock(".$params.")</script>";
223
224 return ob_get_clean();
225 };
226
227 $sendingStart->setView($view($sendingStart, $checkbox));
228 $sendingEnd->setView($view($sendingEnd, $checkbox));
229 }
230}
static getCurrent()
Definition context.php:241
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static prepareMessageConfigurationView($configuration)
static prepareMessageConfiguration($configuration)