/**
  * Adds a message to the queue.
  *
  * @param Message   $message
  * @param           $tokens
  * @param           $language
  *
  * @return $this
  */
 public function addMessage(Message $message, $tokens, $language)
 {
     $gateway = $message->getRelated('gateway');
     if ($gateway === null || $gateway->type !== 'queue') {
         throw new \InvalidArgumentException('You cannot add a message to the queue that does not belong to any queue gateway.');
     }
     $objQueuedMessage = new QueuedMessage();
     $objQueuedMessage->message = $message->id;
     $objQueuedMessage->sourceQueue = $gateway->id;
     $objQueuedMessage->targetGateway = $gateway->queue_targetGateway;
     $objQueuedMessage->dateAdded = time();
     $objQueuedMessage->setTokens($tokens);
     $objQueuedMessage->language = $language;
     $objQueuedMessage->save();
     return $this;
 }
 /**
  * Modifies the palette for the queue gateway so it takes the one from the
  * target gateway
  *
  * @param \DataContainer $dc
  */
 public function modifyPalette(\DataContainer $dc)
 {
     if (\Input::get('act') != 'edit') {
         return;
     }
     $message = Message::findByPk($dc->id);
     $gateway = $message->getRelated('gateway');
     if ($gateway !== null && $gateway->type == 'queue') {
         $targetGateway = Gateway::findByPk($gateway->queue_targetGateway);
         $GLOBALS['TL_DCA']['tl_nc_message']['palettes']['queue'] = $GLOBALS['TL_DCA']['tl_nc_message']['palettes'][$targetGateway->type];
     }
 }
 public function addTokens(Message $objMessage, &$arrTokens, $strLanguage, $objGatewayModel)
 {
     if (($objNotification = $objMessage->getRelated('pid')) === null || !$objNotification->type) {
         return true;
     }
     // check for submission id
     if (!is_numeric($arrTokens['tl_submission'])) {
         return true;
     }
     if (($objSubmission = SubmissionModel::findByPk($arrTokens['tl_submission'])) === null) {
         return true;
     }
     if (!is_array($GLOBALS['SUBMISSION_RELATIONS'])) {
         return true;
     }
     foreach ($GLOBALS['SUBMISSION_RELATIONS'] as $strKey => $arrRelation) {
         if (!$arrRelation['table'] || !$arrRelation['submissionField']) {
             continue;
         }
         if ($objSubmission->{$arrRelation['submissionField']} < 1) {
             continue;
         }
         if (($objItem = SubmissionCreator::findRelatedEntity($objSubmission->{$arrRelation['submissionField']}, $arrRelation)) === null) {
             continue;
         }
         \Controller::loadDataContainer($arrRelation['table']);
         $objDc = new DC_Hybrid($arrRelation['table']);
         $objDc->activeRecord = $objItem;
         $arrData = SubmissionModel::prepareData($objItem, $arrRelation['table'], $GLOBALS['TL_DCA'][$arrRelation['table']], $objDc);
         $arrTokens = array_merge($arrTokens, SubmissionModel::tokenizeData($arrData, 'event'));
         // Options callback
         if (is_array($arrRelation['addTokens_callback']) && isset($arrCallback[0]) && class_exists($arrCallback[0])) {
             $arrCallback = $arrRelation['addTokens_callback'];
             $arrTokens = \Controller::importStatic($arrCallback[0])->{$arrCallback}[1]($objItem, $arrTokens, $arrRelation, $objNotification, $strLanguage, $objGatewayModel);
         } elseif (is_callable($arrRelation['addTokens_callback'])) {
             $arrTokens = $arrRelation['addTokens_callback']($objItem, $arrTokens, $arrRelation, $objNotification, $strLanguage, $objGatewayModel);
         }
     }
     return true;
 }
 public static function getNotificationMessagesAsOptions($objDc = null, $strType = null)
 {
     $arrOptions = array();
     if (!$strType) {
         $objMessages = Message::findAll();
         if ($objMessages === null) {
             return $arrOptions;
         }
         while ($objMessages->next()) {
             if (($objNotification = $objMessages->getRelated('pid')) === null) {
                 continue;
             }
             $arrOptions[$objNotification->title][$objMessages->id] = $objMessages->title;
         }
     } else {
         $objMessages = \Database::getInstance()->execute("SELECT m.id,m.title FROM tl_nc_message m INNER JOIN tl_nc_notification n ON m.pid=n.id WHERE n.type='{$strType}' ORDER BY m.title");
         if ($objMessages->numRows > 0) {
             while ($objMessages->next()) {
                 $arrOptions[$objMessages->id] = $objMessages->title;
             }
         }
     }
     return $arrOptions;
 }
 /**
  * Generate the module
  */
 protected function compile()
 {
     /** @var Message|\Model\Collection $objMessages */
     /** @noinspection PhpUndefinedMethodInspection */
     $objMessages = Message::findBy(array('pid IN (' . implode(',', $this->nc_member_customizable_notifications) . ') AND member_customizable<>\'\''), array());
     $arrOptions = array();
     $arrSelected = array();
     while ($objMessages->next()) {
         if (MemberMessages::memberHasSelected(\FrontendUser::getInstance()->id, $objMessages->id)) {
             $arrSelected[$objMessages->pid][] = $objMessages->id;
         }
         // Fetch tokens for parsing the option labels
         $objNotification = $objMessages->getRelated('pid');
         $objGateway = $objMessages->getRelated('gateway');
         $arrTokens = array_merge(array_combine(array_map(function ($key) {
             return 'message_' . $key;
         }, array_keys($objMessages->row())), $objMessages->row()), array_combine(array_map(function ($key) {
             return 'notification_' . $key;
         }, array_keys($objNotification->row())), $objNotification->row()), array_combine(array_map(function ($key) {
             return 'gateway_' . $key;
         }, array_keys($objGateway->row())), $objGateway->row()));
         $arrOptions[$objMessages->pid][$objMessages->id] = \StringUtil::parseSimpleTokens($this->nc_member_customizable_label ?: '##message_title## (##gateway_title##)', $arrTokens);
     }
     $objForm = new Form('tl_select_notifications', 'POST', function ($objHaste) {
         /** @noinspection PhpUndefinedMethodInspection */
         return \Input::post('FORM_SUBMIT') === $objHaste->getFormId();
     });
     foreach ($arrOptions as $k => $options) {
         /** @noinspection PhpUndefinedMethodInspection */
         $objForm->addFormField('notification_' . $k, array('label' => Notification::findByPk($objMessages->pid)->title, 'inputType' => $this->nc_member_customizable_inputType, 'options' => $options, 'eval' => array('mandatory' => $this->nc_member_customizable_mandatory), 'value' => !empty($arrSelected[$k]) ? $arrSelected[$k] : array()));
         // Add a validator
         // We check whether it is possible to send the message to the recipient by means of the gateway
         // E.g. a sms message requires a phone number set by the member which is not default
         $objForm->addValidator('notification_' . $k, function ($varValue, $objWidget, $objForm) use($k, $arrOptions) {
             if (empty($varValue)) {
                 return $varValue;
             }
             foreach ($varValue as $msg) {
                 /** @noinspection PhpUndefinedMethodInspection */
                 /** @var Message|\Model $objMessage */
                 $objMessage = Message::findByPk($msg);
                 /** @noinspection PhpUndefinedMethodInspection */
                 /** @var GatewayInterface|MessageDraftCheckSendInterface $objGateway */
                 $objGateway = $objMessage->getRelated('gateway')->getGateway();
                 if (!$objGateway instanceof MessageDraftCheckSendInterface) {
                     continue;
                 }
                 // Throw the error message as exception if the method has not yet
                 if (!$objGateway->canSendDraft($objMessage)) {
                     throw new \Exception(sprintf($GLOBALS['TL_LANG']['ERR']['messageNotSelectable'], $arrOptions[$k][$msg]));
                 }
             }
             return $varValue;
         });
     }
     $objForm->addSubmitFormField('submit', $GLOBALS['TL_LANG']['MSC']['saveSettings']);
     // Process form submit
     if ($objForm->validate()) {
         $arrData = $objForm->fetchAll();
         foreach ($arrData as $field => $notification) {
             if (strpos($field, 'notification_') !== 0) {
                 continue;
             }
             list(, $notificationId) = trimsplit('_', $field);
             // Delete
             foreach (array_diff((array) $arrSelected[$notificationId], (array) $notification) as $msg) {
                 /** @noinspection PhpUndefinedMethodInspection */
                 MemberMessages::findByMemberAndMessage(\FrontendUser::getInstance()->id, $msg)->delete();
             }
             // Create
             foreach (array_diff((array) $notification, (array) $arrSelected[$notificationId]) as $msg) {
                 /** @var MemberMessages|\Model $objMemberMessage */
                 $objMemberMessage = new MemberMessages();
                 $objMemberMessage->member_id = \FrontendUser::getInstance()->id;
                 $objMemberMessage->message_id = $msg;
                 $objMemberMessage->save();
             }
         }
     }
     $this->Template->form = $objForm->generate();
 }
 public function getNotificationMessagesAsOptions()
 {
     $arrOptions = array();
     if (($objMessages = Message::findAll()) === null) {
         return $arrOptions;
     }
     while ($objMessages->next()) {
         if (($objNotification = $objMessages->getRelated('pid')) === null) {
             continue;
         }
         $arrOptions[$objNotification->title][$objMessages->id] = $objMessages->title;
     }
     return $arrOptions;
 }
 public static function sendLockDeletionNotification($intNotificationMessage, $strTable, \Model $objEntity, \MemberModel $objFormerEditor, $arrTokens = array())
 {
     if ($intNotificationMessage && ($objMessage = Message::findByPk($intNotificationMessage)) !== null) {
         $objMessage->strEntityTable = $strTable;
         $objMessage->objEntity = $objEntity;
         $objMessage->objFormerEditor = $objFormerEditor;
         $objMessage->send(array_merge($arrTokens, static::generateTokens($strTable, $objEntity, $objFormerEditor)), $GLOBALS['TL_LANGUAGE']);
     }
 }