diff --git a/src/Form/Color.php b/src/Form/Color.php new file mode 100644 index 0000000..c7c0d2d --- /dev/null +++ b/src/Form/Color.php @@ -0,0 +1,8 @@ +value = $value; - foreach ($this->options as &$option) { - $option['selected'] = (in_array($option['value'], $value)); - } - } -} - class TQuestionType extends Form_Select { function setValue($value) @@ -36,127 +26,23 @@ class TQuestionType extends Form_Select } } -/** - * Поле с датой - */ -class TDate extends Form_Field -{ -} - -/** - * Поле с цветом - */ -class TColor extends Form_Field -{ -} - - -/** - * Текстовое поле - */ -class TTextArea extends Form_Field -{ - function setValue($value) - { - $this->value = $value; - } +class TDateTime extends Form_Input { } /** * Поле для ввода пароля */ -class TSecret extends Form_Field -{ +class TSecret extends Form_Field { } -class TUpload extends Form_Field -{ +class TUpload extends Form_Field { } class THidden extends Form_Input { public $hidden = true; } -class TComponentBrowserInput extends Form_Input -{ -} - -/** - * При рендеринге каждому классу соответствует шаблон (см. themes/maxim/templates/macros.html) - */ -class TDateTime extends Form_Input { -} - -class OptionFactory { - public $db; - public $registry; - function __construct($db, $registry = null) { - $this->db = $db; - $this->registry = $registry; - } - - function create(Form_Select $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); - try { - $query_result = $this->db->executeQuery("SELECT * FROM $table"); - } catch(Exception $ex) { - $query_result = []; - } - $field->options = $this->optionsDB($key, $value, $query_result); - } elseif (isset($input['options.pair'])) { - $field->options = $this->optionsPair($input['options.pair']); - } elseif (isset($input['options.model'])) { - $factory = new Model_Factory($this->db, $this->registry); - $model = $factory->getModel($input['options.model']); - $field->options = $model->getAllAsOptions(); - } 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; - } +class TComponentBrowserInput extends Form_Input { } /** @@ -186,14 +72,14 @@ class Form_Form extends View_View { 'input' => 'Form_Input', 'inputreq' => 'Form_Input', // input с проверкой на заполненность - 'date' => 'TDate', - 'datereq' => 'TDate', + 'date' => 'Form_Date', + 'datereq' => 'Form_Date', 'datetime' => 'TDateTime', - 'color' => 'TColor', - 'textarea' => 'TTextArea', - 'text' => 'TTextArea', - 'multiselect' => 'TSelectMany', + 'color' => 'Form_Color', + 'textarea' => 'Form_TextArea', + 'text' => 'Form_TextArea', + 'multiselect' => 'Form_SelectMany', // 'selectmany' => 'TSelectMany', 'select1' => 'Form_SelectOne', 'select' => 'Form_SelectOne', diff --git a/src/Form/OptionFactory.php b/src/Form/OptionFactory.php new file mode 100644 index 0000000..267eff1 --- /dev/null +++ b/src/Form/OptionFactory.php @@ -0,0 +1,73 @@ +db = $db; + $this->registry = $registry; + } + + function create(Form_Select $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); + try { + $query_result = $this->db->executeQuery("SELECT * FROM $table"); + } catch(Exception $ex) { + $query_result = []; + } + $field->options = $this->optionsDB($key, $value, $query_result); + } elseif (isset($input['options.pair'])) { + $field->options = $this->optionsPair($input['options.pair']); + } elseif (isset($input['options.model'])) { + $factory = new Model_Factory($this->db, $this->registry); + $model = $factory->getModel($input['options.model']); + $field->options = $model->getAllAsOptions(); + } 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; + } +} diff --git a/src/Form/SelectMany.php b/src/Form/SelectMany.php new file mode 100644 index 0000000..339ceac --- /dev/null +++ b/src/Form/SelectMany.php @@ -0,0 +1,14 @@ +value = $value; + foreach ($this->options as &$option) { + $option['selected'] = (in_array($option['value'], $value)); + } + } +} \ No newline at end of file diff --git a/src/Form/TextArea.php b/src/Form/TextArea.php new file mode 100644 index 0000000..dfc3e9f --- /dev/null +++ b/src/Form/TextArea.php @@ -0,0 +1,11 @@ +value = $value; + } +}