Passed
Push — EXTRACT_CLASSES ( 0382f2...c25e41 )
by Rafael
52:18
created

FormSms::show_form()   F

Complexity

Conditions 40
Paths > 20000

Size

Total Lines 219
Code Lines 130

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 40
eloc 130
nc 206336
nop 2
dl 0
loc 219
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* Copyright (C) 2005-2011  Laurent Destailleur         <[email protected]>
4
 * Copyright (C) 2010       Juanjo Menent               <[email protected]>
5
 * Copyright (C) 2018-2024  Frédéric France             <[email protected]>
6
 * Copyright (C) 2024       Rafael San José             <[email protected]>
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
namespace Dolibarr\Code\Core\Classes;
23
24
use DoliDB;
25
26
/**
27
 *       \file       htdocs/core/class/html.formsms.class.php
28
 *       \ingroup    core
29
 *       \brief      Fichier de la class permettant la generation du formulaire html d'envoi de mail unitaire
30
 */
31
32
require_once constant('DOL_DOCUMENT_ROOT') . '/core/class/html.form.class.php';
33
34
/**
35
 *      Class permettant la generation du formulaire d'envoi de Sms
36
 *      Usage: $formsms = new FormSms($db)
37
 *             $formsms->proprietes=1 ou chaine ou tableau de valeurs
38
 *             $formsms->show_form() affiche le formulaire
39
 */
40
class FormSms
41
{
42
    /**
43
     * @var DoliDB Database handler.
44
     */
45
    public $db;
46
47
    public $fromid;
48
    public $fromname;
49
    public $fromsms;
50
51
    /**
52
     * @var string
53
     */
54
    public $fromtype;
55
    public $replytoname;
56
    public $replytomail;
57
    public $toname;
58
    public $tomail;
59
60
    public $withsubstit; // Show substitution array
61
62
    /**
63
     * @var int
64
     */
65
    public $withfrom;
66
67
    /**
68
     * @var int
69
     */
70
    public $withto;
71
72
    /**
73
     * @var int
74
     */
75
    public $withtopic;
76
77
    /**
78
     * @var int
79
     */
80
    public $withbody;
81
82
    /**
83
     * @var int     Id of company
84
     */
85
    public $withtosocid;
86
87
    public $withfromreadonly;
88
    public $withreplytoreadonly;
89
    public $withtoreadonly;
90
    public $withtopicreadonly;
91
    public $withbodyreadonly;
92
    public $withcancel;
93
94
    public $substit = array();
95
    public $param = array();
96
97
    /**
98
     * @var string Error code (or message)
99
     */
100
    public $error = '';
101
102
    /**
103
     * @var string[]    Array of error strings
104
     */
105
    public $errors = array();
106
107
108
    /**
109
     *  Constructor
110
     *
111
     *  @param      DoliDB      $db      Database handler
112
     */
113
    public function __construct($db)
114
    {
115
        $this->db = $db;
116
117
        $this->withfrom = 1;
118
        $this->withto = 1;
119
        $this->withtopic = 1;
120
        $this->withbody = 1;
121
122
        $this->withfromreadonly = 1;
123
        $this->withreplytoreadonly = 1;
124
        $this->withtoreadonly = 0;
125
        $this->withtopicreadonly = 0;
126
        $this->withbodyreadonly = 0;
127
    }
128
129
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
130
    /**
131
     *  Show the form to input an sms.
132
     *
133
     *  @param  string  $morecss Class on first column td
134
     *  @param int $showform Show form tags and submit button (recommended is to use with value 0)
135
     *  @return void
136
     */
137
    public function show_form($morecss = 'titlefield', $showform = 1)
138
    {
139
		// phpcs:enable
140
        global $conf, $langs, $form;
141
142
        if (!is_object($form)) {
143
            $form = new Form($this->db);
144
        }
145
146
        // Load translation files required by the page
147
        $langs->loadLangs(array('other', 'mails', 'sms'));
148
149
        $soc = new Societe($this->db);
0 ignored issues
show
Bug introduced by
The type Dolibarr\Code\Core\Classes\Societe was not found. Did you mean Societe? If so, make sure to prefix the type with \.
Loading history...
150
        if (!empty($this->withtosocid) && $this->withtosocid > 0) {
151
            $soc->fetch($this->withtosocid);
152
        }
153
154
        print "\n<!-- Begin form SMS -->\n";
155
156
        print '
157
<script nonce="' . getNonce() . '" type="text/javascript">
158
function limitChars(textarea, limit, infodiv)
159
{
160
    var text = textarea.value;
161
    var textlength = text.length;
162
    var info = document.getElementById(infodiv);
163
164
    info.innerHTML = (limit - textlength);
165
    return true;
166
}
167
</script>';
168
169
        if ($showform) {
170
            print "<form method=\"POST\" name=\"smsform\" enctype=\"multipart/form-data\" action=\"" . $this->param["returnurl"] . "\">\n";
171
        }
172
173
        print '<input type="hidden" name="token" value="' . newToken() . '">';
174
        foreach ($this->param as $key => $value) {
175
            print "<input type=\"hidden\" name=\"$key\" value=\"$value\">\n";
176
        }
177
        print "<table class=\"border centpercent\">\n";
178
179
        // Substitution array
180
        if (!empty($this->withsubstit)) {       // Unset or set ->withsubstit=0 to disable this.
181
            print "<tr><td colspan=\"2\">";
182
            $help = "";
183
            foreach ($this->substit as $key => $val) {
184
                $help .= $key . ' -> ' . $langs->trans($val) . '<br>';
185
            }
186
            print $form->textwithpicto($langs->trans("SmsTestSubstitutionReplacedByGenericValues"), $help);
187
            print "</td></tr>\n";
188
        }
189
190
        // From
191
        if ($this->withfrom) {
192
            if ($this->withfromreadonly) {
193
                print '<tr><td class="titlefield ' . $morecss . '">' . $langs->trans("SmsFrom");
194
                print '<input type="hidden" name="fromsms" value="' . $this->fromsms . '">';
195
                print "</td><td>";
196
                if ($this->fromtype == 'user') {
197
                    $langs->load("users");
198
                    $fuser = new User($this->db);
0 ignored issues
show
Bug introduced by
The type Dolibarr\Code\Core\Classes\User was not found. Did you mean User? If so, make sure to prefix the type with \.
Loading history...
199
                    $fuser->fetch($this->fromid);
200
                    print $fuser->getNomUrl(1);
201
                    print ' &nbsp; ';
202
                }
203
                if ($this->fromsms) {
204
                    print $this->fromsms;
205
                } else {
206
                    if ($this->fromtype) {
207
                        $langs->load("errors");
208
                        print '<span class="warning"> &lt;' . $langs->trans("ErrorNoPhoneDefinedForThisUser") . '&gt; </span>';
209
                    }
210
                }
211
                print "</td></tr>\n";
212
                print "</td></tr>\n";
213
            } else {
214
                print '<tr><td class="' . $morecss . '">' . $langs->trans("SmsFrom") . "</td><td>";
215
                if (getDolGlobalString('MAIN_SMS_SENDMODE')) {
216
                    $sendmode = getDolGlobalString('MAIN_SMS_SENDMODE');    // $conf->global->MAIN_SMS_SENDMODE looks like a value 'module'
217
                    $classmoduleofsender = getDolGlobalString('MAIN_MODULE_' . strtoupper($sendmode) . '_SMS', $sendmode);  // $conf->global->MAIN_MODULE_XXX_SMS looks like a value 'class@module'
218
                    if ($classmoduleofsender == 'ovh') {
219
                        $classmoduleofsender = 'ovhsms@ovh';    // For backward compatibility
220
                    }
221
222
                    $tmp = explode('@', $classmoduleofsender);
223
                    $classfile = $tmp[0];
224
                    $module = (empty($tmp[1]) ? $tmp[0] : $tmp[1]);
225
                    dol_include_once('/' . $module . '/class/' . $classfile . '.class.php');
226
                    try {
227
                        $classname = ucfirst($classfile);
228
                        if (class_exists($classname)) {
229
                            $sms = new $classname($this->db);
230
                            $resultsender = $sms->SmsSenderList();
231
                        } else {
232
                            $sms = new stdClass();
0 ignored issues
show
Bug introduced by
The type Dolibarr\Code\Core\Classes\stdClass was not found. Did you mean stdClass? If so, make sure to prefix the type with \.
Loading history...
233
                            $sms->error = 'The SMS manager "' . $classfile . '" defined into SMS setup MAIN_MODULE_' . strtoupper($sendmode) . '_SMS is not found';
234
                        }
235
                    } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The type Dolibarr\Code\Core\Classes\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
236
                        dol_print_error(null, 'Error to get list of senders: ' . $e->getMessage());
237
                        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
238
                    }
239
                } else {
240
                    dol_syslog("Warning: The SMS sending method has not been defined into MAIN_SMS_SENDMODE", LOG_WARNING);
241
                    $resultsender = array();
242
                    $resultsender[0]->number = $this->fromsms;
243
                }
244
245
                if (is_array($resultsender) && count($resultsender) > 0) {
246
                    print '<select name="fromsms" id="fromsms" class="flat">';
247
                    foreach ($resultsender as $obj) {
248
                        print '<option value="' . $obj->number . '">' . $obj->number . '</option>';
249
                    }
250
                    print '</select>';
251
                } else {
252
                    print '<span class="error wordbreak">' . $langs->trans("SmsNoPossibleSenderFound");
253
                    if (is_object($sms) && !empty($sms->error)) {
254
                        print ' ' . $sms->error;
255
                    }
256
                    print '</span>';
257
                }
258
                print '</td>';
259
                print "</tr>\n";
260
            }
261
        }
262
263
        // To (target)
264
        if ($this->withto || is_array($this->withto)) {
265
            print '<tr><td>';
266
            //$moretext=$langs->trans("YouCanUseCommaSeparatorForSeveralRecipients");
267
            $moretext = '';
268
            print $form->textwithpicto($langs->trans("SmsTo"), $moretext);
269
            print '</td><td>';
270
            if ($this->withtoreadonly) {
271
                print (!is_array($this->withto) && !is_numeric($this->withto)) ? $this->withto : "";
272
            } else {
273
                print '<input class="width150" id="sendto" name="sendto" value="' . dol_escape_htmltag(!is_array($this->withto) && $this->withto != '1' ? (GETPOSTISSET("sendto") ? GETPOST("sendto") : $this->withto) : "+") . '">';
274
                if (!empty($this->withtosocid) && $this->withtosocid > 0) {
275
                    $liste = array();
276
                    foreach ($soc->thirdparty_and_contact_phone_array() as $key => $value) {
277
                        $liste[$key] = $value;
278
                    }
279
                    print " " . $langs->trans("or") . " ";
280
                    //var_dump($_REQUEST);exit;
281
                    print $form->selectarray("receiver", $liste, GETPOST("receiver"), 1);
282
                }
283
                print '<span class="opacitymedium hideonsmartphone"> ' . $langs->trans("SmsInfoNumero") . '</span>';
284
            }
285
            print "</td></tr>\n";
286
        }
287
288
        // Message
289
        if ($this->withbody) {
290
            $defaultmessage = '';
291
            if ($this->param["models"] == 'body') {
292
                $defaultmessage = $this->withbody;
293
            }
294
            $defaultmessage = make_substitutions($defaultmessage, $this->substit);
295
            if (GETPOSTISSET("message")) {
296
                $defaultmessage = GETPOST("message", 'restricthtml');
297
            }
298
            $defaultmessage = str_replace('\n', "\n", $defaultmessage);
299
300
            print "<tr>";
301
            print '<td class="tdtop">' . $langs->trans("SmsText") . "</td>";
302
            print "<td>";
303
            if ($this->withbodyreadonly) {
304
                print nl2br($defaultmessage);
305
                print '<input type="hidden" name="message" value="' . dol_escape_htmltag($defaultmessage) . '">';
306
            } else {
307
                print '<textarea class="quatrevingtpercent" name="message" id="message" rows="' . ROWS_4 . '" onkeyup="limitChars(this, 160, \'charlimitinfospan\')">' . $defaultmessage . '</textarea>';
308
                print '<div id="charlimitinfo" class="opacitymedium">' . $langs->trans("SmsInfoCharRemain") . ': <span id="charlimitinfospan">' . (160 - dol_strlen($defaultmessage)) . '</span></div></td>';
309
            }
310
            print "</td></tr>\n";
311
        }
312
313
        print '
314
           <tr>
315
            <td>' . $langs->trans("DelayBeforeSending") . ':</td>
316
            <td> <input name="deferred" id="deferred" size="4" value="0"></td></tr>
317
318
           <tr><td>' . $langs->trans("Priority") . ' :</td><td>
319
           <select name="priority" id="priority" class="flat">
320
           <option value="0">high</option>
321
           <option value="1">medium</option>
322
           <option value="2" selected>low</option>
323
           <option value="3">veryLow</option>
324
           </select></td></tr>
325
326
           <tr><td>' . $langs->trans("Type") . ' :</td><td>
327
           <select name="class" id="class" class="flat">
328
           <option value="0">Flash</option>
329
           <option value="1" selected>Standard</option>
330
           <option value="2">SIM</option>
331
           <option value="3">ToolKit</option>
332
           </select></td></tr>
333
334
           <tr><td>' . $langs->trans("DisableStopIfSupported") . ' :</td><td>
335
           <select name="disablestop" id="disablestop" class="flat">
336
           <option value="0" selected>No</option>
337
           <option value="1" selected>Yes</option>
338
           </select></td></tr>';
339
340
        print "</table>\n";
341
342
343
        if ($showform) {
344
            print '<div class="center">';
345
            print '<input type="submit" class="button" name="sendmail" value="' . dol_escape_htmltag($langs->trans("SendSms")) . '">';
346
            if ($this->withcancel) {
347
                print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
348
                print '<input class="button button-cancel" type="submit" name="cancel" value="' . dol_escape_htmltag($langs->trans("Cancel")) . '">';
349
            }
350
            print '</div>';
351
352
            print "</form>\n";
353
        }
354
355
        print "<!-- End form SMS -->\n";
356
    }
357
}
358