<?page
title=><?_code $ML{'.title'} _code?>
body<=
<?_code
{
    use strict;
    use vars qw($GET $POST);

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

    my $err = sub { return "<?h1 $ML{'Error'} h1?><?p $_[0] p?>"; };
    return $err->($ML{'.disabled'})
        if $LJ::DISABLED{tags};

    my ($ret, $msg);

    return $err->($ML{'.invalid.link'})
        unless LJ::did_post() || ($GET{journal} && $GET{itemid});
    
    my $journal = $GET{journal} || $POST{journal};
    my $u = LJ::load_user($journal);
    return $err->($ML{'.invalid.journal'}) unless $u;

    my $ditemid = ($GET{itemid} || $POST{itemid})+0;
    my $anum = $ditemid % 256;
    my $jitemid = $ditemid >> 8;
    return $err->($ML{'.invalid.entry'}) unless $jitemid;

    my $logrow = LJ::get_log2_row($u, $jitemid);
    return $err->($ML{'.invalid.entry'}) unless $logrow;
    return $err->($ML{'.invalid.entry'}) unless $logrow->{anum} == $anum;
    return $err->($ML{'.invalid.notauthorized'})
        unless LJ::can_view($remote, $logrow);

    if (LJ::did_post()) {
        return $err->($ML{'.invalid.link'})
            unless LJ::check_form_auth();

        LJ::Tags::update_logtags($u, $jitemid, {
            set_string => $POST{edittags},
            remote => $remote,
        });

        BML::redirect( LJ::journal_base($u) . "/$ditemid.html" );
        #$msg = "<div class='update_good'>Tags successfully updated.</div>";
    }

    my $item = LJ::Talk::get_journal_item($u, $jitemid);

    my $subj = $item->{'subject'};
    my $evt  = $item->{'event'};

    LJ::CleanHTML::clean_subject(\$subj);
    LJ::CleanHTML::clean_event(\$evt);

    ## $item->{'props'}->{'tags'} ???
    my $logtags = LJ::Tags::get_logtags($u, $jitemid);
    my $usertags = LJ::Tags::get_usertags($u, { remote => $remote }) || {};
    $logtags = $logtags->{$jitemid} || {};
    my $logtagstr = join ', ', map { LJ::ejs($_) } sort values %$logtags;

    $ret .= "<?p $ML{'.intro'} p?><br />";
    $ret .= "<script type='text/javascript'> var cur_taglist = '$logtagstr'; </script>";  

    $ret .= '<table class="edittbl" cellpadding="0" cellspacing="0" width="50%">';
    $ret .= "<tr><td class='l'>$ML{'.subject'}</td><td>$subj</td></tr>" if $subj;
    $ret .= "<tr><td class='l'>$ML{'.entry'}</td><td>$evt</td></tr>";
    $ret .= "<tr><td class='l'>&nbsp;</td><td>&nbsp</td></tr>"; # spacer

    $ret .= "<tr><td class='l'>$ML{'.current'}</td>";
    $ret .= '<form method="POST" action="/edittags.bml" id="edit_tagform">';
    $ret .= LJ::form_auth();
    $ret .= "<td class='sep'>";
    if ( LJ::Tags::can_add_tags($u, $remote) ) {
        $ret .= LJ::html_text(
            {
                name  => 'edittags',
                value => (join ', ', sort values %$logtags),
                size  => 40,
                class => 'tagfield',
                id    => 'tagfield',
            }
        );
        $ret .= '&nbsp;&nbsp;';
        $ret .= LJ::html_submit( 'save', $ML{'.button.save'}, { class => 'btn' });
        $ret .= $msg if $msg;
    } else {
        # no widgets
        $ret .= $ML{'.permissions.none'};
    }
    $ret .= "</td></tr>";

    $ret .= "<tr><td class='l'>$ML{'.users'}</td><td class='curtags'>";

    if ( scalar keys %$usertags ) {
        $ret .= "<select name='tags' multiple='multiple' class='tagbox_nohist' " .
                "onChange='edit_tagselect(this)'>";
        foreach (sort { $a->{name} cmp $b->{name} } values %$usertags) {
            $ret .= "<option value='" . LJ::ehtml($_->{name}) . "'>" . LJ::ehtml($_->{name}) . "</option>";
        }
        $ret .= "</select>";
    } else {
        $ret .= "none"
    }      
    
    $ret .= "<br /><br />";
    $ret .= "$ML{'.permissions.add.yes'}<br />" if LJ::Tags::can_add_tags($u, $remote);
    $ret .= "$ML{'.permissions.control.yes'}<br />" if LJ::Tags::can_control_tags($u, $remote);
    $ret .= BML::ml('.view', { aopts => 'href="' . LJ::journal_base($u) . "/$ditemid.html" . '"' });
    $ret .= "</td></tr>";

    $ret .= '</table>';
    $ret .= LJ::html_hidden('journal', $journal);
    $ret .= LJ::html_hidden('itemid', $GET{itemid} || $POST{itemid});
    $ret .= '</form>';

    return $ret;
}
_code?>
<=body
head<=
<link rel='stylesheet' type='text/css' href='/styles/tags.css' />
<script type="text/javascript" src="/js/tags.js"></script>
<=head
page?>
