Overview

Namespaces

  • App
    • Http
      • Controllers
        • Auth
      • Middleware
  • PHP

Classes

  • App\Ad
  • App\AdClick
  • App\AdLocation
  • App\Comment
  • App\Driver
  • App\Emergency
  • App\Http\Controllers\AdController
  • App\Http\Controllers\ApiController
  • App\Http\Controllers\Auth\AuthController
  • App\Http\Controllers\Auth\PasswordController
  • App\Http\Controllers\CommentController
  • App\Http\Controllers\Controller
  • App\Http\Controllers\EmergencyController
  • App\Http\Controllers\NewspaperController
  • App\Http\Controllers\RouteController
  • App\Http\Controllers\TaxiController
  • App\Http\Controllers\UserController
  • App\Http\Kernel
  • App\Http\Middleware\AddHeaders
  • App\Http\Middleware\AdminMiddleware
  • App\Newspaper
  • App\Route
  • App\Tablet
  • App\Taxi
  • App\Taxibase
  • App\User
  • Closure
  • Overview
  • Namespace
  • Class
  • Tree
  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:      * @author Stefano Groenland
 20:      * @return mixed
 21:      *
 22:      * Gets all ads from the database with atleast one location linked to it,
 23:      * and passes them along when making the view.
 24:      */
 25:     public function showAds(){
 26:         $ads = Ad::with('adLocation','adClicks')->get();
 27:         return View::make('/reclames', compact('ads'));
 28:     }
 29: 
 30:     /**
 31:      * @authors Stefano Groenland , Richard Perdaan
 32:      * @return mixed
 33:      *
 34:      * Gets the corresponding advertisement of the route id passed,
 35:      * Gets all locations comma from the database and places them comma delimited in a variable for later use.
 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:      * @author Richard Perdaan
 49:      * @return mixed
 50:      *
 51:      * Makes the reclametoevoegen view.
 52:      */
 53:     public function showAdsAdd(){
 54:         $type = Route::current()->getParameter('type');
 55:         return View::make('/reclametoevoegen', compact('type'));
 56:     }
 57: 
 58:     /**
 59:      * @author Richard Perdaan & Stefano Groenland
 60:      * @param Request $request
 61:      * @return \Illuminate\Http\RedirectResponse
 62:      *
 63:      * Grabs all input values from the request, validates them and if all goes well,
 64:      * The advertisement will be created.
 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:      * @author Richard Perdaan
120:      * @return \Illuminate\Http\RedirectResponse
121:      *
122:      * Gets the id from the route parameter,
123:      * looks for the id in the database. If found the row will be deleted from the Ad table,
124:      * Aswell as the linked rows in the AdLocation table.
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:      * @author Richard Perdaan & Stefano Groenland
141:      * @param Request $request
142:      * @return \Illuminate\Http\RedirectResponse
143:      *
144:      * Uses the route parameter to define which advertisement has to be edited,
145:      * Gets all inputs filled in and checks them for validation.
146:      * If all passed correctly the advertisement gets updated.
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:      * @authors Stefano Groenland
202:      * @param Request $request
203:      * @param $id
204:      * @param $type
205:      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
206:      *
207:      * Grabs the file named 'banner' from the request and uploads it onto the server,
208:      * It updates the corresponding advertisement with the link to the uploaded picture as their banner in the Ad table.
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:                 //$request->session()->flash('alert-danger', 'U heeft geen bestand / geen geldig bestand gekozen om te uploaden, voeg een foto toe.');
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:      * @author Stefano Groenland
262:      * @param $radius
263:      * @param $lat
264:      * @param $lng
265:      * @return string
266:      *
267:      * Uses the geobyte API for nearby cities in a radius arround the lat & long coords of a given location.
268:      */
269:     public function getLocationsInRadius($radius,$lat,$lng){
270: 
271:         $radius = $radius * 0.62137; //km to miles
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:      * @return mixed
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:      * @author Stefano Groenland
298:      * @param $adress
299:      * @return array
300:      *
301:      * Uses the Google maps Geocoding API to convert a location to geodata.
302:      */
303:     public function geoCode($adress){
304:         //We do not need any person filling in more locations so we build an array of all word.
305:         //And only use the first word in the $url
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: 
APIv1 API documentation generated by ApiGen