dolibarr  x.y.z
api_knowledgemanagement.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) 2021 SuperAdmin <test@dolibarr.com>
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 dol_include_once('/knowledgemanagement/class/knowledgerecord.class.php');
22 dol_include_once('/categories/class/categorie.class.php');
23 
24 
25 
39 {
43  public $knowledgerecord;
44 
51  public function __construct()
52  {
53  global $db, $conf;
54  $this->db = $db;
55  $this->knowledgerecord = new KnowledgeRecord($this->db);
56  }
57 
71  public function get($id)
72  {
73  if (!DolibarrApiAccess::$user->rights->knowledgemanagement->knowledgerecord->read) {
74  throw new RestException(401);
75  }
76 
77  $result = $this->knowledgerecord->fetch($id);
78  if (!$result) {
79  throw new RestException(404, 'KnowledgeRecord not found');
80  }
81 
82  if (!DolibarrApi::_checkAccessToResource('knowledgerecord', $this->knowledgerecord->id, 'knowledgemanagement_knowledgerecord')) {
83  throw new RestException(401, 'Access to instance id='.$this->knowledgerecord->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
84  }
85 
86  return $this->_cleanObjectDatas($this->knowledgerecord);
87  }
88 
102  public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
103  {
104  if (!DolibarrApiAccess::$user->rights->categorie->lire) {
105  throw new RestException(401);
106  }
107 
108  $categories = new Categorie($this->db);
109 
110  $result = $categories->getListForItem($id, 'knowledgemanagement', $sortfield, $sortorder, $limit, $page);
111 
112  if (empty($result)) {
113  throw new RestException(404, 'No category found');
114  }
115 
116  if ($result < 0) {
117  throw new RestException(503, 'Error when retrieve category list : '.join(',', array_merge(array($categories->error), $categories->errors)));
118  }
119 
120  return $result;
121  }
122 
140  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters = '')
141  {
142  global $db, $conf;
143 
144  $obj_ret = array();
145  $tmpobject = new KnowledgeRecord($this->db);
146 
147  if (!DolibarrApiAccess::$user->rights->knowledgemanagement->knowledgerecord->read) {
148  throw new RestException(401);
149  }
150 
151  $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
152 
153  $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
154 
155  // If the internal user must only see his customers, force searching by him
156  $search_sale = 0;
157  if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
158  $search_sale = DolibarrApiAccess::$user->id;
159  }
160 
161  $sql = "SELECT t.rowid";
162  if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
163  $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)
164  }
165  $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." as t";
166 
167  if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
168  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
169  }
170  if ($category > 0) {
171  $sql .= ", ".$this->db->prefix()."categorie_knowledgemanagement as c";
172  }
173  $sql .= " WHERE 1 = 1";
174 
175  // Example of use $mode
176  //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
177  //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
178 
179  if ($tmpobject->ismultientitymanaged) {
180  $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
181  }
182  if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
183  $sql .= " AND t.fk_soc = sc.fk_soc";
184  }
185  if ($restrictonsocid && $socid) {
186  $sql .= " AND t.fk_soc = ".((int) $socid);
187  }
188  if ($restrictonsocid && $search_sale > 0) {
189  $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
190  }
191  // Insert sale filter
192  if ($restrictonsocid && $search_sale > 0) {
193  $sql .= " AND sc.fk_user = ".((int) $search_sale);
194  }
195  // Select products of given category
196  if ($category > 0) {
197  $sql .= " AND c.fk_categorie = ".((int) $category);
198  $sql .= " AND c.fk_knowledgemanagement = t.rowid";
199  }
200  if ($sqlfilters) {
201  $errormessage = '';
202  if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
203  throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
204  }
205  $regexstring = '\‍(([^:\'\‍(\‍)]+:[^:\'\‍(\‍)]+:[^\‍(\‍)]+)\‍)';
206  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
207  }
208 
209  $sql .= $this->db->order($sortfield, $sortorder);
210  if ($limit) {
211  if ($page < 0) {
212  $page = 0;
213  }
214  $offset = $limit * $page;
215 
216  $sql .= $this->db->plimit($limit + 1, $offset);
217  }
218 
219  $result = $this->db->query($sql);
220  $i = 0;
221  if ($result) {
222  $num = $this->db->num_rows($result);
223  while ($i < $num) {
224  $obj = $this->db->fetch_object($result);
225  $tmp_object = new KnowledgeRecord($this->db);
226  if ($tmp_object->fetch($obj->rowid)) {
227  $obj_ret[] = $this->_cleanObjectDatas($tmp_object);
228  }
229  $i++;
230  }
231  } else {
232  throw new RestException(503, 'Error when retrieving knowledgerecord list: '.$this->db->lasterror());
233  }
234  if (!count($obj_ret)) {
235  throw new RestException(404, 'No knowledgerecord found');
236  }
237  return $obj_ret;
238  }
239 
250  public function post($request_data = null)
251  {
252  if (!DolibarrApiAccess::$user->rights->knowledgemanagement->knowledgerecord->write) {
253  throw new RestException(401);
254  }
255 
256  // Check mandatory fields
257  $result = $this->_validate($request_data);
258 
259  foreach ($request_data as $field => $value) {
260  $this->knowledgerecord->$field = $this->_checkValForAPI($field, $value, $this->knowledgerecord);
261  }
262 
263  // Clean data
264  // $this->knowledgerecord->abc = sanitizeVal($this->knowledgerecord->abc, 'alphanohtml');
265 
266  if ($this->knowledgerecord->create(DolibarrApiAccess::$user)<0) {
267  throw new RestException(500, "Error creating KnowledgeRecord", array_merge(array($this->knowledgerecord->error), $this->knowledgerecord->errors));
268  }
269  return $this->knowledgerecord->id;
270  }
271 
283  public function put($id, $request_data = null)
284  {
285  if (!DolibarrApiAccess::$user->rights->knowledgemanagement->knowledgerecord->write) {
286  throw new RestException(401);
287  }
288 
289  $result = $this->knowledgerecord->fetch($id);
290  if (!$result) {
291  throw new RestException(404, 'KnowledgeRecord not found');
292  }
293 
294  if (!DolibarrApi::_checkAccessToResource('knowledgerecord', $this->knowledgerecord->id, 'knowledgemanagement_knowledgerecord')) {
295  throw new RestException(401, 'Access to instance id='.$this->knowledgerecord->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
296  }
297 
298  foreach ($request_data as $field => $value) {
299  if ($field == 'id') {
300  continue;
301  }
302  $this->knowledgerecord->$field = $this->_checkValForAPI($field, $value, $this->knowledgerecord);
303  }
304 
305  // Clean data
306  // $this->knowledgerecord->abc = sanitizeVal($this->knowledgerecord->abc, 'alphanohtml');
307 
308  if ($this->knowledgerecord->update(DolibarrApiAccess::$user, false) > 0) {
309  return $this->get($id);
310  } else {
311  throw new RestException(500, $this->knowledgerecord->error);
312  }
313  }
314 
325  public function delete($id)
326  {
327  if (!DolibarrApiAccess::$user->rights->knowledgemanagement->knowledgerecord->delete) {
328  throw new RestException(401);
329  }
330  $result = $this->knowledgerecord->fetch($id);
331  if (!$result) {
332  throw new RestException(404, 'KnowledgeRecord not found');
333  }
334 
335  if (!DolibarrApi::_checkAccessToResource('knowledgerecord', $this->knowledgerecord->id, 'knowledgemanagement_knowledgerecord')) {
336  throw new RestException(401, 'Access to instance id='.$this->knowledgerecord->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
337  }
338 
339  if (!$this->knowledgerecord->delete(DolibarrApiAccess::$user)) {
340  throw new RestException(500, 'Error when deleting KnowledgeRecord : '.$this->knowledgerecord->error);
341  }
342 
343  return array(
344  'success' => array(
345  'code' => 200,
346  'message' => 'KnowledgeRecord deleted'
347  )
348  );
349  }
350 
351 
352  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
359  protected function _cleanObjectDatas($object)
360  {
361  // phpcs:enable
362  $object = parent::_cleanObjectDatas($object);
363 
364  unset($object->rowid);
365  unset($object->canvas);
366 
367  /*unset($object->name);
368  unset($object->lastname);
369  unset($object->firstname);
370  unset($object->civility_id);
371  unset($object->statut);
372  unset($object->state);
373  unset($object->state_id);
374  unset($object->state_code);
375  unset($object->region);
376  unset($object->region_code);
377  unset($object->country);
378  unset($object->country_id);
379  unset($object->country_code);
380  unset($object->barcode_type);
381  unset($object->barcode_type_code);
382  unset($object->barcode_type_label);
383  unset($object->barcode_type_coder);
384  unset($object->total_ht);
385  unset($object->total_tva);
386  unset($object->total_localtax1);
387  unset($object->total_localtax2);
388  unset($object->total_ttc);
389  unset($object->fk_account);
390  unset($object->comments);
391  unset($object->note);
392  unset($object->mode_reglement_id);
393  unset($object->cond_reglement_id);
394  unset($object->cond_reglement);
395  unset($object->shipping_method_id);
396  unset($object->fk_incoterms);
397  unset($object->label_incoterms);
398  unset($object->location_incoterms);
399  */
400 
401  // If object has lines, remove $db property
402  if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
403  $nboflines = count($object->lines);
404  for ($i = 0; $i < $nboflines; $i++) {
405  $this->_cleanObjectDatas($object->lines[$i]);
406 
407  unset($object->lines[$i]->lines);
408  unset($object->lines[$i]->note);
409  }
410  }
411 
412  return $object;
413  }
414 
423  private function _validate($data)
424  {
425  $knowledgerecord = array();
426  foreach ($this->knowledgerecord->fields as $field => $propfield) {
427  if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
428  continue; // Not a mandatory field
429  }
430  if (!isset($data[$field])) {
431  throw new RestException(400, "$field field missing");
432  }
433  $knowledgerecord[$field] = $data[$field];
434  }
435  return $knowledgerecord;
436  }
437 }
Class to manage categories.
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
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:310
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition: api.class.php:86
post($request_data=null)
Create knowledgerecord object.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $category=0, $sqlfilters='')
List knowledgerecords.
_validate($data)
Validate fields before create or update object.
getCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get categories for a knowledgerecord object.
put($id, $request_data=null)
Update knowledgerecord.
_cleanObjectDatas($object)
Clean sensible object datas.
Class for KnowledgeRecord.
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
$conf db
API class for accounts.
Definition: inc.php:41