Browse Source

Form messages group actions

master
Egorka 6 years ago
parent
commit
7859a27ed0
  1. 17
      backend/widgets/grid/CheckBoxColumn.php
  2. 90
      common/modules/forms/controllers/manage/FormMessageController.php
  3. 6
      common/modules/forms/messages/ru/form.php
  4. 9
      common/modules/forms/services/FormMessageManageService.php
  5. 106
      common/modules/forms/views/manage/form-message/index.php

17
backend/widgets/grid/CheckBoxColumn.php

@ -0,0 +1,17 @@
<?php
/**
* Created by Error202
* Date: 20.11.2017
*/
namespace backend\widgets\grid;
class CheckBoxColumn extends \yii\grid\CheckboxColumn
{
public $headerOptions = ['class' => 'text-center', 'style' => 'width: 3em'];
public $contentOptions = [
'class' => 'text-center grid_checkbox',
];
public $name = 'gridSelection';
}

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

@ -5,11 +5,13 @@ namespace common\modules\forms\controllers\manage;
use common\modules\forms\entities\FormMessage;
use common\modules\forms\forms\FormMessageSearch;
use common\modules\forms\services\FormMessageManageService;
use yii\helpers\Json;
use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\web\Controller;
use Yii;
use yii\web\Response;
class FormMessageController extends Controller
{
@ -89,6 +91,94 @@ class FormMessageController extends Controller
return $this->redirect(['index']);
}
public function actionDeleteSelected()
{
$post = Yii::$app->request->post();
$ids = isset($post['ids']) ? $post['ids'] : null;
Yii::$app->response->format = Response::FORMAT_JSON;
if (!$ids) {
return [
'result' => 'error',
];
}
$idsArray = Json::decode($ids, true);
foreach ($idsArray as $id)
{
try {
$this->service->remove($id);
} catch (\DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
return [
'result' => 'failed',
];
}
}
Yii::$app->session->setFlash('success', Yii::t('form', 'Selected messages deleted'));
return [
'result' => 'success',
];
}
public function actionReadSelected()
{
$post = Yii::$app->request->post();
$ids = isset($post['ids']) ? $post['ids'] : null;
Yii::$app->response->format = Response::FORMAT_JSON;
if (!$ids) {
return [
'result' => 'error',
];
}
$idsArray = Json::decode($ids, true);
foreach ($idsArray as $id)
{
try {
$this->service->setRead($id);
} catch (\DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
return [
'result' => 'failed',
];
}
}
return [
'result' => 'success',
];
}
public function actionUnreadSelected()
{
$post = Yii::$app->request->post();
$ids = isset($post['ids']) ? $post['ids'] : null;
Yii::$app->response->format = Response::FORMAT_JSON;
if (!$ids) {
return [
'result' => 'error',
];
}
$idsArray = Json::decode($ids, true);
foreach ($idsArray as $id)
{
try {
$this->service->setUnread($id);
} catch (\DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
return [
'result' => 'failed',
];
}
}
return [
'result' => 'success',
];
}
/**
* @param $id
*

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

@ -26,4 +26,10 @@ return [
'Form Data' => 'Данные формы',
'Date' => 'Дата',
'New forms messages: {count}' => 'Новых сообщений из форм: {count}',
'Mark as read' => 'Отметить как прочитанные',
'Mark as unread' => 'Снять пометку о прочтении',
'Delete selected' => 'Удалить выбранные',
'Group actions {caret}' => 'Групповые операции {caret}',
'Are you sure you want to delete selected messages?' => 'Вы действительно хотите удалить выбранные сообщения?',
'Selected messages deleted' => 'Выбранные сообщения удалены',
];

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

@ -37,7 +37,14 @@ class FormMessageManageService
public function setRead($id)
{
$message = $this->repository->get($id);
$message->new = 0;
$message->new = FormMessage::STATUS_OLD;
$this->repository->save($message);
}
public function setUnread($id)
{
$message = $this->repository->get($id);
$message->new = FormMessage::STATUS_NEW;
$this->repository->save($message);
}

106
common/modules/forms/views/manage/form-message/index.php

@ -3,9 +3,11 @@
use common\modules\forms\entities\FormMessage;
use yii\grid\ActionColumn;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\grid\GridView;
use yii\helpers\ArrayHelper;
use common\modules\forms\entities\Form;
use backend\widgets\grid\CheckBoxColumn;
/* @var $this yii\web\View */
/* @var $searchModel \common\modules\forms\forms\FormMessageSearch */
@ -13,9 +15,104 @@ use common\modules\forms\entities\Form;
$this->title = Yii::t('form', 'Messages');
$this->params['breadcrumbs'][] = $this->title;
$js = <<<JS
var body = $('body');
$('.select-on-check-all').addClass('grid_checkbox');
body.on('change', '.grid_checkbox', function(e) {
var button = $('#groupActions');
if (!multiSelected()) {
button.attr('disabled', 'disabled');
button.addClass('disabled');
}
else {
button.removeAttr('disabled');
button.removeClass('disabled');
}
});
JS;
$this->registerJs($js, $this::POS_READY);
$deleteConfirmMessage = Yii::t('form', 'Are you sure you want to delete selected messages?');
$deleteSelectedUrl = Url::to(['delete-selected']);
$unreadSelectedUrl = Url::to(['unread-selected']);
$readSelectedUrl = Url::to(['read-selected']);
$js2 = <<<JS2
function getRows()
{
var ids = new Array();
$('input[name="gridSelection[]"]:checked').each(function() {
ids.push(this.value);
});
return ids;
}
function multiSelected() {
var result = false;
$('input[name="gridSelection[]"]:checked').each(function() {
result = true;
});
return result;
}
function deleteSelected() {
if (confirm('{$deleteConfirmMessage}')) {
var ids = JSON.stringify(getRows());
$.post( "{$deleteSelectedUrl}", { ids: ids})
.done(function( data ) {
document.location.reload();
});
}
}
function unreadSelected() {
var ids = JSON.stringify(getRows());
$.post( "{$unreadSelectedUrl}", { ids: ids})
.done(function( data ) {
document.location.reload();
});
}
function readSelected() {
var ids = JSON.stringify(getRows());
$.post( "{$readSelectedUrl}", { ids: ids})
.done(function( data ) {
document.location.reload();
});
}
JS2;
$this->registerJs($js2, $this::POS_HEAD);
?>
<div class="messages-index">
<div style="margin-bottom: 10px;">
<div class="btn-group">
<?= Html::a(Yii::t('form', 'Group actions {caret}', ['caret' => '<span class="caret">']), '#', [
'class' => 'btn btn-default dropdown-toggle disabled',
'id' => 'groupActions',
'disabled' => 'disabled',
'data-toggle' => 'dropdown',
'aria-haspopup' => 'true',
'aria-expanded' => 'false',
]) ?>
<ul class="dropdown-menu">
<li><?= Html::a(Yii::t('form', 'Mark as read'), '#', [
'onclick' => new \yii\web\JsExpression('readSelected()'),
]) ?></li>
<li><?= Html::a(Yii::t('form', 'Mark as unread'), '#', [
'onclick' => new \yii\web\JsExpression('unreadSelected()'),
]) ?></li>
<li role="separator" class="divider"></li>
<li><?= Html::a(Yii::t('form', 'Delete selected'), '#', [
'onclick' => new \yii\web\JsExpression('deleteSelected()'),
]) ?></li>
</ul>
</div>
</div>
<div class="box">
<div class="box-body">
<?= GridView::widget([
@ -23,6 +120,14 @@ $this->params['breadcrumbs'][] = $this->title;
'filterModel' => $searchModel,
'columns' => [
[
'class' => CheckBoxColumn::class,
],
[
'attribute' => 'id',
'options' => ['style' => 'width: 50px;'],
'contentOptions' => ['class' => 'text-center'],
],
[
'label' => Yii::t('form', 'Date'),
'attribute' => 'created_at',
'value' => function(FormMessage $model) {
@ -43,6 +148,7 @@ $this->params['breadcrumbs'][] = $this->title;
],
[
'class' => ActionColumn::class,
'template' => '{view} {delete}',
'options' => ['style' => 'width: 100px;'],
'contentOptions' => ['class' => 'text-center'],
],

Loading…
Cancel
Save