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 : '.array_merge(array($categories->error), $categories->errors));
139 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'')
146 if (!DolibarrApiAccess::$user->rights->knowledgemanagement->knowledgerecord->read) {
147 throw new RestException(401);
150 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
152 $restrictonsocid = 0;
156 if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
157 $search_sale = DolibarrApiAccess::$user->id;
160 $sql =
"SELECT t.rowid";
161 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
162 $sql .=
", sc.fk_soc, sc.fk_user";
164 $sql .=
" FROM ".MAIN_DB_PREFIX.$tmpobject->table_element.
" as t";
166 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
167 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
169 $sql .=
" WHERE 1 = 1";
175 if ($tmpobject->ismultientitymanaged) {
176 $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
178 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
179 $sql .=
" AND t.fk_soc = sc.fk_soc";
181 if ($restrictonsocid && $socid) {
182 $sql .=
" AND t.fk_soc = ".((int) $socid);
184 if ($restrictonsocid && $search_sale > 0) {
185 $sql .=
" AND t.rowid = sc.fk_soc";
188 if ($restrictonsocid && $search_sale > 0) {
189 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
194 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
196 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
197 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
200 $sql .= $this->
db->order($sortfield, $sortorder);
205 $offset = $limit * $page;
207 $sql .= $this->
db->plimit($limit + 1, $offset);
210 $result = $this->
db->query($sql);
213 $num = $this->
db->num_rows($result);
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());
225 if (!count($obj_ret)) {
226 throw new RestException(404,
'No knowledgerecord found');
241 public function post($request_data =
null)
243 if (!DolibarrApiAccess::$user->rights->knowledgemanagement->knowledgerecord->write) {
244 throw new RestException(401);
248 $result = $this->
_validate($request_data);
250 foreach ($request_data as $field => $value) {
251 $this->knowledgerecord->$field = $this->
_checkValForAPI($field, $value, $this->knowledgerecord);
257 if ($this->knowledgerecord->create(DolibarrApiAccess::$user)<0) {
258 throw new RestException(500,
"Error creating KnowledgeRecord", array_merge(array($this->knowledgerecord->error), $this->knowledgerecord->errors));
260 return $this->knowledgerecord->id;
274 public function put($id, $request_data =
null)
276 if (!DolibarrApiAccess::$user->rights->knowledgemanagement->knowledgerecord->write) {
277 throw new RestException(401);
280 $result = $this->knowledgerecord->fetch($id);
282 throw new RestException(404,
'KnowledgeRecord not found');
286 throw new RestException(401,
'Access to instance id='.$this->knowledgerecord->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
289 foreach ($request_data as $field => $value) {
290 if ($field ==
'id') {
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->rights->knowledgemanagement->knowledgerecord->delete) {
319 throw new RestException(401);
321 $result = $this->knowledgerecord->fetch($id);
323 throw new RestException(404,
'KnowledgeRecord not found');
327 throw new RestException(401,
'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'
353 $object = parent::_cleanObjectDatas($object);
355 unset($object->rowid);
356 unset($object->canvas);
393 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
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;