Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
vote.php
1<?php
3
14
15class Vote extends CopyImplementer
16{
17 private $resetVotingResult = true;
18
19 public function setResetVotingResult(bool $bool): void
20 {
21 $this->resetVotingResult = $bool;
22 }
23
27 private $questionCopier = null;
28
33 public function setQuestionCopier(EntityCopier $questionCopier): void
34 {
35 $this->questionCopier = $questionCopier;
36 }
37
46 public function add(Container $container, array $fields)
47 {
48 $result = VoteTable::add($fields);
49 if ($result->isSuccess())
50 {
51 return $result->getId();
52 }
53 else
54 {
55 $this->result->addErrors($result->getErrors());
56 return false;
57 }
58 }
59
69 public function getFields(Container $container, $entityId)
70 {
71 $queryObject = VoteTable::getById($entityId);
72 return (($fields = $queryObject->fetch()) ? $fields : []);
73 }
74
82 public function prepareFieldsToCopy(Container $container, array $fields)
83 {
84 unset($fields["ID"]);
85
86 if ($this->resetVotingResult)
87 {
88 unset($fields["COUNTER"]);
89 }
90
91 return $fields;
92 }
93
102 public function copyChildren(Container $container, $entityId, $copiedEntityId)
103 {
104 $results = [];
105
106 $results[] = $this->copyQuestion($entityId, $copiedEntityId);
107
108 $result = $this->getResult($results);
109
110 if (!$this->resetVotingResult)
111 {
112 $this->copyEvents($entityId, $copiedEntityId, $result);
113 }
114
115 return $result;
116 }
117
118 private function copyQuestion(int $voteId, int $copiedVoteId)
119 {
120 if (!$this->questionCopier)
121 {
122 return new Result();
123 }
124
125 $containerCollection = new ContainerCollection();
126
127 $queryObject = QuestionTable::getList(["filter" => ["VOTE_ID" => $voteId]]);
128 while ($question = $queryObject->fetch())
129 {
130 $container = new Container($question["ID"]);
131 $container->setParentId($copiedVoteId);
132 $containerCollection[] = $container;
133 }
134
135 if (!$containerCollection->isEmpty())
136 {
137 return $this->questionCopier->copy($containerCollection);
138 }
139
140 return new Result();
141 }
142
143 private function copyEvents($voteId, $copiedVoteId, Result $result)
144 {
145 try
146 {
147 $copiedIdsRelation = $this->getCopiedIdsRelation($result);
148 $ballots = $this->getEventBallots($voteId, $copiedIdsRelation);
149
150 $voteBaseCopiedVote = new VoteBase($copiedVoteId);
151 $eventObject = new Event($voteBaseCopiedVote);
152
153 $queryObject = EventTable::getList(["filter" => ["VOTE_ID" => $voteId]]);
154 while ($event = $queryObject->fetch())
155 {
156 $ballot = $ballots[$event["ID"]];
157
158 unset($event["ID"]);
159 $event["VOTE_ID"] = $copiedVoteId;
160
161 $eventObject->add($event, $ballot, false);
162 }
163 }
164 catch (\Exception $exception) {}
165 }
166
167 private function getCopiedIdsRelation(Result $result)
168 {
169 $copiedIdsRelation = [];
170
171 $resultData = $result->getData();
172 foreach ($resultData as $data)
173 {
174 array_walk($data, function($item, $key) use (&$copiedIdsRelation) {
175 if (is_array($item))
176 {
177 $copiedIdsRelation["answer"] = $item;
178 }
179 else
180 {
181 $copiedIdsRelation[$key] = $item;
182 }
183 });
184 }
185
186 return $copiedIdsRelation;
187 }
188
195 private function getEventBallots($voteId, $copiedIdsRelation)
196 {
197 $ballots = [];
198
199 $questionIds = [];
200 $answerIds = [];
201 foreach ($copiedIdsRelation as $key => $value)
202 {
203 if (is_int($key))
204 {
205 $questionIds[$key] = $value;
206 }
207 else
208 {
209 $answerIds = $answerIds + $value;
210 }
211 }
212
213 $voteBase = new VoteBase($voteId);
214 foreach ($voteBase->getStatistic() as $data)
215 {
216 $ballot = [];
217 foreach ($data["BALLOT"] as $questionId => $answer)
218 {
219 foreach ($answer as $answerId => $answerMessage)
220 {
221 $ballot[$questionIds[$questionId]] = [
222 $answerIds[$answerId] => $answerMessage
223 ];
224 }
225 }
226 $ballots[$data["ID"]] = ["BALLOT" => $ballot];
227 }
228
229 return $ballots;
230 }
231}
getFields(Container $container, $entityId)
Definition vote.php:69
setResetVotingResult(bool $bool)
Definition vote.php:19
setQuestionCopier(EntityCopier $questionCopier)
Definition vote.php:33
add(Container $container, array $fields)
Definition vote.php:46
copyChildren(Container $container, $entityId, $copiedEntityId)
Definition vote.php:102
prepareFieldsToCopy(Container $container, array $fields)
Definition vote.php:82