Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
base.php
1<?php
2
4
9
10Loc::loadMessages(__FILE__);
11
12abstract class Base
13{
14 public const TYPE = 'BASE';
15 public const POST_TEXT = 'commentAuxBase';
16
17 protected $params = [];
18 protected $options = [];
19
20 final public static function className(): string
21 {
22 return static::class;
23 }
24
25 final public static function getType(): string
26 {
27 return static::TYPE;
28 }
29
30 final public static function getPostText(): string
31 {
32 return static::POST_TEXT;
33 }
34
35 public function getText(): string
36 {
37 return '';
38 }
39
40 public function canDelete(): bool
41 {
42 return true;
43 }
44
45 final public function getLiveParams(): array
46 {
47 $result = [];
48
49 if (
50 !empty($this->params['liveParamList'])
51 && is_array($this->params['liveParamList'])
52 )
53 {
54 $result = $this->params['liveParamList'];
55 }
56
57 return $result;
58 }
59
60 final public function setParams(array $params): void
61 {
62 $this->params = $params;
63 }
64
65 final public function setOptions(array $options): void
66 {
67 $this->options = $options;
68 }
69
70 final public function getOptions(): array
71 {
72 return $this->options;
73 }
74
75 public function checkRecalcNeeded($fields, $params): bool
76 {
77 return false;
78 }
79
80 public static function init($type = 'BASE', array $params = [], array $options = [])
81 {
82 static $extranet = null;
83 static $extranetSite = null;
84 static $handlerManager = null;
85
86 if ($handlerManager === null)
87 {
88 $handlerManager = new HandlerManager();
89 }
90
92 if ($handler = $handlerManager->getHandlerByType($type))
93 {
94 $handler->setParams($params);
95
96 if (!isset($options['extranet']))
97 {
98 if ($extranet === null)
99 {
100 $extranet = Loader::includeModule('extranet');
101 }
102 $options['extranet'] = $extranet;
103 }
104 if (!isset($options['extranetSite']))
105 {
106 if ($extranetSite === null)
107 {
108 $extranetSite = ($extranet ? \CExtranet::getExtranetSiteID() : false);
109 }
110 $options['extranetSite'] = $extranetSite;
111 }
112
113 $handler->setOptions($options);
114 }
115
116 return $handler;
117 }
118
119 final public static function findProvider($fields = array(), $options = array())
120 {
121 static $handlerManager = null;
122
123 $handler = false;
124 $needSetParams = true;
125 if (
126 isset($options['needSetParams'])
127 && $options['needSetParams'] === false
128 )
129 {
130 $needSetParams = false;
131 }
132
133 if (
134 is_array($fields)
135 && isset($fields['POST_TEXT'])
136 )
137 {
138 if ($handlerManager === null)
139 {
140 $handlerManager = new HandlerManager();
141 }
142
144 if ($handler = $handlerManager->getHandlerByPostText($fields['POST_TEXT']))
145 {
146 $handler->setOptions($options);
147
148 if ($needSetParams)
149 {
150 $params = $handler->getParamsFromFields($fields);
151 if (!empty($params))
152 {
153 $handler->setParams($params);
154 }
155 else
156 {
157 $handler = false;
158 }
159 }
160 }
161 }
162
163 return $handler;
164 }
165
166 public function getParamsFromFields($fields = []): array
167 {
168 return [];
169 }
170
171 public function sendRatingNotification($fields = [], $ratingVoteParams = []): void
172 {
173 $userId = (
174 is_array($ratingVoteParams)
175 && isset($ratingVoteParams['OWNER_ID'])
176 ? (int)$ratingVoteParams['OWNER_ID']
177 : 0
178 );
179
180 if (
181 !$this->checkRatingNotificationData($userId, $fields)
182 || !$this->setRatingNotificationParams($fields)
183 )
184 {
185 return;
186 }
187
188 $followValue = $this->getRatingNotificationFollowValue($userId, $ratingVoteParams, $fields);
189
190 if ($followValue === 'N')
191 {
192 return;
193 }
194
195 $ratingVoteParams['ENTITY_LINK'] = $this->getRatingCommentLink([
196 'commentId' => $fields['ID'],
197 'commentAuthorId' => $ratingVoteParams['OWNER_ID'],
198 'ratingEntityTypeId' => $ratingVoteParams['ENTITY_TYPE_ID'],
199 'ratingEntityId' => $ratingVoteParams['ENTITY_ID'],
200 ]);
201
202 $ratingVoteParams['ENTITY_PARAM'] = 'COMMENT';
203 $ratingVoteParams['ENTITY_MESSAGE'] = $this->getRatingNotificationEntityMessage();
204 $ratingVoteParams['ENTITY_TITLE'] = $ratingVoteParams['ENTITY_MESSAGE'];
205
206 $messageFields = [
207 'MESSAGE_TYPE' => IM_MESSAGE_SYSTEM,
208 'TO_USER_ID' => $userId,
209 'FROM_USER_ID' => (int)$ratingVoteParams['USER_ID'],
210 'NOTIFY_TYPE' => IM_NOTIFY_FROM,
211 'NOTIFY_MODULE' => 'main',
212 'NOTIFY_EVENT' => 'rating_vote',
213 'NOTIFY_TAG' => $this->getRatingNotificationNotigyTag($ratingVoteParams, $fields),
214 'NOTIFY_MESSAGE' => \CIMEvent::getMessageRatingVote($ratingVoteParams),
215 'NOTIFY_MESSAGE_OUT' => \CIMEvent::getMessageRatingVote($ratingVoteParams, true),
216 ];
217
218 \CIMNotify::add($messageFields);
219 }
220
221 protected function setRatingNotificationParams(array $fields = []): bool
222 {
223 $params = $this->getParamsFromFields($fields);
224 if (empty($params))
225 {
226 return false;
227 }
228
229 $this->setParams($params);
230
231 return true;
232 }
233
234 protected function checkRatingNotificationData(int $userId = 0, array $fields = []): bool
235 {
236 return (
237 $userId > 0
238 && is_array($fields)
239 && Loader::includeModule('im')
240 );
241 }
242
243 protected function getRatingNotificationEntityMessage(): string
244 {
245 return $this->getText();
246 }
247
248 protected function getRatingNotificationNotigyTag(array $ratingVoteParams = [], array $fields = []): string
249 {
250 return '';
251 }
252
253 protected function getRatingNotificationFollowValue(int $userId = 0, array $ratingVoteParams = [], array $fields = [])
254 {
255 return \CSocNetLogFollow::getExactValueByRating(
256 $userId,
257 $ratingVoteParams['ENTITY_TYPE_ID'],
258 $ratingVoteParams['ENTITY_ID']
259 );
260 }
261
262 final protected function getRatingCommentLink($params)
263 {
264 $result = '';
265
266 if (Loader::includeModule('im'))
267 {
269
270 $commentAuthorId = (!empty($params['commentAuthorId']) && (int)$params['commentAuthorId'] > 0 ? (int)$params['commentAuthorId'] : 0);
271
272 $siteList = $intranetSiteId = $extranetSiteId = false;
273
274 if (Loader::includeModule('extranet'))
275 {
276 $siteList = array();
277 $intranetSiteId = \CExtranet::getExtranetSiteID();
278 $extranetSiteId = \CSite::getDefSite();
279 $res = \CSite::getList("sort", "desc", array("ACTIVE" => "Y"));
280 while($site = $res->fetch())
281 {
282 $siteList[$site["ID"]] = array(
283 "DIR" => (trim($site["DIR"]) !== '' ? $site["DIR"] : '/'),
284 "SERVER_NAME" => (trim($site["SERVER_NAME"]) !== '' ? $site["SERVER_NAME"] : Option::get("main", "server_name", $_SERVER["HTTP_HOST"]))
285 );
286 }
287 }
288
289 $contentId = Livefeed\Provider::getContentId([
290 'RATING_TYPE_ID' => $params['ratingEntityTypeId'],
291 'RATING_ENTITY_ID' => $params['ratingEntityId'],
292 ]);
293
294 if (
295 !empty($contentId['ENTITY_TYPE'])
296 && ($liveFeedProvider = Livefeed\Provider::init([
297 'ENTITY_TYPE' => $contentId['ENTITY_TYPE'],
298 'ENTITY_ID' => $contentId['ENTITY_ID'],
299 'SITE_ID' => (!empty($options['siteId']) ? $options['siteId'] : SITE_ID)
300 ]))
301 )
302 {
303 $liveFeedProvider->initSourceFields();
304 $originalLink = $liveFeedProvider->getLiveFeedUrl();
305
306 $result = \CIMEvent::getMessageRatingEntityURL(
307 $originalLink,
308 $commentAuthorId,
309 $siteList,
310 $intranetSiteId,
311 $extranetSiteId
312 );
313 }
314 }
315
316 return $result;
317 }
318}
static loadMessages($file)
Definition loc.php:64
getRatingNotificationFollowValue(int $userId=0, array $ratingVoteParams=[], array $fields=[])
Definition base.php:253
checkRecalcNeeded($fields, $params)
Definition base.php:75
checkRatingNotificationData(int $userId=0, array $fields=[])
Definition base.php:234
sendRatingNotification($fields=[], $ratingVoteParams=[])
Definition base.php:171
setRatingNotificationParams(array $fields=[])
Definition base.php:221
getRatingNotificationNotigyTag(array $ratingVoteParams=[], array $fields=[])
Definition base.php:248