webgui/lib/WebGUI/Utility/Gallery.pm

289 lines
10 KiB
Perl

package WebGUI::Utility::Gallery;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2008 Plain Black Corporation.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
http://www.plainblack.com info@plainblack.com
-------------------------------------------------------------------
=cut
use strict;
use Carp qw( carp croak );
use File::Find;
use Scalar::Util qw( blessed );
use WebGUI::Asset::Wobject::Collaboration;
use WebGUI::Asset::Wobject::Gallery;
use WebGUI::Asset::Post::Thread;
use WebGUI::Storage::Image;
=head1 NAME
WebGUI::Utility::Gallery -- Utility functions for working
with Gallery assets.
=head1 DESCRIPTION
This module provides utility functions to work with Gallery assets from
utility scripts.
This module is B<NOT> to be used by the Gallery asset itself!
=head1 SYNOPSIS
use WebGUI::Utility::Gallery;
my $utility = "WebGUI::Utility::Gallery" # <- not as cumbersome
# Add albums from a collaboration system's threads
my $gallery = WebGUI::Asset::Wobject::Gallery->new( ... );
my $collab = WebGUI::Asset::Wobject::Collaboration->new( ... );
$utility->addAlbumFromCollaboration( $gallery, $collab );
# Add a single album from a collaboration system thread
my $thread = WebGUI::Asset::Post::Thread->new( ... );
$utility->addAlbumFromThread( $gallery, $thread );
# Add a single album from a Folder asset
my $folder = WebGUI::Asset::Wobject::Folder->new( ... );
$utility->addAlbumFromFolder( $gallery, $folder );
# Add a single album from a filesystem branch
$utility->addAlbumFromFilesystem( $gallery, "/Users/Doug/Photos" );
# Add a single album for every folder in a filesystem branch
$utility->addAlbumFromFilesystem(
$gallery, "/Users/Doug/Photos",
{
multiple => 1,
}
);
=head1 METHODS
These methods are available from this class:
=cut
#----------------------------------------------------------------------------
=head2 addAlbumFromCollaboration ( gallery, collab )
Add an album or albums to the gallery from the given Collaboration System.
C<gallery> is an instanciated Gallery asset. C<collab> is an instanciated
Collaboration System asset.
Will add one album for every thread in the Collaboration System. Will call
C<addAlbumFromThread> to do its dirty work.
=cut
sub addAlbumFromCollaboration {
my $class = shift;
my $gallery = shift;
my $collab = shift;
croak "First argument must be Gallery asset"
unless blessed $gallery && $gallery->isa('WebGUI::Asset::Wobject::Gallery');
croak "Second argument must be Collaboration System asset"
unless blessed $collab && $collab->isa('WebGUI::Asset::Wobject::Collaboration');
my $threads
= $collab->getLineage(['descendants'], {
returnObjects => 1,
includeOnlyClasses => ['WebGUI::Asset::Post::Thread'],
statesToInclude => ['published'],
statusToInclude => ['approved', 'archived', 'pending'],
});
for my $thread ( @$threads ) {
$class->addAlbumFromThread( $gallery, $thread );
}
return undef;
}
#----------------------------------------------------------------------------
=head2 addAlbumFromFilesystem ( gallery, root [, options] )
Add an album to the gallery from the file system. C<gallery> is an
instanciated Gallery asset. C<root> is a location on the file system.
C<options> is a hash reference of options with the following keys:
multiple - Create multiple albums, one for each folder.
=cut
sub addAlbumFromFilesystem {
my $class = shift;
my $gallery = shift;
my $root = shift;
my $options = shift;
# TODO!!!
return undef;
}
#----------------------------------------------------------------------------
=head2 addAlbumFromFolder ( gallery, folder )
Add an album from a Folder asset filled with File assets. C<gallery> is an
instance of a Gallery asset. C<folder> is an instance of a Folder asset.
=cut
sub addAlbumFromFolder {
my $class = shift;
my $gallery = shift;
my $folder = shift;
croak "First argument must be Gallery asset"
unless blessed $gallery && $gallery->isa('WebGUI::Asset::Wobject::Gallery');
croak "Second argument must be Folder asset"
unless blessed $folder && $folder->isa('WebGUI::Asset::Wobject::Folder');
my $session = $gallery->session;
my $addOptions = { skipAutoCommitWorkflows => 1 };
# Create the new album
my $album = $gallery->addChild({
className => 'WebGUI::Asset::Wobject::GalleryAlbum',
description => $folder->get('description'),
menuTitle => $folder->get('menuTitle'),
createdBy => $folder->get('createdBy'),
creationDate => $folder->get('creationDate'),
ownerUserId => $folder->get('ownerUserId'),
synopsis => $folder->get('synopsis'),
title => $folder->get('title'),
url => $session->url->urlize( $gallery->get('url') . "/" . $folder->get('title') ),
}, undef, $folder->get('revisionDate'), $addOptions );
my $fileIds
= $folder->getLineage( ['children'], {
joinClass => 'WebGUI::Asset::File',
} );
for my $fileId ( @{ $fileIds } ) {
my $oldFile = WebGUI::Asset->newByDynamicClass( $session, $fileId );
my $oldStorage = $oldFile->getStorageLocation;
my $className = $gallery->getAssetClassForFile( $oldStorage->getPath( $oldFile->get('filename') ) );
if ( !$className ) {
warn "Skipping " . $oldFile->get('filename') . " Gallery doesn't handle this file type";
next;
}
my $newFile = $album->addChild({
className => $className,
createdBy => $oldFile->get('createdBy'),
creationDate => $oldFile->get('creationDate'),
menuTitle => $oldFile->get('menuTitle'),
ownerUserId => $oldFile->get('ownerUserId'),
synopsis => $oldFile->get('synopsis'),
title => $oldFile->get('title'),
url => $session->url->urlize( $album->get('url') . "/" . $oldFile->get('menuTitle') ),
}, undef, $oldFile->get('revisionDate'), $addOptions );
$newFile->setFile( $oldStorage->getPath( $oldFile->get('filename') ) );
}
return undef;
}
#----------------------------------------------------------------------------
=head2 addAlbumFromThread ( gallery, thread )
Add an album to the gallery from the given Collaboration System thread.
C<gallery> is an instanciated Gallery asset. C<thread> is an instanciated
Thread asset.
=cut
sub addAlbumFromThread {
my $class = shift;
my $gallery = shift;
my $thread = shift;
croak "First argument must be Gallery asset"
unless blessed $gallery && $gallery->isa('WebGUI::Asset::Wobject::Gallery');
croak "Second argument must be Thread asset"
unless blessed $thread && $thread->isa('WebGUI::Asset::Post::Thread');
my $session = $gallery->session;
my $addOptions = { skipAutoCommitWorkflows => 1 };
# Create the new album
my $album = $gallery->addChild({
className => 'WebGUI::Asset::Wobject::GalleryAlbum',
description => $thread->get('content'),
menuTitle => $thread->get('menuTitle'),
createdBy => $thread->get('createdBy'),
creationDate => $thread->get('creationDate'),
ownerUserId => $thread->get('ownerUserId'),
synopsis => $thread->get('synopsis'),
title => $thread->get('title'),
url => $session->url->urlize( $gallery->get('url') . "/" . $thread->get('title') ),
userDefined1 => $thread->get('userDefined1'),
userDefined2 => $thread->get('userDefined2'),
userDefined3 => $thread->get('userDefined3'),
userDefined4 => $thread->get('userDefined4'),
userDefined5 => $thread->get('userDefined5'),
}, undef, $thread->get('revisionDate'), $addOptions );
for my $post ( @{ $thread->getPosts } ) {
if ( my $storageId = $post->get('storageId') ) {
# Use WebGUI::Storage::Image to avoid thumbnails if there
my $storage = WebGUI::Storage::Image->get( $session, $storageId );
for my $filename ( @{$storage->getFiles} ) {
my $className = $gallery->getAssetClassForFile( $filename );
if ( !$className ) {
warn "Skipping $filename because Gallery doesn't handle this file type";
next;
}
# Get rid of that file extention
my ($title) = $filename =~ m{(.*)\.[^.]*$};
# Don't repeat the thread
my $synopsis
= $post->get('content') ne $thread->get('content')
? $post->get('content')
: undef
;
my $file = $album->addChild({
className => $className,
createdBy => $post->get('createdBy'),
creationDate => $post->get('creationDate'),
menuTitle => $title,
ownerUserId => $post->get('ownerUserId'),
synopsis => $synopsis,
title => $title,
url => $session->url->urlize( $album->get('url') . "/" . $title ),
userDefined1 => $post->get('userDefined1'),
userDefined2 => $post->get('userDefined2'),
userDefined3 => $post->get('userDefined3'),
userDefined4 => $post->get('userDefined4'),
userDefined5 => $post->get('userDefined5'),
}, undef, $post->get('revisionDate'), $addOptions );
$file->setFile( $storage->getPath( $filename ) );
}
}
}
return undef;
}
1;