Exemplo n.º 1
0
 public function postAdEdit(Request $request)
 {
     $rules = ['ad_title' => 'required|max:255', 'category_id' => 'required|integer|not_in:0', 'ad_description' => 'required|min:50', 'type_id' => 'required|integer|not_in:0', 'ad_image.*' => 'mimes:jpeg,bmp,png|max:300', 'location_id' => 'required|integer|not_in:0', 'ad_puslisher_name' => 'required|string|max:255', 'ad_email' => 'required|email|max:255', 'policy_agree' => 'required'];
     $messages = ['require_one_of_array' => 'You need to upload at least one ad pic.'];
     $validator = Validator::make($request->all(), $rules, $messages);
     /**
      * type 1 common ads validation
      */
     $validator->sometimes(['ad_price_type_1'], 'required|numeric|not_in:0', function ($input) {
         if ($input->category_type == 1 && $input->price_radio == 1) {
             return true;
         }
         return false;
     });
     $validator->sometimes(['condition_id_type_1'], 'required|integer|not_in:0', function ($input) {
         return $input->category_type == 1 ? 1 : 0;
     });
     /**
      * type 2 estate ads validation
      */
     $validator->sometimes(['ad_price_type_2'], 'required|numeric|not_in:0', function ($input) {
         if ($input->category_type == 2) {
             return true;
         }
         return false;
     });
     $validator->sometimes(['estate_type_id'], 'required|integer|not_in:0', function ($input) {
         return $input->category_type == 2 ? 1 : 0;
     });
     $validator->sometimes(['estate_sq_m'], 'required|numeric|not_in:0', function ($input) {
         return $input->category_type == 2 ? 1 : 0;
     });
     /**
      * type 3 cars ads validation
      */
     $validator->sometimes(['car_brand_id'], 'required|integer|not_in:0', function ($input) {
         return $input->category_type == 3 ? 1 : 0;
     });
     $validator->sometimes(['car_model_id'], 'required|integer|not_in:0', function ($input) {
         return $input->category_type == 3 ? 1 : 0;
     });
     $validator->sometimes(['car_engine_id'], 'required|integer|not_in:0', function ($input) {
         return $input->category_type == 3 ? 1 : 0;
     });
     $validator->sometimes(['car_transmission_id'], 'required|integer|not_in:0', function ($input) {
         return $input->category_type == 3 ? 1 : 0;
     });
     $validator->sometimes(['car_modification_id'], 'required|integer|not_in:0', function ($input) {
         return $input->category_type == 3 ? 1 : 0;
     });
     $validator->sometimes(['car_year'], 'required|integer|not_in:0', function ($input) {
         return $input->category_type == 3 ? 1 : 0;
     });
     $validator->sometimes(['car_kilometeres'], 'required|integer|not_in:0', function ($input) {
         return $input->category_type == 3 ? 1 : 0;
     });
     $validator->sometimes(['car_condition_id'], 'required|integer|not_in:0', function ($input) {
         return $input->category_type == 3 ? 1 : 0;
     });
     $validator->sometimes(['condition_id_type_3'], 'required|integer|not_in:0', function ($input) {
         return $input->category_type == 3 ? 1 : 0;
     });
     $validator->sometimes(['ad_price_type_3'], 'required|numeric|not_in:0', function ($input) {
         return $input->category_type == 3 ? 1 : 0;
     });
     if ($validator->fails()) {
         $this->throwValidationException($request, $validator);
     }
     $ad_data = $request->all();
     //fill aditional fields
     $ad_data['user_id'] = $request->user()->user_id;
     $ad_data['ad_publish_date'] = date('Y-m-d H:i:s');
     $ad_data['ad_valid_until'] = date('Y-m-d', mktime(null, null, null, date('m') + 1, date('d'), date('Y')));
     $ad_data['ad_ip'] = Util::getRemoteAddress();
     $ad_data['ad_description'] = Util::nl2br(strip_tags($ad_data['ad_description']));
     switch ($ad_data['category_type']) {
         case 1:
             if ($ad_data['price_radio'] == 1) {
                 $ad_data['ad_price'] = $ad_data['ad_price_type_1'];
                 $ad_data['ad_free'] = 0;
             } else {
                 $ad_data['ad_price'] = 0;
                 $ad_data['ad_free'] = 1;
             }
             $ad_data['condition_id'] = $ad_data['condition_id_type_1'];
             break;
         case 2:
             $ad_data['ad_price'] = $ad_data['ad_price_type_2'];
             $ad_data['condition_id'] = $ad_data['condition_id_type_2'];
             break;
         case 3:
             $ad_data['ad_price'] = $ad_data['ad_price_type_3'];
             $ad_data['condition_id'] = $ad_data['condition_id_type_3'];
             break;
     }
     $ad_data['ad_description_hash'] = md5($ad_data['ad_description']);
     //save ad
     $ad = Ad::find($ad_data['ad_id']);
     $ad->update($ad_data);
     //upload and fix ad images
     $ad_image = Input::file('ad_image');
     if (!empty(array_filter($ad_image))) {
         //delete current image
         if (!empty($ad->ad_pic)) {
             @unlink(public_path('uf/adata/') . '740_' . $ad->ad_pic);
             @unlink(public_path('uf/adata/') . '1000_' . $ad->ad_pic);
         }
         $more_pics = AdPic::where('ad_id', $ad->ad_id)->get();
         if (!$more_pics->isEmpty()) {
             foreach ($more_pics as $k => $v) {
                 @unlink(public_path('uf/adata/') . '740_' . $v->ad_pic);
                 @unlink(public_path('uf/adata/') . '1000_' . $v->ad_pic);
                 $v->delete();
             }
         }
         //save new images
         $destination_path = public_path('uf/adata/');
         $first_image_uploaded = 0;
         foreach ($ad_image as $k) {
             if (!empty($k) && $k->isValid()) {
                 $file_name = $ad->ad_id . '_' . md5(time() + rand(0, 9999)) . '.' . $k->getClientOriginalExtension();
                 $k->move($destination_path, $file_name);
                 $img = Image::make($destination_path . $file_name);
                 $width = $img->width();
                 $height = $img->height();
                 if ($width == $height || $width > $height) {
                     $img->heighten(1000, function ($constraint) {
                         $constraint->upsize();
                     })->save($destination_path . '1000_' . $file_name);
                 } else {
                     $img->widen(1000, function ($constraint) {
                         $constraint->upsize();
                     })->save($destination_path . '1000_' . $file_name);
                 }
                 if (!$first_image_uploaded) {
                     $img->resizeCanvas(740, 740, 'top')->save($destination_path . '740_' . $file_name);
                     $ad->ad_pic = $file_name;
                     $ad->save();
                     $first_image_uploaded = 1;
                 } else {
                     $adPic = new AdPic();
                     $adPic->ad_id = $ad->ad_id;
                     $adPic->ad_pic = $file_name;
                     $adPic->save();
                 }
                 @unlink($destination_path . $file_name);
             }
         }
     }
     $ad->ad_category_info = $this->category->getParentsByIdFlat($ad->category_id);
     $ad->ad_location_info = $this->location->getParentsByIdFlat($ad->location_id);
     $ad->pics = AdPic::where('ad_id', $ad->ad_id)->get();
     $ad->same_ads = Ad::where([['ad_description_hash', $ad->ad_description_hash], ['ad_id', '<>', $ad->ad_id]])->get();
     //send info mail
     Mail::send('emails.ad_edit', ['user' => $request->user(), 'ad' => $ad], function ($m) use($request) {
         $m->from('*****@*****.**', 'dclassifieds ad edit');
         $m->to($request->user()->email)->subject('Your ad is edited!');
     });
     //send control mail
     Mail::send('emails.control_ad_activation', ['user' => $request->user(), 'ad' => $ad], function ($m) use($request) {
         $m->from('*****@*****.**', '[CONTROL] dclassifieds');
         $m->to('*****@*****.**')->to('*****@*****.**')->subject('[CONTROL] dclasssifieds ad edit');
     });
     Cache::flush();
     //set flash message and return
     session()->flash('message', 'Your ad is saved.');
     return redirect()->back();
 }