/
MultiValueTextField.php
97 lines (79 loc) · 2.36 KB
/
MultiValueTextField.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
<?php
/**
* A text field for multivalued text entry
*
* @author Marcus Nyeholt <marcus@symbiote.com.au>
*/
class MultiValueTextField extends FormField {
const KEY_SEP = '__';
protected $tag = 'input';
public function Field($properties = array()) {
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
Requirements::javascript('multivaluefield/javascript/multivaluefield.js');
Requirements::css('multivaluefield/css/multivaluefield.css');
$name = $this->name . '[]';
$fields = array();
$attributes = array(
'type' => 'text',
'class' => 'text mvtextfield mventryfield ' . ($this->extraClass() ? $this->extraClass() : ''),
// 'id' => $this->id(),
'name' => $name,
// 'value' => $this->Value(),
);
if($this->disabled) $attributes['disabled'] = 'disabled';
$fieldAttr = $attributes;
if ($this->value) {
foreach ($this->value as $i => $v) {
$fieldAttr['id'] = $this->id().MultiValueTextField::KEY_SEP.$i;
$fieldAttr['value'] = $v;
if ($this->readonly) {
unset($fieldAttr['value']);
$fields[] = $this->createReadonlyInput($fieldAttr, $v);
} else {
$fields[] = $this->createInput($fieldAttr, $v);
}
}
}
if (!$this->readonly) {
$fields[] = $this->createInput($attributes);
}
if (count($fields)) {
return '<ul id="'.$this->id().'" class="multivaluefieldlist '.$this->extraClass().'"><li>'.implode('</li><li>', $fields).'</li></ul>';
} else {
return '<div id="'.$this->id().'" class="multivaluefieldlist '.$this->extraClass().'"></div>';
}
}
public function createReadonlyInput($attributes, $value) {
return self::create_tag('span', $attributes, Convert::raw2xml($value));
}
public function createInput($attributes, $value = null) {
$attributes['value'] = $value;
return self::create_tag($this->tag, $attributes);
}
public function performReadonlyTransformation() {
$new = clone $this;
$new->setReadonly(true);
return $new;
}
public function setValue($v) {
if (is_array($v)) {
// we've been set directly via the post - lets prune any empty values
foreach ($v as $key => $val) {
if (!strlen($val)) {
unset($v[$key]);
}
}
}
if ($v instanceof MultiValueField) {
$v = $v->getValues();
}
if (!is_array($v)) {
$v = array();
}
return parent::setValue($v);
}
public function setTag($tag) {
$this->tag = $tag;
return $this;
}
}