dolibarr  16.0.5
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  if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
156  throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
157  }
158  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
159  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
160  }
161 
162  $sql .= $this->db->order($sortfield, $sortorder);
163  if ($limit) {
164  if ($page < 0) {
165  $page = 0;
166  }
167  $offset = $limit * $page;
168 
169  $sql .= $this->db->plimit($limit + 1, $offset);
170  }
171 
172  dol_syslog("API Rest request");
173  $result = $this->db->query($sql);
174 
175  if ($result) {
176  $num = $this->db->num_rows($result);
177  $min = min($num, ($limit <= 0 ? $num : $limit));
178  $i = 0;
179  while ($i < $min) {
180  $obj = $this->db->fetch_object($result);
181  $fichinter_static = new Fichinter($this->db);
182  if ($fichinter_static->fetch($obj->rowid)) {
183  $obj_ret[] = $this->_cleanObjectDatas($fichinter_static);
184  }
185  $i++;
186  }
187  } else {
188  throw new RestException(503, 'Error when retrieve intervention list : '.$this->db->lasterror());
189  }
190  if (!count($obj_ret)) {
191  throw new RestException(404, 'No intervention found');
192  }
193  return $obj_ret;
194  }
195 
202  public function post($request_data = null)
203  {
204  if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
205  throw new RestException(401, "Insuffisant rights");
206  }
207  // Check mandatory fields
208  $result = $this->_validate($request_data);
209  foreach ($request_data as $field => $value) {
210  $this->fichinter->$field = $value;
211  }
212 
213  if ($this->fichinter->create(DolibarrApiAccess::$user) < 0) {
214  throw new RestException(500, "Error creating intervention", array_merge(array($this->fichinter->error), $this->fichinter->errors));
215  }
216 
217  return $this->fichinter->id;
218  }
219 
220 
230  /* TODO
231  public function getLines($id)
232  {
233  if(! DolibarrApiAccess::$user->rights->ficheinter->lire) {
234  throw new RestException(401);
235  }
236 
237  $result = $this->fichinter->fetch($id);
238  if( ! $result ) {
239  throw new RestException(404, 'Intervention not found');
240  }
241 
242  if( ! DolibarrApi::_checkAccessToResource('fichinter',$this->fichinter->id)) {
243  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
244  }
245  $this->fichinter->getLinesArray();
246  $result = array();
247  foreach ($this->fichinter->lines as $line) {
248  array_push($result,$this->_cleanObjectDatas($line));
249  }
250  return $result;
251  }
252  */
253 
264  public function postLine($id, $request_data = null)
265  {
266  if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
267  throw new RestException(401, "Insuffisant rights");
268  }
269  // Check mandatory fields
270  $result = $this->_validateLine($request_data);
271 
272  foreach ($request_data as $field => $value) {
273  $this->fichinter->$field = $value;
274  }
275 
276  if (!$result) {
277  throw new RestException(404, 'Intervention not found');
278  }
279 
280  if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
281  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
282  }
283 
284  $updateRes = $this->fichinter->addLine(
285  DolibarrApiAccess::$user,
286  $id,
287  $this->fichinter->description,
288  $this->fichinter->date,
289  $this->fichinter->duree
290  );
291 
292  if ($updateRes > 0) {
293  return $updateRes;
294  } else {
295  throw new RestException(400, $this->fichinter->error);
296  }
297  }
298 
305  public function delete($id)
306  {
307  if (!DolibarrApiAccess::$user->rights->ficheinter->supprimer) {
308  throw new RestException(401);
309  }
310  $result = $this->fichinter->fetch($id);
311  if (!$result) {
312  throw new RestException(404, 'Intervention not found');
313  }
314 
315  if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
316  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
317  }
318 
319  if (!$this->fichinter->delete(DolibarrApiAccess::$user)) {
320  throw new RestException(500, 'Error when delete intervention : '.$this->fichinter->error);
321  }
322 
323  return array(
324  'success' => array(
325  'code' => 200,
326  'message' => 'Intervention deleted'
327  )
328  );
329  }
330 
346  public function validate($id, $notrigger = 0)
347  {
348  if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
349  throw new RestException(401, "Insuffisant rights");
350  }
351  $result = $this->fichinter->fetch($id);
352  if (!$result) {
353  throw new RestException(404, 'Intervention not found');
354  }
355 
356  if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
357  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
358  }
359 
360  $result = $this->fichinter->setValid(DolibarrApiAccess::$user, $notrigger);
361  if ($result == 0) {
362  throw new RestException(304, 'Error nothing done. May be object is already validated');
363  }
364  if ($result < 0) {
365  throw new RestException(500, 'Error when validating Intervention: '.$this->commande->error);
366  }
367 
368  $this->fichinter->fetchObjectLinked();
369 
370  return $this->_cleanObjectDatas($this->fichinter);
371  }
372 
382  public function closeFichinter($id)
383  {
384  if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
385  throw new RestException(401, "Insuffisant rights");
386  }
387  $result = $this->fichinter->fetch($id);
388  if (!$result) {
389  throw new RestException(404, 'Intervention not found');
390  }
391 
392  if (!DolibarrApi::_checkAccessToResource('fichinter', $this->fichinter->id)) {
393  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
394  }
395 
396  $result = $this->fichinter->setStatut(3);
397 
398  if ($result == 0) {
399  throw new RestException(304, 'Error nothing done. May be object is already closed');
400  }
401  if ($result < 0) {
402  throw new RestException(500, 'Error when closing Intervention: '.$this->fichinter->error);
403  }
404 
405  $this->fichinter->fetchObjectLinked();
406 
407  return $this->_cleanObjectDatas($this->fichinter);
408  }
409 
418  private function _validate($data)
419  {
420  $fichinter = array();
421  foreach (Interventions::$FIELDS as $field) {
422  if (!isset($data[$field])) {
423  throw new RestException(400, "$field field missing");
424  }
425  $fichinter[$field] = $data[$field];
426  }
427  return $fichinter;
428  }
429 
430 
431  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
438  protected function _cleanObjectDatas($object)
439  {
440  // phpcs:enable
441  $object = parent::_cleanObjectDatas($object);
442 
443  unset($object->statuts_short);
444  unset($object->statuts_logo);
445  unset($object->statuts);
446 
447  return $object;
448  }
449 
458  private function _validateLine($data)
459  {
460  $fichinter = array();
461  foreach (Interventions::$FIELDSLINE as $field) {
462  if (!isset($data[$field])) {
463  throw new RestException(400, "$field field missing");
464  }
465  $fichinter[$field] = $data[$field];
466  }
467  return $fichinter;
468  }
469 }
db
$conf db
API class for accounts.
Definition: inc.php:41
Interventions\_cleanObjectDatas
_cleanObjectDatas($object)
Clean sensible object datas.
Definition: api_interventions.class.php:438
Interventions
Definition: api_interventions.class.php:29
DolibarrApi\_checkAccessToResource
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
Interventions\_validate
_validate($data)
Validate fields before create or update object.
Definition: api_interventions.class.php:418
DolibarrApi
Class for API REST v1.
Definition: api.class.php:30
Interventions\index
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='')
List of interventions.
Definition: api_interventions.class.php:109
Interventions\postLine
postLine($id, $request_data=null)
Get lines of an intervention.
Definition: api_interventions.class.php:264
Interventions\_validateLine
_validateLine($data)
Validate fields before create or update object.
Definition: api_interventions.class.php:458
Interventions\__construct
__construct()
Constructor.
Definition: api_interventions.class.php:58
DolibarrApi\_checkFilters
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:310
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
Fichinter
Class to manage interventions.
Definition: fichinter.class.php:37
Interventions\post
post($request_data=null)
Create intervention object.
Definition: api_interventions.class.php:202
Interventions\validate
validate($id, $notrigger=0)
Validate an intervention.
Definition: api_interventions.class.php:346
Interventions\closeFichinter
closeFichinter($id)
Close an intervention.
Definition: api_interventions.class.php:382