19 use Luracast\Restler\RestException;
43 public $knowledgerecord;
71 public function get($id)
73 if (!DolibarrApiAccess::$user->rights->knowledgemanagement->knowledgerecord->read) {
74 throw new RestException(401);
77 $result = $this->knowledgerecord->fetch($id);
79 throw new RestException(404,
'KnowledgeRecord not found');
83 throw new RestException(401,
'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->rights->categorie->lire) {
105 throw new RestException(401);
110 $result = $categories->getListForItem($id,
'knowledgemanagement', $sortfield, $sortorder, $limit, $page);
112 if (empty($result)) {
113 throw new RestException(404,
'No category found');
117 throw new RestException(503,
'Error when retrieve category list : '.join(
',', array_merge(array($categories->error), $categories->errors)));
140 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters =
'')
147 if (!DolibarrApiAccess::$user->rights->knowledgemanagement->knowledgerecord->read) {
148 throw new RestException(401);
151 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
153 $restrictonsocid = 0;
157 if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
158 $search_sale = DolibarrApiAccess::$user->id;
161 $sql =
"SELECT t.rowid";
162 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
163 $sql .=
", sc.fk_soc, sc.fk_user";
165 $sql .=
" FROM ".MAIN_DB_PREFIX.$tmpobject->table_element.
" as t";
167 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
168 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
171 $sql .=
", ".$this->db->prefix().
"categorie_knowledgemanagement as c";
173 $sql .=
" WHERE 1 = 1";
179 if ($tmpobject->ismultientitymanaged) {
180 $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
182 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
183 $sql .=
" AND t.fk_soc = sc.fk_soc";
185 if ($restrictonsocid && $socid) {
186 $sql .=
" AND t.fk_soc = ".((int) $socid);
188 if ($restrictonsocid && $search_sale > 0) {
189 $sql .=
" AND t.rowid = sc.fk_soc";
192 if ($restrictonsocid && $search_sale > 0) {
193 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
197 $sql .=
" AND c.fk_categorie = ".((int) $category);
198 $sql .=
" AND c.fk_knowledgemanagement = t.rowid";
203 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
205 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
206 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
209 $sql .= $this->
db->order($sortfield, $sortorder);
214 $offset = $limit * $page;
216 $sql .= $this->
db->plimit($limit + 1, $offset);
219 $result = $this->
db->query($sql);
222 $num = $this->
db->num_rows($result);
224 $obj = $this->
db->fetch_object($result);
226 if ($tmp_object->fetch($obj->rowid)) {
232 throw new RestException(503,
'Error when retrieving knowledgerecord list: '.$this->
db->lasterror());
234 if (!count($obj_ret)) {
235 throw new RestException(404,
'No knowledgerecord found');
250 public function post($request_data =
null)
252 if (!DolibarrApiAccess::$user->rights->knowledgemanagement->knowledgerecord->write) {
253 throw new RestException(401);
257 $result = $this->
_validate($request_data);
259 foreach ($request_data as $field => $value) {
260 $this->knowledgerecord->$field = $this->
_checkValForAPI($field, $value, $this->knowledgerecord);
266 if ($this->knowledgerecord->create(DolibarrApiAccess::$user)<0) {
267 throw new RestException(500,
"Error creating KnowledgeRecord", array_merge(array($this->knowledgerecord->error), $this->knowledgerecord->errors));
269 return $this->knowledgerecord->id;
283 public function put($id, $request_data =
null)
285 if (!DolibarrApiAccess::$user->rights->knowledgemanagement->knowledgerecord->write) {
286 throw new RestException(401);
289 $result = $this->knowledgerecord->fetch($id);
291 throw new RestException(404,
'KnowledgeRecord not found');
295 throw new RestException(401,
'Access to instance id='.$this->knowledgerecord->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
298 foreach ($request_data as $field => $value) {
299 if ($field ==
'id') {
302 $this->knowledgerecord->$field = $this->
_checkValForAPI($field, $value, $this->knowledgerecord);
308 if ($this->knowledgerecord->update(DolibarrApiAccess::$user,
false) > 0) {
309 return $this->
get($id);
311 throw new RestException(500, $this->knowledgerecord->error);
325 public function delete($id)
327 if (!DolibarrApiAccess::$user->rights->knowledgemanagement->knowledgerecord->delete) {
328 throw new RestException(401);
330 $result = $this->knowledgerecord->fetch($id);
332 throw new RestException(404,
'KnowledgeRecord not found');
336 throw new RestException(401,
'Access to instance id='.$this->knowledgerecord->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
339 if (!$this->knowledgerecord->delete(DolibarrApiAccess::$user)) {
340 throw new RestException(500,
'Error when deleting KnowledgeRecord : '.$this->knowledgerecord->error);
346 'message' =>
'KnowledgeRecord deleted'
362 $object = parent::_cleanObjectDatas($object);
364 unset($object->rowid);
365 unset($object->canvas);
402 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
403 $nboflines = count($object->lines);
404 for ($i = 0; $i < $nboflines; $i++) {
407 unset($object->lines[$i]->lines);
408 unset($object->lines[$i]->note);
425 $knowledgerecord = array();
426 foreach ($this->knowledgerecord->fields as $field => $propfield) {
427 if (in_array($field, array(
'rowid',
'entity',
'date_creation',
'tms',
'fk_user_creat')) || $propfield[
'notnull'] != 1) {
430 if (!isset($data[$field])) {
431 throw new RestException(400,
"$field field missing");
433 $knowledgerecord[$field] = $data[$field];
435 return $knowledgerecord;
Class to manage categories.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
_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='')
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.
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
$conf db
API class for accounts.