package GCExport::GCExportCSV;

###################################################
#
#  Copyright 2005 Tian
#
#  This file is part of GCfilms.
#
#  GCfilms is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  GCfilms is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with GCfilms; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
###################################################

use strict;

use GCExport::GCExportBase;

{
    package GCExport::GCExporterCSV;

    use base qw(GCExport::GCExportBaseClass);

    sub new
    {
        my $proto = shift;
        my $class = ref($proto) || $proto;
        my $self  = $class->SUPER::new();
        
        bless ($self, $class);
        return $self;
    }

    sub getName
    {
        my $self = shift;
        
        return "CSV";
    }
    
    sub needsUTF8
    {
        return 1;
    }

    sub getOptions
    {
        my $self = shift;
        
        return [
            {
                name => 'withHeader',
                type => 'checkBox',
                label => 'ExportCSVHeader',
                default => '1'
            },

            {
                name => 'sep',
                type => 'shortText',
                label => 'ExportCSVSeparator',
                default => ';'
            },

            {
                name => 'rep',
                type => 'shortText',
                label => 'ExportCSVReplacement',
                default => ','
            }
        ];
        
    }
      
    sub wantsFieldsSelection
    {
        return 1;
    }
    
    sub wantsImagesSelection
    {
        return 1;
    }
    
    sub preProcess
    {
        my $self =  shift;
    }

    sub transformValue
    {
        my ($self, $value) = @_;
        
        $value =~ s/,+$//;
        $value =~ s /$self->{options}->{sep}/$self->{options}->{rep}/g;            
        $value =~ s/\n|\r//g;
        $value =~ s/<br>/ /g;
        return $value;
    }

    sub getHeader
    {
        my ($self, $number) = @_;
        my $result = '';
        
        if ($self->{options}->{withHeader})
        {
            foreach (@{$self->{options}->{fields}})
            {
                my $column = $self->{options}->{lang}->{FieldsList}->{$_};
                $result .= $self->transformValue($column).$self->{options}->{sep};
            }
            $result =~ s/$self->{options}->{sep}$//;
            $result .= "\n";
        }
        
        return $result;
    }

    sub getItem
    {
        my ($self, $movie, $number) = @_;
        my $result;

        foreach (@{$self->{options}->{fields}})
        {
            my $value = $movie->{$_};
            $result .= $self->transformValue($value).$self->{options}->{sep};
        }
        $result =~ s/$self->{options}->{sep}$//;
        $result .= "\n";
        
        return $result;
    }
    
    sub getFooter
    {
        my $self = shift;
        my $result;

        return $result;
    }

    sub postProcess
    {
        my ($self, $header, $body) = @_;
    }

    sub getEndInfo
    {
        my $self = shift;
        my $message;
        
        return $message;
    }
}

1;