dolibarr 21.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 *
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
18use Luracast\Restler\RestException;
19
20dol_include_once('/partnership/class/partnership.class.php');
21
22
23
37{
41 public $partnership;
42
49 public function __construct()
50 {
51 global $db;
52 $this->db = $db;
53 $this->partnership = new Partnership($this->db);
54 }
55
69 public function get($id)
70 {
71 if (!DolibarrApiAccess::$user->hasRight('partnership', 'read')) {
72 throw new RestException(403);
73 }
74
75 $result = $this->partnership->fetch($id);
76 if (!$result) {
77 throw new RestException(404, 'Partnership not found');
78 }
79
80 if (!DolibarrApi::_checkAccessToResource('partnership', $this->partnership->id, 'partnership')) {
81 throw new RestException(403, 'Access to instance id='.$this->partnership->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
82 }
83
84 return $this->_cleanObjectDatas($this->partnership);
85 }
86
87
105 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
106 {
107 $obj_ret = array();
108 $tmpobject = new Partnership($this->db);
109
110 if (!DolibarrApiAccess::$user->hasRight('partnership', 'read')) {
111 throw new RestException(403);
112 }
113
114 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : 0;
115
116 $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
117
118 // If the internal user must only see his customers, force searching by him
119 $search_sale = 0;
120 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socid) {
121 $search_sale = DolibarrApiAccess::$user->id;
122 }
123
124 $sql = "SELECT t.rowid";
125 $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." AS t";
126 $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
127 $sql .= " WHERE 1 = 1";
128 if ($tmpobject->ismultientitymanaged) {
129 $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
130 }
131 if ($restrictonsocid && $socid) {
132 $sql .= " AND t.fk_soc = ".((int) $socid);
133 }
134 // Search on sale representative
135 if ($search_sale && $search_sale != '-1') {
136 if ($search_sale == -2) {
137 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
138 } elseif ($search_sale > 0) {
139 $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).")";
140 }
141 }
142 if ($sqlfilters) {
143 $errormessage = '';
144 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
145 if ($errormessage) {
146 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
147 }
148 }
149
150 $sql .= $this->db->order($sortfield, $sortorder);
151 if ($limit) {
152 if ($page < 0) {
153 $page = 0;
154 }
155 $offset = $limit * $page;
156
157 $sql .= $this->db->plimit($limit + 1, $offset);
158 }
159
160 $result = $this->db->query($sql);
161 $i = 0;
162 if ($result) {
163 $num = $this->db->num_rows($result);
164 while ($i < $num) {
165 $obj = $this->db->fetch_object($result);
166 $tmp_object = new Partnership($this->db);
167 if ($tmp_object->fetch($obj->rowid)) {
168 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($tmp_object), $properties);
169 }
170 $i++;
171 }
172 } else {
173 throw new RestException(503, 'Error when retrieving partnership list: '.$this->db->lasterror());
174 }
175
176 return $obj_ret;
177 }
178
189 public function post($request_data = null)
190 {
191 if (!DolibarrApiAccess::$user->hasRight('partnership', 'write')) {
192 throw new RestException(403);
193 }
194
195 // Check mandatory fields
196 $result = $this->_validate($request_data);
197
198 foreach ($request_data as $field => $value) {
199 if ($field === 'caller') {
200 // 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
201 $this->partnership->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
202 continue;
203 }
204
205 $this->partnership->$field = $this->_checkValForAPI($field, $value, $this->partnership);
206 }
207
208 // Clean data
209 // $this->partnership->abc = sanitizeVal($this->partnership->abc, 'alphanohtml');
210
211 if ($this->partnership->create(DolibarrApiAccess::$user)<0) {
212 throw new RestException(500, "Error creating Partnership", array_merge(array($this->partnership->error), $this->partnership->errors));
213 }
214 return $this->partnership->id;
215 }
216
228 public function put($id, $request_data = null)
229 {
230 if (!DolibarrApiAccess::$user->hasRight('partnership', 'write')) {
231 throw new RestException(403);
232 }
233
234 $result = $this->partnership->fetch($id);
235 if (!$result) {
236 throw new RestException(404, 'Partnership not found');
237 }
238
239 if (!DolibarrApi::_checkAccessToResource('partnership', $this->partnership->id, 'partnership')) {
240 throw new RestException(403, 'Access to instance id='.$this->partnership->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
241 }
242
243 foreach ($request_data as $field => $value) {
244 if ($field == 'id') {
245 continue;
246 }
247 if ($field === 'caller') {
248 // 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
249 $this->partnership->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
250 continue;
251 }
252
253 $this->partnership->$field = $this->_checkValForAPI($field, $value, $this->partnership);
254 }
255
256 // Clean data
257 // $this->partnership->abc = sanitizeVal($this->partnership->abc, 'alphanohtml');
258
259 if ($this->partnership->update(DolibarrApiAccess::$user, false) > 0) {
260 return $this->get($id);
261 } else {
262 throw new RestException(500, $this->partnership->error);
263 }
264 }
265
276 public function delete($id)
277 {
278 if (!DolibarrApiAccess::$user->hasRight('partnership', 'delete')) {
279 throw new RestException(403);
280 }
281 $result = $this->partnership->fetch($id);
282 if (!$result) {
283 throw new RestException(404, 'Partnership not found');
284 }
285
286 if (!DolibarrApi::_checkAccessToResource('partnership', $this->partnership->id, 'partnership')) {
287 throw new RestException(403, 'Access to instance id='.$this->partnership->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
288 }
289
290 if (!$this->partnership->delete(DolibarrApiAccess::$user)) {
291 throw new RestException(500, 'Error when deleting Partnership : '.$this->partnership->error);
292 }
293
294 return array(
295 'success' => array(
296 'code' => 200,
297 'message' => 'Partnership deleted'
298 )
299 );
300 }
301
302
303 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
310 protected function _cleanObjectDatas($object)
311 {
312 // phpcs:enable
313 $object = parent::_cleanObjectDatas($object);
314
315 unset($object->rowid);
316 unset($object->canvas);
317
318 /*unset($object->name);
319 unset($object->lastname);
320 unset($object->firstname);
321 unset($object->civility_id);
322 unset($object->statut);
323 unset($object->state);
324 unset($object->state_id);
325 unset($object->state_code);
326 unset($object->region);
327 unset($object->region_code);
328 unset($object->country);
329 unset($object->country_id);
330 unset($object->country_code);
331 unset($object->barcode_type);
332 unset($object->barcode_type_code);
333 unset($object->barcode_type_label);
334 unset($object->barcode_type_coder);
335 unset($object->total_ht);
336 unset($object->total_tva);
337 unset($object->total_localtax1);
338 unset($object->total_localtax2);
339 unset($object->total_ttc);
340 unset($object->fk_account);
341 unset($object->comments);
342 unset($object->note);
343 unset($object->mode_reglement_id);
344 unset($object->cond_reglement_id);
345 unset($object->cond_reglement);
346 unset($object->shipping_method_id);
347 unset($object->fk_incoterms);
348 unset($object->label_incoterms);
349 unset($object->location_incoterms);
350 */
351
352 // If object has lines, remove $db property
353 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
354 $nboflines = count($object->lines);
355 for ($i = 0; $i < $nboflines; $i++) {
356 $this->_cleanObjectDatas($object->lines[$i]);
357
358 unset($object->lines[$i]->lines);
359 unset($object->lines[$i]->note);
360 }
361 }
362
363 return $object;
364 }
365
374 private function _validate($data)
375 {
376 $partnership = array();
377 foreach ($this->partnership->fields as $field => $propfield) {
378 if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
379 continue; // Not a mandatory field
380 }
381 if (!isset($data[$field])) {
382 throw new RestException(400, "$field field missing");
383 }
384 $partnership[$field] = $data[$field];
385 }
386 return $partnership;
387 }
388}
$id
Definition account.php:39
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class for API REST v1.
Definition api.class.php:30
_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.
Definition api.class.php:82
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.
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.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.