#/*
# *  Copyright 2007 hkrn <hikarin@users.sourceforge.jp>
# *
# *  Licensed under the Apache License, Version 2.0 (the "License");
# *  you may not use this file except in compliance with the License.
# *  You may obtain a copy of the License at
# *
# *      http://www.apache.org/licenses/LICENSE-2.0
# *
# *  Unless required by applicable law or agreed to in writing, software
# *  distributed under the License is distributed on an "AS IS" BASIS,
# *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# *  See the License for the specific language governing permissions and
# *  limitations under the License.
# */
#
# $Id: meta.pm 466 2007-04-08 13:01:57Z hikarin $
#

package Zeromin2::App::meta;

use strict;
use Zeromin::Metadata qw();

sub load_header { _load( 'head', @_ ) }

sub load_meta { _load( 'meta', @_ ) }

sub load_footer { _load( 'foot', @_ ) }

sub load_thread_end {
    my ($zApp) = @_;
    $zApp->privilege( 'can_enter_meta_section', 1 )
        or return $zApp->return_value(1);

    my $zBBS  = $zApp->bbs();
    my $zUser = $zApp->user();
    my ( $unijp, $encoding ) = $zApp->unijp();
    my @data = split '<>',
        $unijp->set( Zeromin::Metadata->new($zBBS)->thread_end(), $encoding )
        ->get();
    my $message = $data[3] || '';
    $message =~ s/<br>/\n/gxms;
    $message =~ s/\s+\n\s+/\n/gxms;
    $message =~ s/\A\s+//xms;
    $message =~ s/\s+\z//xms;
    $message =~ s/\n+\z//xms;

    require Img0ch::Setting;
    my $iSetting = Img0ch::Setting->new($zBBS);

    $zApp->add_template_param(
        {   BBS_SUBTITLE =>
                $unijp->set( $iSetting->get('BBS_SUBTITLE'), $encoding )
                ->get(),
            BBS_TITLE =>
                $unijp->set( $iSetting->get('BBS_TITLE'), $encoding )->get(),
            BBS_TYPE_THEME => $iSetting->get('BBS_TYPE_THEME'),
            Date           => $data[2],
            IsTypeTemplate =>
                ( $iSetting->get('BBS_TEMPLATE') =~ /\Atype/xms ? 1 : 0 ),
            Mail    => $data[1],
            Message => $message,
            Name    => $data[0]
        }
    );
    $zApp->return_value(0);
}

sub _load {
    my ( $method, $zApp ) = @_;
    $zApp->privilege( 'can_enter_meta_section', 1 )
        or return $zApp->return_value(1);

    my $zBBS  = $zApp->bbs();
    my $zUser = $zApp->user();
    my ( $unijp, $encoding ) = $zApp->unijp();

    require Img0ch::Setting;
    my $iSetting = Img0ch::Setting->new($zBBS);

    my $data
        = $unijp->set( Zeromin::Metadata->new($zBBS)->$method, $encoding )
        ->get();
    $data =~ s/\s+\n\s+/\n/gxms;
    $data =~ s/\A\s+//xms;
    $data =~ s/\s+\z//xms;
    $data =~ s/\n+\z//xms;

    $zApp->add_template_param(
        {   BBS_SUBTITLE =>
                $unijp->set( $iSetting->get('BBS_SUBTITLE'), $encoding )
                ->get(),
            ,
            BBS_TITLE =>
                $unijp->set( $iSetting->get('BBS_TITLE'), $encoding )->get(),
            BBS_TYPE_THEME => $iSetting->get('BBS_TYPE_THEME'),
            Data           => $data,
            IsTypeTemplate =>
                ( $iSetting->get('BBS_TEMPLATE') =~ /\Atype/xms ? 1 : 0 )
        }
    );
    $zApp->return_value(0);
}

sub save_header { _save_meta( 'head', 'can_edit_head', @_ ) }

sub save_meta { _save_meta( 'meta', 'can_edit_meta', @_ ) }

sub save_footer { _save_meta( 'foot', 'can_edit_meta', @_ ) }

sub save_thread_end {
    my ($zApp) = @_;
    $zApp->check_csrf() or return $zApp->return_value(1);
    $zApp->privilege( 'can_edit_head', 1 ) or return $zApp->return_value(1);

    my ( undef, $encoding ) = $zApp->unijp();
    my $iRequest = $zApp->request();
    my $date     = $iRequest->param('date');
    $date =~ /\AOver\s+\d+\s+Thread\z/xms or return $zApp->return_value(2);

    my $from = $iRequest->param_multibyte( 'FROM',    $encoding, 1 );
    my $mail = $iRequest->param_multibyte( 'mail',    $encoding );
    my $text = $iRequest->param_multibyte( 'MESSAGE', $encoding, 1 );
    $text =~ s/\r\n/\n/gxms;
    $text =~ s/\n/<br>/gxms;

    my $content = join '<>', $from, $mail, $date, $text, '';
    Zeromin::Metadata->new( $zApp->bbs() )->thread_end( \$content );

    $zApp->logger( 1, 'MODIFIED META DATA: %s', ['thread_end'] );
    return $zApp->return_value(0);
}

sub _save_meta {
    my ( $method, $privilege, $zApp ) = @_;
    $zApp->check_csrf() or return $zApp->return_value(1);
    $zApp->privilege( $privilege, 1 ) or return $zApp->return_value(1);
    my ( undef, $encoding ) = $zApp->unijp();

    my $text = $zApp->request()->param_multibyte( 'content', $encoding, 1 );
    $text =~ s/\r\n/\n/gxms;
    Zeromin::Metadata->new($zApp->bbs())->$method(\$text);
    $zApp->logger( 1, 'MODIFIED META DATA: %s', [$method] );

    return $zApp->return_value(0);
}

1;
__END__
