Skip to content

Commit

Permalink
Add sendNotificationMessage hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmolineus committed Aug 12, 2015
1 parent 393749e commit 0bcea88
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,34 @@ if (null !== $objNotificationCollection) {
$objNotification->send($arrTokens, $strLanguage); // Language is optional
}
}
```
```

## Hooks

If you want to enrich each message being send by some meta data or want to disable some messages being sent, you can
use the sendNotificationMessage hook:

```php

// config.php
$GLOBALS['TL_HOOKS']['sendNotificationMessage'][] = array('MyHook', 'execute');

// The hook
class MyHook
{
public function execute($objMessage, $arrTokens, $language, $objGatewayModel)
{
if (!$objMessage->regardUserSettings || !FE_USER_LOGGED_IN
|| $objMessage->getRelated('pid')->type !== 'custom_notification') {
return true;
}

$user = \MemberModel::findByPK($arrTokens['recipient']);
if (!$user || !$user->disableEmailNotifications) {
return true;
}

return false;
}
}
```
19 changes: 15 additions & 4 deletions library/NotificationCenter/Model/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,24 @@ public function send(array $arrTokens, $strLanguage = '')
return false;
}

if (null !== $objGatewayModel->getGateway()) {
return $objGatewayModel->getGateway()->send($this, $arrTokens, $strLanguage);
if (null === $objGatewayModel->getGateway()) {
\System::log(sprintf('Could not find gateway class for gateway ID "%s".', $objGatewayModel->id), __METHOD__, TL_ERROR);

return false;
}

\System::log(sprintf('Could not find gateway class for gateway ID "%s".', $objGatewayModel->id), __METHOD__, TL_ERROR);
if (isset($GLOBALS['TL_HOOKS']['sendNotificationMessage']) && is_array($GLOBALS['TL_HOOKS']['sendNotificationMessage'])) {
foreach ($GLOBALS['TL_HOOKS']['sendNotificationMessage'] as $arrCallback) {
$arrCallback = array(new $arrCallback[0](), $arrCallback[1]);
$blnSuccess = call_user_func($arrCallback, $this, $arrTokens, $strLanguage, $objGatewayModel);

if (!$blnSuccess) {
return false;
}
}
}

return false;
return $objGatewayModel->getGateway()->send($this, $arrTokens, $strLanguage);
}

/**
Expand Down

0 comments on commit 0bcea88

Please sign in to comment.