1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\semantic\widgets\dataform; |
4
|
|
|
|
5
|
|
|
use Ajax\semantic\widgets\base\InstanceViewer; |
6
|
|
|
use Ajax\service\JString; |
7
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormInput; |
8
|
|
|
|
9
|
|
|
class FormInstanceViewer extends InstanceViewer { |
10
|
|
|
protected $separators; |
11
|
|
|
protected $headers; |
12
|
|
|
protected $wrappers; |
13
|
|
|
|
14
|
|
|
public function __construct($identifier,$instance=NULL, $captions=NULL) { |
15
|
|
|
parent::__construct($identifier,$instance=NULL, $captions=NULL); |
16
|
|
|
$this->separators=[-1]; |
17
|
|
|
$this->headers=[]; |
18
|
|
|
$this->wrappers=[]; |
19
|
|
|
$this->defaultValueFunction=function($name,$value,$index){ |
20
|
|
|
$caption=$this->getCaption($index); |
21
|
|
|
$input=new HtmlFormInput($this->widgetIdentifier."-".$name,$caption,"text",$value); |
22
|
|
|
$input->setName($name); |
23
|
|
|
return $input; |
24
|
|
|
}; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected function _beforeAddProperty($index,&$field){ |
28
|
|
|
if(JString::endswith($field, "\n")===true){ |
29
|
|
|
$this->addSeparatorAfter($index); |
30
|
|
|
} |
31
|
|
|
if($index>1 && JString::startswith($field, "\n")===true){ |
32
|
|
|
$this->addSeparatorAfter($index-1); |
33
|
|
|
} |
34
|
|
|
$field=\str_replace("\n", "", $field); |
35
|
|
|
if(($header=$this->hasHeader($field))!==false){ |
36
|
|
|
$this->addHeaderDividerBefore($index, $header); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function hasHeader(&$field){ |
41
|
|
|
$matches=[];$result=false; |
42
|
|
|
if(\preg_match('/\{(.*?)\}/s', $field, $matches)===1){ |
43
|
|
|
$result=$matches[1]; |
44
|
|
|
$field=\str_replace("{".$result."}","", $field); |
45
|
|
|
} |
46
|
|
|
return $result; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
|
51
|
|
|
public function addSeparatorAfter($fieldNum){ |
52
|
|
|
if(\array_search($fieldNum, $this->separators)===false) |
53
|
|
|
$this->separators[]=$fieldNum; |
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function addHeaderDividerBefore($fieldNum,$header){ |
58
|
|
|
$this->headers[$fieldNum]=$header; |
59
|
|
|
if($fieldNum>0) |
60
|
|
|
$this->addSeparatorAfter($fieldNum-1); |
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function addWrapper($fieldNum,$contentBefore,$contentAfter=null){ |
65
|
|
|
$this->wrappers[$fieldNum]=[$contentBefore,$contentAfter]; |
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getSeparators() { |
70
|
|
|
return $this->separators; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function removeSeparator($index){ |
74
|
|
|
\array_splice($this->separators,$index,1); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function removeField($index){ |
78
|
|
|
parent::removeField($index); |
79
|
|
|
$pos=\array_search($index, $this->separators); |
80
|
|
|
if($pos!==false){ |
81
|
|
|
for($i=$pos+1;$i<\sizeof($this->separators);$i++){ |
|
|
|
|
82
|
|
|
$this->separators[$i]--; |
83
|
|
|
} |
84
|
|
|
\array_splice($this->separators, $pos, 1); |
85
|
|
|
} |
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function setSeparators($separators) { |
90
|
|
|
$this->separators=\array_merge([-1], $separators); |
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getHeaders() { |
95
|
|
|
return $this->headers; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getWrappers() { |
99
|
|
|
return $this->wrappers; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function setWrappers($wrappers) { |
103
|
|
|
$this->wrappers=$wrappers; |
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
|
109
|
|
|
} |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: