Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
topic.php
1<?php
3
10
12{
13 const TOPIC_COPY_ERROR = "TOPIC_COPY_ERROR";
14
18 private $commentCopier;
19
23 public function setCommentCopier(EntityCopier $commentCopier): void
24 {
25 $this->commentCopier = $commentCopier;
26 }
27
33 public function add(Container $container, array $fields)
34 {
35 $topicId = \CForumTopic::add($fields);
36
37 if (!$topicId)
38 {
39 $this->result->addError(new Error("Error creating a new topic", self::TOPIC_COPY_ERROR));
40 }
41
42 return $topicId;
43 }
44
52 public function getFields(Container $container, $entityId)
53 {
54 $topic = \CForumTopic::GetByIDEx($entityId);
55
56 return ($topic ? $topic : []);
57 }
58
66 public function prepareFieldsToCopy(Container $container, array $fields)
67 {
68 $fields = $this->cleanDataToCopy($fields);
69
70 $dictionary = $container->getDictionary();
71
72 if (!empty($dictionary["XML_ID"]))
73 {
74 $fields["XML_ID"] = $dictionary["XML_ID"];
75 }
76
77 return $fields;
78 }
79
88 public function copyChildren(Container $topicContainer, $topicId, $copiedTopicId)
89 {
90 if (!$this->commentCopier)
91 {
92 return new Result();
93 }
94
95 $containerCollection = new ContainerCollection();
96
97 $topicDictionary = $topicContainer->getDictionary();
98
99 $queryObject = \CForumMessage::getList([], ["TOPIC_ID" => $topicId]);
100 while ($forumMessage = $queryObject->Fetch())
101 {
102 $container = new Container($forumMessage["ID"]);
103 $container->setParentId($copiedTopicId);
104 $container->setDictionary($topicDictionary);
105 $containerCollection[] = $container;
106 }
107
108 $results = [];
109
110 if (!$containerCollection->isEmpty())
111 {
112 $results[] = $this->commentCopier->copy($containerCollection);
113 }
114
115 return $this->getResult($results);
116 }
117
118 private function cleanDataToCopy(array $fields)
119 {
120 unset($fields["ID"]);
121 unset($fields["POSTS"]);
122 unset($fields["START_DATE"]);
123 unset($fields["LAST_POST_DATE"]);
124 unset($fields["ABS_LAST_POST_DATE"]);
125
126 return $fields;
127 }
128}
getFields(Container $container, $entityId)
Definition topic.php:52
setCommentCopier(EntityCopier $commentCopier)
Definition topic.php:23
add(Container $container, array $fields)
Definition topic.php:33
copyChildren(Container $topicContainer, $topicId, $copiedTopicId)
Definition topic.php:88
prepareFieldsToCopy(Container $container, array $fields)
Definition topic.php:66