Formats field options as javascript

@return string
198438
protected function prepareOptions()
    {
        $options = [];
        foreach ($this->getSelectOptions() as $value => $label) {
            $value     = addslashes($value);
            $label     = addslashes($label);
            $options[] = [
                'sValue' => $value,
                'sLabel' => $label,
            ];
        }

        return $options;
    }
159326
public function fieldName()
    {
        if ($formatted = $this->formatFieldName($this->key)) {
            return $formatted;
        } elseif (strpos($this->key, '_id')) {
            return str_replace('_id', '', $this->key);
        } else {
            return $this->key;
        }
    }
1117108
protected function getOptionsJS()
    {
        $opts = $this->options;
        foreach ( $opts['seriesStorage'] as $label => $options ) {
            $options['label'] = $label;
            $opts['series'][] = $options;
        }
        
        if ( $this->chart->titleHidden() ) {
        	unset( $opts['title'] );
        }
        
        unset($opts['seriesStorage']);
        return $this->makeJSArray( $opts );
    }
773715
public function adminhtml(){
		echo('<select class="form-control" name="'.$this->getid($this->id).'">');
		foreach($this->items as $k=>$v){
			echo('<option value="'.$k.'"'.($this->value==$k?' selected':'').'>'.$v.'</option>');
			}                               
		echo('</select>');
		}
1042663
public function generateAndIncludeJavaScript()
    {
        $filePath = $this->assetHandlerConnectorManager->getFormzGeneratedFilePath() . '.js';

        $this->assetHandlerConnectorManager->createFileInTemporaryDirectory(
            $filePath,
            function () {
                return
                    // Form initialization code.
                    $this->getFormInitializationJavaScriptAssetHandler()
                        ->getFormInitializationJavaScriptCode() .
                    LF .
                    // Fields validation code.
                    $this->getFieldsValidationJavaScriptAssetHandler()
                        ->getJavaScriptCode() .
                    LF .
                    // Fields activation conditions code.
                    $this->getFieldsActivationJavaScriptAssetHandler()
                        ->getFieldsActivationJavaScriptCode() .
                    LF .
                    // Fields validation activation conditions code.
                    $this->getFieldsValidationActivationJavaScriptAssetHandler()
                        ->getFieldsValidationActivationJavaScriptCode();
            }
        );

        $this->includeJsFile(StringService::get()->getResourceRelativePath($filePath));

        return $this;
    }
595588
public function getAttributeValueTextRepresentation()
    {
        $value = $this->getAttributeValueObject();
        if ($value === null) {
            $result = '';
        } else {
            $options = [];
            foreach ($value->getSelectedOptions() as $selectedOption) {
                /* @var SelectValueOption $selectedOption */
                $options[] = $selectedOption->getSelectAttributeOptionValue();
            }
            $result = implode("\n", $options);
        }

        return $result;
    }
1132313
protected function getOptions()
	{
		$options = array();

		foreach ($this->element->children() as $option)
		{
			// Only add <option /> elements.
			if ($option->getName() != 'option')
			{
				continue;
			}

			// Create a new option object based on the <option /> element.
			$tmp = HtmlSelect::option(
				(string) $option['value'],
				Text::alt(trim((string) $option), preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname)), 'value', 'text',
				((string) $option['disabled'] == 'true')
			);

			// Set some option attributes.
			$tmp->class = (string) $option['class'];

			// Set some JavaScript option attributes.
			$tmp->onclick = (string) $option['onclick'];

			// Add the option object to the result set.
			$options[] = $tmp;
		}

		reset($options);

		return $options;
	}
624425
protected function getOptionsString()
    {
        $value   = $this->getValue();
        $options = '';
        if ($this->get('_first_option')) {
            $options .= sprintf('<option value="">%s</option>', $this->get('_first_option'));
        }
        $optionsList = $this->get('_options') ?: array();
        foreach ($optionsList as $k => $v) {
            $selected = '';
            // be flexible, accept a non-array value
            if ((is_string($value) && $k == $value) || (is_array($value) && in_array($k, $value))) {
                $selected = 'selected="selected"';
            }
            $options .= sprintf('<option value="%s" %s>%s</option>', htmlentities($k, ENT_COMPAT), $selected, $v);
        }

        return $options;
    }
294474
private function _optionsHTML()
    {
        $result = '';
        foreach ($this->options() as $key => $label) {
            $selected = ResourceTable::getSearchValue($this->index()) == $key ? ' selected="selected"' : '';
            $result .= '<option value="'.$key.'"'.$selected.'>'.$label.'</option>';
        }
        return $result;
    }
592762
function toHtml()
    {
        // prevent problems with grouped elements
        $arrayName = str_replace(array('[', ']'), array('__', ''), $this->getName()) . '_values';

        $this->updateAttributes(array(
            'onkeypress' => 'return autocomplete(this, event, ' . $arrayName . ');'
        ));
        if ($this->_flagFrozen) {
            $js = '';
        } else {
            $js = "<script type=\"text/javascript\">\n//<![CDATA[\n";
            if (!defined('HTML_QUICKFORM_AUTOCOMPLETE_EXISTS')) {
                $this->_js .= <<<EOS

/* begin javascript for autocomplete */
function setSelectionRange(input, selectionStart, selectionEnd) {
    if (input.setSelectionRange) {
        input.setSelectionRange(selectionStart, selectionEnd);
    }
    else if (input.createTextRange) {
        var range = input.createTextRange();
        range.collapse(true);
        range.moveEnd("character", selectionEnd);
        range.moveStart("character", selectionStart);
        range.select();
    }
    input.focus();
}

function setCaretToPosition(input, position) {
    setSelectionRange(input, position, position);
}

function replaceSelection (input, replaceString) {
	var len = replaceString.length;
    if (input.setSelectionRange) {
        var selectionStart = input.selectionStart;
        var selectionEnd = input.selectionEnd;

        input.value = input.value.substring(0, selectionStart) + replaceString + input.value.substring(selectionEnd);
		input.selectionStart  = selectionStart + len;
		input.selectionEnd  = selectionStart + len;
    }
    else if (document.selection) {
        var range = document.selection.createRange();
		var saved_range = range.duplicate();

        if (range.parentElement() == input) {
            range.text = replaceString;
			range.moveEnd("character", saved_range.selectionStart + len);
			range.moveStart("character", saved_range.selectionStart + len);
			range.select();
        }
    }
    input.focus();
}


function autocompleteMatch (text, values) {
    for (var i = 0; i < values.length; i++) {
        if (values[i].toUpperCase().indexOf(text.toUpperCase()) == 0) {
            return values[i];
        }
    }

    return null;
}

function autocomplete(textbox, event, values) {
    if (textbox.setSelectionRange || textbox.createTextRange) {
        switch (event.keyCode) {
            case 38:    // up arrow
            case 40:    // down arrow
            case 37:    // left arrow
            case 39:    // right arrow
            case 33:    // page up
            case 34:    // page down
            case 36:    // home
            case 35:    // end
            case 13:    // enter
            case 9:     // tab
            case 27:    // esc
            case 16:    // shift
            case 17:    // ctrl
            case 18:    // alt
            case 20:    // caps lock
            case 8:     // backspace
            case 46:    // delete
                return true;
                break;

            default:
                var c = String.fromCharCode(
                    (event.charCode == undefined) ? event.keyCode : event.charCode
                );
                replaceSelection(textbox, c);
                sMatch = autocompleteMatch(textbox.value, values);
                var len = textbox.value.length;
				
                if (sMatch != null) {
                    textbox.value = sMatch;
                    setSelectionRange(textbox, len, textbox.value.length);
                }
                return false;
        }
    }
    else {
        return true;
    }
}
/* end javascript for autocomplete */

EOS;
                define('HTML_QUICKFORM_AUTOCOMPLETE_EXISTS', true);
            }
            $jsEscape = array(
                "\r"    => '\r',
                "\n"    => '\n',
                "\t"    => '\t',
                "'"     => "\\'",
                '"'     => '\"',
                '\\'    => '\\\\'
            );

            $js .= $this->_js;
            $js .= 'var ' . $arrayName . " = new Array();\n";
            for ($i = 0; $i < count($this->_options); $i++) {
                $js .= $arrayName . '[' . $i . "] = '" . strtr($this->_options[$i], $jsEscape) . "';\n";
            }
            $js .= "//]]>\n</script>";
        }
        return $js . parent::toHtml();
    }
428910
function toHTML()
    {

        // get some attributes of the control
        $attributes = $this->get_attributes(array('name', 'value', 'class', 'format', 'hours', 'minutes', 'seconds'));

        // make sure format is in lower characters
        $attributes['format'] = strtolower($attributes['format']);

        // if invalid format specified, revert to the default "hm"
        if (preg_match('/^[hmsg]+$/i', $attributes['format']) == 0 || strlen(preg_replace('/([a-z]{2,})/i', '$1', $attributes['format'])) != strlen($attributes['format'])) $attributes['format'] = 'hm';

        // see what have we sepcified as default time
        $time = array_diff(explode(':', trim(str_replace(array('am', 'pm'), '', strtolower($attributes['value'])))), array(''));

        // if, according to the time format, we have to show the hours, and the hour is given in the default time
        if (($hour_position = strpos($attributes['format'], 'h')) !== false && isset($time[$hour_position]))

            // the default selected hour
            $selected_hour = $time[$hour_position];

        // if, according to the time format, we have to show the minutes, and the minutes are given in the default time
        if (($minutes_position = strpos($attributes['format'], 'm')) !== false && isset($time[$minutes_position]))

            // the default selected minute
            $selected_minute = $time[$minutes_position];

        // if, according to the time format, we have to show the seconds, and the seconds are given in the default time
        if (($seconds_position = strpos($attributes['format'], 's')) !== false && isset($time[$seconds_position]))

            // the default selected minute
            $selected_second = $time[$seconds_position];

        // if 12-hours format is to be used
        if (strpos($attributes['format'], 'g')) {

            // set a flag
            $ampm = true;

            // if this is also present in the default time
            if (preg_match('/\bam\b|\bpm\b/i', $attributes['value'], $matches))

                // extract the format from the default time
                $ampm = strtolower($matches[0]);

        }


        $output = '';

        // if the hour picker is to be shown
        if ($hour_position !== false) {

            // generate the hour picker
            $output .= '
                <select name="' . $attributes['name'] . '_hours" id="' . $attributes['name'] . '_hours" ' . $this->_render_attributes() . '>
                    <option value="">-</option>';

            foreach ($attributes['hours'] as $hour)

                // show 12 or 24 hours depending on the format
                if (!isset($ampm) || ($hour > 0 && $hour < 13))

                    $output .= '<option value="' . str_pad($hour, 2, '0', STR_PAD_LEFT) . '"' . (isset($selected_hour) && ltrim($selected_hour, '0') == ltrim($hour, '0') ? '  selected="selected"' : '') . '>' . str_pad($hour, 2, '0', STR_PAD_LEFT) . '</option>';

            $output .= '
                </select>
            ';

        }
        
        // if the minute picker is to be shown
        if ($minutes_position !== false) {

            // generate the minute picker
            $output .= '
                <select name="' . $attributes['name'] . '_minutes" id="' . $attributes['name'] . '_minutes" ' . $this->_render_attributes() . '>
                    <option value="">-</option>';

            foreach ($attributes['minutes'] as $minute)

                $output .= '<option value="' . str_pad($minute, 2, '0', STR_PAD_LEFT) . '"' . (isset($selected_minute) && ltrim($selected_minute, '0') == ltrim($minute, '0') ? ' selected="selected"' : '') . '>' . str_pad($minute, 2, '0', STR_PAD_LEFT) . '</option>';

            $output .= '
                </select>
            ';

        }

        // if the seconds picker is to be shown
        if ($seconds_position !== false) {

            // generate the seconds picker
            $output .= '
                <select name="' . $attributes['name'] . '_seconds" id="' . $attributes['name'] . '_seconds" ' . $this->_render_attributes() . '>
                    <option value="">-</option>';

            foreach ($attributes['seconds'] as $second)

                $output .= '<option value="' . str_pad($second, 2, '0', STR_PAD_LEFT) . '"' . (isset($selected_second) && ltrim($selected_second, '0') == ltrim($second, '0') ? ' selected="selected"' : '') . '>' . str_pad($second, 2, '0', STR_PAD_LEFT) . '</option>';

            $output .= '
                </select>
            ';

        }

        // if am/pm picker is to be shown
        if (isset($ampm)) {

            // generate the AM/PM picker
            $output .= '
                <select name="' . $attributes['name'] . '_ampm" id="' . $attributes['name'] . '_ampm" ' . $this->_render_attributes() . '>
                    <option value="">-</option>';

            $output .= '<option value="AM"' . ($ampm === 'am' ? ' selected="selected"' : '') . '>AM</option>';
            $output .= '<option value="PM"' . ($ampm === 'pm' ? ' selected="selected"' : '') . '>PM</option>';

            $output .= '
                </select>
            ';

        }

        $output .= '<div class="clear"></div>';

        return $output;

    }
955515
protected function getFieldFormat()
    {
        $format = $this->localeFormat->getPriceFormat();

        $attribute = $this->getFilter()->getAttributeModel();

        $format['pattern']           = (string) $attribute->getDisplayPattern();
        $format['precision']         = (int) $attribute->getDisplayPrecision();
        $format['requiredPrecision'] = (int) $attribute->getDisplayPrecision();
        $format['integerRequired']   = (int) $attribute->getDisplayPrecision() > 0;

        return $format;
    }
1131206
function xmlString()
    {
        $doc = new DOMDocument( '1.0', 'utf-8' );
        $root = $doc->createElement( "ezmultioption" );
        $root->setAttribute( 'option_counter', $this->OptionCounter );
        $doc->appendChild( $root );

        $nameNode = $doc->createElement( 'name' );
        $nameNode->appendChild( $doc->createTextNode( $this->Name ) );
        $root->appendChild( $nameNode );

        $multiOptionsNode = $doc->createElement( "multioptions" );
        $root->appendChild( $multiOptionsNode );

        foreach ( $this->Options as $multioption )
        {
            unset( $multioptionNode );
            $multioptionNode = $doc->createElement( "multioption" );
            $multioptionNode->setAttribute( "id", $multioption['id'] );
            $multioptionNode->setAttribute( "name", $multioption['name'] );
            $multioptionNode->setAttribute( "priority", $multioption['priority'] );
            $multioptionNode->setAttribute( 'default_option_id', $multioption['default_option_id'] );
            foreach ( $multioption['optionlist'] as $option )
            {
                unset( $optionNode );
                $optionNode = $doc->createElement( "option" );
                $optionNode->setAttribute( "id", $option['id'] );
                $optionNode->setAttribute( "option_id", $option['option_id'] );
                $optionNode->setAttribute( "value", $option['value'] );
                $optionNode->setAttribute( 'additional_price', $option['additional_price'] );
                $multioptionNode->appendChild( $optionNode );
            }
            $multiOptionsNode->appendChild( $multioptionNode );
        }
        $xml = $doc->saveXML();
        return $xml;
    }
125229
public function outputElement() : string
    {
        $this->selected = $this->getValue();
        $atts = [];
        foreach ($this->attributes as $name => $value) {
            $atts[] = $name . '="' . $value . '"';
        }
        $return = '<select ' . implode(' ', $atts) . '>' . "\n";
        if ($this->addBlank) {
            $disabled = (in_array('', $this->disabled)) ? ' disabled="disabled"' : '';
            $selected = ($this->selected == '') ? ' selected="selected"' : '';
            $return .= '<option value=""' . $disabled . $selected . '></option>' . "\n";
        }
        foreach ($this->options as $val=>$txt) {
            if (in_array($val, $this->hidden)) break;
            $disabled = (in_array($val, $this->disabled)) ? ' disabled="disabled"' : '';
            $selected = ($this->selected == $val) ? ' selected="selected"' : '';
            $return .= '<option value="' . $val . '"' . $disabled . $selected . '>' . $txt . '</option>' . "\n";
        }
        $return .= '</select>';
        return $return;
    }
1129336
public function getJavaScriptCode()
    {
        $fieldsJavaScriptCode = [];
        $formConfiguration = $this->getFormObject()->getConfiguration();

        foreach ($formConfiguration->getFields() as $field) {
            $fieldsJavaScriptCode[] = $this->processField($field);
        }

        $formName = GeneralUtility::quoteJSvalue($this->getFormObject()->getName());
        $fieldsJavaScriptCode = implode(CRLF, $fieldsJavaScriptCode);

        return <<<JS
(function() {
    Fz.Form.get(
        $formName,
        function(form) {
            var field = null;

$fieldsJavaScriptCode
        }
    );
})();
JS;
    }
1287061
protected function datepickerField()
	{
		if (isset($this->htmlOptions['options']))
		{
			$options = $this->htmlOptions['options'];
			unset($this->htmlOptions['options']);
		}

		if (isset($this->htmlOptions['events']))
		{
			$events = $this->htmlOptions['events'];
			unset($this->htmlOptions['events']);
		}

		echo $this->getLabel();
		echo $this->getPrepend();
		$this->widget('bootstrap.widgets.CiiDatePicker', array(
			'model' => $this->model,
			'attribute' => $this->attribute,
			'options' => isset($options) ? $options : array(),
			'events' => isset($events) ? $events : array(),
			'htmlOptions' => $this->htmlOptions,
		));
		echo $this->getAppend();
		echo $this->getError() . $this->getHint();
	}
146323
public function build_options() {
		$input = '';
		$value = $this->get_value();

		if ( ! empty( $this->struct['choices'] ) ) {
			foreach ( $this->struct['choices'] as $option_value => $option_label ) {
				$sel = null;
				if ( $option_value == $value ) {
					$sel = ' selected="selected"';
				}

				$input .= '<option value="' . esc_attr( $option_value ) . '"' . $sel . '>' . esc_html( $option_label ) . '</option>';
			}
		}

		return $input;
	}