Advertisement
Guest User

Untitled

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