Support Stock Answers body<= " unless $remote; # most things have a category id my $spcatid = ($GET{spcatid} || $POST{spcatid} || 0) + 0; my $cats = LJ::Support::load_cats(); return "" unless !$spcatid || $cats->{$spcatid}; # editing is based on ability to grant supporthelp. and throw an error if they # posted but can't edit. my $canedit = ($spcatid && LJ::check_priv($remote, 'admin', "supporthelp/$cats->{$spcatid}->{catkey}")) || LJ::check_priv($remote, 'admin', 'supporthelp') || LJ::check_priv($remote, 'admin', '*'); return "" if LJ::did_post() && ! $canedit; # viewing is based on having supporthelp over the particular category you're viewing. my %canview; # spcatid => 0/1 foreach my $cat (values %$cats) { $canview{$cat->{spcatid}} = 1 if LJ::Support::can_help({ _cat => $cat }, $remote); } return "" unless %canview; return "" if $spcatid && ! $canview{$spcatid}; # filter down the category list $cats = { map { $_->{spcatid}, $_ } grep { $canview{$_->{spcatid}} } values %$cats }; my $ansid = ($GET{ansid} || 0) + 0; my $ret = ""; my $self = "$LJ::SITEROOT/support/stock_answers.bml"; if ($POST{'action:delete'}) { my $dbh = LJ::get_db_writer(); return "" unless $dbh; my $ct = $dbh->do("DELETE FROM support_answers WHERE ansid = ? AND spcatid = ?", undef, $ansid, $spcatid); return "errstr . " p?>" if $dbh->err; return "" unless $ct; return BML::redirect("$self?spcatid=$spcatid&deleted=1"); } if ($POST{'action:new'} || $POST{'action:save'}) { my ($subj, $body) = ($POST{subject}, $POST{body}); foreach my $ref (\$subj, \$body) { $$ref =~ s/^\s+//; $$ref =~ s/\s+$//; # FIXME: more stuff to clean it up? } return "" unless $spcatid && $subj && $body; my $dbh = LJ::get_db_writer(); return "" unless $dbh; if ($POST{'action:new'}) { my $newid = LJ::alloc_global_counter('A'); return "" unless $newid; $dbh->do("INSERT INTO support_answers (ansid, spcatid, subject, body, lastmodtime, lastmoduserid) " . "VALUES (?, ?, ?, ?, UNIX_TIMESTAMP(), ?)", undef, $newid, $spcatid, $subj, $body, $remote->{userid}); return "errstr . " p?>" if $dbh->err; return BML::redirect("$self?user=$remote->{user}&spcatid=$spcatid&ansid=$newid&added=1"); } else { return "" unless $ansid; $dbh->do("UPDATE support_answers SET subject = ?, body = ?, lastmodtime = UNIX_TIMESTAMP(), " . "lastmoduserid = ? WHERE ansid = ?", undef, $subj, $body, $remote->{userid}, $ansid); return "errstr . " p?>" if $dbh->err; return BML::redirect("$self?user=$remote->{user}&spcatid=$spcatid&ansid=$ansid&saved=1"); } } if ($GET{new}) { $ret .= "
p?>"; return $ret; } my $dbr = LJ::get_db_reader(); return "" unless $dbr; my $cols = "ansid, spcatid, subject, lastmodtime, lastmoduserid"; $cols .= ", body" if $ansid; my $sql = "SELECT $cols FROM support_answers"; my @bind = (); if ($spcatid || $ansid) { $sql .= " WHERE "; if ($spcatid) { $sql .= "spcatid = ?"; push @bind, $spcatid; } if ($ansid) { $sql .= ($spcatid ? " AND " : "") . "ansid = ?"; push @bind, $ansid; } } my $sth = $dbr->prepare($sql); $sth->execute(@bind); return "errstr . " p?>" if $sth->err; $ret .= " p?>"; my %answers; while (my $row = $sth->fetchrow_hashref) { $answers{$row->{spcatid}}->{$row->{ansid}} = { subject => $row->{subject}, body => $row->{body}, lastmodtime => $row->{lastmodtime}, lastmoduser => LJ::load_userid($row->{lastmoduserid}), }; } $ret .= "View All ]"; $ret .= " [ Add New Answer ]" if $canedit; $ret .= " p?>"; if ($GET{added}) { $ret .= "Answer added! p?>"; } elsif ($GET{saved}) { $ret .= "Changes saved! p?>"; } elsif ($GET{deleted}) { $ret .= "Answer deleted! p?>"; } # bilevel sort, fun and messy foreach my $spcatid (sort { $cats->{$a}->{catname} cmp $cats->{$b}->{catname} } keys %$cats) { next unless %{$answers{$spcatid} || {}}; $ret .= "$cats->{$spcatid}->{catname} h2?>"; $ret .= "