PageRenderTime 51ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/bitrix/modules/sender/lib/subscription.php

https://gitlab.com/Rad1calDreamer/honey
PHP | 401 lines | 292 code | 42 blank | 67 comment | 33 complexity | 80c8a8b4253c7e35dc8b850f29fe5f78 MD5 | raw file
  1. <?php
  2. /**
  3. * Bitrix Framework
  4. * @package bitrix
  5. * @subpackage sender
  6. * @copyright 2001-2012 Bitrix
  7. */
  8. namespace Bitrix\Sender;
  9. use Bitrix\Main\EventResult;
  10. class Subscription
  11. {
  12. const MODULE_ID = 'sender';
  13. /**
  14. * @param array $fields
  15. * @return string
  16. */
  17. public static function getLinkUnsub(array $fields)
  18. {
  19. return \Bitrix\Main\Mail\Tracking::getLinkUnsub(static::MODULE_ID, $fields, \Bitrix\Main\Config\Option::get('sender', 'unsub_link'));
  20. }
  21. /**
  22. * @param array $fields
  23. * @return string
  24. */
  25. public static function getLinkSub(array $fields)
  26. {
  27. $tag = \Bitrix\Main\Mail\Tracking::getSignedTag(static::MODULE_ID, $fields);
  28. $urlPage = \Bitrix\Main\Config\Option::get('sender', 'sub_link');
  29. if($urlPage == "")
  30. {
  31. $bitrixDirectory = \Bitrix\Main\Application::getInstance()->getPersonalRoot();
  32. $result = $bitrixDirectory.'/tools/sender_sub_confirm.php?sender_subscription=confirm&tag='.urlencode($tag);
  33. }
  34. else
  35. {
  36. $result = $urlPage.(strpos($urlPage, "?")===false ? "?" : "&").'sender_subscription=confirm&tag='.urlencode($tag);
  37. }
  38. return $result;
  39. }
  40. /**
  41. * @param $data
  42. * @return mixed
  43. */
  44. public static function onMailEventSubscriptionList($data)
  45. {
  46. $data['LIST'] = static::getList($data);
  47. return $data;
  48. }
  49. /**
  50. * @param $data
  51. * @return EventResult
  52. */
  53. public static function onMailEventSubscriptionEnable($data)
  54. {
  55. $data['SUCCESS'] = static::subscribe($data);
  56. if($data['SUCCESS'])
  57. $result = EventResult::SUCCESS;
  58. else
  59. $result = EventResult::ERROR;
  60. return new EventResult($result, $data, static::MODULE_ID);
  61. }
  62. /**
  63. * @param $data
  64. * @return EventResult
  65. */
  66. public static function onMailEventSubscriptionDisable($data)
  67. {
  68. $data['SUCCESS'] = static::unsubscribe($data);
  69. if($data['SUCCESS'])
  70. $result = EventResult::SUCCESS;
  71. else
  72. $result = EventResult::ERROR;
  73. return new EventResult($result, $data, static::MODULE_ID);
  74. }
  75. /**
  76. * @param $data
  77. * @return array
  78. * @throws \Bitrix\Main\ArgumentException
  79. */
  80. public static function getList($data)
  81. {
  82. $resultMailingList = array();
  83. if(isset($data['TEST']) && $data['TEST'] == 'Y')
  84. {
  85. $mailing = MailingTable::getRowById(array('ID' => $data['MAILING_ID']));
  86. if($mailing)
  87. {
  88. $resultMailingList[] = array(
  89. 'ID' => $mailing['ID'],
  90. 'NAME' => $mailing['NAME'],
  91. 'DESC' => $mailing['DESCRIPTION'],
  92. 'SELECTED' => true,
  93. );
  94. }
  95. return $resultMailingList;
  96. }
  97. $mailingUnsub = array();
  98. $recipientUnsubDb = PostingUnsubTable::getList(array(
  99. 'select' => array('MAILING_ID' => 'POSTING.MAILING_ID'),
  100. 'filter' => array('=POSTING_RECIPIENT.EMAIL' => trim(strtolower($data['EMAIL'])))
  101. ));
  102. while($recipientUnsub = $recipientUnsubDb->fetch())
  103. $mailingUnsub[] = $recipientUnsub['MAILING_ID'];
  104. $mailingList = array();
  105. // all receives mailings
  106. $mailingDb = PostingRecipientTable::getList(array(
  107. 'select' => array('MAILING_ID' => 'POSTING.MAILING.ID'),
  108. 'filter' => array(
  109. '=EMAIL' => trim(strtolower($data['EMAIL'])),
  110. '=POSTING.MAILING.ACTIVE' => 'Y',
  111. ),
  112. 'group' => array('MAILING_ID')
  113. ));
  114. while ($mailing = $mailingDb->fetch())
  115. {
  116. $mailingList[] = $mailing['MAILING_ID'];
  117. }
  118. // all subscribed mailings
  119. $mailingDb = MailingSubscriptionTable::getList(array(
  120. 'select' => array('MAILING_ID'),
  121. 'filter' => array(
  122. '=CONTACT.EMAIL' => trim(strtolower($data['EMAIL'])),
  123. '=MAILING.ACTIVE' => 'Y',
  124. )
  125. ));
  126. while ($mailing = $mailingDb->fetch())
  127. {
  128. $mailingList[] = $mailing['MAILING_ID'];
  129. }
  130. $mailingList = array_unique($mailingList);
  131. foreach($mailingList as $mailingId)
  132. {
  133. if(!in_array($mailingId, $mailingUnsub))
  134. {
  135. $mailingDesc = MailingTable::getRowById($mailingId);
  136. if($mailingDesc)
  137. {
  138. $resultMailingList[] = array(
  139. 'ID' => $mailingDesc['ID'],
  140. 'NAME' => $mailingDesc['NAME'],
  141. 'DESC' => $mailingDesc['DESCRIPTION'],
  142. 'SELECTED' => in_array($mailingDesc['ID'], array($data['MAILING_ID'])),
  143. );
  144. }
  145. }
  146. }
  147. return $resultMailingList;
  148. }
  149. /**
  150. * @param $data
  151. * @return bool
  152. */
  153. public static function subscribe($data)
  154. {
  155. $id = static::add($data['EMAIL'], $data['SUBSCRIBE_LIST']);
  156. if($id)
  157. {
  158. return true;
  159. }
  160. return false;
  161. }
  162. /**
  163. * @param $data
  164. * @return bool
  165. * @throws \Bitrix\Main\ArgumentException
  166. */
  167. public static function unsubscribe($data)
  168. {
  169. $result = false;
  170. if(isset($data['TEST']) && $data['TEST'] == 'Y')
  171. return true;
  172. $posting = null;
  173. if($data['RECIPIENT_ID'])
  174. {
  175. $postingDb = PostingRecipientTable::getList(array(
  176. 'select' => array('POSTING_ID', 'POSTING_MAILING_ID' => 'POSTING.MAILING_ID'),
  177. 'filter' => array('ID' => $data['RECIPIENT_ID'], 'EMAIL' => $data['EMAIL'])
  178. ));
  179. $posting = $postingDb->fetch();
  180. }
  181. $mailingDb = MailingTable::getList(array(
  182. 'select' => array('ID'),
  183. 'filter' => array(
  184. 'ID' => $data['UNSUBSCRIBE_LIST'],
  185. )
  186. ));
  187. while($mailing = $mailingDb->fetch())
  188. {
  189. $unsub = null;
  190. if($posting && $posting['POSTING_MAILING_ID'] == $mailing['ID'])
  191. {
  192. $unsub = array(
  193. 'POSTING_ID' => $posting['POSTING_ID'],
  194. 'RECIPIENT_ID' => $data['RECIPIENT_ID'],
  195. );
  196. }
  197. else
  198. {
  199. $mailingPostingDb = PostingRecipientTable::getList(array(
  200. 'select' => array('RECIPIENT_ID' => 'ID', 'POSTING_ID'),
  201. 'filter' => array('=POSTING.MAILING_ID' => $mailing['ID'], 'EMAIL' => $data['EMAIL'])
  202. ));
  203. if($mailingPosting = $mailingPostingDb->fetch())
  204. {
  205. $unsub = $mailingPosting;
  206. }
  207. }
  208. if(!empty($unsub))
  209. {
  210. $unsubExists = PostingUnsubTable::getRowById($unsub);
  211. if(!$unsubExists)
  212. {
  213. $unsubResult = PostingUnsubTable::add($unsub);
  214. if($unsubResult->isSuccess())
  215. {
  216. $eventData = array(
  217. 'MAILING_ID' => $mailing['ID'],
  218. 'RECIPIENT_ID' => $unsub['RECIPIENT_ID'],
  219. 'EMAIL' => $data['EMAIL'],
  220. );
  221. $event = new \Bitrix\Main\Event('sender', 'OnAfterRecipientUnsub', array($eventData));
  222. $event->send();
  223. }
  224. }
  225. $result = true;
  226. }
  227. $contactDb = ContactTable::getList(array(
  228. 'select' => array('ID'),
  229. 'filter' => array('=EMAIL' => $data['EMAIL'])
  230. ));
  231. while($contact = $contactDb->fetch())
  232. {
  233. MailingSubscriptionTable::delete(array('MAILING_ID' => $mailing['ID'], 'CONTACT_ID' => $contact['ID']));
  234. $result = true;
  235. }
  236. }
  237. return $result;
  238. }
  239. /**
  240. * @param string $email
  241. * @param array $mailingList
  242. * @param string $siteId
  243. * @return int $contactId
  244. * //add subscription and returns subscription id
  245. */
  246. public static function add($email, array $mailingIdList)
  247. {
  248. $contactId = null;
  249. $email = strtolower($email);
  250. $contactDb = ContactTable::getList(array('filter' => array('=EMAIL' => $email)));
  251. if($contact = $contactDb->fetch())
  252. {
  253. $contactId = $contact['ID'];
  254. }
  255. else
  256. {
  257. $contactAddDb = ContactTable::add(array('EMAIL' => $email));
  258. if($contactAddDb->isSuccess())
  259. $contactId = $contactAddDb->getId();
  260. }
  261. if(!empty($contactId))
  262. {
  263. foreach ($mailingIdList as $mailingId)
  264. {
  265. $primary = array('MAILING_ID' => $mailingId, 'CONTACT_ID' => $contactId);
  266. $existSub = MailingSubscriptionTable::getRowById($primary);
  267. if (!$existSub) MailingSubscriptionTable::add($primary);
  268. }
  269. }
  270. else
  271. {
  272. }
  273. return $contactId;
  274. }
  275. /**
  276. * @param array $params
  277. * @return array
  278. * // get mailing list
  279. */
  280. public static function getMailingList($params)
  281. {
  282. $filter = array("ACTIVE" => "Y", "IS_TRIGGER" => "N");
  283. if(isset($params["SITE_ID"]))
  284. $filter["SITE_ID"] = $params["SITE_ID"];
  285. if(isset($params["IS_PUBLIC"]))
  286. $filter["IS_PUBLIC"] = $params["IS_PUBLIC"];
  287. if(isset($params["ACTIVE"]))
  288. $filter["ACTIVE"] = $params["ACTIVE"];
  289. if(isset($params["ID"]))
  290. $filter["ID"] = $params["ID"];
  291. $mailingList = array();
  292. $mailingDb = MailingTable::getList(array(
  293. 'select' => array('ID', 'NAME', 'DESCRIPTION', 'IS_PUBLIC'),
  294. 'filter' => $filter,
  295. 'order' => array('SORT' => 'ASC', 'NAME' => 'ASC'),
  296. ));
  297. while($mailing = $mailingDb->fetch())
  298. {
  299. $mailingList[] = $mailing;
  300. }
  301. return $mailingList;
  302. }
  303. /**
  304. * @param string $email
  305. * @param array $mailingList
  306. * @param string $siteId
  307. * @return void
  308. * //send email with url to confirmation of subscription
  309. */
  310. public static function sendEventConfirm($email, array $mailingIdList, $siteId)
  311. {
  312. $mailingNameList = array();
  313. $mailingDb = MailingTable::getList(array('select' => array('NAME'), 'filter' => array("IS_TRIGGER" => "N", 'ID' => $mailingIdList)));
  314. while($mailing = $mailingDb->fetch())
  315. {
  316. $mailingNameList[] = $mailing['NAME'];
  317. }
  318. $subscription = array(
  319. 'EMAIL' => $email,
  320. 'SITE_ID' => $siteId,
  321. 'MAILING_LIST' => $mailingIdList,
  322. );
  323. $confirmUrl = static::getLinkSub($subscription);
  324. $date = new \Bitrix\Main\Type\DateTime;
  325. $eventSendFields = array(
  326. "EVENT_NAME" => "SENDER_SUBSCRIBE_CONFIRM",
  327. "C_FIELDS" => array(
  328. "EMAIL" => $email,
  329. "DATE" => $date->toString(),
  330. "CONFIRM_URL" => $confirmUrl,
  331. "MAILING_LIST" => implode("\r\n",$mailingNameList),
  332. ),
  333. "LID" => is_array($siteId)? implode(",", $siteId): $siteId,
  334. );
  335. \Bitrix\Main\Mail\Event::send($eventSendFields);
  336. }
  337. /**
  338. * @param int $mailingId
  339. * @param string $email
  340. * @return bool
  341. * //is email address was unsubscribed
  342. */
  343. public static function isUnsubscibed($mailingId, $email)
  344. {
  345. $email = strtolower($email);
  346. $recipientUnsubDb = PostingUnsubTable::getList(array(
  347. 'select' => array('ID'),
  348. 'filter' => array(
  349. '=POSTING.MAILING_ID' => $mailingId,
  350. '=POSTING_RECIPIENT.EMAIL' => $email
  351. )
  352. ));
  353. if($recipientUnsubDb->fetch())
  354. {
  355. return true;
  356. }
  357. return false;
  358. }
  359. }