22use Luracast\Restler\RestException;
46 public $knowledgerecord;
73 public function get(
$id)
75 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
76 throw new RestException(403);
79 $result = $this->knowledgerecord->fetch(
$id);
81 throw new RestException(404,
'KnowledgeRecord not found');
85 throw new RestException(403,
'Access to instance id='.$this->knowledgerecord->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
104 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
106 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
107 throw new RestException(403);
112 $result = $categories->getListForItem(
$id,
'knowledgemanagement', $sortfield, $sortorder, $limit, $page);
115 throw new RestException(503,
'Error when retrieve category list : '.implode(
',', array_merge(array($categories->error), $categories->errors)));
142 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
147 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
148 throw new RestException(403);
151 $socid = DolibarrApiAccess::$user->socid ?: 0;
153 $restrictonsocid = 0;
157 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
158 $search_sale = DolibarrApiAccess::$user->id;
161 $sql =
"SELECT t.rowid";
162 $sql .=
" FROM ".MAIN_DB_PREFIX.$tmpobject->table_element.
" AS t";
163 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.$tmpobject->table_element.
"_extrafields AS ef ON (ef.fk_object = t.rowid)";
165 $sql .=
", ".$this->db->prefix().
"categorie_knowledgemanagement as c";
167 $sql .=
" WHERE 1 = 1";
168 if ($tmpobject->ismultientitymanaged) {
169 $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
171 if ($restrictonsocid && $socid) {
172 $sql .=
" AND t.fk_soc = ".((int) $socid);
175 if ($search_sale && $search_sale !=
'-1') {
176 if ($search_sale == -2) {
177 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
178 } elseif ($search_sale > 0) {
179 $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).
")";
184 $sql .=
" AND c.fk_categorie = ".((int) $category);
185 $sql .=
" AND c.fk_knowledgemanagement = t.rowid";
189 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
191 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
196 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
198 $sql .= $this->db->order($sortfield, $sortorder);
203 $offset = $limit * $page;
205 $sql .= $this->db->plimit($limit + 1, $offset);
208 $result = $this->db->query($sql);
211 $num = $this->db->num_rows($result);
212 $min = min($num, ($limit <= 0 ? $num : $limit));
214 $obj = $this->db->fetch_object($result);
216 if ($tmp_object->fetch($obj->rowid)) {
222 throw new RestException(503,
'Error when retrieving knowledgerecord list: '.$this->db->lasterror());
226 if ($pagination_data) {
227 $totalsResult = $this->db->query($sqlTotals);
228 $total = $this->db->fetch_object($totalsResult)->total;
233 $obj_ret[
'data'] = $tmp;
234 $obj_ret[
'pagination'] = [
235 'total' => (int) $total,
237 'page_count' => ceil((
int) $total / $limit),
257 public function post($request_data =
null)
259 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'write')) {
260 throw new RestException(403);
264 $result = $this->
_validate($request_data);
266 foreach ($request_data as $field => $value) {
267 if ($field ===
'caller') {
269 $this->knowledgerecord->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
273 $this->knowledgerecord->$field = $this->
_checkValForAPI($field, $value, $this->knowledgerecord);
279 if ($this->knowledgerecord->create(DolibarrApiAccess::$user) < 0) {
280 throw new RestException(500,
"Error creating KnowledgeRecord", array_merge(array($this->knowledgerecord->error), $this->knowledgerecord->errors));
282 return $this->knowledgerecord->id;
298 public function put(
$id, $request_data =
null)
300 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'write')) {
301 throw new RestException(403);
304 $result = $this->knowledgerecord->fetch(
$id);
306 throw new RestException(404,
'KnowledgeRecord not found');
310 throw new RestException(403,
'Access to instance id='.$this->knowledgerecord->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
313 foreach ($request_data as $field => $value) {
314 if ($field ==
'id') {
317 if ($field ===
'caller') {
319 $this->knowledgerecord->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
323 if ($field ==
'array_options' && is_array($value)) {
324 foreach ($value as $index => $val) {
330 $this->knowledgerecord->$field = $this->
_checkValForAPI($field, $value, $this->knowledgerecord);
336 if ($this->knowledgerecord->update(DolibarrApiAccess::$user, 0) > 0) {
337 return $this->
get(
$id);
339 throw new RestException(500, $this->knowledgerecord->error);
355 public function delete(
$id)
357 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'delete')) {
358 throw new RestException(403);
360 $result = $this->knowledgerecord->fetch(
$id);
362 throw new RestException(404,
'KnowledgeRecord not found');
366 throw new RestException(403,
'Access to instance id='.$this->knowledgerecord->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
369 if (!$this->knowledgerecord->delete(DolibarrApiAccess::$user)) {
370 throw new RestException(500,
'Error when deleting KnowledgeRecord : '.$this->knowledgerecord->error);
376 'message' =>
'KnowledgeRecord deleted'
436 $nboflines = count(
$object->lines);
437 for ($i = 0; $i < $nboflines; $i++) {
440 unset(
$object->lines[$i]->lines);
441 unset(
$object->lines[$i]->note);
464 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'write')) {
465 throw new RestException(403,
"Insufficiant rights");
467 $result = $this->knowledgerecord->fetch(
$id);
469 throw new RestException(404,
'knowledgerecord not found');
473 $result = $this->knowledgerecord->validate(DolibarrApiAccess::$user, $notrigger);
475 throw new RestException(304,
'Error nothing done. May be object is already validated');
478 throw new RestException(500,
'Error when validating knowledgerecord: '.$this->knowledgerecord->error);
501 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'write')) {
502 throw new RestException(403,
"Insufficiant rights");
504 $result = $this->knowledgerecord->fetch(
$id);
506 throw new RestException(404,
'knowledgerecord not found');
510 $result = $this->knowledgerecord->cancel(DolibarrApiAccess::$user, $notrigger);
512 throw new RestException(304,
'Error nothing done. May be object is already validated');
515 throw new RestException(500,
'Error when validating knowledgerecord: '.$this->knowledgerecord->error);
531 if ($data ===
null) {
534 $knowledgerecord = array();
535 foreach ($this->knowledgerecord->fields as $field => $propfield) {
536 if (in_array($field, array(
'rowid',
'entity',
'ref',
'date_creation',
'tms',
'fk_user_creat')) || empty($propfield[
'notnull']) || $propfield[
'notnull'] != 1) {
539 if (!isset($data[$field])) {
540 throw new RestException(400,
"$field field missing");
542 $knowledgerecord[$field] = $data[$field];
544 return $knowledgerecord;
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
Class to manage categories.
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_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.
cancel($id, $notrigger=0)
Cancel a knowledge.
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($id, $notrigger=0)
Validate a knowledge.
_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.
__construct()
Constructor.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
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.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.