View source
<?php
namespace Drupal\sms\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EntityInterface;
use Drupal\sms\Exception\SmsStorageException;
use Drupal\user\Entity\User;
use Drupal\user\UserInterface;
use Drupal\sms\Message\SmsMessageInterface as StdSmsMessageInterface;
use Drupal\sms\Message\SmsMessageResultInterface as StdMessageResultInterface;
class SmsMessage extends ContentEntityBase implements SmsMessageInterface {
protected $result = NULL;
public function getRecipients() {
$recipients = [];
foreach ($this
->get('recipient_phone_number') as $recipient) {
$recipients[] = $recipient->value;
}
return $recipients;
}
public function addRecipient($recipient) {
foreach ($this->recipient_phone_number as $item) {
if ($item->value == $recipient) {
return $this;
}
}
$this->recipient_phone_number
->appendItem($recipient);
return $this;
}
public function addRecipients(array $recipients) {
foreach ($recipients as $recipient) {
$this
->addRecipient($recipient);
}
return $this;
}
public function removeRecipient($recipient) {
$this->recipient_phone_number
->filter(function ($item) use ($recipient) {
return $item->value != $recipient;
});
return $this;
}
public function removeRecipients(array $recipients) {
$this->recipient_phone_number
->filter(function ($item) use ($recipients) {
return !in_array($item->value, $recipients);
});
return $this;
}
public function getOptions() {
return ($first = $this
->get('options')
->first()) ? $first
->getValue() : [];
}
public function getOption($name) {
$options = $this
->getOptions();
return isset($options[$name]) ? $options[$name] : NULL;
}
public function setOption($name, $value) {
$options = $this
->getOptions();
$options[$name] = $value;
$this
->set('options', $options);
return $this;
}
public function removeOption($name) {
$options = $this
->getOptions();
unset($options[$name]);
$this
->set('options', $options);
return $this;
}
public function getResult() {
if ($this->result || $this
->isNew()) {
return $this->result;
}
$results = $this
->entityTypeManager()
->getStorage('sms_result')
->loadByProperties([
'sms_message' => $this
->id(),
]);
return $results ? reset($results) : NULL;
}
public function setResult(StdMessageResultInterface $result = NULL) {
$previous_result = $this
->getResult();
if ($previous_result) {
throw new SmsStorageException('Saved SMS message results cannot be changed or updated.');
}
elseif ($result) {
$this->result = $result;
}
return $this;
}
public function getReport($recipient) {
if ($this->result) {
return $this->result
->getReport($recipient);
}
elseif (!$this
->isNew()) {
$reports = $this
->entityTypeManager()
->getStorage('sms_report')
->loadByProperties([
'sms_message' => $this
->id(),
'recipient' => $recipient,
]);
return $reports ? reset($reports) : NULL;
}
return NULL;
}
public function getReports() {
if ($this->result) {
return $this->result
->getReports();
}
elseif (!$this
->isNew()) {
return array_values($this
->entityTypeManager()
->getStorage('sms_report')
->loadByProperties([
'sms_message' => $this
->id(),
]));
}
return [];
}
public function getSender() {
$sender_name = $this
->get('sender_name');
if (isset($sender_name->value)) {
return $sender_name->value;
}
else {
return ($sender_entity = $this
->getSenderEntity()) ? $sender_entity
->label() : NULL;
}
}
public function setSender($sender) {
$this
->set('sender_name', $sender);
return $this;
}
public function getMessage() {
return $this
->get('message')->value;
}
public function setMessage($message) {
$this
->set('message', $message);
return $this;
}
public function getUuid() {
return $this
->get('uuid')->value;
}
public function getUid() {
$sender = $this
->getSenderEntity();
return $sender instanceof UserInterface ? $sender
->id() : NULL;
}
public function setUid($uid) {
$this
->setSenderEntity(User::load($uid));
return $this;
}
public function isAutomated() {
return $this
->get('automated')->value;
}
public function setAutomated($automated) {
$this
->set('automated', $automated);
return $this;
}
public function getDirection() {
return $this
->get('direction')->value;
}
public function setDirection($direction) {
$this
->set('direction', $direction);
return $this;
}
public function getGateway() {
return $this
->get('gateway')->entity;
}
public function setGateway(SmsGatewayInterface $gateway) {
$this
->set('gateway', $gateway);
return $this;
}
public function getSenderNumber() {
return $this
->get('sender_phone_number')->value;
}
public function setSenderNumber($number) {
$this
->set('sender_phone_number', $number);
return $this;
}
public function getSenderEntity() {
return $this
->get('sender_entity')->entity;
}
public function setSenderEntity(EntityInterface $entity) {
$this
->set('sender_entity', $entity);
return $this;
}
public function getRecipientEntity() {
return $this
->get('recipient_entity')->entity;
}
public function setRecipientEntity(EntityInterface $entity) {
$this
->set('recipient_entity', $entity);
return $this;
}
public function isQueued() {
return (bool) $this
->get('queued')->value;
}
public function setQueued($is_queued) {
$this
->set('queued', $is_queued);
return $this;
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function getSendTime() {
return $this
->get('send_on')->value;
}
public function setSendTime($send_time) {
$this
->set('send_on', $send_time);
return $this;
}
public function getProcessedTime() {
return $this
->get('processed')->value;
}
public function setProcessedTime($processed) {
$this
->set('processed', $processed);
return $this;
}
public function chunkByRecipients($size) {
$recipients_all = $this
->getRecipients();
if ($size < 1 || count($recipients_all) <= $size) {
return [
$this,
];
}
$base = $this
->createDuplicate();
$base
->removeRecipients($recipients_all);
$messages = [];
foreach (array_chunk($recipients_all, $size) as $recipients) {
$messages[] = $base
->createDuplicate()
->addRecipients($recipients);
}
return $messages;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('SMS message ID'))
->setDescription(t('The SMS message ID.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The SMS message UUID.'))
->setReadOnly(TRUE);
$fields['gateway'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Gateway Plugin'))
->setDescription(t('The gateway plugin instance.'))
->setSetting('target_type', 'sms_gateway')
->setReadOnly(TRUE)
->setRequired(TRUE);
$fields['direction'] = BaseFieldDefinition::create('integer')
->setLabel(t('Transmission direction'))
->setDescription(t('Transmission direction, See SmsMessageInterface::DIRECTION_*.'))
->setReadOnly(TRUE)
->setSetting('unsigned', FALSE)
->setSetting('size', 'tiny')
->setRequired(TRUE);
$fields['sender_name'] = BaseFieldDefinition::create('string')
->setLabel(t('Sender name'))
->setDescription(t('The name of the sender.'))
->setRequired(FALSE);
$fields['sender_phone_number'] = BaseFieldDefinition::create('telephone')
->setLabel(t('Sender phone number'))
->setDescription(t('The phone number of the sender.'))
->setDefaultValue('')
->setRequired(FALSE);
$fields['sender_entity'] = BaseFieldDefinition::create('dynamic_entity_reference')
->setLabel(t('Sender entity'))
->setDescription(t('The entity who sent the SMS message.'))
->setRequired(FALSE);
$fields['recipient_phone_number'] = BaseFieldDefinition::create('telephone')
->setLabel(t('Recipient phone number'))
->setDescription(t('The phone number of the recipient.'))
->setRequired(FALSE)
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED);
$fields['recipient_entity'] = BaseFieldDefinition::create('dynamic_entity_reference')
->setLabel(t('Recipient entity'))
->setDescription(t('The entity who received the SMS message.'))
->setRequired(FALSE);
$fields['options'] = BaseFieldDefinition::create('map')
->setLabel(t('Options'))
->setDescription(t('Options to pass to the gateway.'));
$fields['automated'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Is automated'))
->setDescription(t('Whether this SMS message was generated automatically. 0=generated by user action, 1=generated automatically.'))
->setDefaultValue(TRUE)
->setRequired(TRUE)
->setSetting('on_label', t('Automated'))
->setSetting('off_label', t('Not automated'));
$fields['queued'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Queued'))
->setDescription(t('Whether the SMS message is in the queue to be processed.'))
->setDefaultValue(FALSE)
->setRequired(TRUE)
->setSetting('on_label', t('Queued'))
->setSetting('off_label', t('Not queued'));
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Creation date'))
->setDescription(t('The time the SMS message was created.'))
->setRequired(TRUE);
$fields['send_on'] = BaseFieldDefinition::create('created')
->setLabel(t('Send date'))
->setDescription(t('The time to send the SMS message.'))
->setRequired(TRUE);
$fields['processed'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Processed'))
->setDescription(t('The time the SMS message was processed. This value does not indicate whether the message was sent, only that the gateway accepted the request.'))
->setRequired(FALSE);
$fields['message'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Message'))
->setDescription(t('The SMS message.'))
->setDefaultValue('')
->setRequired(TRUE);
return $fields;
}
public static function convertFromSmsMessage(StdSmsMessageInterface $sms_message) {
if ($sms_message instanceof static) {
return $sms_message;
}
$new = static::create();
$new
->setDirection($sms_message
->getDirection())
->setAutomated($sms_message
->isAutomated())
->setSender($sms_message
->getSender())
->setSenderNumber($sms_message
->getSenderNumber())
->addRecipients($sms_message
->getRecipients())
->setMessage($sms_message
->getMessage())
->setResult($sms_message
->getResult());
if ($gateway = $sms_message
->getGateway()) {
$new
->setGateway($gateway);
}
if ($uid = $sms_message
->getUid()) {
$new
->setUid($uid);
}
foreach ($sms_message
->getOptions() as $k => $v) {
$new
->setOption($k, $v);
}
return $new;
}
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
$results = [];
$reports = [];
foreach ($entities as $sms_message) {
if (($result = $sms_message
->getResult()) && $result instanceof EntityInterface) {
$results[] = $result;
}
foreach ($sms_message
->getReports() as $report) {
if ($report instanceof EntityInterface) {
$reports[] = $report;
}
}
}
\Drupal::entityTypeManager()
->getStorage('sms_result')
->delete($results);
\Drupal::entityTypeManager()
->getStorage('sms_report')
->delete($reports);
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
if ($this->result) {
$result_entity = SmsMessageResult::convertFromMessageResult($this->result);
$result_entity
->setSmsMessage($this)
->save();
foreach ($this->result
->getReports() as $report) {
$report_entity = SmsDeliveryReport::convertFromDeliveryReport($report);
$report_entity
->setSmsMessage($this)
->save();
}
unset($this->result);
}
}
}