Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
bloguser.php
1<?php
9namespace Bitrix\Blog;
10
18
19Loc::loadMessages(__FILE__);
20
22{
23 const CACHE_ID = 'BLOG_USERS';
24 private $blogId = NULL;
25
26// this values was be hardcoded in components. If we want customization - need add settings and remake
27 private $avatarSizes = array(
28 'COMMENT' => array('WIDTH' => 30, 'HEIGHT' => 30),
29 'POST' => array('WIDTH' => 100, 'HEIGHT' => 100),
30 );
31
32 private $cacheTime = 0;
33
34 public function addAvatarSize($width, $height, $key = "")
35 {
36 $width = intval($width);
37 $height = intval($height);
38
39// overwrite params if key exist or create new
40 $key = $key <> '' ? $key : "IMG_" . (count($this->avatarSizes) + 1);
41 $this->avatarSizes[$key] = array('WIDTH' => $width, 'HEIGHT' => $height);
42 }
43
44
51 public function __construct($cacheTime = 0)
52 {
53 if ($cacheTime > 0)
54 $this->cacheTime = intval($cacheTime);
55 }
56
61 public function setBlogId($id)
62 {
63 $this->blogId = intval($id);
64 }
65
66 public function setCacheTime($cacheTime)
67 {
68 if ($cacheTime > 0)
69 $this->cacheTime = $cacheTime;
70 }
71
72
79 public function getUsers($ids = array())
80 {
81 if (empty($ids))
82 return array();
83
84 if ($this->cacheTime > 0)
85 {
86 $result = $this->getUsersFromCache($ids);
87 }
88 else
89 {
90 $result = $this->getUsersFromDB($ids);
91 }
92
93 return $result;
94 }
95
96
103 private function getUsersFromCache($ids)
104 {
105 $cache = Cache::createInstance();
106 if ($cache->initCache($this->cacheTime, self::createCacheId($ids), self::createCacheDir($this->blogId)))
107 {
108 $result = $cache->getVars();
109 }
110 elseif ($cache->startDataCache())
111 {
112 $result = self::getUsersFromDB($ids);
113 $cache->endDataCache($result);
114 }
115 else
116 {
117 $result = false;
118 }
119
120 return $result;
121 }
122
123
129 public static function cleanCache($blogId = NULL)
130 {
131 $cache = Cache::createInstance();
132
133 return $cache->cleanDir(self::CACHE_ID, self::createCacheDir($blogId));
134 }
135
136
137 private static function createCacheDir($blogId = NULL)
138 {
139 $dir = '/' . SITE_ID;
140 $dir .= $blogId ? '/BLOG_ID_' . $blogId : '/BLOGS_ALL';
141
142 return $dir;
143 }
144
151 private static function createCacheId($ids)
152 {
153 $ids = array_unique($ids);
154 asort($ids);
155
156 return self::CACHE_ID . '_' . implode('_', $ids);
157 }
158
159
163 private function getUsersFromDB($ids = array())
164 {
165 $result = array();
166
167// BLOG users
168 $filter = array();
169 if (!empty($ids))
170 $filter["=USER_ID"] = $ids;
171 $resBlogUsers = Internals\BlogUserTable::getList(array(
172 'select' => array(
173 'ID', 'USER_ID', 'ALIAS', 'DESCRIPTION', 'AVATAR', 'INTERESTS', 'LAST_VISIT', 'DATE_REG', 'ALLOW_POST',
174 'USER.PERSONAL_PHOTO', 'USER.LOGIN', 'USER.NAME', 'USER.LAST_NAME'
175 ),
176 'filter' => $filter,
177 ));
178
179// find Users then not exists as BlogUser
180 if (is_array($ids) && !empty($ids))
181 $notExistingUsersIds = array_combine($ids, $ids); // set keys = value in new array
182
183 while ($row = $resBlogUsers->fetch())
184 {
185 unset($notExistingUsersIds[$row["USER_ID"]]);
186// specialchars only needed fields
187 $row["BLOG_USER_ID"] = $row["ID"]; // rename for understandability
188
189// make correctly BlogUser structure to use in old components
190 $row["BlogUser"] = array(
191 "ALIAS" => $row["ALIAS"],
192 "DESCRIPTION" => $row["DESCRIPTION"],
193 "INTERESTS" => $row["INTERESTS"],
194 );
195 $row["BlogUser"] = \CBlogTools::htmlspecialcharsExArray($row["BlogUser"]);
196 if($row["DATE_REG"])
197 $row["BlogUser"]["DATE_REG"] = FormatDate("FULL", $row["DATE_REG"]->getTimestamp());
198 if($row["LAST_VISIT"])
199 $row["BlogUser"]["LAST_VISIT"] = FormatDate("FULL", $row["LAST_VISIT"]->getTimestamp());
200 $row["BlogUser"]["ID"] = $row["ID"];
201 $row["BlogUser"]["USER_ID"] = $row["USER_ID"];
202 $row["BlogUser"]["AVATAR"] = $row["AVATAR"];
203 $row["BlogUser"]["ALLOW_POST"] = $row["ALLOW_POST"];
204
205// Avatars for post and for comments
206 $row["BlogUser"]["AVATAR_file"] = intval($row["AVATAR"]) > 0 ?
207 \CFile::GetFileArray($row["AVATAR"]) :
208 \CFile::GetFileArray($row["BLOG_INTERNALS_BLOG_USER_USER_PERSONAL_PHOTO"]);
209 if ($row["BlogUser"]["AVATAR_file"] !== false)
210 {
211 foreach ($this->avatarSizes as $key => $avatarSize)
212 {
213 $row["BlogUser"]["Avatar_resized"][$avatarSize['WIDTH'] . '_' . $avatarSize['HEIGHT']] = \CFile::ResizeImageGet(
214 $row["BlogUser"]["AVATAR_file"],
215 array("width" => $avatarSize['WIDTH'], "height" => $avatarSize['HEIGHT']),
216 BX_RESIZE_IMAGE_EXACT,
217 false
218 );
219 $row["BlogUser"]["AVATAR_img"][$avatarSize['WIDTH'] . '_' . $avatarSize['HEIGHT']] = \CFile::ShowImage(
220 $row["BlogUser"]["Avatar_resized"][$avatarSize['WIDTH'] . '_' . $avatarSize['HEIGHT']]["src"],
221 $avatarSize['WIDTH'],
222 $avatarSize['HEIGHT'],
223 "border=0 align='right'"
224 );
225 }
226 }
227
228// create correct name from alias, login and names
229 $row["AUTHOR_NAME"] = self::GetUserName(
230 $row["ALIAS"],
231 $row["BLOG_INTERNALS_BLOG_USER_USER_NAME"],
232 $row["BLOG_INTERNALS_BLOG_USER_USER_LAST_NAME"],
233 $row["BLOG_INTERNALS_BLOG_USER_USER_LOGIN"]
234 );
235 $row["~AUTHOR_NAME"] = htmlspecialcharsex($row["AUTHOR_NAME"]);
236
237// array for User data
238 $row["arUser"] = array(
239 "ID" => $row["USER_ID"],
240 "NAME" => $row["BLOG_INTERNALS_BLOG_USER_USER_NAME"],
241 "LAST_NAME" => $row["BLOG_INTERNALS_BLOG_USER_USER_LAST_NAME"],
242 "LOGIN" => $row["BLOG_INTERNALS_BLOG_USER_USER_LOGIN"],
243 );
244 $row["arUser"] = \CBlogTools::htmlspecialcharsExArray($row["arUser"]);
245
246// we need work with main user ID - is may be not equal blog user ID
247 $result[$row["arUser"]['ID']] = $row;
248 }
249
250// create new empty BlogUsers for not existing Users
251 if (!empty($notExistingUsersIds))
252 $result = $result + $this->addNotExistingUsers($notExistingUsersIds);
253
254 return $result;
255 }
256
257
258 private function addNotExistingUsers($ids = array())
259 {
260 global $APPLICATION, $DB;
261
262// get Users data
263 $rsUsers = \CUser::GetList(
264 'id',
265 'asc',
266 array('ID' => implode('|', $ids)),
267 array('FIELDS' => array('ID', 'DATE_REGISTER'/*, 'NAME', 'LAST_NAME', 'LOGIN'*/))
268 );
269
270 while ($user = $rsUsers->Fetch())
271 {
272// todo: use new BlogUser class, when finish them
273// check correctly date
274 if (!is_set($user, "DATE_REGISTER") || (!$DB->IsDate($user["DATE_REGISTER"], false, LANG, "FULL")))
275 $user["DATE_REGISTER"] = new DateTime();
276
277 $resId = \CBlogUser::Add(
278 array(
279 'USER_ID' => $user['ID'],
280 'DATE_REG' => $user["DATE_REGISTER"],
281 )
282 );
283
284// during ADD process we can catch errors. If not process them - we get infinity cicle between getUsersFromDB>addNotExistingUsers
285 if(!$resId)
286 if($ex = $APPLICATION->GetException())
287 throw new SystemException($ex->GetString());
288 }
289
290// get created BlogUsers from DB
291 return $this->getUsersFromDB($ids);
292 }
293
294
300 public static function getCommentAuthorsIdsByPostId($postId)
301 {
302 if (!$postId)
303 throw new ArgumentNullException('post ID');
304 $postId = intval($postId);
305 $result = array();
306
307 $resComment = \CBlogComment::GetList(array(), array("POST_ID" => $postId), false, false, array("AUTHOR_ID"));
308 while ($comment = $resComment->Fetch())
309 {
310 if ($comment["AUTHOR_ID"])
311 $result[$comment["AUTHOR_ID"]] = $comment["AUTHOR_ID"];
312 }
313
314 return $result;
315 }
316
321 public static function getPostAuthorsIdsByBlogId($blogId)
322 {
323 if (!$blogId)
324 throw new ArgumentNullException('blog ID');
325 $blogId = intval($blogId);
326 $result = array();
327
328 $resPost = \CBlogPost::GetList(array(), array("BLOG_ID" => $blogId), false, false, array("AUTHOR_ID"));
329 while ($post = $resPost->Fetch())
330 if ($post["AUTHOR_ID"])
331 $result[$post["AUTHOR_ID"]] = $post["AUTHOR_ID"];
332
333 return $result;
334 }
335
336
344 public static function getPostAuthorsIdsByDbFilter($arFilter)
345 {
346 if (!$arFilter)
347 throw new ArgumentNullException('blog ID');
348 if(!is_array($arFilter))
349 return array();
350
351 $result = array();
352 $resPost = \CBlogPost::GetList(array(), $arFilter, false, false, array("AUTHOR_ID"));
353 while ($post = $resPost->Fetch())
354 if ($post["AUTHOR_ID"])
355 $result[$post["AUTHOR_ID"]] = $post["AUTHOR_ID"];
356
357 return $result;
358 }
359
360
372 public static function GetUserName($alias, $name, $lastName, $login, $secondName = "")
373 {
374 $result = "";
375
376 $canUseAlias = \COption::GetOptionString("blog", "allow_alias", "Y");
377 if ($canUseAlias == "Y")
378 $result = $alias;
379
380 if ($result == '')
381 {
382 $result = \CUser::FormatName(
383 \CSite::GetNameFormat(false),
384 array("NAME" => $name,
385 "LAST_NAME" => $lastName,
386 "SECOND_NAME" => $secondName,
387 "LOGIN" => $login),
388 true,
389 false
390 );
391 }
392
393 return $result;
394 }
395
396 public static function GetUserNameEx($user, $blogUser, $params)
397 {
398 $result = "";
399 if (empty($params["bSoNet"]))
400 {
401 $canUseAlias = \COption::GetOptionString("blog", "allow_alias", "Y");
402 if ($canUseAlias == "Y")
403 $result = $blogUser["ALIAS"];
404 }
405
406 if ($result == '')
407 {
408 $params["NAME_TEMPLATE"] = $params["NAME_TEMPLATE"] ? $params["NAME_TEMPLATE"] : \CSite::GetNameFormat();
409 $params["NAME_TEMPLATE"] = str_replace(
410 array("#NOBR#", "#/NOBR#"),
411 array("", ""),
412 $params["NAME_TEMPLATE"]
413 );
414 $isUseLogin = ($params["SHOW_LOGIN"] ?? null) != "N" ? true : false;
415
416 $result = \CUser::FormatName(
417 $params["NAME_TEMPLATE"],
418 $user,
419 $isUseLogin,
420 false
421 );
422 }
423
424 return $result;
425 }
426
433 public static function isUserGivenConsent($userId, $agreementId)
434 {
435 if (!$userId || $userId <= 0)
436 throw new ArgumentNullException('User ID');
437 if (!$agreementId || $agreementId <= 0)
438 throw new ArgumentNullException('Agreement ID');
439
440// Find root URL for current component. We will check this URL in consents table.
441// URL will be common for any constnt in this component
442 $request = Application::getInstance()->getContext()->getRequest();
443 $url = $request->getHttpHost() . $request->getScriptFile();
444 $urlDir = pathinfo($url);
445 $urlDir = $urlDir['dirname'];
446
447 $filter = array(
448 "=USER_ID" => $userId,
449 "%=URL" => "%$urlDir%",
450 "=AGREEMENT_ID" => $agreementId,
451 );
452
453 $isGivenAgreement = false;
454 $consents = ConsentTable::getList(array('filter' => $filter));
455 if ($consents->fetch())
456 $isGivenAgreement = true;
457
458 return $isGivenAgreement;
459 }
460
466 public static function onUserDelete($userId = NULL)
467 {
468 $userId = intval($userId);
469 if ($userId <= 0)
470 {
471 return false;
472 }
473
474 \Bitrix\Blog\PostSocnetRightsTable::deleteByEntity('U'.$userId);
475
476 return \CBlogUser::delete($userId);
477 }
478
479}
static getPostAuthorsIdsByBlogId($blogId)
Definition bloguser.php:321
static GetUserNameEx($user, $blogUser, $params)
Definition bloguser.php:396
static cleanCache($blogId=NULL)
Definition bloguser.php:129
static getPostAuthorsIdsByDbFilter($arFilter)
Definition bloguser.php:344
__construct($cacheTime=0)
Definition bloguser.php:51
static GetUserName($alias, $name, $lastName, $login, $secondName="")
Definition bloguser.php:372
static onUserDelete($userId=NULL)
Definition bloguser.php:466
setCacheTime($cacheTime)
Definition bloguser.php:66
static getCommentAuthorsIdsByPostId($postId)
Definition bloguser.php:300
addAvatarSize($width, $height, $key="")
Definition bloguser.php:34
getUsers($ids=array())
Definition bloguser.php:79
static isUserGivenConsent($userId, $agreementId)
Definition bloguser.php:433
static createInstance($params=[])
Definition cache.php:134
static loadMessages($file)
Definition loc.php:64