dolibarr 19.0.4
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
19use Luracast\Restler\RestException;
20
21dol_include_once('/knowledgemanagement/class/knowledgerecord.class.php');
22dol_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 ($result < 0) {
113 throw new RestException(503, 'Error when retrieve category list : '.join(',', array_merge(array($categories->error), $categories->errors)));
114 }
115
116 return $result;
117 }
118
137 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters = '', $properties = '')
138 {
139 global $db, $conf;
140
141 $obj_ret = array();
142 $tmpobject = new KnowledgeRecord($this->db);
143
144 if (!DolibarrApiAccess::$user->hasRight('knowledgemanagement', 'knowledgerecord', 'read')) {
145 throw new RestException(401);
146 }
147
148 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
149
150 $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
151
152 // If the internal user must only see his customers, force searching by him
153 $search_sale = 0;
154 if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
155 $search_sale = DolibarrApiAccess::$user->id;
156 }
157
158 $sql = "SELECT t.rowid";
159 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
160 $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)
161 }
162 $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
163
164 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
165 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
166 }
167 if ($category > 0) {
168 $sql .= ", ".$this->db->prefix()."categorie_knowledgemanagement as c";
169 }
170 $sql .= " WHERE 1 = 1";
171
172 // Example of use $mode
173 //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
174 //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
175
176 if ($tmpobject->ismultientitymanaged) {
177 $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
178 }
179 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
180 $sql .= " AND t.fk_soc = sc.fk_soc";
181 }
182 if ($restrictonsocid && $socid) {
183 $sql .= " AND t.fk_soc = ".((int) $socid);
184 }
185 if ($restrictonsocid && $search_sale > 0) {
186 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
187 }
188 // Insert sale filter
189 if ($restrictonsocid && $search_sale > 0) {
190 $sql .= " AND sc.fk_user = ".((int) $search_sale);
191 }
192 // Select products of given category
193 if ($category > 0) {
194 $sql .= " AND c.fk_categorie = ".((int) $category);
195 $sql .= " AND c.fk_knowledgemanagement = t.rowid";
196 }
197 if ($sqlfilters) {
198 $errormessage = '';
199 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
200 if ($errormessage) {
201 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
202 }
203 }
204
205 $sql .= $this->db->order($sortfield, $sortorder);
206 if ($limit) {
207 if ($page < 0) {
208 $page = 0;
209 }
210 $offset = $limit * $page;
211
212 $sql .= $this->db->plimit($limit + 1, $offset);
213 }
214
215 $result = $this->db->query($sql);
216 $i = 0;
217 if ($result) {
218 $num = $this->db->num_rows($result);
219 while ($i < $num) {
220 $obj = $this->db->fetch_object($result);
221 $tmp_object = new KnowledgeRecord($this->db);
222 if ($tmp_object->fetch($obj->rowid)) {
223 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($tmp_object), $properties);
224 }
225 $i++;
226 }
227 } else {
228 throw new RestException(503, 'Error when retrieving knowledgerecord list: '.$this->db->lasterror());
229 }
230
231 return $obj_ret;
232 }
233
244 public function post($request_data = null)
245 {
246 if (!DolibarrApiAccess::$user->hasRight('knowledgemanagement', 'knowledgerecord', 'write')) {
247 throw new RestException(401);
248 }
249
250 // Check mandatory fields
251 $result = $this->_validate($request_data);
252
253 foreach ($request_data as $field => $value) {
254 if ($field === 'caller') {
255 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller
256 $this->knowledgerecord->context['caller'] = $request_data['caller'];
257 continue;
258 }
259
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->hasRight('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 if ($field === 'caller') {
303 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller
304 $this->knowledgerecord->context['caller'] = $request_data['caller'];
305 continue;
306 }
307
308 $this->knowledgerecord->$field = $this->_checkValForAPI($field, $value, $this->knowledgerecord);
309 }
310
311 // Clean data
312 // $this->knowledgerecord->abc = sanitizeVal($this->knowledgerecord->abc, 'alphanohtml');
313
314 if ($this->knowledgerecord->update(DolibarrApiAccess::$user, false) > 0) {
315 return $this->get($id);
316 } else {
317 throw new RestException(500, $this->knowledgerecord->error);
318 }
319 }
320
331 public function delete($id)
332 {
333 if (!DolibarrApiAccess::$user->hasRight('knowledgemanagement', 'knowledgerecord', 'delete')) {
334 throw new RestException(401);
335 }
336 $result = $this->knowledgerecord->fetch($id);
337 if (!$result) {
338 throw new RestException(404, 'KnowledgeRecord not found');
339 }
340
341 if (!DolibarrApi::_checkAccessToResource('knowledgerecord', $this->knowledgerecord->id, 'knowledgemanagement_knowledgerecord')) {
342 throw new RestException(401, 'Access to instance id='.$this->knowledgerecord->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
343 }
344
345 if (!$this->knowledgerecord->delete(DolibarrApiAccess::$user)) {
346 throw new RestException(500, 'Error when deleting KnowledgeRecord : '.$this->knowledgerecord->error);
347 }
348
349 return array(
350 'success' => array(
351 'code' => 200,
352 'message' => 'KnowledgeRecord deleted'
353 )
354 );
355 }
356
357
358 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
365 protected function _cleanObjectDatas($object)
366 {
367 // phpcs:enable
368 $object = parent::_cleanObjectDatas($object);
369
370 unset($object->rowid);
371 unset($object->canvas);
372
373 /*unset($object->name);
374 unset($object->lastname);
375 unset($object->firstname);
376 unset($object->civility_id);
377 unset($object->statut);
378 unset($object->state);
379 unset($object->state_id);
380 unset($object->state_code);
381 unset($object->region);
382 unset($object->region_code);
383 unset($object->country);
384 unset($object->country_id);
385 unset($object->country_code);
386 unset($object->barcode_type);
387 unset($object->barcode_type_code);
388 unset($object->barcode_type_label);
389 unset($object->barcode_type_coder);
390 unset($object->total_ht);
391 unset($object->total_tva);
392 unset($object->total_localtax1);
393 unset($object->total_localtax2);
394 unset($object->total_ttc);
395 unset($object->fk_account);
396 unset($object->comments);
397 unset($object->note);
398 unset($object->mode_reglement_id);
399 unset($object->cond_reglement_id);
400 unset($object->cond_reglement);
401 unset($object->shipping_method_id);
402 unset($object->fk_incoterms);
403 unset($object->label_incoterms);
404 unset($object->location_incoterms);
405 */
406
407 // If object has lines, remove $db property
408 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
409 $nboflines = count($object->lines);
410 for ($i = 0; $i < $nboflines; $i++) {
411 $this->_cleanObjectDatas($object->lines[$i]);
412
413 unset($object->lines[$i]->lines);
414 unset($object->lines[$i]->note);
415 }
416 }
417
418 return $object;
419 }
420
429 private function _validate($data)
430 {
431 $knowledgerecord = array();
432 foreach ($this->knowledgerecord->fields as $field => $propfield) {
433 if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
434 continue; // Not a mandatory field
435 }
436 if (!isset($data[$field])) {
437 throw new RestException(400, "$field field missing");
438 }
439 $knowledgerecord[$field] = $data[$field];
440 }
441 return $knowledgerecord;
442 }
443}
Class to manage categories.
Class for API REST v1.
Definition api.class.php:31
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition api.class.php:85
post($request_data=null)
Create knowledgerecord object.
_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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $category=0, $sqlfilters='', $properties='')
List knowledgerecords.
_cleanObjectDatas($object)
Clean sensible object datas.
Class for KnowledgeRecord.
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.