Система управления «Сайт PRO»
Версия 20240107

Класс Base

Объект $Field: Cms\Root\Form\Field\Base

Объект «Поле»

Исходный код
class Base { … }

Свойства

$n

$Base->n
Исходный код
    var $n;

$node

$Base->node
Исходный код
    var $node;

$name

$Base->name
Исходный код
    var $name;

$type

$Base->type
Исходный код
    var $type;

$label

$Base->label
Исходный код
    var $label;

$info

$Base->info
Исходный код
    var $info;

$hidden

$Base->hidden
Исходный код
    var $hidden;

$required

$Base->required
Исходный код
    var $required;

$readonly

$Base->readonly
Исходный код
    var $readonly;

$disabled

$Base->disabled
Исходный код
    var $disabled;

$placeholder

$Base->placeholder
Исходный код
    var $placeholder;

$match

$Base->match
Исходный код
    var $match;

$code

$Base->code
Исходный код
    var $code;

$options

$Base->options
Исходный код
    var $options;

$optionList

$Base->optionList
Исходный код
    var $optionList;

$default

$Base->default
Исходный код
    var $default;

$value

$Base->value
Исходный код
    var $value;

$json

$Base->json
Исходный код
    var $json;

$folder

$Base->folder
Исходный код
    var $folder;

$error

$Base->error
Исходный код
    var $error;

Методы

__construct()

new Base($node);

Конструктор

Исходный код
    function __construct($node) {
        if (is_array($node)) {
            foreach ($node as $attributeName => $attributeValue) {
                $this->set($attributeName, $attributeValue);
            }
        } elseif ($node instanceof \DOMElement) {
            /**
             * @if false
             * @var \Cms\Root\Form\Element $node
             * @endif
             */
            $this->node = $node;
            $node->field = $this;
            $this->parseAttributes($node->attributes);
            $this->parseChildren($node->childNodes);
        }
        if (strval($this->code) !== '') {
            $this->parseOptions($this->evalCode());
        }
    }

parseAttributes()

$Base->parseAttributes($attributes);
Исходный код
    function parseAttributes($attributes) {
        foreach ($attributes as $attributeName => $attribute) {
            $this->set($attributeName, $attribute->value);
        }
    }

parseChildren()

$Base->parseChildren($children);
Исходный код
    function parseChildren($children) {
        foreach ($children as $childNode) {
            if ($childNode->nodeType === XML_PI_NODE) {
                if (isset($this->code)) continue;
                if ($childNode->target === 'php') {
                    $this->set('code', $childNode->data);
                }
            } elseif ($childNode->nodeType === XML_TEXT_NODE) {
                if (isset($this->code)) continue;
                if (($text = trim($childNode->data)) !== '') {
                    $this->set('code', var_export($text, true));
                }
            } elseif ($childNode->nodeType === XML_ELEMENT_NODE) {
                if ($childNode->tagName === 'option') {
                    $this->addOption($childNode->getAttribute('value'), $childNode->textContent);
                } elseif ($childNode->tagName === 'optgroup') {
                    $group = $childNode->getAttribute('label');
                    $parent = $this->addOptionGroup($group, $childNode->attributes);
                    foreach ($childNode->childNodes as $childNode) {
                        if ($childNode->nodeType === XML_ELEMENT_NODE && $childNode->tagName === 'option') {
                            $this->addOption($childNode->getAttribute('value'), $childNode->textContent, $group, $parent);
                        }
                    }
                }
            }
        }
    }

parseOptions()

$Base->parseOptions($options);
Исходный код
    function parseOptions($options) {
        $hasKeys = null;
        if (is_scalar($options)) {
            $options = trim(strval($options));
            if ($options === '' || $options === '-') {
                return;
            } elseif ($options[0] === '{' && $options[strlen($options) - 1] === '}') {
                $options = json_decode($options, true);
                $hasKeys = true;
            } elseif ($options[0] === '[' && $options[strlen($options) - 1] === ']') {
                $options = json_decode($options, true);
                $hasKeys = false;
            } else {
                $options = explode('|', trim($options, '|'));
                $hasKeys = false;
            }
        } elseif (!is_array($options)) {
            return;
        }
        $group = $parent = null;
        foreach ($options as $key => $option) {
            if (is_array($option)) {
                if ($hasKeys === false || ($hasKeys === null && is_int($key) && (array_key_exists('value', $option) || array_key_exists('label', $option)))) {
                    if ($group === $option['group']) {
                        $this->addOption($option['value'], $option['label'], $group, $parent, $option);
                    } elseif (is_scalar($option['group'])) {
                        $group = $option['group'];
                        $parent = $this->addOptionGroup($group);
                        $this->addOption($option['value'], $option['label'], $group, $parent, $option);
                    } else {
                        $group = $parent = null;
                        $this->addOption($option['value'], $option['label'], $group, $parent, $option);
                    }
                } else {
                    if ($group !== $key) {
                        $group = $key;
                        $parent = $this->addOptionGroup($group);
                    }
                    foreach ($option as $key => $option) {
                        if (is_array($option)) {
                            $this->addOption($option['value'], $option['label'], $group, $parent, $option);
                        } else {
                            $this->addOption($key, $option, $group, $parent, $option);
                        }
                    }
                }
            } else {
                if ($hasKeys === false) {
                    $this->addOption(null, $option);
                } else {
                    $this->addOption($key, $option);
                }
                $group = $parent = null;
            }
        }
    }

isField()

$Base->isField();
Исходный код
    function isField() {
        if ($this->node instanceof \DOMElement) {
            return $this->node->tagName === 'field';
        }
    }

hasValue()

$Base->hasValue();
Исходный код
    function hasValue() {
        if (!empty($this->value)) return true;
        if (strval($this->value) !== '') return true;
        return false;
    }

set()

$Base->set($name, $value);
Исходный код
    function set($name, $value) {
        switch ($name) {
            case 'input-placeholder':
                $name = 'placeholder';
                break;
            case 'input-required':
                $name = 'required';
                break;
            case 'input-readonly':
                $name = 'readonly';
                break;
            case 'input-disabled':
                $name = 'disabled';
                break;
            case 'data-type':
                switch ($value) {
                    case 'json':
                        $name = 'json';
                        $value = true;
                        break;
                }
                break;
            case 'data-folder':
                $name = 'folder';
                break;
            case 'data-options':
                $this->parseOptions($value);
                return;
        }
        if (strpos($name, '-') !== false) return;
        $this->$name = $value;
    }

App()

$Base->App();
Исходный код
    function App() {
        return \Cms\Site\App::getInstance();
    }

root()

$Base->root();
Исходный код
    function root() {
        return $this->App()->root();
    }

Conf()

$Base->Conf();
Исходный код
    function Conf() {
        return $this->App()->Conf();
    }

Data()

$Base->Data();
Исходный код
    function Data() {
        return $this->App()->Data();
    }

Auth()

$Base->Auth();
Исходный код
    function Auth() {
        return $this->App()->Auth();
    }

Main()

$Base->Main();
Исходный код
    function Main() {
        return $this->App()->Main();
    }

Form()

$Base->Form();
Исходный код
    function Form() {
        return $this->App()->Form();
    }

Page()

$Base->Page();
Исходный код
    function Page() {
        if ($this->node instanceof \DOMElement && $this->node->ownerDocument instanceof \DOMDocument) {
            /**
             * @if false
             * @var Document $ownerDocument
             * @endif
             */
            $ownerDocument = $this->node->ownerDocument;
            return $ownerDocument->Page;
        }
    }

evalCode()

$Base->evalCode();
Исходный код
    function evalCode() {
        global $_RESULT; if (!is_array($_RESULT)) $_RESULT = array();
        $App = $this->App();
        $root = $this->root();
        $Conf = $this->Conf();
        $Data = $this->Data();
        $Auth = $this->Auth();
        $Main = $this->Main();
        $Form = $this->Form();
        $Page = $this->Page();
        $Request = $Main->Request();
        $Storage = $Main->Storage();
        $Modules = $Main->Modules();
        $Display = $Main->Display();
        $field = $this;
        return eval(strpos($this->code, 'return') === false ? 'return (' . $this->code . ');' : $this->code . ';');
    }

id()

$Base->id($suffix=null);
Исходный код
    function id($suffix = null) {
        $prefix = '';
        if ($this->node instanceof \DOMElement && $this->node->ownerDocument instanceof \DOMDocument) {
            /**
             * @if false
             * @var Document $ownerDocument
             * @endif
             */
            $ownerDocument = $this->node->ownerDocument;
            if (is_callable(array($ownerDocument, 'id'))) $prefix = $ownerDocument->id();
            if ($prefix === 'form') $prefix = '';
            elseif (is_scalar($prefix)) $prefix = $prefix . '-';
            else $prefix = '';
        }
        if (!$name = $this->name) {
            if (!$n = $this->n) $n = $this->n = ++ $this->Form()->n;
            $name = sprintf('field-%02d', $n);
        }
        return $prefix . $name . $suffix;
    }

qId()

$Base->qId($suffix=null);
Исходный код
    function qId($suffix = null) {
        return preg_replace('~([\\[\\]])~', '\\\\\\\\$1', $this->id($suffix));
    }

jId()

$Base->jId($suffix=null);
Исходный код
    function jId($suffix = null) {
        return json_encode($this->id($suffix), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
    }

lang()

$Base->lang();
Исходный код
    function lang() {
        return $this->Main()->lang;
    }

getAttribute()

$Base->getAttribute($name);
Исходный код
    function getAttribute($name) {
        return $this->node instanceof \DOMElement ? $this->node->getAttribute($name) : null;
    }

setAttribute()

$Base->setAttribute($name, $value);
Исходный код
    function setAttribute($name, $value) {
        if ($this->node instanceof \DOMElement) $this->node->setAttribute($name, $value);
        $this->set($name, $value);
    }

getAttributes()

$Base->getAttributes($prefix=null);
Исходный код
    function getAttributes($prefix = null) {
        $attributes = array();
        if ($this->node instanceof \DOMElement) {
            if ($length = strlen($prefix = strval($prefix))) {
                if ($prefix === 'field-') {
                    foreach ($this->node->attributes as $attributeName => $attribute) {
                        if (substr($attributeName, 0, $length) === $prefix && substr($attributeName, 0, 12) !== 'field-input-') {
                            $attributes[substr($attributeName, $length)] = strval($attribute->value);
                        }
                    }
                } elseif ($prefix === 'input-') {
                    if ($this->isField()) {
                        foreach ($this->node->attributes as $attributeName => $attribute) {
                            if (substr($attributeName, 0, $length) === $prefix) {
                                $attributes[substr($attributeName, $length)] = strval($attribute->value);
                            } elseif (substr($attributeName, 0, 5) === 'data-' && $attributeName !== 'data-options') {
                                $attributes[$attributeName] = strval($attribute->value);
                            } elseif (in_array($attributeName, array( 'accept', 'alt', 'autocapitalize', 'autocomplete', 'capture', 'dirname', 'height', 'inputmode', 'max', 'maxlength', 'min', 'minlenth', 'multiple', 'pattern', 'size', 'src', 'step', 'width' ), true)) {
                                $attributes[$attributeName] = strval($attribute->value);
                            }
                        }
                    } else {
                        foreach ($this->node->attributes as $attributeName => $attribute) {
                            if (substr($attributeName, 0, $length) === $prefix) {
                                $attributes[substr($attributeName, $length)] = strval($attribute->value);
                            } else {
                                $attributes[$attributeName] = strval($attribute->value);
                            }
                        }
                    }
                } else {
                    foreach ($this->node->attributes as $attributeName => $attribute) {
                        if (substr($attributeName, 0, $length) === $prefix) {
                            $attributes[substr($attributeName, $length)] = strval($attribute->value);
                        }
                    }
                }
            } else {
                foreach ($this->node->attributes as $attributeName => $attribute) {
                    $attributes[$attributeName] = strval($attribute->value);
                }
            }
        }
        return $attributes;
    }

addOptionGroup()

$Base->addOptionGroup($label, $attributes=null);
Исходный код
    function addOptionGroup($label, $attributes = null) {
        if (!is_array($this->options)) $this->options = array();
        if (!is_array($this->optionList)) $this->optionList = array();
        $label = strval($label);
        $optionItem = array(
            'label' => $label,
            'isGroup' => true,
            'optionList' => array(),
        );
        if ($attributes !== null) {
            $optionItem['attributes'] = $attributes;
        }
        $this->optionList[] = $optionItem;
        return array_key_last($this->optionList);
    }

addOption()

$Base->addOption($value, $label, $group=null, $parent=null, $attributes=null);
Исходный код
    function addOption($value, $label, $group = null, $parent = null, $attributes = null) {
        if (!is_array($this->options)) $this->options = array();
        if (!is_array($this->optionList)) $this->optionList = array();
        if ($value === null && $label === null) return;
        if ($value === null) $value = $label;
        if ($label === null) $label = $value;
        $value = strval($value);
        $label = strval($label);
        $optionItem = array(
            'value' => strval($value),
            'label' => strval($label),
        );
        if ($group !== null) {
            $optionItem['group'] = strval($group);
        }
        if ($attributes !== null) {
            $optionItem['attributes'] = $attributes;
        }
        if ($parent !== null) {
            $optionItem['parent'] = $parent;
            $this->optionList[$parent]['optionList'][] = $optionItem;
        } else {
            $this->optionList[] = $optionItem;
        }
        if (!isset($this->options[$value])) {
            $this->options[$value] = $label;
        }
    }

getDefaultFieldGridN()

$Base->getDefaultFieldGridN();
Исходный код
    function getDefaultFieldGridN() {
        return 1;
    }

getDefaultFieldGridS()

$Base->getDefaultFieldGridS();
Исходный код
    function getDefaultFieldGridS() {
        return 12;
    }

getDefaultLabelGridN()

$Base->getDefaultLabelGridN();
Исходный код
    function getDefaultLabelGridN() {
        return 1;
    }

getDefaultLabelGridS()

$Base->getDefaultLabelGridS();
Исходный код
    function getDefaultLabelGridS() {
        return 4;
    }

getDefaultInputGridN()

$Base->getDefaultInputGridN();
Исходный код
    function getDefaultInputGridN() {
        return 5;
    }

getDefaultInputGridS()

$Base->getDefaultInputGridS();
Исходный код
    function getDefaultInputGridS() {
        return 8;
    }

getFieldMargin()

$Base->getFieldMargin();
Исходный код
    function getFieldMargin() {
        if (!$this->isField()) return;
        if ($n = intval($this->getAttribute('field-grid-n'))) {
            if ($m = $n - $this->getDefaultFieldGridN()) {
                return $m;
            }
        }
    }

getFieldWidth()

$Base->getFieldWidth();
Исходный код
    function getFieldWidth() {
        if (!$this->isField()) return;
        if ($s = intval($this->getAttribute('field-grid-s'))) {
            if ($s !== $this->getDefaultFieldGridS()) {
                return $s;
            }
        }
    }

getLabelMargin()

$Base->getLabelMargin();
Исходный код
    function getLabelMargin() {
        if (!$this->isField()) return;
        if ($n = intval($this->getAttribute('label-grid-n'))) {
            if ($m = $n - $this->getDefaultLabelGridN()) {
                return $m;
            }
        }
    }

getLabelWidth()

$Base->getLabelWidth();
Исходный код
    function getLabelWidth() {
        if (!$this->isField()) return;
        if ($s = intval($this->getAttribute('label-grid-s'))) {
            if ($s !== $this->getDefaultLabelGridS()) {
                return $s;
            }
        }
    }

getInputMargin()

$Base->getInputMargin();
Исходный код
    function getInputMargin() {
        if (!$this->isField()) return;
        if ($n = $this->getInputWidth()) {
            if ($n >= 12) {
                return;
            }
        } elseif ($n = $this->getDefaultInputGridS()) {
            if ($n >= 12) {
                return;
            }
        }
        if ($this->hasLabel()) {
            $w = 0;
            if ($n = intval($this->getAttribute('label-grid-n'))) $w += $n;
            else $w += $this->getDefaultLabelGridN();
            if ($s = intval($this->getAttribute('label-grid-s'))) $w += $s;
            else $w += $this->getDefaultLabelGridS();
        } else {
            $w = 1;
        }
        if ($n = intval($this->getAttribute('input-grid-n'))) {
            if ($m = $n - $w) {
                return $m;
            }
        } else {
            if ($m = $this->getDefaultInputGridN() - $w) {
                return $m;
            }
        }
    }

getInputWidth()

$Base->getInputWidth();
Исходный код
    function getInputWidth() {
        if (!$this->isField()) return;
        if ($s = intval($this->getAttribute('input-grid-s'))) {
            if ($s !== $this->getDefaultInputGridS()) {
                return $s;
            }
        }
    }

hasLabel()

$Base->hasLabel();
Исходный код
    function hasLabel() {
        return strval($this->label) !== '' || strval($this->info) !== '';
    }

getFieldAttributes()

$Base->getFieldAttributes($attributes=array());
Исходный код
    function getFieldAttributes($attributes = array()) {
        return $this->mergeAttributes($attributes, $this->getAttributes('field-'));
    }

getLabelAttributes()

$Base->getLabelAttributes($attributes=array());
Исходный код
    function getLabelAttributes($attributes = array()) {
        return $this->mergeAttributes($attributes, $this->getAttributes('label-'));
    }

getButtonAttributes()

$Base->getButtonAttributes($attributes=array());
Исходный код
    function getButtonAttributes($attributes = array()) {
        return $this->mergeAttributes($attributes, $this->getAttributes('button-'));
    }

getInputAttributes()

$Base->getInputAttributes($attributes=array());
Исходный код
    function getInputAttributes($attributes = array()) {
        return $this->mergeAttributes($attributes, $this->getAttributes('input-'));
    }

getFieldInputAttributes()

$Base->getFieldInputAttributes($attributes=array());
Исходный код
    function getFieldInputAttributes($attributes = array()) {
        return $this->mergeAttributes($attributes, $this->getAttributes('field-input-'));
    }

mergeAttributes()

$Base->mergeAttributes($attributes=array(), $attributes2=array());
Исходный код
    function mergeAttributes($attributes = array(), $attributes2 = array()) {
        foreach ($attributes2 as $attributeName => $attributeValue) {
            if ($attributeName === 'class') {
                if (strval($attributeValue) !== '') {
                    if (strval($attributes['class']) !== '') $attributes['class'] .= ' ' . $attributeValue;
                    else $attributes['class'] = $attributeValue;
                }
            } else {
                $attributes[$attributeName] = $attributeValue;
            }
        }
        if (isset($attributes['grid-margin'])) {
            if (strval($attributes['class']) !== '') $attributes['class'] .= ' grid-margin-' . $attributes['grid-margin'];
            else $attributes['class'] = 'grid-margin-' . $attributes['grid-margin'];
            unset($attributes['grid-margin']);
        }
        if (isset($attributes['grid-width'])) {
            if (strval($attributes['class']) !== '') $attributes['class'] .= ' grid-width-' . $attributes['grid-width'];
            else $attributes['class'] = 'grid-width-' . $attributes['grid-width'];
            unset($attributes['grid-width']);
        }
        unset($attributes['grid-n'], $attributes['grid-s']);
        foreach ($attributes as $attributeName => $attributeValue) {
            if (is_string($attributeValue) && strpos($attributeValue, '"') !== false) {
                $attributes[$attributeName] = htmlspecialchars($attributeValue);
            }
        }
        return $attributes;
    }

request()

$Base->request();

Получение значения поля

Исходный код
    function request() {
        $name = strval($this->name);
        if ($name === '') return;
        if ($Page = $this->Page()) {
            $this->value = $Page->get($this->name);
            if (substr($name, 0, 5) === 'info-' && !is_array($this->value)) $this->value = strval($this->value);
            if ($Page->created || !isset($Page->id)) {
                if ($this->value === '') $this->value = null;
            }
        }
        if ($_POST || $_FILES) {
            $this->value = $_POST[$name];
            if (is_string($this->value)) {
                $this->value = trim(str_replace("\r", '', $this->value));
            }
        } elseif (isset($_GET[$name])) {
            $this->value = $_GET[$name];
            if (is_string($this->value)) {
                $this->value = trim(str_replace("\r", '', $this->value));
            }
        } elseif (!isset($this->value) && isset($this->default)) {
            $this->value = $this->default;
        }
    }

requestDate()

$Base->requestDate();
Исходный код
    function requestDate() {
        if (intval($this->value)) {
            $time = 0;
            if (!$time && preg_match('~\\b(\\d{1,2})[\\.,/](\\d{1,2})[\\.,/](\\d{2}(\\d{2})?)\\b~', $this->value, $m)) {
                if (strlen($m[3]) === 2) {
                    if ($m[3] < 50) $m[3] = 2000 + $m[3];
                    else $m[3] += 1900;
                }
                $time = strtotime(sprintf('%4d-%02d-%02d', $m[3], $m[2], $m[1]));
            }
            if (!$time && preg_match('~\\b(\\d{2}(\\d{2})?)[-/](\\d{1,2})[-/](\\d{1,2})\\b~', $this->value, $m)) {
                if (strlen($m[1]) === 2) {
                    if ($m[1] < 50) $m[1] += 2000;
                    else $m[1] += 1900;
                }
                $time = strtotime(sprintf('%4d-%02d-%02d', $m[1], $m[3], $m[4]));
            }
            if ($time) {
                $this->value = date('Y-m-d', $time);
            }
        } elseif ($this->value === 'now') {
            $this->value = date('Y-m-d');
        } else {
            $this->value = '';
        }
    }

requestTime()

$Base->requestTime();
Исходный код
    function requestTime() {
        if (intval($this->value)) {
            if (preg_match('~\\b(\\d{1,2}):(\\d{1,2})\\b~', $this->value, $m)) {
                $this->value = sprintf('%02d:%02d', $m[1], $m[2]);
            }
        } elseif ($this->value === 'now') {
            $this->value = date('H:i');
        } else {
            $this->value = '';
        }
    }

requestDateTime()

$Base->requestDateTime();
Исходный код
    function requestDateTime() {
        if (intval($this->value)) {
            $value = strtr($this->value, 'T', ' ');
            $time = 0;
            if (!$time && preg_match('~\\b(\\d{1,2})[\\.,/](\\d{1,2})[\\.,/](\\d{2}(\\d{2})?)\\b~', $value, $m)) {
                if (strlen($m[3]) === 2) {
                    if ($m[3] < 50) $m[3] = 2000 + $m[3];
                    else $m[3] += 1900;
                }
                $datePart = sprintf('%4d-%02d-%02d', $m[3], $m[2], $m[1]);
                $time = strtotime($datePart);
            }
            if (!$time && preg_match('~\\b(\\d{2}(\\d{2})?)[-/](\\d{1,2})[-/](\\d{1,2})\\b~', $value, $m)) {
                if (strlen($m[1]) === 2) {
                    if ($m[1] < 50) $m[1] += 2000;
                    else $m[1] += 1900;
                }
                $datePart = sprintf('%4d-%02d-%02d', $m[1], $m[3], $m[4]);
                $time = strtotime($datePart);
            }
            if ($time) {
                if (preg_match('~\\b(\\d{1,2}):(\\d{1,2})(:(\\d{1,2}))?\\b~', $value, $m)) {
                    $timePart = sprintf(' %02d:%02d:%02d', $m[1], $m[2], $m[3]);
                    $_time = strtotime($datePart . $timePart);
                    if ($_time > 0) $time = $_time;
                }
                $this->value = date('Y-m-d H:i:s', $time);
            }
        } elseif ($this->value === 'now') {
            $this->value = date('Y-m-d H:i:00');
        } else {
            $this->value = '';
        }
    }

requestMonth()

$Base->requestMonth();
Исходный код
    function requestMonth() {
        if (intval($this->value)) {
            if (preg_match('~\\b(\\d{4})-(\\d{1,2})\\b~', $this->value, $m)) {
                $this->value = sprintf('%4d-%02d', $m[1], $m[2]);
            }
        } elseif ($this->value === 'now') {
            $this->value = date('Y-m');
        } else {
            $this->value = '';
        }
    }

requestWeek()

$Base->requestWeek();
Исходный код
    function requestWeek() {
        if (intval($this->value)) {
            if (preg_match('~\\b(\\d{4})-W(\\d{1,2})\\b~', $this->value, $m)) {
                $this->value = sprintf('%4d-W%02d', $m[1], $m[2]);
            }
        } elseif ($this->value === 'now') {
            $this->value = date('Y-\\WW');
        } else {
            $this->value = '';
        }
    }

check()

$Base->check();

Проверка правильности ввода

Возвращает: string|null null – успешная проверка; string – ошибка

Исходный код
    function check() {
        if (!empty($e = $this->validateRequired())) return $e;
        if (!empty($e = $this->validatePattern())) return $e;
        if ($this->shouldValidateOptions()) {
            if (!empty($e = $this->validateOptions())) return $e;
        }
    }

validateRequired()

$Base->validateRequired();
Исходный код
    function validateRequired() {
        if ($this->required) {
            if (!$this->hasValue()) {
                return $this->error = $this->requiredError();
            }
        }
    }

validatePattern()

$Base->validatePattern();
Исходный код
    function validatePattern() {
        if ($this->hasValue() && is_scalar($this->value)) {
            if (strval($this->match) !== '') {
                if ($this->match === 'email') {
                    if (!$this->checkEmail($this->value)) {
                        return $this->error = $this->standardError();
                    }
                } else {
                    if (!preg_match($this->match, $this->value)) {
                        return $this->error = $this->standardError();
                    }
                }
            }
        }
    }

validateOptions()

$Base->validateOptions();
Исходный код
    function validateOptions() {
        if ($this->hasValue() && is_scalar($this->value)) {
            if (is_array($this->options)) {
                if (!isset($this->options[$this->value])) {
                    return $this->error = $this->standardError();
                }
            }
        }
    }

shouldValidateOptions()

$Base->shouldValidateOptions();
Исходный код
    function shouldValidateOptions() {
        return false;
    }

standardError()

$Base->standardError();
Исходный код
    function standardError() {
        return $this->Main()->translate('Некорректное значение для поля') . (strval($label = $this->getErrorLabel()) !== '' ? ' «' . $label . '»' : '');
    }

requiredError()

$Base->requiredError();
Исходный код
    function requiredError() {
        return $this->Main()->translate('Не заполнено обязательное поле') . (strval($label = $this->getErrorLabel()) !== '' ? ' «' . $label . '»' : '');
    }

arrayValue()

$Base->arrayValue();
Исходный код
    function arrayValue() {
        if (is_array($this->value)) {
            return $this->value;
        }
        if (is_scalar($this->value)) {
            $value = trim(strval($this->value));
            if ($value === '') {
                $arrayValue = array();
            } elseif ($value[0] === '[' && $value[strlen($value) - 1] === ']') {
                $arrayValue = json_decode($value, true);
            } elseif ($value[0] === '{' && $value[strlen($value) - 1] === '}') {
                $arrayValue = json_decode($value, true);
            } else {
                $arrayValue = array();
                foreach (explode('|', $value) as $value) {
                    if ($value !== '') {
                        $arrayValue[$value] = $value;
                    }
                }
            }
            return $arrayValue;
        }
        return array();
    }

stringValue()

$Base->stringValue();
Исходный код
    function stringValue() {
        if (is_array($this->value)) {
            if ($this->json) {
                return json_encode($this->value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
            } else {
                return implode('|', $this->stringArrayValue());
            }
        }
        return strval($this->value);
    }

stringArrayValue()

$Base->stringArrayValue();
Исходный код
    function stringArrayValue() {
        $r = array();
        foreach ($this->arrayValue() as $value) {
            if (is_array($value)) {
                $r[] = json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
            } else {
                $r[] = strval($value);
            }
        }
        return $r;
    }

arrayStringValue()

$Base->arrayStringValue();
Исходный код
    function arrayStringValue() {
        return $this->stringArrayValue();
    }

getErrorLabel()

$Base->getErrorLabel();
Исходный код
    function getErrorLabel() {
        $label = $this->label;
        if (strval($label) === '') $label = $this->getAttribute('placeholder');
        if (strval($label) === '') $label = $this->getAttribute('data-placeholder');
        if (strval($label) === '') $label = $this->getAttribute('input-placeholder');
        if (strval($label) === '') $label = $this->getAttribute('text');
        $label = strip_tags($label);
        return $label;
    }

checkEmail()

$Base->checkEmail($email);

Проверка правильности ввода адреса e-mail.

Возвращает: true (успешная проверка) или string (ошибка)

Исходный код
    function checkEmail($email) {
        return preg_match('~^[-_\\.a-z0-9]+@([a-z0-9][-a-z0-9]*)(\\.[a-z0-9][-a-z0-9]*)+$~i', $email);
    }

checkDate()

$Base->checkDate($value);
Исходный код
    function checkDate($value) {
        return preg_match('~^\\d\\d\\d\\d-\\d\\d-\\d\\d$~', $value);
    }

checkTime()

$Base->checkTime($value);
Исходный код
    function checkTime($value) {
        return preg_match('~^\\d\\d:\\d\\d$~', $value);
    }

checkDateTime()

$Base->checkDateTime($value);
Исходный код
    function checkDateTime($value) {
        return preg_match('~^\\d\\d\\d\\d-\\d\\d-\\d\\d \\d\\d:\\d\\d:\\d\\d$~', $value);
    }

checkMonth()

$Base->checkMonth($value);
Исходный код
    function checkMonth($value) {
        return preg_match('~^\\d\\d\\d\\d-\\d\\d$~', $value);
    }

checkWeek()

$Base->checkWeek($value);
Исходный код
    function checkWeek($value) {
        return preg_match('~^\\d\\d\\d\\d-W\\d\\d$~', $value);
    }

store()

$Base->store();

Сохранение значения поля

Исходный код
    function store() {
        if (($name = strval($this->name)) === '') return;
        if (!is_object($Page = $this->Page())) return;
        if ($Page->get('_old_' . $name) === null) {
            $Page->set('_old_' . $name, $Page->get($name));
        }
        $Page->set($name, $this->value);
    }

storeArray()

$Base->storeArray();
Исходный код
    function storeArray() {
        if (($name = strval($this->name)) === '') return;
        if (!is_object($Page = $this->Page())) return;
        if (substr($name, 0, 5) === 'info-') {
            $name = substr($name, 5);
            if ($Page->get('_old_info-' . $name) === null) {
                $Page->set('_old_info-' . $name, $Page->getInfo($name));
            }
            $Page->setInfo($name, $this->stringArrayValue());
        } else {
            if ($Page->get('_old_' . $name) === null) {
                $Page->set('_old_' . $name, $Page->get($name));
            }
            $Page->set($name, $this->stringValue());
        }
    }

getDefaultFolder()

$Base->getDefaultFolder();
Исходный код
    function getDefaultFolder() {
        return;
    }

getFolder()

$Base->getFolder();
Исходный код
    function getFolder() {
        if ($this->folder) {
            return $this->folder;
        }
        return $this->getDefaultFolder();
    }

display()

$Base->display();

Отображение поля

Исходный код
    function display() {
        $Display = $this->Main()->Display();
        $Page = $this->Page();
        if ($html = $Display->callTemplate('field', $this->type, $Page, array( 'Field' => $this, 'field' => $this ))) return $html;
        if ($html = $Display->callTemplate('field', 'default', $Page, array( 'Field' => $this, 'field' => $this ))) return $html;
    }

displayInput()

$Base->displayInput();
Исходный код
    function displayInput() {
        $Display = $this->Main()->Display();
        $Page = $this->Page();
        if ($html = $Display->callTemplate('input', $this->type, $Page, array( 'Field' => $this, 'field' => $this ))) return $html;
        if ($html = $Display->callTemplate('input', 'default', $Page, array( 'Field' => $this, 'field' => $this ))) return $html;
    }

export()

$Base->export();
Исходный код
    function export() {
        $r = array();
        if (preg_match('~^[-_a-zA-Z0-9]+$~', $this->name)) $r[] = $this->name;
        else return;
        if (preg_match('~^[^\\[\\]]+$~', $this->type)) $r[] = '[' . $this->type . ']';
        else return;
        if (strval($this->default) !== '') {
            if (preg_match('~^[^\\s\'\'""]+$~', $this->default)) {
                $r[] = '=' . $this->default;
            } elseif (strpos($this->default, '"') === false) {
                $r[] = '="' . $this->default . '"';
            } elseif (strpos($this->default, "'") === false) {
                $r[] = "='" . $this->default . "'";
            }
        }
        if (strval($this->label) !== '') {
            $r[] = str_replace(':', '&#58;', $this->label) . ':';
            if ($this->required) $r[] = '*';
        }
        if (strval($this->match) !== '') {
            $r[] = '~' . $this->match;
        }
        if (strval($this->code) !== '') {
            $r[] = '<' . '? ' . $this->code . ' ?' . '>';
        }
        if (strval($this->info) !== '') {
            $r[] = '# ' . $this->info;
        }
        return implode(' ', $r) . "\n";
    }

text()

$Base->text();
Исходный код
    function text() {
        $label = strval($this->label) !== '' ? $this->label : $this->name;
        $value = str_replace("\r", '', trim($this->stringValue()));
        if ($this->type === 'checkbox') $value = $value ? 'Да' : 'Нет';
        elseif (strval($value) === '') $value = '-';
        if (strpos($value, "\n") !== false) return "$label:\n$value\n\n";
        return "$label: $value\n";
    }

html()

$Base->html();
Исходный код
    function html() {
        $label = $this->label;
        if (strval($label) === '') $label = $this->getAttribute('input-placeholder');
        if (strval($label) === '') $label = $this->getAttribute('text');
        if (strval($label) === '') $label = $this->name;
        $value = str_replace("\r", '', trim($this->stringValue()));
        if (is_array($this->options)) {
            $values = array();
            foreach ($this->arrayValue() as $_value) if (strval($_value) !== '') {
                if (isset($this->options[$_value])) {
                    $_value = $this->options[$_value];
                } else {
                    foreach ($this->options as $option) {
                        if (is_array($option)) {
                            if (isset($option[$_value])) {
                                $_value = $option[$_value];
                                break;
                            }
                        }
                    }
                }
            }
            if ($values) {
                $value = implode('; ', $values);
            }
        }
        if ($this->type === 'checkbox') $value = $value ? 'Да' : 'Нет';
        elseif (strval($value) === '') $value = '-';
        return '<tr><td><b>' . $label . ':</b></td><td>' . nl2br(htmlspecialchars($value)) . '</td></tr>' . "\n";
    }

v()

$Base->v($value=null);
Исходный код
    function v($value = null) {
        if (!func_num_args()) {
            if (is_scalar($this->value)) {
                $value = strval($this->value);
            } else {
                $value = $this->stringValue();
            }
        } else {
            $value = strval($value);
        }
        return htmlspecialchars($value);
    }

cleanup()

$Base->cleanup();
Исходный код
    function cleanup() {
        return;
    }