Add Horde_Url#link(). More Horde_Url usage.
authorJan Schneider <jan@horde.org>
Thu, 3 Dec 2009 15:17:46 +0000 (16:17 +0100)
committerJan Schneider <jan@horde.org>
Thu, 3 Dec 2009 15:17:46 +0000 (16:17 +0100)
framework/Core/lib/Horde.php
framework/Url/lib/Horde/Url.php

index b6c83a3..caa911e 100644 (file)
@@ -544,7 +544,7 @@ HTML;
      * @param string $app        The name of the current Horde application.
      * @param boolean $override  Override Horde settings?
      *
-     * @return string  The HTML to create the link.
+     * @return Horde_Url|boolean  The HTML to create the link.
      */
     static public function getServiceLink($type, $app, $override = false)
     {
@@ -557,11 +557,11 @@ HTML;
 
         switch ($type) {
         case 'help':
-            return Horde_Util::addParameter(self::url($webroot . '/services/help/'), array('module' => $app));
+            return self::url($webroot . '/services/help/')->add('module', $app);
 
         case 'problem':
-            $url = self::url($webroot . '/services/problem.php');
-            return Horde_Util::addParameter($url, array('return_url' => urlencode(self::selfUrl(true, true, true))));
+            return self::url($webroot . '/services/problem.php')
+                ->add('return_url', urlencode(self::selfUrl(true, true, true)));
 
         case 'logout':
             return Horde_Auth::getLogoutUrl(array('reason' => Horde_Auth::REASON_LOGOUT));
@@ -572,8 +572,8 @@ HTML;
         case 'options':
         case 'prefsapi':
             if (!in_array($GLOBALS['conf']['prefs']['driver'], array('', 'none'))) {
-                $url = self::url($webroot . ($type == 'options' ? '/services/prefs.php' : '/services/prefs/'));
-                return Horde_Util::addParameter($url, array('app' => $app));
+                return self::url($webroot . ($type == 'options' ? '/services/prefs.php' : '/services/prefs/'))
+                    ->add('app', $app);
             }
             break;
 
@@ -581,7 +581,8 @@ HTML;
             return self::url($webroot . '/services/cache.php', false, -1);
 
         case 'download':
-            return Horde_Util::addParameter(self::url($webroot . '/services/download/'), array('module' => $app));
+            return self::url($webroot . '/services/download/')
+                ->add('module', $app);
 
         case 'go':
             return self::url($webroot . '/services/go.php');
@@ -1252,22 +1253,24 @@ HTML;
                                 $accesskey = '', $attributes = array(),
                                 $escape = true)
     {
-        if (!empty($title2)) {
-            $title = $title2;
+        if (!($url instanceof Horde_Url)) {
+            $url = new Horde_Url($url);
         }
 
-        $ret = '<a';
-        if (!empty($url)) {
-            $ret .= " href=\"$url\"";
+        if (!empty($title2)) {
+            $title = $title2;
         }
         if (!empty($onclick)) {
-            $ret .= " onclick=\"$onclick\"";
+            $attributes['onclick'] = $onclick;
         }
         if (!empty($class)) {
-            $ret .= " class=\"$class\"";
+            $attributes['class'] = $class;
         }
         if (!empty($target)) {
-            $ret .= " target=\"$target\"";
+            $attributes['target'] = $target;
+        }
+        if (!empty($accesskey)) {
+            $attributes['accesskey'] = $accesskey;
         }
         if (!empty($title)) {
             if ($escape) {
@@ -1279,19 +1282,13 @@ HTML;
                         nl2br(htmlspecialchars($title, ENT_QUOTES, $charset)),
                         ENT_QUOTES, $charset));
                 error_reporting($old_error);
+                $attributes['title.raw'] = $title;
+            } else {
+                $attributes['title'] = $title;
             }
-            $ret .= ' title="' . $title . '"';
-        }
-        if (!empty($accesskey)) {
-            $ret .= ' accesskey="' . htmlspecialchars($accesskey) . '"';
-        }
-
-        foreach ($attributes as $name => $value) {
-            $ret .= ' ' . htmlspecialchars($name) . '="'
-                . htmlspecialchars($value) . '"';
         }
 
-        return "$ret>";
+        return $url->link($attributes);
     }
 
     /**
index 327504f..0c95f2a 100644 (file)
@@ -186,4 +186,33 @@ class Horde_Url
         return $url;
     }
 
+    /**
+     * Generates a HTML link tag out of this URL.
+     *
+     * @param array $attributes A hash with any additional attributes to be
+     *                          added to the link. If the attribute name is
+     *                          suffixed with ".raw", the attribute value
+     *                          won't be HTML-encoded.
+     *
+     * @return string  An <a> tag representing this URL.
+     */
+    public function link(array $attributes = array())
+    {
+        $url = (string)$this;
+        $link = '<a';
+        if (!empty($url)) {
+            $link .= " href=\"$url\"";
+        }
+        foreach ($attributes as $name => $value) {
+            if (substr($name, -4) == '.raw') {
+                $link .= ' ' . htmlspecialchars(substr($name, 0, -4))
+                    . '="' . $value . '"';
+            } else {
+                $link .= ' ' . htmlspecialchars($name)
+                    . '="' . htmlspecialchars($value) . '"';
+            }
+        }
+        return $link . '>';
+    }
+
 }