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