Classes

File builder/form/field/AbstractFormFieldChoice.class.php

File builder/form/field/AbstractFormFieldChoice.class.php

  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 
<?php
/**
 * This class manage radio input fields.
 *
 * @package     Builder
 * @subpackage  Form\field
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Regis VIARRE <crowkait@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2016 10 24
 * @since       PHPBoost 2.0 - 2009 04 28
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
 * @contributor Arnaud GENET <elenwii@phpboost.com>
*/

abstract class AbstractFormFieldChoice extends AbstractFormField
{
    /**
     * @var FormFieldEnumOption[]
     */
    private $options = array();

    /**
     * Constructs a FormFieldChoice.
     * @param string $id Field id
     * @param string $label Field label
     * @param FormFieldEnumOption Default value
     * @param FormFieldEnumOption[] $options Enumeration of the possible values
     * @param string[] $field_options Map of the field options (this field has no specific option, there are only the inherited ones)
     * @param FormFieldConstraint List of the constraints
     */
    public function __construct($id, $label, $value, array $options, array $field_options = array(), array $constraints = array())
    {
        foreach ($options as $option)
        {
            $this->add_option($option);
        }
        parent::__construct($id, $label, $value, $field_options, $constraints);
        $this->set_value($value);
    }

    /**
     * @return FormFieldEnumOption[]
     */
    protected function get_options()
    {
        return $this->options;
    }

    /**
     * Adds an option for the field.
     * @param FormFieldEnumOption option The new option.
     */
    protected function add_option(FormFieldEnumOption $option)
    {
        $option->set_field($this);
        $this->options[] = $option;
    }

    /**
     * Sets the options of the field.
     * @param Array options The list of options.
     */
    public function set_options(Array $options)
    {
        $this->clear_options();
        foreach ($options as $option)
        {
            if ($option instanceof FormFieldEnumOption)
            {
                $this->add_option($option);
            }
        }
    }

    protected function clear_options()
    {
        $this->options = array();
    }

    /**
     * {@inheritdoc}
     */
    public function retrieve_value()
    {
        $request = AppContext::get_request();
        if ($request->has_parameter($this->get_html_id()))
        {
            $raw_value = $request->get_value($this->get_html_id());
            $option = $this->get_option($raw_value);
            if ($option !== null)
            {
                $this->set_value($option);
            }
        }
    }

    protected function get_option($raw_option)
    {
        foreach ($this->options as $option)
        {
            if ($option->get_raw_value() == $raw_option)
            {
                return $option;
            }
        }
        return null;
    }

    public function get_option_id($raw_option)
    {
        foreach ($this->options as $id => $option)
        {
            if ($option->get_raw_value() == $raw_option)
            {
                return $id;
            }
        }
        return null;
    }

    /**
     * {@inheritdoc}
     */
    public function set_value($value)
    {
        if (is_object($value))
        {
            parent::set_value($value);
        }
        else
        {
            parent::set_value($this->get_option($value));
        }
    }
}
?>