/**
  * Adds an existing channel to the folder. If the given channel is already
  * part of another folder, it will be automatically removed from the old folder.
  *
  * @param MessageChannel $channel The channel which should be added to the folder.
  *
  * @return AbstractPostboxFolder Returns itself (fluent-interface)
  */
 public function addChannel(MessageChannel $channel)
 {
     // first remove the channel
     /* @var $oldFolder AbstractPostboxFolder */
     $oldFolder = $channel->loadRelatedObject('PostboxFolder2MessageChannel');
     if ($oldFolder !== null) {
         $oldFolder->removeChannel($channel);
     }
     $this->createAssociation('PostboxFolder2MessageChannel', $channel);
     return $this;
 }
Example #2
0
 /**
  * Creates a new channel in the postbox.
  *
  * @param string $Title The title of the new channel.
  * @param string $MessageText The text for the first message in the channel.
  * @param GenericORMapperDataObject[] $Readers An array of users which will be recipients.
  *
  * @return MessageChannel The new created channel
  * @throws InvalidArgumentException
  */
 public function createChannel($Title, $MessageText, array $Readers)
 {
     $Message = new Message();
     $Message->setText($MessageText);
     $Message->setAuthor($this->User);
     $Channel = new MessageChannel();
     $Channel->setTitle($Title);
     $Channel->addRelatedObject('MessageChannel2Message', $Message);
     $Channel->addRelatedObject('User2MessageChannel', $this->User);
     // we do not use the channel's addReaders() function here, because this would only work for an existing channel!
     $RealReadersPresent = false;
     foreach ($Readers as &$Reader) {
         // author could have been added as reader as well, let's filter this possibility
         // and check if reader is blocking the current user
         if ($Reader->getObjectId() !== $this->User->getObjectId() && !$this->isOnUsersBlacklist($Reader)) {
             $RealReadersPresent = true;
             $Channel->addRelatedObject('User2MessageChannel', $Reader);
             $Message->addRelatedObject('User2UnreadMessage', $Reader);
             $Channel->addRelatedObject('User2UnreadMessageChannel', $Reader);
         }
     }
     if (!$RealReadersPresent) {
         throw new InvalidArgumentException('[Postbox::createChannel()] There are no recipients, maybe you tried to send a message to yourself, or the recipient(s) are blocking you!');
     }
     $Channel->setDataComponent($this->ORM);
     return $Channel->save();
 }