diff --git a/controllers/OrderController.php b/controllers/OrderController.php index 4e9ef0f..94ed2b6 100755 --- a/controllers/OrderController.php +++ b/controllers/OrderController.php @@ -4,6 +4,7 @@ use artweb\artbox\components\SmsSender; use artweb\artbox\ecommerce\models\OrderLabelHistory; + use artweb\artbox\ecommerce\models\OrderLog; use artweb\artbox\ecommerce\models\OrderSearch; use common\models\User; use Yii; @@ -106,10 +107,13 @@ public function actionView($id) { $model = $this->findModel($id); - - $historyData = new ActiveDataProvider([ - 'query' => $model->getLabelsHistory()->with('order', 'label', 'user'), - ]); + + $historyData = new ActiveDataProvider( + [ + 'query' => $model->getLabelsHistory() + ->with('order', 'label', 'user'), + ] + ); $dataProvider = new ActiveDataProvider( [ @@ -119,8 +123,8 @@ return $this->render( 'view', [ - 'model' => $model, - 'products' => $dataProvider, + 'model' => $model, + 'products' => $dataProvider, 'historyData' => $historyData, ] ); @@ -133,6 +137,25 @@ $model->save(); } + public function actionLog($id) + { + $model = Order::findOne($id); + + $logData = new ActiveDataProvider( + [ + 'query' => $model->getLogs(), + ] + ); + + return $this->render( + 'log', + [ + 'model' => $model, + 'logData' => $logData, + ] + ); + } + public function actionDelete($id) { if (\Yii::$app->user->identity->isAdmin()) { diff --git a/models/Order.php b/models/Order.php index 2a7d810..ba83649 100755 --- a/models/Order.php +++ b/models/Order.php @@ -8,6 +8,8 @@ use yii\behaviors\TimestampBehavior; use yii\db\ActiveRecord; use yii\db\Query; + use yii\helpers\Json; + use yii\helpers\VarDumper; /** * Class Order @@ -58,6 +60,7 @@ * @property Label $orderLabel * @property Delivery $orderDelivery * @property OrderPayment $orderPayment + * @property OrderLog[] $logs */ class Order extends ActiveRecord { @@ -124,6 +127,18 @@ return $result; } + public function getLogAttributes(string $attr, array $values) + { + if ($attr == 'deadline') { + return [ + 'old' => '' + /** + * @todo Start here + */ + ]; + } + } + /** * @inheritdoc */ @@ -222,6 +237,25 @@ public function afterSave($insert, $changedAttributes) { + $data = []; + foreach ($changedAttributes as $key => $attribute) { + if ($this->oldAttributes[ $key ] != $attribute) { + $data[ $key ] = [ + 'old' => $attribute, + 'new' => $this->oldAttributes[ $key ], + ]; + } + } + + if (!empty($data) && empty($data[ 'edit_time' ])) { + $log = new OrderLog(); + $log->order_id = (integer) $this->id; + $log->user_id = (integer) \Yii::$app->user->identity->id; + $log->data = Json::encode($data); + + $log->save(); + } + if (!empty($changedAttributes[ 'label' ])) { if ($this->label != (string) $changedAttributes[ 'label' ]) { $history = new OrderLabelHistory(); @@ -382,6 +416,14 @@ } /** + * @return \yii\db\ActiveQuery + */ + public function getLogs() + { + return $this->hasMany(OrderLog::className(), [ 'order_id' => 'id' ]); + } + + /** * If deadline is fucked up returns true, * if deadline is not setted return false, like everything is ok * diff --git a/models/OrderLog.php b/models/OrderLog.php new file mode 100644 index 0000000..cc083f6 --- /dev/null +++ b/models/OrderLog.php @@ -0,0 +1,108 @@ + TimestampBehavior::className(), + 'updatedAtAttribute' => false, + ], + ]; + } + + /** + * @inheritdoc + */ + public function rules() + { + return [ + [ + [ + 'order_id', + 'created_at', + 'user_id', + ], + 'integer', + ], + [ + [ 'data' ], + 'string', + ], + [ + [ 'order_id' ], + 'exist', + 'skipOnError' => true, + 'targetClass' => Order::className(), + 'targetAttribute' => [ 'order_id' => 'id' ], + ], + // [ + // [ 'user_id' ], + // 'exist', + // 'skipOnError' => true, + // 'targetClass' => User::className(), + // 'targetAttribute' => [ 'user_id' => 'id' ], + // ], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id' => Yii::t('app', 'ID'), + 'order_id' => Yii::t('app', 'Order ID'), + 'created_at' => Yii::t('app', 'Created At'), + 'user_id' => Yii::t('app', 'User ID'), + 'data' => Yii::t('app', 'Data'), + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getOrder() + { + return $this->hasOne(Order::className(), [ 'id' => 'order_id' ]); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getUser() + { + return $this->hasOne(User::className(), [ 'id' => 'user_id' ]); + } + } diff --git a/views/order/_log_item.php b/views/order/_log_item.php new file mode 100644 index 0000000..8bd37ee --- /dev/null +++ b/views/order/_log_item.php @@ -0,0 +1,43 @@ + + + +
Поле | +с | +на | +
---|---|---|
=$order->attributeLabels()[$key]?> | +=$item['old']?> | +=$item['new']?> | +