api_subscriptions.class.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 Object with cleaned properties
  51. *
  52. * @throws RestException
  53. */
  54. public function get($id)
  55. {
  56. if (!DolibarrApiAccess::$user->hasRight('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. * @param string $properties Restrict the data returned to theses properties. Ignored if empty. Comma separated list of properties names
  77. * @return array Array of subscription objects
  78. *
  79. * @throws RestException
  80. */
  81. public function index($sortfield = "dateadh", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
  82. {
  83. global $conf;
  84. $obj_ret = array();
  85. if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'lire')) {
  86. throw new RestException(401);
  87. }
  88. $sql = "SELECT rowid";
  89. $sql .= " FROM ".MAIN_DB_PREFIX."subscription as t";
  90. $sql .= ' WHERE 1 = 1';
  91. // Add sql filters
  92. if ($sqlfilters) {
  93. $errormessage = '';
  94. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  95. if ($errormessage) {
  96. throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
  97. }
  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->_filterObjectProperties($this->_cleanObjectDatas($subscription), $properties);
  116. }
  117. $i++;
  118. }
  119. } else {
  120. throw new RestException(503, 'Error when retrieve subscription list : '.$this->db->lasterror());
  121. }
  122. return $obj_ret;
  123. }
  124. /**
  125. * Create subscription object
  126. *
  127. * @param array $request_data Request data
  128. * @return int ID of subscription
  129. */
  130. public function post($request_data = null)
  131. {
  132. if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'creer')) {
  133. throw new RestException(401);
  134. }
  135. // Check mandatory fields
  136. $result = $this->_validate($request_data);
  137. $subscription = new Subscription($this->db);
  138. foreach ($request_data as $field => $value) {
  139. if ($field === 'caller') {
  140. // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller
  141. $subscription->context['caller'] = $request_data['caller'];
  142. continue;
  143. }
  144. $subscription->$field = $value;
  145. }
  146. if ($subscription->create(DolibarrApiAccess::$user) < 0) {
  147. throw new RestException(500, 'Error when creating contribution', array_merge(array($subscription->error), $subscription->errors));
  148. }
  149. return $subscription->id;
  150. }
  151. /**
  152. * Update subscription
  153. *
  154. * @param int $id ID of subscription to update
  155. * @param array $request_data Datas
  156. * @return Object
  157. */
  158. public function put($id, $request_data = null)
  159. {
  160. if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
  161. throw new RestException(401);
  162. }
  163. $subscription = new Subscription($this->db);
  164. $result = $subscription->fetch($id);
  165. if (!$result) {
  166. throw new RestException(404, 'Subscription not found');
  167. }
  168. foreach ($request_data as $field => $value) {
  169. if ($field == 'id') {
  170. continue;
  171. }
  172. if ($field === 'caller') {
  173. // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller
  174. $subscription->context['caller'] = $request_data['caller'];
  175. continue;
  176. }
  177. $subscription->$field = $value;
  178. }
  179. if ($subscription->update(DolibarrApiAccess::$user) > 0) {
  180. return $this->get($id);
  181. } else {
  182. throw new RestException(500, 'Error when updating contribution: '.$subscription->error);
  183. }
  184. }
  185. /**
  186. * Delete subscription
  187. *
  188. * @param int $id ID of subscription to delete
  189. * @return array
  190. */
  191. public function delete($id)
  192. {
  193. // The right to delete a subscription comes with the right to create one.
  194. if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'creer')) {
  195. throw new RestException(401);
  196. }
  197. $subscription = new Subscription($this->db);
  198. $result = $subscription->fetch($id);
  199. if (!$result) {
  200. throw new RestException(404, 'Subscription not found');
  201. }
  202. $res = $subscription->delete(DolibarrApiAccess::$user);
  203. if ($res < 0) {
  204. throw new RestException(500, "Can't delete, error occurs");
  205. } elseif ($res == 0) {
  206. throw new RestException(409, "Can't delete, that product is probably used");
  207. }
  208. return array(
  209. 'success' => array(
  210. 'code' => 200,
  211. 'message' => 'Subscription deleted'
  212. )
  213. );
  214. }
  215. /**
  216. * Validate fields before creating an object
  217. *
  218. * @param array|null $data Data to validate
  219. * @return array
  220. *
  221. * @throws RestException
  222. */
  223. private function _validate($data)
  224. {
  225. $subscription = array();
  226. foreach (Subscriptions::$FIELDS as $field) {
  227. if (!isset($data[$field])) {
  228. throw new RestException(400, "$field field missing");
  229. }
  230. $subscription[$field] = $data[$field];
  231. }
  232. return $subscription;
  233. }
  234. }