# 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: Build.pm 3043 2008-03-31 14:00:38Z josh $

package BigMed::App::Web::Build;
use strict;
use warnings;
use utf8;
use base qw(BigMed::App::Web::CP);
use Carp;
$Carp::Verbose = 1;
use BigMed::Builder;

sub setup {
    my $app = shift;
    $app->start_mode('menu');
    $app->set_cp_selected_nav('Layout');
    $app->run_modes(
        'AUTOLOAD'      => sub { $_[0]->rm_menu() },
        'menu'          => 'rm_menu',
        'ajax-build'    => 'rm_ajax_build',
        'build-all'     => 'rm_build_all',
        'build-section' => 'rm_build_section',
        'build-front'   => 'rm_build_front',
    );
    return;
}

my %category_names;

sub cgiapp_prerun {
    my $app = shift;
    $app->SUPER::cgiapp_prerun;
    $app->require_privilege_level(5);
    return;
}

###########################################################
# RUN MODES
###########################################################

sub rm_menu {
    my $app     = shift;
    my %options = @_;
    my $site    = $app->current_site;

    #default field values build all pages
    #(we keep this if ajax_section is 'site');
    my $v_allpages_choice   = '';
    my $v_frontpages_choice = $site->homepage_id;
    my $v_build_allpages    = 1;
    my $v_build_frontpages  = 0;

    my $ajax_section = $options{ajax_submit} || q{};
    if ( $ajax_section eq 'home' ) {    #homepage only
        $v_build_allpages   = 0;
        $v_build_frontpages = 1;
    }
    elsif ( $ajax_section =~ /^sec(\d+)/ ) {    # one section's front pages
        $v_frontpages_choice = $1;
        $v_build_allpages    = 0;
        $v_build_frontpages  = 1;
    }
    elsif ( $ajax_section eq 'sec' ) {          # all front pages
        $v_build_allpages    = 0;
        $v_build_frontpages  = 1;
        $v_frontpages_choice = q{};
    }
    elsif ($ajax_section) {                     #all pages for one section
        $v_allpages_choice = $ajax_section;
    }

    #otherwise we're sticking with the build-all-pages default

    my $all_pages_choice = $app->prompt_field_ref(
        id                 => 'allpages_choice',
        prompt_as          => 'select_section',
        site               => $site,
        label              => 'BUILD_Section',
        value              => $v_allpages_choice,
        all_sections_option => 1,
    );
    my $front_pages_choice = $app->prompt_field_ref(
        id                 => 'frontpages_choice',
        prompt_as          => 'select_section',
        site               => $site,
        label              => 'BUILD_Section',
        value              => $v_frontpages_choice,
        all_sections_option => 1,
        homepage           => 1,
    );
    my $build_what = $app->prompt_field_ref(
        id        => 'build_what',
        prompt_as => 'radio_toggle',
        label     => 'BUILD_What To Build',
        required  => 1,
        value     => [
            {   id      => 'build_all',
                label   => 'BUILD_All Pages',
                value   => 'all',
                checked => $v_build_allpages,
                field   => $all_pages_choice,
            },
            {   id      => 'build_front',
                label   => 'BUILD_Front Pages',
                value   => 'front',
                field   => $front_pages_choice,
                checked => $v_build_frontpages,
            },
        ],
    );
    my $submit = $app->prompt_field_ref(
        id        => 'build_submit',
        prompt_as => 'submit',
        value     => $app->language('BUILD_Rebuild Pages'),
    );

    my $fieldset = $app->prompt_fieldset_ref(
        id        => 'bmfs_build',
        fields    => [$build_what, $submit],
        title     => 'BUILD_What To Build',
        query     => $options{query},
        field_msg => $options{field_msg},
        help      => 'BUILD_build_help',
    );

    #page title and message
    my $title_unlocal = $options{head} || 'BUILD_Rebuild Pages';
    my ( $title, $message ) = $app->title_and_message(
        field_msg => $options{field_msg},
        message   => $options{message},
        title     => $title_unlocal,
    );

    my $form_url = $app->build_url(
        script => 'bm-build.cgi',
        rm     => 'ajax-build',
        site   => $site->id,
    );

    $app->set_cp_breadcrumbs(
        {   bc_label => 'BM_CP_NAVLABEL_Layout',
            bc_url   => $app->build_url(
                script => 'bm-templates.cgi',
                rm     => 'main-menu',
                site   => $site->id
            )
        },
        { bc_label => 'BUILD_Rebuild Pages' },
    );

    $app->js_add_script( $app->env('BMADMINURL') . '/js/bm-build.js' );
    if ($ajax_section) {
        $app->js_add_onload(
            q~BM.Build.submitForm('bmBuildForm', 'bmBuilderStatus');~ );
    }
    return $app->html_template_screen(
        'screen_build_menu.tmpl',
        bmcp_title  => $title,
        message     => $message,
        form_url    => $form_url,
        fieldsets   => [$fieldset],
        show_status => $ajax_section,
        show_form   => !$ajax_section,
    );
}

sub rm_ajax_build {
    my $app   = shift;
    my $site  = $app->current_site;
    my %param = $app->parse_submission(
        {   id       => 'build_what',
            parse_as => 'radio_toggle',
        },
        {   id       => 'allpages_choice',
            parse_as => 'value_list',
        },
        {   id       => 'frontpages_choice',
            parse_as => 'value_list',
        },
    );
    return $app->ajax_parse_error( $param{_ERROR} ) if $param{_ERROR};

    #initialize build parameters
    my ( $section, $no_detail, $defer_overflow, $top_only );
    if ( $param{build_what} eq 'front' ) {
        $no_detail = $defer_overflow = 1;
        $section = $param{'frontpages_choice'};
    }
    else {
        $section = $param{'allpages_choice'};
    }

    if ( $section && $section == $site->homepage_id ) {   #build homepage only
        undef $section;
        $top_only = 1;
    }
    elsif ($section) {
        defined( my $sec_obj = $site->section_obj_by_id($section) )
          or return $app->ajax_system_error();
        if ( !$sec_obj ) {
            $app->set_error(
                head => 'TEMPLATE_Unknown Section',
                text => 'TEMPLATE_TEXT_Unknown Section',
            );
            return $app->ajax_system_error();
        }
        $section = [$section, $site->all_descendants_ids($sec_obj)];
    }

    my $builder = BigMed::Builder->new( site => $site )
      or return $app->ajax_system_error();
    my $url = $builder->build(
        sections       => $section,
        no_detail      => $no_detail,
        defer_overflow => $defer_overflow,
        top_only       => $top_only,
        statusbar      => 1
      )
      or return $app->ajax_system_error();

    return $app->ajax_json_response( { url => $url } );
}

sub rm_build_all {
    my $app = shift;
    return $app->rm_menu( ajax_submit => 'site' );
}

sub rm_build_section {
    my $app = shift;
    my ($sec_id) = $app->path_args;
    return $app->rm_menu( ajax_submit => $sec_id );
}

sub rm_build_front {
    my $app  = shift;
    my $site = $app->current_site;
    my ($sec_id) = $app->path_args || q{};
    my $param =
      ( $sec_id eq 'home' || $sec_id == $site->homepage_id )
      ? 'home'
      : "sec$sec_id";
    return $app->rm_menu( ajax_submit => $param );
}

1;

__END__

