123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- <?php
- /* Copyright (C) 2016 Xebax Christy <xebax@wanadoo.fr>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
- use Luracast\Restler\RestException;
- require_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
- /**
- * API class for subscriptions
- *
- * @access protected
- * @class DolibarrApiAccess {@requires user,external}
- */
- class Subscriptions extends DolibarrApi
- {
- /**
- * @var array $FIELDS Mandatory fields, checked when create and update object
- */
- static $FIELDS = array(
- 'fk_adherent',
- 'dateh',
- 'datef',
- 'amount',
- );
- /**
- * Constructor
- */
- public function __construct()
- {
- global $db, $conf;
- $this->db = $db;
- }
- /**
- * Get properties of a subscription object
- *
- * Return an array with subscription informations
- *
- * @param int $id ID of subscription
- * @return array|mixed data without useless information
- *
- * @throws RestException
- */
- public function get($id)
- {
- if (!DolibarrApiAccess::$user->rights->adherent->cotisation->lire) {
- throw new RestException(401);
- }
- $subscription = new Subscription($this->db);
- $result = $subscription->fetch($id);
- if (!$result) {
- throw new RestException(404, 'Subscription not found');
- }
- return $this->_cleanObjectDatas($subscription);
- }
- /**
- * List subscriptions
- *
- * Get a list of subscriptions
- *
- * @param string $sortfield Sort field
- * @param string $sortorder Sort order
- * @param int $limit Limit for list
- * @param int $page Page number
- * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.import_key:<:'20160101')"
- * @return array Array of subscription objects
- *
- * @throws RestException
- */
- public function index($sortfield = "dateadh", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
- {
- global $db, $conf;
- $obj_ret = array();
- if (!DolibarrApiAccess::$user->rights->adherent->cotisation->lire) {
- throw new RestException(401);
- }
- $sql = "SELECT rowid";
- $sql .= " FROM ".MAIN_DB_PREFIX."subscription as t";
- $sql .= ' WHERE 1 = 1';
- // Add sql filters
- if ($sqlfilters)
- {
- if (!DolibarrApi::_checkFilters($sqlfilters))
- {
- throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
- }
- $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
- $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
- }
- $sql .= $db->order($sortfield, $sortorder);
- if ($limit) {
- if ($page < 0)
- {
- $page = 0;
- }
- $offset = $limit * $page;
- $sql .= $db->plimit($limit + 1, $offset);
- }
- $result = $db->query($sql);
- if ($result)
- {
- $i = 0;
- $num = $db->num_rows($result);
- while ($i < min($limit, $num))
- {
- $obj = $db->fetch_object($result);
- $subscription = new Subscription($this->db);
- if ($subscription->fetch($obj->rowid)) {
- $obj_ret[] = $this->_cleanObjectDatas($subscription);
- }
- $i++;
- }
- }
- else {
- throw new RestException(503, 'Error when retrieve subscription list : '.$db->lasterror());
- }
- if (!count($obj_ret)) {
- throw new RestException(404, 'No Subscription found');
- }
- return $obj_ret;
- }
- /**
- * Create subscription object
- *
- * @param array $request_data Request data
- * @return int ID of subscription
- */
- public function post($request_data = null)
- {
- if (!DolibarrApiAccess::$user->rights->adherent->cotisation->creer) {
- throw new RestException(401);
- }
- // Check mandatory fields
- $result = $this->_validate($request_data);
- $subscription = new Subscription($this->db);
- foreach ($request_data as $field => $value) {
- $subscription->$field = $value;
- }
- if ($subscription->create(DolibarrApiAccess::$user) < 0) {
- throw new RestException(500, 'Error when creating subscription', array_merge(array($subscription->error), $subscription->errors));
- }
- return $subscription->id;
- }
- /**
- * Update subscription
- *
- * @param int $id ID of subscription to update
- * @param array $request_data Datas
- * @return int
- */
- public function put($id, $request_data = null)
- {
- if (!DolibarrApiAccess::$user->rights->adherent->creer) {
- throw new RestException(401);
- }
- $subscription = new Subscription($this->db);
- $result = $subscription->fetch($id);
- if (!$result) {
- throw new RestException(404, 'Subscription not found');
- }
- foreach ($request_data as $field => $value) {
- if ($field == 'id') continue;
- $subscription->$field = $value;
- }
- if ($subscription->update(DolibarrApiAccess::$user) > 0)
- {
- return $this->get($id);
- }
- else
- {
- throw new RestException(500, $subscription->error);
- }
- }
- /**
- * Delete subscription
- *
- * @param int $id ID of subscription to delete
- * @return array
- */
- public function delete($id)
- {
- // The right to delete a subscription comes with the right to create one.
- if (!DolibarrApiAccess::$user->rights->adherent->cotisation->creer) {
- throw new RestException(401);
- }
- $subscription = new Subscription($this->db);
- $result = $subscription->fetch($id);
- if (!$result) {
- throw new RestException(404, 'Subscription not found');
- }
- if (!$subscription->delete(DolibarrApiAccess::$user)) {
- throw new RestException(401, 'error when deleting subscription');
- }
- return array(
- 'success' => array(
- 'code' => 200,
- 'message' => 'subscription deleted'
- )
- );
- }
- /**
- * Validate fields before creating an object
- *
- * @param array|null $data Data to validate
- * @return array
- *
- * @throws RestException
- */
- private function _validate($data)
- {
- $subscription = array();
- foreach (Subscriptions::$FIELDS as $field) {
- if (!isset($data[$field]))
- throw new RestException(400, "$field field missing");
- $subscription[$field] = $data[$field];
- }
- return $subscription;
- }
- }
|