|
- <?php
-
-
-
- namespace backend\controllers;
-
- use common\helpers\GlobalParam;
- use common\models\ProductDistribution;
- use Yii;
- use yii\filters\AccessControl;
- use common\models\Product;
- use common\models\Distribution;
- use common\models\User;
- use common\models\UserProducer;
- 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 ProductController 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 User::hasAccessBackend();
- }
- ]
- ],
- ],
- ];
- }
-
-
-
- public function actionIndex()
- {
- $searchModel = new ProductSearch() ;
- $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
-
- return $this->render('index', [
- 'searchModel' => $searchModel,
- 'dataProvider' => $dataProvider,
- ]);
- }
-
-
-
- public function actionCreate()
- {
- $model = new Product();
- $model->active = 1;
- $model->id_producer = GlobalParam::getCurrentProducerId();
-
-
-
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
-
- $lastProductOrder = Product::find()->where('id_producer = :id_producer')->params([':id_producer' => GlobalParam::getCurrentProducerId()])->orderBy('order DESC')->one() ;
- if($lastProductOrder) {
- $model->order = ++ $lastProductOrder->order ;
- }
-
- Upload::uploadFile($model, 'photo');
- $model->save();
-
-
- Distribution::linkProductIncomingDistributions($model) ;
-
- Yii::$app->getSession()->setFlash('success', 'Produit <strong>'.Html::encode($model->name).'</strong> ajouté');
-
- return $this->redirect(['index']);
- }
- else {
- return $this->render('create', [
- 'model' => $model,
- ]);
- }
- }
-
-
-
- public function actionUpdate($id)
- {
- $request = Yii::$app->request;
-
- $model = $this->findModel($id);
- $photoFilenameOld = $model->photo;
-
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
-
- Upload::uploadFile($model, 'photo', $photoFilenameOld);
-
- $deletePhoto = $request->post('delete_photo', 0);
- if ($deletePhoto) {
- $model->photo = '';
- $model->save();
- }
-
- if($model->apply_distributions) {
-
- Distribution::linkProductIncomingDistributions($model) ;
- }
-
- Yii::$app->getSession()->setFlash('success', 'Produit <strong>'.Html::encode($model->name).'</strong> modifié');
- return $this->redirect(['index']);
- } else {
- return $this->render('update', [
- 'model' => $model,
- ]);
- }
- }
-
-
-
- public function actionDelete($id, $confirm = false)
- {
- $product = $this->findModel($id) ;
-
- if($confirm) {
- $product->delete();
- ProductDistribution::deleteAll(['id_product' => $id]) ;
- Yii::$app->getSession()->setFlash('success', 'Produit <strong>'.Html::encode($product->name).'</strong> supprimé');
- }
- else {
- Yii::$app->getSession()->setFlash('info', 'Souhaitez-vous vraiment supprimer le produit <strong>'.Html::encode($product->name).'</strong> ? '
- . Html::a('Oui',['product/delete','id' => $id, 'confirm' => 1], ['class' => 'btn btn-default']).' '.Html::a('Non', ['product/index'], ['class' => 'btn btn-default']));
- }
-
- return $this->redirect(['index']);
- }
-
-
-
- public function actionOrder()
- {
- $array = Yii::$app->request->post('array') ;
- $orderArray = json_decode(stripslashes($array));
-
- foreach($orderArray as $id => $order) {
- $product = $this->findModel($id);
- $product->order = $order;
- $product->save();
- }
- }
-
-
-
- protected function findModel($id)
- {
- if (($model = Product::findOne($id)) !== null) {
- return $model;
- } else {
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- }
-
- }
|