Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
contact.php
1<?php
3
9
10Loc::loadMessages(__FILE__);
11
44class ContactTable extends Main\Entity\DataManager
45{
46 const NOTIFY = 'Y';
47 const DONT_NOTIFY = 'N';
48
51
53 const POSSIBLE_RESET_TIME = 2592000; // 86400 * 30
55
56 protected static $notifyStack = array();
57
63 public static function getTableName()
64 {
65 return 'b_socialservices_contact';
66 }
67
73 public static function getMap()
74 {
75 return array(
76 'ID' => array(
77 'data_type' => 'integer',
78 'primary' => true,
79 'autocomplete' => true,
80 ),
81 'TIMESTAMP_X' => array(
82 'data_type' => 'datetime',
83 ),
84 'USER_ID' => array(
85 'data_type' => 'integer',
86 'required' => true,
87 ),
88 'CONTACT_USER_ID' => array(
89 'data_type' => 'integer',
90 ),
91 'CONTACT_XML_ID' => array(
92 'data_type' => 'integer',
93 ),
94 'CONTACT_NAME' => array(
95 'data_type' => 'string',
96 ),
97 'CONTACT_LAST_NAME' => array(
98 'data_type' => 'string',
99 ),
100 'CONTACT_PHOTO' => array(
101 'data_type' => 'string',
102 ),
103 'LAST_AUTHORIZE' => array(
104 'data_type' => 'datetime',
105 ),
106 'NOTIFY' => array(
107 'data_type' => 'boolean',
108 'values' => array(static::DONT_NOTIFY, static::NOTIFY),
109 ),
110 'USER' => array(
111 'data_type' => 'Bitrix\Main\UserTable',
112 'reference' => array('=this.USER_ID' => 'ref.ID'),
113 ),
114 'CONTACT_USER' => array(
115 'data_type' => 'Bitrix\Main\UserTable',
116 'reference' => array('=this.CONTACT_USER_ID' => 'ref.ID'),
117 ),
118 );
119 }
120
121 public static function onBeforeUpdate(Entity\Event $event)
122 {
123 $result = new Entity\EventResult();
124 $data = $event->getParameter("fields");
125
126 if(!isset($data['TIMESTAMP_X']))
127 {
128 $data['TIMESTAMP_X'] = new DateTime();
129 $result->modifyFields($data);
130 }
131 }
132
136 public static function onUserLoginSocserv($params)
137 {
138 global $USER;
139
140 if(
141 $params['EXTERNAL_AUTH_ID'] === \CSocServBitrix24Net::ID
142 && \Bitrix\Main\ModuleManager::isModuleInstalled('bitrix24')
143 )
144 {
145 $notificationOptions = \CUserOptions::getOption("socialservices", "notifications", array());
146
147 $lastDate = 0;
148 if(isset($notificationOptions["CONTACTS_NOTIFY_DATE"]))
149 {
150 $lastDate = $notificationOptions["CONTACTS_NOTIFY_DATE"];
151 }
152
153 if($lastDate < time() - 86400)
154 {
155 static::notifyPossible($USER->getId());
156
157 $notificationOptions["CONTACTS_NOTIFY_DATE"] = time();
158 \CUserOptions::setOption("socialservices", "notifications", $notificationOptions);
159 }
160 }
161 }
162
163 public static function onNetworkBroadcast($data)
164 {
165 $contactsList = array();
166 $possibleContactsList = array();
167
168 if(isset($data["contact"]) && is_array($data["contact"]))
169 {
170 foreach($data["contact"] as $contact)
171 {
172 if(!isset($contactsList[$contact['CONTACT_OWNER']]))
173 {
174 $contactsList[$contact['CONTACT_OWNER']] = array();
175 }
176
177 $contactsList[$contact['CONTACT_OWNER']][] = $contact;
178 }
179 }
180
181 if(isset($data["contact_possible"]) && is_array($data["contact_possible"]))
182 {
183 foreach($data["contact_possible"] as $contact)
184 {
185 if(!isset($possibleContactsList[$contact['CONTACT_OWNER']]))
186 {
187 $possibleContactsList[$contact['CONTACT_OWNER']] = array();
188 }
189
190 $possibleContactsList[$contact['CONTACT_OWNER']][] = $contact;
191 }
192 }
193
194 $dbRes = UserTable::getList(array(
195 'filter' => array(
196 '=EXTERNAL_AUTH_ID' => \CSocServBitrix24Net::ID,
197 '=XML_ID' => array_unique(
198 array_merge(
199 array_keys($contactsList),
200 array_keys($possibleContactsList)
201 )
202 ),
203 ),
204 'select' => array('ID', 'USER_ID', 'XML_ID')
205 ));
206
207 while($owner = $dbRes->fetch())
208 {
209 if(
210 count($contactsList) > 0
211 && isset($contactsList[$owner["XML_ID"]])
212 && is_array($contactsList[$owner["XML_ID"]])
213 && count($contactsList[$owner["XML_ID"]]) > 0
214 )
215 {
216 static::processContacts($owner, $contactsList[$owner["XML_ID"]]);
217 }
218
219 if(
220 count($possibleContactsList) > 0
221 && isset($possibleContactsList[$owner["XML_ID"]])
222 && is_array($possibleContactsList[$owner["XML_ID"]])
223 && count($possibleContactsList[$owner["XML_ID"]]) > 0
224 )
225 {
226 static::processPossibleContacts($owner, $possibleContactsList[$owner["XML_ID"]]);
227 }
228 }
229 }
230
231 protected static function processContacts($owner, array $contactsList)
232 {
233 if(!Main\Loader::includeModule('rest'))
234 {
235 return;
236 }
237
238 $existedContacts = array();
239 $dbRes = ContactTable::getList(array(
240 'filter' => array(
241 '=USER_ID' => $owner["USER_ID"],
242 ),
243 'select' => array('ID', 'CONTACT_XML_ID')
244 ));
245 while($existedContact = $dbRes->fetch())
246 {
247 $existedContacts[$existedContact['CONTACT_XML_ID']] = $existedContact['ID'];
248 }
249
250 foreach($contactsList as $contact)
251 {
252 $contactFields = array(
253 "USER_ID" => $owner["USER_ID"],
254 "CONTACT_XML_ID" => $contact["CONTACT_ID"],
255 "CONTACT_NAME" => $contact["NAME"],
256 "CONTACT_LAST_NAME" => $contact["LAST_NAME"],
257 "CONTACT_PHOTO" => $contact["PHOTO"],
258 "NOTIFY" => $contact["NOTIFY"],
259 "LAST_AUTHORIZE" => DateTime::createFromUserTime(\CRestUtil::unConvertDateTime($contact['LAST_AUTHORIZE'])),
260 );
261
262 $contactId = false;
263 if(isset($existedContacts[$contactFields["CONTACT_XML_ID"]]))
264 {
265 $contactId = $existedContacts[$contactFields["CONTACT_XML_ID"]];
266 $result = static::update($contactId, $contactFields);
267 if(!$result->isSuccess())
268 {
269 AddMessage2Log($result->getErrorMessages());
270 }
271 }
272 else
273 {
274 $result = static::add($contactFields);
275 if($result->isSuccess())
276 {
277 $contactId = $result->getId();
278 }
279 else
280 {
281 AddMessage2Log($result->getErrorMessages());
282 }
283 }
284
285 if(
286 $contactId > 0
287 && isset($contact["profile"])
288 && count($contact["profile"]) > 0
289 )
290 {
291 if(isset($existedContacts[$contactFields["CONTACT_XML_ID"]]))
292 {
294 }
295
296 foreach($contact["profile"] as $profile)
297 {
298 $connectFields = array(
299 'CONTACT_ID' => $contactId,
300 'CONTACT_PROFILE_ID' => $profile['PROFILE_ID'],
301 'CONTACT_PORTAL' => $profile['PORTAL'],
302 'CONNECT_TYPE' => $profile['TYPE'],
303 'LAST_AUTHORIZE' => DateTime::createFromUserTime(\CRestUtil::unConvertDateTime($profile['LAST_AUTHORIZE'])),
304 );
305
306 $r = ContactConnectTable::add($connectFields);
307 if($r->isSuccess())
308 {
309 if(!isset($contactFields["CONNECT"]))
310 {
311 $contactFields["CONNECT"] = array($connectFields);
312 }
313 else
314 {
315 $contactFields["CONNECT"][] = $connectFields;
316 }
317 }
318 }
319
320 if(!isset($existedContacts[$contactFields["CONTACT_XML_ID"]]))
321 {
322 static::notifyJoin($contactId, $contactFields);
323 }
324 }
325 }
326
327 static::notifyJoinFinish($owner["USER_ID"]);
328 }
329
330 protected static function processPossibleContacts($owner, array $contactsList)
331 {
332 if(!Main\Loader::includeModule('rest'))
333 {
334 return;
335 }
336
337 $existedContacts = array();
338 $dbRes = UserLinkTable::getList(array(
339 'filter' => array(
340 '=SOCSERV_USER_ID' => $owner["ID"],
341 '=SOCSERV_USER.EXTERNAL_AUTH_ID' => \CSocServBitrix24Net::ID,
342 ),
343 'select' => array('ID', 'LINK_UID')
344 ));
345 while($existedContact = $dbRes->fetch())
346 {
347 $existedContacts[$existedContact['LINK_UID']] = $existedContact['ID'];
348 }
349
350 foreach($contactsList as $contact)
351 {
352 $contactFields = array(
353 "USER_ID" => $owner["USER_ID"],
354 "SOCSERV_USER_ID" => $owner["ID"],
355 "LINK_UID" => $contact["CONTACT_ID"],
356 "LINK_NAME" => $contact["NAME"],
357 "LINK_LAST_NAME" => $contact["LAST_NAME"],
358 "LINK_PICTURE" => $contact["PHOTO"],
359 );
360
361 $linkId = false;
362 if(isset($existedContacts[$contactFields["LINK_UID"]]))
363 {
364 $linkId = $existedContacts[$contactFields["LINK_UID"]];
365 UserLinkTable::update($linkId, $contactFields);
366 }
367 else
368 {
369 $result = UserLinkTable::add($contactFields);
370 if($result->isSuccess())
371 {
372 $linkId = $result->getId();
373 }
374 }
375
376 if(
377 $linkId !== false
378 && isset($contact["profile"])
379 && count($contact["profile"]) > 0
380 )
381 {
382 if(isset($existedContacts[$contactFields["LINK_UID"]]))
383 {
385 }
386
387 foreach($contact["profile"] as $profile)
388 {
389 $result = ContactConnectTable::add(array(
390 'LINK_ID' => $linkId,
391 'CONTACT_PROFILE_ID' => $profile['PROFILE_ID'],
392 'CONTACT_PORTAL' => $profile['PORTAL'],
393 'CONNECT_TYPE' => $profile['TYPE'],
394 'LAST_AUTHORIZE' => DateTime::createFromUserTime(\CRestUtil::unConvertDateTime($profile['LAST_AUTHORIZE'])),
395 ));
396 }
397 }
398 }
399 }
400
401 public static function getConnectId($connect)
402 {
403 return $connect["CONNECT_TYPE"].$connect["CONTACT_PROFILE_ID"];
404 }
405
406 protected static function notifyJoin($contactId, array $contactInfo = null)
407 {
408 return false;
409 }
410
411 protected static function notifyJoinFinish($userId)
412 {
413 $network = new Network();
414 if(
415 $network->isOptionEnabled()
416 && count(static::$notifyStack) > 0
417 && Main\Loader::includeModule('im')
418 )
419 {
420 $attach = new \CIMMessageParamAttach(null, \CIMMessageParamAttach::NORMAL);
421
422 $count = 0;
423 foreach(static::$notifyStack as $contactInfo)
424 {
425 if(++$count > static::NOTIFY_CONTACT_COUNT)
426 {
427 $attach->AddHtml('<a href="'.str_replace("#USER_ID#", $userId, Option::get("intranet", "path_user", "/company/persona/user/#USER_ID#/")).'">'.Loc::getMessage("SS_JOIN_NOTIFY_MORE", array("#NUM#" => count(static::$notifyStack)-$count+1)).'</a>');
428 break;
429 }
430 else
431 {
432 $attachParams = array(
433 "NAME" => \CUser::FormatName(\CSite::GetNameFormat(), array(
434 "NAME" => $contactInfo["CONTACT_NAME"],
435 "LAST_NAME" => $contactInfo["CONTACT_LAST_NAME"]
436 ), false, false),
437 );
438 if($contactInfo["CONTACT_PHOTO"])
439 {
440 $attachParams["AVATAR"] = $contactInfo["CONTACT_PHOTO"];
441 }
442 $attachParams["NETWORK_ID"] = static::getConnectId($contactInfo["CONNECT"][0]);
443
444 $attach->AddUser($attachParams);
445 }
446 }
447
448 $messageFields = array(
449 "TO_USER_ID" => $userId,
450 "FROM_USER_ID" => 0,
451 "NOTIFY_TYPE" => IM_NOTIFY_SYSTEM,
452 "NOTIFY_MODULE" => "socialservices",
453 "NOTIFY_EVENT" => "multiple_contacts",
454 "NOTIFY_MESSAGE" => Loc::getMessage("SS_JOIN_NOTIFY_MULTIPLE"),
455 "NOTIFY_MESSAGE_OUT" => IM_MAIL_SKIP,
456 "ATTACH" => array($attach),
457 );
458
459 \CIMNotify::Add($messageFields);
460
461 static::$notifyStack = array();
462 }
463 }
464
468 protected static function notifyPossible($userId)
469 {
470 }
471}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static isModuleInstalled($moduleName)
static getList(array $parameters=array())
static update($primary, array $data)
static createFromUserTime($timeString)
Definition datetime.php:180
static onBeforeUpdate(Entity\Event $event)
Definition contact.php:121
static notifyJoin($contactId, array $contactInfo=null)
Definition contact.php:406
static processPossibleContacts($owner, array $contactsList)
Definition contact.php:330
static processContacts($owner, array $contactsList)
Definition contact.php:231