Browse Source

Adaptations refactoring/traduction backend/controllers/ProduitController > ProductController

dev
Guillaume Bourgeois 6 years ago
parent
commit
4ecc6504b6
1 changed files with 39 additions and 52 deletions
  1. +39
    -52
      backend/controllers/ProductController.php

backend/controllers/ProduitController.php → backend/controllers/ProductController.php View File



namespace backend\controllers; namespace backend\controllers;


use common\models\ProductionProduit;
use common\models\ProductDistribution;
use Yii; use Yii;
use yii\filters\AccessControl; use yii\filters\AccessControl;
use common\models\Produit;
use common\models\Production;
use common\models\Product;
use common\models\Distribution;
use common\models\User; use common\models\User;
use common\models\UserBoulangerie;
use common\models\UserProducer;
use yii\data\ActiveDataProvider; use yii\data\ActiveDataProvider;
use yii\web\Controller; use yii\web\Controller;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
/** /**
* ProduitController implements the CRUD actions for Produit model. * ProduitController implements the CRUD actions for Produit model.
*/ */
class ProduitController extends BackendController
class ProductController extends BackendController
{ {
var $enableCsrfValidation = false; var $enableCsrfValidation = false;


'allow' => true, 'allow' => true,
'roles' => ['@'], 'roles' => ['@'],
'matchCallback' => function ($rule, $action) { 'matchCallback' => function ($rule, $action) {
return Yii::$app->user->identity->status == USER::STATUS_ADMIN
|| Yii::$app->user->identity->status == USER::STATUS_BOULANGER;
return User::hasAccessBackend();
} }
] ]
], ],
public function actionIndex() public function actionIndex()
{ {
$dataProvider = new ActiveDataProvider([ $dataProvider = new ActiveDataProvider([
'query' => Produit::find()
->where('(vrac IS NULL OR vrac = 0)')
->andWhere(['id_etablissement' => Yii::$app->user->identity->id_etablissement])
'query' => Product::find()
->where(['id_producer' => Producer::getId()])
->orderBy('order ASC'), ->orderBy('order ASC'),
'pagination' => [ 'pagination' => [
'pageSize' => 1000, 'pageSize' => 1000,
]); ]);


return $this->render('index', [ return $this->render('index', [
'dataProvider' => $dataProvider,
'dataProvider' => $dataProvider,
]); ]);
} }


*/ */
public function actionCreate() public function actionCreate()
{ {
$model = new Produit();
$model->actif = 1;
$model->id_etablissement = Yii::$app->user->identity->id_etablissement;
$model->saison = 'all';
$model = new Product();
$model->active = 1;
$model->id_producer = Producer::getId();


if ($model->load(Yii::$app->request->post()) && $model->save()) { if ($model->load(Yii::$app->request->post()) && $model->save()) {
Upload::uploadFile($model, 'illustration');
Upload::uploadFile($model, 'photo'); Upload::uploadFile($model, 'photo');
$model->save(); $model->save();


// on ajoute un enregistrement ProductionProduit pour chaque production // on ajoute un enregistrement ProductionProduit pour chaque production
$productions = Production::find()
$distributionsArray = Distribution::find()
->where('date > ' . date('Y-m-d')) ->where('date > ' . date('Y-m-d'))
->andWhere(['id_etablissement' => Yii::$app->user->identity->id_etablissement])
->andWhere(['id_producer' => Producer::getId()])
->all(); ->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();
foreach ($distributionsArray as $distribution) {
$productDistribution = new ProductDistribution;
$productDistribution->id_distribution = $distribution->id;
$productDistribution->id_product = $model->id;
$productDistribution->active = 0;
$productDistribution->save();
} }


return $this->redirect(['index']); return $this->redirect(['index']);
} else { } else {
return $this->render('create', [ return $this->render('create', [
'model' => $model,
'model' => $model,
]); ]);
} }
} }
$request = Yii::$app->request; $request = Yii::$app->request;


$model = $this->findModel($id); $model = $this->findModel($id);
$illustration_filename_old = $model->illustration;
$photo_filename_old = $model->photo;
$photoFilenameOld = $model->photo;


if ($model->load(Yii::$app->request->post()) && $model->save()) { if ($model->load(Yii::$app->request->post()) && $model->save()) {
Upload::uploadFile($model, 'illustration', $illustration_filename_old);
Upload::uploadFile($model, 'photo', $photo_filename_old);
Upload::uploadFile($model, 'photo', $photoFilenameOld);


$delete_illustration = $request->post('delete_illustration', 0);
if ($delete_illustration) {
$model->illustration = '';
$model->save();
}

$delete_photo = $request->post('delete_photo', 0);
if ($delete_photo) {
$deletePhoto = $request->post('delete_photo', 0);
if ($deletePhoto) {
$model->photo = ''; $model->photo = '';
$model->save(); $model->save();
} }
return $this->redirect(['index']); return $this->redirect(['index']);
} else { } else {
return $this->render('update', [ return $this->render('update', [
'model' => $model,
'model' => $model,
]); ]);
} }
} }
public function actionDelete($id) public function actionDelete($id)
{ {
$this->findModel($id)->delete(); $this->findModel($id)->delete();

$productions_produits = ProductionProduit::find()->where(['id_produit' => $id])->all();
foreach ($productions_produits as $pp) {
$pp->delete();
}

ProductDistribution::deleteAll(['id_product' => $id]) ;
return $this->redirect(['index']); return $this->redirect(['index']);
} }


/** /**
* Modifie l'ordre des produits. * Modifie l'ordre des produits.
* *
* @param array $tab
* @param array $array
*/ */
public function actionOrdre($tab)
public function actionOrder($array)
{ {
$tab_ordre = json_decode(stripslashes($tab));
$orderArray = json_decode(stripslashes($array));


foreach ($tab_ordre as $id => $o) {
$produit = $this->findModel($id);
$produit->order = $o;
$produit->save();
foreach($orderArray as $id => $order) {
$product = $this->findModel($id);
$product->order = $order;
$product->save();
} }
} }


*/ */
protected function findModel($id) protected function findModel($id)
{ {
if (($model = Produit::findOne($id)) !== null) {
if (($model = Product::findOne($id)) !== null) {
return $model; return $model;
} else { } else {
throw new NotFoundHttpException('The requested page does not exist.'); throw new NotFoundHttpException('The requested page does not exist.');

Loading…
Cancel
Save