1: <?php
2:
3: namespace App\Http\Controllers;
4: use Illuminate\Http\Request;
5: use App\Http\Requests;
6: use App\Http\Controllers\Controller;
7: use Route, View;
8: use App\Ad;
9: use App\AdLocation;
10: use App\adClick;
11: use Illuminate\Support\Facades\Validator;
12: use Image as Image;
13: use DB;
14: use Carbon\Carbon;
15:
16: class AdController extends Controller
17: {
18: 19: 20: 21: 22: 23: 24:
25: public function showAds(){
26: $ads = Ad::with('adLocation','adClicks')->get();
27: return View::make('/reclames', compact('ads'));
28: }
29:
30: 31: 32: 33: 34: 35: 36:
37: public function showAdsEdit(){
38: $id = Route::current()->getParameter('id');
39: $type = Route::current()->getParameter('type');
40: $obj = Ad::find($id);
41: return View::make('/reclamewijzigen', compact('id', 'obj', 'type'));
42: }
43:
44: public function showAdsPanel(){
45: return View::make('/reclametype');
46: }
47: 48: 49: 50: 51: 52:
53: public function showAdsAdd(){
54: $type = Route::current()->getParameter('type');
55: return View::make('/reclametoevoegen', compact('type'));
56: }
57:
58: 59: 60: 61: 62: 63: 64: 65:
66: public function addAd(Request $request){
67: $type = Route::current()->getParameter('type');
68:
69: $data = array(
70: 'link' => $request['link'],
71: 'central_location' => $request['location'],
72: 'radius' => $request['radius'],
73: 'type' => $type,
74: 'title' => $request['title']
75: );
76: if($type !== 'center'){
77: array_forget($data,'title');
78: }
79: $rules = array(
80: 'link' => 'required',
81: 'central_location' => 'required',
82: 'radius' => 'required|numeric'
83: );
84: $validator = Validator::make($data, $rules);
85: if ($validator->fails()){
86: return redirect('reclametoevoegen'.$type)->withErrors($validator)->withInput($data);
87: }
88:
89: if($data['radius'] < 10){
90: $data['radius'] = 10;
91: }
92:
93: $advertisement = Ad::create($data);
94: $this->upload($request,$advertisement->id,$type);
95: $dataLocation = array(
96: 'ad_id' => $advertisement->id,
97: 'location' => $request['location']
98: );
99: $geo = $this->geoCode($dataLocation['location']);
100:
101:
102:
103: $in_radius = $this->getLocationsInRadius($data['radius'],$geo[0],$geo[1]);
104:
105: foreach($in_radius as $inbound){
106: $cities[] = array(
107: 'city' => $inbound[1],
108: 'lat' => $inbound[8],
109: 'lng' => $inbound[10]
110: );
111: }
112: AdLocation::insertLocals($advertisement->id,$cities);
113:
114: session()->flash('alert-success', 'reclame ' . $advertisement->link.' toegevoegd.');
115: return redirect()->route('reclames');
116: }
117:
118: 119: 120: 121: 122: 123: 124: 125:
126: public function deleteAd(){
127: $id = Route::current()->getParameter('id');
128: $find = Ad::find($id);
129: $find->delete();
130: if(!$find->logo == ""){
131: unlink($find->logo);
132: }
133: AdLocation::where('ad_id','=',$id)->delete();
134: Adclick::where('ad_id','=',$id)->delete();
135: session()->flash('alert-success', 'reclame ' . $find->link.' verwijderd.');
136: return redirect()->route('reclames');
137: }
138:
139: 140: 141: 142: 143: 144: 145: 146: 147:
148: public function editAd(Request $request){
149: $id = Route::current()->getParameter('id');
150: $type = Route::current()->getParameter('type');
151: $data = array(
152: 'link' => $request['link'],
153: 'central_location' => $request['location'],
154: 'radius' => $request['radius'],
155: 'title' => $request['title']
156: );
157: if($type !== 'center'){
158: array_forget($data,'title');
159: }
160: $rules = array(
161: 'link' => 'required',
162: 'central_location' => 'required',
163: 'radius' => 'required|numeric'
164: );
165:
166: $validator = Validator::make($data, $rules);
167: if ($validator->fails()){
168: return redirect('reclamewijzigen/'.$id.'/'.$type)->withErrors($validator)->withInput($data);
169: }
170:
171: if($data['radius'] < 10){
172: $data['radius'] = 10;
173: }
174:
175: Ad::where('id', '=', $id)->update($data);
176: AdLocation::deleteLocals($id);
177: $this->upload($request,$id, $type);
178:
179: $dataLocation = array(
180: 'ad_id' => $id,
181: 'location' => $request['location']
182: );
183: $geo = $this->geoCode($dataLocation['location']);
184:
185: $in_radius = $this->getLocationsInRadius($data['radius'],$geo[0],$geo[1]);
186:
187: foreach($in_radius as $inbound){
188: $cities[] = array(
189: 'city' => $inbound[1],
190: 'lat' => $inbound[8],
191: 'lng' => $inbound[10]
192: );
193: }
194: AdLocation::insertLocals($id,$cities);
195:
196: $request->session()->flash('alert-success', 'Reclame ' . $request['link'] . ' is veranderd.');
197: return redirect()->route('reclames');
198: }
199:
200: 201: 202: 203: 204: 205: 206: 207: 208: 209:
210: public function upload(Request $request , $id, $type){
211: $fitW = 0;
212: $fitH = 0;
213: switch($type){
214: case "bottom":
215: $fitW = 1280;
216: $fitH = 135;
217: break;
218: case "center":
219: $fitW = 340;
220: $fitH = 200;
221: break;
222: case "side":
223: $fitW = 160;
224: $fitH = 600;
225: break;
226: }
227:
228: $x = $request['x'];
229: $y = $request['y'];
230: $h = $request['h'];
231: $w = $request['w'];
232:
233: $file = array('banner' => $request->file('banner'));
234: $rules = array('banner' => 'required|mimes:jpeg,bmp,png,jpg',);
235: $validator = Validator::make($file, $rules);
236: if ($validator->fails()) {
237: if ($file) {
238:
239: }
240: return redirect('/reclames');
241: } else {
242: if ($request->file('banner')->isValid()) {
243: $destinationPath = 'assets/uploads';
244: $extension = $request->file('banner')->getClientOriginalExtension();
245: $fileName = rand(1111, 9999) . '.' . $extension;
246: $request->file('banner')->move($destinationPath, $fileName);
247: $ava = $destinationPath . '/' . $fileName;
248: $img = Image::make($ava)->fit($fitW, $fitH)->save();
249: $final = $destinationPath . '/' . $img->basename;
250: Ad::uploadPicture($id, $final);
251: return redirect('/reclames');
252:
253: } else {
254: $request->session()->flash('alert-danger', 'Er is een fout opgetreden tijdens het uploaden van uw bestand.');
255: }
256: }
257:
258: }
259:
260: 261: 262: 263: 264: 265: 266: 267: 268:
269: public function getLocationsInRadius($radius,$lat,$lng){
270:
271: $radius = $radius * 0.62137;
272: $url = 'http://gd.geobytes.com/GetNearbyCities?radius='.$radius.'&Latitude='.$lat.'&Longitude='.$lng.'&limit=999';
273:
274: $response_json = file_get_contents($url);
275:
276: $response = json_decode($response_json, true);
277:
278: return $response;
279: }
280:
281:
282:
283: 284: 285:
286: public function showAdStats(){
287:
288: $id = Route::current()->getParameter('id');
289: $ad = Ad::where('id',$id)->first();
290: return View::make('/reclameprofiel', compact('id','ad'));
291: }
292:
293:
294:
295:
296: 297: 298: 299: 300: 301: 302:
303: public function geoCode($adress){
304:
305:
306: $adressArray = str_word_count($adress,1);
307:
308: $url = 'https://maps.googleapis.com/maps/api/geocode/json?address='.$adressArray[0].',NL&key=AIzaSyAKW-_1s45jicXozxFSRolEJpQIFSmC7NM';
309: $response_json = file_get_contents($url);
310:
311: $response = json_decode($response_json, true);
312:
313: if($response['status'] == 'OK'){
314: $lat = $response['results'][0]['geometry']['location']['lat'];
315: $lng = $response['results'][0]['geometry']['location']['lng'];
316:
317: if(!empty($lat) && !empty($lng)){
318: $result = array();
319: array_push($result,$lat,$lng);
320: return $result;
321: }
322: }
323: }
324: }
325: