Skip to content

Commit 82e101b

Browse files
committed
DataForm V1
1 parent 163c98d commit 82e101b

File tree

8 files changed

+253
-10
lines changed

8 files changed

+253
-10
lines changed

Ajax/common/Widget.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected function _getFieldIdentifier($prefix){
4242
return $this->identifier."-{$prefix}-".$this->_instanceViewer->getIdentifier();
4343
}
4444

45-
protected abstract function _setToolbarPosition($table,$captions=NULL);
45+
abstract protected function _setToolbarPosition($table,$captions=NULL);
4646

4747
public function show($modelInstance){
4848
$this->_modelInstance=$modelInstance;

Ajax/semantic/html/collections/form/HtmlFormField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function __construct($identifier, $field,$label=NULL) {
1616
parent::__construct($identifier, "div","field");
1717
$this->content=array();
1818
$this->_states=[State::ERROR,State::DISABLED];
19-
if(isset($label))
19+
if(isset($label)===true)
2020
$this->setLabel($label);
2121
$this->setField($field);
2222
$this->_validation=NULL;

Ajax/semantic/html/collections/form/HtmlFormFields.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public function setLabel($label) {
5757

5858
public function addItem($item) {
5959
$item=parent::addItem($item);
60-
$item->setContainer($this);
60+
if($item instanceof HtmlFormField)
61+
$item->setContainer($this);
6162
return $item;
6263
}
6364

Ajax/semantic/traits/SemanticWidgetsTrait.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use Ajax\semantic\widgets\datatable\DataTable;
55
use Ajax\semantic\widgets\dataelement\DataElement;
6+
use Ajax\semantic\widgets\dataform\DataForm;
67

78
trait SemanticWidgetsTrait {
89

@@ -26,4 +27,13 @@ public function dataTable($identifier,$model, $instances){
2627
public function dataElement($identifier, $instance){
2728
return $this->addHtmlComponent(new DataElement($identifier,$instance));
2829
}
30+
31+
/**
32+
* @param string $identifier
33+
* @param object $instance
34+
* @return DataForm
35+
*/
36+
public function dataForm($identifier, $instance){
37+
return $this->addHtmlComponent(new DataForm($identifier,$instance));
38+
}
2939
}

Ajax/semantic/widgets/base/InstanceViewer.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,22 @@ public function getValue($index){
4444
return $this->_getValue($property, $index);
4545
}
4646

47-
private function _getValue($property,$index){
47+
protected function _beforeAddProperty($index,&$field){
48+
49+
}
50+
51+
protected function _getDefaultValue($name,$value,$index){
52+
return $value;
53+
}
54+
55+
protected function _getValue($property,$index){
4856
if($property instanceof \ReflectionProperty){
4957
$property->setAccessible(true);
5058
$value=$property->getValue($this->instance);
5159
if(isset($this->values[$index])){
52-
$value= $this->values[$index]($value);
60+
$value= $this->values[$index]($value,$this->instance,$index);
61+
}else{
62+
$value=$this->_getDefaultValue($property->getName(),$value, $index);
5363
}
5464
}else{
5565
if(\is_callable($property))
@@ -100,7 +110,11 @@ public function visiblePropertiesCount(){
100110
return \sizeof($this->visibleProperties);
101111
}
102112

103-
private function showableProperty(\ReflectionProperty $rProperty){
113+
public function getProperty($index){
114+
return $this->properties[$index];
115+
}
116+
117+
protected function showableProperty(\ReflectionProperty $rProperty){
104118
return JString::startswith($rProperty->getName(),"_")===false;
105119
}
106120

@@ -119,9 +133,11 @@ public function setInstance($instance) {
119133
$this->properties[]=$property;
120134
}elseif(\is_string($property)){
121135
try{
136+
$this->_beforeAddProperty(\sizeof($this->properties), $property);
122137
$rProperty=$this->reflect->getProperty($property);
123138
$this->properties[]=$rProperty;
124139
}catch(\Exception $e){
140+
$this->_beforeAddProperty(\sizeof($this->properties), $property);
125141
$this->properties[]=$property;
126142
}
127143
}elseif(\is_int($property)){
@@ -138,7 +154,7 @@ public function setInstance($instance) {
138154
return $this;
139155
}
140156

141-
private function getDefaultProperties(){
157+
protected function getDefaultProperties(){
142158
$result=[];
143159
$properties=$this->reflect->getProperties();
144160
foreach ($properties as $property){

Ajax/semantic/widgets/dataform/DataForm.php

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,88 @@
33
namespace Ajax\semantic\widgets\dataform;
44

55
use Ajax\common\Widget;
6+
use Ajax\semantic\html\collections\form\HtmlForm;
7+
use Ajax\semantic\widgets\datatable\PositionInTable;
8+
use Ajax\service\JArray;
9+
use Ajax\JsUtils;
610

711
/**
812
* DataForm widget for editing model objects
913
* @version 1.0
1014
* @author jc
1115
* @since 2.2
12-
*
1316
*/
1417
class DataForm extends Widget {
18+
use FormFieldAsTrait;
1519

20+
public function __construct($identifier, $modelInstance=NULL) {
21+
parent::__construct($identifier, null,$modelInstance);
22+
$this->_instanceViewer=new FormInstanceViewer();
23+
$this->content=["form"=>new HtmlForm($identifier)];
24+
$this->_toolbarPosition=PositionInTable::BEFORETABLE;
25+
}
26+
27+
public function compile(JsUtils $js=NULL,&$view=NULL){
28+
$this->_instanceViewer->setInstance($this->_modelInstance);
29+
30+
$form=$this->content["form"];
31+
$this->_generateContent($form);
32+
33+
if(isset($this->_toolbar)){
34+
$this->_setToolbarPosition($form);
35+
}
36+
$this->content=JArray::sortAssociative($this->content, [PositionInTable::BEFORETABLE,"form",PositionInTable::AFTERTABLE]);
37+
return parent::compile($js,$view);
38+
}
39+
40+
/**
41+
* @param HtmlForm $form
42+
*/
43+
protected function _generateContent($form){
44+
$values= $this->_instanceViewer->getValues();
45+
$count=$this->_instanceViewer->count();
46+
$separators=$this->_instanceViewer->getSeparators();
47+
$separators[]=$count;
48+
for($i=0;$i<\sizeof($separators)-1;$i++){
49+
$fields=\array_slice($values, $separators[$i]+1,$separators[$i+1]-$separators[$i]);
50+
if(\sizeof($fields)===1){
51+
$form->addField($fields[0]);
52+
}else
53+
$form->addFields($fields);
54+
}
55+
}
56+
57+
public function addSeparatorAfter($fieldNum){
58+
$this->_instanceViewer->addSeparatorAfter($fieldNum);
59+
return $this;
60+
}
61+
62+
public function getSeparators() {
63+
return $this->_instanceViewer->getSeparators();
64+
}
65+
66+
public function setSeparators($separators) {
67+
$this->_instanceViewer->setSeparators($separators);
68+
return $this;
69+
}
70+
71+
/**
72+
* {@inheritDoc}
73+
* @see \Ajax\common\Widget::getHtmlComponent()
74+
* @return HtmlForm
75+
*/
1676
public function getHtmlComponent() {
17-
// TODO Auto-generated method stub
77+
return $this->content["form"];
1878
}
1979
/**
2080
* {@inheritdoc}
2181
* @see \Ajax\common\Widget::_setToolbarPosition()
2282
*/
2383
protected function _setToolbarPosition($table, $captions=NULL) {
24-
// TODO: Auto-generated method stub
84+
$this->content[$this->_toolbarPosition]=$this->_toolbar;
85+
}
86+
87+
public function setValidationParams(array $_validationParams){
88+
return $this->getHtmlComponent()->setValidationParams($_validationParams);
2589
}
2690
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
namespace Ajax\semantic\widgets\dataform;
3+
use Ajax\semantic\html\elements\HtmlLabel;
4+
use Ajax\semantic\html\collections\form\HtmlFormRadio;
5+
use Ajax\semantic\html\collections\form\HtmlFormTextarea;
6+
use Ajax\semantic\html\collections\form\HtmlFormField;
7+
use Ajax\semantic\html\collections\form\HtmlFormInput;
8+
use Ajax\semantic\html\collections\form\HtmlFormCheckbox;
9+
use Ajax\semantic\html\collections\form\HtmlFormDropdown;
10+
11+
/**
12+
* @author jc
13+
* @property FormInstanceViewer $_instanceViewer
14+
* @property object $_modelInstance;
15+
*/
16+
17+
trait FormFieldAsTrait{
18+
19+
abstract protected function _getFieldIdentifier($prefix);
20+
abstract public function setValueFunction($index,$callback);
21+
22+
private function _getLabelField($caption,$icon=NULL){
23+
$label=new HtmlLabel($this->_getFieldIdentifier("lbl"),$caption,$icon);
24+
return $label;
25+
}
26+
27+
/**
28+
* @param HtmlFormField $element
29+
* @param array $attributes
30+
*/
31+
protected function _applyAttributes($element,&$attributes,$index){
32+
if(isset($attributes["rules"])){
33+
$rules=$attributes["rules"];
34+
if(\is_array($rules))
35+
$element->addRules($rules);
36+
else
37+
$element->addRule($rules);
38+
unset($attributes["rules"]);
39+
}
40+
if(isset($attributes["callback"])){
41+
$callback=$attributes["callback"];
42+
if(\is_callable($callback)){
43+
$callback($element,$this->_modelInstance,$index);
44+
unset($attributes["callback"]);
45+
}
46+
}
47+
$element->fromArray($attributes);
48+
}
49+
50+
protected function _fieldAs($elementCallback,$index,$attributes=NULL){
51+
$this->setValueFunction($index,function($value)use ($index,&$attributes,$elementCallback){
52+
$caption=$this->_instanceViewer->getCaption($index);
53+
$name=$this->_instanceViewer->getFieldName($index);
54+
$element=$elementCallback($name,$caption,$value);
55+
if(\is_array($attributes))
56+
$this->_applyAttributes($element, $attributes,$index);
57+
return $element;
58+
});
59+
return $this;
60+
}
61+
62+
63+
public function fieldAsRadio($index,$attributes=NULL){
64+
return $this->_fieldAs(function($name,$caption,$value){
65+
return new HtmlFormRadio($name,$name,$caption,$value);
66+
}, $index,$attributes);
67+
}
68+
69+
public function fieldAsTextarea($index,$attributes=NULL){
70+
return $this->_fieldAs(function($name,$caption,$value){
71+
return new HtmlFormTextarea($name,$caption,$value);
72+
}, $index,$attributes);
73+
}
74+
75+
public function fieldAsInput($index,$attributes=NULL){
76+
return $this->_fieldAs(function($name,$caption,$value){
77+
return new HtmlFormInput($name,$caption,"text",$value);
78+
}, $index,$attributes);
79+
}
80+
81+
public function fieldAsCheckbox($index,$attributes=NULL){
82+
return $this->_fieldAs(function($name,$caption,$value){
83+
return new HtmlFormCheckbox($name,$caption,$value);
84+
}, $index,$attributes);
85+
}
86+
87+
public function fieldAsDropDown($index,$elements=[],$multiple=false,$attributes=NULL){
88+
return $this->_fieldAs(function($name,$caption,$value) use ($elements,$multiple){
89+
return new HtmlFormDropdown($name,$elements,$caption,$value,$multiple);
90+
}, $index,$attributes);
91+
}
92+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
12+
public function __construct($instance=NULL, $captions=NULL) {
13+
parent::__construct($instance=NULL, $captions=NULL);
14+
$this->separators=[-1];
15+
}
16+
17+
protected function _beforeAddProperty($index,&$field){
18+
if(JString::endswith($field, "\n")===true){
19+
$this->addSeparatorAfter($index);
20+
}
21+
if($index>1 && JString::startswith($field, "\n")===true){
22+
$this->addSeparatorAfter($index-1);
23+
}
24+
$field=\str_replace("\n", "", $field);
25+
}
26+
27+
protected function _getDefaultValue($name,$value,$index){
28+
$caption=$this->getCaption($index);
29+
$input=new HtmlFormInput($name,$caption,"text",$value);
30+
return $input;
31+
}
32+
33+
public function getFieldName($index){
34+
$property=$this->getProperty($index);
35+
if($property instanceof \ReflectionProperty){
36+
$result=$property->getName();
37+
}elseif(\is_callable($property)){
38+
$result=$this->visibleProperties[$index];
39+
}else{
40+
$result=\strtolower($this->getCaption($index));
41+
}
42+
return $result;
43+
}
44+
45+
public function addSeparatorAfter($fieldNum){
46+
if(\array_search($fieldNum, $this->separators)===false)
47+
$this->separators[]=$fieldNum;
48+
return $this;
49+
}
50+
51+
public function getSeparators() {
52+
return $this->separators;
53+
}
54+
55+
public function setSeparators($separators) {
56+
$this->separators=\array_merge([-1], $separators);
57+
return $this;
58+
}
59+
60+
}

0 commit comments

Comments
 (0)