api_subscriptions.class.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 <https://www.gnu.org/licenses/>.
  16. */
  17. use Luracast\Restler\RestException;
  18. require_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
  19. /**
  20. * API class for subscriptions
  21. *
  22. * @access protected
  23. * @class DolibarrApiAccess {@requires user,external}
  24. */
  25. class Subscriptions extends DolibarrApi
  26. {
  27. /**
  28. * @var array $FIELDS Mandatory fields, checked when create and update object
  29. */
  30. static $FIELDS = array(
  31. 'fk_adherent',
  32. 'dateh',
  33. 'datef',
  34. 'amount',
  35. );
  36. /**
  37. * Constructor
  38. */
  39. public function __construct()
  40. {
  41. global $db, $conf;
  42. $this->db = $db;
  43. }
  44. /**
  45. * Get properties of a subscription object
  46. *
  47. * Return an array with subscription informations
  48. *
  49. * @param int $id ID of subscription
  50. * @return array|mixed data without useless information
  51. *
  52. * @throws RestException
  53. */
  54. public function get($id)
  55. {
  56. if(! DolibarrApiAccess::$user->rights->adherent->cotisation->lire) {
  57. throw new RestException(401);
  58. }
  59. $subscription = new Subscription($this->db);
  60. $result = $subscription->fetch($id);
  61. if( ! $result ) {
  62. throw new RestException(404, 'Subscription not found');
  63. }
  64. return $this->_cleanObjectDatas($subscription);
  65. }
  66. /**
  67. * List subscriptions
  68. *
  69. * Get a list of subscriptions
  70. *
  71. * @param string $sortfield Sort field
  72. * @param string $sortorder Sort order
  73. * @param int $limit Limit for list
  74. * @param int $page Page number
  75. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.import_key:<:'20160101')"
  76. * @return array Array of subscription objects
  77. *
  78. * @throws RestException
  79. */
  80. public function index($sortfield = "dateadh", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
  81. {
  82. global $db, $conf;
  83. $obj_ret = array();
  84. if(! DolibarrApiAccess::$user->rights->adherent->cotisation->lire) {
  85. throw new RestException(401);
  86. }
  87. $sql = "SELECT rowid";
  88. $sql.= " FROM ".MAIN_DB_PREFIX."subscription as t";
  89. $sql.= ' WHERE 1 = 1';
  90. // Add sql filters
  91. if ($sqlfilters)
  92. {
  93. if (! DolibarrApi::_checkFilters($sqlfilters))
  94. {
  95. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  96. }
  97. $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  98. $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  99. }
  100. $sql.= $db->order($sortfield, $sortorder);
  101. if ($limit) {
  102. if ($page < 0)
  103. {
  104. $page = 0;
  105. }
  106. $offset = $limit * $page;
  107. $sql.= $db->plimit($limit + 1, $offset);
  108. }
  109. $result = $db->query($sql);
  110. if ($result)
  111. {
  112. $i=0;
  113. $num = $db->num_rows($result);
  114. while ($i < min($limit, $num))
  115. {
  116. $obj = $db->fetch_object($result);
  117. $subscription = new Subscription($this->db);
  118. if($subscription->fetch($obj->rowid)) {
  119. $obj_ret[] = $this->_cleanObjectDatas($subscription);
  120. }
  121. $i++;
  122. }
  123. }
  124. else {
  125. throw new RestException(503, 'Error when retrieve subscription list : '.$db->lasterror());
  126. }
  127. if( ! count($obj_ret)) {
  128. throw new RestException(404, 'No Subscription found');
  129. }
  130. return $obj_ret;
  131. }
  132. /**
  133. * Create subscription object
  134. *
  135. * @param array $request_data Request data
  136. * @return int ID of subscription
  137. */
  138. public function post($request_data = null)
  139. {
  140. if(! DolibarrApiAccess::$user->rights->adherent->cotisation->creer) {
  141. throw new RestException(401);
  142. }
  143. // Check mandatory fields
  144. $result = $this->_validate($request_data);
  145. $subscription = new Subscription($this->db);
  146. foreach($request_data as $field => $value) {
  147. $subscription->$field = $value;
  148. }
  149. if ($subscription->create(DolibarrApiAccess::$user) < 0) {
  150. throw new RestException(500, 'Error when creating subscription', array_merge(array($subscription->error), $subscription->errors));
  151. }
  152. return $subscription->id;
  153. }
  154. /**
  155. * Update subscription
  156. *
  157. * @param int $id ID of subscription to update
  158. * @param array $request_data Datas
  159. * @return int
  160. */
  161. public function put($id, $request_data = null)
  162. {
  163. if(! DolibarrApiAccess::$user->rights->adherent->creer) {
  164. throw new RestException(401);
  165. }
  166. $subscription = new Subscription($this->db);
  167. $result = $subscription->fetch($id);
  168. if( ! $result ) {
  169. throw new RestException(404, 'Subscription not found');
  170. }
  171. foreach($request_data as $field => $value) {
  172. if ($field == 'id') continue;
  173. $subscription->$field = $value;
  174. }
  175. if ($subscription->update(DolibarrApiAccess::$user) > 0)
  176. {
  177. return $this->get($id);
  178. }
  179. else
  180. {
  181. throw new RestException(500, $subscription->error);
  182. }
  183. }
  184. /**
  185. * Delete subscription
  186. *
  187. * @param int $id ID of subscription to delete
  188. * @return array
  189. */
  190. public function delete($id)
  191. {
  192. // The right to delete a subscription comes with the right to create one.
  193. if(! DolibarrApiAccess::$user->rights->adherent->cotisation->creer) {
  194. throw new RestException(401);
  195. }
  196. $subscription = new Subscription($this->db);
  197. $result = $subscription->fetch($id);
  198. if( ! $result ) {
  199. throw new RestException(404, 'Subscription not found');
  200. }
  201. if (! $subscription->delete(DolibarrApiAccess::$user)) {
  202. throw new RestException(401, 'error when deleting subscription');
  203. }
  204. return array(
  205. 'success' => array(
  206. 'code' => 200,
  207. 'message' => 'subscription deleted'
  208. )
  209. );
  210. }
  211. /**
  212. * Validate fields before creating an object
  213. *
  214. * @param array|null $data Data to validate
  215. * @return array
  216. *
  217. * @throws RestException
  218. */
  219. private function _validate($data)
  220. {
  221. $subscription = array();
  222. foreach (Subscriptions::$FIELDS as $field) {
  223. if (!isset($data[$field]))
  224. throw new RestException(400, "$field field missing");
  225. $subscription[$field] = $data[$field];
  226. }
  227. return $subscription;
  228. }
  229. }