19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/zapier/class/hook.class.php';
40 static $FIELDS = array(
60 $this->hook =
new Hook($this->
db);
74 public function get($id)
76 if (!DolibarrApiAccess::$user->rights->zapier->read) {
77 throw new RestException(401);
80 $result = $this->hook->fetch($id);
82 throw new RestException(404,
'Hook not found');
86 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
104 if (!DolibarrApiAccess::$user->rights->zapier->read) {
105 throw new RestException(401);
107 $arraychoices = array(
108 'invoices' =>
'Invoices',
109 'orders' =>
'Orders',
110 'thirdparties' =>
'Thirparties',
111 'contacts' =>
'Contacts',
123 return $arraychoices;
142 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'')
148 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
151 $restrictonsocid = 0;
155 if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
156 $search_sale = DolibarrApiAccess::$user->id;
159 $sql =
"SELECT t.rowid";
160 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
162 $sql .=
", sc.fk_soc, sc.fk_user";
164 $sql .=
" FROM ".MAIN_DB_PREFIX.
"hook_mytable as t";
166 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
167 $sql .=
" WHERE 1 = 1";
173 $tmpobject =
new Hook($this->
db);
174 if ($tmpobject->ismultientitymanaged) {
175 $sql .=
' AND t.entity IN ('.getEntity(
'hook').
')';
177 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
178 $sql .=
" AND t.fk_soc = sc.fk_soc";
180 if ($restrictonsocid && $socid) {
181 $sql .=
" AND t.fk_soc = ".$socid;
183 if ($restrictonsocid && $search_sale > 0) {
185 $sql .=
" AND t.rowid = sc.fk_soc";
188 if ($restrictonsocid && $search_sale > 0) {
189 $sql .=
" AND sc.fk_user = ".$search_sale;
193 throw new RestException(503,
'Error when validating parameter sqlfilters '.$sqlfilters);
195 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
196 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
199 $sql .= $this->
db->order($sortfield, $sortorder);
204 $offset = $limit * $page;
206 $sql .= $this->
db->plimit($limit + 1, $offset);
209 $result = $this->
db->query($sql);
212 $num = $this->
db->num_rows($result);
214 $obj = $this->
db->fetch_object($result);
215 $hook_static =
new Hook($this->
db);
216 if ($hook_static->fetch($obj->rowid)) {
222 throw new RestException(503,
'Error when retrieve hook list');
224 if (!count($obj_ret)) {
225 throw new RestException(404,
'No hook found');
238 public function post($request_data = null)
240 if (!DolibarrApiAccess::$user->rights->zapier->write) {
241 throw new RestException(401);
247 dol_syslog(
"API Zapier create hook receive : ".print_r($request_data,
true), LOG_DEBUG);
248 $result = $this->
validate($request_data, $fields);
250 foreach ($request_data as $field => $value) {
251 $this->hook->$field = $value;
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,
309 public function delete($id)
311 if (!DolibarrApiAccess::$user->rights->zapier->delete) {
312 throw new RestException(401);
314 $result = $this->hook->fetch($id);
316 throw new RestException(404,
'Hook not found');
320 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
323 if (!$this->hook->delete(DolibarrApiAccess::$user)) {
324 throw new RestException(500,
'Error when deleting Hook : '.$this->hook->error);
330 'message' =>
'Hook deleted'
345 $object = parent::_cleanObjectDatas($object);
369 foreach ($fields as $field) {
370 if (!isset($data[$field])) {
371 throw new RestException(400, $field.
" field missing");
373 $hook[$field] = $data[$field];
$conf db
API class for accounts.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
getModulesChoices()
Get list of possibles choices for module.
post($request_data=null)
Create hook object.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check user access to a resource.
validate($data, $fields)
Validate fields before create or update object.
_cleanObjectDatas($object)
Clean sensible object datas.
__construct()
Constructor.
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $sqlfilters= '')
List hooks.