blob: b07bf6685ecb4a027f907047f5e36ef1473fdc69 [file] [log] [blame]
Copybara botbe50d492023-11-30 00:16:42 +01001<?php
2class recurringIncidents {
3 /*public static function oldAdd($worker, $type, $details, $ifirstday, $ilastday, $begins, $ends, $creator = "ME", $typedays, $days, $alreadyTimestamp = false) {
4 global $con, $conf;
5
6 $sworker = (int)$worker;
7 $workerDetails = workers::get($sworker);
8 if ($workerDetails === false) return 1;
9
10 if ($creator === "ME") $creator = people::userData("id");
11 $screator = (int)$creator;
12
13 if (!security::isAllowed(security::ADMIN) && $workerDetails["person"] != $creator) return 5;
14
15 $stype = (int)$type;
16 $sverified = (int)$verified;
17 $sdetails = db::sanitize($details);
18
19 $incidenttype = self::getType($stype);
20 if ($incidenttype === false) return -1;
21
22 if ($alreadyTimestamp) {
23 $sfirstday = (int)$ifirstday;
24 $slastday = (int)$ilastday;
25 } else {
26 $firstday = new DateTime($ifirstday);
27 $sfirstday = (int)$firstday->getTimestamp();
28 $lastday = new DateTime($ilastday);
29 $slastday = (int)$lastday->getTimestamp();
30 }
31
32 if ($sfirstday >= $slastday) return 3;
33
34 $sbegins = (int)$begins;
35 $sends = (int)$ends;
36 if ($sbegins >= $sends) return 3;
37
38 $typedays = array_unique(array_map(function($el) {
39 return (int)$el;
40 }, $typedays));
41 foreach ($typedays as $typeday) {
42 if (!in_array($typeday, $workingTypes)) return 6;
43 }
44 $stypedays = json_encode($typedays);
45
46 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;
47
48 return 0;
49 }*/ // 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.
50
51 public static function add($worker, $type, $details, $ifirstday, $ilastday, $begins, $ends, $creator = "ME", $typeDays, $days, $alreadyTimestamp = false) {
52 if ($alreadyTimestamp) {
53 $current = new DateTime();
54 $current->setTimestamp($ifirstday);
55 $lastday = new DateTime();
56 $lastday->setTimestamp($ilastday);
57 } else {
58 $current = new DateTime($ifirstday);
59 $lastday = new DateTime($ilastday);
60 }
61
62 $oneDay = new DateInterval("P1D");
63
64 $category = registry::getWorkerCategory($worker);
65 if ($category === false) return false;
66
67 $flag = true;
68
69 for (; $current->diff($lastday)->invert === 0; $current->add($oneDay)) {
70 $currentTimestamp = $current->getTimestamp();
71 $currentDay = (int)$current->format("N") - 1;
72
73 if (!in_array($currentDay, $days)) continue;
74
75 $calendarDays = registry::getDayTypes($currentTimestamp);
76 if ($calendarDays === false) return false;
77
78 if (isset($calendarDays[$category])) {
79 $typeDay = $calendarDays[$category];
80 } else if (isset($calendarDays[-1])) {
81 $typeDay = $calendarDays[-1];
82 } else {
83 $flag = false;
84 continue;
85 }
86
87 if (!in_array($typeDay, $typeDays)) continue;
88
89 if ($status = incidents::add($worker, $type, $details, $currentTimestamp, $begins, $ends, $creator, 1, true, false, false) !== 0) {
90 $flag = false;
91 continue;
92 }
93 }
94
95 return $flag;
96 }
97}