Classes

File builder/form/HTMLForm.class.php

File builder/form/HTMLForm.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: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 
<?php
/**
 * This class enables you to handle all the operations regarding forms. Indeed, you build a
 * form using object components (fieldsets, fields, buttons) and it's able to display, to retrieve
 * the posted values and also validate the entered data from constraints you define. The validation
 * is done in PHP when the form is received, but also in live thanks to Javascript (each field is
 * validated when it looses the focus and the whole form is validated when the user submits it).
 * @package     Builder
 * @subpackage  Form
 * @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: 2018 11 28
 * @since       PHPBoost 3.0 - 2009 04 28
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
 * @contributor Arnaud GENET <elenwii@phpboost.com>
*/

class HTMLForm implements HTMLElement
{
    const HTTP_METHOD_POST = 'post';
    const HTTP_METHOD_GET = 'get';

    const SMALL_CSS_CLASS = 'fieldset-mini';
    const NORMAL_CSS_CLASS = 'fieldset-content';

    private static $instance_id = 0;

    /**
     * @var FormConstraint[]
     */
    private $constraints = array();
    /**
     * @var FormFieldset[]
     */
    private $fieldsets = array();
    /**
     * @var FormButton[]
     */
    private $buttons = array();
    /**
     * @var string
     */
    private $html_id = '';
    /**
     * @var string
     */
    private $target = '';
    /**
     * @var string
     */
    private $method = self::HTTP_METHOD_POST;
    /**
     * @var string
     */
    private $css_class = self::NORMAL_CSS_CLASS;
    /**
     * @var boolean
     */
    private static $js_already_included = false;
    /**
     * @var string[]
     */
    private $validation_error_messages = array();
    /**
     * @var Template
     */
    private $template = null;

    private $enable_captcha_protection;

    /**
     * Constructs a HTMLForm object
     * @param string $html_id The HTML name of the form
     * @param string $target The url where the form sends data
     * @param bool $enable_captcha_protection True if the CAPTCHA is enabled
     */
    public function __construct($html_id, $target = '', $enable_captcha_protection = true)
    {
        $this->enable_captcha_protection = $enable_captcha_protection;
        $this->set_html_id($html_id);
        $this->set_target($target);

        if ($this->enable_captcha_protection)
            $this->add_catpcha_protection(); //Add captcha protection for visitor

        self::$instance_id++;
    }

    private function add_catpcha_protection()
    {
        $captcha_protection_fieldset = new FormFieldsetHTML('captcha_fieldset');
        $captcha_protection_fieldset->add_field(new FormFieldCaptcha());
        $this->add_fieldset($captcha_protection_fieldset);
    }

    public function move_captcha_protection_in_last_position()
    {
        try {
            $this->fieldsets[] = $this->fieldsets[0];
            unset($this->fieldsets[0]);
        } catch (FormBuilderException $e) {
        }
    }

    /**
     * Adds fieldset in the form
     * @param FormFieldset The fieldset to add
     */
    public function add_fieldset(FormFieldset $fieldset)
    {
        $fieldset->set_form_id($this->html_id);
        $this->fieldsets[] = $fieldset;
    }

    /**
     * Adds a constraint on the form. This kind of constraints are rules regarding several fields.
     * @param FormConstraint $constraint The constraint to add
     */
    public function add_constraint(FormConstraint $constraint)
    {
        $this->constraints[] = $constraint;
    }

    /**
     * Adds a button to the form
     * @param FormButton $button The button to add
     */
    public function add_button(FormButton $button)
    {
        $button->set_form_id($this->html_id);
        $this->buttons[] = $button;
    }

    /**
     * Returns the value of a form field.
     * @param string $field_id The HTML id of the field and string $default_value The default value
     * @return mixed The value of the field (the type depends of the field)
     * @throws FormBuilderException
     */
    public function get_value($field_id, $default_value = null)
    {
        try {
            $field = $this->get_field_by_id($field_id);

            if ($field->is_disabled() && $default_value !== null)
            {
                $field->set_value($default_value);
            }
            else if ($field->is_disabled() && $default_value == null)
            {
                throw new FormBuilderDisabledFieldException($field->get_id(), $field->get_value());
            }
            return $field->get_value();
        } catch (FormBuilderException $e) {
            return $default_value;
        }
    }

    /**
     * Returns true if field is disabled
     * @param string $field_id The HTML id of the field and string $default_value The default value
     * @return Boolean true if field is disabled
     */
    public function field_is_disabled($field_id)
    {
        $field = $this->get_field_by_id($field_id);
        if ($field->is_disabled())
        {
            return true;
        }
        return false;
    }

    /**
     * Returns true if the $field_id is in the form.
     * @param string $field_id The HTML id of the field
     * @return mixed true if the $field_id is in the form, false otherwise
     */
    public function has_field($field_id) {
        try {
            $this->field_is_disabled($field_id);
        } catch (FormBuilderException $ex) {
            return false;
        }
        return true;
    }

    public function get_field_by_id($field_id)
    {
        foreach ($this->fieldsets as $fieldset)
        {
            if ($fieldset->has_field($field_id))
            {
                return $fieldset->get_field($field_id);
            }
        }
        throw new FormBuilderException('The field "' . $field_id .
            '" doesn\'t exists in the "' . $this->html_id . '" form');
    }

    private function get_fieldset_by_id($fieldset_id)
    {
        foreach ($this->fieldsets as $fieldset)
        {
            if ($fieldset->get_id() == $fieldset_id)
            {
                return $fieldset;
            }
        }
        throw new FormBuilderException('The fieldset "' . $fieldset_id .
            '" doesn\'t exists in the "' . $this->html_id . '" form');
    }

    /**
     * Displays the form
     * @return Template The template containing all the form elements which is ready to be displayed.
     */
    public function display()
    {
        if ($this->enable_captcha_protection)
            $this->move_captcha_protection_in_last_position();

        $template = $this->get_template_to_use();

        $template->put_all(array(
            'C_JS_NOT_ALREADY_INCLUDED' => !self::$js_already_included,
            'C_HAS_REQUIRED_FIELDS' => (self::$instance_id == 1) ? $this->has_required_fields() : false,
            'C_TARGET' => !empty($this->target),
            'FORMCLASS' => $this->css_class,
            'TARGET' => $this->target,
            'HTML_ID' => $this->html_id,
            'L_REQUIRED_FIELDS' => LangLoader::get_message('form.explain_required_fields', 'status-messages-common'),
            'C_VALIDATION_ERROR' => count($this->validation_error_messages),
            'TITLE_VALIDATION_ERROR_MESSAGE' => LangLoader::get_message('form.validation_error', 'status-messages-common'),
            'METHOD' => $this->method
        ));

        foreach ($this->validation_error_messages as $error_message)
        {
            if (!empty($error_message))
            {
                $template->assign_block_vars('validation_error_messages', array(
                    'ERROR_MESSAGE' => $error_message
                ));
            }
        }

        self::$js_already_included = true;

        foreach ($this->fieldsets as $fieldset)
        {
            $template->assign_block_vars('fieldsets', array(), array(
                'FIELDSET' => $fieldset->display()
            ));

            //Onsubmit constraints
            foreach ($fieldset->get_onsubmit_validations() as $constraints)
            {
                foreach ($constraints as $constraint)
                {
                    $template->assign_block_vars('check_constraints', array(
                        'ONSUBMIT_CONSTRAINTS' => $constraint
                    ));
                }
            }
        }

        if (count($this->buttons) > 0)
        {
            $buttons_fieldset = new FormFieldsetSubmit('fbuttons');
            $buttons_fieldset->set_form_id($this->html_id);
            foreach ($this->buttons as $button)
            {
                $buttons_fieldset->add_element($button);
            }
            $template->assign_block_vars('fieldsets', array(), array(
                'FIELDSET' => $buttons_fieldset->display()
            ));
        }

        return $template;
    }

    /**
     * @return Template
     */
    private function get_template_to_use()
    {
        if ($this->template !== null)
        {
            return $this->template;
        }
        else
        {
            return new FileTemplate('framework/builder/form/Form.tpl');
        }
    }

    private function has_required_fields()
    {
        foreach ($this->fieldsets as $fieldset)
        {
            foreach($fieldset->get_fields() as $field)
            {
                if ($field->is_required())
                {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Validates the form from all its constraints. If the constraints are satisfied, the
     * validation errors will be displayed at the top of the form.
     * @return boolean true if the form is valid, false otherwise
     */
    public function validate()
    {
        $this->handle_disabled_fields();

        $validation_result = true;

        foreach ($this->fieldsets as $fieldset)
        {
            if (!$fieldset->validate())
            {
                $validation_error_message = $fieldset->get_validation_error_messages();
                if (!empty($validation_error_message))
                {
                    $this->validation_error_messages = array_merge($this->validation_error_messages, (array)$validation_error_message);
                }
                $validation_result = false;
            }
        }
        return $validation_result;
    }

    /**
     * Sets the form's HTML id
     * @param string $html_id the HTML id
     */
    public function set_html_id($html_id)
    {
        $this->html_id = $html_id;
    }

    public function get_html_id()
    {
        return $this->html_id;
    }

    /**
     * Sets the form's target
     * @param string $target The URL at which the form will be submited
     */
    public function set_target($target)
    {
        if ($target instanceof Url)
        {
            $this->target = $target->rel();
        }
        else
        {
            $this->target = $target;
        }
    }

    /**
     * Sets the form's CSS class
     * @param string $css_class The CSS class (see the HTMLForm::SMALL_CSS_CLASS and
     * HTMLForm::NORMAL_CSS_CLASS constants)
     */
    public function set_css_class($css_class)
    {
        $this->css_class = $css_class;
    }

    /**
     * Sets the HTTP method with which the form will be submited
     * @param string $method The method name (HTMLForm::HTTP_METHOD_POST or HTMLForm::HTTP_METHOD_POST).
     */
    public function set_method($method)
    {
        if ($method == self::HTTP_METHOD_POST)
        {
            $this->method = self::HTTP_METHOD_POST;
        }
        else
        {
            $this->method = self::HTTP_METHOD_GET;
        }
    }

    /**
     * Sets the template to use to display the form. If this method is not called,
     * a default template will be used (<code>/template/default/framework/builder/form/Form.tpl</code>).
     * @param Template $template The template to use
     */
    public function set_template(Template $template)
    {
        $this->template = $template;
    }

    private function handle_disabled_fields()
    {
        $this->enable_all_fields();
        $request = AppContext::get_request();

        $disabled_fieldsets_str = $request->get_string($this->html_id . '_disabled_fieldsets', '');
        $disabled_fieldsets_str = trim($disabled_fieldsets_str, '|');
        if ($disabled_fieldsets_str != '')
        {
            $disabled_fieldsets = explode('|', $disabled_fieldsets_str);
            foreach ($disabled_fieldsets as $fieldset_id)
            {
                try {
                    $fieldset = $this->get_fieldset_by_id(str_replace($this->html_id . '_', '', $fieldset_id));
                    $fieldset->disable();
                } catch (FormBuilderException $e) {
                }
            }
        }

        $disabled_fields_str = $request->get_string($this->html_id . '_disabled_fields');
        $disabled_fields_str = trim($disabled_fields_str, '|');
        if ($disabled_fields_str != '')
        {
            $disabled_fields = explode('|', $disabled_fields_str);
            foreach ($disabled_fields as $field_id)
            {
                try {
                    $field = $this->get_field_by_id(str_replace($this->html_id . '_', '', $field_id));
                    $field->disable();
                } catch (FormBuilderException $e) {
                }
            }
        }
    }

    private function enable_all_fields()
    {
        foreach ($this->fieldsets as $fieldset)
        {
            $fieldset->enable();
        }
    }
}
?>
Annonces