22use Luracast\Restler\RestException;
26require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
47 public $knowledgerecord;
74 public function get(
$id)
76 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
77 throw new RestException(403);
80 $result = $this->knowledgerecord->fetch(
$id);
82 throw new RestException(404,
'KnowledgeRecord not found');
86 throw new RestException(403,
'Access to instance id='.$this->knowledgerecord->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
105 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
107 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
108 throw new RestException(403);
113 $result = $categories->getListForItem(
$id,
'knowledgemanagement', $sortfield, $sortorder, $limit, $page);
116 throw new RestException(503,
'Error when retrieve category list : '.implode(
',', array_merge(array($categories->error), $categories->errors)));
143 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
148 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
149 throw new RestException(403);
152 $socid = DolibarrApiAccess::$user->socid ?: 0;
154 $restrictonsocid = 0;
158 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
159 $search_sale = DolibarrApiAccess::$user->id;
162 $sql =
"SELECT t.rowid";
163 $sql .=
" FROM ".MAIN_DB_PREFIX.$tmpobject->table_element.
" AS t";
164 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.$tmpobject->table_element.
"_extrafields AS ef ON (ef.fk_object = t.rowid)";
166 $sql .=
", ".$this->db->prefix().
"categorie_knowledgemanagement as c";
168 $sql .=
" WHERE 1 = 1";
169 if ($tmpobject->ismultientitymanaged) {
170 $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
172 if ($restrictonsocid && $socid) {
173 $sql .=
" AND t.fk_soc = ".((int) $socid);
176 if ($search_sale && $search_sale !=
'-1') {
177 if ($search_sale == -2) {
178 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', 0, 1);
179 } elseif ($search_sale > 0) {
180 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', (
int) $search_sale);
185 $sql .=
" AND c.fk_categorie = ".((int) $category);
186 $sql .=
" AND c.fk_knowledgemanagement = t.rowid";
192 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
197 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
199 $sql .= $this->db->order($sortfield, $sortorder);
204 $offset = $limit * $page;
206 $sql .= $this->db->plimit($limit + 1, $offset);
209 $result = $this->db->query($sql);
212 $num = $this->db->num_rows($result);
213 $min = min($num, ($limit <= 0 ? $num : $limit));
215 $obj = $this->db->fetch_object($result);
217 if ($tmp_object->fetch($obj->rowid)) {
223 throw new RestException(503,
'Error when retrieving knowledgerecord list: '.$this->db->lasterror());
227 if ($pagination_data) {
228 $totalsResult = $this->db->query($sqlTotals);
229 $total = $this->db->fetch_object($totalsResult)->total;
234 $obj_ret[
'data'] = $tmp;
235 $obj_ret[
'pagination'] = [
236 'total' => (int) $total,
238 'page_count' => ceil((
int) $total / $limit),
258 public function post($request_data =
null)
260 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'write')) {
261 throw new RestException(403);
265 $result = $this->
_validate($request_data);
267 foreach ($request_data as $field => $value) {
268 if ($field ===
'caller') {
270 $this->knowledgerecord->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
274 $this->knowledgerecord->$field = $this->
_checkValForAPI($field, $value, $this->knowledgerecord);
280 if ($this->knowledgerecord->create(DolibarrApiAccess::$user) < 0) {
281 throw new RestException(500,
"Error creating KnowledgeRecord", array_merge(array($this->knowledgerecord->error), $this->knowledgerecord->errors));
283 return $this->knowledgerecord->id;
299 public function put(
$id, $request_data =
null)
301 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'write')) {
302 throw new RestException(403);
305 $result = $this->knowledgerecord->fetch(
$id);
307 throw new RestException(404,
'KnowledgeRecord not found');
311 throw new RestException(403,
'Access to instance id='.$this->knowledgerecord->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
314 foreach ($request_data as $field => $value) {
315 if ($field ==
'id') {
318 if ($field ===
'caller') {
320 $this->knowledgerecord->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
324 if ($field ==
'array_options' && is_array($value)) {
325 foreach ($value as $index => $val) {
331 $this->knowledgerecord->$field = $this->
_checkValForAPI($field, $value, $this->knowledgerecord);
337 if ($this->knowledgerecord->update(DolibarrApiAccess::$user, 0) > 0) {
338 return $this->
get(
$id);
340 throw new RestException(500, $this->knowledgerecord->error);
356 public function delete(
$id)
358 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'delete')) {
359 throw new RestException(403);
361 $result = $this->knowledgerecord->fetch(
$id);
363 throw new RestException(404,
'KnowledgeRecord not found');
367 throw new RestException(403,
'Access to instance id='.$this->knowledgerecord->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
370 if (!$this->knowledgerecord->delete(DolibarrApiAccess::$user)) {
371 throw new RestException(500,
'Error when deleting KnowledgeRecord : '.$this->knowledgerecord->error);
377 'message' =>
'KnowledgeRecord deleted'
437 $nboflines = count(
$object->lines);
438 for ($i = 0; $i < $nboflines; $i++) {
441 unset(
$object->lines[$i]->lines);
442 unset(
$object->lines[$i]->note);
465 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'write')) {
466 throw new RestException(403,
"Insufficiant rights");
468 $result = $this->knowledgerecord->fetch(
$id);
470 throw new RestException(404,
'knowledgerecord not found');
474 $result = $this->knowledgerecord->validate(DolibarrApiAccess::$user, $notrigger);
476 throw new RestException(304,
'Error nothing done. May be object is already validated');
479 throw new RestException(500,
'Error when validating knowledgerecord: '.$this->knowledgerecord->error);
502 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'write')) {
503 throw new RestException(403,
"Insufficiant rights");
505 $result = $this->knowledgerecord->fetch(
$id);
507 throw new RestException(404,
'knowledgerecord not found');
511 $result = $this->knowledgerecord->cancel(DolibarrApiAccess::$user, $notrigger);
513 throw new RestException(304,
'Error nothing done. May be object is already validated');
516 throw new RestException(500,
'Error when validating knowledgerecord: '.$this->knowledgerecord->error);
532 if ($data ===
null) {
535 $knowledgerecord = array();
536 foreach ($this->knowledgerecord->fields as $field => $propfield) {
537 if (in_array($field, array(
'rowid',
'entity',
'ref',
'date_creation',
'tms',
'fk_user_creat')) || empty($propfield[
'notnull']) || $propfield[
'notnull'] != 1) {
540 if (!isset($data[$field])) {
541 throw new RestException(400,
"$field field missing");
543 $knowledgerecord[$field] = $data[$field];
545 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.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
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.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.