Before using this module, you have to run its migrations scripts:
./yii migrate/up --migrationPath=vendor/machour/yii2-notifications/migrations/
You also need to enable the module in Yii modules
section of the configuration file:
return [
// ...
'modules' => [
'notifications' => [
'class' => 'machour\yii2\notifications\NotificationsModule',
// Point this to your own Notification class
// See the "Declaring your notifications" section below
'notificationClass' => 'app\models\Notification',
// Allow to have notification with same (user_id, key, key_id)
// Default to FALSE
'allowDuplicate' => false,
// Allow custom date formatting in database
'dbDateFormat' => 'Y-m-d H:i:s',
// This callable should return your logged in user Id
'userId' => function() {
return \Yii::$app->user->id;
}
],
// your other modules ..
],
// ...
]
namespace backend\components;
use Yii;
use common\models\Meeting;
use common\models\Message;
use machour\yii2\notifications\models\Notification as BaseNotification;
class Notification extends BaseNotification
{
/**
* A new message notification
*/
const KEY_NEW_MESSAGE = 'new_message';
/**
* A meeting reminder notification
*/
const KEY_MEETING_REMINDER = 'meeting_reminder';
/**
* No disk space left !
*/
const KEY_NO_DISK_SPACE = 'no_disk_space';
/**
* @var array Holds all usable notifications
*/
public static $keys = [
self::KEY_NEW_MESSAGE,
self::KEY_MEETING_REMINDER,
self::KEY_NO_DISK_SPACE,
];
/**
* @inheritdoc
*/
public function getTitle()
{
switch ($this->key) {
case self::KEY_MEETING_REMINDER:
return Yii::t('app', 'Meeting reminder');
case self::KEY_NEW_MESSAGE:
return Yii::t('app', 'You got a new message');
case self::KEY_NO_DISK_SPACE:
return Yii::t('app', 'No disk space left');
}
}
/**
* @inheritdoc
*/
public function getDescription()
{
switch ($this->key) {
case self::KEY_MEETING_REMINDER:
$meeting = Meeting::findOne($this->key_id);
return Yii::t('app', 'You are meeting with {customer}', [
'customer' => $meeting->customer->name
]);
case self::KEY_NEW_MESSAGE:
$message = Message::findOne($this->key_id);
return Yii::t('app', '{customer} sent you a message', [
'customer' => $meeting->customer->name
]);
case self::KEY_NO_DISK_SPACE:
// We don't have a key_id here
return 'Please buy more space immediately';
}
}
/**
* @inheritdoc
*/
public function getRoute()
{
switch ($this->key) {
case self::KEY_MEETING_REMINDER:
return ['meeting', 'id' => $this->key_id];
case self::KEY_NEW_MESSAGE:
return ['message/read', 'id' => $this->key_id];
case self::KEY_NO_DISK_SPACE:
return 'https://aws.amazon.com/';
};
}
}