api_members.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. /* Copyright (C) 2016 Xebax Christy <xebax@wanadoo.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 <http://www.gnu.org/licenses/>.
  16. */
  17. use Luracast\Restler\RestException;
  18. require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
  19. require_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
  20. /**
  21. * API class for members
  22. *
  23. * @access protected
  24. * @class DolibarrApiAccess {@requires user,external}
  25. */
  26. class Members extends DolibarrApi
  27. {
  28. /**
  29. * @var array $FIELDS Mandatory fields, checked when create and update object
  30. */
  31. static $FIELDS = array(
  32. 'morphy',
  33. 'typeid'
  34. );
  35. /**
  36. * Constructor
  37. */
  38. function __construct()
  39. {
  40. global $db, $conf;
  41. $this->db = $db;
  42. }
  43. /**
  44. * Get properties of a member object
  45. *
  46. * Return an array with member informations
  47. *
  48. * @param int $id ID of member
  49. * @return array|mixed data without useless information
  50. *
  51. * @throws RestException
  52. */
  53. function get($id)
  54. {
  55. if(! DolibarrApiAccess::$user->rights->adherent->lire) {
  56. throw new RestException(401);
  57. }
  58. $member = new Adherent($this->db);
  59. $result = $member->fetch($id);
  60. if( ! $result ) {
  61. throw new RestException(404, 'member not found');
  62. }
  63. if( ! DolibarrApi::_checkAccessToResource('adherent',$member->id)) {
  64. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  65. }
  66. return $this->_cleanObjectDatas($member);
  67. }
  68. /**
  69. * List members
  70. *
  71. * Get a list of members
  72. *
  73. * @param string $sortfield Sort field
  74. * @param string $sortorder Sort order
  75. * @param int $limit Limit for list
  76. * @param int $page Page number
  77. * @param string $typeid ID of the type of member
  78. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')"
  79. * @return array Array of member objects
  80. *
  81. * @throws RestException
  82. */
  83. function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 0, $page = 0, $typeid = '', $sqlfilters = '') {
  84. global $db, $conf;
  85. $obj_ret = array();
  86. if(! DolibarrApiAccess::$user->rights->adherent->lire) {
  87. throw new RestException(401);
  88. }
  89. $sql = "SELECT t.rowid";
  90. $sql.= " FROM ".MAIN_DB_PREFIX."adherent as t";
  91. $sql.= ' WHERE t.entity IN ('.getEntity('adherent').')';
  92. if (!empty($typeid))
  93. {
  94. $sql.= ' AND t.fk_adherent_type='.$typeid;
  95. }
  96. // Add sql filters
  97. if ($sqlfilters)
  98. {
  99. if (! DolibarrApi::_checkFilters($sqlfilters))
  100. {
  101. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  102. }
  103. $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  104. $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  105. }
  106. $sql.= $db->order($sortfield, $sortorder);
  107. if ($limit) {
  108. if ($page < 0)
  109. {
  110. $page = 0;
  111. }
  112. $offset = $limit * $page;
  113. $sql.= $db->plimit($limit + 1, $offset);
  114. }
  115. $result = $db->query($sql);
  116. if ($result)
  117. {
  118. $i=0;
  119. $num = $db->num_rows($result);
  120. $min = min($num, ($limit <= 0 ? $num : $limit));
  121. while ($i < $min)
  122. {
  123. $obj = $db->fetch_object($result);
  124. $member = new Adherent($this->db);
  125. if($member->fetch($obj->rowid)) {
  126. $obj_ret[] = $this->_cleanObjectDatas($member);
  127. }
  128. $i++;
  129. }
  130. }
  131. else {
  132. throw new RestException(503, 'Error when retrieve member list : '.$db->lasterror());
  133. }
  134. if( ! count($obj_ret)) {
  135. throw new RestException(404, 'No member found');
  136. }
  137. return $obj_ret;
  138. }
  139. /**
  140. * Create member object
  141. *
  142. * @param array $request_data Request data
  143. * @return int ID of member
  144. */
  145. function post($request_data = null)
  146. {
  147. if(! DolibarrApiAccess::$user->rights->adherent->creer) {
  148. throw new RestException(401);
  149. }
  150. // Check mandatory fields
  151. $result = $this->_validate($request_data);
  152. $member = new Adherent($this->db);
  153. foreach($request_data as $field => $value) {
  154. $member->$field = $value;
  155. }
  156. if ($member->create(DolibarrApiAccess::$user) < 0) {
  157. throw new RestException(500, 'Error creating member', array_merge(array($member->error), $member->errors));
  158. }
  159. return $member->id;
  160. }
  161. /**
  162. * Update member
  163. *
  164. * @param int $id ID of member to update
  165. * @param array $request_data Datas
  166. * @return int
  167. */
  168. function put($id, $request_data = null)
  169. {
  170. if(! DolibarrApiAccess::$user->rights->adherent->creer) {
  171. throw new RestException(401);
  172. }
  173. $member = new Adherent($this->db);
  174. $result = $member->fetch($id);
  175. if( ! $result ) {
  176. throw new RestException(404, 'member not found');
  177. }
  178. if( ! DolibarrApi::_checkAccessToResource('member',$member->id)) {
  179. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  180. }
  181. foreach($request_data as $field => $value) {
  182. if ($field == 'id') continue;
  183. // Process the status separately because it must be updated using
  184. // the validate() and resiliate() methods of the class Adherent.
  185. if ($field == 'statut') {
  186. if ($value == '0') {
  187. $result = $member->resiliate(DolibarrApiAccess::$user);
  188. if ($result < 0) {
  189. throw new RestException(500, 'Error when resiliating member: '.$member->error);
  190. }
  191. } else if ($value == '1') {
  192. $result = $member->validate(DolibarrApiAccess::$user);
  193. if ($result < 0) {
  194. throw new RestException(500, 'Error when validating member: '.$member->error);
  195. }
  196. }
  197. } else {
  198. $member->$field = $value;
  199. }
  200. }
  201. // If there is no error, update() returns the number of affected rows
  202. // so if the update is a no op, the return value is zero.
  203. if($member->update(DolibarrApiAccess::$user) >= 0)
  204. return $this->get($id);
  205. return false;
  206. }
  207. /**
  208. * Delete member
  209. *
  210. * @param int $id member ID
  211. * @return array
  212. */
  213. function delete($id)
  214. {
  215. if(! DolibarrApiAccess::$user->rights->adherent->supprimer) {
  216. throw new RestException(401);
  217. }
  218. $member = new Adherent($this->db);
  219. $result = $member->fetch($id);
  220. if( ! $result ) {
  221. throw new RestException(404, 'member not found');
  222. }
  223. if( ! DolibarrApi::_checkAccessToResource('member',$member->id)) {
  224. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  225. }
  226. // The Adherent::delete() method uses the global variable $user.
  227. global $user;
  228. $user = DolibarrApiAccess::$user;
  229. if (! $member->delete($member->id)) {
  230. throw new RestException(401,'error when deleting member');
  231. }
  232. return array(
  233. 'success' => array(
  234. 'code' => 200,
  235. 'message' => 'member deleted'
  236. )
  237. );
  238. }
  239. /**
  240. * Validate fields before creating an object
  241. *
  242. * @param array|null $data Data to validate
  243. * @return array
  244. *
  245. * @throws RestException
  246. */
  247. function _validate($data)
  248. {
  249. $member = array();
  250. foreach (Members::$FIELDS as $field) {
  251. if (!isset($data[$field]))
  252. throw new RestException(400, "$field field missing");
  253. $member[$field] = $data[$field];
  254. }
  255. return $member;
  256. }
  257. /**
  258. * Clean sensible object datas
  259. *
  260. * @param object $object Object to clean
  261. * @return array Array of cleaned object properties
  262. */
  263. function _cleanObjectDatas($object) {
  264. $object = parent::_cleanObjectDatas($object);
  265. // Remove the subscriptions because they are handled as a subresource.
  266. unset($object->subscriptions);
  267. return $object;
  268. }
  269. /**
  270. * List subscriptions of a member
  271. *
  272. * Get a list of subscriptions
  273. *
  274. * @param int $id ID of member
  275. * @return array Array of subscription objects
  276. *
  277. * @throws RestException
  278. *
  279. * @url GET {id}/subscriptions
  280. */
  281. function getSubscriptions($id)
  282. {
  283. $obj_ret = array();
  284. if(! DolibarrApiAccess::$user->rights->adherent->cotisation->lire) {
  285. throw new RestException(401);
  286. }
  287. $member = new Adherent($this->db);
  288. $result = $member->fetch($id);
  289. if( ! $result ) {
  290. throw new RestException(404, 'member not found');
  291. }
  292. $obj_ret = array();
  293. foreach ($member->subscriptions as $subscription) {
  294. $obj_ret[] = $this->_cleanObjectDatas($subscription);
  295. }
  296. return $obj_ret;
  297. }
  298. /**
  299. * Add a subscription for a member
  300. *
  301. * @param int $id ID of member
  302. * @param int $start_date Start date {@from body} {@type timestamp}
  303. * @param int $end_date End date {@from body} {@type timestamp}
  304. * @param float $amount Amount (may be 0) {@from body}
  305. * @param string $label Label {@from body}
  306. * @return int ID of subscription
  307. *
  308. * @url POST {id}/subscriptions
  309. */
  310. function createSubscription($id, $start_date, $end_date, $amount, $label='')
  311. {
  312. if(! DolibarrApiAccess::$user->rights->adherent->cotisation->creer) {
  313. throw new RestException(401);
  314. }
  315. $member = new Adherent($this->db);
  316. $result = $member->fetch($id);
  317. if( ! $result ) {
  318. throw new RestException(404, 'member not found');
  319. }
  320. return $member->subscription($start_date, $amount, 0, '', $label, '', '', '', $end_date);
  321. }
  322. }