Advertisement
Guest User

Untitled

a guest
Feb 16th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 26.42 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * phpVMS - Virtual Airline Administration Software
  5.  * Copyright (c) 2008 Nabeel Shahzad
  6.  * For more information, visit www.phpvms.net
  7.  *  Forums: http://www.phpvms.net/forum
  8.  *  Documentation: http://www.phpvms.net/docs
  9.  *
  10.  * phpVMS is licenced under the following license:
  11.  *   Creative Commons Attribution Non-commercial Share Alike (by-nc-sa)
  12.  *   View license.txt in the root, or visit http://creativecommons.org/licenses/by-nc-sa/3.0/
  13.  *
  14.  * @author Nabeel Shahzad
  15.  * @copyright Copyright (c) 2008, Nabeel Shahzad
  16.  * @link http://www.phpvms.net
  17.  * @license http://creativecommons.org/licenses/by-nc-sa/3.0/
  18.  */
  19.  
  20. class SchedulesData extends CodonData {
  21.  
  22.     /**
  23.      * A generic find function for schedules. As parameters, do:
  24.      *
  25.      * $params = array( 's.depicao' => 'value',
  26.      *                  's.arricao' => array ('multiple', 'values'),
  27.      *  );
  28.      *
  29.      * Syntax is ('s.columnname' => 'value'), where value can be
  30.      *  an array is multiple values, or with a SQL wildcard (%)
  31.      *  if that's what is desired.
  32.      *
  33.      * Columns from the schedules table should be prefixed by 's.',
  34.      * the aircraft table as 'a.'
  35.      *
  36.      * You can also pass offsets ($start and $count) in order to
  37.      * facilitate pagination
  38.      *
  39.      * @tutorial http://docs.phpvms.net/media/development/searching_and_retriving_schedules
  40.      */
  41.     public static function findSchedules($params, $count = '', $start = '') {
  42.         $sql = 'SELECT s.*,
  43.                     a.id as aircraftid, a.name as aircraft, a.registration,
  44.                     a.minrank as aircraft_minrank, a.ranklevel as aircraftlevel,
  45.                     dep.name as depname, dep.lat AS deplat, dep.lng AS deplng,
  46.                     arr.name as arrname, arr.lat AS arrlat, arr.lng AS arrlng
  47.                 FROM ' . TABLE_PREFIX . 'schedules AS s
  48.                 LEFT JOIN ' . TABLE_PREFIX . 'airports AS dep ON dep.icao = s.depicao
  49.                 LEFT JOIN ' . TABLE_PREFIX . 'airports AS arr ON arr.icao = s.arricao
  50.                 LEFT JOIN ' . TABLE_PREFIX . 'aircraft AS a ON a.id = s.aircraft ';
  51.  
  52.         /* Build the select "WHERE" based on the columns passed, this is a generic function */
  53.         $sql .= DB::build_where($params);
  54.  
  55.         // Order matters
  56.         if (Config::Get('SCHEDULES_ORDER_BY') != '') {
  57.             $sql .= ' ORDER BY ' . Config::Get('SCHEDULES_ORDER_BY');
  58.         }
  59.  
  60.         if (strlen($count) != 0) {
  61.             $sql .= ' LIMIT ' . $count;
  62.         }
  63.  
  64.         if (strlen($start) != 0) {
  65.             $sql .= ' OFFSET ' . $start;
  66.         }
  67.  
  68.         $ret = DB::get_results($sql);
  69.        
  70.         if(!$ret) {
  71.             return array();
  72.         }
  73.        
  74.         return $ret;
  75.     }
  76.  
  77.  
  78.     /**
  79.      * Get the total number of schedules based on criterea
  80.      *
  81.      * @param array $params key => value list
  82.      * @return int Returns the total number
  83.      *
  84.      */
  85.     public static function countSchedules($params) {
  86.         $sql = 'SELECT COUNT(s.id) as total
  87.                 FROM ' . TABLE_PREFIX . 'schedules AS s
  88.                 LEFT JOIN ' . TABLE_PREFIX . 'airports AS dep ON dep.icao = s.depicao
  89.                 LEFT JOIN ' . TABLE_PREFIX . 'airports AS arr ON arr.icao = s.arricao
  90.                 LEFT JOIN ' . TABLE_PREFIX . 'aircraft AS a ON a.id = s.aircraft ';
  91.  
  92.         $sql .= DB::build_where($params);
  93.         $res = DB::get_row($sql);
  94.  
  95.         return $res->total;
  96.     }
  97.  
  98.     /**
  99.      * Return information about a schedule (pass the ID)
  100.      */
  101.     public static function getSchedule($id) {
  102.         return self::getScheduleDetailed($id);
  103.     }
  104.  
  105.  
  106.     /**
  107.      * Return a flight given the airline code and flight number
  108.      *
  109.      * @deprecated
  110.      *
  111.      * @param string $code Airline code
  112.      * @param mixed $flightnum Flight number
  113.      * @return array Returns a full flight
  114.      *
  115.      */
  116.     public static function getScheduleByFlight($code, $flightnum) {
  117.         $params = array('s.code' => strtoupper($code), 's.flightnum' => strtoupper($flightnum), );
  118.  
  119.         $schedule = self::findSchedules($params);
  120.         return $schedule[0];
  121.     }
  122.  
  123.  
  124.     /**
  125.      * Find a flight on the flightnumber and departure airport
  126.      *
  127.      * @param string $flightnum Flight numbers
  128.      * @param string $depicao Departure airport
  129.      * @return array Returns one flight
  130.      *
  131.      */
  132.     public static function findFlight($flightnum, $depicao = '') {
  133.         $params = array('s.flightnum' => strtoupper($flightnum));
  134.  
  135.         if ($depicao != '') {
  136.             $params['s.depicao'] = $depicao;
  137.         }
  138.  
  139.         $schedule = self::findSchedules($params);
  140.         return $schedule[0];
  141.     }
  142.  
  143.     /**
  144.      * Extract the code and flight number portions from the flight number
  145.      * Ensures that the code and number are properly split
  146.      */
  147.     public static function getProperFlightNum($flightnum) {
  148.         if ($flightnum == '') return false;
  149.  
  150.         $ret = array();
  151.         $flightnum = strtoupper($flightnum);
  152.         $airlines = OperationsData::getAllAirlines(false);
  153.  
  154.         foreach ($airlines as $a) {
  155.             $a->code = strtoupper($a->code);
  156.  
  157.             if (strpos($flightnum, $a->code) === false) {
  158.                 continue;
  159.             }
  160.  
  161.             $ret['code'] = $a->code;
  162.             $ret['flightnum'] = str_ireplace($a->code, '', $flightnum);
  163.  
  164.             return $ret;
  165.         }
  166.  
  167.         # Invalid flight number
  168.        $ret['code'] = '';
  169.         $ret['flightnum'] = $flightnum;
  170.         return $ret;
  171.     }
  172.  
  173.  
  174.     /**
  175.      * Increment the flown count for a schedule
  176.      *
  177.      * @param string $code Airline code
  178.      * @param int $flightnum Flight number
  179.      * @return bool
  180.      *
  181.      */
  182.     public static function incrementFlownCount($code, $flightnum) {
  183.         return self::changeFlownCount($code, $flightnum, '+1');
  184.     }
  185.  
  186.    
  187.     /**
  188.      * SchedulesData::changeFlownCount()
  189.      *
  190.      * @param mixed $code
  191.      * @param mixed $flightnum
  192.      * @param mixed $amount
  193.      * @return void
  194.      */
  195.     public static function changeFlownCount($code, $flightnum, $amount) {
  196.        
  197.         $schedid = intval($schedid);
  198.  
  199.         $code = strtoupper($code);
  200.         $flightnum = strtoupper($flightnum);
  201.        
  202.         if(substr_count($amount, '+') == 0) {
  203.             $amount = '+'.$amount;
  204.         }
  205.  
  206.         $sql = 'UPDATE ' . TABLE_PREFIX . "schedules
  207.                 SET timesflown=timesflown {$amount}
  208.                 WHERE code='{$code}' AND flightnum='{$flightnum}'";
  209.  
  210.         $res = DB::query($sql);
  211.  
  212.         if (DB::errno() != 0) return false;
  213.  
  214.         return true;
  215.        
  216.     }
  217.  
  218.  
  219.     /**
  220.      * Get detailed information about a schedule
  221.      *
  222.      * @param int $id ID of the schedule
  223.      * @return array Schedule details
  224.      *
  225.      */
  226.     public static function getScheduleDetailed($id) {
  227.         $schedules = self::findSchedules(array('s.id' => $id));
  228.         if (!$schedules) return false;
  229.  
  230.         $schedule = $schedules[0];
  231.         unset($schedules);
  232.  
  233.         /*$schedule->route_details = unserialize($schedule->route_details);
  234.         if(!empty($schedule->route) && !$schedule->route_details)
  235.         {
  236.         $schedule->route_details = SchedulesData::getRouteDetails($schedule->id, $schedule->route);
  237.         }*/
  238.  
  239.         if ($schedule->route != '') {
  240.             $schedule->route_details = NavData::parseRoute($schedule);
  241.         }
  242.  
  243.         return $schedule;
  244.     }
  245.  
  246.     /**
  247.      * Return all the airports by depature, which have a schedule, for
  248.      *  a certain airline. If the airline
  249.      * @return object_array
  250.      */
  251.     public static function getDepartureAirports($airlinecode = '', $onlyenabled = false) {
  252.         $airlinecode = DB::escape($airlinecode);
  253.  
  254.         if ($onlyenabled) $enabled = 'AND s.enabled=1';
  255.         else  $enabled = '';
  256.  
  257.         $sql = 'SELECT DISTINCT s.depicao AS icao, a.name
  258.                 FROM ' . TABLE_PREFIX . 'schedules s, ' . TABLE_PREFIX . 'airports a
  259.                 WHERE s.depicao = a.icao ' . $enabled;
  260.  
  261.         if ($airlinecode != '') $sql .= " AND s.code='{$airlinecode}' ";
  262.  
  263.         $sql .= ' ORDER BY depicao ASC';
  264.  
  265.         return DB::get_results($sql);
  266.     }
  267.  
  268.     /**
  269.      * Get all of the airports which have a schedule, from
  270.      *  a certain airport, using the airline code. Code
  271.      *  is optional, otherwise it returns all of the airports.
  272.      *
  273.      * @return database object
  274.      */
  275.     public static function getArrivalAiports($depicao, $airlinecode = '', $onlyenabled = true) {
  276.         $depicao = strtoupper($depicao);
  277.         $airlinecode = strtoupper($airlinecode);
  278.         $depicao = DB::escape($depicao);
  279.  
  280.         if ($onlyenabled) $enabled = 'AND s.enabled=1';
  281.         else  $enabled = '';
  282.  
  283.         $sql = 'SELECT DISTINCT s.arricao AS icao, a.name
  284.                 FROM ' . TABLE_PREFIX . 'schedules s, ' . TABLE_PREFIX . 'airports a
  285.                 WHERE s.arricao = a.icao ' . $enabled;
  286.  
  287.         if ($airlinecode != '') $sql .= " AND s.code='{$airlinecode}' ";
  288.  
  289.         $sql .= ' ORDER BY depicao ASC';
  290.  
  291.         return DB::get_results($sql);
  292.     }
  293.  
  294.     /**
  295.      * Get all the schedules, $limit is the number to return
  296.      */
  297.     public static function getSchedules($onlyenabled = true, $limit = '', $start =
  298.         '') {
  299.         $params = array();
  300.         if ($onlyenabled) $params['s.enabled'] = '1';
  301.  
  302.         return self::findSchedules($params, $limit, $start);
  303.     }
  304.  
  305.     /**
  306.      * This gets all of the schedules which are disabled
  307.      */
  308.     /*public static function getInactiveSchedules($count='', $start='')
  309.     {
  310.     $params = array('s.enabled'=>0);
  311.     return self::findSchedules($params, $count, $start);
  312.     }*/
  313.  
  314.  
  315.     /**
  316.      * Calculate the distance between two coordinates
  317.      * Using a revised equation found on http://www.movable-type.co.uk/scripts/latlong.html
  318.      *
  319.      * Also converts to proper type based on UNIT setting
  320.      *
  321.      */
  322.     public static function distanceBetweenPoints($lat1, $lng1, $lat2, $lng2) {
  323.         /*  Use a radius depending on the final units we want to be in
  324.         New formula, from http://jan.ucc.nau.edu/~cvm/latlon_formula.html
  325.         */
  326.         if (strtolower(Config::Get('UNITS')) === 'mi') # miles
  327.  
  328.             $radius = 3963.192;
  329.         elseif (strtolower(Config::Get('UNITS')) === 'km') # Convert to km
  330.  
  331.             $radius = 6378.14;
  332.         else  $radius = 3443.92;
  333.  
  334.         /*
  335.         $distance = ($radius * 3.1415926 * sqrt(($lat2-$lat1) * ($lat2-$lat1)
  336.         +cos($lat2/57.29578) * cos($lat1/57.29578) * ($lng2-$lng1) * ($lng2-$lng1))/180);
  337.        
  338.         return $distance;
  339.         */
  340.         $lat1 = deg2rad(floatval($lat1));
  341.         $lat2 = deg2rad(floatval($lat2));
  342.         $lng1 = deg2rad(floatval($lng1));
  343.         $lng2 = deg2rad(floatval($lng2));
  344.  
  345.         $a = sin(($lat2 - $lat1) / 2.0);
  346.         $b = sin(($lng2 - $lng1) / 2.0);
  347.         $h = ($a * $a) + cos($lat1) * cos($lat2) * ($b * $b);
  348.         $theta = 2 * asin(sqrt($h)); # distance in radians
  349.  
  350.         $distance = $theta * $radius;
  351.  
  352.         return $distance;
  353.  
  354.         /* Convert all decimal degrees to radians */
  355.  
  356.         $dlat = $lat2 - $lat1;
  357.         $dlng = $lng2 - $lng1;
  358.  
  359.         $a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlng / 2) *
  360.             sin($dlng / 2);
  361.         $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
  362.         $distance = $r * $c;
  363.  
  364.         return $distance;
  365.         /*$distance = acos(cos($lat1)*cos($lng1)*cos($lat2)*cos($lng2)
  366.         + cos($lat1)*sin($lng1)*cos($lat2)*sin($lng2)
  367.         + sin($lat1)*sin($lat2)) * $r;
  368.  
  369.         return floatval(round($distance, 2));*/
  370.     }
  371.  
  372.     /**
  373.      * Add a schedule
  374.      *
  375.      * Pass in the following:
  376.      * $data = array(   'code'=>'',
  377.      * 'flightnum'=''
  378.      * 'depicao'=>'',
  379.      * 'arricao'=>'',
  380.      * 'route'=>'',
  381.      * 'aircraft'=>'',
  382.      * 'distance'=>'',
  383.      * 'deptime'=>'',
  384.      * 'arrtime'=>'',
  385.      * 'flighttime'=>'',
  386.      * 'notes'=>'',
  387.      * 'enabled'=>'',
  388.      * 'price'=>''
  389.      * 'flighttype'=>'');
  390.      */
  391.     public static function addSchedule($data) {
  392.        
  393.         if (!is_array($data)) return false;
  394.  
  395.         # Commented out to allow flights to/from the same airport
  396.        #if($data['depicao'] == $data['arricao'])
  397.         #   return false;
  398.  
  399.         $data['code'] = strtoupper($data['code']);
  400.         $data['flightnum'] = strtoupper($data['flightnum']);
  401.         $data['deptime'] = strtoupper($data['deptime']);
  402.         $data['arrtime'] = strtoupper($data['arrtime']);
  403.         $data['depicao'] = strtoupper($data['depicao']);
  404.         $data['arricao'] = strtoupper($data['arricao']);
  405.  
  406.         if ($data['enabled'] == true) $data['enabled'] = 1;
  407.         else  $data['enabled'] = 0;
  408.  
  409.         # If they didn't specify
  410.        $data['flighttype'] = strtoupper($data['flighttype']);
  411.         if ($data['flighttype'] == '') $data['flighttype'] = 'P';
  412.  
  413.         $data['flightlevel'] = str_replace(',', '', $data['flightlevel']);
  414.        
  415.         if(isset($data['week1'])) {
  416.            
  417.         }
  418.  
  419.         if (isset($fields['route'])) {
  420.             $fields['route'] = str_replace('SID', '', $fields['route']);
  421.             $fields['route'] = str_replace('STAR', '', $fields['route']);
  422.             $fields['route'] = str_replace('DCT', '', $fields['route']);
  423.             $fields['route'] = trim($fields['route']);
  424.             $fields['route_details'] = '';
  425.         }
  426.  
  427.         foreach ($data as $key => $value) {
  428.             $data[$key] = DB::escape($value);
  429.         }
  430.  
  431.         $data['flighttime'] = str_replace(':', '.', $data['flighttime']);
  432.        
  433.        
  434.         # Do the insert based on the columns here
  435.        $cols = array();
  436.         $col_values = array();
  437.        
  438.         foreach ($data as $key => $value) {
  439.            
  440.             if($key == 'daysofweek' || $key == 'week1' || $key == 'week2' || $key == 'week3' || $key == 'week4') {        
  441.                 $value = str_replace('7', '0', $value);
  442.             } else {
  443.                 $value = DB::escape($value);
  444.             }
  445.            
  446.            
  447.             $cols[] = "`{$key}`";
  448.             $col_values[] = "'{$value}'";
  449.         }
  450.  
  451.         $cols = implode(', ', $cols);
  452.         $col_values = implode(', ', $col_values);
  453.         $sql = 'INSERT INTO ' . TABLE_PREFIX . "schedules ({$cols}) VALUES ({$col_values});";
  454.  
  455.         $res = DB::query($sql);
  456.  
  457.         if (!empty($data['route'])) {
  458.             self::getRouteDetails(DB::$insert_id, $data['route']);
  459.         }
  460.  
  461.         if (DB::errno() != 0) return false;
  462.  
  463.         return true;
  464.     }
  465.  
  466.     /**
  467.      * Edit a schedule
  468.      *  Pass in the columns - deprecated
  469.      */
  470.  
  471.     public static function editSchedule($data) {
  472.         if (!is_array($data)) return false;
  473.  
  474.         $id = $data['id'];
  475.         unset($data['id']);
  476.  
  477.         self::editScheduleFields($id, $data);
  478.     }
  479.  
  480.  
  481.     /**
  482.      * Parse a schedule's route, and store it in the route_details
  483.      * column for later on. It will store a serialized array of the
  484.      * route's details.
  485.      *
  486.      * @param int $schedule_id ID of the schedule to parse
  487.      * @param string $route Optional route to parse, otherwise it will look it up
  488.      * @return array Returns the route's details
  489.      *
  490.      */
  491.     public static function getRouteDetails($schedule_id, $route = '') {
  492.        
  493.         $schedule = self::findSchedules(array('s.id' => $schedule_id), 1);
  494.         $schedule = $schedule[0];
  495.  
  496.         if (empty($schedule->route)) {
  497.             return;
  498.         }
  499.  
  500.         $route_details = NavData::parseRoute($schedule);
  501.         $store_details = DB::escape(serialize($route_details));
  502.  
  503.         $val = self::editScheduleFields($schedule_id, array('route_details' => $store_details));
  504.  
  505.         return $route_details;
  506.     }
  507.  
  508.     /**
  509.      * Update any fields in a schedule, other update functions come down to this
  510.      *
  511.      * @param int $scheduleid ID of the schedule to update
  512.      * @param array $fields Array, column name as key, with values to update
  513.      * @return bool
  514.      *
  515.      */
  516.     public static function updateScheduleFields($scheduleid, $fields) {
  517.         return self::editScheduleFields($scheduleid, $fields);
  518.     }
  519.  
  520.     /**
  521.      * Update any fields in a schedule, other update functions come down to this
  522.      *
  523.      * @param int $scheduleid ID of the schedule to update
  524.      * @param array $fields Array, column name as key, with values to update
  525.      * @return bool
  526.      *
  527.      */
  528.     public static function editScheduleFields($scheduleid, $fields) {
  529.        
  530.         if (!is_array($fields)) {
  531.             return false;
  532.         }
  533.  
  534.         if (isset($fields['depicao']) && isset($fields['arricao'])) {
  535.             if ($fields['depicao'] == $fields['arricao']) {
  536.                 return false;
  537.             }
  538.         }
  539.  
  540.         /* Ensure data is ok and formatted properly */
  541.         if (isset($fields['code'])) {
  542.             $fields['code'] = strtoupper($fields['code']);
  543.         }
  544.  
  545.         if (isset($fields['flightnum'])) {
  546.             $fields['flightnum'] = strtoupper($fields['flightnum']);
  547.         }
  548.  
  549.         if (isset($fields['depicao'])) {
  550.             $fields['depicao'] = strtoupper($fields['depicao']);
  551.         }
  552.  
  553.         if (isset($fields['arricao'])) {
  554.             $fields['arricao'] = strtoupper($fields['arricao']);
  555.         }
  556.  
  557.         if (isset($fields['deptime'])) {
  558.             $fields['deptime'] = strtoupper($fields['deptime']);
  559.         }
  560.  
  561.         if (isset($fields['arrtime'])) {
  562.             $fields['arrtime'] = strtoupper($fields['arrtime']);
  563.         }
  564.  
  565.         if (isset($fields['enabled'])) {
  566.             if ($fields['enabled'] == true) $fields['enabled'] = 1;
  567.             else  $fields['enabled'] = 0;
  568.         }
  569.  
  570.         # If they didn't specify a flight type, just default to pax
  571.        if (isset($fields['flighttype'])) {
  572.             $fields['flighttype'] = strtoupper($fields['flighttype']);
  573.             if ($fields['flighttype'] == '') {
  574.                 $fields['flighttype'] = 'P';
  575.             }
  576.         }
  577.  
  578.         if (isset($fields['flightlevel'])) {
  579.             $fields['flightlevel'] = str_replace(',', '', $fields['flightlevel']);
  580.         }
  581.  
  582.  
  583.         if (isset($fields['flighttime'])) {
  584.             $fields['flighttime'] = str_replace(':', '.', $fields['flighttime']);
  585.         }
  586.  
  587.         if (isset($fields['route'])) {
  588.             $fields['route'] = str_replace('SID', '', $fields['route']);
  589.             $fields['route'] = str_replace('STAR', '', $fields['route']);
  590.             $fields['route'] = str_replace('DCT', '', $fields['route']);
  591.             $fields['route'] = trim($fields['route']);
  592.         }
  593.  
  594.         foreach ($fields as $key => $value) {
  595.             $fields[$key] = DB::escape($value);
  596.         }
  597.  
  598.         $sql = "UPDATE `" . TABLE_PREFIX . "schedules` SET ";
  599.         $sql .= DB::build_update($fields);
  600.         $sql .= ' WHERE `id`=' . $scheduleid;
  601.  
  602.         $res = DB::query($sql);
  603.  
  604.         if (DB::errno() != 0) {
  605.             return false;
  606.         }
  607.  
  608.         return true;
  609.     }
  610.  
  611.     /**
  612.      * Delete a schedule
  613.      */
  614.     public static function deleteSchedule($scheduleid) {
  615.        
  616.         $scheduleid = DB::escape($scheduleid);
  617.         $sql = 'DELETE FROM ' . TABLE_PREFIX . 'schedules
  618.                 WHERE id=' . $scheduleid;
  619.  
  620.         $res = DB::query($sql);
  621.  
  622.         if (DB::errno() != 0) return false;
  623.  
  624.         return true;
  625.     }
  626.  
  627.     public static function deleteAllSchedules() {
  628.         $sql = 'DELETE FROM ' . TABLE_PREFIX . 'schedules';
  629.  
  630.         $res = DB::query($sql);
  631.  
  632.         if (DB::errno() != 0) return false;
  633.  
  634.         return true;
  635.     }
  636.  
  637.     public static function deleteAllScheduleDetails() {
  638.        
  639.         $sql = 'UPDATE ' . TABLE_PREFIX . "schedules
  640.                 SET `route_details` = ''";
  641.  
  642.         $res = DB::query($sql);
  643.  
  644.         if (DB::errno() != 0) return false;
  645.  
  646.         return true;
  647.     }
  648.  
  649.     public static function getAllBids() {
  650.         $sql = 'SELECT  p.*, s.*,
  651.                         b.bidid as bidid, b.dateadded, a.name as aircraft, a.registration
  652.                 FROM ' . TABLE_PREFIX . 'schedules s,
  653.                      ' . TABLE_PREFIX . 'bids b,
  654.                      ' . TABLE_PREFIX . 'aircraft a,
  655.                      ' . TABLE_PREFIX . 'pilots p
  656.                 WHERE b.routeid = s.id AND s.aircraft=a.id AND p.pilotid = b.pilotid
  657.                 ORDER BY b.bidid DESC';
  658.  
  659.         return DB::get_results($sql);
  660.     }
  661.  
  662.     /**
  663.      * Get the latest bids
  664.      */
  665.  
  666.     public static function getLatestBids($limit = 5) {
  667.         $sql = 'SELECT  p.*, s.*,
  668.                         b.bidid as bidid, a.name as aircraft, a.registration
  669.                 FROM ' . TABLE_PREFIX . 'schedules s,
  670.                      ' . TABLE_PREFIX . 'bids b,
  671.                      ' . TABLE_PREFIX . 'aircraft a,
  672.                      ' . TABLE_PREFIX . 'pilots p
  673.                 WHERE b.routeid = s.id AND s.aircraft=a.id AND p.pilotid = b.pilotid
  674.                 ORDER BY b.bidid DESC
  675.                 LIMIT ' . $limit;
  676.  
  677.         return DB::get_results($sql);
  678.     }
  679.  
  680.     public function getLatestBid($pilotid) {
  681.         $pilotid = DB::escape($pilotid);
  682.  
  683.         $sql = 'SELECT s.*, b.bidid, a.id as aircraftid, a.name as aircraft, a.registration, a.maxpax, a.maxcargo
  684.                 FROM ' . TABLE_PREFIX . 'schedules s,
  685.                      ' . TABLE_PREFIX . 'bids b,
  686.                      ' . TABLE_PREFIX . 'aircraft a
  687.                 WHERE b.routeid = s.id
  688.                     AND s.aircraft=a.id
  689.                     AND b.pilotid=' . $pilotid . '
  690.                 ORDER BY b.bidid ASC LIMIT 1';
  691.  
  692.         return DB::get_row($sql);
  693.     }
  694.  
  695.     /**
  696.      * Get a specific bid with route information
  697.      *
  698.      * @param unknown_type $bidid
  699.      * @return unknown
  700.      */
  701.     public static function getBid($bidid) {
  702.         $bidid = DB::escape($bidid);
  703.  
  704.         $sql = 'SELECT s.*, b.bidid, b.pilotid, b.routeid,
  705.                         a.name as aircraft, a.registration
  706.                 FROM ' . TABLE_PREFIX . 'schedules s, ' . TABLE_PREFIX . 'bids b,
  707.                     ' . TABLE_PREFIX . 'aircraft a
  708.                 WHERE b.routeid = s.id
  709.                         AND s.aircraft=a.id
  710.                         AND b.bidid=' . $bidid;
  711.  
  712.         return DB::get_row($sql);
  713.     }
  714.  
  715.     /**
  716.      * Get all of the bids for a pilot
  717.      *
  718.      * @param unknown_type $pilotid
  719.      * @return unknown
  720.      */
  721.     public static function getBids($pilotid) {
  722.         $pilotid = DB::escape($pilotid);
  723.         $sql = 'SELECT s.*, b.bidid, a.name as aircraft, a.registration
  724.                 FROM ' . TABLE_PREFIX . 'schedules s, ' . TABLE_PREFIX . 'bids b,
  725.                     ' . TABLE_PREFIX . 'aircraft a
  726.                 WHERE b.routeid = s.id
  727.                     AND s.aircraft=a.id
  728.                     AND b.pilotid=' . $pilotid;
  729.  
  730.         return DB::get_results($sql);
  731.     }
  732.  
  733.     /**
  734.      * Get find a bid for the pilot based on ID,
  735.      *  the airline code for the flight, and the flight number
  736.      */
  737.     public static function getBidWithRoute($pilotid, $code, $flightnum) {
  738.         if ($pilotid == '') return;
  739.  
  740.         $sql = 'SELECT b.bidid
  741.                 FROM ' . TABLE_PREFIX . 'bids b, ' . TABLE_PREFIX . 'schedules s
  742.                 WHERE b.pilotid=' . $pilotid . '
  743.                     AND b.routeid=s.id
  744.                     AND s.code=\'' . $code . '\'
  745.                     AND s.flightnum=\'' . $flightnum . '\'';
  746.  
  747.         return DB::get_row($sql);
  748.     }
  749.  
  750.     public function setBidOnSchedule($scheduleid, $bidid) {
  751.         $scheduleid = intval($scheduleid);
  752.         $bidid = intval($bidid);
  753.  
  754.         $sql = 'UPDATE ' . TABLE_PREFIX . 'schedules
  755.                 SET `bidid`=' . $bidid . '
  756.                 WHERE `id`=' . $scheduleid;
  757.  
  758.         DB::query($sql);
  759.  
  760.         if (DB::errno() != 0) return false;
  761.  
  762.         return true;
  763.     }
  764.  
  765.     /**
  766.      * Add a bid
  767.      */
  768.     public static function addBid($pilotid, $routeid) {
  769.         $pilotid = DB::escape($pilotid);
  770.         $routeid = DB::escape($routeid);
  771.  
  772.         if (DB::get_row('SELECT bidid FROM ' . TABLE_PREFIX . 'bids
  773.                         WHERE pilotid=' . $pilotid . ' AND routeid=' . $routeid)) {
  774.             return false;
  775.         }
  776.  
  777.         $pilotid = DB::escape($pilotid);
  778.         $routeid = DB::escape($routeid);
  779.  
  780.         $sql = 'INSERT INTO ' . TABLE_PREFIX . 'bids (pilotid, routeid, dateadded)
  781.                 VALUES (' . $pilotid . ', ' . $routeid . ', NOW())';
  782.  
  783.         DB::query($sql);
  784.  
  785.         self::setBidOnSchedule($routeid, DB::$insert_id);
  786.  
  787.         if (DB::errno() != 0) return false;
  788.  
  789.         return true;
  790.     }
  791.  
  792.     public static function deleteExpiredBids() {
  793.  
  794.         $cache_time = Config::Get('BID_EXPIRE_TIME');
  795.         if ($cache_time == '') {
  796.             return;
  797.         }
  798.  
  799.         /* Make sure the schedule bidids */
  800.         $sql = 'SELECT * FROM ' . TABLE_PREFIX . "bids
  801.                 WHERE `dateadded` + INTERVAL {$cache_time} HOUR < NOW()";
  802.  
  803.         $results = DB::get_results($sql);
  804.         if (count($results) > 0) {
  805.             foreach ($results as $row) {
  806.                 $sql = 'UPDATE ' . TABLE_PREFIX . "schedules
  807.                         SET `bidid`=0 WHERE `id`={$row->routeid}";
  808.  
  809.                 DB::query($sql);
  810.             }
  811.         }
  812.  
  813.         $sql = 'DELETE FROM ' . TABLE_PREFIX . "bids
  814.                 WHERE `dateadded` + INTERVAL {$cache_time} HOUR < NOW()";
  815.  
  816.         DB::query($sql);
  817.  
  818.     }
  819.     /**
  820.      * Remove a bid, by passing it's bid id
  821.      */
  822.     public static function deleteBid($bidid) {
  823.         self::removeBid($bidid);
  824.     }
  825.  
  826.     /**
  827.      * Remove a bid, by passing it's bid id
  828.      */
  829.     public static function removeBid($bidid) {
  830.         $bidid = intval($bidid);
  831.         $bid_info = self::getBid($bidid);
  832.  
  833.         $sql = 'DELETE FROM ' . TABLE_PREFIX . 'bids
  834.                 WHERE `bidid`=' . $bidid;
  835.  
  836.         DB::query($sql);
  837.  
  838.         self::SetBidOnSchedule($bid_info->routeid, 0);
  839.  
  840.         if (DB::errno() != 0) return false;
  841.  
  842.         return true;
  843.     }
  844.  
  845.  
  846.     /**
  847.      * @deprecated
  848.      *
  849.      */
  850.     public static function getScheduleFlownCounts($code, $flightnum, $days = 7) {
  851.         $max = 0;
  852.  
  853.         $code = strtoupper($code);
  854.         $flightnum = strtoupper($flightnum);
  855.  
  856.         $start = strtotime("- $days days");
  857.         $end = time();
  858.         $data = array();
  859.  
  860.         # Turn on caching:
  861.        DB::enableCache();
  862.  
  863.         do {
  864.             $count = PIREPData::getReportCountForRoute($code, $flightnum, $start);
  865.             $date = date('m-d', $start);
  866.  
  867.             $data[$date] = $count;
  868.  
  869.             $start += SECONDS_PER_DAY;
  870.  
  871.         } while ($start <= $end);
  872.  
  873.         DB::disableCache();
  874.  
  875.         return $data;
  876.     }
  877.  
  878.     /**
  879.      * Show the graph of the past week's reports. Outputs the
  880.      *  image unless $ret == true
  881.      *
  882.      * @deprecated
  883.      */
  884.     public static function showReportCounts() {
  885.         // Recent PIREP #'s
  886.         $max = 0;
  887.         $data = array();
  888.  
  889.         $time_start = strtotime('-7 days');
  890.         $time_end = time();
  891.  
  892.         // This is for the past 7 days
  893.         do {
  894.             $count = PIREPData::getReportCount($time_start);
  895.  
  896.             $data[] = $count;
  897.  
  898.             if ($count > $max) $max = $count;
  899.  
  900.             $time_start += SECONDS_PER_DAY;
  901.         } while ($time_start < $time_end);
  902.  
  903.         return $data;
  904.     }
  905. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement