26use Luracast\Restler\RestException;
28require_once DOL_DOCUMENT_ROOT.
'/zapier/class/hook.class.php';
42 public static $FIELDS = array(
63 $this->hook =
new Hook($this->db);
77 public function get(
$id)
79 if (!DolibarrApiAccess::$user->hasRight(
'zapier',
'read')) {
80 throw new RestException(403);
83 $result = $this->hook->fetch(
$id);
85 throw new RestException(404,
'Hook not found');
89 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
107 if (!DolibarrApiAccess::$user->hasRight(
'zapier',
'read')) {
108 throw new RestException(403);
111 $arraychoices = array(
112 'invoices' =>
'Invoices',
113 'orders' =>
'Orders',
114 'thirdparties' =>
'Thirparties',
115 'contacts' =>
'Contacts',
127 return $arraychoices;
147 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
149 if (!DolibarrApiAccess::$user->hasRight(
'zapier',
'read')) {
150 throw new RestException(403);
155 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : 0;
158 $restrictonsocid = 0;
162 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
163 $search_sale = DolibarrApiAccess::$user->id;
166 $sql =
"SELECT t.rowid";
167 $sql .=
" FROM ".MAIN_DB_PREFIX.
"hook_mytable as t";
168 $sql .=
" WHERE 1 = 1";
169 $tmpobject =
new Hook($this->db);
170 if ($tmpobject->ismultientitymanaged) {
171 $sql .=
' AND t.entity IN ('.getEntity(
'hook').
')';
173 if ($restrictonsocid && $socid) {
174 $sql .=
" AND t.fk_soc = ".((int) $socid);
177 if ($search_sale && $search_sale !=
'-1') {
178 if ($search_sale == -2) {
179 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
180 } elseif ($search_sale > 0) {
181 $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).
")";
188 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
192 $sql .= $this->db->order($sortfield, $sortorder);
197 $offset = $limit * $page;
199 $sql .= $this->db->plimit($limit + 1, $offset);
202 $result = $this->db->query($sql);
205 $num = $this->db->num_rows($result);
207 $obj = $this->db->fetch_object($result);
208 $hook_static =
new Hook($this->db);
209 if ($hook_static->fetch($obj->rowid)) {
215 throw new RestException(503,
'Error when retrieve hook list');
229 public function post($request_data =
null)
231 if (!DolibarrApiAccess::$user->hasRight(
'zapier',
'write')) {
232 throw new RestException(403);
235 dol_syslog(
"API Zapier create hook receive : ".print_r($request_data,
true), LOG_DEBUG);
241 $result = $this->
validate($request_data, $fields);
243 foreach ($request_data as $field => $value) {
244 if ($field ===
'caller') {
246 $this->hook->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
250 $this->hook->$field = $this->
_checkValForAPI($field, $value, $this->hook);
253 $this->hook->fk_user = DolibarrApiAccess::$user->id;
255 if (!$this->hook->create(DolibarrApiAccess::$user)) {
256 throw new RestException(500,
"Error creating Hook", array_merge(array($this->hook->error), $this->hook->errors));
259 'id' => $this->hook->id,
271 public function delete(
$id)
273 if (!DolibarrApiAccess::$user->hasRight(
'zapier',
'delete')) {
274 throw new RestException(403);
277 $result = $this->hook->fetch(
$id);
279 throw new RestException(404,
'Hook not found');
283 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
286 if (!$this->hook->delete(DolibarrApiAccess::$user)) {
287 throw new RestException(500,
'Error when deleting Hook : '.$this->hook->error);
293 'message' =>
'Hook deleted'
325 foreach ($fields as $field) {
326 if (!isset($data[$field])) {
327 throw new RestException(400, $field.
" field missing");
329 $hook[$field] = $data[$field];
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $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.