]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Refs #2143: Added elgg_format_attributes() for generating an attribute string from...
authorewinslow <ewinslow@36083f99-b078-4883-b0ff-0f9b5a30f544>
Sat, 20 Nov 2010 06:04:43 +0000 (06:04 +0000)
committerewinslow <ewinslow@36083f99-b078-4883-b0ff-0f9b5a30f544>
Sat, 20 Nov 2010 06:04:43 +0000 (06:04 +0000)
git-svn-id: http://code.elgg.org/elgg/trunk@7354 36083f99-b078-4883-b0ff-0f9b5a30f544

engine/lib/output.php
views/default/input/url.php
views/default/output/url.php

index dd5c4cf59f48386fa072821d6b8f7d163e0ca757..61db401a1a392c0eeb41ce59520574eac62c01d1 100644 (file)
@@ -139,6 +139,85 @@ function elgg_format_url($url) {
        return preg_replace('/&(?!amp;)/', '&amp;', $url);
 }
 
+/**
+ * Converts an associative array into a string of well-formed attributes
+ *
+ * @note usually for HTML, but could be useful for XML too...
+ *
+ * @param array $attrs An associative array of attr => val pairs
+ *
+ * @return string HTML attributes to be inserted into a tag (e.g., <tag $attrs>)
+ */
+function elgg_format_attributes(array $attrs = array()) {
+       $attrs = elgg_clean_vars($attrs);
+       $attributes = array();
+
+       if (isset($attrs['js'])) {
+               //@todo deprecated notice?
+
+               if (!empty($attrs['js'])) {
+                       $attributes[] = $attrs['js'];
+               }
+
+               unset($attrs['js']);
+       }
+
+       foreach ($attrs as $attr => $val) {
+               $attr = strtolower($attr);
+
+               if ($val === TRUE) {
+                       $val = $attr; //e.g. checked => TRUE ==> checked="checked"
+               }
+
+               // ignore $vars['entity'] => ElggEntity stuff
+               if (is_not_null($val) && (is_array($val) || is_string($val))) {
+
+                       // allow $vars['class'] => array('one', 'two');
+                       // @todo what about $vars['style']? Needs to be semi-colon separated...
+                       if (is_array($val)) {
+                               $val = implode(' ', $val);
+                       }
+
+                       $val = htmlspecialchars($val);
+                       $attributes[] = "$attr=\"$val\"";
+               }
+       }
+
+       return implode(' ', $attributes);
+}
+
+
+/**
+ * Preps an associative array for use in {@link elgg_format_attributes()}.
+ *
+ * Removes all the junk that {@link elgg_view()} puts into $vars.
+ * Maintains backward compatibility with attributes like 'internalname' and 'internalid'
+ *
+ * @note This function is called automatically by elgg_format_attributes(). No need to
+ *       call it yourself before using elgg_format_attributes().
+ *
+ * @param array $vars The raw $vars array with all it's dirtiness (config, url, etc.)
+ *
+ * @return array The array, ready to be used in elgg_format_attributes().
+ */
+function elgg_clean_vars(array $vars = array()) {
+       unset($vars['config']);
+       unset($vars['url']);
+
+       // backwards compatibility code
+       if (isset($vars['internalname'])) {
+               $vars['name'] = $vars['internalname'];
+               unset($vars['internalname']);
+       }
+
+       if (isset($vars['internalid'])) {
+               $vars['id'] = $vars['internalid'];
+               unset($vars['internalid']);
+       }
+
+       return $vars;
+}
+
 /**
  * Converts shorthand urls to absolute urls.
  *
@@ -164,7 +243,7 @@ function elgg_normalize_url($url) {
        elseif (preg_match("#^[^/]*\.php(\?.*)?$#i", $url)) {
                return elgg_get_site_url().$url;
        }
-       
+
        // 'example.com', 'example.com/subpage'
        elseif (preg_match("#^[^/]*\.#i", $url)) {
                return "http://$url";
@@ -311,6 +390,8 @@ function elgg_strip_tags($string) {
        return $string;
 }
 
+
+
 /**
   * Filters a string into an array of significant words
   *
index 1271bd3be8751679c35acfaeeccaa54b5aeea44d..29f5edb3097886f3ca7a08ee501f96c5b6b65342 100644 (file)
@@ -5,22 +5,17 @@
  *
  * @package Elgg
  * @subpackage Core
- *
- * @uses $vars['value'] The current value, if any
- * @uses $vars['js'] Any Javascript to enter into the input tag
- * @uses $vars['internalname'] The name of the input field
- * @uses $vars['class'] Class override
  */
 
-$class = $vars['class'];
-if (!$class) {
-       $class = "input_url";
-}
+$defaults = array(
+       'class' => 'input_url',
+);
+
+$vars = array_merge($defaults, $vars);
 
 if (!isset($vars['value']) || $vars['value'] === FALSE) {
        $vars['value'] = elgg_get_sticky_value($vars['internalname']);
 }
-
 ?>
 
-<input type="text" <?php if ($vars['disabled']) echo ' disabled="yes" '; ?> <?php echo $vars['js']; ?> name="<?php echo $vars['internalname']; ?>" <?php if (isset($vars['internalid'])) echo "id=\"{$vars['internalid']}\""; ?> value="<?php echo htmlentities($vars['value'], ENT_QUOTES, 'UTF-8'); ?>" class="<?php echo $class; ?>"/>
\ No newline at end of file
+<input type="text" <?php echo elgg_format_attributes($vars); ?> />
index c857d24d12fa1671db88f47de66db844c60d120e..23b77419891fd56fc47ee994a56251c0e300b528 100644 (file)
@@ -6,68 +6,42 @@
  * @package Elgg
  * @subpackage Core
  *
- * @uses string $vars['href'] The URL.
- * @uses string $vars['text'] The string between the <a></a> tags.
- * @uses string $vars['target'] Set the target="" attribute.
- * @uses bool $vars['encode_text'] Run $vars['text'] through htmlentities()?
- * @uses string $vars['class'] what to add in class=""
- * @uses string $vars['js'] Javascript to insert in <a> tag
- * @uses string $vars['title'] Title attribute to <a> tag
- * @uses bool $vars['is_action'] Is this a link to an action?
+ * @uses string $vars['text']        The string between the <a></a> tags.
+ * @uses bool   $vars['encode_text'] Run $vars['text'] through htmlentities()?
+ * @uses bool   $vars['is_action']   Is this a link to an action?
  *
  */
 
 $url = trim($vars['href']);
 if (!$url and isset($vars['value'])) {
        $url = trim($vars['value']);
+       unset($vars['value']);
 }
 
-if (!empty($url)) {
-       if (isset($vars['target'])) {
-               $target = "target = \"{$vars['target']}\"";
-       } else {
-               $target = '';
-       }
-
-       if (isset($vars['class'])) {
-               $class = "class = \"{$vars['class']}\"";
-       } else {
-               $class = '';
-       }
-
-       if (isset($vars['internalid'])) {
-               $id = "id = \"{$vars['internalid']}\"";
-       } else {
-               $id = '';
-       }
-
-       if (isset($vars['js'])) {
-               $js = "{$vars['js']}";
-       } else {
-               $js = '';
-       }
-
+if (!empty($url)) {\r
        if (isset($vars['text'])) {
                if (isset($vars['encode_text']) && $vars['encode_text']) {
                        $text = htmlentities($vars['text'], ENT_QUOTES, 'UTF-8');
                } else {
                        $text = $vars['text'];
                }
+
+               unset($vars['text']);
        } else {
                $text = htmlentities($url, ENT_QUOTES, 'UTF-8');
        }
 
+       unset($vars['encode_text']);
+
        $url = elgg_normalize_url($url);
 
        if (isset($vars['is_action'])) {
                $url = elgg_add_action_tokens_to_url($url);
+               unset($vars['is_action']);
        }
 
-       if (isset($vars['title'])) {
-               $title = 'title="' . htmlentities($vars['title']) . '"';
-       } else {
-               $title = '';
-       }
-
-       echo "<a href=\"{$url}\" $target $class $id $js $title>$text</a>";
+       $vars['href'] = $url;
+\r
+       $attributes = elgg_format_attributes($vars);
+       echo "<a $attributes>$text</a>";\r
 }
\ No newline at end of file