示例#1
0
 public function getAllHierarhy($_parent_id = null, $_level = 0, $_active = 1)
 {
     $cache_key = __CLASS__ . '_' . __LINE__ . '_' . md5(config('dc.site_domain') . serialize(func_get_args()));
     $ret = Cache::get($cache_key, []);
     if (empty($ret)) {
         $_level++;
         $lquery = $this->where('location_parent_id', $_parent_id)->with('children')->orderBy('location_ord', 'asc')->orderBy('location_name', 'asc');
         if ($_active) {
             $lquery->where('location_active', '=', 1);
         }
         $locationCollection = $lquery->get();
         if (!empty($locationCollection)) {
             foreach ($locationCollection as $k => $v) {
                 $ret[$v->location_id] = array('lid' => $v->location_id, 'title' => $v->location_name, 'slug' => $v->location_slug, 'active' => $v->location_active, 'post_code' => $v->location_post_code, 'ord' => $v->location_ord, 'level' => $_level, 'ad_count' => Ad::where('location_id', $v->location_id)->count());
                 if (!empty($v->location_post_code)) {
                     $ret[$v->location_id]['title'] = $v->location_name . ' [ZIP:' . $v->location_post_code . ']';
                 }
                 if ($v->children->count() > 0) {
                     $ret[$v->location_id]['c'] = $this->getAllHierarhy($v->location_id, $_level, $_active);
                 }
             }
             Cache::put($cache_key, $ret, config('dc.cache_expire'));
         }
     }
     return $ret;
 }
 public function dashboard(Request $request)
 {
     $stat = new \Stdclass();
     $stat->num_ads = Ad::count();
     $stat->num_promo_ads = Ad::where('ad_promo', 1)->count();
     $stat->users = User::count();
     $stat->reports = AdReport::count();
     //get last 10 days ads
     $ads_by_date = Ad::select(DB::raw("count(ad_id) AS ad_count, DATE_FORMAT(ad_publish_date, '%Y-%m-%d') AS date_formated"))->groupBy('date_formated')->orderBy('date_formated', 'desc')->take(10)->get()->toArray();
     $stat->ads_by_date = [];
     if (!empty($ads_by_date)) {
         $stat->ads_by_date = array_reverse($ads_by_date);
     }
     //get last 10 promo days ads
     $promo_ads_by_date = Ad::select(DB::raw("count(ad_id) AS ad_count, DATE_FORMAT(ad_publish_date, '%Y-%m-%d') AS date_formated"))->where('ad_promo', 1)->groupBy('date_formated')->orderBy('date_formated', 'desc')->take(10)->get()->toArray();
     $stat->promo_ads_by_date = [];
     if (!empty($promo_ads_by_date)) {
         $stat->promo_ads_by_date = array_reverse($promo_ads_by_date);
     }
     //get last 10 months ads
     $ads_by_month = Ad::select(DB::raw("count(ad_id) AS ad_count, DATE_FORMAT(ad_publish_date, '%Y-%m') AS date_formated"))->groupBy('date_formated')->orderBy('date_formated', 'desc')->take(10)->get()->toArray();
     $stat->ads_by_month = [];
     if (!empty($ads_by_month)) {
         $stat->ads_by_month = array_reverse($ads_by_month);
     }
     //get last 10 years ads
     $ads_by_year = Ad::select(DB::raw("count(ad_id) AS ad_count, DATE_FORMAT(ad_publish_date, '%Y') AS date_formated"))->groupBy('date_formated')->orderBy('date_formated', 'desc')->take(10)->get()->toArray();
     $stat->ads_by_year = [];
     if (!empty($ads_by_year)) {
         $stat->ads_by_year = array_reverse($ads_by_year);
     }
     return view('admin.dashboard.dashboard', ['stat' => $stat]);
 }
示例#3
0
 public function ads()
 {
     if (Auth::check()) {
         $ads = Ad::where('author_id', Auth::user()->id)->orderBy('created_at', 'desc')->paginate(30);
         return view('profile.ads', ['ads' => $ads]);
     } else {
         return redirect('/');
     }
 }
示例#4
0
 /**
  * Display a listing of the resource.
  *
  * @return Response
  */
 public function getIndex()
 {
     $ads = Ad::paginate(15);
     if (\Request::has('key')) {
         $key = \Request::input('key');
         if (!empty($key)) {
             $ads = Ad::where('title', 'like', '%' . $key . '%')->paginate(15);
         }
     }
     return view('admin.ad.index', compact('ads'));
 }
示例#5
0
 /**
  * Get the ads for search based on type, (longitude, latitude, region) or keyword.
  *
  * @return Json
  */
 public function getSearchAds(Request $request)
 {
     $uid = $request->input('uid');
     $type = $request->input('type');
     $region = $request->input('region', 0.2);
     $lng = $request->input('lng');
     $lat = $request->input('lat');
     $keyword = $request->input('keyword');
     $earthRadius = 6378.138;
     $dlng = 2 * asin(sin($region / (2 * $earthRadius)) / cos(deg2rad($lat)));
     $dlng = rad2deg($dlng);
     $dlat = $region / $earthRadius;
     $dlat = rad2deg($dlat);
     //return 'region='.$region.', lnt='.$lng.', lat='.$lat.', dlng='.$dlng.', dlat='.$dlat;
     if ($uid) {
         $ads = Ad::where('uid', $uid);
         $ads = $ads->where('status', 1)->paginate(10);
         return $ads->toJson();
     } else {
         if ($type) {
             $ads = Ad::where('type', $type);
             if ($lng && $lat) {
                 $ads = $ads->whereBetween('longitude', [$lng - $dlng, $lng + $dlng])->whereBetween('latitude', [$lat - $dlat, $lat + $dlat]);
             }
             if ($keyword) {
                 $ads = $ads->where('title', 'like', '%' . $keyword . '%');
             }
             $ads = $ads->where('status', 1)->paginate(10);
             return $ads->toJson();
         } else {
             if ($lng && $lat) {
                 $ads = Ad::whereBetween('longitude', [$lng - $dlng, $lng + $dlng])->whereBetween('latitude', [$lat - $dlat, $lat + $dlat]);
                 if ($keyword) {
                     $ads = $ads->where('title', 'like', '%' . $keyword . '%');
                 }
                 $ads = $ads->where('status', 1)->paginate(10);
                 return $ads->toJson();
             } else {
                 if ($keyword) {
                     $ads = Ad::where('title', 'like', '%' . $keyword . '%');
                     $ads = $ads->where('status', 1)->paginate(10);
                     return $ads->toJson();
                 }
             }
         }
     }
 }
示例#6
0
 public function getAllHierarhy($_parent_id = null, $_level = 0, $_active = 1)
 {
     $ret = array();
     $_level++;
     $query = $this->where('category_parent_id', $_parent_id)->with('children')->orderBy('category_ord', 'asc');
     if ($_active) {
         $query->where('category_active', '=', 1);
     }
     $categoryCollection = $query->get();
     if (!empty($categoryCollection)) {
         foreach ($categoryCollection as $k => $v) {
             $ret[$v->category_id] = array('cid' => $v->category_id, 'title' => $v->category_title, 'level' => $_level, 'category_type' => $v->category_type, 'ord' => $v->category_ord, 'slug' => $v->category_slug, 'active' => $v->category_active, 'ad_count' => Ad::where('category_id', $v->category_id)->count());
             if ($v->children->count() > 0) {
                 $ret[$v->category_id]['c'] = $this->getAllHierarhy($v->category_id, $_level, $_active);
             }
         }
     }
     return $ret;
 }
 public function deletemainimg(Request $request)
 {
     $id = 0;
     if (isset($request->id)) {
         $id = $request->id;
     }
     //delete
     if (!empty($id)) {
         $ad = Ad::where('ad_id', $id)->first();
         if (!empty($ad)) {
             //delete images
             if (!empty($ad->ad_pic)) {
                 @unlink(public_path('uf/adata/') . '740_' . $ad->ad_pic);
                 @unlink(public_path('uf/adata/') . '1000_' . $ad->ad_pic);
             }
             $ad->ad_pic = '';
             $ad->save();
         }
         //clear cache, set message, redirect to list
         Cache::flush();
         return redirect(url('admin/ad/edit/' . $id));
     }
     return redirect(url('admin/ad/edit/' . $id));
 }
 public function delete(Request $request)
 {
     //users to be deleted
     $data = [];
     //check for single delete
     if (isset($request->id)) {
         $data[] = $request->id;
     }
     //check for mass delete if no single delete
     if (empty($data)) {
         $data = $request->input('user_id');
     }
     //delete
     if (!empty($data)) {
         foreach ($data as $k => $v) {
             $u = User::find($v);
             //get all user ads
             $userAds = Ad::where('user_id', $u->user_id)->get();
             if (!$userAds->isEmpty()) {
                 foreach ($userAds as $ka => $va) {
                     //delete ads images
                     $more_pics = AdPic::where('ad_id', $va->ad_id)->get();
                     if (!$more_pics->isEmpty()) {
                         foreach ($more_pics as $km => $vm) {
                             @unlink(public_path('uf/adata/') . '740_' . $vm->ad_pic);
                             @unlink(public_path('uf/adata/') . '1000_' . $vm->ad_pic);
                             $vm->delete();
                         }
                     }
                     if (!empty($va->ad_pic)) {
                         @unlink(public_path('uf/adata/') . '740_' . $va->ad_pic);
                         @unlink(public_path('uf/adata/') . '1000_' . $va->ad_pic);
                     }
                     $va->delete();
                 }
             }
             //check for avatar and delete if any
             if (!empty($u->avatar)) {
                 @unlink(public_path('uf/udata/') . '100_' . $u->avatar);
             }
             $u->delete();
         }
         //clear cache, set message, redirect to list
         Cache::flush();
         session()->flash('message', trans('admin_common.User and All User Ads Deleted'));
         return redirect(url('admin/user'));
     }
     //nothing for deletion set message and redirect
     session()->flash('message', trans('admin_common.Nothing for deletion'));
     return redirect(url('admin/user'));
 }
 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 mailviewsave(Request $request)
 {
     //get params
     $hash = $request->hash;
     $user_id_from = $request->user_id_from;
     $ad_id = $request->ad_id;
     $current_user_id = $request->user()->user_id;
     //calc hash
     $hash_array = array($current_user_id, $user_id_from, $ad_id);
     sort($hash_array);
     $calculated_hash = md5(join('-', $hash_array));
     //check hash
     if ($calculated_hash != $hash) {
         return redirect(url('mymail'));
     }
     //get ad info
     $ad_detail = Ad::where('ad_active', 1)->findOrFail($ad_id);
     //validate form
     $rules = ['contact_message' => 'required|min:20'];
     $validator = Validator::make($request->all(), $rules);
     if ($validator->fails()) {
         $this->throwValidationException($request, $validator);
     }
     //if user save message
     if ($current_user_id > 0) {
         //get other user info
         $userInfo = $this->user->getUserById($user_id_from);
         //save in db and send mail
         $this->mail->saveMailToDbAndSendMail($current_user_id, $user_id_from, $ad_id, $request->contact_message, $userInfo->email);
         //set flash message and return
         session()->flash('message', 'Your message was send.');
         //clear the cache
         Cache::flush();
     } else {
         //set error flash message and return
         session()->flash('message', 'Ups something is wrong, please try again later or contact our team.');
     }
     return redirect()->back();
 }
示例#11
0
文件: Ad.php 项目: elberd/maoh
 /**
  * Get the ad's similar ads.
  *
  * @return object
  */
 public function getSimilarAds()
 {
     $count = 5;
     $collection = Ad::where('id', '!=', $this->id)->where('category_id', $this->category_id)->orderByRaw('RAND()')->take($count)->get();
     return $collection;
 }