package Kwiki::UserPhoto;
use Kwiki::Plugin qw(-Base -XXX);
use mixin 'Kwiki::Installer';
our $VERSION = '0.01';

const class_id => 'user_photo';
const class_title => 'User Photo';
const css_file => 'user_photo.css';
const cgi_class => 'Kwiki::UserPhoto::CGI';
const config_file => 'user_photo.yaml';

field 'display_msg';

sub register {
    my $registry = shift;
    $registry->add(widget => 'user_photo',
		   template => 'user_photo_widget.html');
    $registry->add(action => 'user_photo');
}

# Return the url of current user.
sub url {
    my $user = $self->hub->users->current->name;
    my $dir = io->catdir($self->plugin_directory,$user);
    if($dir->exists) {
	for($dir->all) {
	    return $_->name if($_->name =~ /thumb./);
	}
    }
    return $self->config->user_photo_default;
}

sub user_photo {
    my $mode = $self->cgi->run_mode;
    # Valid modes: upload
    $self->$mode if($mode && $self->can($mode));
    $self->render_screen;
}

sub upload {
    require YAML;
    my $username = $self->users->current->name;
    my $file = $self->cgi->uploaded_file;
    return $self->display_msg("You must setup a proper user name to upload your photo") unless($username);
    if($file){
	my $fh = $file->{handle};
	my $newfile = io->catfile($self->plugin_directory, $username, $file->{filename});
	$_->unlink for io->catdir($self->plugin_directory,$username)->assert->all;
	binmode($fh);
	$newfile->assert->print(<$fh>);
	$newfile->close();
	$self->make_thumbnail($newfile,
			      $self->config->user_photo_width,
			      $self->config->user_photo_height,
			     );
    } else {
	$self->display_msg("Please specify a file to upload.");
    }
}

# From Kwiki::Attachments
sub make_thumbnail {
   use File::Basename;

   my ($file,$width,$height) = @_;
   my ($fname, $fpath, $ftype) = fileparse($file, qr(\..*$));
   my $thumb = io->catfile($fpath, "thumb$ftype");
   if (eval { require Imager }) {
      my $found = 0;
      if ($ftype =~ /jpg/i) {
         $found = 1;
      } else {
         for (keys %Imager::format) {
            if ($ftype =~ /$_/oi) {
               $found = 1;
               last;
            }
         }
      }
      if ($found) {
         my $image = Imager->new;
         return unless ref($image);
         $image->read(file=>$file);
         my $thumb_img = $image->scale(xpixels=>$width,ypixels=>$height);
         $thumb_img->write(file=>$thumb);
      }
   } elsif (eval { require Image::Magick }) {
      my $image = Image::Magick->new;
      return unless ref($image);
      if (!$image->Read($file)) {
         if (!$image->Scale(geometry=>"${width}x${height}")) {
            if (!$image->Contrast(sharpen=>"true")) {
               $image->Write($thumb);
            }
         }
      }
   }
}

package Kwiki::UserPhoto::CGI;
use base 'Kwiki::CGI';

cgi 'run_mode';
cgi 'uploaded_file' => qw(-upload);

package Kwiki::UserPhoto;

__DATA__


__config/user_photo.yaml__
user_photo_width: 128
user_photo_height: 128
user_photo_default: user_photo_default.jpg
__template/tt2/user_photo_content.html__
<div class="user_photo">
<p class="message">[% self.display_msg %]</p>
<form method="post" action="[% script_name %]" enctype="multipart/form-data">
<input type="hidden" name="action"   value="user_photo" />
<input type="hidden" name="run_mode" value="upload" />
<input type="file"   name="uploaded_file" />
<input type="submit" name="Upload"   value="Upload" />
</form>
</div>
__template/tt2/user_photo_widget.html__
<div class="user_photo">
<a href="[% script_name %]?action=user_photo"><img src="[% hub.user_photo.url %]" /></a>
</div>
__css/user_photo.css__
.user_photo a { border: none; }
.user_photo img { border: none; }
__user_photo_default.jpg__
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0a
HBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/wAALCABkAGABAREA/8QAHwAAAQUBAQEB
AQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1Fh
ByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZ
WmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXG
x8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/9oACAEBAAA/APf6KKo6vq1homnTajqV
1HbWkK5e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