dolibarr 21.0.0-alpha
html.formadvtargetemailing.class.php
Go to the documentation of this file.
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
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
30{
34 public $db;
35
39 public $error = '';
40
46 public function __construct($db)
47 {
48 global $langs;
49
50 $this->db = $db;
51 }
52
60 public function multiselectProspectionStatus($selected_array = array(), $htmlname = 'cust_prospect_status')
61 {
62 global $conf, $langs;
63 $options_array = array();
64
65 $sql = "SELECT code, label";
66 $sql .= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
67 $sql .= " WHERE active > 0";
68 $sql .= " ORDER BY sortorder";
69
70 $resql = $this->db->query($sql);
71 if ($resql) {
72 $num = $this->db->num_rows($resql);
73 $i = 0;
74 while ($i < $num) {
75 $obj = $this->db->fetch_object($resql);
76
77 $level = $langs->trans($obj->code);
78 if ($level == $obj->code) {
79 $level = $langs->trans($obj->label);
80 }
81 $options_array[$obj->code] = $level;
82
83 $i++;
84 }
85 } else {
86 dol_print_error($this->db);
87 }
88 return $this->advMultiselectarray($htmlname, $options_array, $selected_array);
89 }
90
98 public function multiselectState($htmlname = 'state_id', $selected_array = array())
99 {
100 global $conf, $langs;
101
102 $langs->load("dict");
103 $maxlength = 0;
104
105 $out = '';
106 $stateArray = array();
107 $label = array();
108
109 $options_array = array();
110
111 $sql = "SELECT d.rowid as rowid, d.code_departement as code, d.nom as department, r.nom as region";
112 $sql .= " FROM ".MAIN_DB_PREFIX."c_departements d";
113 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_regions r on d.fk_region=r.code_region";
114 $sql .= " WHERE d.active = 1 AND d.code_departement<>'' AND r.code_region<>''";
115 //$sql .= " ORDER BY r.nom ASC, d.nom ASC";
116
117 $resql = $this->db->query($sql);
118 if ($resql) {
119 $num = $this->db->num_rows($resql);
120 $i = 0;
121 if ($num) {
122 $foundselected = false;
123
124 while ($i < $num) {
125 $obj = $this->db->fetch_object($resql);
126 $stateArray [$i] ['rowid'] = $obj->rowid;
127 $stateArray [$i] ['code'] = $obj->code;
128 $stateArray [$i] ['label'] = $obj->region.'/'.$obj->department;
129 $label[$i] = $stateArray[$i]['label'];
130 $i++;
131 }
132
133 $array1_sort_order = SORT_ASC;
134 array_multisort($label, $array1_sort_order, $stateArray);
135
136 foreach ($stateArray as $row) {
137 $label = dol_trunc($row['label'], $maxlength, 'middle');
138 if ($row['code']) {
139 $label .= ' ('.$row['code'].')';
140 }
141
142 $options_array[$row['rowid']] = $label;
143 }
144 }
145 } else {
146 dol_print_error($this->db);
147 }
148
149 return $this->advMultiselectarray($htmlname, $options_array, $selected_array);
150 }
151
159 public function multiselectCountry($htmlname = 'country_id', $selected_array = array())
160 {
161 global $conf, $langs;
162
163 $langs->load("dict");
164 $maxlength = 0;
165
166 $out = '';
167 $countryArray = array();
168 $label = array();
169
170 $options_array = array();
171
172 $sql = "SELECT rowid, code as code_iso, label";
173 $sql .= " FROM ".MAIN_DB_PREFIX."c_country";
174 $sql .= " WHERE active = 1 AND code<>''";
175 $sql .= " ORDER BY code ASC";
176
177 $resql = $this->db->query($sql);
178 if ($resql) {
179 $num = $this->db->num_rows($resql);
180 $i = 0;
181 if ($num) {
182 $foundselected = false;
183
184 while ($i < $num) {
185 $obj = $this->db->fetch_object($resql);
186 $countryArray [$i] ['rowid'] = $obj->rowid;
187 $countryArray [$i] ['code_iso'] = $obj->code_iso;
188 $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 : ''));
189 $label[$i] = $countryArray[$i]['label'];
190 $i++;
191 }
192
193 $array1_sort_order = SORT_ASC;
194 array_multisort($label, $array1_sort_order, $countryArray);
195
196 foreach ($countryArray as $row) {
197 $label = dol_trunc($row['label'], $maxlength, 'middle');
198 if ($row['code_iso']) {
199 $label .= ' ('.$row['code_iso'].')';
200 }
201
202 $options_array[$row['rowid']] = $label;
203 }
204 }
205 } else {
206 dol_print_error($this->db);
207 }
208
209 return $this->advMultiselectarray($htmlname, $options_array, $selected_array);
210 }
211
220 public function multiselectselectSalesRepresentatives($htmlname, $selected_array, $user)
221 {
222 global $conf;
223
224 $options_array = array();
225
226 $sql_usr = '';
227 $sql_usr .= "SELECT DISTINCT u2.rowid, u2.lastname as name, u2.firstname, u2.login";
228 $sql_usr .= " FROM ".MAIN_DB_PREFIX."user as u2, ".MAIN_DB_PREFIX."societe_commerciaux as sc";
229 $sql_usr .= " WHERE u2.entity IN (0,".$conf->entity.")";
230 $sql_usr .= " AND u2.rowid = sc.fk_user";
231 if (getDolGlobalString('USER_HIDE_INACTIVE_IN_COMBOBOX')) {
232 $sql_usr .= " AND u2.statut <> 0";
233 }
234 if (getDolGlobalString('USER_HIDE_NONEMPLOYEE_IN_COMBOBOX')) {
235 $sql_usr .= " AND u2.employee<>0 ";
236 }
237 if (getDolGlobalString('USER_HIDE_EXTERNAL_IN_COMBOBOX')) {
238 $sql_usr .= " AND u2.fk_soc IS NULL ";
239 }
240 $sql_usr .= " ORDER BY name ASC";
241 // print $sql_usr;exit;
242
243 $resql_usr = $this->db->query($sql_usr);
244 if ($resql_usr) {
245 while ($obj_usr = $this->db->fetch_object($resql_usr)) {
246 $label = $obj_usr->firstname." ".$obj_usr->name." (".$obj_usr->login.')';
247
248 $options_array [$obj_usr->rowid] = $label;
249 }
250 $this->db->free($resql_usr);
251 } else {
252 dol_print_error($this->db);
253 }
254
255 return $this->advMultiselectarray($htmlname, $options_array, $selected_array);
256 }
257
265 public function multiselectselectLanguage($htmlname = '', $selected_array = array())
266 {
267 global $conf, $langs;
268
269 $options_array = array();
270
271 $langs_available = $langs->get_available_languages(DOL_DOCUMENT_ROOT, 12);
272
273 foreach ($langs_available as $key => $value) {
274 $label = $value;
275 $options_array[$key] = $label;
276 }
277 asort($options_array);
278 return $this->advMultiselectarray($htmlname, $options_array, $selected_array);
279 }
280
290 public function advMultiselectarraySelllist($htmlname, $sqlqueryparam = array(), $selected_array = array())
291 {
292 $options_array = array();
293
294 if (is_array($sqlqueryparam)) {
295 $param_list = array_keys($sqlqueryparam);
296 $InfoFieldList = explode(":", $param_list[0], 4);
297
298 // 0 1 : Table name
299 // 1 2 : Name of field that contains the label
300 // 2 3 : Key fields name (if differ of rowid)
301 // 3 4 : Where clause filter on column or table extrafield, syntax field='value' or extra.field=value
302
303 $keyList = 'rowid';
304 if (count($InfoFieldList) >= 3) {
305 if (strpos($InfoFieldList[3], 'extra.') !== false) {
306 $keyList = 'main.'.$InfoFieldList[2].' as rowid';
307 } else {
308 $keyList = $InfoFieldList[2].' as rowid';
309 }
310 }
311
312 $sql = "SELECT ".$this->db->sanitize($keyList).", ".$this->db->sanitize($InfoFieldList[1]);
313 $sql .= " FROM ".$this->db->sanitize(MAIN_DB_PREFIX.$InfoFieldList[0]);
314 if (!empty($InfoFieldList[3])) {
315 $errorstr = '';
316 // We have to join on extrafield table
317 if (strpos($InfoFieldList[3], 'extra') !== false) {
318 $sql .= ' as main, '.$this->db->sanitize(MAIN_DB_PREFIX.$InfoFieldList[0]).'_extrafields as extra';
319 $sql .= " WHERE extra.fk_object = main.".$this->db->sanitize(empty($InfoFieldList[2]) ? 'rowid' : $InfoFieldList[2]);
320 $sql .= " AND ".forgeSQLFromUniversalSearchCriteria($InfoFieldList[3], $errorstr, 1);
321 } else {
322 $sql .= " WHERE ".forgeSQLFromUniversalSearchCriteria($InfoFieldList[3], $errorstr, 1);
323 }
324 }
325 if (!empty($InfoFieldList[1])) {
326 $sql .= " ORDER BY nom";
327 }
328 // $sql.= ' WHERE entity = '.$conf->entity;
329
330 $resql = $this->db->query($sql);
331 if ($resql) {
332 $num = $this->db->num_rows($resql);
333 $i = 0;
334 if ($num) {
335 while ($i < $num) {
336 $obj = $this->db->fetch_object($resql);
337 $fieldtoread = $InfoFieldList[1];
338 $labeltoshow = dol_trunc($obj->$fieldtoread, 90);
339 $options_array[$obj->rowid] = $labeltoshow;
340 $i++;
341 }
342 }
343 $this->db->free($resql);
344 }
345 }
346
347 return $this->advMultiselectarray($htmlname, $options_array, $selected_array);
348 }
349
357 public function multiselectCivility($htmlname = 'civilite_id', $selected_array = array())
358 {
359 global $conf, $langs, $user;
360 $langs->load("dict");
361
362 $options_array = array();
363
364 $sql = "SELECT rowid, code, label as civilite, active FROM ".MAIN_DB_PREFIX."c_civility";
365 $sql .= " WHERE active = 1";
366
367 dol_syslog(__METHOD__, LOG_DEBUG);
368 $resql = $this->db->query($sql);
369 if ($resql) {
370 $num = $this->db->num_rows($resql);
371 $i = 0;
372 if ($num) {
373 while ($i < $num) {
374 $obj = $this->db->fetch_object($resql);
375 // If a translation exists, we use it, else we use the default label
376 $label = ($langs->trans("Civility".$obj->code) != "Civility".$obj->code ? $langs->trans("Civility".$obj->code) : ($obj->civilite != '-' ? $obj->civilite : ''));
377
378 $options_array[$obj->code] = $label;
379
380 $i++;
381 }
382 }
383 } else {
384 dol_print_error($this->db);
385 }
386
387 return $this->advMultiselectarray($htmlname, $options_array, $selected_array);
388 }
389
399 public function advMultiselectarray($htmlname, $options_array = array(), $selected_array = array(), $showempty = 0)
400 {
401 global $conf, $langs;
402
403 $form = new Form($this->db);
404 $return = $form->multiselectarray($htmlname, $options_array, $selected_array, 0, 0, '', 0, 295);
405 return $return;
406 }
407
418 public function selectAdvtargetemailingTemplate($htmlname = 'template_id', $selected = 0, $showempty = 0, $type_element = 'mailing', $morecss = '')
419 {
420 global $conf, $user, $langs;
421
422 $out = '';
423
424 $sql = "SELECT c.rowid, c.name, c.fk_element";
425 $sql .= " FROM ".MAIN_DB_PREFIX."mailing_advtarget as c";
426 $sql .= " WHERE type_element = '".$this->db->escape($type_element)."'";
427 $sql .= " ORDER BY c.name";
428
429 dol_syslog(__METHOD__, LOG_DEBUG);
430 $resql = $this->db->query($sql);
431 if ($resql) {
432 $out .= '<select id="'.$htmlname.'" class="flat'.($morecss ? ' '.$morecss : '').'" name="'.$htmlname.'">';
433 if ($showempty) {
434 $out .= '<option value=""></option>';
435 }
436 $num = $this->db->num_rows($resql);
437 $i = 0;
438 if ($num) {
439 while ($i < $num) {
440 $obj = $this->db->fetch_object($resql);
441 $label = $obj->name;
442 if (empty($label)) {
443 $label = $obj->fk_element;
444 }
445
446 if ($selected > 0 && $selected == $obj->rowid) {
447 $out .= '<option value="'.$obj->rowid.'" selected="selected">'.$label.'</option>';
448 } else {
449 $out .= '<option value="'.$obj->rowid.'">'.$label.'</option>';
450 }
451 $i++;
452 }
453 }
454 $out .= '</select>';
455 } else {
456 dol_print_error($this->db);
457 }
458 $this->db->free($resql);
459 return $out;
460 }
461}
Class to manage building of HTML components.
multiselectState($htmlname='state_id', $selected_array=array())
Return combo list of activated countries, into language of user.
multiselectCivility($htmlname='civilite_id', $selected_array=array())
Return combo list with people title.
advMultiselectarraySelllist($htmlname, $sqlqueryparam=array(), $selected_array=array())
Return multiselect list of entities for extrafield type sellist.
multiselectCountry($htmlname='country_id', $selected_array=array())
Return combo list of activated countries, into language of user.
advMultiselectarray($htmlname, $options_array=array(), $selected_array=array(), $showempty=0)
Return multiselect list of entities.
multiselectselectLanguage($htmlname='', $selected_array=array())
Return select list for categories (to use in form search selectors)
selectAdvtargetemailingTemplate($htmlname='template_id', $selected=0, $showempty=0, $type_element='mailing', $morecss='')
Return a combo list to select emailing target selector.
multiselectselectSalesRepresentatives($htmlname, $selected_array, $user)
Return select list for categories (to use in form search selectors)
multiselectProspectionStatus($selected_array=array(), $htmlname='cust_prospect_status')
Affiche un champs select contenant une liste.
Class to manage generation of HTML components Only common components must be here.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.