# Copyright 2002-2008 Josh Clark and Global Moxie, LLC. This code cannot be 
# redistributed without permission from globalmoxie.com.  For more
# information, consult your Big Medium license.
#
# $Id: AV.pm 3233 2008-08-21 12:47:26Z josh $

package BigMed::Media::AV;
use strict;
use utf8;
use Carp;
use base qw(BigMed::Media::Document);


###########################################################
# SET IMAGE DATA SCHEMA
###########################################################

my @editor_fields = (
    {   column       => 'title',
        label        => 'DOCUMENT_Title',
        required     => 1,
    },
    {   column       => 'filename',
        label        => 'AV_Media File',
    },
    {   column       => 'shared',
        option_label => 'LIBRARY_Allow others to use this item',
        default      => 1,
    },
    {   column       => 'in_lib',
        option_label => 'LIBRARY_Display in library for reuse',
        default      => 1,
    },
);

BigMed::Media::AV->register_minicontent(
    editor_fields => \@editor_fields,
    can_link      => 1,
    can_embed     => 1,
    preview       => { html => \&preview_html },
    post_parse    => \&process_upload,
    batch_upload  => 'filename',
);

#accepted media files
my %MEDIA = (
    'qt'   => 1,
    'qtm'  => 1,
    'mov'  => 1,
    'moov' => 1,
    'mpeg' => 1,
    'mpe'  => 1,
    'mpg'  => 1,
    'mp4'  => 1,
    'mp3'  => 1,
    'mpu'  => 1,
    'm4a'  => 1,
    'mid'  => 1,
    'midi' => 1,
    'rmi'  => 1,
    'm4p'  => 1,
    'aac'  => 1,
    'avi'  => 1,
    'aif'  => 1,
    'aiff' => 1,
    'aifc' => 1,
    'ra'   => 1,
    'ram'  => 1,
    'rm'  => 1,
    'wav'  => 1,
    'wmv'  => 1,
    'wma'  => 1,
    'swf'  => 1,
);

sub preview_html {
    my ( $app, $obj, $roptions ) = @_;
    my ($filename, $file_url, $doc_class, $fs);
    if ( $filename = $obj->filename ) { # *should* always be true...
        $fs = $obj->filesize($app->current_site);
        $fs &&= "($fs)";
        my $url_dir = $app->current_site->doc_url;
        $file_url = "$url_dir/$filename";
        $doc_class =
          ( $filename =~ /[.]([a-zA-Z0-9]+)$/ ) ? 'docIconSm_' . lc $1 : '';
    }
    my $html = $app->html_template(
        'wi_av_preview.tmpl',
        TITLE      => $obj->title,
        CLASS_NAME => $doc_class,
        FILE_URL   => $file_url,
        FILENAME   => $filename,
        FILESIZE  => $fs,
    );
    return ( PREVIEW_HTML => $html );
}

sub process_upload {
    my ( $self, $rfields ) = @_;
    my $rdoc = $rfields->{filename} or return;    #there was probably an error
    my ( $filename, $tmpfile, $handle ) = @$rdoc;
    if ($filename) {
        my $suffix = ( $filename =~ /[.]([a-zA-Z0-9]+)$/ ) ? lc $1 : '';
        if ( !$MEDIA{$suffix} ) {
            close($handle);
            $rfields->{_ERROR}->{filename} = [
                'AV_Unsupported file format',
                $suffix,
                join( ', ', sort keys %MEDIA )
            ];
            return;
        }
    }
    $self->SUPER::process_upload($rfields);
}


1;

=head1 NAME

BigMed::Media:AV - Big Medium audio/video objects.

=head1 DESCRIPTION

BigMed::Media::AV objects represent audio/video documents in the Big Medium
system.

=head1 USAGE

BigMed::Media::AV is a subclass of BigMed::Media::Document. In addition to the
methods  documented below, please see the BigMed::Data documentation for details
about topics including:

=over 4

=item Creating a new data object

=item Saving a data object

=item Finding and sorting saved data objects

=item Data access methods

=item Error handling

=back

=head1 METHODS

=head2 Data Access Methods

BigMed::Media::AV objects hold the following pieces of data. They can be
accessed and set using the standard data access methods described in the
BigMed::Data documentation. See the L<"Searching and Sorting"> section below
for details on the data columns available to search and sort.

=over 4

=item * id

The numeric ID of the object

=item * site

The numeric ID of the site to which the object belongs

=item * owner

The numeric ID of the user who "owns" the media object.

=item * shared

Boolean value indicating whether the owner allows this media to be used by other
users with privileges at the same site. (Admins and webmasters are always
allowed to use the media objects).

= item * slug

The slug name for the media object. This may be used to generate a filename
or be used as an id string in widgets. The slug must be unique within the
BigMed::Media subclass.

= item * title

The name of the media object.

= item * version

The version of the media object.

= item * description

A text description of the media object (this might be used as a caption for
some types of media).

= item * filetype

The file extension for the document type.

=item * mod_time

Timestamp for when the last time the object was saved.

=item * create_time

Timestamp for when the object was first created.

=back

=head3 Searching and Sorting

You can look up and sort records by any combination of the following fields.
See the C<fetch> and C<select> documentation in BigMed::Data for more info.

=over 4

=item * id

=item * mod_time

=item * site

=item * owner

=item * shared

=item * slug

=item * title

=item * version

=back

=head1 SEE ALSO

=over 4

=item * BigMed::Data

=item * BigMed::Media

=back

=head1 AUTHOR & COPYRIGHTS

This module and all Big Medium modules are copyright Josh Clark
and Global Moxie. All rights reserved.

Use of this module and the Big Medium content
management system are governed by Global Moxie's software licenses
and may not be used outside of the terms and conditions outlined
there.

For more information, visit the Global Moxie website at
L<http://globalmoxie.com/>.

Big Medium and Global Moxie are service marks of Global Moxie
and Josh Clark. All rights reserved.

=cut
