blob: 258fba634a7fe36737ac9eeb6bc9794833843a02 [file] [log] [blame]
Copybara botbe50d492023-11-30 00:16:42 +01001<?php
Adrià Vilanova Martínez5af86512023-12-02 20:44:16 +01002/*
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 botbe50d492023-11-30 00:16:42 +010021class recurringIncidents {
22 /*public static function oldAdd($worker, $type, $details, $ifirstday, $ilastday, $begins, $ends, $creator = "ME", $typedays, $days, $alreadyTimestamp = false) {
23 global $con, $conf;
24
25 $sworker = (int)$worker;
26 $workerDetails = workers::get($sworker);
27 if ($workerDetails === false) return 1;
28
29 if ($creator === "ME") $creator = people::userData("id");
30 $screator = (int)$creator;
31
32 if (!security::isAllowed(security::ADMIN) && $workerDetails["person"] != $creator) return 5;
33
34 $stype = (int)$type;
35 $sverified = (int)$verified;
36 $sdetails = db::sanitize($details);
37
38 $incidenttype = self::getType($stype);
39 if ($incidenttype === false) return -1;
40
41 if ($alreadyTimestamp) {
42 $sfirstday = (int)$ifirstday;
43 $slastday = (int)$ilastday;
44 } else {
45 $firstday = new DateTime($ifirstday);
46 $sfirstday = (int)$firstday->getTimestamp();
47 $lastday = new DateTime($ilastday);
48 $slastday = (int)$lastday->getTimestamp();
49 }
50
51 if ($sfirstday >= $slastday) return 3;
52
53 $sbegins = (int)$begins;
54 $sends = (int)$ends;
55 if ($sbegins >= $sends) return 3;
56
57 $typedays = array_unique(array_map(function($el) {
58 return (int)$el;
59 }, $typedays));
60 foreach ($typedays as $typeday) {
61 if (!in_array($typeday, $workingTypes)) return 6;
62 }
63 $stypedays = json_encode($typedays);
64
65 if (!mysqli_query($con, "INSERT INTO recurringincidents (worker, creator, type, firstday, lastday, typedays, begins, ends, details) VALUES ($sworker, $screator, $stype, $sfirstday, $slastday, '$stypedays', $sbegins, $sends, '$sdetails')")) return -1;
66
67 return 0;
68 }*/ // NOTE: This was a first idea, to allow setting up recurring incidents like schedules, but we've changed how we'll handle them and so this is no longer useful.
69
70 public static function add($worker, $type, $details, $ifirstday, $ilastday, $begins, $ends, $creator = "ME", $typeDays, $days, $alreadyTimestamp = false) {
71 if ($alreadyTimestamp) {
72 $current = new DateTime();
73 $current->setTimestamp($ifirstday);
74 $lastday = new DateTime();
75 $lastday->setTimestamp($ilastday);
76 } else {
77 $current = new DateTime($ifirstday);
78 $lastday = new DateTime($ilastday);
79 }
80
81 $oneDay = new DateInterval("P1D");
82
83 $category = registry::getWorkerCategory($worker);
84 if ($category === false) return false;
85
86 $flag = true;
87
88 for (; $current->diff($lastday)->invert === 0; $current->add($oneDay)) {
89 $currentTimestamp = $current->getTimestamp();
90 $currentDay = (int)$current->format("N") - 1;
91
92 if (!in_array($currentDay, $days)) continue;
93
94 $calendarDays = registry::getDayTypes($currentTimestamp);
95 if ($calendarDays === false) return false;
96
97 if (isset($calendarDays[$category])) {
98 $typeDay = $calendarDays[$category];
99 } else if (isset($calendarDays[-1])) {
100 $typeDay = $calendarDays[-1];
101 } else {
102 $flag = false;
103 continue;
104 }
105
106 if (!in_array($typeDay, $typeDays)) continue;
107
108 if ($status = incidents::add($worker, $type, $details, $currentTimestamp, $begins, $ends, $creator, 1, true, false, false) !== 0) {
109 $flag = false;
110 continue;
111 }
112 }
113
114 return $flag;
115 }
116}