Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
blogpost.php
1<?php
3
4use Bitrix\Blog\Integration\Socialnetwork\Log as SocnetLogIntegration;
12
13class BlogPost extends Base
14{
15 const BLOG_POST_COPY_ERROR = "BLOG_POST_COPY_ERROR";
16
20 private $blogCommentCopier = null;
21
22 private $features = [];
23 private $changedRights = [];
24
25 protected $ufEntityObject = "BLOG_POST";
26 protected $ufDiskFileField = "UF_BLOG_POST_FILE";
27
33 public function setBlogCommentCopier(EntityCopier $blogCommentCopier)
34 {
35 $this->blogCommentCopier = $blogCommentCopier;
36 }
37
43 public function setFeatures(array $features)
44 {
45 $this->features = $features;
46 }
47
53 public function setChangedRights($changedRights)
54 {
55 $this->changedRights = $changedRights;
56 }
57
65 public function add(Container $container, array $fields)
66 {
67 $blogPostId = \CBlogPost::add($fields);
68
69 if ($blogPostId)
70 {
71 $this->addCategory($blogPostId, $fields);
72 $this->addNotify($blogPostId, $fields);
73 }
74 else
75 {
76 $this->result->addError(new Error("Blog post hasn't been added", self::BLOG_POST_COPY_ERROR));
77 }
78
79 return $blogPostId;
80 }
81
82 public function update($blogPostId, array $fields)
83 {
84 return \CBlogPost::update($blogPostId, $fields);
85 }
86
94 public function getFields(Container $container, $entityId)
95 {
96 $queryObject = \CBlogPost::getlist([], ["ID" => $entityId], false, false, ["*"]);
97
98 return (($fields = $queryObject->fetch()) ? $fields : []);
99 }
100
108 public function prepareFieldsToCopy(Container $container, array $fields)
109 {
110 $blogPostId = $fields["ID"];
111
112 $fields["SOCNET_RIGHTS"] = $this->getSocnetRights($blogPostId);
113
114 unset($fields["ID"]);
115
116 return $fields;
117 }
118
127 public function copyChildren(Container $container, $entityId, $copiedEntityId)
128 {
129 $this->copyUfFields($entityId, $copiedEntityId, $this->ufEntityObject);
130
131 $results = [];
132
133 if (in_array("comments", $this->features))
134 {
135 $results[] = $this->copyComments($entityId, $copiedEntityId);
136 }
137
138 return $this->getResult($results);
139 }
140
147 public function copyVote(int $voteId): int
148 {
149 $copyManager = new Manager([$voteId]);
150
151 if (in_array("voteResult", $this->features))
152 {
153 $copyManager->setResetVotingResult(false);
154 }
155
156 $result = $copyManager->startCopy();
157
158 return $this->getCopiedVoteId($voteId, $result->getData());
159 }
160
161 protected function getText($blogPostId)
162 {
163 $queryObject = \CBlogPost::getlist([], ["ID" => $blogPostId], false, false, ["DETAIL_TEXT"]);
164
165 if ($fields = $queryObject->fetch())
166 {
167 return ["DETAIL_TEXT", $fields["DETAIL_TEXT"]];
168 }
169 else
170 {
171 return ["DETAIL_TEXT", ""];
172 }
173 }
174
175 private function addCategory($blogPostId, array $fields)
176 {
177 if (!empty($fields["CATEGORY_ID"]))
178 {
179 \CBlogPostCategory::deleteByPostID($blogPostId);
180
181 $categoryIds = explode(",", $fields["CATEGORY_ID"]);
182 foreach ($categoryIds as $categoryId)
183 {
184 \CBlogPostCategory::add([
185 "BLOG_ID" => $fields["BLOG_ID"],
186 "POST_ID" => $blogPostId,
187 "CATEGORY_ID" => $categoryId
188 ]);
189 }
190 }
191 }
192
193 private function getSocnetRights($blogPostId)
194 {
195 $socnetRights = [];
196
197 $prevSocnetRights = \CBlogPost::getSocNetPerms($blogPostId, false);
198
199 foreach ($prevSocnetRights as $entityPrefix => $entities)
200 {
201 foreach ($entities as $entityId => $rights)
202 {
203 if ($entityPrefix != "SG")
204 {
205 $socnetRights[$entityId] = $entityPrefix.$entityId;
206 }
207 }
208 }
209
210 if ($this->changedRights)
211 {
212 foreach ($this->changedRights as $entityPrefix => $entitiesRatio)
213 {
214 foreach ($entitiesRatio as $oldEntityId => $newEntityId)
215 {
216 $socnetRights[$newEntityId] = $entityPrefix.$newEntityId;
217 }
218 }
219 }
220
221 return ($socnetRights ? $socnetRights : []);
222 }
223
224 private function copyComments($blogPostId, $copiedBlogPostId)
225 {
226 $containerCollection = new ContainerCollection();
227
228 $queryObject = \CBlogComment::getList([], ["POST_ID" => $blogPostId], false, false, ["ID"]);
229 while ($blogPostComment = $queryObject->fetch())
230 {
231 $container = new Container($blogPostComment["ID"]);
232 $container->setParentId($copiedBlogPostId);
233 $containerCollection[] = $container;
234 }
235
236 if (!$containerCollection->isEmpty())
237 {
238 return $this->blogCommentCopier->copy($containerCollection);
239 }
240
241 return new Result();
242 }
243
244 private function addNotify($blogPostId, $fields)
245 {
246 $fields["ID"] = $blogPostId;
247
248 $pathToPost = \COption::getOptionString("socialnetwork", "userblogpost_page",
249 "/company/personal/user/#user_id#/blog/#post_id#/", SITE_ID);
250 $pathToSmile = \COption::getOptionString("socialnetwork", "smile_page", false, SITE_ID);
251
252 $paramsNotify = [
253 "bSoNet" => true,
254 "UserID" => $fields["AUTHOR_ID"],
255 "allowVideo" => \COption::getOptionString("blog","allow_video", "Y"),
256 "PATH_TO_SMILE" => $pathToSmile,
257 "PATH_TO_POST" => $pathToPost,
258 "user_id" => $fields["AUTHOR_ID"],
259 "NAME_TEMPLATE" => \CSite::getNameFormat(false),
260 "SITE_ID" => SITE_ID
261 ];
262
263 $logId = \CBlogPost::notify($fields, [], $paramsNotify);
264
265 if ($logId)
266 {
268 $logFields = ["EVENT_ID" => $eventId];
269 if ($post = Post::getById($blogPostId))
270 {
271 $logFields["TAG"] = $post->getTags();
272 }
273 \CSocNetLog::update($logId, $logFields);
274 }
275 }
276
277 private function getCopiedVoteId($voteId, array $result)
278 {
279 $copiedVoteId = "";
280 foreach ($result as $value)
281 {
282 if (array_key_exists($voteId, $value))
283 {
284 $copiedVoteId = $value[$voteId];
285 break;
286 }
287 }
288 return $copiedVoteId;
289 }
290}
Definition blogpost.php:14
$ufEntityObject
Definition blogpost.php:25
getText($blogPostId)
Definition blogpost.php:161
setChangedRights($changedRights)
Definition blogpost.php:53
$ufDiskFileField
Definition blogpost.php:26
getFields(Container $container, $entityId)
Definition blogpost.php:94
setBlogCommentCopier(EntityCopier $blogCommentCopier)
Definition blogpost.php:33
copyVote(int $voteId)
Definition blogpost.php:147
setFeatures(array $features)
Definition blogpost.php:43
update($blogPostId, array $fields)
Definition blogpost.php:82
add(Container $container, array $fields)
Definition blogpost.php:65
copyChildren(Container $container, $entityId, $copiedEntityId)
Definition blogpost.php:127
const BLOG_POST_COPY_ERROR
Definition blogpost.php:15
prepareFieldsToCopy(Container $container, array $fields)
Definition blogpost.php:108
Definition post.php:20
static getById($postId=0, $params=array())
Definition post.php:35
copyUfFields(int $entityId, int $copiedEntityId, string $ufObject)