dolibarr 21.0.0-alpha
api_subscriptions.class.php
1<?php
2/* Copyright (C) 2016 Xebax Christy <xebax@wanadoo.fr>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.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 <https://www.gnu.org/licenses/>.
17 */
18
19use Luracast\Restler\RestException;
20
21require_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
22
30{
34 public static $FIELDS = array(
35 'fk_adherent',
36 'dateh',
37 'datef',
38 'amount',
39 );
40
44 public function __construct()
45 {
46 global $db, $conf;
47 $this->db = $db;
48 }
49
61 public function get($id)
62 {
63 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'lire')) {
64 throw new RestException(403);
65 }
66
67 $subscription = new Subscription($this->db);
68 $result = $subscription->fetch($id);
69 if (!$result) {
70 throw new RestException(404, 'Subscription not found');
71 }
72
73 return $this->_cleanObjectDatas($subscription);
74 }
75
96 public function index($sortfield = "dateadh", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
97 {
98 global $conf;
99
100 $obj_ret = array();
101
102 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'lire')) {
103 throw new RestException(403);
104 }
105
106 $sql = "SELECT rowid";
107 $sql .= " FROM ".MAIN_DB_PREFIX."subscription as t";
108 $sql .= ' WHERE 1 = 1';
109 // Add sql filters
110 if ($sqlfilters) {
111 $errormessage = '';
112 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
113 if ($errormessage) {
114 throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
115 }
116 }
117
118 //this query will return total orders with the filters given
119 $sqlTotals = str_replace('SELECT rowid', 'SELECT count(rowid) as total', $sql);
120
121 $sql .= $this->db->order($sortfield, $sortorder);
122 if ($limit) {
123 if ($page < 0) {
124 $page = 0;
125 }
126 $offset = $limit * $page;
127
128 $sql .= $this->db->plimit($limit + 1, $offset);
129 }
130
131 $result = $this->db->query($sql);
132 if ($result) {
133 $i = 0;
134 $num = $this->db->num_rows($result);
135 while ($i < min($limit, $num)) {
136 $obj = $this->db->fetch_object($result);
137 $subscription = new Subscription($this->db);
138 if ($subscription->fetch($obj->rowid)) {
139 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($subscription), $properties);
140 }
141 $i++;
142 }
143 } else {
144 throw new RestException(503, 'Error when retrieve subscription list : '.$this->db->lasterror());
145 }
146
147 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
148 if ($pagination_data) {
149 $totalsResult = $this->db->query($sqlTotals);
150 $total = $this->db->fetch_object($totalsResult)->total;
151
152 $tmp = $obj_ret;
153 $obj_ret = [];
154
155 $obj_ret['data'] = $tmp;
156 $obj_ret['pagination'] = [
157 'total' => (int) $total,
158 'page' => $page, //count starts from 0
159 'page_count' => ceil((int) $total / $limit),
160 'limit' => $limit
161 ];
162 }
163
164 return $obj_ret;
165 }
166
178 public function post($request_data = null)
179 {
180 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'creer')) {
181 throw new RestException(403);
182 }
183 // Check mandatory fields
184 $result = $this->_validate($request_data);
185
186 $subscription = new Subscription($this->db);
187 foreach ($request_data as $field => $value) {
188 if ($field === 'caller') {
189 // 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 with the caller
190 $subscription->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
191 continue;
192 }
193
194 $subscription->$field = $this->_checkValForAPI($field, $value, $subscription);
195 }
196 if ($subscription->create(DolibarrApiAccess::$user) < 0) {
197 throw new RestException(500, 'Error when creating subscription', array_merge(array($subscription->error), $subscription->errors));
198 }
199 return $subscription->id;
200 }
201
215 public function put($id, $request_data = null)
216 {
217 if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
218 throw new RestException(403);
219 }
220
221 $subscription = new Subscription($this->db);
222 $result = $subscription->fetch($id);
223 if (!$result) {
224 throw new RestException(404, 'Subscription not found');
225 }
226
227 foreach ($request_data as $field => $value) {
228 if ($field == 'id') {
229 continue;
230 }
231 if ($field === 'caller') {
232 // 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 with the caller
233 $subscription->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
234 continue;
235 }
236
237 $subscription->$field = $this->_checkValForAPI($field, $value, $subscription);
238 }
239
240 if ($subscription->update(DolibarrApiAccess::$user) > 0) {
241 return $this->get($id);
242 } else {
243 throw new RestException(500, 'Error when updating contribution: '.$subscription->error);
244 }
245 }
246
260 public function delete($id)
261 {
262 // The right to delete a subscription comes with the right to create one.
263 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'creer')) {
264 throw new RestException(403);
265 }
266 $subscription = new Subscription($this->db);
267 $result = $subscription->fetch($id);
268 if (!$result) {
269 throw new RestException(404, 'Subscription not found');
270 }
271
272 $res = $subscription->delete(DolibarrApiAccess::$user);
273 if ($res < 0) {
274 throw new RestException(500, "Can't delete, error occurs");
275 } elseif ($res == 0) {
276 throw new RestException(409, "No subscription whas deleted");
277 }
278
279 return array(
280 'success' => array(
281 'code' => 200,
282 'message' => 'Subscription deleted'
283 )
284 );
285 }
286
295 private function _validate($data)
296 {
297 $subscription = array();
298 foreach (Subscriptions::$FIELDS as $field) {
299 if (!isset($data[$field])) {
300 throw new RestException(400, "$field field missing");
301 }
302 $subscription[$field] = $data[$field];
303 }
304 return $subscription;
305 }
306}
$id
Definition account.php:39
Class for API REST v1.
Definition api.class.php:30
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition api.class.php:82
_cleanObjectDatas($object)
Clean sensitive object data @phpstan-template T of Object.
Class to manage subscriptions of foundation members.
_validate($data)
Validate fields before creating an object.
post($request_data=null)
Create subscription object.
index($sortfield="dateadh", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='', $pagination_data=false)
List subscriptions.
put($id, $request_data=null)
Update subscription.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.