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