TAccordionView::getCaption()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * TAccordion class file.
5
 *
6
 * @author Gabor Berczi, DevWorx Hungary <[email protected]>
7
 * @link https://github.com/pradosoft/prado
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 * @since 3.2
10
 */
11
12
namespace Prado\Web\UI\WebControls;
13
14
use Prado\TPropertyValue;
15
16
/**
17
 * Class TAccordionView.
18
 *
19
 * TAccordionView represents a single view in a {@see \Prado\Web\UI\WebControls\TAccordion}.
20
 *
21
 * TAccordionView is represented inside the {@see \Prado\Web\UI\WebControls\TAccordion} with an header label whose text is defined by
22
 * the {@see setCaption Caption} property; optionally the label can be an hyperlink: use the
23
 * {@see setNavigateUrl NavigateUrl} property to define the destination url.
24
 *
25
 * @author Gabor Berczi, DevWorx Hungary <[email protected]>
26
 * @since 3.2
27
 * @method TAccordion getParent()
28
 */
29
class TAccordionView extends \Prado\Web\UI\WebControls\TWebControl
30
{
31
	private $_active = false;
32
33
	/**
34
	 * @return string the tag name for the view element
35
	 */
36
	protected function getTagName()
37
	{
38
		return 'div';
39
	}
40
41
	/**
42
	 * Adds attributes to renderer.
43
	 * @param \Prado\Web\UI\THtmlWriter $writer the renderer
44
	 */
45
	protected function addAttributesToRender($writer)
46
	{
47
		if (!$this->getActive() && $this->getPage()->getClientSupportsJavaScript()) {
48
			$this->getStyle()->setStyleField('display', 'none');
49
		}
50
51
		$this->getStyle()->mergeWith($this->getParent()->getViewStyle());
52
53
		parent::addAttributesToRender($writer);
54
55
		$writer->addAttribute('id', $this->getClientID());
56
	}
57
58
	/**
59
	 * @return string the caption displayed on this header. Defaults to ''.
60
	 */
61
	public function getCaption()
62
	{
63
		return $this->getViewState('Caption', '');
64
	}
65
66
	/**
67
	 * @param string $value the caption displayed on this header
68
	 */
69
	public function setCaption($value)
70
	{
71
		$this->setViewState('Caption', TPropertyValue::ensureString($value), '');
72
	}
73
74
	/**
75
	 * @return string the URL of the target page. Defaults to ''.
76
	 */
77
	public function getNavigateUrl()
78
	{
79
		return $this->getViewState('NavigateUrl', '');
80
	}
81
82
	/**
83
	 * Sets the URL of the target page.
84
	 * If not empty, clicking on this header will redirect the browser to the specified URL.
85
	 * @param string $value the URL of the target page.
86
	 */
87
	public function setNavigateUrl($value)
88
	{
89
		$this->setViewState('NavigateUrl', TPropertyValue::ensureString($value), '');
90
	}
91
92
	/**
93
	 * @return string the text content displayed on this view. Defaults to ''.
94
	 */
95
	public function getText()
96
	{
97
		return $this->getViewState('Text', '');
98
	}
99
100
	/**
101
	 * Sets the text content to be displayed on this view.
102
	 * If this is not empty, the child content of the view will be ignored.
103
	 * @param string $value the text content displayed on this view
104
	 */
105
	public function setText($value)
106
	{
107
		$this->setViewState('Text', TPropertyValue::ensureString($value), '');
108
	}
109
110
	/**
111
	 * @return bool whether this accordion view is active. Defaults to false.
112
	 */
113
	public function getActive()
114
	{
115
		return $this->_active;
116
	}
117
118
	/**
119
	 * @param bool $value whether this accordion view is active.
120
	 */
121
	public function setActive($value)
122
	{
123
		$this->_active = TPropertyValue::ensureBoolean($value);
124
	}
125
126
	/**
127
	 * Renders body contents of the accordion view.
128
	 * @param \Prado\Web\UI\THtmlWriter $writer the writer used for the rendering purpose.
129
	 */
130
	public function renderContents($writer)
131
	{
132
		if (($text = $this->getText()) !== '') {
133
			$writer->write($text);
134
		} elseif ($this->getHasControls()) {
135
			parent::renderContents($writer);
136
		}
137
	}
138
139
	/**
140
	 * Renders the header associated with the accordion view.
141
	 * @param \Prado\Web\UI\THtmlWriter $writer the writer for rendering purpose.
142
	 */
143
	public function renderHeader($writer)
144
	{
145
		if ($this->getVisible(false) && $this->getPage()->getClientSupportsJavaScript()) {
146
			$writer->addAttribute('id', $this->getClientID() . '_0');
147
148
			$style = $this->getActive() ? $this->getParent()->getActiveHeaderStyle() : $this->getParent()->getHeaderStyle();
149
150
			$style->addAttributesToRender($writer);
151
152
			$writer->renderBeginTag($this->getTagName());
153
154
			$this->renderHeaderContent($writer);
155
156
			$writer->renderEndTag();
157
		}
158
	}
159
160
	/**
161
	 * Renders the content in the header.
162
	 * By default, a hyperlink is displayed.
163
	 * @param \Prado\Web\UI\THtmlWriter $writer the HTML writer
164
	 */
165
	protected function renderHeaderContent($writer)
166
	{
167
		$url = $this->getNavigateUrl();
168
		if (($caption = $this->getCaption()) === '') {
169
			$caption = '&nbsp;';
170
		}
171
172
		if ($url != '') {
173
			$writer->write("<a href=\"{$url}\">");
174
		}
175
		$writer->write("{$caption}");
176
		if ($url != '') {
177
			$writer->write("</a>");
178
		}
179
	}
180
}
181