|
- <?php
-
- namespace backend\controllers;
-
- use common\models\ProductionProduit;
-
- use Yii;
- use yii\filters\AccessControl;
- use common\models\Produit;
- use common\models\Production;
- use common\models\User;
- use common\models\UserBoulangerie;
- use yii\data\ActiveDataProvider;
- use yii\web\Controller;
- use yii\web\NotFoundHttpException;
- use yii\filters\VerbFilter;
- use yii\web\UploadedFile;
- use common\helpers\Upload;
-
-
- /**
- * ProduitController implements the CRUD actions for Produit model.
- */
- class ProduitController extends Controller
- {
-
- var $enableCsrfValidation = false ;
-
- public function behaviors()
- {
- return [
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
-
- ],
- ],
- 'access' => [
- 'class' => AccessControl::className(),
- 'rules' => [
- [
- 'allow' => true,
- 'roles' => ['@'],
- 'matchCallback' => function ($rule, $action) {
- return Yii::$app->user->identity->status == USER::STATUS_ADMIN
- || Yii::$app->user->identity->status == USER::STATUS_BOULANGER;
- }
- ]
- ],
- ],
- ];
- }
-
- /**
- * Lists all Produit models.
- * @return mixed
- */
- public function actionIndex()
- {
- $dataProvider = new ActiveDataProvider([
- 'query' => Produit::find()
- ->where('(vrac IS NULL OR vrac = 0)')
- ->andWhere(['id_etablissement'=>Yii::$app->user->identity->id_etablissement])
- ->orderBy('order ASC'),
- 'pagination' => [
- 'pageSize' => 1000,
- ],
- ]);
-
- return $this->render('index', [
- 'dataProvider' => $dataProvider,
- ]);
- }
-
- /**
- * Displays a single Produit model.
- * @param integer $id
- * @return mixed
- */
- public function actionView($id)
- {
- return $this->render('view', [
- 'model' => $this->findModel($id),
- ]);
- }
-
- /**
- * Creates a new Produit model.
- * If creation is successful, the browser will be redirected to the 'view' page.
- * @return mixed
- */
- public function actionCreate()
- {
- $model = new Produit();
-
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
- Upload::uploadFile($model, 'illustration') ;
- Upload::uploadFile($model, 'photo') ;
- $model->save() ;
-
- // on ajoute un enregistrement ProductionProduit pour chaque production
- $productions = Production::find()->where('date > '.date('Y-m-d'))->all() ;
- foreach($productions as $prod) {
- $production_produit = new ProductionProduit ;
- $production_produit->id_production = $prod->id ;
- $production_produit->id_produit = $model->id ;
- $production_produit->actif = 0 ;
- $production_produit->save() ;
- }
-
- return $this->redirect(['index']);
- } else {
- return $this->render('create', [
- 'model' => $model,
- ]);
- }
- }
-
- /**
- * Updates an existing Produit model.
- * If update is successful, the browser will be redirected to the 'view' page.
- * @param integer $id
- * @return mixed
- */
- public function actionUpdate($id)
- {
- $request = Yii::$app->request ;
-
- $model = $this->findModel($id);
- $illustration_filename_old = $model->illustration ;
- $photo_filename_old = $model->photo ;
-
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
- Upload::uploadFile($model, 'illustration', $illustration_filename_old) ;
- Upload::uploadFile($model, 'photo', $photo_filename_old) ;
-
- $delete_illustration = $request->post('delete_illustration',0) ;
- if($delete_illustration) {
- $model->illustration = '' ;
- $model->save() ;
- }
-
- $delete_photo = $request->post('delete_photo',0) ;
- if($delete_photo) {
- $model->photo = '' ;
- $model->save() ;
- }
-
- return $this->redirect(['index']);
- } else {
- return $this->render('update', [
- 'model' => $model,
- ]);
- }
- }
-
- /**
- * Deletes an existing Produit 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();
-
- $productions_produits = ProductionProduit::find()->where(['id_produit'=>$id])->all() ;
- foreach($productions_produits as $pp)
- {
- $pp->delete() ;
- }
-
- return $this->redirect(['index']);
- }
-
- /**
- * Finds the Produit model based on its primary key value.
- * If the model is not found, a 404 HTTP exception will be thrown.
- * @param integer $id
- * @return Produit the loaded model
- * @throws NotFoundHttpException if the model cannot be found
- */
- protected function findModel($id)
- {
- if (($model = Produit::findOne($id)) !== null) {
- return $model;
- } else {
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- }
- }
|