<?php
/*
 * function get_links_sidebar()
 *
 * by laen
 * based on get_links_list by Dougal
 *
 * Output a list of all links, listed by category, using the
 * settings in $wpdb->linkcategories and output it as a nested
 * HTML unordered list.
 *
 * Parameters:
 *   order (default 'name')  - Sort link categories by 'name' or 'id'
 *   hide_if_empty (default true)  - Supress listing empty link categories
 */
function get_links_sidebar($order 'name'$hide_if_empty 'obsolete') {
    global 
$wpdb;

    
$order strtolower($order);

    
// Handle link category sorting
    
if (substr($order,0,1) == '_') {
        
$direction ' DESC';
        
$order substr($order,1);
    }

    
// if 'name' wasn't specified, assume 'id':
    
$cat_order = ('name' == $order) ? 'cat_name' 'cat_id';

    if (!isset(
$direction)) $direction '';
    
// Fetch the link category data as an array of hashesa
    
$cats $wpdb->get_results("
        SELECT DISTINCT link_category, cat_name, show_images, 
            show_description, show_rating, show_updated, sort_order, 
            sort_desc, list_limit
        FROM `$wpdb->links` 
        LEFT JOIN `$wpdb->linkcategories` ON (link_category = cat_id)
        WHERE link_visible =  'Y'
            AND list_limit <> 0
        ORDER BY $cat_order $direction "
ARRAY_A);

    
// Display each category
    
if ($cats) {
        foreach (
$cats as $cat) {
            
// Handle each category.
            // First, fix the sort_order info
            
$orderby $cat['sort_order'];
            
$orderby = (bool_from_yn($cat['sort_desc'])?'_':'') . $orderby;

            
// Display the category name
            // echo '    <li id="'.sanitize_title($cat['cat_name']).'">' . $cat['cat_name'] . "\n\t<ul>\n";
            
echo '<h2>' _e($cat['cat_name'] . ":") . "</h2>\n\t<ul>\n";
            
// Call get_links() with all the appropriate params
            
get_links($cat['link_category'],
                
'<li>',"<br>","\n",
                
bool_from_yn($cat['show_images']),
                
$orderby,
                
bool_from_yn($cat['show_description']),
                
bool_from_yn($cat['show_rating']),
                
$cat['list_limit'],
                
bool_from_yn($cat['show_updated']));

            
// Close the last category
            
echo "\n\t</ul>\n</li>\n";
        }
    }
}

?>