Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 1 | <?php |
Adrià Vilanova Martínez | 5af8651 | 2023-12-02 20:44:16 +0100 | [diff] [blame^] | 2 | /* |
| 3 | * hores |
| 4 | * Copyright (c) 2023 Adrià Vilanova Martínez |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as |
| 8 | * published by the Free Software Foundation, either version 3 of the |
| 9 | * License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public |
| 17 | * License along with this program. |
| 18 | * If not, see http://www.gnu.org/licenses/. |
| 19 | */ |
| 20 | |
Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 21 | class calendars { |
| 22 | const TYPE_FESTIU = 0; |
| 23 | const TYPE_FEINER = 1; |
| 24 | const TYPE_LECTIU = 2; |
| 25 | |
| 26 | const NO_CALENDAR_APPLICABLE = 0; |
| 27 | |
| 28 | public static $types = array( |
| 29 | 0 => "Festivo", |
| 30 | 2 => "Lectivo", |
| 31 | 1 => "No lectivo" |
| 32 | ); |
| 33 | public static $workingTypes = [1, 2]; |
| 34 | public static $days = ["Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado", "Domingo"]; |
| 35 | public static $months = ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"]; |
| 36 | |
| 37 | public static function parseFormCalendar($form, $ibegins, $iends, $timestamp = false) { |
| 38 | if ($timestamp) { |
| 39 | $current = new DateTime(); |
| 40 | $current->setTimestamp((int)$ibegins); |
| 41 | $ends = new DateTime(); |
| 42 | $ends->setTimestamp((int)$iends); |
| 43 | } else { |
| 44 | $current = new DateTime($ibegins); |
| 45 | $ends = new DateTime($iends); |
| 46 | } |
| 47 | $interval = new DateInterval("P1D"); |
| 48 | |
| 49 | if ($current->diff($ends)->invert === 1) { |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | $return = array( |
| 54 | "begins" => $current->getTimestamp(), |
| 55 | "ends" => $ends->getTimestamp(), |
| 56 | "calendar" => [] |
| 57 | ); |
| 58 | |
| 59 | $possible_values = array_keys(self::$types); |
| 60 | |
| 61 | $day = 0; |
| 62 | while ($current->diff($ends)->invert === 0) { |
| 63 | if (!isset($form[$day]) || !in_array($form[$day], $possible_values)) { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | $return["calendar"][$current->getTimestamp()] = (int)$form[$day]; |
| 68 | |
| 69 | $day++; |
| 70 | $current->add($interval); |
| 71 | } |
| 72 | |
| 73 | return $return; |
| 74 | } |
| 75 | |
| 76 | public static function checkOverlap($category, $begins, $ends) { |
| 77 | global $con; |
| 78 | |
| 79 | $scategory = (int)$category; |
| 80 | $sbegins = (int)$begins; |
| 81 | $sends = (int)$ends; |
| 82 | |
| 83 | $query = mysqli_query($con, "SELECT * FROM calendars WHERE begins <= $sends AND ends >= $sbegins AND category = $scategory LIMIT 1"); |
| 84 | |
| 85 | return (mysqli_num_rows($query) > 0); |
| 86 | } |
| 87 | |
| 88 | public static function add($category, $begins, $ends, $calendar) { |
| 89 | global $con; |
| 90 | |
| 91 | if (self::checkOverlap($category, $begins, $ends)) { |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | $scategory = (int)$category; |
| 96 | $sbegins = (int)$begins; |
| 97 | $sends = (int)$ends; |
| 98 | $scalendar = db::sanitize(json_encode($calendar)); |
| 99 | |
| 100 | return (mysqli_query($con, "INSERT INTO calendars (category, begins, ends, details) VALUES ($scategory, $sbegins, $sends, '$scalendar')") ? 0 : -2); |
| 101 | } |
| 102 | |
| 103 | public static function edit($id, $calendar) { |
| 104 | global $con; |
| 105 | |
| 106 | $sid = (int)$id; |
| 107 | $scalendar = db::sanitize(json_encode($calendar)); |
| 108 | |
| 109 | return (mysqli_query($con, "UPDATE calendars SET details = '$scalendar' WHERE id = $sid LIMIT 1")); |
| 110 | } |
| 111 | |
| 112 | public static function get($id) { |
| 113 | global $con; |
| 114 | |
| 115 | $sid = (int)$id; |
| 116 | |
| 117 | $query = mysqli_query($con, "SELECT c.id id, c.begins begins, c.ends ends, c.details details, c.category category, ca.name categoryname FROM calendars c LEFT JOIN categories ca ON c.category = ca.id WHERE c.id = $sid"); |
| 118 | |
| 119 | if (!mysqli_num_rows($query)) { |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | $row = mysqli_fetch_assoc($query); |
| 124 | |
| 125 | if ($row["category"] == -1) { |
| 126 | $row["categoryname"] = "Calendario por defecto"; |
| 127 | } |
| 128 | |
| 129 | return $row; |
| 130 | } |
| 131 | |
| 132 | public static function getByCategory($id, $details = false) { |
| 133 | global $con; |
| 134 | |
| 135 | $sid = (int)$id; |
| 136 | |
| 137 | $query = mysqli_query($con, "SELECT id, begins, ends".($details ? ", details" : "")." FROM calendars WHERE category = $sid"); |
| 138 | |
| 139 | $calendars = []; |
| 140 | |
| 141 | while ($row = mysqli_fetch_assoc($query)) { |
| 142 | $calendars[] = $row; |
| 143 | } |
| 144 | |
| 145 | return $calendars; |
| 146 | } |
| 147 | |
| 148 | public static function getAll() { |
| 149 | $categories = categories::getAll(); |
| 150 | $categories[-1] = "Calendario por defecto"; |
| 151 | |
| 152 | $return = []; |
| 153 | |
| 154 | foreach ($categories as $id => $category) { |
| 155 | $return[] = array( |
| 156 | "category" => $category, |
| 157 | "categoryid" => $id, |
| 158 | "calendars" => self::getByCategory($id) |
| 159 | ); |
| 160 | } |
| 161 | |
| 162 | return $return; |
| 163 | } |
| 164 | |
| 165 | public static function getCurrentCalendarByCategory($category) { |
| 166 | global $con; |
| 167 | |
| 168 | $scategory = (int)$category; |
| 169 | $stime = (int)time(); |
| 170 | |
| 171 | $query = mysqli_query($con, "SELECT id, category, begins, ends, details FROM calendars WHERE category IN (-1, $scategory) AND begins <= $stime AND ends >= $stime"); |
| 172 | if ($query === false) return false; |
| 173 | |
| 174 | $calendars = []; |
| 175 | while ($row = mysqli_fetch_assoc($query)) { |
| 176 | $row["details"] = json_decode($row["details"], true); |
| 177 | if (json_last_error() !== JSON_ERROR_NONE) return false; |
| 178 | |
| 179 | $calendars[$row["category"]] = $row; |
| 180 | } |
| 181 | |
| 182 | return $calendars[$category] ?? $calendars[-1] ?? self::NO_CALENDAR_APPLICABLE; |
| 183 | } |
| 184 | |
| 185 | public static function remove($id) { |
| 186 | global $con; |
| 187 | |
| 188 | $sid = (int)$id; |
| 189 | |
| 190 | return mysqli_query($con, "DELETE FROM calendars WHERE id = $sid LIMIT 1"); |
| 191 | } |
| 192 | |
| 193 | public static function exists($id) { |
| 194 | global $con; |
| 195 | |
| 196 | $sid = (int)$id; |
| 197 | $query = mysqli_query($con, "SELECT id FROM calendars WHERE id = $sid"); |
| 198 | |
| 199 | return (mysqli_num_rows($query) > 0); |
| 200 | } |
| 201 | } |