render () -> html, * $form->adjust ($scheme); * $form->input ($name, $type, $label, ); * $form->set($name, $value); * $form->get($name); -> value * $form->parse ($request), * $form->validate () -> boolean, * $form->values () -> pair[key] = value */ /** * Элемент формы * @package core */ class TField { protected $_value; // Форматированное значение поля var $label; // Метка поля var $rule = array ();// Правила для проверки поля var $value; // Форматированное Значение поля // var $default; // Значение по умолчанию var $error = false; // в XRule Правила для проверки значений var $error_msg = "Поле не может быть пустым"; var $type; // Каждому типу элемента соответствует макрос TAL public function __construct ($input) { // $this->deafult = null; $this->require = false; // Инициализация свойст обьетка foreach ($input as $key => $value) { $this->$key = $value; } } public function __toString () { return $this->value; } public function isValid ($name) { if ($this->require == true && empty($this->value)) { $this->error = true; return false; } $this->setValue ($this->value); return true; } // Добавить методы getString, setString ?? function setValue ($value) { $this->_value = $value; $this->value = $value; } function getValue () { return $this->_value; } } /** * Поле ввода Input * @package core */ class TInput extends TField { public function __construct ($input) { parent::__construct ($input); $this->setValue (""); } } // checkbox class TCheckbox extends TField { public $checked = false; public function __construct ($input) { parent::__construct ($input); $this->setValue (1); } function setValue ($value) { $this->_value = intval ($value); $this->value = 1; if ($this->_value == 1) $this->checked = true; else $this->checked = false; } } /** * Выбор из одного элемента */ class TSelect1 extends TField { var $options = array (); public function __construct ($input) { parent::__construct ($input); $this->setValue (0); } function setValue ($value) { $this->_value = $value; $this->value = $value; foreach ($this->options as $key => $o) { $this->options[$key]['selected'] = ($this->options[$key]['value'] == $this->_value); } } } class TSelectGroup extends TField { var $groups = array (); public function __construct ($input) { parent::__construct ($input); $this->setValue (0); } function setValue ($value) { $this->_value = $value; $this->value = $value; foreach ($this->groups as $gkey => $o) { foreach ($this->groups[$gkey]['options'] as $key => $v) { $this->groups[$gkey]['options'][$key]['selected'] = ($this->groups[$gkey]['options'][$key]['value'] == $this->_value); } } } } /** * Поле с датой * @package core */ class TDate extends TField { var $error_msg = "Неверный формат даты"; var $separator = "."; public function __construct ($input) { parent::__construct ($input); $this->setValue (time ()); } function isValid ($name) { $value = $this->value; if ($tmp = explode(".", $value, 3)) { if ($tmp[1] && $tmp[0] && $tmp[2]) { if (checkdate ($tmp[1], $tmp[0], $tmp[2])) { $this->setValue (mktime (0, 0, 0, $tmp[1], $tmp[0], $tmp[2])); return true; } } } $this->error = true; return false; } function setValue ($value) { $this->_value = $value; $this->value = date ("d.m.Y", $value); } } class TTime extends TField { var $error_msg = "Неверный формат времени"; public function __construct ($input) { parent::__construct ($input); $this->setValue (mktime(0, 0, 0, 11, 30, 1999)); } function isValid ($name) { $value = $this->value; if ($tmp = explode(":", $value, 2)) { if ($this->checktime ($tmp[0], $tmp[1])) { $this->setValue (mktime ($tmp[0], $tmp[1], 0, 0, 0, 0)); return true; } } $this->error = true; return false; } function checktime($hour, $minute) { if ($hour > -1 && $hour < 24 && $minute > -1 && $minute < 60) { return true; } } function setValue ($value) { $this->_value = $value; $this->value = date ("H:i", $value); } } /* * * Текстовое поле * @package core */ class TTextArea extends TField { public function __construct ($input) { parent::__construct ($input); $this->setValue (""); } } /** * Поле для ввода пароля * @package core */ class TSecret extends TField { public function __construct ($input) { parent::__construct ($input); $this->setValue (""); } } class TUpload extends TField { public $types = array (); public function __construct ($input) { parent::__construct ($input); $this->setValue (""); } public function setValue ($value) { $this->_value = basename ($value); $this->value = $value; } } /** * Форма для ввода * @package core */ class TForm { var $field = array (); var $action = ""; var $method = 'post'; var $request; var $replace; public function __construct ($request) { $this->uid = get_form_uid (); $this->constructor = array ( 'input' => 'TInput', 'checkbox' => 'TCheckbox', 'date' => 'TDate', 'time' => 'TTime', 'textarea' => 'TTextArea', 'select' => 'TSelect', 'select1' => 'TSelect1', 'selgroup' => 'TSelectGroup', 'secret' => 'TSecret', 'upload' => 'TUpload' ); $this->request = $request; } function get ($name) { return $this->field [$name]->getValue (); } function addFieldObject ($name, $el) { $this->field [$name] = $el; } /** * Метод должен проверять значения полей формы полсле заполнения * Проверка правильности заполнения формы и установка значений */ function isValid () { $haveErrors = false; foreach ($this->field as $name => $el) { // ссылка if ($this->field [$name] instanceof TUpload) { // print_r ($_POST); $filename = $this->request->getRawData ('files', $name); if ((bool) $filename['name']) { $this->field [$name]->value = $filename['name']; } else { $this->field [$name]->value = $this->request->getRawData ($this->method, $name."_file"); } } else { $this->field [$name]->value = $this->request->getRawData ($this->method, $name); } if (!$this->field [$name]->isValid($name)) { $haveErrors = true; } } return !$haveErrors; } /** * Добавляет одно поле ввода на форму */ public function addField ($init) { assert ($init['type']); assert ($init['name']); $constructor = $this->constructor[$init['type']]; $el = new $constructor ($init); $el->type = $init['type']; $this->addFieldObject ($init['name'], $el); return $el; } /** * Добавляет спсок полей для формы * @param array $list */ public function addFieldList ($list) { foreach ($list as $init) { $this->addField ($init); } } /** * Заполняет форму данными из коллекции * Для обьектов и массивов можно использовать Adapter pattern * @param object $data * @param array $schema Связь между элементами формы и свойствами обьекта */ public function fill ($data) { foreach ($this->field as $name => $el) { $this->field [$name]->setValue ($data->get ($name)); } } } ?>