Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * THead class file
4
 *
5
 * @author Marcus Nyeholt <tanus@users.sourceforge.net> and Qiang Xue <qiang.xue@gmail.com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: THead.php 2590 2008-12-10 11:34:24Z carlgmathisen $
10
 * @package System.Web.UI
11
 */
12
 
13
/**
14
 * THead class
15
 *
16
 * THead displays a head element on a page. It displays the content
17
 * enclosed in its body and the page title set by the
18
 * {@link setTitle Title} property. In addition, stylesheets and JavaScripts registered via
19
 * {@link TClientScriptManager::registerStyleSheet}, {@link TClientScriptManager::registerStyleSheetFile}
20
 * {@link TClientScriptManager::registerHeadJavaScript}, and
21
 * {@link TClientScriptManager::registerHeadJavaScriptFile} will also be displayed
22
 * in the head.
23
 * THead also manages and displays meta tags through its {@link getMetaTags MetaTags}
24
 * property. You can add a meta object to the collection in code dynamically,
25
 * or add it in template using the following syntax,
26
 * <code>
27
 * <com:THead>
28
 *   <com:TMetaTag HttpEquiv="Pragma" Content="no-cache" />
29
 *   <com:TMetaTag Name="keywords" Content="Prado" />
30
 * </com:THead>
31
 * </code>
32
 *
33
 * Note, {@link TPage} has a property {@link TPage::getHead Head} that refers to
34
 * the THead control currently on the page. A page can have at most once THead
35
 * control. Although not required, it is recommended to place a THead on your page.
36
 * Without a THead on the page, stylesheets and javascripts in the current page
37
 * theme will not be rendered.
38
 *
39
 * @author Marcus Nyeholt <tanus@users.sourceforge.net> and Qiang Xue <qiang.xue@gmail.com>
40
 * @version $Id: THead.php 2590 2008-12-10 11:34:24Z carlgmathisen $
41
 * @package System.Web.UI
42
 * @since 3.0
43
 */
44
class THead extends TControl
45
{
46
	/**
47
	 * @var TList list of meta name tags to be loaded by {@link THead}
48
	 */
49
	private $_metaTags=null;
50
 
51
	/**
52
	 * Registers the head control with the current page.
53
	 * This method is invoked when the control enters 'Init' stage.
54
	 * The method raises 'Init' event.
55
	 * If you override this method, be sure to call the parent implementation
56
	 * so that the event handlers can be invoked.
57
	 * @param TEventParameter event parameter to be passed to the event handlers
58
	 */
59
	public function onInit($param)
60
	{
61
		parent::onInit($param);
62
		$this->getPage()->setHead($this);
63
	}
64
 
65
	/**
66
	 * Processes an object that is created during parsing template.
67
	 * This method adds TMetaTag components into the {@link getMetaTags MetaTags}
68
	 * collection of the head control.
69
	 * @param string|TComponent text string or component parsed and instantiated in template
70
	 * @see createdOnTemplate
71
	 */
72
	public function addParsedObject($object)
73
	{
74
		if($object instanceof TMetaTag)
75
			$this->getMetaTags()->add($object);
76
		else
77
			parent::addParsedObject($object);
78
	}
79
 
80
	/**
81
	 * @return string the page title.
82
	 */
83
	public function getTitle()
84
	{
85
		return $this->getViewState('Title','');
86
	}
87
 
88
	/**
89
	 * Sets the page title.
90
	 * This title will be rendered only if the {@link TPage::getTitle Title} property
91
	 * of the page is empty.
92
	 * @param string the page title.
93
	 */
94
	public function setTitle($value)
95
	{
96
		$this->setViewState('Title',$value,'');
97
	}
98
 
99
	/**
100
	 * @return string base URL of the page. This URL is rendered as the 'href' attribute of <base> tag. Defaults to ''.
101
	 */
102
	public function getBaseUrl()
103
	{
104
		return $this->getViewState('BaseUrl','');
105
	}
106
 
107
	/**
108
	 * @param string base URL of the page. This URL is rendered as the 'href' attribute of <base> tag.
109
	 */
110
	public function setBaseUrl($url)
111
	{
112
		$this->setViewState('BaseUrl',$url,'');
113
	}
114
 
115
	/**
116
	 * @return string the URL for the shortcut icon of the page. Defaults to ''.
117
	 */
118
	public function getShortcutIcon()
119
	{
120
		return $this->getViewState('ShortcutIcon','');
121
	}
122
 
123
	/**
124
	 * @param string the URL for the shortcut icon of the page.
125
	 */
126
	public function setShortcutIcon($url)
127
	{
128
		$this->setViewState('ShortcutIcon',$url,'');
129
	}
130
 
131
	/**
132
	 * @return TMetaTagCollection meta tag collection
133
	 */
134
	public function getMetaTags()
135
	{
136
		if(($metaTags=$this->getViewState('MetaTags',null))===null)
137
		{
138
			$metaTags=new TMetaTagCollection;
139
			$this->setViewState('MetaTags',$metaTags,null);
140
		}
141
		return $metaTags;
142
	}
143
 
144
	/**
145
	 * Renders the head control.
146
	 * @param THtmlWriter the writer for rendering purpose.
147
	 */
148
	public function render($writer)
149
	{
150
		$page=$this->getPage();
151
		$title=$this->getTitle();
152
		$writer->write("<head>\n<title>".THttpUtility::htmlEncode($title)."</title>\n");
153
		if(($baseUrl=$this->getBaseUrl())!=='')
154
			$writer->write('<base href="'.$baseUrl."\" />\n");
155
		if(($icon=$this->getShortcutIcon())!=='')
156
			$writer->write('<link rel="shortcut icon" href="'.$icon."\" />\n");
157
 
158
		if(($metaTags=$this->getMetaTags())!==null)
159
		{
160
			foreach($metaTags as $metaTag)
161
			{
162
				$metaTag->render($writer);
163
				$writer->writeLine();
164
			}
165
		}
166
		$cs=$page->getClientScript();
167
		$cs->renderStyleSheetFiles($writer);
168
		$cs->renderStyleSheets($writer);
169
		if($page->getClientSupportsJavaScript())
170
		{
171
			$cs->renderHeadScriptFiles($writer);
172
			$cs->renderHeadScripts($writer);
173
		}
174
		parent::render($writer);
175
		$writer->write("</head>\n");
176
	}
177
}
178
 
179
/**
180
 * TMetaTag class.
181
 *
182
 * TMetaTag represents a meta tag appearing in a page head section.
183
 * You can set its {@link setID ID}, {@link setHttpEquiv HttpEquiv},
184
 * {@link setName Name}, {@link setContent Content}, {@link setScheme Scheme}
185
 * properties, which correspond to id, http-equiv, name, content, and scheme
186
 * attributes for a meta tag, respectively.
187
 *
188
 * @author Qiang Xue <qiang.xue@gmail.com>
189
 * @version $Id: THead.php 2590 2008-12-10 11:34:24Z carlgmathisen $
190
 * @package System.Web.UI.WebControls
191
 * @since 3.0
192
 */
193
class TMetaTag extends TComponent
194
{
195
	/**
196
	 * @var string id of the meta tag
197
	 */
198
	private $_id='';
199
	/**
200
	 * @var string http-equiv attribute of the meta tag
201
	 */
202
	private $_httpEquiv='';
203
	/**
204
	 * @var string name attribute of the meta tag
205
	 */
206
	private $_name='';
207
	/**
208
	 * @var string content attribute of the meta tag
209
	 */
210
	private $_content='';
211
	/**
212
	 * @var string scheme attribute of the meta tag
213
	 */
214
	private $_scheme='';
215
 
216
	/**
217
	 * @return string id of the meta tag
218
	 */
219
	public function getID()
220
	{
221
		return $this->_id;
222
	}
223
 
224
	/**
225
	 * @param string id of the meta tag
226
	 */
227
	public function setID($value)
228
	{
229
		$this->_id=$value;
230
	}
231
 
232
	/**
233
	 * @return string http-equiv attribute of the meta tag
234
	 */
235
	public function getHttpEquiv()
236
	{
237
		return $this->_httpEquiv;
238
	}
239
 
240
	/**
241
	 * @param string http-equiv attribute of the meta tag
242
	 */
243
	public function setHttpEquiv($value)
244
	{
245
		$this->_httpEquiv=$value;
246
	}
247
 
248
	/**
249
	 * @return string name attribute of the meta tag
250
	 */
251
	public function getName()
252
	{
253
		return $this->_name;
254
	}
255
 
256
	/**
257
	 * @param string name attribute of the meta tag
258
	 */
259
	public function setName($value)
260
	{
261
		$this->_name=$value;
262
	}
263
 
264
	/**
265
	 * @return string content attribute of the meta tag
266
	 */
267
	public function getContent()
268
	{
269
		return $this->_content;
270
	}
271
 
272
	/**
273
	 * @param string content attribute of the meta tag
274
	 */
275
	public function setContent($value)
276
	{
277
		$this->_content=$value;
278
	}
279
 
280
	/**
281
	 * @return string scheme attribute of the meta tag
282
	 */
283
	public function getScheme()
284
	{
285
		return $this->_scheme;
286
	}
287
 
288
	/**
289
	 * @param string scheme attribute of the meta tag
290
	 */
291
	public function setScheme($value)
292
	{
293
		$this->_scheme=$value;
294
	}
295
 
296
	/**
297
	 * Renders the meta tag.
298
	 * @param THtmlWriter writer for the rendering purpose
299
	 */
300
	public function render($writer)
301
	{
302
		if($this->_id!=='')
303
			$writer->addAttribute('id',$this->_id);
304
		if($this->_name!=='')
305
			$writer->addAttribute('name',$this->_name);
306
		if($this->_httpEquiv!=='')
307
			$writer->addAttribute('http-equiv',$this->_httpEquiv);
308
		if($this->_scheme!=='')
309
			$writer->addAttribute('scheme',$this->_scheme);
310
		$writer->addAttribute('content',$this->_content);
311
		$writer->renderBeginTag('meta');
312
		$writer->renderEndTag();
313
	}
314
}
315
 
316
 
317
/**
318
 * TMetaTagCollection class
319
 *
320
 * TMetaTagCollection represents a collection of meta tags
321
 * contained in a {@link THead} control.
322
 *
323
 * @author Qiang Xue <qiang.xue@gmail.com>
324
 * @version $Id: THead.php 2590 2008-12-10 11:34:24Z carlgmathisen $
325
 * @package System.Web.UI.WebControls
326
 * @since 3.0
327
 */
328
class TMetaTagCollection extends TList
329
{
330
	/**
331
	 * Inserts an item at the specified position.
332
	 * This overrides the parent implementation by performing type
333
	 * check on the item being added.
334
	 * @param integer the speicified position.
335
	 * @param mixed new item
336
	 * @throws TInvalidDataTypeException if the item to be inserted is not a {@link TMetaTag}
337
	 */
338
	public function insertAt($index,$item)
339
	{
340
		if($item instanceof TMetaTag)
341
			parent::insertAt($index,$item);
342
		else
343
			throw new TInvalidDataTypeException('metatagcollection_metatag_invalid');
344
	}
345
 
346
	/**
347
	 * Finds the lowest cardinal index of the meta tag whose id is the one being looked for.
348
	 * @param string the ID of the meta tag to be looked for
349
	 * @return integer the index of the meta tag found, -1 if not found.
350
	 */
351
	public function findIndexByID($id)
352
	{
353
		$index=0;
354
		foreach($this as $item)
355
		{
356
			if($item->getID()===$id)
357
				return $index;
358
			$index++;
359
		}
360
		return -1;
361
	}
362
 
363
	/**
364
	 * Finds the item whose value is the one being looked for.
365
	 * @param string the id of the meta tag to be looked for
366
	 * @return TMetaTag the meta tag found, null if not found.
367
	 */
368
	public function findMetaTagByID($id)
369
	{
370
		if(($index=$this->findIndexByID($id))>=0)
371
			return $this->itemAt($index);
372
		else
373
			return null;
374
	}
375
}
376
 
377
?>