phpMv -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
Loading...
Searching...
No Matches
DataForm.php
Go to the documentation of this file.
1<?php
2
4
12
20class DataForm extends Widget , BaseTrait{
23 private $_inverted;
24
25 public function __construct($identifier, $modelInstance=NULL) {
26 parent::__construct($identifier, null,$modelInstance);
27 $this->_form=new HtmlForm($identifier);
28 $this->_init(new FormInstanceViewer($identifier), 'form', $this->_form, true);
29 }
30
31 protected function _getFieldIdentifier($prefix,$name=''){
32 return $this->identifier."-{$name}-".$this->_instanceViewer->getIdentifier();
33 }
34
35 public function compile(JsUtils $js=NULL,&$view=NULL){
36 if(!$this->_generated){
37 $this->_instanceViewer->setInstance($this->_modelInstance);
38
39 $form=$this->content['form'];
40 $this->_generateContent($form);
41
42 if(isset($this->_toolbar)){
43 $this->_setToolbarPosition($form);
44 }
45 $this->content=JArray::sortAssociative($this->content, [PositionInTable::BEFORETABLE,'form',PositionInTable::AFTERTABLE]);
46 if($this->_inverted){
47 $this->content['form']->setInverted(true);
48 }
49 $this->_generated=true;
50 }
51 return parent::compile($js,$view);
52 }
53
57 protected function _generateContent($form){
58 $values= $this->_instanceViewer->getValues();
59 $count=$this->_instanceViewer->count();
60 $separators=$this->_instanceViewer->getSeparators();
61 $headers=$this->_instanceViewer->getHeaders();
62 $wrappers=$this->_instanceViewer->getWrappers();
63 $names=$this->_instanceViewer->getProperties();
64 \sort($separators);
65 $size=\count($separators);
66 $nb=0;
67 if($size===1){
68 $i=-1;
69 foreach ($values as $k=>$v){
70 $this->_generateFields($form, [$v], $headers, $i, $wrappers,$nb++,$names[$k]??'');
71 $i++;
72 }
73 }else{
74 $separators[]=$count;
75 for($i=0;$i<$size;$i++){
76 $fields=\array_slice($values, $separators[$i]+1,$separators[$i+1]-$separators[$i]);
77 $this->_generateFields($form, $fields, $headers, $separators[$i], $wrappers,$nb++,$names[$i]??'');
78 }
79 }
80 if($this->_hasRules && !$this->getForm()->hasValidationParams()){
81 $this->setValidationParams(['inline'=>true]);
82 }
83 }
84
85 protected function _generateFields($form, $values, $headers, $sepFirst, $wrappers, $nb, $name){
86 $wrapper=null;
87 if(isset($headers[$sepFirst+1]))
88 $form->addHeader($headers[$sepFirst+1],4,true);
89 if(isset($wrappers[$sepFirst+1])){
90 $wrapper=$wrappers[$sepFirst+1];
91 }
92 $count=\count($values);
93 if($count===1){
94 $added=$form->addField($values[0]);
95 }elseif($count>1){
96 $added=$form->addFields($values);
97 }else
98 return;
99 if(isset($wrapper)){
100 $added->wrap($wrapper[0],$wrapper[1]);
101 }
102 $this->execHook('onGenerateFields',$added,$nb,$name);
103 }
104
110 public function onGenerateField($callback){
111 $this->addHook('onGenerateFields',$callback);
112 }
113
117 public function getForm(){
118 return $this->content['form'];
119 }
120
121 public function addSeparatorAfter($fieldNum){
122 $fieldNum=$this->_getIndex($fieldNum);
123 $this->_instanceViewer->addSeparatorAfter($fieldNum);
124 return $this;
125 }
126
127 public function getSeparators() {
128 return $this->_instanceViewer->getSeparators();
129 }
130
131 public function setSeparators($separators) {
132 $this->_instanceViewer->setSeparators($separators);
133 return $this;
134 }
135
136 public function fieldAsReset($index,$cssStyle=NULL,$attributes=NULL){
137 return $this->_fieldAs(function($id,$name,$value) use ($cssStyle){
138 $button=new HtmlButton($id,$value,$cssStyle);
139 $button->setProperty('type', 'reset');
140 return $button;
141 }, $index,$attributes);
142 }
143
149 public function getHtmlComponent() {
150 return $this->content['form'];
151 }
156 protected function _setToolbarPosition($table, $captions=NULL) {
158 }
159
160 public function addDividerBefore($index,$title){
161 $index=$this->_getIndex($index);
162 $this->_instanceViewer->addHeaderDividerBefore($index, $title);
163 return $this;
164 }
165
166 public function addWrapper($index,$contentBefore,$contentAfter=null){
167 $index=$this->_getIndex($index);
168 $this->_instanceViewer->addWrapper($index, $contentBefore,$contentAfter);
169 return $this;
170 }
171
172 public function run(JsUtils $js){
173 parent::run($js);
174 }
175
176 public function setInverted($recursive=true) {
177 $this->setInvertedTrait($recursive);
178 $this->_inverted=$recursive;
179 }
180}
JQuery PHP library.
Definition JsUtils.php:23
setValidationParams(array $_validationParams)
Sets the parameters for the Form validation (on, inline, delay...)
Definition Widget.php:497
_init($instanceViewer, $contentKey, $content, $edition)
Definition Widget.php:81
_getIndex($fieldName)
Definition Widget.php:96
execHook($hookKey,... $variables)
Executes the hook with key $hookKey.
addHook($hookKey, $callable)
Adds a new Hook.
Semantic Button component.
_fieldAs($elementCallback, &$index, $attributes=NULL, $prefix=null)
__construct($identifier, $modelInstance=NULL)
Definition DataForm.php:25
run(JsUtils $js)
SimpleExtComponent\Ajax\common\html\BaseHtmlrun()\Ajax\common\html\BaseHtmlrun()\Ajax\common\html\Bas...
Definition DataForm.php:172
_setToolbarPosition($table, $captions=NULL)
{}
Definition DataForm.php:156
setInverted($recursive=true)
can be formatted to appear on dark backgrounds
Definition DataForm.php:176
_generateFields($form, $values, $headers, $sepFirst, $wrappers, $nb, $name)
Definition DataForm.php:85
compile(JsUtils $js=NULL, &$view=NULL)
Definition DataForm.php:35
fieldAsReset($index, $cssStyle=NULL, $attributes=NULL)
Definition DataForm.php:136
addWrapper($index, $contentBefore, $contentAfter=null)
Definition DataForm.php:166
onGenerateField($callback)
Function called when a field is generated the generated field is the first parameter.
Definition DataForm.php:110