Completed
Push — master ( 5f7aee...f5f495 )
by Jean-Christophe
03:30
created

DataElement::setColCaptionWidth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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
11
/**
12
 * DataElement widget for displaying an instance of model
13
 * @version 1.0
14
 * @author jc
15
 * @since 2.2
16
 *
17
 */
18
class DataElement extends Widget {
19
	protected $_colWidths;
20
21
	public function __construct($identifier, $modelInstance=NULL) {
22
		parent::__construct($identifier, null,$modelInstance);
23
		$this->_init(new DeInstanceViewer($identifier), "table", new HtmlTable($identifier, 0,2), false);
24
		$this->content["table"]->setDefinition();
25
	}
26
27
	public function compile(JsUtils $js=NULL,&$view=NULL){
28
		if(!$this->_generated){
29
			$this->_instanceViewer->setInstance($this->_modelInstance);
30
31
			$table=$this->content["table"];
32
			$this->_generateContent($table);
33
34
			if(isset($this->_toolbar)){
35
				$this->_setToolbarPosition($table);
36
			}
37
			if(isset($this->_colWidths)){
38
				$this->_applyStyleAttributes($table);
39
			}
40
			$this->content=JArray::sortAssociative($this->content, [PositionInTable::BEFORETABLE,"table",PositionInTable::AFTERTABLE]);
41
			$this->_compileForm();
42
			$this->_generated=true;
43
		}
44
		return parent::compile($js,$view);
45
	}
46
47
	/**
48
	 * @param HtmlTable $table
49
	 */
50
	protected function _generateContent($table){
51
		$values= $this->_instanceViewer->getValues();
52
		$captions=$this->_instanceViewer->getCaptions();
53
		$count=$this->_instanceViewer->count();
54
55
		for($i=0;$i<$count;$i++){
56
			$table->addRow([$captions[$i],$values[$i]]);
57
		}
58
	}
59
60
	protected function _applyStyleAttributes(HtmlTable $table){
61
		$table->setColWidths($this->_colWidths);
62
	}
63
	protected function _getFieldName($index){
64
		return $this->_instanceViewer->getFieldName($index);
65
	}
66
67
	protected function _getFieldCaption($index){
68
		return null;
69
	}
70
71
	protected function _getFieldIdentifier($prefix,$name=""){
72
		return $this->identifier."-{$prefix}-".$name;
73
	}
74
75
	/**
76
	 * {@inheritDoc}
77
	 * @see \Ajax\common\Widget::getHtmlComponent()
78
	 * @return HtmlTable
79
	 */
80
	public function getHtmlComponent() {
81
		return $this->content["table"];
82
	}
83
84
	/**
85
	 * {@inheritdoc}
86
	 * @see \Ajax\common\Widget::_setToolbarPosition()
87
	 */
88
	protected function _setToolbarPosition($table, $captions=NULL) {
89
		$this->content[$this->_toolbarPosition]=$this->_toolbar;
90
	}
91
92
	/**
93
	 * The callback function called after the insertion of each row when fromDatabaseObjects is called
94
	 * callback function takes the parameters $row : the row inserted and $object: the instance of model used
95
	 * @param callable $callback
96
	 * @return DataElement
97
	 */
98
	public function onNewRow($callback) {
99
		$this->content["table"]->onNewRow($callback);
100
		return $this;
101
	}
102
103
	public function asForm(){
104
		return $this->getForm();
105
	}
106
107
	public function setColCaptionWidth($width){
108
		$this->_colWidths[0]=$width;
109
		return $this;
110
	}
111
112
	public function setColValueWidth($width) {
113
		$this->_colWidths[1]=$width;
114
		return $this;
115
	}
116
117
	public function setColWidths($widths){
118
		$this->_colWidths=$widths;
119
		return $this;
120
	}
121
}