<?page
title=><?_ml .title _ml?>
body<=

<?_code
{
    use strict;
    use vars qw(%GET %POST);

    LJ::set_active_crumb('createstyle');

    return LJ::server_down_html() if $LJ::SERVER_DOWN;
    return BML::redirect("create.bml") unless LJ::did_post();

    my $remote = LJ::get_remote();
    return "<?needlogin?>"
        unless $remote;

    my $authas = $GET{'authas'} || $remote->{'user'};
    my $u = LJ::get_authas_user($authas);
    return LJ::bad_input($ML{'error.authinvalid'})
        unless $u;
 
    unless (LJ::get_cap($u, "styles")) {
        return LJ::bad_input($ML{'.error.accounttype'});
    }

    # error check submission
    return LJ::bad_input($ML{'.error.invalidview'})
        unless $POST{'type'} && defined $LJ::viewinfo{$POST{'type'}};

    my $view = $POST{'type'};
    my $base = "";
    my $baseid = $POST{"base_$view"} || $POST{"basenum_$view"};

    if ($baseid) {
        my $rec = LJ::S1::get_style($baseid);

        return LJ::bad_input(BML::ml('.error.stylenotfound',{ 'baseid' => $baseid, }))
            unless $rec && $rec->{'type'} eq $view;

        return LJ::bad_input(BML::ml('.error.stylenotpublic',{ 'baseid' => $baseid, }))
            unless $rec->{'is_public'} eq "Y" || $rec->{'user'} eq $u->{'user'};

        # they're allowed to base on this style
        $base = $rec->{'formatdata'};
    }

    ### start of output

    my $ret = "";
    my $des = "$u->{'user'}-$view-new";

    # let's see if they accidentally double-clicked:
    my $double_clicked = 0;
    my $new_id = 0;
    my $oldstyle = LJ::S1::check_dup_style($u, $view, $des);

    # closure to show edit form
    my $editform = sub {
        my $getextra = "?authas=$authas" if $u->{'user'} ne $remote->{'user'};
        return "<div align='center'><form method='post' action='$LJ::SITEROOT/styles/edit_do.bml$getextra'>\n" .
               LJ::html_hidden('mode', 'editstyle', 'styleid', $_[0]) . "\n" .
               LJ::html_submit(undef, $ML{'.editstyle'}) . "</form></div>\n";
    };

    if ($oldstyle) {
        if ($oldstyle->{'formatdata'} eq $base) {
            # the user double-clicked
            $double_clicked = 1;
            $new_id = $oldstyle->{'styleid'};

        } else {
            $ret .= "<?h1 $ML{'Error'} h1?>" .
                "<?p " . BML::ml('.error.styleexists',{ 'des' => $des, 'view' => $view, }) . " p?>";

            $ret .= $editform->($oldstyle->{'styleid'});
            return $ret;
        }
    }

    unless ($double_clicked) {
        $new_id = LJ::S1::create_style($u,
                                       { 'styledes' => $des,
                                         'type' => $view,
                                         'formatdata' => $base });
        return "<?h1 $ML{'Error'} h1?><?p $ML{'error.nodb'} p?>"
            unless defined $new_id;            
    }

    $ret .= "<?h1 $ML{'.success.head'} h1?><?p " . BML::ml('.success.text',{ 'des' => $des, }) . " p?>\n";

    $ret .= $editform->($new_id);

    return $ret;
}
_code?>
<=body
page?><?_c <LJDEP>
post: htdocs/styles/edit_do.bml
</LJDEP> _c?>
