OrderController.php 9.66 KB
<?php
    
    namespace artbox\order\controllers;
    
    use artbox\order\models\Delivery;
    use artbox\order\models\Label;
    use artbox\order\models\Payment;
    use Yii;
    use artbox\order\models\Order;
    use artbox\order\models\OrderSearch;
    use yii\base\Model;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;
    use yii\filters\VerbFilter;
    
    /**
     * OrderController implements the CRUD actions for Order model.
     */
    class OrderController extends Controller
    {
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [
                'verbs' => [
                    'class'   => VerbFilter::className(),
                    'actions' => [
                        'delete' => [ 'POST' ],
                    ],
                ],
            ];
        }
        
        /**
         * Lists all Order models.
         *
         * @return mixed
         */
        public function actionIndex()
        {
            $searchModel = new OrderSearch();
            $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
            $labels = Label::find()
                           ->select(
                               [
                                   'title',
                                   'id',
                               ]
                           )
                           ->joinWith('lang')
                           ->andWhere(
                               [
                                   'status' => true,
                               ]
                           )
                           ->indexBy('id')
                           ->column();
            
            return $this->render(
                '@artbox/order/views/order/index',
                [
                    'searchModel'  => $searchModel,
                    'dataProvider' => $dataProvider,
                    'labels'       => $labels,
                ]
            );
        }
        
        /**
         * Displays a single Order model.
         *
         * @param integer $id
         *
         * @return mixed
         */
        public function actionView($id)
        {
            return $this->render(
                '@artbox/order/views/order/view',
                [
                    'model' => $this->findModel($id),
                ]
            );
        }
        
        /**
         * Creates a new Order model.
         * If creation is successful, the browser will be redirected to the 'view' page.
         *
         * @return mixed
         */
        public function actionCreate()
        {
            $model = new Order();
            
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
                return $this->redirect(
                    [
                        'view',
                        'id' => $model->id,
                    ]
                );
            } else {
                $labels = Label::find()
                               ->joinWith('lang')
                               ->select(
                                   [
                                       'title',
                                       'id',
                                   ]
                               )
                               ->where(
                                   [
                                       'status' => true,
                                   ]
                               )
                               ->indexBy('id')
                               ->column();
                $deliveries = Delivery::find()
                                      ->joinWith('lang')
                                      ->select(
                                          [
                                              'title',
                                              'id',
                                          ]
                                      )
                                      ->where(
                                          [
                                              'status' => true,
                                          ]
                                      )
                                      ->indexBy('id')
                                      ->column();
                $payments = Payment::find()
                                   ->joinWith('lang')
                                   ->select(
                                       [
                                           'title',
                                           'id',
                                       ]
                                   )
                                   ->where(
                                       [
                                           'status' => true,
                                       ]
                                   )
                                   ->indexBy('id')
                                   ->column();
                return $this->render(
                    '@artbox/order/views/order/create',
                    [
                        'model'      => $model,
                        'labels'     => $labels,
                        'payments'   => $payments,
                        'deliveries' => $deliveries,
                    ]
                );
            }
        }
        
        /**
         * Updates an existing Order model.
         * If update is successful, the browser will be redirected to the 'view' page.
         *
         * @param integer $id
         *
         * @return mixed
         */
        public function actionUpdate($id)
        {
            $model = $this->findModel($id);
            
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
                if (Model::loadMultiple($model->orderProducts, \Yii::$app->request->post()) && Model::validateMultiple(
                        $model->orderProducts
                    )
                ) {
                    foreach ($model->orderProducts as $orderProduct) {
                        $orderProduct->save(false);
                    }
                }
                return $this->redirect(
                    [
                        'view',
                        'id' => $model->id,
                    ]
                );
            } else {
                $labels = Label::find()
                               ->joinWith('lang')
                               ->select(
                                   [
                                       'title',
                                       'id',
                                   ]
                               )
                               ->where(
                                   [
                                       'status' => true,
                                   ]
                               )
                               ->indexBy('id')
                               ->column();
                $deliveries = Delivery::find()
                                      ->joinWith('lang')
                                      ->select(
                                          [
                                              'title',
                                              'id',
                                          ]
                                      )
                                      ->where(
                                          [
                                              'status' => true,
                                          ]
                                      )
                                      ->indexBy('id')
                                      ->column();
                $payments = Payment::find()
                                   ->joinWith('lang')
                                   ->select(
                                       [
                                           'title',
                                           'id',
                                       ]
                                   )
                                   ->where(
                                       [
                                           'status' => true,
                                       ]
                                   )
                                   ->indexBy('id')
                                   ->column();
                return $this->render(
                    '@artbox/order/views/order/update',
                    [
                        'model'      => $model,
                        'labels'     => $labels,
                        'payments'   => $payments,
                        'deliveries' => $deliveries,
                    ]
                );
            }
        }
        
        /**
         * Deletes an existing Order model.
         * If deletion is successful, the browser will be redirected to the 'index' page.
         *
         * @param integer $id
         *
         * @return mixed
         */
        public function actionDelete($id)
        {
            $this->findModel($id)
                 ->delete();
            
            return $this->redirect([ 'index' ]);
        }
        
        /**
         * Finds the Order model based on its primary key value.
         * If the model is not found, a 404 HTTP exception will be thrown.
         *
         * @param integer $id
         *
         * @return Order the loaded model
         * @throws NotFoundHttpException if the model cannot be found
         */
        protected function findModel($id)
        {
            if (( $model = Order::findOne($id) ) !== null) {
                return $model;
            } else {
                throw new NotFoundHttpException('The requested page does not exist.');
            }
        }
    }