| 1 |
<?php |
| 2 |
|
| 3 |
class SendinblueList extends SendinblueClass |
| 4 |
{ |
| 5 |
public function getListExternalSendingMethod(&$listId, $mailId, $createIfNotExists = true) |
| 6 |
{ |
| 7 |
$brevoLists = $this->config->get('list_sendinblue', '[]'); |
| 8 |
$brevoLists = json_decode($brevoLists, true); |
| 9 |
|
| 10 |
if (!empty($brevoLists[$mailId])) { |
| 11 |
$listId = $brevoLists[$mailId]; |
| 12 |
|
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
if (!$createIfNotExists) return; |
| 17 |
|
| 18 |
$listId = $this->createList('list_for_acym_mail_'.$mailId); |
| 19 |
|
| 20 |
$brevoLists[$mailId] = $listId; |
| 21 |
$this->config->saveConfig(['list_sendinblue' => json_encode($brevoLists)]); |
| 22 |
} |
| 23 |
|
| 24 |
public function createList($name) |
| 25 |
{ |
| 26 |
$data = [ |
| 27 |
'name' => $name, |
| 28 |
'folderId' => intval($this->getFolderId()), |
| 29 |
]; |
| 30 |
|
| 31 |
$response = $this->callApiSendingMethod('contacts/lists', $data, $this->headers, 'POST'); |
| 32 |
|
| 33 |
// The folder Id is saved in the config but the user removed it from Sendinblue, recreate a folder |
| 34 |
if (!empty($response['message']) && $response['message'] === 'Folder ID does not exist') { |
| 35 |
$this->config->saveConfig(['sendinblue_folder_id' => 0]); |
| 36 |
|
| 37 |
return $this->createList($name); |
| 38 |
} |
| 39 |
|
| 40 |
return $response['id']; |
| 41 |
} |
| 42 |
|
| 43 |
public function getFolderId() |
| 44 |
{ |
| 45 |
$folderId = $this->config->get('sendinblue_folder_id', 0); |
| 46 |
if (!empty($folderId)) return $folderId; |
| 47 |
|
| 48 |
$data = [ |
| 49 |
'name' => 'AcyMailing Integration', |
| 50 |
]; |
| 51 |
|
| 52 |
$response = $this->callApiSendingMethod('contacts/folders', $data, $this->headers, 'POST'); |
| 53 |
|
| 54 |
// The API key may be wrong |
| 55 |
if (empty($response['id'])) return 0; |
| 56 |
|
| 57 |
$this->config->saveConfig(['sendinblue_folder_id' => $response['id']]); |
| 58 |
|
| 59 |
return $response['id']; |
| 60 |
} |
| 61 |
|
| 62 |
public function deleteList($mailId): bool |
| 63 |
{ |
| 64 |
$listId = 0; |
| 65 |
$this->getListExternalSendingMethod($listId, $mailId, false); |
| 66 |
|
| 67 |
if (empty($listId)) return false; |
| 68 |
|
| 69 |
$this->callApiSendingMethod(plgAcymSendinblue::SENDING_METHOD_API_URL.'contacts/lists/'.$listId, [], $this->headers, 'DELETE'); |
| 70 |
|
| 71 |
return true; |
| 72 |
} |
| 73 |
} |
| 74 |
|