184 lines
4.2 KiB
PHP
184 lines
4.2 KiB
PHP
<?php
|
||
|
||
/**
|
||
* Элемент формы
|
||
*/
|
||
class TField
|
||
{
|
||
public $hidden = false;
|
||
public $name;
|
||
public $label; // Метка поля
|
||
public $value; // Значение поля
|
||
public $type = ""; // Каждому типу элемента соответствует макрос TAL
|
||
public $error_msg = null;
|
||
public $default = null;
|
||
public $error = false;
|
||
public $require = false;
|
||
public $hint = null;
|
||
// Блоки (Убрать в отдельный класс!!!)
|
||
public $_title = array();
|
||
public $description = "";
|
||
public $alias = array();
|
||
|
||
public function __construct ($input)
|
||
{
|
||
$this->deafult = null;
|
||
if (isset($input['validate'])) {
|
||
$this->require = strpos($input['validate'], 'require') !== false;
|
||
}
|
||
// Инициализация свойст обьетка
|
||
foreach (array('label', 'name', 'type') as $name) {
|
||
$this->$name = $input[$name];
|
||
}
|
||
}
|
||
|
||
function setValue($value)
|
||
{
|
||
$this->value = $value;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Поле ввода Input
|
||
* @package core
|
||
*/
|
||
class TInput extends TField {
|
||
}
|
||
|
||
/**
|
||
* Выбор из одного элемента
|
||
*/
|
||
class TSelect1 extends TField
|
||
{
|
||
public $options = array ();
|
||
public function __construct ($input) {
|
||
parent::__construct($input);
|
||
$this->options = $input['options'];
|
||
}
|
||
|
||
function setValue($value)
|
||
{
|
||
// Установить selected у options
|
||
$this->value = $value;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Поле с датой
|
||
* @package core
|
||
*/
|
||
class TDate extends TField
|
||
{
|
||
}
|
||
|
||
/* *
|
||
* Текстовое поле
|
||
* @package core
|
||
*/
|
||
class TTextArea extends TField
|
||
{
|
||
}
|
||
|
||
/**
|
||
* Поле для ввода пароля
|
||
* @package core
|
||
*/
|
||
class TSecret extends TField
|
||
{
|
||
}
|
||
|
||
class TUpload extends TField
|
||
{
|
||
}
|
||
|
||
/**
|
||
* Форма для ввода
|
||
* @package core
|
||
*/
|
||
class Form_Form
|
||
{
|
||
public $field = array ();
|
||
public $action = "";
|
||
public $method = 'post';
|
||
protected $replace;
|
||
protected $before;
|
||
|
||
public function __construct ()
|
||
{
|
||
$this->constructor = array (
|
||
'input' => 'TInput',
|
||
'date' => 'TDate',
|
||
'textarea' => 'TTextArea',
|
||
'select' => 'TSelect',
|
||
'select1' => 'TSelect1',
|
||
'secret' => 'TSecret',
|
||
'upload' => 'TUpload'
|
||
);
|
||
}
|
||
|
||
/**
|
||
* Добавляет одно поле ввода на форму
|
||
*/
|
||
public function addField (array $init)
|
||
{
|
||
assert (isset($init['type']));
|
||
assert (isset($init['name']));
|
||
|
||
$constructor = $this->constructor[$init['type']];
|
||
$el = new $constructor ($init);
|
||
$el->type = $init['type'];
|
||
|
||
$this->field [$init['name']] = $el;
|
||
return $el;
|
||
}
|
||
|
||
/**
|
||
* Добавляет спсок полей для формы
|
||
* @param array $list
|
||
*/
|
||
public function addFieldList (array $list)
|
||
{
|
||
foreach ($list as $init) {
|
||
$this->addField ($init);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Устанавливает ошибки после проверки
|
||
*/
|
||
function setError (Validator $validator)
|
||
{
|
||
foreach ($validator->getErrorMsg() as $name => $error)
|
||
{
|
||
$this->field[$name]->error = true;
|
||
$this->field[$name]->error_msg = $error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Устанавливает значения из масива
|
||
*/
|
||
function setValues (Collection $request) {
|
||
foreach ($this->field as $key => $el) {
|
||
$value = $request->getRawData ($this->method, $key);
|
||
$this->field[$key]->setValue($value);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Заполняет форму данными из обьекта
|
||
* @param object $data
|
||
* @param array $schema Связь между элементами формы и свойствами обьекта
|
||
*/
|
||
public function fill ($data, array $schema)
|
||
{
|
||
foreach ($schema as $key => $value) {
|
||
$this->field [$value]->setValue($data->$value->getString ());
|
||
}
|
||
}
|
||
|
||
public function set($name, $value)
|
||
{
|
||
$this->field[$name]->setValue($value);
|
||
}
|
||
}
|