71 public function get($id)
73 if (!DolibarrApiAccess::$user->hasRight(
'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);
113 throw new RestException(503,
'Error when retrieve category list : '.join(
',', array_merge(array($categories->error), $categories->errors)));
137 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters =
'', $properties =
'')
144 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
145 throw new RestException(401);
148 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
150 $restrictonsocid = 0;
154 if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
155 $search_sale = DolibarrApiAccess::$user->id;
158 $sql =
"SELECT t.rowid";
159 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
160 $sql .=
", sc.fk_soc, sc.fk_user";
162 $sql .=
" FROM ".MAIN_DB_PREFIX.$tmpobject->table_element.
" AS t LEFT JOIN ".MAIN_DB_PREFIX.$tmpobject->table_element.
"_extrafields AS ef ON (ef.fk_object = t.rowid)";
164 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
165 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
168 $sql .=
", ".$this->db->prefix().
"categorie_knowledgemanagement as c";
170 $sql .=
" WHERE 1 = 1";
176 if ($tmpobject->ismultientitymanaged) {
177 $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
179 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
180 $sql .=
" AND t.fk_soc = sc.fk_soc";
182 if ($restrictonsocid && $socid) {
183 $sql .=
" AND t.fk_soc = ".((int) $socid);
185 if ($restrictonsocid && $search_sale > 0) {
186 $sql .=
" AND t.rowid = sc.fk_soc";
189 if ($restrictonsocid && $search_sale > 0) {
190 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
194 $sql .=
" AND c.fk_categorie = ".((int) $category);
195 $sql .=
" AND c.fk_knowledgemanagement = t.rowid";
201 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
205 $sql .= $this->db->order($sortfield, $sortorder);
210 $offset = $limit * $page;
212 $sql .= $this->db->plimit($limit + 1, $offset);
215 $result = $this->db->query($sql);
218 $num = $this->db->num_rows($result);
220 $obj = $this->db->fetch_object($result);
222 if ($tmp_object->fetch($obj->rowid)) {
228 throw new RestException(503,
'Error when retrieving knowledgerecord list: '.$this->db->lasterror());
244 public function post($request_data =
null)
246 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'write')) {
247 throw new RestException(401);
251 $result = $this->
_validate($request_data);
253 foreach ($request_data as $field => $value) {
254 if ($field ===
'caller') {
256 $this->knowledgerecord->context[
'caller'] = $request_data[
'caller'];
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->hasRight(
'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 if ($field ===
'caller') {
304 $this->knowledgerecord->context[
'caller'] = $request_data[
'caller'];
308 $this->knowledgerecord->$field = $this->
_checkValForAPI($field, $value, $this->knowledgerecord);
314 if ($this->knowledgerecord->update(DolibarrApiAccess::$user,
false) > 0) {
315 return $this->
get($id);
317 throw new RestException(500, $this->knowledgerecord->error);
331 public function delete($id)
333 if (!DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'delete')) {
334 throw new RestException(401);
336 $result = $this->knowledgerecord->fetch($id);
338 throw new RestException(404,
'KnowledgeRecord not found');
342 throw new RestException(401,
'Access to instance id='.$this->knowledgerecord->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
345 if (!$this->knowledgerecord->delete(DolibarrApiAccess::$user)) {
346 throw new RestException(500,
'Error when deleting KnowledgeRecord : '.$this->knowledgerecord->error);
352 'message' =>
'KnowledgeRecord deleted'
368 $object = parent::_cleanObjectDatas($object);
370 unset($object->rowid);
371 unset($object->canvas);
408 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
409 $nboflines = count($object->lines);
410 for ($i = 0; $i < $nboflines; $i++) {
413 unset($object->lines[$i]->lines);
414 unset($object->lines[$i]->note);