Advertisement
Guest User

Untitled

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