cb-techservices / yii2-notification-system
一个提供完全功能的通知系统并具有可定制UI的Yii2扩展。
Requires
- bower-asset/toastr: ^2.1
- rmm5t/jquery-timeago: ^1.6
- yiisoft/yii2: ~2.0.0
This package is auto-updated.
Last update: 2024-09-20 13:17:22 UTC
README
这个Yii2扩展提供了一个完全功能的通知系统,该系统由ActiveRecord支持,并具有可定制的UI。
安装
Composer
php composer.phar require cb-techservices/yii2-notification-system "*"
或者添加
"cb-techservices/yii2-notification-system": "*"
到你的composer.json
文件的require部分。
配置
在使用此模块之前,你需要运行它的迁移脚本。这将把通知表添加到你的数据库中。
从你的Yii项目根目录运行以下命令
./yii migrate/up --migrationPath=vendor/cb-techservices/yii2-notification-system/migrations/
将以下内容添加到你的Yii项目配置的modules
部分。
'modules'=>[ 'notifications' => [ 'class' => 'cbtech\notification_system\NotificationSystemModule', //The controller's namespace for where to find the Controller actions. //You may use the default NotificationsController provided or create your own custom controller. 'controllerNamespace' => 'cbtech\notification_system\controllers', // Point this to your own Notification class // See the "Declaring your notifications" section below 'notificationClass' => 'common\models\Notification', // Allow to have notification with same (user_id, key, key_id) // Default to FALSE 'allowDuplicate' => true, // 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; }, 'expirationTime'=>0 ], ],
模块参数
声明你的通知
你的自定义通知类必须 继承 cbtech\notification_system\models\NotificationBase
示例请参考 examples/Notification.php
使用方法
触发通知
// A connection request made by a user to the $recipient_id Notification::notify(Notification::KEY_NEW_CONNECTION_REQUEST, $recipient_id, $connectionRequest->id); // You may also use the following static methods to set the notification type: Notification::warning(Notification::KEY_NEW_MESSAGE, $recipient_id, $message->id); Notification::success(Notification::ORDER_PLACED, $admin_id, $order->id); Notification::error(Notification::KEY_NO_DISK_SPACE, $admin_id);
在UI中监听和显示通知
此扩展附带一个NotificationsWidget
,用于定期从服务器获取新通知。
小部件参数
小部件使用
以下是一个包含所有可能参数的小部件示例。可选值已标注。这应该添加到你的主要布局模板顶部。
NotificationsWidget::widget([ 'pollUrl' => '/notifications/notifications/poll', //Optional, default value 'markAsReadUrl' => '/notifications/notifications/read', //Optional, default value 'markAsUnreadUrl' => '/notifications/notifications/unread', //Optional, default value 'flashUrl' => '/notifications/notifications/flash', //Optional, default value 'readAllUrl' => '/notifications/notifications/read-all', //Optional, default value 'unreadAllUrl' => '/notifications/notifications/unread-all', //Optional, default value 'clientOptions' => [ 'location' => 'tr', ], 'delay' => 5000, 'xhrTimeout' => 2000, 'pollInterval' => 5000, 'counters' => [ '.notifications-header-count', '.notifications-icon-count' ], 'markAllReadSelector' => '#notification-read-all', 'markAllUnreadSelector' => '#notification-unread-all', 'listSelector' => '#notifications', 'viewAllSelector' => '#viewAll', 'viewUnreadSelector' => '#viewUnread', 'headerSelector' => '#notifications-header', 'headerTitle' => 'Notifications', 'headerTemplate' => '<div class="col-xs-12">' . '<div class="pull-left" style="font-size:14px;font-weight:bold;margin-left:10px;">{title}</div>' . '<button id="{readAllId}" class="btn btn-xs btn-link pull-right" style="color:#3399ff;" data-keepOpenOnClick>Read</button>' . '<button id="{unreadAllId}" class="btn btn-xs btn-link pull-right" style="color:#3399ff;" data-keepOpenOnClick>Unread</button>' . '<label style="font-size:12px;padding-top:1px;" class="pull-right">Mark All as </label>' . '</div>', //Optional, default value 'listItemTemplate' => '<div class="notificationRow" id="notification_{id}" data-keepOpenOnClick>' . '<div class="col-xs-11" onclick="goToRoute(\'{id}\');">' . '<div class="notification-title">{title}</div>' . '<div class="notification-body">{body}</div>' . '</div>' . '<div class="col-xs-1">' . '<div class="notification-actions pull-right">{read}{unread}</div>' . '</div>' . '<div class="clearfix"></div>' . '<div class="col-xs-1">' . '<div class="notification-timeago">{timeago}</div>' . '</div>' . '<div class="col-xs-10">' . '<div class="notification-footer">{footer}</div>' . '</div>' . '<div class="clearfix"></div>' . '</div>', //Optional, default value ]);
如果你已经为headerSelector
和/或listItemTemplate
提供了值,你可以在navbar中添加以下内容以包含通知列表视图
$menuItems[] = '<li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"> <span style="font-size:18px;top: 5px;margin-right:3px;" class="glyphicon glyphicon-bell"></span> <span class="badge notifications-icon-count">0</span> </a> <ul class="dropdown-menu"> <li id="notifications-header" style="text-align:center;"></li> <li id="notifications"></li> <li style="text-align:center;border-bottom:1px #cccccc solid;padding:10px;"> <button class="btn btn-xs btn-primary" id="viewAll" data-keepOpenOnClick>View All</button> / <button class="btn btn-xs btn-primary" id="viewUnread" data-keepOpenOnClick>View Unread</button> </li> </ul> </li>';
通知列表视图
Tostr通知
贡献者
Carl Burnstein https://github.com/carlb0329
致谢
受machour/yii2-notifications的启发
使用CodeSeven/toastr
许可
Yii2 Notification System遵循MIT许可 - https://github.com/cb-techservices/yii2-notification-system/blob/master/LICENSE
版权(c)2018 CB Tech Services