Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
blogpost.php
1<?php
2
4
9
10Loc::loadMessages(__FILE__);
11
12class BlogPost extends Provider
13{
14 public const PROVIDER_ID = 'BLOG_POST';
15 public const CONTENT_TYPE_ID = 'BLOG_POST';
16
17 protected static $blogPostClass = \CBlogPost::class;
18
19 public static function getId(): string
20 {
21 return static::PROVIDER_ID;
22 }
23
24 public function getEventId(): array
25 {
26 $result = [ 'blog_post', 'blog_post_important', 'blog_post_micro' ];
27 if (ModuleManager::isModuleInstalled('intranet'))
28 {
29 $result[] = 'blog_post_grat';
30 }
31 if (ModuleManager::isModuleInstalled('vote'))
32 {
33 $result[] = 'blog_post_vote';
34 }
35
36 return $result;
37 }
38
39 public function getType(): string
40 {
42 }
43
44 public function getCommentProvider(): Provider
45 {
46 return new BlogComment();
47 }
48
49 public function initSourceFields()
50 {
51 static $cache = [];
52
53 $postId = $this->entityId;
54
55 if ($postId <= 0)
56 {
57 return;
58 }
59
60 if (isset($cache[$postId]))
61 {
62 $post = $cache[$postId];
63 }
64 elseif (Loader::includeModule('blog'))
65 {
66 $res = self::$blogPostClass::getList(
67 [],
68 [
69 'ID' => $postId
70 ]
71 );
72
73 $post = $res->fetch();
74 $cache[$postId] = $post;
75 }
76
77 if (empty($post))
78 {
79 return;
80 }
81
82 $checkAccess = ($this->getOption('checkAccess') !== false);
83 if (
84 $checkAccess
85 && !self::canRead([
86 'POST' => $post
87 ])
88 )
89 {
90 return;
91 }
92
93 if (!empty($post['DETAIL_TEXT']))
94 {
95 $post['DETAIL_TEXT'] = Emoji::decode($post['DETAIL_TEXT']);
96 }
97
98 $this->setSourceFields($post);
99 $this->setSourceDescription($post['DETAIL_TEXT']);
100 $this->setSourceTitle(truncateText(($post['MICRO'] === 'N' ? $post['TITLE'] : htmlspecialcharsback($post['TITLE'])), 100));
101 $this->setSourceAttachedDiskObjects($this->getAttachedDiskObjects($this->cloneDiskObjects));
102 $this->setSourceDiskObjects($this->getDiskObjects($postId, $this->cloneDiskObjects));
103 }
104
105 public function getPinnedTitle(): string
106 {
107 $result = '';
108
109 if (empty($this->sourceFields))
110 {
111 $this->initSourceFields();
112 }
113
114 $post = $this->getSourceFields();
115 if (empty($post))
116 {
117 return $result;
118 }
119
120 return ($post['MICRO'] === 'N' ? truncateText($post['TITLE'], 100) : '');
121 }
122
123 public function getPinnedDescription()
124 {
125 $result = '';
126
127 if (empty($this->sourceFields))
128 {
129 $this->initSourceFields();
130 }
131
132 $post = $this->getSourceFields();
133 if (empty($post))
134 {
135 return $result;
136 }
137
138 $result = truncateText(str_replace('&#39;', "'", htmlspecialcharsBack(\CTextParser::clearAllTags($post['DETAIL_TEXT']))), 100);
139 $result = preg_replace('/^' . (\Bitrix\Main\Application::isUtfMode() ? "\xC2\xA0" : "\xA0") . '$/', '', $result);
140
141 if (
142 $result === ''
143 && Loader::includeModule('disk')
144 )
145 {
146 $fileNameList = [];
147 $res = \Bitrix\Disk\AttachedObject::getList([
148 'filter' => [
149 '=ENTITY_TYPE' => \Bitrix\Disk\Uf\BlogPostConnector::className(),
150 'ENTITY_ID' => $this->entityId
151 ],
152 'select' => [ 'ID', 'FILENAME' => 'OBJECT.NAME' ]
153 ]);
154 foreach ($res as $attachedObjectFields)
155 {
156 $fileNameList[] = $attachedObjectFields['FILENAME'];
157 }
158 $result = truncateText(implode(' ', $fileNameList), 100);
159 }
160
161 return $result;
162 }
163
164 protected function getAttachedDiskObjects($clone = false)
165 {
166 return $this->getEntityAttachedDiskObjects([
167 'userFieldEntity' => 'BLOG_POST',
168 'userFieldCode' => 'UF_BLOG_POST_FILE',
169 'clone' => $clone,
170 ]);
171 }
172
173 public static function canRead($params): bool
174 {
175 static $blogPostProvider = null;
176
177 if (
178 !is_array($params)
179 && (int)$params > 0
180 )
181 {
182 $params = [
183 'POST' => \CBlogPost::getById($params),
184 ];
185 }
186
187 $result = false;
188 if (
189 isset($params['POST'])
190 && is_array($params['POST'])
191 )
192 {
193 if ($blogPostProvider === null)
194 {
195 $blogPostProvider = new self;
196 }
197
198 $permissions = $blogPostProvider->getPermissions($params['POST']);
199 $result = ($permissions > self::PERMISSION_DENY);
200 }
201
202 return $result;
203 }
204
205 protected function getPermissions(array $post): string
206 {
207 global $USER;
208
209 $result = self::PERMISSION_DENY;
210
211 if (Loader::includeModule('blog'))
212 {
213 if ((int)$post['AUTHOR_ID'] === (int)$USER->getId())
214 {
215 $result = self::PERMISSION_FULL;
216 }
217 else
218 {
219 if (
220 !empty($post['BLOG_ID'])
221 && ModuleManager::isModuleInstalled('idea')
222 && (int)$post['BLOG_ID'] === $this->getIdeaBlogId()
223 )
224 {
225 $perms = \CBlogPost::getBlogUserPostPerms($post['ID'], (int)$USER->getId());
226 }
227 else
228 {
229 $perms = \CBlogPost::getSocNetPostPerms([
230 'POST_ID' => $post['ID'],
231 'NEED_FULL' => true,
232 'USER_ID' => false,
233 'POST_AUTHOR_ID' => $post['AUTHOR_ID'],
234 'PUBLIC' => false,
235 'LOG_ID' => false,
236 ]);
237 }
238
239 if ($perms >= BLOG_PERMS_FULL)
240 {
241 $result = self::PERMISSION_FULL;
242 }
243 elseif ($perms >= BLOG_PERMS_READ)
244 {
245 $result = self::PERMISSION_READ;
246 }
247 }
248 }
249
250 return $result;
251 }
252
253 protected function getIdeaBlogId(array $params = []): ?int
254 {
255 static $resultCache = [];
256
257 if (!ModuleManager::isModuleInstalled('idea'))
258 {
259 return null;
260 }
261
262 $siteId = ($params['siteId'] ?? SITE_ID);
263
264 if (isset($resultCache[$siteId]))
265 {
266 return $resultCache[$siteId];
267 }
268
269 $resultCache[$siteId] = null;
270
271 if ($blogFields = \CBlog::getByUrl('idea_' . $siteId))
272 {
273 $resultCache[$siteId] = (int)$blogFields['ID'];
274 }
275
276 return $resultCache[$siteId];
277 }
278
279 public function getLiveFeedUrl(BlogPostService $service = null): string
280 {
281 if ($service === null)
282 {
283 $service = new BlogPostService();
284 }
285
286 $pathToPost = $service->getPathToPost();
287
288 if (
289 !empty($pathToPost)
290 && ($post = $this->getSourceFields())
291 && !empty($post)
292 )
293 {
294 $pathToPost = \CComponentEngine::makePathFromTemplate($pathToPost, [
295 'post_id' => $post['ID'],
296 'user_id' => $post['AUTHOR_ID']
297 ]);
298 }
299
300 return $pathToPost;
301 }
302
303 public function getSuffix(): string
304 {
305 return '2';
306 }
307}
308
310{
311 public function getPathToPost(): string
312 {
313 return \Bitrix\Socialnetwork\Helper\Path::get('userblogpost_page');
314 }
315}
static loadMessages($file)
Definition loc.php:64
static decode($text)
Definition emoji.php:24
Definition blogpost.php:13
initSourceFields()
Definition blogpost.php:49
getCommentProvider()
Definition blogpost.php:44
getIdeaBlogId(array $params=[])
Definition blogpost.php:253
const CONTENT_TYPE_ID
Definition blogpost.php:15
getPinnedTitle()
Definition blogpost.php:105
getPermissions(array $post)
Definition blogpost.php:205
static $blogPostClass
Definition blogpost.php:17
getAttachedDiskObjects($clone=false)
Definition blogpost.php:164
getType()
Definition blogpost.php:39
getEventId()
Definition blogpost.php:24
static canRead($params)
Definition blogpost.php:173
getSuffix()
Definition blogpost.php:303
static getId()
Definition blogpost.php:19
const PROVIDER_ID
Definition blogpost.php:14
getLiveFeedUrl(BlogPostService $service=null)
Definition blogpost.php:279
getPinnedDescription()
Definition blogpost.php:123
Definition blogpost.php:310
getPathToPost()
Definition blogpost.php:311
setSourceAttachedDiskObjects(array $diskAttachedObjects)
Definition provider.php:739
getEntityAttachedDiskObjects(array $params=[])
getDiskObjects($entityId, $clone=false)
Definition provider.php:822