dolibarr  18.0.6
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->hasRight('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->hasRight('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 LEFT JOIN ".MAIN_DB_PREFIX.$tmpobject->table_element."_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
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  $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
203  if ($errormessage) {
204  throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
205  }
206  }
207 
208  $sql .= $this->db->order($sortfield, $sortorder);
209  if ($limit) {
210  if ($page < 0) {
211  $page = 0;
212  }
213  $offset = $limit * $page;
214 
215  $sql .= $this->db->plimit($limit + 1, $offset);
216  }
217 
218  $result = $this->db->query($sql);
219  $i = 0;
220  if ($result) {
221  $num = $this->db->num_rows($result);
222  while ($i < $num) {
223  $obj = $this->db->fetch_object($result);
224  $tmp_object = new KnowledgeRecord($this->db);
225  if ($tmp_object->fetch($obj->rowid)) {
226  $obj_ret[] = $this->_cleanObjectDatas($tmp_object);
227  }
228  $i++;
229  }
230  } else {
231  throw new RestException(503, 'Error when retrieving knowledgerecord list: '.$this->db->lasterror());
232  }
233  if (!count($obj_ret)) {
234  throw new RestException(404, 'No knowledgerecord found');
235  }
236  return $obj_ret;
237  }
238 
249  public function post($request_data = null)
250  {
251  if (!DolibarrApiAccess::$user->hasRight('knowledgemanagement', 'knowledgerecord', 'write')) {
252  throw new RestException(401);
253  }
254 
255  // Check mandatory fields
256  $result = $this->_validate($request_data);
257 
258  foreach ($request_data as $field => $value) {
259  $this->knowledgerecord->$field = $this->_checkValForAPI($field, $value, $this->knowledgerecord);
260  }
261 
262  // Clean data
263  // $this->knowledgerecord->abc = sanitizeVal($this->knowledgerecord->abc, 'alphanohtml');
264 
265  if ($this->knowledgerecord->create(DolibarrApiAccess::$user)<0) {
266  throw new RestException(500, "Error creating KnowledgeRecord", array_merge(array($this->knowledgerecord->error), $this->knowledgerecord->errors));
267  }
268  return $this->knowledgerecord->id;
269  }
270 
282  public function put($id, $request_data = null)
283  {
284  if (!DolibarrApiAccess::$user->hasRight('knowledgemanagement', 'knowledgerecord', 'write')) {
285  throw new RestException(401);
286  }
287 
288  $result = $this->knowledgerecord->fetch($id);
289  if (!$result) {
290  throw new RestException(404, 'KnowledgeRecord not found');
291  }
292 
293  if (!DolibarrApi::_checkAccessToResource('knowledgerecord', $this->knowledgerecord->id, 'knowledgemanagement_knowledgerecord')) {
294  throw new RestException(401, 'Access to instance id='.$this->knowledgerecord->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
295  }
296 
297  foreach ($request_data as $field => $value) {
298  if ($field == 'id') {
299  continue;
300  }
301  $this->knowledgerecord->$field = $this->_checkValForAPI($field, $value, $this->knowledgerecord);
302  }
303 
304  // Clean data
305  // $this->knowledgerecord->abc = sanitizeVal($this->knowledgerecord->abc, 'alphanohtml');
306 
307  if ($this->knowledgerecord->update(DolibarrApiAccess::$user, false) > 0) {
308  return $this->get($id);
309  } else {
310  throw new RestException(500, $this->knowledgerecord->error);
311  }
312  }
313 
324  public function delete($id)
325  {
326  if (!DolibarrApiAccess::$user->hasRight('knowledgemanagement', 'knowledgerecord', 'delete')) {
327  throw new RestException(401);
328  }
329  $result = $this->knowledgerecord->fetch($id);
330  if (!$result) {
331  throw new RestException(404, 'KnowledgeRecord not found');
332  }
333 
334  if (!DolibarrApi::_checkAccessToResource('knowledgerecord', $this->knowledgerecord->id, 'knowledgemanagement_knowledgerecord')) {
335  throw new RestException(401, 'Access to instance id='.$this->knowledgerecord->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
336  }
337 
338  if (!$this->knowledgerecord->delete(DolibarrApiAccess::$user)) {
339  throw new RestException(500, 'Error when deleting KnowledgeRecord : '.$this->knowledgerecord->error);
340  }
341 
342  return array(
343  'success' => array(
344  'code' => 200,
345  'message' => 'KnowledgeRecord deleted'
346  )
347  );
348  }
349 
350 
351  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
358  protected function _cleanObjectDatas($object)
359  {
360  // phpcs:enable
361  $object = parent::_cleanObjectDatas($object);
362 
363  unset($object->rowid);
364  unset($object->canvas);
365 
366  /*unset($object->name);
367  unset($object->lastname);
368  unset($object->firstname);
369  unset($object->civility_id);
370  unset($object->statut);
371  unset($object->state);
372  unset($object->state_id);
373  unset($object->state_code);
374  unset($object->region);
375  unset($object->region_code);
376  unset($object->country);
377  unset($object->country_id);
378  unset($object->country_code);
379  unset($object->barcode_type);
380  unset($object->barcode_type_code);
381  unset($object->barcode_type_label);
382  unset($object->barcode_type_coder);
383  unset($object->total_ht);
384  unset($object->total_tva);
385  unset($object->total_localtax1);
386  unset($object->total_localtax2);
387  unset($object->total_ttc);
388  unset($object->fk_account);
389  unset($object->comments);
390  unset($object->note);
391  unset($object->mode_reglement_id);
392  unset($object->cond_reglement_id);
393  unset($object->cond_reglement);
394  unset($object->shipping_method_id);
395  unset($object->fk_incoterms);
396  unset($object->label_incoterms);
397  unset($object->location_incoterms);
398  */
399 
400  // If object has lines, remove $db property
401  if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
402  $nboflines = count($object->lines);
403  for ($i = 0; $i < $nboflines; $i++) {
404  $this->_cleanObjectDatas($object->lines[$i]);
405 
406  unset($object->lines[$i]->lines);
407  unset($object->lines[$i]->note);
408  }
409  }
410 
411  return $object;
412  }
413 
422  private function _validate($data)
423  {
424  $knowledgerecord = array();
425  foreach ($this->knowledgerecord->fields as $field => $propfield) {
426  if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
427  continue; // Not a mandatory field
428  }
429  if (!isset($data[$field])) {
430  throw new RestException(400, "$field field missing");
431  }
432  $knowledgerecord[$field] = $data[$field];
433  }
434  return $knowledgerecord;
435  }
436 }
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:282
_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(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.