blob: a8b4a9fbd738c8c37d3fc9d703de0802aa97158c [file] [log] [blame]
Copybara botbe50d492023-11-30 00:16:42 +01001<?php
2class calendars {
3 const TYPE_FESTIU = 0;
4 const TYPE_FEINER = 1;
5 const TYPE_LECTIU = 2;
6
7 const NO_CALENDAR_APPLICABLE = 0;
8
9 public static $types = array(
10 0 => "Festivo",
11 2 => "Lectivo",
12 1 => "No lectivo"
13 );
14 public static $workingTypes = [1, 2];
15 public static $days = ["Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado", "Domingo"];
16 public static $months = ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"];
17
18 public static function parseFormCalendar($form, $ibegins, $iends, $timestamp = false) {
19 if ($timestamp) {
20 $current = new DateTime();
21 $current->setTimestamp((int)$ibegins);
22 $ends = new DateTime();
23 $ends->setTimestamp((int)$iends);
24 } else {
25 $current = new DateTime($ibegins);
26 $ends = new DateTime($iends);
27 }
28 $interval = new DateInterval("P1D");
29
30 if ($current->diff($ends)->invert === 1) {
31 return false;
32 }
33
34 $return = array(
35 "begins" => $current->getTimestamp(),
36 "ends" => $ends->getTimestamp(),
37 "calendar" => []
38 );
39
40 $possible_values = array_keys(self::$types);
41
42 $day = 0;
43 while ($current->diff($ends)->invert === 0) {
44 if (!isset($form[$day]) || !in_array($form[$day], $possible_values)) {
45 return false;
46 }
47
48 $return["calendar"][$current->getTimestamp()] = (int)$form[$day];
49
50 $day++;
51 $current->add($interval);
52 }
53
54 return $return;
55 }
56
57 public static function checkOverlap($category, $begins, $ends) {
58 global $con;
59
60 $scategory = (int)$category;
61 $sbegins = (int)$begins;
62 $sends = (int)$ends;
63
64 $query = mysqli_query($con, "SELECT * FROM calendars WHERE begins <= $sends AND ends >= $sbegins AND category = $scategory LIMIT 1");
65
66 return (mysqli_num_rows($query) > 0);
67 }
68
69 public static function add($category, $begins, $ends, $calendar) {
70 global $con;
71
72 if (self::checkOverlap($category, $begins, $ends)) {
73 return -1;
74 }
75
76 $scategory = (int)$category;
77 $sbegins = (int)$begins;
78 $sends = (int)$ends;
79 $scalendar = db::sanitize(json_encode($calendar));
80
81 return (mysqli_query($con, "INSERT INTO calendars (category, begins, ends, details) VALUES ($scategory, $sbegins, $sends, '$scalendar')") ? 0 : -2);
82 }
83
84 public static function edit($id, $calendar) {
85 global $con;
86
87 $sid = (int)$id;
88 $scalendar = db::sanitize(json_encode($calendar));
89
90 return (mysqli_query($con, "UPDATE calendars SET details = '$scalendar' WHERE id = $sid LIMIT 1"));
91 }
92
93 public static function get($id) {
94 global $con;
95
96 $sid = (int)$id;
97
98 $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");
99
100 if (!mysqli_num_rows($query)) {
101 return false;
102 }
103
104 $row = mysqli_fetch_assoc($query);
105
106 if ($row["category"] == -1) {
107 $row["categoryname"] = "Calendario por defecto";
108 }
109
110 return $row;
111 }
112
113 public static function getByCategory($id, $details = false) {
114 global $con;
115
116 $sid = (int)$id;
117
118 $query = mysqli_query($con, "SELECT id, begins, ends".($details ? ", details" : "")." FROM calendars WHERE category = $sid");
119
120 $calendars = [];
121
122 while ($row = mysqli_fetch_assoc($query)) {
123 $calendars[] = $row;
124 }
125
126 return $calendars;
127 }
128
129 public static function getAll() {
130 $categories = categories::getAll();
131 $categories[-1] = "Calendario por defecto";
132
133 $return = [];
134
135 foreach ($categories as $id => $category) {
136 $return[] = array(
137 "category" => $category,
138 "categoryid" => $id,
139 "calendars" => self::getByCategory($id)
140 );
141 }
142
143 return $return;
144 }
145
146 public static function getCurrentCalendarByCategory($category) {
147 global $con;
148
149 $scategory = (int)$category;
150 $stime = (int)time();
151
152 $query = mysqli_query($con, "SELECT id, category, begins, ends, details FROM calendars WHERE category IN (-1, $scategory) AND begins <= $stime AND ends >= $stime");
153 if ($query === false) return false;
154
155 $calendars = [];
156 while ($row = mysqli_fetch_assoc($query)) {
157 $row["details"] = json_decode($row["details"], true);
158 if (json_last_error() !== JSON_ERROR_NONE) return false;
159
160 $calendars[$row["category"]] = $row;
161 }
162
163 return $calendars[$category] ?? $calendars[-1] ?? self::NO_CALENDAR_APPLICABLE;
164 }
165
166 public static function remove($id) {
167 global $con;
168
169 $sid = (int)$id;
170
171 return mysqli_query($con, "DELETE FROM calendars WHERE id = $sid LIMIT 1");
172 }
173
174 public static function exists($id) {
175 global $con;
176
177 $sid = (int)$id;
178 $query = mysqli_query($con, "SELECT id FROM calendars WHERE id = $sid");
179
180 return (mysqli_num_rows($query) > 0);
181 }
182}