浏览代码

Merge pull request #5027 from marcosgdf/refactor-option

Refactored Form classes to use Form::selectarray instead of HTML code
Laurent Destailleur 9 年之前
父节点
当前提交
30f7d4cd52

+ 122 - 222
htdocs/accountancy/class/html.formventilation.class.php

@@ -3,6 +3,7 @@
  * Copyright (C) 2013-2014 Olivier Geffroy      <jeff@jeffinfo.com>
  * Copyright (C) 2013-2016 Alexandre Spangaro   <aspangaro.dolibarr@gmail.com>
  * Copyright (C) 2015      Ari Elbaz (elarifr)  <github@accedinfo.com>
+ * Copyright (C) 2016      Marcos García        <marcosgdf@gmail.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -29,18 +30,6 @@
  */
 class FormVentilation extends Form
 {
-	var $db;
-	var $error;
-	
-	/**
-	 * Constructor
-	 *
-	 * @param DoliDB $db Database handler
-	 */
-	public function __construct($db) {
-		$this->db = $db;
-	}
-	
 	/**
 	 * Return select filter with date of transaction
 	 *
@@ -49,38 +38,25 @@ class FormVentilation extends Form
 	 * @return string HTML edit field
 	 */
 	function select_bookkeeping_importkey($htmlname = 'importkey', $selectedkey = '') {
+		$options = array();
+
 		$sql = 'SELECT DISTINCT import_key from ' . MAIN_DB_PREFIX . 'accounting_bookkeeping';
 		$sql .= ' ORDER BY import_key DESC';
-		
-		$out = '<SELECT name="' . $htmlname . '">';
-		
-		dol_syslog(get_class($this) . "::select_bookkeeping_importkey sql=" . $sql, LOG_DEBUG);
+
+		dol_syslog(get_class($this) . "::select_bookkeeping_importkey", LOG_DEBUG);
 		$resql = $this->db->query($sql);
-		if ($resql) {
-			$i = 0;
-			$num = $this->db->num_rows($resql);
-			
-			while ( $i < $num ) {
-				$obj = $this->db->fetch_object($resql);
-				
-				$selected = '';
-				if ($selectedkey == $obj->import_key) {
-					$selected = ' selected ';
-				}
-				
-				$out .= '<OPTION value="' . $obj->import_key . '"' . $selected . '>' . dol_print_date($obj->import_key, 'dayhourtext') . '</OPTION>';
-				
-				$i ++;
-			}
-		} else {
+
+		if (!$resql) {
 			$this->error = "Error " . $this->db->lasterror();
 			dol_syslog(get_class($this) . "::select_bookkeeping_importkey " . $this->error, LOG_ERR);
 			return - 1;
 		}
-		
-		$out .= '</SELECT>';
-		
-		return $out;
+
+		while ($obj = $this->db->fetch_object($resql)) {
+			$options[$obj->import_key] = dol_print_date($obj->import_key, 'dayhourtext');
+		}
+
+		return Form::selectarray($htmlname, $options, $selectedkey);
 	}
 	
 	/**
@@ -100,8 +76,8 @@ class FormVentilation extends Form
 		global $conf;
 		
 		require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
-		
-		$out = '';
+
+		$trunclength = defined('ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT') ? $conf->global->ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT : 50;
 		
 		$sql = "SELECT DISTINCT aa.account_number, aa.label, aa.rowid, aa.fk_pcg_version";
 		$sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa";
@@ -110,49 +86,44 @@ class FormVentilation extends Form
 		$sql .= " AND aa.active = 1";
 		$sql .= " ORDER BY aa.account_number";
 		
-		dol_syslog(get_class($this) . "::select_account sql=" . $sql, LOG_DEBUG);
+		dol_syslog(get_class($this) . "::select_account", LOG_DEBUG);
 		$resql = $this->db->query($sql);
-		if ($resql) {
-			
-			$out .= ajax_combobox($htmlname, $event);
-			$out .= '<select id="' . $htmlname . '" class="flat" name="' . $htmlname . '">';
-			if ($showempty)
-				$out .= '<option value="-1"></option>';
-			$num = $this->db->num_rows($resql);
-			$trunclength = defined('ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT') ? $conf->global->ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT : '50';
-			
-			$i = 0;
-			if ($num) {
-				while ( $i < $num ) {
-					$obj = $this->db->fetch_object($resql);
-					$label = length_accountg($obj->account_number) . ' - ' . $obj->label;
-					$label = dol_trunc($label, $trunclength);
-					if ($select_in == 0)
-						$select_value_in = $obj->rowid;
-					if ($select_in == 1)
-						$select_value_in = $obj->account_number;
-					if ($select_out == 0)
-						$select_value_out = $obj->rowid;
-					if ($select_out == 1)
-						$select_value_out = $obj->account_number;
-						// Remember guy's we store in database llx_facturedet the rowid of accounting_account and not the account_number
-						// Because same account_number can be share between different accounting_system and do have the same meaning
-					if (($selectid != '') && $selectid == $select_value_in) {
-						// $out .= '<option value="' . $obj->account_number . '" selected>' . $label . '</option>';
-						$out .= '<option value="' . $select_value_out . '" selected>' . $label . '</option>';
-					} else {
-						// $out .= '<option value="' . $obj->account_number . '">' . $label . '</option>';
-						$out .= '<option value="' . $select_value_out . '">' . $label . '</option>';
-					}
-					$i ++;
-				}
-			}
-			$out .= '</select>';
-		} else {
+
+		if (!$resql) {
 			$this->error = "Error " . $this->db->lasterror();
 			dol_syslog(get_class($this) . "::select_account " . $this->error, LOG_ERR);
-			return - 1;
+			return -1;
+		}
+
+		$out = ajax_combobox($htmlname, $event);
+
+		$options = array();
+		$selected = null;
+
+		while ($obj = $this->db->fetch_object($resql)) {
+			$label = length_accountg($obj->account_number) . ' - ' . $obj->label;
+			$label = dol_trunc($label, $trunclength);
+
+			$select_value_in = $obj->rowid;
+			$select_value_out = $obj->rowid;
+
+			if ($select_in == 1) {
+				$select_value_in = $obj->account_number;
+			}
+			if ($select_out == 1) {
+				$select_value_out = $obj->account_number;
+			}
+
+			// Remember guy's we store in database llx_facturedet the rowid of accounting_account and not the account_number
+			// Because same account_number can be share between different accounting_system and do have the same meaning
+			if (($selectid != '') && $selectid == $select_value_in) {
+				$selected = $select_value_out;
+			}
+
+			$options[$select_value_out] = $label;
 		}
+
+		$out .= Form::selectarray($htmlname, $options, $selected, $showempty);
 		$this->db->free($resql);
 		return $out;
 	}
@@ -170,44 +141,30 @@ class FormVentilation extends Form
 	function select_pcgtype($selectid, $htmlname = 'pcg_type', $showempty = 0, $event = array()) {
 		global $conf;
 		
-		$out = '';
-		
 		$sql = "SELECT DISTINCT pcg_type ";
 		$sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa";
 		$sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
 		$sql .= " AND asy.rowid = " . $conf->global->CHARTOFACCOUNTS;
 		$sql .= " ORDER BY pcg_type";
 		
-		dol_syslog(get_class($this) . "::select_pcgtype sql=" . $sql, LOG_DEBUG);
+		dol_syslog(get_class($this) . "::select_pcgtype", LOG_DEBUG);
 		$resql = $this->db->query($sql);
-		if ($resql) {
-			
-			$out .= ajax_combobox($htmlname, $event);
-			
-			$out .= '<select id="' . $htmlname . '" class="flat" name="' . $htmlname . '">';
-			if ($showempty)
-				$out .= '<option value="-1"></option>';
-			$num = $this->db->num_rows($resql);
-			$i = 0;
-			if ($num) {
-				while ( $i < $num ) {
-					$obj = $this->db->fetch_object($resql);
-					$label = $obj->pcg_type;
-					
-					if (($selectid != '') && $selectid == $obj->pcg_type) {
-						$out .= '<option value="' . $obj->pcg_type . '" selected>' . $label . '</option>';
-					} else {
-						$out .= '<option value="' . $obj->pcg_type . '">' . $label . '</option>';
-					}
-					$i ++;
-				}
-			}
-			$out .= '</select>';
-		} else {
-			$this->error = "Error " . $this->db->lasterror();
-			dol_syslog(get_class($this) . "::select_pcgtype " . $this->error, LOG_ERR);
-			return - 1;
+
+		if (!$resql) {
+			$this->error = "Error ".$this->db->lasterror();
+			dol_syslog(get_class($this)."::select_pcgtype ".$this->error, LOG_ERR);
+			return -1;
+		}
+
+		$options = array();
+		$out = ajax_combobox($htmlname, $event);
+
+		while ($obj = $this->db->fetch_object($resql)) {
+			$options[$obj->pcg_type] = $obj->pcg_type;
 		}
+
+		$out .= Form::selectarray($htmlname, $options, $selectid, $showempty);
+
 		$this->db->free($resql);
 		return $out;
 	}
@@ -225,44 +182,30 @@ class FormVentilation extends Form
 	function select_pcgsubtype($selectid, $htmlname = 'pcg_subtype', $showempty = 0, $event = array()) {
 		global $conf;
 		
-		$out = '';
-		
 		$sql = "SELECT DISTINCT pcg_subtype ";
 		$sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa";
 		$sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
 		$sql .= " AND asy.rowid = " . $conf->global->CHARTOFACCOUNTS;
 		$sql .= " ORDER BY pcg_subtype";
 		
-		dol_syslog(get_class($this) . "::select_pcgsubtype sql=" . $sql, LOG_DEBUG);
+		dol_syslog(get_class($this) . "::select_pcgsubtype", LOG_DEBUG);
 		$resql = $this->db->query($sql);
-		if ($resql) {
-			
-			$out .= ajax_combobox($htmlname, $event);
-			
-			$out .= '<select id="' . $htmlname . '" class="flat" name="' . $htmlname . '">';
-			if ($showempty)
-				$out .= '<option value="-1"></option>';
-			$num = $this->db->num_rows($resql);
-			$i = 0;
-			if ($num) {
-				while ( $i < $num ) {
-					$obj = $this->db->fetch_object($resql);
-					$label = $obj->pcg_subtype;
-					
-					if (($selectid != '') && $selectid == $obj->pcg_subtype) {
-						$out .= '<option value="' . $obj->pcg_subtype . '" selected>' . $label . '</option>';
-					} else {
-						$out .= '<option value="' . $obj->pcg_subtype . '">' . $label . '</option>';
-					}
-					$i ++;
-				}
-			}
-			$out .= '</select>';
-		} else {
-			$this->error = "Error " . $this->db->lasterror();
-			dol_syslog(get_class($this) . "::select_pcgsubtype " . $this->error, LOG_ERR);
-			return - 1;
+
+		if (!$resql) {
+			$this->error = "Error ".$this->db->lasterror();
+			dol_syslog(get_class($this)."::select_pcgsubtype ".$this->error, LOG_ERR);
+			return -1;
+		}
+
+		$options = array();
+		$out = ajax_combobox($htmlname, $event);
+
+		while ($obj = $this->db->fetch_object($resql)) {
+			$options[$obj->pcg_subtype] = $obj->pcg_subtype;
 		}
+
+		$out .= Form::selectarray($htmlname, $options, $selectid, $showempty);
+
 		$this->db->free($resql);
 		return $out;
 	}
@@ -278,68 +221,51 @@ class FormVentilation extends Form
 	 * @return string String with HTML select
 	 */
 	function select_auxaccount($selectid, $htmlname = 'account_num_aux', $showempty = 0, $event = array()) {
-		global $conf;
-		
-		$out = '';
-		
-		$aux_account = array ();
-		
+
+		$aux_account = array();
+
 		// Auxiliary customer account
 		$sql = "SELECT DISTINCT code_compta, nom ";
-		$sql .= " FROM " . MAIN_DB_PREFIX . "societe";
+		$sql .= " FROM ".MAIN_DB_PREFIX."societe";
 		$sql .= " ORDER BY code_compta";
-		dol_syslog(get_class($this) . "::select_auxaccount", LOG_DEBUG);
+		dol_syslog(get_class($this)."::select_auxaccount", LOG_DEBUG);
 		$resql = $this->db->query($sql);
 		if ($resql) {
-			while ( $obj = $this->db->fetch_object($resql) ) {
-				if (! empty($obj->code_compta)) {
-					$aux_account[$obj->code_compta] = $obj->code_compta . ' (' . $obj->nom . ')';
+			while ($obj = $this->db->fetch_object($resql)) {
+				if (!empty($obj->code_compta)) {
+					$aux_account[$obj->code_compta] = $obj->code_compta.' ('.$obj->nom.')';
 				}
 			}
 		} else {
-			$this->error = "Error " . $this->db->lasterror();
-			dol_syslog(get_class($this) . "::select_pcgsubtype " . $this->error, LOG_ERR);
-			return - 1;
+			$this->error = "Error ".$this->db->lasterror();
+			dol_syslog(get_class($this)."::select_pcgsubtype ".$this->error, LOG_ERR);
+			return -1;
 		}
 		$this->db->free($resql);
-		
+
 		// Auxiliary supplier account
 		$sql = "SELECT DISTINCT code_compta_fournisseur, nom ";
-		$sql .= " FROM " . MAIN_DB_PREFIX . "societe";
+		$sql .= " FROM ".MAIN_DB_PREFIX."societe";
 		$sql .= " ORDER BY code_compta";
-		dol_syslog(get_class($this) . "::select_auxaccount", LOG_DEBUG);
+		dol_syslog(get_class($this)."::select_auxaccount", LOG_DEBUG);
 		$resql = $this->db->query($sql);
 		if ($resql) {
-			while ( $obj = $this->db->fetch_object($resql) ) {
-				if (! empty($obj->code_compta_fournisseur)) {
-					$aux_account[$obj->code_compta_fournisseur] = $obj->code_compta_fournisseur . ' (' . $obj->nom . ')';
+			while ($obj = $this->db->fetch_object($resql)) {
+				if (!empty($obj->code_compta_fournisseur)) {
+					$aux_account[$obj->code_compta_fournisseur] = $obj->code_compta_fournisseur.' ('.$obj->nom.')';
 				}
 			}
 		} else {
-			$this->error = "Error " . $this->db->lasterror();
-			dol_syslog(get_class($this) . "::select_pcgsubtype " . $this->error, LOG_ERR);
-			return - 1;
+			$this->error = "Error ".$this->db->lasterror();
+			dol_syslog(get_class($this)."::select_pcgsubtype ".$this->error, LOG_ERR);
+			return -1;
 		}
 		$this->db->free($resql);
-		
+
 		// Build select
-		if (count($aux_account) > 0) {
-			
-			$out .= ajax_combobox($htmlname, $event);
-			
-			$out .= '<select id="' . $htmlname . '" class="flat" name="' . $htmlname . '">';
-			if ($showempty)
-				$out .= '<option value="-1"></option>';
-			foreach ( $aux_account as $key => $val ) {
-				if (($selectid != '') && $selectid == $key) {
-					$out .= '<option value="' . $key . '" selected>' . $val . '</option>';
-				} else {
-					$out .= '<option value="' . $key . '">' . $val . '</option>';
-				}
-			}
-			$out .= '</select>';
-		}
-		
+		$out = ajax_combobox($htmlname, $event);
+		$out .= Form::selectarray($htmlname, $aux_account, $selectid, $showempty);
+
 		return $out;
 	}
 	
@@ -352,55 +278,29 @@ class FormVentilation extends Form
 	 * @param string $output_format (html/opton (for option html only)/array (to return options arrays
 	 * @return string/array
 	 */
-	function selectyear_accountancy_bookkepping($selected = '', $htmlname = 'yearid', $useempty = 0, $output_format = 'html') {
-		$out = '';
-		$out_array = array ();
-		
-		if ($output_format == 'html') {
-			$out .= '<select class="flat" placeholder="aa" id="' . $htmlname . '" name="' . $htmlname . '"' . $option . ' >';
-		}
-		if ($useempty) {
-			$selected_html = '';
-			if ($selected == '') {
-				$selected_html = ' selected';
-			}
-			if ($output_format == 'html' || $output_format == 'options') {
-				$out .= '<option value=""' . $selected_html . '>&nbsp;</option>';
-			} elseif ($output_format == 'array') {
-				$out_array[''] = '';
-			}
-		}
-		
+	function selectyear_accountancy_bookkepping($selected = '', $htmlname = 'yearid', $useempty = 0, $output_format = 'html')
+	{
+		$out_array = array();
+
 		$sql = "SELECT DISTINCT date_format(doc_date,'%Y') as dtyear";
-		$sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping";
+		$sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping";
 		$sql .= " ORDER BY doc_date";
-		dol_syslog(get_class($this) . "::" . __METHOD__, LOG_DEBUG);
+		dol_syslog(get_class($this)."::".__METHOD__, LOG_DEBUG);
 		$resql = $this->db->query($sql);
-		if ($resql) {
-			while ( $obj = $this->db->fetch_object($resql) ) {
-				$selected_html = '';
-				if ($selected > 0 && $obj->dtyear == $selected)
-					$selected_html = ' selected';
-				if ($output_format == 'html' || $output_format == 'options') {
-					$out .= '<option value="' . $obj->dtyear . '"' . $selected_html . ' >' . $obj->dtyear . '</option>';
-				} elseif ($output_format == 'array') {
-					$out_array[$obj->dtyear] = $obj->dtyear;
-				}
-			}
-		} else {
-			$this->error = "Error " . $this->db->lasterror();
-			dol_syslog(get_class($this) . "::" . __METHOD__ . $this->error, LOG_ERR);
-			return - 1;
+
+		if (!$resql) {
+			$this->error = "Error ".$this->db->lasterror();
+			dol_syslog(get_class($this)."::".__METHOD__.$this->error, LOG_ERR);
+			return -1;
+		}
+		while ($obj = $this->db->fetch_object($resql)) {
+			$out_array[$obj->dtyear] = $obj->dtyear;
 		}
 		$this->db->free($resql);
-		
+
 		if ($output_format == 'html') {
-			$out .= "</select>\n";
-		}
-		
-		if ($output_format == 'html' || $output_format == 'options') {
-			return $out;
-		} elseif ($output_format == 'array') {
+			return Form::selectarray($htmlname, $out_array, $selected, $useempty, 0, 0, 'placeholder="aa"');
+		} else {
 			return $out_array;
 		}
 	}

+ 22 - 22
htdocs/core/class/dolreceiptprinter.class.php

@@ -300,17 +300,17 @@ class dolReceiptPrinter extends Escpos
     function selectTypePrinter($selected='', $htmlname='printertypeid')
     {
         global $langs;
-        $error = 0;
-        $html = '<select class="flat" name="'.$htmlname.'">';
-        $html.= '<option value="1" '.($selected==1?'selected="selected"':'').'>'.$langs->trans('CONNECTOR_DUMMY').'</option>';
-        $html.= '<option value="2" '.($selected==2?'selected="selected"':'').'>'.$langs->trans('CONNECTOR_FILE_PRINT').'</option>';
-        $html.= '<option value="3" '.($selected==3?'selected="selected"':'').'>'.$langs->trans('CONNECTOR_NETWORK_PRINT').'</option>';
-        $html.= '<option value="4" '.($selected==4?'selected="selected"':'').'>'.$langs->trans('CONNECTOR_WINDOWS_PRINT').'</option>';
-        //$html.= '<option value="5" '.($selected==5?'selected="selected"':'').'>'.$langs->trans('CONNECTOR_JAVA').'</option>';
-        $html.= '</select>';
-
-        $this->resprint = $html;
-        return $error;
+        
+        $options = array(
+            1 => $langs->trans('CONNECTOR_DUMMY'),
+            2 => $langs->trans('CONNECTOR_FILE_PRINT'),
+            3 => $langs->trans('CONNECTOR_NETWORK_PRINT'),
+            4 => $langs->trans('CONNECTOR_WINDOWS_PRINT')
+        );
+        
+        $this->resprint = Form::selectarray($htmlname, $options, $selected);
+        
+        return 0;
     }
 
 
@@ -324,17 +324,17 @@ class dolReceiptPrinter extends Escpos
     function selectProfilePrinter($selected='', $htmlname='printerprofileid')
     {
         global $langs;
-        $error = 0;
-        $html = '<select class="flat" name="'.$htmlname.'">';
-        $html.= '<option value="0" '.($selected==0?'selected="selected"':'').'>'.$langs->trans('PROFILE_DEFAULT').'</option>';
-        $html.= '<option value="1" '.($selected==1?'selected="selected"':'').'>'.$langs->trans('PROFILE_SIMPLE').'</option>';
-        $html.= '<option value="2" '.($selected==2?'selected="selected"':'').'>'.$langs->trans('PROFILE_EPOSTEP').'</option>';
-        $html.= '<option value="3" '.($selected==3?'selected="selected"':'').'>'.$langs->trans('PROFILE_P822D').'</option>';
-        $html.= '<option value="4" '.($selected==4?'selected="selected"':'').'>'.$langs->trans('PROFILE_STAR').'</option>';
-        $html.= '</select>';
-
-        $this->profileresprint = $html;
-        return $error;
+        
+        $options = array(
+            0 => $langs->trans('PROFILE_DEFAULT'),
+            1 => $langs->trans('PROFILE_SIMPLE'),
+            2 => $langs->trans('PROFILE_EPOSTEP'),
+            3 => $langs->trans('PROFILE_P822D'),
+            4 => $langs->trans('PROFILE_STAR')
+        );
+        
+        $this->profileresprint = Form::selectarray($htmlname, $options, $selected);
+        return 0;
     }
 
 

+ 14 - 17
htdocs/core/class/html.formcompany.class.php

@@ -146,9 +146,6 @@ class FormCompany
 		print '<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
 		print '<tr><td>';
 
-		print '<select class="flat" name="'.$htmlname.'">';
-		if ($empty) print '<option value="">&nbsp;</option>';
-
 		dol_syslog(get_class($this).'::form_prospect_level',LOG_DEBUG);
 		$sql = "SELECT code, label";
 		$sql.= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
@@ -157,25 +154,25 @@ class FormCompany
 		$resql = $this->db->query($sql);
 		if ($resql)
 		{
-			$num = $this->db->num_rows($resql);
-			$i = 0;
-			while ($i < $num)
-			{
-				$obj = $this->db->fetch_object($resql);
+			$options = array();
 
-				print '<option value="'.$obj->code.'"';
-				if ($selected == $obj->code) print ' selected';
-				print '>';
-				$level=$langs->trans($obj->code);
-				if ($level == $obj->code) $level=$langs->trans($obj->label);
-				print $level;
-				print '</option>';
+			if ($empty) {
+				$options[''] = '';
+			}
 
-				$i++;
+			while ($obj = $this->db->fetch_object($resql)) {
+				$level = $langs->trans($obj->code);
+
+				if ($level == $obj->code) {
+					$level = $langs->trans($obj->label);
+				}
+
+				$options[$obj->code] = $level;
 			}
+
+			print Form::selectarray($htmlname, $options, $selected);
 		}
 		else dol_print_error($this->db);
-		print '</select>';
 
 		print '</td>';
 		print '<td align="left"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></td>';

+ 4 - 30
htdocs/core/class/html.formmailing.class.php

@@ -27,22 +27,8 @@ require_once DOL_DOCUMENT_ROOT .'/core/class/html.form.class.php';
  */
 class FormMailing  extends Form
 {
-	public $db;
-	public $error;
 	public $errors=array();
 
-
-	/**
-	 *	Constructor
-	 *
-	 *  @param		DoliDB		$db      Database handler
-	*/
-	function __construct($db)
-	{
-		$this->db = $db;
-		return 1;
-	}
-
 	/**
 	 * Output a select with destinaries status
 	 * 
@@ -59,26 +45,14 @@ class FormMailing  extends Form
 		require_once DOL_DOCUMENT_ROOT.'/comm/mailing/class/mailing.class.php';
 		$mailing = new Mailing($this->db);
 
-
-		$array = $mailing->statut_dest;
-		//Cannot use form->selectarray because empty value is defaulted to -1 in this method and we use here status -1...
-
-		$out = '<select name="'.$htmlname.'" class="flat">';
+		$options = array();
 
 		if ($show_empty) {
-			$out .= '<option value=""></option>';
+			$options[''] = '';
 		}
 
-		foreach($mailing->statut_dest as $id=>$status) {
-			if ($selectedid==$id)  {
-				$selected=" selected ";
-			}else {
-				$selected="";
-			}
-			$out .= '<option '.$selected.' value="'.$id.'">'.$langs->trans($status).'</option>';
-		}
+		$options = array_merge($options, $mailing->statut_dest);
 
-		$out .= '</select>';
-		return $out;
+		return Form::selectarray($htmlname, $options, $selectedid, 0, 0, 0, '', 1);
 	}
 }

+ 36 - 77
htdocs/core/class/html.formorder.class.php

@@ -1,5 +1,6 @@
 <?php
 /* Copyright (C) 2008-2012 Laurent Destailleur  <eldy@users.sourceforge.net>
+ * Copyright (C) 2016      Marcos García        <marcosgdf@gmail.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -21,29 +22,14 @@
  *	\brief      File of predefined functions for HTML forms for order module
  */
 
+require_once DOL_DOCUMENT_ROOT .'/core/class/html.form.class.php';
 
 /**
  *	Class to manage HTML output components for orders
  *	Before adding component here, check they are not into common part Form.class.php
  */
-class FormOrder
+class FormOrder extends Form
 {
-	var $db;
-	var $error;
-
-
-
-	/**
-	 *	Constructor
-	 *
-	 *	@param	DoliDB	$db		Database handler
-	 */
-	function __construct($db)
-	{
-		$this->db = $db;
-		return 1;
-	}
-
 
 	/**
      *    Return combo list of differents status of a orders
@@ -53,51 +39,32 @@ class FormOrder
      *    @param	string	$hmlname	Name of HTML select element
      *    @return	void
      */
-    function selectSupplierOrderStatus($selected='', $short=0, $hmlname='order_status')
+    public function selectSupplierOrderStatus($selected='', $short=0, $hmlname='order_status')
     {
-        $tmpsupplierorder=new CommandeFournisseur($db);
-        
-        print '<select class="flat" name="'.$hmlname.'">';
-        print '<option value="-1">&nbsp;</option>';
-        $statustohow=array('0'=>'0','1'=>'1','2'=>'2','3'=>'3','4'=>'4','5'=>'5','6'=>'6,7','9'=>'9');	// 7 is same label than 6. 8 does not exists (billed is another field)
-
-        foreach($statustohow as $key => $value)
-        {
-			print '<option value="'.$value.'"'.(($selected == $key || $selected == $value)?' selected':'').'>';
-			$tmpsupplierorder->statut=$key;
-			print $tmpsupplierorder->getLibStatut($short);
-	        print '</option>';
-        }
-        print '</select>';
+	    $options = array();
+
+	    // 7 is same label than 6. 8 does not exists (billed is another field)
+	    $statustohow = array(
+		    '0' => '0',
+		    '1' => '1',
+		    '2' => '2',
+		    '3' => '3',
+		    '4' => '4',
+		    '5' => '5',
+		    '6' => '6,7',
+		    '9' => '9'
+	    );
+
+	    $tmpsupplierorder = new CommandeFournisseur($this->db);
+
+	    foreach ($statustohow as $key => $value) {
+		    $tmpsupplierorder->statut = $key;
+		    $options[$value] = $tmpsupplierorder->getLibStatut($short);
+	    }
+
+	    print Form::selectarray($hmlname, $options, $selected, 1);
     }
 
-	/**
-	 *  Return list of way to order
-	 *
-	 *	@param	string	$selected		Id of preselected order origin
-	 *  @param  string	$htmlname 		Name of HTML select list
-	 *  @param  int		$addempty		0=liste sans valeur nulle, 1=ajoute valeur inconnue
-	 *  @return	array					Tableau des sources de commandes
-	 */
-	function selectSourcesCommande($selected='',$htmlname='source_id',$addempty=0)
-	{
-		global $conf,$langs;
-		print '<select class="flat" name="'.$htmlname.'">';
-		if ($addempty) print '<option value="-1" selected>&nbsp;</option>';
-
-		// TODO Use the table called llx_c_input_reason
-		print '<option value="0"'.($selected=='0'?' selected':'').'>'.$langs->trans('OrderSource0').'</option>';
-		print '<option value="1"'.($selected=='1'?' selected':'').'>'.$langs->trans('OrderSource1').'</option>';
-		print '<option value="2"'.($selected=='2'?' selected':'').'>'.$langs->trans('OrderSource2').'</option>';
-		print '<option value="3"'.($selected=='3'?' selected':'').'>'.$langs->trans('OrderSource3').'</option>';
-		print '<option value="4"'.($selected=='4'?' selected':'').'>'.$langs->trans('OrderSource4').'</option>';
-		print '<option value="5"'.($selected=='5'?' selected':'').'>'.$langs->trans('OrderSource5').'</option>';
-		print '<option value="6"'.($selected=='6'?' selected':'').'>'.$langs->trans('OrderSource6').'</option>';
-
-		print '</select>';
-	}
-
-
 	/**
 	 *	Return list of input method (mode used to receive order, like order received by email, fax, online)
 	 *  List found into table c_input_method.
@@ -107,11 +74,9 @@ class FormOrder
 	 *  @param  int		$addempty		0=list with no empty value, 1=list with empty value
 	 *  @return	array					Tableau des sources de commandes
 	 */
-	function selectInputMethod($selected='',$htmlname='source_id',$addempty=0)
+	public function selectInputMethod($selected='',$htmlname='source_id',$addempty=0)
 	{
-		global $conf,$langs,$form;
-
-        if (! is_object($form)) $form=new Form($this->db);
+		global $langs;
 
         $listofmethods=array();
 
@@ -121,24 +86,18 @@ class FormOrder
 
 		dol_syslog(get_class($this)."::selectInputMethod", LOG_DEBUG);
 		$resql=$this->db->query($sql);
-		if ($resql)
-		{
-			$i = 0;
-			$num = $this->db->num_rows($resql);
-			while ($i < $num)
-			{
-				$obj = $this->db->fetch_object($resql);
-				$listofmethods[$obj->rowid] = $langs->trans($obj->code)!=$obj->code?$langs->trans($obj->code):$obj->label;
-				$i++;
-			}
-		}
-		else
-		{
+
+		if (!$resql) {
 			dol_print_error($this->db);
 			return -1;
 		}
 
-		print $form->selectarray($htmlname,$listofmethods,$selected,$addempty);
+		while ($obj = $this->db->fetch_object($resql)) {
+			$listofmethods[$obj->rowid] = $langs->trans($obj->code) != $obj->code ? $langs->trans($obj->code) : $obj->label;
+		}
+
+		print Form::selectarray($htmlname,$listofmethods,$selected,$addempty);
+
 		return 1;
 	}
 

+ 7 - 14
htdocs/margin/admin/margin.php

@@ -236,6 +236,12 @@ print '<td>'.$langs->trans('ForceBuyingPriceIfNullDetails').'</td>';
 print '</tr>';
 
 // GLOBAL DISCOUNT MANAGEMENT
+$methods = array(
+	1 => $langs->trans('UseDiscountAsProduct'),
+	2 => $langs->trans('UseDiscountAsService'),
+	3 => $langs->trans('UseDiscountOnTotal')
+);
+
 $var=!$var;
 print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
 print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
@@ -243,20 +249,7 @@ print "<input type=\"hidden\" name=\"action\" value=\"remises\">";
 print '<tr '.$bc[$var].'>';
 print '<td>'.$langs->trans("MARGIN_METHODE_FOR_DISCOUNT").'</td>';
 print '<td align="left">';
-print '<select name="MARGIN_METHODE_FOR_DISCOUNT" class="flat">';
-print '<option value="1" ';
-if (isset($conf->global->MARGIN_METHODE_FOR_DISCOUNT) && $conf->global->MARGIN_METHODE_FOR_DISCOUNT == '1')
-	print 'selected ';
-print '>'.$langs->trans('UseDiscountAsProduct').'</option>';
-print '<option value="2" ';
-if (isset($conf->global->MARGIN_METHODE_FOR_DISCOUNT) && $conf->global->MARGIN_METHODE_FOR_DISCOUNT == '2')
-	print 'selected ';
-print '>'.$langs->trans('UseDiscountAsService').'</option>';
-print '<option value="3" ';
-if (isset($conf->global->MARGIN_METHODE_FOR_DISCOUNT) && $conf->global->MARGIN_METHODE_FOR_DISCOUNT == '3')
-	print 'selected ';
-print '>'.$langs->trans('UseDiscountOnTotal').'</option>';
-print '</select>';
+print Form::selectarray('MARGIN_METHODE_FOR_DISCOUNT', $methods, $conf->global->MARGIN_METHODE_FOR_DISCOUNT);
 print '</td>';
 print '<td>';
 print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';

+ 2 - 34
htdocs/product/document.php

@@ -262,43 +262,11 @@ if ($object->id)
 
     			print  '<tr class="liste_titre"><td>';
 
-    			$delauft_lang = (empty($lang_id)) ? $langs->getDefaultLang() : $lang_id;
+    			$delauft_lang = empty($lang_id) ? $langs->getDefaultLang() : $lang_id;
 
     			$langs_available = $langs->get_available_languages(DOL_DOCUMENT_ROOT, 12);
 
-    			print  '<select class="flat" id="lang_id" name="lang_id">';
-
-    			asort($langs_available);
-
-    			$uncompletelanguages = array (
-    					'da_DA',
-    					'fi_FI',
-    					'hu_HU',
-    					'is_IS',
-    					'pl_PL',
-    					'ro_RO',
-    					'ru_RU',
-    					'sv_SV',
-    					'tr_TR',
-    					'zh_CN'
-    			);
-    			foreach ( $langs_available as $key => $value )
-    			{
-    				if ($showwarning && in_array($key, $uncompletelanguages))
-    				{
-    					// $value.=' - '.$langs->trans("TranslationUncomplete",$key);
-    				}
-    				if ($filter && is_array($filter)) {
-    					if (! array_key_exists($key, $filter)) {
-    						print  '<option value="' . $key . '">' . $value . '</option>';
-    					}
-    				} else if ($delauft_lang == $key) {
-    					print  '<option value="' . $key . '" selected>' . $value . '</option>';
-    				} else {
-    					print  '<option value="' . $key . '">' . $value . '</option>';
-    				}
-    			}
-    			print  '</select>';
+			    print Form::selectarray('lang_id', $langs_available, $delauft_lang, 0, 0, 0, '', 0, 0, 0, 'ASC');
 
     			if ($conf->global->MAIN_MULTILANGS) {
     				print  '<input type="submit" class="button" name="refresh" value="' . $langs->trans('Refresh') . '">';

+ 6 - 6
htdocs/societe/canvas/actions_card_common.class.php

@@ -439,12 +439,12 @@ abstract class ActionsCardCommon
             if ($modCodeClient->code_auto) $this->tpl['prefix_customercode'] = $modCodeClient->verif_prefixIsUsed();
 
             // TODO create a function
-            $this->tpl['select_customertype'] = '<select class="flat" name="client">';
-            $this->tpl['select_customertype'].= '<option value="2"'.($this->object->client==2?' selected':'').'>'.$langs->trans('Prospect').'</option>';
-            $this->tpl['select_customertype'].= '<option value="3"'.($this->object->client==3?' selected':'').'>'.$langs->trans('ProspectCustomer').'</option>';
-            $this->tpl['select_customertype'].= '<option value="1"'.($this->object->client==1?' selected':'').'>'.$langs->trans('Customer').'</option>';
-            $this->tpl['select_customertype'].= '<option value="0"'.($this->object->client==0?' selected':'').'>'.$langs->trans('NorProspectNorCustomer').'</option>';
-            $this->tpl['select_customertype'].= '</select>';
+            $this->tpl['select_customertype'] = Form::selectarray('client', array(
+                0 => $langs->trans('NorProspectNorCustomer'),
+                1 => $langs->trans('Customer'),
+                2 => $langs->trans('Prospect'),
+                3 => $langs->trans('ProspectCustomer')
+            ), $this->object->client);
 
             // Customer
             $this->tpl['customercode'] = $this->object->code_client;