Completed
Push — master ( f2779d...72aa1a )
by Jean-Christophe
03:29
created

DataElement::_generateContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
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\semantic\html\collections\HtmlTable;
9
use Ajax\JsUtils;
10
use Ajax\service\JArray;
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 View Code Duplication
	public function __construct($identifier, $modelInstance=NULL) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
22
		parent::__construct($identifier, null,$modelInstance);
23
		$this->_instanceViewer=new InstanceViewer();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Ajax\semantic\widgets\base\InstanceViewer() of type object<Ajax\semantic\widgets\base\InstanceViewer> is incompatible with the declared type object<Ajax\common\InstanceViewer> of property $_instanceViewer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
24
		$this->content=["table"=>new HtmlTable($identifier, 0,2)];
25
		$this->content["table"]->setDefinition();
26
		$this->_toolbarPosition=PositionInTable::BEFORETABLE;
27
	}
28
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
	 */
59
	public function getHtmlComponent() {
60
		return $this->content["table"];
61
	}
62
63
	/**
64
	 * {@inheritdoc}
65
	 * @see \Ajax\common\Widget::_setToolbarPosition()
66
	 */
67
	protected function _setToolbarPosition($table, $captions=NULL) {
0 ignored issues
show
Unused Code introduced by
The parameter $table is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $captions is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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;
80
	}
81
}