api_members.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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->rights->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->rights->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->rights->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->rights->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->rights->adherent->lire) {
  196. throw new RestException(401);
  197. }
  198. $sql = "SELECT t.rowid";
  199. $sql .= " FROM ".MAIN_DB_PREFIX."adherent as t";
  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. if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
  216. throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
  217. }
  218. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  219. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  220. }
  221. $sql .= $this->db->order($sortfield, $sortorder);
  222. if ($limit) {
  223. if ($page < 0) {
  224. $page = 0;
  225. }
  226. $offset = $limit * $page;
  227. $sql .= $this->db->plimit($limit + 1, $offset);
  228. }
  229. $result = $this->db->query($sql);
  230. if ($result) {
  231. $i = 0;
  232. $num = $this->db->num_rows($result);
  233. $min = min($num, ($limit <= 0 ? $num : $limit));
  234. while ($i < $min) {
  235. $obj = $this->db->fetch_object($result);
  236. $member = new Adherent($this->db);
  237. if ($member->fetch($obj->rowid)) {
  238. $obj_ret[] = $this->_cleanObjectDatas($member);
  239. }
  240. $i++;
  241. }
  242. } else {
  243. throw new RestException(503, 'Error when retrieve member list : '.$this->db->lasterror());
  244. }
  245. if (!count($obj_ret)) {
  246. throw new RestException(404, 'No member found');
  247. }
  248. return $obj_ret;
  249. }
  250. /**
  251. * Create member object
  252. *
  253. * @param array $request_data Request data
  254. * @return int ID of member
  255. */
  256. public function post($request_data = null)
  257. {
  258. if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
  259. throw new RestException(401);
  260. }
  261. // Check mandatory fields
  262. $result = $this->_validate($request_data);
  263. $member = new Adherent($this->db);
  264. foreach ($request_data as $field => $value) {
  265. $member->$field = $value;
  266. }
  267. if ($member->create(DolibarrApiAccess::$user) < 0) {
  268. throw new RestException(500, 'Error creating member', array_merge(array($member->error), $member->errors));
  269. }
  270. return $member->id;
  271. }
  272. /**
  273. * Update member
  274. *
  275. * @param int $id ID of member to update
  276. * @param array $request_data Datas
  277. * @return Object Updated object
  278. */
  279. public function put($id, $request_data = null)
  280. {
  281. if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
  282. throw new RestException(401);
  283. }
  284. $member = new Adherent($this->db);
  285. $result = $member->fetch($id);
  286. if (!$result) {
  287. throw new RestException(404, 'member not found');
  288. }
  289. if (!DolibarrApi::_checkAccessToResource('member', $member->id)) {
  290. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  291. }
  292. foreach ($request_data as $field => $value) {
  293. if ($field == 'id') {
  294. continue;
  295. }
  296. // Process the status separately because it must be updated using
  297. // the validate(), resiliate() and exclude() methods of the class Adherent.
  298. if ($field == 'statut') {
  299. if ($value == '0') {
  300. $result = $member->resiliate(DolibarrApiAccess::$user);
  301. if ($result < 0) {
  302. throw new RestException(500, 'Error when resiliating member: '.$member->error);
  303. }
  304. } elseif ($value == '1') {
  305. $result = $member->validate(DolibarrApiAccess::$user);
  306. if ($result < 0) {
  307. throw new RestException(500, 'Error when validating member: '.$member->error);
  308. }
  309. } elseif ($value == '-2') {
  310. $result = $member->exclude(DolibarrApiAccess::$user);
  311. if ($result < 0) {
  312. throw new RestException(500, 'Error when excluding member: '.$member->error);
  313. }
  314. }
  315. } else {
  316. $member->$field = $value;
  317. }
  318. }
  319. // If there is no error, update() returns the number of affected rows
  320. // so if the update is a no op, the return value is zero.
  321. if ($member->update(DolibarrApiAccess::$user) >= 0) {
  322. return $this->get($id);
  323. } else {
  324. throw new RestException(500, 'Error when updating member: '.$member->error);
  325. }
  326. }
  327. /**
  328. * Delete member
  329. *
  330. * @param int $id member ID
  331. * @return array
  332. */
  333. public function delete($id)
  334. {
  335. if (!DolibarrApiAccess::$user->rights->adherent->supprimer) {
  336. throw new RestException(401);
  337. }
  338. $member = new Adherent($this->db);
  339. $result = $member->fetch($id);
  340. if (!$result) {
  341. throw new RestException(404, 'member not found');
  342. }
  343. if (!DolibarrApi::_checkAccessToResource('member', $member->id)) {
  344. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  345. }
  346. if (!$member->delete($member->id, DolibarrApiAccess::$user)) {
  347. throw new RestException(401, 'error when deleting member');
  348. }
  349. return array(
  350. 'success' => array(
  351. 'code' => 200,
  352. 'message' => 'member deleted'
  353. )
  354. );
  355. }
  356. /**
  357. * Validate fields before creating an object
  358. *
  359. * @param array|null $data Data to validate
  360. * @return array
  361. *
  362. * @throws RestException
  363. */
  364. private function _validate($data)
  365. {
  366. $member = array();
  367. foreach (Members::$FIELDS as $field) {
  368. if (!isset($data[$field])) {
  369. throw new RestException(400, "$field field missing");
  370. }
  371. $member[$field] = $data[$field];
  372. }
  373. return $member;
  374. }
  375. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  376. /**
  377. * Clean sensible object datas
  378. *
  379. * @param Object $object Object to clean
  380. * @return Object Object with cleaned properties
  381. */
  382. protected function _cleanObjectDatas($object)
  383. {
  384. // phpcs:enable
  385. $object = parent::_cleanObjectDatas($object);
  386. // Remove the subscriptions because they are handled as a subresource.
  387. unset($object->subscriptions);
  388. unset($object->fk_incoterms);
  389. unset($object->label_incoterms);
  390. unset($object->location_incoterms);
  391. unset($object->fk_delivery_address);
  392. unset($object->shipping_method_id);
  393. unset($object->total_ht);
  394. unset($object->total_ttc);
  395. unset($object->total_tva);
  396. unset($object->total_localtax1);
  397. unset($object->total_localtax2);
  398. return $object;
  399. }
  400. /**
  401. * List subscriptions of a member
  402. *
  403. * Get a list of subscriptions
  404. *
  405. * @param int $id ID of member
  406. * @return array Array of subscription objects
  407. *
  408. * @throws RestException
  409. *
  410. * @url GET {id}/subscriptions
  411. */
  412. public function getSubscriptions($id)
  413. {
  414. $obj_ret = array();
  415. if (!DolibarrApiAccess::$user->rights->adherent->cotisation->lire) {
  416. throw new RestException(401);
  417. }
  418. $member = new Adherent($this->db);
  419. $result = $member->fetch($id);
  420. if (!$result) {
  421. throw new RestException(404, 'member not found');
  422. }
  423. $obj_ret = array();
  424. foreach ($member->subscriptions as $subscription) {
  425. $obj_ret[] = $this->_cleanObjectDatas($subscription);
  426. }
  427. return $obj_ret;
  428. }
  429. /**
  430. * Add a subscription for a member
  431. *
  432. * @param int $id ID of member
  433. * @param string $start_date Start date {@from body} {@type timestamp}
  434. * @param string $end_date End date {@from body} {@type timestamp}
  435. * @param float $amount Amount (may be 0) {@from body}
  436. * @param string $label Label {@from body}
  437. * @return int ID of subscription
  438. *
  439. * @url POST {id}/subscriptions
  440. */
  441. public function createSubscription($id, $start_date, $end_date, $amount, $label = '')
  442. {
  443. if (!DolibarrApiAccess::$user->rights->adherent->cotisation->creer) {
  444. throw new RestException(401);
  445. }
  446. $member = new Adherent($this->db);
  447. $result = $member->fetch($id);
  448. if (!$result) {
  449. throw new RestException(404, 'member not found');
  450. }
  451. return $member->subscription($start_date, $amount, 0, '', $label, '', '', '', $end_date);
  452. }
  453. /**
  454. * Get categories for a member
  455. *
  456. * @param int $id ID of member
  457. * @param string $sortfield Sort field
  458. * @param string $sortorder Sort order
  459. * @param int $limit Limit for list
  460. * @param int $page Page number
  461. *
  462. * @return mixed
  463. *
  464. * @url GET {id}/categories
  465. */
  466. public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
  467. {
  468. if (!DolibarrApiAccess::$user->rights->categorie->lire) {
  469. throw new RestException(401);
  470. }
  471. $categories = new Categorie($this->db);
  472. $result = $categories->getListForItem($id, 'member', $sortfield, $sortorder, $limit, $page);
  473. if (empty($result)) {
  474. throw new RestException(404, 'No category found');
  475. }
  476. if ($result < 0) {
  477. throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
  478. }
  479. return $result;
  480. }
  481. }