Schedule.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. class Schedule {
  3. /** @var string[] $abbreviations */
  4. private $abbreviations;
  5. /** @var DateTime[] $displaydates */
  6. private $displaydates;
  7. /** @var string[] $stations */
  8. private $stations;
  9. /** @var string[] $stations */
  10. private $optstations;
  11. /** @var array[] $stations */
  12. private $wantedstations;
  13. /** @var array $improps */
  14. private $improps;
  15. /** @var GDIMAGE $this->image */
  16. private $image;
  17. /** @var boolean $darkmode */
  18. private $darkmode;
  19. /** @var boolean $debug */
  20. private $debug;
  21. public function __construct(array $displaydates, array $stations, array $optstations = [], $wantedstations = [], $darkmode = false) {
  22. $this->abbreviations = [];
  23. $this->training = $this->event = false;
  24. $this->displaydates = $displaydates;
  25. $this->stations = $stations;
  26. $this->optstations = $optstations;
  27. $this->wantedstations = $wantedstations;
  28. $this->improps = [];
  29. $this->darkmode = $darkmode;
  30. }
  31. public function createImage() {
  32. $rawData = $this->fetchData();
  33. $relevantBookings = [];
  34. foreach($this->stations as $station) {
  35. $relevantBookings[$station] = [];
  36. }
  37. foreach($rawData as $item) {
  38. if(in_array($item['station']['ident'], $this->stations)) {
  39. $relevantBookings[$item['station']['ident']][] = [
  40. 'starts_at' => new DateTime($item['starts_at']),
  41. 'ends_at' => new DateTime($item['ends_at']),
  42. 'lastname' => $item['controller']['lastname'],
  43. 'firstname' => $item['controller']['firstname'],
  44. 'training' => $item['training'],
  45. 'event' => $item['event']
  46. ];
  47. }
  48. }
  49. putenv('GDFONTPATH=' . realpath('.'));
  50. $this->improps['font'] = "UbuntuMono-Regular";
  51. // $this->improps['font'] = "EuroScope";
  52. $this->improps['width'] = 100 * count($this->displaydates) + 100;
  53. $this->improps['height'] = 25 * count($this->stations) + 600;
  54. if(!$this->darkmode) {
  55. $this->image = imagecreatetruecolor($this->improps['width'], $this->improps['height']);
  56. $this->improps['bg'] = imagecolorallocate($this->image, 255, 255, 255);
  57. $this->improps['black'] = imagecolorexact($this->image, 0, 0, 0);
  58. $this->improps['open'] = imagecolorexact($this->image, 155, 155, 155);
  59. $this->improps['wanted'] = imagecolorexact($this->image, 255, 0, 0);
  60. $this->improps['event'] = imagecolorexact($this->image, 0, 255, 0);
  61. $this->improps['training'] = imagecolorexact($this->image, 50, 100, 255);
  62. $this->improps['eventtraining'] = imagecolorexact($this->image, 0, 255, 255);
  63. } else {
  64. $this->image = imagecreatetruecolor($this->improps['width'], $this->improps['height']);
  65. $this->improps['bg'] = imagecolorallocate($this->image, 0, 0, 0);
  66. $this->improps['black'] = imagecolorexact($this->image, 255, 255, 255);
  67. $this->improps['open'] = imagecolorexact($this->image, 155, 155, 155);
  68. $this->improps['wanted'] = imagecolorexact($this->image, 255, 0, 0);
  69. $this->improps['event'] = imagecolorexact($this->image, 0, 255, 0);
  70. $this->improps['training'] = imagecolorexact($this->image, 50, 100, 255);
  71. $this->improps['eventtraining'] = imagecolorexact($this->image, 0, 255, 255);
  72. }
  73. imagefill($this->image, 0, 0, $this->improps['bg']);
  74. $this->improps['lineheight'] = 20;
  75. $sepheight = 25;
  76. for($i = 0; $i < count($this->displaydates); $i++) {
  77. imagefttext($this->image, 11, 0, ($i) * 100 + 100, 25, $this->improps['black'], $this->improps['font'], $this->displaydates[$i]->format("D d.m."));
  78. }
  79. $height = 40;
  80. foreach($this->stations as $station) {
  81. if($this->debug) echo "\n". $station . "\th$height";
  82. imageline($this->image, 0, $height + 5, $this->improps['width'], $height + 5, $this->improps['open']);
  83. if($station == '--') {
  84. $height += $sepheight;
  85. continue;
  86. }
  87. $height += $this->improps['lineheight'];
  88. $lines = 0;
  89. for($i = 0; $i < count($this->displaydates); $i++) {
  90. $l = $this->addBookingData($station, $this->displaydates[$i], $relevantBookings[$station], $i * 100 + 100, $height);
  91. $lines = $l > $lines ? $l : $lines;
  92. if($this->debug) echo "\t" . $this->displaydates[$i]->format("m-d") . ": " . $lines;
  93. }
  94. if(!in_array($station, $this->optstations) && $lines < 1) $lines = 1;
  95. if($this->debug) echo "\tl$lines";
  96. if($lines > 0) {
  97. imagefttext($this->image, 11, 0, 10, $height, $this->improps['black'], $this->improps['font'], $station);
  98. imageline($this->image, 0, $height + 5, $this->improps['width'], $height + 5, $this->improps['open']);
  99. }
  100. $height += ($lines - 1) * $this->improps['lineheight'];
  101. }
  102. for($i = 0; $i < count($this->displaydates); $i++) {
  103. imageline($this->image, 100 * $i + 92, 0, 100 * $i + 92, $height + 5, $this->improps['open']);
  104. }
  105. $height += $this->improps['lineheight'] + 5;
  106. imagefttext($this->image, 11, 0, 10, $height, $this->improps['wanted'], $this->improps['font'], 'Wanted');
  107. imagefttext($this->image, 11, 0, 100, $height, $this->improps['training'], $this->improps['font'], 'Training');
  108. imagefttext($this->image, 11, 0, 200, $height, $this->improps['event'], $this->improps['font'], 'Event');
  109. imagefttext($this->image, 11, 0, 300, $height, $this->improps['eventtraining'], $this->improps['font'], 'Event + Training');
  110. $height += $this->improps['lineheight'] * 2;
  111. $namelength = 0;
  112. asort($this->abbreviations);
  113. foreach($this->abbreviations as $name) {
  114. $namelength = strlen($name) > $namelength ? strlen($name) : $namelength;
  115. }
  116. $collength = ($namelength + 10) * 8;
  117. $cols = floor( ($this->improps['width'] - 10) / $collength);
  118. $col = 0;
  119. $row = 0;
  120. foreach($this->abbreviations as $abbrv => $name) {
  121. imagefttext($this->image, 11, 0, $col * $collength + 10, $height + $row * $this->improps['lineheight'], $this->improps['black'], $this->improps['font'], $abbrv . ": " . $name);
  122. $row = ($col + 1) >= $cols ? $row + 1 : $row;
  123. $col = ($col + 1) >= $cols ? 0 : $col + 1;
  124. }
  125. $height += $this->improps['lineheight'] * ($row +2);
  126. imagefttext($this->image, 11, 0, 10, $height, $this->improps['open'], $this->improps['font'], "Generated " . (new DateTime("now", new DateTimeZone('UTC')))->format('d.m.Y H:i:s') . "z" );
  127. $this->image = imagecrop($this->image, ['x' => 0, 'y' => 0, 'width' => $this->improps['width'], 'height' => $height+5]);
  128. return $this->image;
  129. }
  130. private function addBookingData($station, $date, $bookings, $x, $height) {
  131. $count = 0;
  132. foreach($bookings as $booking) {
  133. if($booking['starts_at']->format('Y-m-d') == $date->format('Y-m-d')) {
  134. $abbrv = substr($booking['firstname'], 0, 3) . substr($booking['lastname'], 0, 3); //TODO
  135. $this->abbreviations[$abbrv] = $booking['firstname'] . " " . $booking['lastname'];
  136. if($booking['training'] && $booking['event']) $color = $this->improps['eventtraining'];
  137. elseif($booking['training']) $color = $this->improps['training'];
  138. elseif($booking['event']) $color = $this->improps['event'];
  139. else $color = $this->improps['black'];
  140. imagefttext($this->image, 11, 0, $x, $height + $count * $this->improps['lineheight'], $color,
  141. $this->improps['font'], $abbrv . " " . $booking['starts_at']->format('H') . "-" . $booking['ends_at']->format('H'));
  142. $count++;
  143. }
  144. }
  145. if($count == 0 && !in_array($station, $this->optstations)) {
  146. if(!$this->dayIsWanted($station, $date))
  147. imagefttext($this->image, 10, 0, $x, $height, $this->improps['open'], $this->improps['font'], "Open");
  148. else
  149. imagefttext($this->image, 10, 0, $x, $height, $this->improps['wanted'], $this->improps['font'], "WANTED!");
  150. }
  151. return $count;
  152. }
  153. private function fetchData() {
  154. $url = sprintf("https://vatsim-germany.org/api/booking/atc/daterange/%s/%s", $this->displaydates[0]->format("d.m.Y"), $this->displaydates[count($this->displaydates) -1]->format("d.m.Y"));
  155. $curl = curl_init();
  156. curl_setopt($curl, CURLOPT_URL, $url ) ;
  157. curl_setopt($curl, CURLOPT_HTTPHEADER, ["X-Requested-With: XMLHttpRequest"]);
  158. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  159. // curl_setopt($curl, CURLOPT_VERBOSE, true);
  160. // $verbose = fopen('php://temp', 'w+');
  161. // curl_setopt($curl, CURLOPT_STDERR, $verbose);
  162. $result = curl_exec($curl);
  163. // if ($result === FALSE) {
  164. // printf("cUrl error (#%d): %s<br>\n", curl_errno($curl),
  165. // htmlspecialchars(curl_error($curl)));
  166. // }
  167. // rewind($verbose);
  168. // $verboseLog = stream_get_contents($verbose);
  169. // echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog), "</pre>\n";
  170. curl_close($curl);
  171. return json_decode($result, true);
  172. }
  173. public static function EveryXDaysFromStartdate($startdate, $x = 1, $numberofdates = 1) {
  174. $startdate = new DateTime($startdate, new DateTimeZone('UTC'));
  175. $days = [];
  176. while(count($days) < $numberofdates) {
  177. if(new DateTime('today', new DateTimeZone('UTC')) <= $startdate) {
  178. $days[] = clone($startdate);
  179. }
  180. $startdate->add(new DateInterval(sprintf("P%dD", $x)));
  181. }
  182. return $days;
  183. }
  184. public static function EveryXWeeksFromStartdate($startdate, $x = 1, $numberofdates = 1) {
  185. return self::EveryXDaysFromStartdate($startdate, $x * 7, $numberofdates);
  186. }
  187. public static function EveryWeekday($dayoftheweek, $numberofdates = 1) {
  188. $startdate = new DateTime($dayoftheweek, new DateTimeZone('UTC'));
  189. $days = [];
  190. while(count($days) < $numberofdates) {
  191. $days[] = clone($startdate);
  192. $startdate->add(new DateInterval("P7D"));
  193. }
  194. return $days;
  195. }
  196. public static function WholeWeek($startdate = "today") {
  197. $days = [];
  198. $startdate = new DateTime($startdate, new DateTimeZone('UTC'));
  199. for($i = 0; $i < 7; $i++) {
  200. $days[] = clone($startdate);
  201. $startdate->add(new DateInterval("P1D"));
  202. }
  203. return $days;
  204. }
  205. public static function processWantedDays(...$args) {
  206. if(count($args) % 2 == 1) throw new Exception("Even number of arguments required.");
  207. $r = [];
  208. for($i = 0; $i < count($args); $i += 2) {
  209. foreach($args[$i] as $station) {
  210. if(!isset($r[$station])) $r[$station] = [];
  211. foreach($args[$i + 1] as $date) {
  212. $r[$station][] = $date;
  213. }
  214. }
  215. }
  216. return $r;
  217. }
  218. private function dayIsWanted($station, $date) {
  219. if(!isset($this->wantedstations[$station])) return false;
  220. foreach($this->wantedstations[$station] as $target) {
  221. if($target->format("Y-m-d") == $date->format("Y-m-d")) return true;
  222. }
  223. return false;
  224. }
  225. }