File size: 1,592 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
<?php

namespace MauticPlugin\MauticCrmBundle\Api;

use Mautic\PluginBundle\Exception\ApiErrorException;

class VtigerApi extends CrmApi
{
    protected $element = 'Leads';

    protected function request($operation, $element, $elementData = [], $method = 'GET')
    {
        $tokenData = $this->integration->getKeys();

        $request_url = $this->integration->getApiUrl();
        $parameters  = [
            'operation'   => $operation,
            'sessionName' => $tokenData['sessionName'],
            'elementType' => $element,
        ];

        if (!empty($elementData)) {
            $parameters['element'] = json_encode($elementData);
        }
        $response = $this->integration->makeRequest($request_url, $parameters, $method);

        if (!empty($response['error'])) {
            $error = $response['error']['message'];

            throw new ApiErrorException($error);
        }

        return $response['result'];
    }

    /**
     * List types.
     *
     * @return mixed
     */
    public function listTypes()
    {
        return $this->request('listtypes', $this->element);
    }

    /**
     * List leads.
     *
     * @return mixed
     */
    public function getLeadFields($object)
    {
        if ('company' === $object) {
            $object = 'Accounts';
        } else {
            $object = $this->element;
        }

        return $this->request('describe', $object);
    }

    /**
     * @return mixed
     */
    public function createLead(array $data)
    {
        return $this->request('create', $this->element, $data, 'POST');
    }
}