html.formadvtargetemailing.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. /* Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
  3. * Copyright (C) 2019 Frédéric France <frederic.france@netlogic.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file comm/mailing/class/html.formadvtargetemailing.class.php
  20. * \ingroup mailing
  21. * \brief Fichier de la classe des fonctions predefinies de composant html advtargetemailing
  22. */
  23. /**
  24. * Class to manage building of HTML components
  25. */
  26. class FormAdvTargetEmailing extends Form
  27. {
  28. /**
  29. * @var DoliDB Database handler.
  30. */
  31. public $db;
  32. /**
  33. * @var string Error code (or message)
  34. */
  35. public $error = '';
  36. /**
  37. * Constructor
  38. *
  39. * @param DoliDB $db handler
  40. */
  41. public function __construct($db)
  42. {
  43. global $langs;
  44. $this->db = $db;
  45. }
  46. /**
  47. * Affiche un champs select contenant une liste
  48. *
  49. * @param array $selected_array à preselectionner
  50. * @param string $htmlname select field
  51. * @return string select field
  52. */
  53. public function multiselectProspectionStatus($selected_array = array(), $htmlname = 'cust_prospect_status')
  54. {
  55. global $conf, $langs;
  56. $options_array = array();
  57. $sql = "SELECT code, label";
  58. $sql .= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
  59. $sql .= " WHERE active > 0";
  60. $sql .= " ORDER BY sortorder";
  61. $resql = $this->db->query($sql);
  62. if ($resql) {
  63. $num = $this->db->num_rows($resql);
  64. $i = 0;
  65. while ($i < $num) {
  66. $obj = $this->db->fetch_object($resql);
  67. $level = $langs->trans($obj->code);
  68. if ($level == $obj->code) {
  69. $level = $langs->trans($obj->label);
  70. }
  71. $options_array[$obj->code] = $level;
  72. $i++;
  73. }
  74. } else {
  75. dol_print_error($this->db);
  76. }
  77. return $this->advMultiselectarray($htmlname, $options_array, $selected_array);
  78. }
  79. /**
  80. * Return combo list of activated countries, into language of user
  81. *
  82. * @param string $htmlname of html select object
  83. * @param array $selected_array or Code or Label of preselected country
  84. * @return string HTML string with select
  85. */
  86. public function multiselectCountry($htmlname = 'country_id', $selected_array = array())
  87. {
  88. global $conf, $langs;
  89. $langs->load("dict");
  90. $maxlength = 0;
  91. $out = '';
  92. $countryArray = array();
  93. $label = array();
  94. $options_array = array();
  95. $sql = "SELECT rowid, code as code_iso, label";
  96. $sql .= " FROM ".MAIN_DB_PREFIX."c_country";
  97. $sql .= " WHERE active = 1 AND code<>''";
  98. $sql .= " ORDER BY code ASC";
  99. $resql = $this->db->query($sql);
  100. if ($resql) {
  101. $num = $this->db->num_rows($resql);
  102. $i = 0;
  103. if ($num) {
  104. $foundselected = false;
  105. while ($i < $num) {
  106. $obj = $this->db->fetch_object($resql);
  107. $countryArray [$i] ['rowid'] = $obj->rowid;
  108. $countryArray [$i] ['code_iso'] = $obj->code_iso;
  109. $countryArray [$i] ['label'] = ($obj->code_iso && $langs->transnoentitiesnoconv("Country".$obj->code_iso) != "Country".$obj->code_iso ? $langs->transnoentitiesnoconv("Country".$obj->code_iso) : ($obj->label != '-' ? $obj->label : ''));
  110. $label[$i] = $countryArray[$i]['label'];
  111. $i++;
  112. }
  113. $array1_sort_order = SORT_ASC;
  114. array_multisort($label, $array1_sort_order, $countryArray);
  115. foreach ($countryArray as $row) {
  116. $label = dol_trunc($row['label'], $maxlength, 'middle');
  117. if ($row['code_iso']) {
  118. $label .= ' ('.$row['code_iso'].')';
  119. }
  120. $options_array[$row['rowid']] = $label;
  121. }
  122. }
  123. } else {
  124. dol_print_error($this->db);
  125. }
  126. return $this->advMultiselectarray($htmlname, $options_array, $selected_array);
  127. }
  128. /**
  129. * Return select list for categories (to use in form search selectors)
  130. *
  131. * @param string $htmlname control name
  132. * @param array $selected_array array of data
  133. * @param User $user User action
  134. * @return string combo list code
  135. */
  136. public function multiselectselectSalesRepresentatives($htmlname, $selected_array, $user)
  137. {
  138. global $conf;
  139. $options_array = array();
  140. $sql_usr = '';
  141. $sql_usr .= "SELECT DISTINCT u2.rowid, u2.lastname as name, u2.firstname, u2.login";
  142. $sql_usr .= " FROM ".MAIN_DB_PREFIX."user as u2, ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  143. $sql_usr .= " WHERE u2.entity IN (0,".$conf->entity.")";
  144. $sql_usr .= " AND u2.rowid = sc.fk_user ";
  145. if (getDolGlobalString('USER_HIDE_INACTIVE_IN_COMBOBOX')) {
  146. $sql_usr .= " AND u2.statut<>0 ";
  147. }
  148. $sql_usr .= " ORDER BY name ASC";
  149. // print $sql_usr;exit;
  150. $resql_usr = $this->db->query($sql_usr);
  151. if ($resql_usr) {
  152. while ($obj_usr = $this->db->fetch_object($resql_usr)) {
  153. $label = $obj_usr->firstname." ".$obj_usr->name." (".$obj_usr->login.')';
  154. $options_array [$obj_usr->rowid] = $label;
  155. }
  156. $this->db->free($resql_usr);
  157. } else {
  158. dol_print_error($this->db);
  159. }
  160. return $this->advMultiselectarray($htmlname, $options_array, $selected_array);
  161. }
  162. /**
  163. * Return select list for categories (to use in form search selectors)
  164. *
  165. * @param string $htmlname of combo list (example: 'search_sale')
  166. * @param array $selected_array selected array
  167. * @return string combo list code
  168. */
  169. public function multiselectselectLanguage($htmlname = '', $selected_array = array())
  170. {
  171. global $conf, $langs;
  172. $options_array = array();
  173. $langs_available = $langs->get_available_languages(DOL_DOCUMENT_ROOT, 12);
  174. foreach ($langs_available as $key => $value) {
  175. $label = $value;
  176. $options_array[$key] = $label;
  177. }
  178. asort($options_array);
  179. return $this->advMultiselectarray($htmlname, $options_array, $selected_array);
  180. }
  181. /**
  182. * Return multiselect list of entities for extrafeild type sellist
  183. *
  184. * @param string $htmlname control name
  185. * @param array $sqlqueryparam array
  186. * @param array $selected_array array
  187. *
  188. * @return string HTML combo
  189. */
  190. public function advMultiselectarraySelllist($htmlname, $sqlqueryparam = array(), $selected_array = array())
  191. {
  192. $options_array = array();
  193. if (is_array($sqlqueryparam)) {
  194. $param_list = array_keys($sqlqueryparam);
  195. $InfoFieldList = explode(":", $param_list [0]);
  196. // 0 1 : tableName
  197. // 1 2 : label field name Name of field that contains the label
  198. // 2 3 : key fields name (if differ of rowid)
  199. // 3 4 : where clause filter on column or table extrafield, syntax field='value' or extra.field=value
  200. $keyList = 'rowid';
  201. if (count($InfoFieldList) >= 3) {
  202. if (strpos($InfoFieldList[3], 'extra.') !== false) {
  203. $keyList = 'main.'.$InfoFieldList[2].' as rowid';
  204. } else {
  205. $keyList = $InfoFieldList[2].' as rowid';
  206. }
  207. }
  208. $sql = "SELECT ".$keyList.", ".$InfoFieldList[1];
  209. $sql .= " FROM ".MAIN_DB_PREFIX.$InfoFieldList[0];
  210. if (!empty($InfoFieldList[3])) {
  211. // We have to join on extrafield table
  212. if (strpos($InfoFieldList[3], 'extra') !== false) {
  213. $sql .= ' as main, '.MAIN_DB_PREFIX.$InfoFieldList[0].'_extrafields as extra';
  214. $sql .= " WHERE extra.fk_object=main.".$InfoFieldList[2]." AND ".$InfoFieldList[3];
  215. } else {
  216. $sql .= " WHERE ".$InfoFieldList[3];
  217. }
  218. }
  219. if (!empty($InfoFieldList[1])) {
  220. $sql .= " ORDER BY nom";
  221. }
  222. // $sql.= ' WHERE entity = '.$conf->entity;
  223. $resql = $this->db->query($sql);
  224. if ($resql) {
  225. $num = $this->db->num_rows($resql);
  226. $i = 0;
  227. if ($num) {
  228. while ($i < $num) {
  229. $obj = $this->db->fetch_object($resql);
  230. $labeltoshow = dol_trunc($obj->$InfoFieldList[1], 90);
  231. $options_array[$obj->rowid] = $labeltoshow;
  232. $i++;
  233. }
  234. }
  235. $this->db->free($resql);
  236. }
  237. }
  238. return $this->advMultiselectarray($htmlname, $options_array, $selected_array);
  239. }
  240. /**
  241. * Return combo list with people title
  242. *
  243. * @param string $htmlname Name of HTML select combo field
  244. * @param array $selected_array Array
  245. * @return string HTML combo
  246. */
  247. public function multiselectCivility($htmlname = 'civilite_id', $selected_array = array())
  248. {
  249. global $conf, $langs, $user;
  250. $langs->load("dict");
  251. $options_array = array();
  252. $sql = "SELECT rowid, code, label as civilite, active FROM ".MAIN_DB_PREFIX."c_civility";
  253. $sql .= " WHERE active = 1";
  254. dol_syslog(__METHOD__, LOG_DEBUG);
  255. $resql = $this->db->query($sql);
  256. if ($resql) {
  257. $num = $this->db->num_rows($resql);
  258. $i = 0;
  259. if ($num) {
  260. while ($i < $num) {
  261. $obj = $this->db->fetch_object($resql);
  262. // If a translation exists, we use it, else we use the default label
  263. $label = ($langs->trans("Civility".$obj->code) != "Civility".$obj->code ? $langs->trans("Civility".$obj->code) : ($obj->civilite != '-' ? $obj->civilite : ''));
  264. $options_array[$obj->code] = $label;
  265. $i++;
  266. }
  267. }
  268. } else {
  269. dol_print_error($this->db);
  270. }
  271. return $this->advMultiselectarray($htmlname, $options_array, $selected_array);
  272. }
  273. /**
  274. * Return multiselect list of entities.
  275. *
  276. * @param string $htmlname select
  277. * @param array $options_array to manage
  278. * @param array $selected_array to manage
  279. * @param int $showempty show empty
  280. * @return string HTML combo
  281. */
  282. public function advMultiselectarray($htmlname, $options_array = array(), $selected_array = array(), $showempty = 0)
  283. {
  284. global $conf, $langs;
  285. $form = new Form($this->db);
  286. $return = $form->multiselectarray($htmlname, $options_array, $selected_array, 0, 0, '', 0, 295);
  287. return $return;
  288. }
  289. /**
  290. * Return a combo list to select emailing target selector
  291. *
  292. * @param string $htmlname control name
  293. * @param integer $selected defaut selected
  294. * @param integer $showempty empty lines
  295. * @param string $type_element Type element. Example: 'mailing'
  296. * @param string $morecss More CSS
  297. * @return string HTML combo
  298. */
  299. public function selectAdvtargetemailingTemplate($htmlname = 'template_id', $selected = 0, $showempty = 0, $type_element = 'mailing', $morecss = '')
  300. {
  301. global $conf, $user, $langs;
  302. $out = '';
  303. $sql = "SELECT c.rowid, c.name, c.fk_element";
  304. $sql .= " FROM ".MAIN_DB_PREFIX."mailing_advtarget as c";
  305. $sql .= " WHERE type_element = '".$this->db->escape($type_element)."'";
  306. $sql .= " ORDER BY c.name";
  307. dol_syslog(__METHOD__, LOG_DEBUG);
  308. $resql = $this->db->query($sql);
  309. if ($resql) {
  310. $out .= '<select id="'.$htmlname.'" class="flat'.($morecss ? ' '.$morecss : '').'" name="'.$htmlname.'">';
  311. if ($showempty) {
  312. $out .= '<option value=""></option>';
  313. }
  314. $num = $this->db->num_rows($resql);
  315. $i = 0;
  316. if ($num) {
  317. while ($i < $num) {
  318. $obj = $this->db->fetch_object($resql);
  319. $label = $obj->name;
  320. if (empty($label)) {
  321. $label = $obj->fk_element;
  322. }
  323. if ($selected > 0 && $selected == $obj->rowid) {
  324. $out .= '<option value="'.$obj->rowid.'" selected="selected">'.$label.'</option>';
  325. } else {
  326. $out .= '<option value="'.$obj->rowid.'">'.$label.'</option>';
  327. }
  328. $i++;
  329. }
  330. }
  331. $out .= '</select>';
  332. } else {
  333. dol_print_error($this->db);
  334. }
  335. $this->db->free($resql);
  336. return $out;
  337. }
  338. }