api_categories.class.php 27 KB

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