25 use Luracast\Restler\RestException;
27 require_once DOL_DOCUMENT_ROOT.
'/zapier/class/hook.class.php';
41 public static $FIELDS = array(
61 $this->hook =
new Hook($this->
db);
75 public function get($id)
77 if (!DolibarrApiAccess::$user->rights->zapier->read) {
78 throw new RestException(401);
81 $result = $this->hook->fetch($id);
83 throw new RestException(404,
'Hook not found');
87 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
105 if (!DolibarrApiAccess::$user->rights->zapier->read) {
106 throw new RestException(401);
109 $arraychoices = array(
110 'invoices' =>
'Invoices',
111 'orders' =>
'Orders',
112 'thirdparties' =>
'Thirparties',
113 'contacts' =>
'Contacts',
125 return $arraychoices;
144 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'')
148 if (!DolibarrApiAccess::$user->rights->zapier->read) {
149 throw new RestException(401);
154 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
157 $restrictonsocid = 0;
161 if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
162 $search_sale = DolibarrApiAccess::$user->id;
165 $sql =
"SELECT t.rowid";
166 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
168 $sql .=
", sc.fk_soc, sc.fk_user";
170 $sql .=
" FROM ".MAIN_DB_PREFIX.
"hook_mytable as t";
172 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
173 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
175 $sql .=
" WHERE 1 = 1";
181 $tmpobject =
new Hook($this->
db);
182 if ($tmpobject->ismultientitymanaged) {
183 $sql .=
' AND t.entity IN ('.getEntity(
'hook').
')';
185 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
186 $sql .=
" AND t.fk_soc = sc.fk_soc";
188 if ($restrictonsocid && $socid) {
189 $sql .=
" AND t.fk_soc = ".((int) $socid);
191 if ($restrictonsocid && $search_sale > 0) {
193 $sql .=
" AND t.rowid = sc.fk_soc";
196 if ($restrictonsocid && $search_sale > 0) {
197 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
202 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
204 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
205 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
208 $sql .= $this->
db->order($sortfield, $sortorder);
213 $offset = $limit * $page;
215 $sql .= $this->
db->plimit($limit + 1, $offset);
218 $result = $this->
db->query($sql);
221 $num = $this->
db->num_rows($result);
223 $obj = $this->
db->fetch_object($result);
224 $hook_static =
new Hook($this->
db);
225 if ($hook_static->fetch($obj->rowid)) {
231 throw new RestException(503,
'Error when retrieve hook list');
233 if (!count($obj_ret)) {
234 throw new RestException(404,
'No hook found');
247 public function post($request_data =
null)
249 if (!DolibarrApiAccess::$user->rights->zapier->write) {
250 throw new RestException(401);
257 dol_syslog(
"API Zapier create hook receive : ".print_r($request_data,
true), LOG_DEBUG);
258 $result = $this->
validate($request_data, $fields);
260 foreach ($request_data as $field => $value) {
261 $this->hook->$field = $value;
263 $this->hook->fk_user = DolibarrApiAccess::$user->id;
265 if (!$this->hook->create(DolibarrApiAccess::$user)) {
266 throw new RestException(500,
"Error creating Hook", array_merge(array($this->hook->error), $this->hook->errors));
269 'id' => $this->hook->id,
319 public function delete($id)
321 if (!DolibarrApiAccess::$user->rights->zapier->delete) {
322 throw new RestException(401);
325 $result = $this->hook->fetch($id);
327 throw new RestException(404,
'Hook not found');
331 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
334 if (!$this->hook->delete(DolibarrApiAccess::$user)) {
335 throw new RestException(500,
'Error when deleting Hook : '.$this->hook->error);
341 'message' =>
'Hook deleted'
356 $object = parent::_cleanObjectDatas($object);
380 foreach ($fields as $field) {
381 if (!isset($data[$field])) {
382 throw new RestException(400, $field.
" field missing");
384 $hook[$field] = $data[$field];
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
_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='')
List hooks.
getModulesChoices()
Get list of possibles choices for module.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db
API class for accounts.