1: <?php
2:
3: namespace webfilesframework\io\form;
4:
5: use webfilesframework\MSite;
6: use webfilesframework\io\form\formItem\MTextfieldFormItem;
7: use webfilesframework\io\form\formItem\MTextareaFormItem;
8: use webfilesframework\io\form\formItem\MHtmlTextareaFormItem;
9: use webfilesframework\io\form\formItem\MTimeFormItem;
10: use webfilesframework\io\form\formItem\MDateFormItem;
11: use webfilesframework\io\form\formItem\MWeekdayFormItem;
12: use webfilesframework\io\form\formItem\MDropdownMenueFormItem;
13: use webfilesframework\io\form\formItem\MCheckboxesFormItem;
14:
15:
16: 17: 18: 19: 20: 21:
22: class MFormItemFactory
23: {
24:
25: 26: 27: 28: 29: 30: 31: 32: 33:
34: public static function getFormItemByAttributeName($attributeName, $attributeValue, $localizedName = "")
35: {
36:
37: $attributeType = substr($attributeName, 2, 1);
38:
39: if ($attributeType == "s") {
40: $formItem = static::getFormItemByAttributeType("shorttext", $attributeName, $attributeValue, $localizedName);
41: } else if ($attributeType == "l") {
42: $formItem = static::getFormItemByAttributeType("longtext", $attributeName, $attributeValue, $localizedName);
43: } else if ($attributeType == "h") {
44: $formItem = static::getFormItemByAttributeType("htmllongtext", $attributeName, $attributeValue, $localizedName);
45: } else if ($attributeType == "t") {
46: $formItem = static::getFormItemByAttributeType("time", $attributeName, $attributeValue, $localizedName);
47: } else if ($attributeType == "d") {
48: $formItem = static::getFormItemByAttributeType("date", $attributeName, $attributeValue, $localizedName);
49: } else if ($attributeType == "w") {
50: $formItem = static::getFormItemByAttributeType("weekday", $attributeName, $attributeValue, $localizedName);
51: } else {
52: $formItem = static::getFormItemByAttributeType("shorttext", $attributeName, $attributeValue, $localizedName);
53: }
54:
55: return $formItem;
56:
57: }
58:
59: 60: 61: 62: 63: 64: 65: 66:
67: public static function getFormItemByAttributeType($attributeType, $attributeName, $attributeValue = "", $localizedName = "")
68: {
69:
70: if ($attributeType == "integer") {
71: $formItem = new MTextfieldFormItem($attributeName, $attributeValue, $localizedName);
72: } else if ($attributeType == "shorttext") {
73: $formItem = new MTextfieldFormItem($attributeName, $attributeValue, $localizedName);
74: } else if ($attributeType == "longtext") {
75: $formItem = new MTextareaFormItem($attributeName, $attributeValue, $localizedName);
76: } else if ($attributeType == "htmllongtext") {
77: $formItem = new MHtmlTextareaFormItem($attributeName, $attributeValue, $localizedName);
78: } else if ($attributeType == "time") {
79: $formItem = new MTimeFormItem($attributeName, $attributeValue, $localizedName);
80: } else if ($attributeType == "date") {
81: $formItem = new MDateFormItem($attributeName, $attributeValue, $localizedName);
82: } else if ($attributeType == "weekday") {
83: $formItem = new MWeekdayFormItem($attributeName, $attributeValue, $localizedName);
84: } else {
85: $formItem = new MTextfieldFormItem($attributeName, $attributeValue, $localizedName);
86: }
87: return $formItem;
88:
89: }
90:
91:
92: public static function getDropdownMenueFormItemByTemplate($formItemName, $template, $selectedValue, $isFiltered = false, $localizedName = "")
93: {
94:
95:
96: $formItem = new MDropdownMenueFormItem($formItemName, $selectedValue, $localizedName, $isFiltered);
97:
98: if (!$isFiltered) {
99: $webfiles = MSite::getInstance()->getDefaultDatastore()->getByTemplate($template);
100: $possibleValues = array();
101: foreach ($webfiles as $webfile) {
102: array_push($possibleValues, $webfile);
103: }
104: $formItem->setPossibleValues($possibleValues);
105: }
106:
107: return $formItem;
108:
109: }
110:
111: public static function getCheckboxesFormItemByTemplate($formItemName, $template, $selectedValues)
112: {
113: $webfiles = MSite::getInstance()->getDefaultDatastore()->getByTemplate($template);
114:
115: $formItem = new MCheckboxesFormItem($formItemName, null);
116:
117: $possibleValues = array();
118: foreach ($webfiles as $webfile) {
119: array_push($possibleValues, $webfile);
120: }
121: $formItem->setPossibleValues($possibleValues);
122: $formItem->setSelectedValues($selectedValues);
123:
124: return $formItem;
125:
126: }
127:
128: }