|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- <?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 yii\data\ActiveDataProvider;
- use yii\web\Controller;
- use yii\web\NotFoundHttpException;
- use yii\filters\VerbFilter;
- use yii\web\UploadedFile;
-
-
- /**
- * ProduitController implements the CRUD actions for Produit model.
- */
- class VracController extends BackendController
- {
-
- var $enableCsrfValidation = false ;
-
- public function behaviors()
- {
- return [
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'delete' => ['post'],
- ],
- ],
- '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 = 1')->orderBy('order ASC'),
- ]);
-
- 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()) {
- self::uploadFile($model, 'illustration') ;
- self::uploadFile($model, 'photo') ;
-
- $model->vrac = 1 ;
- $model->lundi = 1 ;
- $model->mardi = 1 ;
- $model->mercredi = 1 ;
- $model->jeudi = 1 ;
- $model->vendredi = 1 ;
- $model->samedi = 1 ;
- $model->dimanche = 1 ;
- $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()) {
- self::uploadFile($model, 'illustration', $illustration_filename_old) ;
- self::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,
- ]);
- }
- }
-
- public static function uploadFile($model, $champs, $filename_old = '') {
- $file = UploadedFile::getInstance($model, $champs);
- if($file) {
- $file_name = $file->baseName.'-'.uniqid().'.' . $file->extension ;
- $file->saveAs('../../frontend/web/uploads/' . $file_name);
- $model->$champs = $file_name ;
- }
- else {
- $model->$champs = $filename_old ;
- }
-
- $model->save() ;
- }
-
- /**
- * 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.');
- }
- }
- }
|