api_members.class.php 13 KB

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