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)));
137 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters =
'', $properties =
'')
142 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
143 throw new RestException(403);
146 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : 0;
148 $restrictonsocid = 0;
152 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
153 $search_sale = DolibarrApiAccess::$user->id;
156 $sql =
"SELECT t.rowid";
157 $sql .=
" FROM ".MAIN_DB_PREFIX.$tmpobject->table_element.
" AS t";
158 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.$tmpobject->table_element.
"_extrafields AS ef ON (ef.fk_object = t.rowid)";
160 $sql .=
", ".$this->db->prefix().
"categorie_knowledgemanagement as c";
162 $sql .=
" WHERE 1 = 1";
163 if ($tmpobject->ismultientitymanaged) {
164 $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
166 if ($restrictonsocid && $socid) {
167 $sql .=
" AND t.fk_soc = ".((int) $socid);
170 if ($search_sale && $search_sale !=
'-1') {
171 if ($search_sale == -2) {
172 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
173 } elseif ($search_sale > 0) {
174 $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).
")";
179 $sql .=
" AND c.fk_categorie = ".((int) $category);
180 $sql .=
" AND c.fk_knowledgemanagement = t.rowid";
186 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
190 $sql .= $this->db->order($sortfield, $sortorder);
195 $offset = $limit * $page;
197 $sql .= $this->db->plimit($limit + 1, $offset);
200 $result = $this->db->query($sql);
203 $num = $this->db->num_rows($result);
205 $obj = $this->db->fetch_object($result);
207 if ($tmp_object->fetch($obj->rowid)) {
213 throw new RestException(503,
'Error when retrieving knowledgerecord list: '.$this->db->lasterror());
229 public function post($request_data =
null)
231 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'write')) {
232 throw new RestException(403);
236 $result = $this->
_validate($request_data);
238 foreach ($request_data as $field => $value) {
239 if ($field ===
'caller') {
241 $this->knowledgerecord->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
245 $this->knowledgerecord->$field = $this->
_checkValForAPI($field, $value, $this->knowledgerecord);
251 if ($this->knowledgerecord->create(DolibarrApiAccess::$user)<0) {
252 throw new RestException(500,
"Error creating KnowledgeRecord", array_merge(array($this->knowledgerecord->error), $this->knowledgerecord->errors));
254 return $this->knowledgerecord->id;
268 public function put($id, $request_data =
null)
270 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'write')) {
271 throw new RestException(403);
274 $result = $this->knowledgerecord->fetch($id);
276 throw new RestException(404,
'KnowledgeRecord not found');
280 throw new RestException(403,
'Access to instance id='.$this->knowledgerecord->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
283 foreach ($request_data as $field => $value) {
284 if ($field ==
'id') {
287 if ($field ===
'caller') {
289 $this->knowledgerecord->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
293 $this->knowledgerecord->$field = $this->
_checkValForAPI($field, $value, $this->knowledgerecord);
299 if ($this->knowledgerecord->update(DolibarrApiAccess::$user,
false) > 0) {
300 return $this->
get($id);
302 throw new RestException(500, $this->knowledgerecord->error);
316 public function delete($id)
318 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'delete')) {
319 throw new RestException(403);
321 $result = $this->knowledgerecord->fetch($id);
323 throw new RestException(404,
'KnowledgeRecord not found');
327 throw new RestException(403,
'Access to instance id='.$this->knowledgerecord->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
330 if (!$this->knowledgerecord->delete(DolibarrApiAccess::$user)) {
331 throw new RestException(500,
'Error when deleting KnowledgeRecord : '.$this->knowledgerecord->error);
337 'message' =>
'KnowledgeRecord deleted'
394 $nboflines = count(
$object->lines);
395 for ($i = 0; $i < $nboflines; $i++) {
398 unset(
$object->lines[$i]->lines);
399 unset(
$object->lines[$i]->note);
416 $knowledgerecord = array();
417 foreach ($this->knowledgerecord->fields as $field => $propfield) {
418 if (in_array($field, array(
'rowid',
'entity',
'date_creation',
'tms',
'fk_user_creat')) || $propfield[
'notnull'] != 1) {
421 if (!isset($data[$field])) {
422 throw new RestException(400,
"$field field missing");
424 $knowledgerecord[$field] = $data[$field];
426 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.
_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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $category=0, $sqlfilters='', $properties='')
List knowledgerecords.
__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.