<?php

include_once(TEMPLATEPATH '/widget-profil.php');
include_once(
TEMPLATEPATH '/widget-youtube.php');
include_once(
TEMPLATEPATH '/widget-google-video.php');
include_once(
TEMPLATEPATH '/widget-flickr.php');

function 
sf_theme_widget_rss($args$number 1) {
    
extract($args);
    
$options get_option('widget_rss');
    
$url $options[$number]['url'];
    
$title $options[$number]['title'];
    
$count $options[$number]['items'];
    
$showsummary $options[$number]['show_summary'];
    if ( empty(
$url) )
        return;
    if ( empty(
$count) || $count || $count 10 )
        
$count 5;
    
$rss=sf_theme_fetch_rss($url);
    if ( !
sizeof($rss->items) )
        return;
    
$rss->items array_slice($rss->items0$count);
    if ( empty(
$title) )
        
$title '&nbsp;';
    elseif (!empty(
$rss->channel['link']))
        
$title '<a href="' htmlspecialchars($rss->channel['link']) . '">' $title '</a>';
    print 
$before_widget;
    print 
$before_title $title $after_title '<ul>';
    foreach (
$rss->items as $item) {
        
$item sf_theme_fix_feed_item($item);
        
?>
            <li>
                <span class="date"><?php print $item['date']; ?></span>
                <span class="link"><a title="<?php print $item['description']; ?>" href="<?php print $item['link']; ?>"><?php print $item['title']; ?></a></span>
                <?php if($showsummary) { ?><div class="summary"><?php print $item['summary']; ?></span><?php ?>
            </li>
        <?php
    
}
    print 
'</ul>';
#    print '<span class="feedicon"><a href="'.htmlspecialchars($url).'" title="Feed: '.htmlspecialchars($rss->channel['title']).'"><img src="'.get_template_directory_uri().'/images/feed-icon-12x12.png" alt="Feed"/></a></span>';
    
print $after_widget;
}

function 
sf_theme_widget_pages($args) {
    global 
$wp$wpdb;
        
    
// $args is an array of strings that help widgets to conform to
    // the active theme: before_widget, before_title, after_widget,
    // and after_title are the array keys. Default tags: li and h2.
    
extract($args);
    
$options get_option('widget_pages');
    
$title = empty($options['title']) ? '' $options['title'];

    
// Don't display if there are no pages...
    
if (!$wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status='static' OR post_type='page'")) {
        print 
'<!-- no pages to display for pages widget -->';
        return;
    }

    
// These lines generate our output. Widgets can be very complex
    // but as you can see here, they can also be very, very simple.
    
print str_replace('<div class="box">''<div class="nobox">'$before_widget);
    if (
''!=$title)
        print 
$before_title $title $after_title;
    print 
'<ul>';
    
wp_list_pages(sf_theme_fold_pages(($wp->authorize && !$wp->authorized)?join(',',array_merge(sf_theme_protected_pages(),sf_theme_topline_pages())):join(',',sf_theme_topline_pages()))."title_li=&sort_column=menu_order");
    if (
$links=get_settings('sf_sidemenu_link_kategori'))
        
get_links($links'<li class="page_item link_item">''</li>'''false'rating'false);
    print 
'</ul>' $after_widget;
}

function 
sf_theme_fold_pages($exclusions) {
    
/*
        Function based on fold_page_menus plugin
    */
    
global $wpdb$posts;
    
/*
        Find the parents of the pages to be displayed
        by working back up the tree from the current page
    */
    
if (!is_page())
        
$x $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_status='publish' AND post_type!='page' LIMIT 1");
    else
        
$x $posts[0]->ID;
    
$inclusions "(post_parent <> " strval($x) . ")";
    do {
        
$include $wpdb->get_results("SELECT post_parent FROM $wpdb->posts WHERE ID = $x LIMIT 1"ARRAY_N);
        
$x $include[0][0];
        
$inclusions .= " AND (post_parent <> " $x ")";
    } while (
$x <> 0);
    
/*
        The exclude list consist of all the pages 
        whose IDs are *not* on the include list
    */
    
$rows $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE (post_status = 'static' OR post_type = 'page') AND $inclusions"ARRAY_N);
    if ( 
count($rows) ) {
        foreach ( 
$rows as $row ) {
            foreach ( 
$row as $ro ) {
                if (
$exclusions != '')
                    
$exclusions .= ',';
                
$exclusions .= strval($ro);
            }
        }
    } 
    if (
$exclusions != ''
        
$exclusions 'exclude=' $exclusions '&';
    return 
$exclusions;
}

function 
sf_theme_widget_xsf($args) {
    
sf_theme_image_widget('<img src="'.get_template_directory_uri().'/images/xSF_110x130.png" width="110" height="130" alt="Sæt kryds ved SF!"/>'$args);
}

function 
sf_theme_widget_xchilif($args) {
    
sf_theme_image_widget('<img src="'.get_template_directory_uri().'/images/xFchili_120x136.png" width="120" height="136" alt="Sæt kryds ved liste F!"/>'$args);
}

function 
sf_theme_widget_chili($args) {
    
sf_theme_image_widget('<img src="'.get_template_directory_uri().'/images/chili_120x130.png" width="120" height="130" alt="Sæt kryds ved liste F!"/>'$args'<a href="http://www.sf.dk/">SF &mdash; lidt skarpere!</a>');
}

function 
sf_theme_widget_logo($args) {
    
sf_theme_image_widget('<img src="'.get_template_directory_uri().'/images/sflogo_120x114.png" width="120" height="114" alt="det ku\' være så godt!"/>'$args'<a href="http://www.sf.dk/">det ku\' være så godt</a>');
}

function 
sf_theme_image_widget($img$args$title '') {
    
extract($args);
    print 
$before_widget;
    if (
''!=$title) {
      
$title '<div style="text-align: center">' $title '</div>';
        print 
$before_title $title $after_title;
    }
    print 
'<div style="text-align: center; padding-bottom: 6px"><a href="http://www.sf.dk/" title="www.sf.dk">' $img '</a></div>';
    print 
$after_widget;
}

function 
sf_theme_widget_init() {
    global 
$sf_theme_box$registered_widgets$registered_widget_controls;

    if (!
function_exists('register_sidebar'))
        return;

    
register_sidebar(array('name'=>'Venstre',
            
'before_widget' => "\n".'<li id="%1$s" class="widget %2$s">'.$sf_theme_box['prew'],
            
'after_widget' => $sf_theme_box['prel'].$sf_theme_box['postl'].$sf_theme_box['postw'].'</li>'."\n",
            
'before_title' => $sf_theme_box['pret'],
            
'after_title' => $sf_theme_box['postt']));
    
register_sidebar(array('name'=>'Højre',
            
'before_widget' => "\n".'<li id="%1$s" class="widget %2$s">'.$sf_theme_box['prew'],
            
'after_widget' => $sf_theme_box['prel'].$sf_theme_box['postl'].$sf_theme_box['postw'].'</li>'."\n",
            
'before_title' => $sf_theme_box['pret'],
            
'after_title' => $sf_theme_box['postt']));

    if (
function_exists('wp_register_sidebar_widget')) {
        
$dims90 = array('height' => 90'width' => 300);
        
wp_register_sidebar_widget('pages'__('Pages'), 'sf_theme_widget_pages');
        unset(
$registered_widgets['Pages']);
    } else {
        unset(
$registered_widgets['Pages']);
        unset(
$registered_widget_controls['Pages']);
        
register_sidebar_widget('Pages''sf_theme_widget_pages');
        
register_widget_control('Pages''widget_pages_control'30090);
    }
    
register_sidebar_widget('xSF''sf_theme_widget_xsf');
    
register_sidebar_widget('xChiliF''sf_theme_widget_xchilif');
    
register_sidebar_widget('Chili''sf_theme_widget_chili');
    
register_sidebar_widget('Logo''sf_theme_widget_logo');

    
$options get_option('widget_rss');
    
$number $options['number'];
    if ( 
$number $number 1;
    if ( 
$number $number 9;
    for (
$i 1$i <= 9$i++) {
        
$name = array('RSS %s'null$i);
        
register_sidebar_widget($name$i <= $number 'sf_theme_widget_rss' /* unregister */ ''$i);
    }
    
remove_action('wp_head''widget_rss_head');

}

add_action('init''sf_theme_widget_init');

?>