50 if (!static::isAvailable())
52 return (
new Result())->addError(
new Error(Loc::getMessage(
'VOTE_INTEGRATION_IM_NOT_AVAILABLE')));
58 return (
new Result())->addError(
new Error(Loc::getMessage(
'VOTE_INTEGRATION_IM_CHANNEL_FAILED')));
61 $voteFields = self::prepareVoteData(
$channelId, $voteFields, $ownerUserId);
62 $validateResult = self::validateVoteData($voteFields);
63 if (!$validateResult->isSuccess())
65 return $validateResult;
70 $voteId = Vote::saveData(0, $voteFields);
74 return (
new Result())->addError(
new Error($exception->getMessage()));
79 return (
new Result())->addError(
new Error(Loc::getMessage(
'VOTE_INTEGRATION_IM_SAVE_ERROR')));
83 if (empty($savedVoteData))
85 return (
new Result())->addError(
new Error(Loc::getMessage(
'VOTE_INTEGRATION_IM_SAVE_ERROR')));
90 'TO_CHAT_ID' => $chatId,
91 'FROM_USER_ID' => $ownerUserId,
92 'MESSAGE' => self::getFallbackText($savedVoteData),
95 'COMPONENT_ID' => self::MESSAGE_COMPONENT_ID,
96 'COMPONENT_PARAMS' => [
97 self::MESSAGE_COMPONENT_PARAM_VOTE_ID => $voteId,
98 'data' => self::formatVoteData($savedVoteData),
105 return (
new Result())->addError(
new Error(Loc::getMessage(
'VOTE_INTEGRATION_IM_MESSAGE_FAILED')));
118 private static function getFallbackText(
array $voteFields): ?string
120 if (isset($voteFields[
'QUESTIONS']) && is_array($voteFields[
'QUESTIONS']))
122 $firstKey = array_key_first($voteFields[
'QUESTIONS']);
124 return trim($voteFields[
'QUESTIONS'][$firstKey][
'QUESTION'] ??
'');
133 $voteFields[
'DATE_START'] =
new DateTime();
134 $voteFields[
'DATE_END'] = (
new DateTime())->add(
"10Y");
135 $voteFields[
'UNIQUE_TYPE'] = EventLimits::BY_USER_ID | EventLimits::BY_USER_AUTH;
136 $voteFields[
'AUTHOR_ID'] = $ownerId;
137 $voteFields[
'OPTIONS'] = $voteFields[
'OPTIONS'] ?? 0;
142 private static function validateVoteData(
array &$voteFields): Result
146 $check = Vote::checkData($voteFields);
148 catch (ArgumentException $exception)
150 return (
new Result())->addError(
new Error($exception->getMessage()));
153 if (empty($voteFields[
'QUESTIONS']) || !is_array($voteFields[
'QUESTIONS']))
155 return (
new Result())->addError(
new Error(Loc::getMessage(
'VOTE_INTEGRATION_IM_QUESTIONS_EMPTY')));
160 return (
new Result())->addError(
new Error(Loc::getMessage(
'VOTE_INTEGRATION_IM_NOT_VALID')));
163 if (
count($voteFields[
'QUESTIONS']) > self::getMaxQuestionsCount())
165 return (
new Result())->addError(
new Error(
166 Loc::getMessage(
'VOTE_INTEGRATION_IM_TO_MANY_QUESTIONS', [
167 '#COUNT#' => self::getMaxQuestionsCount(),
172 foreach ($voteFields[
'QUESTIONS'] as $question)
175 empty($question[
'ANSWERS'])
176 || !is_array($question)
177 ||
count($question[
'ANSWERS']) < self::getMinAnswersCount()
180 return (
new Result())->addError(
new Error(Loc::getMessage(
'VOTE_INTEGRATION_IM_MIN_ANSWERS', [
181 '#MIN_ANSWERS_COUNT#' => self::getMinAnswersCount(),
186 !empty($question[
'ANSWERS'])
187 && is_array($question[
'ANSWERS'])
188 &&
count($question[
'ANSWERS']) > self::getMaxAnswersCount()
191 return (
new Result())->addError(
new Error(
192 Loc::getMessage(
'VOTE_INTEGRATION_IM_TO_MANY_ANSWERS', [
193 '#COUNT#' => self::getMaxAnswersCount(),
205 'ANONYMITY' => (int)
$fields[
'ANONYMITY'],
206 'OPTIONS' => (
int)
$fields[
'OPTIONS'],
207 'QUESTIONS' => array_map(
208 fn(
array $question) => self::formatVoteQuestion($question),
214 private static function formatVoteQuestion(
array $question):
array
217 'ID' => (int)$question[
'ID'],
218 'QUESTION' => $question[
'QUESTION'],
219 'FIELD_TYPE' => (
int)$question[
'FIELD_TYPE'],
220 'ANSWERS' => array_map(
221 fn(
array $answer) => self::formatVoteAnswer($answer),
222 $question[
'ANSWERS'],
227 private static function formatVoteAnswer(
array $answer):
array
230 'ID' => (int)$answer[
'ID'],
231 'MESSAGE' => $answer[
'MESSAGE'],
232 'REACTION' => $answer[
'REACTION'] ??
null,
static includeModule($moduleName)