/** * 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()) { if (!is_array($attrs) || !count($attrs)) { return ''; } $attrs = _elgg_clean_vars($attrs); $attributes = array(); if (isset($attrs['js'])) { elgg_deprecated_notice('Use associative array of attr => val pairs instead of $vars[\'js\']', 1.8); 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 non-array values and allow attribute values to be an array * <code> * $attrs = array( * 'entity' => <\ElggObject>, // will be ignored * 'class' => array('elgg-input', 'elgg-input-text'), // will be imploded with spaces * 'style' => array('margin-left:10px;', 'color: #666;'), // will be imploded with spaces * 'alt' => 'Alt text', // will be left as is * ); * </code> */ if ($val !== NULL && $val !== false && (is_array($val) || !is_object($val))) { if (is_array($val)) { $val = implode(' ', $val); } $val = htmlspecialchars($val, ENT_QUOTES, 'UTF-8', false); $attributes[] = "{$attr}=\"{$val}\""; } } return implode(' ', $attributes); }
/** * Tab navigation * * @uses string $vars['type'] horizontal || vertical - Defaults to horizontal * @uses string $vars['class'] Additional class to add to ul * @uses array $vars['tabs'] A multi-dimensional array of tab entries in the format array( * 'text' => string, // The string between the <a></a> tags * 'href' => string, // URL for the link * 'class' => string // Class of the li element * 'id' => string, // ID of the li element * 'selected' => bool // if this tab is currently selected (applied to li element) * 'link_class' => string, // Class to pass to the link * 'link_id' => string, // ID to pass to the link * ) */ $options = _elgg_clean_vars($vars); $type = elgg_extract('type', $vars, 'horizontal'); if ($type == 'horizontal') { $options['class'] = "elgg-tabs elgg-htabs elgg-menu elgg-menu-page nav nav-tabs mrgn-bttm-md elgg-menu-page-default"; } else { $options['class'] = "elgg-tabs elgg-vtabs elgg-menu elgg-menu-page nav nav-tabs mrgn-bttm-md elgg-menu-page-default"; } if (isset($vars['class'])) { $options['class'] = "{$options['class']} {$vars['class']}"; } unset($options['tabs']); unset($options['type']); $attributes = elgg_format_attributes($options); if (isset($vars['tabs']) && is_array($vars['tabs']) && !empty($vars['tabs'])) { ?> <ul <?php
/** * 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) { $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 ($val !== null && $val !== false && (is_array($val) || !is_object($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, ENT_QUOTES, 'UTF-8', false); $attributes[] = "{$attr}=\"{$val}\""; } } return implode(' ', $attributes); }