Completed
Push — master ( a95fe7...9db12b )
by Jean-Christophe
03:11
created

HtmlViewItem::addImage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
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
use Ajax\semantic\html\elements\HtmlIcon;
16
17
abstract class HtmlViewItem extends HtmlSemDoubleElement {
18
	use ContentPartTrait;
19
20
	public function __construct($identifier,$baseClass,$content=NULL) {
21
		parent::__construct($identifier, "div", $baseClass);
22
		$this->content=["content"=>new HtmlViewContent("content-".$this->identifier)];
23
		if(isset($content))
24
			$this->setContent($content);
25
	}
26
27
	public function setContent($value){
28
		if (\is_array($value)) {
29
			$image=JArray::getValue($value, "image", 0);
30
			$content=JArray::getValue($value, "content", 1);
31
			$extra=JArray::getValue($value, "extra", 2);
32
			if (isset($image)) {
33
				$this->addImage($image);
34
			}
35
			if(isset($content))
36
				$this->content["content"]->setContent($content);
37
			if(isset($extra))
38
				$this->addExtraContent($extra);
39
		}
40
	}
41
42 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...
43
		$count=\sizeof($this->content);
44
		$result=new HtmlViewContent("content-" . $count . "-" . $this->identifier, $content);
45
		$result->setClass($baseClass);
46
		return $result;
47
	}
48
49
	private function addElementIn($key, $element) {
50
		if (\array_key_exists($key, $this->content) === false) {
51
			$this->content[$key]=[];
52
		}
53
		if($this->content[$key] instanceof HtmlViewContent)
54
			$this->content[$key]->addElement($element);
55
		else
56
			$this->content[$key][]=$element;
57
		return $element;
58
	}
59
60
	public function addIcon($icon,$before=true){
61
		return $this->addElementIn("icon",new HtmlIcon("icon-" . $this->identifier, $icon));
62
	}
63
64
65
	public function addHeader($header, $niveau=4, $type="page") {
66
		if (!$header instanceof HtmlHeader) {
67
			$header=new HtmlHeader("header-" . $this->identifier, $niveau, $header, $type);
68
		}
69
		return $this->addElementIn("header",$this->createContent($header));
70
	}
71
72
	public function addImage($image, $title="") {
73
		if (!$image instanceof HtmlImage) {
74
			$image=new HtmlImage("image-" . $this->identifier, $image, $title);
75
		}
76
		$image->setClass("ui image");
77
		return $this->content["image"]= $image;
78
	}
79
80
	public function addReveal($visibleContent, $hiddenContent=NULL, $type=RevealType::FADE, $attributeType=NULL,$key="extra-content") {
81
		$reveal=$visibleContent;
82
		if (!$visibleContent instanceof HtmlReveal) {
83
			$reveal=new HtmlReveal("reveral-" . $this->identifier, $visibleContent, $hiddenContent, $type, $attributeType);
84
		}
85
		return $this->content[$key]= $reveal;
86
	}
87
88
	public function addRevealImage($visibleContent, $hiddenContent=NULL, $type=RevealType::FADE, $attributeType=NULL) {
89
		$reveal=$visibleContent;
90
		if (!$visibleContent instanceof HtmlReveal) {
91
			return $this->addReveal(new HtmlImage("", $visibleContent), new HtmlImage("", $hiddenContent), $type, $attributeType);
92
		}
93
		return $this->content["image"]= $reveal;
94
	}
95
96
	public function addExtraContent($content=array()) {
97
		return $this->content["extra-content"]= $this->createContent($content, "extra content");
98
	}
99
100
	/*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...
101
		if (!$content instanceof HtmlViewContent) {
102
			$content=$this->createContent($content);
103
		}
104
		$this->content["content"]->addElement($content);
105
		return $content;
106
	}*/
107
108
	/**
109
	 * @param array $elements
110
	 * @param boolean $asIcons
111
	 * @param string $part
112
	 * @param boolean $before
113
	 * @return HtmlButtonGroups
114
	 */
115
	public function addContentButtons($elements=array(), $asIcons=false,$part="extra",$before=false){
116
		return $this->content["content"]->addContentButtons($elements,$asIcons,$part, $before);
117
	}
118
119
120
121
	public function addItemHeaderContent($header, $metas=array(), $description=NULL,$extra=NULL) {
122
		return $this->content["content"]->addHeaderContent($header, $metas, $description,$extra);
123
	}
124
125
	public function addItemContent($content=array()) {
126
		$count=\sizeof($this->content);
127
		return $this->addElementIn("content", new HtmlViewContent("content-" . $count . "-" . $this->identifier, $content));
128
	}
129
130
	public function getItemContent() {
131
		return $this->getPart("content",null,true);
132
	}
133
134
	public function getItemExtraContent() {
135
		return $this->getPart("extra-content");
136
	}
137
138
	public function getItemImage() {
139
		return $this->getPart("image");
140
	}
141
142
	public function getItemHeader() {
143
		return $this->getPart("header");
144
	}
145
146
	/**
147
	 *
148
	 * {@inheritDoc}
149
	 *
150
	 * @see HtmlSemDoubleElement::compile()
151
	 */
152
	public function compile(JsUtils $js=NULL, &$view=NULL) {
153
		$this->content=JArray::sortAssociative($this->content, ["header","image","icon","content","extra-content"]);
154
		return parent::compile($js, $view);
155
	}
156
157
	public function asLink($href="",$target=NULL) {
158
		$this->addToProperty("class", "link");
159
		if ($href !== "") {
160
			$this->setProperty("href", $href);
161
			if (isset($target))
162
				$this->setProperty("target", $target);
163
		}
164
		return $this;
165
	}
166
}
167