Tabs
6 years ago
DSTemplate.php
6 years ago
DailyTimetablePrinter.php
6 years ago
HelpsAndTips.php
6 years ago
MonthlyTimetablePrinter.php
6 years ago
TimetablePrinter.php
6 years ago
dptWidgetForm.php
7 years ago
horizontal-div.php
9 years ago
widget-admin.php
6 years ago
TimetablePrinter.php
542 lines
| 1 | <?php |
| 2 | |
| 3 | require_once(__DIR__ . '/../Models/HijriDate.php'); |
| 4 | |
| 5 | class TimetablePrinter |
| 6 | { |
| 7 | protected $prayerLocal = array( |
| 8 | "fajr" => "Fajr", |
| 9 | "sunrise" => "Sunrise", |
| 10 | "zuhr" => "Zuhr", |
| 11 | "asr" => "Asr", |
| 12 | "maghrib" => "Maghrib", |
| 13 | "isha" => "Isha" |
| 14 | ); |
| 15 | |
| 16 | protected $headersLocal = array( |
| 17 | "prayer" => "Prayer", |
| 18 | "begins" => "Begins", |
| 19 | "iqamah" => "Iqamah", |
| 20 | "standard" => "Standard", |
| 21 | "hanafi" => "Hanafi", |
| 22 | "fast_begins" => "Fast Begins", |
| 23 | "fast_ends" => "Fast Ends", |
| 24 | "jumuah" => "Jumuah" |
| 25 | ); |
| 26 | |
| 27 | protected $numbersLocal = array( |
| 28 | 0 => '0', |
| 29 | 1 => '1', |
| 30 | 2 => '2', |
| 31 | 3 => '3', |
| 32 | 4 => '4', |
| 33 | 5 => '5', |
| 34 | 6 => '6', |
| 35 | 7 => '7', |
| 36 | 8 => '8', |
| 37 | 9 => '9' |
| 38 | ); |
| 39 | |
| 40 | protected $monthsLocal = array( |
| 41 | 'january' => 'January', |
| 42 | 'february' => 'February', |
| 43 | 'march' => 'March', |
| 44 | 'april' => 'April', |
| 45 | 'may' => 'May', |
| 46 | 'june' => 'June', |
| 47 | 'july' => 'July', |
| 48 | 'august' => 'August', |
| 49 | 'september' => 'September', |
| 50 | 'october' => 'October', |
| 51 | 'november' => 'November', |
| 52 | 'december' => 'December' |
| 53 | ); |
| 54 | |
| 55 | |
| 56 | /** @var array */ |
| 57 | protected $timesLocal = array( |
| 58 | 'date' => 'Date', |
| 59 | 'day' => 'Day', |
| 60 | 'minute' => 'Minutes', |
| 61 | 'hours' => 'Hours', |
| 62 | 'iqamah update' => 'IQAMAH UPDATE FOR TOMORROW', |
| 63 | 'next prayer' => 'Next ...' |
| 64 | ); |
| 65 | |
| 66 | /** @var string */ |
| 67 | protected $tableClass; |
| 68 | |
| 69 | /** @var array */ |
| 70 | protected $localPrayerNames; |
| 71 | |
| 72 | /** @var array */ |
| 73 | protected $localHeaders; |
| 74 | |
| 75 | /** @var array */ |
| 76 | protected $localNumbers; |
| 77 | |
| 78 | /** @var array */ |
| 79 | protected $localTimes; |
| 80 | |
| 81 | /** @var HijriDate */ |
| 82 | protected $hijriDate; |
| 83 | |
| 84 | /** @var string */ |
| 85 | protected $hijridateString; |
| 86 | |
| 87 | /** $var bool */ |
| 88 | protected $isVertical = false; |
| 89 | |
| 90 | /** $var bool */ |
| 91 | protected $isHorizontal = false; |
| 92 | |
| 93 | /** |
| 94 | * TimetablePrinter constructor. |
| 95 | */ |
| 96 | public function __construct() |
| 97 | { |
| 98 | $this->localPrayerNames = $this->getLocalPrayerNames(); |
| 99 | $this->localHeaders = $this->getLocalHeaders(); |
| 100 | $this->localNumbers = $this->getLocalNumbers(); |
| 101 | $this->localTimes = $this->getLocalTimes(); |
| 102 | |
| 103 | $this->hijriDate = new HijriDate(); |
| 104 | $this->hijridateString = $this->hijriDate->getDate(date("d"), date("m"), date("Y"), true); |
| 105 | } |
| 106 | |
| 107 | public function getTableClass() |
| 108 | { |
| 109 | return get_option('hideTableBorder'); |
| 110 | } |
| 111 | |
| 112 | public function getLocalPrayerNames($forAdmin=false, $enableJumuah=false) |
| 113 | { |
| 114 | $prayers_local = get_option('prayersLocal'); |
| 115 | |
| 116 | $localPrayerName = $prayers_local; |
| 117 | if ( empty($prayers_local)) { |
| 118 | $localPrayerName = $this->prayerLocal; |
| 119 | } elseif (count($prayers_local) != count($this->prayerLocal)) { |
| 120 | delete_option( 'prayersLocal' ); |
| 121 | $localPrayerName = $this->prayerLocal; |
| 122 | } |
| 123 | |
| 124 | if (! $forAdmin && $enableJumuah){ |
| 125 | if ($this->todayIsFriday()) { |
| 126 | $localPrayerName['zuhr'] = $this->getLocalHeaders()['jumuah']; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return array_map('stripslashes', $localPrayerName); |
| 131 | } |
| 132 | |
| 133 | public function getLocalHeaders() |
| 134 | { |
| 135 | $headers_local = get_option('headersLocal'); |
| 136 | |
| 137 | if ( empty($headers_local)) { |
| 138 | return $this->headersLocal; |
| 139 | } elseif (count($headers_local) != count($this->headersLocal)) { |
| 140 | delete_option( 'headersLocal' ); |
| 141 | return $this->headersLocal; |
| 142 | } |
| 143 | |
| 144 | return array_map('stripslashes', $headers_local); |
| 145 | } |
| 146 | |
| 147 | public function getLocalMonths() |
| 148 | { |
| 149 | $monthsLocal = get_option('monthsLocal'); |
| 150 | |
| 151 | if ( empty( $monthsLocal )) { |
| 152 | $monthsLocal = $this->monthsLocal; |
| 153 | } |
| 154 | |
| 155 | if ( get_option("ramadan-chbox") ) { |
| 156 | $monthsLocal['ramadan'] = 'Ramadan'; |
| 157 | } else { |
| 158 | unset( $monthsLocal['ramadan'] ); |
| 159 | } |
| 160 | |
| 161 | return array_map('stripslashes', $monthsLocal); |
| 162 | } |
| 163 | |
| 164 | public function getLocalNumbers() |
| 165 | { |
| 166 | $numbers_local = get_option('numbersLocal'); |
| 167 | |
| 168 | return empty($numbers_local) ? $this->numbersLocal : $numbers_local; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | public function getLocalTimes() |
| 173 | { |
| 174 | $times = get_option('timesLocal'); |
| 175 | |
| 176 | if ( empty($times)) { |
| 177 | return $this->timesLocal; |
| 178 | } elseif (count($times) != count($this->timesLocal)) { |
| 179 | delete_option( 'timesLocal' ); |
| 180 | return $this->timesLocal; |
| 181 | } |
| 182 | |
| 183 | return $times; |
| 184 | } |
| 185 | |
| 186 | public function setVertical($value=true) |
| 187 | { |
| 188 | $this->isVertical = $value; |
| 189 | } |
| 190 | |
| 191 | public function setHorizontal($value=true) |
| 192 | { |
| 193 | $this->isHorizontal = $value; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * @param string $mysqlDate |
| 198 | * @param string $format |
| 199 | * @return string |
| 200 | */ |
| 201 | public function formatDate($mysqlDate, $format=null) |
| 202 | { |
| 203 | $phpDate = strtotime($mysqlDate); |
| 204 | |
| 205 | $date = date( get_option('date_format'), $phpDate ); |
| 206 | if ($format) { |
| 207 | $date = date($format, $phpDate); |
| 208 | } |
| 209 | |
| 210 | return $date; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * @param string $mysqlDate |
| 215 | * @return string |
| 216 | */ |
| 217 | public function formatDateForPrayer($mysqlDate, $imsak=false) |
| 218 | { |
| 219 | $phpDate = strtotime($mysqlDate); |
| 220 | if ($imsak) { |
| 221 | $phpDate = $phpDate - ((int)get_option('imsaq') * 60); |
| 222 | } |
| 223 | $wpDate = date(get_option('time_format'), $phpDate); |
| 224 | |
| 225 | $result = str_split($wpDate); |
| 226 | $intlDate = ''; |
| 227 | $this->localNumbers = $this->getLocalNumbers(); |
| 228 | foreach ($result as $number) { |
| 229 | $intlDate .= $this->localNumbers[$number]; |
| 230 | if (empty($this->localNumbers[$number]) && $number !== '0') { |
| 231 | $intlDate .= $number; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | return $intlDate; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * @param string $month |
| 240 | * @param string $day |
| 241 | * @return string |
| 242 | */ |
| 243 | protected function getClass($month, $day) |
| 244 | { |
| 245 | if ($day == user_current_time('j') && $month == user_current_time('m')){ |
| 246 | return "class = highlight"; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * @param bool $isRamadan |
| 252 | * @param array $data |
| 253 | * @param bool $azanOnly |
| 254 | * @return string|null |
| 255 | */ |
| 256 | protected function getFastingTdWithData($isRamadan, $data, $azanOnly=null, $imsaq=false) |
| 257 | { |
| 258 | $html = ""; |
| 259 | |
| 260 | if ($isRamadan && ! $azanOnly && $imsaq) { |
| 261 | $html = "<td class='fasting'>" . $this->formatDateForPrayer($data, $imsaq) . "</td>"; |
| 262 | } elseif ($isRamadan && $imsaq) { |
| 263 | $html = "<td class='fasting'>" . $this->formatDateForPrayer($data, $imsaq). "</td>"; |
| 264 | $html .= "<td>" . $this->formatDateForPrayer($data). "</td>"; |
| 265 | |
| 266 | } elseif ($isRamadan && ! $imsaq) { |
| 267 | $html = "<td class='fasting'>" . $this->formatDateForPrayer($data). "</td>"; |
| 268 | } elseif ($isRamadan && ! $azanOnly && $imsaq) { |
| 269 | $html = "<td class='fasting'>" . $this->formatDateForPrayer($data). "</td>"; |
| 270 | } elseif ($azanOnly) { |
| 271 | $html = "<td>" . $this->formatDateForPrayer($data). "</td>"; |
| 272 | } |
| 273 | |
| 274 | return $html; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * @param array $row |
| 279 | * |
| 280 | * @param int $countStart |
| 281 | * @return string |
| 282 | */ |
| 283 | protected function getNextIqamahTime(array $row) |
| 284 | { |
| 285 | $diff = $this->getNextIqamahTimeDiff($row); |
| 286 | $nextPrayer = $this->getNextPrayer($row); |
| 287 | |
| 288 | return |
| 289 | ' |
| 290 | <div class="dptScNextPrayer"> |
| 291 | <span class="green"> |
| 292 | <span class="nextPrayer">' . $this->getHeading($row, $nextPrayer). '</span> ' . |
| 293 | $this->getTimeLeftString($diff, $row); |
| 294 | } |
| 295 | |
| 296 | protected function getTimeLeftString($nextIqamah, $row) |
| 297 | { |
| 298 | if ($nextIqamah) { |
| 299 | $timeLeftText = $this->getLocalizedNumber( $nextIqamah ) .':00'; |
| 300 | $minLeftText = $this->localTimes["minute"]; |
| 301 | if ($nextIqamah > 60) { |
| 302 | $hours = $nextIqamah / 60; |
| 303 | $hours = (int)$hours; |
| 304 | $mins = $nextIqamah % 60; |
| 305 | $mins = (int)$mins; |
| 306 | $timeLeftText = $this->getLocalizedNumber( $hours ) .' '.$this->localTimes["hours"] .' '. $this->getLocalizedNumber( $mins ); |
| 307 | } |
| 308 | |
| 309 | } |
| 310 | return $this->getNextPrayerTime($row, $nextIqamah, $timeLeftText, $minLeftText); |
| 311 | } |
| 312 | |
| 313 | protected function getNextIqamahTimeDiff(array $row) |
| 314 | { |
| 315 | $jamahTime = $this->getJamahTime( $row ); |
| 316 | $now = current_time( 'H:i'); |
| 317 | foreach ($jamahTime as $key=>$jamah) { |
| 318 | $this->nextIqamah = $this->localPrayerNames[lcfirst($key)] . ' ' . $this->localHeaders['iqamah'] . ':'; |
| 319 | if ($jamah >$now ) { |
| 320 | if ($key == 'Sunrise') { |
| 321 | $this->nextIqamah = $this->localPrayerNames[lcfirst($key)] . ':'; |
| 322 | } |
| 323 | if ($key == 'Zuhr' && $this->isJumahDisplay($row)) { |
| 324 | $this->nextIqamah = $this->localPrayerNames[lcfirst($key)] . ':'; |
| 325 | } |
| 326 | $toTime = strtotime( $jamah ); |
| 327 | $fromTime = strtotime( $now ); |
| 328 | $diff = round(abs($toTime - $fromTime)/60,2); |
| 329 | |
| 330 | return $diff; |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * @param $row |
| 337 | * |
| 338 | * @return string |
| 339 | */ |
| 340 | protected function getNextPrayer($row) |
| 341 | { |
| 342 | $now = current_time( 'H:i'); |
| 343 | |
| 344 | $jamahTime = $this->getJamahTime( $row ); |
| 345 | foreach ($jamahTime as $jamah) { |
| 346 | if ($jamah > $now ) { |
| 347 | $prayer = array_search( $jamah, $row ); // asr_jamah or asr_begins |
| 348 | $prayer = explode( '_', $prayer); |
| 349 | return $prayer[0]; // asr |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | protected function getHeading($dbRow, $nextPrayer) |
| 355 | { |
| 356 | $iqamah = ($nextPrayer == 'sunrise') ? '' : $this->localHeaders['iqamah']; |
| 357 | if ( is_null($nextPrayer)) { |
| 358 | return $this->localPrayerNames['fajr'].' '. $iqamah; |
| 359 | } |
| 360 | if ( $this->isJumahDisplay($dbRow) ) { |
| 361 | return $this->getLocalHeaders()['jumuah']; |
| 362 | } |
| 363 | |
| 364 | return $this->localPrayerNames[$nextPrayer] .' '. $iqamah; |
| 365 | } |
| 366 | |
| 367 | protected function getNextPrayerTime($dbRow, $nextIqamah, $timeLeftText, $minLeftText) |
| 368 | { |
| 369 | $nextPrayer = $this->getNextPrayer($dbRow); |
| 370 | |
| 371 | $key = ($nextPrayer == 'sunrise') ? $nextPrayer : strtolower($nextPrayer.'_jamah'); |
| 372 | $nextPrayerName = $dbRow[$key]; |
| 373 | if ( is_null($nextPrayer) ) { |
| 374 | $nextPrayerName = $dbRow['nextFajr']; |
| 375 | } |
| 376 | |
| 377 | if ( $this->isJumahDisplay($dbRow) ) { |
| 378 | return '<p class="jumuah">' . get_option('jumuah') . '</span>'; |
| 379 | } else { |
| 380 | return |
| 381 | '<h2 class="dptScTime">' . |
| 382 | $this->formatDateForPrayer($nextPrayerName). ' |
| 383 | </h2> |
| 384 | <span class="timeLeftCountDown timeLeft '.$this->getIqamahClass( $nextIqamah ).'"> |
| 385 | '. $timeLeftText .' |
| 386 | </span><span class="minLeftText"> ' . $minLeftText .'</span> |
| 387 | </div>'; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | |
| 392 | /** |
| 393 | * @param array $row |
| 394 | * |
| 395 | * @return array |
| 396 | */ |
| 397 | protected function getJamahTime(array $row) |
| 398 | { |
| 399 | $value = array( $row["fajr_jamah"], $row['sunrise'], $row["zuhr_jamah"], $row["asr_jamah"], $row["maghrib_jamah"], $row["isha_jamah"]); |
| 400 | |
| 401 | return array_combine( array_keys($this->prayerLocal), $value ); |
| 402 | |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * @param array $row |
| 407 | * |
| 408 | * @return array |
| 409 | */ |
| 410 | protected function getAzanTime(array $row) |
| 411 | { |
| 412 | $value = array( $row["fajr_begins"], $row['sunrise'], $row["zuhr_begins"], $row["asr_begins"], $row["maghrib_begins"], $row["isha_begins"]); |
| 413 | |
| 414 | return array_combine( array_keys($this->prayerLocal), $value ); |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * @param $numbers |
| 419 | * |
| 420 | * @return string |
| 421 | */ |
| 422 | public function getLocalizedNumber($numbers) |
| 423 | { |
| 424 | $numbers = str_split( $numbers ); |
| 425 | $localNumber = ""; |
| 426 | foreach ($numbers as $number) { |
| 427 | $localNumber .= $this->localNumbers[$number]; |
| 428 | } |
| 429 | |
| 430 | return $localNumber; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * @param $number |
| 435 | * |
| 436 | * @return string |
| 437 | */ |
| 438 | protected function getIqamahClass($number) |
| 439 | { |
| 440 | if ( $number > 30 ) { |
| 441 | return 'green'; |
| 442 | } elseif ( $number > 15 ) { |
| 443 | return 'orange'; |
| 444 | } |
| 445 | |
| 446 | return 'red'; |
| 447 | } |
| 448 | |
| 449 | protected function getJamahChange(array $row, $isDigitalScreen=false, $orientation="") |
| 450 | { |
| 451 | $style = null; |
| 452 | if ($this->isVertical && ! $isDigitalScreen) { |
| 453 | $style = "style='display: block;'"; |
| 454 | } |
| 455 | $timeClass = ""; |
| 456 | $digitalScreenClass = ""; |
| 457 | if ($isDigitalScreen) { |
| 458 | $timeClass = "class='x-time-change'"; |
| 459 | $digitalScreenClass = "jamahChanges-" . $orientation; |
| 460 | |
| 461 | } |
| 462 | $timeRelated = $this->getLocalTimes(); |
| 463 | $print = "<div class='jamahChanges " . $digitalScreenClass . "'> |
| 464 | <span class='x-time-text'>" . stripslashes($timeRelated['iqamah update']) . "</span>"; |
| 465 | $prayerNames = $this->getLocalPrayerNames(); |
| 466 | |
| 467 | foreach($row['jamah_changes'] as $key=>$time) { |
| 468 | if (! empty($key) ){ |
| 469 | $prayer = explode('_', $key); |
| 470 | if ( $this->tomorrowIsFriday() ) { |
| 471 | $prayerNames['zuhr'] = $this->getLocalHeaders()['jumuah']; |
| 472 | } else { |
| 473 | $prayerNames['zuhr'] = $this->prayerLocal['zuhr']; |
| 474 | } |
| 475 | $print .= "<span " . $style . $timeClass ." >" . $prayerNames[$prayer[0]] . ": " . $this->getTimeForIqamahUpdate($prayerNames[$prayer[0]], $time) . "</span>"; |
| 476 | } |
| 477 | } |
| 478 | $print .= "</div>"; |
| 479 | |
| 480 | return $print; |
| 481 | } |
| 482 | |
| 483 | private function getTimeForIqamahUpdate($key, $time) |
| 484 | { |
| 485 | $jumuahTime = get_option('jumuah'); |
| 486 | if ( $key === $this->getLocalHeaders()['jumuah'] && $jumuahTime) { |
| 487 | return $jumuahTime; |
| 488 | } |
| 489 | return $this->formatDateForPrayer($time); |
| 490 | } |
| 491 | |
| 492 | protected function todayIsFriday() |
| 493 | { |
| 494 | return date_i18n('D') == 'Fri'; |
| 495 | } |
| 496 | |
| 497 | protected function tomorrowIsFriday() |
| 498 | { |
| 499 | return date_i18n('D') == 'Thu'; |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * @param $day |
| 504 | * @param $month |
| 505 | * @param $year |
| 506 | * @param array $dbRow |
| 507 | * @param bool $forMonth |
| 508 | * @return string |
| 509 | */ |
| 510 | public function getHijriDate($day, $month, $year, $dbRow, $forMonth=false) |
| 511 | { |
| 512 | $hijriCheckbox = get_option('hijri-chbox'); |
| 513 | if ( ! empty($hijriCheckbox) ) { |
| 514 | if( ! empty($dbRow['hijri_date']) ) { |
| 515 | $hijriDate = $dbRow['hijri_date']; |
| 516 | } else { |
| 517 | $hijridateArray = $this->hijriDate->getDate($day, $month, $year, false, $this->isSunset($dbRow, $forMonth)); |
| 518 | $hijriDate = $hijridateArray['day']. ' '. $hijridateArray['month'] . ' ' . $hijridateArray['year']; |
| 519 | } |
| 520 | |
| 521 | return '<p class="hijriDate"> '. $hijriDate .'</p>'; |
| 522 | } |
| 523 | return; |
| 524 | } |
| 525 | |
| 526 | public function isJumahDisplay($dbRow) |
| 527 | { |
| 528 | if ( date_i18n('D') == 'Fri' && |
| 529 | current_time('timestamp') > strtotime($dbRow['sunrise']) && |
| 530 | current_time('timestamp') <= strtotime($dbRow['zuhr_jamah']) + 60 |
| 531 | ) { |
| 532 | return true; |
| 533 | } |
| 534 | |
| 535 | return false; |
| 536 | } |
| 537 | |
| 538 | private function isSunset($dbRow, $forMonth=false) |
| 539 | { |
| 540 | return !$forMonth && current_time('timestamp') > strtotime($dbRow['maghrib_begins']); |
| 541 | } |
| 542 | } |