/**
 * 
 * Generate a select form using as options values from a db table.
 * 
 * $sdata values related to select form:
 * -------------------------------------
 * name: Name of select form used also if "ref_value" is empty
 * default: Selector default (selected) value.
 * order: Data base order field, used to order selector field.
 *        By defaulr "ord" if empty. If "use_custom_order" is given as value
 *        the order or selector field will be done by "order_by" value.
 * order_desc: Order by order field descending
 * class: CSS Selector class, empty by default. 
 *        Bootstrap default is: "form-control input-sm"
 * style: Aditional CSS style to the selector.
 * 
 * empty_ini: If "true", an empty option will be added as first selector 
 *            value. Any text supplied but true will be used insted the 
 *            blank character.
 * empty_value: If "empty_ini" is created, a value can be supplied here.
 * name_array: If selector name is supplied by an array, this field must be
 *             the name of this array and value must match "name" parameter.
 *             I.E. if form name is "myform" and it is suplied as:
 *             myarray['myform'], the value to this option must be "myarray"
 *             and "name" option value must be "myname".
 * disable_err: True to return empty text on errors.
 * get_text_value: If true, get the text value from framework text using 
 *                 name as keyword
 * onchange: If true add the event onchange="this.form.submit()", if any 
 *           other data is supplied, use it as parameter of onchange event.
 * multiple: Generate a multiselect selector. If array name supplied, the 
 *           selector name will be name[] to allow multiple values.
 * multiple_na: Avoid to use array names in multiple selects
 * id_ref: Generate a selector id with given value.
 * select2: Generate javascript for select2 if true
 * responsive: Válido sólo si select2 está activo. Añade un 100% width en JS.
 * placeholder: Placeholder for the selector if using select2
 * disabled: Create the selector disabled.
 * readonly: Create a read only selector.
 * required: Add required attribute to select if true
 * selector_empty: Show this text if selector's data set is empty
 * title: Select title
 * tooltip: true to add data-toggle, bootstrap CSS & JS must be loaded
 * placement: data-placement used in data-toggle, default top
 * name_as_value: Use name as option value
 * 
 * 
 * 
 * $sdata values related to MySQL data set:
 * ----------------------------------------
 * ref_field: Data base field to cross value with "ref_value", used to get 
 *            selector values from a table with many selectors data.
 *            The default value can by set in framework conf as class "def" and 
 *            key "selectors_ref_field". If all is empty, default is "ref".
 *            If value is "use_custom_where", a SQL where clause can
 *            be supplied in "ref_where".
 * ref_value: Value to suppy to "ref_field", same as name if empty.
 *            This value must match with the reference value to get actual
 *            selector list of options from selector's data base table.
 * ref_where: If selector list of values must be obtained with a complex
 *            where clause, it can be stated here, adding "use_custom_where"
 *            as "ref_field" value in configuration array.
 * no_where: Select all fields of a database if true (no where sentence)
 * select_list: Load only from a list of IDs like ('id1','id2','id3')
 * select_not_in_list: Load all but a list of IDs like ('id1','id2','id3')
 * order_by: If "use_custom_order" is supplied as value in "order" option
 *           you can supply a custom SQL order statement here.
 * only_name: If true (or not empty), return only the name of given 
 *            value, instead an entire selector.
 * plain_text: Valid with "only_name" switch. Return a plain text with value
 *             if true. Else, return a readonly select with one value.
 * field_name: Valid with "only_name" switch. This is the name of field 
 *              that contains the data.
 * value_name: Valid with "only_name" switch. This is the name of data 
 *              to cross with $this->opt->field_name.
 * sqland: Any sentence after WHERE and before ORDER, i.e. "AND lang = '$lang'"
 * debug: true to activate debug output of MySQL query.
 * 
 * 
 * 
 * $sdata alternative data set:
 * ----------------------------
 * mysql_off: Disable the MySQL data set and load alternative data. 
 *            Data must be supplied in $dbdata as array or object.
 * dataset_id: Field that contains the data set ID (option id)
 * dataset_name: Field that contains the data set value (option name)
 * 
 * Alternative data set must be an array or object with this format:
 * (
 *   ['id']   => ['{id_value}']
 *   ['name'] => ['{name_value}']
 * )
 * 
 * 
 * 
 * 
 * MySQL database values to feed the select:
 * -----------------------------------------
 * $dbdata: Database table to get options for selectors. Must be supplied in
 *          JSON format or as array. If empty, value will be loaded from 
 *          framework conf category "def" key "selectors_data" as JSON, ie:
 *          ["selectors","value","name"]. If not value given and no 
 *          defaul found in conf, the method will return with error.
 *          Array must contain in this order: 
 *          "table name","field value","field name"
 * 
 * 
 * 
 * Alternative error messages option fields:
 * -----------------------------------------
 * txt_data_mut_be_array
 * txt_name_must_be_supplied
 * txt_json_dbdata_incorrect
 * txt_table_not_found
 * txt_value_field_not_found
 * txt_name_field_not_found
 * txt_selector_empty
 * txt_invalid_data_set
 * txt_only_name_incompatible_with_mysql_off
 * 
 * 
 * 
 * @param array|string $sdata array or json with selector data:
   [name,default,order,order_desc,class,style,ref_field,ref_value,ref_where,no_where,
    order_by,empty_ini,empty_value,name_array,disable_err,only_name,
    plain_text,field_name,value_name,get_text_value,onchange,multiple,
    id_ref,select2,placeholder,disabled,required,selector_empty,debug,
    multiple_na]
 * @param array|string $dbdata array or json with db data
 * @return string|boolean Select form | false if empty db data
 * 
 * default text keys: [name_must_be_supplied,selectors_data,
 * json_dbdata_incorrect,table_not_found,value_field_not_found,
 * name_field_not_found,selectors_ref_field,selectors_order_field,
 * selector_empty]
 * 
 */


Mensajes de error de la clase

Los mensajes de error se pueden reescribir, los valores por defectro son:
  • txt_data_mut_be_array: Selector data must be an array
  • txt_name_must_be_supplied: Name must be supplied
  • txt_json_dbdata_incorrect: JSON db data is incorrect
  • txt_table_not_found: Table not found
  • txt_value_field_not_found: Value field not found
  • txt_name_field_not_found: Name field not found
  • txt_selector_empt: Selector is empty
  • txt_invalid_data_set: If mysql_off active, you must supply a valid data set to $dbdata
  • txt_only_name_incompatible_with_mysql_off: only_name is incompatible with mysql_off

Si se quieren obtener errores personalizados, se puede hacer de dos formas, cargando un nuevo valor antes de llamar a getSelect.
Por ejemplo, reescribiendo el valor de txt_table_not_found:
$wv->selectForm->default->txt_table_not_found = "Tabla no encontrada";
echo $wv->selectForm->getSelect($sdata,$dbdata);

Otra forma es añadiendo la clave de configuración "select_defaults" en conf_framework o antes de iniciar la clase. Por ejemplo:
$wv_conf['select_defaults']['txt_table_not_found'] = "Tabla no encontrada";


EJEMPLOS

Ejemplo de uso con base de datos externa:
$datos = $wv->db->get_results("
    SELECT * FROM $dbn->db_datos1
        LEFT JOIN $dbn->db_datos2
            ON $dbn->db_datos1.id1 = $dbn->db_datos2.id2 ");

$dbdata = [];
foreach ((object)$datos as $d) {
    $dbdata[] = ['product_id'=>$d->id,'product_name'=>$d->name];
}

$sdata['mysql_off'] = true; //Desconecta el uso de MySQL
$sdata['name'] = "nombre";
$sdata['default'] = $default;
$sdata['empty_ini'] = "Elija uno";
$sdata['class'] = "form-control input-sm";
$sdata['id_ref'] = "fabricante";
$sdata['select2'] = true;

//Si los datos del selector no se llaman id y name
$sdata['dataset_id'] = "product_id"; //Por defecto id
$sdata['dataset_name'] = "product_name"; //Por defecto name

//Init classes
$wv->initClass(array('class_name' => 'selectForm','class_this' => true),1);

//Generate selector
$fabricante_selector = $wv->selectForm->getSelect($sdata,$dbdata);