ActionsContactCardCommon::assign_post()   B
last analyzed

Complexity

Conditions 6
Paths 18

Size

Total Lines 36
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 29
nc 18
nop 0
dl 0
loc 36
rs 8.8337
c 0
b 0
f 0
1
<?php
2
3
/* Copyright (C) 2010-2012  Regis Houssin               <[email protected]>
4
 * Copyright (C) 2024		MDW							<[email protected]>
5
 * Copyright (C) 2024       Rafael San José             <[email protected]>
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
use Dolibarr\Code\Contact\Classes\Contact;
22
use Dolibarr\Code\Societe\Classes\Societe;
23
use Dolibarr\Code\User\Classes\User;
24
use Dolibarr\Lib\ViewMain;
25
26
/**
27
 *  \file       htdocs/contact/canvas/actions_contactcard_common.class.php
28
 *  \ingroup    thirdparty
29
 *  \brief      File for the class Thirdparty contact card controller (common)
30
 */
31
32
/**
33
 *  \class      ActionsContactCardCommon
34
 *  \brief      Common Abstract Class for contact managmeent
35
 */
36
abstract class ActionsContactCardCommon
37
{
38
    /**
39
     * @var DoliDB Database handler.
40
     */
41
    public $db;
42
43
    public $dirmodule;
44
    public $targetmodule;
45
    public $canvas;
46
    public $card;
47
48
    //! Template container
49
    public $tpl = array();
50
    //! Object container
51
    public $object;
52
53
    /**
54
     * @var string Error code (or message)
55
     */
56
    public $error = '';
57
58
59
    /**
60
     * @var string[] Error codes (or messages)
61
     */
62
    public $errors = array();
63
64
65
    /**
66
     *  Get object
67
     *
68
     * @param int $id Object id
69
     * @return object          Object loaded
70
     */
71
    public function getObject($id)
72
    {
73
        /*$ret = $this->getInstanceDao();
74
75
        if (is_object($this->object) && method_exists($this->object,'fetch'))
76
        {
77
            if (!empty($id)) $this->object->fetch($id);
78
        }
79
        else
80
        {*/
81
        $object = new Contact($this->db);
82
        if (!empty($id)) {
83
            $object->fetch($id);
84
        }
85
86
        $this->object = $object;
87
88
        return $object;
89
    }
90
91
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
92
93
    /**
94
     *  Set content of ->tpl array, to use into template
95
     *
96
     * @param string $action Type of action
97
     * @param int $id Id
98
     * @return void
99
     */
100
    public function assign_values(&$action, $id)
101
    {
102
        // phpcs:enable
103
        global $conf, $langs, $user, $canvas;
104
        global $form, $formcompany, $objsoc;
105
106
        if ($action == 'add' || $action == 'update') {
107
            $this->assign_post();
108
        }
109
110
        foreach ($this->object as $key => $value) {
111
            $this->tpl[$key] = $value;
112
        }
113
114
        $this->tpl['error'] = $this->error;
115
        $this->tpl['errors'] = $this->errors;
116
117
        if ($action == 'create' || $action == 'edit') {
118
            if ($conf->use_javascript_ajax) {
119
                $this->tpl['ajax_selectcountry'] = "\n" . '<script type="text/javascript">
120
				jQuery(document).ready(function () {
121
						jQuery("#selectcountry_id").change(function() {
122
							document.formsoc.action.value="' . $action . '";
123
							document.formsoc.canvas.value="' . $canvas . '";
124
							document.formsoc.submit();
125
						});
126
					})
127
				</script>' . "\n";
128
            }
129
130
            if (is_object($objsoc) && $objsoc->id > 0) {
131
                $this->tpl['company'] = $objsoc->getNomUrl(1);
132
                $this->tpl['company_id'] = $objsoc->id;
133
            } else {
134
                $this->tpl['company'] = $form->select_company($this->object->socid, 'socid', '', 1);
135
            }
136
137
            // Civility
138
            $this->tpl['select_civility'] = $formcompany->select_civility($this->object->civility_id);
139
140
            // Predefined with third party
141
            if ((isset($objsoc->typent_code) && $objsoc->typent_code == 'TE_PRIVATE') || getDolGlobalString('CONTACT_USE_COMPANY_ADDRESS')) {
142
                if (dol_strlen(trim($this->object->address)) == 0) {
143
                    $this->tpl['address'] = $objsoc->address;
144
                }
145
                if (dol_strlen(trim($this->object->zip)) == 0) {
146
                    $this->object->zip = $objsoc->zip;
147
                }
148
                if (dol_strlen(trim($this->object->town)) == 0) {
149
                    $this->object->town = $objsoc->town;
150
                }
151
                if (dol_strlen(trim($this->object->phone_pro)) == 0) {
152
                    $this->object->phone_pro = $objsoc->phone;
153
                }
154
                if (dol_strlen(trim($this->object->fax)) == 0) {
155
                    $this->object->fax = $objsoc->fax;
156
                }
157
                if (dol_strlen(trim($this->object->email)) == 0) {
158
                    $this->object->email = $objsoc->email;
159
                }
160
            }
161
162
            // Zip
163
            $this->tpl['select_zip'] = $formcompany->select_ziptown($this->object->zip, 'zipcode', array('town', 'selectcountry_id', 'state_id'), 6);
164
165
            // Town
166
            $this->tpl['select_town'] = $formcompany->select_ziptown($this->object->town, 'town', array('zipcode', 'selectcountry_id', 'state_id'));
167
168
            if (dol_strlen(trim((string)$this->object->country_id)) == 0) {
169
                $this->object->country_id = $objsoc->country_id;
170
            }
171
172
            // Country
173
            $this->tpl['select_country'] = $form->select_country($this->object->country_id, 'country_id');
174
            $countrynotdefined = $langs->trans("ErrorSetACountryFirst") . ' (' . $langs->trans("SeeAbove") . ')';
175
176
            if ($user->admin) {
177
                $this->tpl['info_admin'] = info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
178
            }
179
180
            // State
181
            if ($this->object->country_id) {
182
                $this->tpl['select_state'] = $formcompany->select_state($this->object->state_id, $this->object->country_code);
183
            } else {
184
                $this->tpl['select_state'] = $countrynotdefined;
185
            }
186
187
            // Public or private
188
            $selectarray = array('0' => $langs->trans("ContactPublic"), '1' => $langs->trans("ContactPrivate"));
189
            $this->tpl['select_visibility'] = $form->selectarray('priv', $selectarray, $this->object->priv, 0);
190
        }
191
192
        if ($action == 'view' || $action == 'edit' || $action == 'delete') {
193
            // Emailing
194
            if (isModEnabled('mailing')) {
195
                $langs->load("mails");
196
                $this->tpl['nb_emailing'] = $this->object->getNbOfEMailings();
197
            }
198
199
            // Linked element
200
            $this->tpl['contact_element'] = array();
201
            $i = 0;
202
203
            $this->object->load_ref_elements();
204
205
            if (isModEnabled('order')) {
206
                $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForOrders");
207
                $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_commande ? $this->object->ref_commande : $langs->trans("NoContactForAnyOrder");
208
                $i++;
209
            }
210
            if (isModEnabled("propal")) {
211
                $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForProposals");
212
                $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_propal ? $this->object->ref_propal : $langs->trans("NoContactForAnyProposal");
213
                $i++;
214
            }
215
            if (isModEnabled('contract')) {
216
                $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForContracts");
217
                $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_contrat ? $this->object->ref_contrat : $langs->trans("NoContactForAnyContract");
218
                $i++;
219
            }
220
            if (isModEnabled('invoice')) {
221
                $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForInvoices");
222
                $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_facturation ? $this->object->ref_facturation : $langs->trans("NoContactForAnyInvoice");
223
                $i++;
224
            }
225
226
            // Dolibarr user
227
            if ($this->object->user_id) {
228
                $dolibarr_user = new User($this->db);
229
                $result = $dolibarr_user->fetch($this->object->user_id);
230
                $this->tpl['dolibarr_user'] = $dolibarr_user->getLoginUrl(1);
231
            } else {
232
                $this->tpl['dolibarr_user'] = $langs->trans("NoDolibarrAccess");
233
            }
234
        }
235
236
        if ($action == 'view' || $action == 'delete') {
237
            $this->tpl['showrefnav'] = $form->showrefnav($this->object, 'id');
238
239
            if ($this->object->socid > 0) {
240
                $objsoc = new Societe($this->db);
241
242
                $objsoc->fetch($this->object->socid);
243
                $this->tpl['company'] = $objsoc->getNomUrl(1);
244
            } else {
245
                $this->tpl['company'] = $langs->trans("ContactNotLinkedToCompany");
246
            }
247
248
            $this->tpl['civility'] = $this->object->getCivilityLabel();
249
250
            $this->tpl['address'] = dol_nl2br($this->object->address);
251
252
            $this->tpl['zip'] = ($this->object->zip ? $this->object->zip . '&nbsp;' : '');
253
254
            $img = picto_from_langcode($this->object->country_code);
255
            $this->tpl['country'] = ($img ? $img . ' ' : '') . $this->object->country;
256
257
            $this->tpl['phone_pro'] = dol_print_phone($this->object->phone_pro, $this->object->country_code, 0, $this->object->id, 'AC_TEL');
258
            $this->tpl['phone_perso'] = dol_print_phone($this->object->phone_perso, $this->object->country_code, 0, $this->object->id, 'AC_TEL');
259
            $this->tpl['phone_mobile'] = dol_print_phone($this->object->phone_mobile, $this->object->country_code, 0, $this->object->id, 'AC_TEL');
260
            $this->tpl['fax'] = dol_print_phone($this->object->fax, $this->object->country_code, 0, $this->object->id, 'AC_FAX');
261
            $this->tpl['email'] = dol_print_email($this->object->email, 0, $this->object->id, 1);
262
263
            $this->tpl['visibility'] = $this->object->LibPubPriv($this->object->priv);
264
265
            $this->tpl['note'] = $this->object->note_private;
266
        }
267
268
        if ($action == 'create_user') {
269
            // Full firstname and lastname separated with a dot : firstname.lastname
270
            include_once DOL_DOCUMENT_ROOT . '/core/lib/functions2.lib.php';
271
            require_once constant('DOL_DOCUMENT_ROOT') . '/core/lib/security2.lib.php';
272
            $login = dol_buildlogin($this->object->lastname, $this->object->firstname);
273
274
            $generated_password = getRandomPassword(false);
275
            $password = $generated_password;
276
277
            // Create a form array
278
            $formquestion = array(
279
                array('label' => $langs->trans("LoginToCreate"), 'type' => 'text', 'name' => 'login', 'value' => $login),
280
                array('label' => $langs->trans("Password"), 'type' => 'text', 'name' => 'password', 'value' => $password));
281
282
            $this->tpl['action_create_user'] = $form->formconfirm($_SERVER["PHP_SELF"] . "?id=" . $this->object->id, $langs->trans("CreateDolibarrLogin"), $langs->trans("ConfirmCreateContact"), "confirm_create_user", $formquestion, 'no');
283
        }
284
    }
285
286
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
287
288
    /**
289
     *  Assign POST values into object
290
     *
291
     * @return     void
292
     */
293
    private function assign_post()
294
    {
295
        // phpcs:enable
296
        global $langs, $mysoc;
297
298
        $this->object->socid = GETPOSTINT("socid");
299
        $this->object->lastname = GETPOST("name");
300
        $this->object->firstname = GETPOST("firstname");
301
        $this->object->civility_id = GETPOST("civility_id");
302
        $this->object->poste = GETPOST("poste");
303
        $this->object->address = GETPOST("address");
304
        $this->object->zip = GETPOST("zipcode");
305
        $this->object->town = GETPOST("town");
306
        $this->object->country_id = GETPOST("country_id") ? GETPOST("country_id") : $mysoc->country_id;
307
        $this->object->state_id = GETPOST("state_id");
308
        $this->object->phone_pro = GETPOST("phone_pro");
309
        $this->object->phone_perso = GETPOST("phone_perso");
310
        $this->object->phone_mobile = GETPOST("phone_mobile");
311
        $this->object->fax = GETPOST("fax");
312
        $this->object->email = GETPOST("email");
313
        $this->object->priv = GETPOST("priv");
314
        $this->object->note = GETPOST("note", "restricthtml");
315
        $this->object->canvas = GETPOST("canvas");
316
317
        // We set country_id, and country_code label of the chosen country
318
        if ($this->object->country_id) {
319
            $sql = "SELECT code, label FROM " . MAIN_DB_PREFIX . "c_country WHERE rowid = " . ((int)$this->object->country_id);
320
            $resql = $this->db->query($sql);
321
            if ($resql) {
322
                $obj = $this->db->fetch_object($resql);
323
            } else {
324
                dol_print_error($this->db);
325
            }
326
            $this->object->country_id = $langs->trans("Country" . $obj->code) ? $langs->trans("Country" . $obj->code) : $obj->label;
327
            $this->object->country_code = $obj->code;
328
            $this->object->country = $langs->trans("Country" . $obj->code) ? $langs->trans("Country" . $obj->code) : $obj->label;
329
        }
330
    }
331
}
332