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 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
24use Luracast\Restler\RestException;
25
26require_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php';
27
28
36{
37
41 public static $FIELDS = array(
42 'socid',
43 'fk_project',
44 'description',
45 );
46
50 public static $FIELDSLINE = array(
51 'description',
52 'date',
53 'duree',
54 );
55
59 public $fichinter;
60
64 public function __construct()
65 {
66 global $db, $conf;
67 $this->db = $db;
68 $this->fichinter = new Fichinter($this->db);
69 }
70
80 public function get($id)
81 {
82 if (!DolibarrApiAccess::$user->rights->ficheinter->lire) {
83 throw new RestException(401);
84 }
85
86 $result = $this->fichinter->fetch($id);
87 if (!$result) {
88 throw new RestException(404, 'Intervention not found');
89 }
90
91 if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
92 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
93 }
94
95 $this->fichinter->fetchObjectLinked();
96 return $this->_cleanObjectDatas($this->fichinter);
97 }
98
113 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '')
114 {
115 global $db, $conf;
116
117 if (!DolibarrApiAccess::$user->rights->ficheinter->lire) {
118 throw new RestException(401);
119 }
120
121 $obj_ret = array();
122
123 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
124 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
125
126 // If the internal user must only see his customers, force searching by him
127 $search_sale = 0;
128 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
129 $search_sale = DolibarrApiAccess::$user->id;
130 }
131
132 $sql = "SELECT t.rowid";
133 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
134 $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)
135 }
136 $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
137
138 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
139 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
140 }
141
142 $sql .= ' WHERE t.entity IN ('.getEntity('intervention').')';
143 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
144 $sql .= " AND t.fk_soc = sc.fk_soc";
145 }
146 if ($socids) {
147 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
148 }
149 if ($search_sale > 0) {
150 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
151 }
152 // Insert sale filter
153 if ($search_sale > 0) {
154 $sql .= " AND sc.fk_user = ".((int) $search_sale);
155 }
156 // Add sql filters
157 if ($sqlfilters) {
158 $errormessage = '';
159 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
160 if ($errormessage) {
161 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
162 }
163 }
164
165 $sql .= $this->db->order($sortfield, $sortorder);
166 if ($limit) {
167 if ($page < 0) {
168 $page = 0;
169 }
170 $offset = $limit * $page;
171
172 $sql .= $this->db->plimit($limit + 1, $offset);
173 }
174
175 dol_syslog("API Rest request");
176 $result = $this->db->query($sql);
177
178 if ($result) {
179 $num = $this->db->num_rows($result);
180 $min = min($num, ($limit <= 0 ? $num : $limit));
181 $i = 0;
182 while ($i < $min) {
183 $obj = $this->db->fetch_object($result);
184 $fichinter_static = new Fichinter($this->db);
185 if ($fichinter_static->fetch($obj->rowid)) {
186 $obj_ret[] = $this->_cleanObjectDatas($fichinter_static);
187 }
188 $i++;
189 }
190 } else {
191 throw new RestException(503, 'Error when retrieve intervention list : '.$this->db->lasterror());
192 }
193 if (!count($obj_ret)) {
194 throw new RestException(404, 'No intervention found');
195 }
196 return $obj_ret;
197 }
198
205 public function post($request_data = null)
206 {
207 if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
208 throw new RestException(401, "Insuffisant rights");
209 }
210 // Check mandatory fields
211 $result = $this->_validate($request_data);
212 foreach ($request_data as $field => $value) {
213 $this->fichinter->$field = $value;
214 }
215
216 if ($this->fichinter->create(DolibarrApiAccess::$user) < 0) {
217 throw new RestException(500, "Error creating intervention", array_merge(array($this->fichinter->error), $this->fichinter->errors));
218 }
219
220 return $this->fichinter->id;
221 }
222
223
233 /* TODO
234 public function getLines($id)
235 {
236 if(! DolibarrApiAccess::$user->rights->ficheinter->lire) {
237 throw new RestException(401);
238 }
239
240 $result = $this->fichinter->fetch($id);
241 if( ! $result ) {
242 throw new RestException(404, 'Intervention not found');
243 }
244
245 if( ! DolibarrApi::_checkAccessToResource('fichinter',$this->fichinter->id)) {
246 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
247 }
248 $this->fichinter->getLinesArray();
249 $result = array();
250 foreach ($this->fichinter->lines as $line) {
251 array_push($result,$this->_cleanObjectDatas($line));
252 }
253 return $result;
254 }
255 */
256
267 public function postLine($id, $request_data = null)
268 {
269 if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
270 throw new RestException(401, "Insuffisant rights");
271 }
272 // Check mandatory fields
273 $result = $this->_validateLine($request_data);
274
275 foreach ($request_data as $field => $value) {
276 $this->fichinter->$field = $value;
277 }
278
279 if (!$result) {
280 throw new RestException(404, 'Intervention not found');
281 }
282
283 if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
284 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
285 }
286
287 $updateRes = $this->fichinter->addLine(
288 DolibarrApiAccess::$user,
289 $id,
290 $this->fichinter->description,
291 $this->fichinter->date,
292 $this->fichinter->duree
293 );
294
295 if ($updateRes > 0) {
296 return $updateRes;
297 } else {
298 throw new RestException(400, $this->fichinter->error);
299 }
300 }
301
308 public function delete($id)
309 {
310 if (!DolibarrApiAccess::$user->rights->ficheinter->supprimer) {
311 throw new RestException(401);
312 }
313 $result = $this->fichinter->fetch($id);
314 if (!$result) {
315 throw new RestException(404, 'Intervention not found');
316 }
317
318 if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
319 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
320 }
321
322 if (!$this->fichinter->delete(DolibarrApiAccess::$user)) {
323 throw new RestException(500, 'Error when delete intervention : '.$this->fichinter->error);
324 }
325
326 return array(
327 'success' => array(
328 'code' => 200,
329 'message' => 'Intervention deleted'
330 )
331 );
332 }
333
349 public function validate($id, $notrigger = 0)
350 {
351 if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
352 throw new RestException(401, "Insuffisant rights");
353 }
354 $result = $this->fichinter->fetch($id);
355 if (!$result) {
356 throw new RestException(404, 'Intervention not found');
357 }
358
359 if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
360 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
361 }
362
363 $result = $this->fichinter->setValid(DolibarrApiAccess::$user, $notrigger);
364 if ($result == 0) {
365 throw new RestException(304, 'Error nothing done. May be object is already validated');
366 }
367 if ($result < 0) {
368 throw new RestException(500, 'Error when validating Intervention: '.$this->commande->error);
369 }
370
371 $this->fichinter->fetchObjectLinked();
372
373 return $this->_cleanObjectDatas($this->fichinter);
374 }
375
385 public function closeFichinter($id)
386 {
387 if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
388 throw new RestException(401, "Insuffisant rights");
389 }
390 $result = $this->fichinter->fetch($id);
391 if (!$result) {
392 throw new RestException(404, 'Intervention not found');
393 }
394
395 if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
396 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
397 }
398
399 $result = $this->fichinter->setStatut(3);
400
401 if ($result == 0) {
402 throw new RestException(304, 'Error nothing done. May be object is already closed');
403 }
404 if ($result < 0) {
405 throw new RestException(500, 'Error when closing Intervention: '.$this->fichinter->error);
406 }
407
408 $this->fichinter->fetchObjectLinked();
409
410 return $this->_cleanObjectDatas($this->fichinter);
411 }
412
421 private function _validate($data)
422 {
423 $fichinter = array();
424 foreach (Interventions::$FIELDS as $field) {
425 if (!isset($data[$field])) {
426 throw new RestException(400, "$field field missing");
427 }
428 $fichinter[$field] = $data[$field];
429 }
430 return $fichinter;
431 }
432
433
434 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
441 protected function _cleanObjectDatas($object)
442 {
443 // phpcs:enable
444 $object = parent::_cleanObjectDatas($object);
445
446 unset($object->statuts_short);
447 unset($object->statuts_logo);
448 unset($object->statuts);
449
450 return $object;
451 }
452
461 private function _validateLine($data)
462 {
463 $fichinter = array();
464 foreach (Interventions::$FIELDSLINE as $field) {
465 if (!isset($data[$field])) {
466 throw new RestException(400, "$field field missing");
467 }
468 $fichinter[$field] = $data[$field];
469 }
470 return $fichinter;
471 }
472}
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.