Classes

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

File builder/form/field/FormFieldCheckbox.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: 
<?php
/**
 * The class FormCheckBox represents a checkbox field in a form. It corresponds to a boolean.
 * @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 3.0 - 2009 04 28
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
 * @contributor Arnaud GENET <elenwii@phpboost.com>
*/

class FormFieldCheckbox extends AbstractFormField
{
    const CHECKED = true;
    const UNCHECKED = false;

    /**
     * Constructs a FormFieldCheckbox.
     * @param string $id Field identifier
     * @param string $label Field label
     * @param bool $checked FormFieldCheckbox::CHECKED if it's checked by default or FormFieldCheckbox::UNCHECKED if not checked.
     * @param string[] $field_options Map containing the options
     * @param FormFieldConstraint[] $constraints The constraints checked during the validation
     */
    public function __construct($id, $label, $checked = self::UNCHECKED, array $field_options = array(), array $constraints = array())
    {
        parent::__construct($id, $label, $checked, $field_options, $constraints);
        $this->set_css_form_field_class('form-field-checkbox');
    }

    /**
     * {@inheritdoc}
     */
    public function display()
    {
        $template = $this->get_template_to_use();

        $this->assign_common_template_variables($template);

        $template->put_all(array(
            'C_REQUIRED_AND_HAS_VALUE' => $this->is_required() && $this->get_value(),
            'C_CHECKED' => $this->is_checked()
        ));

        return $template;
    }

    /**
     * Tells whether the checkbox is checked
     * @return bool
     */
    public function is_checked()
    {
        return $this->get_value() == self::CHECKED;
    }

    /**
     * {@inheritdoc}
     */
    public function retrieve_value()
    {
        $request = AppContext::get_request();
        if ($request->has_parameter($this->get_html_id()))
        {
            $this->set_value((int)$request->get_value($this->get_html_id()) == 'on');
        }
        else
        {
            $this->set_value(0);
        }
    }

    protected function get_default_template()
    {
        return new FileTemplate('framework/builder/form/FormFieldCheckbox.tpl');
    }
}
?>