18use 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)
85 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
89 if (!DolibarrApiAccess::$user->hasRight(
'webhook',
'webhook_target',
'read')) {
90 throw new RestException(403);
93 $sql =
"SELECT t.rowid";
94 $sql .=
" FROM ".MAIN_DB_PREFIX.
"webhook_target as t";
101 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
106 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
108 $sql .= $this->db->order($sortfield, $sortorder);
113 $offset = $limit * $page;
115 $sql .= $this->db->plimit($limit + 1, $offset);
118 $result = $this->db->query($sql);
120 $num = $this->db->num_rows($result);
121 $min = min($num, ($limit <= 0 ? $num : $limit));
124 $obj = $this->db->fetch_object($result);
125 $target_static =
new Target($this->db);
126 if ($target_static->fetch($obj->rowid)) {
132 throw new RestException(503,
'Error when retrieve targets : '.$this->db->lasterror());
134 if (!count($obj_ret)) {
135 throw new RestException(404,
'Targets not found');
139 if ($pagination_data) {
140 $totalsResult = $this->db->query($sqlTotals);
141 $total = $this->db->fetch_object($totalsResult)->total;
146 $obj_ret[
'data'] = $tmp;
147 $obj_ret[
'pagination'] = [
148 'total' => (int) $total,
150 'page_count' => ceil((
int) $total / $limit),
164 public function post($request_data =
null)
166 if (!DolibarrApiAccess::$user->hasRight(
'webhook',
'webhook_target',
'write')) {
167 throw new RestException(403);
172 foreach ($request_data as $field => $value) {
173 $this->target->$field = $this->
_checkValForAPI($field, $value, $this->target);
176 if (!array_key_exists(
'status', $request_data)) {
177 $this->target->status = 1;
180 if ($this->target->create(DolibarrApiAccess::$user) < 0) {
181 throw new RestException(500,
'Error creating target', array_merge(array($this->target->error), $this->target->errors));
184 return $this->target->id;
198 public function put(
$id, $request_data =
null)
200 if (!DolibarrApiAccess::$user->hasRight(
'webhook',
'webhook_target',
'write')) {
201 throw new RestException(403);
204 $result = $this->target->fetch(
$id);
206 throw new RestException(404,
'Target not found');
209 foreach ($request_data as $field => $value) {
210 if ($field ==
'id') {
213 $this->target->$field = $this->
_checkValForAPI($field, $value, $this->target);
216 if ($this->target->update(DolibarrApiAccess::$user, 1) > 0) {
217 return $this->
get(
$id);
219 throw new RestException(500, $this->target->error);
229 public function delete(
$id)
231 if (!DolibarrApiAccess::$user->hasRight(
'webhook',
'webhook_target',
'delete')) {
232 throw new RestException(403);
235 $result = $this->target->fetch(
$id);
237 throw new RestException(404,
'Target not found');
240 $res = $this->target->delete(DolibarrApiAccess::$user);
242 throw new RestException(500,
"Can't delete target, error occurs");
248 'message' =>
'Target deleted'
262 if (!DolibarrApiAccess::$user->hasRight(
'webhook',
'webhook_target',
'read')) {
263 throw new RestException(403);
268 $sql =
"SELECT c.code, c.label FROM ".MAIN_DB_PREFIX.
"c_action_trigger as c ORDER BY c.rang ASC";
269 $resql = $this->db->query($sql);
271 $num = $this->db->num_rows($resql);
274 $obj = $this->db->fetch_object($resql);
275 $triggers[$obj->code] = $obj->label;
279 throw new RestException(500,
"Can't get list of triggers");
296 foreach (self::$FIELDS as $field) {
297 if (!isset($data[$field])) {
298 throw new RestException(400,
"$field field missing");
300 $target[$field] = $data[$field];
319 unset(
$object->array_languages);
321 unset(
$object->linkedObjectsIds);
335 unset(
$object->barcode_type_coder);
336 unset(
$object->mode_reglement_id);
337 unset(
$object->cond_reglement_id);
338 unset(
$object->demand_reason_id);
339 unset(
$object->transport_mode_id);
340 unset(
$object->shipping_method_id);
341 unset(
$object->shipping_method);
342 unset(
$object->fk_multicurrency);
343 unset(
$object->multicurrency_code);
344 unset(
$object->multicurrency_tx);
345 unset(
$object->multicurrency_total_ht);
346 unset(
$object->multicurrency_total_tva);
347 unset(
$object->multicurrency_total_ttc);
348 unset(
$object->multicurrency_total_localtax1);
349 unset(
$object->multicurrency_total_localtax2);
354 unset(
$object->total_localtax1);
355 unset(
$object->total_localtax2);
358 unset(
$object->actiontypecode);
363 unset(
$object->date_validation);
364 unset(
$object->date_modification);
368 unset(
$object->user_creation_id);
370 unset(
$object->user_validation);
371 unset(
$object->user_validation_id);
372 unset(
$object->user_closing_id);
373 unset(
$object->user_modification);
374 unset(
$object->user_modification_id);
378 unset(
$object->cond_reglement_supplier_id);
379 unset(
$object->deposit_percent);
380 unset(
$object->retained_warranty_fk_cond_reglement);
398 private function _fetch($rowid, $ref =
'')
400 if (!DolibarrApiAccess::$user->hasRight(
'webhook',
'webhook_target',
'read')) {
401 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
'. No read permission on target.');
405 $result = $this->target->initAsSpecimen();
407 $result = $this->target->fetch($rowid, $ref);
411 throw new RestException(404,
'Target not found');
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