19use Luracast\Restler\RestException;
32 public static $FIELDS = array(
50 require_once DOL_DOCUMENT_ROOT.
'/webhook/class/target.class.php';
52 $this->target =
new Target($this->db);
65 public function get(
$id)
86 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
90 if (!DolibarrApiAccess::$user->hasRight(
'webhook',
'webhook_target',
'read')) {
91 throw new RestException(403);
94 $sql =
"SELECT t.rowid";
95 $sql .=
" FROM ".MAIN_DB_PREFIX.
"webhook_target as t";
102 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
107 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
109 $sql .= $this->db->order($sortfield, $sortorder);
114 $offset = $limit * $page;
116 $sql .= $this->db->plimit($limit + 1, $offset);
119 $result = $this->db->query($sql);
121 $num = $this->db->num_rows($result);
122 $min = min($num, ($limit <= 0 ? $num : $limit));
125 $obj = $this->db->fetch_object($result);
126 $target_static =
new Target($this->db);
127 if ($target_static->fetch($obj->rowid)) {
133 throw new RestException(503,
'Error when retrieve targets : '.$this->db->lasterror());
135 if (!count($obj_ret)) {
136 throw new RestException(404,
'Targets not found');
140 if ($pagination_data) {
141 $totalsResult = $this->db->query($sqlTotals);
142 $total = $this->db->fetch_object($totalsResult)->total;
147 $obj_ret[
'data'] = $tmp;
148 $obj_ret[
'pagination'] = [
149 'total' => (int) $total,
151 'page_count' => ceil((
int) $total / $limit),
167 public function post($request_data =
null)
169 if (!DolibarrApiAccess::$user->hasRight(
'webhook',
'webhook_target',
'write')) {
170 throw new RestException(403);
173 if (!is_array($request_data)) {
174 $request_data = array();
180 foreach ($request_data as $field => $value) {
181 $this->target->$field = $this->
_checkValForAPI($field, $value, $this->target);
184 if (!array_key_exists(
'status', $request_data)) {
185 $this->target->status = 1;
188 if ($this->target->create(DolibarrApiAccess::$user) < 0) {
189 throw new RestException(500,
'Error creating target', array_merge(array($this->target->error), $this->target->errors));
192 return $this->target->id;
208 public function put(
$id, $request_data =
null)
210 if (!DolibarrApiAccess::$user->hasRight(
'webhook',
'webhook_target',
'write')) {
211 throw new RestException(403);
214 $result = $this->target->fetch(
$id);
216 throw new RestException(404,
'Target not found');
219 foreach ($request_data as $field => $value) {
220 if ($field ==
'id') {
223 $this->target->$field = $this->
_checkValForAPI($field, $value, $this->target);
226 if ($this->target->update(DolibarrApiAccess::$user, 1) > 0) {
227 return $this->
get(
$id);
229 throw new RestException(500, $this->target->error);
241 public function delete(
$id)
243 if (!DolibarrApiAccess::$user->hasRight(
'webhook',
'webhook_target',
'delete')) {
244 throw new RestException(403);
247 $result = $this->target->fetch(
$id);
249 throw new RestException(404,
'Target not found');
252 $res = $this->target->delete(DolibarrApiAccess::$user);
254 throw new RestException(500,
"Can't delete target, error occurs");
260 'message' =>
'Target deleted'
276 if (!DolibarrApiAccess::$user->hasRight(
'webhook',
'webhook_target',
'read')) {
277 throw new RestException(403);
282 $sql =
"SELECT c.code, c.label FROM ".MAIN_DB_PREFIX.
"c_action_trigger as c ORDER BY c.rang ASC";
283 $resql = $this->db->query($sql);
285 $num = $this->db->num_rows($resql);
288 $obj = $this->db->fetch_object($resql);
289 $triggers[$obj->code] = $obj->label;
293 throw new RestException(500,
"Can't get list of triggers");
309 if ($data ===
null) {
313 foreach (self::$FIELDS as $field) {
314 if (!isset($data[$field])) {
315 throw new RestException(400,
"$field field missing");
317 $target[$field] = $data[$field];
336 unset(
$object->array_languages);
338 unset(
$object->linkedObjectsIds);
352 unset(
$object->barcode_type_coder);
353 unset(
$object->mode_reglement_id);
354 unset(
$object->cond_reglement_id);
355 unset(
$object->demand_reason_id);
356 unset(
$object->transport_mode_id);
357 unset(
$object->shipping_method_id);
358 unset(
$object->shipping_method);
359 unset(
$object->fk_multicurrency);
360 unset(
$object->multicurrency_code);
361 unset(
$object->multicurrency_tx);
362 unset(
$object->multicurrency_total_ht);
363 unset(
$object->multicurrency_total_tva);
364 unset(
$object->multicurrency_total_ttc);
365 unset(
$object->multicurrency_total_localtax1);
366 unset(
$object->multicurrency_total_localtax2);
371 unset(
$object->total_localtax1);
372 unset(
$object->total_localtax2);
375 unset(
$object->actiontypecode);
380 unset(
$object->date_validation);
381 unset(
$object->date_modification);
385 unset(
$object->user_creation_id);
387 unset(
$object->user_validation);
388 unset(
$object->user_validation_id);
389 unset(
$object->user_closing_id);
390 unset(
$object->user_modification);
391 unset(
$object->user_modification_id);
395 unset(
$object->cond_reglement_supplier_id);
396 unset(
$object->deposit_percent);
397 unset(
$object->retained_warranty_fk_cond_reglement);
415 private function _fetch($rowid, $ref =
'')
417 if (!DolibarrApiAccess::$user->hasRight(
'webhook',
'webhook_target',
'read')) {
418 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
'. No read permission on target.');
422 $result = $this->target->initAsSpecimen();
424 $result = $this->target->fetch($rowid, $ref);
428 throw new RestException(404,
'Target not found');
$id
Support class for third parties, contacts, members, users or resources.
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $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.
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