[ 'class' => AccessControl::className(), 'rules' => [ [ 'allow' => true, 'roles' => ['@'], 'matchCallback' => function ($rule, $action) { return User::hasAccessBackend() ; } ] ], ], ]; } /** * Liste les commandes récurrente du producteur. * * @return string */ public function actionIndex() { $dataProvider = new ActiveDataProvider([ 'query' => Subscription::find() ->with(['user', 'producer', 'pointSale', 'productSubscription']) ->joinWith(['user']) ->where(['subscription.id_producer' => Producer::getId()]) ->orderBy('subscription.id_point_sale ASC, CASE `subscription`.`username` WHEN "" THEN `user`.`name` ELSE `subscription`.`username` END ASC'), 'pagination' => [ 'pageSize' => 1000, ], ]); return $this->render('index', [ 'dataProvider' => $dataProvider ]); } /** * Crée une commande récurrente. * * @return string */ public function actionCreate() { // form $model = new SubscriptionForm; $model->id_producer = Producer::getId(); // produits $productsArray = Product::searchAll() ; if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->save()) { $this->redirect(['subscription/index']); } return $this->render('create', [ 'model' => $model, 'productsArray' => $productsArray ]); } /** * Modifie une commande récurrente. * * @param integer $id * @return string * @throws NotFoundHttpException */ public function actionUpdate($id) { // form $model = new SubscriptionForm; $subscription = Subscription::findOne($id); if ($subscription) { $model->id = $id; $model->id_producer = $subscription->id_producer; $model->id_user = $subscription->id_user; $model->username = $subscription->username; $model->id_point_sale = $subscription->id_point_sale; $model->date_begin = date('d/m/Y', strtotime($subscription->date_begin)); if (strlen($subscription->date_end)) { $model->date_end = date('d/m/Y', strtotime($subscription->date_end)); } $model->monday = $subscription->monday; $model->tuesday = $subscription->tuesday; $model->wednesday = $subscription->wednesday; $model->thursday = $subscription->thursday; $model->friday = $subscription->friday; $model->saterday = $subscription->saterday; $model->sunday = $subscription->sunday; $model->auto_payment = $subscription->auto_payment; $model->week_frequency = $subscription->week_frequency; // produits $arrayProductsSubscription = ProductSubscription::searchAll([ 'id_subscription' => $model->id ]) ; foreach ($arrayProductsSubscription as $productSubscription) { $model->products['product_' . $productSubscription->id_product] = $productSubscription->quantity; } } else { throw new NotFoundHttpException('L\'abonnement est introuvable.', 404); } // produits $productsArray = Product::searchAll() ; if ($model->load(Yii::$app->request->post()) && $model->validate()) { if (!strlen($model->date_end)) { $model->date_end = null; } if ($model->save()) { $this->redirect(['subscription/index']); } } return $this->render('update', [ 'model' => $model, 'productsArray' => $productsArray ]); } /** * Supprime une commande récurrente. * * @param integer $id */ public function actionDelete($id) { ProductSubscription::deleteAll(['id_subscription' => $id]); Subscription::findOne($id)->delete(); $this->redirect(['subscription/index']); } }