Browse Source

Notification register

Notification widget
master
Egorka 6 years ago
parent
commit
3ba6054630
  1. 1
      backend/messages/ru/main.php
  2. 71
      backend/views/layouts/header.php
  3. 26
      backend/widgets/NotificationCountWidget.php
  4. 38
      backend/widgets/views/notification-count.php
  5. 17
      common/modules/forms/FormsModule.php
  6. 1
      common/modules/forms/controllers/manage/FormMessageController.php
  7. 10
      common/modules/forms/entities/FormMessage.php
  8. 20
      common/modules/forms/entities/queries/FormMessageQuery.php
  9. 1
      common/modules/forms/messages/ru/form.php
  10. 7
      common/modules/forms/services/FormMessageManageService.php
  11. 4
      common/modules/forms/views/manage/form-message/index.php

1
backend/messages/ru/main.php

@ -31,4 +31,5 @@ return [
'Title attribute' => 'Атрибут тега Title', 'Title attribute' => 'Атрибут тега Title',
'Url' => 'Ссылка', 'Url' => 'Ссылка',
'Sign in to start your session' => 'Вход в панель управления', 'Sign in to start your session' => 'Вход в панель управления',
'You have {count} notifications' => 'Новых уведомлений: {count}',
]; ];

71
backend/views/layouts/header.php

@ -23,6 +23,7 @@ use core\components\avatar_generator\AvatarGenerator;
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
<!-- Messages: style can be found in dropdown.less--> <!-- Messages: style can be found in dropdown.less-->
<!--
<li class="dropdown messages-menu"> <li class="dropdown messages-menu">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-envelope-o"></i> <i class="fa fa-envelope-o"></i>
@ -31,9 +32,9 @@ use core\components\avatar_generator\AvatarGenerator;
<ul class="dropdown-menu"> <ul class="dropdown-menu">
<li class="header">You have 4 messages</li> <li class="header">You have 4 messages</li>
<li> <li>
<!-- inner menu: contains the actual data -->
<ul class="menu"> <ul class="menu">
<li><!-- start message --> <li>
<a href="#"> <a href="#">
<div class="pull-left"> <div class="pull-left">
<img src="<?= $directoryAsset ?>/img/user2-160x160.jpg" class="img-circle" <img src="<?= $directoryAsset ?>/img/user2-160x160.jpg" class="img-circle"
@ -46,7 +47,7 @@ use core\components\avatar_generator\AvatarGenerator;
<p>Why not buy a new awesome theme?</p> <p>Why not buy a new awesome theme?</p>
</a> </a>
</li> </li>
<!-- end message -->
<li> <li>
<a href="#"> <a href="#">
<div class="pull-left"> <div class="pull-left">
@ -104,49 +105,12 @@ use core\components\avatar_generator\AvatarGenerator;
<li class="footer"><a href="#">See All Messages</a></li> <li class="footer"><a href="#">See All Messages</a></li>
</ul> </ul>
</li> </li>
<li class="dropdown notifications-menu"> -->
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-bell-o"></i> <?= \backend\widgets\NotificationCountWidget::widget() ?>
<span class="label label-warning">10</span>
</a>
<ul class="dropdown-menu">
<li class="header">You have 10 notifications</li>
<li>
<!-- inner menu: contains the actual data -->
<ul class="menu">
<li>
<a href="#">
<i class="fa fa-users text-aqua"></i> 5 new members joined today
</a>
</li>
<li>
<a href="#">
<i class="fa fa-warning text-yellow"></i> Very long description here that may
not fit into the page and may cause design problems
</a>
</li>
<li>
<a href="#">
<i class="fa fa-users text-red"></i> 5 new members joined
</a>
</li>
<li>
<a href="#">
<i class="fa fa-shopping-cart text-green"></i> 25 sales made
</a>
</li>
<li>
<a href="#">
<i class="fa fa-user text-red"></i> You changed your username
</a>
</li>
</ul>
</li>
<li class="footer"><a href="#">View all</a></li>
</ul>
</li>
<!-- Tasks: style can be found in dropdown.less --> <!-- Tasks: style can be found in dropdown.less -->
<!--
<li class="dropdown tasks-menu"> <li class="dropdown tasks-menu">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-flag-o"></i> <i class="fa fa-flag-o"></i>
@ -155,9 +119,8 @@ use core\components\avatar_generator\AvatarGenerator;
<ul class="dropdown-menu"> <ul class="dropdown-menu">
<li class="header">You have 9 tasks</li> <li class="header">You have 9 tasks</li>
<li> <li>
<!-- inner menu: contains the actual data -->
<ul class="menu"> <ul class="menu">
<li><!-- Task item --> <li>
<a href="#"> <a href="#">
<h3> <h3>
Design some buttons Design some buttons
@ -172,8 +135,8 @@ use core\components\avatar_generator\AvatarGenerator;
</div> </div>
</a> </a>
</li> </li>
<!-- end task item -->
<li><!-- Task item --> <li>
<a href="#"> <a href="#">
<h3> <h3>
Create a nice theme Create a nice theme
@ -188,8 +151,8 @@ use core\components\avatar_generator\AvatarGenerator;
</div> </div>
</a> </a>
</li> </li>
<!-- end task item -->
<li><!-- Task item --> <li>
<a href="#"> <a href="#">
<h3> <h3>
Some task I need to do Some task I need to do
@ -204,8 +167,8 @@ use core\components\avatar_generator\AvatarGenerator;
</div> </div>
</a> </a>
</li> </li>
<!-- end task item -->
<li><!-- Task item --> <li>
<a href="#"> <a href="#">
<h3> <h3>
Make beautiful transitions Make beautiful transitions
@ -220,7 +183,7 @@ use core\components\avatar_generator\AvatarGenerator;
</div> </div>
</a> </a>
</li> </li>
<!-- end task item -->
</ul> </ul>
</li> </li>
<li class="footer"> <li class="footer">
@ -228,7 +191,7 @@ use core\components\avatar_generator\AvatarGenerator;
</li> </li>
</ul> </ul>
</li> </li>
<!-- User Account: style can be found in dropdown.less --> -->
<li class="dropdown user user-menu"> <li class="dropdown user user-menu">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">

26
backend/widgets/NotificationCountWidget.php

@ -0,0 +1,26 @@
<?php
/**
* Created by Error202
* Date: 13.08.2018
*/
namespace backend\widgets;
use yii\base\Widget;
class NotificationCountWidget extends Widget
{
public function run()
{
$count = 0;
foreach (\Yii::$app->params['notifications'] as $notification) {
$count += $notification['count'];
}
return $this->render('notification-count', [
'notifications' => \Yii::$app->params['notifications'],
'count' => $count,
]);
}
}

38
backend/widgets/views/notification-count.php

@ -0,0 +1,38 @@
<?php
/**
* Created by Error202
* Date: 13.08.2018
*/
use yii\helpers\Url;
/**
* @var $this \yii\web\View
* @var $notifications array
* @var $count integer
*/
?>
<li class="dropdown notifications-menu">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-bell-o"></i>
<span class="label label-warning"><?= $count ?></span>
</a>
<ul class="dropdown-menu">
<li class="header"><?= Yii::t('main', 'You have {count} notifications', ['count' => $count]) ?></li>
<li>
<ul class="menu">
<?php foreach ($notifications as $notification): ?>
<li>
<a href="<?= Url::to([$notification['url']]) ?>">
<i class="fa fa-<?= $notification['icon'] ?> text-<?= $notification['color'] ?>"></i> <?= Yii::t($notification['message_file'], $notification['message'], ['count' => $notification['count']]) ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</li>
<!-- <li class="footer"><a href="#">View all</a></li> -->
</ul>
</li>

17
common/modules/forms/FormsModule.php

@ -2,8 +2,11 @@
namespace common\modules\forms; namespace common\modules\forms;
use common\modules\forms\entities\Form;
use common\modules\forms\entities\FormMessage;
use core\components\modules\ModuleInterface; use core\components\modules\ModuleInterface;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/** /**
@ -28,6 +31,7 @@ class FormsModule extends \yii\base\Module implements ModuleInterface
public function bootstrap($app) public function bootstrap($app)
{ {
// prepare rules
$app->getUrlManager()->addRules([ $app->getUrlManager()->addRules([
'forms/manage/form/view/<id:\d+>' => 'forms/manage/form/view', 'forms/manage/form/view/<id:\d+>' => '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' ), '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,
];
}
} }
} }

1
common/modules/forms/controllers/manage/FormMessageController.php

@ -68,6 +68,7 @@ class FormMessageController extends Controller
*/ */
public function actionView($id) public function actionView($id)
{ {
$this->service->setRead($id);
return $this->render('view', [ return $this->render('view', [
'message' => $this->findModel($id), 'message' => $this->findModel($id),
]); ]);

10
common/modules/forms/entities/FormMessage.php

@ -2,6 +2,7 @@
namespace common\modules\forms\entities; namespace common\modules\forms\entities;
use common\modules\forms\entities\queries\FormMessageQuery;
use yii\behaviors\TimestampBehavior; use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord; use yii\db\ActiveRecord;
use Yii; use Yii;
@ -19,8 +20,8 @@ use Yii;
*/ */
class FormMessage extends ActiveRecord class FormMessage extends ActiveRecord
{ {
const STATUS_ACTIVE = 1; const STATUS_OLD = 0;
const STATUS_DRAFT = 0; const STATUS_NEW = 1;
public static function create( public static function create(
$form_id, $form_id,
@ -72,4 +73,9 @@ class FormMessage extends ActiveRecord
{ {
return $this->hasOne(Form::class, ['id' => 'form_id']); return $this->hasOne(Form::class, ['id' => 'form_id']);
} }
public static function find(): FormMessageQuery
{
return new FormMessageQuery(static::class);
}
} }

20
common/modules/forms/entities/queries/FormMessageQuery.php

@ -0,0 +1,20 @@
<?php
/**
* Created by Error202
* Date: 27.07.2018
*/
namespace common\modules\forms\entities\queries;
use common\modules\forms\entities\FormMessage;
use yii\db\ActiveQuery;
class FormMessageQuery extends ActiveQuery
{
public function unread()
{
return $this->andWhere([
'new' => FormMessage::STATUS_NEW,
]);
}
}

1
common/modules/forms/messages/ru/form.php

@ -25,4 +25,5 @@ return [
'For editor' => 'Для редактора', 'For editor' => 'Для редактора',
'Form Data' => 'Данные формы', 'Form Data' => 'Данные формы',
'Date' => 'Дата', 'Date' => 'Дата',
'New forms messages: {count}' => 'Новых сообщений из форм: {count}',
]; ];

7
common/modules/forms/services/FormMessageManageService.php

@ -34,6 +34,13 @@ class FormMessageManageService
$this->repository->save($message); $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 public function remove($id): void
{ {
$message = $this->repository->get($id); $message = $this->repository->get($id);

4
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'), 'filter' => ArrayHelper::map(Form::find()->all(), 'id', 'name'),
'attribute' => 'form_id', 'attribute' => 'form_id',
'value' => function (FormMessage $model) { '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', 'format' => 'raw',
], ],

Loading…
Cancel
Save