PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/sender/lib/transport/timelimiter.php

https://gitlab.com/alexprowars/bitrix
PHP | 230 lines | 159 code | 32 blank | 39 comment | 7 complexity | 2237fca10d382d848cc9fbd04b6c079e MD5 | raw file
  1. <?php
  2. namespace Bitrix\Sender\Transport;
  3. use Bitrix\Main\Config\Option;
  4. use Bitrix\Main\Context;
  5. use Bitrix\Main\Localization\Loc;
  6. use Bitrix\Main\Type\DateTime;
  7. use Bitrix\Main\UI\Extension;
  8. use Bitrix\Sender\Entity\Letter;
  9. use Bitrix\Sender\Message;
  10. Loc::loadMessages(__FILE__);
  11. class TimeLimiter implements iLimiter
  12. {
  13. public const DEFAULT_SENDING_START = '09:00';
  14. public const DEFAULT_SENDING_END = '18:00';
  15. /**
  16. * @var Message\iBase
  17. */
  18. private $letter;
  19. private $parameters;
  20. public function withLetter($letter)
  21. {
  22. $this->letter = $letter;
  23. return $this;
  24. }
  25. /**
  26. * Create instance.
  27. * @return static
  28. */
  29. public static function create()
  30. {
  31. return new static();
  32. }
  33. /**
  34. * @inheritDoc
  35. */
  36. public function getLimit()
  37. {
  38. return 1;
  39. }
  40. /**
  41. * @inheritDoc
  42. */
  43. public function getCurrent()
  44. {
  45. if (!$this->letter)
  46. {
  47. return 0;
  48. }
  49. $sendingStart = $this->letter->getConfiguration()->get('SENDING_START');
  50. $sendingEnd = $this->letter->getConfiguration()->get('SENDING_END');
  51. $sendingTimeEnabled = $this->letter->getConfiguration()->get('SENDING_TIME');
  52. if (!$sendingEnd || !$sendingStart || $sendingTimeEnabled !== 'Y')
  53. {
  54. return 0;
  55. }
  56. $checkTime = strtotime($sendingStart);
  57. $sendingStart = strtotime($sendingStart);
  58. $sendingEnd = strtotime($sendingEnd);
  59. $currentTime = strtotime((new DateTime())->format("H:i:s"));
  60. $sendingStart = $sendingStart > $sendingEnd ? $sendingEnd : $sendingStart;
  61. $sendingEnd = $checkTime > $sendingEnd ? $checkTime : $sendingEnd;
  62. $this->setParameter('sendingStart', $sendingStart);
  63. $this->setParameter('sendingEnd', $sendingEnd);
  64. $this->setParameter('currentTime', $currentTime);
  65. if ($currentTime > $sendingStart && $currentTime < $sendingEnd)
  66. {
  67. return 0;
  68. }
  69. return 1;
  70. }
  71. /**
  72. * @inheritDoc
  73. */
  74. public function getUnitName()
  75. {
  76. return '';
  77. }
  78. /**
  79. * @inheritDoc
  80. */
  81. public function getUnit()
  82. {
  83. return '';
  84. }
  85. /**
  86. * @inheritDoc
  87. */
  88. public function getCaption()
  89. {
  90. return '';
  91. }
  92. /**
  93. * @inheritDoc
  94. */
  95. public function getParameter($name)
  96. {
  97. return $this->parameters[$name] ?? null;
  98. }
  99. /**
  100. * @inheritDoc
  101. */
  102. public function setParameter($name, $value)
  103. {
  104. $this->parameters[$name] = $value;
  105. }
  106. /**
  107. * @inheritDoc
  108. */
  109. public function isHidden()
  110. {
  111. return true;
  112. }
  113. /**
  114. * add options SENDING_START and SENDING_END to the Message Configuration
  115. * @param $configuration
  116. */
  117. public static function prepareMessageConfiguration($configuration)
  118. {
  119. $configuration->addOption(new Message\ConfigurationOption([
  120. 'type' => Message\ConfigurationOption::TYPE_CHECKBOX,
  121. 'code' => 'SENDING_TIME',
  122. 'group' => Message\ConfigurationOption::GROUP_ADDITIONAL,
  123. 'name' => Loc::getMessage('SENDER_INTEGRATION_MESSAGE_CONFIG_SENDING_TIME'),
  124. 'show_in_list' => false,
  125. 'required' => false,
  126. 'value' => Option::get('sender', 'sending_time')
  127. ]));
  128. $configuration->addOption(new Message\ConfigurationOption([
  129. 'type' => Message\ConfigurationOption::TYPE_STRING,
  130. 'code' => 'SENDING_START',
  131. 'name' => Loc::getMessage('SENDER_INTEGRATION_MESSAGE_CONFIG_SENDING_START'),
  132. 'group' => Message\ConfigurationOption::GROUP_ADDITIONAL,
  133. 'show_in_list' => false,
  134. 'required' => false,
  135. 'value' => Option::get('sender', 'sending_start', TimeLimiter::DEFAULT_SENDING_START)
  136. ]));
  137. $configuration->addOption(new Message\ConfigurationOption([
  138. 'type' => Message\ConfigurationOption::TYPE_STRING,
  139. 'code' => 'SENDING_END',
  140. 'name' => Loc::getMessage('SENDER_INTEGRATION_MESSAGE_CONFIG_SENDING_END'),
  141. 'group' => Message\ConfigurationOption::GROUP_ADDITIONAL,
  142. 'show_in_list' => false,
  143. 'required' => false,
  144. 'value' => Option::get('sender', 'sending_end', TimeLimiter::DEFAULT_SENDING_END)
  145. ]));
  146. }
  147. /**
  148. * add options view SENDING_START and SENDING_END to the Message Configuration
  149. * @param $configuration
  150. */
  151. public static function prepareMessageConfigurationView($configuration)
  152. {
  153. $sendingStart = $configuration->getOption('SENDING_START');
  154. $sendingEnd = $configuration->getOption('SENDING_END');
  155. $checkbox = $configuration->getOption('SENDING_TIME');
  156. $view = function($input, $checkbox)
  157. {
  158. $prefix = 'CONFIGURATION_';
  159. $inputCode = htmlspecialcharsbx($prefix.$input->getCode());
  160. $checkboxCode = htmlspecialcharsbx($prefix.$checkbox->getCode());
  161. ob_start();
  162. Extension::load("sender.secret_block");
  163. $inputHtml = "<select
  164. id=\"$inputCode\"
  165. name=\"$inputCode\"
  166. value='".$input->getValue()."'
  167. class=\"bx-sender-form-control bx-sender-message-editor-field-select\">";
  168. for ($hour = 0; $hour < 24; $hour++)
  169. {
  170. foreach ([0, 30] as $minute)
  171. {
  172. $time = strtotime(sprintf("%02d:%02d", $hour, $minute));
  173. $formatted = (new \DateTime())
  174. ->setTimestamp($time)
  175. ->format(Context::getCurrent()
  176. ->getCulture()
  177. ->getShortTimeFormat()
  178. );
  179. $inputHtml .= "<option value='{$formatted}'";
  180. $inputHtml .= $time === strtotime($input->getValue()) ? "selected" : "";
  181. $inputHtml .= ">{$formatted}</option>";
  182. }
  183. }
  184. $inputHtml .= '</select>';
  185. echo $inputHtml;
  186. $params = \Bitrix\Main\Web\Json::encode(
  187. [
  188. 'elementId' => $inputCode,
  189. 'conditionElementId' => $checkboxCode
  190. ]
  191. );
  192. echo "<script>new BX.Sender.SecretBlock(".$params.")</script>";
  193. return ob_get_clean();
  194. };
  195. $sendingStart->setView($view($sendingStart, $checkbox));
  196. $sendingEnd->setView($view($sendingEnd, $checkbox));
  197. }
  198. }