<?php

/**
 *
 * Cube Framework $Id$ GMkcZk/MWdcfItv/ExaALlToUS8v9wndJJ+qidS5/U0=
 *
 * @link        http://codecu.be/framework
 * @copyright   Copyright (c) 2017 CodeCube SRL
 * @license     http://codecu.be/framework/license Commercial License
 *
 * @version     1.10 [rev.1.10.01]
 */
/**
 * select form element generator class
 * apply translator to select options
 */

namespace Cube\Form\Element;

use Cube\Form\Element;

class Select extends Element
{

    /**
     *
     * type of element - override the variable from the parent class
     *
     * @var string
     */
    protected $_element = 'select';

    /**
     *
     * hide default value
     *
     * @var bool
     */
    protected $_hideDefault = false;

    /**
     *
     * class constructor
     *
     * @param string $name
     */
    public function __construct($name)
    {
        parent::__construct($this->_element, $name);
    }

    /**
     *
     * get hide default option
     *
     * @return string
     */
    public function isHideDefault()
    {
        return $this->_hideDefault;
    }

    /**
     *
     * set hide default option
     *
     * @param boolean $default
     *
     * @return $this
     */
    public function setHideDefault($default = true)
    {
        $this->_hideDefault = (bool)$default;

        return $this;
    }

    /**
     *
     * render the form element
     *
     * @return string
     */
    public function render()
    {
        $output = null;
        $brackets = null;
        $value = $this->getValue();

        $translate = $this->getTranslate();

        $multipleAttribute = $this->getAttribute('multiple');

        if ($this->getMultiple() === true || !empty($multipleAttribute)) {
            $brackets = $this->getBrackets();

            if (!empty($multipleAttribute)) {
                $output .= '<input type="hidden" name="' . $this->_name . '" value=""'
                    . $this->_endTag;
            }
        }

        $output .=
            $this->getPrefix()
            . ' <select name="' . $this->_name . $brackets . '" '
            . $this->renderAttributes() . '>';

        $required = $this->getRequired();
        $hideDefault = $this->isHideDefault();

        if ($required && !$hideDefault) {
            $output .= '<option value="" selected>' . $translate->_('-- select --') . '</option>';
        }

        $output .= '<optgroup disabled hidden></optgroup>';

        foreach ((array)$this->_multiOptions as $key => $option) {
            $selected = (in_array($key, (array)$value)) ? 'selected' : '';

            if (is_array($option)) {
                $title = isset($option[0]) ? $option[0] : null;

                if (isset($option[1])) {
                    $this->addMultiOptionAttributes($key, $option[1]);
                }
            }
            else {
                $title = $option;
            }

            $attributes = array(
                'value="' . $key . '"',
                $this->renderOptionAttributes($key),
                $selected
            );

            $output .= '<option ' . implode(' ', array_filter($attributes)) . '>'
                . $translate->_($title) . '</option>';
        }

        $output .= '</select> '
            . $this->getSuffix();

        return $output;
    }

}

