19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/zapier/class/hook.class.php';
40 public 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);
108 $arraychoices = array(
109 'invoices' =>
'Invoices',
110 'orders' =>
'Orders',
111 'thirdparties' =>
'Thirparties',
112 'contacts' =>
'Contacts',
124 return $arraychoices;
143 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'')
147 if (!DolibarrApiAccess::$user->rights->zapier->read) {
148 throw new RestException(401);
153 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
156 $restrictonsocid = 0;
160 if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
161 $search_sale = DolibarrApiAccess::$user->id;
164 $sql =
"SELECT t.rowid";
165 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
167 $sql .=
", sc.fk_soc, sc.fk_user";
169 $sql .=
" FROM ".MAIN_DB_PREFIX.
"hook_mytable as t";
171 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
172 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
174 $sql .=
" WHERE 1 = 1";
180 $tmpobject =
new Hook($this->
db);
181 if ($tmpobject->ismultientitymanaged) {
182 $sql .=
' AND t.entity IN ('.getEntity(
'hook').
')';
184 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
185 $sql .=
" AND t.fk_soc = sc.fk_soc";
187 if ($restrictonsocid && $socid) {
188 $sql .=
" AND t.fk_soc = ".((int) $socid);
190 if ($restrictonsocid && $search_sale > 0) {
192 $sql .=
" AND t.rowid = sc.fk_soc";
195 if ($restrictonsocid && $search_sale > 0) {
196 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
201 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
203 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
204 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
207 $sql .= $this->
db->order($sortfield, $sortorder);
212 $offset = $limit * $page;
214 $sql .= $this->
db->plimit($limit + 1, $offset);
217 $result = $this->
db->query($sql);
220 $num = $this->
db->num_rows($result);
222 $obj = $this->
db->fetch_object($result);
223 $hook_static =
new Hook($this->
db);
224 if ($hook_static->fetch($obj->rowid)) {
230 throw new RestException(503,
'Error when retrieve hook list');
232 if (!count($obj_ret)) {
233 throw new RestException(404,
'No hook found');
246 public function post($request_data =
null)
248 if (!DolibarrApiAccess::$user->rights->zapier->write) {
249 throw new RestException(401);
256 dol_syslog(
"API Zapier create hook receive : ".print_r($request_data,
true), LOG_DEBUG);
257 $result = $this->
validate($request_data, $fields);
259 foreach ($request_data as $field => $value) {
260 $this->hook->$field = $value;
262 $this->hook->fk_user = DolibarrApiAccess::$user->id;
264 if (!$this->hook->create(DolibarrApiAccess::$user)) {
265 throw new RestException(500,
"Error creating Hook", array_merge(array($this->hook->error), $this->hook->errors));
268 'id' => $this->hook->id,
318 public function delete($id)
320 if (!DolibarrApiAccess::$user->rights->zapier->delete) {
321 throw new RestException(401);
324 $result = $this->hook->fetch($id);
326 throw new RestException(404,
'Hook not found');
330 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
333 if (!$this->hook->delete(DolibarrApiAccess::$user)) {
334 throw new RestException(500,
'Error when deleting Hook : '.$this->hook->error);
340 'message' =>
'Hook deleted'
355 $object = parent::_cleanObjectDatas($object);
379 foreach ($fields as $field) {
380 if (!isset($data[$field])) {
381 throw new RestException(400, $field.
" field missing");
383 $hook[$field] = $data[$field];