namespace app\controllers;
use Yii;
+use yii\base\DynamicModel;
+use yii\data\ActiveDataProvider;
+use yii\db\Exception;
+use yii\helpers\ArrayHelper;
+use yii\helpers\Json;
use yii\web\Controller;
use yii_app\records\UsersMessageManagement;
+use yii_app\records\UsersMessageManagementLogs;
class UsersMessageManagementController extends Controller
{
+ public static function saveLogs($old, $new) {
+ foreach (array_keys($old) as $oldKey) {
+ if ($new[$oldKey] != $old[$oldKey]) {
+ $log = new UsersMessageManagementLogs;
+ $log->field_name = $oldKey;
+ $log->value_old = '' . $old[$oldKey];
+ $log->value_new = '' . $new[$oldKey];
+ $log->created_at = date('Y-m-d H:i:s');
+ $log->created_by = Yii::$app->user->id;
+ $log->save();
+ if ($log->getErrors()) {
+ throw new Exception(Json::encode($log->getErrors()));
+ }
+ }
+ }
+ }
+
public function actionIndex() {
$model = UsersMessageManagement::find()->one();
$model->day_before_step2_active = 1;
$model->day_before_step3_active = 1;
$model->date_start = date('Y-m-d H:i:s');
+ $model->date_last_scenario = $model->date_start;
}
+ $oldAttributes = $model->getAttributes();
+
if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
if (!$model->created_at) {
$model->created_at = date('Y-m-d H:i:s');
$model->updated_by = Yii::$app->user->id;
if ($model->validate()) {
$model->save();
+ $newAttributes = $model->getAttributes();
+ self::saveLogs($oldAttributes, $newAttributes);
} else {
if (!empty(array_intersect_key($model->getErrors(), ['offer_1' => 1, 'offer_2' => 2, 'offer_whatsapp' => 3, 'offer_text' => 4]))) {
Yii::$app->session->setFlash('error', "Перейдите на вкладку Сообщения рассылки, чтобы заполнить пустые поля");
return $this->render('index', compact('model'));
}
+
+ public function actionLogs() {
+
+ $model = DynamicModel::validateData([
+ 'date_from' => date('Y-m-d H:i:s', strtotime('-1 week', time())),
+ 'date_to' => date('Y-m-d H:i:s'),
+ 'field_name' => null,
+ 'value' => null,
+ 'created_by' => null,
+ ], [
+ [['date_from', 'date_to', 'field_name', 'value', 'created_by'], 'safe']
+ ]);
+
+ $model->load(Yii::$app->request->get());
+
+ $query = UsersMessageManagementLogs::find()
+ ->where(['BETWEEN', 'created_at', $model->date_from, $model->date_to]);
+ if ($model->field_name) {
+ $query->andWhere(['field_name' => $model->field_name]);
+ }
+ if (!empty($model->value)) {
+ $query->andWhere([
+ 'or',
+ ['like', 'value_old', '%' . $model->value . '%', false],
+ ['like', 'value_new', '%' . $model->value . '%', false],
+ ]);
+ }
+ if (!empty($model->created_by)) {
+ $query->andWhere(['created_by' => $model->created_by]);
+ }
+
+ $dataProvider = new ActiveDataProvider(['query' => $query]);
+
+ $log = new UsersMessageManagementLogs;
+ $keys = array_keys($log->getAttributes());
+ $fieldNames = array_merge([null => '__Любые__'], array_combine($keys, $keys));
+
+ $adminArr = [];
+ $adminArrOther = [];
+ foreach (\yii_app\records\Admin::find()->with('adminGroup')->all() as $admin) {
+ if ($admin->group_id > 0) {
+ $tmp = ['id' => $admin->id, 'name' => $admin->name, 'groupName' => $admin->adminGroup->name ?? "Другие"];
+ if ($tmp['groupName'] == 'Другие') {
+ $adminArrOther[] = $tmp;
+ } else {
+ $adminArr[] = $tmp;
+ }
+ }
+ }
+ $adminArr = ArrayHelper::merge([['id' => null, 'name' => '__Любой__', 'groupName' => '__Любой__']], $adminArr, $adminArrOther);
+ $admins = ArrayHelper::map($adminArr, 'id', 'name', 'groupName');
+
+ return $this->render('logs', compact('model', 'fieldNames', 'dataProvider', 'admins'));
+ }
}
--- /dev/null
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Class m241212_082256_create_table_users_message_management_logs
+ */
+class m241212_082256_create_table_users_message_management_logs extends Migration
+{
+ const TABLE_NAME = 'erp24.users_message_management_logs';
+ /**
+ * {@inheritdoc}
+ */
+ public function safeUp()
+ {
+ $this->createTable(self::TABLE_NAME, [
+ 'id' => $this->primaryKey(),
+ 'field_name' => $this->string()->notNull()->comment('Название заменяемого поля'),
+ 'value_old' => $this->text()->null()->comment('Заменяемое значение'),
+ 'value_new' => $this->text()->null()->comment('Новое значение'),
+ 'created_at' => $this->dateTime()->notNull()->comment('Время создания записи'),
+ 'created_by' => $this->integer()->notNull()->comment('Создатель записи'),
+ ]);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function safeDown()
+ {
+ $this->dropTable(self::TABLE_NAME);
+ }
+}
--- /dev/null
+<?php
+
+namespace yii_app\records;
+
+use Yii;
+
+/**
+ * This is the model class for table "users_message_management_logs".
+ *
+ * @property int $id
+ * @property string $field_name Название заменяемого поля
+ * @property string|null $value_old Заменяемое значение
+ * @property string|null $value_new Новое значение
+ * @property string $created_at Время создания записи
+ * @property int $created_by Создатель записи
+ */
+class UsersMessageManagementLogs extends \yii\db\ActiveRecord
+{
+ /**
+ * {@inheritdoc}
+ */
+ public static function tableName()
+ {
+ return 'users_message_management_logs';
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function rules()
+ {
+ return [
+ [['field_name', 'created_at', 'created_by'], 'required'],
+ [['value_old', 'value_new'], 'string'],
+ [['created_at'], 'safe'],
+ [['created_by'], 'default', 'value' => null],
+ [['created_by'], 'integer'],
+ [['field_name'], 'string', 'max' => 255],
+ ];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function attributeLabels()
+ {
+ return [
+ 'id' => 'ID',
+ 'field_name' => 'Field Name',
+ 'value_old' => 'Value Old',
+ 'value_new' => 'Value New',
+ 'created_at' => 'Created At',
+ 'created_by' => 'Created By',
+ ];
+ }
+
+ function getAdmin() {
+ return $this->hasOne(Admin::class, ['id' => 'created_by']);
+ }
+}
--- /dev/null
+<?php
+
+use yii\helpers\Html;
+use yii\widgets\ActiveForm;
+use yii\base\DynamicModel;
+use yii\data\ActiveDataProvider;
+use yii\grid\GridView;
+
+use kartik\select2\Select2;
+use dosamigos\datetimepicker\DateTimePicker;
+
+/* @var $model DynamicModel */
+/* @var $fieldNames array */
+/* @var $dataProvider ActiveDataProvider */
+/* @var $admins array */
+
+?>
+
+<div class="usersMessageManagementLogs m-5">
+
+ <?php $form = ActiveForm::begin([
+ 'method' => 'GET',
+ 'action' => '/users-message-management/logs'
+ ]) ?>
+
+ <div class="row">
+ <div class="col-12">
+ Время от:
+ <div class="d-inline-block">
+ <?= $form->field($model, 'date_from')->widget(DateTimePicker::class, [
+ 'language' => 'ru',
+ 'template' => '{input}',
+ 'clientOptions' => [
+ 'autoclose' => true,
+ 'format' => 'Y-m-d H:i:s',
+ 'todayBtn' => true
+ ],
+ ])->label(false) ?>
+ </div>
+ до:
+ <div class="d-inline-block">
+ <?= $form->field($model, 'date_to')->widget(DateTimePicker::class, [
+ 'language' => 'ru',
+ 'template' => '{input}',
+ 'clientOptions' => [
+ 'autoclose' => true,
+ 'format' => 'Y-m-d H:i:s',
+ 'todayBtn' => true
+ ],
+ ])->label(false) ?>
+ </div>
+ Название поля:
+ <div class="d-inline-block">
+ <?= $form->field($model, 'field_name')->dropDownList($fieldNames)->label(false) ?>
+ </div>
+ Значение поля:
+ <div class="d-inline-block">
+ <?= $form->field($model, 'value')->textInput()->label(false) ?>
+ </div>
+ Автор:
+ <div class="d-inline-block">
+ <?= $form->field($model, 'created_by')->widget(Select2::class, [
+ 'data' => $admins,
+ 'language' => 'ru',
+ 'options' => ['placeholder' => 'Сотрудник...'],
+ 'pluginOptions' => [
+ 'allowClear' => true
+ ],
+ ])->label(false) ?>
+ </div>
+
+ <div class="d-inline-block">
+ <?= Html::submitButton('Применить фильтр', ['class' => 'btn btn-secondary btn-sm']) ?>
+ </div>
+ </div>
+ </div>
+
+ <?php ActiveForm::end() ?>
+
+ <div class="row">
+ <div class="col-12">
+ <?= GridView::widget([
+ 'dataProvider' => $dataProvider,
+ 'columns' => [
+ 'id',
+ 'field_name',
+ 'value_old',
+ 'value_new',
+ 'created_at',
+ [
+ 'attribute' => 'created_by',
+ 'label' => 'Создано',
+ 'value' => function ($model) {
+ return ($model->admin->name ?? '') . ' #' . $model->created_by;
+ }
+ ],
+ ]
+ ]) ?>
+ </div>
+ </div>
+
+</div>