File size: 1,820 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php

namespace MauticPlugin\MauticEmailMarketingBundle\Api;

use Mautic\PluginBundle\Exception\ApiErrorException;

class IcontactApi extends EmailMarketingApi
{
    protected function request($endpoint, $parameters = [], $method = 'GET')
    {
        $url = sprintf('%s/%s/c/%s/%s', $this->integration->getApiUrl(), $this->keys['accountId'], $this->keys['clientFolderId'], $endpoint);

        $response = $this->integration->makeRequest($url, $parameters, $method, [
            'encode_parameters'    => 'json',
            'encoding_headers_set' => true,
        ]);

        if (is_array($response) && !empty($response['errors'])) {
            throw new ApiErrorException(implode(' ', $response['errors']));
        } else {
            return $response;
        }
    }

    /**
     * @return mixed|string
     *
     * @throws ApiErrorException
     */
    public function getLists()
    {
        return $this->request('lists');
    }

    /**
     * @return mixed|string
     *
     * @throws ApiErrorException
     */
    public function getCustomFields()
    {
        return $this->request('customfields');
    }

    /**
     * @param array $fields
     *
     * @return mixed|string
     *
     * @throws ApiErrorException
     */
    public function subscribeLead($listId, $fields)
    {
        $fields['status'] = 'normal';

        $contacts = $this->request('contacts', [$fields], 'POST');

        if (!empty($contacts['contacts'][0]['contactId'])) {
            $contactId = $contacts['contacts'][0]['contactId'];

            $fields = [
                'status'    => 'normal',
                'listId'    => $listId,
                'contactId' => $contactId,
            ];

            return $this->request('subscriptions', [$fields], 'POST');
        }

        return $contacts;
    }
}