1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\semantic\widgets\dataelement; |
4
|
|
|
|
5
|
|
|
use Ajax\common\Widget; |
6
|
|
|
use Ajax\semantic\widgets\base\InstanceViewer; |
7
|
|
|
use Ajax\semantic\widgets\datatable\PositionInTable; |
8
|
|
|
use Ajax\JsUtils; |
9
|
|
|
use Ajax\service\JArray; |
10
|
|
|
use Ajax\semantic\html\collections\table\HtmlTable; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* DataElement widget for displaying an instance of model |
14
|
|
|
* @version 1.0 |
15
|
|
|
* @author jc |
16
|
|
|
* @since 2.2 |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
class DataElement extends Widget { |
20
|
|
|
|
21
|
|
|
public function __construct($identifier, $modelInstance=NULL) { |
22
|
|
|
parent::__construct($identifier, null,$modelInstance); |
23
|
|
|
$this->_init(new InstanceViewer($identifier), "table", new HtmlTable($identifier, 0,2), false); |
24
|
|
|
$this->content["table"]->setDefinition(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
View Code Duplication |
public function compile(JsUtils $js=NULL,&$view=NULL){ |
|
|
|
|
28
|
|
|
$this->_instanceViewer->setInstance($this->_modelInstance); |
29
|
|
|
|
30
|
|
|
$table=$this->content["table"]; |
31
|
|
|
$this->_generateContent($table); |
32
|
|
|
|
33
|
|
|
if(isset($this->_toolbar)){ |
34
|
|
|
$this->_setToolbarPosition($table); |
35
|
|
|
} |
36
|
|
|
$this->content=JArray::sortAssociative($this->content, [PositionInTable::BEFORETABLE,"table",PositionInTable::AFTERTABLE]); |
37
|
|
|
return parent::compile($js,$view); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param HtmlTable $table |
42
|
|
|
*/ |
43
|
|
|
protected function _generateContent($table){ |
44
|
|
|
$captions=$this->_instanceViewer->getCaptions(); |
45
|
|
|
$values= $this->_instanceViewer->getValues(); |
46
|
|
|
$count=$this->_instanceViewer->count(); |
47
|
|
|
for($i=0;$i<$count;$i++){ |
48
|
|
|
$table->addRow([$captions[$i],$values[$i]]); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function _getFieldName($index){ |
53
|
|
|
return $this->_instanceViewer->getFieldName($index); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function _getFieldCaption($index){ |
57
|
|
|
return null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritDoc} |
62
|
|
|
* @see \Ajax\common\Widget::getHtmlComponent() |
63
|
|
|
* @return HtmlTable |
64
|
|
|
*/ |
65
|
|
|
public function getHtmlComponent() { |
66
|
|
|
return $this->content["table"]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
* @see \Ajax\common\Widget::_setToolbarPosition() |
72
|
|
|
*/ |
73
|
|
|
protected function _setToolbarPosition($table, $captions=NULL) { |
74
|
|
|
$this->content[$this->_toolbarPosition]=$this->_toolbar; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* The callback function called after the insertion of each row when fromDatabaseObjects is called |
79
|
|
|
* callback function takes the parameters $row : the row inserted and $object: the instance of model used |
80
|
|
|
* @param callable $callback |
81
|
|
|
* @return DataElement |
82
|
|
|
*/ |
83
|
|
|
public function onNewRow($callback) { |
84
|
|
|
$this->content["table"]->onNewRow($callback); |
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.