dolibarr 23.0.3
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';
50$action = GETPOST('action', 'aZ09');
51$id = GETPOSTINT('id');
52$datetocheckbooking = GETPOSTINT('datetocheck');
53$error = 0;
54
55// Security check
56/*if (!defined("NOLOGIN")) { // No need of restrictedArea if not logged: Later the select will filter on public articles only if not logged.
57 restrictedArea($user, 'knowledgemanagement', 0, 'knowledgemanagement_knowledgerecord', 'knowledgerecord');
58}*/
59
60$result = "{}";
61
62
63/*
64 * Actions
65 */
66
67// None
68
69
70/*
71 * View
72 */
73
74top_httphead('application/json');
75
76if ($action == 'verifyavailability') { // Test on permission not required here (anonymous action protected by mitigation of /public/... urls)
77 $response = array();
78 if (empty($id)) {
79 $error++;
80 $response["code"] = "MISSING_ID";
81 $response["message"] = "Missing parameter id";
82 header('HTTP/1.0 400 Bad Request');
83 echo json_encode($response);
84 exit;
85 }
86 if (empty($datetocheckbooking)) {
87 $error++;
88 $response["code"] = "MISSING_DATE_AVAILABILITY";
89 $response["message"] = "Missing parameter datetocheck";
90 header('HTTP/1.0 400 Bad Request');
91 echo json_encode($response);
92 exit;
93 }
94
95 // First get all ranges for the calendar
96 if (!$error) {
97 // Select in database all availabilities
98 $availabilitytab = array();
99 $sql = "SELECT ba.rowid as id, ba.duration, ba.startHour, ba.endHour, ba.start, ba.end";
100 $sql .= " FROM ".MAIN_DB_PREFIX."bookcal_availabilities as ba";
101 $sql .= " WHERE ba.fk_bookcal_calendar = ".((int) $id);
102 $sql .= " AND ba.status = 1";
103 $resql = $db->query($sql);
104 if ($resql) {
105 $num = $db->num_rows($resql);
106 $i = 0;
107 while ($i < $num) {
108 $obj = $db->fetch_object($resql);
109 $starttime = $db->jdate($obj->start);
110 $endtime = $db->jdate($obj->end);
111 $offsetmin = $obj->duration % 60;
112 if ($offsetmin == 0) {
113 $offsetmin = 60;
114 }
115 $startHourstring = $obj->startHour;
116 $endHourstring = $obj->endHour;
117 if ($startHourstring <= 0) {
118 $startHourstring = 0;
119 }
120 if ($endHourstring >= 24) {
121 $endHourstring = 24;
122 }
123 $offsethour = round($obj->duration / 60);
124 // Creation of array of availabilties range
125 if ($datetocheckbooking >= $starttime && $datetocheckbooking <= $endtime) {
126 for ($hour=$startHourstring; $hour < $endHourstring; $hour+= $offsethour) {
127 for ($min=0; $min < 60; $min += $offsetmin) {
128 $hourstring = $hour;
129 $minstring = $min;
130 if ($hour < 10) {
131 $hourstring = "0".$hourstring;
132 }
133 if ($min < 10) {
134 $minstring = "0".$minstring;
135 }
136 $response["availability"][$hourstring.":".$minstring] = intval($obj->duration);
137 }
138 }
139 }
140 $i++;
141 }
142 if ($i == $num) {
143 $response["code"] = "SUCCESS";
144 } else {
145 $response["code"] = "ERROR";
146 $error ++;
147 }
148 }
149
150 // Select also all not available ranges
151 if (!$error) {
152 $datetocheckbooking_end = dol_time_plus_duree($datetocheckbooking, 1, 'd');
153
154 $sql = "SELECT b.datep, b.id";
155 $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm as b";
156 $sql .= " WHERE b.datep >= '".$db->idate($datetocheckbooking)."'";
157 $sql .= " AND b.datep < '".$db->idate($datetocheckbooking_end)."'";
158 $sql .= " AND b.code = 'AC_RDV'";
159 $sql .= " AND b.status = 0";
160 $sql .= " AND b.fk_bookcal_calendar = ".((int) $id);
161 $resql = $db->query($sql);
162 if ($resql) {
163 $num = $db->num_rows($resql);
164 $i = 0;
165 while ($i < $num) {
166 $obj = $db->fetch_object($resql);
167 $datebooking = $db->jdate($obj->datep);
168 $datebookingarray = dol_getdate($datebooking);
169 $hourstring = $datebookingarray["hours"];
170 $minstring = $datebookingarray["minutes"];
171 if ($hourstring < 10) {
172 $hourstring = "0".$hourstring;
173 }
174 if ($minstring < 10) {
175 $minstring = "0".$minstring;
176 }
177 $response["availability"][$hourstring.":".$minstring] *= -1;
178 $i++;
179 }
180 }
181 }
182 }
183 $result = $response;
184}
185
186
187echo json_encode($result);
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
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.