api_members.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. <?php
  2. /* Copyright (C) 2016 Xebax Christy <xebax@wanadoo.fr>
  3. * Copyright (C) 2017 Regis Houssin <regis.houssin@inodbox.com>
  4. * Copyright (C) 2020 Thibault FOUCART<support@ptibogxiv.net>
  5. * Copyright (C) 2020 Frédéric France <frederic.france@netlogic.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. use Luracast\Restler\RestException;
  21. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  22. require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
  23. require_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
  24. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  25. /**
  26. * API class for members
  27. *
  28. * @access protected
  29. * @class DolibarrApiAccess {@requires user,external}
  30. */
  31. class Members extends DolibarrApi
  32. {
  33. /**
  34. * @var array $FIELDS Mandatory fields, checked when create and update object
  35. */
  36. public static $FIELDS = array(
  37. 'morphy',
  38. 'typeid'
  39. );
  40. /**
  41. * Constructor
  42. */
  43. public function __construct()
  44. {
  45. global $db, $conf;
  46. $this->db = $db;
  47. }
  48. /**
  49. * Get properties of a member object
  50. *
  51. * Return an array with member informations
  52. *
  53. * @param int $id ID of member
  54. * @return array|mixed data without useless information
  55. *
  56. * @throws RestException
  57. */
  58. public function get($id)
  59. {
  60. if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
  61. throw new RestException(401);
  62. }
  63. $member = new Adherent($this->db);
  64. if ($id == 0) {
  65. $result = $member->initAsSpecimen();
  66. } else {
  67. $result = $member->fetch($id);
  68. }
  69. if (!$result) {
  70. throw new RestException(404, 'member not found');
  71. }
  72. if (!DolibarrApi::_checkAccessToResource('adherent', $member->id) && $id > 0) {
  73. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  74. }
  75. return $this->_cleanObjectDatas($member);
  76. }
  77. /**
  78. * Get properties of a member object by linked thirdparty
  79. *
  80. * Return an array with member informations
  81. *
  82. * @param int $thirdparty ID of third party
  83. *
  84. * @return Object Data without useless information
  85. *
  86. * @url GET thirdparty/{thirdparty}
  87. *
  88. * @throws RestException 401
  89. * @throws RestException 404
  90. */
  91. public function getByThirdparty($thirdparty)
  92. {
  93. if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
  94. throw new RestException(401);
  95. }
  96. $member = new Adherent($this->db);
  97. $result = $member->fetch('', '', $thirdparty);
  98. if (!$result) {
  99. throw new RestException(404, 'member not found');
  100. }
  101. if (!DolibarrApi::_checkAccessToResource('adherent', $member->id)) {
  102. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  103. }
  104. return $this->_cleanObjectDatas($member);
  105. }
  106. /**
  107. * Get properties of a member object by linked thirdparty email
  108. *
  109. * Return an array with member informations
  110. *
  111. * @param string $email Email of third party
  112. *
  113. * @return Object Data without useless information
  114. *
  115. * @url GET thirdparty/email/{email}
  116. *
  117. * @throws RestException 401
  118. * @throws RestException 404
  119. */
  120. public function getByThirdpartyEmail($email)
  121. {
  122. if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
  123. throw new RestException(401);
  124. }
  125. $thirdparty = new Societe($this->db);
  126. $result = $thirdparty->fetch('', '', '', '', '', '', '', '', '', '', $email);
  127. if (!$result) {
  128. throw new RestException(404, 'thirdparty not found');
  129. }
  130. $member = new Adherent($this->db);
  131. $result = $member->fetch('', '', $thirdparty->id);
  132. if (!$result) {
  133. throw new RestException(404, 'member not found');
  134. }
  135. if (!DolibarrApi::_checkAccessToResource('adherent', $member->id)) {
  136. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  137. }
  138. return $this->_cleanObjectDatas($member);
  139. }
  140. /**
  141. * Get properties of a member object by linked thirdparty barcode
  142. *
  143. * Return an array with member informations
  144. *
  145. * @param string $barcode Barcode of third party
  146. *
  147. * @return Object Data without useless information
  148. *
  149. * @url GET thirdparty/barcode/{barcode}
  150. *
  151. * @throws RestException 401
  152. * @throws RestException 404
  153. */
  154. public function getByThirdpartyBarcode($barcode)
  155. {
  156. if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
  157. throw new RestException(401);
  158. }
  159. $thirdparty = new Societe($this->db);
  160. $result = $thirdparty->fetch('', '', '', $barcode);
  161. if (!$result) {
  162. throw new RestException(404, 'thirdparty not found');
  163. }
  164. $member = new Adherent($this->db);
  165. $result = $member->fetch('', '', $thirdparty->id);
  166. if (!$result) {
  167. throw new RestException(404, 'member not found');
  168. }
  169. if (!DolibarrApi::_checkAccessToResource('adherent', $member->id)) {
  170. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  171. }
  172. return $this->_cleanObjectDatas($member);
  173. }
  174. /**
  175. * List members
  176. *
  177. * Get a list of members
  178. *
  179. * @param string $sortfield Sort field
  180. * @param string $sortorder Sort order
  181. * @param int $limit Limit for list
  182. * @param int $page Page number
  183. * @param string $typeid ID of the type of member
  184. * @param int $category Use this param to filter list by category
  185. * @param string $sqlfilters Other criteria to filter answers separated by a comma.
  186. * Example: "(t.ref:like:'SO-%') and ((t.date_creation:<:'20160101') or (t.nature:is:NULL))"
  187. * @return array Array of member objects
  188. *
  189. * @throws RestException
  190. */
  191. public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $typeid = '', $category = 0, $sqlfilters = '')
  192. {
  193. global $db, $conf;
  194. $obj_ret = array();
  195. if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
  196. throw new RestException(401);
  197. }
  198. $sql = "SELECT t.rowid";
  199. $sql .= " FROM ".MAIN_DB_PREFIX."adherent AS t LEFT JOIN ".MAIN_DB_PREFIX."adherent_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call
  200. if ($category > 0) {
  201. $sql .= ", ".MAIN_DB_PREFIX."categorie_member as c";
  202. }
  203. $sql .= ' WHERE t.entity IN ('.getEntity('adherent').')';
  204. if (!empty($typeid)) {
  205. $sql .= ' AND t.fk_adherent_type='.((int) $typeid);
  206. }
  207. // Select members of given category
  208. if ($category > 0) {
  209. $sql .= " AND c.fk_categorie = ".((int) $category);
  210. $sql .= " AND c.fk_member = t.rowid";
  211. }
  212. // Add sql filters
  213. if ($sqlfilters) {
  214. $errormessage = '';
  215. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  216. if ($errormessage) {
  217. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  218. }
  219. }
  220. $sql .= $this->db->order($sortfield, $sortorder);
  221. if ($limit) {
  222. if ($page < 0) {
  223. $page = 0;
  224. }
  225. $offset = $limit * $page;
  226. $sql .= $this->db->plimit($limit + 1, $offset);
  227. }
  228. $result = $this->db->query($sql);
  229. if ($result) {
  230. $i = 0;
  231. $num = $this->db->num_rows($result);
  232. $min = min($num, ($limit <= 0 ? $num : $limit));
  233. while ($i < $min) {
  234. $obj = $this->db->fetch_object($result);
  235. $member = new Adherent($this->db);
  236. if ($member->fetch($obj->rowid)) {
  237. $obj_ret[] = $this->_cleanObjectDatas($member);
  238. }
  239. $i++;
  240. }
  241. } else {
  242. throw new RestException(503, 'Error when retrieve member list : '.$this->db->lasterror());
  243. }
  244. if (!count($obj_ret)) {
  245. throw new RestException(404, 'No member found');
  246. }
  247. return $obj_ret;
  248. }
  249. /**
  250. * Create member object
  251. *
  252. * @param array $request_data Request data
  253. * @return int ID of member
  254. */
  255. public function post($request_data = null)
  256. {
  257. if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
  258. throw new RestException(401);
  259. }
  260. // Check mandatory fields
  261. $result = $this->_validate($request_data);
  262. $member = new Adherent($this->db);
  263. foreach ($request_data as $field => $value) {
  264. $member->$field = $value;
  265. }
  266. if ($member->create(DolibarrApiAccess::$user) < 0) {
  267. throw new RestException(500, 'Error creating member', array_merge(array($member->error), $member->errors));
  268. }
  269. return $member->id;
  270. }
  271. /**
  272. * Update member
  273. *
  274. * @param int $id ID of member to update
  275. * @param array $request_data Datas
  276. * @return Object Updated object
  277. */
  278. public function put($id, $request_data = null)
  279. {
  280. if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
  281. throw new RestException(401);
  282. }
  283. $member = new Adherent($this->db);
  284. $result = $member->fetch($id);
  285. if (!$result) {
  286. throw new RestException(404, 'member not found');
  287. }
  288. if (!DolibarrApi::_checkAccessToResource('member', $member->id)) {
  289. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  290. }
  291. foreach ($request_data as $field => $value) {
  292. if ($field == 'id') {
  293. continue;
  294. }
  295. // Process the status separately because it must be updated using
  296. // the validate(), resiliate() and exclude() methods of the class Adherent.
  297. if ($field == 'statut') {
  298. if ($value == '0') {
  299. $result = $member->resiliate(DolibarrApiAccess::$user);
  300. if ($result < 0) {
  301. throw new RestException(500, 'Error when resiliating member: '.$member->error);
  302. }
  303. } elseif ($value == '1') {
  304. $result = $member->validate(DolibarrApiAccess::$user);
  305. if ($result < 0) {
  306. throw new RestException(500, 'Error when validating member: '.$member->error);
  307. }
  308. } elseif ($value == '-2') {
  309. $result = $member->exclude(DolibarrApiAccess::$user);
  310. if ($result < 0) {
  311. throw new RestException(500, 'Error when excluding member: '.$member->error);
  312. }
  313. }
  314. } else {
  315. $member->$field = $value;
  316. }
  317. }
  318. // If there is no error, update() returns the number of affected rows
  319. // so if the update is a no op, the return value is zero.
  320. if ($member->update(DolibarrApiAccess::$user) >= 0) {
  321. return $this->get($id);
  322. } else {
  323. throw new RestException(500, 'Error when updating member: '.$member->error);
  324. }
  325. }
  326. /**
  327. * Delete member
  328. *
  329. * @param int $id member ID
  330. * @return array
  331. */
  332. public function delete($id)
  333. {
  334. if (!DolibarrApiAccess::$user->hasRight('adherent', 'supprimer')) {
  335. throw new RestException(401);
  336. }
  337. $member = new Adherent($this->db);
  338. $result = $member->fetch($id);
  339. if (!$result) {
  340. throw new RestException(404, 'member not found');
  341. }
  342. if (!DolibarrApi::_checkAccessToResource('member', $member->id)) {
  343. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  344. }
  345. $res = $member->delete($member->id, DolibarrApiAccess::$user);
  346. if ($res < 0) {
  347. throw new RestException(500, "Can't delete, error occurs");
  348. } elseif ($res == 0) {
  349. throw new RestException(409, "Can't delete, that product is probably used");
  350. }
  351. return array(
  352. 'success' => array(
  353. 'code' => 200,
  354. 'message' => 'Member deleted'
  355. )
  356. );
  357. }
  358. /**
  359. * Validate fields before creating an object
  360. *
  361. * @param array|null $data Data to validate
  362. * @return array
  363. *
  364. * @throws RestException
  365. */
  366. private function _validate($data)
  367. {
  368. $member = array();
  369. foreach (Members::$FIELDS as $field) {
  370. if (!isset($data[$field])) {
  371. throw new RestException(400, "$field field missing");
  372. }
  373. $member[$field] = $data[$field];
  374. }
  375. return $member;
  376. }
  377. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  378. /**
  379. * Clean sensible object datas
  380. *
  381. * @param Object $object Object to clean
  382. * @return Object Object with cleaned properties
  383. */
  384. protected function _cleanObjectDatas($object)
  385. {
  386. // phpcs:enable
  387. $object = parent::_cleanObjectDatas($object);
  388. // Remove the subscriptions because they are handled as a subresource.
  389. unset($object->subscriptions);
  390. unset($object->fk_incoterms);
  391. unset($object->label_incoterms);
  392. unset($object->location_incoterms);
  393. unset($object->fk_delivery_address);
  394. unset($object->shipping_method_id);
  395. unset($object->total_ht);
  396. unset($object->total_ttc);
  397. unset($object->total_tva);
  398. unset($object->total_localtax1);
  399. unset($object->total_localtax2);
  400. return $object;
  401. }
  402. /**
  403. * List subscriptions of a member
  404. *
  405. * Get a list of subscriptions
  406. *
  407. * @param int $id ID of member
  408. * @return array Array of subscription objects
  409. *
  410. * @throws RestException
  411. *
  412. * @url GET {id}/subscriptions
  413. */
  414. public function getSubscriptions($id)
  415. {
  416. $obj_ret = array();
  417. if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'lire')) {
  418. throw new RestException(401);
  419. }
  420. $member = new Adherent($this->db);
  421. $result = $member->fetch($id);
  422. if (!$result) {
  423. throw new RestException(404, 'member not found');
  424. }
  425. $obj_ret = array();
  426. foreach ($member->subscriptions as $subscription) {
  427. $obj_ret[] = $this->_cleanObjectDatas($subscription);
  428. }
  429. return $obj_ret;
  430. }
  431. /**
  432. * Add a subscription for a member
  433. *
  434. * @param int $id ID of member
  435. * @param string $start_date Start date {@from body} {@type timestamp}
  436. * @param string $end_date End date {@from body} {@type timestamp}
  437. * @param float $amount Amount (may be 0) {@from body}
  438. * @param string $label Label {@from body}
  439. * @return int ID of subscription
  440. *
  441. * @url POST {id}/subscriptions
  442. */
  443. public function createSubscription($id, $start_date, $end_date, $amount, $label = '')
  444. {
  445. if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'creer')) {
  446. throw new RestException(401);
  447. }
  448. $member = new Adherent($this->db);
  449. $result = $member->fetch($id);
  450. if (!$result) {
  451. throw new RestException(404, 'member not found');
  452. }
  453. return $member->subscription($start_date, $amount, 0, '', $label, '', '', '', $end_date);
  454. }
  455. /**
  456. * Get categories for a member
  457. *
  458. * @param int $id ID of member
  459. * @param string $sortfield Sort field
  460. * @param string $sortorder Sort order
  461. * @param int $limit Limit for list
  462. * @param int $page Page number
  463. *
  464. * @return mixed
  465. *
  466. * @url GET {id}/categories
  467. */
  468. public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
  469. {
  470. if (!DolibarrApiAccess::$user->rights->categorie->lire) {
  471. throw new RestException(401);
  472. }
  473. $categories = new Categorie($this->db);
  474. $result = $categories->getListForItem($id, 'member', $sortfield, $sortorder, $limit, $page);
  475. if (empty($result)) {
  476. throw new RestException(404, 'No category found');
  477. }
  478. if ($result < 0) {
  479. throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
  480. }
  481. return $result;
  482. }
  483. }