dolibarr 21.0.0-alpha
bookcalAjax.php
Go to the documentation of this file.
1<?php
2/*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <https://www.gnu.org/licenses/>.
15 */
16
22if (!defined('NOTOKENRENEWAL')) {
23 define('NOTOKENRENEWAL', '1'); // Disables token renewal
24}
25if (!defined('NOREQUIREHTML')) {
26 define('NOREQUIREHTML', '1');
27}
28if (!defined('NOREQUIREAJAX')) {
29 define('NOREQUIREAJAX', '1');
30}
31if (!defined('NOREQUIRESOC')) {
32 define('NOREQUIRESOC', '1');
33}
34// If there is no need to load and show top and left menu
35if (!defined('NOREQUIREMENU')) {
36 define('NOREQUIREMENU', '1');
37}
38if (!defined("NOLOGIN")) {
39 define("NOLOGIN", '1');
40}
41if (!defined('NOBROWSERNOTIF')) {
42 define('NOBROWSERNOTIF', '1');
43}
44
45require '../../main.inc.php';
46require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
47
48$action = GETPOST('action', 'aZ09');
49$id = GETPOSTINT('id');
50$datetocheckbooking = GETPOSTINT('datetocheck');
51$error = 0;
52
53// Security check
54/*if (!defined("NOLOGIN")) { // No need of restrictedArea if not logged: Later the select will filter on public articles only if not logged.
55 restrictedArea($user, 'knowledgemanagement', 0, 'knowledgemanagement_knowledgerecord', 'knowledgerecord');
56}*/
57
58$result = "{}";
59
60
61/*
62 * Actions
63 */
64
65// None
66
67
68/*
69 * View
70 */
71
72top_httphead('application/json');
73
74if ($action == 'verifyavailability') { // Test on permission not required here (anonymous action protected by mitigation of /public/... urls)
75 $response = array();
76 if (empty($id)) {
77 $error++;
78 $response["code"] = "MISSING_ID";
79 $response["message"] = "Missing parameter id";
80 header('HTTP/1.0 400 Bad Request');
81 echo json_encode($response);
82 exit;
83 }
84 if (empty($datetocheckbooking)) {
85 $error++;
86 $response["code"] = "MISSING_DATE_AVAILABILITY";
87 $response["message"] = "Missing parameter datetocheck";
88 header('HTTP/1.0 400 Bad Request');
89 echo json_encode($response);
90 exit;
91 }
92
93 // First get all ranges for the calendar
94 if (!$error) {
95 // Select in database all availabilities
96 $availabilitytab = array();
97 $sql = "SELECT ba.rowid as id, ba.duration, ba.startHour, ba.endHour, ba.start, ba.end";
98 $sql .= " FROM ".MAIN_DB_PREFIX."bookcal_availabilities as ba";
99 $sql .= " WHERE ba.fk_bookcal_calendar = ".((int) $id);
100 $sql .= " AND ba.status = 1";
101 $resql = $db->query($sql);
102 if ($resql) {
103 $num = $db->num_rows($resql);
104 $i = 0;
105 while ($i < $num) {
106 $obj = $db->fetch_object($resql);
107 $starttime = $db->jdate($obj->start);
108 $endtime = $db->jdate($obj->end);
109 $offsetmin = $obj->duration % 60;
110 if ($offsetmin == 0) {
111 $offsetmin = 60;
112 }
113 $startHourstring = $obj->startHour;
114 $endHourstring = $obj->endHour;
115 if ($startHourstring <= 0) {
116 $startHourstring = 0;
117 }
118 if ($endHourstring >= 24) {
119 $endHourstring = 24;
120 }
121 $offsethour = round($obj->duration / 60);
122 // Creation of array of availabilties range
123 if ($datetocheckbooking >= $starttime && $datetocheckbooking <= $endtime) {
124 for ($hour=$startHourstring; $hour < $endHourstring; $hour+= $offsethour) {
125 for ($min=0; $min < 60; $min += $offsetmin) {
126 $hourstring = $hour;
127 $minstring = $min;
128 if ($hour < 10) {
129 $hourstring = "0".$hourstring;
130 }
131 if ($min < 10) {
132 $minstring = "0".$minstring;
133 }
134 $response["availability"][$hourstring.":".$minstring] = intval($obj->duration);
135 }
136 }
137 }
138 $i++;
139 }
140 if ($i == $num) {
141 $response["code"] = "SUCCESS";
142 } else {
143 $response["code"] = "ERROR";
144 $error ++;
145 }
146 }
147
148 // Select also all not available ranges
149 if (!$error) {
150 $datetocheckbooking_end = dol_time_plus_duree($datetocheckbooking, 1, 'd');
151
152 $sql = "SELECT b.datep, b.id";
153 $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm as b";
154 $sql .= " WHERE b.datep >= '".$db->idate($datetocheckbooking)."'";
155 $sql .= " AND b.datep < '".$db->idate($datetocheckbooking_end)."'";
156 $sql .= " AND b.code = 'AC_RDV'";
157 $sql .= " AND b.status = 0";
158 $sql .= " AND b.fk_bookcal_calendar = ".((int) $id);
159 $resql = $db->query($sql);
160 if ($resql) {
161 $num = $db->num_rows($resql);
162 $i = 0;
163 while ($i < $num) {
164 $obj = $db->fetch_object($resql);
165 $datebooking = $db->jdate($obj->datep);
166 $datebookingarray = dol_getdate($datebooking);
167 $hourstring = $datebookingarray["hours"];
168 $minstring = $datebookingarray["minutes"];
169 if ($hourstring < 10) {
170 $hourstring = "0".$hourstring;
171 }
172 if ($minstring < 10) {
173 $minstring = "0".$minstring;
174 }
175 $response["availability"][$hourstring.":".$minstring] *= -1;
176 $i++;
177 }
178 }
179 }
180 }
181 $result = $response;
182}
183
184
185echo json_encode($result);
$id
Definition account.php:39
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition date.lib.php:125
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.