function topAirports()
 {
     $other = 0;
     $result = array();
     $chart = array();
     $names = array();
     $counter = $this->query()->select(DB::raw('airport_id, count(airport_id) as counter'))->groupBy('airport_id')->whereNotNull('airport_id')->orderBy('counter', 'DESC')->lists('counter', 'airport_id');
     $other = $this->_total - array_sum($counter);
     if (count($counter) > 0) {
         $namesRaw = Airport::with('country')->whereIn('icao', array_keys($counter))->get();
         foreach ($namesRaw as $airport) {
             $names[$airport->icao] = $airport;
         }
         foreach ($counter as $key => $flights) {
             if (count($result) < 5 && array_key_exists($key, $names)) {
                 $percentage = $this->_total == 0 ? 0 : number_format($flights / $this->_total * 100, 1);
                 $result[] = array('data' => $names[$key], 'count' => $flights, 'percent' => $percentage, 'key' => $key);
                 if ($percentage > 0) {
                     $chart[] = [$key, $percentage];
                 }
             } else {
                 $other += $flights;
             }
         }
     }
     $result['Other'] = array('count' => $other, 'percent' => $this->_total == 0 ? 0 : number_format($other / $this->_total * 100, 1), 'key' => 'Other');
     $chart[] = ['Other', $result['Other']['percent']];
     $piechartData = piechartData($chart);
     return array('table' => $result, 'chart' => $piechartData['javascript'], 'colours' => $piechartData['colours']);
 }
 function index()
 {
     $airports = Airport::with('country', 'departures', 'arrivals')->orderBy('country_id')->orderBy('city')->orderBy('id')->paginate(100);
     $this->autoRender(compact('airports'), 'Airports');
 }
Exemple #3
0
 function topAirports()
 {
     $other = 0;
     $result = array();
     $chart = array();
     $names = array();
     $origCounter = $this->query()->select(DB::raw('departure_id, count(departure_id) as counter'))->groupBy('departure_id')->where('departure_id', '!=', '')->orderBy('counter', 'DESC')->lists('counter', 'departure_id');
     $destCounter = $this->query()->select(DB::raw('arrival_id, count(arrival_id) as counter'))->groupBy('arrival_id')->where('arrival_id', '!=', '')->orderBy('counter', 'DESC')->lists('counter', 'arrival_id');
     $airportsId = array_unique(array_merge(array_keys($origCounter), array_keys($destCounter)));
     $counter = array_combine($airportsId, $airportsId);
     foreach ($counter as &$airportId) {
         $airportId = @$origCounter[$airportId] + @$destCounter[$airportId];
     }
     arsort($counter);
     if (count($counter) > 0) {
         $namesRaw = Airport::with('country')->whereIn('icao', array_slice(array_keys($counter), 0, 5))->get();
         foreach ($namesRaw as $airport) {
             $names[$airport->icao] = $airport;
         }
     }
     foreach ($counter as $key => $flights) {
         if (count($result) < 5 && array_key_exists($key, $names)) {
             $percentage = $this->_total == 0 ? 0 : number_format($flights / ($this->_total * 2) * 100, 1);
             $result[] = array('data' => $names[$key], 'count' => $flights, 'percent' => $percentage, 'key' => $names[$key]->icao);
             if ($percentage > 0) {
                 $chart[] = [$names[$key]->icao, $percentage];
             }
         } else {
             $other += $flights;
         }
     }
     $result['Other'] = array('count' => $other, 'percent' => $this->_total == 0 ? 0 : number_format($other / ($this->_total * 2) * 100, 1), 'key' => 'Other');
     $chart[] = ['Other', $result['Other']['percent']];
     $piechartData = piechartData($chart);
     return array('table' => $result, 'chart' => $piechartData['javascript'], 'colours' => $piechartData['colours']);
 }