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
|
|
|
|
13
|
|
|
public function __construct($identifier,$instance=NULL, $captions=NULL) { |
14
|
|
|
parent::__construct($identifier,$instance=NULL, $captions=NULL); |
15
|
|
|
$this->separators=[-1]; |
16
|
|
|
$this->headers=[]; |
17
|
|
|
$this->defaultValueFunction=function($name,$value,$index){ |
18
|
|
|
$caption=$this->getCaption($index); |
19
|
|
|
$input=new HtmlFormInput($this->widgetIdentifier."-".$name,$caption,"text",$value); |
20
|
|
|
$input->setName($name); |
21
|
|
|
return $input; |
22
|
|
|
}; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function _beforeAddProperty($index,&$field){ |
26
|
|
|
if(JString::endswith($field, "\n")===true){ |
27
|
|
|
$this->addSeparatorAfter($index); |
28
|
|
|
} |
29
|
|
|
if($index>1 && JString::startswith($field, "\n")===true){ |
30
|
|
|
$this->addSeparatorAfter($index-1); |
31
|
|
|
} |
32
|
|
|
$field=\str_replace("\n", "", $field); |
33
|
|
|
if(($header=$this->hasHeader($field))!==false){ |
34
|
|
|
$this->addHeaderDividerBefore($index, $header); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function hasHeader(&$field){ |
39
|
|
|
$matches=[];$result=false; |
40
|
|
|
if(\preg_match('/\{(.*?)\}/s', $field, $matches)===1){ |
41
|
|
|
$result=$matches[1]; |
42
|
|
|
$field=\str_replace("{".$result."}","", $field); |
43
|
|
|
} |
44
|
|
|
return $result; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
|
49
|
|
|
public function addSeparatorAfter($fieldNum){ |
50
|
|
|
if(\array_search($fieldNum, $this->separators)===false) |
51
|
|
|
$this->separators[]=$fieldNum; |
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function addHeaderDividerBefore($fieldNum,$header){ |
56
|
|
|
$this->headers[$fieldNum]=$header; |
57
|
|
|
if($fieldNum>0) |
58
|
|
|
$this->addSeparatorAfter($fieldNum-1); |
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getSeparators() { |
63
|
|
|
return $this->separators; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function removeSeparator($index){ |
67
|
|
|
\array_splice($this->separators,$index,1); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function removeField($index){ |
71
|
|
|
parent::removeField($index); |
72
|
|
|
$pos=\array_search($index, $this->separators); |
73
|
|
|
if($pos!==false){ |
74
|
|
|
for($i=$pos+1;$i<\sizeof($this->separators);$i++){ |
|
|
|
|
75
|
|
|
$this->separators[$i]--; |
76
|
|
|
} |
77
|
|
|
\array_splice($this->separators, $pos, 1); |
78
|
|
|
} |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function setSeparators($separators) { |
83
|
|
|
$this->separators=\array_merge([-1], $separators); |
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getHeaders() { |
88
|
|
|
return $this->headers; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
} |
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: