Horde::getAccessKey PHP Method

getAccessKey() public static method

Returns an un-used access key from the label given.
public static getAccessKey ( string $label, boolean $nocheck = false, boolean $shutdown = false ) : string
$label string The label to choose an access key from.
$nocheck boolean Don't check if the access key already has been used?
$shutdown boolean Is this called as a shutdown function?
return string A single lower case character access key, or an empty string if no key can be found.
    public static function getAccessKey($label, $nocheck = false, $shutdown = false)
    {
        /* Shutdown call for translators? */
        if ($shutdown) {
            if (!count(self::$_labels)) {
                return;
            }
            $script = basename($_SERVER['PHP_SELF']);
            $labels = array_keys(self::$_labels);
            sort($labels);
            $used = array_keys(self::$_used);
            sort($used);
            $remaining = str_replace($used, array(), 'abcdefghijklmnopqrstuvwxyz');
            self::log('Access key information for ' . $script);
            self::log('Used labels: ' . implode(',', $labels));
            self::log('Used keys: ' . implode('', $used));
            self::log('Free keys: ' . $remaining);
            return;
        }
        /* Use access keys at all? */
        if (!isset(self::$_noAccessKey)) {
            self::$_noAccessKey = !$GLOBALS['browser']->hasFeature('accesskey') || !$GLOBALS['prefs']->getValue('widget_accesskey');
        }
        if (self::$_noAccessKey || !preg_match('/_(\\w)/u', $label, $match)) {
            return '';
        }
        $key = Horde_String_Transliterate::toAscii($match[1]);
        /* Has this key already been used? */
        if (isset(self::$_used[strtolower($key)]) && !($nocheck && isset(self::$_labels[$label]))) {
            return '';
        }
        /* Save key and label. */
        self::$_used[strtolower($key)] = true;
        self::$_labels[$label] = true;
        return $key;
    }

Usage Example

Example #1
0
 /**
  * Adds a row to the sidebar.
  *
  * If containers/sections are not added explicitly to the view
  * through the "containers" property, these rows will be used
  * instead.
  *
  * @param array $row         A hash with the row information. Possible
  *                           values:
  *   - label: (string) The row text.
  *   - selected: (boolean) Whether to mark the row as active.
  *   - style: (string) Additional CSS styles to apply to the row.
  *   - url (string) URL to link the row to.
  *   - type (string, optional) The row type, defaults to "tree". Further
  *     $row properties depending on the type:
  *     - tree:
  *       - cssClass: (string) CSS class for the icon.
  *       - id: (string) DOM ID for the row link.
  *     - checkbox:
  *     - radiobox:
  *       - color: (string, optional) Background color.
  *       - edit: (string, optional) URL for extra edit icon.
  * @param string $container  If using multiple sidebar sections, the ID of
  *                           the section to add the row to. Sections will
  *                           be rendered in the order of their first usage.
  */
 public function addRow(array $row, $container = '')
 {
     if (!isset($this->containers[$container])) {
         $this->containers[$container] = array('rows' => array());
         if ($container) {
             $this->containers[$container]['id'] = $container;
         }
     }
     $boxrow = isset($row['type']) && ($row['type'] == 'checkbox' || $row['type'] == 'radiobox');
     $label = htmlspecialchars($row['label']);
     if (isset($row['url'])) {
         $url = empty($row['url']) ? new Horde_Url() : $row['url'];
         if ($boxrow) {
             $attributes = array();
         } else {
             $ak = Horde::getAccessKey($label);
             $attributes = $ak ? array('accesskey' => $ak) : array();
         }
         foreach (array('onclick', 'target', 'class') as $attribute) {
             if (!empty($row[$attribute])) {
                 $attributes[$attribute] = $row[$attribute];
             }
         }
         if ($boxrow) {
             $class = 'horde-resource-' . (empty($row['selected']) ? 'off' : 'on');
             if ($row['type'] == 'radiobox') {
                 $class .= ' horde-radiobox';
             }
             if (empty($attributes['class'])) {
                 $attributes['class'] = $class;
             } else {
                 $attributes['class'] .= ' ' . $class;
             }
             $row['link'] = $url->link($attributes) . $label . '</a>';
         } else {
             $row['link'] = $url->link($attributes) . Horde::highlightAccessKey($label, $ak) . '</a>';
         }
     } else {
         $row['link'] = '<span class="horde-resource-none">' . $label . '</span>';
     }
     if ($boxrow) {
         $this->containers[$container]['type'] = $row['type'];
         if (!isset($row['style'])) {
             $row['style'] = '';
         }
         if (!isset($row['color'])) {
             $row['color'] = '#dddddd';
         }
         $foreground = '000';
         if (Horde_Image::brightness($row['color']) < 128) {
             $foreground = 'fff';
         }
         if (strlen($row['style'])) {
             $row['style'] .= ';';
         }
         $row['style'] .= 'background-color:' . $row['color'] . ';color:#' . $foreground;
         if (isset($row['edit'])) {
             $row['editLink'] = $row['edit']->link(array('title' => _("Edit"), 'class' => 'horde-resource-edit-' . $foreground)) . '&#9658;' . '</a>';
         }
     }
     $this->containers[$container]['rows'][] = $row;
 }
All Usage Examples Of Horde::getAccessKey