Pārlūkot izejas kodu

Adaptations refactoring/traduction backend/controllers/ProduitController > ProductController

refactoring
Guillaume Bourgeois pirms 6 gadiem
vecāks
revīzija
4ecc6504b6
1 mainītis faili ar 39 papildinājumiem un 52 dzēšanām
  1. +39
    -52
      backend/controllers/ProductController.php

backend/controllers/ProduitController.php → backend/controllers/ProductController.php Parādīt failu

@@ -38,13 +38,13 @@ termes.

namespace backend\controllers;

use common\models\ProductionProduit;
use common\models\ProductDistribution;
use Yii;
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\UserBoulangerie;
use common\models\UserProducer;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
@@ -55,7 +55,7 @@ use common\helpers\Upload;
/**
* ProduitController implements the CRUD actions for Produit model.
*/
class ProduitController extends BackendController
class ProductController extends BackendController
{
var $enableCsrfValidation = false;

@@ -74,8 +74,7 @@ class ProduitController extends BackendController
'allow' => true,
'roles' => ['@'],
'matchCallback' => function ($rule, $action) {
return Yii::$app->user->identity->status == USER::STATUS_ADMIN
|| Yii::$app->user->identity->status == USER::STATUS_BOULANGER;
return User::hasAccessBackend();
}
]
],
@@ -91,9 +90,8 @@ class ProduitController extends BackendController
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])
'query' => Product::find()
->where(['id_producer' => Producer::getId()])
->orderBy('order ASC'),
'pagination' => [
'pageSize' => 1000,
@@ -101,7 +99,7 @@ class ProduitController extends BackendController
]);

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

@@ -113,33 +111,33 @@ class ProduitController extends BackendController
*/
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()) {
Upload::uploadFile($model, 'illustration');
Upload::uploadFile($model, 'photo');
$model->save();

// on ajoute un enregistrement ProductionProduit pour chaque production
$productions = Production::find()
$distributionsArray = Distribution::find()
->where('date > ' . date('Y-m-d'))
->andWhere(['id_etablissement' => Yii::$app->user->identity->id_etablissement])
->andWhere(['id_producer' => Producer::getId()])
->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']);
} else {
return $this->render('create', [
'model' => $model,
'model' => $model,
]);
}
}
@@ -156,21 +154,14 @@ class ProduitController extends BackendController
$request = Yii::$app->request;

$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()) {
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->save();
}
@@ -178,7 +169,7 @@ class ProduitController extends BackendController
return $this->redirect(['index']);
} else {
return $this->render('update', [
'model' => $model,
'model' => $model,
]);
}
}
@@ -194,28 +185,24 @@ class ProduitController extends BackendController
public function actionDelete($id)
{
$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']);
}

/**
* 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();
}
}

@@ -228,7 +215,7 @@ class ProduitController extends BackendController
*/
protected function findModel($id)
{
if (($model = Produit::findOne($id)) !== null) {
if (($model = Product::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');

Notiek ielāde…
Atcelt
Saglabāt