Completed
Push — master ( b7a7dd...b1fd35 )
by Jean-Christophe
03:34
created

HtmlViewContent::addHeaderContent()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 8
nop 4
1
<?php
2
3
namespace Ajax\semantic\html\content\view;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
use Ajax\semantic\html\base\constants\Direction;
7
use Ajax\semantic\html\elements\HtmlIcon;
8
use Ajax\semantic\html\elements\html5\HtmlImg;
9
use Ajax\JsUtils;
10
use Ajax\service\JReflection;
11
use Ajax\service\JArray;
12
13
class HtmlViewContent extends HtmlSemDoubleElement {
14
15
	public function __construct($identifier, $content=array()) {
16
		parent::__construct($identifier, "div", "content",[]);
17
		$this->setContent($content);
18
	}
19
20
	public function setContent($value){
21
		if (\is_array($value)) {
22
			$header=JArray::getValue($value, "header", 0);
23
			$metas=JArray::getValue($value, "metas", 1);
24
			$description=JArray::getValue($value, "description", 2);
25
			$image=JArray::getValue($value, "image", 3);
26
			$extra=JArray::getValue($value, "extra", 4);
27
			if (isset($image)) {
28
				$this->addImage($image);
29
			}
30
			$this->addHeaderContent($header, $metas, $description,$extra);
31
		} else
32
			$this->addContent($value);
33
	}
34
35
	public function addElement($content, $baseClass="") {
36
		$count=\sizeof($this->content);
37
		$result=new HtmlViewContent("element-" . $count . "-" . $this->identifier, $content);
38
		$result->setClass($baseClass);
39
		$this->addContent($result);
40
		return $result;
41
	}
42
43
	public function addMeta($value, $direction=Direction::LEFT) {
44
		if (\array_key_exists("meta", $this->content) === false) {
45
			$this->content["meta"]=new HtmlSemDoubleElement("meta-" . $this->identifier, "div", "meta", array ());
46
		}
47
		if ($direction === Direction::RIGHT) {
48
			$value=new HtmlSemDoubleElement("", "span", "", $value);
49
			$value->setFloated($direction);
50
		}
51
		$this->content["meta"]->addContent($value);
52
		return $this->content["meta"];
53
	}
54
55
	public function addExtra($value) {
56
		if (\array_key_exists("extra", $this->content) === false) {
57
			$this->content["extra"]=new HtmlSemDoubleElement("extra-" . $this->identifier, "div", "extra", array ());
58
		}
59
		$this->content["extra"]->addContent($value);
60
		return $this->content["extra"];
61
	}
62
63
	public function addImage($src="", $alt="", $size=NULL) {
64
		$image=new HtmlImg("img-", $src, $alt);
65
		if (isset($size))
66
			$image->setSize($size);
67
		$this->content['image']=$image;
68
		return $image;
69
	}
70
71
	public function addMetas($metas) {
72
		if (\is_array($metas)) {
73
			foreach ( $metas as $meta ) {
74
				$this->addMeta($meta);
75
			}
76
		} else
77
			$this->addMeta($metas);
78
		return $this;
79
	}
80
81
	public function addContentIcon($icon, $caption=NULL, $direction=Direction::LEFT) {
82
		if ($direction === Direction::RIGHT) {
83
			if (isset($caption)) {
84
				$result=new HtmlSemDoubleElement("", "span", "", $caption);
85
				$result->addIcon($icon);
86
				$this->addContent($result);
87
			} else {
88
				$result=new HtmlIcon("", $icon);
89
				$this->addContent($result);
90
			}
91
			$result->setFloated($direction);
92
		} else {
93
			$this->addIcon($icon);
94
			$result=$this->addContent($caption);
95
		}
96
		return $result;
97
	}
98
99
	public function addContentText($caption, $direction=Direction::LEFT) {
100
		if ($direction === Direction::RIGHT) {
101
			$result=new HtmlSemDoubleElement("", "span", "", $caption);
102
			$this->addContent($result);
103
			$result->setFloated($direction);
104
		} else
105
			$result=$this->addContent($caption);
106
		return $result;
107
	}
108
109
	public function addContentIcons($icons, $direction=Direction::LEFT) {
110
		foreach ( $icons as $icon ) {
111
			$this->addContentIcon($icon, NULL, $direction);
112
		}
113
		return $this;
114
	}
115
116
	public function addHeaderContent($header, $metas=array(), $description=NULL,$extra=NULL) {
117
		if(isset($header))
118
			$this->addElement($header, "header");
119
		$this->addMetas($metas);
120
		if (isset($description)) {
121
			$this->addElement($description, "description");
122
		}
123
		if(isset($extra)){
124
			$this->addExtra($extra);
125
		}
126
		return $this;
127
	}
128
129 View Code Duplication
	public function getPart($part, $index=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...
130
		if($this->content instanceof HtmlViewContent){
131
			return $this->content->getPart($part,$index);
132
		}
133
		if (\array_key_exists($part, $this->content)) {
134
			if (isset($index))
135
				return $this->content[$part][$index];
136
				return $this->content[$part];
137
		}
138
		return NULL;
139
	}
140
141
	public function compile(JsUtils $js=NULL, &$view=NULL) {
142
		//$this->content=JArray::sortAssociative($this->content, [ "header","meta","description","extra" ]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
143
		return parent::compile($js, $view);
144
	}
145
}