Passed
Push — master ( 0c3e91...9673ec )
by Jean-Christophe
02:10
created

HtmlAbsractItem::addContent()   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 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ajax\semantic\html\content;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
use Ajax\semantic\html\elements\HtmlIcon;
7
use Ajax\JsUtils;
8
9
use Ajax\service\JArray;
10
use Ajax\semantic\html\elements\html5\HtmlImg;
11
12
abstract class HtmlAbsractItem extends HtmlSemDoubleElement {
13
14
	public function __construct($identifier, $baseClass,$content=NULL) {
15
		parent::__construct($identifier, "div", $baseClass);
16
		$this->content=array();
17
		$this->initContent($content);
18
	}
19
20
	abstract protected function initContent($content);
21
22
	public function setIcon($icon){
23
		$this->content["icon"]=new HtmlIcon("icon-".$this->identifier, $icon);
24
	}
25
26
	public function removeIcon(){
27
		if(isset($this->content["icon"]))
28
			unset($this->content["icon"]);
29
		return $this;
30
	}
31
32
	public function setImage($image){
33
		$image=new HtmlImg("icon-".$this->identifier, $image);
34
		$image->asAvatar();
35
		$this->content["image"]=$image;
36
	}
37
38
	private function _getContent($key="content",$baseClass="content"){
39
		if(\array_key_exists($key, $this->content)===false){
40
			$this->content[$key]=new HtmlSemDoubleElement($key."-".$this->identifier,"div",$baseClass);
41
		}
42
		return $this->content[$key];
43
	}
44
	
45
	private function _getRightContent(){
46
		return $this->_getContent("right-content","right floated content");
47
	}
48
	
49
	public function addContent($content,$before=false){
50
		$this->_getContent("content")->addContent($content,$before);
51
		return $this;
52
	}
53
	
54
	public function addRightContent($content,$before=false){
55
		$this->_getRightContent()->addContent($content,$before);
56
		return $this;
57
	}
58
59
	public function setTitle($title,$description=NULL,$baseClass="title"){
60
		$title=new HtmlSemDoubleElement("","div",$baseClass,$title);
61
		$content=$this->_getContent();
62
		$content->addContent($title);
63
		if(isset($description)){
64
			$description=new HtmlSemDoubleElement("","div","description",$description);
65
			$content->addContent($description);
66
		}
67
		return $this;
68
	}
69
70
	public function getPart($partName="header"){
71
		$content=\array_merge($this->_getContent()->getContent(),array(@$this->content["icon"],@$this->content["image"]));
72
		return $this->getElementByPropertyValue("class", $partName, $content);
73
	}
74
75
	public function setActive($value=true){
76
		if($value){
77
			$this->setTagName("div");
78
			$this->removeProperty("href");
79
			$this->addToPropertyCtrl("class", "active", array("active"));
80
		}else{
81
			$this->removePropertyValue("class", "active");
82
		}
83
		return $this;
84
	}
85
86
	public function asLink($href=NULL,$part=NULL){
87
		$this->setTagName("a");
88
		if(isset($href))
89
			$this->setProperty("href", $href);
90
		return $this;
91
	}
92
93
	/**
94
	 *
95
	 * {@inheritDoc}
96
	 *
97
	 * @see \Ajax\semantic\html\base\HtmlSemDoubleElement::compile()
98
	 */
99
	public function compile(JsUtils $js=NULL, &$view=NULL) {
100
		if(\is_array($this->content) && JArray::isAssociative($this->content))
101
			$this->content=JArray::sortAssociative($this->content, [ "right-content","icon","image","content" ]);
102
		return parent::compile($js, $view);
103
	}
104
}
105