Month.php
109 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Date_Picker; |
| 4 | |
| 5 | use IAWP\Utils\Timezone; |
| 6 | /** @internal */ |
| 7 | class Month |
| 8 | { |
| 9 | private $date; |
| 10 | private $first_month; |
| 11 | public function __construct(\DateTime $date, \DateTime $first_month) |
| 12 | { |
| 13 | $this->date = $date; |
| 14 | $this->first_month = $first_month; |
| 15 | } |
| 16 | public function name() : string |
| 17 | { |
| 18 | return \IAWPSCOPED\iawp()->date_i18n('F Y', $this->date); |
| 19 | } |
| 20 | public function date_string() : string |
| 21 | { |
| 22 | return $this->date->format('Y-m'); |
| 23 | } |
| 24 | public function days() : \DatePeriod |
| 25 | { |
| 26 | $start = $this->date->modify('first day of this month'); |
| 27 | $end = (clone $this->date)->modify('last day of this month'); |
| 28 | $end->setTime(23, 59, 59); |
| 29 | $interval = new \DateInterval('P1D'); |
| 30 | // 1 day interval |
| 31 | return new \DatePeriod($start, $interval, $end); |
| 32 | } |
| 33 | public function extra_cells() : int |
| 34 | { |
| 35 | $user_dow = \IAWPSCOPED\iawp()->get_option('iawp_dow', 0); |
| 36 | $start = $this->date->modify('first day of this month'); |
| 37 | $month_dow = $start->format('w'); |
| 38 | if ($month_dow >= $user_dow) { |
| 39 | return $month_dow - $user_dow; |
| 40 | } else { |
| 41 | return 7 - ($user_dow - $month_dow); |
| 42 | } |
| 43 | } |
| 44 | public function month_class() : string |
| 45 | { |
| 46 | $now = new \DateTime('now', Timezone::utc_timezone()); |
| 47 | $month = clone $this->date; |
| 48 | $class = 'iawp-calendar-month'; |
| 49 | if ($month->format('Y-m') === $this->first_month->format('Y-m')) { |
| 50 | $class .= ' iawp-first-month'; |
| 51 | } |
| 52 | if ($now->format('Y-m') === $month->format('Y-m')) { |
| 53 | $class .= ' iawp-current iawp-last-month'; |
| 54 | } |
| 55 | // Modifying $month here |
| 56 | if ($now->format('Y n') === $month->modify('first day of +1 month')->format('Y n')) { |
| 57 | $class .= ' iawp-previous'; |
| 58 | } |
| 59 | return $class; |
| 60 | } |
| 61 | public function day_class(\DateTime $day, string $first_data, string $start_date, string $end_date) : string |
| 62 | { |
| 63 | $date = $day->format('Y-m-d'); |
| 64 | $class = 'iawp-day iawp-cell'; |
| 65 | if ($date === \date('Y-m-d')) { |
| 66 | $class .= ' iawp-today'; |
| 67 | } |
| 68 | if ($day->format('j') == '1') { |
| 69 | $class .= ' first-of-month'; |
| 70 | } |
| 71 | if ($date === $first_data) { |
| 72 | $class .= ' iawp-first-data'; |
| 73 | } |
| 74 | if ($date < $first_data || $date > \date('Y-m-d')) { |
| 75 | $class .= ' out-of-range'; |
| 76 | } |
| 77 | if ($date === $start_date) { |
| 78 | $class .= ' iawp-start'; |
| 79 | if ($date === $end_date) { |
| 80 | $class .= ' iawp-end'; |
| 81 | } |
| 82 | } elseif ($date === $end_date) { |
| 83 | $class .= ' iawp-end'; |
| 84 | } elseif ($date > $start_date && $date < $end_date) { |
| 85 | $class .= ' in-range'; |
| 86 | } |
| 87 | return $class; |
| 88 | } |
| 89 | public function days_of_week() : string |
| 90 | { |
| 91 | $days = []; |
| 92 | $html = ''; |
| 93 | // Get the correct HTML |
| 94 | for ($i = 0; $i < 7; $i++) { |
| 95 | $days[] = '<span class="iawp-day-name">' . \date_i18n("D", \strtotime("Sunday +{$i} days")) . '</span>'; |
| 96 | } |
| 97 | // Shift based on the user's selection |
| 98 | for ($i = 0; $i < \IAWPSCOPED\iawp()->get_option('iawp_dow', 0); $i++) { |
| 99 | $first_day = \array_shift($days); |
| 100 | \array_push($days, $first_day); |
| 101 | } |
| 102 | // Create the HTMl string |
| 103 | foreach ($days as $day) { |
| 104 | $html .= $day; |
| 105 | } |
| 106 | return $html; |
| 107 | } |
| 108 | } |
| 109 |