dolibarr 18.0.6
api_partnership.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
3 * Copyright (C) 2022 Alice Adminson <aadminson@example.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use Luracast\Restler\RestException;
20
21dol_include_once('/partnership/class/partnership.class.php');
22
23
24
38{
42 public $partnership;
43
50 public function __construct()
51 {
52 global $db;
53 $this->db = $db;
54 $this->partnership = new Partnership($this->db);
55 }
56
70 public function get($id)
71 {
72 if (!DolibarrApiAccess::$user->rights->partnership->read) {
73 throw new RestException(401);
74 }
75
76 $result = $this->partnership->fetch($id);
77 if (!$result) {
78 throw new RestException(404, 'Partnership not found');
79 }
80
81 if (!DolibarrApi::_checkAccessToResource('partnership', $this->partnership->id, 'partnership')) {
82 throw new RestException(401, 'Access to instance id='.$this->partnership->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
83 }
84
85 return $this->_cleanObjectDatas($this->partnership);
86 }
87
88
105 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
106 {
107 global $db, $conf;
108
109 $obj_ret = array();
110 $tmpobject = new Partnership($this->db);
111
112 if (!DolibarrApiAccess::$user->rights->partnership->read) {
113 throw new RestException(401);
114 }
115
116 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
117
118 $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
119
120 // If the internal user must only see his customers, force searching by him
121 $search_sale = 0;
122 if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
123 $search_sale = DolibarrApiAccess::$user->id;
124 }
125
126 $sql = "SELECT t.rowid";
127 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
128 $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
129 }
130 $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." AS t LEFT JOIN ".MAIN_DB_PREFIX.$tmpobject->table_element."_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
131
132 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
133 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
134 }
135 $sql .= " WHERE 1 = 1";
136
137 // Example of use $mode
138 //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
139 //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
140
141 if ($tmpobject->ismultientitymanaged) {
142 $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
143 }
144 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
145 $sql .= " AND t.fk_soc = sc.fk_soc";
146 }
147 if ($restrictonsocid && $socid) {
148 $sql .= " AND t.fk_soc = ".((int) $socid);
149 }
150 if ($restrictonsocid && $search_sale > 0) {
151 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
152 }
153 // Insert sale filter
154 if ($restrictonsocid && $search_sale > 0) {
155 $sql .= " AND sc.fk_user = ".((int) $search_sale);
156 }
157 if ($sqlfilters) {
158 $errormessage = '';
159 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
160 if ($errormessage) {
161 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
162 }
163 }
164
165 $sql .= $this->db->order($sortfield, $sortorder);
166 if ($limit) {
167 if ($page < 0) {
168 $page = 0;
169 }
170 $offset = $limit * $page;
171
172 $sql .= $this->db->plimit($limit + 1, $offset);
173 }
174
175 $result = $this->db->query($sql);
176 $i = 0;
177 if ($result) {
178 $num = $this->db->num_rows($result);
179 while ($i < $num) {
180 $obj = $this->db->fetch_object($result);
181 $tmp_object = new Partnership($this->db);
182 if ($tmp_object->fetch($obj->rowid)) {
183 $obj_ret[] = $this->_cleanObjectDatas($tmp_object);
184 }
185 $i++;
186 }
187 } else {
188 throw new RestException(503, 'Error when retrieving partnership list: '.$this->db->lasterror());
189 }
190 if (!count($obj_ret)) {
191 throw new RestException(404, 'No partnership found');
192 }
193 return $obj_ret;
194 }
195
206 public function post($request_data = null)
207 {
208 if (!DolibarrApiAccess::$user->rights->partnership->write) {
209 throw new RestException(401);
210 }
211
212 // Check mandatory fields
213 $result = $this->_validate($request_data);
214
215 foreach ($request_data as $field => $value) {
216 $this->partnership->$field = $this->_checkValForAPI($field, $value, $this->partnership);
217 }
218
219 // Clean data
220 // $this->partnership->abc = sanitizeVal($this->partnership->abc, 'alphanohtml');
221
222 if ($this->partnership->create(DolibarrApiAccess::$user)<0) {
223 throw new RestException(500, "Error creating Partnership", array_merge(array($this->partnership->error), $this->partnership->errors));
224 }
225 return $this->partnership->id;
226 }
227
239 public function put($id, $request_data = null)
240 {
241 if (!DolibarrApiAccess::$user->rights->partnership->write) {
242 throw new RestException(401);
243 }
244
245 $result = $this->partnership->fetch($id);
246 if (!$result) {
247 throw new RestException(404, 'Partnership not found');
248 }
249
250 if (!DolibarrApi::_checkAccessToResource('partnership', $this->partnership->id, 'partnership')) {
251 throw new RestException(401, 'Access to instance id='.$this->partnership->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
252 }
253
254 foreach ($request_data as $field => $value) {
255 if ($field == 'id') {
256 continue;
257 }
258 $this->partnership->$field = $this->_checkValForAPI($field, $value, $this->partnership);
259 }
260
261 // Clean data
262 // $this->partnership->abc = sanitizeVal($this->partnership->abc, 'alphanohtml');
263
264 if ($this->partnership->update(DolibarrApiAccess::$user, false) > 0) {
265 return $this->get($id);
266 } else {
267 throw new RestException(500, $this->partnership->error);
268 }
269 }
270
281 public function delete($id)
282 {
283 if (!DolibarrApiAccess::$user->rights->partnership->delete) {
284 throw new RestException(401);
285 }
286 $result = $this->partnership->fetch($id);
287 if (!$result) {
288 throw new RestException(404, 'Partnership not found');
289 }
290
291 if (!DolibarrApi::_checkAccessToResource('partnership', $this->partnership->id, 'partnership')) {
292 throw new RestException(401, 'Access to instance id='.$this->partnership->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
293 }
294
295 if (!$this->partnership->delete(DolibarrApiAccess::$user)) {
296 throw new RestException(500, 'Error when deleting Partnership : '.$this->partnership->error);
297 }
298
299 return array(
300 'success' => array(
301 'code' => 200,
302 'message' => 'Partnership deleted'
303 )
304 );
305 }
306
307
308 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
315 protected function _cleanObjectDatas($object)
316 {
317 // phpcs:enable
318 $object = parent::_cleanObjectDatas($object);
319
320 unset($object->rowid);
321 unset($object->canvas);
322
323 /*unset($object->name);
324 unset($object->lastname);
325 unset($object->firstname);
326 unset($object->civility_id);
327 unset($object->statut);
328 unset($object->state);
329 unset($object->state_id);
330 unset($object->state_code);
331 unset($object->region);
332 unset($object->region_code);
333 unset($object->country);
334 unset($object->country_id);
335 unset($object->country_code);
336 unset($object->barcode_type);
337 unset($object->barcode_type_code);
338 unset($object->barcode_type_label);
339 unset($object->barcode_type_coder);
340 unset($object->total_ht);
341 unset($object->total_tva);
342 unset($object->total_localtax1);
343 unset($object->total_localtax2);
344 unset($object->total_ttc);
345 unset($object->fk_account);
346 unset($object->comments);
347 unset($object->note);
348 unset($object->mode_reglement_id);
349 unset($object->cond_reglement_id);
350 unset($object->cond_reglement);
351 unset($object->shipping_method_id);
352 unset($object->fk_incoterms);
353 unset($object->label_incoterms);
354 unset($object->location_incoterms);
355 */
356
357 // If object has lines, remove $db property
358 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
359 $nboflines = count($object->lines);
360 for ($i = 0; $i < $nboflines; $i++) {
361 $this->_cleanObjectDatas($object->lines[$i]);
362
363 unset($object->lines[$i]->lines);
364 unset($object->lines[$i]->note);
365 }
366 }
367
368 return $object;
369 }
370
379 private function _validate($data)
380 {
381 $partnership = array();
382 foreach ($this->partnership->fields as $field => $propfield) {
383 if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
384 continue; // Not a mandatory field
385 }
386 if (!isset($data[$field])) {
387 throw new RestException(400, "$field field missing");
388 }
389 $partnership[$field] = $data[$field];
390 }
391 return $partnership;
392 }
393}
Class for API REST v1.
Definition api.class.php:31
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition api.class.php:86
_cleanObjectDatas($object)
Clean sensible object datas.
put($id, $request_data=null)
Update partnership.
__construct()
Constructor.
post($request_data=null)
Create partnership object.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
List partnerships.
_validate($data)
Validate fields before create or update object.
Class for Partnership.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.