/** * Create a message from options * @param array $options An array of options * 'sender' => Sender guid or entity * 'recipients' => Recipient guid or entity, or an array of guids or entities * 'subject' => Message subject * 'body' => Message body * 'hash' => Message hash * 'message_type' => Message type * 'attachments' => Entities to attach, or their guids * @return Message */ public static function factory(array $options = array()) { $defaults = array('sender' => 0, 'recipients' => array(), 'subject' => '', 'body' => '', 'hash' => '', 'message_type' => Message::TYPE_PRIVATE, 'attachments' => array()); $options = array_merge($defaults, $options); $message = new Message(); $message->setSubject(elgg_extract('subject', $options))->setBody(elgg_extract('body', $options))->setSender(elgg_extract('sender', $options))->setRecipients(elgg_extract('recipients', $options))->setMessageType(elgg_extract('message_type', $options))->setHash(elgg_extract('hash', $options))->setAttachments(elgg_extract('attachments', $options)); return $message; }
/** * {@inheritdoc} */ public function execute() { if ($this->entity instanceof Message) { $this->message_hash = $this->entity->getHash(); $this->message_type = $this->entity->getMessageType(); } else { if (!$this->message_type) { $this->message_type = Message::TYPE_PRIVATE; } } // files being uploaded via $_FILES $uploads = UploadHandler::handle('attachments'); if ($uploads) { foreach ($uploads as $upload) { if ($upload instanceof ElggFile) { $this->attachment_guids[] = $upload->guid; } } } $this->attachments = Group::create($this->attachment_guids)->entities(); $access_id = AccessCollection::create(array($this->sender_guid, $this->recipient_guids))->getCollectionId(); foreach ($this->attachments as $attachment) { $attachment->origin = 'messages'; $attachment->access_id = $access_id; $attachment->save(); } $guid = Message::factory(array('sender' => $this->sender_guid, 'recipients' => $this->recipient_guids, 'subject' => $this->subject, 'body' => $this->body, 'message_hash' => $this->message_hash, 'attachments' => $this->attachments))->send(); $this->entity = $guid ? get_entity($guid) : false; if (!$this->entity) { // delete attachment if message failed to send foreach ($this->attachments as $attachment) { $attachment->delete(); } $this->result->addError(elgg_echo('inbox:send:error:generic')); return; } $sender = $this->entity->getSender(); $this->message_type = $this->entity->getMessageType(); $this->message_hash = $this->entity->getHash(); $ruleset = hypeInbox()->config->getRuleset($this->message_type); $this->attachments = array_map(array(hypeInbox()->model, 'getLinkTag'), $this->attachments); $body = array_filter(array($ruleset->hasSubject() ? $this->entity->subject : '', $this->entity->getBody(), implode(', ', array_filter($this->attachments)))); $notification_body = implode(PHP_EOL, $body); foreach ($this->recipient_guids as $recipient_guid) { $recipient = get_entity($recipient_guid); if (!$recipient) { continue; } $type_label = strtolower($ruleset->getSingularLabel($recipient->language)); $subject = elgg_echo('inbox:notification:subject', array($type_label), $recipient->language); $notification = elgg_echo('inbox:notification:body', array($type_label, $sender->name, $notification_body, elgg_view('output/url', array('href' => $this->entity->getURL())), $sender->name, elgg_view('output/url', array('href' => elgg_normalize_url("messages/thread/{$this->message_hash}#reply")))), $recipient->language); $summary = elgg_echo('inbox:notification:summary', array($type_label), $recipient->language); notify_user($recipient->guid, $sender->guid, $subject, $notification, array('action' => 'send', 'object' => $this->entity, 'summary' => $summary)); } $this->result->addMessage(elgg_echo('inbox:send:success')); $this->result->setForwardURL($this->entity->getURL()); }
public static function getAttachmentsProp(\hypeJunction\Data\PropertyInterface $prop, Message $message) { $options = $message->getAttachmentsFilterOptions(array('limit' => 0)); return new \hypeJunction\Graph\BatchResult('elgg_get_entities_from_relationship', $options); }
} if (empty(elgg_strip_tags($body))) { register_error(elgg_echo('inbox:send:error:no_body')); forward(REFERRER); } $enable_html = elgg_get_plugin_setting('enable_html', 'hypeInbox'); if (!$enable_html) { $body = elgg_strip_tags($body); } $message_hash = ''; $message_type = get_input('message_type', Message::TYPE_PRIVATE); if ($original_message instanceof Message) { $message_hash = $original_message->getHash(); $message_type = $original_message->getMessageType(); } $message = Message::factory(array('sender' => $sender_guid, 'recipients' => $recipient_guids, 'subject' => $subject, 'body' => $body, 'hash' => $message_hash, 'message_type' => $message_type)); $guid = $message->send(); if (!$guid) { register_error(elgg_echo('inbox:send:error:generic')); forward(REFERRER); } $new_message = get_entity($guid); $sender = $new_message->getSender(); $message_type = $new_message->getMessageType(); $message_hash = $new_message->getHash(); $ruleset = hypeInbox()->config->getRuleset($message_type); $recipients = $new_message->getRecipients(); foreach ($recipients as $recipient) { if ($recipient->guid == $sender->guid) { continue; }
/** * Returns normalized message URL * * @param Message $entity Message * @return string */ public function getMessageURL(Message $entity) { $friendly = elgg_get_friendly_title($entity->getDisplayName()); return $this->normalize(array('read', $entity->guid, $friendly . "#elgg-object-{$entity->guid}")); }