25use Luracast\Restler\RestException;
27require_once DOL_DOCUMENT_ROOT.
'/zapier/class/hook.class.php';
41 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);
106 if (!DolibarrApiAccess::$user->hasRight(
'zapier',
'read')) {
107 throw new RestException(403);
110 $arraychoices = array(
111 'invoices' =>
'Invoices',
112 'orders' =>
'Orders',
113 'thirdparties' =>
'Thirparties',
114 'contacts' =>
'Contacts',
126 return $arraychoices;
146 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
148 if (!DolibarrApiAccess::$user->hasRight(
'zapier',
'read')) {
149 throw new RestException(403);
154 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : 0;
157 $restrictonsocid = 0;
161 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
162 $search_sale = DolibarrApiAccess::$user->id;
165 $sql =
"SELECT t.rowid";
166 $sql .=
" FROM ".MAIN_DB_PREFIX.
"hook_mytable as t";
167 $sql .=
" WHERE 1 = 1";
168 $tmpobject =
new Hook($this->db);
169 if ($tmpobject->ismultientitymanaged) {
170 $sql .=
' AND t.entity IN ('.getEntity(
'hook').
')';
172 if ($restrictonsocid && $socid) {
173 $sql .=
" AND t.fk_soc = ".((int) $socid);
176 if ($search_sale && $search_sale !=
'-1') {
177 if ($search_sale == -2) {
178 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
179 } elseif ($search_sale > 0) {
180 $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).
")";
187 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
191 $sql .= $this->db->order($sortfield, $sortorder);
196 $offset = $limit * $page;
198 $sql .= $this->db->plimit($limit + 1, $offset);
201 $result = $this->db->query($sql);
204 $num = $this->db->num_rows($result);
206 $obj = $this->db->fetch_object($result);
207 $hook_static =
new Hook($this->db);
208 if ($hook_static->fetch($obj->rowid)) {
214 throw new RestException(503,
'Error when retrieve hook list');
228 public function post($request_data =
null)
230 if (!DolibarrApiAccess::$user->hasRight(
'zapier',
'write')) {
231 throw new RestException(403);
234 dol_syslog(
"API Zapier create hook receive : ".print_r($request_data,
true), LOG_DEBUG);
240 $result = $this->
validate($request_data, $fields);
242 foreach ($request_data as $field => $value) {
243 if ($field ===
'caller') {
245 $this->hook->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
249 $this->hook->$field = $this->
_checkValForAPI($field, $value, $this->hook);
252 $this->hook->fk_user = DolibarrApiAccess::$user->id;
254 if (!$this->hook->create(DolibarrApiAccess::$user)) {
255 throw new RestException(500,
"Error creating Hook", array_merge(array($this->hook->error), $this->hook->errors));
258 'id' => $this->hook->id,
270 public function delete($id)
272 if (!DolibarrApiAccess::$user->hasRight(
'zapier',
'delete')) {
273 throw new RestException(403);
276 $result = $this->hook->fetch($id);
278 throw new RestException(404,
'Hook not found');
282 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
285 if (!$this->hook->delete(DolibarrApiAccess::$user)) {
286 throw new RestException(500,
'Error when deleting Hook : '.$this->hook->error);
292 'message' =>
'Hook deleted'
324 foreach ($fields as $field) {
325 if (!isset($data[$field])) {
326 throw new RestException(400, $field.
" field missing");
328 $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.