api_subscriptions.class.php 6.5 KB

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