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