Passed
Push — master ( af3469...21fb75 )
by Jean-Christophe
02:16
created

DataElement::compile()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 5
nop 2
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Ajax\semantic\widgets\dataelement;
4
5
use Ajax\common\Widget;
6
use Ajax\semantic\widgets\datatable\PositionInTable;
7
use Ajax\JsUtils;
8
use Ajax\service\JArray;
9
use Ajax\semantic\html\collections\table\HtmlTable;
10
use Ajax\semantic\html\base\traits\BaseTrait;
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
	use BaseTrait;
21
	protected $_colWidths;
22
23
	public function __construct($identifier, $modelInstance=NULL) {
24
		parent::__construct($identifier, null,$modelInstance);
25
		$this->_init(new DeInstanceViewer($identifier), "table", new HtmlTable($identifier, 0,2), false);
26
		$this->content["table"]->setDefinition();
27
	}
28
29
	public function compile(JsUtils $js=NULL,&$view=NULL){
30
		if(!$this->_generated){
31
			$this->_instanceViewer->setInstance($this->_modelInstance);
32
33
			$table=$this->content["table"];
34
			$this->_generateContent($table);
35
36
			if(isset($this->_toolbar)){
37
				$this->_setToolbarPosition($table);
38
			}
39
			if(isset($this->_colWidths)){
40
				$this->_applyStyleAttributes($table);
41
			}
42
			$this->content=JArray::sortAssociative($this->content, [PositionInTable::BEFORETABLE,"table",PositionInTable::AFTERTABLE]);
43
			$this->_compileForm();
44
			$this->_generated=true;
45
		}
46
		return parent::compile($js,$view);
47
	}
48
49
	/**
50
	 * @param HtmlTable $table
51
	 */
52
	protected function _generateContent($table){
53
		$values= $this->_instanceViewer->getValues();
54
		$captions=$this->_instanceViewer->getCaptions();
55
		$count=$this->_instanceViewer->count();
56
57
		for($i=0;$i<$count;$i++){
58
			$table->addRow([$captions[$i],$values[$i]]);
59
		}
60
	}
61
62
	protected function _applyStyleAttributes(HtmlTable $table){
63
		$table->setColWidths($this->_colWidths);
64
	}
65
	protected function _getFieldName($index){
66
		return $this->_instanceViewer->getFieldName($index);
67
	}
68
69
	protected function _getFieldCaption($index){
70
		return null;
71
	}
72
73
	protected function _getFieldIdentifier($prefix,$name=""){
74
		return $this->identifier."-{$prefix}-".$name;
75
	}
76
77
	/**
78
	 * {@inheritDoc}
79
	 * @see \Ajax\common\Widget::getHtmlComponent()
80
	 * @return HtmlTable
81
	 */
82
	public function getHtmlComponent() {
83
		return $this->content["table"];
84
	}
85
86
	/**
87
	 * {@inheritdoc}
88
	 * @see \Ajax\common\Widget::_setToolbarPosition()
89
	 */
90
	protected function _setToolbarPosition($table, $captions=NULL) {
91
		$this->content[$this->_toolbarPosition]=$this->_toolbar;
92
	}
93
94
	/**
95
	 * The callback function called after the insertion of each row when fromDatabaseObjects is called
96
	 * callback function takes the parameters $row : the row inserted and $object: the instance of model used
97
	 * @param callable $callback
98
	 * @return DataElement
99
	 */
100
	public function onNewRow($callback) {
101
		$this->content["table"]->onNewRow($callback);
102
		return $this;
103
	}
104
105
	public function asForm(){
106
		return $this->getForm();
107
	}
108
109
	public function setColCaptionWidth($width){
110
		$this->_colWidths[0]=$width;
111
		return $this;
112
	}
113
114
	public function setColValueWidth($width) {
115
		$this->_colWidths[1]=$width;
116
		return $this;
117
	}
118
119
	public function setColWidths($widths){
120
		$this->_colWidths=$widths;
121
		return $this;
122
	}
123
	
124
	public function run(JsUtils $js){
125
		$js->execOn("click", ".ui.toggle", 'var active=$(this).hasClass("active");$(this).children("i").toggleClass("up",active).toggleClass("down",!active);$(this).closest("td").next("td").children().toggle(active);');
126
		parent::run($js);
127
	}
128
}
129