'; if (!empty($values['ajax-source'])) { if (!empty($values['value']) || is_numeric($values['value'])) { $result .= $this->select2($values['ajax-source'], $values['value']); } } // Generazione da JSON // esempio creazione select con opzioni: Maschio, Femmina, Unisex // {[ "type": "select", "label": "Sesso", "name": "sesso", "values": "list=\"\": \"\", \"M\": \"Maschio\", \"F\": \"Femmina\", \"U\": \"Unisex\"", "value": "$sesso$" ]} elseif (preg_match_all('/^list=(.+?)$/', $values['values'], $matches)) { $result .= ' '; $result .= $this->selectList(json_decode('{'.$matches[1][0].'}', true), $values); } elseif (preg_match_all('/^json=(.+?)$/', $values['values'], $matches)) { $result .= ' '; $result .= $this->selectJSON(json_decode('[{'.$matches[1][0].'}]', true), $values['value']); } $values['placeholder'] = !empty($values['placeholder']) ? $values['placeholder'] : '- '._("Seleziona un'opzione").' -'; $values['data-placeholder'] = $values['placeholder']; unset($values['values']); $result .= ' '; if (in_array('disabled', $extras) || in_array('readonly', $extras)) { $result .= ' '; } if (in_array('readonly', $extras) && empty($values['ajax-source'])) { $result .= ' '; } return $result; } protected function select2($op, $elements) { // Richiamo alla pagina ajax_select.php per aggiungere il valore iniziale al select ob_start(); $dbo = \Database::getConnection(); include DOCROOT.'/ajax_select.php'; $text = ob_get_clean(); $result = ''; $array = (array) json_decode($text, true); foreach ($array as $element) { $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)) { $attributes[] = 'data-'.$key.'="'.\HTMLBuilder\prepareToField($value).'"'; } } $result .= ' '; } return $result; } protected function selectJSON($array, $values) { $prev = ''; foreach ($array as $element) { if (!empty($element['optgroup'])) { if ($prev != $element['optgroup']) { $result .= ' '; $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_']).';"'; } $exclude = ['optgroup']; // 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)) { $attributes[] = 'data-'.$key.'="'.\HTMLBuilder\prepareToField($value).'"'; } } $result .= ' '; } return $result; } protected function selectQuery($query, $values) { $result = ''; $database = \Database::getConnection(); $array = $database->fetchArray($query); return $this->selectJSON($array, $values); } protected function selectList($datas, &$values) { $result = ''; foreach ($datas as $key => $value) { if (!empty($key)) { $attributes = []; if (in_array($key, $values['value'])) { $attributes[] = 'selected'; } $result .= ' '; } elseif (empty($values['placeholder'])) { $values['placeholder'] = $value; } } return $result; } }