blob: fff0536146d31ebb1f6987865584e09b6c5ee336 [file] [log] [blame]
Copybara botbe50d492023-11-30 00:16:42 +01001<?php
2require_once("core.php");
3security::checkType(security::ADMIN);
4
5if (!isset($_GET["id"])) {
6 security::go("calendars.php");
7}
8
9$category = $_GET["id"];
10
11$categoryd = categories::get($category);
12
13if ($categoryd === false && $category != -1) {
14 security::go("calendars.php");
15}
16
17if ($category == -1) {
18 $categoryd = array("name" => "Calendario por defecto");
19}
20
21// These checks are just to know whether the user filled in the form or not.
22// Thus, I added true as the third parameter to not display debug information when they fail.
23$newCalendar = security::checkParams("GET", [
24 ["begins", security::PARAM_ISDATE],
25 ["ends", security::PARAM_ISDATE]
26], true);
27
28$importCalendar = security::checkParams("POST", [
29 ["import", security::PARAM_NEMPTY]
30], true);
31
32$calendarEditor = ($newCalendar || $importCalendar);
33
34if ($importCalendar) {
35 $calendar = json_decode($_POST["import"], true);
36
37 if (json_last_error() !== JSON_ERROR_NONE || !isset($calendar["begins"]) || !isset($calendar["ends"]) || !isset($calendar["calendar"])) {
38 security::go("addcalendar.php?id=".(int)$category."&msg=jsoninvalid");
39 }
40}
41
42$mdHeaderRowBefore = visual::backBtn(($calendarEditor ? "addcalendar.php?id=".(int)$category : "calendars.php"));
43?>
44<!DOCTYPE html>
45<html>
46<head>
47 <title><?php echo $conf["appName"]; ?></title>
48 <?php visual::includeHead(); ?>
49 <link rel="stylesheet" href="css/dashboard.css">
50 <link rel="stylesheet" href="css/calendar.css">
51</head>
52<?php visual::printBodyTag(); ?>
53 <div class="mdl-layout mdl-js-layout mdl-layout--fixed-header mdl-layout--fixed-drawer">
54 <?php visual::includeNav(); ?>
55 <main class="mdl-layout__content">
56 <div class="page-content">
57 <div class="main mdl-shadow--4dp">
58 <h2>Añadir calendario a &ldquo;<?=security::htmlsafe($categoryd["name"])?>&rdquo;</h2>
59 <?php
60 if ($calendarEditor) {
61 if ($newCalendar) {
62 $current = new DateTime($_GET["begins"]);
63 $ends = new DateTime($_GET["ends"]);
64 } else {
65 $current = new DateTime();
66 $current->setTimestamp((int)$calendar["begins"]);
67 $ends = new DateTime();
68 $ends->setTimestamp((int)$calendar["ends"]);
69 }
70
71 if ($current->diff($ends)->invert === 1) {
72 security::go("addcalendar.php?id=".(int)$category."&msg=inverted");
73 }
74
75 if (calendars::checkOverlap($category, $current->getTimestamp(), $ends->getTimestamp())) {
76 security::go("addcalendar.php?id=".(int)$category."&msg=overlap");
77 }
78
79 if ($importCalendar) {
80 echo "<p>Este es el calendario que has importado. Ahora puedes hacer las modificaciones que creas oportunas y añadirlo.</p>";
81 }
82 ?>
83 <form action="doaddcalendar.php" method="POST">
84 <input type="hidden" name="id" value="<?=(int)$category?>">
85 <input type="hidden" name="begins" value="<?=security::htmlsafe(($newCalendar ? $_GET["begins"] : $current->format("Y-m-d")))?>">
86 <input type="hidden" name="ends" value="<?=security::htmlsafe(($newCalendar ? $_GET["ends"] : $ends->format("Y-m-d")))?>">
87 <?php
88 calendarsView::renderCalendar($current, $ends, ($newCalendar ? function ($timestamp, $id, $dow, $dom, $extra) {
89 return (($dow >= 6 && $id == calendars::TYPE_FESTIU) || $dow < 6 && $id == calendars::TYPE_LECTIU);
90 } : function ($timestamp, $id, $dow, $dom, $extra) {
91 return ($extra[$timestamp] == $id);
92 }), false, ($newCalendar ? false : $calendar["calendar"]));
93 ?>
94 <button class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent">Añadir</button>
95 </form>
96 <?php
97 } else {
98 ?>
99 <p>Introduce las fechas de inicio y fin del calendario que quieres configurar:</p>
100 <form action="addcalendar.php" method="GET">
101 <input type="hidden" name="id" value="<?=(int)$category?>">
102 <p><label for="begins">Fecha inicio:</label> <input type="date" id="begins" name="begins" required> <label for="ends">Fecha fin:</label> <input type="date" id="ends" name="ends" required></p>
103 <p><button class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent">Empezar a configurar el calendario</button></p>
104 </form>
105 <hr>
106 <p>Alternativamente, puedes importar un calendario para usarlo como plantilla y editarlo antes de crearlo:</p>
107 <form action="addcalendar.php?id=<?=(int)$category?>" method="POST">
108 <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
109 <textarea class="mdl-textfield__input" name="import" id="import" rows="3" data-required></textarea>
110 <label class="mdl-textfield__label" for="import">Código JSON del calendario original</label>
111 </div>
112 <p><button class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent">Importar calendario</button></p>
113 </form>
114 <?php
115 }
116 ?>
117 </div>
118 </div>
119 </main>
120 </div>
121
122 <script src="js/calendar.js"></script>
123
124 <?php
125 visual::smartSnackbar([
126 ["inverted", "La fecha de inicio debe ser anterior a la fecha de fin."],
127 ["overlap", "El calendario que intentabas añadir se solapa con uno ya existente."],
128 ["jsoninvalid", "El código JSON del calendario que estás importando es incorrecto."]
129 ]);
130 ?>
131</body>
132</html>