163 if (!$checkResult->isSuccess())
166 $result->addErrors($checkResult->getErrors());
170 if ($this->checkRestrictions)
173 if (!$restrictionManager->isCanSendMessage())
175 return (
new Main\
Result())->addError(
184 $result = Internal\Entity\MessageTable::add([
186 'SENDER_ID' =>
$sender->getId(),
187 'AUTHOR_ID' => $this->getAuthorId(),
188 'MESSAGE_FROM' => $this->getFrom(),
189 'MESSAGE_TO' => $this->getTo(),
191 'MESSAGE_BODY' => $this->
getBody(),
192 'CLUSTER_GROUP' => defined(
'BX_CLUSTER_GROUP') ? BX_CLUSTER_GROUP : null
194 if ($result->isSuccess())
196 $this->
id = $result->getId();
197 if (Main\
Config\Option::get(
'messageservice',
'event_log_message_send',
'N') ===
'Y')
199 $userId = $USER instanceof \CUser ? $USER->getId() : 0;
200 \CEventLog::Log(
'INFO',
'MESSAGE_SEND',
'messageservice', $userId, $this->
getTo());
213 if (!$checkResult->isSuccess())
215 $result =
new Result\SendMessage();
216 return $result->addErrors($checkResult->getErrors());
219 if ($this->checkRestrictions)
222 if (!$restrictionManager->isCanSendMessage())
232 if (!Sender\Limitation::checkDailyLimit(
$sender->getId(), $this->getFrom()))
234 $result =
new Result\SendMessage();
244 'SENDER_ID' =>
$sender->getId(),
246 'MESSAGE_FROM' => $this->
getFrom(),
247 'MESSAGE_TO' => $this->
getTo(),
249 'MESSAGE_BODY' => $this->
getBody(),
250 'CLUSTER_GROUP' => defined(
'BX_CLUSTER_GROUP') ? BX_CLUSTER_GROUP : null
256 $result =
$sender->sendMessage($messageFields);
258 $messageFields[
'DATE_EXEC'] =
new DateTime();
259 $messageFields[
'SUCCESS_EXEC'] = $result->isSuccess() ?
'Y' :
'N';
261 if ($result->getExternalId() !==
null)
263 $messageFields[
'EXTERNAL_ID'] = $result->getExternalId();
265 if ($result->getStatus() !==
null)
267 $messageFields[
'STATUS_ID'] = $result->getStatus();
270 $addResult = Internal\Entity\MessageTable::add($messageFields);
271 if (!$addResult->isSuccess())
273 $result->addErrors($addResult->getErrors());
279 $this->
id = $addResult->getId();
280 $result->setId($this->
id);
282 if (Main\
Config\Option::get(
'messageservice',
'event_log_message_send',
'N') ===
'Y')
284 $userId = $USER instanceof \CUser ? $USER->getId() : 0;
285 \CEventLog::Log(
'INFO',
'MESSAGE_SEND',
'messageservice', $userId, $this->
getTo());
423 public function update(array $fields): bool
425 $updateResult = MessageTable::update($this->
id, $fields);
426 if (!$updateResult->isSuccess())
431 $this->setFields($fields);
434 $eventFields = array_merge([
'ID' => $this->
id], $fields);
435 Main\EventManager::getInstance()->send(
new Main\
Event(
437 static::EVENT_MESSAGE_UPDATED,
447 $toUpdate = [
'SUCCESS_EXEC' =>
'E',
'DATE_EXEC' =>
new DateTime()];
448 if ($result->isSuccess())
450 $toUpdate[
'SUCCESS_EXEC'] =
'Y';
451 if ($result->getExternalId() !==
null)
453 $toUpdate[
'EXTERNAL_ID'] = $result->getExternalId();
455 if ($result->getStatus() !==
null)
457 $toUpdate[
'STATUS_ID'] = $result->getStatus();
463 'SUCCESS_EXEC' =>
'N',
464 'NEXT_EXEC' => $nextExec,
473 $errors = $result->getErrorMessages();
476 $toUpdate[
'EXEC_ERROR'] = implode(PHP_EOL, $errors);