dolibarr  x.y.z
api_multicurrencies.class.php
1 <?php
2 /* Copyright (C) 2022 J-F Bouculat <jfbouculat@gmail.com>
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.'/multicurrency/class/multicurrency.class.php';
21 require_once DOL_DOCUMENT_ROOT.'/core/lib/multicurrency.lib.php';
22 
30 {
31 
35  public function __construct()
36  {
37  global $db, $conf;
38  $this->db = $db;
39  }
40 
51  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $sqlfilters = '')
52  {
53  global $db, $conf;
54 
55  $obj_ret = array();
56 
57  $sql = "SELECT t.rowid";
58  $sql .= " FROM ".$this->db->prefix()."multicurrency as t";
59  $sql .= ' WHERE 1 = 1';
60  // Add sql filters
61  if ($sqlfilters) {
62  $errormessage = '';
63  if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
64  throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
65  }
66  $regexstring = '\‍(([^:\'\‍(\‍)]+:[^:\'\‍(\‍)]+:[^\‍(\‍)]+)\‍)';
67  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
68  }
69 
70  $sql .= $this->db->order($sortfield, $sortorder);
71  if ($limit) {
72  if ($page < 0) {
73  $page = 0;
74  }
75  $offset = $limit * $page;
76 
77  $sql .= $this->db->plimit($limit + 1, $offset);
78  }
79 
80  $result = $this->db->query($sql);
81  if ($result) {
82  $i = 0;
83  $num = $this->db->num_rows($result);
84  $min = min($num, ($limit <= 0 ? $num : $limit));
85  while ($i < $min) {
86  $obj = $this->db->fetch_object($result);
87  $multicurrency_static = new MultiCurrency($this->db);
88  if ($multicurrency_static->fetch($obj->rowid)) {
89  $obj_ret[] = $this->_cleanObjectDatas($multicurrency_static);
90  }
91  $i++;
92  }
93  } else {
94  throw new RestException(503, 'Error when retrieve currencies list : '.$this->db->lasterror());
95  }
96  if (!count($obj_ret)) {
97  throw new RestException(404, 'No currencies found');
98  }
99 
100  return $obj_ret;
101  }
102 
103  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
110  protected function _cleanObjectDatas($object)
111  {
112  // phpcs:enable
113  $object = parent::_cleanObjectDatas($object);
114 
115  // Clear all fields out of interrest
116  foreach ($object as $key => $value) {
117  if ($key == "rate") $object->$key = $this->_cleanObjectDatas($object->$key);
118  if ($key == "id" || $key == "code" || $key == "rate" || $key == "date_sync")
119  continue;
120  unset($object->$key);
121  }
122 
123  return $object;
124  }
125 }
Class for API REST v1.
Definition: api.class.php:31
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:310
index($sortfield="t.rowid", $sortorder='ASC', $sqlfilters='')
Get a list of currencies.
_cleanObjectDatas($object)
Clean sensible object datas.
Class Currency.
$conf db
API class for accounts.
Definition: inc.php:41