Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
contact.php
1<?php
9
13
18
19Loc::loadMessages(__FILE__);
20
25class Contact extends Base
26{
33 public static function getList(array $parameters = array())
34 {
35 if (!isset($parameters['select']))
36 {
37 $parameters['select'] = array(
38 '*',
39 );
40 }
41 if (!isset($parameters['filter']))
42 {
43 $parameters['filter'] = array();
44 }
45
46 return ContactTable::getList($parameters);
47 }
48
54 protected function getDefaultData()
55 {
56 return array(
57 'NAME' => '',
58 'CODE' => '',
59 'TYPE_ID' => Recipient\Type::EMAIL,
60 'BLACKLISTED' => 'N',
61 'SET_LIST' => [],
62 'SUB_LIST' => [],
63 'UNSUB_LIST' => [],
64 );
65 }
66
75 protected function saveData($id, array $data)
76 {
77 $setList = array_filter($data['SET_LIST'], 'is_numeric');
78 $subList = array_filter($data['SUB_LIST'], 'is_numeric');
79 $unsubList = array_filter($data['UNSUB_LIST'], 'is_numeric');
80
81 $this->filterDataByEntityFields(ContactTable::getEntity(), $data);
82
83 try
84 {
85 $id = $this->saveByEntity(ContactTable::getEntity(), $id, $data);
86 }
87 catch (SqlQueryException $exception)
88 {
89 if (mb_strpos($exception->getMessage(), '(1062) Duplicate entry') !== false)
90 {
91 $this->errors->setError(new Error(Loc::getMessage('SENDER_ENTITY_CONTACT_ERROR_DUPLICATE')));
92 return $id;
93 }
94
95 throw $exception;
96 }
97
98 if ($this->hasErrors())
99 {
100 return $id;
101 }
102
103 $this->saveDataLists($id, $setList, $subList, $unsubList);
104
105 return $id;
106 }
107
108 protected function saveDataLists($id, $setList, $subList, $unsubList)
109 {
110 $setList = array_unique($setList);
111 $unsubList = array_unique($unsubList);
112 $subList = array_unique($subList);
113 $subList = array_diff($subList, $unsubList);
114
115 ContactListTable::deleteList(['CONTACT_ID' => $id]);
116 foreach ($setList as $itemId)
117 {
118 ContactListTable::add(['CONTACT_ID' => $id, 'LIST_ID' => $itemId]);
119 }
120
121 MailingSubscriptionTable::deleteList(['CONTACT_ID' => $id]);
122 foreach ($subList as $itemId)
123 {
124 MailingSubscriptionTable::add(['CONTACT_ID' => $id, 'MAILING_ID' => $itemId, 'IS_UNSUB' => 'N']);
125 }
126 foreach ($unsubList as $itemId)
127 {
128 MailingSubscriptionTable::add(['CONTACT_ID' => $id, 'MAILING_ID' => $itemId, 'IS_UNSUB' => 'Y']);
129 }
130 }
131
138 public function loadData($id)
139 {
140 $data = ContactTable::getRowById($id);
141 if ($data)
142 {
143 $list = ContactListTable::getList([
144 'select' => ['LIST_ID'],
145 'filter' => ['CONTACT_ID' => $id]
146 ])->fetchAll();
147 $data['SET_LIST'] = array_column($list, 'LIST_ID');
148
149 $list = MailingSubscriptionTable::getList([
150 'select' => ['MAILING_ID'],
151 'filter' => ['CONTACT_ID' => $id, 'IS_UNSUB' => 'N']
152 ])->fetchAll();
153 $data['SUB_LIST'] = array_column($list, 'MAILING_ID');
154
155 $list = MailingSubscriptionTable::getList([
156 'select' => ['MAILING_ID'],
157 'filter' => ['CONTACT_ID' => $id, 'IS_UNSUB' => 'Y']
158 ])->fetchAll();
159 $data['UNSUB_LIST'] = array_column($list, 'MAILING_ID');
160 }
161
162 return $data;
163 }
164
170 public function remove()
171 {
172 return $this->removeByEntity(ContactTable::getEntity(), $this->getId());
173 }
174
181 public static function removeById($id)
182 {
183 return static::create()->removeByEntity(ContactTable::getEntity(), $id);
184 }
185
192 public static function removeFromBlacklistById($id)
193 {
194 return ContactTable::update($id, array('BLACKLISTED' => 'N'))->isSuccess();
195 }
196
203 public function subscribe($campaignId = null)
204 {
205 if (!$this->getId())
206 {
207 return false;
208 }
209
210 $campaignId = $campaignId ?: Campaign::getDefaultId(SITE_ID);
212 'MAILING_ID' => $campaignId,
213 'CONTACT_ID' => $this->getId(),
214 ));
215 }
216
223 public function unsubscribe($campaignId = null)
224 {
225 if (!$this->getId())
226 {
227 return false;
228 }
229
230 $campaignId = $campaignId ?: Campaign::getDefaultId(SITE_ID);
232 'MAILING_ID' => $campaignId,
233 'CONTACT_ID' => $this->getId(),
234 ));
235 }
236
242 public function addToBlacklist()
243 {
244 if (!$this->getId())
245 {
246 return false;
247 }
248
249 return ContactTable::update($this->getId(), array('BLACKLISTED' => 'Y'))->isSuccess();
250 }
251
257 public function removeFromBlacklist()
258 {
259 if (!$this->getId())
260 {
261 return false;
262 }
263
264 return self::removeFromBlacklistById($this->getId());
265 }
266
273 public function addToList($listId)
274 {
275 if (!$this->getId())
276 {
277 return false;
278 }
279
280 return ContactListTable::addIfNotExist($this->getId(), $listId);
281 }
282
289 public function removeFromList($listId)
290 {
291 if (!$this->getId())
292 {
293 return false;
294 }
295
296 return ContactListTable::delete(array(
297 'CONTACT_ID' => $this->getId(),
298 'LIST_ID' => $listId
299 ))->isSuccess();
300 }
301}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static deleteList(array $filter)
static addIfNotExist($contactId, $listId)
static getDefaultId($siteId=null)
Definition campaign.php:63
saveDataLists($id, $setList, $subList, $unsubList)
Definition contact.php:108
static removeFromBlacklistById($id)
Definition contact.php:192
static getList(array $parameters=array())
Definition contact.php:33
subscribe($campaignId=null)
Definition contact.php:203
unsubscribe($campaignId=null)
Definition contact.php:223
saveData($id, array $data)
Definition contact.php:75
static addUnSubscription(array $parameters=array())
Definition mailing.php:874
static deleteList(array $filter)
Definition mailing.php:899
static addSubscription(array $parameters=array())
Definition mailing.php:851