1 | <?php |
---|
2 | include_once("../includes/ctrlacc.php"); |
---|
3 | include_once("../includes/CreaComando.php"); |
---|
4 | include_once("../includes/timezone.php"); |
---|
5 | include_once("../clases/AdoPhp.php"); |
---|
6 | |
---|
7 | function add_cal_to_db ($desc, $j) { |
---|
8 | global $cadenaconexion; |
---|
9 | $cmd = CreaComando ($cadenaconexion); |
---|
10 | if (!$cmd) { |
---|
11 | die ($TbMsg['ACCESS_ERROR']); |
---|
12 | } |
---|
13 | $cmd->CreaParametro ('@desc', $desc, 0); |
---|
14 | $cmd->CreaParametro ('@json_text', $j, 0); |
---|
15 | $cmd->texto = 'INSERT INTO calendarios (description, json_text) VALUES (@desc, @json_text);'; |
---|
16 | if (!$cmd->Ejecutar()) { |
---|
17 | return $cmd->DescripUltimoError(); |
---|
18 | } |
---|
19 | return; |
---|
20 | } |
---|
21 | |
---|
22 | function modify_cal_in_db ($id, $desc, $j) { |
---|
23 | global $cadenaconexion; |
---|
24 | $cmd = CreaComando ($cadenaconexion); |
---|
25 | if (!$cmd) { |
---|
26 | die ($TbMsg['ACCESS_ERROR']); |
---|
27 | } |
---|
28 | $cmd->CreaParametro ('@id', $id, 0); |
---|
29 | $cmd->CreaParametro ('@desc', $desc, 0); |
---|
30 | $cmd->CreaParametro ('@json_text', $j, 0); |
---|
31 | $cmd->texto = 'UPDATE calendarios SET description=@desc, json_text=@json_text WHERE idcalendario=@id;'; |
---|
32 | if (!$cmd->Ejecutar()) { |
---|
33 | return $cmd->DescripUltimoError(); |
---|
34 | } |
---|
35 | return; |
---|
36 | } |
---|
37 | |
---|
38 | function fetch_cal_from_db ($id) { |
---|
39 | global $cadenaconexion; |
---|
40 | $cmd = CreaComando ($cadenaconexion); |
---|
41 | if (!$cmd) { |
---|
42 | die ($TbMsg['ACCESS_ERROR']); |
---|
43 | } |
---|
44 | $cmd->CreaParametro ('@id', $id, 0); |
---|
45 | $cmd->texto = 'SELECT description, json_text FROM calendarios WHERE idcalendario=@id;'; |
---|
46 | $rs = new Recordset; |
---|
47 | $rs->Comando = &$cmd; |
---|
48 | |
---|
49 | if (!$rs->Abrir()) return (false); |
---|
50 | |
---|
51 | $rs->Primero(); |
---|
52 | if ($rs->EOF) { |
---|
53 | return array ('', '{"remotepc_calendar":{"timezone":"' . $tz . '","ruleset":[]}}'); |
---|
54 | } |
---|
55 | $desc = $rs->campos['description']; |
---|
56 | $json = $rs->campos['json_text']; |
---|
57 | return array ($desc, $json); |
---|
58 | } |
---|
59 | |
---|
60 | function process_post ($cal_id, $desc, $json_str) { |
---|
61 | $errors = array(); |
---|
62 | |
---|
63 | if (empty ($desc)) { |
---|
64 | array_push ($errors, 'Description of new calendar is unset'); |
---|
65 | } |
---|
66 | if (sizeof ($errors)) { |
---|
67 | return $errors; |
---|
68 | } |
---|
69 | |
---|
70 | if (-1 == $cal_id) { |
---|
71 | $db_err = add_cal_to_db ($desc, $json_str); |
---|
72 | } else { |
---|
73 | $db_err = modify_cal_in_db ($cal_id, $desc, $json_str); |
---|
74 | } |
---|
75 | |
---|
76 | if ($db_err) { |
---|
77 | array_push ($errors, $db_err); |
---|
78 | } |
---|
79 | |
---|
80 | if (!sizeof ($errors)) { |
---|
81 | header ('Location: calendars.php'); |
---|
82 | } |
---|
83 | return $errors; |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | $error_str = ''; |
---|
88 | function parse_params() { |
---|
89 | global $tz, $error_str; |
---|
90 | |
---|
91 | if (isset ($_GET['cal_id']) && isset ($_POST['cal_id'])) { |
---|
92 | $error_str = "Got a cal_id in both a GET and a POST??\n"; |
---|
93 | return 0; |
---|
94 | } |
---|
95 | |
---|
96 | if (isset ($_GET['cal_id'])) { |
---|
97 | $cal_id = $_GET['cal_id']; |
---|
98 | $is_get = 1; |
---|
99 | } elseif (isset ($_POST['cal_id'])) { |
---|
100 | $cal_id = $_POST['cal_id']; |
---|
101 | $is_get = 0; |
---|
102 | } else { |
---|
103 | $error_str = "Didn't get a cal_id in neither GET nor POST\n"; |
---|
104 | return 0; |
---|
105 | } |
---|
106 | |
---|
107 | $ret = 0; |
---|
108 | if (-1 == $cal_id) { |
---|
109 | // nuevo calendario |
---|
110 | // seguro que es un POST |
---|
111 | |
---|
112 | if (!isset ($_POST['desc']) and !isset ($_POST['json_str'])) { |
---|
113 | // en la lista de calendarios han pinchado en el boton de "nuevo calendario" |
---|
114 | $desc = ''; |
---|
115 | $json = '{"remotepc_calendar":{"timezone":"' . $tz . '","ruleset":[{"remote":false,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"from_hr":"8","to_hr":"17"}]}}'; // hagamos que los calendarios nuevos por defecto ya tengan una regla lun-vie 8-17 |
---|
116 | $ret = 1; |
---|
117 | |
---|
118 | } else { |
---|
119 | // en los detalles del calendario han pinchado en "crear" |
---|
120 | $desc = $_POST['desc']; |
---|
121 | $json = $_POST['json_str']; |
---|
122 | $errors = process_post ($cal_id, $desc, $json); |
---|
123 | if (sizeof ($errors)) { |
---|
124 | $error_str = implode (',', $errors); |
---|
125 | //printf ('after process_post(), error_str (%s)', $error_str); |
---|
126 | } else { |
---|
127 | $ret = 1; |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | } else { |
---|
132 | // puede ser GET o POST |
---|
133 | |
---|
134 | if ($is_get) { // si GET: ver calendario |
---|
135 | list ($desc, $json) = fetch_cal_from_db ($cal_id); |
---|
136 | $ret = 1; |
---|
137 | |
---|
138 | } else { // si POST: modificar calendario |
---|
139 | $desc = $_POST['desc']; |
---|
140 | $json = $_POST['json_str']; |
---|
141 | $errors = process_post ($cal_id, $desc, $json); |
---|
142 | if (sizeof ($errors)) { |
---|
143 | $error_str = implode (',', $errors); |
---|
144 | } else { |
---|
145 | $ret = 1; |
---|
146 | } |
---|
147 | } |
---|
148 | } |
---|
149 | return array ($ret, $cal_id, $desc, $json); |
---|
150 | } |
---|
151 | |
---|
152 | list ($ok, $cal_id, $desc, $json) = parse_params(); |
---|
153 | ?> |
---|
154 | |
---|
155 | <html> |
---|
156 | <head> |
---|
157 | <title>Administración web de calendarios</title> |
---|
158 | <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> |
---|
159 | <link rel="shortcut icon" href="images/iconos/logocirculos.png" type="image/png" /> |
---|
160 | <link rel="stylesheet" type="text/css" href="estilos.css" /> |
---|
161 | <script language="javascript"> |
---|
162 | |
---|
163 | // recibimos las globales del php |
---|
164 | var tz = "<?= $tz ?>"; |
---|
165 | var cal_id = <?= $cal_id ?>; |
---|
166 | var jsdesc = "<?= $desc ?>"; // if js vars have the same name as html elems, then things like document.forms[0].desc.value return "[object htmlinputelement]" rather than the actual value. Hence, "jsdesc". |
---|
167 | var cal = <?= $json ?>; // este json en php es un string, pero al ponerlo sin comillas aqui se transforma en un objeto automagicamente :) |
---|
168 | |
---|
169 | // copypaste from elsewhere to handle the calendar popup |
---|
170 | function vertabla_calendario(ofecha) { |
---|
171 | currentFecha=ofecha; |
---|
172 | url="../varios/calendario_ventana.php?fecha="+ofecha.value; |
---|
173 | window.open(url,"vf","top=160,left=250,height=220,width=150,scrollbars=no") |
---|
174 | } |
---|
175 | |
---|
176 | function anade_fecha(fecha){ |
---|
177 | currentFecha.value=fecha |
---|
178 | } |
---|
179 | // end of copypaste |
---|
180 | |
---|
181 | |
---|
182 | // https://stackoverflow.com/questions/15141762/how-to-initialize-a-javascript-date-to-a-particular-time-zone |
---|
183 | function dateWithTimeZone (timeZone, year, month, day, hour, minute, second) { |
---|
184 | let date = new Date (Date.UTC (year, month-1, day, hour, minute, second)); |
---|
185 | |
---|
186 | let utcDate = new Date (date.toLocaleString ('en-US', { timeZone: "UTC" })); |
---|
187 | let tzDate = new Date (date.toLocaleString ('en-US', { timeZone: timeZone })); |
---|
188 | let offset = utcDate.getTime() - tzDate.getTime(); |
---|
189 | |
---|
190 | date.setTime (date.getTime() + offset); |
---|
191 | |
---|
192 | return date; |
---|
193 | }; |
---|
194 | |
---|
195 | days_of_week = [ |
---|
196 | ['mon', 'Lun'], |
---|
197 | ['tue', 'Mar'], |
---|
198 | ['wed', 'Mié'], |
---|
199 | ['thu', 'Jue'], |
---|
200 | ['fri', 'Vie'], |
---|
201 | ['sat', 'Sáb'], |
---|
202 | ['sun', 'Dom'] |
---|
203 | ]; |
---|
204 | |
---|
205 | function json2tbl() { |
---|
206 | new_tbl = document.createElement ('table'); |
---|
207 | |
---|
208 | // desc |
---|
209 | tr = document.createElement ('tr'); |
---|
210 | tr.innerHTML = '<td>Descripción</td><td><input type="text" name="desc" value="' + jsdesc + '" onblur="jsdesc=document.forms[0].desc.value;"/></td>'; |
---|
211 | new_tbl.appendChild (tr); |
---|
212 | |
---|
213 | rule_no = -1; |
---|
214 | |
---|
215 | // para cada regla en el ruleset, añadir un tr |
---|
216 | for (idx in cal.remotepc_calendar.ruleset) { |
---|
217 | rule_no++; |
---|
218 | rule_id = 'rule' + rule_no; |
---|
219 | rule = cal.remotepc_calendar.ruleset[idx]; |
---|
220 | if (!rule.remote) { |
---|
221 | tr = document.createElement ('tr'); |
---|
222 | |
---|
223 | // un td con los radio "presencial" (checked) y "remoto", y el boton de borrar |
---|
224 | td = document.createElement ('td'); |
---|
225 | td.innerHTML = '<input type="radio" name="' + rule_id + '" value="onsite" checked="checked" onclick=\'set_rule_onsite("' + rule_no + '");\' /> Período presencial<br>'; |
---|
226 | td.innerHTML += '<input type="radio" name="' + rule_id + '" value="except" onclick=\'set_rule_exception("' + rule_no + '");\' /> Excepción<br>'; |
---|
227 | td.innerHTML += '<input type="button" name="' + rule_id + '_delete" value="Borrar regla" onclick=\'del_rule("' + rule_no + '");\' />'; |
---|
228 | tr.appendChild (td); |
---|
229 | |
---|
230 | html = ''; |
---|
231 | // y otro td con los dias... |
---|
232 | days_of_week.forEach (dow => { |
---|
233 | if (rule[dow[0]] != undefined) { |
---|
234 | html += '<input type="checkbox" name="' + rule_id + '_' + dow[0] + '" checked />' + dow[1] + ' '; |
---|
235 | } else { |
---|
236 | html += '<input type="checkbox" name="' + rule_id + '_' + dow[0] + '" />' + dow[1] + ' '; |
---|
237 | } |
---|
238 | }); |
---|
239 | |
---|
240 | // ...la hora inicio... |
---|
241 | html += 'de <select name="' + rule_id + '_from_hr">'; |
---|
242 | for (var h = 0; h <= 23; h++) { |
---|
243 | if (h == rule.from_hr) { |
---|
244 | html += '<option value="' + h + '" selected>' + h + ':00</option>'; |
---|
245 | } else { |
---|
246 | html += '<option value="' + h + '" >' + h + ':00</option>'; |
---|
247 | } |
---|
248 | } |
---|
249 | html += '</select>'; |
---|
250 | |
---|
251 | // ...y la hora fin |
---|
252 | html += 'a <select name="' + rule_id + '_to_hr">'; |
---|
253 | for (var h = 0; h <= 23; h++) { |
---|
254 | if (h == rule.to_hr) { |
---|
255 | html += '<option value="' + h + '" selected>' + h + ':59</option>'; |
---|
256 | } else { |
---|
257 | html += '<option value="' + h + '" >' + h + ':59</option>'; |
---|
258 | } |
---|
259 | } |
---|
260 | html += '</select>'; |
---|
261 | |
---|
262 | td = document.createElement ('td'); |
---|
263 | td.innerHTML = html; |
---|
264 | tr.appendChild (td); |
---|
265 | |
---|
266 | new_tbl.appendChild (tr); |
---|
267 | |
---|
268 | } else { |
---|
269 | tr = document.createElement ('tr'); |
---|
270 | |
---|
271 | // un td con los radio "presencial" y "remoto" (checked), y el boton de borrar |
---|
272 | td = document.createElement ('td'); |
---|
273 | td.innerHTML = '<input type="radio" name="' + rule_id + '" value="onsite" onclick=\'set_rule_onsite("' + rule_no + '");\' /> Período presencial<br>'; |
---|
274 | td.innerHTML += '<input type="radio" name="' + rule_id + '" value="except" checked="checked" onclick=\'set_rule_exception("' + rule_no + '");\' /> Excepción<br>'; |
---|
275 | td.innerHTML += '<input type="button" name="' + rule_id + '_delete" value="Borrar regla" onclick=\'del_rule("' + rule_no + '");\' />'; |
---|
276 | tr.appendChild (td); |
---|
277 | |
---|
278 | html = ''; |
---|
279 | // y otro td con reason,from,to |
---|
280 | html += 'Motivo: <input type="text" name="' + rule_id + '_reason" value="' + rule.reason + '" /> '; |
---|
281 | from_ts = new Intl.DateTimeFormat ('es-ES', { year: 'numeric', month: 'numeric', day: 'numeric', timeZone: tz }).format (new Date (1000 * rule.from_ts)); |
---|
282 | to_ts = new Intl.DateTimeFormat ('es-ES', { year: 'numeric', month: 'numeric', day: 'numeric', timeZone: tz }).format (new Date (1000 * rule.to_ts )); |
---|
283 | html += 'de <input onclick="vertabla_calendario(this)" name="' + rule_id + '_from_ts" value="' + from_ts + '" /> '; |
---|
284 | html += 'a <input onclick="vertabla_calendario(this)" name="' + rule_id + '_to_ts" value="' + to_ts + '" />'; |
---|
285 | td = document.createElement ('td'); |
---|
286 | td.innerHTML = html; |
---|
287 | tr.appendChild (td); |
---|
288 | |
---|
289 | new_tbl.appendChild (tr); |
---|
290 | |
---|
291 | } |
---|
292 | } |
---|
293 | |
---|
294 | // boton Añadir regla |
---|
295 | tr = document.createElement ('tr'); |
---|
296 | tr.innerHTML = '<tr><td colspan="2"><input type="submit" value="Añadir regla" onclick=\'add_rule(); return false;\' /></td></tr>'; |
---|
297 | new_tbl.appendChild (tr); |
---|
298 | |
---|
299 | // boton Crear/Modificar calendario |
---|
300 | tr = document.createElement ('tr'); |
---|
301 | btn_lbl = -1 == cal_id ? 'Crear' : 'Modificar'; |
---|
302 | tr.innerHTML = '<tr><td colspan="2"><input type="submit" value="' + btn_lbl + ' calendario" onclick=\'tbl2json(); document.forms[0].json_str.value = JSON.stringify(cal); document.forms[0].action="./calendars-modify.php";\' /></td></tr>'; |
---|
303 | new_tbl.appendChild (tr); |
---|
304 | |
---|
305 | return new_tbl; |
---|
306 | } |
---|
307 | |
---|
308 | function replace_tbl() { |
---|
309 | old_tbl = document.getElementById ('tbl'); |
---|
310 | parentElement = old_tbl.parentElement; |
---|
311 | |
---|
312 | try { |
---|
313 | new_tbl = json2tbl(); |
---|
314 | new_tbl.id = 'tbl'; |
---|
315 | new_tbl.border = 1; |
---|
316 | parentElement.replaceChild (new_tbl, old_tbl); |
---|
317 | } catch (error) { |
---|
318 | console.error ("Error parsing JSON:", error); |
---|
319 | } |
---|
320 | } |
---|
321 | |
---|
322 | function tbl2json() { |
---|
323 | f = new FormData (document.forms[0]); |
---|
324 | re = /^rule(\d+)(_(.*))?$/; |
---|
325 | ruleset = []; |
---|
326 | for (let [key, val] of f.entries()) { |
---|
327 | if ('cal_id' == key) { cal_id = val; continue; } |
---|
328 | if ('desc' == key) { jsdesc = val; continue; } |
---|
329 | m = re.exec (key); |
---|
330 | if (m === null) { continue; } // no match |
---|
331 | |
---|
332 | rule = m[1]; |
---|
333 | k = m[3]; |
---|
334 | if (k === undefined) { |
---|
335 | // rule0, rule1... |
---|
336 | if ('onsite' == val) { ruleset[rule] = { 'remote': false }; } |
---|
337 | else if ('except' == val) { ruleset[rule] = { 'remote': true }; } |
---|
338 | continue; |
---|
339 | } |
---|
340 | |
---|
341 | // rule0_whatever, rule1_whatever... |
---|
342 | if (['mon','tue','wed','thu','fri','sat','sun'].indexOf (k) >= 0) { val = true; } // val is "on", turn it into boolean |
---|
343 | if ('from_ts' == k) { |
---|
344 | m = /^(\d+)\/(\d+)\/(\d+)$/.exec (val); |
---|
345 | if (m != null) { |
---|
346 | day = m[1]; |
---|
347 | month = m[2]; |
---|
348 | year = m[3]; |
---|
349 | val = dateWithTimeZone (tz, year, month, day, 0, 0, 0).getTime() / 1000; |
---|
350 | } |
---|
351 | } |
---|
352 | if ('to_ts' == k) { |
---|
353 | m = /^(\d+)\/(\d+)\/(\d+)$/.exec (val); |
---|
354 | if (m != null) { |
---|
355 | day = m[1]; |
---|
356 | month = m[2]; |
---|
357 | year = m[3]; |
---|
358 | val = dateWithTimeZone (tz, year, month, day, 23, 59, 59).getTime() / 1000; |
---|
359 | } |
---|
360 | } |
---|
361 | ruleset[rule][k] = val; |
---|
362 | } |
---|
363 | |
---|
364 | cal = { 'remotepc_calendar': { 'timezone': tz, ruleset: ruleset } }; |
---|
365 | } |
---|
366 | |
---|
367 | function set_rule_onsite (idx) { |
---|
368 | rule_no = parseInt(idx); |
---|
369 | rule_id = 'rule' + rule_no; |
---|
370 | tbl = document.getElementById ('tbl'); |
---|
371 | tr = tbl.rows[rule_no+1]; |
---|
372 | |
---|
373 | // comprobar si ya está checked, entonces no hacer nada |
---|
374 | if (!cal.remotepc_calendar.ruleset[idx].remote) { return; } |
---|
375 | |
---|
376 | td = tr.cells[1]; |
---|
377 | td.innerHTML = ' \ |
---|
378 | <input type="checkbox" name="' + rule_id + '_mon" />Lun <input type="checkbox" name="' + rule_id + '_tue" />Mar <input type="checkbox" name="' + rule_id + '_wed" />Mié <input type="checkbox" name="' + rule_id + '_thu" />Jue <input type="checkbox" name="' + rule_id + '_fri" />Vie <input type="checkbox" name="' + rule_id + '_sat" />Sáb <input type="checkbox" name="' + rule_id + '_sun" />Dom, \ |
---|
379 | de <select name="' + rule_id + '_from_hr"><option value="0">0:00</option> <option value="1">1:00</option> <option value="2">2:00</option> <option value="3">3:00</option> <option value="4">4:00</option> <option value="5">5:00</option> <option value="6">6:00</option> <option value="7">7:00</option> <option value="8" selected>8:00</option> <option value="9">9:00</option> <option value="10">10:00</option> <option value="11">11:00</option> <option value="12">12:00</option> <option value="13">13:00</option> <option value="14">14:00</option> <option value="15">15:00</option> <option value="16">16:00</option> <option value="17" >17:00</option> <option value="18">18:00</option> <option value="19">19:00</option> <option value="20">20:00</option> <option value="21">21:00</option> <option value="22">22:00</option> <option value="23">23:00</option> </select> \ |
---|
380 | a <select name="' + rule_id + '_to_hr" ><option value="0">0:59</option> <option value="1">1:59</option> <option value="2">2:59</option> <option value="3">3:59</option> <option value="4">4:59</option> <option value="5">5:59</option> <option value="6">6:59</option> <option value="7">7:59</option> <option value="8" >8:59</option> <option value="9">9:59</option> <option value="10">10:59</option> <option value="11">11:59</option> <option value="12">12:59</option> <option value="13">13:59</option> <option value="14">14:59</option> <option value="15">15:59</option> <option value="16">16:59</option> <option value="17" selected>17:59</option> <option value="18">18:59</option> <option value="19">19:59</option> <option value="20">20:59</option> <option value="21">21:59</option> <option value="22">22:59</option> <option value="23">23:59</option> </select> \ |
---|
381 | '; |
---|
382 | |
---|
383 | tbl2json(); |
---|
384 | } |
---|
385 | |
---|
386 | function set_rule_exception (idx) { |
---|
387 | rule_no = parseInt(idx); |
---|
388 | rule_id = 'rule' + rule_no; |
---|
389 | tbl = document.getElementById ('tbl'); |
---|
390 | tr = tbl.rows[rule_no+1]; |
---|
391 | |
---|
392 | // comprobar si ya está checked, entonces no hacer nada |
---|
393 | if (cal.remotepc_calendar.ruleset[idx].remote) { return; } |
---|
394 | |
---|
395 | td = tr.cells[1]; |
---|
396 | td.innerHTML = ' \ |
---|
397 | Motivo: <input type="text" name="' + rule_id + '_reason" /> \ |
---|
398 | de <input onclick="vertabla_calendario(this)" name="' + rule_id + '_from_ts" /> \ |
---|
399 | a <input onclick="vertabla_calendario(this)" name="' + rule_id + '_to_ts" /> \ |
---|
400 | '; |
---|
401 | |
---|
402 | tbl2json(); |
---|
403 | } |
---|
404 | |
---|
405 | function add_rule() { |
---|
406 | cal.remotepc_calendar.ruleset.push ({}); |
---|
407 | |
---|
408 | replace_tbl(); |
---|
409 | } |
---|
410 | |
---|
411 | function del_rule (idx) { |
---|
412 | tbl2json(); |
---|
413 | cal.remotepc_calendar.ruleset.splice (idx, 1); |
---|
414 | replace_tbl(); |
---|
415 | } |
---|
416 | |
---|
417 | document.addEventListener("DOMContentLoaded", function(){ |
---|
418 | replace_tbl(); |
---|
419 | }); |
---|
420 | </script> |
---|
421 | |
---|
422 | |
---|
423 | </head> |
---|
424 | |
---|
425 | <body> |
---|
426 | <?php if ($error_str) { printf ('<p>Error: (%s)</p>', $error_str); } ?> |
---|
427 | <form method="post"> |
---|
428 | <input type="hidden" name="cal_id" value="<?= $cal_id ?>" /> |
---|
429 | <input type="hidden" name="json_str" value="" /> |
---|
430 | <p><table id="tbl"></table></p> |
---|
431 | </form> |
---|
432 | </body> |
---|
433 | </html> |
---|