19use Luracast\Restler\RestException;
43 public $knowledgerecord;
71 public function get(
$id)
73 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
74 throw new RestException(403);
77 $result = $this->knowledgerecord->fetch(
$id);
79 throw new RestException(404,
'KnowledgeRecord not found');
83 throw new RestException(403,
'Access to instance id='.$this->knowledgerecord->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
102 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
104 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
105 throw new RestException(403);
110 $result = $categories->getListForItem(
$id,
'knowledgemanagement', $sortfield, $sortorder, $limit, $page);
113 throw new RestException(503,
'Error when retrieve category list : '.implode(
',', array_merge(array($categories->error), $categories->errors)));
138 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
143 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
144 throw new RestException(403);
147 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : 0;
149 $restrictonsocid = 0;
153 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
154 $search_sale = DolibarrApiAccess::$user->id;
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)";
161 $sql .=
", ".$this->db->prefix().
"categorie_knowledgemanagement as c";
163 $sql .=
" WHERE 1 = 1";
164 if ($tmpobject->ismultientitymanaged) {
165 $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
167 if ($restrictonsocid && $socid) {
168 $sql .=
" AND t.fk_soc = ".((int) $socid);
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).
")";
180 $sql .=
" AND c.fk_categorie = ".((int) $category);
181 $sql .=
" AND c.fk_knowledgemanagement = t.rowid";
187 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
192 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
194 $sql .= $this->db->order($sortfield, $sortorder);
199 $offset = $limit * $page;
201 $sql .= $this->db->plimit($limit + 1, $offset);
204 $result = $this->db->query($sql);
207 $num = $this->db->num_rows($result);
209 $obj = $this->db->fetch_object($result);
211 if ($tmp_object->fetch($obj->rowid)) {
217 throw new RestException(503,
'Error when retrieving knowledgerecord list: '.$this->db->lasterror());
221 if ($pagination_data) {
222 $totalsResult = $this->db->query($sqlTotals);
223 $total = $this->db->fetch_object($totalsResult)->total;
228 $obj_ret[
'data'] = $tmp;
229 $obj_ret[
'pagination'] = [
230 'total' => (int) $total,
232 'page_count' => ceil((
int) $total / $limit),
250 public function post($request_data =
null)
252 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'write')) {
253 throw new RestException(403);
257 $result = $this->
_validate($request_data);
259 foreach ($request_data as $field => $value) {
260 if ($field ===
'caller') {
262 $this->knowledgerecord->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
266 $this->knowledgerecord->$field = $this->
_checkValForAPI($field, $value, $this->knowledgerecord);
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));
275 return $this->knowledgerecord->id;
289 public function put(
$id, $request_data =
null)
291 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'write')) {
292 throw new RestException(403);
295 $result = $this->knowledgerecord->fetch(
$id);
297 throw new RestException(404,
'KnowledgeRecord not found');
301 throw new RestException(403,
'Access to instance id='.$this->knowledgerecord->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
304 foreach ($request_data as $field => $value) {
305 if ($field ==
'id') {
308 if ($field ===
'caller') {
310 $this->knowledgerecord->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
314 $this->knowledgerecord->$field = $this->
_checkValForAPI($field, $value, $this->knowledgerecord);
320 if ($this->knowledgerecord->update(DolibarrApiAccess::$user,
false) > 0) {
321 return $this->
get(
$id);
323 throw new RestException(500, $this->knowledgerecord->error);
337 public function delete(
$id)
339 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'delete')) {
340 throw new RestException(403);
342 $result = $this->knowledgerecord->fetch(
$id);
344 throw new RestException(404,
'KnowledgeRecord not found');
348 throw new RestException(403,
'Access to instance id='.$this->knowledgerecord->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
351 if (!$this->knowledgerecord->delete(DolibarrApiAccess::$user)) {
352 throw new RestException(500,
'Error when deleting KnowledgeRecord : '.$this->knowledgerecord->error);
358 'message' =>
'KnowledgeRecord deleted'
415 $nboflines = count(
$object->lines);
416 for ($i = 0; $i < $nboflines; $i++) {
419 unset(
$object->lines[$i]->lines);
420 unset(
$object->lines[$i]->note);
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) {
442 if (!isset($data[$field])) {
443 throw new RestException(400,
"$field field missing");
445 $knowledgerecord[$field] = $data[$field];
447 return $knowledgerecord;
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.
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.
__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.