dolibarr 19.0.3
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{
40 public static $FIELDS = array(
41 'socid',
42 'fk_project',
43 'description',
44 );
45
49 public static $FIELDSLINE = array(
50 'description',
51 'date',
52 'duree',
53 );
54
58 public $fichinter;
59
63 public function __construct()
64 {
65 global $db, $conf;
66 $this->db = $db;
67 $this->fichinter = new Fichinter($this->db);
68 }
69
79 public function get($id)
80 {
81 if (!DolibarrApiAccess::$user->rights->ficheinter->lire) {
82 throw new RestException(401);
83 }
84
85 $result = $this->fichinter->fetch($id);
86 if (!$result) {
87 throw new RestException(404, 'Intervention not found');
88 }
89
90 if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
91 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
92 }
93
94 $this->fichinter->fetchObjectLinked();
95 return $this->_cleanObjectDatas($this->fichinter);
96 }
97
113 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $properties = '')
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->_filterObjectProperties($this->_cleanObjectDatas($fichinter_static), $properties);
187 }
188 $i++;
189 }
190 } else {
191 throw new RestException(503, 'Error when retrieve intervention list : '.$this->db->lasterror());
192 }
193
194 return $obj_ret;
195 }
196
203 public function post($request_data = null)
204 {
205 if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
206 throw new RestException(401, "Insuffisant rights");
207 }
208 // Check mandatory fields
209 $result = $this->_validate($request_data);
210 foreach ($request_data as $field => $value) {
211 if ($field === 'caller') {
212 // 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 whith the caller
213 $this->fichinter->context['caller'] = $request_data['caller'];
214 continue;
215 }
216
217 $this->fichinter->$field = $value;
218 }
219
220 if ($this->fichinter->create(DolibarrApiAccess::$user) < 0) {
221 throw new RestException(500, "Error creating intervention", array_merge(array($this->fichinter->error), $this->fichinter->errors));
222 }
223
224 return $this->fichinter->id;
225 }
226
227
237 /* TODO
238 public function getLines($id)
239 {
240 if(! DolibarrApiAccess::$user->rights->ficheinter->lire) {
241 throw new RestException(401);
242 }
243
244 $result = $this->fichinter->fetch($id);
245 if( ! $result ) {
246 throw new RestException(404, 'Intervention not found');
247 }
248
249 if( ! DolibarrApi::_checkAccessToResource('fichinter',$this->fichinter->id)) {
250 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
251 }
252 $this->fichinter->getLinesArray();
253 $result = array();
254 foreach ($this->fichinter->lines as $line) {
255 array_push($result,$this->_cleanObjectDatas($line));
256 }
257 return $result;
258 }
259 */
260
271 public function postLine($id, $request_data = null)
272 {
273 if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
274 throw new RestException(401, "Insuffisant rights");
275 }
276 // Check mandatory fields
277 $result = $this->_validateLine($request_data);
278
279 foreach ($request_data as $field => $value) {
280 if ($field === 'caller') {
281 // 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 whith the caller
282 $this->fichinter->context['caller'] = $request_data['caller'];
283 continue;
284 }
285
286 $this->fichinter->$field = $value;
287 }
288
289 if (!$result) {
290 throw new RestException(404, 'Intervention not found');
291 }
292
293 if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
294 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
295 }
296
297 $updateRes = $this->fichinter->addLine(
298 DolibarrApiAccess::$user,
299 $id,
300 $this->fichinter->description,
301 $this->fichinter->date,
302 $this->fichinter->duree
303 );
304
305 if ($updateRes > 0) {
306 return $updateRes;
307 } else {
308 throw new RestException(400, $this->fichinter->error);
309 }
310 }
311
318 public function delete($id)
319 {
320 if (!DolibarrApiAccess::$user->rights->ficheinter->supprimer) {
321 throw new RestException(401);
322 }
323 $result = $this->fichinter->fetch($id);
324 if (!$result) {
325 throw new RestException(404, 'Intervention not found');
326 }
327
328 if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
329 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
330 }
331
332 if (!$this->fichinter->delete(DolibarrApiAccess::$user)) {
333 throw new RestException(500, 'Error when delete intervention : '.$this->fichinter->error);
334 }
335
336 return array(
337 'success' => array(
338 'code' => 200,
339 'message' => 'Intervention deleted'
340 )
341 );
342 }
343
359 public function validate($id, $notrigger = 0)
360 {
361 if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
362 throw new RestException(401, "Insuffisant rights");
363 }
364 $result = $this->fichinter->fetch($id);
365 if (!$result) {
366 throw new RestException(404, 'Intervention not found');
367 }
368
369 if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
370 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
371 }
372
373 $result = $this->fichinter->setValid(DolibarrApiAccess::$user, $notrigger);
374 if ($result == 0) {
375 throw new RestException(304, 'Error nothing done. May be object is already validated');
376 }
377 if ($result < 0) {
378 throw new RestException(500, 'Error when validating Intervention: '.$this->fichinter->error);
379 }
380
381 $this->fichinter->fetchObjectLinked();
382
383 return $this->_cleanObjectDatas($this->fichinter);
384 }
385
395 public function closeFichinter($id)
396 {
397 if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
398 throw new RestException(401, "Insuffisant rights");
399 }
400 $result = $this->fichinter->fetch($id);
401 if (!$result) {
402 throw new RestException(404, 'Intervention not found');
403 }
404
405 if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
406 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
407 }
408
409 $result = $this->fichinter->setStatut(3);
410
411 if ($result == 0) {
412 throw new RestException(304, 'Error nothing done. May be object is already closed');
413 }
414 if ($result < 0) {
415 throw new RestException(500, 'Error when closing Intervention: '.$this->fichinter->error);
416 }
417
418 $this->fichinter->fetchObjectLinked();
419
420 return $this->_cleanObjectDatas($this->fichinter);
421 }
422
431 private function _validate($data)
432 {
433 $fichinter = array();
434 foreach (Interventions::$FIELDS as $field) {
435 if (!isset($data[$field])) {
436 throw new RestException(400, "$field field missing");
437 }
438 $fichinter[$field] = $data[$field];
439 }
440 return $fichinter;
441 }
442
443
444 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
451 protected function _cleanObjectDatas($object)
452 {
453 // phpcs:enable
454 $object = parent::_cleanObjectDatas($object);
455
456 unset($object->labelStatus);
457 unset($object->labelStatusShort);
458
459 return $object;
460 }
461
470 private function _validateLine($data)
471 {
472 $fichinter = array();
473 foreach (Interventions::$FIELDSLINE as $field) {
474 if (!isset($data[$field])) {
475 throw new RestException(400, "$field field missing");
476 }
477 $fichinter[$field] = $data[$field];
478 }
479 return $fichinter;
480 }
481}
Class for API REST v1.
Definition api.class.php:31
_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.
Class to manage interventions.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='')
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.