/** * 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(); }
/** * Skip messages that are member customizable and were not selected by the member * * @param Message|\Model $objMessage * @param array $arrTokens * @param string $strLanguage * @param Gateway|\Model $objGateway * * @return bool */ public function skipUnselectedMessages($objMessage, $arrTokens, $strLanguage, $objGateway) { $intMemberId = $arrTokens['member_id']; if (!$intMemberId) { return true; } // User did not customize their messages for this notification if (\Database::getInstance()->prepare(<<<SQL \t\tSELECT n.id FROM tl_nc_member_messages mm INNER JOIN tl_nc_message m ON mm.message_id = m.id INNER JOIN tl_nc_notification n ON m.pid = n.id WHERE mm.member_id=? AND m.member_customizable<>'' SQL )->execute($intMemberId)->numRows < 1) { return true; } // Message is member customizable but was not selected by the member if ($objMessage->member_customizable && !MemberMessages::memberHasSelected($intMemberId, $objMessage->id)) { return false; } return true; }