public function edit(Request $request)
 {
     //get ad id
     $ad_id = $request->id;
     //get ad info
     $ad_detail = $this->ad->getAdDetail($ad_id, 0);
     $ad_detail->ad_price_type_1 = $ad_detail->ad_price_type_2 = $ad_detail->ad_price_type_3 = $ad_detail->ad_price;
     $ad_detail->condition_id_type_1 = $ad_detail->condition_id_type_3 = $ad_detail->condition_id;
     $ad_detail->ad_description = Util::br2nl($ad_detail->ad_description);
     //get ad pics
     $ad_pic = AdPic::where('ad_id', $ad_id)->get();
     $car_model_id = array();
     if (old('car_brand_id')) {
         if (is_numeric(old('car_brand_id')) && old('car_brand_id') > 0) {
             $car_models = CarModel::where('car_brand_id', old('car_brand_id'))->orderBy('car_model_name', 'asc')->get();
             if (!$car_models->isEmpty()) {
                 $car_model_id = array(0 => 'Select Car Model');
                 foreach ($car_models as $k => $v) {
                     $car_model_id[$v->car_model_id] = $v->car_model_name;
                 }
             }
         }
     }
     $ad_detail->ad_category_info = $this->category->getParentsByIdFlat($ad_detail->category_id);
     $ad_detail->pics = AdPic::where('ad_id', $ad_detail->ad_id)->get();
     return view('admin.ad.ad_edit', ['ad_detail' => $ad_detail, 'ad_pic' => $ad_pic, 'c' => $this->category->getAllHierarhy(), 'l' => $this->location->getAllHierarhy(), 'at' => AdType::all(), 'ac' => AdCondition::all(), 'estate_construction_type' => EstateConstructionType::all(), 'estate_furnishing_type' => EstateFurnishingType::all(), 'estate_heating_type' => EstateHeatingType::all(), 'estate_type' => EstateType::all(), 'car_brand_id' => CarBrand::all(), 'car_model_id' => $car_model_id, 'car_engine_id' => CarEngine::all(), 'car_transmission_id' => CarTransmission::all(), 'car_condition_id' => CarCondition::all(), 'car_modification_id' => CarModification::all()]);
 }
 public function import(Request $request)
 {
     /**
      * form is submitted check values and save if needed
      */
     if ($request->isMethod('post')) {
         /**
          * validate data
          */
         $rules = ['csv_file' => 'required'];
         $validator = Validator::make($request->all(), $rules);
         if ($validator->fails()) {
             $this->throwValidationException($request, $validator);
         }
         /**
          * save data if validated
          */
         if ($request->file('csv_file')->isValid()) {
             //rename and move uploaded file
             $csv_file = Input::file('csv_file');
             $tmp_import_name = time() . '_car_brand_import_.' . $csv_file->getClientOriginalExtension();
             $csv_file->move(storage_path() . '/app', $tmp_import_name);
             //read csv
             $csv_data = [];
             if (($handle = fopen(storage_path() . '/app/' . $tmp_import_name, "r")) !== FALSE) {
                 while (($data = fgetcsv($handle, 1000, ",", '"')) !== FALSE) {
                     $csv_data[] = $data;
                 }
                 fclose($handle);
             }
             if (!empty($csv_data)) {
                 //import erros holder
                 $import_error_array = [];
                 foreach ($csv_data as $k => $v) {
                     if (is_array($v)) {
                         $data_to_save = [];
                         //set fields to be imported
                         if (isset($v[0]) && !empty($v[0])) {
                             $data_to_save['car_brand_name'] = trim($v[0]);
                         }
                         if (isset($v[1]) && !empty($v[1])) {
                             $data_to_save['car_brand_active'] = trim($v[1]);
                         }
                         //check if all fields are here
                         if (count($data_to_save) == 2) {
                             try {
                                 CarBrand::create($data_to_save);
                             } catch (\Exception $e) {
                                 $import_error_array[] = trans('admin_common.Possible doublicate') . '<strong>' . trans('admin_common.Car Brand') . '</strong>' . trans('admin_common.on line') . ': ' . join(',', $v) . ' <br />' . trans('admin_common.Error Message') . ': ' . $e->getMessage();
                             }
                         } else {
                             $import_error_array[] = trans('admin_common.Missing data line') . ': ' . join(',', $v);
                         }
                     }
                 }
             } else {
                 session()->flash('message', trans('admin_common.Cant read the csv file.'));
                 return redirect(url('admin/carbrand'));
             }
         }
         /**
          * delete temp file, clear cache, set message, redirect to list
          */
         @unlink(storage_path() . '/app/' . $tmp_import_name);
         Cache::flush();
         if (!empty($import_error_array)) {
             session()->flash('message', trans('admin_common.Car Brands imported with the following errors') . ': <br />' . join('<br />', $import_error_array));
         } else {
             session()->flash('message', trans('admin_common.Car Brands imported'));
         }
         return redirect(url('admin/carbrand'));
     }
     return view('admin.car_brand.car_brand_import');
 }