<commit_before><?php

namespace o;

class u_Form extends StdModule {

    private $isOpen = false;
    private $wasOpen = false;
    private $isFileUpload = false;

    private $data = [];
    private $schema = [];
    private $formId = '';
    private $fillData = [];

    // TODO: populate validator JS
    private $strings = [
        'noscript' => 'Please turn on JavaScript to use this form.',
        'optional' => 'optional',
        'firstSelectOption' => 'Select...'
    ];

    function __construct($schema, $formId) {

        $this->schema = uv($schema);
        $this->formId = $formId;

        $web = Tht::module('Web');

        $rules = [];
        foreach ($this->schema as $k => $v) {
            $rules[$k] = isset($v['rule']) ? $v['rule'] : '';
        }
        $web->u_reader($rules, $formId);
    }



    // Data getters

    function u_ok () {

        $web = Tht::module('Web');
        if ($web->u_request()['method'] === 'post') {
            return Security::validateCsrfToken();
        }
        return false;
    }

    function u_data($fieldName=null) {

        if (!$this->u_ok()) {
            return is_null($fieldName) ? OMap::create([]) : '';
        }

        if (!is_null($fieldName)) {
            if (!isset($this->schema[$fieldName])) {
                Tht::error("Unknown form field name: `" . $fieldName . "`");
            }
            return $this->getFieldData($fieldName);
        }
        else {
            $data = [];
            foreach ($this->schema as $name => $s) {
                $data[$name] = $this->getFieldData($name);
            }
            return OMap::create($data);
        }
    }

    function getFieldData($name) {
        $val = Tht::getPhpGlobal('post', $name);
        $schema = $this->schema[$name];    
    
        if ($schema['type'] == 'password') {
            return Security::createPassword($val);
        }

        return $val;
    }


    // Form construction

    function u_schema() {
        return v($this->schema);
    }

    function u_fill($data) {
        // TODO: validate map
        $this->fillData = $data;
    }

    function u_strings($map) {<commit_msg>rename fill to setFields<commit_after><?php

namespace o;

class u_Form extends StdModule {

    private $isOpen = false;
    private $wasOpen = false;
    private $isFileUpload = false;

    private $data = [];
    private $schema = [];
    private $formId = '';
    private $fillData = [];

    // TODO: populate validator JS
    private $strings = [
        'noscript' => 'Please turn on JavaScript to use this form.',
        'optional' => 'optional',
        'firstSelectOption' => 'Select...'
    ];

    function __construct($schema, $formId) {

        $this->schema = uv($schema);
        $this->formId = $formId;

        $web = Tht::module('Web');

        $rules = [];
        foreach ($this->schema as $k => $v) {
            $rules[$k] = isset($v['rule']) ? $v['rule'] : '';
        }
        $web->u_reader($rules, $formId);
    }



    // Data getters

    function u_ok () {

        $web = Tht::module('Web');
        if ($web->u_request()['method'] === 'post') {
            return Security::validateCsrfToken();
        }
        return false;
    }

    function u_data($fieldName=null) {

        if (!$this->u_ok()) {
            return is_null($fieldName) ? OMap::create([]) : '';
        }

        if (!is_null($fieldName)) {
            if (!isset($this->schema[$fieldName])) {
                Tht::error("Unknown form field name: `" . $fieldName . "`");
            }
            return $this->getFieldData($fieldName);
        }
        else {
            $data = [];
            foreach ($this->schema as $name => $s) {
                $data[$name] = $this->getFieldData($name);
            }
            return OMap::create($data);
        }
    }

    function getFieldData($name) {
        $val = Tht::getPhpGlobal('post', $name);
        $schema = $this->schema[$name];    
    
        if ($schema['type'] == 'password') {
            return Security::createPassword($val);
        }

        return $val;
    }


    // Form construction

    function u_schema() {
        return v($this->schema);
    }

    function u_set_fields($data) {
        // TODO: validate map
        $this->fillData = $data;
    }

    function u_strings($map) {
{'fraction_non_alphanumeric': 0.1701637076320037, 'fraction_numerical': 0.0, 'mean_word_length': 1.8464566929133859, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '739575', 'n_tokens_mistral': 1329, 'n_tokens_neox': 1279, 'n_words': 271}