html.formsms.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 Frédéric France <frederic.france@netlogic.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 <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/core/class/html.formsms.class.php
  21. * \ingroup core
  22. * \brief Fichier de la classe permettant la generation du formulaire html d'envoi de mail unitaire
  23. */
  24. require_once DOL_DOCUMENT_ROOT .'/core/class/html.form.class.php';
  25. /**
  26. * Classe permettant la generation du formulaire d'envoi de Sms
  27. * Usage: $formsms = new FormSms($db)
  28. * $formsms->proprietes=1 ou chaine ou tableau de valeurs
  29. * $formsms->show_form() affiche le formulaire
  30. */
  31. class FormSms
  32. {
  33. /**
  34. * @var DoliDB Database handler.
  35. */
  36. public $db;
  37. public $fromname;
  38. public $fromsms;
  39. public $replytoname;
  40. public $replytomail;
  41. public $toname;
  42. public $tomail;
  43. public $withsubstit; // Show substitution array
  44. public $withfrom;
  45. public $withto;
  46. public $withtopic;
  47. public $withbody;
  48. public $withfromreadonly;
  49. public $withreplytoreadonly;
  50. public $withtoreadonly;
  51. public $withtopicreadonly;
  52. public $withcancel;
  53. public $substit=array();
  54. public $param=array();
  55. /**
  56. * @var string Error code (or message)
  57. */
  58. public $error='';
  59. /**
  60. * @var string[] Array of error strings
  61. */
  62. public $errors=array();
  63. /**
  64. * Constructor
  65. *
  66. * @param DoliDB $db Database handler
  67. */
  68. function __construct($db)
  69. {
  70. $this->db = $db;
  71. $this->withfrom=1;
  72. $this->withto=1;
  73. $this->withtopic=1;
  74. $this->withbody=1;
  75. $this->withfromreadonly=1;
  76. $this->withreplytoreadonly=1;
  77. $this->withtoreadonly=0;
  78. $this->withtopicreadonly=0;
  79. $this->withbodyreadonly=0;
  80. }
  81. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  82. /**
  83. * Show the form to input an sms.
  84. *
  85. * @param string $morecss Class on first column td
  86. * @param int $showform Show form tags and submit button (recommanded is to use with value 0)
  87. * @return void
  88. */
  89. function show_form($morecss='titlefield', $showform=1)
  90. {
  91. // phpcs:enable
  92. global $conf, $langs, $user, $form;
  93. if (! is_object($form)) $form=new Form($this->db);
  94. // Load translation files required by the page
  95. $langs->loadLangs(array('other', 'mails', 'sms'));
  96. $soc=new Societe($this->db);
  97. if (!empty($this->withtosocid) && $this->withtosocid > 0)
  98. {
  99. $soc->fetch($this->withtosocid);
  100. }
  101. print "\n<!-- Begin form SMS -->\n";
  102. print '
  103. <script language="javascript">
  104. function limitChars(textarea, limit, infodiv)
  105. {
  106. var text = textarea.value;
  107. var textlength = text.length;
  108. var info = document.getElementById(infodiv);
  109. info.innerHTML = (limit - textlength);
  110. return true;
  111. }
  112. </script>';
  113. if ($showform) print "<form method=\"POST\" name=\"smsform\" enctype=\"multipart/form-data\" action=\"".$this->param["returnurl"]."\">\n";
  114. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  115. foreach ($this->param as $key=>$value)
  116. {
  117. print "<input type=\"hidden\" name=\"$key\" value=\"$value\">\n";
  118. }
  119. print "<table class=\"border centpercent\">\n";
  120. // Substitution array
  121. if (! empty($this->withsubstit)) // Unset or set ->withsubstit=0 to disable this.
  122. {
  123. print "<tr><td colspan=\"2\">";
  124. $help="";
  125. foreach($this->substit as $key => $val)
  126. {
  127. $help.=$key.' -> '.$langs->trans($val).'<br>';
  128. }
  129. print $form->textwithpicto($langs->trans("SmsTestSubstitutionReplacedByGenericValues"),$help);
  130. print "</td></tr>\n";
  131. }
  132. // From
  133. if ($this->withfrom)
  134. {
  135. if ($this->withfromreadonly)
  136. {
  137. print '<tr><td class="titlefield '.$morecss.'">'.$langs->trans("SmsFrom");
  138. print '<input type="hidden" name="fromsms" value="'.$this->fromsms.'">';
  139. print "</td><td>";
  140. if ($this->fromtype == 'user')
  141. {
  142. $langs->load("users");
  143. $fuser=new User($this->db);
  144. $fuser->fetch($this->fromid);
  145. print $fuser->getNomUrl(1);
  146. print ' &nbsp; ';
  147. }
  148. if ($this->fromsms)
  149. {
  150. print $this->fromsms;
  151. }
  152. else
  153. {
  154. if ($this->fromtype)
  155. {
  156. $langs->load("errors");
  157. print '<font class="warning"> &lt;'.$langs->trans("ErrorNoPhoneDefinedForThisUser").'&gt; </font>';
  158. }
  159. }
  160. print "</td></tr>\n";
  161. print "</td></tr>\n";
  162. }
  163. else
  164. {
  165. print '<tr><td class="'.$morecss.'">'.$langs->trans("SmsFrom")."</td><td>";
  166. //print '<input type="text" name="fromname" size="30" value="'.$this->fromsms.'">';
  167. if ($conf->global->MAIN_SMS_SENDMODE == 'ovh') // For backward compatibility @deprecated
  168. {
  169. dol_include_once('/ovh/class/ovhsms.class.php');
  170. try
  171. {
  172. $sms = new OvhSms($this->db);
  173. if (empty($conf->global->OVHSMS_ACCOUNT))
  174. {
  175. $resultsender = 'ErrorOVHSMS_ACCOUNT not defined';
  176. }
  177. else
  178. {
  179. $resultsender = $sms->SmsSenderList();
  180. }
  181. }
  182. catch(Exception $e)
  183. {
  184. dol_print_error('','Error to get list of senders: '.$e->getMessage());
  185. }
  186. }
  187. else if (!empty($conf->global->MAIN_SMS_SENDMODE)) // $conf->global->MAIN_SMS_SENDMODE looks like a value 'class@module'
  188. {
  189. $tmp=explode('@',$conf->global->MAIN_SMS_SENDMODE);
  190. $classfile=$tmp[0]; $module=(empty($tmp[1])?$tmp[0]:$tmp[1]);
  191. dol_include_once('/'.$module.'/class/'.$classfile.'.class.php');
  192. try
  193. {
  194. $classname=ucfirst($classfile);
  195. if (class_exists($classname))
  196. {
  197. $sms = new $classname($this->db);
  198. $resultsender = $sms->SmsSenderList();
  199. }
  200. else
  201. {
  202. $sms = new stdClass();
  203. $sms->error='The SMS manager "'.$classfile.'" defined into SMS setup MAIN_SMS_SENDMODE is not found';
  204. }
  205. }
  206. catch(Exception $e)
  207. {
  208. dol_print_error('','Error to get list of senders: '.$e->getMessage());
  209. exit;
  210. }
  211. }
  212. else
  213. {
  214. dol_syslog("Warning: The SMS sending method has not been defined into MAIN_SMS_SENDMODE", LOG_WARNING);
  215. $resultsender=array();
  216. $resultsender[0]->number=$this->fromsms;
  217. }
  218. if (is_array($resultsender) && count($resultsender) > 0)
  219. {
  220. print '<select name="fromsms" id="fromsms" class="flat">';
  221. foreach($resultsender as $obj)
  222. {
  223. print '<option value="'.$obj->number.'">'.$obj->number.'</option>';
  224. }
  225. print '</select>';
  226. }
  227. else
  228. {
  229. print '<span class="error">'.$langs->trans("SmsNoPossibleSenderFound");
  230. if (is_object($sms) && ! empty($sms->error)) print ' '.$sms->error;
  231. print '</span>';
  232. }
  233. print '</td>';
  234. print "</tr>\n";
  235. }
  236. }
  237. // To (target)
  238. if ($this->withto || is_array($this->withto))
  239. {
  240. print '<tr><td>';
  241. //$moretext=$langs->trans("YouCanUseCommaSeparatorForSeveralRecipients");
  242. $moretext='';
  243. print $form->textwithpicto($langs->trans("SmsTo"),$moretext);
  244. print '</td><td>';
  245. if ($this->withtoreadonly)
  246. {
  247. print (! is_array($this->withto) && ! is_numeric($this->withto))?$this->withto:"";
  248. }
  249. else
  250. {
  251. print "<input size=\"16\" id=\"sendto\" name=\"sendto\" value=\"".dol_escape_htmltag(! is_array($this->withto) && $this->withto != '1'? (isset($_REQUEST["sendto"])?GETPOST("sendto"):$this->withto):"+")."\">";
  252. if (! empty($this->withtosocid) && $this->withtosocid > 0)
  253. {
  254. $liste=array();
  255. foreach ($soc->thirdparty_and_contact_phone_array() as $key => $value)
  256. {
  257. $liste[$key]=$value;
  258. }
  259. print " ".$langs->trans("or")." ";
  260. //var_dump($_REQUEST);exit;
  261. print $form->selectarray("receiver", $liste, GETPOST("receiver"), 1);
  262. }
  263. print ' '.$langs->trans("SmsInfoNumero");
  264. }
  265. print "</td></tr>\n";
  266. }
  267. // Message
  268. if ($this->withbody)
  269. {
  270. $defaultmessage='';
  271. if ($this->param["models"]=='body')
  272. {
  273. $defaultmessage=$this->withbody;
  274. }
  275. $defaultmessage=make_substitutions($defaultmessage,$this->substit);
  276. if (isset($_POST["message"])) $defaultmessage=$_POST["message"];
  277. $defaultmessage=str_replace('\n',"\n",$defaultmessage);
  278. print "<tr>";
  279. print '<td class="tdtop">'.$langs->trans("SmsText")."</td>";
  280. print "<td>";
  281. if ($this->withbodyreadonly)
  282. {
  283. print nl2br($defaultmessage);
  284. print '<input type="hidden" name="message" value="'.dol_escape_htmltag($defaultmessage).'">';
  285. }
  286. else
  287. {
  288. print '<textarea class="quatrevingtpercent" name="message" id="message" rows="'.ROWS_4.'" onkeyup="limitChars(this, 160, \'charlimitinfospan\')">'.$defaultmessage.'</textarea>';
  289. print '<div id="charlimitinfo">'.$langs->trans("SmsInfoCharRemain").': <span id="charlimitinfospan">'.(160-dol_strlen($defaultmessage)).'</span></div></td>';
  290. }
  291. print "</td></tr>\n";
  292. }
  293. print '
  294. <tr>
  295. <td>'.$langs->trans("DelayBeforeSending").':</td>
  296. <td> <input name="deferred" id="deferred" size="4" value="0"></td></tr>
  297. <tr><td>'.$langs->trans("Priority").' :</td><td>
  298. <select name="priority" id="priority" class="flat">
  299. <option value="0">high</option>
  300. <option value="1">medium</option>
  301. <option value="2" selected>low</option>
  302. <option value="3">veryLow</option>
  303. </select></td></tr>
  304. <tr><td>'.$langs->trans("Type").' :</td><td>
  305. <select name="class" id="class" class="flat">
  306. <option value="0">Flash</option>
  307. <option value="1" selected>Standard</option>
  308. <option value="2">SIM</option>
  309. <option value="3">ToolKit</option>
  310. </select></td></tr>
  311. <tr><td>'.$langs->trans("DisableStopIfSupported").' :</td><td>
  312. <select name="disablestop" id="disablestop" class="flat">
  313. <option value="0" selected>No</option>
  314. <option value="1" selected>Yes</option>
  315. </select></td></tr>';
  316. print "</table>\n";
  317. if ($showform)
  318. {
  319. print '<div class="center">';
  320. print '<input class="button" type="submit" name="sendmail" value="'.dol_escape_htmltag($langs->trans("SendSms")).'">';
  321. if ($this->withcancel)
  322. {
  323. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  324. print '<input class="button" type="submit" name="cancel" value="'.dol_escape_htmltag($langs->trans("Cancel")).'">';
  325. }
  326. print '</div>';
  327. print "</form>\n";
  328. }
  329. print "<!-- End form SMS -->\n";
  330. }
  331. }