|
- <?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;
-
-
- class ProduitController extends BackendController {
-
- 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;
- }
- ]
- ],
- ],
- ];
- }
-
-
-
- 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,
- ]);
- }
-
-
-
- public function actionView($id) {
- return $this->render('view', [
- 'model' => $this->findModel($id),
- ]);
- }
-
-
-
- public function actionCreate() {
- $model = new Produit();
- $model->actif = 1;
- $model->id_etablissement = Yii::$app->user->identity->id_etablissement;
- $model->saison = 'all';
-
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
- Upload::uploadFile($model, 'illustration');
- Upload::uploadFile($model, 'photo');
- $model->save();
-
-
- $productions = Production::find()
- ->where('date > ' . date('Y-m-d'))
- ->andWhere(['id_etablissement' => Yii::$app->user->identity->id_etablissement])
- ->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,
- ]);
- }
- }
-
-
-
- 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,
- ]);
- }
- }
-
-
-
- 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']);
- }
-
- public function actionOrdre($tab) {
- $tab_ordre = json_decode(stripslashes($tab));
-
- foreach ($tab_ordre as $id => $o) {
- $produit = $this->findModel($id);
- $produit->order = $o;
- $produit->save();
- }
- }
-
-
-
- protected function findModel($id) {
- if (($model = Produit::findOne($id)) !== null) {
- return $model;
- } else {
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- }
-
- }
|