20use Luracast\Restler\RestException;
33 public static $FIELDS = array(
51 require_once DOL_DOCUMENT_ROOT.
'/webhook/class/target.class.php';
53 $this->target =
new Target($this->db);
66 public function get(
$id)
87 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
91 if (!DolibarrApiAccess::$user->hasRight(
'webhook',
'webhook_target',
'read')) {
92 throw new RestException(403);
95 $sql =
"SELECT t.rowid";
96 $sql .=
" FROM ".MAIN_DB_PREFIX.
"webhook_target as t";
103 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
108 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
110 $sql .= $this->db->order($sortfield, $sortorder);
115 $offset = $limit * $page;
117 $sql .= $this->db->plimit($limit + 1, $offset);
120 $result = $this->db->query($sql);
122 $num = $this->db->num_rows($result);
123 $min = min($num, ($limit <= 0 ? $num : $limit));
126 $obj = $this->db->fetch_object($result);
127 $target_static =
new Target($this->db);
128 if ($target_static->fetch($obj->rowid)) {
134 throw new RestException(503,
'Error when retrieve targets : '.$this->db->lasterror());
136 if (!count($obj_ret)) {
137 throw new RestException(404,
'Targets not found');
141 if ($pagination_data) {
142 $totalsResult = $this->db->query($sqlTotals);
143 $total = $this->db->fetch_object($totalsResult)->total;
148 $obj_ret[
'data'] = $tmp;
149 $obj_ret[
'pagination'] = [
150 'total' => (int) $total,
152 'page_count' => ceil((
int) $total / $limit),
168 public function post($request_data =
null)
170 if (!DolibarrApiAccess::$user->hasRight(
'webhook',
'webhook_target',
'write')) {
171 throw new RestException(403);
174 if (!is_array($request_data)) {
175 $request_data = array();
181 foreach ($request_data as $field => $value) {
182 $this->target->$field = $this->
_checkValForAPI($field, $value, $this->target);
185 if (!array_key_exists(
'status', $request_data)) {
186 $this->target->status = 1;
189 if ($this->target->create(DolibarrApiAccess::$user) < 0) {
190 throw new RestException(500,
'Error creating target', array_merge(array($this->target->error), $this->target->errors));
193 return $this->target->id;
209 public function put(
$id, $request_data =
null)
211 if (!DolibarrApiAccess::$user->hasRight(
'webhook',
'webhook_target',
'write')) {
212 throw new RestException(403);
215 $result = $this->target->fetch(
$id);
217 throw new RestException(404,
'Target not found');
220 foreach ($request_data as $field => $value) {
221 if ($field ==
'id') {
224 $this->target->$field = $this->
_checkValForAPI($field, $value, $this->target);
227 if ($this->target->update(DolibarrApiAccess::$user, 1) > 0) {
228 return $this->
get(
$id);
230 throw new RestException(500, $this->target->error);
242 public function delete(
$id)
244 if (!DolibarrApiAccess::$user->hasRight(
'webhook',
'webhook_target',
'delete')) {
245 throw new RestException(403);
248 $result = $this->target->fetch(
$id);
250 throw new RestException(404,
'Target not found');
253 $res = $this->target->delete(DolibarrApiAccess::$user);
255 throw new RestException(500,
"Can't delete target, error occurs");
261 'message' =>
'Target deleted'
277 if (!DolibarrApiAccess::$user->hasRight(
'webhook',
'webhook_target',
'read')) {
278 throw new RestException(403);
283 $sql =
"SELECT c.code, c.label FROM ".MAIN_DB_PREFIX.
"c_action_trigger as c ORDER BY c.rang ASC";
284 $resql = $this->db->query($sql);
286 $num = $this->db->num_rows($resql);
289 $obj = $this->db->fetch_object($resql);
290 $triggers[$obj->code] = $obj->label;
294 throw new RestException(500,
"Can't get list of triggers");
310 if ($data ===
null) {
314 foreach (self::$FIELDS as $field) {
315 if (!isset($data[$field])) {
316 throw new RestException(400,
"$field field missing");
318 $target[$field] = $data[$field];
340 unset(
$object->array_languages);
342 unset(
$object->linkedObjectsIds);
356 unset(
$object->barcode_type_coder);
357 unset(
$object->mode_reglement_id);
358 unset(
$object->cond_reglement_id);
359 unset(
$object->demand_reason_id);
360 unset(
$object->transport_mode_id);
361 unset(
$object->shipping_method_id);
362 unset(
$object->shipping_method);
363 unset(
$object->fk_multicurrency);
364 unset(
$object->multicurrency_code);
365 unset(
$object->multicurrency_tx);
366 unset(
$object->multicurrency_total_ht);
367 unset(
$object->multicurrency_total_tva);
368 unset(
$object->multicurrency_total_ttc);
369 unset(
$object->multicurrency_total_localtax1);
370 unset(
$object->multicurrency_total_localtax2);
375 unset(
$object->total_localtax1);
376 unset(
$object->total_localtax2);
379 unset(
$object->actiontypecode);
384 unset(
$object->date_validation);
385 unset(
$object->date_modification);
387 unset(
$object->user_creation_id);
388 unset(
$object->user_validation_id);
389 unset(
$object->user_closing_id);
390 unset(
$object->user_modification_id);
394 unset(
$object->cond_reglement_supplier_id);
395 unset(
$object->deposit_percent);
396 unset(
$object->retained_warranty_fk_cond_reglement);
414 private function _fetch($rowid, $ref =
'')
416 if (!DolibarrApiAccess::$user->hasRight(
'webhook',
'webhook_target',
'read')) {
417 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
'. No read permission on target.');
421 $result = $this->target->initAsSpecimen();
423 $result = $this->target->fetch($rowid, $ref);
427 throw new RestException(404,
'Target not found');
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
_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.
_fetch($rowid, $ref='')
Fetch properties of a target object.
listOfTriggers()
Get the list of all available triggers.
put($id, $request_data=null)
Update target.
post($request_data=null)
Create target object.
_validate($data)
Validate fields before create or update object.
__construct()
Constructor.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='', $pagination_data=false)
List targets.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria