diff --git a/admin/survey/modules/mod_squalo/class.SqualoApi.php b/admin/survey/modules/mod_squalo/class.SqualoApi.php index f5f4e593d..ce821357a 100644 --- a/admin/survey/modules/mod_squalo/class.SqualoApi.php +++ b/admin/survey/modules/mod_squalo/class.SqualoApi.php @@ -17,53 +17,253 @@ class SqualoApi { } - public function executeCall($action, $method, $data){ + private function executeCall($action, $method, $data){ global $squalo_user; global $squalo_key; // Add credentials - $data['apiKey'] = $squalo_key; $data['apiUser'] = $squalo_user; - - $params = ''; + $data['apiKey'] = $squalo_key; // GET call - set url params if($method == 'GET'){ - - $params .= '?'; - - // GET params - foreach($data as $name => $value){ - $params .= '&'.$name.'='.$value; - } - - $params = substr($params, 1); + $response = $this->executeGET($action, $data); + } + // POST call + else{ + $response = $this->executePOST($action, $data); } - // Init curl - $ch = curl_init($this->api_url.$action.$params); + // Decode json response + $response_array = json_decode($response, true); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - - // POST call - set curl data - if($method == 'POST'){ - // JSON string za POST - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); - - curl_setopt($ch, CURLOPT_POST, true); - - curl_setopt($ch, CURLOPT_HEADER , 1); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION , 1); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); - curl_setopt($ch, CURLINFO_HEADER_OUT, true); - } - - // Izvedemo klic - $result = curl_exec($ch); + // Error + if($response_array['errorCode'] != '0'){ + $result['error'] = $response_array["errorMessage"]. ' (code '.$response_array["errorCode"].')'; + $result['success'] = false; + } + else{ + $result = $response_array; + $result['success'] = true; + } return $result; } + + // Izvedemo post klic + private function executePOST($action, $data){ + + // Nastavimo url + $url = $this->api_url.$action; + + + // Init curl + $ch = curl_init($url); + + // JSON string za POST + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); + + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + // for debug only! + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + + $headers = array( + "Content-Type: application/json", + ); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + + + // Izvedemo klic + $response = curl_exec($ch); + curl_close($ch); + + + return $response; + } + + // Izvedemo get klic + private function executeGET($action, $data){ + + // GET params + $params = '?'; + foreach($data as $name => $value){ + $params .= $name.'='.$value.'&'; + } + $params = substr($params, 0, -1); + + // Nastavimo celoten url s parametri + $url = $this->api_url.$action.$params; + + + // Init curl + $ch = curl_init($url); + + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + + // Izvedemo klic + $response = curl_exec($ch); + + return $response; + } + + /* + Ustvarimo seznam uporabnikov za pošiljanje + create-list + + "name":"String content", + "description":"String content", + "listTag":"String content", + "color":"String content", + "ordering":2147483647, + "published":true + + {subtag:name} + {subtag:code} + {subtag:url} + + {date:4} + + {unsubscribe}{/unsubscribe} + */ + public function createList($list_name){ + + $action = 'create-list'; + $method = 'POST'; + + $data = array( + 'name' => $list_name, + 'ordering' => $list_number, + 'published' => true + ); + + $response = $this->executeCall($action, $method, $data); + + return $response; + } + + /* + Ustvarimo email,ki se bo poslal + + "altBody":"String content", + "body":"String content", + "fromEmail":"String content", + "fromName":"String content", + "language":"String content", + "listIds":[2147483647], + "published":2147483647, + "replyToEmail":"String content", + "replyToName":"String content", + "subject":"String content", + "visible":true + */ + public function createNewsletter($list_id, $subject, $body, $body_alt, $from_email, $from_name, $reply_to_email, $language){ + + $action = 'create-newsletter'; + $method = 'POST'; + + $data = array( + 'listIds' => array($list_id), + 'subject' => $subject, + 'body' => $body, + 'altBody' => $body_alt, + 'fromEmail' => $from_email, + 'fromName' => $from_name, + 'replyToEmail' => $reply_to_email, + 'language' => $language, + 'visible' => true + ); + + $response = $this->executeCall($action, $method, $data); + + return $response; + } + + /* + Dodamo prejemnika + + "accept": true, + "confirmed": true, + "customAttributes": [{ + "name": "firstname", + "value": "John" + }, + { + "name": "lastname", + "value": "Smith" + } + ], + "email": "String content", + "enabled": true, + "html": true, + "listIds": [2147483647], + "name": "String content", + "surname": "String content", + "gender": "null | male | female | other", + "gdprCanSend": true, + "gdprCanTrack": true + */ + public function addRecipient($email, $list_id, $custom_attributes=array()){ + + $action = 'create-recipient'; + $method = 'POST'; + + $data = array( + 'email' => $email, + 'listIds' => array($list_id), + 'accept' => true, + 'confirmed' => true, + 'enabled' => true, + 'gdprCanSend' => true, + 'gdprCanTrack' => true + ); + + $response = $this->executeCall($action, $method, $data); + + return $response; + } + + /* + Pošljemo emaile + + "newsletterId":2147483647, + "sendDate":2147483647 + */ + public function sendEmails($newsletter_id){ + + $action = 'send-newsletter'; + $method = 'GET'; + + $data = array( + 'newsletterId' => $newsletter_id, + 'sendDate' => time()+30 + ); + + $response = $this->executeCall($action, $method, $data); + + return $response; + } + + + // Pobrisemo prejemnika + public function deleteRecipient($recipient_id){ + + $action = 'delete-recipient'; + $method = 'GET'; + + $data = array( + 'recipientId' => $recipient_id + ); + + $response = $this->executeCall($action, $method, $data); + + return $response; + } + } \ No newline at end of file