Issues (3627)

MauticCrmBundle/Integration/VtigerIntegration.php (3 issues)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace MauticPlugin\MauticCrmBundle\Integration;
13
14
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
15
16
/**
17
 * Class VtigerIntegration.
18
 */
19
class VtigerIntegration extends CrmAbstractIntegration
20
{
21
    private $authorzationError = '';
22
23
    /**
24
     * Returns the name of the social integration that must match the name of the file.
25
     *
26
     * @return string
27
     */
28
    public function getName()
29
    {
30
        return 'Vtiger';
31
    }
32
33
    /**
34
     * @return array
35
     */
36
    public function getSupportedFeatures()
37
    {
38
        return ['push_lead'];
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function getDisplayName()
45
    {
46
        return 'vTiger';
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     *
52
     * @return array
53
     */
54
    public function getRequiredKeyFields()
55
    {
56
        return [
57
            'url'       => 'mautic.vtiger.form.url',
58
            'username'  => 'mautic.vtiger.form.username',
59
            'accessKey' => 'mautic.vtiger.form.password',
60
        ];
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    public function getClientIdKey()
67
    {
68
        return 'username';
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getClientSecretKey()
75
    {
76
        return 'accessKey';
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    public function getAuthTokenKey()
83
    {
84
        return 'sessionName';
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function getApiUrl()
91
    {
92
        return sprintf('%s/webservice.php', $this->keys['url']);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     *
98
     * @return bool
99
     */
100
    public function isAuthorized()
101
    {
102
        if (!isset($this->keys['url'])) {
103
            return false;
104
        }
105
106
        $url        = $this->getApiUrl();
107
        $parameters = [
108
            'operation' => 'getchallenge',
109
            'username'  => $this->keys['username'],
110
        ];
111
112
        $response = $this->makeRequest($url, $parameters, 'GET', ['authorize_session' => true]);
113
114
        if (empty($response['success'])) {
115
            return $this->getErrorsFromResponse($response);
116
        }
117
118
        $loginParameters = [
119
            'operation' => 'login',
120
            'username'  => $this->keys['username'],
121
            'accessKey' => md5($response['result']['token'].$this->keys['accessKey']),
122
        ];
123
124
        $response = $this->makeRequest($url, $loginParameters, 'POST', ['authorize_session' => true]);
125
126
        if (empty($response['success'])) {
127
            if (is_array($response) && array_key_exists('error', $response)) {
128
                $this->authorzationError = $response['error']['message'];
129
            }
130
131
            return false;
132
        } else {
133
            $error = $this->extractAuthKeys($response['result']);
134
135
            if (empty($error)) {
136
                return true;
137
            } else {
138
                $this->authorzationError = $error;
139
140
                return false;
141
            }
142
        }
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     *
148
     * @return string
149
     */
150
    public function getAuthLoginUrl()
151
    {
152
        return $this->router->generate('mautic_integration_auth_callback', ['integration' => $this->getName()]);
153
    }
154
155
    /**
156
     * Retrieves and stores tokens returned from oAuthLogin.
157
     *
158
     * @param array $settings
159
     * @param array $parameters
160
     *
161
     * @return array
162
     */
163
    public function authCallback($settings = [], $parameters = [])
164
    {
165
        $success = $this->isAuthorized();
166
        if (!$success) {
167
            return $this->authorzationError;
168
        } else {
169
            return false;
170
        }
171
    }
172
173
    /**
174
     * @return array|mixed
175
     */
176
    public function getAvailableLeadFields($settings = [])
177
    {
178
        $vTigerFields      = [];
179
        $silenceExceptions = (isset($settings['silence_exceptions'])) ? $settings['silence_exceptions'] : true;
180
181
        if (isset($settings['feature_settings']['objects'])) {
182
            $vTigerObjects = $settings['feature_settings']['objects'];
183
        } else {
184
            $settings      = $this->settings->getFeatureSettings();
185
            $vTigerObjects = isset($settings['objects']) ? $settings['objects'] : ['contacts'];
186
        }
187
188
        try {
189
            if ($this->isAuthorized()) {
190
                if (!empty($vTigerObjects) && is_array($vTigerObjects)) {
191
                    foreach ($vTigerObjects as $object) {
192
                        // The object key for contacts should be 0 for some BC reasons
193
                        if ('contacts' == $object) {
194
                            $object = 0;
195
                        }
196
197
                        // Check the cache first
198
                        $settings['cache_suffix'] = $cacheSuffix = '.'.$object;
199
                        if ($fields = parent::getAvailableLeadFields($settings)) {
200
                            $vTigerFields[$object] = $fields;
201
                            continue;
202
                        }
203
204
                        // Create the array if it doesn't exist to prevent PHP notices
205
                        if (!isset($vTigerFields[$object])) {
206
                            $vTigerFields[$object] = [];
207
                        }
208
209
                        $leadFields = $this->getApiHelper()->getLeadFields($object);
0 ignored issues
show
The method getLeadFields() does not exist on MauticPlugin\MauticCrmBundle\Api\CrmApi. It seems like you code against a sub-type of said class. However, the method does not exist in MauticPlugin\MauticCrmBundle\Api\PipedriveApi or MauticPlugin\MauticCrmBundle\Api\ConnectwiseApi. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

209
                        $leadFields = $this->getApiHelper()->/** @scrutinizer ignore-call */ getLeadFields($object);
Loading history...
210
                        if (isset($leadFields['fields'])) {
211
                            foreach ($leadFields['fields'] as $fieldInfo) {
212
                                if (!isset($fieldInfo['name']) || !$fieldInfo['editable'] || in_array(
213
                                        $fieldInfo['type']['name'],
214
                                        ['owner', 'reference', 'boolean', 'autogenerated']
215
                                    )
216
                                ) {
217
                                    continue;
218
                                }
219
220
                                $vTigerFields[$object][$fieldInfo['name']] = [
221
                                    'type'     => 'string',
222
                                    'label'    => $fieldInfo['label'],
223
                                    'required' => (in_array($fieldInfo['name'], ['email', 'accountname'])),
224
                                ];
225
                            }
226
                        }
227
228
                        $this->cache->set('leadFields'.$cacheSuffix, $vTigerFields[$object]);
229
                    }
230
                }
231
            }
232
        } catch (\Exception $e) {
233
            $this->logIntegrationError($e);
234
235
            if (!$silenceExceptions) {
236
                throw $e;
237
            }
238
        }
239
240
        return $vTigerFields;
241
    }
242
243
    /**
244
     * {@inheritdoc}
245
     *
246
     * @param $section
247
     *
248
     * @return string
249
     */
250
    public function getFormNotes($section)
251
    {
252
        if ('leadfield_match' == $section) {
253
            return ['mautic.vtiger.form.field_match_notes', 'info'];
254
        }
255
256
        return parent::getFormNotes($section);
257
    }
258
259
    /**
260
     * {@inheritdoc}
261
     *
262
     * @param $mappedData
263
     */
264
    public function amendLeadDataBeforePush(&$mappedData)
265
    {
266
        if (!empty($mappedData)) {
267
            //vtiger requires assigned_user_id so default to authenticated user
268
            $mappedData['assigned_user_id'] = $this->keys['userId'];
269
        }
270
    }
271
272
    /**
273
     * @param \Mautic\PluginBundle\Integration\Form|FormBuilder $builder
0 ignored issues
show
The type Mautic\PluginBundle\Integration\Form was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
The type MauticPlugin\MauticCrmBu...Integration\FormBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
274
     * @param array                                             $data
275
     * @param string                                            $formArea
276
     */
277
    public function appendToForm(&$builder, $data, $formArea)
278
    {
279
        if ('features' == $formArea) {
280
            $builder->add(
281
                'objects',
282
                ChoiceType::class,
283
                [
284
                    'choices' => [
285
                        'mautic.vtiger.object.contact' => 'contacts',
286
                        'mautic.vtiger.object.company' => 'company',
287
                    ],
288
                    'expanded'          => true,
289
                    'multiple'          => true,
290
                    'label'             => 'mautic.vtiger.form.objects_to_pull_from',
291
                    'label_attr'        => ['class' => ''],
292
                    'placeholder'       => false,
293
                    'required'          => false,
294
                ]
295
            );
296
        }
297
    }
298
299
    /**
300
     * Get available company fields for choices in the config UI.
301
     *
302
     * @param array $settings
303
     *
304
     * @return array
305
     */
306
    public function getFormCompanyFields($settings = [])
307
    {
308
        return parent::getAvailableLeadFields(['cache_suffix' => '.company']);
309
    }
310
}
311