Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
userlink.php
1<?
3
4use \Bitrix\Main\Entity\DataManager;
5use \Bitrix\Socialservices\UserTable as SocservUserTable;
6
24{
25 public static function getFilePath()
26 {
27 return __FILE__;
28 }
29
30 public static function getTableName()
31 {
32 return 'b_socialservices_user_link';
33 }
34
35 public static function getMap()
36 {
37 $fieldsMap = array(
38 'ID' => array(
39 'data_type' => 'integer',
40 'primary' => true,
41 'autocomplete' => true,
42 ),
43 'USER_ID' => array(
44 'data_type' => 'integer',
45 'required' => true,
46 ),
47 'SOCSERV_USER_ID' => array(
48 'data_type' => 'integer',
49 'required' => true,
50 ),
51 'LINK_USER_ID' => array(
52 'data_type' => 'integer',
53 ),
54 'LINK_UID' => array(
55 'data_type' => 'string',
56 'required' => true,
57 ),
58 'LINK_NAME' => array(
59 'data_type' => 'string',
60 ),
61 'LINK_LAST_NAME' => array(
62 'data_type' => 'string',
63 ),
64 'LINK_PICTURE' => array(
65 'data_type' => 'string',
66 ),
67 'USER' => array(
68 'data_type' => 'Bitrix\Main\UserTable',
69 'reference' => array('=this.USER_ID' => 'ref.ID'),
70 ),
71 'SOCSERV_USER' => array(
72 'data_type' => 'Bitrix\Socialservices\UserTable',
73 'reference' => array('=this.SOCSERV_USER_ID' => 'ref.ID'),
74 ),
75 'LINK_USER' => array(
76 'data_type' => 'Bitrix\Main\UserTable',
77 'reference' => array('=this.LINK_USER_ID' => 'ref.ID'),
78 ),
79 );
80
81 return $fieldsMap;
82 }
83
84 public static function deleteBySocserv($userId, $socservProfileId)
85 {
86 $connection = \Bitrix\Main\Application::getConnection();
87 $query = $connection->query("
88DELETE
89FROM ".self::getTableName()."
90WHERE USER_ID='".intval($userId)."' AND SOCSERV_USER_ID='".intval($socservProfileId)."'
91");
92 }
93
94 public static function compareUserLinks($userId, $socservUserId, $links)
95 {
96 $dbRes = static::getList(array(
97 'filter' => array(
98 //'USER_ID' => $userId, // link USER_ID doesn't update with socserv_user
99 '=SOCSERV_USER_ID' => $socservUserId,
100 ),
101 'select' => array('ID', 'LINK_UID')
102 ));
103
104 $currentList = array();
105 while($linkInfo = $dbRes->fetch())
106 {
107 $currentList[$linkInfo['LINK_UID']] = $linkInfo['ID'];
108 }
109
110 foreach($links as $key => $link)
111 {
112 if(array_key_exists($link['uid'], $currentList))
113 {
114 unset($currentList[$link['uid']]);
115 unset($links[$key]);
116 }
117 }
118
119 foreach($currentList as $linkId)
120 {
121 static::delete($linkId);
122 }
123
124 foreach($links as $link)
125 {
126 static::add(array(
127 'USER_ID' => $userId,
128 'SOCSERV_USER_ID' => $socservUserId,
129 'LINK_USER_ID' => null, // !!!!!!
130 'LINK_UID' => $link['uid'],
131 'LINK_NAME' => $link['first_name'],
132 'LINK_LAST_NAME' => $link['last_name'],
133 'LINK_PICTURE' => $link['picture'],
134 ));
135 }
136 }
137
138 public static function checkUserLinks($socservUserId)
139 {
140 $dbRes = UserTable::getByPrimary($socservUserId);
141 $socservUserInfo = $dbRes->fetch();
142 if($socservUserInfo)
143 {
144 $connection = \Bitrix\Main\Application::getConnection();
145 $sqlHelper = $connection->getSqlHelper();
146
147 $sql = "
148SELECT sul.ID, su_link.USER_ID
149FROM ".static::getTableName()." sul
150LEFT JOIN ".SocservUserTable::getTableName()." su_link ON sul.LINK_UID=su_link.XML_ID
151WHERE (1=1)
152AND sul.SOCSERV_USER_ID='".intval($socservUserInfo['ID'])."'
153AND su_link.EXTERNAL_AUTH_ID='".$sqlHelper->forSql($socservUserInfo['EXTERNAL_AUTH_ID'])."'
154AND sul.LINK_USER_ID IS NULL
155";
156
157 return $connection->query($sql);
158 }
159 else
160 {
161 return false;
162 }
163 }
164}