Версия полностью совместимая c CMS
This commit is contained in:
parent
7ce493414e
commit
75bb35d5bf
21 changed files with 1404 additions and 783 deletions
|
|
@ -20,68 +20,147 @@ class TField
|
|||
public $description = "";
|
||||
public $alias = array();
|
||||
|
||||
public function __construct ($input)
|
||||
public function __construct ($input = array(), $factory = null)
|
||||
{
|
||||
$this->deafult = null;
|
||||
|
||||
$this->default = null;
|
||||
if (isset($input['validate'])) {
|
||||
$this->require = strpos($input['validate'], 'require') !== false;
|
||||
$this->require = strpos($input['validate'], 'require') !== false;
|
||||
}
|
||||
// Инициализация свойст обьетка
|
||||
foreach (array('label', 'name', 'type') as $name) {
|
||||
$this->$name = $input[$name];
|
||||
foreach (array('label', 'name', 'type', 'description') as $name) {
|
||||
if (isset($input[$name])) {
|
||||
$this->$name = $input[$name];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function setValue($value)
|
||||
function setValue(/*.any.*/$value)
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
function getId()
|
||||
{
|
||||
return $this->name . '_label';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Поле ввода Input
|
||||
* @package core
|
||||
*/
|
||||
class TInput extends TField {
|
||||
}
|
||||
|
||||
class TCheckbox extends TField
|
||||
{
|
||||
public $checked = false;
|
||||
function setValue($value)
|
||||
{
|
||||
$this->value = $value;
|
||||
$this->checked = $value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class TSelect extends TField
|
||||
{
|
||||
public $options = array();
|
||||
|
||||
public function __construct ($input, $factory) {
|
||||
parent::__construct($input, $factory);
|
||||
|
||||
if ($factory != null) {
|
||||
$factory->create($this, $input);
|
||||
} else if (isset($input['options.pair'])) {
|
||||
$this->options = $this->optionsPair($input['options.pair']);
|
||||
} else if (isset($input['options'])) {
|
||||
$this->options = $input['options'];
|
||||
}
|
||||
|
||||
foreach ($this->options as &$option) {
|
||||
$option['selected'] = false;
|
||||
$option['class'] = (isset($option['class'])) ? $option['class'] : false;
|
||||
}
|
||||
}
|
||||
|
||||
public function optionsPair($list, $selected = false) {
|
||||
$result = array();
|
||||
foreach ($list as $key => $value) {
|
||||
$result [] = array('value' => $key, 'name' => $value, 'selected' => $key == $selected);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Выбор из одного элемента
|
||||
*/
|
||||
class TSelect1 extends TField
|
||||
class TSelectOne extends TSelect
|
||||
{
|
||||
public $options = array ();
|
||||
public function __construct ($input) {
|
||||
parent::__construct($input);
|
||||
$this->options = $input['options'];
|
||||
}
|
||||
|
||||
function setValue($value)
|
||||
{
|
||||
// Установить selected у options
|
||||
$this->value = $value;
|
||||
foreach ($this->options as &$option) {
|
||||
$option['selected'] = ($option['value'] == $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class TSelectMany extends TSelect
|
||||
{
|
||||
function setValue($value)
|
||||
{
|
||||
// Установить selected у options
|
||||
if (!is_array($value)) { $value = array($value); }
|
||||
$this->value = $value;
|
||||
foreach ($this->options as &$option) {
|
||||
$option['selected'] = (in_array($option['value'], $value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class TQuestionType extends TSelect
|
||||
{
|
||||
function setValue($value)
|
||||
{
|
||||
// Установить selected у options
|
||||
$this->value = $value;
|
||||
foreach ($this->options as &$option) {
|
||||
$option['selected'] = ($option['value'] == $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Поле с датой
|
||||
* @package core
|
||||
*/
|
||||
class TDate extends TField
|
||||
{
|
||||
}
|
||||
|
||||
/* *
|
||||
/**
|
||||
* Поле с цветом
|
||||
*/
|
||||
class TColor extends TField
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Текстовое поле
|
||||
* @package core
|
||||
*/
|
||||
class TTextArea extends TField
|
||||
{
|
||||
function setValue($value)
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Поле для ввода пароля
|
||||
* @package core
|
||||
*/
|
||||
class TSecret extends TField
|
||||
{
|
||||
|
|
@ -91,62 +170,181 @@ class TUpload extends TField
|
|||
{
|
||||
}
|
||||
|
||||
class THidden extends TInput {
|
||||
public $hidden = true;
|
||||
}
|
||||
|
||||
class TComponentBrowserInput extends TInput
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* При рендеринге каждому классу соответствует шаблон (см. themes/maxim/templates/macros.html)
|
||||
*/
|
||||
class TDateTime extends TInput {
|
||||
}
|
||||
|
||||
class OptionFactory {
|
||||
public $db;
|
||||
|
||||
function __construct($db) {
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
function create(TSelect $field, $input) {
|
||||
if (isset($input['options.resid'])) {
|
||||
$type = $input['options.resid'];
|
||||
|
||||
$res = new Model_Resources($this->db);
|
||||
$field->options = $this->optionsArray('id_section', 'title', $res->getSubsections('', $type));
|
||||
|
||||
} else if (isset($input['options.res'])) {
|
||||
$type = $input['options.res'];
|
||||
|
||||
$res = new Model_Resources($this->db);
|
||||
$field->options = $this->optionsArray('path', 'title', $res->getSubsections('', $type));
|
||||
|
||||
} else if (isset($input['options.all_res'])) {
|
||||
$type = $input['options.all_res'];
|
||||
|
||||
$res = new Model_Resources($this->db);
|
||||
$field->options = $this->optionsArray('id_resource', 'subtitle', $res->getAllResource($type));
|
||||
|
||||
} else if (isset($input['options.db'])) {
|
||||
list($table, $keyvalue) = explode(":", $input['options.db']);
|
||||
list($key, $value) = explode(",", $keyvalue);
|
||||
|
||||
$field->options = $this->optionsDB($key, $value, $this->db->executeQuery("SELECT * FROM $table"));
|
||||
} elseif (isset($input['options.pair'])) {
|
||||
$field->options = $this->optionsPair($input['options.pair']);
|
||||
} else {
|
||||
$field->options = $input['options'];
|
||||
}
|
||||
}
|
||||
|
||||
public function optionsDB($key, $val, $res) {
|
||||
$result = array();
|
||||
while($res->next()) {
|
||||
$result[] = array('value' => $res->getInt($key), 'name' => $res->getString($val));
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function optionsArray($key, $val, $res) {
|
||||
$result = array();
|
||||
foreach($res as $item) {
|
||||
$result[] = array('value' => $item->{$key}, 'name' => $item->{$val});
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function optionsPair($list, $selected = false) {
|
||||
$result = array();
|
||||
foreach ($list as $key => $value) {
|
||||
$result [] = array('value' => $key, 'name' => $value, 'selected' => $key == $selected);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Форма для ввода
|
||||
* @package core
|
||||
*/
|
||||
class Form_Form
|
||||
{
|
||||
public $field = array ();
|
||||
class Form_Form extends View_View {
|
||||
public $field = array();
|
||||
public $action = "";
|
||||
public $method = 'post';
|
||||
public $header;
|
||||
|
||||
protected $replace;
|
||||
protected $before;
|
||||
|
||||
public $_title = array();
|
||||
public $alias = array();
|
||||
public $constructor = array();
|
||||
|
||||
public function __construct ()
|
||||
/**
|
||||
* Строим форму по ее структуре. Каждому типу соответствует определенный класс.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->constructor = array (
|
||||
'input' => 'TInput',
|
||||
'date' => 'TDate',
|
||||
'textarea' => 'TTextArea',
|
||||
'select' => 'TSelect',
|
||||
'select1' => 'TSelect1',
|
||||
'secret' => 'TSecret',
|
||||
'upload' => 'TUpload'
|
||||
$this->constructor = array(
|
||||
'input' => 'TInput',
|
||||
'inputreq' => 'TInput', // input с проверкой на заполненность
|
||||
|
||||
'date' => 'TDate',
|
||||
'datereq' => 'TDate',
|
||||
'datetime' => 'TDateTime',
|
||||
|
||||
'color' => 'TColor',
|
||||
'textarea' => 'TTextArea',
|
||||
'text' => 'TTextArea',
|
||||
'multiselect' => 'TSelectMany',
|
||||
// 'selectmany' => 'TSelectMany',
|
||||
'select1' => 'TSelectOne',
|
||||
'select' => 'TSelectOne',
|
||||
'questiontype'=> 'TQuestionType',
|
||||
'secret' => 'TSecret',
|
||||
'upload' => 'TUpload',
|
||||
'image' => 'TUpload',
|
||||
'checkbox' => 'TCheckbox',
|
||||
'checkmany' => 'TSelectMany',
|
||||
'hidden' => 'THidden',
|
||||
'radio' => 'TSelectOne',
|
||||
'filebrowser' => 'TComponentBrowserInput',
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
function getId()
|
||||
{
|
||||
return '_form_edit';
|
||||
}
|
||||
|
||||
public function addFieldClass($name, $class)
|
||||
{
|
||||
$this->constructor [$name] = $class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Добавляет одно поле ввода на форму
|
||||
*/
|
||||
public function addField (array $init)
|
||||
public function addField(array $init, $factory = null)
|
||||
{
|
||||
assert (isset($init['type']));
|
||||
assert (isset($init['name']));
|
||||
assert(isset($init['type']));
|
||||
assert(isset($init['name']));
|
||||
|
||||
// print_r($init);
|
||||
|
||||
$constructor = $this->constructor[$init['type']];
|
||||
$el = new $constructor ($init);
|
||||
$el->type = $init['type'];
|
||||
$el = new $constructor($init, $factory);
|
||||
if (!$el->type) {
|
||||
$el->type = $init['type'];
|
||||
}
|
||||
|
||||
if(isset($init['hint'])) {
|
||||
$el->hint = $init['hint'];
|
||||
}
|
||||
|
||||
$this->field [$init['name']] = $el;
|
||||
return $el;
|
||||
}
|
||||
|
||||
/**
|
||||
* Добавляет спсок полей для формы
|
||||
* Добавляет список полей для формы
|
||||
* @param array $list
|
||||
*/
|
||||
public function addFieldList (array $list)
|
||||
public function addFieldList(array $list, $factory = null)
|
||||
{
|
||||
foreach ($list as $init) {
|
||||
$this->addField ($init);
|
||||
$this->addField($init, $factory);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Устанавливает ошибки после проверки
|
||||
*/
|
||||
function setError (Validator $validator)
|
||||
function setError(Validator_Validator $validator)
|
||||
{
|
||||
foreach ($validator->getErrorMsg() as $name => $error)
|
||||
{
|
||||
|
|
@ -155,12 +353,18 @@ class Form_Form
|
|||
}
|
||||
}
|
||||
|
||||
function setFieldError($name, $message)
|
||||
{
|
||||
$this->field[$name]->error = true;
|
||||
$this->field[$name]->error_msg = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Устанавливает значения из масива
|
||||
*/
|
||||
function setValues (Collection $request) {
|
||||
function setValues(HttpRequest $request) {
|
||||
foreach ($this->field as $key => $el) {
|
||||
$value = $request->getRawData ($this->method, $key);
|
||||
$value = $request->getRawData($this->method, $key);
|
||||
$this->field[$key]->setValue($value);
|
||||
}
|
||||
}
|
||||
|
|
@ -170,10 +374,11 @@ class Form_Form
|
|||
* @param object $data
|
||||
* @param array $schema Связь между элементами формы и свойствами обьекта
|
||||
*/
|
||||
public function fill ($data, array $schema)
|
||||
public function fill($data, array $schema)
|
||||
{
|
||||
foreach ($schema as $key => $value) {
|
||||
$this->field [$value]->setValue($data->$value->getString ());
|
||||
foreach ($schema as $key => $conv) {
|
||||
list($value, $type) = $conv;
|
||||
$this->field [$key]->setValue(call_user_func(array('Primitive', 'from_' . $type), $data->$value));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -181,4 +386,9 @@ class Form_Form
|
|||
{
|
||||
$this->field[$name]->setValue($value);
|
||||
}
|
||||
|
||||
function execute()
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue