[ '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()) && $this->hasProducts() && $model->save()) { OrderProduct::saveItems(\Yii::$app->request->post('OrderProduct'), $model->id); return $this->redirect( [ 'view', 'id' => $model->id, ] ); } else { return $this->render( '@artbox/order/views/order/create', [ 'model' => $model, 'labels' => $this->getLabels(), 'payments' => $this->getPayments(), 'deliveries' => $this->getDeliveries(), ] ); } } /** * 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()) && $this->hasProducts() && $model->save()) { OrderProduct::saveItems(\Yii::$app->request->post('OrderProduct'), $id); return $this->redirect( [ 'view', 'id' => $model->id, ] ); } else { return $this->render( '@artbox/order/views/order/update', [ 'model' => $model, 'labels' => $this->getLabels(), 'payments' => $this->getPayments(), 'deliveries' => $this->getDeliveries(), ] ); } } /** * 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' ]); } /** * @param string $q * @param null $id * * @return array */ public function actionProductList(string $q, $id = null) { \Yii::$app->response->format = Response::FORMAT_JSON; $out = [ 'results' => [ 'id' => '', 'text' => '', ], ]; if (!is_null($q)) { $out[ 'results' ] = []; /** * @var Variant[] $variants */ $variants = Variant::find() ->joinWith('lang', false) ->joinWith('product.lang', false) ->where( [ 'ilike', 'product_lang.title', $q, ] ) ->orWhere( [ 'ilike', 'variant_lang.title', $q, ] ) ->orWhere([ 'variant.sku' => $q ]) ->limit(20) ->all(); foreach ($variants as $variant) { $out[ 'results' ][] = [ 'id' => $variant->id, 'text' => $variant->product->lang->title, ]; } } elseif ($id > 0) { /** * @var Variant $variant */ $variant = Variant::find() ->with('lang', 'product.lang') ->where([ 'id' => $id ]) ->one(); $out[ 'results' ] = [ 'id' => $id, 'text' => $variant->product->lang->title, ]; } return $out; } /** * @return string */ public function actionAddToOrder() { /** * @var Variant $variant * @var ActiveForm $form * @var OrderProduct $orderProduct */ \Yii::$app->response->format = Response::FORMAT_JSON; $id = \Yii::$app->request->post('id'); $count = \Yii::$app->request->post('count') ? \Yii::$app->request->post('count') : 1; $variant = Variant::find() ->with('product.lang') ->where( [ 'id' => $id, ] ) ->one(); if ($variant) { $form = new ActiveForm(); $orderProduct = new OrderProduct( [ 'count' => $count, 'variant_id' => $variant->id, ] ); $row = $this->renderPartial( '@artbox/order/views/order/_order_product', [ 'orderProduct' => $orderProduct, 'index' => rand(0, 10000), 'variant' => $variant, 'price' => $variant->price, 'form' => $form, ] ); return [ 'success' => true, 'row' => $row, ]; } return [ 'success' => false, 'message' => \Yii::t('app', 'Product not found'), ]; } /** * 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.'); } } /** * @return bool */ protected function hasProducts() { if (empty(\Yii::$app->request->post('OrderProduct'))) { \Yii::$app->session->setFlash('error', \Yii::t('app', 'Заказ не может быть без товаров')); return false; } return true; } /** * @return array */ protected function getLabels() { return Label::find() ->joinWith('lang') ->select( [ 'title', 'id', ] ) ->where( [ 'status' => true, ] ) ->indexBy('id') ->column(); } /** * @return array */ protected function getPayments() { return Payment::find() ->joinWith('lang') ->select( [ 'title', 'id', ] ) ->where( [ 'status' => true, ] ) ->indexBy('id') ->column(); } /** * @return array */ protected function getDeliveries() { return Delivery::find() ->joinWith('lang') ->select( [ 'title', 'id', ] ) ->where( [ 'status' => true, ] ) ->indexBy('id') ->column(); } }