Completed
Push — master ( b1fd35...62c434 )
by Jean-Christophe
03:27
created

HtmlViewItem::addElementIn()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 2
1
<?php
2
3
namespace Ajax\semantic\html\content\view;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
use Ajax\semantic\html\elements\HtmlHeader;
7
use Ajax\JsUtils;
8
use Ajax\service\JArray;
9
10
use Ajax\semantic\html\elements\HtmlImage;
11
use Ajax\semantic\html\elements\HtmlReveal;
12
use Ajax\semantic\html\base\constants\RevealType;
13
use Ajax\semantic\html\elements\HtmlButtonGroups;
14
use Ajax\semantic\html\content\view\HtmlViewContent;
15
16
abstract class HtmlViewItem extends HtmlSemDoubleElement {
17
	use ContentPartTrait;
18
19
	public function __construct($identifier,$baseClass,$content=NULL) {
20
		parent::__construct($identifier, "div", $baseClass);
21
		$this->content=["content"=>new HtmlViewContent("content-".$this->identifier)];
22
		if(isset($content))
23
			$this->setContent($content);
24
	}
25
26
	public function setContent($value){
27
		if (\is_array($value)) {
28
			$image=JArray::getValue($value, "image", 0);
29
			$content=JArray::getValue($value, "content", 1);
30
			$extra=JArray::getValue($value, "extra", 2);
31
			if (isset($image)) {
32
				$this->addImage($image);
33
			}
34
			if(isset($content))
35
				$this->content["content"]->setContent($content);
36
			if(isset($extra))
37
				$this->addExtraContent($extra);
38
		}
39
	}
40
41 View Code Duplication
	private function createContent($content, $baseClass="content") {
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...
42
		$count=\sizeof($this->content);
43
		$result=new HtmlViewContent("content-" . $count . "-" . $this->identifier, $content);
44
		$result->setClass($baseClass);
45
		return $result;
46
	}
47
48
	private function addElementIn($key, $element) {
49
		if (\array_key_exists($key, $this->content) === false) {
50
			$this->content[$key]=[];
51
		}
52
		if($this->content[$key] instanceof HtmlViewContent)
53
			$this->content[$key]->addElement($element);
54
		else
55
			$this->content[$key][]=$element;
56
		return $element;
57
	}
58
59
60
	public function addHeader($header, $niveau=4, $type="page") {
61
		if (!$header instanceof HtmlHeader) {
62
			$header=new HtmlHeader("header-" . $this->identifier, $niveau, $header, $type);
63
		}
64
		return $this->addElementIn("header",$this->createContent($header));
65
	}
66
67
	public function addImage($image, $title="") {
68
		if (!$image instanceof HtmlImage) {
69
			$image=new HtmlImage("image-" . $this->identifier, $image, $title);
70
		}
71
		$image->setClass("ui image");
72
		return $this->content["image"]= $image;
73
	}
74
75
	public function addReveal($visibleContent, $hiddenContent=NULL, $type=RevealType::FADE, $attributeType=NULL,$key="extra-content") {
76
		$reveal=$visibleContent;
77
		if (!$visibleContent instanceof HtmlReveal) {
78
			$reveal=new HtmlReveal("reveral-" . $this->identifier, $visibleContent, $hiddenContent, $type, $attributeType);
79
		}
80
		return $this->content[$key]= $reveal;
81
	}
82
83
	public function addRevealImage($visibleContent, $hiddenContent=NULL, $type=RevealType::FADE, $attributeType=NULL) {
84
		$reveal=$visibleContent;
85
		if (!$visibleContent instanceof HtmlReveal) {
86
			return $this->addReveal(new HtmlImage("", $visibleContent), new HtmlImage("", $hiddenContent), $type, $attributeType);
87
		}
88
		return $this->content["image"]= $reveal;
89
	}
90
91
	public function addExtraContent($content=array()) {
92
		return $this->content["extra-content"]= $this->createContent($content, "extra content");
93
	}
94
95
	/*public function addContent($content=array(), $before=false) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
96
		if (!$content instanceof HtmlViewContent) {
97
			$content=$this->createContent($content);
98
		}
99
		$this->content["content"]->addElement($content);
100
		return $content;
101
	}*/
102
103
	/**
104
	 * @param array $elements
105
	 * @param boolean $asIcons
106
	 * @param string $part
107
	 * @param boolean $before
108
	 * @return HtmlButtonGroups
109
	 */
110
	public function addContentButtons($elements=array(), $asIcons=false,$part="extra",$before=false){
111
		return $this->content["content"]->addContentButtons($elements,$asIcons,$part, $before);
112
	}
113
114
115
116
	public function addItemHeaderContent($header, $metas=array(), $description=NULL,$extra=NULL) {
117
		return $this->content["content"]->addHeaderContent($header, $metas, $description,$extra);
118
	}
119
120
	public function addItemContent($content=array()) {
121
		$count=\sizeof($this->content);
122
		return $this->addElementIn("content", new HtmlViewContent("content-" . $count . "-" . $this->identifier, $content));
123
	}
124
125
	public function getItemContent() {
126
		return $this->getPart("content",null,true);
127
	}
128
129
	public function getItemExtraContent() {
130
		return $this->getPart("extra-content");
131
	}
132
133
	public function getItemImage() {
134
		return $this->getPart("image");
135
	}
136
137
	public function getItemHeader() {
138
		return $this->getPart("header");
139
	}
140
141
	/**
142
	 *
143
	 * {@inheritDoc}
144
	 *
145
	 * @see HtmlSemDoubleElement::compile()
146
	 */
147
	public function compile(JsUtils $js=NULL, &$view=NULL) {
148
		$this->content=JArray::sortAssociative($this->content, ["header","image","content","extra-content"]);
149
		return parent::compile($js, $view);
150
	}
151
152
	public function asLink($href="",$target=NULL) {
153
		$this->addToProperty("class", "link");
154
		if ($href !== "") {
155
			$this->setProperty("href", $href);
156
			if (isset($target))
157
				$this->setProperty("target", $target);
158
		}
159
		return $this;
160
	}
161
}