Completed
Branch master (099915)
by Fabio
08:02
created

THead::render()   A

Complexity

Conditions 6
Paths 16

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 19
c 1
b 0
f 0
nc 16
nop 1
dl 0
loc 27
ccs 0
cts 26
cp 0
crap 42
rs 9.0111
1
<?php
2
/**
3
 * THead class file
4
 *
5
 * @author Marcus Nyeholt <[email protected]> and Qiang Xue <[email protected]>
6
 * @link https://github.com/pradosoft/prado
7
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
8
 * @package Prado\Web\UI\WebControls
9
 */
10
11
namespace Prado\Web\UI\WebControls;
12
13
use Prado\Web\THttpUtility;
14
15
/**
16
 * THead class
17
 *
18
 * THead displays a head element on a page. It displays the content
19
 * enclosed in its body and the page title set by the
20
 * {@link setTitle Title} property. In addition, stylesheets and JavaScripts registered via
21
 * {@link TClientScriptManager::registerStyleSheet}, {@link TClientScriptManager::registerStyleSheetFile}
22
 * {@link TClientScriptManager::registerHeadJavaScript}, and
23
 * {@link TClientScriptManager::registerHeadJavaScriptFile} will also be displayed
24
 * in the head.
25
 * THead also manages and displays meta tags through its {@link getMetaTags MetaTags}
26
 * property. You can add a meta object to the collection in code dynamically,
27
 * or add it in template using the following syntax,
28
 * <code>
29
 * <com:THead>
30
 *   <com:TMetaTag HttpEquiv="Pragma" Content="no-cache" />
31
 *   <com:TMetaTag Name="keywords" Content="Prado" />
32
 * </com:THead>
33
 * </code>
34
 *
35
 * Note, {@link TPage} has a property {@link TPage::getHead Head} that refers to
36
 * the THead control currently on the page. A page can have at most one THead
37
 * control. Although not required, it is recommended to place a THead on your page.
38
 * Without a THead on the page, stylesheets and javascripts in the current page
39
 * theme will not be rendered.
40
 *
41
 * @author Marcus Nyeholt <[email protected]> and Qiang Xue <[email protected]>
42
 * @package Prado\Web\UI\WebControls
43
 * @since 3.0
44
 */
45
class THead extends \Prado\Web\UI\TControl
46
{
47
	/**
48
	 * @var TList list of meta name tags to be loaded by {@link THead}
49
	 */
50
	private $_metaTags;
0 ignored issues
show
introduced by
The private property $_metaTags is not used, and could be removed.
Loading history...
51
52
	/**
53
	 * Registers the head control with the current page.
54
	 * This method is invoked when the control enters 'Init' stage.
55
	 * The method raises 'Init' event.
56
	 * If you override this method, be sure to call the parent implementation
57
	 * so that the event handlers can be invoked.
58
	 * @param TEventParameter $param event parameter to be passed to the event handlers
0 ignored issues
show
Bug introduced by
The type Prado\Web\UI\WebControls\TEventParameter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
59
	 */
60
	public function onInit($param)
61
	{
62
		parent::onInit($param);
63
		$this->getPage()->setHead($this);
64
	}
65
66
	/**
67
	 * Processes an object that is created during parsing template.
68
	 * This method adds TMetaTag components into the {@link getMetaTags MetaTags}
69
	 * collection of the head control.
70
	 * @param string|TComponent $object text string or component parsed and instantiated in template
0 ignored issues
show
Bug introduced by
The type Prado\Web\UI\WebControls\TComponent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
71
	 * @see createdOnTemplate
72
	 */
73
	public function addParsedObject($object)
74
	{
75
		if ($object instanceof TMetaTag) {
76
			$this->getMetaTags()->add($object);
77
		} else {
78
			parent::addParsedObject($object);
79
		}
80
	}
81
82
	/**
83
	 * @return string the page title.
84
	 */
85
	public function getTitle()
86
	{
87
		return $this->getViewState('Title', '');
88
	}
89
90
	/**
91
	 * Sets the page title.
92
	 * This title will be rendered only if the {@link TPage::getTitle Title} property
93
	 * of the page is empty.
94
	 * @param string $value the page title.
95
	 */
96
	public function setTitle($value)
97
	{
98
		$this->setViewState('Title', $value, '');
99
	}
100
101
	/**
102
	 * @return string base URL of the page. This URL is rendered as the 'href' attribute of <base> tag. Defaults to ''.
103
	 */
104
	public function getBaseUrl()
105
	{
106
		return $this->getViewState('BaseUrl', '');
107
	}
108
109
	/**
110
	 * @param string $url base URL of the page. This URL is rendered as the 'href' attribute of <base> tag.
111
	 */
112
	public function setBaseUrl($url)
113
	{
114
		$this->setViewState('BaseUrl', $url, '');
115
	}
116
117
	/**
118
	 * @return string the URL for the shortcut icon of the page. Defaults to ''.
119
	 */
120
	public function getShortcutIcon()
121
	{
122
		return $this->getViewState('ShortcutIcon', '');
123
	}
124
125
	/**
126
	 * @param string $url the URL for the shortcut icon of the page.
127
	 */
128
	public function setShortcutIcon($url)
129
	{
130
		$this->setViewState('ShortcutIcon', $url, '');
131
	}
132
133
	/**
134
	 * @return TMetaTagCollection meta tag collection
135
	 */
136
	public function getMetaTags()
137
	{
138
		if (($metaTags = $this->getViewState('MetaTags', null)) === null) {
139
			$metaTags = new TMetaTagCollection;
140
			$this->setViewState('MetaTags', $metaTags, null);
141
		}
142
		return $metaTags;
143
	}
144
145
	/**
146
	 * Renders the head control.
147
	 * @param THtmlWriter $writer the writer for rendering purpose.
0 ignored issues
show
Bug introduced by
The type Prado\Web\UI\WebControls\THtmlWriter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
148
	 */
149
	public function render($writer)
150
	{
151
		$page = $this->getPage();
152
		$title = $this->getTitle();
153
		$writer->write("<head>\n<title>" . THttpUtility::htmlEncode($title) . "</title>\n");
154
		if (($baseUrl = $this->getBaseUrl()) !== '') {
155
			$writer->write('<base href="' . $baseUrl . "\" />\n");
156
		}
157
		if (($icon = $this->getShortcutIcon()) !== '') {
158
			$writer->write('<link rel="shortcut icon" href="' . $icon . "\" />\n");
159
		}
160
161
		if (($metaTags = $this->getMetaTags()) !== null) {
162
			foreach ($metaTags as $metaTag) {
163
				$metaTag->render($writer);
164
				$writer->writeLine();
165
			}
166
		}
167
		$cs = $page->getClientScript();
168
		$cs->renderStyleSheetFiles($writer);
169
		$cs->renderStyleSheets($writer);
170
		if ($page->getClientSupportsJavaScript()) {
171
			$cs->renderHeadScriptFiles($writer);
172
			$cs->renderHeadScripts($writer);
173
		}
174
		parent::render($writer);
175
		$writer->write("</head>\n");
176
	}
177
}
178