2 // vim: set binary expandtab tabstop=4 shiftwidth=4 textwidth=80:
3 // +----------------------------------------------------------------------+
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2003 The PHP Group |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.02 of the PHP license, |
9 // | that is bundled with this package in the file LICENSE, and is |
10 // | available at through the world-wide-web at |
11 // | http://www.php.net/license/2_02.txt. |
12 // | If you did not receive a copy of the PHP license and are unable to |
13 // | obtain it through the world-wide-web, please send a note to |
14 // | license@php.net so we can mail you a copy immediately. |
15 // +----------------------------------------------------------------------+
16 // | Created: Tue Jul 22 18:16:09 2003 |
17 // | Author: Leandro Lucarella <llucar@mecon.gov.ar> |
18 // +----------------------------------------------------------------------+
24 require_once 'HTML/Common.php';
27 // +X2C Class 214 :HTML_Link
29 * HTML Link representation.
30 When adding GET variables, if the value is an object, it looks for a toString() method, if it doesn't exists or if is an array, it serializes the object/array to get a string value.
31 This is done in toHtml() method. Object are stored as references.
36 class HTML_Link extends HTML_Common {
38 * Variables to send via GET HTTP method.
43 var $_getVars = array();
48 * @var array $contents
51 var $_contents = array();
61 return $this->_getVars;
66 * @param array $getVars GetVars.
71 function setGetVars($getVars)
73 $this->_getVars = $getVars;
82 * @param string $href Hypertext reference.
83 * @param mixed $contents Link contents.
84 * @param array $getVars Array (as key => value pairs) of GET variables to pass to the link.
85 * @param array $attrs Other links (A tag) attributes.
90 function HTML_Link($href = '', $contents = '', $getVars = array(), $attrs = array()) // ~X2C
92 if (is_array($attrs)) {
93 $attrs['href'] = $href;
95 $attrs .= " href=$href";
97 parent::HTML_Common($attrs);
98 $this->_getVars = $getVars;
99 $this->addContents($contents);
103 // +X2C Operation 216
105 * Converts to HTML output.
110 function toHtml() // ~X2C
113 foreach ($this->getAttributes() as $key => $val) {
114 if ($key == 'href') {
116 foreach ($this->_getVars as $var => $v) {
117 if (is_object($v) and method_exists($v, 'tostring')) {
119 } elseif (is_object($v) or is_array($v)) {
122 $vars[] = urlencode($var) . '=' . urlencode($v);
125 $val = '?' . join('&', $vars);
128 $attrs .= ' ' . $key . '="' . htmlentities($val) . '"';
130 return "<A$attrs>" . $this->getContents() . '</A>';
134 // +X2C Operation 217
136 * Gets hypertext reference.
141 function getHref() // ~X2C
143 return $this->getAttribute('href');
147 // +X2C Operation 218
149 * Sets hypertext reference.
151 * @param string $href Hypertext reference.
156 function setHref($href) // ~X2C
158 $this->updateAttributes(array('href' => $href));
162 // +X2C Operation 225
164 * Set a GET variable.
166 * @param string $key Key for the GET variable.
167 * @param mixed &$value Value for the variable.
172 function setGetVar($key, &$value) // ~X2C
174 if (is_object($value)) {
175 $this->attrs[$key] =& $value;
177 $this->attrs[$key] = $value;
182 // +X2C Operation 226
184 * Updates GET variables.
186 * @param array $vars Array (as key => value pairs) of GET variables to update.
187 If they doesn't exists, they are added, if they exists, they are updated.
192 function updateGetVars($vars) // ~X2C
194 $this->_getVars += $vars;
198 // +X2C Operation 227
200 * Unsets (removes) GET variables. This method supports variable arguments.
202 * @param string $key Key of the GET variable to remove.
207 function unsetGetVars($key) // ~X2C
209 $keys = func_get_args();
210 foreach ($keys as $key) {
211 unset($this->_getVars[$key]);
216 // +X2C Operation 230
218 * Adds contents to the link.
220 * @param mixed &$contents Contents to add. Can be an object with a toHtml() method.
225 function addContents(&$contents) // ~X2C
227 if (is_object($contents)) {
228 $this->_contents[] =& $contents;
230 $this->_contents[] = $contents;
235 // +X2C Operation 231
240 function getContents() // ~X2C
243 foreach ($this->_contents as $c) {
244 if (is_object($c) and method_exists($c, 'tohtml')) {
245 $html .= $c->toHtml();
247 $html .= htmlentities($c);
254 // +X2C Operation 232
256 * @param mixed $contents New link contents.
261 function setContents($contents) // ~X2C
263 $this->_contents = $contents;
267 } // -X2C Class :HTML_Link