dolibarr  17.0.4
api_interventions.class.php
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 
19  use Luracast\Restler\RestException;
20 
21  require_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php';
22 
30 {
31 
35  public static $FIELDS = array(
36  'socid',
37  'fk_project',
38  'description',
39  );
40 
44  public static $FIELDSLINE = array(
45  'description',
46  'date',
47  'duree',
48  );
49 
53  public $fichinter;
54 
58  public function __construct()
59  {
60  global $db, $conf;
61  $this->db = $db;
62  $this->fichinter = new Fichinter($this->db);
63  }
64 
75  public function get($id)
76  {
77  if (!DolibarrApiAccess::$user->rights->ficheinter->lire) {
78  throw new RestException(401);
79  }
80 
81  $result = $this->fichinter->fetch($id);
82  if (!$result) {
83  throw new RestException(404, 'Intervention not found');
84  }
85 
86  if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
87  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
88  }
89 
90  $this->fichinter->fetchObjectLinked();
91  return $this->_cleanObjectDatas($this->fichinter);
92  }
93 
109  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '')
110  {
111  global $db, $conf;
112 
113  if (!DolibarrApiAccess::$user->rights->ficheinter->lire) {
114  throw new RestException(401);
115  }
116 
117  $obj_ret = array();
118 
119  // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
120  $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
121 
122  // If the internal user must only see his customers, force searching by him
123  $search_sale = 0;
124  if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
125  $search_sale = DolibarrApiAccess::$user->id;
126  }
127 
128  $sql = "SELECT t.rowid";
129  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
130  $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)
131  }
132  $sql .= " FROM ".MAIN_DB_PREFIX."fichinter as t";
133 
134  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
135  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
136  }
137 
138  $sql .= ' WHERE t.entity IN ('.getEntity('intervention').')';
139  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
140  $sql .= " AND t.fk_soc = sc.fk_soc";
141  }
142  if ($socids) {
143  $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
144  }
145  if ($search_sale > 0) {
146  $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
147  }
148  // Insert sale filter
149  if ($search_sale > 0) {
150  $sql .= " AND sc.fk_user = ".((int) $search_sale);
151  }
152  // Add sql filters
153  if ($sqlfilters) {
154  $errormessage = '';
155  $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
156  if ($errormessage) {
157  throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
158  }
159  }
160 
161  $sql .= $this->db->order($sortfield, $sortorder);
162  if ($limit) {
163  if ($page < 0) {
164  $page = 0;
165  }
166  $offset = $limit * $page;
167 
168  $sql .= $this->db->plimit($limit + 1, $offset);
169  }
170 
171  dol_syslog("API Rest request");
172  $result = $this->db->query($sql);
173 
174  if ($result) {
175  $num = $this->db->num_rows($result);
176  $min = min($num, ($limit <= 0 ? $num : $limit));
177  $i = 0;
178  while ($i < $min) {
179  $obj = $this->db->fetch_object($result);
180  $fichinter_static = new Fichinter($this->db);
181  if ($fichinter_static->fetch($obj->rowid)) {
182  $obj_ret[] = $this->_cleanObjectDatas($fichinter_static);
183  }
184  $i++;
185  }
186  } else {
187  throw new RestException(503, 'Error when retrieve intervention list : '.$this->db->lasterror());
188  }
189  if (!count($obj_ret)) {
190  throw new RestException(404, 'No intervention found');
191  }
192  return $obj_ret;
193  }
194 
201  public function post($request_data = null)
202  {
203  if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
204  throw new RestException(401, "Insuffisant rights");
205  }
206  // Check mandatory fields
207  $result = $this->_validate($request_data);
208  foreach ($request_data as $field => $value) {
209  $this->fichinter->$field = $value;
210  }
211 
212  if ($this->fichinter->create(DolibarrApiAccess::$user) < 0) {
213  throw new RestException(500, "Error creating intervention", array_merge(array($this->fichinter->error), $this->fichinter->errors));
214  }
215 
216  return $this->fichinter->id;
217  }
218 
219 
229  /* TODO
230  public function getLines($id)
231  {
232  if(! DolibarrApiAccess::$user->rights->ficheinter->lire) {
233  throw new RestException(401);
234  }
235 
236  $result = $this->fichinter->fetch($id);
237  if( ! $result ) {
238  throw new RestException(404, 'Intervention not found');
239  }
240 
241  if( ! DolibarrApi::_checkAccessToResource('fichinter',$this->fichinter->id)) {
242  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
243  }
244  $this->fichinter->getLinesArray();
245  $result = array();
246  foreach ($this->fichinter->lines as $line) {
247  array_push($result,$this->_cleanObjectDatas($line));
248  }
249  return $result;
250  }
251  */
252 
263  public function postLine($id, $request_data = null)
264  {
265  if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
266  throw new RestException(401, "Insuffisant rights");
267  }
268  // Check mandatory fields
269  $result = $this->_validateLine($request_data);
270 
271  foreach ($request_data as $field => $value) {
272  $this->fichinter->$field = $value;
273  }
274 
275  if (!$result) {
276  throw new RestException(404, 'Intervention not found');
277  }
278 
279  if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
280  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
281  }
282 
283  $updateRes = $this->fichinter->addLine(
284  DolibarrApiAccess::$user,
285  $id,
286  $this->fichinter->description,
287  $this->fichinter->date,
288  $this->fichinter->duree
289  );
290 
291  if ($updateRes > 0) {
292  return $updateRes;
293  } else {
294  throw new RestException(400, $this->fichinter->error);
295  }
296  }
297 
304  public function delete($id)
305  {
306  if (!DolibarrApiAccess::$user->rights->ficheinter->supprimer) {
307  throw new RestException(401);
308  }
309  $result = $this->fichinter->fetch($id);
310  if (!$result) {
311  throw new RestException(404, 'Intervention not found');
312  }
313 
314  if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
315  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
316  }
317 
318  if (!$this->fichinter->delete(DolibarrApiAccess::$user)) {
319  throw new RestException(500, 'Error when delete intervention : '.$this->fichinter->error);
320  }
321 
322  return array(
323  'success' => array(
324  'code' => 200,
325  'message' => 'Intervention deleted'
326  )
327  );
328  }
329 
345  public function validate($id, $notrigger = 0)
346  {
347  if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
348  throw new RestException(401, "Insuffisant rights");
349  }
350  $result = $this->fichinter->fetch($id);
351  if (!$result) {
352  throw new RestException(404, 'Intervention not found');
353  }
354 
355  if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
356  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
357  }
358 
359  $result = $this->fichinter->setValid(DolibarrApiAccess::$user, $notrigger);
360  if ($result == 0) {
361  throw new RestException(304, 'Error nothing done. May be object is already validated');
362  }
363  if ($result < 0) {
364  throw new RestException(500, 'Error when validating Intervention: '.$this->commande->error);
365  }
366 
367  $this->fichinter->fetchObjectLinked();
368 
369  return $this->_cleanObjectDatas($this->fichinter);
370  }
371 
381  public function closeFichinter($id)
382  {
383  if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
384  throw new RestException(401, "Insuffisant rights");
385  }
386  $result = $this->fichinter->fetch($id);
387  if (!$result) {
388  throw new RestException(404, 'Intervention not found');
389  }
390 
391  if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
392  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
393  }
394 
395  $result = $this->fichinter->setStatut(3);
396 
397  if ($result == 0) {
398  throw new RestException(304, 'Error nothing done. May be object is already closed');
399  }
400  if ($result < 0) {
401  throw new RestException(500, 'Error when closing Intervention: '.$this->fichinter->error);
402  }
403 
404  $this->fichinter->fetchObjectLinked();
405 
406  return $this->_cleanObjectDatas($this->fichinter);
407  }
408 
417  private function _validate($data)
418  {
419  $fichinter = array();
420  foreach (Interventions::$FIELDS as $field) {
421  if (!isset($data[$field])) {
422  throw new RestException(400, "$field field missing");
423  }
424  $fichinter[$field] = $data[$field];
425  }
426  return $fichinter;
427  }
428 
429 
430  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
437  protected function _cleanObjectDatas($object)
438  {
439  // phpcs:enable
440  $object = parent::_cleanObjectDatas($object);
441 
442  unset($object->statuts_short);
443  unset($object->statuts_logo);
444  unset($object->statuts);
445 
446  return $object;
447  }
448 
457  private function _validateLine($data)
458  {
459  $fichinter = array();
460  foreach (Interventions::$FIELDSLINE as $field) {
461  if (!isset($data[$field])) {
462  throw new RestException(400, "$field field missing");
463  }
464  $fichinter[$field] = $data[$field];
465  }
466  return $fichinter;
467  }
468 }
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.
Definition: api.class.php:283
Class to manage interventions.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='')
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.
__construct()
Constructor.
postLine($id, $request_data=null)
Get lines of an intervention.
forgeSQLFromUniversalSearchCriteria($filter, &$error='')
forgeSQLFromUniversalSearchCriteria
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db
API class for accounts.
Definition: inc.php:41