dolibarr  16.0.5
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 : '.array_merge(array($categories->error), $categories->errors));
118  }
119 
120  return $result;
121  }
122 
139  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
140  {
141  global $db, $conf;
142 
143  $obj_ret = array();
144  $tmpobject = new KnowledgeRecord($this->db);
145 
146  if (!DolibarrApiAccess::$user->rights->knowledgemanagement->knowledgerecord->read) {
147  throw new RestException(401);
148  }
149 
150  $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
151 
152  $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
153 
154  // If the internal user must only see his customers, force searching by him
155  $search_sale = 0;
156  if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
157  $search_sale = DolibarrApiAccess::$user->id;
158  }
159 
160  $sql = "SELECT t.rowid";
161  if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
162  $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)
163  }
164  $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." as t";
165 
166  if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
167  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
168  }
169  $sql .= " WHERE 1 = 1";
170 
171  // Example of use $mode
172  //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
173  //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
174 
175  if ($tmpobject->ismultientitymanaged) {
176  $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
177  }
178  if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
179  $sql .= " AND t.fk_soc = sc.fk_soc";
180  }
181  if ($restrictonsocid && $socid) {
182  $sql .= " AND t.fk_soc = ".((int) $socid);
183  }
184  if ($restrictonsocid && $search_sale > 0) {
185  $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
186  }
187  // Insert sale filter
188  if ($restrictonsocid && $search_sale > 0) {
189  $sql .= " AND sc.fk_user = ".((int) $search_sale);
190  }
191  if ($sqlfilters) {
192  $errormessage = '';
193  if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
194  throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
195  }
196  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
197  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
198  }
199 
200  $sql .= $this->db->order($sortfield, $sortorder);
201  if ($limit) {
202  if ($page < 0) {
203  $page = 0;
204  }
205  $offset = $limit * $page;
206 
207  $sql .= $this->db->plimit($limit + 1, $offset);
208  }
209 
210  $result = $this->db->query($sql);
211  $i = 0;
212  if ($result) {
213  $num = $this->db->num_rows($result);
214  while ($i < $num) {
215  $obj = $this->db->fetch_object($result);
216  $tmp_object = new KnowledgeRecord($this->db);
217  if ($tmp_object->fetch($obj->rowid)) {
218  $obj_ret[] = $this->_cleanObjectDatas($tmp_object);
219  }
220  $i++;
221  }
222  } else {
223  throw new RestException(503, 'Error when retrieving knowledgerecord list: '.$this->db->lasterror());
224  }
225  if (!count($obj_ret)) {
226  throw new RestException(404, 'No knowledgerecord found');
227  }
228  return $obj_ret;
229  }
230 
241  public function post($request_data = null)
242  {
243  if (!DolibarrApiAccess::$user->rights->knowledgemanagement->knowledgerecord->write) {
244  throw new RestException(401);
245  }
246 
247  // Check mandatory fields
248  $result = $this->_validate($request_data);
249 
250  foreach ($request_data as $field => $value) {
251  $this->knowledgerecord->$field = $this->_checkValForAPI($field, $value, $this->knowledgerecord);
252  }
253 
254  // Clean data
255  // $this->knowledgerecord->abc = sanitizeVal($this->knowledgerecord->abc, 'alphanohtml');
256 
257  if ($this->knowledgerecord->create(DolibarrApiAccess::$user)<0) {
258  throw new RestException(500, "Error creating KnowledgeRecord", array_merge(array($this->knowledgerecord->error), $this->knowledgerecord->errors));
259  }
260  return $this->knowledgerecord->id;
261  }
262 
274  public function put($id, $request_data = null)
275  {
276  if (!DolibarrApiAccess::$user->rights->knowledgemanagement->knowledgerecord->write) {
277  throw new RestException(401);
278  }
279 
280  $result = $this->knowledgerecord->fetch($id);
281  if (!$result) {
282  throw new RestException(404, 'KnowledgeRecord not found');
283  }
284 
285  if (!DolibarrApi::_checkAccessToResource('knowledgerecord', $this->knowledgerecord->id, 'knowledgemanagement_knowledgerecord')) {
286  throw new RestException(401, 'Access to instance id='.$this->knowledgerecord->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
287  }
288 
289  foreach ($request_data as $field => $value) {
290  if ($field == 'id') {
291  continue;
292  }
293  $this->knowledgerecord->$field = $this->_checkValForAPI($field, $value, $this->knowledgerecord);
294  }
295 
296  // Clean data
297  // $this->knowledgerecord->abc = sanitizeVal($this->knowledgerecord->abc, 'alphanohtml');
298 
299  if ($this->knowledgerecord->update(DolibarrApiAccess::$user, false) > 0) {
300  return $this->get($id);
301  } else {
302  throw new RestException(500, $this->knowledgerecord->error);
303  }
304  }
305 
316  public function delete($id)
317  {
318  if (!DolibarrApiAccess::$user->rights->knowledgemanagement->knowledgerecord->delete) {
319  throw new RestException(401);
320  }
321  $result = $this->knowledgerecord->fetch($id);
322  if (!$result) {
323  throw new RestException(404, 'KnowledgeRecord not found');
324  }
325 
326  if (!DolibarrApi::_checkAccessToResource('knowledgerecord', $this->knowledgerecord->id, 'knowledgemanagement_knowledgerecord')) {
327  throw new RestException(401, 'Access to instance id='.$this->knowledgerecord->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
328  }
329 
330  if (!$this->knowledgerecord->delete(DolibarrApiAccess::$user)) {
331  throw new RestException(500, 'Error when deleting KnowledgeRecord : '.$this->knowledgerecord->error);
332  }
333 
334  return array(
335  'success' => array(
336  'code' => 200,
337  'message' => 'KnowledgeRecord deleted'
338  )
339  );
340  }
341 
342 
343  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
350  protected function _cleanObjectDatas($object)
351  {
352  // phpcs:enable
353  $object = parent::_cleanObjectDatas($object);
354 
355  unset($object->rowid);
356  unset($object->canvas);
357 
358  /*unset($object->name);
359  unset($object->lastname);
360  unset($object->firstname);
361  unset($object->civility_id);
362  unset($object->statut);
363  unset($object->state);
364  unset($object->state_id);
365  unset($object->state_code);
366  unset($object->region);
367  unset($object->region_code);
368  unset($object->country);
369  unset($object->country_id);
370  unset($object->country_code);
371  unset($object->barcode_type);
372  unset($object->barcode_type_code);
373  unset($object->barcode_type_label);
374  unset($object->barcode_type_coder);
375  unset($object->total_ht);
376  unset($object->total_tva);
377  unset($object->total_localtax1);
378  unset($object->total_localtax2);
379  unset($object->total_ttc);
380  unset($object->fk_account);
381  unset($object->comments);
382  unset($object->note);
383  unset($object->mode_reglement_id);
384  unset($object->cond_reglement_id);
385  unset($object->cond_reglement);
386  unset($object->shipping_method_id);
387  unset($object->fk_incoterms);
388  unset($object->label_incoterms);
389  unset($object->location_incoterms);
390  */
391 
392  // If object has lines, remove $db property
393  if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
394  $nboflines = count($object->lines);
395  for ($i = 0; $i < $nboflines; $i++) {
396  $this->_cleanObjectDatas($object->lines[$i]);
397 
398  unset($object->lines[$i]->lines);
399  unset($object->lines[$i]->note);
400  }
401  }
402 
403  return $object;
404  }
405 
414  private function _validate($data)
415  {
416  $knowledgerecord = array();
417  foreach ($this->knowledgerecord->fields as $field => $propfield) {
418  if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
419  continue; // Not a mandatory field
420  }
421  if (!isset($data[$field])) {
422  throw new RestException(400, "$field field missing");
423  }
424  $knowledgerecord[$field] = $data[$field];
425  }
426  return $knowledgerecord;
427  }
428 }
db
$conf db
API class for accounts.
Definition: inc.php:41
KnowledgeManagement\__construct
__construct()
Constructor.
Definition: api_knowledgemanagement.class.php:51
KnowledgeManagement\index
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
List knowledgerecords.
Definition: api_knowledgemanagement.class.php:139
dol_include_once
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
Definition: functions.lib.php:1033
KnowledgeRecord
Class for KnowledgeRecord.
Definition: knowledgerecord.class.php:32
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
Categorie
Class to manage categories.
Definition: categorie.class.php:47
DolibarrApi
Class for API REST v1.
Definition: api.class.php:30
KnowledgeManagement\put
put($id, $request_data=null)
Update knowledgerecord.
Definition: api_knowledgemanagement.class.php:274
KnowledgeManagement\getCategories
getCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get categories for a knowledgerecord object.
Definition: api_knowledgemanagement.class.php:102
KnowledgeManagement\_cleanObjectDatas
_cleanObjectDatas($object)
Clean sensible object datas.
Definition: api_knowledgemanagement.class.php:350
KnowledgeManagement\post
post($request_data=null)
Create knowledgerecord object.
Definition: api_knowledgemanagement.class.php:241
DolibarrApi\_checkFilters
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:310
KnowledgeManagement\_validate
_validate($data)
Validate fields before create or update object.
Definition: api_knowledgemanagement.class.php:414
KnowledgeManagement
Definition: api_knowledgemanagement.class.php:38
DolibarrApi\_checkValForAPI
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition: api.class.php:86