dolibarr  16.0.1
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 
18 use Luracast\Restler\RestException;
19 
20 require_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->rights->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->rights->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  if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
105  throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
106  }
107  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
108  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
109  }
110 
111  $sql .= $this->db->order($sortfield, $sortorder);
112  if ($limit) {
113  if ($page < 0) {
114  $page = 0;
115  }
116  $offset = $limit * $page;
117 
118  $sql .= $this->db->plimit($limit + 1, $offset);
119  }
120 
121  $result = $this->db->query($sql);
122  if ($result) {
123  $i = 0;
124  $num = $this->db->num_rows($result);
125  while ($i < min($limit, $num)) {
126  $obj = $this->db->fetch_object($result);
127  $subscription = new Subscription($this->db);
128  if ($subscription->fetch($obj->rowid)) {
129  $obj_ret[] = $this->_cleanObjectDatas($subscription);
130  }
131  $i++;
132  }
133  } else {
134  throw new RestException(503, 'Error when retrieve subscription list : '.$this->db->lasterror());
135  }
136  if (!count($obj_ret)) {
137  throw new RestException(404, 'No Subscription found');
138  }
139 
140  return $obj_ret;
141  }
142 
149  public function post($request_data = null)
150  {
151  if (!DolibarrApiAccess::$user->rights->adherent->cotisation->creer) {
152  throw new RestException(401);
153  }
154  // Check mandatory fields
155  $result = $this->_validate($request_data);
156 
157  $subscription = new Subscription($this->db);
158  foreach ($request_data as $field => $value) {
159  $subscription->$field = $value;
160  }
161  if ($subscription->create(DolibarrApiAccess::$user) < 0) {
162  throw new RestException(500, 'Error when creating contribution', array_merge(array($subscription->error), $subscription->errors));
163  }
164  return $subscription->id;
165  }
166 
174  public function put($id, $request_data = null)
175  {
176  if (!DolibarrApiAccess::$user->rights->adherent->creer) {
177  throw new RestException(401);
178  }
179 
180  $subscription = new Subscription($this->db);
181  $result = $subscription->fetch($id);
182  if (!$result) {
183  throw new RestException(404, 'Subscription not found');
184  }
185 
186  foreach ($request_data as $field => $value) {
187  if ($field == 'id') {
188  continue;
189  }
190  $subscription->$field = $value;
191  }
192 
193  if ($subscription->update(DolibarrApiAccess::$user) > 0) {
194  return $this->get($id);
195  } else {
196  throw new RestException(500, 'Error when updating contribution: '.$subscription->error);
197  }
198  }
199 
206  public function delete($id)
207  {
208  // The right to delete a subscription comes with the right to create one.
209  if (!DolibarrApiAccess::$user->rights->adherent->cotisation->creer) {
210  throw new RestException(401);
211  }
212  $subscription = new Subscription($this->db);
213  $result = $subscription->fetch($id);
214  if (!$result) {
215  throw new RestException(404, 'Subscription not found');
216  }
217 
218  if (!$subscription->delete(DolibarrApiAccess::$user)) {
219  throw new RestException(401, 'error when deleting subscription');
220  }
221 
222  return array(
223  'success' => array(
224  'code' => 200,
225  'message' => 'subscription deleted'
226  )
227  );
228  }
229 
238  private function _validate($data)
239  {
240  $subscription = array();
241  foreach (Subscriptions::$FIELDS as $field) {
242  if (!isset($data[$field])) {
243  throw new RestException(400, "$field field missing");
244  }
245  $subscription[$field] = $data[$field];
246  }
247  return $subscription;
248  }
249 }
index($sortfield="dateadh", $sortorder= 'ASC', $limit=100, $page=0, $sqlfilters= '')
List subscriptions.
$conf db
API class for accounts.
Definition: inc.php:41
_cleanObjectDatas($object)
Clean sensible object datas.
Definition: api.class.php:104
_checkFilters($sqlfilters, &$error= '')
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:310
Class for API REST v1.
Definition: api.class.php:30
post($request_data=null)
Create subscription object.
Class to manage subscriptions of foundation members.
_validate($data)
Validate fields before creating an object.
put($id, $request_data=null)
Update subscription.
__construct()
Constructor.