dolibarr  20.0.0-beta
html.formsms.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2005-2011 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2010 Juanjo Menent <jmenent@2byte.es>
4  * Copyright (C) 2018-2024 Frédéric France <frederic.france@free.fr>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
25 require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
26 
27 
34 class FormSms
35 {
39  public $db;
40 
41  public $fromid;
42  public $fromname;
43  public $fromsms;
44 
48  public $fromtype;
49  public $replytoname;
50  public $replytomail;
51  public $toname;
52  public $tomail;
53 
54  public $withsubstit; // Show substitution array
55 
59  public $withfrom;
60 
64  public $withto;
65 
69  public $withtopic;
70 
74  public $withbody;
75 
79  public $withtosocid;
80 
81  public $withfromreadonly;
82  public $withreplytoreadonly;
83  public $withtoreadonly;
84  public $withtopicreadonly;
85  public $withbodyreadonly;
86  public $withcancel;
87 
88  public $substit = array();
89  public $param = array();
90 
94  public $error = '';
95 
99  public $errors = array();
100 
101 
107  public function __construct($db)
108  {
109  $this->db = $db;
110 
111  $this->withfrom = 1;
112  $this->withto = 1;
113  $this->withtopic = 1;
114  $this->withbody = 1;
115 
116  $this->withfromreadonly = 1;
117  $this->withreplytoreadonly = 1;
118  $this->withtoreadonly = 0;
119  $this->withtopicreadonly = 0;
120  $this->withbodyreadonly = 0;
121  }
122 
123  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
131  public function show_form($morecss = 'titlefield', $showform = 1)
132  {
133  // phpcs:enable
134  global $conf, $langs, $form;
135 
136  if (!is_object($form)) {
137  $form = new Form($this->db);
138  }
139 
140  // Load translation files required by the page
141  $langs->loadLangs(array('other', 'mails', 'sms'));
142 
143  $soc = new Societe($this->db);
144  if (!empty($this->withtosocid) && $this->withtosocid > 0) {
145  $soc->fetch($this->withtosocid);
146  }
147 
148  print "\n<!-- Begin form SMS -->\n";
149 
150  print '
151 <script nonce="'.getNonce().'" type="text/javascript">
152 function limitChars(textarea, limit, infodiv)
153 {
154  var text = textarea.value;
155  var textlength = text.length;
156  var info = document.getElementById(infodiv);
157 
158  info.innerHTML = (limit - textlength);
159  return true;
160 }
161 </script>';
162 
163  if ($showform) {
164  print "<form method=\"POST\" name=\"smsform\" enctype=\"multipart/form-data\" action=\"".$this->param["returnurl"]."\">\n";
165  }
166 
167  print '<input type="hidden" name="token" value="'.newToken().'">';
168  foreach ($this->param as $key => $value) {
169  print "<input type=\"hidden\" name=\"$key\" value=\"$value\">\n";
170  }
171  print "<table class=\"border centpercent\">\n";
172 
173  // Substitution array
174  if (!empty($this->withsubstit)) { // Unset or set ->withsubstit=0 to disable this.
175  print "<tr><td colspan=\"2\">";
176  $help = "";
177  foreach ($this->substit as $key => $val) {
178  $help .= $key.' -> '.$langs->trans($val).'<br>';
179  }
180  print $form->textwithpicto($langs->trans("SmsTestSubstitutionReplacedByGenericValues"), $help);
181  print "</td></tr>\n";
182  }
183 
184  // From
185  if ($this->withfrom) {
186  if ($this->withfromreadonly) {
187  print '<tr><td class="titlefield '.$morecss.'">'.$langs->trans("SmsFrom");
188  print '<input type="hidden" name="fromsms" value="'.$this->fromsms.'">';
189  print "</td><td>";
190  if ($this->fromtype == 'user') {
191  $langs->load("users");
192  $fuser = new User($this->db);
193  $fuser->fetch($this->fromid);
194  print $fuser->getNomUrl(1);
195  print ' &nbsp; ';
196  }
197  if ($this->fromsms) {
198  print $this->fromsms;
199  } else {
200  if ($this->fromtype) {
201  $langs->load("errors");
202  print '<span class="warning"> &lt;'.$langs->trans("ErrorNoPhoneDefinedForThisUser").'&gt; </span>';
203  }
204  }
205  print "</td></tr>\n";
206  print "</td></tr>\n";
207  } else {
208  print '<tr><td class="'.$morecss.'">'.$langs->trans("SmsFrom")."</td><td>";
209  if (getDolGlobalString('MAIN_SMS_SENDMODE')) {
210  $sendmode = getDolGlobalString('MAIN_SMS_SENDMODE'); // $conf->global->MAIN_SMS_SENDMODE looks like a value 'module'
211  $classmoduleofsender = getDolGlobalString('MAIN_MODULE_'.strtoupper($sendmode).'_SMS', $sendmode); // $conf->global->MAIN_MODULE_XXX_SMS looks like a value 'class@module'
212  if ($classmoduleofsender == 'ovh') {
213  $classmoduleofsender = 'ovhsms@ovh'; // For backward compatibility
214  }
215 
216  $tmp = explode('@', $classmoduleofsender);
217  $classfile = $tmp[0];
218  $module = (empty($tmp[1]) ? $tmp[0] : $tmp[1]);
219  dol_include_once('/'.$module.'/class/'.$classfile.'.class.php');
220  try {
221  $classname = ucfirst($classfile);
222  if (class_exists($classname)) {
223  $sms = new $classname($this->db);
224  $resultsender = $sms->SmsSenderList();
225  } else {
226  $sms = new stdClass();
227  $sms->error = 'The SMS manager "'.$classfile.'" defined into SMS setup MAIN_MODULE_'.strtoupper($sendmode).'_SMS is not found';
228  }
229  } catch (Exception $e) {
230  dol_print_error(null, 'Error to get list of senders: '.$e->getMessage());
231  exit;
232  }
233  } else {
234  dol_syslog("Warning: The SMS sending method has not been defined into MAIN_SMS_SENDMODE", LOG_WARNING);
235  $resultsender = array();
236  $resultsender[0]->number = $this->fromsms;
237  }
238 
239  if (is_array($resultsender) && count($resultsender) > 0) {
240  print '<select name="fromsms" id="fromsms" class="flat">';
241  foreach ($resultsender as $obj) {
242  print '<option value="'.$obj->number.'">'.$obj->number.'</option>';
243  }
244  print '</select>';
245  } else {
246  print '<span class="error wordbreak">'.$langs->trans("SmsNoPossibleSenderFound");
247  if (is_object($sms) && !empty($sms->error)) {
248  print ' '.$sms->error;
249  }
250  print '</span>';
251  }
252  print '</td>';
253  print "</tr>\n";
254  }
255  }
256 
257  // To (target)
258  if ($this->withto || is_array($this->withto)) {
259  print '<tr><td>';
260  //$moretext=$langs->trans("YouCanUseCommaSeparatorForSeveralRecipients");
261  $moretext = '';
262  print $form->textwithpicto($langs->trans("SmsTo"), $moretext);
263  print '</td><td>';
264  if ($this->withtoreadonly) {
265  print (!is_array($this->withto) && !is_numeric($this->withto)) ? $this->withto : "";
266  } else {
267  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) : "+").'">';
268  if (!empty($this->withtosocid) && $this->withtosocid > 0) {
269  $liste = array();
270  foreach ($soc->thirdparty_and_contact_phone_array() as $key => $value) {
271  $liste[$key] = $value;
272  }
273  print " ".$langs->trans("or")." ";
274  //var_dump($_REQUEST);exit;
275  print $form->selectarray("receiver", $liste, GETPOST("receiver"), 1);
276  }
277  print '<span class="opacitymedium hideonsmartphone"> '.$langs->trans("SmsInfoNumero").'</span>';
278  }
279  print "</td></tr>\n";
280  }
281 
282  // Message
283  if ($this->withbody) {
284  $defaultmessage = '';
285  if ($this->param["models"] == 'body') {
286  $defaultmessage = $this->withbody;
287  }
288  $defaultmessage = make_substitutions($defaultmessage, $this->substit);
289  if (GETPOSTISSET("message")) {
290  $defaultmessage = GETPOST("message", 'restricthtml');
291  }
292  $defaultmessage = str_replace('\n', "\n", $defaultmessage);
293 
294  print "<tr>";
295  print '<td class="tdtop">'.$langs->trans("SmsText")."</td>";
296  print "<td>";
297  if ($this->withbodyreadonly) {
298  print nl2br($defaultmessage);
299  print '<input type="hidden" name="message" value="'.dol_escape_htmltag($defaultmessage).'">';
300  } else {
301  print '<textarea class="quatrevingtpercent" name="message" id="message" rows="'.ROWS_4.'" onkeyup="limitChars(this, 160, \'charlimitinfospan\')">'.$defaultmessage.'</textarea>';
302  print '<div id="charlimitinfo" class="opacitymedium">'.$langs->trans("SmsInfoCharRemain").': <span id="charlimitinfospan">'.(160 - dol_strlen($defaultmessage)).'</span></div></td>';
303  }
304  print "</td></tr>\n";
305  }
306 
307  print '
308  <tr>
309  <td>'.$langs->trans("DelayBeforeSending").':</td>
310  <td> <input name="deferred" id="deferred" size="4" value="0"></td></tr>
311 
312  <tr><td>'.$langs->trans("Priority").' :</td><td>
313  <select name="priority" id="priority" class="flat">
314  <option value="0">high</option>
315  <option value="1">medium</option>
316  <option value="2" selected>low</option>
317  <option value="3">veryLow</option>
318  </select></td></tr>
319 
320  <tr><td>'.$langs->trans("Type").' :</td><td>
321  <select name="class" id="class" class="flat">
322  <option value="0">Flash</option>
323  <option value="1" selected>Standard</option>
324  <option value="2">SIM</option>
325  <option value="3">ToolKit</option>
326  </select></td></tr>
327 
328  <tr><td>'.$langs->trans("DisableStopIfSupported").' :</td><td>
329  <select name="disablestop" id="disablestop" class="flat">
330  <option value="0" selected>No</option>
331  <option value="1" selected>Yes</option>
332  </select></td></tr>';
333 
334  print "</table>\n";
335 
336 
337  if ($showform) {
338  print '<div class="center">';
339  print '<input type="submit" class="button" name="sendmail" value="'.dol_escape_htmltag($langs->trans("SendSms")).'">';
340  if ($this->withcancel) {
341  print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
342  print '<input class="button button-cancel" type="submit" name="cancel" value="'.dol_escape_htmltag($langs->trans("Cancel")).'">';
343  }
344  print '</div>';
345 
346  print "</form>\n";
347  }
348 
349  print "<!-- End form SMS -->\n";
350  }
351 }
Class to manage generation of HTML components Only common components must be here.
Class permettant la generation du formulaire d'envoi de Sms Usage: $formsms = new FormSms($db) $forms...
show_form($morecss='titlefield', $showform=1)
Show the form to input an sms.
__construct($db)
Constructor.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
Definition: user.class.php:50
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
make_substitutions($text, $substitutionarray, $outputlangs=null, $converttextinhtmlifnecessary=0)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.