dolibarr 20.0.2
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
65top_httphead('application/json');
66
67
68if ($action == 'verifyavailability') {
69 $response = array();
70 if (empty($id)) {
71 $error++;
72 $response["code"] = "MISSING_ID";
73 $response["message"] = "Missing parameter id";
74 header('HTTP/1.0 400 Bad Request');
75 echo json_encode($response);
76 exit;
77 }
78 if (empty($datetocheckbooking)) {
79 $error++;
80 $response["code"] = "MISSING_DATE_AVAILABILITY";
81 $response["message"] = "Missing parameter datetocheck";
82 header('HTTP/1.0 400 Bad Request');
83 echo json_encode($response);
84 exit;
85 }
86
87 // First get all ranges for the calendar
88 if (!$error) {
89 // Select in database all availabilities
90 $availabilitytab = array();
91 $sql = "SELECT ba.rowid as id, ba.duration, ba.startHour, ba.endHour, ba.start, ba.end";
92 $sql .= " FROM ".MAIN_DB_PREFIX."bookcal_availabilities as ba";
93 $sql .= " WHERE ba.fk_bookcal_calendar = ".((int) $id);
94 $sql .= " AND ba.status = 1";
95 $resql = $db->query($sql);
96 if ($resql) {
97 $num = $db->num_rows($resql);
98 $i = 0;
99 while ($i < $num) {
100 $obj = $db->fetch_object($resql);
101 $starttime = $db->jdate($obj->start);
102 $endtime = $db->jdate($obj->end);
103 $offsetmin = $obj->duration % 60;
104 if ($offsetmin == 0) {
105 $offsetmin = 60;
106 }
107 $startHourstring = $obj->startHour;
108 $endHourstring = $obj->endHour;
109 if ($startHourstring <= 0) {
110 $startHourstring = 0;
111 }
112 if ($endHourstring >= 24) {
113 $endHourstring = 24;
114 }
115 $offsethour = round($obj->duration / 60);
116 // Creation of array of availabilties range
117 if ($datetocheckbooking >= $starttime && $datetocheckbooking <= $endtime) {
118 for ($hour=$startHourstring; $hour < $endHourstring; $hour+= $offsethour) {
119 for ($min=0; $min < 60; $min += $offsetmin) {
120 $hourstring = $hour;
121 $minstring = $min;
122 if ($hour < 10) {
123 $hourstring = "0".$hourstring;
124 }
125 if ($min < 10) {
126 $minstring = "0".$minstring;
127 }
128 $response["availability"][$hourstring.":".$minstring] = intval($obj->duration);
129 }
130 }
131 }
132 $i++;
133 }
134 if ($i == $num) {
135 $response["code"] = "SUCCESS";
136 } else {
137 $response["code"] = "ERROR";
138 $error ++;
139 }
140 }
141
142 // Select also all not available ranges
143 if (!$error) {
144 $datetocheckbooking_end = dol_time_plus_duree($datetocheckbooking, 1, 'd');
145
146 $sql = "SELECT b.datep, b.id";
147 $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm as b";
148 $sql .= " WHERE b.datep >= '".$db->idate($datetocheckbooking)."'";
149 $sql .= " AND b.datep < '".$db->idate($datetocheckbooking_end)."'";
150 $sql .= " AND b.code = 'AC_RDV'";
151 $sql .= " AND b.status = 0";
152 $sql .= " AND b.fk_bookcal_calendar = ".((int) $id);
153 $resql = $db->query($sql);
154 if ($resql) {
155 $num = $db->num_rows($resql);
156 $i = 0;
157 while ($i < $num) {
158 $obj = $db->fetch_object($resql);
159 $datebooking = $db->jdate($obj->datep);
160 $datebookingarray = dol_getdate($datebooking);
161 $hourstring = $datebookingarray["hours"];
162 $minstring = $datebookingarray["minutes"];
163 if ($hourstring < 10) {
164 $hourstring = "0".$hourstring;
165 }
166 if ($minstring < 10) {
167 $minstring = "0".$minstring;
168 }
169 $response["availability"][$hourstring.":".$minstring] *= -1;
170 $i++;
171 }
172 }
173 }
174 }
175 $result = $response;
176}
177
178
179/*
180 * View
181 */
182
183echo json_encode($result);
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.