Passed
Push — master ( 9a9b82...74dc3c )
by Jean-Christophe
02:28
created

DataElement::setColCaptionWidth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
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
use Ajax\service\JString;
12
13
/**
14
 * DataElement widget for displaying an instance of model
15
 * @version 1.0
16
 * @author jc
17
 * @since 2.2
18
 *
19
 */
20
class DataElement extends Widget {
21
	use BaseTrait;
22
	protected $_colWidths;
23
24
	public function __construct($identifier, $modelInstance=NULL) {
25
		parent::__construct($identifier, null,$modelInstance);
26
		$this->_init(new DeInstanceViewer($identifier), "table", new HtmlTable($identifier, 0,2), false);
27
		$this->content["table"]->setDefinition()->addClass("_element");
28
	}
29
30
	public function compile(JsUtils $js=NULL,&$view=NULL){
31
		if(!$this->_generated){
32
			$this->_instanceViewer->setInstance($this->_modelInstance);
33
34
			$table=$this->content["table"];
35
			$this->_generateContent($table);
36
37
			if(isset($this->_toolbar)){
38
				$this->_setToolbarPosition($table);
39
			}
40
			if(isset($this->_colWidths)){
41
				$this->_applyStyleAttributes($table);
42
			}
43
			$this->content=JArray::sortAssociative($this->content, [PositionInTable::BEFORETABLE,"table",PositionInTable::AFTERTABLE]);
44
			$this->_compileForm();
45
			$this->_generated=true;
46
		}
47
		return parent::compile($js,$view);
48
	}
49
50
	/**
51
	 * @param HtmlTable $table
52
	 */
53
	protected function _generateContent($table){
54
		$values= $this->_instanceViewer->getValues();
55
		$captions=$this->_instanceViewer->getCaptions();
56
		$fields=$this->_instanceViewer->getVisibleProperties();
57
		$count=$this->_instanceViewer->count();
58
		$this->setProperty("data-ajax", $this->_instanceViewer->getIdentifier());
59
		for($i=0;$i<$count;$i++){
60
			$row=$table->addRow([$captions[$i],$values[$i]]);
61
			$row->getItem(1)->setProperty("data-field", $fields[$i]);
62
		}
63
	}
64
	
65
	public function getFieldValue($index){
66
		if(is_string($index)){
67
			$fields=$this->_instanceViewer->getVisibleProperties();
68
			$index=array_search($index, $fields);
69
		}
70
		if(is_numeric($index)){
71
			$values= $this->_instanceViewer->getValues();
72
			if(isset($values[$index])){
73
				return $values[$index];
74
			}
75
		}
76
		return null;
77
	}
78
79
	protected function _applyStyleAttributes(HtmlTable $table){
80
		$table->setColWidths($this->_colWidths);
81
	}
82
	protected function _getFieldName($index){
83
		return $this->_instanceViewer->getFieldName($index);
84
	}
85
86
	protected function _getFieldCaption($index){
87
		return null;
88
	}
89
90
	protected function _getFieldIdentifier($prefix,$name=""){
91
		return $this->identifier."-{$prefix}-".$name;
92
	}
93
94
	/**
95
	 * {@inheritDoc}
96
	 * @see \Ajax\common\Widget::getHtmlComponent()
97
	 * @return HtmlTable
98
	 */
99
	public function getHtmlComponent() {
100
		return $this->content["table"];
101
	}
102
103
	/**
104
	 * {@inheritdoc}
105
	 * @see \Ajax\common\Widget::_setToolbarPosition()
106
	 */
107
	protected function _setToolbarPosition($table, $captions=NULL) {
108
		$this->content[$this->_toolbarPosition]=$this->_toolbar;
109
	}
110
111
	/**
112
	 * The callback function called after the insertion of each row when fromDatabaseObjects is called
113
	 * callback function takes the parameters $row : the row inserted and $object: the instance of model used
114
	 * @param callable $callback
115
	 * @return DataElement
116
	 */
117
	public function onNewRow($callback) {
118
		$this->content["table"]->onNewRow($callback);
119
		return $this;
120
	}
121
122
	public function asForm(){
123
		return $this->getForm();
124
	}
125
126
	public function setColCaptionWidth($width){
127
		$this->_colWidths[0]=$width;
128
		return $this;
129
	}
130
131
	public function setColValueWidth($width) {
132
		$this->_colWidths[1]=$width;
133
		return $this;
134
	}
135
136
	public function setColWidths($widths){
137
		$this->_colWidths=$widths;
138
		return $this;
139
	}
140
	
141
	public function run(JsUtils $js){
142
		if(JString::isNotNull($this->_identifier))
143
			$js->execOn("click", "#".$this->_identifier." .ui.toggle", 'var active=$(this).hasClass("active");$(this).children("i").toggleClass("up",active).toggleClass("down",!active);var nextTd=$(this).closest("td").next("td");nextTd.children(":not(.toggle-caption)").toggle(active);nextTd.children(".toggle-caption").toggle(!active);$(this).trigger({type:"toggled",active: active,caption: nextTd.children(".toggle-caption")});');
144
		parent::run($js);
145
	}
146
}
147