dolibarr  13.0.2
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  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 $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  if (!DolibarrApi::_checkFilters($sqlfilters)) {
104  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
105  }
106  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
107  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
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->rights->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 subscription', 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->rights->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') continue;
187  $subscription->$field = $value;
188  }
189 
190  if ($subscription->update(DolibarrApiAccess::$user) > 0) {
191  return $this->get($id);
192  } else {
193  throw new RestException(500, $subscription->error);
194  }
195  }
196 
203  public function delete($id)
204  {
205  // The right to delete a subscription comes with the right to create one.
206  if (!DolibarrApiAccess::$user->rights->adherent->cotisation->creer) {
207  throw new RestException(401);
208  }
209  $subscription = new Subscription($this->db);
210  $result = $subscription->fetch($id);
211  if (!$result) {
212  throw new RestException(404, 'Subscription not found');
213  }
214 
215  if (!$subscription->delete(DolibarrApiAccess::$user)) {
216  throw new RestException(401, 'error when deleting subscription');
217  }
218 
219  return array(
220  'success' => array(
221  'code' => 200,
222  'message' => 'subscription deleted'
223  )
224  );
225  }
226 
235  private function _validate($data)
236  {
237  $subscription = array();
238  foreach (Subscriptions::$FIELDS as $field) {
239  if (!isset($data[$field]))
240  throw new RestException(400, "$field field missing");
241  $subscription[$field] = $data[$field];
242  }
243  return $subscription;
244  }
245 }
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:98
$conf db
API class for accounts.
Definition: inc.php:54
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:278
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.