From 3ba6054630485138b869d90c4d6c0bc356697113 Mon Sep 17 00:00:00 2001 From: Egorka Date: Mon, 13 Aug 2018 16:24:45 +0300 Subject: [PATCH] Notification register Notification widget --- backend/messages/ru/main.php | 1 + backend/views/layouts/header.php | 71 ++++++---------------- backend/widgets/NotificationCountWidget.php | 26 ++++++++ backend/widgets/views/notification-count.php | 38 ++++++++++++ common/modules/forms/FormsModule.php | 17 ++++++ .../controllers/manage/FormMessageController.php | 1 + common/modules/forms/entities/FormMessage.php | 10 ++- .../forms/entities/queries/FormMessageQuery.php | 20 ++++++ common/modules/forms/messages/ru/form.php | 1 + .../forms/services/FormMessageManageService.php | 7 +++ .../forms/views/manage/form-message/index.php | 4 +- 11 files changed, 139 insertions(+), 57 deletions(-) create mode 100644 backend/widgets/NotificationCountWidget.php create mode 100644 backend/widgets/views/notification-count.php create mode 100644 common/modules/forms/entities/queries/FormMessageQuery.php diff --git a/backend/messages/ru/main.php b/backend/messages/ru/main.php index 7905a65..ab7246a 100644 --- a/backend/messages/ru/main.php +++ b/backend/messages/ru/main.php @@ -31,4 +31,5 @@ return [ 'Title attribute' => 'Атрибут тега Title', 'Url' => 'Ссылка', 'Sign in to start your session' => 'Вход в панель управления', + 'You have {count} notifications' => 'Новых уведомлений: {count}', ]; \ No newline at end of file diff --git a/backend/views/layouts/header.php b/backend/views/layouts/header.php index e0cf4e0..58d56d7 100644 --- a/backend/views/layouts/header.php +++ b/backend/views/layouts/header.php @@ -23,6 +23,7 @@ use core\components\avatar_generator\AvatarGenerator; - + --> + diff --git a/common/modules/forms/FormsModule.php b/common/modules/forms/FormsModule.php index 07990dc..8dc03ec 100644 --- a/common/modules/forms/FormsModule.php +++ b/common/modules/forms/FormsModule.php @@ -2,8 +2,11 @@ namespace common\modules\forms; +use common\modules\forms\entities\Form; +use common\modules\forms\entities\FormMessage; use core\components\modules\ModuleInterface; use yii\helpers\ArrayHelper; +use yii\helpers\Html; /** @@ -28,6 +31,7 @@ class FormsModule extends \yii\base\Module implements ModuleInterface public function bootstrap($app) { + // prepare rules $app->getUrlManager()->addRules([ 'forms/manage/form/view/' => 'forms/manage/form/view', ]); @@ -60,5 +64,18 @@ class FormsModule extends \yii\base\Module implements ModuleInterface 'visible' => \Yii::$app->user->can( 'admin' ) || \Yii::$app->user->can( 'FormsManagement' ), ]; } + + // prepare notifications + $new_messages_count = FormMessage::find()->unread()->count(); + if ($new_messages_count > 0) { + $app->params['notifications'][] = [ + 'icon' => 'address-card-o', + 'color' => 'yellow', + 'message' => 'New forms messages: {count}', + 'message_file' => 'form', + 'url' => '/forms/manage/form-message/index', + 'count' => $new_messages_count, + ]; + } } } diff --git a/common/modules/forms/controllers/manage/FormMessageController.php b/common/modules/forms/controllers/manage/FormMessageController.php index 3dcd6b4..4231d37 100644 --- a/common/modules/forms/controllers/manage/FormMessageController.php +++ b/common/modules/forms/controllers/manage/FormMessageController.php @@ -68,6 +68,7 @@ class FormMessageController extends Controller */ public function actionView($id) { + $this->service->setRead($id); return $this->render('view', [ 'message' => $this->findModel($id), ]); diff --git a/common/modules/forms/entities/FormMessage.php b/common/modules/forms/entities/FormMessage.php index 50be6ce..d44b05d 100644 --- a/common/modules/forms/entities/FormMessage.php +++ b/common/modules/forms/entities/FormMessage.php @@ -2,6 +2,7 @@ namespace common\modules\forms\entities; +use common\modules\forms\entities\queries\FormMessageQuery; use yii\behaviors\TimestampBehavior; use yii\db\ActiveRecord; use Yii; @@ -19,8 +20,8 @@ use Yii; */ class FormMessage extends ActiveRecord { - const STATUS_ACTIVE = 1; - const STATUS_DRAFT = 0; + const STATUS_OLD = 0; + const STATUS_NEW = 1; public static function create( $form_id, @@ -72,4 +73,9 @@ class FormMessage extends ActiveRecord { return $this->hasOne(Form::class, ['id' => 'form_id']); } + + public static function find(): FormMessageQuery + { + return new FormMessageQuery(static::class); + } } \ No newline at end of file diff --git a/common/modules/forms/entities/queries/FormMessageQuery.php b/common/modules/forms/entities/queries/FormMessageQuery.php new file mode 100644 index 0000000..fd6de70 --- /dev/null +++ b/common/modules/forms/entities/queries/FormMessageQuery.php @@ -0,0 +1,20 @@ +andWhere([ + 'new' => FormMessage::STATUS_NEW, + ]); + } +} \ No newline at end of file diff --git a/common/modules/forms/messages/ru/form.php b/common/modules/forms/messages/ru/form.php index 2701efc..8026967 100644 --- a/common/modules/forms/messages/ru/form.php +++ b/common/modules/forms/messages/ru/form.php @@ -25,4 +25,5 @@ return [ 'For editor' => 'Для редактора', 'Form Data' => 'Данные формы', 'Date' => 'Дата', + 'New forms messages: {count}' => 'Новых сообщений из форм: {count}', ]; \ No newline at end of file diff --git a/common/modules/forms/services/FormMessageManageService.php b/common/modules/forms/services/FormMessageManageService.php index 824b860..3ce1964 100644 --- a/common/modules/forms/services/FormMessageManageService.php +++ b/common/modules/forms/services/FormMessageManageService.php @@ -34,6 +34,13 @@ class FormMessageManageService $this->repository->save($message); } + public function setRead($id) + { + $message = $this->repository->get($id); + $message->new = 0; + $this->repository->save($message); + } + public function remove($id): void { $message = $this->repository->get($id); diff --git a/common/modules/forms/views/manage/form-message/index.php b/common/modules/forms/views/manage/form-message/index.php index 55efcf8..211ade7 100644 --- a/common/modules/forms/views/manage/form-message/index.php +++ b/common/modules/forms/views/manage/form-message/index.php @@ -35,7 +35,9 @@ $this->params['breadcrumbs'][] = $this->title; 'filter' => ArrayHelper::map(Form::find()->all(), 'id', 'name'), 'attribute' => 'form_id', 'value' => function (FormMessage $model) { - return Html::a(Html::encode($model->form->name), ['view', 'id' => $model->id]); + return Html::a(Html::encode($model->form->name), ['view', 'id' => $model->id], [ + 'style' => $model->new == 1 ? 'font-weight: bold' : '', + ]); }, 'format' => 'raw', ],