api_members.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. /* Copyright (C) 2016 Xebax Christy <xebax@wanadoo.fr>
  3. * Copyright (C) 2017 Regis Houssin <regis.houssin@capnetworks.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 <http://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. 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. 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. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')"
  81. * @return array Array of member objects
  82. *
  83. * @throws RestException
  84. */
  85. function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $typeid = '', $sqlfilters = '')
  86. {
  87. global $db, $conf;
  88. $obj_ret = array();
  89. if(! DolibarrApiAccess::$user->rights->adherent->lire) {
  90. throw new RestException(401);
  91. }
  92. $sql = "SELECT t.rowid";
  93. $sql.= " FROM ".MAIN_DB_PREFIX."adherent as t";
  94. $sql.= ' WHERE t.entity IN ('.getEntity('adherent').')';
  95. if (!empty($typeid))
  96. {
  97. $sql.= ' AND t.fk_adherent_type='.$typeid;
  98. }
  99. // Add sql filters
  100. if ($sqlfilters)
  101. {
  102. if (! DolibarrApi::_checkFilters($sqlfilters))
  103. {
  104. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  105. }
  106. $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  107. $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  108. }
  109. $sql.= $db->order($sortfield, $sortorder);
  110. if ($limit) {
  111. if ($page < 0)
  112. {
  113. $page = 0;
  114. }
  115. $offset = $limit * $page;
  116. $sql.= $db->plimit($limit + 1, $offset);
  117. }
  118. $result = $db->query($sql);
  119. if ($result)
  120. {
  121. $i=0;
  122. $num = $db->num_rows($result);
  123. $min = min($num, ($limit <= 0 ? $num : $limit));
  124. while ($i < $min)
  125. {
  126. $obj = $db->fetch_object($result);
  127. $member = new Adherent($this->db);
  128. if($member->fetch($obj->rowid)) {
  129. $obj_ret[] = $this->_cleanObjectDatas($member);
  130. }
  131. $i++;
  132. }
  133. }
  134. else {
  135. throw new RestException(503, 'Error when retrieve member list : '.$db->lasterror());
  136. }
  137. if( ! count($obj_ret)) {
  138. throw new RestException(404, 'No member found');
  139. }
  140. return $obj_ret;
  141. }
  142. /**
  143. * Create member object
  144. *
  145. * @param array $request_data Request data
  146. * @return int ID of member
  147. */
  148. function post($request_data = null)
  149. {
  150. if(! DolibarrApiAccess::$user->rights->adherent->creer) {
  151. throw new RestException(401);
  152. }
  153. // Check mandatory fields
  154. $result = $this->_validate($request_data);
  155. $member = new Adherent($this->db);
  156. foreach($request_data as $field => $value) {
  157. $member->$field = $value;
  158. }
  159. if ($member->create(DolibarrApiAccess::$user) < 0) {
  160. throw new RestException(500, 'Error creating member', array_merge(array($member->error), $member->errors));
  161. }
  162. return $member->id;
  163. }
  164. /**
  165. * Update member
  166. *
  167. * @param int $id ID of member to update
  168. * @param array $request_data Datas
  169. * @return int
  170. */
  171. function put($id, $request_data = null)
  172. {
  173. if(! DolibarrApiAccess::$user->rights->adherent->creer) {
  174. throw new RestException(401);
  175. }
  176. $member = new Adherent($this->db);
  177. $result = $member->fetch($id);
  178. if( ! $result ) {
  179. throw new RestException(404, 'member not found');
  180. }
  181. if( ! DolibarrApi::_checkAccessToResource('member',$member->id)) {
  182. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  183. }
  184. foreach($request_data as $field => $value) {
  185. if ($field == 'id') continue;
  186. // Process the status separately because it must be updated using
  187. // the validate() and resiliate() methods of the class Adherent.
  188. if ($field == 'statut') {
  189. if ($value == '0') {
  190. $result = $member->resiliate(DolibarrApiAccess::$user);
  191. if ($result < 0) {
  192. throw new RestException(500, 'Error when resiliating member: '.$member->error);
  193. }
  194. } else if ($value == '1') {
  195. $result = $member->validate(DolibarrApiAccess::$user);
  196. if ($result < 0) {
  197. throw new RestException(500, 'Error when validating member: '.$member->error);
  198. }
  199. }
  200. } else {
  201. $member->$field = $value;
  202. }
  203. }
  204. // If there is no error, update() returns the number of affected rows
  205. // so if the update is a no op, the return value is zero.
  206. if ($member->update(DolibarrApiAccess::$user) >= 0)
  207. {
  208. return $this->get($id);
  209. }
  210. else
  211. {
  212. throw new RestException(500, $member->error);
  213. }
  214. }
  215. /**
  216. * Delete member
  217. *
  218. * @param int $id member ID
  219. * @return array
  220. */
  221. function delete($id)
  222. {
  223. if(! DolibarrApiAccess::$user->rights->adherent->supprimer) {
  224. throw new RestException(401);
  225. }
  226. $member = new Adherent($this->db);
  227. $result = $member->fetch($id);
  228. if( ! $result ) {
  229. throw new RestException(404, 'member not found');
  230. }
  231. if( ! DolibarrApi::_checkAccessToResource('member',$member->id)) {
  232. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  233. }
  234. if (! $member->delete($member->id, DolibarrApiAccess::$user)) {
  235. throw new RestException(401,'error when deleting member');
  236. }
  237. return array(
  238. 'success' => array(
  239. 'code' => 200,
  240. 'message' => 'member deleted'
  241. )
  242. );
  243. }
  244. /**
  245. * Validate fields before creating an object
  246. *
  247. * @param array|null $data Data to validate
  248. * @return array
  249. *
  250. * @throws RestException
  251. */
  252. function _validate($data)
  253. {
  254. $member = array();
  255. foreach (Members::$FIELDS as $field) {
  256. if (!isset($data[$field]))
  257. throw new RestException(400, "$field field missing");
  258. $member[$field] = $data[$field];
  259. }
  260. return $member;
  261. }
  262. /**
  263. * Clean sensible object datas
  264. *
  265. * @param object $object Object to clean
  266. * @return array Array of cleaned object properties
  267. */
  268. function _cleanObjectDatas($object)
  269. {
  270. $object = parent::_cleanObjectDatas($object);
  271. // Remove the subscriptions because they are handled as a subresource.
  272. unset($object->subscriptions);
  273. unset($object->fk_incoterms);
  274. unset($object->libelle_incoterms);
  275. unset($object->location_incoterms);
  276. unset($object->fk_delivery_address);
  277. unset($object->shipping_method_id);
  278. unset($object->total_ht);
  279. unset($object->total_ttc);
  280. unset($object->total_tva);
  281. unset($object->total_localtax1);
  282. unset($object->total_localtax2);
  283. return $object;
  284. }
  285. /**
  286. * List subscriptions of a member
  287. *
  288. * Get a list of subscriptions
  289. *
  290. * @param int $id ID of member
  291. * @return array Array of subscription objects
  292. *
  293. * @throws RestException
  294. *
  295. * @url GET {id}/subscriptions
  296. */
  297. function getSubscriptions($id)
  298. {
  299. $obj_ret = array();
  300. if(! DolibarrApiAccess::$user->rights->adherent->cotisation->lire) {
  301. throw new RestException(401);
  302. }
  303. $member = new Adherent($this->db);
  304. $result = $member->fetch($id);
  305. if( ! $result ) {
  306. throw new RestException(404, 'member not found');
  307. }
  308. $obj_ret = array();
  309. foreach ($member->subscriptions as $subscription) {
  310. $obj_ret[] = $this->_cleanObjectDatas($subscription);
  311. }
  312. return $obj_ret;
  313. }
  314. /**
  315. * Add a subscription for a member
  316. *
  317. * @param int $id ID of member
  318. * @param int $start_date Start date {@from body} {@type timestamp}
  319. * @param int $end_date End date {@from body} {@type timestamp}
  320. * @param float $amount Amount (may be 0) {@from body}
  321. * @param string $label Label {@from body}
  322. * @return int ID of subscription
  323. *
  324. * @url POST {id}/subscriptions
  325. */
  326. function createSubscription($id, $start_date, $end_date, $amount, $label='')
  327. {
  328. if(! DolibarrApiAccess::$user->rights->adherent->cotisation->creer) {
  329. throw new RestException(401);
  330. }
  331. $member = new Adherent($this->db);
  332. $result = $member->fetch($id);
  333. if( ! $result ) {
  334. throw new RestException(404, 'member not found');
  335. }
  336. return $member->subscription($start_date, $amount, 0, '', $label, '', '', '', $end_date);
  337. }
  338. /**
  339. * Get categories for a member
  340. *
  341. * @param int $id ID of member
  342. * @param string $sortfield Sort field
  343. * @param string $sortorder Sort order
  344. * @param int $limit Limit for list
  345. * @param int $page Page number
  346. *
  347. * @return mixed
  348. *
  349. * @url GET {id}/categories
  350. */
  351. function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
  352. {
  353. if (! DolibarrApiAccess::$user->rights->categorie->lire) {
  354. throw new RestException(401);
  355. }
  356. $categories = new Categorie($this->db);
  357. $result = $categories->getListForItem($id, 'member', $sortfield, $sortorder, $limit, $page);
  358. if (empty($result)) {
  359. throw new RestException(404, 'No category found');
  360. }
  361. if ($result < 0) {
  362. throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
  363. }
  364. return $result;
  365. }
  366. }