1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* Copyright (C) 2015 Jean-François Ferry <[email protected]> |
4
|
|
|
* Copyright (C) 2016 Laurent Destailleur <[email protected]> |
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
|
|
|
|
20
|
|
|
namespace DoliModules\Agenda\Api; |
21
|
|
|
|
22
|
|
|
use DoliCore\Api\DolibarrApi; |
23
|
|
|
use Luracast\Restler\RestException; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* API class for Agenda Events |
28
|
|
|
* |
29
|
|
|
* @access protected |
30
|
|
|
* @class DolibarrApiAccess {@requires user,external} |
31
|
|
|
*/ |
32
|
|
|
class AgendaEvents extends DolibarrApi |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var array $FIELDS Mandatory fields, checked when create and update object |
36
|
|
|
*/ |
37
|
|
|
public static $FIELDS = array( |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var ActionComm $actioncomm {@type ActionComm} |
42
|
|
|
*/ |
43
|
|
|
public $actioncomm; |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Constructor |
48
|
|
|
*/ |
49
|
|
|
public function __construct() |
50
|
|
|
{ |
51
|
|
|
global $db, $conf; |
52
|
|
|
$this->db = $db; |
53
|
|
|
$this->actioncomm = new ActionComm($this->db); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get properties of a Agenda Events object |
58
|
|
|
* |
59
|
|
|
* Return an array with Agenda Events information |
60
|
|
|
* |
61
|
|
|
* @param int $id ID of Agenda Events |
62
|
|
|
* @return Object Object with cleaned properties |
63
|
|
|
* |
64
|
|
|
* @throws RestException |
65
|
|
|
*/ |
66
|
|
|
public function get($id) |
67
|
|
|
{ |
68
|
|
|
if (!DolibarrApiAccess::$user->hasRight('agenda', 'myactions', 'read')) { |
|
|
|
|
69
|
|
|
throw new RestException(401, "Insufficient rights to read an event"); |
70
|
|
|
} |
71
|
|
|
if ($id === 0) { |
72
|
|
|
$result = $this->actioncomm->initAsSpecimen(); |
73
|
|
|
} else { |
74
|
|
|
$result = $this->actioncomm->fetch($id); |
75
|
|
|
if ($result) { |
76
|
|
|
$this->actioncomm->fetch_optionals(); |
77
|
|
|
$this->actioncomm->fetchObjectLinked(); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
if (!$result) { |
81
|
|
|
throw new RestException(404, 'Agenda Events not found'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (!DolibarrApiAccess::$user->hasRight('agenda', 'allactions', 'read') && $this->actioncomm->userownerid != DolibarrApiAccess::$user->id) { |
85
|
|
|
throw new RestException(401, 'Insufficient rights to read event of this owner id. Your id is ' . DolibarrApiAccess::$user->id); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (!DolibarrApi::_checkAccessToResource('agenda', $this->actioncomm->id, 'actioncomm', '', 'fk_soc', 'id')) { |
89
|
|
|
throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
90
|
|
|
} |
91
|
|
|
return $this->_cleanObjectDatas($this->actioncomm); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* List Agenda Events |
96
|
|
|
* |
97
|
|
|
* Get a list of Agenda Events |
98
|
|
|
* |
99
|
|
|
* @param string $sortfield Sort field |
100
|
|
|
* @param string $sortorder Sort order |
101
|
|
|
* @param int $limit Limit for list |
102
|
|
|
* @param int $page Page number |
103
|
|
|
* @param string $user_ids User ids filter field (owners of event). Example: '1' or '1,2,3' {@pattern /^[0-9,]*$/i} |
104
|
|
|
* @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.label:like:'%dol%') and (t.datec:<:'20160101')" |
105
|
|
|
* @param string $properties Restrict the data returned to these properties. Ignored if empty. Comma separated list of properties names |
106
|
|
|
* @return array Array of Agenda Events objects |
107
|
|
|
*/ |
108
|
|
|
public function index($sortfield = "t.id", $sortorder = 'ASC', $limit = 100, $page = 0, $user_ids = '', $sqlfilters = '', $properties = '') |
109
|
|
|
{ |
110
|
|
|
global $db, $conf; |
111
|
|
|
|
112
|
|
|
$obj_ret = array(); |
113
|
|
|
|
114
|
|
|
if (!DolibarrApiAccess::$user->hasRight('agenda', 'myactions', 'read')) { |
115
|
|
|
throw new RestException(401, "Insufficient rights to read events"); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// case of external user |
119
|
|
|
$socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : 0; |
120
|
|
|
|
121
|
|
|
// If the internal user must only see his customers, force searching by him |
122
|
|
|
$search_sale = 0; |
123
|
|
|
if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socid) { |
124
|
|
|
$search_sale = DolibarrApiAccess::$user->id; |
125
|
|
|
} |
126
|
|
|
if (!isModEnabled('societe')) { |
127
|
|
|
$search_sale = 0; // If module thirdparty not enabled, sale representative is something that does not exists |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$sql = "SELECT t.id as rowid"; |
131
|
|
|
$sql .= " FROM " . MAIN_DB_PREFIX . "actioncomm AS t"; |
132
|
|
|
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "actioncomm_extrafields AS ef ON (ef.fk_object = t.id)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields |
133
|
|
|
$sql .= ' WHERE t.entity IN (' . getEntity('agenda') . ')'; |
134
|
|
|
if ($user_ids) { |
135
|
|
|
$sql .= " AND t.fk_user_action IN (" . $this->db->sanitize($user_ids) . ")"; |
136
|
|
|
} |
137
|
|
|
if ($socid > 0) { |
138
|
|
|
$sql .= " AND t.fk_soc = " . ((int) $socid); |
139
|
|
|
} |
140
|
|
|
// Search on sale representative |
141
|
|
|
if ($search_sale && $search_sale != '-1') { |
142
|
|
|
if ($search_sale == -2) { |
143
|
|
|
$sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM " . MAIN_DB_PREFIX . "societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)"; |
144
|
|
|
} elseif ($search_sale > 0) { |
145
|
|
|
$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) . ")"; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
// Add sql filters |
149
|
|
|
if ($sqlfilters) { |
150
|
|
|
$errormessage = ''; |
151
|
|
|
$sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage); |
152
|
|
|
if ($errormessage) { |
153
|
|
|
throw new RestException(400, 'Error when validating parameter sqlfilters -> ' . $errormessage); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$sql .= $this->db->order($sortfield, $sortorder); |
158
|
|
|
if ($limit) { |
159
|
|
|
if ($page < 0) { |
160
|
|
|
$page = 0; |
161
|
|
|
} |
162
|
|
|
$offset = $limit * $page; |
163
|
|
|
|
164
|
|
|
$sql .= $this->db->plimit($limit + 1, $offset); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$result = $this->db->query($sql); |
168
|
|
|
|
169
|
|
|
if ($result) { |
170
|
|
|
$i = 0; |
171
|
|
|
$num = $this->db->num_rows($result); |
172
|
|
|
$min = min($num, ($limit <= 0 ? $num : $limit)); |
173
|
|
|
while ($i < $min) { |
174
|
|
|
$obj = $this->db->fetch_object($result); |
175
|
|
|
$actioncomm_static = new ActionComm($this->db); |
176
|
|
|
if ($actioncomm_static->fetch($obj->rowid)) { |
177
|
|
|
$obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($actioncomm_static), $properties); |
178
|
|
|
} |
179
|
|
|
$i++; |
180
|
|
|
} |
181
|
|
|
} else { |
182
|
|
|
throw new RestException(503, 'Error when retrieve Agenda Event list : ' . $this->db->lasterror()); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $obj_ret; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Create Agenda Event object |
190
|
|
|
* |
191
|
|
|
* @param array $request_data Request data |
192
|
|
|
* @return int ID of Agenda Event |
193
|
|
|
*/ |
194
|
|
|
public function post($request_data = null) |
195
|
|
|
{ |
196
|
|
|
if (!DolibarrApiAccess::$user->hasRight('agenda', 'myactions', 'create')) { |
197
|
|
|
throw new RestException(401, "Insufficient rights to create your Agenda Event"); |
198
|
|
|
} |
199
|
|
|
if (!DolibarrApiAccess::$user->hasRight('agenda', 'allactions', 'create') && DolibarrApiAccess::$user->id != $request_data['userownerid']) { |
200
|
|
|
throw new RestException(401, "Insufficient rights to create an Agenda Event for owner id " . $request_data['userownerid'] . ' Your id is ' . DolibarrApiAccess::$user->id); |
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->actioncomm->context['caller'] = $request_data['caller']; |
210
|
|
|
continue; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
$this->actioncomm->$field = $this->_checkValForAPI($field, $value, $this->actioncomm); |
214
|
|
|
} |
215
|
|
|
/*if (isset($request_data["lines"])) { |
216
|
|
|
$lines = array(); |
217
|
|
|
foreach ($request_data["lines"] as $line) { |
218
|
|
|
array_push($lines, (object) $line); |
219
|
|
|
} |
220
|
|
|
$this->expensereport->lines = $lines; |
221
|
|
|
}*/ |
222
|
|
|
|
223
|
|
|
if ($this->actioncomm->create(DolibarrApiAccess::$user) < 0) { |
224
|
|
|
throw new RestException(500, "Error creating event", array_merge(array($this->actioncomm->error), $this->actioncomm->errors)); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $this->actioncomm->id; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Update Agenda Event general fields |
233
|
|
|
* |
234
|
|
|
* @param int $id Id of Agenda Event to update |
235
|
|
|
* @param array $request_data Datas |
236
|
|
|
* @return Object|false Object with cleaned properties |
237
|
|
|
*/ |
238
|
|
|
public function put($id, $request_data = null) |
239
|
|
|
{ |
240
|
|
|
if (!DolibarrApiAccess::$user->hasRight('agenda', 'myactions', 'create')) { |
241
|
|
|
throw new RestException(401, "Insufficient rights to create your Agenda Event"); |
242
|
|
|
} |
243
|
|
|
if (!DolibarrApiAccess::$user->hasRight('agenda', 'allactions', 'create') && DolibarrApiAccess::$user->id != $request_data['userownerid']) { |
244
|
|
|
throw new RestException(401, "Insufficient rights to create an Agenda Event for owner id " . $request_data['userownerid'] . ' Your id is ' . DolibarrApiAccess::$user->id); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
$result = $this->actioncomm->fetch($id); |
248
|
|
|
if ($result) { |
249
|
|
|
$this->actioncomm->fetch_optionals(); |
250
|
|
|
$this->actioncomm->fetch_userassigned(); |
251
|
|
|
$this->actioncomm->oldcopy = clone $this->actioncomm; |
252
|
|
|
} |
253
|
|
|
if (!$result) { |
254
|
|
|
throw new RestException(404, 'actioncomm not found'); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
if (!DolibarrApi::_checkAccessToResource('actioncomm', $this->actioncomm->id, 'actioncomm', '', 'fk_soc', 'id')) { |
258
|
|
|
throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
259
|
|
|
} |
260
|
|
|
foreach ($request_data as $field => $value) { |
261
|
|
|
if ($field == 'id') { |
262
|
|
|
continue; |
263
|
|
|
} |
264
|
|
|
if ($field === 'caller') { |
265
|
|
|
// 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 |
266
|
|
|
$this->actioncomm->context['caller'] = $request_data['caller']; |
267
|
|
|
continue; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
$this->actioncomm->$field = $this->_checkValForAPI($field, $value, $this->actioncomm); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
if ($this->actioncomm->update(DolibarrApiAccess::$user, 1) > 0) { |
274
|
|
|
return $this->get($id); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
return false; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Delete Agenda Event |
282
|
|
|
* |
283
|
|
|
* @param int $id Agenda Event ID |
284
|
|
|
* |
285
|
|
|
* @return array |
286
|
|
|
*/ |
287
|
|
|
public function delete($id) |
288
|
|
|
{ |
289
|
|
|
if (!DolibarrApiAccess::$user->hasRight('agenda', 'myactions', 'delete')) { |
290
|
|
|
throw new RestException(401, "Insufficient rights to delete your Agenda Event"); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
$result = $this->actioncomm->fetch($id); |
294
|
|
|
if ($result) { |
295
|
|
|
$this->actioncomm->fetch_optionals(); |
296
|
|
|
$this->actioncomm->fetch_userassigned(); |
297
|
|
|
$this->actioncomm->oldcopy = clone $this->actioncomm; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
if (!DolibarrApiAccess::$user->hasRight('agenda', 'allactions', 'delete') && DolibarrApiAccess::$user->id != $this->actioncomm->userownerid) { |
301
|
|
|
throw new RestException(401, "Insufficient rights to delete an Agenda Event of owner id " . $this->actioncomm->userownerid . ' Your id is ' . DolibarrApiAccess::$user->id); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
if (!$result) { |
305
|
|
|
throw new RestException(404, 'Agenda Event not found'); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
if (!DolibarrApi::_checkAccessToResource('actioncomm', $this->actioncomm->id, 'actioncomm', '', 'fk_soc', 'id')) { |
309
|
|
|
throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
if (!$this->actioncomm->delete(DolibarrApiAccess::$user)) { |
313
|
|
|
throw new RestException(500, 'Error when delete Agenda Event : ' . $this->actioncomm->error); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
return array( |
317
|
|
|
'success' => array( |
318
|
|
|
'code' => 200, |
319
|
|
|
'message' => 'Agenda Event deleted' |
320
|
|
|
) |
321
|
|
|
); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Validate fields before create or update object |
326
|
|
|
* |
327
|
|
|
* @param array $data Array with data to verify |
328
|
|
|
* @return array |
329
|
|
|
* @throws RestException |
330
|
|
|
*/ |
331
|
|
|
private function _validate($data) |
332
|
|
|
{ |
333
|
|
|
$event = array(); |
334
|
|
|
foreach (AgendaEvents::$FIELDS as $field) { |
335
|
|
|
if (!isset($data[$field])) { |
336
|
|
|
throw new RestException(400, "$field field missing"); |
337
|
|
|
} |
338
|
|
|
$event[$field] = $data[$field]; |
339
|
|
|
} |
340
|
|
|
return $event; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore |
344
|
|
|
/** |
345
|
|
|
* Clean sensible object datas |
346
|
|
|
* |
347
|
|
|
* @param Object $object Object to clean |
348
|
|
|
* @return Object Object with cleaned properties |
349
|
|
|
*/ |
350
|
|
|
protected function _cleanObjectDatas($object) |
351
|
|
|
{ |
352
|
|
|
// phpcs:enable |
353
|
|
|
$object = parent::_cleanObjectDatas($object); |
354
|
|
|
|
355
|
|
|
unset($object->note); // alreaydy into note_private |
356
|
|
|
unset($object->usermod); |
357
|
|
|
unset($object->libelle); |
358
|
|
|
unset($object->context); |
359
|
|
|
unset($object->canvas); |
360
|
|
|
unset($object->contact); |
361
|
|
|
unset($object->contact_id); |
362
|
|
|
unset($object->thirdparty); |
363
|
|
|
unset($object->user); |
364
|
|
|
unset($object->origin); |
365
|
|
|
unset($object->origin_id); |
366
|
|
|
unset($object->ref_ext); |
367
|
|
|
unset($object->statut); |
368
|
|
|
unset($object->state_code); |
369
|
|
|
unset($object->state_id); |
370
|
|
|
unset($object->state); |
371
|
|
|
unset($object->region); |
372
|
|
|
unset($object->region_code); |
373
|
|
|
unset($object->country); |
374
|
|
|
unset($object->country_id); |
375
|
|
|
unset($object->country_code); |
376
|
|
|
unset($object->barcode_type); |
377
|
|
|
unset($object->barcode_type_code); |
378
|
|
|
unset($object->barcode_type_label); |
379
|
|
|
unset($object->barcode_type_coder); |
380
|
|
|
unset($object->mode_reglement_id); |
381
|
|
|
unset($object->cond_reglement_id); |
382
|
|
|
unset($object->cond_reglement); |
383
|
|
|
unset($object->fk_delivery_address); |
384
|
|
|
unset($object->shipping_method_id); |
385
|
|
|
unset($object->fk_account); |
386
|
|
|
unset($object->total_ht); |
387
|
|
|
unset($object->total_tva); |
388
|
|
|
unset($object->total_localtax1); |
389
|
|
|
unset($object->total_localtax2); |
390
|
|
|
unset($object->total_ttc); |
391
|
|
|
unset($object->fk_incoterms); |
392
|
|
|
unset($object->label_incoterms); |
393
|
|
|
unset($object->location_incoterms); |
394
|
|
|
unset($object->name); |
395
|
|
|
unset($object->lastname); |
396
|
|
|
unset($object->firstname); |
397
|
|
|
unset($object->civility_id); |
398
|
|
|
unset($object->contact); |
399
|
|
|
unset($object->societe); |
400
|
|
|
unset($object->demand_reason_id); |
401
|
|
|
unset($object->transport_mode_id); |
402
|
|
|
unset($object->region_id); |
403
|
|
|
unset($object->actions); |
404
|
|
|
unset($object->lines); |
405
|
|
|
|
406
|
|
|
return $object; |
407
|
|
|
} |
408
|
|
|
} |
409
|
|
|
|