public function save(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', 'location_id' => 'required|integer|not_in:0', 'ad_puslisher_name' => 'required|string|max:255', 'ad_email' => 'required|email|max:255'];
     $validator = Validator::make($request->all(), $rules);
     /**
      * 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['ad_description'] = Util::nl2br(strip_tags($ad_data['ad_description']));
     if (!isset($ad_data['ad_active'])) {
         $ad_data['ad_active'] = 0;
     } else {
         $ad_data['ad_active'] = 1;
     }
     if (!isset($ad_data['ad_promo'])) {
         $ad_data['ad_promo'] = 0;
         $ad_data['ad_promo_until'] = NULL;
     } else {
         $ad_data['ad_promo'] = 1;
     }
     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);
     /**
      * clear cache, set message, redirect to list
      */
     Cache::flush();
     session()->flash('message', trans('admin_common.Ad saved'));
     return redirect(url('admin/ad'));
 }
 /**
  * Bootstrap any application services.
  *
  * @return void
  */
 public function boot()
 {
     /**
      * Register custom Validator
      */
     Validator::extend('require_one_of_array', function ($attribute, $value, $parameters, $validator) {
         if (!is_array($value)) {
             return false;
         }
         foreach ($value as $k => $v) {
             if (!empty($v)) {
                 return true;
             }
         }
         return false;
     });
     /**
      * Get Admin Menu from DB
      */
     view()->composer('admin.layout.admin_index_layout', function ($view) {
         $adminMenu = new AdminMenu();
         $view->with('adminMenu', $adminMenu->getMenu());
         $controller = Util::getController();
         $view->with('controller', $controller);
         $view->with('controller_parent_id', $adminMenu->getParent($controller));
     });
     /**
      * Get Settings From DB
      */
     $settings = Cache::rememberForever('settings', function () {
         return Settings::all();
     });
     if (!$settings->isEmpty()) {
         foreach ($settings as $k => $v) {
             config(['dc.' . $v->setting_name => $v->setting_value]);
         }
     }
     /**
      * Get Custom Pages From DB, Get Central Banner
      */
     view()->composer('layout.index_layout', function ($view) {
         //header menu
         $headerMenu = Cache::rememberForever('headerMenu', function () {
             return Page::select('page_title', 'page_slug')->where('page_position', Page::HEADER_MENU)->where('page_active', 1)->orderBy('page_ord', 'ASC')->get();
         });
         $view->with('headerMenu', $headerMenu);
         //footer menu
         $footerMenu = Cache::rememberForever('footerMenu', function () {
             return Page::select('page_title', 'page_slug')->where('page_position', Page::FOOTER_MENU)->where('page_active', 1)->orderBy('page_ord', 'ASC')->get();
         });
         $view->with('footerMenu', $footerMenu);
         //get central banner if any
         $today = date('Y-m-d');
         $centralBanner = Banner::where('banner_active_from', '<=', $today)->where('banner_active_to', '>=', $today)->where('banner_position', Banner::BANNER_POSITION_LIST)->orderByRaw('rand()')->take(1)->first();
         if (!empty($centralBanner)) {
             $centralBanner->increment('banner_num_views');
         }
         $view->with('centralBanner', $centralBanner);
     });
     /**
      * get ad detail/ad contact banner
      */
     view()->composer(['ad.detail', 'ad.contact'], function ($view) {
         $today = date('Y-m-d');
         $adDetailBanner = Banner::where('banner_active_from', '<=', $today)->where('banner_active_to', '>=', $today)->where('banner_position', Banner::BANNER_POSITION_DETAIL)->orderByRaw('rand()')->take(1)->first();
         if (!empty($adDetailBanner)) {
             $adDetailBanner->increment('banner_num_views');
         }
         $view->with('adDetailBanner', $adDetailBanner);
     });
 }
 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();
 }
 public function saveMailToDbAndSendMail($_current_user_id, $_ad_user_id, $_ad_id, $_text, $_mail_to)
 {
     //generate conversation hash
     $hash_array = array($_current_user_id, $_ad_user_id, $_ad_id);
     sort($hash_array);
     $hash = md5(join('-', $hash_array));
     //save message
     $userMail = new UserMail();
     $userMail->ad_id = $_ad_id;
     $userMail->user_id_from = $_current_user_id;
     $userMail->user_id_to = $_ad_user_id;
     $userMail->mail_text = Util::nl2br(strip_tags($_text));
     $userMail->mail_date = date('Y-m-d H:i:s');
     $userMail->mail_hash = $hash;
     $userMail->save();
     //save message status
     $userMailStatus = new UserMailStatus();
     $userMailStatus->mail_id = $userMail->mail_id;
     $userMailStatus->user_id = $_ad_user_id;
     $userMailStatus->mail_status = UserMailStatus::MAIL_STATUS_UNREAD;
     $userMailStatus->mail_deleted = UserMailStatus::MAIL_STATUS_NOT_DELETED;
     $userMailStatus->mail_hash = $hash;
     $userMailStatus->save();
     //save status for the other user
     $userMailStatus = new UserMailStatus();
     $userMailStatus->mail_id = $userMail->mail_id;
     $userMailStatus->user_id = $_current_user_id;
     $userMailStatus->mail_status = UserMailStatus::MAIL_STATUS_SEND;
     $userMailStatus->mail_deleted = UserMailStatus::MAIL_STATUS_NOT_DELETED;
     $userMailStatus->mail_hash = $hash;
     $userMailStatus->save();
     //send mail to ad publisher
     Mail::send('emails.ad_contact', ['userMail' => $userMail], function ($m) use($_mail_to) {
         $m->from('*****@*****.**', 'dclassifieds ad contact');
         $m->to($_mail_to)->subject('You have new message in DClassifieds');
     });
 }