api_categories.class.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. <?php
  2. /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. use Luracast\Restler\RestException;
  18. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  19. require_once DOL_DOCUMENT_ROOT.'/societe/class/client.class.php';
  20. require_once DOL_DOCUMENT_ROOT.'/adherents/class/api_members.class.php';
  21. require_once DOL_DOCUMENT_ROOT.'/product/class/api_products.class.php';
  22. require_once DOL_DOCUMENT_ROOT.'/societe/class/api_contacts.class.php';
  23. require_once DOL_DOCUMENT_ROOT.'/societe/class/api_thirdparties.class.php';
  24. require_once DOL_DOCUMENT_ROOT.'/projet/class/api_projects.class.php';
  25. /**
  26. * API class for categories
  27. *
  28. * @access protected
  29. * @class DolibarrApiAccess {@requires user,external}
  30. */
  31. class Categories extends DolibarrApi
  32. {
  33. /**
  34. * @var array $FIELDS Mandatory fields, checked when create and update object
  35. */
  36. static $FIELDS = array(
  37. 'label',
  38. 'type'
  39. );
  40. static $TYPES = array(
  41. 0 => 'product',
  42. 1 => 'supplier',
  43. 2 => 'customer',
  44. 3 => 'member',
  45. 4 => 'contact',
  46. 5 => 'account',
  47. 6 => 'project',
  48. //7 => 'user',
  49. //8 => 'bank_line',
  50. //9 => 'warehouse',
  51. //10 => 'actioncomm',
  52. );
  53. /**
  54. * @var Categorie $category {@type Categorie}
  55. */
  56. public $category;
  57. /**
  58. * Constructor
  59. */
  60. public function __construct()
  61. {
  62. global $db, $conf;
  63. $this->db = $db;
  64. $this->category = new Categorie($this->db);
  65. }
  66. /**
  67. * Get properties of a category object
  68. *
  69. * Return an array with category informations
  70. *
  71. * @param int $id ID of category
  72. * @param bool $include_childs Include child categories list (true or false)
  73. * @return array|mixed data without useless information
  74. *
  75. * @throws RestException
  76. */
  77. public function get($id, $include_childs = false)
  78. {
  79. if (!DolibarrApiAccess::$user->rights->categorie->lire) {
  80. throw new RestException(401);
  81. }
  82. $result = $this->category->fetch($id);
  83. if (!$result) {
  84. throw new RestException(404, 'category not found');
  85. }
  86. if (!DolibarrApi::_checkAccessToResource('categorie', $this->category->id)) {
  87. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  88. }
  89. if ($include_childs) {
  90. $cats = $this->category->get_filles();
  91. if (!is_array($cats)) {
  92. throw new RestException(500, 'Error when fetching child categories', array_merge(array($this->category->error), $this->category->errors));
  93. }
  94. $this->category->childs = [];
  95. foreach ($cats as $cat) {
  96. $this->category->childs[] = $this->_cleanObjectDatas($cat);
  97. }
  98. }
  99. return $this->_cleanObjectDatas($this->category);
  100. }
  101. /**
  102. * List categories
  103. *
  104. * Get a list of categories
  105. *
  106. * @param string $sortfield Sort field
  107. * @param string $sortorder Sort order
  108. * @param int $limit Limit for list
  109. * @param int $page Page number
  110. * @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact')
  111. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')"
  112. * @return array Array of category objects
  113. *
  114. * @throws RestException
  115. */
  116. public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $type = '', $sqlfilters = '')
  117. {
  118. global $db, $conf;
  119. $obj_ret = array();
  120. if (!DolibarrApiAccess::$user->rights->categorie->lire) {
  121. throw new RestException(401);
  122. }
  123. $sql = "SELECT t.rowid";
  124. $sql .= " FROM ".MAIN_DB_PREFIX."categorie as t";
  125. $sql .= ' WHERE t.entity IN ('.getEntity('category').')';
  126. if (!empty($type)) {
  127. $sql .= ' AND t.type='.array_search($type, Categories::$TYPES);
  128. }
  129. // Add sql filters
  130. if ($sqlfilters) {
  131. if (!DolibarrApi::_checkFilters($sqlfilters)) {
  132. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  133. }
  134. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  135. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  136. }
  137. $sql .= $this->db->order($sortfield, $sortorder);
  138. if ($limit) {
  139. if ($page < 0) {
  140. $page = 0;
  141. }
  142. $offset = $limit * $page;
  143. $sql .= $this->db->plimit($limit + 1, $offset);
  144. }
  145. $result = $this->db->query($sql);
  146. if ($result) {
  147. $i = 0;
  148. $num = $this->db->num_rows($result);
  149. $min = min($num, ($limit <= 0 ? $num : $limit));
  150. while ($i < $min) {
  151. $obj = $this->db->fetch_object($result);
  152. $category_static = new Categorie($this->db);
  153. if ($category_static->fetch($obj->rowid)) {
  154. $obj_ret[] = $this->_cleanObjectDatas($category_static);
  155. }
  156. $i++;
  157. }
  158. } else {
  159. throw new RestException(503, 'Error when retrieve category list : '.$this->db->lasterror());
  160. }
  161. if (!count($obj_ret)) {
  162. throw new RestException(404, 'No category found');
  163. }
  164. return $obj_ret;
  165. }
  166. /**
  167. * Create category object
  168. *
  169. * @param array $request_data Request data
  170. * @return int ID of category
  171. */
  172. public function post($request_data = null)
  173. {
  174. if (!DolibarrApiAccess::$user->rights->categorie->creer) {
  175. throw new RestException(401);
  176. }
  177. // Check mandatory fields
  178. $result = $this->_validate($request_data);
  179. foreach ($request_data as $field => $value) {
  180. $this->category->$field = $value;
  181. }
  182. if ($this->category->create(DolibarrApiAccess::$user) < 0) {
  183. throw new RestException(500, 'Error when creating category', array_merge(array($this->category->error), $this->category->errors));
  184. }
  185. return $this->category->id;
  186. }
  187. /**
  188. * Update category
  189. *
  190. * @param int $id Id of category to update
  191. * @param array $request_data Datas
  192. * @return int
  193. */
  194. public function put($id, $request_data = null)
  195. {
  196. if (!DolibarrApiAccess::$user->rights->categorie->creer) {
  197. throw new RestException(401);
  198. }
  199. $result = $this->category->fetch($id);
  200. if (!$result) {
  201. throw new RestException(404, 'category not found');
  202. }
  203. if (!DolibarrApi::_checkAccessToResource('categorie', $this->category->id)) {
  204. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  205. }
  206. foreach ($request_data as $field => $value) {
  207. if ($field == 'id') {
  208. continue;
  209. }
  210. $this->category->$field = $value;
  211. }
  212. if ($this->category->update(DolibarrApiAccess::$user) > 0) {
  213. return $this->get($id);
  214. } else {
  215. throw new RestException(500, $this->category->error);
  216. }
  217. }
  218. /**
  219. * Delete category
  220. *
  221. * @param int $id Category ID
  222. * @return array
  223. */
  224. public function delete($id)
  225. {
  226. if (!DolibarrApiAccess::$user->rights->categorie->supprimer) {
  227. throw new RestException(401);
  228. }
  229. $result = $this->category->fetch($id);
  230. if (!$result) {
  231. throw new RestException(404, 'category not found');
  232. }
  233. if (!DolibarrApi::_checkAccessToResource('categorie', $this->category->id)) {
  234. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  235. }
  236. if (!$this->category->delete(DolibarrApiAccess::$user)) {
  237. throw new RestException(401, 'error when delete category');
  238. }
  239. return array(
  240. 'success' => array(
  241. 'code' => 200,
  242. 'message' => 'Category deleted'
  243. )
  244. );
  245. }
  246. /**
  247. * List categories of an object
  248. *
  249. * Get the list of categories linked to an object
  250. *
  251. * @param int $id Object ID
  252. * @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact', 'project')
  253. * @param string $sortfield Sort field
  254. * @param string $sortorder Sort order
  255. * @param int $limit Limit for list
  256. * @param int $page Page number
  257. * @return array Array of category objects
  258. *
  259. * @throws RestException
  260. *
  261. * @url GET /object/{type}/{id}
  262. */
  263. public function getListForObject($id, $type, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
  264. {
  265. if (!in_array($type, [
  266. Categorie::TYPE_PRODUCT,
  267. Categorie::TYPE_CONTACT,
  268. Categorie::TYPE_CUSTOMER,
  269. Categorie::TYPE_SUPPLIER,
  270. Categorie::TYPE_MEMBER,
  271. Categorie::TYPE_PROJECT
  272. ])) {
  273. throw new RestException(401);
  274. }
  275. if ($type == Categorie::TYPE_PRODUCT && !(DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) {
  276. throw new RestException(401);
  277. } elseif ($type == Categorie::TYPE_CONTACT && !DolibarrApiAccess::$user->rights->contact->lire) {
  278. throw new RestException(401);
  279. } elseif ($type == Categorie::TYPE_CUSTOMER && !DolibarrApiAccess::$user->rights->societe->lire) {
  280. throw new RestException(401);
  281. } elseif ($type == Categorie::TYPE_SUPPLIER && !DolibarrApiAccess::$user->rights->fournisseur->lire) {
  282. throw new RestException(401);
  283. } elseif ($type == Categorie::TYPE_MEMBER && !DolibarrApiAccess::$user->rights->adherent->lire) {
  284. throw new RestException(401);
  285. } elseif ($type == Categorie::TYPE_PROJECT && !DolibarrApiAccess::$user->rights->projet->lire) {
  286. throw new RestException(401);
  287. }
  288. $categories = $this->category->getListForItem($id, $type, $sortfield, $sortorder, $limit, $page);
  289. if (!is_array($categories)) {
  290. if ($categories == 0) {
  291. throw new RestException(404, 'No category found for this object');
  292. }
  293. throw new RestException(600, 'Error when fetching object categories', array_merge(array($this->category->error), $this->category->errors));
  294. }
  295. return $categories;
  296. }
  297. /**
  298. * Link an object to a category by id
  299. *
  300. * @param int $id ID of category
  301. * @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact')
  302. * @param int $object_id ID of object
  303. *
  304. * @return array
  305. * @throws RestException
  306. *
  307. * @url POST {id}/objects/{type}/{object_id}
  308. */
  309. public function linkObjectById($id, $type, $object_id)
  310. {
  311. if (empty($type) || empty($object_id)) {
  312. throw new RestException(401);
  313. }
  314. if (!DolibarrApiAccess::$user->rights->categorie->lire) {
  315. throw new RestException(401);
  316. }
  317. $result = $this->category->fetch($id);
  318. if (!$result) {
  319. throw new RestException(404, 'category not found');
  320. }
  321. if ($type === Categorie::TYPE_PRODUCT) {
  322. if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
  323. throw new RestException(401);
  324. }
  325. $object = new Product($this->db);
  326. } elseif ($type === Categorie::TYPE_CUSTOMER) {
  327. if (!DolibarrApiAccess::$user->rights->societe->creer) {
  328. throw new RestException(401);
  329. }
  330. $object = new Societe($this->db);
  331. } elseif ($type === Categorie::TYPE_SUPPLIER) {
  332. if (!DolibarrApiAccess::$user->rights->societe->creer) {
  333. throw new RestException(401);
  334. }
  335. $object = new Societe($this->db);
  336. } elseif ($type === Categorie::TYPE_CONTACT) {
  337. if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
  338. throw new RestException(401);
  339. }
  340. $object = new Contact($this->db);
  341. } elseif ($type === Categorie::TYPE_MEMBER) {
  342. if (!DolibarrApiAccess::$user->rights->adherent->creer) {
  343. throw new RestException(401);
  344. }
  345. $object = new Adherent($this->db);
  346. } else {
  347. throw new RestException(401, "this type is not recognized yet.");
  348. }
  349. if (!empty($object)) {
  350. $result = $object->fetch($object_id);
  351. if ($result > 0) {
  352. $result = $this->category->add_type($object, $type);
  353. if ($result < 0) {
  354. if ($this->category->error != 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  355. throw new RestException(500, 'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
  356. }
  357. }
  358. } else {
  359. throw new RestException(500, 'Error when fetching object', array_merge(array($object->error), $object->errors));
  360. }
  361. return array(
  362. 'success' => array(
  363. 'code' => 200,
  364. 'message' => 'Objects succefully linked to the category'
  365. )
  366. );
  367. }
  368. throw new RestException(401);
  369. }
  370. /**
  371. * Link an object to a category by ref
  372. *
  373. * @param int $id ID of category
  374. * @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact')
  375. * @param string $object_ref Reference of object
  376. *
  377. * @return array
  378. * @throws RestException
  379. *
  380. * @url POST {id}/objects/{type}/ref/{object_ref}
  381. */
  382. public function linkObjectByRef($id, $type, $object_ref)
  383. {
  384. if (empty($type) || empty($object_ref)) {
  385. throw new RestException(401);
  386. }
  387. if (!DolibarrApiAccess::$user->rights->categorie->lire) {
  388. throw new RestException(401);
  389. }
  390. $result = $this->category->fetch($id);
  391. if (!$result) {
  392. throw new RestException(404, 'category not found');
  393. }
  394. if ($type === Categorie::TYPE_PRODUCT) {
  395. if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
  396. throw new RestException(401);
  397. }
  398. $object = new Product($this->db);
  399. } elseif ($type === Categorie::TYPE_CUSTOMER) {
  400. if (!DolibarrApiAccess::$user->rights->societe->creer) {
  401. throw new RestException(401);
  402. }
  403. $object = new Societe($this->db);
  404. } elseif ($type === Categorie::TYPE_SUPPLIER) {
  405. if (!DolibarrApiAccess::$user->rights->societe->creer) {
  406. throw new RestException(401);
  407. }
  408. $object = new Societe($this->db);
  409. } elseif ($type === Categorie::TYPE_CONTACT) {
  410. if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
  411. throw new RestException(401);
  412. }
  413. $object = new Contact($this->db);
  414. } elseif ($type === Categorie::TYPE_MEMBER) {
  415. if (!DolibarrApiAccess::$user->rights->adherent->creer) {
  416. throw new RestException(401);
  417. }
  418. $object = new Adherent($this->db);
  419. } else {
  420. throw new RestException(401, "this type is not recognized yet.");
  421. }
  422. if (!empty($object)) {
  423. $result = $object->fetch('', $object_ref);
  424. if ($result > 0) {
  425. $result = $this->category->add_type($object, $type);
  426. if ($result < 0) {
  427. if ($this->category->error != 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  428. throw new RestException(500, 'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
  429. }
  430. }
  431. } else {
  432. throw new RestException(500, 'Error when fetching object', array_merge(array($object->error), $object->errors));
  433. }
  434. return array(
  435. 'success' => array(
  436. 'code' => 200,
  437. 'message' => 'Objects succefully linked to the category'
  438. )
  439. );
  440. }
  441. throw new RestException(401);
  442. }
  443. /**
  444. * Unlink an object from a category by id
  445. *
  446. * @param int $id ID of category
  447. * @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact')
  448. * @param int $object_id ID of the object
  449. *
  450. * @return array
  451. * @throws RestException
  452. *
  453. * @url DELETE {id}/objects/{type}/{object_id}
  454. */
  455. public function unlinkObjectById($id, $type, $object_id)
  456. {
  457. if (empty($type) || empty($object_id)) {
  458. throw new RestException(401);
  459. }
  460. if (!DolibarrApiAccess::$user->rights->categorie->lire) {
  461. throw new RestException(401);
  462. }
  463. $result = $this->category->fetch($id);
  464. if (!$result) {
  465. throw new RestException(404, 'category not found');
  466. }
  467. if ($type === Categorie::TYPE_PRODUCT) {
  468. if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
  469. throw new RestException(401);
  470. }
  471. $object = new Product($this->db);
  472. } elseif ($type === Categorie::TYPE_CUSTOMER) {
  473. if (!DolibarrApiAccess::$user->rights->societe->creer) {
  474. throw new RestException(401);
  475. }
  476. $object = new Societe($this->db);
  477. } elseif ($type === Categorie::TYPE_SUPPLIER) {
  478. if (!DolibarrApiAccess::$user->rights->societe->creer) {
  479. throw new RestException(401);
  480. }
  481. $object = new Societe($this->db);
  482. } elseif ($type === Categorie::TYPE_CONTACT) {
  483. if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
  484. throw new RestException(401);
  485. }
  486. $object = new Contact($this->db);
  487. } elseif ($type === Categorie::TYPE_MEMBER) {
  488. if (!DolibarrApiAccess::$user->rights->adherent->creer) {
  489. throw new RestException(401);
  490. }
  491. $object = new Adherent($this->db);
  492. } else {
  493. throw new RestException(401, "this type is not recognized yet.");
  494. }
  495. if (!empty($object)) {
  496. $result = $object->fetch((int) $object_id);
  497. if ($result > 0) {
  498. $result = $this->category->del_type($object, $type);
  499. if ($result < 0) {
  500. throw new RestException(500, 'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors));
  501. }
  502. } else {
  503. throw new RestException(500, 'Error when fetching object', array_merge(array($object->error), $object->errors));
  504. }
  505. return array(
  506. 'success' => array(
  507. 'code' => 200,
  508. 'message' => 'Objects succefully unlinked from the category'
  509. )
  510. );
  511. }
  512. throw new RestException(401);
  513. }
  514. /**
  515. * Unlink an object from a category by ref
  516. *
  517. * @param int $id ID of category
  518. * @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact')
  519. * @param string $object_ref Reference of the object
  520. *
  521. * @return array
  522. * @throws RestException
  523. *
  524. * @url DELETE {id}/objects/{type}/ref/{object_ref}
  525. */
  526. public function unlinkObjectByRef($id, $type, $object_ref)
  527. {
  528. if (empty($type) || empty($object_ref)) {
  529. throw new RestException(401);
  530. }
  531. if (!DolibarrApiAccess::$user->rights->categorie->lire) {
  532. throw new RestException(401);
  533. }
  534. $result = $this->category->fetch($id);
  535. if (!$result) {
  536. throw new RestException(404, 'category not found');
  537. }
  538. if ($type === Categorie::TYPE_PRODUCT) {
  539. if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
  540. throw new RestException(401);
  541. }
  542. $object = new Product($this->db);
  543. } elseif ($type === Categorie::TYPE_CUSTOMER) {
  544. if (!DolibarrApiAccess::$user->rights->societe->creer) {
  545. throw new RestException(401);
  546. }
  547. $object = new Societe($this->db);
  548. } elseif ($type === Categorie::TYPE_SUPPLIER) {
  549. if (!DolibarrApiAccess::$user->rights->societe->creer) {
  550. throw new RestException(401);
  551. }
  552. $object = new Societe($this->db);
  553. } elseif ($type === Categorie::TYPE_CONTACT) {
  554. if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
  555. throw new RestException(401);
  556. }
  557. $object = new Contact($this->db);
  558. } elseif ($type === Categorie::TYPE_MEMBER) {
  559. if (!DolibarrApiAccess::$user->rights->adherent->creer) {
  560. throw new RestException(401);
  561. }
  562. $object = new Adherent($this->db);
  563. } else {
  564. throw new RestException(401, "this type is not recognized yet.");
  565. }
  566. if (!empty($object)) {
  567. $result = $object->fetch('', (string) $object_ref);
  568. if ($result > 0) {
  569. $result = $this->category->del_type($object, $type);
  570. if ($result < 0) {
  571. throw new RestException(500, 'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors));
  572. }
  573. } else {
  574. throw new RestException(500, 'Error when fetching object', array_merge(array($object->error), $object->errors));
  575. }
  576. return array(
  577. 'success' => array(
  578. 'code' => 200,
  579. 'message' => 'Objects succefully unlinked from the category'
  580. )
  581. );
  582. }
  583. throw new RestException(401);
  584. }
  585. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  586. /**
  587. * Clean sensible object datas
  588. *
  589. * @param Categorie $object Object to clean
  590. * @return Object Object with cleaned properties
  591. */
  592. protected function _cleanObjectDatas($object)
  593. {
  594. // phpcs:enable
  595. $object = parent::_cleanObjectDatas($object);
  596. // Remove fields not relevent to categories
  597. unset($object->country);
  598. unset($object->country_id);
  599. unset($object->country_code);
  600. unset($object->total_ht);
  601. unset($object->total_ht);
  602. unset($object->total_localtax1);
  603. unset($object->total_localtax2);
  604. unset($object->total_ttc);
  605. unset($object->total_tva);
  606. unset($object->lines);
  607. unset($object->fk_incoterms);
  608. unset($object->label_incoterms);
  609. unset($object->location_incoterms);
  610. unset($object->civility_id);
  611. unset($object->name);
  612. unset($object->lastname);
  613. unset($object->firstname);
  614. unset($object->shipping_method_id);
  615. unset($object->fk_delivery_address);
  616. unset($object->cond_reglement);
  617. unset($object->cond_reglement_id);
  618. unset($object->mode_reglement_id);
  619. unset($object->barcode_type_coder);
  620. unset($object->barcode_type_label);
  621. unset($object->barcode_type_code);
  622. unset($object->barcode_type);
  623. unset($object->canvas);
  624. unset($object->cats);
  625. unset($object->motherof);
  626. unset($object->context);
  627. unset($object->socid);
  628. unset($object->thirdparty);
  629. unset($object->contact);
  630. unset($object->contact_id);
  631. unset($object->user);
  632. unset($object->fk_account);
  633. unset($object->fk_project);
  634. unset($object->note);
  635. unset($object->statut);
  636. return $object;
  637. }
  638. /**
  639. * Validate fields before create or update object
  640. *
  641. * @param array|null $data Data to validate
  642. * @return array
  643. *
  644. * @throws RestException
  645. */
  646. private function _validate($data)
  647. {
  648. $category = array();
  649. foreach (Categories::$FIELDS as $field) {
  650. if (!isset($data[$field])) {
  651. throw new RestException(400, "$field field missing");
  652. }
  653. $category[$field] = $data[$field];
  654. }
  655. return $category;
  656. }
  657. /**
  658. * Get the list of objects in a category.
  659. *
  660. * @param int $id ID of category
  661. * @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact', 'project')
  662. * @param int $onlyids Return only ids of objects (consume less memory)
  663. *
  664. * @return mixed
  665. *
  666. * @url GET {id}/objects
  667. */
  668. public function getObjects($id, $type, $onlyids = 0)
  669. {
  670. dol_syslog("getObjects($id, $type, $onlyids)", LOG_DEBUG);
  671. if (!DolibarrApiAccess::$user->rights->categorie->lire) {
  672. throw new RestException(401);
  673. }
  674. if (empty($type)) {
  675. throw new RestException(500, 'The "type" parameter is required.');
  676. }
  677. $result = $this->category->fetch($id);
  678. if (!$result) {
  679. throw new RestException(404, 'category not found');
  680. }
  681. if (!DolibarrApi::_checkAccessToResource('categorie', $this->category->id)) {
  682. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  683. }
  684. $result = $this->category->getObjectsInCateg($type, $onlyids);
  685. if ($result < 0) {
  686. throw new RestException(503, 'Error when retrieving objects list : '.$this->category->error);
  687. }
  688. $objects = $result;
  689. $cleaned_objects = array();
  690. $objects_api = null;
  691. if ($type == 'member') {
  692. $objects_api = new Members();
  693. } elseif ($type == 'customer' || $type == 'supplier') {
  694. $objects_api = new Thirdparties();
  695. } elseif ($type == 'product') {
  696. $objects_api = new Products();
  697. } elseif ($type == 'contact') {
  698. $objects_api = new Contacts();
  699. } elseif ($type == 'project') {
  700. $objects_api = new Projects();
  701. }
  702. if (is_object($objects_api)) {
  703. foreach ($objects as $obj) {
  704. $cleaned_objects[] = $objects_api->_cleanObjectDatas($obj);
  705. }
  706. }
  707. return $cleaned_objects;
  708. }
  709. }