dolibarr 23.0.3
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 $subscription;
45
49 public function __construct()
50 {
51 global $db, $conf;
52 $this->db = $db;
53 $this->subscription = new Subscription($this->db);
54 }
55
67 public function get($id)
68 {
69 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'lire')) {
70 throw new RestException(403);
71 }
72
73 $result = $this->subscription->fetch($id);
74 if (!$result) {
75 throw new RestException(404, 'Subscription not found');
76 }
77
78 $this->subscription->fetchObjectLinked();
79
80 return $this->_cleanObjectDatas($this->subscription);
81 }
82
103 public function index($sortfield = "dateadh", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
104 {
105 global $conf;
106
107 $obj_ret = array();
108
109 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'lire')) {
110 throw new RestException(403);
111 }
112
113 $sql = "SELECT rowid";
114 $sql .= " FROM ".MAIN_DB_PREFIX."subscription as t";
115 $sql .= ' WHERE 1 = 1';
116 // Add sql filters
117 if ($sqlfilters) {
118 $errormessage = '';
119 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
120 if ($errormessage) {
121 throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
122 }
123 }
124
125 //this query will return total orders with the filters given
126 $sqlTotals = str_replace('SELECT rowid', 'SELECT count(rowid) as total', $sql);
127
128 $sql .= $this->db->order($sortfield, $sortorder);
129 if ($limit) {
130 if ($page < 0) {
131 $page = 0;
132 }
133 $offset = $limit * $page;
134
135 $sql .= $this->db->plimit($limit + 1, $offset);
136 }
137
138 $result = $this->db->query($sql);
139 if ($result) {
140 $i = 0;
141 $num = $this->db->num_rows($result);
142 $min = min($num, ($limit <= 0 ? $num : $limit));
143 while ($i < $min) {
144 $obj = $this->db->fetch_object($result);
145 $subscription = new Subscription($this->db);
146 if ($subscription->fetch($obj->rowid)) {
147 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($subscription), $properties);
148 }
149 $i++;
150 }
151 } else {
152 throw new RestException(503, 'Error when retrieve subscription list : '.$this->db->lasterror());
153 }
154
155 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
156 if ($pagination_data) {
157 $totalsResult = $this->db->query($sqlTotals);
158 $total = $this->db->fetch_object($totalsResult)->total;
159
160 $tmp = $obj_ret;
161 $obj_ret = [];
162
163 $obj_ret['data'] = $tmp;
164 $obj_ret['pagination'] = [
165 'total' => (int) $total,
166 'page' => $page, //count starts from 0
167 'page_count' => ceil((int) $total / $limit),
168 'limit' => $limit
169 ];
170 }
171
172 return $obj_ret;
173 }
174
186 public function post($request_data = null)
187 {
188 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'creer')) {
189 throw new RestException(403);
190 }
191 // Check mandatory fields
192 $result = $this->_validate($request_data);
193
194 $subscription = new Subscription($this->db);
195 foreach ($request_data as $field => $value) {
196 if ($field === 'caller') {
197 // 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
198 $subscription->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
199 continue;
200 }
201
202 $subscription->$field = $this->_checkValForAPI($field, $value, $subscription);
203 }
204 if ($subscription->create(DolibarrApiAccess::$user) < 0) {
205 throw new RestException(500, 'Error when creating subscription', array_merge(array($subscription->error), $subscription->errors));
206 }
207 return $subscription->id;
208 }
209
223 public function put($id, $request_data = null)
224 {
225 if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
226 throw new RestException(403);
227 }
228
229 $subscription = new Subscription($this->db);
230 $result = $subscription->fetch($id);
231 if (!$result) {
232 throw new RestException(404, 'Subscription not found');
233 }
234
235 foreach ($request_data as $field => $value) {
236 if ($field == 'id') {
237 continue;
238 }
239 if ($field === 'caller') {
240 // 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
241 $subscription->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
242 continue;
243 }
244
245 if ($field == 'array_options' && is_array($value)) {
246 foreach ($value as $index => $val) {
247 $subscription->array_options[$index] = $this->_checkValForAPI($field, $val, $subscription);
248 }
249 continue;
250 }
251 $subscription->$field = $this->_checkValForAPI($field, $value, $subscription);
252 }
253
254 if ($subscription->update(DolibarrApiAccess::$user) > 0) {
255 return $this->get($id);
256 } else {
257 throw new RestException(500, 'Error when updating contribution: '.$subscription->error);
258 }
259 }
260
274 public function delete($id)
275 {
276 // The right to delete a subscription comes with the right to create one.
277 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'creer')) {
278 throw new RestException(403);
279 }
280 $subscription = new Subscription($this->db);
281 $result = $subscription->fetch($id);
282 if (!$result) {
283 throw new RestException(404, 'Subscription not found');
284 }
285
286 $res = $subscription->delete(DolibarrApiAccess::$user);
287 if ($res < 0) {
288 throw new RestException(500, "Can't delete, error occurs");
289 } elseif ($res == 0) {
290 throw new RestException(409, "No subscription whas deleted");
291 }
292
293 return array(
294 'success' => array(
295 'code' => 200,
296 'message' => 'Subscription deleted'
297 )
298 );
299 }
300
309 private function _validate($data)
310 {
311 $subscription = array();
312 foreach (Subscriptions::$FIELDS as $field) {
313 if (!isset($data[$field])) {
314 throw new RestException(400, "$field field missing");
315 }
316 $subscription[$field] = $data[$field];
317 }
318 return $subscription;
319 }
320}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
Class for API REST v1.
Definition api.class.php:33
_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:98
_cleanObjectDatas($object)
Clean sensitive object data @phpstan-template T.
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.