2017-08-04 16:28:16 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace HTMLBuilder\Handler;
|
|
|
|
|
|
|
|
/**
|
2018-01-12 16:23:26 +01:00
|
|
|
* Gestione dell'input di tipo "select".
|
|
|
|
*
|
2017-08-04 16:28:16 +02:00
|
|
|
* @since 2.3
|
|
|
|
*/
|
|
|
|
class SelectHandler implements HandlerInterface
|
|
|
|
{
|
|
|
|
public function handle(&$values, &$extras)
|
|
|
|
{
|
2018-01-12 16:23:26 +01:00
|
|
|
// Individuazione della classe per la corretta gestione JavaScript
|
|
|
|
$values['class'][] = !empty($values['ajax-source']) ? 'superselectajax' : 'superselect';
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2018-01-12 16:23:26 +01:00
|
|
|
// Individuazione della richiesta AJAX (se presente)
|
|
|
|
$values['data-source'] = !empty($values['ajax-source']) ? $values['ajax-source'] : '';
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2018-01-12 16:23:26 +01:00
|
|
|
// Individuazione e gestione dei valori tramite array
|
2017-08-04 16:28:16 +02:00
|
|
|
$values['value'] = explode(',', $values['value']);
|
|
|
|
if (count($values['value']) === 1 && strlen($values['value'][0]) === 0) {
|
|
|
|
$values['value'] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Se il valore presente non è valido, carica l'eventuale valore predefinito
|
|
|
|
if (empty($values['value']) && !is_numeric($values['value']) && !empty($values['valore_predefinito'])) {
|
2018-07-08 18:11:17 +02:00
|
|
|
$values['value'] = setting($values['valore_predefinito']);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$values['value'] = (array) $values['value'];
|
|
|
|
|
2018-01-12 16:23:26 +01:00
|
|
|
// Inizializzazione del codice HTML
|
2017-08-04 16:28:16 +02:00
|
|
|
$result = '
|
|
|
|
<select |attr|>';
|
|
|
|
|
2018-01-12 16:23:26 +01:00
|
|
|
// Delega della generazione del codice HTML in base alle caratteristiche del formato
|
|
|
|
// Gestione delle richieste AJAX (se il campo "ajax-source" è impostato)
|
2017-08-04 16:28:16 +02:00
|
|
|
if (!empty($values['ajax-source'])) {
|
|
|
|
if (!empty($values['value']) || is_numeric($values['value'])) {
|
|
|
|
$result .= $this->select2($values['ajax-source'], $values['value']);
|
|
|
|
}
|
2018-05-11 14:56:57 +02:00
|
|
|
} else {
|
|
|
|
if (!in_array('multiple', $extras)) {
|
|
|
|
$result .= '
|
|
|
|
<option></option>';
|
|
|
|
}
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2018-06-23 15:41:32 +02:00
|
|
|
// Gestione del select dal formato JSON completo, convertito in array
|
|
|
|
if (is_array($values['values'])) {
|
|
|
|
$result .= $this->selectArray($values['values'], $values['value']);
|
|
|
|
}
|
|
|
|
|
2018-05-11 14:56:57 +02:00
|
|
|
// Gestione del select da query specifica (se il campo "values" è impostato a "query=SQL")
|
2018-06-23 15:41:32 +02:00
|
|
|
elseif (preg_match_all('/^query=(.+?)$/', $values['values'], $matches)) {
|
2018-05-11 14:56:57 +02:00
|
|
|
$result .= $this->selectQuery($matches[1][0], $values['value']);
|
|
|
|
}
|
2018-01-12 16:23:26 +01:00
|
|
|
|
2018-05-11 14:56:57 +02:00
|
|
|
// Gestione del select dal formato JSON parziale (valori singoli)
|
|
|
|
elseif (preg_match_all('/^list=(.+?)$/', $values['values'], $matches)) {
|
|
|
|
$result .= $this->selectList(json_decode('{'.$matches[1][0].'}', true), $values);
|
|
|
|
}
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
2018-01-12 16:23:26 +01:00
|
|
|
// Impostazione del placeholder
|
2019-07-08 18:10:52 +02:00
|
|
|
$values['placeholder'] = !empty($values['placeholder']) ? $values['placeholder'] : tr("Seleziona un'opzione");
|
2017-08-04 16:28:16 +02:00
|
|
|
$values['data-placeholder'] = $values['placeholder'];
|
|
|
|
|
|
|
|
unset($values['values']);
|
|
|
|
|
|
|
|
$result .= '
|
|
|
|
</select>';
|
|
|
|
|
2018-01-12 16:23:26 +01:00
|
|
|
// Gestione delle proprietà "disabled" e "readonly"
|
2017-08-04 16:28:16 +02:00
|
|
|
if (in_array('disabled', $extras) || in_array('readonly', $extras)) {
|
|
|
|
$result .= '
|
|
|
|
<script>$("#'.$values['id'].'").prop("disabled", true);</script>';
|
|
|
|
}
|
|
|
|
|
2018-01-12 16:23:26 +01:00
|
|
|
// Ulteriore gestione della proprietà "readonly" (per rendere il select utilizzabile dopo il submit)
|
2017-08-04 16:28:16 +02:00
|
|
|
if (in_array('readonly', $extras) && empty($values['ajax-source'])) {
|
|
|
|
$result .= '
|
2017-09-15 09:48:56 +02:00
|
|
|
<select class="hide" name="'.prepareToField($values['name']).'"'.((in_array('multiple', $extras)) ? ' multiple' : '').'>';
|
2017-08-04 16:28:16 +02:00
|
|
|
|
|
|
|
foreach ($values['value'] as $value) {
|
|
|
|
$result .= '
|
2017-09-15 09:48:56 +02:00
|
|
|
<option value="'.prepareToField($value).'" selected></option>';
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$result .= '
|
|
|
|
</select>';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2018-01-12 16:23:26 +01:00
|
|
|
/**
|
|
|
|
* Gestione dell'input di tipo "select" con richieste AJAX (nome della richiesta indicato tramite attributo "ajax-source").
|
|
|
|
* Esempio: {[ "type": "select", "label": "Select di test", "name": "test", "ajax-source": "test" ]}.
|
|
|
|
*
|
2019-02-22 10:37:37 +01:00
|
|
|
* @param string $op
|
|
|
|
* @param array $elements
|
2018-01-12 16:23:26 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-08-04 16:28:16 +02:00
|
|
|
protected function select2($op, $elements)
|
|
|
|
{
|
2018-01-12 16:23:26 +01:00
|
|
|
// Richiamo del file dedicato alle richieste AJAX per ottenere il valore iniziale del select
|
2017-08-04 16:28:16 +02:00
|
|
|
ob_start();
|
2018-09-20 12:05:22 +02:00
|
|
|
$dbo = database();
|
2017-08-04 16:28:16 +02:00
|
|
|
include DOCROOT.'/ajax_select.php';
|
|
|
|
$text = ob_get_clean();
|
|
|
|
|
2019-02-22 10:37:37 +01:00
|
|
|
$html = '';
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2019-02-22 10:37:37 +01:00
|
|
|
$response = (array) json_decode($text, true);
|
|
|
|
$results = $response['results'];
|
|
|
|
foreach ($results as $element) {
|
2017-08-04 16:28:16 +02:00
|
|
|
$element = (array) $element;
|
|
|
|
if (isset($element['children'][0])) {
|
|
|
|
$element = (array) $element['children'][0];
|
|
|
|
}
|
|
|
|
|
|
|
|
$attributes = [];
|
|
|
|
if (in_array($element['id'], $elements)) {
|
|
|
|
$attributes[] = 'selected';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($element['_bgcolor_'])) {
|
|
|
|
$attributes[] = 'style="background:'.$element['_bgcolor_'].'; color:'.color_inverse($element['_bgcolor_'].';"');
|
|
|
|
}
|
|
|
|
|
|
|
|
$exclude = ['id', 'text'];
|
|
|
|
// Leggo ulteriori campi oltre a id e descrizione per inserirli nell'option nella forma "data-nomecampo1", "data-nomecampo2", ecc
|
|
|
|
foreach ($element as $key => $value) {
|
|
|
|
if (!in_array($key, $exclude)) {
|
2017-09-15 09:48:56 +02:00
|
|
|
$attributes[] = 'data-'.$key.'="'.prepareToField($value).'"';
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 10:37:37 +01:00
|
|
|
$html .= '
|
2017-09-15 09:48:56 +02:00
|
|
|
<option value="'.prepareToField($element['id']).'" '.implode(' ', $attributes).'>'.$element['text'].'</option>';
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
2019-02-22 10:37:37 +01:00
|
|
|
return $html;
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
2018-01-12 16:23:26 +01:00
|
|
|
/**
|
|
|
|
* Gestione dell'input di tipo "select" basato su un array associativo.
|
|
|
|
* Esempio: {[ "type": "select", "name": "tipo", "values": [{"id":"M","text":"Maschio"},{"id":"F","text":"Femmina"},{"id":"U","text":"Unisex"}], "value": "U", "placeholder": "Non specificato" ]}.
|
|
|
|
*
|
|
|
|
* @param array $values
|
|
|
|
* @param array $extras
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function selectArray($array, $values)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2018-06-23 15:41:32 +02:00
|
|
|
$result = '';
|
|
|
|
|
2017-08-04 16:28:16 +02:00
|
|
|
$prev = '';
|
|
|
|
foreach ($array as $element) {
|
|
|
|
if (!empty($element['optgroup'])) {
|
|
|
|
if ($prev != $element['optgroup']) {
|
|
|
|
$result .= '
|
2017-09-15 09:48:56 +02:00
|
|
|
<optgroup label="'.prepareToField($element['optgroup']).'"></optgroup>';
|
2017-08-04 16:28:16 +02:00
|
|
|
$prev = $element['optgroup'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$element['text'] = empty($element['text']) ? $element['descrizione'] : $element['text'];
|
|
|
|
|
|
|
|
$attributes = [];
|
|
|
|
if (in_array($element['id'], $values)) {
|
|
|
|
$attributes[] = 'selected';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($element['_bgcolor_'])) {
|
|
|
|
$attributes[] = 'style="background:'.$element['_bgcolor_'].'; color:'.color_inverse($element['_bgcolor_']).';"';
|
|
|
|
}
|
|
|
|
|
2018-01-12 16:23:26 +01:00
|
|
|
$exclude = [
|
|
|
|
'optgroup',
|
|
|
|
];
|
2017-08-04 16:28:16 +02:00
|
|
|
// Leggo ulteriori campi oltre a id e descrizione per inserirli nell'option nella forma "data-nomecampo1", "data-nomecampo2", ecc
|
|
|
|
foreach ($element as $key => $value) {
|
|
|
|
if (!in_array($key, $exclude)) {
|
2017-09-15 09:48:56 +02:00
|
|
|
$attributes[] = 'data-'.$key.'="'.prepareToField($value).'"';
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$result .= '
|
2017-09-15 09:48:56 +02:00
|
|
|
<option value="'.prepareToField($element['id']).'" '.implode(' ', $attributes).'>'.$element['text'].'</option>';
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2018-01-12 16:23:26 +01:00
|
|
|
/**
|
|
|
|
* Gestione dell'input di tipo "select" basato su una query specifica.
|
|
|
|
* Esempio: {[ "type": "select", "label": "Select di test", "name": "select", "values": "query=SELECT id, name as text FROM table" ]}.
|
|
|
|
*
|
|
|
|
* @param array $values
|
|
|
|
* @param array $extras
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-08-04 16:28:16 +02:00
|
|
|
protected function selectQuery($query, $values)
|
|
|
|
{
|
|
|
|
$result = '';
|
|
|
|
|
2018-09-20 12:05:22 +02:00
|
|
|
$database = database();
|
2017-08-04 16:28:16 +02:00
|
|
|
|
|
|
|
$array = $database->fetchArray($query);
|
|
|
|
|
2018-01-12 16:23:26 +01:00
|
|
|
return $this->selectArray($array, $values);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
2018-01-12 16:23:26 +01:00
|
|
|
/**
|
|
|
|
* Gestione dell'input di tipo "select" basato su una lista parzialmente JSON.
|
|
|
|
* Esempio: {[ "type": "select", "label": "Sesso", "name": "sesso", "values": "list=\"\": \"Non specificato\", \"M\": \"Maschio\", \"F\": \"Femmina\", \"U\": \"Unisex\"", "value": "M" ]}.
|
|
|
|
*
|
|
|
|
* @param array $values
|
|
|
|
* @param array $extras
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-08-04 16:28:16 +02:00
|
|
|
protected function selectList($datas, &$values)
|
|
|
|
{
|
2018-01-12 16:23:26 +01:00
|
|
|
$array = [];
|
2017-08-04 16:28:16 +02:00
|
|
|
|
|
|
|
foreach ($datas as $key => $value) {
|
|
|
|
if (!empty($key)) {
|
2018-01-12 16:23:26 +01:00
|
|
|
$array[] = ['id' => $key, 'text' => $value];
|
2017-08-04 16:28:16 +02:00
|
|
|
} elseif (empty($values['placeholder'])) {
|
|
|
|
$values['placeholder'] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 16:23:26 +01:00
|
|
|
return $this->selectArray($array, $values['value']);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
}
|