Completed
Branch develop (0fc56d)
by
unknown
26:25
created

FormAdvTargetEmailing::advMultiselectarray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 4
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) 2014  Florian Henry   <[email protected]>
3
*
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 <http://www.gnu.org/licenses/>.
17
*/
18
19
/**
20
 * \file    comm/mailing/class/html.formadvtragetemaling.class.php
21
 * \ingroup mailing
22
 * \brief   Fichier de la classe des fonctions predefinie de composants html advtargetemaling
23
 */
24
25
/**
26
 * Class to manage building of HTML components
27
 */
28
class FormAdvTargetEmailing extends Form
29
{
30
	var $db;
31
	var $error;
32
33
	/**
34
	 * Constructor
35
	 *
36
	 * @param DoliDB $db handler
37
	 */
38
	function __construct($db) {
39
		global $langs;
40
41
		$this->db = $db;
42
	}
43
44
	/**
45
	 * Affiche un champs select contenant une liste
46
	 *
47
	 * @param array $selected_array à preselectionner
48
	 * @param string $htmlname select field
49
	 * @return string select field
50
	 */
51
	function multiselectProspectionStatus($selected_array = array(), $htmlname = 'cust_prospect_status') {
52
		global $conf, $langs;
53
		$options_array = array ();
54
55
		$sql = "SELECT code, label";
56
		$sql .= " FROM " . MAIN_DB_PREFIX . "c_prospectlevel";
57
		$sql .= " WHERE active > 0";
58
		$sql .= " ORDER BY sortorder";
59
		dol_syslog ( get_class ( $this ) . '::multiselectProspectionStatus sql=' . $sql, LOG_DEBUG );
60
		$resql = $this->db->query ( $sql );
61
		if ($resql) {
62
			$num = $this->db->num_rows ( $resql );
63
			$i = 0;
64
			while ( $i < $num ) {
65
				$obj = $this->db->fetch_object ( $resql );
66
67
				$level = $langs->trans ( $obj->code );
68
				if ($level == $obj->code)
69
					$level = $langs->trans ( $obj->label );
70
				$options_array [$obj->code] = $level;
71
72
				$i ++;
73
			}
74
		} else {
75
			dol_print_error ( $this->db );
76
		}
77
		return $this->advMultiselectarray ( $htmlname, $options_array, $selected_array );
78
	}
79
80
	/**
81
	 * Return combo list of activated countries, into language of user
82
	 *
83
	 * @param string $htmlname of html select object
84
	 * @param array $selected_array or Code or Label of preselected country
85
	 * @return string HTML string with select
86
	 */
87
	function multiselectCountry($htmlname = 'country_id', $selected_array=array()) {
88
		global $conf, $langs;
89
90
		$langs->load("dict");
91
		$maxlength = 0;
92
93
		$out = '';
94
		$countryArray = array();
95
		$label = array ();
96
97
		$options_array = array();
98
99
		$sql = "SELECT rowid, code as code_iso, label";
100
		$sql .= " FROM " . MAIN_DB_PREFIX . "c_country";
101
		$sql .= " WHERE active = 1 AND code<>''";
102
		$sql .= " ORDER BY code ASC";
103
104
		dol_syslog(get_class($this) . "::select_country sql=" . $sql);
105
		$resql = $this->db->query($sql);
106
		if ($resql) {
107
108
			$num = $this->db->num_rows($resql);
109
			$i = 0;
110
			if ($num) {
111
				$foundselected = false;
112
113
				while ($i < $num) {
114
					$obj = $this->db->fetch_object ( $resql );
115
					$countryArray [$i] ['rowid'] = $obj->rowid;
116
					$countryArray [$i] ['code_iso'] = $obj->code_iso;
117
					$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 : ''));
118
					$label[$i] = $countryArray[$i]['label'];
119
					$i ++;
120
				}
121
122
				array_multisort($label, SORT_ASC, $countryArray);
123
124
				foreach ($countryArray as $row) {
125
					$label = dol_trunc($row['label'], $maxlength, 'middle');
126
					if ($row['code_iso'])
127
						$label .= ' (' . $row['code_iso'] . ')';
128
129
					$options_array[$row['rowid']] = $label;
130
				}
131
			}
132
		} else {
133
			dol_print_error($this->db);
134
		}
135
136
		return $this->advMultiselectarray($htmlname, $options_array, $selected_array);
137
	}
138
139
	/**
140
	 * Return select list for categories (to use in form search selectors)
141
	 *
142
	 * @param string $htmlname control name
143
	 * @param array $selected_array array of data
144
	 * @param User $user User action
145
	 * @return string combo list code
146
	 */
147
	function multiselectselectSalesRepresentatives($htmlname, $selected_array, $user) {
148
149
		global $conf;
150
151
		$options_array = array ();
152
153
        $sql_usr  = '';
154
		$sql_usr .= "SELECT DISTINCT u2.rowid, u2.lastname as name, u2.firstname, u2.login";
155
		$sql_usr .= " FROM " . MAIN_DB_PREFIX . "user as u2, " . MAIN_DB_PREFIX . "societe_commerciaux as sc";
156
		$sql_usr .= " WHERE u2.entity IN (0," . $conf->entity . ")";
157
		$sql_usr .= " AND u2.rowid = sc.fk_user ";
158
159
		if (! empty ( $conf->global->USER_HIDE_INACTIVE_IN_COMBOBOX ))
160
			$sql_usr .= " AND u2.statut<>0 ";
161
		$sql_usr .= " ORDER BY name ASC";
162
		// print $sql_usr;exit;
163
164
		$resql_usr = $this->db->query ( $sql_usr );
165
		if ($resql_usr) {
166
			while ( $obj_usr = $this->db->fetch_object ( $resql_usr ) ) {
167
168
				$label = $obj_usr->firstname . " " . $obj_usr->name . " (" . $obj_usr->login . ')';
169
170
				$options_array [$obj_usr->rowid] = $label;
171
172
			}
173
			$this->db->free ( $resql_usr );
174
		} else {
175
			dol_print_error ( $this->db );
176
		}
177
178
		return $this->advMultiselectarray ( $htmlname, $options_array, $selected_array );
179
	}
180
181
	/**
182
	 * Return select list for categories (to use in form search selectors)
183
	 *
184
	 * @param string $htmlname of combo list (example: 'search_sale')
185
	 * @param array $selected_array selected array
186
	 * @return string combo list code
187
	 */
188
	function multiselectselectLanguage($htmlname='', $selected_array=array()) {
189
190
		global $conf,$langs;
191
192
		$options_array = array ();
193
194
		$langs_available=$langs->get_available_languages(DOL_DOCUMENT_ROOT,12);
195
196
		foreach ($langs_available as $key => $value)
197
		{
198
			$label = $value;
199
			$options_array[$key] = $label;
200
		}
201
		asort($options_array);
202
		return $this->advMultiselectarray($htmlname, $options_array, $selected_array);
203
	}
204
205
	/**
206
	 * Return multiselect list of entities for extrafeild type sellist
207
	 *
208
	 * @param string $htmlname control name
209
	 * @param array $sqlqueryparam array
210
	 * @param array $selected_array array
211
	 *
212
	 *  @return	string HTML combo
213
	 */
214
	function advMultiselectarraySelllist($htmlname, $sqlqueryparam = array(), $selected_array = array()) {
215
216
		if (is_array ( $sqlqueryparam )) {
217
			$options_array=array();
218
219
			$param_list = array_keys ( $sqlqueryparam );
220
			$InfoFieldList = explode ( ":", $param_list [0] );
221
222
			// 0 1 : tableName
223
			// 1 2 : label field name Nom du champ contenant le libelle
224
			// 2 3 : key fields name (if differ of rowid)
225
			// 3 4 : where clause filter on column or table extrafield, syntax field='value' or extra.field=value
226
227
			$keyList = 'rowid';
228
229
			if (count ( $InfoFieldList ) >= 3) {
230
				if (strpos ( $InfoFieldList [3], 'extra.' ) !== false) {
231
					$keyList = 'main.' . $InfoFieldList [2] . ' as rowid';
232
				} else {
233
					$keyList = $InfoFieldList [2] . ' as rowid';
234
				}
235
			}
236
237
			$sql = 'SELECT ' . $keyList . ', ' . $InfoFieldList [1];
238
			$sql .= ' FROM ' . MAIN_DB_PREFIX . $InfoFieldList [0];
239
			if (! empty ( $InfoFieldList [3] )) {
240
241
				// We have to join on extrafield table
242
				if (strpos ( $InfoFieldList [3], 'extra' ) !== false) {
243
					$sql .= ' as main, ' . MAIN_DB_PREFIX . $InfoFieldList [0] . '_extrafields as extra';
244
					$sql .= ' WHERE  extra.fk_object=main.' . $InfoFieldList [2] . ' AND ' . $InfoFieldList [3];
245
				} else {
246
					$sql .= ' WHERE ' . $InfoFieldList [3];
247
				}
248
			}
249
			if (! empty($InfoFieldList[1])) {
250
				$sql .= " ORDER BY nom";
251
			}
252
			// $sql.= ' WHERE entity = '.$conf->entity;
253
254
			$options_array = array();
255
256
			dol_syslog(get_class($this) . "::".__METHOD__,LOG_DEBUG);
257
			$resql = $this->db->query($sql);
258
			if ($resql) {
259
260
				$num = $this->db->num_rows($resql);
261
				$i = 0;
262
				if ($num) {
263
					while ( $i < $num ) {
264
						$obj = $this->db->fetch_object ( $resql );
265
						$labeltoshow = dol_trunc ( $obj->$InfoFieldList [1], 90 );
266
						$options_array[$obj->rowid]=$labeltoshow;
267
						$i ++;
268
					}
269
				}
270
				$this->db->free ( $resql );
271
			}
272
		}
273
274
		return $this->advMultiselectarray($htmlname, $options_array, $selected_array);
0 ignored issues
show
Bug introduced by
The variable $options_array does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
275
	}
276
277
	/**
278
	 *  Return combo list with people title
279
	 *
280
	 * 	@param	string $htmlname	       Name of HTML select combo field
281
	 *  @param  array  $selected_array     Array
282
	 *  @return	string                     HTML combo
283
	 */
284
	function multiselectCivility($htmlname='civilite_id',$selected_array = array())
285
	{
286
		global $conf,$langs,$user;
287
		$langs->load("dict");
288
289
		$options_array=array();
290
291
292
		$sql = "SELECT rowid, code, label as civilite, active FROM ".MAIN_DB_PREFIX."c_civility";
293
		$sql.= " WHERE active = 1";
294
295
		dol_syslog(get_class($this)."::".__METHOD__,LOG_DEBUG);
296
		$resql=$this->db->query($sql);
297
		if ($resql)
298
		{
299
300
			$num = $this->db->num_rows($resql);
301
			$i = 0;
302
			if ($num)
303
			{
304
				while ($i < $num)
305
				{
306
					$obj = $this->db->fetch_object($resql);
307
					// Si traduction existe, on l'utilise, sinon on prend le libelle par defaut
308
					$label= ($langs->trans("Civility".$obj->code)!="Civility".$obj->code ? $langs->trans("Civility".$obj->code) : ($obj->civilite!='-'?$obj->civilite:''));
309
310
311
					$options_array[$obj->code]=$label;
312
313
					$i++;
314
				}
315
			}
316
317
		}
318
		else
319
		{
320
			dol_print_error($this->db);
321
		}
322
323
		return $this->advMultiselectarray ( $htmlname, $options_array, $selected_array );
324
	}
325
326
	/**
327
	 * Return multiselect list of entities.
328
	 *
329
	 * @param string $htmlname select
330
	 * @param array $options_array to manage
331
	 * @param array $selected_array to manage
332
	 * @param int $showempty show empty
333
	 * @return string HTML combo
334
	 */
335
	function advMultiselectarray($htmlname, $options_array = array(), $selected_array = array(), $showempty = 0) {
336
		global $conf, $langs;
337
338
		$form=new Form($this->db);
339
		$return = $form->multiselectarray($htmlname, $options_array, $selected_array,0,0,'',0,295);
340
		return $return;
341
	}
342
343
	/**
344
	 *  Return combo list with customer categories
345
	 *
346
	 *  @param  string	$htmlname   Name of categorie
347
	 * 	@param	array	$selected_array	value selected
348
	 *  @return	string HTML combo
349
	 */
350
	function multiselectCustomerCategories($htmlname='cust_cat',$selected_array = array())
351
	{
352
		return $this->multiselectCategories($htmlname,$selected_array,2);
353
	}
354
355
	/**
356
	 *  Return combo list with customer contact
357
	 *
358
	 *  @param  string	$htmlname   Name of categorie
359
	 * 	@param	array	$selected_array	value selected
360
	 *  @return	string HTML combo
361
	 */
362
	function multiselectContactCategories($htmlname='contact_cat',$selected_array = array())
363
	{
364
		return $this->multiselectCategories($htmlname,$selected_array,4);
365
	}
366
367
	/**
368
	 *  Return combo list of categories
369
	 *
370
	 *  @param  string	$htmlname   Name of categorie
371
	 * 	@param	array	$selected_array	value selected
372
	 * 	@param	int	$type	type
373
	 *  @return	string HTML combo
374
	 */
375
	public function multiselectCategories($htmlname='',$selected_array = array(), $type=0)
376
	{
377
		global $conf,$langs,$user;
378
		$langs->load("dict");
379
380
		$options_array=array();
381
382
		$sql = "SELECT rowid, label FROM ".MAIN_DB_PREFIX."categorie";
383
		$sql.= " WHERE type=".$type;
384
385
		dol_syslog(get_class($this)."::".__METHOD__,LOG_DEBUG);
386
		$resql=$this->db->query($sql);
387
		if ($resql)
388
		{
389
390
			$num = $this->db->num_rows($resql);
391
			$i = 0;
392
			if ($num)
393
			{
394
				while ($i < $num)
395
				{
396
					$obj = $this->db->fetch_object($resql);
397
398
					$options_array[$obj->rowid]=$obj->label;
399
400
					$i++;
401
				}
402
			}
403
404
		}
405
		else
406
		{
407
			dol_print_error($this->db);
408
		}
409
410
		return $this->advMultiselectarray( $htmlname, $options_array, $selected_array );
411
	}
412
413
	/**
414
	 * selectAdvtargetemailingTemplate
415
	 *
416
	 * @param string $htmlname control name
417
	 * @param integer $selected  defaut selected
418
	 * @param integer $showempty empty lines
419
	 *
420
	 * @return	string HTML combo
421
	 */
422
	public function selectAdvtargetemailingTemplate($htmlname='template_id',$selected=0,$showempty=0) {
423
		global $conf, $user, $langs;
424
425
		$out = '';
426
427
		$sql = "SELECT c.rowid, c.name, c.fk_mailing";
428
		$sql .= " FROM " . MAIN_DB_PREFIX . "advtargetemailing as c";
429
		$sql .= " ORDER BY c.name";
430
431
		dol_syslog ( get_class ( $this ) . "::".__METHOD__, LOG_DEBUG );
432
		$resql = $this->db->query ( $sql );
433
		if ($resql) {
434
435
436
			$out .= '<select id="' . $htmlname . '" class="flat" name="' . $htmlname . '">';
437
			if ($showempty)
438
				$out .= '<option value=""></option>';
439
			$num = $this->db->num_rows ( $resql );
440
			$i = 0;
441
			if ($num) {
442
				while ( $i < $num ) {
443
					$obj = $this->db->fetch_object ( $resql );
444
					$label = $obj->name;
445
					if (empty($label)) {
446
						$label=$obj->fk_mailing;
447
					}
448
449
					if ($selected > 0 && $selected == $obj->rowid) {
450
						$out .= '<option value="' . $obj->rowid . '" selected="selected">' . $label . '</option>';
451
					} else {
452
						$out .= '<option value="' . $obj->rowid . '">' . $label . '</option>';
453
					}
454
					$i ++;
455
				}
456
			}
457
			$out .= '</select>';
458
		} else {
459
			dol_print_error ( $this->db );
460
		}
461
		$this->db->free ( $resql );
462
		return $out;
463
	}
464
}