21use Luracast\Restler\RestException;
45 public $knowledgerecord;
72 public function get(
$id)
74 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
75 throw new RestException(403);
78 $result = $this->knowledgerecord->fetch(
$id);
80 throw new RestException(404,
'KnowledgeRecord not found');
84 throw new RestException(403,
'Access to instance id='.$this->knowledgerecord->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
103 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
105 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
106 throw new RestException(403);
111 $result = $categories->getListForItem(
$id,
'knowledgemanagement', $sortfield, $sortorder, $limit, $page);
114 throw new RestException(503,
'Error when retrieve category list : '.implode(
',', array_merge(array($categories->error), $categories->errors)));
141 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
146 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
147 throw new RestException(403);
150 $socid = DolibarrApiAccess::$user->socid ?: 0;
152 $restrictonsocid = 0;
156 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
157 $search_sale = DolibarrApiAccess::$user->id;
160 $sql =
"SELECT t.rowid";
161 $sql .=
" FROM ".MAIN_DB_PREFIX.$tmpobject->table_element.
" AS t";
162 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.$tmpobject->table_element.
"_extrafields AS ef ON (ef.fk_object = t.rowid)";
164 $sql .=
", ".$this->db->prefix().
"categorie_knowledgemanagement as c";
166 $sql .=
" WHERE 1 = 1";
167 if ($tmpobject->ismultientitymanaged) {
168 $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
170 if ($restrictonsocid && $socid) {
171 $sql .=
" AND t.fk_soc = ".((int) $socid);
174 if ($search_sale && $search_sale !=
'-1') {
175 if ($search_sale == -2) {
176 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
177 } elseif ($search_sale > 0) {
178 $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).
")";
183 $sql .=
" AND c.fk_categorie = ".((int) $category);
184 $sql .=
" AND c.fk_knowledgemanagement = t.rowid";
190 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
195 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
197 $sql .= $this->db->order($sortfield, $sortorder);
202 $offset = $limit * $page;
204 $sql .= $this->db->plimit($limit + 1, $offset);
207 $result = $this->db->query($sql);
210 $num = $this->db->num_rows($result);
212 $obj = $this->db->fetch_object($result);
214 if ($tmp_object->fetch($obj->rowid)) {
220 throw new RestException(503,
'Error when retrieving knowledgerecord list: '.$this->db->lasterror());
224 if ($pagination_data) {
225 $totalsResult = $this->db->query($sqlTotals);
226 $total = $this->db->fetch_object($totalsResult)->total;
231 $obj_ret[
'data'] = $tmp;
232 $obj_ret[
'pagination'] = [
233 'total' => (int) $total,
235 'page_count' => ceil((
int) $total / $limit),
255 public function post($request_data =
null)
257 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'write')) {
258 throw new RestException(403);
262 $result = $this->
_validate($request_data);
264 foreach ($request_data as $field => $value) {
265 if ($field ===
'caller') {
267 $this->knowledgerecord->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
271 $this->knowledgerecord->$field = $this->
_checkValForAPI($field, $value, $this->knowledgerecord);
277 if ($this->knowledgerecord->create(DolibarrApiAccess::$user) < 0) {
278 throw new RestException(500,
"Error creating KnowledgeRecord", array_merge(array($this->knowledgerecord->error), $this->knowledgerecord->errors));
280 return $this->knowledgerecord->id;
296 public function put(
$id, $request_data =
null)
298 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'write')) {
299 throw new RestException(403);
302 $result = $this->knowledgerecord->fetch(
$id);
304 throw new RestException(404,
'KnowledgeRecord not found');
308 throw new RestException(403,
'Access to instance id='.$this->knowledgerecord->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
311 foreach ($request_data as $field => $value) {
312 if ($field ==
'id') {
315 if ($field ===
'caller') {
317 $this->knowledgerecord->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
321 if ($field ==
'array_options' && is_array($value)) {
322 foreach ($value as $index => $val) {
323 $this->knowledgerecord->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->knowledgerecord);
328 $this->knowledgerecord->$field = $this->
_checkValForAPI($field, $value, $this->knowledgerecord);
334 if ($this->knowledgerecord->update(DolibarrApiAccess::$user, 0) > 0) {
335 return $this->
get(
$id);
337 throw new RestException(500, $this->knowledgerecord->error);
353 public function delete(
$id)
355 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'delete')) {
356 throw new RestException(403);
358 $result = $this->knowledgerecord->fetch(
$id);
360 throw new RestException(404,
'KnowledgeRecord not found');
364 throw new RestException(403,
'Access to instance id='.$this->knowledgerecord->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
367 if (!$this->knowledgerecord->delete(DolibarrApiAccess::$user)) {
368 throw new RestException(500,
'Error when deleting KnowledgeRecord : '.$this->knowledgerecord->error);
374 'message' =>
'KnowledgeRecord deleted'
431 $nboflines = count(
$object->lines);
432 for ($i = 0; $i < $nboflines; $i++) {
435 unset(
$object->lines[$i]->lines);
436 unset(
$object->lines[$i]->note);
459 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'write')) {
460 throw new RestException(403,
"Insuffisant rights");
462 $result = $this->knowledgerecord->fetch(
$id);
464 throw new RestException(404,
'knowledgerecord not found');
468 $result = $this->knowledgerecord->validate(DolibarrApiAccess::$user, $notrigger);
470 throw new RestException(304,
'Error nothing done. May be object is already validated');
473 throw new RestException(500,
'Error when validating knowledgerecord: '.$this->knowledgerecord->error);
496 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'write')) {
497 throw new RestException(403,
"Insuffisant rights");
499 $result = $this->knowledgerecord->fetch(
$id);
501 throw new RestException(404,
'knowledgerecord not found');
505 $result = $this->knowledgerecord->cancel(DolibarrApiAccess::$user, $notrigger);
507 throw new RestException(304,
'Error nothing done. May be object is already validated');
510 throw new RestException(500,
'Error when validating knowledgerecord: '.$this->knowledgerecord->error);
526 if ($data ===
null) {
529 $knowledgerecord = array();
530 foreach ($this->knowledgerecord->fields as $field => $propfield) {
531 if (in_array($field, array(
'rowid',
'entity',
'ref',
'date_creation',
'tms',
'fk_user_creat')) || empty($propfield[
'notnull']) || $propfield[
'notnull'] != 1) {
534 if (!isset($data[$field])) {
535 throw new RestException(400,
"$field field missing");
537 $knowledgerecord[$field] = $data[$field];
539 return $knowledgerecord;
$id
Support class for third parties, contacts, members, users or resources.
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Class to manage categories.
_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.
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...