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