dolibarr 22.0.5
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
39{
43 public static $FIELDS = array(
44 'socid',
45 'fk_project',
46 'description',
47 );
48
52 public static $FIELDSLINE = array(
53 'description',
54 'date',
55 'duration',
56 );
57
61 public $fichinter;
62
66 public function __construct()
67 {
68 global $db;
69 $this->db = $db;
70 $this->fichinter = new Fichinter($this->db);
71 }
72
87 public function get($id, $ref = '', $ref_ext = '', $contact_list = 1)
88 {
89 if (!DolibarrApiAccess::$user->hasRight('ficheinter', 'lire')) {
90 throw new RestException(403);
91 }
92
93 $result = $this->fichinter->fetch($id, $ref, $ref_ext);
94 if (!$result) {
95 throw new RestException(404, 'Intervention not found');
96 }
97
98 if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
99 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
100 }
101
102 if ($contact_list > -1) {
103 // Add external contacts ids
104 $tmparray = $this->fichinter->liste_contact(-1, 'external', $contact_list);
105 if (is_array($tmparray)) {
106 $this->fichinter->contacts_ids = $tmparray;
107 }
108 $tmparray = $this->fichinter->liste_contact(-1, 'internal', $contact_list);
109 if (is_array($tmparray)) {
110 $this->fichinter->contacts_ids_internal = $tmparray;
111 }
112 }
113
114 $this->fichinter->fetchObjectLinked();
115
116 return $this->_cleanObjectDatas($this->fichinter);
117 }
118
141 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $properties = '', $contact_type = '', $pagination_data = false)
142 {
143 if (!DolibarrApiAccess::$user->hasRight('ficheinter', 'lire')) {
144 throw new RestException(403);
145 }
146
147 $obj_ret = array();
148
149 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
150 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
151
152 // If the internal user must only see his customers, force searching by him
153 $search_sale = 0;
154 if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
155 $search_sale = DolibarrApiAccess::$user->id;
156 }
157
158 $sql = "SELECT t.rowid";
159 $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
160 $sql .= ' WHERE t.entity IN ('.getEntity('intervention').')';
161 if ($socids) {
162 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
163 }
164 // Search on sale representative
165 if ($search_sale && $search_sale != '-1') {
166 if ($search_sale == -2) {
167 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
168 } elseif ($search_sale > 0) {
169 $sql .= " AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).")";
170 }
171 }
172 // Add sql filters
173 if ($sqlfilters) {
174 $errormessage = '';
175 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
176 if ($errormessage) {
177 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
178 }
179 }
180
181 //this query will return total interventions with the filters given
182 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
183
184 $sql .= $this->db->order($sortfield, $sortorder);
185 if ($limit) {
186 if ($page < 0) {
187 $page = 0;
188 }
189 $offset = $limit * $page;
190
191 $sql .= $this->db->plimit($limit + 1, $offset);
192 }
193
194 dol_syslog("API Rest request");
195 $result = $this->db->query($sql);
196
197 if ($result) {
198 $num = $this->db->num_rows($result);
199 $min = min($num, ($limit <= 0 ? $num : $limit));
200 $i = 0;
201 while ($i < $min) {
202 $obj = $this->db->fetch_object($result);
203 $fichinter_static = new Fichinter($this->db);
204 if ($fichinter_static->fetch($obj->rowid)) {
205 if ($contact_type) {
206 $fichinter_static->contacts_ids = $fichinter_static->liste_contact(-1, $contact_type, 1);
207 }
208 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($fichinter_static), $properties);
209 }
210 $i++;
211 }
212 } else {
213 throw new RestException(503, 'Error when retrieve intervention list : '.$this->db->lasterror());
214 }
215
216 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
217 if ($pagination_data) {
218 $totalsResult = $this->db->query($sqlTotals);
219 $total = $this->db->fetch_object($totalsResult)->total;
220
221 $tmp = $obj_ret;
222 $obj_ret = [];
223
224 $obj_ret['data'] = $tmp;
225 $obj_ret['pagination'] = [
226 'total' => (int) $total,
227 'page' => $page, //count starts from 0
228 'page_count' => ceil((int) $total / $limit),
229 'limit' => $limit
230 ];
231 }
232
233 return $obj_ret;
234 }
235
248 public function post($request_data = null)
249 {
250 if (!DolibarrApiAccess::$user->hasRight('ficheinter', 'creer')) {
251 throw new RestException(403, "Insuffisant rights");
252 }
253 // Check mandatory fields
254 $result = $this->_validate($request_data);
255 foreach ($request_data as $field => $value) {
256 if ($field === 'caller') {
257 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
258 $this->fichinter->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
259 continue;
260 }
261
262 $this->fichinter->$field = $this->_checkValForAPI($field, $value, $this->fichinter);
263 }
264
265 if ($this->fichinter->create(DolibarrApiAccess::$user) < 0) {
266 throw new RestException(500, "Error creating intervention", array_merge(array($this->fichinter->error), $this->fichinter->errors));
267 }
268
269 return $this->fichinter->id;
270 }
271
285 public function put($id, $request_data = null)
286 {
287 if (!DolibarrApiAccess::$user->hasRight('ficheinter', 'creer')) {
288 throw new RestException(403);
289 }
290
291 $result = $this->fichinter->fetch($id);
292 if (!$result) {
293 throw new RestException(404, 'Fichinter not found');
294 }
295
296 if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
297 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
298 }
299 foreach ($request_data as $field => $value) {
300 if ($field == 'id') {
301 continue;
302 }
303 if ($field === 'caller') {
304 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
305 $this->fichinter->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
306 continue;
307 }
308 if ($field == 'array_options' && is_array($value)) {
309 foreach ($value as $index => $val) {
310 $this->fichinter->array_options[$index] = $this->_checkValForAPI($field, $val, $this->fichinter);
311 }
312 continue;
313 }
314
315 $this->fichinter->$field = $this->_checkValForAPI($field, $value, $this->fichinter);
316 }
317
318 if ($this->fichinter->update(DolibarrApiAccess::$user) > 0) {
319 return $this->get($id);
320 } else {
321 throw new RestException(500, $this->fichinter->error);
322 }
323 }
324
334 /* TODO
335 public function getLines($id)
336 {
337 if(! DolibarrApiAccess::$user->hasRight('ficheinter', 'lire')) {
338 throw new RestException(403);
339 }
340
341 $result = $this->fichinter->fetch($id);
342 if( ! $result ) {
343 throw new RestException(404, 'Intervention not found');
344 }
345
346 if( ! DolibarrApi::_checkAccessToResource('fichinter',$this->fichinter->id)) {
347 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
348 }
349 $this->fichinter->getLinesArray();
350 $result = array();
351 foreach ($this->fichinter->lines as $line) {
352 array_push($result,$this->_cleanObjectDatas($line));
353 }
354 return $result;
355 }
356 */
357
374 public function postLine($id, $request_data = null)
375 {
376 if (!DolibarrApiAccess::$user->hasRight('ficheinter', 'creer')) {
377 throw new RestException(403, "Insuffisant rights");
378 }
379 // Check mandatory fields
380 $result = $this->_validateLine($request_data);
381
382 foreach ($request_data as $field => $value) {
383 if ($field === 'caller') {
384 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
385 $this->fichinter->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
386 continue;
387 }
388
389 $this->fichinter->$field = $this->_checkValForAPI($field, $value, $this->fichinter);
390 }
391
392 if (!$result) {
393 throw new RestException(404, 'Intervention not found');
394 }
395
396 if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
397 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
398 }
399
400 $updateRes = $this->fichinter->addLine(
401 DolibarrApiAccess::$user,
402 $id,
403 $this->fichinter->description,
404 $this->fichinter->date,
405 $this->fichinter->duration
406 );
407
408 if ($updateRes > 0) {
409 return $updateRes;
410 } else {
411 throw new RestException(400, $this->fichinter->error);
412 }
413 }
414
427 public function delete($id)
428 {
429 if (!DolibarrApiAccess::$user->hasRight('ficheinter', 'supprimer')) {
430 throw new RestException(403);
431 }
432 $result = $this->fichinter->fetch($id);
433 if (!$result) {
434 throw new RestException(404, 'Intervention not found');
435 }
436
437 if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
438 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
439 }
440
441 if (!$this->fichinter->delete(DolibarrApiAccess::$user)) {
442 throw new RestException(500, 'Error when delete intervention : '.$this->fichinter->error);
443 }
444
445 return array(
446 'success' => array(
447 'code' => 200,
448 'message' => 'Intervention deleted'
449 )
450 );
451 }
452
466 public function reopen($id)
467 {
468 if (!DolibarrApiAccess::$user->hasRight('ficheinter', 'creer')) {
469 throw new RestException(403, "Insuffisant rights");
470 }
471 $result = $this->fichinter->fetch($id);
472 if (!$result) {
473 throw new RestException(404, 'Intervention not found');
474 }
475
476 if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
477 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
478 }
479 $result = $this->fichinter->setDraft(DolibarrApiAccess::$user);
480 if ($result == 0) {
481 throw new RestException(304, 'Error nothing done. May be object is already set as draft');
482 }
483 if ($result < 0) {
484 throw new RestException(500, 'Error when closing Intervention: '.$this->fichinter->error);
485 }
486 $this->fichinter->fetchObjectLinked();
487 return $this->_cleanObjectDatas($this->fichinter);
488 }
489
509 public function validate($id, $notrigger = 0)
510 {
511 if (!DolibarrApiAccess::$user->hasRight('ficheinter', 'creer')) {
512 throw new RestException(403, "Insuffisant rights");
513 }
514 $result = $this->fichinter->fetch($id);
515 if (!$result) {
516 throw new RestException(404, 'Intervention not found');
517 }
518
519 if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
520 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
521 }
522
523 $result = $this->fichinter->setValid(DolibarrApiAccess::$user, $notrigger);
524 if ($result == 0) {
525 throw new RestException(304, 'Error nothing done. May be object is already validated');
526 }
527 if ($result < 0) {
528 throw new RestException(500, 'Error when validating Intervention: '.$this->fichinter->error);
529 }
530
531 $this->fichinter->fetchObjectLinked();
532
533 return $this->_cleanObjectDatas($this->fichinter);
534 }
535
549 public function closeFichinter($id)
550 {
551 if (!DolibarrApiAccess::$user->hasRight('ficheinter', 'creer')) {
552 throw new RestException(403, "Insuffisant rights");
553 }
554 $result = $this->fichinter->fetch($id);
555 if (!$result) {
556 throw new RestException(404, 'Intervention not found');
557 }
558
559 if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
560 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
561 }
562
563 $result = $this->fichinter->setStatut(3);
564
565 if ($result == 0) {
566 throw new RestException(304, 'Error nothing done. May be object is already closed');
567 }
568 if ($result < 0) {
569 throw new RestException(500, 'Error when closing Intervention: '.$this->fichinter->error);
570 }
571
572 $this->fichinter->fetchObjectLinked();
573
574 return $this->_cleanObjectDatas($this->fichinter);
575 }
576
585 private function _validate($data)
586 {
587 $fichinter = array();
588 foreach (Interventions::$FIELDS as $field) {
589 if (!isset($data[$field])) {
590 throw new RestException(400, "$field field missing");
591 }
592 $fichinter[$field] = $data[$field];
593 }
594 return $fichinter;
595 }
596
597
598 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
605 protected function _cleanObjectDatas($object)
606 {
607 // phpcs:enable
608 $object = parent::_cleanObjectDatas($object);
609
610 unset($object->labelStatus);
611 unset($object->labelStatusShort);
612
613 return $object;
614 }
615
624 private function _validateLine($data)
625 {
626 if ($data === null) {
627 $data = array();
628 }
629 $fichinter = array();
630 foreach (Interventions::$FIELDSLINE as $field) {
631 if (!isset($data[$field])) {
632 throw new RestException(400, "$field field missing");
633 }
634 $fichinter[$field] = $data[$field];
635 }
636 return $fichinter;
637 }
638}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:67
Class for API REST v1.
Definition api.class.php:33
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition api.class.php:98
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='', $contact_type='', $pagination_data=false)
List interventions.
_cleanObjectDatas($object)
Clean sensible object data.
validate($id, $notrigger=0)
Validate an intervention.
_validateLine($data)
Validate fields before create or update object.
put($id, $request_data=null)
Update intervention general fields (won't touch lines of fichinter)
post($request_data=null)
Create an intervention.
closeFichinter($id)
Close an intervention.
_validate($data)
Validate fields before create or update object.
reopen($id)
Reopen an intervention.
postLine($id, $request_data=null)
Get lines of intervention.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.