22
33namespace Granada \Form ;
44
5+ /**
6+ * @property \Granada\Form\FormField $field
7+ * @property \Granada\Builder\ExtendedModel $model
8+ */
59class Form {
610
7- /**
8- * @var \Granada\Builder\ExtendedModel
9- */
11+ protected $ fieldname ;
1012 protected $ model ;
1113
12- public function __construct ($ model = null ) {
14+ public function __construct ($ fieldname , $ model = null ) {
15+ $ this ->fieldname = $ fieldname ;
1316 $ this ->model = $ model ;
1417 }
1518
19+ public function field () {
20+ $ class = $ this ->fieldname ;
21+ $ field = new $ class ($ this );
22+ return $ field ->setItem ($ this ->model );
23+ }
24+
1625 public function getFields ($ field_names = null ) {
1726 if (is_null ($ field_names )) {
1827 $ field_names = $ this ->model ->formFields ();
@@ -32,7 +41,7 @@ public function getFields($field_names = null) {
3241 */
3342 public function build ($ field_name = null , $ name_override = null ) {
3443 if (!$ field_name ) {
35- return new FormField ( get_class ( $ this ) );
44+ return $ this -> field ( );
3645 }
3746 $ options = [];
3847 if ($ this ->model ->fieldType ($ field_name ) == 'reference ' ) {
@@ -43,7 +52,12 @@ public function build($field_name = null, $name_override = null) {
4352 if ($ this ->model ->fieldType ($ field_name ) == 'enum ' ) {
4453 $ options = $ this ->model ->enum_options ($ field_name );
4554 }
46- return (new FormField (get_class ($ this )))
55+ if ($ this ->model ->fieldType ($ field_name ) == 'booltristate ' ) {
56+ $ options ['' ] = 'Neither ' ;
57+ $ options ['1 ' ] = 'Yes ' ;
58+ $ options ['0 ' ] = 'No ' ;
59+ }
60+ return $ this ->field ()
4761 ->setItem ($ this ->model )
4862 ->setType ($ this ->model ->fieldType ($ field_name ))
4963 ->setName ($ name_override ?: $ field_name )
@@ -57,63 +71,30 @@ public function build($field_name = null, $name_override = null) {
5771 }
5872
5973 /**
60- * @param string $type The tag type, e.g input or a
61- * @param array $htmlOptions list of pairs of values
62- * @param string $close 'short', 'long' or 'none' for closing type
63- * @param string $contents What to put between open and close tags if it is a long close type
74+ * Render a form field
75+ * @param string $field
76+ * @param string|null $name_override
6477 * @return string
6578 */
66- public function tag ($ type , $ htmlOptions , $ close = 'short ' , $ contents = '' ) {
67- ob_start ();
68- echo '< ' ;
69- echo $ type ;
70- foreach ($ htmlOptions as $ name => $ value ) {
71- if ($ value === '' ) {
72- if ($ name != 'value ' ) {
73- continue ;
74- }
75- }
76- echo ' ' ;
77- if ($ name == 'custom ' ) {
78- echo $ value ;
79- } else {
80- echo $ name ;
81- echo '=" ' ;
82- if ($ name == 'id ' ) {
83- echo $ this ->slug ($ value );
84- } else {
85- echo htmlentities ($ value );
86- }
87- echo '" ' ;
88- }
89- }
90- if ($ close == 'short ' ) {
91- echo ' / ' ;
92- }
93- if ($ close == 'long ' ) {
94- echo '> ' ;
95- echo $ contents ;
96- echo '</ ' ;
97- echo $ type ;
98- }
99- echo '> ' ;
100- return ob_get_clean ();
79+ public function renderField ($ field , $ name_override = null ) {
80+ return $ this ->build ($ field , $ name_override )
81+ ->render ();
10182 }
10283
10384 /**
104- * Get a slug for a string
105- *
106- * @param string $string
85+ * Render a number of fields at once
86+ * @param string[] $fields
10787 * @return string
10888 */
109- public function slug ($ string ) {
110- // Trim and to lower
111- $ slug = strtolower (trim ($ string ));
112- // Remove non-chars
113- $ slug = preg_replace ("/[^a-zA-Z0-9\/_|+ -]/ " , '' , $ slug );
114- // Replace separators with dash
115- $ slug = preg_replace ("/[\/_|+ -]+/ " , '- ' , $ slug );
116- return $ slug ;
89+ public function renderFields ($ fields = array ()) {
90+ $ response = '' ;
91+ if (!$ fields ) {
92+ $ fields = $ this ->model ->formFields ();
93+ }
94+ foreach ($ fields as $ field ) {
95+ $ response .= $ this ->renderField ($ field );
96+ }
97+ return $ response ;
11798 }
11899
119100 /**
@@ -122,15 +103,10 @@ public function slug($string) {
122103 * @param string $action
123104 * @param string $method
124105 * @param array $htmlOptions
125- * @param boolean $fileupload Include the enctype for forms that have a basic file upload field
126106 * @return string
127107 */
128- public function beginForm ($ action = '' , $ method = 'post ' , $ htmlOptions = array (), $ fileupload = false ) {
129- return $ this ->tag ('form ' , array_merge (array (
130- 'action ' => $ action ,
131- 'method ' => $ method ,
132- 'enctype ' => $ fileupload ? 'multipart/form-data ' : '' ,
133- ), $ htmlOptions ), 'none ' );
108+ public function beginForm ($ action = '' , $ method = 'post ' , $ htmlOptions = array ()) {
109+ return $ this ->field ()->beginForm ($ action , $ method , $ htmlOptions );
134110 }
135111
136112 /**
@@ -139,62 +115,23 @@ public function beginForm($action = '', $method = 'post', $htmlOptions = array()
139115 * @return string
140116 */
141117 public function endForm () {
142- return '</form> ' ;
143- }
144-
145- /**
146- * Get the form template for this field in twig format
147- * @param string $type
148- * @param integer $length
149- * @param string[] $tags
150- * @param \Granada\Granada|null $item
151- * @param string $fieldname
152- * @return string
153- */
154- public function fieldTemplate ($ type , $ length , $ tags , $ item , $ fieldname ) {
155- return '<label for={{ label_for }}>{{ label }}{% if help %}
156- <i class="fas fa-question-circle" title="{{ help }}"></i>
157- {% endif %}</label><input type="text" name="{{ name }}" value="{{ value }}" /> ' ;
158- }
159-
160- /**
161- * Render a form field
162- * @param string $field
163- * @param string|null $name_override
164- * @return string
165- */
166- public function renderField ($ field , $ name_override = null ) {
167- return $ this ->build ($ field , $ name_override )
168- ->render ();
169- }
170-
171- /**
172- * Render a number of fields at once
173- * @param string[] $fields
174- * @return string
175- */
176- public function renderFields ($ fields = array ()) {
177- $ response = '' ;
178- if (!$ fields ) {
179- $ fields = $ this ->model ->formFields ();
180- }
181- foreach ($ fields as $ field ) {
182- $ response .= $ this ->renderField ($ field );
183- }
184- return $ response ;
118+ return $ this ->field ()->endForm ();
185119 }
186120
187121 /**
188122 * Insert a hidden input field
189123 *
190124 * @param string $name
191125 * @param string $value
126+ * @param array $htmlOptions
192127 * @return string
193128 */
194- public function hiddenField ($ name , $ value = '' ) {
195- return $ this ->tag ('input ' , array (
196- 'type ' => 'hidden ' ,
197- 'name ' => $ name ,
198- ));
129+ public function hiddenField ($ name , $ value = '' , $ htmlOptions = array ()) {
130+ echo $ this ->field ()
131+ ->setType ('hidden ' )
132+ ->setName ($ name )
133+ ->setValue ($ value )
134+ ->setHtmlOptions ($ htmlOptions )
135+ ->render ();
199136 }
200137}
0 commit comments