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
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, false) > 0) {
275 return $this->get($id);
276 } else {
277 throw new RestException(500, $this->partnership->error);
278 }
279 }
280
291 public function delete($id)
292 {
293 if (!DolibarrApiAccess::$user->rights->partnership->delete) {
294 throw new RestException(401);
295 }
296 $result = $this->partnership->fetch($id);
297 if (!$result) {
298 throw new RestException(404, 'Partnership not found');
299 }
300
301 if (!DolibarrApi::_checkAccessToResource('partnership', $this->partnership->id, 'partnership')) {
302 throw new RestException(401, 'Access to instance id='.$this->partnership->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
303 }
304
305 if (!$this->partnership->delete(DolibarrApiAccess::$user)) {
306 throw new RestException(500, 'Error when deleting Partnership : '.$this->partnership->error);
307 }
308
309 return array(
310 'success' => array(
311 'code' => 200,
312 'message' => 'Partnership deleted'
313 )
314 );
315 }
316
317
318 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
325 protected function _cleanObjectDatas($object)
326 {
327 // phpcs:enable
328 $object = parent::_cleanObjectDatas($object);
329
330 unset($object->rowid);
331 unset($object->canvas);
332
333 /*unset($object->name);
334 unset($object->lastname);
335 unset($object->firstname);
336 unset($object->civility_id);
337 unset($object->statut);
338 unset($object->state);
339 unset($object->state_id);
340 unset($object->state_code);
341 unset($object->region);
342 unset($object->region_code);
343 unset($object->country);
344 unset($object->country_id);
345 unset($object->country_code);
346 unset($object->barcode_type);
347 unset($object->barcode_type_code);
348 unset($object->barcode_type_label);
349 unset($object->barcode_type_coder);
350 unset($object->total_ht);
351 unset($object->total_tva);
352 unset($object->total_localtax1);
353 unset($object->total_localtax2);
354 unset($object->total_ttc);
355 unset($object->fk_account);
356 unset($object->comments);
357 unset($object->note);
358 unset($object->mode_reglement_id);
359 unset($object->cond_reglement_id);
360 unset($object->cond_reglement);
361 unset($object->shipping_method_id);
362 unset($object->fk_incoterms);
363 unset($object->label_incoterms);
364 unset($object->location_incoterms);
365 */
366
367 // If object has lines, remove $db property
368 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
369 $nboflines = count($object->lines);
370 for ($i = 0; $i < $nboflines; $i++) {
371 $this->_cleanObjectDatas($object->lines[$i]);
372
373 unset($object->lines[$i]->lines);
374 unset($object->lines[$i]->note);
375 }
376 }
377
378 return $object;
379 }
380
389 private function _validate($data)
390 {
391 $partnership = array();
392 foreach ($this->partnership->fields as $field => $propfield) {
393 if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
394 continue; // Not a mandatory field
395 }
396 if (!isset($data[$field])) {
397 throw new RestException(400, "$field field missing");
398 }
399 $partnership[$field] = $data[$field];
400 }
401 return $partnership;
402 }
403}
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.