PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/calendar/lib/ical/mailinvitation/senderinvitation.php

https://gitlab.com/alexprowars/bitrix
PHP | 333 lines | 209 code | 38 blank | 86 comment | 14 complexity | 7bd8fd9549abfc93a76b3aab706f4942 MD5 | raw file
  1. <?php
  2. namespace Bitrix\Calendar\ICal\MailInvitation;
  3. use Bitrix\Calendar\ICal\Builder\Attach;
  4. use Bitrix\Calendar\ICal\Builder\Attendee;
  5. use Bitrix\Calendar\SerializeObject;
  6. use Bitrix\Calendar\Util;
  7. use Bitrix\Mail;
  8. use Bitrix\Main\Loader;
  9. use Bitrix\Main\LoaderException;
  10. use Bitrix\Main\Localization\Loc;
  11. use Bitrix\Main\Mail\Event;
  12. use CEvent;
  13. use COption;
  14. use \Serializable;
  15. /**
  16. * Class SenderInvitation
  17. * @package Bitrix\Calendar\ICal\MailInvitation
  18. */
  19. abstract class SenderInvitation implements Serializable
  20. {
  21. use SerializeObject;
  22. public const CHARSET = 'utf-8';
  23. public const CONTENT_TYPE = 'text/calendar';
  24. public const DECISION_YES = 'Y';
  25. public const DECISION_NO = 'N';
  26. protected const ATTACHMENT_NAME = 'invite.ics';
  27. protected const MAIL_TEMPLATE = 'SEND_ICAL_INVENT';
  28. /**
  29. * @var int
  30. */
  31. protected $counterInvitations = 0;
  32. /**
  33. * @var array
  34. */
  35. protected $event;
  36. /**
  37. * @var Context
  38. */
  39. protected $context;
  40. /**
  41. * @var string
  42. */
  43. protected $uid = '';
  44. abstract public function executeAfterSuccessfulInvitation();
  45. abstract protected function getContent();
  46. abstract protected function getMailEventField();
  47. abstract protected function getSubjectTitle();
  48. public static function createInstance(array $event, Context $context): SenderInvitation
  49. {
  50. return new static($event, $context);
  51. }
  52. public function __construct(array $event, Context $context)
  53. {
  54. $this->event = $event;
  55. $this->context = $context;
  56. }
  57. /**
  58. * @return bool
  59. * @throws LoaderException
  60. */
  61. public function send(): bool
  62. {
  63. $this->checkEventOrganizer();
  64. $this->checkAddresserEmail();
  65. $this->prepareEventFields();
  66. $status = CEvent::sendImmediate(
  67. self::MAIL_TEMPLATE,
  68. SITE_ID,
  69. $this->getMailEventField(),
  70. "Y",
  71. "",
  72. [],
  73. '',
  74. $this->getContent()
  75. );
  76. return $status === Event::SEND_RESULT_SUCCESS;
  77. }
  78. /**
  79. * @return string
  80. */
  81. public function getMethod(): string
  82. {
  83. return static::METHOD;
  84. }
  85. /**
  86. * @return array
  87. */
  88. public function getEvent(): array
  89. {
  90. return $this->event;
  91. }
  92. /**
  93. *
  94. */
  95. public function incrementCounterInvitations(): void
  96. {
  97. $this->counterInvitations++;
  98. }
  99. /**
  100. * @return string
  101. */
  102. public function getUid(): string
  103. {
  104. return $this->uid;
  105. }
  106. /**
  107. * @return int
  108. */
  109. public function getCountAttempsSend(): int
  110. {
  111. return $this->counterInvitations;
  112. }
  113. /**
  114. * @return MailReceiver
  115. */
  116. public function getReceiver(): MailReceiver
  117. {
  118. return $this->context->getReceiver();
  119. }
  120. /**
  121. * @return int
  122. */
  123. public function getEventId(): int
  124. {
  125. return (int) $this->event['ID'];
  126. }
  127. /**
  128. * @return int
  129. */
  130. public function getEventParentId(): int
  131. {
  132. return (int) $this->event['PARENT_ID'];
  133. }
  134. /**
  135. * @throws LoaderException
  136. */
  137. protected function checkAddresserEmail(): void
  138. {
  139. if (Loader::includeModule('mail') && empty($this->context->getAddresser()->getMailto()))
  140. {
  141. $boxes = Mail\MailboxTable::getUserMailboxes($this->event['MEETING_HOST']);
  142. if (!empty($boxes) && is_array($boxes))
  143. {
  144. $this->context->getAddresser()->setMailto(array_shift($boxes)['EMAIL']);
  145. }
  146. }
  147. }
  148. /**
  149. * @return string
  150. */
  151. protected function getBodyMessage(): string
  152. {
  153. //@TODO edit body message
  154. return 'ical body message';
  155. }
  156. /**
  157. * @return string
  158. * @throws \Bitrix\Main\ObjectException
  159. */
  160. protected function getDateForTemplate(): string
  161. {
  162. $res = Helper::getIcalTemplateDate([
  163. 'DATE_FROM' => $this->event['DATE_FROM'],
  164. 'DATE_TO' => $this->event['DATE_TO'],
  165. 'TZ_FROM' => $this->event['TZ_FROM'],
  166. 'TZ_TO' => $this->event['TZ_TO'],
  167. 'FULL_DAY' => $this->event['SKIP_TIME'],
  168. ]);
  169. $offset = (Helper::getDateObject(null, false, $this->event['TZ_FROM']))->format('P');
  170. $res .= ' (' . $this->event['TZ_FROM'] . ', ' . 'UTC' . $offset . ')';
  171. if (isset($this->event['RRULE']['FREQ']) && $this->event['RRULE']['FREQ'] !== 'NONE')
  172. {
  173. $rruleString = Helper::getIcalTemplateRRule($this->event['RRULE'],
  174. [
  175. 'DATE_FROM' => $this->event['DATE_FROM'],
  176. 'DATE_TO' => $this->event['DATE_TO'],
  177. 'TZ_FROM' => $this->event['TZ_FROM'],
  178. 'TZ_TO' => $this->event['TZ_TO'],
  179. 'FULL_DAY' => $this->event['SKIP_TIME'],
  180. ]
  181. );
  182. $res .= ', (' . $rruleString . ')';
  183. }
  184. return $res;
  185. }
  186. /**
  187. * @return string
  188. */
  189. protected function getFilesLink(): string
  190. {
  191. $result = "";
  192. if (is_iterable($this->event['ICAL_ATTACHES']))
  193. {
  194. foreach ($this->event['ICAL_ATTACHES'] as $attach)
  195. {
  196. if ($attach instanceof Attach)
  197. {
  198. $result .= "<a href=\"{$attach->getLink()}\"> {$attach->getName() }</a> <br />";
  199. }
  200. }
  201. }
  202. return $result;
  203. }
  204. /**
  205. * @return string
  206. */
  207. protected function getSiteName(): string
  208. {
  209. if (!empty($siteName = COption::GetOptionString("main", "site_name", '', '-')))
  210. {
  211. return "[{$siteName}]";
  212. }
  213. return '';
  214. }
  215. /**
  216. * @return string
  217. */
  218. protected function getSubjectMessage(): string
  219. {
  220. return $this->getSiteName(). ' ' . $this->getSubjectTitle();
  221. }
  222. /**
  223. * @return string
  224. */
  225. protected function getMessageId(): string
  226. {
  227. $serverName = COption::GetOptionString("main", "server_name", $GLOBALS["SERVER_NAME"]);
  228. return "<CALENDAR_EVENT_{$this->getEventParentId()}@{$serverName}>";
  229. }
  230. /**
  231. * @return string
  232. */
  233. protected function getAttendeesListForTemplate(): string
  234. {
  235. if ($this->event['MEETING']['HIDE_GUESTS'])
  236. {
  237. return Loc::getMessage('EC_CALENDAR_ICAL_MAIL_HIDE_GUESTS_INFORMATION');
  238. }
  239. return $this->event['ICAL_ATTENDEES'];
  240. }
  241. /**
  242. * @return int
  243. */
  244. protected function getEventDateCreateTimestamp(): int
  245. {
  246. return (int) Util::getTimestamp($this->event['CREATED']);
  247. }
  248. /**
  249. * @return int
  250. */
  251. protected function getEventOwnerId(): int
  252. {
  253. return (int) $this->event['OWNER_ID'];
  254. }
  255. /**
  256. * @throws \Bitrix\Main\ArgumentException
  257. * @throws \Bitrix\Main\ObjectPropertyException
  258. * @throws \Bitrix\Main\SystemException
  259. */
  260. protected function prepareEventFields(): void
  261. {
  262. $this->event['DESCRIPTION'] = Helper::getEventDescriptionById((int) $this->event['ID']);
  263. if (is_array($this->event['TEXT_LOCATION']) && isset($this->event['TEXT_LOCATION']['NEW']))
  264. {
  265. $this->event['TEXT_LOCATION'] = $this->event['TEXT_LOCATION']['NEW'];
  266. }
  267. elseif (!is_string($this->event['TEXT_LOCATION']))
  268. {
  269. $this->event['TEXT_LOCATION'] = null;
  270. }
  271. }
  272. /**
  273. * @throws \Bitrix\Main\ArgumentException
  274. * @throws \Bitrix\Main\ObjectPropertyException
  275. * @throws \Bitrix\Main\SystemException
  276. */
  277. protected function checkEventOrganizer(): void
  278. {
  279. if (empty($this->event['ICAL_ORGANIZER']))
  280. {
  281. if ($user = Helper::getUserById($this->event['MEETING_HOST']))
  282. {
  283. $this->event['ICAL_ORGANIZER'] = Attendee::createInstance(
  284. $user['EMAIL'],
  285. $user['NAME'],
  286. $user['LAST_NAME'],
  287. null,
  288. null,
  289. null,
  290. $user['EMAIL']
  291. );
  292. }
  293. }
  294. }
  295. }