dolibarr 18.0.6
api_subscriptions.class.php
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
18use Luracast\Restler\RestException;
19
20require_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
21
29{
33 public static $FIELDS = array(
34 'fk_adherent',
35 'dateh',
36 'datef',
37 'amount',
38 );
39
43 public function __construct()
44 {
45 global $db, $conf;
46 $this->db = $db;
47 }
48
59 public function get($id)
60 {
61 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'lire')) {
62 throw new RestException(401);
63 }
64
65 $subscription = new Subscription($this->db);
66 $result = $subscription->fetch($id);
67 if (!$result) {
68 throw new RestException(404, 'Subscription not found');
69 }
70
71 return $this->_cleanObjectDatas($subscription);
72 }
73
88 public function index($sortfield = "dateadh", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
89 {
90 global $conf;
91
92 $obj_ret = array();
93
94 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'lire')) {
95 throw new RestException(401);
96 }
97
98 $sql = "SELECT rowid";
99 $sql .= " FROM ".MAIN_DB_PREFIX."subscription as t";
100 $sql .= ' WHERE 1 = 1';
101 // Add sql filters
102 if ($sqlfilters) {
103 $errormessage = '';
104 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
105 if ($errormessage) {
106 throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
107 }
108 }
109
110 $sql .= $this->db->order($sortfield, $sortorder);
111 if ($limit) {
112 if ($page < 0) {
113 $page = 0;
114 }
115 $offset = $limit * $page;
116
117 $sql .= $this->db->plimit($limit + 1, $offset);
118 }
119
120 $result = $this->db->query($sql);
121 if ($result) {
122 $i = 0;
123 $num = $this->db->num_rows($result);
124 while ($i < min($limit, $num)) {
125 $obj = $this->db->fetch_object($result);
126 $subscription = new Subscription($this->db);
127 if ($subscription->fetch($obj->rowid)) {
128 $obj_ret[] = $this->_cleanObjectDatas($subscription);
129 }
130 $i++;
131 }
132 } else {
133 throw new RestException(503, 'Error when retrieve subscription list : '.$this->db->lasterror());
134 }
135 if (!count($obj_ret)) {
136 throw new RestException(404, 'No Subscription found');
137 }
138
139 return $obj_ret;
140 }
141
148 public function post($request_data = null)
149 {
150 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'creer')) {
151 throw new RestException(401);
152 }
153 // Check mandatory fields
154 $result = $this->_validate($request_data);
155
156 $subscription = new Subscription($this->db);
157 foreach ($request_data as $field => $value) {
158 $subscription->$field = $value;
159 }
160 if ($subscription->create(DolibarrApiAccess::$user) < 0) {
161 throw new RestException(500, 'Error when creating contribution', array_merge(array($subscription->error), $subscription->errors));
162 }
163 return $subscription->id;
164 }
165
173 public function put($id, $request_data = null)
174 {
175 if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
176 throw new RestException(401);
177 }
178
179 $subscription = new Subscription($this->db);
180 $result = $subscription->fetch($id);
181 if (!$result) {
182 throw new RestException(404, 'Subscription not found');
183 }
184
185 foreach ($request_data as $field => $value) {
186 if ($field == 'id') {
187 continue;
188 }
189 if ($field == 'array_options' && is_array($value)) {
190 foreach ($value as $index => $val) {
191 $subscription->array_options[$index] = $this->_checkValForAPI($field, $val, $subscription);
192 }
193 continue;
194 }
195 $subscription->$field = $value;
196 }
197
198 if ($subscription->update(DolibarrApiAccess::$user) > 0) {
199 return $this->get($id);
200 } else {
201 throw new RestException(500, 'Error when updating contribution: '.$subscription->error);
202 }
203 }
204
211 public function delete($id)
212 {
213 // The right to delete a subscription comes with the right to create one.
214 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'creer')) {
215 throw new RestException(401);
216 }
217 $subscription = new Subscription($this->db);
218 $result = $subscription->fetch($id);
219 if (!$result) {
220 throw new RestException(404, 'Subscription not found');
221 }
222
223 $res = $subscription->delete(DolibarrApiAccess::$user);
224 if ($res < 0) {
225 throw new RestException(500, "Can't delete, error occurs");
226 } elseif ($res == 0) {
227 throw new RestException(409, "Can't delete, that product is probably used");
228 }
229
230 return array(
231 'success' => array(
232 'code' => 200,
233 'message' => 'Subscription deleted'
234 )
235 );
236 }
237
246 private function _validate($data)
247 {
248 $subscription = array();
249 foreach (Subscriptions::$FIELDS as $field) {
250 if (!isset($data[$field])) {
251 throw new RestException(400, "$field field missing");
252 }
253 $subscription[$field] = $data[$field];
254 }
255 return $subscription;
256 }
257}
Class for API REST v1.
Definition api.class.php:31
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition api.class.php:86
_cleanObjectDatas($object)
Clean sensible object datas.
Class to manage subscriptions of foundation members.
index($sortfield="dateadh", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
List subscriptions.
_validate($data)
Validate fields before creating an object.
post($request_data=null)
Create subscription object.
put($id, $request_data=null)
Update subscription.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria