Advertisement
Guest User

Untitled

a guest
Jun 10th, 2010
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 33.84 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 PilotData extends CodonData
  20. {
  21.  
  22. public static $pilot_data = array();
  23.  
  24.  
  25. /**
  26. * Find any pilots based on the parameters passed in
  27. *
  28. * @param array $params All the parameters
  29. * @param int $limit Number of results to return
  30. * @param int $start Record to start from
  31. * @return array Returns all the pilots requested
  32. *
  33. */
  34. public static function findPilots($params, $limit='', $start = '')
  35. {
  36. $sql = "SELECT p.*, r.`rankimage`, r.`payrate`
  37. FROM ".TABLE_PREFIX."pilots p
  38. LEFT JOIN ".TABLE_PREFIX."ranks r ON r.rank=p.rank ";
  39.  
  40. /* Build the select "WHERE" based on the columns passed, this is a generic function */
  41. $sql .= DB::build_where($params);
  42.  
  43. // Order matters
  44. if(Config::Get('PILOT_ORDER_BY') != '')
  45. {
  46. $sql .= ' ORDER BY '.Config::Get('PILOT_ORDER_BY');
  47. }
  48.  
  49. if(strlen($limit) != 0)
  50. {
  51. $sql .= ' LIMIT '.$limit;
  52. }
  53.  
  54. if(strlen($start) != 0)
  55. {
  56. $sql .= ' OFFSET '. $start;
  57. }
  58.  
  59. $ret = DB::get_results($sql);
  60. return $ret;
  61. }
  62.  
  63. /**
  64. * Get all the pilots, or the pilots who's last names start
  65. * with the letter
  66. */
  67. public static function getAllPilots($letter='')
  68. {
  69. $params = array();
  70.  
  71. if(!empty($letter))
  72. {
  73. $params['lastname'] = $letter.'%';
  74. }
  75.  
  76. return self::findPilots($params);
  77. }
  78.  
  79. /**
  80. * Get all the detailed pilot's information
  81. */
  82. public static function getAllPilotsDetailed($limit=20, $start='')
  83. {
  84. return self::findPilots(array(), $limit, $start);
  85. }
  86.  
  87. /**
  88. * Get a pilot's avatar
  89. */
  90. public static function getPilotAvatar($pilotid)
  91. {
  92. if(is_numeric($pilotid))
  93. {
  94. $pilot = self::getPilotData($pilotid);
  95. $pilotid = self::getPilotCode($pilot->code, $pilot->pilotid);
  96. }
  97.  
  98. $link = AVATAR_PATH.'/'.$pilotid.'.png';
  99.  
  100. if(!file_exists(SITE_ROOT.'/'.$link))
  101. {
  102. return SITE_URL.'/lib/images/noavatar.png';
  103. }
  104.  
  105. return SITE_URL.'/'.$link;
  106. }
  107.  
  108. /**
  109. * Get all the pilots on a certain hub
  110. */
  111. public static function getAllPilotsByHub($hub)
  112. {
  113. return self::findPilots(array('p.hub'=>$hub));
  114. }
  115.  
  116. /**
  117. * Return the pilot's code (ie DVA1031), using
  118. * the code and their DB ID
  119. */
  120. public static function getPilotCode($code, $pilotid)
  121. {
  122. # Make sure values are entered
  123. if(Config::Get('PILOTID_LENGTH') == '')
  124. Config::Set('PILOTID_LENGTH', 4);
  125.  
  126. if(Config::Get('PILOTID_OFFSET') == '')
  127. Config::Set('PILOTID_OFFSET', 0);
  128.  
  129. $pilotid = $pilotid + Config::Get('PILOTID_OFFSET');
  130. return $code . str_pad($pilotid, Config::Get('PILOTID_LENGTH'), '0', STR_PAD_LEFT);
  131. }
  132.  
  133. /**
  134. * The the basic pilot information
  135. * Quasi 'cached' in case it's called multiple times
  136. * for the same pilot in one script
  137. */
  138. public static function getPilotData($pilotid)
  139. {
  140. $pilot = self::findPilots(array('p.pilotid' => $pilotid), 1);
  141.  
  142. if(!$pilot)
  143. {
  144. return false;
  145. }
  146.  
  147. return $pilot[0];
  148. }
  149.  
  150. /**
  151. * Get a pilot's information by email
  152. */
  153. public static function getPilotByEmail($email)
  154. {
  155. $pilot = self::findPilots(array('p.email'=>$email));
  156. if(!$pilot)
  157. {
  158. return false;
  159. }
  160.  
  161. return $pilot[0];
  162. }
  163.  
  164.  
  165. /**
  166. * Parse a pilot ID from a passed ID
  167. *
  168. * @param int $pilotid Pass the ID string
  169. * @return int Returns the integer database ID
  170. *
  171. */
  172. public static function getProperPilotID($pilotid)
  173. {
  174. return self::parsePilotID($pilotid);
  175. }
  176.  
  177. /**
  178. * Parse a pilot ID from a passed ID
  179. *
  180. * @param int $pilotid Pass the ID string
  181. * @return int Returns the integer database ID
  182. *
  183. */
  184. public static function parsePilotID($pilotid)
  185. {
  186. if(!is_numeric($pilotid))
  187. {
  188. $airlines = OperationsData::getAllAirlines();
  189. foreach($airlines as $a)
  190. {
  191. $a->code = strtoupper($a->code);
  192.  
  193. if(strpos($pilotid, $a->code) === false)
  194. {
  195. continue;
  196. }
  197.  
  198.  
  199. $pilotid = intval(str_ireplace($a->code, '', $pilotid));
  200. $pilotid = $pilotid - Config::Get('PILOTID_OFFSET');
  201. }
  202. }
  203.  
  204. return $pilotid;
  205. }
  206.  
  207. /**
  208. * Get the list of all the pending pilots
  209. */
  210. public static function getPendingPilots($count='')
  211. {
  212. $params = array(
  213. 'p.confirmed' => PILOT_PENDING
  214. );
  215.  
  216. return self::findPilots($params, $count);
  217. }
  218.  
  219. public static function getLatestPilots($count=10)
  220. {
  221. $sql = 'SELECT * FROM '.TABLE_PREFIX.'pilots
  222. ORDER BY `pilotid` DESC
  223. LIMIT '.$count;
  224.  
  225. return DB::get_results($sql);
  226. }
  227.  
  228. /**
  229. * Change a pilot's name. This is separate because this is an
  230. * admin-only operation (strictly speaking), and isn't included
  231. * in a normal change of a pilot's profile (whereas SaveProfile
  232. * only changes minute information
  233. */
  234.  
  235. public static function changeName($pilotid, $firstname, $lastname)
  236. {
  237. # Non-blank
  238. if(empty($pilotid) || empty($firstname) || empty($lastname))
  239. {
  240. return false;
  241. }
  242.  
  243. $params = array(
  244. 'firstname' => $firstname,
  245. 'lastname' => $lastname,
  246. );
  247.  
  248. return self::updateProfile($pilotid, $params);
  249. }
  250.  
  251. public static function changePilotID($old_pilotid, $new_pilotid)
  252. {
  253. $pilot_exists = self::getPilotData($new_pilotid);
  254. if(is_object($pilot_exists))
  255. {
  256. return false;
  257. }
  258.  
  259. DB::query('SET foreign_key_checks = 0;');
  260. // List of all the tables which need to update
  261. $table_list = array(
  262. 'groupmembers',
  263. 'pilots',
  264. 'adminlog',
  265. 'awardsgranted',
  266. 'acarsdata',
  267. 'sessions',
  268. 'pireps',
  269. 'pirepcomments',
  270. 'fieldvalues',
  271. 'bids',
  272. );
  273.  
  274. foreach($table_list as $table)
  275. {
  276. $sql = 'UPDATE `'.TABLE_PREFIX.$table.'`
  277. SET `pilotid`='.$new_pilotid.'
  278. WHERE `pilotid`='.$old_pilotid;
  279.  
  280. DB::query($sql);
  281. //DB::debug();
  282. }
  283.  
  284. return true;
  285. }
  286.  
  287. public static function changePilotRank($pilotid, $rankid)
  288. {
  289. $rank = RanksData::getRankInfo($rankid);
  290. $rank_level = RanksData::getRankLevel($rankid);
  291. if(!$rank)
  292. {
  293. return false;
  294. }
  295.  
  296. $data = array(
  297. 'rankid' => $rank->rankid,
  298. 'rank' => $rank->rank,
  299. 'ranklevel' => $rank_level,
  300. );
  301.  
  302. return self::updateProfile($pilotid, $data);
  303. }
  304.  
  305. /**
  306. * Save any changes to a pilot's profile, calls updateProfile()
  307. *
  308. * @deprecated Use updateProfile() instead!!
  309. *
  310. */
  311. /*public static function saveProfile($params)
  312. {
  313. $pilotid = $params['pilotid'];
  314. unset($params['pilotid']);
  315.  
  316. return self::updateProfile($pilotid, $params);
  317. }*/
  318.  
  319.  
  320. /**
  321. * Update a pilot, $params is an array of column_name=>value
  322. *
  323. * @param mixed $pilotid This is a description
  324. * @param mixed $params This is a description
  325. * @return mixed This is the return value description
  326. *
  327. */
  328. public static function updateProfile($pilotid, $params)
  329. {
  330. /*$params = array(
  331. 'pilotid' => '',
  332. 'code' => '',
  333. 'email' => '',
  334. 'location' => '',
  335. 'hub' => '',
  336. 'bgimage' => '',
  337. 'retired' => false,
  338. );*/
  339.  
  340.  
  341. if(!is_array($params))
  342. {
  343. return false;
  344. }
  345.  
  346. /* Cleanup any specific parameters */
  347. if(isset($params['location']))
  348. {
  349. $params['location'] = strtoupper($params['location']);
  350. }
  351.  
  352. if(isset($params['retired']))
  353. {
  354. if(is_bool($params['retired']))
  355. {
  356. //$params['retired'] = intval($params['retired']);
  357.  
  358. # The above doesn't work for false =\
  359. if($params['retired'] == true)
  360. $params['retired'] = 1;
  361. else
  362. $params['retired'] = 0;
  363. }
  364. }
  365.  
  366. if(isset($params['pilotid']))
  367. {
  368. unset($params['pilotid']);
  369. }
  370.  
  371. $sql = "UPDATE ".TABLE_PREFIX."pilots SET ";
  372. $sql .= DB::build_update($params);
  373. $sql .= " WHERE `pilotid`={$pilotid}";
  374.  
  375. $res = DB::query($sql);
  376.  
  377. if(DB::errno() != 0)
  378. {
  379. return false;
  380. }
  381.  
  382. return true;
  383. }
  384.  
  385. public static function updatePilotRankLevels()
  386. {
  387. $all_pilots = self::findPilots(array());
  388.  
  389. foreach($all_pilots as $pilot)
  390. {
  391. $rank_level = RanksData::getRankLevel($pilot->rankid);
  392. self::updateProfile($pilot->pilotid, array('ranklevel'=>$rank_level));
  393. }
  394. }
  395.  
  396. public static function setPilotRetired($pilotid, $retired)
  397. {
  398. if($retired === true || $retired == '1')
  399. {
  400. $retired = 1;
  401. }
  402. else
  403. {
  404. $retired = 0;
  405. }
  406.  
  407. return self::updateProfile($pilotid, array('retired'=>$retired));
  408. }
  409.  
  410.  
  411. /**
  412. * Returns an array with a list of background images available
  413. *
  414. * @return array The background images list
  415. *
  416. */
  417. public static function getBackgroundImages()
  418. {
  419. $list = array();
  420. $files = scandir(SITE_ROOT.'/lib/signatures/background');
  421.  
  422. foreach($files as $file)
  423. {
  424. if($file == '.' || $file == '..') continue;
  425.  
  426. if(strstr($file, '.png') !== false)
  427. $list[] = $file;
  428. }
  429.  
  430. return $list;
  431. }
  432.  
  433. /**
  434. * Save avatars
  435. */
  436. public static function saveAvatar($code, $pilotid, $_FILES)
  437. {
  438. # Check the proper file size
  439. # Ignored for now since there is a resize
  440. /*if ($_FILES['avatar']['size'] > Config::Get('AVATAR_FILE_SIZE'))
  441. {
  442. return false;
  443. }*/
  444.  
  445. if(!$_FILES['avatar']['type'])
  446. return false;
  447.  
  448. # Create the image so we can convert it to PNG
  449. if($_FILES['avatar']['type'] == 'image/gif')
  450. {
  451. $img = imagecreatefromgif($_FILES['file']['tmp_name']);
  452. }
  453. elseif($_FILES['avatar']['type'] == 'image/jpeg'
  454. || $_FILES['avatar']['type'] == 'image/pjpeg')
  455. {
  456. $img = imagecreatefromjpeg($_FILES['avatar']['tmp_name']);
  457. }
  458. elseif($_FILES['avatar']['type'] == 'image/png')
  459. {
  460. $img = imagecreatefrompng($_FILES['avatar']['tmp_name']);
  461. }
  462.  
  463. # Resize it
  464. $height = imagesy($img);
  465. $width = imagesx($img);
  466.  
  467. $new_width = Config::Get('AVATAR_MAX_WIDTH');
  468. $new_height = floor( $height * ( Config::Get('AVATAR_MAX_HEIGHT') / $width ) );
  469.  
  470. $avatarimg = imagecreatetruecolor($new_width, $new_height);
  471. imagecopyresized($avatarimg, $img, 0,0,0,0,$new_width, $new_height, $width, $height);
  472.  
  473. # Output the file, to /lib/avatar/pilotcode.png
  474. $pilotCode = self::getPilotCode($code, $pilotid);
  475. imagepng($avatarimg, SITE_ROOT.AVATAR_PATH.'/'.$pilotCode.'.png');
  476. imagedestroy($img);
  477. }
  478.  
  479. /**
  480. * Accept the pilot (allow them into the system)
  481. */
  482. public static function AcceptPilot($pilotid)
  483. {
  484. return self::updateProfile($pilotid, array('confirmed'=>PILOT_ACCEPTED, 'retired'=>'0'));
  485. }
  486.  
  487. /**
  488. * Reject a pilot
  489. */
  490. public static function RejectPilot($pilotid)
  491. {
  492. return self::DeletePilot($pilotid);
  493. }
  494.  
  495.  
  496. /**
  497. * Completely delete a pilot
  498. *
  499. * @param int $pilotid Pilot ID
  500. * @return mixed This is the return value description
  501. *
  502. */
  503. public static function deletePilot($pilotid)
  504. {
  505. $sql = array();
  506. unset(self::$pilot_data[$pilotid]);
  507.  
  508. $sql[] = 'DELETE FROM '.TABLE_PREFIX.'acarsdata WHERE pilotid='.$pilotid;
  509. $sql[] = 'DELETE FROM '.TABLE_PREFIX.'bids WHERE pilotid='.$pilotid;
  510. $sql[] = 'DELETE FROM '.TABLE_PREFIX.'pireps WHERE pilotid='.$pilotid;
  511. $sql[] = 'DELETE FROM '.TABLE_PREFIX.'pilots WHERE pilotid='.$pilotid;
  512.  
  513. # These SHOULD delete on cascade, but incase they don't
  514. $sql[] = 'DELETE FROM '.TABLE_PREFIX.'fieldvalues WHERE pilotid='.$pilotid;
  515. $sql[] = 'DELETE FROM '.TABLE_PREFIX.'groupmembers WHERE pilotid='.$pilotid;
  516. $sql[] = 'DELETE FROM '.TABLE_PREFIX.'pirepcomments WHERE pilotid='.$pilotid;
  517.  
  518. foreach($sql as $query)
  519. {
  520. $res = DB::query($query);
  521. }
  522.  
  523. if(DB::errno() != 0)
  524. return false;
  525.  
  526. return true;
  527. }
  528.  
  529. /**
  530. * Update the login time
  531. */
  532. public static function updateLogin($pilotid)
  533. {
  534. return self::updateProfile($pilotid, array(
  535. 'lastlogin'=>'NOW()',
  536. 'lastip' => $_SERVER['REMOTE_ADDR'],
  537. )
  538. );
  539. }
  540.  
  541. public static function getPilotHours($pilotid)
  542. {
  543. $sql = 'SELECT `flighttime` FROM '.TABLE_PREFIX.'pireps
  544. WHERE `accepted`='.PIREP_ACCEPTED.'
  545. AND `pilotid`='.$pilotid;
  546.  
  547. $pireps = DB::get_results($sql);
  548.  
  549. if(!$pireps)
  550. return '0.0';
  551.  
  552. $total = 0;
  553. foreach($pireps as $report)
  554. {
  555. $total = Util::AddTime($total, $report->flighttime);
  556. }
  557.  
  558. return $total;
  559. }
  560.  
  561. /**
  562. * Get the total number of hours for a pilot, add them up
  563. *
  564. * @param int $pilotid The pilot ID
  565. * @return int Total hours for pilot
  566. *
  567. */
  568. public static function updateFlightHours($pilotid)
  569. {
  570. $total = self::getPilotHours($pilotid);
  571. $ret = self::updateProfile($pilotid, array('totalhours'=>$total));
  572. return $total;
  573. }
  574.  
  575. /**
  576. * Update a pilot's flight data, ie after a pirep
  577. *
  578. * @param int $pilotid Pilot ID
  579. * @param int $flighttime Number of hours.minutes to increment by
  580. * @param int $numflights Number of flights (default 1)
  581. * @return bool Success
  582. *
  583. */
  584. public static function updateFlightData($pilotid, $flighttime, $numflights=1)
  585. {
  586.  
  587. # Update the flighttime
  588. $pilotdata = PilotData::getPilotData($pilotid);
  589. $flighttime = Util::AddTime($pilotdata->totalhours, $flighttime);
  590.  
  591. if($numflights == '')
  592. $numflights = 1;
  593.  
  594. $params = array(
  595. 'totalhours' => $flighttime,
  596. 'totalflights' => ($pilotdata->totalflights + $numflights),
  597. );
  598.  
  599. return self::updateProfile($pilotid, $params);
  600. }
  601.  
  602. public static function updatePilotStats($pilotid)
  603. {
  604. $pireps = PIREPData::findPIREPS(array('p.pilotid'=>$pilotid));
  605.  
  606. $totalpireps = 0;
  607. $totalhours = 0;
  608.  
  609. if(is_array($pireps))
  610. {
  611. foreach($pireps as $p)
  612. {
  613. if($p->accepted != PIREP_ACCEPTED)
  614. {
  615. continue;
  616. }
  617.  
  618. $totalpireps++;
  619. $totalhours = Util::AddTime($p->flighttime, $totalhours);
  620. }
  621. }
  622.  
  623.  
  624. $params = array(
  625. 'totalhours'=>$totalhours,
  626. 'totalflights'=>$totalpireps,
  627. );
  628.  
  629. return self::updateProfile($pilotid, $params);
  630. }
  631.  
  632.  
  633. /**
  634. * Update the last PIREP date for a pilot
  635. *
  636. * @param int $pilotid Pilot ID
  637. * @return bool Success
  638. *
  639. */
  640. public static function updateLastPIREPDate($pilotid)
  641. {
  642. return self::updateProfile($pilotid, array('lastpirep'=>'NOW()'));
  643. }
  644.  
  645. /**
  646. * Don't update the pilot's flight data, but just replace it
  647. * with the values given
  648. *
  649. * $data = array(
  650. 'pilotid' => '',
  651. 'flighttime' => '',
  652. 'numflights' => '',
  653. 'totalpay' => '',
  654. 'transferhours' => '',
  655. );
  656. *
  657. * @param int $pilotid Pilot ID
  658. * @param int $flighttime Number of flight hours
  659. * @param int $numflights Number of flights
  660. * @param int $totalpay The total amount of money they have
  661. * @return bool Success
  662. *
  663. */
  664. public static function replaceFlightData($params)
  665. {
  666. /*$data = array(
  667. 'pilotid' => '',
  668. 'flighttime' => '',
  669. 'numflights' => '',
  670. 'totalpay' => '',
  671. 'transferhours' => '',
  672. );*/
  673.  
  674. $pilotid = $data['pilotid'];
  675. unset($data['pilotid']);
  676.  
  677. return self::updateProfile($pilotid, $params);
  678. }
  679.  
  680. public static function resetPilotPay($pilotid)
  681. {
  682. $total = 0;
  683.  
  684. self::updateProfile($pilotid, array('totalpay'=>0));
  685.  
  686. $sql = "SELECT `pirepid`, `flighttime`, `pilotpay`
  687. FROM ".TABLE_PREFIX."pireps
  688. WHERE `pilotid`={$pilotid}";
  689.  
  690. $results = DB::get_results($sql);
  691.  
  692. if(!$results)
  693. {
  694. return $total;
  695. }
  696.  
  697. foreach($results as $row)
  698. {
  699. $payupdate = self::getPilotPay($row->flighttime, $row->pilotpay);
  700. $total += $payupdate;
  701. }
  702.  
  703. self::updateProfile($pilotid, array('totalpay'=>$total));
  704.  
  705. return $total;
  706. }
  707.  
  708. /**
  709. * Update a pilot's pay. Pass the pilot ID, and the number of
  710. * hours they are being paid for
  711. *
  712. * @param int $pilotid The pilot ID
  713. * @param int $flighthours Number of hours to pay the pilot for
  714. * @return bool Success
  715. *
  716. */
  717. public static function updatePilotPay($pilotid, $flighthours)
  718. {
  719. $sql = 'SELECT payrate
  720. FROM '.TABLE_PREFIX.'ranks r, '.TABLE_PREFIX.'pilots p
  721. WHERE p.rank=r.rank
  722. AND p.pilotid='.$pilotid;
  723.  
  724. $payrate = DB::get_row($sql);
  725.  
  726. $payupdate = self::getPilotPay($flighthours, $payrate->payrate);
  727.  
  728. $sql = 'UPDATE '.TABLE_PREFIX.'pilots
  729. SET totalpay=totalpay+'.$payupdate.'
  730. WHERE pilotid='.$pilotid;
  731.  
  732. DB::query($sql);
  733.  
  734. if(DB::errno() != 0)
  735. return false;
  736.  
  737. return true;
  738. }
  739.  
  740. /**
  741. * Get the total pay for a flight at a certain rate,
  742. * for a certain number of hours
  743. *
  744. * @param float $hours Number of hours in Hours.Minutes format
  745. * @param float $rate Hourly rate
  746. * @return float Returns the total
  747. *
  748. */
  749. public static function getPilotPay($hours, $rate)
  750. {
  751. /* Hours are in hours.minutes
  752. convert to minutes */
  753. $peices = explode('.', $hours);
  754. $minutes = ($peices[0] * 60) + $peices[1];
  755. $payupdate = $minutes * ($rate/60);
  756.  
  757. return $payupdate;
  758. }
  759.  
  760.  
  761. /**
  762. * Find and set any pilots as retired
  763. *
  764. * @return mixed This is the return value description
  765. *
  766. */
  767. public static function findRetiredPilots()
  768. {
  769. $days = Config::Get('PILOT_INACTIVE_TIME');
  770.  
  771. if($days == '')
  772. $days = 90;
  773.  
  774. $sql = "SELECT * FROM ".TABLE_PREFIX."pilots
  775. WHERE DATE_SUB(CURDATE(), INTERVAL {$days} DAY) > `lastlogin`
  776. AND `totalflights` = 0 AND `lastlogin` != 0
  777. AND `retired` = 0";
  778.  
  779. $results = DB::get_results($sql);
  780.  
  781. $sql = "SELECT * FROM ".TABLE_PREFIX."pilots
  782. WHERE DATE_SUB(CURDATE(), INTERVAL {$days} DAY) > `lastpirep`
  783. AND `totalflights` > 0 AND `lastpirep` != 0
  784. AND `retired` = 0";
  785.  
  786. $results2 = DB::get_results($sql);
  787.  
  788. // messy but two queries, merge them both
  789. if(!is_array($results) && !is_array($results2))
  790. {
  791. return false;
  792. }
  793. else
  794. {
  795. if(is_array($results) && is_array($results2))
  796. {
  797. $results = array_merge($results, $results2);
  798. }
  799.  
  800. if(!is_array($results) && is_array($results2))
  801. {
  802. $results = $results2;
  803. }
  804. }
  805.  
  806. if(!$results)
  807. {
  808. return false;
  809. }
  810.  
  811. foreach($results as $row)
  812. {
  813. // Set them retired
  814. self::updateProfile($row->pilotid, array('retired'=>1));
  815.  
  816. Template::Set('pilot', $row);
  817. $pilot_retired_template = Template::Get('email_pilot_retired.tpl', true, true, true);
  818.  
  819. Util::SendEmail($row->email, Lang::get('email.pilot.retired.subject'), $pilot_retired_template);
  820. }
  821. }
  822.  
  823.  
  824. /**
  825. * This saves all of the custom fields attributed to pilot
  826. * Pass an associated array (fieldname NOT title) to value
  827. *
  828. * @param int $pilotid Pilot ID
  829. * @param array $list fieldname=>fieldvalue associated array
  830. * @return bool Success value
  831. *
  832. */
  833. public static function SaveFields($pilotid, $list)
  834. {
  835. $allfields = RegistrationData::getCustomFields(true);
  836.  
  837. if(!$allfields)
  838. return true;
  839.  
  840. foreach($allfields as $field)
  841. {
  842. $sql = 'SELECT id FROM '.TABLE_PREFIX.'fieldvalues
  843. WHERE fieldid='.$field->fieldid.'
  844. AND pilotid='.$pilotid;
  845.  
  846. $res = DB::get_row($sql);
  847.  
  848. $fieldname =str_replace(' ', '_', $field->fieldname);
  849.  
  850. if(!isset($list[$fieldname]))
  851. continue;
  852.  
  853. $value = $list[$fieldname];
  854.  
  855. // if it exists
  856. if($res)
  857. {
  858. $sql = 'UPDATE '.TABLE_PREFIX.'fieldvalues
  859. SET value="'.$value.'"
  860. WHERE fieldid='.$field->fieldid.' AND pilotid='.$pilotid;
  861. }
  862. else
  863. {
  864. $sql = "INSERT INTO ".TABLE_PREFIX."fieldvalues
  865. (fieldid, pilotid, value) VALUES ($field->fieldid, $pilotid, '$value')";
  866. }
  867.  
  868. DB::query($sql);
  869. }
  870.  
  871. return true;
  872. }
  873.  
  874. /**
  875. * Get all of the custom fields and values for a pilot
  876. *
  877. * @param int $pilotid The pilot ID
  878. * @param bool $inclprivate TRUE to also include private fields (default false)
  879. * @return array Returns all of the fields (names and values)
  880. *
  881. */
  882. public static function getFieldData($pilotid, $inclprivate=false)
  883. {
  884. $sql = 'SELECT f.fieldid, f.title, f.type, f.fieldname, f.value as fieldvalues, v.value, f.public
  885. FROM '.TABLE_PREFIX.'customfields f
  886. LEFT JOIN '.TABLE_PREFIX.'fieldvalues v
  887. ON f.fieldid=v.fieldid
  888. AND v.pilotid='.$pilotid;
  889.  
  890. if($inclprivate == false)
  891. $sql .= ' WHERE f.public=1 ';
  892.  
  893. return DB::get_results($sql);
  894. }
  895.  
  896.  
  897. /**
  898. * Get the value of a "custom field" for a pilot
  899. *
  900. * @param int $pilotid The pilot ID
  901. * @param string $title Full title of field, as enter "VATSIM ID"
  902. * @return string Returns the value of that field
  903. *
  904. */
  905. public static function getFieldValue($pilotid, $title)
  906. {
  907. $sql = "SELECT f.fieldid, v.value
  908. FROM ".TABLE_PREFIX."customfields f, ".TABLE_PREFIX."fieldvalues v
  909. WHERE f.fieldid=v.fieldid
  910. AND f.title='$title'
  911. AND v.pilotid=$pilotid";
  912.  
  913. $res = DB::get_row($sql);
  914. return $res->value;
  915. }
  916.  
  917.  
  918. /**
  919. * Get all of the groups a pilot is a member of
  920. *
  921. * @param int $pilotid The pilot ID
  922. * @return array Groups the pilot is in (groupid and groupname)
  923. *
  924. */
  925. public static function getPilotGroups($pilotid)
  926. {
  927. $pilotid = DB::escape($pilotid);
  928.  
  929. $sql = 'SELECT g.groupid, g.name
  930. FROM ' . TABLE_PREFIX . 'groupmembers u,'.TABLE_PREFIX.'groups g
  931. WHERE u.pilotid='.$pilotid.' AND g.groupid=u.groupid';
  932.  
  933. $ret = DB::get_results($sql);
  934.  
  935. return $ret;
  936. }
  937.  
  938. /**
  939. * This generates the forum signature of a pilot which
  940. * can be used wherever. It's dynamic, and adjusts it's
  941. * size, etc based on the background image.
  942. *
  943. * Each image is output into the /lib/signatures directory,
  944. * and is named by the pilot code+number (ie, VMA0001.png)
  945. *
  946. * This is called whenever a PIREP is accepted by an admin,
  947. * as not to burden a server with image generation
  948. *
  949. * Also requires GD to be installed on the server
  950. *
  951. * @param int The pilot ID for which to generate a signature for
  952. * @return bool Success
  953. */
  954. public function generateSignature($pilotid)
  955. {
  956. $pilot = self::getPilotData($pilotid);
  957.  
  958. $last_location = PIREPData::getLastReports($pilotid, 1, PIREP_ACCEPTED);
  959. $pilotcode = self::getPilotCode($pilot->code, $pilot->pilotid);
  960.  
  961. if(Config::Get('TRANSFER_HOURS_IN_RANKS') === true)
  962. {
  963. $totalhours = $pilot->totalhours + $pilot->transferhours;
  964. }
  965. else
  966. {
  967. $totalhours = $pilot->totalhours;
  968. }
  969.  
  970. # Configure what we want to show on each line
  971. $output = array();
  972. $output[] = $pilotcode.' '. $pilot->firstname.' '.$pilot->lastname;
  973. $output[] = $pilot->rank.', '.$pilot->hub;
  974. $output[] = 'Total Flights: ' . $pilot->totalflights;
  975. $output[] = 'Total Hours: ' . $totalhours;
  976.  
  977. if(Config::Get('SIGNATURE_SHOW_EARNINGS') == true)
  978. {
  979. $output[] = 'Total Earnings: ' . $pilot->totalpay;
  980. }
  981.  
  982. $output[] = 'Last Location: ' . $last_location->arricao;
  983.  
  984. # Load up our image
  985. # Get the background image the pilot selected
  986. if(empty($pilot->bgimage))
  987. $bgimage = SITE_ROOT.'/lib/signatures/background/background.png';
  988. else
  989. $bgimage = SITE_ROOT.'/lib/signatures/background/'.$pilot->bgimage;
  990.  
  991. if(!file_exists($bgimage))
  992. {
  993. # Doesn't exist so use the default
  994. $bgimage = SITE_ROOT.'/lib/signatures/background/background.png';
  995.  
  996. if(!file_exists($bgimage))
  997. {
  998. return false;
  999. }
  1000. }
  1001.  
  1002. $img = @imagecreatefrompng($bgimage);
  1003. if(!$img)
  1004. {
  1005. $img = imagecreatetruecolor(300, 50);
  1006. }
  1007.  
  1008. $height = imagesy($img);
  1009. $width = imagesx($img);
  1010.  
  1011. $txtcolor = str_replace('#', '', Config::Get('SIGNATURE_TEXT_COLOR'));
  1012. $color = sscanf($txtcolor, '%2x%2x%2x');
  1013. $textcolor = imagecolorallocate($img, $color[0], $color[1], $color[2]);
  1014. $font = 3; // Set the font-size
  1015.  
  1016. $xoffset = Config::Get('SIGNATURE_X_OFFSET'); # How many pixels, from left, to start
  1017. $yoffset = Config::Get('SIGNATURE_Y_OFFSET'); # How many pixels, from top, to start
  1018.  
  1019. $font = Config::Get('SIGNATURE_FONT_PATH');
  1020. $font_size = Config::Get('SIGNATURE_FONT_SIZE');
  1021.  
  1022. if(function_exists('imageantialias'))
  1023. {
  1024. imageantialias($img, true);
  1025. }
  1026.  
  1027.  
  1028. /* Font stuff */
  1029.  
  1030. if(!function_exists('imagettftext'))
  1031. {
  1032. Config::Set('SIGNATURE_USE_CUSTOM_FONT', false);
  1033. }
  1034.  
  1035. # The line height of each item to fit nicely, dynamic
  1036.  
  1037. if(Config::Get('SIGNATURE_USE_CUSTOM_FONT') == false)
  1038. {
  1039. $stepsize = imagefontheight($font);
  1040. $fontwidth = imagefontwidth($font);
  1041. }
  1042. else
  1043. {
  1044. // get the font width and step size
  1045. $bb = imagettfbbox ( $font_size, 0, $font, 'A');
  1046.  
  1047. $stepsize = $bb[3] - $bb[5] + Config::Get('SIGNATURE_FONT_PADDING');
  1048. $fontwidth = $bb[2] - $bb[0];
  1049. }
  1050.  
  1051.  
  1052. $currline = $yoffset;
  1053. $total = count($output);
  1054. for($i=0;$i<$total;$i++)
  1055. {
  1056. if(Config::Get('SIGNATURE_USE_CUSTOM_FONT') == false)
  1057. {
  1058. imagestring($img, $font, $xoffset, $currline, $output[$i], $textcolor);
  1059. }
  1060. else
  1061. {
  1062. // Use TTF
  1063. $tmp = imagettftext($img, $font_size, 0, $xoffset, $currline, $textcolor, $font, $output[$i]);
  1064.  
  1065. // Flag is placed at the end of of the first line, so have that bounding box there
  1066. if($i==0)
  1067. {
  1068. $flag_bb = $tmp;
  1069. }
  1070. }
  1071.  
  1072. $currline+=$stepsize;
  1073. }
  1074.  
  1075. # Add the country flag, line it up with the first line, which is the
  1076. # pilot code/name
  1077. $country = strtolower($pilot->location);
  1078. if(file_exists(SITE_ROOT.'/lib/images/countries/'.$country.'.png'))
  1079. {
  1080. $flagimg = imagecreatefrompng(SITE_ROOT.'/lib/images/countries/'.$country.'.png');
  1081.  
  1082. if(Config::Get('SIGNATURE_USE_CUSTOM_FONT') == false)
  1083. {
  1084. $ret = imagecopy($img, $flagimg, strlen($output[0])*$fontwidth,
  1085. ($yoffset+($stepsize/2)-5.5), 0, 0, 16, 11);
  1086. }
  1087. else
  1088. {
  1089. # figure out where it would go
  1090. $ret = imagecopy($img, $flagimg, $flag_bb[4]+5, $flag_bb[5]+2, 0, 0, 16, 11);
  1091. }
  1092. }
  1093.  
  1094. # Add the Rank image
  1095. if(Config::Get('SIGNATURE_SHOW_RANK_IMAGE') == true && $pilot->rankimage!=''
  1096. && file_exists($pilot->rankimage))
  1097. {
  1098. $ext = substr($pilot->rankimage, strlen($pilot->rankimage)-3, 3);
  1099.  
  1100. # Get the rank image type, just jpg, gif or png
  1101. if($ext == 'png')
  1102. $rankimg = @imagecreatefrompng($pilot->rankimage);
  1103. elseif($ext == 'gif')
  1104. $rankimg = @imagecreatefromgif($pilot->rankimage);
  1105. else
  1106. $rankimg = @imagecreatefromjpg($pilot->rankimage);
  1107.  
  1108. if(!$rankimg) { echo '';}
  1109. else
  1110. {
  1111. $r_width = imagesx($rankimg);
  1112. $r_height = imagesy($rankimg);
  1113.  
  1114. imagecopy($img, $rankimg, $width-$r_width-$xoffset, $yoffset, 0, 0, $r_width, $r_height);
  1115. }
  1116. }
  1117.  
  1118. if(Config::Get('SIGNATURE_SHOW_COPYRIGHT') == true)
  1119. {
  1120. #
  1121. # DO NOT remove this, as per the phpVMS license
  1122. $font = 1;
  1123. $text = 'powered by phpvms, '. SITE_NAME.' ';
  1124. imagestring($img, $font, $width-(strlen($text)*imagefontwidth($font)),
  1125. $height-imagefontheight($font), $text, $textcolor);
  1126. }
  1127.  
  1128. imagepng($img, SITE_ROOT.SIGNATURE_PATH.'/'.$pilotcode.'.png', 1);
  1129. imagedestroy($img);
  1130. }
  1131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement