Skip to content

Commit 72aa1af

Browse files
committed
DataElement v1
1 parent f2779d0 commit 72aa1af

File tree

6 files changed

+120
-52
lines changed

6 files changed

+120
-52
lines changed

Ajax/semantic/traits/SemanticWidgetsTrait.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Ajax\semantic\traits;
33

44
use Ajax\semantic\widgets\datatable\DataTable;
5+
use Ajax\semantic\widgets\dataelement\DataElement;
56

67
trait SemanticWidgetsTrait {
78

@@ -16,4 +17,13 @@ public abstract function addHtmlComponent($htmlComponent);
1617
public function dataTable($identifier,$model, $instances){
1718
return $this->addHtmlComponent(new DataTable($identifier,$model,$instances));
1819
}
20+
21+
/**
22+
* @param string $identifier
23+
* @param object $instance
24+
* @return DataElement
25+
*/
26+
public function dataElement($identifier, $instance){
27+
return $this->addHtmlComponent(new DataElement($identifier,$instance));
28+
}
1929
}

Ajax/semantic/widgets/base/InstanceViewer.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ class InstanceViewer {
99
protected $visibleProperties;
1010
protected $values;
1111
protected $afterCompile;
12+
protected $captions;
13+
14+
1215
public static $index=0;
1316

14-
public function __construct($instance=NULL){
17+
public function __construct($instance=NULL,$captions=NULL){
1518
$this->values=[];
1619
$this->afterCompile=[];
1720
if(isset($instance))
1821
$this->setInstance($instance);
22+
$this->setCaptions($captions);
1923
}
2024

2125
public function getValues(){
@@ -169,6 +173,37 @@ public function getProperties() {
169173
return $this->properties;
170174
}
171175

176+
public function getCaption($index){
177+
if($this->properties[$index] instanceof \ReflectionProperty)
178+
return $this->properties[$index]->getName();
179+
elseif(\is_callable($this->properties[$index]))
180+
return "";
181+
else
182+
return $this->properties[$index];
183+
}
184+
185+
public function getCaptions(){
186+
if(isset($this->captions)){
187+
$result= $this->captions;
188+
for($i=\sizeof($result);$i<$this->count();$i++){
189+
$result[]="";
190+
}
191+
return $result;
192+
}
193+
$captions=[];
194+
$index=0;
195+
$count=$this->count();
196+
while($index<$count){
197+
$captions[]=$this->getCaption($index++);
198+
}
199+
return $captions;
200+
}
201+
202+
public function setCaptions($captions) {
203+
$this->captions=$captions;
204+
return $this;
205+
}
206+
172207
/**
173208
* Associates a $callback function after the compilation of the field at $index position
174209
* The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position

Ajax/semantic/widgets/base/InstanceViewerCaption.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

Ajax/semantic/widgets/dataelement/DataElement.php

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
namespace Ajax\semantic\widgets\dataelement;
44

55
use Ajax\common\Widget;
6+
use Ajax\semantic\widgets\base\InstanceViewer;
7+
use Ajax\semantic\widgets\datatable\PositionInTable;
8+
use Ajax\semantic\html\collections\HtmlTable;
9+
use Ajax\JsUtils;
10+
use Ajax\service\JArray;
611

712
/**
813
* DataElement widget for displaying an instance of model
@@ -13,11 +18,64 @@
1318
*/
1419
class DataElement extends Widget {
1520

16-
public function __construct($identifier, $model, $modelInstance=NULL) {
17-
parent::__construct($identifier, $model, $modelInstance=NULL);
21+
public function __construct($identifier, $modelInstance=NULL) {
22+
parent::__construct($identifier, null,$modelInstance);
23+
$this->_instanceViewer=new InstanceViewer();
24+
$this->content=["table"=>new HtmlTable($identifier, 0,2)];
25+
$this->content["table"]->setDefinition();
26+
$this->_toolbarPosition=PositionInTable::BEFORETABLE;
1827
}
1928

29+
public function compile(JsUtils $js=NULL,&$view=NULL){
30+
$this->_instanceViewer->setInstance($this->_modelInstance);
31+
32+
$table=$this->content["table"];
33+
$this->_generateContent($table);
34+
35+
if(isset($this->_toolbar)){
36+
$this->_setToolbarPosition($table);
37+
}
38+
$this->content=JArray::sortAssociative($this->content, [PositionInTable::BEFORETABLE,"table",PositionInTable::AFTERTABLE]);
39+
return parent::compile($js,$view);
40+
}
41+
42+
/**
43+
* @param HtmlTable $table
44+
*/
45+
protected function _generateContent($table){
46+
$captions=$this->_instanceViewer->getCaptions();
47+
$values= $this->_instanceViewer->getValues();
48+
$count=$this->_instanceViewer->count();
49+
for($i=0;$i<$count;$i++){
50+
$table->addRow([$captions[$i],$values[$i]]);
51+
}
52+
}
53+
54+
/**
55+
* {@inheritDoc}
56+
* @see \Ajax\common\Widget::getHtmlComponent()
57+
* @return HtmlTable
58+
*/
2059
public function getHtmlComponent() {
21-
// TODO Auto-generated method stub
60+
return $this->content["table"];
61+
}
62+
63+
/**
64+
* {@inheritdoc}
65+
* @see \Ajax\common\Widget::_setToolbarPosition()
66+
*/
67+
protected function _setToolbarPosition($table, $captions=NULL) {
68+
$this->content[$this->_toolbarPosition]=$this->_toolbar;
69+
}
70+
71+
/**
72+
* The callback function called after the insertion of each row when fromDatabaseObjects is called
73+
* callback function takes the parameters $row : the row inserted and $object: the instance of model used
74+
* @param callable $callback
75+
* @return DataElement
76+
*/
77+
public function onNewRow($callback) {
78+
$this->content["table"]->onNewRow($callback);
79+
return $this;
2280
}
2381
}

Ajax/semantic/widgets/dataform/DataForm.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,11 @@ class DataForm extends Widget {
1616
public function getHtmlComponent() {
1717
// TODO Auto-generated method stub
1818
}
19+
/**
20+
* {@inheritdoc}
21+
* @see \Ajax\common\Widget::_setToolbarPosition()
22+
*/
23+
protected function _setToolbarPosition($table, $captions=NULL) {
24+
// TODO: Auto-generated method stub
25+
}
1926
}

Ajax/semantic/widgets/datatable/DataTable.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Ajax\semantic\widgets\base\FieldAsTrait;
1515
use Ajax\semantic\html\base\HtmlSemDoubleElement;
1616
use Ajax\semantic\widgets\base\InstanceViewerCaption;
17+
use Ajax\semantic\widgets\base\InstanceViewer;
1718

1819
/**
1920
* DataTable widget for displaying list of objects
@@ -43,7 +44,7 @@ public function run(JsUtils $js){
4344

4445
public function __construct($identifier,$model,$modelInstance=NULL) {
4546
parent::__construct($identifier, $model,$modelInstance);
46-
$this->_instanceViewer=new InstanceViewerCaption();
47+
$this->_instanceViewer=new InstanceViewer();
4748
$this->content=["table"=>new HtmlTable($identifier, 0,0)];
4849
$this->_toolbarPosition=PositionInTable::BEFORETABLE;
4950
}
@@ -87,15 +88,15 @@ public function compile(JsUtils $js=NULL,&$view=NULL){
8788
return parent::compile($js,$view);
8889
}
8990

90-
private function _generateContent($table){
91+
protected function _generateContent($table){
9192
$objects=$this->_modelInstance;
9293
if(isset($this->_pagination)){
9394
$objects=$this->_pagination->getObjects($this->_modelInstance);
9495
}
95-
InstanceViewerCaption::setIndex(0);
96+
InstanceViewer::setIndex(0);
9697
$table->fromDatabaseObjects($objects, function($instance){
9798
$this->_instanceViewer->setInstance($instance);
98-
InstanceViewerCaption::$index++;
99+
InstanceViewer::$index++;
99100
$result= $this->_instanceViewer->getValues();
100101
if($this->_hasCheckboxes){
101102
$ck=new HtmlCheckbox("ck-".$this->identifier,"");
@@ -118,7 +119,7 @@ private function _generatePagination($table){
118119
$menu->postOnClick($this->_urls,"{'p':$(this).attr('data-page')}","-#".$this->identifier." tbody",["preventDefault"=>false]);
119120
}
120121

121-
private function _setToolbarPosition($table,$captions){
122+
protected function _setToolbarPosition($table,$captions=NULL){
122123
switch ($this->_toolbarPosition){
123124
case PositionInTable::BEFORETABLE:case PositionInTable::AFTERTABLE:
124125
if(isset($this->_compileParts)===false){

0 commit comments

Comments
 (0)