ssafronova (ssafronova) wrote in changelog,
ssafronova
ssafronova
changelog

[livejournal] r16113: LJSV-696: Add a Preview button to the FA...

Committer: ssafronova
LJSV-696: Add a Preview button to the FAQ Edit page
Based on several Dreamwidth patches, attached to task. Simplified.
htdocs/admin/faq/faqedit_do.bml unused now.
U   trunk/htdocs/admin/faq/faqedit.bml
Modified: trunk/htdocs/admin/faq/faqedit.bml
===================================================================
--- trunk/htdocs/admin/faq/faqedit.bml	2010-01-20 10:30:56 UTC (rev 16112)
+++ trunk/htdocs/admin/faq/faqedit.bml	2010-01-20 12:29:51 UTC (rev 16113)
@@ -9,7 +9,6 @@
 </CENTER>
 
 
-<FORM ACTION="faqedit_do.bml" METHOD=POST>
 <?_code
 
  $id = $FORM{'id'} + 0;
@@ -21,10 +20,14 @@
  my %ac_edit;
  my %ac_add;
  LJ::remote_has_priv($remote, "faqadd", \%ac_add);
+ LJ::remote_has_priv($remote, "faqedit", \%ac_edit);
+ my $faqd = LJ::Lang::get_dom("faq");
+ my $rlang = LJ::Lang::get_root_lang($faqd);
+ my ($question, $summary, $answer, $faqcat, $sortorder);
+ my ($sev_question, $sev_summary, $sev_answer);
 
  if ($id)
  {
-     LJ::remote_has_priv($remote, "faqedit", \%ac_edit);
      my $sth = $dbh->prepare("SELECT question, summary, answer, faqcat, sortorder FROM faq WHERE faqid=$id");
      $sth->execute;
      ($question, $summary, $answer, $faqcat, $sortorder) = $sth->fetchrow_array or
@@ -40,7 +43,7 @@
  }
  else
  {
-     unless (%ac_add) {
+     unless ($ac_add{'*'} || $ac_add{$FORM{'faqcat'}}) {
          return "<B>Error: </B> You do not have access to add to the FAQ.";
      }
  }
@@ -49,6 +52,144 @@
  $sortorder += 0;
  $sortorder ||= 50;
 
+ if ( LJ::did_post() ) {
+     $question = $FORM{q};
+     $summary = $FORM{s};
+     $answer = $FORM{a};
+     $faqcat = $FORM{faqcat};
+     $sortorder = $FORM{sortorder} + 0 || 50;
+     $sev_question = $FORM{sev_question} + 0;
+     $sev_summary = $FORM{sev_summary} + 0;
+     $sev_answer = $FORM{sev_answer} + 0;
+
+     if ( $POST{'action:save'} ) { # Save FAQ
+         return "<b>$ML{'Error'}</b> $ML{'error.invalidform'}" unless LJ::check_form_auth();
+
+         unless ($rlang) { undef $faqd; }
+         my $opts_question = { changeseverity => $sev_question };
+         my $opts_summary = { changeseverity => $sev_summary };
+         my $opts_answer = { changeseverity => $sev_answer };
+         my $do_trans = sub {
+             my $id = shift;
+             return unless $faqd;
+             LJ::Lang::set_text($dbh, $faqd->{'dmid'}, $rlang->{'lncode'},
+                                "$id.1question", $question, $opts_question);
+             LJ::Lang::set_text($dbh, $faqd->{'dmid'}, $rlang->{'lncode'},
+                                "$id.3summary", $summary, $opts_summary);
+             LJ::Lang::set_text($dbh, $faqd->{'dmid'}, $rlang->{'lncode'},
+                                "$id.2answer", $answer, $opts_answer);
+         };
+
+         if ($id == 0) {
+             unless ($FORM{'faqcat'})
+             {
+                 return "<B>Error: </B> You did not select a FAQ category.";
+             }
+             $dbh->do("INSERT INTO faq (faqid, question, summary, answer, faqcat, sortorder, lastmoduserid, lastmodtime) "
+                      . "VALUES (NULL, ?, ?, ?, ?, ?, ?, NOW())",
+                      undef, $question, $summary, $answer, $faqcat, $sortorder, $remote->{userid});
+             $id = $dbh->{'mysql_insertid'};
+             $ret .= $dbh->errstr || "Added FAQ item.  All good.";
+
+             $opts_question->{'childrenlatest'} = 1;
+             $opts_summary->{'childrenlatest'} = 1;
+             $opts_answer->{'childrenlatest'} = 1;
+             $do_trans->($id) if $id;
+         } elsif ($question =~ /\S/) {
+             $dbh->do("UPDATE faq SET question=?, summary=?, answer=?, faqcat=?, lastmoduserid=?, lastmodtime=NOW(), sortorder=? WHERE faqid=?",
+                      undef, $question, $summary, $answer, $faqcat, $remote->{userid}, $sortorder, $id);
+             $ret .= "Updated FAQ item.  All good.  faqid is <b><a href='$LJ::SITEROOT/support/faqbrowse.bml?faqid=$id'>$id</a></b>";
+
+             $do_trans->($id);
+         } else {
+             $dbh->do("DELETE FROM faq WHERE faqid=$id");
+             $ret .= "FAQ item deleted.";
+
+             # TODO: delete translation from ml_* ?
+         }
+
+         return $ret;
+     }
+
+    if ( $POST{'action:preview'} ) { # Preview FAQ
+        # FIXME: make lastmodtime look more like in LJ::Faq->load
+        my $fake_faq
+            = LJ::Faq->new( faqid => $id, question => $question,
+                            summary => $summary, answer => $answer,
+                            faqcat => $faqcat, lang => $rlang->{lncode},
+                            lastmoduserid => $remote->{userid},
+                            sortorder => $sortorder, unixmodtime => time,
+                            lastmodtime => scalar gmtime );
+
+        # Get remote username and journal URL, or example user's username
+        # and journal URL
+        my ( $user, $user_url );
+        if ( $remote ) {
+            $user = $remote->user;
+            $user_url = $remote->journal_base;
+        } else {
+            my $u = LJ::load_user( $LJ::EXAMPLE_USER_ACCOUNT );
+            $user = $u
+                ? $u->user
+                : "<b>[Unknown or undefined example username]</b>";
+            $user_url = $u
+                ? $u->journal_base
+                : "<b>[Unknown or undefined example username]</b>";
+        }
+
+        $fake_faq->render_in_place( { user => $user, url => $user_url } );
+        # Always display answer. (Except if hook says not to - see below)
+        my $display_answer = 1;
+        # Display summary if enabled and present.
+        my $display_summary = $fake_faq->has_summary;
+
+        # escape question
+        # FIXME: do we still need to check for \n? Did we ever?
+        my $q_html
+            = LJ::html_newlines( LJ::trim( $fake_faq->question_html ) );
+
+        # Clean this as if it were an entry, but don't allow lj-cuts
+        my $s_html = $fake_faq->summary_html;
+        LJ::CleanHTML::clean_event( \$s_html, { ljcut_disable => 1 } )
+            if $display_summary;
+        my $a_html = $fake_faq->answer_raw;
+        LJ::CleanHTML::clean_event( \$a_html, { ljcut_disable => 1 } )
+            if $display_answer;
+
+        # run hook to transform the text of this FAQ before it's rendered
+        # FIXME: hook is responsible for clean-up if it changes $display_*
+        # from false to true. Should that be checked/enforced here instead?
+        # FIXME: do we even need that hook? It looks like LJ only ever used
+        # it to add stuff to LJ Talk FAQs, for Gizmo sponsorship.
+        LJ::run_hook( "faq.$id.transform", $remote, answer => \$a_html,
+                      question => \$q_html, summary => \$s_html,
+                      display_summary => \$display_summary,
+                      display_answer => \$display_answer )
+            if LJ::are_hooks( "faq.$id.transform" );
+
+        # display output
+        $ret .= "<hr /><?h1 $q_html h1?>";
+        $ret .= "<div style='margin-left: 20px;'>";
+        $ret .= "<div name='summary' id='summary'>$s_html</div><br />"
+            if $display_summary;
+
+        $ret .= "<div id='answer' name='answer'>$a_html</div>"
+            if $display_answer;
+
+        $ret .= "</div>";
+
+        # FIXME: lastmodtime and lastmodwho should be vars in .lastupdated
+        # Also, using the string in htdocs/support/faqbrowse.bml.text for
+        # now, but this may change (either duplicate it here, or remove it
+        # altogether, since $remote presumably knows who they are and what
+        # time it is).
+        $ret .= "<p align='right'><b>$ML{'/support/faqbrowse.bml.lastupdated'}</b><br />"
+            . $fake_faq->lastmodtime . " (" . $remote->user . ")</p><hr />";
+    }
+    # Fall through to form
+ }
+
+ $ret .= "<form action='faqedit.bml' method='post'>";
  $ret .= LJ::form_auth();
  $ret .= LJ::html_hidden('id', $id);
  $ret .= "<p>Category: ";
@@ -147,7 +288,9 @@
  }
 
 
- $ret .= "<p>" . LJ::html_submit('', 'Add/Edit FAQ Item') . "</p>";
+ $ret .= "<p>" . LJ::html_submit('action:save', 'Add/Edit FAQ Item');
+ $ret .= " " . LJ::html_submit( 'action:preview', 'Preview FAQ Item')
+         . "</p>";
  return $ret;
 
  _code?>

Tags: bml, livejournal, ssafronova
Subscribe

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments