AccountController.php 4.45 KB
<?php
    
    namespace frontend\controllers;
    
    use artbox\order\models\Customer;
    use artbox\order\models\Order;
    use artbox\order\models\PasswordForm;
    use yii\web\Controller;
    use yii\web\ForbiddenHttpException;
    use yii\web\NotFoundHttpException;
    
    /**
     * Class AccountController
     *
     * @package frontend\controllers
     */
    class AccountController extends Controller
    {
        public function actionIndex()
        {
            $user = Customer::findOne(\Yii::$app->user->identity->getId());
            
            $orders = $user->getOrders()
                           ->with(
                               [
                                   'label.lang',
                                   'orderProducts',
                               ]
                           )
                           ->orderBy(
                               [
                                   'id' => SORT_DESC,
                               ]
                           )
                           ->all();
            
            return $this->render(
                'index',
                [
                    'orders' => $orders,
                ]
            );
        }
    
        public function actionOrder($id)
        {
            /**
             * @var Order $order
             */
            $order = Order::find()
                          ->with('orderProducts.variant.product.lang')
                          ->where(
                              [
                                  'id' => $id,
                              ]
                          )
                          ->one();
    
            if (empty($order)) {
                throw new NotFoundHttpException(\Yii::t('app', 'Order not found'));
            } elseif ($order->user_id !== \Yii::$app->user->identity->getId()) {
                throw new ForbiddenHttpException();
            }
    
            return $this->render(
                'order',
                [
                    'order' => $order,
                ]
            );
        }
    
        public function actionAccount()
        {
            $user = \Yii::$app->user->identity;
            return $this->render(
                'account',
                [
                    'userModel'     => $user,
                    'passwordModel' => new PasswordForm(),
                ]
            );
        }
    
        public function actionChangePassword()
        {
            /**
             * @var Customer $modeluser
             */
            $model = new PasswordForm();
            $modeluser = \Yii::$app->user->identity;
    
            if ($model->load(\Yii::$app->request->post())) {
                if ($model->validate()) {
                    $modeluser->setPassword($model->newpass);
                    if ($modeluser->save()) {
                        return $this->redirect([ 'index' ]);
                    } else {
                        return $this->render(
                            'account',
                            [
                                'userModel'     => $modeluser,
                                'passwordModel' => $model,
                            ]
                        );
                    }
                } else {
                    return $this->render(
                        'account',
                        [
                            'userModel'     => $modeluser,
                            'passwordModel' => $model,
                        ]
                    );
                }
            }
            return $this->render(
                'account',
                [
                    'userModel'     => $modeluser,
                    'passwordModel' => $model,
                ]
            );
    
        }
    
        public function actionChangeData()
        {
            /**
             * @var Customer $model
             */
            $model = \Yii::$app->user->identity;
        
            if ($model->load(\Yii::$app->request->post())) {
                //                VarDumper::dump($model, 10, 1);die();
                $model->markAttributeDirty('birthday');
                if ($model->save()) {
                    return $this->redirect([ 'index' ]);
                }
            }
            return $this->render(
                'account',
                [
                    'userModel'     => $model,
                    'passwordModel' => new PasswordForm(),
                ]
            );
        }
    }