public function save()
 {
     $transaction = Yii::$app->db->beginTransaction();
     try {
         $characteristics = new Characteristics();
         $characteristics->display_type = $this->displayType;
         $characteristics->mechanism_type = $this->mechanismType;
         $characteristics->starp_type = $this->starpType;
         $characteristics->sex = $this->sex;
         if (!$characteristics->save(false)) {
             throw new \Exception('Charasteristic not save, transaction rollback');
         }
         $products = new Products();
         $products->clk_name = $this->name;
         $products->clk_description = $this->description;
         $products->characteristics_id = $characteristics->id;
         $products->price = $this->price;
         if (!$products->save(false)) {
             throw new \Exception('Product not save, transaction rollback');
         }
         $hashName = Yii::$app->security->generateRandomString();
         $fullImagePath = self::FULL_IMAGES_PATH . $hashName . '.' . $this->images->extension;
         if (!$this->images->saveAs($fullImagePath)) {
             throw new \Exception('Image not save in full image path');
         }
         $imgSizeReduct = function ($side = 'width') use($fullImagePath) {
             $size = getimagesize($fullImagePath);
             if ($side === 'width') {
                 return $size[0] / self::THUMB_REDUCTION;
             }
             if ($side === 'height') {
                 return $size[1] / self::THUMB_REDUCTION;
             }
         };
         $images = new Images();
         $transformation = new Transformation();
         $imagine = new Imagine();
         $transformation->thumbnail(new Box($imgSizeReduct('width'), $imgSizeReduct('height')))->save(Yii::getAlias('@webroot/' . self::THUMBS_IMAGES_PATH . $hashName . '.' . $this->images->extension));
         $transformation->apply($imagine->open(Yii::getAlias('@webroot/' . self::FULL_IMAGES_PATH . $hashName . '.' . $this->images->extension)));
         $images->product_id = $products->id;
         $images->img_name = $hashName . '.' . $this->images->extension;
         if (!$images->save(false)) {
             throw new \Exception('Images not save, transaction rollback');
         }
         $transaction->commit();
         Yii::$app->session->addFlash('success', 'Product successfully added');
     } catch (\Exception $e) {
         $transaction->rollBack();
         throw $e;
     }
 }
 public function actionDelete($id)
 {
     $product = Products::findOne($id);
     $characteristics = Characteristics::findOne($product->characteristics_id);
     $image = Images::findOne(['product_id' => $product->id]);
     $transaction = Products::getDb()->beginTransaction();
     try {
         if (!$product->delete()) {
             throw new \Exception('Product not deleted');
         }
         if (!$characteristics->delete()) {
             throw new \Exception('Characteristics not deleted');
         }
         if (!$image->delete()) {
             throw new \Exception('Image not deleted, from data base');
         }
         if (!unlink(Yii::getAlias('@webroot/' . AddProduct::THUMBS_IMAGES_PATH . $image->img_name))) {
             throw new \Exception('Thumb image not deleted');
         }
         if (!unlink(Yii::getAlias('@webroot/' . AddProduct::FULL_IMAGES_PATH . $image->img_name))) {
             throw new \Exception('Full image not deleted');
         }
         $transaction->commit();
         Yii::$app->session->addFlash('success', 'Product successfully deleted');
         $this->goBack();
     } catch (\Exception $e) {
         $transaction->rollBack();
         throw $e;
     }
 }
 protected function findModel($id)
 {
     if (($model = Products::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 public function search($params)
 {
     $query = Products::find()->joinWith(['characteristics']);
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 20], 'sort' => ['defaultOrder' => ['id' => SORT_ASC, 'clk_name' => SORT_ASC, 'characteristics.display_type' => SORT_ASC, 'characteristics.mechanism_type' => SORT_ASC, 'characteristics.starp_type' => SORT_ASC], 'attributes' => ['id', 'clk_name', 'characteristics.display_type', 'characteristics.starp_type', 'characteristics.mechanism_type']], 'key' => 'id']);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['products.id' => $this->id])->andFilterWhere(['like', 'clk_name', $this->clk_name])->andFilterWhere(['like', 'clk_description', $this->clk_description])->andFilterWhere(['like', 'characteristics.display_type', $this->getAttribute('characteristics.display_type')])->andFilterWhere(['like', 'characteristics.mechanism_type', $this->getAttribute('characteristics.mechanism_type')])->andFilterWhere(['like', 'characteristics.starp_type', $this->getAttribute('characteristics.starp_type')]);
     return $dataProvider;
 }
 public function fillInTheRest()
 {
     $product = Products::find()->with(['characteristics'])->where(['id' => $this->id])->one();
     if (!$product) {
         throw new NotFoundHttpException('Product Not Found');
     }
     $this->name = $product->clk_name;
     $this->description = $product->clk_description;
     $this->displayType = $product->characteristics->display_type;
     $this->mechanismType = $product->characteristics->mechanism_type;
     $this->starpType = $product->characteristics->starp_type;
     $this->sex = $product->characteristics->sex;
     $this->price = $product->price;
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Products::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'section_id' => $this->section_id, 'price' => $this->price, 'sensitivity' => $this->sensitivity, 'connector' => $this->connector, 'length' => $this->length, 'date' => $this->date]);
     $query->andFilterWhere(['like', 'img', $this->img])->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'description', $this->description])->andFilterWhere(['like', 'acoustic', $this->acoustic])->andFilterWhere(['like', 'frequency', $this->frequency]);
     return $dataProvider;
 }
 public function getProduct()
 {
     return $this->hasOne(Products::className(), ['id' => 'product_id']);
 }
 public function actionCatalog()
 {
     $query = Products::find()->joinWith(['images', 'characteristics']);
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 4], 'sort' => ['attributes' => ['price' => ['label' => 'Цене'], 'clk_name' => ['label' => 'Названию']]]]);
     return $this->render('catalog', ['dataProvider' => $dataProvider]);
 }