|
- <?php
-
-
-
- namespace backend\controllers;
-
- 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()
- {
- $dataProvider = new ActiveDataProvider([
- 'query' => Product::find()
- ->where(['id_producer' => Producer::getId()])
- ->orderBy('order ASC'),
- 'pagination' => [
- 'pageSize' => 1000,
- ],
- ]);
-
- return $this->render('index', [
- 'dataProvider' => $dataProvider,
- ]);
- }
-
-
-
- public function actionCreate()
- {
- $model = new Product();
- $model->active = 1;
- $model->id_producer = Producer::getId();
-
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
-
- Upload::uploadFile($model, 'photo');
- $model->save();
-
-
- $distributionsArray = Distribution::find()
- ->where('date > ' . date('Y-m-d'))
- ->andWhere(['id_producer' => Producer::getId()])
- ->all();
-
- 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,
- ]);
- }
- }
-
-
-
- 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();
- }
-
- return $this->redirect(['index']);
- } else {
- return $this->render('update', [
- 'model' => $model,
- ]);
- }
- }
-
-
-
- public function actionDelete($id)
- {
- $this->findModel($id)->delete();
- ProductDistribution::deleteAll(['id_product' => $id]) ;
-
- return $this->redirect(['index']);
- }
-
-
-
- public function actionOrder($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.');
- }
- }
-
- }
|