File size: 4,874 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php

namespace MauticPlugin\MauticEmailMarketingBundle\Integration;

use MauticPlugin\MauticEmailMarketingBundle\Form\Type\ConstantContactType;

class ConstantContactIntegration extends EmailAbstractIntegration
{
    public function getName(): string
    {
        return 'ConstantContact';
    }

    public function getDisplayName(): string
    {
        return 'Constant Contact';
    }

    public function getAuthenticationType(): string
    {
        return 'oauth2';
    }

    /**
     * Get the URL required to obtain an oauth2 access token.
     */
    public function getAccessTokenUrl(): string
    {
        return 'https://oauth2.constantcontact.com/oauth2/oauth/token';
    }

    /**
     * Get the authentication/login URL for oauth2 access.
     */
    public function getAuthenticationUrl(): string
    {
        return 'https://oauth2.constantcontact.com/oauth2/oauth/siteowner/authorize';
    }

    /**
     * Retrieves and stores tokens returned from oAuthLogin.
     *
     * @param array $settings
     * @param array $parameters
     *
     * @return bool|string false if no error; otherwise the error string
     */
    public function authCallback($settings = [], $parameters = [])
    {
        // Constanct Contact doesn't like POST
        $settings['method'] = 'GET';

        return parent::authCallback($settings, $parameters);
    }

    /**
     * @return mixed[]
     */
    public function getAvailableLeadFields($settings = []): array
    {
        if (!$this->isAuthorized()) {
            return [];
        }

        $fields = [
            'email',
            'prefix_name',
            'first_name',
            'last_name',
            'company_name',
            'job_title',
            'address_line1',
            'address_line2',
            'address_city',
            'address_state',
            'address_country_code',
            'address_postal_code',
            'cell_phone',
            'fax',
            'work_phone',
            'home_phone',
        ];

        $leadFields = [];
        foreach ($fields as $f) {
            $leadFields[$f] = [
                'label'    => $this->translator->trans('mautic.constantcontact.field.'.$f),
                'type'     => 'string',
                'required' => ('email' == $f) ? true : false,
            ];
        }

        $c = 1;
        while ($c <= 15) {
            $leadFields['customfield_'.$c] = [
                'label'    => $this->translator->trans('mautic.constantcontact.customfield.'.$f),
                'type'     => 'string',
                'required' => false,
            ];
            ++$c;
        }

        return $leadFields;
    }

    public function pushLead($lead, $config = []): bool
    {
        $config = $this->mergeConfigToFeatureSettings($config);

        $mappedData = $this->populateLeadData($lead, $config);

        if (empty($mappedData)) {
            return false;
        } elseif (empty($mappedData['email'])) {
            return false;
        } elseif (!isset($config['list_settings'])) {
            return false;
        }

        try {
            if ($this->isAuthorized()) {
                $email = $mappedData['email'];
                unset($mappedData['email']);

                $addresses    = [];
                $customfields = [];
                foreach ($mappedData as $k => $v) {
                    if (str_starts_with($v, 'address_')) {
                        $addresses[str_replace('address_', '', $k)] = $v;
                        unset($mappedData[$k]);
                    } elseif (str_starts_with($v, 'customfield_')) {
                        $key            = str_replace('customfield_', 'CustomField', $k);
                        $customfields[] = [
                            'name'  => $key,
                            'value' => $v,
                        ];
                        unset($mappedData[$k]);
                    }
                }

                if (!empty($addresses)) {
                    $addresses['address_type'] = 'PERSONAL';
                    $mappedData['addresses']   = $addresses;
                }

                if (!empty($customfields)) {
                    $mappedData['custom_fields'] = $customfields;
                }

                $options              = [];
                $options['action_by'] = (!empty($config['list_settings']['sendWelcome'])) ? 'ACTION_BY_VISITOR' : 'ACTION_BY_OWNER';
                $listId               = $config['list_settings']['list'];

                $this->getApiHelper()->subscribeLead($email, $listId, $mappedData, $options);

                return true;
            }
        } catch (\Exception $e) {
            $this->logIntegrationError($e);
        }

        return false;
    }

    public function getFormType(): string
    {
        return ConstantContactType::class;
    }
}