api_subscriptions.class.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. public 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 Object 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 $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. $errormessage = '';
  93. if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
  94. throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
  95. }
  96. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  97. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  98. }
  99. $sql .= $this->db->order($sortfield, $sortorder);
  100. if ($limit) {
  101. if ($page < 0) {
  102. $page = 0;
  103. }
  104. $offset = $limit * $page;
  105. $sql .= $this->db->plimit($limit + 1, $offset);
  106. }
  107. $result = $this->db->query($sql);
  108. if ($result) {
  109. $i = 0;
  110. $num = $this->db->num_rows($result);
  111. while ($i < min($limit, $num)) {
  112. $obj = $this->db->fetch_object($result);
  113. $subscription = new Subscription($this->db);
  114. if ($subscription->fetch($obj->rowid)) {
  115. $obj_ret[] = $this->_cleanObjectDatas($subscription);
  116. }
  117. $i++;
  118. }
  119. } else {
  120. throw new RestException(503, 'Error when retrieve subscription list : '.$this->db->lasterror());
  121. }
  122. if (!count($obj_ret)) {
  123. throw new RestException(404, 'No Subscription found');
  124. }
  125. return $obj_ret;
  126. }
  127. /**
  128. * Create subscription object
  129. *
  130. * @param array $request_data Request data
  131. * @return int ID of subscription
  132. */
  133. public function post($request_data = null)
  134. {
  135. if (!DolibarrApiAccess::$user->rights->adherent->cotisation->creer) {
  136. throw new RestException(401);
  137. }
  138. // Check mandatory fields
  139. $result = $this->_validate($request_data);
  140. $subscription = new Subscription($this->db);
  141. foreach ($request_data as $field => $value) {
  142. $subscription->$field = $value;
  143. }
  144. if ($subscription->create(DolibarrApiAccess::$user) < 0) {
  145. throw new RestException(500, 'Error when creating contribution', array_merge(array($subscription->error), $subscription->errors));
  146. }
  147. return $subscription->id;
  148. }
  149. /**
  150. * Update subscription
  151. *
  152. * @param int $id ID of subscription to update
  153. * @param array $request_data Datas
  154. * @return Object
  155. */
  156. public function put($id, $request_data = null)
  157. {
  158. if (!DolibarrApiAccess::$user->rights->adherent->creer) {
  159. throw new RestException(401);
  160. }
  161. $subscription = new Subscription($this->db);
  162. $result = $subscription->fetch($id);
  163. if (!$result) {
  164. throw new RestException(404, 'Subscription not found');
  165. }
  166. foreach ($request_data as $field => $value) {
  167. if ($field == 'id') {
  168. continue;
  169. }
  170. $subscription->$field = $value;
  171. }
  172. if ($subscription->update(DolibarrApiAccess::$user) > 0) {
  173. return $this->get($id);
  174. } else {
  175. throw new RestException(500, 'Error when updating contribution: '.$subscription->error);
  176. }
  177. }
  178. /**
  179. * Delete subscription
  180. *
  181. * @param int $id ID of subscription to delete
  182. * @return array
  183. */
  184. public function delete($id)
  185. {
  186. // The right to delete a subscription comes with the right to create one.
  187. if (!DolibarrApiAccess::$user->rights->adherent->cotisation->creer) {
  188. throw new RestException(401);
  189. }
  190. $subscription = new Subscription($this->db);
  191. $result = $subscription->fetch($id);
  192. if (!$result) {
  193. throw new RestException(404, 'Subscription not found');
  194. }
  195. $res = $subscription->delete(DolibarrApiAccess::$user);
  196. if ($res < 0) {
  197. throw new RestException(500, "Can't delete, error occurs");
  198. } elseif ($res == 0) {
  199. throw new RestException(409, "Can't delete, that product is probably used");
  200. }
  201. return array(
  202. 'success' => array(
  203. 'code' => 200,
  204. 'message' => 'Subscription deleted'
  205. )
  206. );
  207. }
  208. /**
  209. * Validate fields before creating an object
  210. *
  211. * @param array|null $data Data to validate
  212. * @return array
  213. *
  214. * @throws RestException
  215. */
  216. private function _validate($data)
  217. {
  218. $subscription = array();
  219. foreach (Subscriptions::$FIELDS as $field) {
  220. if (!isset($data[$field])) {
  221. throw new RestException(400, "$field field missing");
  222. }
  223. $subscription[$field] = $data[$field];
  224. }
  225. return $subscription;
  226. }
  227. }