Passed
Push — EXTRACT_CLASSES ( d3ffdd...d4f850 )
by Rafael
39:11
created

InterfaceContactRoles   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 25

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
D runTrigger() 0 54 24
1
<?php
2
3
/*
4
 * Copyright (C) 2005-2017  Laurent Destailleur         <[email protected]>
5
 * Copyright (C) 2009-2017  Regis Houssin               <[email protected]>
6
 * Copyright (C) 2011-2014  Juanjo Menent               <[email protected]>
7
 * Copyright (C) 2013       Cedric GROSS                <[email protected]>
8
 * Copyright (C) 2014       Marcos García               <[email protected]>
9
 * Copyright (C) 2015       Bahfir Abbes                <[email protected]>
10
 * Copyright (C) 2024		MDW							<[email protected]>
11
 * Copyright (C) 2024       Rafael San José             <[email protected]>
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 3 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
25
 */
26
27
use Dolibarr\Code\Contact\Classes\Contact;
28
use Dolibarr\Code\Core\Classes\Conf;
29
use Dolibarr\Code\Core\Classes\Translate;
30
use Dolibarr\Code\User\Classes\User;
31
32
/**
33
 * \file htdocs/core/triggers/interface_90_modSociete_ContactRoles.class.php
34
 * \ingroup agenda
35
 * \brief Trigger file for company - contactroles
36
 */
37
38
require_once constant('DOL_DOCUMENT_ROOT') . '/core/triggers/dolibarrtriggers.class.php';
39
40
/**
41
 * Class of triggered functions for agenda module
42
 */
43
class InterfaceContactRoles extends DolibarrTriggers
44
{
45
    /**
46
     * Constructor
47
     *
48
     * @param DoliDB $db Database handler
49
     */
50
    public function __construct($db)
51
    {
52
        $this->db = $db;
53
54
        $this->name = preg_replace('/^Interface/i', '', get_class($this));
55
        $this->family = "agenda";
56
        $this->description = "Triggers of this module auto link contact to company.";
57
        $this->version = self::VERSIONS['prod'];
58
        $this->picto = 'company';
59
    }
60
61
    /**
62
     * Function called when a Dolibarr business event is done.
63
     * All functions "runTrigger" are triggered if file is inside directory htdocs/core/triggers or htdocs/module/code/triggers (and declared)
64
     *
65
     * Following properties may be set before calling trigger. The may be completed by this trigger to be used for writing the event into database:
66
     * $object->socid or $object->fk_soc(id of thirdparty)
67
     * $object->element (element type of object)
68
     *
69
     * @param string $action    Event action code
70
     * @param Object $object    Object
71
     * @param User $user        Object user
72
     * @param Translate $langs  Object langs
73
     * @param Conf $conf        Object conf
74
     * @return int Return integer <0 if KO, 0 if no triggered ran, >0 if OK
75
     */
76
    public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
77
    {
78
        if (
79
            $action === 'PROPAL_CREATE' || $action === 'ORDER_CREATE' || $action === 'BILL_CREATE'
80
            || $action === 'ORDER_SUPPLIER_CREATE' || $action === 'BILL_SUPPLIER_CREATE' || $action === 'PROPOSAL_SUPPLIER_CREATE'
81
            || $action === 'CONTRACT_CREATE' || $action === 'FICHINTER_CREATE' || $action === 'PROJECT_CREATE' || $action === 'TICKET_CREATE'
82
        ) {
83
            dol_syslog("Trigger '" . $this->name . "' for action '" . $action . "' launched by " . __FILE__ . ". id=" . $object->id);
84
85
            $socid = (property_exists($object, 'socid') ? $object->socid : $object->fk_soc);
86
87
            if (!empty($socid) && $socid > 0) {
88
                $contactdefault = new Contact($this->db);
89
                $contactdefault->socid = $socid;
90
91
                $TContact = array();
92
                if (method_exists($contactdefault, 'getContactRoles')) {    // For backward compatibility
93
                    $TContact = $contactdefault->getContactRoles($object->element);
94
                }
95
96
                if (is_array($TContact) && !empty($TContact)) {
97
                    $TContactAlreadyLinked = array();
98
99
                    if ($object->id > 0) {
100
                        $TContactAlreadyLinked = array_merge($object->liste_contact(-1, 'external'), $object->liste_contact(-1, 'internal'));
101
                    }
102
103
                    foreach ($TContact as $i => $infos) {
104
                        foreach ($TContactAlreadyLinked as $contactData) {
105
                            if ($contactData['id'] == $infos['fk_socpeople'] && $contactData['fk_c_type_contact'] == $infos['type_contact']) {
106
                                unset($TContact[$i]);
107
                            }
108
                        }
109
                    }
110
111
                    $nb = 0;
112
                    foreach ($TContact as $infos) {
113
                        $res = $object->add_contact($infos['fk_socpeople'], $infos['type_contact']);
114
                        if ($res > 0) {
115
                            $nb++;
116
                        }
117
                    }
118
119
                    // We disable this message, it shows the message in api, public page or batch actions when it should not.
120
                    // Message setting must be done by the calling GUI page and not set inside the trigger.
121
                    /*
122
                    if ($nb > 0) {
123
                        setEventMessages($langs->trans('ContactAddedAutomatically', $nb), null, 'mesgs');
124
                    }
125
                    */
126
                }
127
            }
128
        }
129
        return 0;
130
    }
131
}
132