PageRenderTime 68ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/components/bitrix/sender.letter.time/class.php

https://gitlab.com/alexprowars/bitrix
PHP | 333 lines | 277 code | 48 blank | 8 comment | 36 complexity | 30206c015eea0c6d99ffaa9562b65018 MD5 | raw file
  1. <?php
  2. use Bitrix\Main\Context;
  3. use Bitrix\Main\Error;
  4. use Bitrix\Main\ErrorCollection;
  5. use Bitrix\Main\Localization\Loc;
  6. use Bitrix\Main\Mail\SenderSendCounter;
  7. use Bitrix\Main\Type;
  8. use Bitrix\Main\Web\Uri;
  9. use Bitrix\Main\ModuleManager;
  10. use Bitrix\Sender\Access\ActionDictionary;
  11. use Bitrix\Sender\Dispatch;
  12. use Bitrix\Sender\Entity;
  13. use Bitrix\Sender\Integration;
  14. use Bitrix\Sender\Internals\CommonSenderComponent;
  15. use Bitrix\Sender\Internals\PrettyDate;
  16. use Bitrix\Sender\Security;
  17. if (!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED !== true)
  18. {
  19. die();
  20. }
  21. if (!Bitrix\Main\Loader::includeModule('sender'))
  22. {
  23. ShowError('Module `sender` not installed');
  24. die();
  25. }
  26. Loc::loadMessages(__FILE__);
  27. class SenderLetterTimeComponent extends CommonSenderComponent
  28. {
  29. /** @var ErrorCollection $errors */
  30. protected $errors;
  31. protected $user_errors = [];
  32. /** @var Entity\Letter $letter */
  33. protected $letter;
  34. protected function checkRequiredParams()
  35. {
  36. if (!$this->arParams['ID'])
  37. {
  38. $this->errors->setError(new Error(Loc::getMessage('SENDER_LETTER_TIME_COMP_ERR_NO_LETTER')));
  39. }
  40. return $this->errors->isEmpty();
  41. }
  42. protected function initParams()
  43. {
  44. global $USER;
  45. if (empty($this->arParams['ID']))
  46. {
  47. $this->arParams['ID'] = (int) $this->request->get('ID');
  48. }
  49. $this->arParams['IS_OUTSIDE'] = isset($this->arParams['IS_OUTSIDE']) ? (bool) $this->arParams['IS_OUTSIDE'] : $this->request->get('isOutside') === 'Y';
  50. $this->arParams['SET_TITLE'] = isset($this->arParams['SET_TITLE']) ? $this->arParams['SET_TITLE'] == 'Y' : true;
  51. $this->arParams['IS_BX24_INSTALLED'] = Integration\Bitrix24\Service::isCloud();
  52. $this->arParams['IS_PHONE_CONFIRMED'] = \Bitrix\Sender\Integration\Bitrix24\Limitation\Verification::isPhoneConfirmed();
  53. $this->arParams['CAN_VIEW'] =
  54. isset($this->arParams['CAN_VIEW'])
  55. ? $this->arParams['CAN_VIEW']
  56. : Security\Access::getInstance()->canViewLetters()
  57. ;
  58. $transportCode = self::getTransportCode($this->arParams['ID'], $this->arParams['MESSAGE_CODE_LIST']);
  59. $this->arParams['IS_MAIL_TRANSPORT'] = $transportCode === \Bitrix\Sender\Transport\iBase::CODE_MAIL;
  60. }
  61. private static function getTransportCode($letterId, $transportCodesAlist): ?string
  62. {
  63. if ($letter = Entity\Letter::createInstanceById($letterId, $transportCodesAlist))
  64. {
  65. return $letter->getMessage()->getTransport()->getCode();
  66. }
  67. return null;
  68. }
  69. protected function preparePost()
  70. {
  71. $dateTime = null;
  72. $code = $this->request->get('LETTER_TIME');
  73. if (Type\DateTime::isCorrect($code))
  74. {
  75. /** @var Type\DateTime $dateTime Time */
  76. $dateTime = new Type\DateTime($code);
  77. $diff = \CTimeZone::GetOffset();
  78. $dateTime->add(($diff > 0 ? "-" : "") . "PT" . abs($diff) . "S");
  79. $code = 'time';
  80. }
  81. $userId = Security\User::current()->getId();
  82. if ($userId)
  83. {
  84. $this->letter->set('UPDATED_BY', $userId);
  85. }
  86. $method = $this->letter->getMethod();
  87. if ($method->canChange() && $this->accessController->check(ActionDictionary::ACTION_MAILING_PAUSE_START_STOP))
  88. {
  89. switch ($code)
  90. {
  91. case 'now':
  92. $method->now();
  93. break;
  94. case Dispatch\Method::SCHEDULE:
  95. $scheduleTime = Dispatch\MethodSchedule::parseTimesOfDay($this->request->get('TIMES_OF_DAY'));
  96. $scheduleMonths = Dispatch\MethodSchedule::parseMonthsOfYear($this->request->get('MONTHS_OF_YEAR'));
  97. $scheduleWeekDays = Dispatch\MethodSchedule::parseDaysOfWeek($this->request->get('DAYS_OF_WEEK'));
  98. $scheduleMonthDays = Dispatch\MethodSchedule::parseDaysOfMonth($this->request->get('DAYS_OF_MONTH'));
  99. if (empty($scheduleTime))
  100. {
  101. $this->errors->setError(new Error(Loc::getMessage('SENDER_LETTER_TIME_COMP_ERR_SCHEDULE_WRONG_TIME')));
  102. }
  103. if (empty($scheduleWeekDays))
  104. {
  105. $this->errors->setError(new Error(Loc::getMessage('SENDER_LETTER_TIME_COMP_ERR_SCHEDULE_WRONG_WEEK_DAYS')));
  106. }
  107. $method->set(
  108. (new Dispatch\MethodSchedule($this->letter))
  109. ->setMonthsOfYear($scheduleMonths)
  110. ->setDaysOfMonth($scheduleMonthDays)
  111. ->setDaysOfWeek($scheduleWeekDays)
  112. ->setTime($scheduleTime[0], $scheduleTime[1])
  113. );
  114. break;
  115. case Dispatch\Method::TIME:
  116. $method->time($dateTime);
  117. break;
  118. case Dispatch\Method::DEFERED:
  119. default:
  120. $method->defer();
  121. break;
  122. }
  123. if ($this->letter->hasErrors())
  124. {
  125. $this->errors->add($this->letter->getErrors());
  126. return;
  127. }
  128. $method->apply();
  129. if ($this->letter->hasErrors())
  130. {
  131. foreach ($this->letter->getErrorCollection() as $error)
  132. {
  133. /** @var \Bitrix\Main\Error $error Error. */
  134. if ($error->getCode())
  135. $this->user_errors[] = $error;
  136. }
  137. $this->errors->add($this->letter->getErrors());
  138. return;
  139. }
  140. }
  141. if ($this->errors->isEmpty())
  142. {
  143. $url = str_replace('#id#', $this->letter->getId(), $this->arParams['PATH_TO_TIME']);
  144. $uri = new Uri($url);
  145. if ($this->arParams['IFRAME'] == 'Y')
  146. {
  147. $uri->addParams(array('IFRAME' => 'Y'));
  148. $uri->addParams(array('IS_SAVED' => 'Y'));
  149. }
  150. if ($this->arParams['IS_OUTSIDE'])
  151. {
  152. $uri->addParams(array('isOutside' => 'Y'));
  153. }
  154. LocalRedirect($uri->getLocator());
  155. }
  156. }
  157. protected function prepareResult()
  158. {
  159. /* Set title */
  160. if ($this->arParams['SET_TITLE'])
  161. {
  162. /**@var CAllMain*/
  163. $GLOBALS['APPLICATION']->SetTitle($this->getMessage('SENDER_LETTER_TIME_COMP_TITLE'));
  164. }
  165. if (!$this->arParams['CAN_VIEW'])
  166. {
  167. Security\AccessChecker::addError($this->errors);
  168. return false;
  169. }
  170. $this->letter = Entity\Letter::createInstanceById(
  171. $this->arParams['ID'],
  172. $this->arParams['MESSAGE_CODE_LIST']
  173. );
  174. if (!$this->letter)
  175. {
  176. Security\AccessChecker::addError($this->errors, Security\AccessChecker::ERR_CODE_NOT_FOUND);
  177. return false;
  178. }
  179. $email = $this->letter->getMessage()->getConfiguration()->get('EMAIL_FROM', false);
  180. if ($email)
  181. {
  182. $this->arParams['DAY_LIMIT'] = Bitrix\Main\Mail\Sender::getEmailLimit($email);
  183. }
  184. $this->arParams['CAN_EDIT'] = isset($this->arParams['CAN_EDIT'])
  185. ?
  186. $this->arParams['CAN_EDIT']
  187. :
  188. Security\Access::getInstance()->canStopStartPause(get_class($this->letter));
  189. if (!$this->letter->getId())
  190. {
  191. $this->errors->setError(new Error(Loc::getMessage('SENDER_LETTER_TIME_COMP_ERR_NO_LETTER')));
  192. return false;
  193. }
  194. // Process POST
  195. if ($this->request->isPost() && check_bitrix_sessid() && $this->arParams['CAN_EDIT'])
  196. {
  197. $this->preparePost();
  198. $this->printErrors();
  199. $this->arResult['USER_ERRORS'] = $this->user_errors;
  200. }
  201. $this->arResult['SUBMIT_FORM_URL'] = Context::getCurrent()->getRequest()->getRequestUri();
  202. $this->arResult['ACTION_URL'] = $this->getPath() . '/ajax.php';
  203. $this->arResult['TITLE'] = $this->letter->get('TITLE');
  204. $method = $this->letter->getMethod();
  205. $code = $this->request->get('METHOD_CODE') ?: $method->getCode();
  206. switch ($code)
  207. {
  208. case Dispatch\Method::TIME:
  209. /** @var Dispatch\MethodTime $methodInstance */
  210. $methodInstance = $method->get();
  211. $code = $methodInstance->getDateTime();
  212. $this->arResult['DATE_SEND'] = $this->letter->get('DATE_SEND') ?: $this->letter->get('AUTO_SEND_TIME');
  213. $this->arResult['DATE_SEND'] = PrettyDate::formatDateTime($this->arResult['DATE_SEND']);
  214. break;
  215. case Dispatch\Method::SCHEDULE:
  216. $code = Dispatch\Method::SCHEDULE;
  217. $this->arResult['DATE_SEND'] = $this->letter->get('DATE_SEND') ?: $this->letter->get('AUTO_SEND_TIME');
  218. $this->arResult['DATE_SEND'] = PrettyDate::formatDateTime($this->arResult['DATE_SEND']);
  219. break;
  220. case Dispatch\Method::DEFERED:
  221. default:
  222. $code = Dispatch\Method::DEFERED;
  223. $this->arResult['DATE_SEND'] = '';
  224. break;
  225. }
  226. $this->arResult['LETTER_TIME'] = $code;
  227. $this->arResult['CAN_CHANGE'] = $method->canChange() && $this->arParams['CAN_EDIT'];
  228. foreach (['DAYS_OF_MONTH', 'DAYS_OF_WEEK', 'MONTHS_OF_YEAR', 'TIMES_OF_DAY'] as $key)
  229. {
  230. $this->arResult[$key] = $this->letter->get($key) ?: $this->request->get($key);
  231. }
  232. $this->arResult['TIME_LIST'] = Dispatch\MethodSchedule::getTimeList();
  233. $this->arResult['IS_SAVED'] = $this->request->get('IS_SAVED') == 'Y';
  234. $this->arResult['IS_SUPPORT_REITERATE'] = $this->letter->isSupportReiterate();
  235. $this->arResult['IS_SUPPORT_REITERATE_DAYS'] = !Integration\Bitrix24\Service::isPortal();
  236. $this->arResult['LIMITATION'] = array();
  237. $transport = $this->letter->getMessage()->getTransport();
  238. if ($transport->getCode() === $transport::CODE_MAIL)
  239. {
  240. if (Integration\Bitrix24\Service::isCloud())
  241. {
  242. $limiter = Integration\Bitrix24\Limitation\Limiter::getMonthly();
  243. $this->arResult['LIMITATION']['TEXT'] = $limiter->getText();
  244. $this->arResult['LIMITATION']['SETUP_URI'] = $limiter->getParameter('setupUri');
  245. $this->arResult['LIMITATION']['SETUP_CAPTION'] = $limiter->getParameter('setupCaption');
  246. }
  247. }
  248. return true;
  249. }
  250. protected function printErrors()
  251. {
  252. foreach ($this->errors as $error)
  253. {
  254. if (!in_array($error, $this->user_errors))
  255. {
  256. ShowError($error);
  257. }
  258. }
  259. }
  260. public function executeComponent()
  261. {
  262. parent::executeComponent();
  263. parent::prepareResultAndTemplate();
  264. }
  265. public function getMessage($messageCode, $replace = [])
  266. {
  267. if (empty($this->arParams['~MESS'][$messageCode]))
  268. {
  269. return Loc::getMessage($messageCode, $replace);
  270. }
  271. return str_replace(
  272. array_keys($replace),
  273. array_values($replace),
  274. $this->arParams['~MESS'][$messageCode]
  275. );
  276. }
  277. public function getEditAction()
  278. {
  279. return $this->getViewAction();
  280. }
  281. public function getViewAction()
  282. {
  283. return ActionDictionary::ACTION_MAILING_VIEW;
  284. }
  285. }