api_categories.class.php 24 KB

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