dolibarr 25.0.0-alpha
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');
23require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
24
25
26
40{
44 public $partnership;
45
51 public function __construct()
52 {
53 global $db;
54 $this->db = $db;
55 $this->partnership = new Partnership($this->db);
56 }
57
71 public function get($id)
72 {
73 if (!DolibarrApiAccess::$user->hasRight('partnership', 'read')) {
74 throw new RestException(403);
75 }
76
77 $result = $this->partnership->fetch($id);
78 if (!$result) {
79 throw new RestException(404, 'Partnership not found');
80 }
81
82 if (!DolibarrApi::_checkAccessToResource('partnership', $this->partnership->id, 'partnership')) {
83 throw new RestException(403, 'Access to instance id='.$this->partnership->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
84 }
85
86 return $this->_cleanObjectDatas($this->partnership);
87 }
88
89
109 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
110 {
111 $obj_ret = array();
112 $tmpobject = new Partnership($this->db);
113
114 if (!DolibarrApiAccess::$user->hasRight('partnership', 'read')) {
115 throw new RestException(403);
116 }
117
118 $socid = DolibarrApiAccess::$user->socid ?: 0;
119
120 $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
121
122 // If the internal user must only see his customers, force searching by him
123 $search_sale = 0;
124 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socid) {
125 $search_sale = DolibarrApiAccess::$user->id;
126 }
127
128 $sql = "SELECT t.rowid";
129 $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." AS t";
130 $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
131 $sql .= " WHERE 1 = 1";
132 if ($tmpobject->ismultientitymanaged) {
133 $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
134 }
135 if ($restrictonsocid && $socid) {
136 $sql .= " AND t.fk_soc = ".((int) $socid);
137 }
138 // Search on sale representative
139 if ($search_sale && $search_sale != '-1') {
140 if ($search_sale == -2) {
141 $sql .= " AND ".getSalesRepresentativeSqlFilter('t.fk_soc', 0, 1);
142 } elseif ($search_sale > 0) {
143 $sql .= " AND ".getSalesRepresentativeSqlFilter('t.fk_soc', (int) $search_sale);
144 }
145 }
146 if ($sqlfilters) {
147 $errormessage = '';
148 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
149 if ($errormessage) {
150 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
151 }
152 }
153
154 $sql .= $this->db->order($sortfield, $sortorder);
155 if ($limit) {
156 if ($page < 0) {
157 $page = 0;
158 }
159 $offset = $limit * $page;
160
161 $sql .= $this->db->plimit($limit + 1, $offset);
162 }
163
164 $result = $this->db->query($sql);
165 $i = 0;
166 if ($result) {
167 $num = $this->db->num_rows($result);
168 $min = min($num, ($limit <= 0 ? $num : $limit));
169 while ($i < $min) {
170 $obj = $this->db->fetch_object($result);
171 $tmp_object = new Partnership($this->db);
172 if ($tmp_object->fetch($obj->rowid)) {
173 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($tmp_object), $properties);
174 }
175 $i++;
176 }
177 } else {
178 throw new RestException(503, 'Error when retrieving partnership list: '.$this->db->lasterror());
179 }
180
181 return $obj_ret;
182 }
183
196 public function post($request_data = null)
197 {
198 if (!DolibarrApiAccess::$user->hasRight('partnership', 'write')) {
199 throw new RestException(403);
200 }
201
202 // Check mandatory fields
203 $result = $this->_validate($request_data);
204
205 foreach ($request_data as $field => $value) {
206 if ($field === 'caller') {
207 // 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
208 $this->partnership->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
209 continue;
210 }
211
212 $this->partnership->$field = $this->_checkValForAPI($field, $value, $this->partnership);
213 }
214
215 // Clean data
216 // $this->partnership->abc = sanitizeVal($this->partnership->abc, 'alphanohtml');
217
218 if ($this->partnership->create(DolibarrApiAccess::$user) < 0) {
219 throw new RestException(500, "Error creating Partnership", array_merge(array($this->partnership->error), $this->partnership->errors));
220 }
221 return $this->partnership->id;
222 }
223
237 public function put($id, $request_data = null)
238 {
239 if (!DolibarrApiAccess::$user->hasRight('partnership', 'write')) {
240 throw new RestException(403);
241 }
242
243 $result = $this->partnership->fetch($id);
244 if (!$result) {
245 throw new RestException(404, 'Partnership not found');
246 }
247
248 if (!DolibarrApi::_checkAccessToResource('partnership', $this->partnership->id, 'partnership')) {
249 throw new RestException(403, 'Access to instance id='.$this->partnership->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
250 }
251
252 foreach ($request_data as $field => $value) {
253 if ($field == 'id') {
254 continue;
255 }
256 if ($field === 'caller') {
257 // 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
258 $this->partnership->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
259 continue;
260 }
261 if ($field == 'array_options' && is_array($value)) {
262 foreach ($value as $index => $val) {
263 $this->partnership->array_options[$index] = $this->_checkValExtrafieldsForAPI($index, $val, $this->partnership);
264 }
265 continue;
266 }
267
268 $this->partnership->$field = $this->_checkValForAPI($field, $value, $this->partnership);
269 }
270
271 // Clean data
272 // $this->partnership->abc = sanitizeVal($this->partnership->abc, 'alphanohtml');
273
274 if ($this->partnership->update(DolibarrApiAccess::$user, 0) > 0) {
275 return $this->get($id);
276 } else {
277 throw new RestException(500, $this->partnership->error);
278 }
279 }
280
293 public function delete($id)
294 {
295 if (!DolibarrApiAccess::$user->hasRight('partnership', 'delete')) {
296 throw new RestException(403);
297 }
298 $result = $this->partnership->fetch($id);
299 if (!$result) {
300 throw new RestException(404, 'Partnership not found');
301 }
302
303 if (!DolibarrApi::_checkAccessToResource('partnership', $this->partnership->id, 'partnership')) {
304 throw new RestException(403, 'Access to instance id='.$this->partnership->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
305 }
306
307 if (!$this->partnership->delete(DolibarrApiAccess::$user)) {
308 throw new RestException(500, 'Error when deleting Partnership : '.$this->partnership->error);
309 }
310
311 return array(
312 'success' => array(
313 'code' => 200,
314 'message' => 'Partnership deleted'
315 )
316 );
317 }
318
319
320 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
331 protected function _cleanObjectDatas($object)
332 {
333 // phpcs:enable
334 $object = parent::_cleanObjectDatas($object);
335
336 unset($object->rowid);
337 unset($object->canvas);
338
339 /*unset($object->name);
340 unset($object->lastname);
341 unset($object->firstname);
342 unset($object->civility_id);
343 unset($object->statut);
344 unset($object->state);
345 unset($object->state_id);
346 unset($object->state_code);
347 unset($object->region);
348 unset($object->region_code);
349 unset($object->country);
350 unset($object->country_id);
351 unset($object->country_code);
352 unset($object->barcode_type);
353 unset($object->barcode_type_code);
354 unset($object->barcode_type_label);
355 unset($object->barcode_type_coder);
356 unset($object->total_ht);
357 unset($object->total_tva);
358 unset($object->total_localtax1);
359 unset($object->total_localtax2);
360 unset($object->total_ttc);
361 unset($object->fk_account);
362 unset($object->comments);
363 unset($object->note);
364 unset($object->mode_reglement_id);
365 unset($object->cond_reglement_id);
366 unset($object->cond_reglement);
367 unset($object->shipping_method_id);
368 unset($object->fk_incoterms);
369 unset($object->label_incoterms);
370 unset($object->location_incoterms);
371 */
372
373 // If object has lines, remove $db property
374 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
375 $nboflines = count($object->lines);
376 for ($i = 0; $i < $nboflines; $i++) {
377 $this->_cleanObjectDatas($object->lines[$i]);
378
379 unset($object->lines[$i]->lines);
380 unset($object->lines[$i]->note);
381 }
382 }
383
384 return $object;
385 }
386
395 private function _validate($data)
396 {
397 if ($data === null) {
398 $data = array();
399 }
400 $partnership = array();
401 foreach ($this->partnership->fields as $field => $propfield) {
402 if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || empty($propfield['notnull']) || $propfield['notnull'] != 1) {
403 continue; // Not a mandatory field
404 }
405 if (!isset($data[$field])) {
406 throw new RestException(400, "$field field missing");
407 }
408 $partnership[$field] = $data[$field];
409 }
410 return $partnership;
411 }
412}
$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.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
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.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.