dolibarr 19.0.4
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->rights->partnership->read) {
72 throw new RestException(401);
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(401, '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 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->_filterObjectProperties($this->_cleanObjectDatas($tmp_object), $properties);
184 }
185 $i++;
186 }
187 } else {
188 throw new RestException(503, 'Error when retrieving partnership list: '.$this->db->lasterror());
189 }
190
191 return $obj_ret;
192 }
193
204 public function post($request_data = null)
205 {
206 if (!DolibarrApiAccess::$user->rights->partnership->write) {
207 throw new RestException(401);
208 }
209
210 // Check mandatory fields
211 $result = $this->_validate($request_data);
212
213 foreach ($request_data as $field => $value) {
214 if ($field === 'caller') {
215 // 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 whith the caller
216 $this->partnership->context['caller'] = $request_data['caller'];
217 continue;
218 }
219
220 $this->partnership->$field = $this->_checkValForAPI($field, $value, $this->partnership);
221 }
222
223 // Clean data
224 // $this->partnership->abc = sanitizeVal($this->partnership->abc, 'alphanohtml');
225
226 if ($this->partnership->create(DolibarrApiAccess::$user)<0) {
227 throw new RestException(500, "Error creating Partnership", array_merge(array($this->partnership->error), $this->partnership->errors));
228 }
229 return $this->partnership->id;
230 }
231
243 public function put($id, $request_data = null)
244 {
245 if (!DolibarrApiAccess::$user->rights->partnership->write) {
246 throw new RestException(401);
247 }
248
249 $result = $this->partnership->fetch($id);
250 if (!$result) {
251 throw new RestException(404, 'Partnership not found');
252 }
253
254 if (!DolibarrApi::_checkAccessToResource('partnership', $this->partnership->id, 'partnership')) {
255 throw new RestException(401, 'Access to instance id='.$this->partnership->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
256 }
257
258 foreach ($request_data as $field => $value) {
259 if ($field == 'id') {
260 continue;
261 }
262 if ($field === 'caller') {
263 // 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 whith the caller
264 $this->partnership->context['caller'] = $request_data['caller'];
265 continue;
266 }
267 if ($field == 'array_options' && is_array($value)) {
268 foreach ($value as $index => $val) {
269 $this->partnership->array_options[$index] = $this->_checkValForAPI($field, $val, $this->partnership);
270 }
271 continue;
272 }
273
274 $this->partnership->$field = $this->_checkValForAPI($field, $value, $this->partnership);
275 }
276
277 // Clean data
278 // $this->partnership->abc = sanitizeVal($this->partnership->abc, 'alphanohtml');
279
280 if ($this->partnership->update(DolibarrApiAccess::$user, false) > 0) {
281 return $this->get($id);
282 } else {
283 throw new RestException(500, $this->partnership->error);
284 }
285 }
286
297 public function delete($id)
298 {
299 if (!DolibarrApiAccess::$user->rights->partnership->delete) {
300 throw new RestException(401);
301 }
302 $result = $this->partnership->fetch($id);
303 if (!$result) {
304 throw new RestException(404, 'Partnership not found');
305 }
306
307 if (!DolibarrApi::_checkAccessToResource('partnership', $this->partnership->id, 'partnership')) {
308 throw new RestException(401, 'Access to instance id='.$this->partnership->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
309 }
310
311 if (!$this->partnership->delete(DolibarrApiAccess::$user)) {
312 throw new RestException(500, 'Error when deleting Partnership : '.$this->partnership->error);
313 }
314
315 return array(
316 'success' => array(
317 'code' => 200,
318 'message' => 'Partnership deleted'
319 )
320 );
321 }
322
323
324 // 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 $partnership = array();
398 foreach ($this->partnership->fields as $field => $propfield) {
399 if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
400 continue; // Not a mandatory field
401 }
402 if (!isset($data[$field])) {
403 throw new RestException(400, "$field field missing");
404 }
405 $partnership[$field] = $data[$field];
406 }
407 return $partnership;
408 }
409}
Class for API REST v1.
Definition api.class.php:31
_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:85
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.