Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
gratitude.php
1<?php
2
4
8
10{
11 protected $component;
12
13 public function __construct($params)
14 {
15 if (!empty($params['component']))
16 {
17 $this->component = $params['component'];
18 }
19 }
20
21 public static function getGratitudesIblockData(array $params = []): array
22 {
23 $result = [
24 'BADGES_DATA' => [],
25 'ELEMENT_ID_LIST' => [],
26 'GRAT_VALUE' => ''
27 ];
28
29 $userId = (!empty($params['userId']) && (int)$params['userId'] > 0 ? (int)$params['userId'] : 0);
30 if ($userId <= 0)
31 {
32 return $result;
33 }
34
35 if (!Loader::includeModule('iblock'))
36 {
37 return $result;
38 }
39
40 $honourIblockId = self::getGratitudesIblockId();
41 $filter = [
42 'IBLOCK_ID' => $honourIblockId,
43 'ACTIVE' => 'Y',
44 '=PROPERTY_USERS' => $userId,
45 ];
46
47 $gratCode = (!empty($params['gratCode']) ? $params['gratCode'] : false);
48 if ($gratCode)
49 {
50 $res = \CIBlockPropertyEnum::getList(
51 [],
52 [
53 'IBLOCK_ID' => $honourIblockId,
54 'CODE' => 'GRATITUDE',
55 'XML_ID' => $gratCode
56 ]
57 );
58 if ($enumFields = $res->fetch())
59 {
60 $filter['PROPERTY_GRATITUDE'] = $enumFields['ID'];
61 $result['GRAT_VALUE'] = $enumFields['VALUE'];
62 }
63 }
64
65 $iblockElementsIdList = [];
66 $badgesData = [];
67
68 $res = \CIBlockElement::getList(
69 [],
70 $filter,
71 false,
72 false,
73 [ 'ID', 'PROPERTY_GRATITUDE' ]
74 );
75 while($iblockElementFields = $res->fetch())
76 {
77 $badgeEnumId = $iblockElementFields['PROPERTY_GRATITUDE_ENUM_ID'];
78 if (!isset($badgesData[$badgeEnumId]))
79 {
80 $badgesData[$badgeEnumId] = [
81 'NAME' => $iblockElementFields['PROPERTY_GRATITUDE_VALUE'],
82 'COUNT' => 0,
83 'ID' => [],
84 ];
85 }
86 $badgesData[$badgeEnumId]['ID'][] = (int)$iblockElementFields['ID'];
87 $iblockElementsIdList[] = $iblockElementFields['ID'];
88 }
89
90 $result['BADGES_DATA'] = $badgesData;
91 $result['ELEMENT_ID_LIST'] = $iblockElementsIdList;
92
93 return $result;
94 }
95
96 public static function getGratitudesIblockId()
97 {
98 return \Bitrix\Socialnetwork\Helper\Gratitude::getIblockId();
99 }
100
101 public static function getGratitudesBlogData(array $params = []): array
102 {
103 global $CACHE_MANAGER;
104
105 $result = [
106 'POST_ID_LIST' => [],
107 'AUTHOR_ID_LIST' => [],
108 'ELEMENT_ID_LIST' => [],
109 ];
110
111 $iblockElementsIdList = (!empty($params['iblockElementsIdList']) && is_array($params['iblockElementsIdList']) ? $params['iblockElementsIdList'] : []);
112 if (empty($iblockElementsIdList))
113 {
114 return $result;
115 }
116
117 if (!Loader::includeModule('blog'))
118 {
119 return $result;
120 }
121
122 $authorsIdList = [];
123
124 $res = \Bitrix\Blog\PostTable::getList([
125 'filter' => [
126 '@UF_GRATITUDE' => $iblockElementsIdList
127 ],
128 'select' => ['ID', 'AUTHOR_ID', 'UF_GRATITUDE']
129 ]);
130
131 $iblockElementsIdList = [];
132 while($postFields = $res->fetch())
133 {
134 $postIdList[] = $postFields['ID'];
135 $authorsIdList[] = $postFields['AUTHOR_ID'];
136 $iblockElementsIdList[] = $postFields['UF_GRATITUDE'];
137
138 if (defined('BX_COMP_MANAGED_CACHE'))
139 {
140 $CACHE_MANAGER->registerTag('blog_post_' . $postFields['ID']);
141 $CACHE_MANAGER->registerTag('USER_CARD_' . (int)($postFields['AUTHOR_ID'] / TAGGED_user_card_size));
142 }
143 }
144
145 $result['POST_ID_LIST'] = $postIdList;
146 $result['AUTHOR_ID_LIST'] = array_unique($authorsIdList);
147 $result['ELEMENT_ID_LIST'] = $iblockElementsIdList;
148
149 return $result;
150 }
151
158 public function prepareGratPostFilter(&$result): void
159 {
160 global $APPLICATION;
161
162 $request = Util::getRequest();
163
164 $result['GRAT_POST_FILTER'] = [];
165 $result['RETURN_EMPTY_LIST'] = false;
166
167 $userId = (int)$request->get('gratUserId');
168 $gratCode = $request->get('gratCode');
169
170 if (
171 $userId
172 && ModuleManager::isModuleInstalled('intranet')
173 )
174 {
175 $res = \CUser::getById($userId);
176 $gratUserName = '';
177 if ($userFields = $res->fetch())
178 {
179 $gratUserName = \CUser::formatName(\CSite::getNameFormat(false), $userFields, true);
180 }
181
182 $result['RETURN_EMPTY_LIST'] = true;
183 $filterParams = [
184 'userId' => $userId
185 ];
186
187 if ($gratCode <> '')
188 {
189 $filterParams['gratCode'] = $gratCode;
190 }
191
192 $gratitudesData = self::getGratitudesIblockData($filterParams);
193 $iblockElementsIdList = $gratitudesData['ELEMENT_ID_LIST'];
194 $gratValue = '';
195
196 if ($gratitudesData['GRAT_VALUE'] <> '')
197 {
198 $gratValue = $gratitudesData['GRAT_VALUE'];
199 }
200
201 $postIdList = [];
202 if (!empty($iblockElementsIdList))
203 {
204 $gratitudesData = self::getGratitudesBlogData([
205 'iblockElementsIdList' => $iblockElementsIdList,
206 ]);
207 $postIdList = $gratitudesData['POST_ID_LIST'];
208 }
209
210 if (!empty($postIdList))
211 {
212 $result['GRAT_POST_FILTER'] = $postIdList;
213 $result['RETURN_EMPTY_LIST'] = false;
214 }
215
216 if ($gratUserName <> '')
217 {
218 $APPLICATION->setTitle(Loc::getMessage($gratValue <> '' ? 'SONET_LOG_LIST_TITLE_GRAT2' : 'SONET_LOG_LIST_TITLE_GRAT', [
219 '#USER_NAME#' => $gratUserName,
220 '#GRAT_NAME#' => $gratValue,
221 ]));
222 }
223 }
224 }
225}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getGratitudesIblockData(array $params=[])
Definition gratitude.php:21