<?php

function sf_theme_widget_profile($args) {
    global 
$wpdb$post$posts;
    if (
is_author())
      return;
    
extract($args);
    
$options get_option('sf_theme_widget_profile');
    if (!
$options['user'])
      return;
    
$title $options['title'];
    if (
'random' == $options['user']) {
      if (!
$usercount $wpdb->get_var('SELECT count(ID) FROM '.$wpdb->users.' JOIN '.$wpdb->usermeta.' WHERE ID=user_id AND meta_key="sf_theme_image" AND meta_value>0'))
        return;
      if (!
$user $wpdb->get_var('SELECT ID FROM '.$wpdb->users.' JOIN '.$wpdb->usermeta.' WHERE ID=user_id AND meta_key="sf_theme_image" AND meta_value>0 LIMIT '.rand(0$usercount-1).',1'))
        return;
    } elseif (
'latest' == $options['user']) {
        if (!
$posts || !is_array($posts) || !is_object($posts[0]))
            return;
        
$user $posts[0]->post_author;
        if (!
$wpdb->get_var('SELECT count(*) FROM '.$wpdb->usermeta.' WHERE '.$user.'=user_id AND meta_key="sf_theme_image" AND meta_value>0'))
            return;
    } else
      
$user $options['user'];
    if (
is_single() && $user!=$post->post_author)
      return;
    
$user get_userdata($user);
    if (!
$user->ID)
      return;
    print 
$before_widget $before_title $title $after_title;
    print 
'<span>' sf_theme_linked_author_name($user) . '</span>';
    print 
sf_theme_profile($user);
    print 
$after_widget;
}

function 
sf_theme_widget_profile_control() {
    global 
$wpdb;
    
$options get_option('sf_theme_widget_profile');
    if ( !
is_array($options) )
        
$options = array('title'=>'''user'=>'');
    if ( 
$_POST['profile-submit'] ) {
        
$options['title'] = strip_tags(stripslashes($_POST['profile-title']));
        
$options['user'] = strip_tags(stripslashes($_POST['profile-user']));
        
update_option('sf_theme_widget_profile'$options);
    }
    
$title htmlspecialchars($options['title'], ENT_QUOTES);
    
$user $options['user'];
    
$users $wpdb->get_results('SELECT ID, display_name FROM '.$wpdb->users.' JOIN '.$wpdb->usermeta.' WHERE ID=user_id AND meta_key="sf_theme_image" AND meta_value>0 ORDER BY display_name');
    
?>
    <label for="profile-title" style="line-height:35px;display:block;">Titel</label><input style="width: 200px;" id="profile-title" name="profile-title" type="text" value="<?php echo $title?>" />
    <label for="profile-user" style="line-height:35px;display:block;">Bruger</label>
    <select id="profile-user" name="profile-user">
      <option value="latest" <?php print ($user=='latest'?'selected="selected"':''); ?>>Seneste</option>
      <option value="random" <?php print ($user=='random'?'selected="selected"':''); ?>>Tilfældig</option>
    <?php if (sizeof($users)) { foreach ($users as $u) print '<option value="'.$u->ID.'" '.($user==$u->ID?'selected="selected"':'').'>'.$u->display_name.'</option>'; }; ?>
    </select>
    <input type="hidden" id="profile-submit" name="profile-submit" value="1" />
    <?php
}

function 
sf_theme_widget_profile_init() {
    if (!
function_exists('register_sidebar'))
        return;
    
register_sidebar_widget('Profil''sf_theme_widget_profile');
    
register_widget_control('Profil''sf_theme_widget_profile_control'300150);
}

add_action('init''sf_theme_widget_profile_init');

?>