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