1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TestMonitor\ActiveCampaign\Actions; |
4
|
|
|
|
5
|
|
|
use TestMonitor\ActiveCampaign\Actions\Action; |
6
|
|
|
use TestMonitor\ActiveCampaign\Resources\Contact; |
7
|
|
|
use TestMonitor\ActiveCampaign\Resources\ContactsList; |
8
|
|
|
|
9
|
|
|
trait ManagesLists |
10
|
|
|
{ |
11
|
|
|
use Action; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Returns all lists. |
15
|
|
|
* |
16
|
|
|
* @return ContactsList[] |
17
|
|
|
*/ |
18
|
|
|
public function lists() |
19
|
|
|
{ |
20
|
|
|
return $this->transformCollection( |
21
|
|
|
$this->get('lists'), |
22
|
|
|
ContactsList::class, |
23
|
|
|
'lists' |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Returns list by ID. |
29
|
|
|
* |
30
|
|
|
* @param string $id |
31
|
|
|
* |
32
|
|
|
* @return ContactsList|null |
33
|
|
|
*/ |
34
|
|
|
public function getList($id) |
35
|
|
|
{ |
36
|
|
|
try { |
37
|
|
|
$lists = $this->get('lists/'.$id); |
38
|
|
|
|
39
|
|
|
if (isset($lists['list']) && count($lists['list'])) { |
40
|
|
|
return new ContactsList($lists['list']); |
41
|
|
|
} |
42
|
|
|
} catch (\TestMonitor\ActiveCampaign\Exceptions\NotFoundException $e) { |
43
|
|
|
// return null if list not foun |
44
|
|
|
} |
45
|
|
|
return null; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Finds list by it's name or URL-safe name. |
50
|
|
|
* |
51
|
|
|
* @param string $name name of list to find |
52
|
|
|
* |
53
|
|
|
* @return null|ContactsList |
54
|
|
|
*/ |
55
|
|
|
public function findList($name) |
56
|
|
|
{ |
57
|
|
|
$lists = $this->transformCollection( |
58
|
|
|
$this->get('lists', ['query' => ['filters[name]' => $name]]), |
59
|
|
|
ContactsList::class, |
60
|
|
|
'lists' |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
return array_shift($lists); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Creates a new list. |
68
|
|
|
* |
69
|
|
|
* @param string $name Name of the list to create |
70
|
|
|
* @param string $senderUrl The website URL this list is for. |
71
|
|
|
* @param array $params other options to create list |
72
|
|
|
* |
73
|
|
|
* @return ContactsList |
74
|
|
|
*/ |
75
|
|
|
public function createList($name, $senderUrl, $params = []) |
76
|
|
|
{ |
77
|
|
|
$params['name'] = $name; |
78
|
|
|
if (! isset($params['stringid'])) { |
79
|
|
|
$params['stringid'] = strtolower(preg_replace('/[^A-Z0-9]+/i', '-', $name)); |
80
|
|
|
} |
81
|
|
|
$params['sender_url'] = $senderUrl; |
82
|
|
|
$params['sender_reminder'] = 'You signed up for my mailing list.'; |
83
|
|
|
|
84
|
|
|
$lists = $this->transformCollection( |
85
|
|
|
$this->post('lists', ['json' => ['list' => $params]]), |
86
|
|
|
ContactsList::class |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
return array_shift($lists); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Removes list. |
94
|
|
|
* |
95
|
|
|
* @param int $id ID of the list to delete |
96
|
|
|
* |
97
|
|
|
* @throws \TestMonitor\ActiveCampaign\Exceptions\NotFoundException |
98
|
|
|
*/ |
99
|
|
|
public function deleteList($id) |
100
|
|
|
{ |
101
|
|
|
$this->delete('lists/'.$id); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Subscribe a contact to a list or unsubscribe a contact from a list. |
106
|
|
|
* |
107
|
|
|
* @param int $list ID of list to remove contact from |
108
|
|
|
* @param int $contact ID of contact to remove from list |
109
|
|
|
* @param bool $subscribe TRUE to subscribe, FALSE otherwise |
110
|
|
|
*/ |
111
|
|
|
public function updateListStatus($list, $contact, $subscribe) |
112
|
|
|
{ |
113
|
|
|
$this->post('contactLists', ['json' => [ |
114
|
|
|
'contactList' => [ |
115
|
|
|
'list' => $list, |
116
|
|
|
'contact' => $contact, |
117
|
|
|
'status' => $subscribe ? 1 : 2, |
118
|
|
|
], ]]); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get all contacts related to the list. |
123
|
|
|
* |
124
|
|
|
* @return Contact[] |
125
|
|
|
*/ |
126
|
|
|
public function contactsByList($listId) |
127
|
|
|
{ |
128
|
|
|
return $this->transformCollection( |
129
|
|
|
$this->get('contacts', ['query' => ['listid' => $listId]]), |
130
|
|
|
Contact::class, |
131
|
|
|
'contacts' |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|