dolibarr  9.0.0
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 <http://www.gnu.org/licenses/>.
16  */
17 
19 
20 require_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
21 
29 {
33  static $FIELDS = array(
34  'fk_adherent',
35  'dateh',
36  'datef',
37  'amount',
38  );
39 
43  function __construct()
44  {
45  global $db, $conf;
46  $this->db = $db;
47  }
48 
59  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  function index($sortfield = "dateadh", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
89  {
90  global $db, $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  {
104  if (! DolibarrApi::_checkFilters($sqlfilters))
105  {
106  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
107  }
108  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
109  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
110  }
111 
112  $sql.= $db->order($sortfield, $sortorder);
113  if ($limit) {
114  if ($page < 0)
115  {
116  $page = 0;
117  }
118  $offset = $limit * $page;
119 
120  $sql.= $db->plimit($limit + 1, $offset);
121  }
122 
123  $result = $db->query($sql);
124  if ($result)
125  {
126  $i=0;
127  $num = $db->num_rows($result);
128  while ($i < min($limit, $num))
129  {
130  $obj = $db->fetch_object($result);
131  $subscription = new Subscription($this->db);
132  if($subscription->fetch($obj->rowid)) {
133  $obj_ret[] = $this->_cleanObjectDatas($subscription);
134  }
135  $i++;
136  }
137  }
138  else {
139  throw new RestException(503, 'Error when retrieve subscription list : '.$db->lasterror());
140  }
141  if( ! count($obj_ret)) {
142  throw new RestException(404, 'No Subscription found');
143  }
144 
145  return $obj_ret;
146  }
147 
154  function post($request_data = null)
155  {
156  if(! DolibarrApiAccess::$user->rights->adherent->cotisation->creer) {
157  throw new RestException(401);
158  }
159  // Check mandatory fields
160  $result = $this->_validate($request_data);
161 
162  $subscription = new Subscription($this->db);
163  foreach($request_data as $field => $value) {
164  $subscription->$field = $value;
165  }
166  if ($subscription->create(DolibarrApiAccess::$user) < 0) {
167  throw new RestException(500, 'Error when creating subscription', array_merge(array($subscription->error), $subscription->errors));
168  }
169  return $subscription->id;
170  }
171 
179  function put($id, $request_data = null)
180  {
181  if(! DolibarrApiAccess::$user->rights->adherent->creer) {
182  throw new RestException(401);
183  }
184 
185  $subscription = new Subscription($this->db);
186  $result = $subscription->fetch($id);
187  if( ! $result ) {
188  throw new RestException(404, 'Subscription not found');
189  }
190 
191  foreach($request_data as $field => $value) {
192  if ($field == 'id') continue;
193  $subscription->$field = $value;
194  }
195 
196  if ($subscription->update(DolibarrApiAccess::$user) > 0)
197  {
198  return $this->get($id);
199  }
200  else
201  {
202  throw new RestException(500, $subscription->error);
203  }
204  }
205 
212  function delete($id)
213  {
214  // The right to delete a subscription comes with the right to create one.
215  if(! DolibarrApiAccess::$user->rights->adherent->cotisation->creer) {
216  throw new RestException(401);
217  }
218  $subscription = new Subscription($this->db);
219  $result = $subscription->fetch($id);
220  if( ! $result ) {
221  throw new RestException(404, 'Subscription not found');
222  }
223 
224  if (! $subscription->delete(DolibarrApiAccess::$user)) {
225  throw new RestException(401,'error when deleting subscription');
226  }
227 
228  return array(
229  'success' => array(
230  'code' => 200,
231  'message' => 'subscription deleted'
232  )
233  );
234  }
235 
244  function _validate($data)
245  {
246  $subscription = array();
247  foreach (Subscriptions::$FIELDS as $field) {
248  if (!isset($data[$field]))
249  throw new RestException(400, "$field field missing");
250  $subscription[$field] = $data[$field];
251  }
252  return $subscription;
253  }
254 }
index($sortfield="dateadh", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
List subscriptions.
_cleanObjectDatas($object)
Executed method when API is called without parameter.
Definition: api.class.php:97
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:251
Class for API REST v1.
Definition: api.class.php:29
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.