api_knowledgemanagement.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  3. * Copyright (C) 2021 SuperAdmin <test@dolibarr.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use Luracast\Restler\RestException;
  19. dol_include_once('/knowledgemanagement/class/knowledgerecord.class.php');
  20. dol_include_once('/categories/class/categorie.class.php');
  21. /**
  22. * \file knowledgemanagement/class/api_knowledgemanagement.class.php
  23. * \ingroup knowledgemanagement
  24. * \brief File for API management of knowledgerecord.
  25. */
  26. /**
  27. * API class for knowledgemanagement knowledgerecord
  28. *
  29. * @access protected
  30. * @class DolibarrApiAccess {@requires user,external}
  31. */
  32. class KnowledgeManagement extends DolibarrApi
  33. {
  34. /**
  35. * @var KnowledgeRecord $knowledgerecord {@type KnowledgeRecord}
  36. */
  37. public $knowledgerecord;
  38. /**
  39. * Constructor
  40. *
  41. * @url GET /
  42. *
  43. */
  44. public function __construct()
  45. {
  46. global $db, $conf;
  47. $this->db = $db;
  48. $this->knowledgerecord = new KnowledgeRecord($this->db);
  49. }
  50. /**
  51. * Get properties of a knowledgerecord object
  52. *
  53. * Return an array with knowledgerecord informations
  54. *
  55. * @param int $id ID of knowledgerecord
  56. * @return Object Object with cleaned properties
  57. *
  58. * @url GET knowledgerecords/{id}
  59. *
  60. * @throws RestException 401 Not allowed
  61. * @throws RestException 404 Not found
  62. */
  63. public function get($id)
  64. {
  65. if (!DolibarrApiAccess::$user->hasRight('knowledgemanagement', 'knowledgerecord', 'read')) {
  66. throw new RestException(401);
  67. }
  68. $result = $this->knowledgerecord->fetch($id);
  69. if (!$result) {
  70. throw new RestException(404, 'KnowledgeRecord not found');
  71. }
  72. if (!DolibarrApi::_checkAccessToResource('knowledgerecord', $this->knowledgerecord->id, 'knowledgemanagement_knowledgerecord')) {
  73. throw new RestException(401, 'Access to instance id='.$this->knowledgerecord->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
  74. }
  75. return $this->_cleanObjectDatas($this->knowledgerecord);
  76. }
  77. /**
  78. * Get categories for a knowledgerecord object
  79. *
  80. * @param int $id ID of knowledgerecord object
  81. * @param string $sortfield Sort field
  82. * @param string $sortorder Sort order
  83. * @param int $limit Limit for list
  84. * @param int $page Page number
  85. *
  86. * @return mixed
  87. *
  88. * @url GET /knowledgerecords/{id}/categories
  89. */
  90. public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
  91. {
  92. if (!DolibarrApiAccess::$user->rights->categorie->lire) {
  93. throw new RestException(401);
  94. }
  95. $categories = new Categorie($this->db);
  96. $result = $categories->getListForItem($id, 'knowledgemanagement', $sortfield, $sortorder, $limit, $page);
  97. if (empty($result)) {
  98. throw new RestException(404, 'No category found');
  99. }
  100. if ($result < 0) {
  101. throw new RestException(503, 'Error when retrieve category list : '.join(',', array_merge(array($categories->error), $categories->errors)));
  102. }
  103. return $result;
  104. }
  105. /**
  106. * List knowledgerecords
  107. *
  108. * Get a list of knowledgerecords
  109. *
  110. * @param string $sortfield Sort field
  111. * @param string $sortorder Sort order
  112. * @param int $limit Limit for list
  113. * @param int $page Page number
  114. * @param int $category Use this param to filter list by category
  115. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')"
  116. * @return array Array of order objects
  117. *
  118. * @throws RestException
  119. *
  120. * @url GET /knowledgerecords/
  121. */
  122. public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters = '')
  123. {
  124. global $db, $conf;
  125. $obj_ret = array();
  126. $tmpobject = new KnowledgeRecord($this->db);
  127. if (!DolibarrApiAccess::$user->hasRight('knowledgemanagement', 'knowledgerecord', 'read')) {
  128. throw new RestException(401);
  129. }
  130. $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
  131. $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
  132. // If the internal user must only see his customers, force searching by him
  133. $search_sale = 0;
  134. if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
  135. $search_sale = DolibarrApiAccess::$user->id;
  136. }
  137. $sql = "SELECT t.rowid";
  138. if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  139. $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
  140. }
  141. $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)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
  142. if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  143. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
  144. }
  145. if ($category > 0) {
  146. $sql .= ", ".$this->db->prefix()."categorie_knowledgemanagement as c";
  147. }
  148. $sql .= " WHERE 1 = 1";
  149. // Example of use $mode
  150. //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
  151. //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
  152. if ($tmpobject->ismultientitymanaged) {
  153. $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
  154. }
  155. if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  156. $sql .= " AND t.fk_soc = sc.fk_soc";
  157. }
  158. if ($restrictonsocid && $socid) {
  159. $sql .= " AND t.fk_soc = ".((int) $socid);
  160. }
  161. if ($restrictonsocid && $search_sale > 0) {
  162. $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
  163. }
  164. // Insert sale filter
  165. if ($restrictonsocid && $search_sale > 0) {
  166. $sql .= " AND sc.fk_user = ".((int) $search_sale);
  167. }
  168. // Select products of given category
  169. if ($category > 0) {
  170. $sql .= " AND c.fk_categorie = ".((int) $category);
  171. $sql .= " AND c.fk_knowledgemanagement = t.rowid";
  172. }
  173. if ($sqlfilters) {
  174. $errormessage = '';
  175. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  176. if ($errormessage) {
  177. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  178. }
  179. }
  180. $sql .= $this->db->order($sortfield, $sortorder);
  181. if ($limit) {
  182. if ($page < 0) {
  183. $page = 0;
  184. }
  185. $offset = $limit * $page;
  186. $sql .= $this->db->plimit($limit + 1, $offset);
  187. }
  188. $result = $this->db->query($sql);
  189. $i = 0;
  190. if ($result) {
  191. $num = $this->db->num_rows($result);
  192. while ($i < $num) {
  193. $obj = $this->db->fetch_object($result);
  194. $tmp_object = new KnowledgeRecord($this->db);
  195. if ($tmp_object->fetch($obj->rowid)) {
  196. $obj_ret[] = $this->_cleanObjectDatas($tmp_object);
  197. }
  198. $i++;
  199. }
  200. } else {
  201. throw new RestException(503, 'Error when retrieving knowledgerecord list: '.$this->db->lasterror());
  202. }
  203. if (!count($obj_ret)) {
  204. throw new RestException(404, 'No knowledgerecord found');
  205. }
  206. return $obj_ret;
  207. }
  208. /**
  209. * Create knowledgerecord object
  210. *
  211. * @param array $request_data Request datas
  212. * @return int ID of knowledgerecord
  213. *
  214. * @throws RestException
  215. *
  216. * @url POST knowledgerecords/
  217. */
  218. public function post($request_data = null)
  219. {
  220. if (!DolibarrApiAccess::$user->hasRight('knowledgemanagement', 'knowledgerecord', 'write')) {
  221. throw new RestException(401);
  222. }
  223. // Check mandatory fields
  224. $result = $this->_validate($request_data);
  225. foreach ($request_data as $field => $value) {
  226. $this->knowledgerecord->$field = $this->_checkValForAPI($field, $value, $this->knowledgerecord);
  227. }
  228. // Clean data
  229. // $this->knowledgerecord->abc = sanitizeVal($this->knowledgerecord->abc, 'alphanohtml');
  230. if ($this->knowledgerecord->create(DolibarrApiAccess::$user)<0) {
  231. throw new RestException(500, "Error creating KnowledgeRecord", array_merge(array($this->knowledgerecord->error), $this->knowledgerecord->errors));
  232. }
  233. return $this->knowledgerecord->id;
  234. }
  235. /**
  236. * Update knowledgerecord
  237. *
  238. * @param int $id Id of knowledgerecord to update
  239. * @param array $request_data Datas
  240. * @return int
  241. *
  242. * @throws RestException
  243. *
  244. * @url PUT knowledgerecords/{id}
  245. */
  246. public function put($id, $request_data = null)
  247. {
  248. if (!DolibarrApiAccess::$user->hasRight('knowledgemanagement', 'knowledgerecord', 'write')) {
  249. throw new RestException(401);
  250. }
  251. $result = $this->knowledgerecord->fetch($id);
  252. if (!$result) {
  253. throw new RestException(404, 'KnowledgeRecord not found');
  254. }
  255. if (!DolibarrApi::_checkAccessToResource('knowledgerecord', $this->knowledgerecord->id, 'knowledgemanagement_knowledgerecord')) {
  256. throw new RestException(401, 'Access to instance id='.$this->knowledgerecord->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
  257. }
  258. foreach ($request_data as $field => $value) {
  259. if ($field == 'id') {
  260. continue;
  261. }
  262. $this->knowledgerecord->$field = $this->_checkValForAPI($field, $value, $this->knowledgerecord);
  263. }
  264. // Clean data
  265. // $this->knowledgerecord->abc = sanitizeVal($this->knowledgerecord->abc, 'alphanohtml');
  266. if ($this->knowledgerecord->update(DolibarrApiAccess::$user, false) > 0) {
  267. return $this->get($id);
  268. } else {
  269. throw new RestException(500, $this->knowledgerecord->error);
  270. }
  271. }
  272. /**
  273. * Delete knowledgerecord
  274. *
  275. * @param int $id KnowledgeRecord ID
  276. * @return array
  277. *
  278. * @throws RestException
  279. *
  280. * @url DELETE knowledgerecords/{id}
  281. */
  282. public function delete($id)
  283. {
  284. if (!DolibarrApiAccess::$user->hasRight('knowledgemanagement', 'knowledgerecord', 'delete')) {
  285. throw new RestException(401);
  286. }
  287. $result = $this->knowledgerecord->fetch($id);
  288. if (!$result) {
  289. throw new RestException(404, 'KnowledgeRecord not found');
  290. }
  291. if (!DolibarrApi::_checkAccessToResource('knowledgerecord', $this->knowledgerecord->id, 'knowledgemanagement_knowledgerecord')) {
  292. throw new RestException(401, 'Access to instance id='.$this->knowledgerecord->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
  293. }
  294. if (!$this->knowledgerecord->delete(DolibarrApiAccess::$user)) {
  295. throw new RestException(500, 'Error when deleting KnowledgeRecord : '.$this->knowledgerecord->error);
  296. }
  297. return array(
  298. 'success' => array(
  299. 'code' => 200,
  300. 'message' => 'KnowledgeRecord deleted'
  301. )
  302. );
  303. }
  304. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  305. /**
  306. * Clean sensible object datas
  307. *
  308. * @param Object $object Object to clean
  309. * @return Object Object with cleaned properties
  310. */
  311. protected function _cleanObjectDatas($object)
  312. {
  313. // phpcs:enable
  314. $object = parent::_cleanObjectDatas($object);
  315. unset($object->rowid);
  316. unset($object->canvas);
  317. /*unset($object->name);
  318. unset($object->lastname);
  319. unset($object->firstname);
  320. unset($object->civility_id);
  321. unset($object->statut);
  322. unset($object->state);
  323. unset($object->state_id);
  324. unset($object->state_code);
  325. unset($object->region);
  326. unset($object->region_code);
  327. unset($object->country);
  328. unset($object->country_id);
  329. unset($object->country_code);
  330. unset($object->barcode_type);
  331. unset($object->barcode_type_code);
  332. unset($object->barcode_type_label);
  333. unset($object->barcode_type_coder);
  334. unset($object->total_ht);
  335. unset($object->total_tva);
  336. unset($object->total_localtax1);
  337. unset($object->total_localtax2);
  338. unset($object->total_ttc);
  339. unset($object->fk_account);
  340. unset($object->comments);
  341. unset($object->note);
  342. unset($object->mode_reglement_id);
  343. unset($object->cond_reglement_id);
  344. unset($object->cond_reglement);
  345. unset($object->shipping_method_id);
  346. unset($object->fk_incoterms);
  347. unset($object->label_incoterms);
  348. unset($object->location_incoterms);
  349. */
  350. // If object has lines, remove $db property
  351. if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
  352. $nboflines = count($object->lines);
  353. for ($i = 0; $i < $nboflines; $i++) {
  354. $this->_cleanObjectDatas($object->lines[$i]);
  355. unset($object->lines[$i]->lines);
  356. unset($object->lines[$i]->note);
  357. }
  358. }
  359. return $object;
  360. }
  361. /**
  362. * Validate fields before create or update object
  363. *
  364. * @param array $data Array of data to validate
  365. * @return array
  366. *
  367. * @throws RestException
  368. */
  369. private function _validate($data)
  370. {
  371. $knowledgerecord = array();
  372. foreach ($this->knowledgerecord->fields as $field => $propfield) {
  373. if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
  374. continue; // Not a mandatory field
  375. }
  376. if (!isset($data[$field])) {
  377. throw new RestException(400, "$field field missing");
  378. }
  379. $knowledgerecord[$field] = $data[$field];
  380. }
  381. return $knowledgerecord;
  382. }
  383. }