1 <?php /* vim: set binary expandtab tabstop=4 shiftwidth=4 textwidth=80:
2 -------------------------------------------------------------------------------
3 Ministerio de EconomÃa
5 -------------------------------------------------------------------------------
6 This file is part of meconlib.
8 meconlib is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2 of the License, or (at your option)
13 meconlib is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License; if not,
18 write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 Boston, MA 02111-1307 USA
20 -------------------------------------------------------------------------------
21 Creado: Thu Aug 21 15:09:10 2003
22 Autor: @@author <@@email>
23 -------------------------------------------------------------------------------
25 -----------------------------------------------------------------------------*/
27 require_once 'HTML/Common.php';
30 * HTML Link representation.
31 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.
32 This is done in toHtml() method. Object are stored as references.
37 class MECON_HTML_Link extends HTML_Common {
39 * Variables to send via GET HTTP method.
44 var $_getVars = array();
49 * @var array $contents
52 var $_contents = array();
62 return $this->_getVars;
68 * @param array $getVars GetVars.
73 function setGetVars($getVars)
75 $this->_getVars = $getVars;
81 * @param string $href Hypertext reference.
82 * @param mixed $contents Link contents.
83 * @param array $getVars Array (as key => value pairs) of GET variables to pass to the link.
84 * @param array $attrs Other links (A tag) attributes.
89 function MECON_HTML_Link($href = '', $contents = '', $getVars = array(), $attrs = array())
91 if (is_array($attrs)) {
92 $attrs['href'] = $href;
93 if (!isset($attrs['class'])){
94 $attrs['class'] = 'mecon_html_link';
97 $attrs .= " href=$href";
99 parent::HTML_Common($attrs);
100 $this->_getVars = $getVars;
101 $this->addContents($contents);
105 * Converts to HTML output.
113 foreach ($this->getAttributes() as $key => $val) {
114 if ($key == 'href') {
115 $val = $this->getURI();
117 $val = htmlentities($val);
119 $attrs .= ' ' . $key . '="' . $val . '"';
121 return "<a$attrs>" . $this->getContents() . '</a>';
125 * Gets hypertext reference.
132 return $this->getAttribute('href');
136 * Sets hypertext reference.
138 * @param string $href Hypertext reference.
143 function setHref($href)
145 $this->updateAttributes(array('href' => $href));
149 * Set a GET variable.
151 * @param string $key Key for the GET variable.
152 * @param mixed $value Value for the variable.
157 function setGetVar($key, $value)
159 $this->_getVars[$key] = $value;
163 * Updates GET variables.
165 * @param array $vars Array (as key => value pairs) of GET variables to update.
166 If they doesn't exists, they are added, if they exists, they are updated.
171 function updateGetVars($vars)
173 foreach ($vars as $key => $value)
175 $this->_getVars[$key] = $value;
180 * Unsets (removes) GET variables. This method supports variable arguments.
182 * @param string $key Key of the GET variable to remove.
187 function unsetGetVars($key)
189 $keys = func_get_args();
190 foreach ($keys as $key) {
191 unset($this->_getVars[$key]);
196 * Adds contents to the link.
198 * @param mixed &$contents Contents to add. Can be an object with a toHtml() method.
199 * @param bool $front Tells where to put the new content.
204 function addContents($contents, $front = false)
207 array_unshift($this->_contents, $contents);
210 $this->_contents[] = $contents;
218 function getContents()
221 foreach ($this->_contents as $c) {
222 if (is_object($c) and method_exists($c, 'tohtml')) {
223 $html .= $c->toHtml();
225 $html .= htmlentities($c);
232 * @param mixed $contents New link contents.
237 function setContents($contents)
239 $this->_contents = array($contents);
243 * @param mixed $contents New link contents.
250 return '/MECON/css/html/link';
254 * Gets the query string generated with the GET vars.
256 * @return GET query string.
258 function getQueryString()
261 foreach ($this->_getVars as $var => $v) {
262 if (is_object($v) and method_exists($v, 'tostring')) {
264 } elseif (is_object($v)) {
269 $vars[] = urlencode($var) . '[]=' . urlencode($i);
272 $vars[] = urlencode($var) . '=' . urlencode($v);
275 return join('&', $vars);
279 * Gets the URI (base page with the query string).
281 * @return URI string.
285 $query = $this->getQueryString();
286 return $this->getAttribute('href') . ($query ? "?$query" : '');