dolibarr 21.0.0-beta
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(403);
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(403, '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->hasRight('categorie', 'lire')) {
105 throw new RestException(403);
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 : '.implode(',', array_merge(array($categories->error), $categories->errors)));
114 }
115
116 return $result;
117 }
118
138 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
139 {
140 $obj_ret = array();
141 $tmpobject = new KnowledgeRecord($this->db);
142
143 if (!DolibarrApiAccess::$user->hasRight('knowledgemanagement', 'knowledgerecord', 'read')) {
144 throw new RestException(403);
145 }
146
147 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : 0;
148
149 $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
150
151 // If the internal user must only see his customers, force searching by him
152 $search_sale = 0;
153 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socid) {
154 $search_sale = DolibarrApiAccess::$user->id;
155 }
156
157 $sql = "SELECT t.rowid";
158 $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." AS t";
159 $sql .= " 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
160 if ($category > 0) {
161 $sql .= ", ".$this->db->prefix()."categorie_knowledgemanagement as c";
162 }
163 $sql .= " WHERE 1 = 1";
164 if ($tmpobject->ismultientitymanaged) {
165 $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
166 }
167 if ($restrictonsocid && $socid) {
168 $sql .= " AND t.fk_soc = ".((int) $socid);
169 }
170 // Search on sale representative
171 if ($search_sale && $search_sale != '-1') {
172 if ($search_sale == -2) {
173 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
174 } elseif ($search_sale > 0) {
175 $sql .= " AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).")";
176 }
177 }
178 // Select products of given category
179 if ($category > 0) {
180 $sql .= " AND c.fk_categorie = ".((int) $category);
181 $sql .= " AND c.fk_knowledgemanagement = t.rowid";
182 }
183 if ($sqlfilters) {
184 $errormessage = '';
185 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
186 if ($errormessage) {
187 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
188 }
189 }
190
191 //this query will return total orders with the filters given
192 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
193
194 $sql .= $this->db->order($sortfield, $sortorder);
195 if ($limit) {
196 if ($page < 0) {
197 $page = 0;
198 }
199 $offset = $limit * $page;
200
201 $sql .= $this->db->plimit($limit + 1, $offset);
202 }
203
204 $result = $this->db->query($sql);
205 $i = 0;
206 if ($result) {
207 $num = $this->db->num_rows($result);
208 while ($i < $num) {
209 $obj = $this->db->fetch_object($result);
210 $tmp_object = new KnowledgeRecord($this->db);
211 if ($tmp_object->fetch($obj->rowid)) {
212 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($tmp_object), $properties);
213 }
214 $i++;
215 }
216 } else {
217 throw new RestException(503, 'Error when retrieving knowledgerecord list: '.$this->db->lasterror());
218 }
219
220 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
221 if ($pagination_data) {
222 $totalsResult = $this->db->query($sqlTotals);
223 $total = $this->db->fetch_object($totalsResult)->total;
224
225 $tmp = $obj_ret;
226 $obj_ret = [];
227
228 $obj_ret['data'] = $tmp;
229 $obj_ret['pagination'] = [
230 'total' => (int) $total,
231 'page' => $page, //count starts from 0
232 'page_count' => ceil((int) $total / $limit),
233 'limit' => $limit
234 ];
235 }
236
237 return $obj_ret;
238 }
239
250 public function post($request_data = null)
251 {
252 if (!DolibarrApiAccess::$user->hasRight('knowledgemanagement', 'knowledgerecord', 'write')) {
253 throw new RestException(403);
254 }
255
256 // Check mandatory fields
257 $result = $this->_validate($request_data);
258
259 foreach ($request_data as $field => $value) {
260 if ($field === 'caller') {
261 // 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 with the caller
262 $this->knowledgerecord->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
263 continue;
264 }
265
266 $this->knowledgerecord->$field = $this->_checkValForAPI($field, $value, $this->knowledgerecord);
267 }
268
269 // Clean data
270 // $this->knowledgerecord->abc = sanitizeVal($this->knowledgerecord->abc, 'alphanohtml');
271
272 if ($this->knowledgerecord->create(DolibarrApiAccess::$user)<0) {
273 throw new RestException(500, "Error creating KnowledgeRecord", array_merge(array($this->knowledgerecord->error), $this->knowledgerecord->errors));
274 }
275 return $this->knowledgerecord->id;
276 }
277
289 public function put($id, $request_data = null)
290 {
291 if (!DolibarrApiAccess::$user->hasRight('knowledgemanagement', 'knowledgerecord', 'write')) {
292 throw new RestException(403);
293 }
294
295 $result = $this->knowledgerecord->fetch($id);
296 if (!$result) {
297 throw new RestException(404, 'KnowledgeRecord not found');
298 }
299
300 if (!DolibarrApi::_checkAccessToResource('knowledgerecord', $this->knowledgerecord->id, 'knowledgemanagement_knowledgerecord')) {
301 throw new RestException(403, 'Access to instance id='.$this->knowledgerecord->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
302 }
303
304 foreach ($request_data as $field => $value) {
305 if ($field == 'id') {
306 continue;
307 }
308 if ($field === 'caller') {
309 // 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 with the caller
310 $this->knowledgerecord->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
311 continue;
312 }
313
314 $this->knowledgerecord->$field = $this->_checkValForAPI($field, $value, $this->knowledgerecord);
315 }
316
317 // Clean data
318 // $this->knowledgerecord->abc = sanitizeVal($this->knowledgerecord->abc, 'alphanohtml');
319
320 if ($this->knowledgerecord->update(DolibarrApiAccess::$user, false) > 0) {
321 return $this->get($id);
322 } else {
323 throw new RestException(500, $this->knowledgerecord->error);
324 }
325 }
326
337 public function delete($id)
338 {
339 if (!DolibarrApiAccess::$user->hasRight('knowledgemanagement', 'knowledgerecord', 'delete')) {
340 throw new RestException(403);
341 }
342 $result = $this->knowledgerecord->fetch($id);
343 if (!$result) {
344 throw new RestException(404, 'KnowledgeRecord not found');
345 }
346
347 if (!DolibarrApi::_checkAccessToResource('knowledgerecord', $this->knowledgerecord->id, 'knowledgemanagement_knowledgerecord')) {
348 throw new RestException(403, 'Access to instance id='.$this->knowledgerecord->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
349 }
350
351 if (!$this->knowledgerecord->delete(DolibarrApiAccess::$user)) {
352 throw new RestException(500, 'Error when deleting KnowledgeRecord : '.$this->knowledgerecord->error);
353 }
354
355 return array(
356 'success' => array(
357 'code' => 200,
358 'message' => 'KnowledgeRecord deleted'
359 )
360 );
361 }
362
363
364 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
371 protected function _cleanObjectDatas($object)
372 {
373 // phpcs:enable
374 $object = parent::_cleanObjectDatas($object);
375
376 unset($object->rowid);
377 unset($object->canvas);
378
379 /*unset($object->name);
380 unset($object->lastname);
381 unset($object->firstname);
382 unset($object->civility_id);
383 unset($object->statut);
384 unset($object->state);
385 unset($object->state_id);
386 unset($object->state_code);
387 unset($object->region);
388 unset($object->region_code);
389 unset($object->country);
390 unset($object->country_id);
391 unset($object->country_code);
392 unset($object->barcode_type);
393 unset($object->barcode_type_code);
394 unset($object->barcode_type_label);
395 unset($object->barcode_type_coder);
396 unset($object->total_ht);
397 unset($object->total_tva);
398 unset($object->total_localtax1);
399 unset($object->total_localtax2);
400 unset($object->total_ttc);
401 unset($object->fk_account);
402 unset($object->comments);
403 unset($object->note);
404 unset($object->mode_reglement_id);
405 unset($object->cond_reglement_id);
406 unset($object->cond_reglement);
407 unset($object->shipping_method_id);
408 unset($object->fk_incoterms);
409 unset($object->label_incoterms);
410 unset($object->location_incoterms);
411 */
412
413 // If object has lines, remove $db property
414 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
415 $nboflines = count($object->lines);
416 for ($i = 0; $i < $nboflines; $i++) {
417 $this->_cleanObjectDatas($object->lines[$i]);
418
419 unset($object->lines[$i]->lines);
420 unset($object->lines[$i]->note);
421 }
422 }
423
424 return $object;
425 }
426
435 private function _validate($data)
436 {
437 $knowledgerecord = array();
438 foreach ($this->knowledgerecord->fields as $field => $propfield) {
439 if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
440 continue; // Not a mandatory field
441 }
442 if (!isset($data[$field])) {
443 throw new RestException(400, "$field field missing");
444 }
445 $knowledgerecord[$field] = $data[$field];
446 }
447 return $knowledgerecord;
448 }
449}
$id
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
Class to manage categories.
Class for API REST v1.
Definition api.class.php:30
_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:82
post($request_data=null)
Create knowledgerecord object.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $category=0, $sqlfilters='', $properties='', $pagination_data=false)
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.
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.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79