Advertisement
slo_nik

CarController

Mar 15th, 2018
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 65.38 KB | None | 0 0
  1. <?php
  2.  
  3. namespace frontend\controllers;
  4.  
  5. use app\models\UploadForm;
  6. use backend\models\Regions;
  7. use backend\models\UserAccountManager;
  8. use app\models\Watermark;
  9. use Eventviva\ImageResize;
  10. use frontend\models\FavoritesCars;
  11. use Yii;
  12. use app\models\AddCar;
  13. use app\models\CarCharacteristicValue;
  14. use app\models\CarModification;
  15. use app\models\CarSerie;
  16. use app\models\ContactSeller;
  17. use backend\models\Cars;
  18. use backend\models\City;
  19. use backend\models\CarModel;
  20. use backend\models\CarFeatured;
  21. use DateTime;
  22.  
  23. use yii\data\Pagination;
  24. use yii\helpers\Json;
  25. use yii\db\Expression;
  26. use common\models\User;
  27. use yii\web\UploadedFile;
  28. use yii\helpers\FileHelper;
  29. use yii\imagine\Image;
  30.  
  31.  
  32. class CarController extends \yii\web\Controller
  33. {
  34.  
  35.     public $list;
  36.  
  37. //    public function beforeAction($action)
  38. //    {
  39. //        if (in_array($action->id, ['/car/show'])) {
  40. //            $this->enableCsrfValidation = false;
  41. //        }
  42. //        if($action->id = 'add-car-form') {
  43. //            Yii::$app->request->enableCsrfValidation = false;
  44. //        }
  45. //        return parent::beforeAction($action);
  46. //    }
  47.  
  48.     public function actionIndex()
  49.     {
  50.  
  51.  
  52.         return $this->render('index', [
  53.  
  54.         ]);
  55.     }
  56.  
  57.     public function actionSearch()
  58.     {
  59.  
  60.         $model = Cars::find()->asArray()->all();
  61.  
  62.         return $this->render('search', [
  63.             'cars' => $model
  64.         ]);
  65.     }
  66.  
  67.     public function actionShow($id)
  68.     {
  69.  
  70.         $contactSeller = new ContactSeller();
  71.  
  72.         $car = Cars::find()->where(['id' => $id])->asArray()->one();
  73.  
  74.         $carExists = Cars::find()->where(['id' => $id])->exists();
  75.         if ($carExists) {
  76.             $car = Cars::find()->where(['id' => $id])->one();
  77.             $car->viewed += 1;
  78.             $car->save();
  79.             return $this->render(('show2'), compact('car', 'contactSeller'));
  80.         } else {
  81.             $this->goHome();
  82.             echo 'Oops.. 404 error, <a href="/"> Prodejvuz.cz </a>';
  83.         }
  84.  
  85.     }
  86.  
  87.  
  88.     public function actionShowNew($id)
  89.     {
  90.  
  91.         $contactSeller = new ContactSeller();
  92.  
  93.         $car = Cars::find()->where(['id' => $id])->asArray()->one();
  94.  
  95.         $carExists = Cars::find()->where(['id' => $id])->exists();
  96.         if ($carExists) {
  97.             $car = Cars::find()->where(['id' => $id])->one();
  98.             $car->viewed += 1;
  99.             $car->save();
  100.             return $this->render(('show2'), compact('car', 'contactSeller'));
  101.         } else {
  102.             $this->goHome();
  103.             echo 'Oops.. 404 error, <a href="/"> Prodejvuz.cz </a>';
  104.         }
  105.  
  106.     }
  107.  
  108.     public function actionMyCars()
  109.     {
  110.         if (Yii::$app->user->isGuest) {
  111.             $this->goHome();
  112.         } else {
  113.             return $this->render('myCars');
  114.         }
  115.     }
  116.  
  117.     public function actionSellCar()
  118.     {
  119.         if (Yii::$app->user->isGuest) {
  120.             $this->goHome();
  121.         } else {
  122.             return $this->render('sellCar');
  123.         }
  124.     }
  125.  
  126.     public function actionMyTariff()
  127.     {
  128.         if (Yii::$app->user->isGuest) {
  129.             $this->goHome();
  130.         } else {
  131.             return $this->render('myTariff');
  132.         }
  133.     }
  134.  
  135.     public function actionBuyTariff()
  136.     {
  137.         if (Yii::$app->user->isGuest) {
  138.             $this->goHome();
  139.         } else {
  140.             return $this->render('buyTariff');
  141.         }
  142.     }
  143.  
  144.     public function actionContactSeller()
  145.     {
  146.         $contactSeller = new ContactSeller();
  147.  
  148.         if (\Yii::$app->request->isAjax) {
  149.             if ($contactSeller->load(\Yii::$app->request->post()) && $contactSeller->validate()) {
  150.                 $data = \Yii::$app->request->post('ContactSeller');
  151.  
  152.                 if (\Yii::$app->mailer->compose(['html' => '@common/mail/views/test'], ['model' => $data])
  153.                     ->setFrom(['robot@prodejvuz.cz' => 'ProdejVuz.cz'])
  154. //                    ->setTo()
  155.                     ->setTo(\app\models\ContactSeller::getUserEmailByCarId($data['id_car']))
  156.                     //TODO взять почту владельца машины
  157.                     ->setSubject('ProdejVuz.cz - odpoved na inzerat')
  158.                     ->setTextBody('Текст сообщения')
  159.                     ->send()) {
  160.                     return 'sent';
  161.                 } else {
  162.                     return 'error';
  163.                 }
  164.             }
  165.  
  166.         }
  167.  
  168.  
  169.     }
  170.  
  171.     public function actionGetModel($id)
  172.     {
  173.         $carModel = CarModel::find()
  174.             ->select('name,id_car_model')
  175.             ->where(['id_car_mark' => $id])
  176.             ->asArray()
  177.             ->all();
  178.  
  179. //        return json_encode($carModel);
  180.         return Json::encode($carModel);
  181.     }
  182.  
  183.     public function actionGetCity($id)
  184.     {
  185.         $carCity = City::find()
  186.             ->where(['region_id' => $id])
  187.             ->orderBy(['city_name' => SORT_ASC])
  188.             ->asArray()
  189.             ->all();
  190.  
  191. //        return json_encode($carModel);
  192.         return Json::encode($carCity);
  193.     }
  194.  
  195.     public function actionGetBody($id)
  196.     {
  197.         $carBody = CarSerie::find()
  198.             ->select('name')
  199.             ->where(['id_car_model' => $id])
  200.             ->one();
  201.         return Json::encode($carBody);
  202.     }
  203.  
  204.     public function actionExpCars()
  205.     {
  206.         return CarFeatured::CarFeatureExp();
  207.     }
  208.  
  209.     public function idCar($id)
  210.     {
  211.         return $id_car_modification = CarModification::find()
  212.             ->select('id_car_modification')
  213.             ->where(['id_car_model' => $id])
  214.             ->one();
  215.     }
  216.  
  217.     public function actionGetCarTank($id)
  218.     {
  219.         $id_car_modification = $this->idCar($id);
  220.         $carHp = CarCharacteristicValue::find()
  221.             ->select('value')
  222.             ->where(['id_car_modification' => $id_car_modification])
  223.             ->andWhere(['id_car_characteristic' => 35])
  224.             ->all();
  225.         return Json::encode($carHp);
  226.     }
  227.  
  228.     public function actionGetCarEs($id)
  229.     {
  230.         $id_car_modification = $this->idCar($id);
  231.         $carHp = CarCharacteristicValue::find()
  232.             ->select('value')
  233.             ->where(['id_car_modification' => $id_car_modification])
  234.             ->andWhere(['id_car_characteristic' => 37])
  235.             ->all();
  236.         return Json::encode($carHp);
  237.     }
  238.  
  239.     public function actionGetCarEngineSize($id)
  240.     {
  241.         $id_car_modification = $this->idCar($id);
  242.         $carHp = CarCharacteristicValue::find()
  243.             ->select('value')
  244.             ->where(['id_car_modification' => $id_car_modification])
  245.             ->andWhere(['id_car_characteristic' => 13])
  246.             ->all();
  247.         return Json::encode($carHp);
  248.     }
  249.  
  250.     public function actionGetCarGearBox($id)
  251.     {
  252.         $id_car_modification = $this->idCar($id);
  253.         $carHp = CarCharacteristicValue::find()
  254.             ->select('value')
  255.             ->where(['id_car_modification' => $id_car_modification])
  256.             ->andWhere(['id_car_characteristic' => 24])
  257.             ->all();
  258.         return Json::encode($carHp);
  259.     }
  260.  
  261.     public function actionGetCarGearCount($id)
  262.     {
  263.         $id_car_modification = $this->idCar($id);
  264.         $carHp = CarCharacteristicValue::find()
  265.             ->select('value')
  266.             ->where(['id_car_modification' => $id_car_modification])
  267.             ->andWhere(['id_car_characteristic' => 26])
  268.             ->all();
  269.         return Json::encode($carHp);
  270.     }
  271.  
  272.     public function actionGetCarFuel($id)
  273.     {
  274.         $id_car_modification = $this->idCar($id);
  275.         $carHp = CarCharacteristicValue::find()
  276.             ->select('value')
  277.             ->where(['id_car_modification' => $id_car_modification])
  278.             ->andWhere(['id_car_characteristic' => 12])
  279.             ->all();
  280.         return Json::encode($carHp);
  281.     }
  282.  
  283.     public function actionGetCarFuelCity($id)
  284.     {
  285.         $id_car_modification = $this->idCar($id);
  286.         $carHp = CarCharacteristicValue::find()
  287.             ->select('value')
  288.             ->where(['id_car_modification' => $id_car_modification])
  289.             ->andWhere(['id_car_characteristic' => 50])
  290.             ->all();
  291.         return Json::encode($carHp);
  292.     }
  293.  
  294.     public function actionGetCarFuelHighway($id)
  295.     {
  296.         $id_car_modification = $this->idCar($id);
  297.         $carHp = CarCharacteristicValue::find()
  298.             ->select('value')
  299.             ->where(['id_car_modification' => $id_car_modification])
  300.             ->andWhere(['id_car_characteristic' => 51])
  301.             ->all();
  302.         return Json::encode($carHp);
  303.     }
  304.  
  305.     public function actionGetCarFuelCombined($id)
  306.     {
  307.         $id_car_modification = $this->idCar($id);
  308.         $carHp = CarCharacteristicValue::find()
  309.             ->select('value')
  310.             ->where(['id_car_modification' => $id_car_modification])
  311.             ->andWhere(['id_car_characteristic' => 52])
  312.             ->all();
  313.         return Json::encode($carHp);
  314.     }
  315.  
  316.     public function actionGetCarHp($id)
  317.     {
  318.         $id_car_modification = $this->idCar($id);
  319.         $carHp = CarCharacteristicValue::find()
  320.             ->select('value')
  321.             ->where(['id_car_modification' => $id_car_modification])
  322.             ->andWhere(['id_car_characteristic' => 14])
  323.             ->all();
  324.         return Json::encode($carHp);
  325.     }
  326.  
  327.     public function actionGetCarInfo()
  328.     {
  329.         if (Yii::$app->user->isGuest) {
  330.             $this->goHome();
  331.         } else {
  332.             $id = \Yii::$app->request->get('id');
  333.  
  334.             if (($model = Cars::find()->where(['id' => $id])) !== null) {
  335.                 $model = Cars::find()->where(['id' => $id])->one();
  336.             } else {
  337.                 $this->goHome();
  338.             }
  339.         }
  340.         return Json::encode($model);
  341.     }
  342.  
  343.  
  344.     public function actionAddCarForm()
  345.     {
  346.  
  347.         $car = new Cars();
  348.         $car->car_created_date = new Expression('NOW()');
  349.         $form = new AddCar();
  350.  
  351.  
  352.         if (!$car::UserCarsLeftCount()) {
  353.             return $this->redirect(['/']);
  354.         }
  355.         $folder = Yii::$app->request->post('folder');
  356.         $form->images = $folder;
  357.  
  358.         if ($form->load(Yii::$app->request->post()) && $form->validate()) {
  359.  
  360.             #$images = UploadedFile::getInstances($form, 'images');
  361.  
  362.             #var_dump($images);
  363.  
  364.  
  365.             #var_dump(Yii::$app->request->post());
  366.  
  367.             $form->features = Yii::$app->request->post('AddCar')['features'][0];
  368.  
  369.             $car->user_id = Yii::$app->user->getId();
  370.             $car->car_region = $form->state;
  371.             $car->car_city = $form->city;
  372.             $car->mark = $form->mark;
  373.             $car->model = $form->model;
  374.             $car->car_body = $form->body ? $form->body : 0;
  375.             $car->car_condition = $form->condition ? $form->condition : 0;
  376.             $car->car_mileage = $form->mileage;
  377.             $car->car_year = $form->year;
  378.             $car->car_fuel_type = $form->fuel ? $form->fuel : 0;
  379.             $car->car_gearbox_type = $form->transmission ? $form->transmission : 0;
  380.             $car->car_sale_type = $form->vendor;
  381.             $car->car_vin = $form->vin ? $form->vin : 0;
  382.             $car->car_price = abs($form->price);
  383.             $car->car_comment = $form->comment;
  384.             $car->car_tires = $form->tires ? $form->tires : 0;
  385.             $car->car_air_conditioning = $form->climate ? $form->climate : 0;
  386.             $car->car_fuel_combined_100km = $form->fuelSpending ? $form->fuelSpending : 0;
  387.             $car->car_environmental_standard = $form->emise ? $form->emise : 0;
  388.             $car->car_owner_number = $form->countOwner ? $form->countOwner : 0;
  389.             $car->car_features = $form->features;
  390.             $car->car_video = $form->video;
  391.             $car->car_sold = 0;
  392. //            other
  393.  
  394.             $car->car_doors_count = 0;
  395.             $car->car_engine_size = $form->engine_size ? $form->engine_size : 0;
  396.             $car->car_engine_additional = ' ';
  397.             $car->car_power_hp = $form->engine_power ? $form->engine_power : 0;
  398.             $car->car_country = $form->car_country ? $form->car_country : 0;
  399.             $car->car_is_featured = 0;
  400.             $car->car_number_gears = 0;
  401.             $car->car_fuel_city_100km = 0;
  402.             $car->car_fuel_highway_100km = 0;
  403.             $car->car_volume_tank = 0;
  404.             $car->car_is_featured = 0;
  405.             $car->autobaza = 0;
  406. //            $car->image = count($images);
  407.             $car->image = $form->images;
  408.  
  409.             $user = User::find()->where(['id' => Yii::$app->user->getId()])->one();
  410.             $user->phone = $form->phone;
  411.             $user->save();
  412.             if ($car->save()) {
  413.                 $id = $car->getPrimaryKey();
  414.  
  415.                 $pathTemp = Yii::getAlias('@frontend/web/cars/tmp_upload/' . $folder);
  416.  
  417.  
  418.                 if (is_dir($pathTemp)) {
  419.                     $images = \yii\helpers\FileHelper::findFiles($pathTemp);
  420.  
  421.                     $path = 'cars/upload/' . $id;
  422.                     FileHelper::createDirectory($path);
  423.  
  424.  
  425.                     $img_mini_path = \Yii::getAlias('@frontend/web/') . 'cars/upload/' . $id . '/mini';
  426.                     FileHelper::createDirectory($img_mini_path);
  427.  
  428.                     $i = 1;
  429.  
  430.                     $watermark_img = \Yii::getAlias('@frontend/web/img/logo-header-png.png');
  431.  
  432.                     foreach (array_values($images) as $file) {
  433.  
  434.                         $img = $file;
  435.                         $image = new ImageResize($img);
  436.                         $image->quality_jpg = 90;
  437.                         $image->save($pathTemp . $i . '.jpg');
  438.                         $img = $pathTemp . $i . '.jpg';
  439.                         $watermark = new Watermark();
  440.                         $watermark->apply($img, $img, $watermark_img, 3);
  441.  
  442.  
  443.                         Image::thumbnail($img, 274, 210)
  444.                             ->save($img_mini_path . '/' . $i . '.jpg');
  445.  
  446.                         rename($file, \Yii::getAlias('@frontend/web/cars/upload/' . $id . '/' . $i . '.jpg'));
  447.                         $i++;
  448.  
  449.                     }
  450.                     FileHelper::removeDirectory($pathTemp);
  451.                 }
  452.  
  453.  
  454.                 $userCountCar = User::find()->where(['id' => Yii::$app->user->getId()])->one();
  455.                 $userCountCar->max_cars_count -= 1;
  456.                 $userCountCar->save();
  457.  
  458.  
  459.                 Yii::$app->session->setFlash('car_added', "Your car was added!");
  460.                 $this->redirect(['site/ok', 'id' => $id]);
  461.             } else {
  462. //                TODO alert error
  463.                 Yii::$app->session->setFlash('car_not_added', "Your car wasn't added!");
  464.                 #var_dump(Yii::$app->request->post());
  465.                $this->goHome();
  466.  
  467.             }
  468.  
  469.         }
  470.     }
  471.  
  472.     public function actionAddCarFormUpdate()
  473.     {
  474.  
  475.         $id = \Yii::$app->request->get('id');
  476.         $car = Cars::findOne($id);
  477. //        var_dump($car);
  478.         $car->car_updated_date = new Expression('NOW()');
  479.         $form = new AddCar();
  480.  
  481.  
  482. //        if ( $car::CheckStatus() ) {
  483. //            return $this->redirect(['/']);
  484. //        }
  485.  
  486. //        $images = UploadedFile::getInstances($form, 'images');
  487. //        var_dump($images);
  488.  
  489.         $folder = Yii::$app->request->post('folder');
  490.         $form->images = $folder;
  491.  
  492.         if ($form->load(Yii::$app->request->post()) && $form->validate()) {
  493.             #var_dump(Yii::$app->request->post());
  494.  
  495.  
  496.             $form->features = Yii::$app->request->post('AddCar')['features'][0];
  497.  
  498.             $car->user_id = Yii::$app->user->getId();
  499.             $car->car_region = $form->state;
  500.             $car->car_city = $form->city;
  501.             $car->mark = $form->mark;
  502.             $car->model = $form->model;
  503.             $car->car_body = $form->body ? $form->body : 0;
  504.             $car->car_condition = $form->condition ? $form->condition : 0;
  505.             $car->car_mileage = $form->mileage;
  506.             $car->car_year = $form->year;
  507.             $car->car_fuel_type = $form->fuel ? $form->fuel : 0;
  508.             $car->car_gearbox_type = $form->transmission ? $form->transmission : 0;
  509.             $car->car_sale_type = $form->vendor;
  510.             $car->car_vin = $form->vin ? $form->vin : 0;
  511.  
  512.             $car->car_comment = $form->comment;
  513.             $car->car_tires = $form->tires ? $form->tires : 0;
  514.             $car->car_air_conditioning = $form->climate ? $form->climate : 0;
  515.             $car->car_fuel_combined_100km = $form->fuelSpending ? $form->fuelSpending : 0;
  516.             $car->car_environmental_standard = $form->emise ? $form->emise : 0;
  517.             $car->car_owner_number = $form->countOwner ? $form->countOwner : 0;
  518.             $car->car_features = $form->features;
  519.             $car->car_video = $form->video;
  520.             $car->car_sold = 0;
  521. //            other
  522.  
  523.             if ($form->price > $car->car_price) {
  524.                 $car->car_price = $form->price;
  525.             } elseif ($form->price < $car->car_price) {
  526.                 $car->car_price_old = $car->car_price;
  527.                 $car->car_price = $form->price;
  528.             }
  529.  
  530.             $car->car_doors_count = 0;
  531.             $car->car_engine_size = $form->engine_size ? $form->engine_size : 0;
  532.             $car->car_engine_additional = ' ';
  533.             $car->car_power_hp = $form->engine_power ? $form->engine_power : 0;
  534.             $car->car_country = $form->car_country ? $form->car_country : 0;
  535.             $car->car_is_featured = 0;
  536.             $car->car_number_gears = 0;
  537.             $car->car_fuel_city_100km = 0;
  538.             $car->car_fuel_highway_100km = 0;
  539.             $car->car_volume_tank = 0;
  540.             $car->car_is_featured = 0;
  541.             $car->autobaza = 0;
  542.             $car->image = $folder;
  543.  
  544.             $user = User::find()->where(['id' => Yii::$app->user->getId()])->one();
  545.             $user->phone = $form->phone;
  546.             $user->save();
  547.             #print_r($car->save());
  548.            if ($car->save()) {
  549.                 Yii::$app->session->setFlash('car_updated');
  550.                 if (!empty($form->images)) {
  551.  
  552.  
  553.                     $pathTemp = Yii::getAlias('@frontend/web/cars/tmp_upload/' . $folder);
  554.  
  555.                     if (is_dir($pathTemp)) {
  556.                         $images = \yii\helpers\FileHelper::findFiles($pathTemp);
  557.  
  558.                         $path = 'cars/upload/' . $id;
  559.                         FileHelper::removeDirectory($path);
  560.                         FileHelper::createDirectory($path);
  561.  
  562.  
  563.                         $img_mini_path = \Yii::getAlias('@frontend/web/') . 'cars/upload/' . $id . '/mini';
  564.                         FileHelper::createDirectory($img_mini_path);
  565.  
  566.                         $i = 1;
  567.  
  568.                         $watermark_img = \Yii::getAlias('@frontend/web/img/logo-header-png.png');
  569.  
  570.                         foreach (array_values($images) as $file) {
  571.  
  572.                             $img = $file;
  573.                             $image = new ImageResize($img);
  574.                             $image->quality_jpg = 90;
  575.                             $image->save($pathTemp . $i . '.jpg');
  576.  
  577.  
  578.                             $img = $pathTemp . $i . '.jpg';
  579.                             $watermark = new Watermark();
  580.                             $watermark->apply($img, $img, $watermark_img, 3);
  581.  
  582.                             Image::thumbnail($img, 274, 210)
  583.                                 ->save($img_mini_path . '/' . $i . '.jpg');
  584.  
  585.                             rename($file, \Yii::getAlias('@frontend/web/cars/upload/' . $id . '/' . $i . '.jpg'));
  586.                             $i++;
  587.  
  588.  
  589.                         }
  590.                         FileHelper::removeDirectory($pathTemp);
  591.  
  592.                     }
  593.                     $this->goHome();
  594.  
  595.                 } else {
  596.                     //TODO alert
  597.                     $this->goHome();
  598.                 }
  599.             } else {
  600.                 Yii::$app->session->setFlash('car_not_updated');
  601.                 $this->goHome();
  602.             }
  603.  
  604.         } else {
  605.             $this->goHome();
  606.         }
  607.     }
  608.  
  609.     //imgs upload
  610.  
  611.     public function png2jpg($originalFile, $outputFile, $quality = 90)
  612.     {
  613.         $source = imagecreatefrompng($originalFile);
  614.         $image = imagecreatetruecolor(imagesx($source), imagesy($source));
  615.  
  616.         $white = imagecolorallocate($image, 255, 255, 255);
  617.         imagefill($image, 0, 0, $white);
  618.  
  619.         imagecopy($image, $source, 0, 0, 0, 0, imagesx($image), imagesy($image));
  620.  
  621.         imagejpeg($image, $outputFile, $quality);
  622.         imagedestroy($image);
  623.         imagedestroy($source);
  624.     }
  625.  
  626.  
  627.     public function actionImgLoad()
  628.     {
  629.         $this->enableCsrfValidation = false;
  630.  
  631.         $model = new UploadForm();
  632.         $images = UploadedFile::getInstance($model, 'images');
  633.  
  634.         $folder = Yii::$app->request->post('folder');
  635.  
  636.         $path = 'cars/tmp_upload/' . $folder;
  637.         FileHelper::createDirectory($path);
  638.  
  639.         $images->saveAs($path . '/' . $images->name);
  640.  
  641.  
  642.         if ($images->extension == 'png') {
  643.             $this->png2jpg($path . '/' . $images->name, $path . '/' . $images->baseName . '.jpg');
  644.             unlink($path . '/' . $images->name);
  645.         }
  646.  
  647.         return true;
  648.         #var_dump(Yii::$app->request->post('folder'));
  649.        #var_dump($images);
  650.    }
  651.     //imgs upload
  652. //    img delete
  653.     public function actionImgDelete()
  654.     {
  655.         if (Yii::$app->request->isAjax) {
  656.  
  657.             $folder = Yii::$app->request->post('folder');
  658.             $img = Yii::$app->request->post('img');
  659.  
  660.             $img = 'cars/tmp_upload/' . $folder . '/' . $img;
  661.             if (unlink($img)) {
  662.                 return 'deleted';
  663.             }
  664.         } else {
  665.             $this->goHome();
  666.         }
  667.  
  668. //        return \GuzzleHttp\json_encode(Yii::$app->request->get());
  669.  
  670.     }
  671.  
  672. //    img delete
  673.     public function actionEditMyCar($id)
  674.     {
  675.  
  676.         $addcarform = new \app\models\AddCar();
  677.         $model = Cars::find()->where(['id' => $id])->one();
  678.         return $this->render('editMyCar', [
  679.             'addcarform' => $addcarform,
  680.             'model' => $model,
  681.             'id' => $id
  682.         ]);
  683.     }
  684.  
  685. //TODO поиск ТОЧНЫЙ
  686.     public function actionSearchCarCount()
  687.     {
  688.  
  689.         if (!empty(\Yii::$app->request->get())) {
  690.             $data = \Yii::$app->request->get();
  691.             $features = json_decode($data['SearchCar']['features']);
  692.             $list = $this->list;
  693.             foreach ($features as $key => $feature) {
  694.                 if ($feature) {
  695. //                $list .= '\"'.$key.'\":1|';
  696.                     $list .= '\"' . $key . '\":1|';
  697.                 }
  698.             }
  699.             $list = substr($list, 0, -1);
  700. //        echo  $list;
  701. //        return print_r($data);
  702.  
  703.             $state = $data['SearchCar']['state'] ? $data['SearchCar']['state'] : '';
  704.             $city = $data['SearchCar']['city'] ? $data['SearchCar']['city'] : '';
  705.             $condition = $data['SearchCar']['condition'] ? $data['SearchCar']['condition'] : '';
  706.             $price_from = $data['SearchCar']['price_from'] ? $data['SearchCar']['price_from'] : '';
  707.             $price_to = $data['SearchCar']['price_to'] ? $data['SearchCar']['price_to'] : '99999999';
  708.             $mark = $data['SearchCar']['mark'] ? $data['SearchCar']['mark'] : '';
  709.             $model = $data['SearchCar']['model'] ? $data['SearchCar']['model'] : '';
  710.             $year_from = $data['SearchCar']['year_from'] ? $data['SearchCar']['year_from'] : '';
  711.             $year_to = $data['SearchCar']['year_to'] ? $data['SearchCar']['year_to'] : date('Y');
  712.             $mileage_from = $data['SearchCar']['mileage_from'] ? $data['SearchCar']['mileage_from'] : '0';
  713.             $mileage_to = $data['SearchCar']['mileage_to'] ? $data['SearchCar']['mileage_to'] : '9999999';
  714.             $fuel = $data['SearchCar']['fuel'] ? $data['SearchCar']['fuel'] : '';
  715.             $transmission = $data['SearchCar']['transmission'] ? $data['SearchCar']['transmission'] : '';
  716.             $emise = $data['SearchCar']['emise'] ? $data['SearchCar']['emise'] : '';
  717.             $fuelSpending = $data['SearchCar']['fuelSpending'] ? $data['SearchCar']['fuelSpending'] : '';
  718.             $body = $data['SearchCar']['body'] ? $data['SearchCar']['body'] : '';
  719.             $climate = $data['SearchCar']['climate'] ? $data['SearchCar']['climate'] : '';
  720.             $tires = $data['SearchCar']['tires'] ? $data['SearchCar']['tires'] : '';
  721.             $countOwner = $data['SearchCar']['countOwner'] ? $data['SearchCar']['countOwner'] : '';
  722.             $vendor = $data['SearchCar']['vendor'] ? $data['SearchCar']['vendor'] : '';
  723.  
  724.             if (!empty($state)) {
  725.                 $state = ['=', 'car_region', $state];
  726.             } else {
  727.                 $state = ['like', 'car_region', $state];
  728.             }
  729.  
  730.             if (!empty($mark)) {
  731.                 $mark = ['=', 'mark', $mark];
  732.             } else {
  733.                 $mark = ['like', 'mark', $mark];
  734.             }
  735.  
  736.             if (!empty($transmission)) {
  737.                 $transmission = ['=', 'car_gearbox_type', $transmission];
  738.             } else {
  739.  
  740.             }
  741.  
  742.             $res = Cars::find()
  743.                 ->where($state)
  744.                 ->andWhere(['like', 'car_city', $city])
  745.                 ->andWhere(['like', 'car_condition', $condition])
  746.                 ->andWhere(['between', 'car_price', $price_from, $price_to])
  747.                 ->andWhere($mark)
  748.                 ->andWhere(['like', 'model', $model])
  749.                 ->andWhere(['between', 'car_year', $year_from, $year_to])
  750.                 ->andWhere(['between', 'car_mileage', $mileage_from, $mileage_to])
  751.                 ->andWhere($transmission)
  752.                 ->andWhere(['like', 'car_fuel_type', $fuel])
  753.                 ->andWhere(['like', 'car_environmental_standard', $emise])
  754.                 ->andWhere(['like', 'car_fuel_combined_100km', $fuelSpending])
  755.                 ->andWhere(['like', 'car_body', $body])
  756.                 ->andWhere(['like', 'car_air_conditioning', $climate])
  757.                 ->andWhere(['like', 'car_tires', $tires])
  758.                 ->andWhere(['like', 'car_owner_number', $countOwner])
  759.                 ->andWhere(['like', 'car_sale_type', $vendor])
  760.                 ->andWhere(['=', 'car_sold', 0])
  761. //                ->andWhere( ['=', 'car_features',  $features] )
  762.                 ->count();
  763.  
  764.             if ($res > 16000) {
  765.                 return $res + 100000;
  766.             } else {
  767.                 return $res;
  768.             }
  769.         } else {
  770.             $this->goHome();
  771.         }
  772.  
  773.     }
  774.  
  775.     public function actionSearchCar()
  776.     {
  777.         if (\Yii::$app->request->isGet) {
  778.  
  779.             $region = \Yii::$app->request->get('region');
  780.             $city = \Yii::$app->request->get('city');
  781.             $mark = \Yii::$app->request->get('mark');
  782.             $model = \Yii::$app->request->get('model');
  783.  
  784.  
  785.             if ($model && $mark && $city && $region) {
  786.  
  787.                 $cars = Cars::find()
  788.                     ->where(['=', 'model', $model])
  789.                     ->andWhere(['=', 'mark', $mark])
  790.                     ->andWhere(['=', 'car_city', $city])
  791.                     ->andWhere(['=', 'car_region', $region])
  792.                     ->andWhere(['=', 'car_sold', 0])
  793.                     ->orderBy(['id' => SORT_DESC])
  794.                     ->asArray();
  795.  
  796.                 $query = $cars;
  797.                 $countQuery = clone $query;
  798.                 $pages = new Pagination([
  799.                     'totalCount' => $countQuery->count(),
  800.                     'defaultPageSize' => 12,
  801.                 ]);
  802.                 $models = $query->offset($pages->offset)->limit($pages->limit)->all();
  803.  
  804.                 return $this->render('search', [
  805.                     'cars_res' => $models,
  806.                     'pages' => $pages,
  807.                 ]);
  808.             }
  809.             if ($mark && $city && $region) {
  810.                 $cars = Cars::find()
  811.                     ->where(['=', 'mark', $mark])
  812.                     ->andWhere(['=', 'car_city', $city])
  813.                     ->andWhere(['=', 'car_region', $region])
  814.                     ->andWhere(['=', 'car_sold', 0])
  815.                     ->orderBy(['id' => SORT_DESC])
  816.                     ->asArray();
  817.                 $query = $cars;
  818.                 $countQuery = clone $query;
  819.                 $pages = new Pagination([
  820.                     'totalCount' => $countQuery->count(),
  821.                     'defaultPageSize' => 12,
  822.                 ]);
  823.                 $models = $query->offset($pages->offset)->limit($pages->limit)->all();
  824.  
  825.                 return $this->render('search', [
  826.                     'cars_res' => $models,
  827.                     'pages' => $pages,
  828.                 ]);
  829.             }
  830.  
  831.             if ($city && $region) {
  832.                 $cars = Cars::find()
  833.                     ->where(['=', 'car_city', $city])
  834.                     ->andWhere(['=', 'car_region', $region])
  835.                     ->andWhere(['=', 'car_sold', 0])
  836.                     ->orderBy(['id' => SORT_DESC])
  837.                     ->asArray();
  838.  
  839.                 $query = $cars;
  840.                 $countQuery = clone $query;
  841.                 $pages = new Pagination([
  842.                     'totalCount' => $countQuery->count(),
  843.                     'defaultPageSize' => 12,
  844.                 ]);
  845.                 $models = $query->offset($pages->offset)->limit($pages->limit)->all();
  846.  
  847.                 return $this->render('search', [
  848.                     'cars_res' => $models,
  849.                     'pages' => $pages,
  850.                 ]);
  851.             }
  852.  
  853.         }
  854.  
  855.         if (\Yii::$app->request->isGet) {
  856.  
  857.             $data = \Yii::$app->request->get();
  858.  
  859.             $state = $data['SearchCar']['state'] ? $data['SearchCar']['state'] : '';
  860.             $city = $data['SearchCar']['city'] ? $data['SearchCar']['city'] : '';
  861.             $condition = $data['SearchCar']['condition'] ? $data['SearchCar']['condition'] : '';
  862.             $price_from = $data['SearchCar']['price_from'] ? $data['SearchCar']['price_from'] : '0';
  863.             $price_to = $data['SearchCar']['price_to'] ? $data['SearchCar']['price_to'] : '99999999';
  864.             $mark = $data['SearchCar']['mark'] ? $data['SearchCar']['mark'] : '';
  865.             $model = $data['SearchCar']['model'] ? $data['SearchCar']['model'] : '';
  866.             $year_from = $data['SearchCar']['year_from'] ? $data['SearchCar']['year_from'] : '';
  867.             $year_to = $data['SearchCar']['year_to'] ? $data['SearchCar']['year_to'] : date('Y');
  868.             $mileage_from = $data['SearchCar']['mileage_from'] ? $data['SearchCar']['mileage_from'] : '0';
  869.             $mileage_to = $data['SearchCar']['mileage_to'] ? $data['SearchCar']['mileage_to'] : '9999999';
  870.             $fuel = $data['SearchCar']['fuel'] ? $data['SearchCar']['fuel'] : '';
  871.             $transmission = $data['SearchCar']['transmission'] ? $data['SearchCar']['transmission'] : '';
  872.             $emise = $data['SearchCar']['emise'] ? $data['SearchCar']['emise'] : '';
  873.             $fuelSpending = $data['SearchCar']['fuelSpending'] ? $data['SearchCar']['fuelSpending'] : '';
  874.             $body = $data['SearchCar']['body'] ? $data['SearchCar']['body'] : '';
  875.             $climate = $data['SearchCar']['climate'] ? $data['SearchCar']['climate'] : '';
  876.             $tires = $data['SearchCar']['tires'] ? $data['SearchCar']['tires'] : '';
  877.             $countOwner = $data['SearchCar']['countOwner'] ? $data['SearchCar']['countOwner'] : '';
  878.             $vendor = $data['SearchCar']['vendor'] ? $data['SearchCar']['vendor'] : '';
  879.  
  880.  
  881.             if (!empty($state)) {
  882.                 $state = ['=', 'car_region', $state];
  883.             } else {
  884.                 $state = ['like', 'car_region', $state];
  885.             }
  886.  
  887.             if (!empty($mark)) {
  888.                 $mark = ['=', 'mark', $mark];
  889.             } else {
  890.                 $mark = ['like', 'mark', $mark];
  891.             }
  892.             if (!empty($transmission)) {
  893.                 $transmission = ['=', 'car_gearbox_type', $transmission];
  894.             } else {
  895.  
  896.             }
  897.  
  898.             $orderTypeCookie = Yii::$app->request->cookies;
  899.             if ($orderTypeCookie->has('order-type')) {
  900.                 $orderTypeCookieValue = $orderTypeCookie->getValue('order-type');
  901.             } else {
  902.                 $orderTypeCookieValue = $orderTypeCookie->getValue('order-type', '1');
  903.             }
  904.  
  905.             $order = [
  906.                 'car_is_featured' => SORT_DESC,
  907.                 'id' => SORT_DESC,
  908.             ];
  909.             if ($orderTypeCookieValue == 1) {
  910.                 $order = [
  911.                     'car_is_featured' => SORT_DESC,
  912.                     'id' => SORT_DESC,
  913.                 ];
  914.             }
  915.             if ($orderTypeCookieValue == 2) {
  916.                 $order = [
  917.                     'car_is_featured' => SORT_DESC,
  918.                     'car_price' => SORT_ASC,
  919.                     'id' => SORT_DESC,
  920.                 ];
  921.             }
  922.             if ($orderTypeCookieValue == 3) {
  923.                 $order = [
  924.                     'car_is_featured' => SORT_DESC,
  925.                     'car_price' => SORT_DESC,
  926.                     'id' => SORT_DESC,
  927.                 ];
  928.             }
  929.  
  930.             $cars = Cars::find()
  931.                 ->where($state)
  932.                 ->andWhere(['like', 'car_city', $city])
  933.                 ->andWhere(['like', 'car_condition', $condition])
  934.                 ->andWhere(['between', 'car_price', $price_from, $price_to])
  935.                 ->andWhere($mark)
  936.                 ->andWhere(['like', 'model', $model])
  937.                 ->andWhere(['between', 'car_year', $year_from, $year_to])
  938.                 ->andWhere(['between', 'car_mileage', $mileage_from, $mileage_to])
  939.                 ->andWhere($transmission)
  940.                 ->andWhere(['like', 'car_fuel_type', $fuel])
  941.                 ->andWhere(['like', 'car_environmental_standard', $emise])
  942.                 ->andWhere(['like', 'car_fuel_combined_100km', $fuelSpending])
  943.                 ->andWhere(['like', 'car_body', $body])
  944.                 ->andWhere(['like', 'car_air_conditioning', $climate])
  945.                 ->andWhere(['like', 'car_tires', $tires])
  946.                 ->andWhere(['like', 'car_owner_number', $countOwner])
  947.                 ->andWhere(['like', 'car_sale_type', $vendor])
  948.                 ->andWhere(['=', 'car_sold', 0])
  949.                 ->orderBy(
  950.                     $order
  951.                 )
  952.                 ->asArray();
  953.  
  954.             $query = $cars;
  955.             $countQuery = clone $query;
  956.             $pages = new Pagination([
  957.                 'totalCount' => $countQuery->count(),
  958.                 'defaultPageSize' => 24,
  959.             ]);
  960.             $models = $query->offset($pages->offset)->limit($pages->limit)->all();
  961.  
  962.             return $this->render('search', [
  963.                 'cars_res' => $models,
  964.                 'pages' => $pages,
  965.             ]);
  966.         } else {
  967.             return $this->goHome();
  968.         }
  969.  
  970.     }
  971.  
  972.     public function actionSearchCarTest()
  973.     {
  974.         return $this->render('search-test');
  975.     }
  976.  
  977.     public function actionSearchCarTop()
  978.     {
  979.         $data = \Yii::$app->request->post();
  980.  
  981.         print_r($data);
  982.  
  983.         $cars = Cars::find()
  984.             ->andWhere(['REGEXP', '*', $data])
  985.             ->asArray()
  986.             ->all();
  987.         return $this->render('search', [
  988.             'cars' => $cars
  989.         ]);
  990.     }
  991.  
  992.     public function actionCarDelete()
  993.     {
  994.  
  995.         if (Yii::$app->user->isGuest) {
  996.             $this->goHome();
  997.         } else {
  998.             $id = \Yii::$app->request->get('id');
  999.  
  1000.             $modelExists = Cars::find()->where(['id' => $id])->exists();
  1001.  
  1002.             if ($modelExists) {
  1003.                 $userId = Yii::$app->user->identity->getId();
  1004.                 $model = Cars::find()->where(['id' => $id])->one();
  1005.                 if ($userId === $model->user_id) {
  1006.                     if ($model->delete()) {
  1007.                         $path = 'cars/upload/' . $id;
  1008.                         FileHelper::removeDirectory($path);
  1009.  
  1010.  
  1011.                         $userCountCar = User::find()->where(['id' => Yii::$app->user->getId()])->one();
  1012.                         $userCountCar->max_cars_count += 1;
  1013.                         if ($userCountCar->save()) {
  1014.                             return 'ok';
  1015.                         }
  1016.                     }
  1017.                 } else {
  1018.                     return 'error';
  1019.                 }
  1020.             } else {
  1021.                 $this->goHome();
  1022.             }
  1023.  
  1024.  
  1025.         }
  1026.  
  1027.  
  1028.     }
  1029.  
  1030.     public function actionCarUp()
  1031.     {
  1032.         if (Yii::$app->user->isGuest) {
  1033.             $this->goHome();
  1034.         } else {
  1035.  
  1036.             if (Yii::$app->request->isAjax) {
  1037.  
  1038.                 $id = \Yii::$app->request->get('id');
  1039.  
  1040.                 if (($model = Cars::find()->where(['id' => $id])) !== null) {
  1041.                     $model = Cars::find()->where(['id' => $id])->one();
  1042.                 } else {
  1043.                     $this->goHome();
  1044.                 }
  1045.  
  1046.                 $user = User::find()->where(['id' => Yii::$app->user->identity->getId()])->one();
  1047.  
  1048.  
  1049.                 $user_id = $user->id;
  1050.                 $car_user_id = $model->user_id;
  1051.  
  1052.                 if ($model->car_is_featured == 1) {
  1053.                     $data = [
  1054.                         'up',
  1055.                         $user->features_cars_count
  1056.                     ];
  1057.                     return Json::encode($data);
  1058.                 }
  1059.  
  1060.                 $days_featured = UserAccountManager::getUserFeaturesCarsDays($user->id_account_manager);
  1061.  
  1062.  
  1063.                 if ($user_id === $car_user_id) {
  1064.  
  1065.                     if ($user->features_cars_count >= 1) {
  1066.  
  1067.                         $user->features_cars_count -= 1;
  1068.                         $user->save();
  1069.  
  1070.                         $model->car_is_featured = 1;
  1071.                         $model->car_sold = 0;
  1072.                         $model->save();
  1073.  
  1074.  
  1075.                         $d = new DateTime();
  1076.                         $time_length = $d->getTimestamp();
  1077.                         $exp_date = $d->getTimestamp() + ($days_featured * 86400);
  1078.  
  1079.                         $carFeatured = new CarFeatured();
  1080.                         $carFeatured->user_id = $user_id;
  1081.                         $carFeatured->car_id = $id;
  1082.                         $carFeatured->time_length = $time_length;
  1083.                         $carFeatured->exp_date = $exp_date;
  1084.                         $carFeatured->is_ended = 0;
  1085.                         $carFeatured->save();
  1086.  
  1087.                         $data = [
  1088.                             'ok',
  1089.                             $user->features_cars_count
  1090.                         ];
  1091.                         return Json::encode($data);
  1092.                     } else {
  1093.                         $data = [
  1094.                             'error',
  1095.                             $user->features_cars_count
  1096.                         ];
  1097.                         return Json::encode($data);
  1098.                     }
  1099.                 } else {
  1100.                     $data = [
  1101.                         'error_owner',
  1102.                         $user->features_cars_count
  1103.                     ];
  1104.                     return Json::encode($data);
  1105.                 }
  1106.  
  1107.             } else {
  1108.                 $this->goHome();
  1109.             }
  1110.  
  1111.  
  1112.         }
  1113.     }
  1114.  
  1115.     public function actionCarArchive()
  1116.     {
  1117.         if (Yii::$app->user->isGuest) {
  1118.             $this->goHome();
  1119.         } else {
  1120.  
  1121.             $id = \Yii::$app->request->get('id');
  1122.  
  1123.             if (($model = Cars::find()->where(['id' => $id])) !== null) {
  1124.                 $model = Cars::find()->where(['id' => $id])->one();
  1125.             } else {
  1126.                 $this->goHome();
  1127.             }
  1128.  
  1129.  
  1130.             $user = User::find()->where(['id' => Yii::$app->user->identity->getId()])->one();
  1131.  
  1132.             $user_id = $user->id;
  1133.             $car_user_id = $model->user_id;
  1134.             if ($user_id === $car_user_id) {
  1135.                 if ($model->car_sold == 1) {
  1136.                     $model->car_sold = 0;
  1137.                     $model->save();
  1138.                     return 'sold';
  1139.                 } else if ($model->car_sold == 0) {
  1140.  
  1141.                     $model->car_sold = 1;
  1142.                     $model->car_is_featured = 0;
  1143.                     $model->save();
  1144.  
  1145.                     if (CarFeatured::find()->where(['car_id' => $id])->one() !== null) {
  1146.                         $carF = CarFeatured::find()->where(['car_id' => $id])->one();
  1147.                         $carF->is_ended = 1;
  1148.                         $carF->save();
  1149.                     }
  1150.  
  1151.  
  1152.                     return 'ok';
  1153.                 } else {
  1154.                     return 'error';
  1155.                 }
  1156.             }
  1157.  
  1158.         }
  1159.     }
  1160.  
  1161.     public function actionViewMode($id)
  1162.     {
  1163.         $viewModeCookie = Yii::$app->response->cookies;
  1164.         $viewModeCookie->add(new \yii\web\Cookie([
  1165.                 'name' => 'view-mode',
  1166.                 'value' => $id
  1167.             ])
  1168.         );
  1169.     }
  1170.  
  1171.     public function actionOrderType($id)
  1172.     {
  1173.         $viewModeCookie = Yii::$app->response->cookies;
  1174.         $viewModeCookie->add(new \yii\web\Cookie([
  1175.                 'name' => 'order-type',
  1176.                 'value' => $id
  1177.             ])
  1178.         );
  1179.     }
  1180.  
  1181.  
  1182. //URLS
  1183.     public function actionSearchByRegion($region = null)
  1184.     {
  1185.         if (\Yii::$app->request->isGet) {
  1186.  
  1187.             $region = \Yii::$app->request->get('region-name');
  1188.             $region = str_replace('-', ' ', $region);
  1189. //            $region = substr($region, 0, 5);
  1190.             $region = Regions::find()
  1191.                 ->select('region_id')
  1192.                 ->where(['like', 'region_name', $region])
  1193.                 ->asArray()
  1194.                 ->one();
  1195.  
  1196.             if (!empty($region)) {
  1197.                 $cars = Cars::find()
  1198.                     ->where(['=', 'car_region', $region['region_id']])
  1199.                     ->andWhere(['=', 'car_sold', 0])
  1200.                     ->orderBy(['id' => SORT_DESC])
  1201.                     ->asArray();
  1202.  
  1203.                 $query = $cars;
  1204.                 $countQuery = clone $query;
  1205.                 $pages = new Pagination([
  1206.                     'totalCount' => $countQuery->count(),
  1207.                     'defaultPageSize' => 12,
  1208.                 ]);
  1209.                 $models = $query->offset($pages->offset)->limit($pages->limit)->all();
  1210.  
  1211.                 return $this->render('search', [
  1212.                     'cars_res' => $models,
  1213.                     'pages' => $pages,
  1214.                 ]);
  1215.             }
  1216.  
  1217.         }
  1218.     }
  1219.  
  1220.     public function actionSearchByCity($city = null)
  1221.     {
  1222.         echo Yii::$app->controller->id . '<br>';
  1223.         echo Yii::$app->controller->action->id . '<br>';
  1224.         print_r(Yii::$app->request->get());
  1225.         if (\Yii::$app->request->isGet) {
  1226.  
  1227.             $city = \Yii::$app->request->get('city-name');
  1228.             $city_ch = str_replace('-', ' ', $city);
  1229.  
  1230.             $city = City::find()
  1231.                 ->select('city_id')
  1232.                 ->where(['like', 'city_name', $city_ch])
  1233.                 ->orWhere(['like', 'city_name', $city])
  1234.                 ->asArray()
  1235.                 ->one();
  1236.  
  1237.             if (!empty($city)) {
  1238.                 $cars = Cars::find()
  1239.                     ->where(['=', 'car_city', $city['city_id']])
  1240.                     ->andWhere(['=', 'car_sold', 0])
  1241.                     ->orderBy(['id' => SORT_DESC])
  1242.                     ->asArray();
  1243.  
  1244.                 $query = $cars;
  1245.                 $countQuery = clone $query;
  1246.                 $pages = new Pagination([
  1247.                     'totalCount' => $countQuery->count(),
  1248.                     'defaultPageSize' => 12,
  1249.                 ]);
  1250.                 $models = $query->offset($pages->offset)->limit($pages->limit)->all();
  1251.  
  1252.                 return $this->render('search', [
  1253.                     'cars_res' => $models,
  1254.                     'pages' => $pages,
  1255.                 ]);
  1256.             }
  1257.  
  1258.         }
  1259.     }
  1260.  
  1261.     public function actionSearchByRegionCityMark($region = null, $city = null, $mark = null)
  1262.     {
  1263.         echo Yii::$app->controller->id . '<br>';
  1264.         echo Yii::$app->controller->action->id . '<br>';
  1265.         print_r(Yii::$app->request->get());
  1266.         if (\Yii::$app->request->isGet) {
  1267.  
  1268.             $region = \Yii::$app->request->get('region-name');
  1269.             $region = str_replace('-', ' ', $region);
  1270.             if (!empty($region)) {
  1271.  
  1272.                 $region = Regions::find()
  1273.                     ->select('region_id')
  1274.                     ->where(['like', 'region_name', $region])
  1275.                     ->asArray()
  1276.                     ->one();
  1277.                 $region = ['=', 'car_region', $region['region_id']];
  1278.             } else {
  1279.                 $region = ['like', 'car_region', ''];
  1280.             }
  1281.  
  1282.             $city = \Yii::$app->request->get('city-name');
  1283.             $city = str_replace('-', ' ', $city);
  1284.             if (!empty($city)) {
  1285.                 $city = City::find()
  1286.                     ->select('city_id')
  1287.                     ->where(['like', 'city_name', $city])
  1288.                     ->asArray()
  1289.                     ->one();
  1290.  
  1291.                 $city = ['=', 'car_city', $city['city_id']];
  1292.             } else {
  1293.                 $city = ['like', 'car_city', ''];
  1294.             }
  1295.  
  1296.  
  1297.             $mark = \Yii::$app->request->get('mark-name');
  1298.             $markId = Cars::getMarkId($mark);
  1299.  
  1300.  
  1301.             if (!empty($markId)) {
  1302.                 $cars = Cars::find()
  1303.                     ->where(['=', 'mark', $markId])
  1304.                     ->andwhere($city)
  1305.                     ->andWhere($region)
  1306.                     ->andWhere(['=', 'car_sold', 0])
  1307.                     ->orderBy(['id' => SORT_DESC])
  1308.                     ->asArray();
  1309.  
  1310.                 $query = $cars;
  1311.                 $countQuery = clone $query;
  1312.                 $pages = new Pagination([
  1313.                     'totalCount' => $countQuery->count(),
  1314.                     'defaultPageSize' => 12,
  1315.                 ]);
  1316.                 $models = $query->offset($pages->offset)->limit($pages->limit)->all();
  1317.  
  1318.                 return $this->render('search', [
  1319.                     'cars_res' => $models,
  1320.                     'pages' => $pages,
  1321.                 ]);
  1322.             } else {
  1323.                 $this->redirect(['car/search-car']);
  1324.             }
  1325.  
  1326.         }
  1327.     }
  1328.  
  1329.     public function actionSearchByRegionCityMarkModel($mark = null, $model = null, $region = null, $city = null)
  1330.     {
  1331.  
  1332.         echo Yii::$app->controller->id . '<br>';
  1333.         echo Yii::$app->controller->action->id . '<br>';
  1334.         print_r(Yii::$app->request->get());
  1335.         if (\Yii::$app->request->isGet) {
  1336.  
  1337.             $region = \Yii::$app->request->get('region-name');
  1338.             $region = str_replace('-', ' ', $region);
  1339.  
  1340.             if (!empty($region)) {
  1341.  
  1342.                 $region = Regions::find()
  1343.                     ->select('region_id')
  1344.                     ->where(['like', 'region_name', $region])
  1345.                     ->asArray()
  1346.                     ->one();
  1347.                 $region = ['=', 'car_region', $region['region_id']];
  1348.             } else {
  1349.                 $region = ['like', 'car_region', ''];
  1350.             }
  1351.  
  1352.  
  1353.             $city = \Yii::$app->request->get('city-name');
  1354.             $city = str_replace('-', ' ', $city);
  1355.  
  1356.             if (!empty($city)) {
  1357.                 $city = City::find()
  1358.                     ->select('city_id')
  1359.                     ->where(['like', 'city_name', $city])
  1360.                     ->asArray()
  1361.                     ->one();
  1362.  
  1363.                 $city = ['=', 'car_city', $city['city_id']];
  1364.             } else {
  1365.                 $city = ['like', 'car_city', ''];
  1366.             }
  1367.  
  1368.             $mark = \Yii::$app->request->get('mark-name');
  1369.             $markId = Cars::getMarkId($mark);
  1370.  
  1371.             $model = \Yii::$app->request->get('model-name');
  1372.  
  1373.             $res = CarModel::find()
  1374.                 ->select('id_car_model')
  1375.                 ->where(['name' => $model])
  1376.                 ->andWhere(['id_car_mark' => $markId])
  1377.                 ->one();
  1378.             $modelId = $res['id_car_model'];
  1379.  
  1380.  
  1381.             if (!empty($modelId)) {
  1382.                 $cars = Cars::find()
  1383.                     ->where($city)
  1384.                     ->andWhere($region)
  1385.                     ->andWhere(['=', 'mark', $markId])
  1386.                     ->andWhere(['=', 'model', $modelId])
  1387.                     ->andWhere(['=', 'car_sold', 0])
  1388.                     ->orderBy(['id' => SORT_DESC])
  1389.                     ->asArray();
  1390.  
  1391.                 $query = $cars;
  1392.                 $countQuery = clone $query;
  1393.                 $pages = new Pagination([
  1394.                     'totalCount' => $countQuery->count(),
  1395.                     'defaultPageSize' => 12,
  1396.                 ]);
  1397.                 $models = $query->offset($pages->offset)->limit($pages->limit)->all();
  1398.  
  1399.                 return $this->render('search', [
  1400.                     'cars_res' => $models,
  1401.                     'pages' => $pages,
  1402.                 ]);
  1403.             }
  1404.  
  1405.         }
  1406.     }
  1407.  
  1408.     public function actionSearchByMark($mark = null, $model = null)
  1409.     {
  1410.         echo Yii::$app->controller->id . '<br>';
  1411.         echo Yii::$app->controller->action->id . '<br>';
  1412.         print_r(Yii::$app->request->get());
  1413.         if (\Yii::$app->request->isGet) {
  1414.  
  1415.             $mark = \Yii::$app->request->get('mark-name');
  1416.             $markId = Cars::getMarkId($mark);
  1417.  
  1418.             $model = \Yii::$app->request->get('model-name');
  1419.             $modelId = Cars::getModelId($model);
  1420.  
  1421.             if (!empty($modelId)) {
  1422.                 $modelId = ['=', 'model', $modelId];
  1423.             } else {
  1424.                 $modelId = ['like', 'model', ''];
  1425.             }
  1426.  
  1427.  
  1428.             if (!empty($markId)) {
  1429.                 $cars = Cars::find()
  1430.                     ->where(['=', 'mark', $markId])
  1431.                     ->andwhere($modelId)
  1432.                     ->andWhere(['=', 'car_sold', 0])
  1433.                     ->orderBy(['id' => SORT_DESC])
  1434.                     ->asArray();
  1435.  
  1436.                 $query = $cars;
  1437.                 $countQuery = clone $query;
  1438.                 $pages = new Pagination([
  1439.                     'totalCount' => $countQuery->count(),
  1440.                     'defaultPageSize' => 12,
  1441.                 ]);
  1442.                 $models = $query->offset($pages->offset)->limit($pages->limit)->all();
  1443.  
  1444.                 return $this->render('search', [
  1445.                     'cars_res' => $models,
  1446.                     'pages' => $pages,
  1447.                 ]);
  1448.             }
  1449.             else{
  1450.                 echo 'error';
  1451.             }
  1452.  
  1453.         }
  1454.         else{
  1455.             echo 'error';
  1456.         }
  1457.     }
  1458. //URLS
  1459.  
  1460. //FAV CARS
  1461.     public function actionAddFav($id)
  1462.     {
  1463.  
  1464.         $user_id = Yii::$app->user->getId();
  1465.  
  1466.         $res = FavoritesCars::find()
  1467.             ->where(['id_car' => $id])
  1468.             ->andWhere(['id_user' => $user_id])
  1469.             ->one();
  1470.  
  1471.         if ($res) {
  1472.             FavoritesCars::find()
  1473.                 ->where(['id_car' => $id])
  1474.                 ->andWhere(['id_user' => $user_id])
  1475.                 ->one()->delete();
  1476.             return false;
  1477.         }
  1478.  
  1479.         $model = new FavoritesCars();
  1480.         $model->id_car = $id;
  1481.         $model->id_user = $user_id;
  1482.         $model->save();
  1483.         return true;
  1484.     }
  1485.  
  1486. //FAV CARS
  1487.     public function actionEmptyCars()
  1488.     {
  1489.  
  1490.         $cars = Cars::find()->asArray()->where(['user_id' => 2])->all();
  1491.  
  1492.         foreach ($cars as $car) {
  1493.  
  1494.             $pathImgs = Yii::getAlias('@frontend/web/cars/upload/' . $car['id'] . '/mini/');
  1495.  
  1496.             if (is_dir($pathImgs)) {
  1497.                 $countImg = \yii\helpers\FileHelper::findFiles($pathImgs);
  1498.                 $countImg = count($countImg);
  1499.  
  1500.                 if ($countImg <= 0) {
  1501.                     $car = Cars::find()->where(['id' => $car['id']])->one();
  1502.                     $car->car_sold = 1;
  1503.                     $car->save();
  1504.                 }
  1505.             }
  1506.         }
  1507.  
  1508.  
  1509.     }
  1510.  
  1511. //    public function actionAllEmpty(){
  1512. //        $cars = Cars::find()
  1513. //            ->where(['car_phone' => ''])
  1514. //            ->andWhere(['user_id' => 217])
  1515. //            ->andWhere(['autobaza' => 1])
  1516. //            ->all();
  1517. //
  1518. //        echo 'Машин без Телефонов :' . count($cars) . '<br>';
  1519. //
  1520. //        $cars = Cars::find()
  1521. //            ->where(['car_region' => ''])
  1522. //            ->andWhere(['user_id' => 217])
  1523. //            ->andWhere(['autobaza' => 1])
  1524. //            ->all();
  1525. //
  1526. //        echo 'Машин без Региона :' . count($cars) . '<br>';
  1527. //
  1528. //        $cars = Cars::find()
  1529. //            ->select('id')
  1530. //            ->where(['car_city' => ''])
  1531. //            ->andWhere(['autobaza' => 1])
  1532. //            ->limit(20)
  1533. //            ->asArray()
  1534. //            ->all();
  1535. //
  1536. //        print_r($cars);
  1537. //
  1538. //        echo 'Машин без Города или не отнесен к какому-либо региону :' . count($cars) . '<br>';
  1539. //
  1540. //        $cars = Cars::find()
  1541. //            ->where(['autobaza_id' => ''])
  1542. //            ->andWhere(['user_id' => 217])
  1543. //            ->all();
  1544. //
  1545. //        echo 'Кол-во машин с пустым названием салонов:' . count($cars) . '<br>';
  1546. //
  1547. //    }
  1548.  
  1549.     public function actionSearchAjax(){
  1550.  
  1551.         $cars = Cars::find()->limit(20)->all();
  1552.  
  1553.         return $this->render('search-ajax', [
  1554.             'cars_res' => $cars,
  1555.         ]);
  1556.  
  1557.     }
  1558.     public function actionCarsAjax(){
  1559.         Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  1560.         $car = <<<'TAG'
  1561. <div class="col-md-4 animated fadeIn">
  1562.                                 <a target="_blank" data-pjax="0" class="list-auto__a" href="/car/show/moravskoslezsky-kraj/ostrava-mesto/volkswagen/golf/2369">
  1563.                                     <div class="car-item">
  1564.                                                                                 <!--                                        -->
  1565.                                         <div class="owl-carousel owl-theme owl-car owl-loaded owl-drag">
  1566.  
  1567.                                                                                                                                                                                                                                                    
  1568.                                                    
  1569.                                                                                            
  1570.                                         <div class="owl-stage-outer"><div class="owl-stage" style="transform: translate3d(-548px, 0px, 0px); transition: 0s; width: 1370px;"><div class="owl-item cloned" style="width: 274px;"><div class="item">
  1571.                                                             <img src="/cars/upload/2369/mini/1.jpg" alt="Volkswagen Golf" class="img-responsive">
  1572.                                                         </div></div><div class="owl-item cloned" style="width: 274px;"><div class="item">
  1573.                                                             <img src="/cars/upload/2369/mini/1.jpg" alt="Volkswagen Golf" class="img-responsive">
  1574.                                                         </div></div><div class="owl-item active" style="width: 274px;"><div class="item">
  1575.                                                             <img src="/cars/upload/2369/mini/1.jpg" alt="Volkswagen Golf" class="img-responsive">
  1576.                                                         </div></div><div class="owl-item cloned" style="width: 274px;"><div class="item">
  1577.                                                             <img src="/cars/upload/2369/mini/1.jpg" alt="Volkswagen Golf" class="img-responsive">
  1578.                                                         </div></div><div class="owl-item cloned" style="width: 274px;"><div class="item">
  1579.                                                             <img src="/cars/upload/2369/mini/1.jpg" alt="Volkswagen Golf" class="img-responsive">
  1580.                                                         </div></div></div></div><div class="owl-nav disabled"><div class="owl-prev">prev</div><div class="owl-next">next</div></div><div class="owl-dots disabled"><div class="owl-dot active"><span></span></div></div></div>
  1581.  
  1582.  
  1583.                                         <!--                                        <img src="/cars/upload/-->
  1584.                                         <!--/1.jpg" alt="" class="img-responsive">-->
  1585.                                         <div class="car-item__bottom">
  1586.                                             <p class="car-item__model">Volkswagen Golf</p>
  1587.                                             <p class="car-item__addinfo">2004,
  1588.                                                 Benzín                                                , 110.000 km</p>
  1589.                                             <p class="car-item__city"><i class="glyphicon glyphicon-map-marker" aria-hidden="true"></i> Moravskoslezský kraj , Ostrava-město                                            </p>
  1590.                                            
  1591.                                             <p class="car-item__date">30/08/2017 </p>
  1592.                                             <p class="car-item__price">
  1593.                                                                                                     <i data-fav="" data-car-id="2369" class="isfav fa fa-heart-o" aria-hidden="true"></i>
  1594.                                                                                                 115 000Kč
  1595.                                             </p>
  1596.                                         </div>
  1597.                                     </div>
  1598.                                 </a>
  1599.                             </div>
  1600. <div class="col-md-4 animated fadeIn">
  1601.                                 <a target="_blank" data-pjax="0" class="list-auto__a" href="/car/show/moravskoslezsky-kraj/ostrava-mesto/volkswagen/golf/2369">
  1602.                                     <div class="car-item">
  1603.                                                                                 <!--                                        -->
  1604.                                         <div class="owl-carousel owl-theme owl-car owl-loaded owl-drag">
  1605.  
  1606.                                                                                                                                                                                                                                                    
  1607.                                                    
  1608.                                                                                            
  1609.                                         <div class="owl-stage-outer"><div class="owl-stage" style="transform: translate3d(-548px, 0px, 0px); transition: 0s; width: 1370px;"><div class="owl-item cloned" style="width: 274px;"><div class="item">
  1610.                                                             <img src="/cars/upload/2369/mini/1.jpg" alt="Volkswagen Golf" class="img-responsive">
  1611.                                                         </div></div><div class="owl-item cloned" style="width: 274px;"><div class="item">
  1612.                                                             <img src="/cars/upload/2369/mini/1.jpg" alt="Volkswagen Golf" class="img-responsive">
  1613.                                                         </div></div><div class="owl-item active" style="width: 274px;"><div class="item">
  1614.                                                             <img src="/cars/upload/2369/mini/1.jpg" alt="Volkswagen Golf" class="img-responsive">
  1615.                                                         </div></div><div class="owl-item cloned" style="width: 274px;"><div class="item">
  1616.                                                             <img src="/cars/upload/2369/mini/1.jpg" alt="Volkswagen Golf" class="img-responsive">
  1617.                                                         </div></div><div class="owl-item cloned" style="width: 274px;"><div class="item">
  1618.                                                             <img src="/cars/upload/2369/mini/1.jpg" alt="Volkswagen Golf" class="img-responsive">
  1619.                                                         </div></div></div></div><div class="owl-nav disabled"><div class="owl-prev">prev</div><div class="owl-next">next</div></div><div class="owl-dots disabled"><div class="owl-dot active"><span></span></div></div></div>
  1620.  
  1621.  
  1622.                                         <!--                                        <img src="/cars/upload/-->
  1623.                                         <!--/1.jpg" alt="" class="img-responsive">-->
  1624.                                         <div class="car-item__bottom">
  1625.                                             <p class="car-item__model">Volkswagen Golf</p>
  1626.                                             <p class="car-item__addinfo">2004,
  1627.                                                 Benzín                                                , 110.000 km</p>
  1628.                                             <p class="car-item__city"><i class="glyphicon glyphicon-map-marker" aria-hidden="true"></i> Moravskoslezský kraj , Ostrava-město                                            </p>
  1629.                                            
  1630.                                             <p class="car-item__date">30/08/2017 </p>
  1631.                                             <p class="car-item__price">
  1632.                                                                                                     <i data-fav="" data-car-id="2369" class="isfav fa fa-heart-o" aria-hidden="true"></i>
  1633.                                                                                                 115 000Kč
  1634.                                             </p>
  1635.                                         </div>
  1636.                                     </div>
  1637.                                 </a>
  1638.                             </div>
  1639. <div class="col-md-4 animated fadeIn">
  1640.                                 <a target="_blank" data-pjax="0" class="list-auto__a" href="/car/show/moravskoslezsky-kraj/ostrava-mesto/volkswagen/golf/2369">
  1641.                                     <div class="car-item">
  1642.                                                                                 <!--                                        -->
  1643.                                         <div class="owl-carousel owl-theme owl-car owl-loaded owl-drag">
  1644.  
  1645.                                                                                                                                                                                                                                                    
  1646.                                                    
  1647.                                                                                            
  1648.                                         <div class="owl-stage-outer"><div class="owl-stage" style="transform: translate3d(-548px, 0px, 0px); transition: 0s; width: 1370px;"><div class="owl-item cloned" style="width: 274px;"><div class="item">
  1649.                                                             <img src="/cars/upload/2369/mini/1.jpg" alt="Volkswagen Golf" class="img-responsive">
  1650.                                                         </div></div><div class="owl-item cloned" style="width: 274px;"><div class="item">
  1651.                                                             <img src="/cars/upload/2369/mini/1.jpg" alt="Volkswagen Golf" class="img-responsive">
  1652.                                                         </div></div><div class="owl-item active" style="width: 274px;"><div class="item">
  1653.                                                             <img src="/cars/upload/2369/mini/1.jpg" alt="Volkswagen Golf" class="img-responsive">
  1654.                                                         </div></div><div class="owl-item cloned" style="width: 274px;"><div class="item">
  1655.                                                             <img src="/cars/upload/2369/mini/1.jpg" alt="Volkswagen Golf" class="img-responsive">
  1656.                                                         </div></div><div class="owl-item cloned" style="width: 274px;"><div class="item">
  1657.                                                             <img src="/cars/upload/2369/mini/1.jpg" alt="Volkswagen Golf" class="img-responsive">
  1658.                                                         </div></div></div></div><div class="owl-nav disabled"><div class="owl-prev">prev</div><div class="owl-next">next</div></div><div class="owl-dots disabled"><div class="owl-dot active"><span></span></div></div></div>
  1659.  
  1660.  
  1661.                                         <!--                                        <img src="/cars/upload/-->
  1662.                                         <!--/1.jpg" alt="" class="img-responsive">-->
  1663.                                         <div class="car-item__bottom">
  1664.                                             <p class="car-item__model">Volkswagen Golf</p>
  1665.                                             <p class="car-item__addinfo">2004,
  1666.                                                 Benzín                                                , 110.000 km</p>
  1667.                                             <p class="car-item__city"><i class="glyphicon glyphicon-map-marker" aria-hidden="true"></i> Moravskoslezský kraj , Ostrava-město                                            </p>
  1668.                                            
  1669.                                             <p class="car-item__date">30/08/2017 </p>
  1670.                                             <p class="car-item__price">
  1671.                                                                                                     <i data-fav="" data-car-id="2369" class="isfav fa fa-heart-o" aria-hidden="true"></i>
  1672.                                                                                                 115 000Kč
  1673.                                             </p>
  1674.                                         </div>
  1675.                                     </div>
  1676.                                 </a>
  1677.                             </div>
  1678. TAG;
  1679.  
  1680.         return $car;
  1681.     }
  1682. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement