added history of changes
This commit is contained in:
		| @@ -5,6 +5,7 @@ namespace backend\modules\card\controllers; | |||||||
| use common\classes\Debug; | use common\classes\Debug; | ||||||
| use common\models\AdditionalFields; | use common\models\AdditionalFields; | ||||||
| use common\models\CardSkill; | use common\models\CardSkill; | ||||||
|  | use common\Models\ChangeHistory; | ||||||
| use common\models\User; | use common\models\User; | ||||||
| use common\models\FieldsValue; | use common\models\FieldsValue; | ||||||
| use common\models\FieldsValueNew; | use common\models\FieldsValueNew; | ||||||
| @@ -105,12 +106,19 @@ class UserCardController extends Controller | |||||||
|         $skills = CardSkill::find()->where(['card_id' => $id])->with('skill')->all(); |         $skills = CardSkill::find()->where(['card_id' => $id])->with('skill')->all(); | ||||||
|  |  | ||||||
|         $id_current_user = $this->findModel($id)->id_user; |         $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', [ |         return $this->render('view', [ | ||||||
|             'model' => $this->findModel($id), |             'model' => $this->findModel($id), | ||||||
|             'modelFildValue' => $dataProvider, |             'modelFieldValue' => $dataProvider, | ||||||
|             'skills' => $skills, |             'skills' => $skills, | ||||||
|             'userData' => User::findOne($id_current_user), |             'userData' => User::findOne($id_current_user), | ||||||
|  |             'changeDataProvider' => $changeDataProvider, | ||||||
|         ]); |         ]); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -2,6 +2,7 @@ | |||||||
|  |  | ||||||
| namespace backend\modules\card\models; | namespace backend\modules\card\models; | ||||||
|  |  | ||||||
|  | use Common\Behaviors\LogBehavior; | ||||||
| use Yii; | use Yii; | ||||||
| use backend\modules\settings\models\Skill; | use backend\modules\settings\models\Skill; | ||||||
| use common\classes\Debug; | 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) |     public function beforeSave($insert) | ||||||
|  |     { | ||||||
|  |         if(is_array(\Yii::$app->request->post('UserCard'))) | ||||||
|         { |         { | ||||||
|             $this->salary = str_replace(' ', '', \Yii::$app->request->post('UserCard')['salary']); |             $this->salary = str_replace(' ', '', \Yii::$app->request->post('UserCard')['salary']); | ||||||
|  |         } | ||||||
|         return parent::beforeSave($insert); // TODO: Change the autogenerated stub |         return parent::beforeSave($insert); // TODO: Change the autogenerated stub | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public function afterSave($insert, $changedAttributes) |     public function afterSave($insert, $changedAttributes) | ||||||
|     { |     { | ||||||
|         $post = \Yii::$app->request->post('UserCard'); |         $post = \Yii::$app->request->post('UserCard'); | ||||||
|  |         if($post) { | ||||||
|         if ($post['fields']) { |             if (isset($post['fields'])) { | ||||||
|                 FieldsValueNew::deleteAll(['item_id' => $this->id, 'item_type' => FieldsValueNew::TYPE_PROFILE]); |                 FieldsValueNew::deleteAll(['item_id' => $this->id, 'item_type' => FieldsValueNew::TYPE_PROFILE]); | ||||||
|                 foreach ($post['fields'] as $item) { |                 foreach ($post['fields'] as $item) { | ||||||
|                     $item['value'] = urldecode($item['value']); |                     $item['value'] = urldecode($item['value']); | ||||||
| @@ -85,19 +98,18 @@ class UserCard extends \common\models\UserCard | |||||||
|                     $fieldsValue->order = $item['order']; |                     $fieldsValue->order = $item['order']; | ||||||
|                     $fieldsValue->item_id = $this->id; |                     $fieldsValue->item_id = $this->id; | ||||||
|                     $fieldsValue->item_type = FieldsValueNew::TYPE_PROFILE; |                     $fieldsValue->item_type = FieldsValueNew::TYPE_PROFILE; | ||||||
|                 if(is_file(Yii::getAlias('@frontend') . '/web/' . $item['value'])){ |                     if (is_file(Yii::getAlias('@frontend') . '/web/' . $item['value'])) { | ||||||
|                         $fieldsValue->type_file = 'file'; |                         $fieldsValue->type_file = 'file'; | ||||||
|                 }else{ |                     } else { | ||||||
|                         $fieldsValue->type_file = 'text'; |                         $fieldsValue->type_file = 'text'; | ||||||
|                     } |                     } | ||||||
|  |  | ||||||
|                     $fieldsValue->save(); |                     $fieldsValue->save(); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|  |             if (is_array($post['fields'])) { | ||||||
|         if ($post['skill']) { |  | ||||||
|                 CardSkill::deleteAll(['card_id' => $this->id]); |                 CardSkill::deleteAll(['card_id' => $this->id]); | ||||||
|  |                 if (is_array($post['skill'])) | ||||||
|                     foreach ($post['skill'] as $item) { |                     foreach ($post['skill'] as $item) { | ||||||
|                         $skill = new CardSkill(); |                         $skill = new CardSkill(); | ||||||
|                         $skill->skill_id = $item; |                         $skill->skill_id = $item; | ||||||
| @@ -106,10 +118,9 @@ class UserCard extends \common\models\UserCard | |||||||
|                         $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) |     public static function getParameter($params, $key) | ||||||
|     { |     { | ||||||
|   | |||||||
| @@ -9,7 +9,8 @@ use yii\widgets\DetailView; | |||||||
| /* @var $userData common\models\User */ | /* @var $userData common\models\User */ | ||||||
| /* @var $skills \common\models\CardSkill */ | /* @var $skills \common\models\CardSkill */ | ||||||
| /* @var $skill \common\models\Skill */ | /* @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->title = $model->fio; | ||||||
| $this->params['breadcrumbs'][] = ['label' => 'Профили', 'url' => ['index']]; | $this->params['breadcrumbs'][] = ['label' => 'Профили', 'url' => ['index']]; | ||||||
| @@ -75,7 +76,7 @@ $this->params['breadcrumbs'][] = $this->title; | |||||||
|     <h2>Дополнительные сведения</h2> |     <h2>Дополнительные сведения</h2> | ||||||
|  |  | ||||||
|     <?= GridView::widget([ |     <?= GridView::widget([ | ||||||
|         'dataProvider' => $modelFildValue, |         'dataProvider' => $modelFieldValue, | ||||||
|         'layout' => "{items}", |         'layout' => "{items}", | ||||||
|         'columns' => [ |         'columns' => [ | ||||||
|             'field.name:text:Поле', |             '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> | </div> | ||||||
| @@ -12,7 +12,7 @@ | |||||||
| .itemImg, .itemImgs{ | .itemImg, .itemImgs{ | ||||||
|     float: left !important; |     float: left !important; | ||||||
|     display: inline-block; |     display: inline-block; | ||||||
|     width: 30%!important; |     /*width: 30%!important;*/ | ||||||
| } | } | ||||||
|  |  | ||||||
| .media__upload_img img{ | .media__upload_img img{ | ||||||
| @@ -28,12 +28,21 @@ | |||||||
|     flex-direction: column; |     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; |     display: flex; | ||||||
|     width: auto; |     width: auto; | ||||||
| } | } | ||||||
|  |  | ||||||
| .field-usercard-fields-0-value .itemImg { | input .itemImg{ | ||||||
|     width: 100% !important; |     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}}'); | ||||||
|  |     } | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user
	 vladrigos
					vladrigos