-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHTML5FieldsDecorator.php
119 lines (103 loc) · 3.42 KB
/
HTML5FieldsDecorator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* Tries to keep the classes in this module as DRY as possible by creating
* "magic methods" for common functionality
*/
class HTML5FieldsDecorator extends DataObjectDecorator {
/**
* Construct and return HTML tag
* Most fields are rendered with the same logic
*/
public function createHTML5Tag($tag, $attributes, $content = null) {
$preparedAttributes = '';
foreach($attributes as $k => $v) {
if($v || in_array($v, array('0', 0), true) || $k == 'value')
$preparedAttributes .= " $k=\"" . Convert::raw2att($v) . "\"";
}
$required = $this->owner->required ? ' required' : '';
if($content || $tag != 'input') return "<{$tag}{$preparedAttributes}{$required}>$content</$tag>";
else return "<{$tag}{$preparedAttributes}{$required}>";
}
/**
* @param string $content content between opening and closing tags (where used)
*/
public function defaultField($content = null) {
// We can generally assume that we want to make an input tag
$tagType = isset($this->owner->tagType) ? $this->owner->tagType : 'input';
// The default class will be the HTML5 class name
// Also add the standard class name so legacy styles are not broken
$this->owner->addExtraClass($this->owner->parentClass());
$attributes = array(
'type' => $this->owner->type,
'id' => $this->owner->id(),
'name' => $this->owner->Name(),
'tabindex' => $this->owner->getTabIndex(),
'maxlength' => $this->owner->maxLength ?: null,
'size' => $this->owner->maxLength ? min($this->owner->maxLength, 30) : null
);
$classes = array();
if ($tagType == 'input') {
$classes[] = 'text';
}
$classes[] = $this->owner->XML_val('Type');
if ($extraClasses = $this->owner->extraClass()) {
$classes[] = $extraClasses;
}
$attributes['class'] = implode(' ', $classes);
if ($this->owner->pattern) {
$attributes['pattern'] = $this->owner->pattern;
}
if ($this->owner->hasMethod('amendAttributes')) {
$attributes = array_merge($attributes, $this->owner->amendAttributes($attributes));
}
$vals = array(
'placeholder' => $this->owner->placeholder,
'value' => $this->owner->Value(),
'disabled' => $this->owner->disabled ? 'disabled' : false
);
foreach ($vals as $key => $val) {
if ($val) {
$attributes[$key] = $val;
}
}
return $this->createHTML5Tag($tagType, $attributes, $content);
}
/**
* Most FieldHolders are rendered in exactly the same way
*/
public function defaultFieldHolder() {
$varLabels = array(
'extraClass', 'Field', 'Name', 'Message',
'MessageType', 'RightTitle', 'Title', 'Type'
);
$vars = array();
foreach( $varLabels as $label ) {
$vars[$label] = $this->owner->XML_val($label);
}
$vars['ID'] = $this->owner->id();
return $this->owner->renderWith('HTML5FieldHolder', $vars);
}
/**
* When using a template for FieldHolder
* this will render the field by calling the owner's Field method
*
* @return string
*/
public function getRenderedField() {
return $this->owner->Field();
}
/**
* Sets the required attribute on the field
* As a help, can also add or remove the field from a validator
*
* @param boolean $required
* @param Validator $validator Most likely going to be a RequiredFields object
*/
public function setAsRequired($required = true, $validator = null) {
$this->owner->required = $required;
if( $validator ) {
$func = ($required ? 'add' : 'remove') . 'RequiredField';
$validator->$func($this->owner->Name());
}
}
}