9use \Bitrix\Main\AccessDeniedException;
10use \Bitrix\Main\ArgumentNullException;
11use \Bitrix\Main\ArgumentTypeException;
12use \Bitrix\Main\Entity;
13use \Bitrix\Main\Error;
14use \Bitrix\Main\ErrorCollection;
15use \Bitrix\Main\InvalidOperationException;
16use \Bitrix\Main\Localization\Loc;
17use \Bitrix\Main\ArgumentException;
18use \Bitrix\Main\NotSupportedException;
19use \Bitrix\Main\Type\DateTime;
20use \Bitrix\Vote\Attachment\Connector;
21use \Bitrix\Vote\Base\BaseObject;
22use \Bitrix\Vote\DBResult;
23use \Bitrix\Main\SystemException;
24use \Bitrix\Vote\Event;
25use \Bitrix\Main\ObjectNotFoundException;
64 return 'b_vote_attached_object';
75 'data_type' =>
'integer',
77 'autocomplete' =>
true,
81 'data_type' =>
'integer',
85 'data_type' =>
'string',
89 'ENTITY_TYPE' => array(
90 'data_type' =>
'string',
95 'data_type' =>
'integer',
98 'CREATE_TIME' => array(
99 'data_type' =>
'datetime',
102 'CREATED_BY' => array(
103 'data_type' =>
'integer',
107 'data_type' =>
'\Bitrix\Vote\VoteTable',
108 'reference' => array(
109 '=this.OBJECT_ID' =>
'ref.ID',
111 'join_type' =>
'INNER',
120 public static function getList(array $parameters = array())
122 return new DBResult(parent::getList($parameters));
135 throw new \Bitrix\Main\ArgumentNullException(
'filter');
138 $result = static::getList(array(
139 'select' => array(
'ID'),
142 while($row = $result->fetch())
144 if(!empty($row[
'ID']))
146 $resultDelete = static::delete($row[
'ID']);
147 if(!$resultDelete->isSuccess())
170 "attachIds" => array(),
171 "voteIds" => array(),
172 "entities" => array()
183 if (is_array($this->
id))
201 if (!array_key_exists(
"MODULE_ID",
$attach) ||
$attach[
"MODULE_ID"] ==
'')
203 if (!array_key_exists(
"ENTITY_TYPE",
$attach) ||
$attach[
"ENTITY_TYPE"] ==
'')
205 if (array_key_exists(
"ID",
$attach))
206 $this->
id = intval(
$attach[
"ID"]);
252 $filter = array_change_key_case(
$id, CASE_UPPER);
253 $id = md5(serialize($filter));
260 if (!array_key_exists(
$id, self::$storage))
262 self::$storage[
$id] =
null;
267 'V_LAMP' =>
'VOTE.LAMP',
268 'Q_' =>
'VOTE.QUESTION.*',
269 'A_' =>
'VOTE.QUESTION.ANSWER',
273 'VOTE.QUESTION.C_SORT' =>
'ASC',
274 'VOTE.QUESTION.ID' =>
'ASC',
275 'VOTE.QUESTION.ANSWER.C_SORT' =>
'ASC',
276 'VOTE.QUESTION.ANSWER.ID' =>
'ASC',
283 $vote = [
"ID" =>
null];
284 $question = [
"ID" =>
null];
286 while (($res = $dbRes->fetch()) && $res)
288 $buffer = [
"attach" => [],
"vote" => [],
"question" => []];
291 foreach ($res as $key => $val)
293 if (mb_strpos($key,
"O_") === 0)
294 $buffer[
"attach"][mb_substr($key, 2)] = $val;
295 else if (mb_strpos($key,
"V_") === 0)
296 $buffer[
"vote"][mb_substr($key, 2)] = $val;
297 else if (mb_strpos($key,
"Q_") === 0)
298 $buffer[
"question"][mb_substr($key, 2)] = $val;
299 else if (mb_strpos($key,
"A_") === 0)
300 $answer[mb_substr($key, 2)] = $val;
302 if ($buffer[
"attach"][
"ID"] !=
$attach[
"ID"])
308 if ($buffer[
"vote"][
"ID"] !=
$vote[
"ID"])
311 $vote = $buffer[
"vote"] + array(
314 "QUESTIONS" => array());
315 if (
$vote[
"IMAGE_ID"] > 0)
317 if (!array_key_exists(
$vote[
"ID"], Vote::$storage))
320 if ($buffer[
"question"][
"ID"] != $question[
"ID"])
323 $question = $buffer[
"question"] + array(
328 if ($question[
"IMAGE_ID"] > 0)
329 $images[$question[
"IMAGE_ID"]] = &$question[
"IMAGE"];
332 $vote[
"QUESTIONS"][$question[
"ID"]] = &$question;
334 $answer[
"FIELD_NAME"] = $answer[
"~FIELD_NAME"] = \Bitrix\Vote\Event::getFieldName(
$attach[
"ID"], $question[
"ID"]);
335 $answer[
"MESSAGE_FIELD_NAME"] = \Bitrix\Vote\Event::getMessageFieldName(
$attach[
"ID"], $question[
"ID"], $answer[
"ID"]);
342 $answer[
"FIELD_NAME"] = $answer[
"MESSAGE_FIELD_NAME"];
346 $answer[
"FIELD_TYPE"] = $question[
"FIELD_TYPE"];
348 $answer[
"~PERCENT"] = ($question[
"COUNTER"] > 0 ? $answer[
"COUNTER"] * 100 / $question[
"COUNTER"] : 0);
349 $answer[
"PERCENT"] = round($answer[
"~PERCENT"], 2);
350 $question[
"ANSWERS"][$answer[
"ID"]] = &$answer;
354 unset(
$vote); unset($question);
356 if (count($images) > 0)
358 $dbRes = \Bitrix\Main\FileTable::getList(array(
'select' => array(
'*'),
'filter' => array(
'ID' => array_keys($images))));
359 while ($res = $dbRes->fetch())
361 $images[$res[
"ID"]] = $res + array(
"SRC" => \CFile::GetFileSRC($res));
371 self::$storage[
$id] = (is_array(self::$storage[
$id]) ? self::$storage[
$id] : array());
377 return self::$storage[
$id];
387 $id1 = md5(serialize(
$id));
388 if (!array_key_exists($id1, self::$storage))
390 self::$storage[$id1] = array();
396 'V_LAMP' =>
'VOTE.LAMP',
397 'Q_' =>
'VOTE.QUESTION.*',
398 'A_' =>
'VOTE.QUESTION.ANSWER',
401 'VOTE.QUESTION.C_SORT' =>
'ASC',
402 'VOTE.QUESTION.ID' =>
'ASC',
403 'VOTE.QUESTION.ANSWER.C_SORT' =>
'ASC',
404 'VOTE.QUESTION.ANSWER.ID' =>
'ASC',
407 'ENTITY_TYPE' =>
$id[
'ENTITY_TYPE'],
408 'ENTITY_ID' =>
$id[
'ENTITY_ID']
411 if (($res = $dbRes->fetch()) && $res)
415 foreach ($res as $key => $val)
416 if (mb_strpos($key,
"O_") === 0)
417 $attach[mb_substr($key, 2)] = $val;
418 else if (mb_strpos($key,
"V_") === 0)
419 $vote[mb_substr($key, 2)] = $val;
420 $vote[
"QUESTIONS"] = array();
421 $questions = &
$vote[
"QUESTIONS"];
424 $question = array(); $answer = array();
425 foreach ($res as $key => $val)
427 if (mb_strpos($key,
"Q_") === 0)
428 $question[mb_substr($key, 2)] = $val;
429 else if (mb_strpos($key,
"A_") === 0)
430 $answer[mb_substr($key, 2)] = $val;
432 $qid =
"".$question[
"ID"];
433 if (!array_key_exists($qid, $questions))
434 $questions[$qid] = array_merge($question, array(
"ANSWERS" => array()));
437 $answers = &$questions[$qid][
"ANSWERS"];
440 if (!array_key_exists($answer[
"ID"], $answers))
441 $answers[$answer[
"ID"]] = $answer;
446 }
while (($res = $dbRes->fetch()) && $res);
451 return self::$storage[$id1];
472 return $this->
getConnector()->canRead($userId) && is_object($this->vote) && $this->vote[
"LAMP"] ==
"green";
477 return $this->vote->canVote($userId);
482 return $this->vote->canRevote($userId);
487 return $this->vote->canReadResult($userId);
508 if ($this->connector ===
null)
510 $this->connector = Connector::buildFromAttachedObject($this);
520 if (!($this->channel instanceof
Channel))
522 $this->
setStorage($this->vote instanceof
Vote ? $this->vote[
"CHANNEL_ID"] :
null);
532 return array_key_exists(
"ID", $this->attach) ? $this->attach[
"ID"] :
null;
540 return is_object($this->vote) ? $this->vote[
"ID"] :
null;
548 return $this->attach[
"MODULE_ID"];
556 return $this->attach[
"ENTITY_TYPE"];
564 return $this->attach[
"ENTITY_ID"];
572 if (is_object($this->vote))
573 $this->vote->fillStatistic();
580 public function delete()
582 if (empty($this->vote))
585 if ($this->attach[
"ID"] > 0)
586 AttachTable::delete($this->attach[
"ID"]);
589 "select" => array(
"ID",
"OBJECT_ID"),
590 "filter" => array(
"OBJECT_ID" => $this->vote[
"ID"]),
628 $data = array_merge($data, (is_null($this->vote) ? [
633 "DATE_END" => (isset($data[
"DATE_END"]) ?
new DateTime($data[
"DATE_END"]) : (
new DateTime())->add(
"1Y"))
636 Vote::checkData($data, $data[
"ID"]);
637 if (($data[
"TITLE"] ??
null) ==
'' && is_array($data[
"QUESTIONS"]))
639 $q = reset($data[
"QUESTIONS"]);
640 if (is_array($q) && $q[
"QUESTION"] <>
'')
642 $data[
"TITLE"] = $q[
"QUESTION"];
668 public function save($data, $createdBy = 0)
670 if (!isset($data[
"AUTHOR_ID"]))
671 $data[
"AUTHOR_ID"] = $createdBy;
675 $voteId = Vote::saveData(is_null($this->vote) ? 0 : $this->vote[
"ID"], $data);
678 if (!array_key_exists(
"ID", $this->attach))
680 $id = AttachTable::add(array(
682 'OBJECT_ID' => $voteId,
685 'CREATED_BY' => $createdBy,
691 $id = $this->attach[
"ID"];
697 else if ($this->attach[
"ID"] ??
null > 0)
699 $this->attach =
null;
718 if (!is_object($this->vote))
720 $res = \Bitrix\Vote\Event::getDataFromRequest($this->
getAttachId(), $request);
722 $result = $this->vote->voteFor($request, [
"revote" =>
true]);
724 $result = $this->vote->registerEvent($res, [
"revote" =>
true],
User::getCurrent());
726 $this->errorCollection->add($this->vote->getErrors());
736 if (!is_object($this->vote))
738 $this->vote->exportExcel();
748 return $this->vote->isVotedFor($userId);
759 if (!is_object($this->vote))
761 return $this->vote->resume();
771 if (!is_object($this->vote))
773 return $this->vote->stop();
782 if (is_array($this->attach) && array_key_exists($offset, $this->attach) || is_object($this->vote) && isset($this->vote[$offset]))
784 if ($offset ==
"VOTE_ID" && is_object($this->vote))
795 if (is_array($this->attach) && array_key_exists($offset, $this->attach))
796 return $this->attach[$offset];
797 if (is_object($this->vote))
799 if (isset($this->vote[$offset]))
800 return $this->vote[$offset];
801 if ($offset ==
"VOTE_ID")
802 return $this->vote[
"ID"];
836 return parent::loadFromId(
$id,
true);
static loadMessages($file)
static getMessage($code, $replace=null, $language=null)
save($data, $createdBy=0)
static getDataByEntity(array $id)
offsetSet($offset, $value)
static loadFromId($id, $shouldBeNewIfIdIsNull=false)
static deleteByFilter(array $filter)
static getList(array $parameters=array())
static loadFromId($id, $shouldBeNewIfIdIsNull=false)
static getExtrasFieldName($id, $name)
static getFieldName($id, $questionId)