dolibarr 18.0.6
api_interventions.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
3 * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2025 William Mead <william@m34d.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (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 General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
25use Luracast\Restler\RestException;
26
27require_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php';
28
29
37{
38
42 public static $FIELDS = array(
43 'socid',
44 'fk_project',
45 'description',
46 );
47
51 public static $FIELDSLINE = array(
52 'description',
53 'date',
54 'duree',
55 );
56
60 public $fichinter;
61
65 public function __construct()
66 {
67 global $db, $conf;
68 $this->db = $db;
69 $this->fichinter = new Fichinter($this->db);
70 }
71
82 public function get($id)
83 {
84 if (!DolibarrApiAccess::$user->rights->ficheinter->lire) {
85 throw new RestException(401);
86 }
87
88 $result = $this->fichinter->fetch($id);
89 if (!$result) {
90 throw new RestException(404, 'Intervention not found');
91 }
92
93 if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
94 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
95 }
96
97 $this->fichinter->fetchObjectLinked();
98 return $this->_cleanObjectDatas($this->fichinter);
99 }
100
115 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '')
116 {
117 global $db, $conf;
118
119 if (!DolibarrApiAccess::$user->rights->ficheinter->lire) {
120 throw new RestException(401);
121 }
122
123 $obj_ret = array();
124
125 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
126 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
127
128 // If the internal user must only see his customers, force searching by him
129 $search_sale = 0;
130 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
131 $search_sale = DolibarrApiAccess::$user->id;
132 }
133
134 $sql = "SELECT t.rowid";
135 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
136 $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
137 }
138 $sql .= " FROM ".MAIN_DB_PREFIX."fichinter AS t LEFT JOIN ".MAIN_DB_PREFIX."fichinter_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
139
140 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
141 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
142 }
143
144 $sql .= ' WHERE t.entity IN ('.getEntity('intervention').')';
145 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
146 $sql .= " AND t.fk_soc = sc.fk_soc";
147 }
148 if ($socids) {
149 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
150 }
151 if ($search_sale > 0) {
152 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
153 }
154 // Insert sale filter
155 if ($search_sale > 0) {
156 $sql .= " AND sc.fk_user = ".((int) $search_sale);
157 }
158 // Add sql filters
159 if ($sqlfilters) {
160 $errormessage = '';
161 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
162 if ($errormessage) {
163 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
164 }
165 }
166
167 $sql .= $this->db->order($sortfield, $sortorder);
168 if ($limit) {
169 if ($page < 0) {
170 $page = 0;
171 }
172 $offset = $limit * $page;
173
174 $sql .= $this->db->plimit($limit + 1, $offset);
175 }
176
177 dol_syslog("API Rest request");
178 $result = $this->db->query($sql);
179
180 if ($result) {
181 $num = $this->db->num_rows($result);
182 $min = min($num, ($limit <= 0 ? $num : $limit));
183 $i = 0;
184 while ($i < $min) {
185 $obj = $this->db->fetch_object($result);
186 $fichinter_static = new Fichinter($this->db);
187 if ($fichinter_static->fetch($obj->rowid)) {
188 $obj_ret[] = $this->_cleanObjectDatas($fichinter_static);
189 }
190 $i++;
191 }
192 } else {
193 throw new RestException(503, 'Error when retrieve intervention list : '.$this->db->lasterror());
194 }
195 if (!count($obj_ret)) {
196 throw new RestException(404, 'No intervention found');
197 }
198 return $obj_ret;
199 }
200
207 public function post($request_data = null)
208 {
209 if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
210 throw new RestException(401, "Insuffisant rights");
211 }
212 // Check mandatory fields
213 $result = $this->_validate($request_data);
214 foreach ($request_data as $field => $value) {
215 $this->fichinter->$field = $value;
216 }
217
218 if ($this->fichinter->create(DolibarrApiAccess::$user) < 0) {
219 throw new RestException(500, "Error creating intervention", array_merge(array($this->fichinter->error), $this->fichinter->errors));
220 }
221
222 return $this->fichinter->id;
223 }
224
225
235 /* TODO
236 public function getLines($id)
237 {
238 if(! DolibarrApiAccess::$user->rights->ficheinter->lire) {
239 throw new RestException(401);
240 }
241
242 $result = $this->fichinter->fetch($id);
243 if( ! $result ) {
244 throw new RestException(404, 'Intervention not found');
245 }
246
247 if( ! DolibarrApi::_checkAccessToResource('fichinter',$this->fichinter->id)) {
248 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
249 }
250 $this->fichinter->getLinesArray();
251 $result = array();
252 foreach ($this->fichinter->lines as $line) {
253 array_push($result,$this->_cleanObjectDatas($line));
254 }
255 return $result;
256 }
257 */
258
269 public function postLine($id, $request_data = null)
270 {
271 if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
272 throw new RestException(401, "Insuffisant rights");
273 }
274 // Check mandatory fields
275 $result = $this->_validateLine($request_data);
276
277 foreach ($request_data as $field => $value) {
278 $this->fichinter->$field = $value;
279 }
280
281 if (!$result) {
282 throw new RestException(404, 'Intervention not found');
283 }
284
285 if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
286 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
287 }
288
289 $updateRes = $this->fichinter->addLine(
290 DolibarrApiAccess::$user,
291 $id,
292 $this->fichinter->description,
293 $this->fichinter->date,
294 $this->fichinter->duree
295 );
296
297 if ($updateRes > 0) {
298 return $updateRes;
299 } else {
300 throw new RestException(400, $this->fichinter->error);
301 }
302 }
303
310 public function delete($id)
311 {
312 if (!DolibarrApiAccess::$user->rights->ficheinter->supprimer) {
313 throw new RestException(401);
314 }
315 $result = $this->fichinter->fetch($id);
316 if (!$result) {
317 throw new RestException(404, 'Intervention not found');
318 }
319
320 if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
321 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
322 }
323
324 if (!$this->fichinter->delete(DolibarrApiAccess::$user)) {
325 throw new RestException(500, 'Error when delete intervention : '.$this->fichinter->error);
326 }
327
328 return array(
329 'success' => array(
330 'code' => 200,
331 'message' => 'Intervention deleted'
332 )
333 );
334 }
335
351 public function validate($id, $notrigger = 0)
352 {
353 if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
354 throw new RestException(401, "Insuffisant rights");
355 }
356 $result = $this->fichinter->fetch($id);
357 if (!$result) {
358 throw new RestException(404, 'Intervention not found');
359 }
360
361 if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
362 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
363 }
364
365 $result = $this->fichinter->setValid(DolibarrApiAccess::$user, $notrigger);
366 if ($result == 0) {
367 throw new RestException(304, 'Error nothing done. May be object is already validated');
368 }
369 if ($result < 0) {
370 throw new RestException(500, 'Error when validating Intervention: '.$this->commande->error);
371 }
372
373 $this->fichinter->fetchObjectLinked();
374
375 return $this->_cleanObjectDatas($this->fichinter);
376 }
377
387 public function closeFichinter($id)
388 {
389 if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
390 throw new RestException(401, "Insuffisant rights");
391 }
392 $result = $this->fichinter->fetch($id);
393 if (!$result) {
394 throw new RestException(404, 'Intervention not found');
395 }
396
397 if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
398 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
399 }
400
401 $result = $this->fichinter->setStatut(3);
402
403 if ($result == 0) {
404 throw new RestException(304, 'Error nothing done. May be object is already closed');
405 }
406 if ($result < 0) {
407 throw new RestException(500, 'Error when closing Intervention: '.$this->fichinter->error);
408 }
409
410 $this->fichinter->fetchObjectLinked();
411
412 return $this->_cleanObjectDatas($this->fichinter);
413 }
414
423 private function _validate($data)
424 {
425 $fichinter = array();
426 foreach (Interventions::$FIELDS as $field) {
427 if (!isset($data[$field])) {
428 throw new RestException(400, "$field field missing");
429 }
430 $fichinter[$field] = $data[$field];
431 }
432 return $fichinter;
433 }
434
435
436 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
443 protected function _cleanObjectDatas($object)
444 {
445 // phpcs:enable
446 $object = parent::_cleanObjectDatas($object);
447
448 unset($object->statuts_short);
449 unset($object->statuts_logo);
450 unset($object->statuts);
451
452 return $object;
453 }
454
463 private function _validateLine($data)
464 {
465 $fichinter = array();
466 foreach (Interventions::$FIELDSLINE as $field) {
467 if (!isset($data[$field])) {
468 throw new RestException(400, "$field field missing");
469 }
470 $fichinter[$field] = $data[$field];
471 }
472 return $fichinter;
473 }
474}
Class for API REST v1.
Definition api.class.php:31
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
Class to manage interventions.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='')
List of interventions Return a list of interventions.
_cleanObjectDatas($object)
Clean sensible object datas.
validate($id, $notrigger=0)
Validate an intervention.
_validateLine($data)
Validate fields before create or update object.
post($request_data=null)
Create intervention object.
closeFichinter($id)
Close an intervention.
_validate($data)
Validate fields before create or update object.
postLine($id, $request_data=null)
Get lines of an intervention.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.