26use Luracast\Restler\RestException;
28require_once DOL_DOCUMENT_ROOT.
'/zapier/class/hook.class.php';
42 public static $FIELDS = array(
62 $this->hook =
new Hook($this->db);
76 public function get(
$id)
78 if (!DolibarrApiAccess::$user->hasRight(
'zapier',
'read')) {
79 throw new RestException(403);
82 $result = $this->hook->fetch(
$id);
84 throw new RestException(404,
'Hook not found');
88 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
108 if (!DolibarrApiAccess::$user->hasRight(
'zapier',
'read')) {
109 throw new RestException(403);
112 $arraychoices = array(
113 'invoices' =>
'Invoices',
114 'orders' =>
'Orders',
115 'thirdparties' =>
'ThirdParties',
116 'contacts' =>
'Contacts',
128 return $arraychoices;
150 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
152 if (!DolibarrApiAccess::$user->hasRight(
'zapier',
'read')) {
153 throw new RestException(403);
158 $socid = DolibarrApiAccess::$user->socid ?: 0;
160 $sql =
"SELECT t.rowid";
161 $sql .=
" FROM ".MAIN_DB_PREFIX.
"zapier_hook as t";
162 $sql .=
" WHERE 1 = 1";
163 $tmpobject =
new Hook($this->db);
164 if ($tmpobject->ismultientitymanaged) {
165 $sql .=
' AND t.entity IN ('.getEntity(
'hook').
')';
171 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
175 $sql .= $this->db->order($sortfield, $sortorder);
180 $offset = $limit * $page;
182 $sql .= $this->db->plimit($limit + 1, $offset);
185 $result = $this->db->query($sql);
189 $num = $this->db->num_rows($result);
190 $min = min($num, ($limit <= 0 ? $num : $limit));
192 $obj = $this->db->fetch_object($result);
193 $hook_static =
new Hook($this->db);
194 if ($hook_static->fetch($obj->rowid)) {
200 throw new RestException(503,
'Error when retrieve hook list');
218 public function post($request_data =
null)
220 if (!DolibarrApiAccess::$user->hasRight(
'zapier',
'write')) {
221 throw new RestException(403);
224 dol_syslog(
"API Zapier create hook receive : ".print_r($request_data,
true), LOG_DEBUG);
230 $request_data = $this->
validate($request_data, $fields);
232 foreach ($request_data as $field => $value) {
233 if ($field ===
'caller') {
235 $this->hook->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
239 $this->hook->$field = $this->
_checkValForAPI($field, $value, $this->hook);
242 $this->hook->fk_user = DolibarrApiAccess::$user->id;
244 if (!$this->hook->create(DolibarrApiAccess::$user)) {
245 throw new RestException(500,
"Error creating Hook", array_merge(array($this->hook->error), $this->hook->errors));
248 'id' => $this->hook->id,
262 public function delete(
$id)
264 if (!DolibarrApiAccess::$user->hasRight(
'zapier',
'delete')) {
265 throw new RestException(403);
268 $result = $this->hook->fetch(
$id);
270 throw new RestException(404,
'Hook not found');
274 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
277 if (!$this->hook->delete(DolibarrApiAccess::$user)) {
278 throw new RestException(500,
'Error when deleting Hook : '.$this->hook->error);
284 'message' =>
'Hook deleted'
321 foreach ($fields as $field) {
322 if (!isset($data[$field])) {
323 throw new RestException(400, $field.
" field missing");
325 $hook[$field] = $data[$field];
$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.
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.
_cleanObjectDatas($object)
Clean sensible object datas.
validate($data, $fields)
Validate fields before create or update object.
__construct()
Constructor.
post($request_data=null)
Create hook object.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='')
List hooks.
getModulesChoices()
Get list of possibles choices for module.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.