示例#1
0
 public function actionAdd()
 {
     $db = new Query();
     //подключаемся к классу бд
     //делаем выборку городов и регионов
     $query = 'SELECT GC.*,GR.name AS GR_name
         from geobase_city AS GC
         LEFT JOIN geobase_region AS GR ON (GC.region_id=GR.id)
         ORDER BY name ASC
         ';
     $regions = Yii::$app->db->createCommand($query)->queryAll();
     //делаем выборку категорий
     $cats = ModArendaTree::find()->select(['mod_arenda_tree.*', 'images_for_cats.url AS image_url'])->leftJoin('images_for_cats', 'images_for_cats.cat_id=mod_arenda_tree.id')->where(['parent_id' => 0])->andWhere(['active' => '1'])->orderBy(['sort' => SORT_ASC])->asArray()->all();
     $model = new UploadFile();
     if (Yii::$app->request->post('asd')) {
         $ads = new Ads();
         $result = ModArendaTree::findOne(['id' => Yii::$app->request->post('sel_cats')[0]]);
         if ($result->use_name_for_ads == 0) {
             $naame = '';
             foreach (Yii::$app->request->post('sel_cats') as $key => $value) {
                 $m = ModArendaTree::findOne(['id' => $value]);
                 $naame = $naame . $m->name . ' ';
             }
             $ads->name = $naame;
         } else {
             $ads->name = empty(Yii::$app->request->post('ad_name')) ? 'Без названия' : Yii::$app->request->post('ad_name');
         }
         $region_id = GeobaseCity::findOne(['id' => Yii::$app->request->post('sel_regions')]);
         $ads->region_id = $region_id->region_id;
         $ads->city_id = Yii::$app->request->post('sel_regions');
         $ads->min_arenda_hours = Yii::$app->request->post('min_arenda_hours');
         $ads->min_arenda_days = Yii::$app->request->post('min_arenda_days');
         $ads->arenda_price_hour = str_replace(' ', '', Yii::$app->request->post('arenda_price_hour'));
         $ads->arenda_price_day = str_replace(' ', '', Yii::$app->request->post('arenda_price_day'));
         $ads->arenda_price_km = str_replace(' ', '', Yii::$app->request->post('arenda_price_km'));
         $ads->zalog = !Yii::$app->request->post('zalog') ? 0 : str_replace(' ', '', Yii::$app->request->post('zalog'));
         $ads->oplata_nal = Yii::$app->request->post('oplata_nal') ? Yii::$app->request->post('oplata_nal') : 'none';
         $ads->oplata_beznal = Yii::$app->request->post('oplata_beznal') ? Yii::$app->request->post('oplata_beznal') : 'none';
         $ads->video_url = Yii::$app->request->post('video');
         $ads->email = Yii::$app->request->post('email');
         $ads->person = Yii::$app->request->post('person');
         $ads->comment = Yii::$app->request->post('comment');
         $ads->ad_type = Yii::$app->request->post('ad_type');
         if ($ads->save()) {
             $id = Yii::$app->db->getLastInsertID();
             if (Yii::$app->request->post('charact')) {
                 foreach (Yii::$app->request->post('charact') as $key => $value) {
                     $char_for_ads = new CharacteristicsForAds();
                     $char_for_ads->ad_id = $id;
                     $char_for_ads->charact_id = $value;
                     $char_for_ads->save();
                 }
             }
             if (Yii::$app->request->post('charact2')) {
             }
             if (Yii::$app->request->post('sel_cats')) {
                 foreach (Yii::$app->request->post('sel_cats') as $key => $value) {
                     $cats_for_ads = new CatsForAds();
                     $cats_for_ads->ad_id = $id;
                     $cats_for_ads->cat_id = $value;
                     $cats_for_ads->save();
                 }
             }
             if (Yii::$app->request->isPost) {
                 $model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');
                 if (!empty($model->imageFiles)) {
                     if ($model->upload($id)) {
                     }
                 }
             }
             return $this->render('success_add');
         }
     }
     return $this->render('add', ['regions_and_cities' => $regions, 'categories' => $cats, 'model' => $model]);
 }