1: <?php
2:
3: namespace webfilesframework\io\form\webfile;
4:
5: use webfilesframework\core\datasystem\file\format\MWebfile;
6: use webfilesframework\core\io\form\MForm;
7: use webfilesframework\core\io\request\MUrl;
8: use webfilesframework\core\io\form\MFormItemFactory;
9: use webfilesframework\core\io\form\formItem\MHiddenFormItem;
10:
11: 12: 13: 14: 15: 16: 17: 18:
19: class MWebfileFormVisualizer
20: {
21:
22: private $webfile;
23:
24: private $ignoredFieldsList;
25: private $localizedNamesList;
26: private $hiddenFieldsList;
27:
28:
29: private $form;
30:
31: public function __construct(MWebfile $webfile)
32: {
33: $this->init($webfile);
34: }
35:
36: public function init(MWebfile $webfile)
37: {
38:
39: $this->webfile = $webfile;
40: if (!isset($this->ignoredFieldsList)) {
41: $this->ignoredFieldsList = array();
42: }
43:
44: if (!isset($this->hiddenFieldsList)) {
45: $this->hiddenFieldsList = array();
46: }
47:
48: $action = "index.php?" . MUrl::getInstance()->getQueryString();
49: $method = "POST";
50: $this->form = new MForm($action, $method);
51:
52: $attributes = $this->webfile->getAttributes();
53:
54:
55: foreach ($attributes as $attribute) {
56: $attributeName = $attribute->getName();
57:
58: $attribute->setAccessible(true);
59: if (
60: MWebfile::isSimpleDatatype($attributeName)
61: && !array_key_exists($attributeName, $this->ignoredFieldsList)
62: && !in_array($attributeName, $this->ignoredFieldsList)
63: ) {
64:
65: $attributeValue = $attribute->getValue($this->webfile);
66:
67: if (!array_key_exists($attributeName, $this->hiddenFieldsList)
68: && !in_array($attributeName, $this->hiddenFieldsList)
69: ) {
70:
71:
72:
73: if (isset($this->localizedNamesList[$attributeName])) {
74: $formItem = MFormItemFactory::getFormItemByAttributeName(
75: $attributeName,
76: $attributeValue,
77: $this->localizedNamesList[$attributeName]
78: );
79: } else {
80: $formItem = MFormItemFactory::getFormItemByAttributeName(
81: $attributeName,
82: $attributeValue
83: );
84: }
85: } else {
86:
87:
88: $formItem = new MHiddenFormItem($attributeName, $attributeValue);
89: }
90:
91: $this->form->addFormItem($formItem);
92: }
93: }
94:
95:
96: $formItem = new MHiddenFormItem("classname", get_class($this->webfile));
97: $this->form->addFormItem($formItem);
98:
99: }
100:
101: public function setWebfile(MWebfile $webfile)
102: {
103: $this->webfile = $webfile;
104: }
105:
106: 107: 108: 109:
110: public function getCode()
111: {
112: return $this->form->getCode();
113: }
114:
115: public function setIgnoredFieldsList($ignoredFieldsList)
116: {
117: $this->ignoredFieldsList = $ignoredFieldsList;
118: }
119:
120: public function setHiddenFieldsList($hiddenFieldsList)
121: {
122: $this->hiddenFieldsList = $hiddenFieldsList;
123: }
124:
125: public function setLocalizedNamesList($fieldNameList)
126: {
127: $this->localizedNamesList = $fieldNameList;
128: }
129:
130: 131: 132:
133: public function getForm()
134: {
135: return $this->form;
136: }
137:
138: public function setLabelWidthOnEachFormItem($labelWidth)
139: {
140: $this->form->setLabelWidthOnEachFormItem($labelWidth);
141: }
142:
143: }
144: