1 <?php /* vim: set binary expandtab tabstop=4 shiftwidth=4 textwidth=80:
2 -------------------------------------------------------------------------------
4 -------------------------------------------------------------------------------
5 This file is part of mlib.
7 mlib is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2 of the License, or (at your option)
12 mlib is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License; if not,
17 write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 Boston, MA 02111-1307 USA
19 -------------------------------------------------------------------------------
20 Creado: Thu Aug 21 15:09:10 2003
21 Autor: @@author <@@email>
22 -------------------------------------------------------------------------------
24 -----------------------------------------------------------------------------*/
26 require_once 'HTML/Common.php';
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 MLIB_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;
67 * @param array $getVars GetVars.
72 function setGetVars($getVars)
74 $this->_getVars = $getVars;
80 * @param string $href Hypertext reference.
81 * @param mixed $contents Link contents.
82 * @param array $getVars Array (as key => value pairs) of GET variables to pass to the link.
83 * @param array $attrs Other links (A tag) attributes.
88 function MLIB_HTML_Link($href = '', $contents = '', $getVars = array(), $attrs = array())
90 if (is_array($attrs)) {
91 $attrs['href'] = $href;
92 if (!isset($attrs['class'])){
93 $attrs['class'] = 'MLIB_html_link';
96 $attrs .= " href=$href";
98 parent::HTML_Common($attrs);
99 $this->_getVars = $getVars;
100 $this->addContents($contents);
104 * Converts to HTML output.
112 foreach ($this->getAttributes() as $key => $val) {
113 if ($key == 'href') {
114 $val = $this->getURI();
116 $val = htmlentities($val);
118 $attrs .= ' ' . $key . '="' . $val . '"';
120 return "<a$attrs>" . $this->getContents() . '</a>';
124 * Gets hypertext reference.
131 return $this->getAttribute('href');
135 * Sets hypertext reference.
137 * @param string $href Hypertext reference.
142 function setHref($href)
144 $this->updateAttributes(array('href' => $href));
148 * Set a GET variable.
150 * @param string $key Key for the GET variable.
151 * @param mixed $value Value for the variable.
156 function setGetVar($key, $value)
158 $this->_getVars[$key] = $value;
162 * Updates GET variables.
164 * @param array $vars Array (as key => value pairs) of GET variables to update.
165 If they doesn't exists, they are added, if they exists, they are updated.
170 function updateGetVars($vars)
172 foreach ($vars as $key => $value)
174 $this->_getVars[$key] = $value;
179 * Unsets (removes) GET variables. This method supports variable arguments.
181 * @param string $key Key of the GET variable to remove.
186 function unsetGetVars($key)
188 $keys = func_get_args();
189 foreach ($keys as $key) {
190 unset($this->_getVars[$key]);
195 * Adds contents to the link.
197 * @param mixed &$contents Contents to add. Can be an object with a toHtml() method.
198 * @param bool $front Tells where to put the new content.
203 function addContents($contents, $front = false)
206 array_unshift($this->_contents, $contents);
209 $this->_contents[] = $contents;
217 function getContents()
220 foreach ($this->_contents as $c) {
221 if (is_object($c) and method_exists($c, 'tohtml')) {
222 $html .= $c->toHtml();
224 $html .= htmlentities($c);
231 * @param mixed $contents New link contents.
236 function setContents($contents)
238 $this->_contents = array($contents);
242 * @param mixed $contents New link contents.
249 return '/MLIB/css/html/link';
253 * Gets the query string generated with the GET vars.
255 * @return GET query string.
257 function getQueryString()
260 foreach ($this->_getVars as $var => $v) {
261 if (is_object($v) and method_exists($v, 'tostring')) {
263 } elseif (is_object($v)) {
268 $vars[] = urlencode($var) . '[]=' . urlencode($i);
271 $vars[] = urlencode($var) . '=' . urlencode($v);
274 return join('&', $vars);
278 * Gets the URI (base page with the query string).
280 * @return URI string.
284 $query = $this->getQueryString();
285 return $this->getAttribute('href') . ($query ? "?$query" : '');