dolibarr 24.0.0-beta
api_partnerships.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) 2025 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20use Luracast\Restler\RestException;
21
22dol_include_once('/partnership/class/partnership.class.php');
23
24
25
39{
43 public $partnership;
44
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->hasRight('partnership', 'read')) {
73 throw new RestException(403);
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(403, '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
108 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
109 {
110 $obj_ret = array();
111 $tmpobject = new Partnership($this->db);
112
113 if (!DolibarrApiAccess::$user->hasRight('partnership', 'read')) {
114 throw new RestException(403);
115 }
116
117 $socid = DolibarrApiAccess::$user->socid ?: 0;
118
119 $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
120
121 // If the internal user must only see his customers, force searching by him
122 $search_sale = 0;
123 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socid) {
124 $search_sale = DolibarrApiAccess::$user->id;
125 }
126
127 $sql = "SELECT t.rowid";
128 $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." AS t";
129 $sql .= " 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
130 $sql .= " WHERE 1 = 1";
131 if ($tmpobject->ismultientitymanaged) {
132 $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
133 }
134 if ($restrictonsocid && $socid) {
135 $sql .= " AND t.fk_soc = ".((int) $socid);
136 }
137 // Search on sale representative
138 if ($search_sale && $search_sale != '-1') {
139 if ($search_sale == -2) {
140 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
141 } elseif ($search_sale > 0) {
142 $sql .= " AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).")";
143 }
144 }
145 if ($sqlfilters) {
146 $errormessage = '';
147 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
148 if ($errormessage) {
149 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
150 }
151 }
152
153 $sql .= $this->db->order($sortfield, $sortorder);
154 if ($limit) {
155 if ($page < 0) {
156 $page = 0;
157 }
158 $offset = $limit * $page;
159
160 $sql .= $this->db->plimit($limit + 1, $offset);
161 }
162
163 $result = $this->db->query($sql);
164 $i = 0;
165 if ($result) {
166 $num = $this->db->num_rows($result);
167 $min = min($num, ($limit <= 0 ? $num : $limit));
168 while ($i < $min) {
169 $obj = $this->db->fetch_object($result);
170 $tmp_object = new Partnership($this->db);
171 if ($tmp_object->fetch($obj->rowid)) {
172 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($tmp_object), $properties);
173 }
174 $i++;
175 }
176 } else {
177 throw new RestException(503, 'Error when retrieving partnership list: '.$this->db->lasterror());
178 }
179
180 return $obj_ret;
181 }
182
195 public function post($request_data = null)
196 {
197 if (!DolibarrApiAccess::$user->hasRight('partnership', 'write')) {
198 throw new RestException(403);
199 }
200
201 // Check mandatory fields
202 $result = $this->_validate($request_data);
203
204 foreach ($request_data as $field => $value) {
205 if ($field === 'caller') {
206 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
207 $this->partnership->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
208 continue;
209 }
210
211 $this->partnership->$field = $this->_checkValForAPI($field, $value, $this->partnership);
212 }
213
214 // Clean data
215 // $this->partnership->abc = sanitizeVal($this->partnership->abc, 'alphanohtml');
216
217 if ($this->partnership->create(DolibarrApiAccess::$user) < 0) {
218 throw new RestException(500, "Error creating Partnership", array_merge(array($this->partnership->error), $this->partnership->errors));
219 }
220 return $this->partnership->id;
221 }
222
236 public function put($id, $request_data = null)
237 {
238 if (!DolibarrApiAccess::$user->hasRight('partnership', 'write')) {
239 throw new RestException(403);
240 }
241
242 $result = $this->partnership->fetch($id);
243 if (!$result) {
244 throw new RestException(404, 'Partnership not found');
245 }
246
247 if (!DolibarrApi::_checkAccessToResource('partnership', $this->partnership->id, 'partnership')) {
248 throw new RestException(403, 'Access to instance id='.$this->partnership->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
249 }
250
251 foreach ($request_data as $field => $value) {
252 if ($field == 'id') {
253 continue;
254 }
255 if ($field === 'caller') {
256 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
257 $this->partnership->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
258 continue;
259 }
260 if ($field == 'array_options' && is_array($value)) {
261 foreach ($value as $index => $val) {
262 $this->partnership->array_options[$index] = $this->_checkValExtrafieldsForAPI($index, $val, $this->partnership);
263 }
264 continue;
265 }
266
267 $this->partnership->$field = $this->_checkValForAPI($field, $value, $this->partnership);
268 }
269
270 // Clean data
271 // $this->partnership->abc = sanitizeVal($this->partnership->abc, 'alphanohtml');
272
273 if ($this->partnership->update(DolibarrApiAccess::$user, 0) > 0) {
274 return $this->get($id);
275 } else {
276 throw new RestException(500, $this->partnership->error);
277 }
278 }
279
292 public function delete($id)
293 {
294 if (!DolibarrApiAccess::$user->hasRight('partnership', 'delete')) {
295 throw new RestException(403);
296 }
297 $result = $this->partnership->fetch($id);
298 if (!$result) {
299 throw new RestException(404, 'Partnership not found');
300 }
301
302 if (!DolibarrApi::_checkAccessToResource('partnership', $this->partnership->id, 'partnership')) {
303 throw new RestException(403, 'Access to instance id='.$this->partnership->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
304 }
305
306 if (!$this->partnership->delete(DolibarrApiAccess::$user)) {
307 throw new RestException(500, 'Error when deleting Partnership : '.$this->partnership->error);
308 }
309
310 return array(
311 'success' => array(
312 'code' => 200,
313 'message' => 'Partnership deleted'
314 )
315 );
316 }
317
318
319 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
330 protected function _cleanObjectDatas($object)
331 {
332 // phpcs:enable
333 $object = parent::_cleanObjectDatas($object);
334
335 unset($object->rowid);
336 unset($object->canvas);
337
338 /*unset($object->name);
339 unset($object->lastname);
340 unset($object->firstname);
341 unset($object->civility_id);
342 unset($object->statut);
343 unset($object->state);
344 unset($object->state_id);
345 unset($object->state_code);
346 unset($object->region);
347 unset($object->region_code);
348 unset($object->country);
349 unset($object->country_id);
350 unset($object->country_code);
351 unset($object->barcode_type);
352 unset($object->barcode_type_code);
353 unset($object->barcode_type_label);
354 unset($object->barcode_type_coder);
355 unset($object->total_ht);
356 unset($object->total_tva);
357 unset($object->total_localtax1);
358 unset($object->total_localtax2);
359 unset($object->total_ttc);
360 unset($object->fk_account);
361 unset($object->comments);
362 unset($object->note);
363 unset($object->mode_reglement_id);
364 unset($object->cond_reglement_id);
365 unset($object->cond_reglement);
366 unset($object->shipping_method_id);
367 unset($object->fk_incoterms);
368 unset($object->label_incoterms);
369 unset($object->location_incoterms);
370 */
371
372 // If object has lines, remove $db property
373 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
374 $nboflines = count($object->lines);
375 for ($i = 0; $i < $nboflines; $i++) {
376 $this->_cleanObjectDatas($object->lines[$i]);
377
378 unset($object->lines[$i]->lines);
379 unset($object->lines[$i]->note);
380 }
381 }
382
383 return $object;
384 }
385
394 private function _validate($data)
395 {
396 if ($data === null) {
397 $data = array();
398 }
399 $partnership = array();
400 foreach ($this->partnership->fields as $field => $propfield) {
401 if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || empty($propfield['notnull']) || $propfield['notnull'] != 1) {
402 continue; // Not a mandatory field
403 }
404 if (!isset($data[$field])) {
405 throw new RestException(400, "$field field missing");
406 }
407 $partnership[$field] = $data[$field];
408 }
409 return $partnership;
410 }
411}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class for API REST v1.
Definition api.class.php:35
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
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.
Class for Partnership.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='')
List partnerships.
__construct()
Constructor.
post($request_data=null)
Create partnership object.
_validate($data)
Validate fields before create or update object.
put($id, $request_data=null)
Update partnership.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.