api_categories.class.php 23 KB

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