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 $this->_getVars += $vars;
177 * Unsets (removes) GET variables. This method supports variable arguments.
179 * @param string $key Key of the GET variable to remove.
184 function unsetGetVars($key)
186 $keys = func_get_args();
187 foreach ($keys as $key) {
188 unset($this->_getVars[$key]);
193 * Adds contents to the link.
195 * @param mixed &$contents Contents to add. Can be an object with a toHtml() method.
200 function addContents($contents)
202 $this->_contents[] = $contents;
209 function getContents()
212 foreach ($this->_contents as $c) {
213 if (is_object($c) and method_exists($c, 'tohtml')) {
214 $html .= $c->toHtml();
216 $html .= htmlentities($c);
223 * @param mixed $contents New link contents.
228 function setContents($contents)
230 $this->_contents = array($contents);
234 * @param mixed $contents New link contents.
241 return '/MECON/css/html/link';
245 * Gets the query string generated with the GET vars.
247 * @return GET query string.
249 function getQueryString()
252 foreach ($this->_getVars as $var => $v) {
253 if (is_object($v) and method_exists($v, 'tostring')) {
255 } elseif (is_object($v)) {
260 $vars[] = urlencode($var) . '[]=' . urlencode($i);
263 $vars[] = urlencode($var) . '=' . urlencode($v);
266 return join('&', $vars);
270 * Gets the URI (base page with the query string).
272 * @return URI string.
276 $query = $this->getQueryString();
277 return $this->getAttribute('href') . ($query ? "?$query" : '');