added history of changes
This commit is contained in:
parent
38069c57d4
commit
812bb2b23a
@ -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,
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -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,17 +65,29 @@ class UserCard extends \common\models\UserCard
|
||||
}
|
||||
}
|
||||
|
||||
public function behaviors()
|
||||
{
|
||||
return [
|
||||
'log' => [
|
||||
'class' => \common\behaviors\LogBehavior::class,
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public function beforeSave($insert)
|
||||
{
|
||||
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['fields']) {
|
||||
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']);
|
||||
@ -94,10 +107,9 @@ class UserCard extends \common\models\UserCard
|
||||
$fieldsValue->save();
|
||||
}
|
||||
}
|
||||
|
||||
if ($post['skill']) {
|
||||
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;
|
||||
@ -106,10 +118,9 @@ class UserCard extends \common\models\UserCard
|
||||
$skill->save();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
|
||||
}
|
||||
}
|
||||
|
||||
public static function getParameter($params, $key)
|
||||
{
|
||||
|
@ -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>
|
@ -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;
|
||||
}
|
||||
|
||||
|
41
common/behaviors/LogBehavior.php
Normal file
41
common/behaviors/LogBehavior.php
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
28
common/models/ChangeHistory.php
Normal file
28
common/models/ChangeHistory.php
Normal 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()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
@ -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}}');
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user