added history of changes

This commit is contained in:
vladrigos 2020-08-04 10:47:53 +03:00
parent 38069c57d4
commit 812bb2b23a
7 changed files with 186 additions and 36 deletions

View File

@ -5,6 +5,7 @@ namespace backend\modules\card\controllers;
use common\classes\Debug;
use common\models\AdditionalFields;
use common\models\CardSkill;
use common\Models\ChangeHistory;
use common\models\User;
use common\models\FieldsValue;
use common\models\FieldsValueNew;
@ -105,12 +106,19 @@ class UserCardController extends Controller
$skills = CardSkill::find()->where(['card_id' => $id])->with('skill')->all();
$id_current_user = $this->findModel($id)->id_user;
$changeDataProvider = new ActiveDataProvider([
'query' => \common\models\ChangeHistory::find()->where(['type_id' => $this->findModel($id)->id]),
'pagination' => [
'pageSize' => 200,
]
]);
return $this->render('view', [
'model' => $this->findModel($id),
'modelFildValue' => $dataProvider,
'modelFieldValue' => $dataProvider,
'skills' => $skills,
'userData' => User::findOne($id_current_user),
'changeDataProvider' => $changeDataProvider,
]);
}

View File

@ -2,6 +2,7 @@
namespace backend\modules\card\models;
use Common\Behaviors\LogBehavior;
use Yii;
use backend\modules\settings\models\Skill;
use common\classes\Debug;
@ -64,51 +65,61 @@ class UserCard extends \common\models\UserCard
}
}
public function behaviors()
{
return [
'log' => [
'class' => \common\behaviors\LogBehavior::class,
]
];
}
public function beforeSave($insert)
{
$this->salary = str_replace(' ', '', \Yii::$app->request->post('UserCard')['salary']);
if(is_array(\Yii::$app->request->post('UserCard')))
{
$this->salary = str_replace(' ', '', \Yii::$app->request->post('UserCard')['salary']);
}
return parent::beforeSave($insert); // TODO: Change the autogenerated stub
}
public function afterSave($insert, $changedAttributes)
{
$post = \Yii::$app->request->post('UserCard');
if($post) {
if (isset($post['fields'])) {
FieldsValueNew::deleteAll(['item_id' => $this->id, 'item_type' => FieldsValueNew::TYPE_PROFILE]);
foreach ($post['fields'] as $item) {
$item['value'] = urldecode($item['value']);
if ($post['fields']) {
FieldsValueNew::deleteAll(['item_id' => $this->id, 'item_type' => FieldsValueNew::TYPE_PROFILE]);
foreach ($post['fields'] as $item) {
$item['value'] = urldecode($item['value']);
$fieldsValue = new FieldsValueNew();
$fieldsValue->field_id = $item['field_id'];
$fieldsValue->value = $item['value'];
$fieldsValue->order = $item['order'];
$fieldsValue->item_id = $this->id;
$fieldsValue->item_type = FieldsValueNew::TYPE_PROFILE;
if (is_file(Yii::getAlias('@frontend') . '/web/' . $item['value'])) {
$fieldsValue->type_file = 'file';
} else {
$fieldsValue->type_file = 'text';
}
$fieldsValue = new FieldsValueNew();
$fieldsValue->field_id = $item['field_id'];
$fieldsValue->value = $item['value'];
$fieldsValue->order = $item['order'];
$fieldsValue->item_id = $this->id;
$fieldsValue->item_type = FieldsValueNew::TYPE_PROFILE;
if(is_file(Yii::getAlias('@frontend') . '/web/' . $item['value'])){
$fieldsValue->type_file = 'file';
}else{
$fieldsValue->type_file = 'text';
$fieldsValue->save();
}
$fieldsValue->save();
}
}
if (is_array($post['fields'])) {
CardSkill::deleteAll(['card_id' => $this->id]);
if (is_array($post['skill']))
foreach ($post['skill'] as $item) {
$skill = new CardSkill();
$skill->skill_id = $item;
$skill->card_id = $this->id;
if ($post['skill']) {
CardSkill::deleteAll(['card_id' => $this->id]);
foreach ($post['skill'] as $item) {
$skill = new CardSkill();
$skill->skill_id = $item;
$skill->card_id = $this->id;
$skill->save();
$skill->save();
}
}
parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
}
parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
}
public static function getParameter($params, $key)

View File

@ -9,7 +9,8 @@ use yii\widgets\DetailView;
/* @var $userData common\models\User */
/* @var $skills \common\models\CardSkill */
/* @var $skill \common\models\Skill */
/* @var $modelFildValue yii\data\ActiveDataProvider */
/* @var $modelFieldValue yii\data\ActiveDataProvider */
/* @var $changeDataProvider yii\data\ActiveDataProvider */
$this->title = $model->fio;
$this->params['breadcrumbs'][] = ['label' => 'Профили', 'url' => ['index']];
@ -75,7 +76,7 @@ $this->params['breadcrumbs'][] = $this->title;
<h2>Дополнительные сведения</h2>
<?= GridView::widget([
'dataProvider' => $modelFildValue,
'dataProvider' => $modelFieldValue,
'layout' => "{items}",
'columns' => [
'field.name:text:Поле',
@ -89,4 +90,16 @@ $this->params['breadcrumbs'][] = $this->title;
],
]); ?>
<h2>История изменений</h2>
<?= GridView::widget([
'dataProvider' => $changeDataProvider,
'columns' => [
'label',
'old_value',
'new_value',
'created_at',
],
]); ?>
</div>

View File

@ -12,7 +12,7 @@
.itemImg, .itemImgs{
float: left !important;
display: inline-block;
width: 30%!important;
/*width: 30%!important;*/
}
.media__upload_img img{
@ -28,12 +28,21 @@
flex-direction: column;
}
.field-usercard-fields-0-value{
/*.field-usercard-fields-0-value{*/
/* display: flex;*/
/* width: auto;*/
/*}*/
tr td div input{
width: 100% !important;
}
.list-cell__value div{
display: flex;
width: auto;
}
.field-usercard-fields-0-value .itemImg {
input .itemImg{
width: 100% !important;
}

View File

@ -0,0 +1,41 @@
<?php
namespace Common\Behaviors;
use yii\base\Event;
use yii\db\ActiveRecord;
use yii\base\Behavior;
class LogBehavior extends Behavior
{
public function events()
{
return[
ActiveRecord::EVENT_BEFORE_UPDATE => 'beforeUpdate',
];
}
public function beforeUpdate(Event $event)
{
$model = $event->sender;
$dirtyAttributes = $model->getDirtyAttributes();
foreach ($dirtyAttributes as $key => $value)
{
if($model->getOldAttribute($key) == $value)
{
continue;
}
$change = new \common\models\ChangeHistory([
'type' => get_class($model),
'type_id' => $model->getAttribute('id'),
'field_name' => $key,
'label' => $model->getAttributeLabel($key),
'old_value' => $model->getOldAttribute($key),
'new_value' => $value,
'created_at' => date('Y-m-d-H:i:s', time()),
]);
$change->save();
}
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace common\Models;
class ChangeHistory extends \yii\db\ActiveRecord
{
public static function tableName()
{
return 'change_history';
}
public function attributeLabels()
{
return [
'table' => 'Таблица',
'row_id' => 'ID строки',
'field_name' => 'Имя поля',
'old_value' => 'Старое значение',
'new_value' => 'Новое значение',
'label' => 'Поле',
];
}
public function translate()
{
}
}

View File

@ -0,0 +1,40 @@
<?php
use yii\db\Migration;
/**
* Handles the creation of table `{{%change_history}}`.
*/
class m200729_104128_create_change_history_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$tableOptions = null;
if ($this->db->driverName === 'mysql') {
// http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
}
$this->createTable('{{%change_history}}', [
'id' => $this->primaryKey(),
'type' => $this->string(255),
'type_id' => $this->integer(),
'field_name' => $this->string(255),
'label' => $this->string(255),
'old_value' => $this->string(255),
'new_value' => $this->string(255),
'created_at' => $this->dateTime(),
], $tableOptions);
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropTable('{{%change_history}}');
}
}