Passed
Push — master ( 18a30f...0da08b )
by Jean-Christophe
07:40
created

HtmlMessage::setError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Ajax\semantic\html\collections;
3
4
use Ajax\semantic\html\base\HtmlSemDoubleElement;
5
use Ajax\semantic\html\elements\HtmlIcon;
6
use Ajax\JsUtils;
7
use Ajax\semantic\html\base\constants\Style;
8
use Ajax\semantic\html\base\traits\AttachedTrait;
9
use Ajax\semantic\html\base\traits\HasTimeoutTrait;
10
11
/**
12
 * Semantic Message component
13
 *
14
 * @see http://semantic-ui.com/collections/message.html
15
 * @author jc
16
 * @version 1.001
17
 */
18
class HtmlMessage extends HtmlSemDoubleElement {
19
	use AttachedTrait,HasTimeoutTrait;
20
21
	protected $icon;
22
23
	protected $close;
24
25
	public function __construct($identifier, $content = "") {
26
		parent::__construct($identifier, "div");
27
		$this->_template = '<%tagName% id="%identifier%" %properties%>%close%%icon%%wrapContentBefore%%content%%wrapContentAfter%</%tagName%>';
28
		$this->setClass("ui message");
29
		$this->setContent($content);
30
	}
31
32
	/**
33
	 * Adds an header to the message
34
	 *
35
	 * @param string|HtmlSemDoubleElement $header
36
	 * @return \Ajax\semantic\html\collections\HtmlMessage
37
	 */
38
	public function addHeader($header) {
39
		$headerO = $header;
40
		if (\is_string($header)) {
41
			$headerO = new HtmlSemDoubleElement("header-" . $this->identifier, "div");
42
			$headerO->setClass("header");
43
			$headerO->setContent($header);
44
		}
45
		return $this->addContent($headerO, true);
46
	}
47
48
	public function setHeader($header) {
49
		return $this->addHeader($header);
50
	}
51
52
	public function setIcon($icon) {
53
		$this->addToProperty("class", "icon");
54
		$this->wrapContent("<div class='content'>", "</div>");
55
		if (\is_string($icon)) {
56
			$this->icon = new HtmlIcon("icon-" . $this->identifier, $icon);
57
		} else {
58
			$this->icon = $icon;
59
		}
60
		return $this;
61
	}
62
63
	/**
64
	 * Performs a get to $url on the click event on close button
65
	 * and display it in $responseElement
66
	 *
67
	 * @param string $url
68
	 *        	The url of the request
69
	 * @param string $responseElement
70
	 *        	The selector of the HTML element displaying the answer
71
	 * @param array $parameters
72
	 *        	default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null,"historize"=>false)
73
	 * @return $this
74
	 */
75
	public function getOnClose($url, $responseElement = "", $parameters = array()) {
76
		if (isset($this->close)) {
77
			$this->close->getOnClick($url, $responseElement, $parameters);
78
		}
79
	}
80
81
	public function addLoader($loaderIcon = "notched circle") {
82
		$this->setIcon($loaderIcon);
83
		$this->icon->addToIcon("loading");
84
		return $this;
85
	}
86
87
	public function setDismissable($dismiss = true) {
88
		if ($dismiss === true)
89
			$this->close = new HtmlIcon("close-" . $this->identifier, "close");
90
		else
91
			$this->close = NULL;
92
		return $this;
93
	}
94
95
	/**
96
	 *
97
	 * {@inheritdoc}
98
	 * @see \Ajax\semantic\html\base\HtmlSemDoubleElement::run()
99
	 */
100
	public function run(JsUtils $js) {
101
		if (! isset($this->_bsComponent)) {
102
			if (isset($this->close)) {
103
				$js->execOn("click", "#" . $this->identifier . " .close", "$(this).closest('.message').transition({$this->_closeTransition})");
104
			}
105
			if (isset($this->_timeout)) {
106
				$js->exec("setTimeout(function() { $('#{$this->identifier}').transition({$this->_closeTransition}); }, {$this->_timeout});", true);
107
			}
108
		}
109
		return parent::run($js);
110
	}
111
112
	public function setState($visible = true) {
113
		$visible = ($visible === true) ? "visible" : "hidden";
114
		return $this->addToPropertyCtrl("class", $visible, array(
115
			"visible",
116
			"hidden"
117
		));
118
	}
119
120
	public function setVariation($value = "floating") {
121
		return $this->addToPropertyCtrl("class", $value, array(
122
			"floating",
123
			"compact"
124
		));
125
	}
126
127
	public function setStyle($style) {
128
		return $this->addToPropertyCtrl("class", $style, Style::getConstants());
129
	}
130
131
	public function setError() {
132
		return $this->setStyle("error");
133
	}
134
135
	public function setWarning() {
136
		return $this->setStyle("warning");
137
	}
138
139
	public function setMessage($message) {
140
		if (\is_array($this->content)) {
141
			$this->content[\sizeof($this->content) - 1] = $message;
142
		} else
143
			$this->setContent($message);
144
	}
145
}
146