Changeset 2859

Show
Ignore:
Timestamp:
07/29/08 20:50:43 (4 months ago)
Author:
arvind
Message:

Abstracted code that generates entry live preview into two routines:
* _create_temp_entry creates a temporary MT::Entry from posted values
* _build_entry_preview builds the actual live preview file

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/feature-revision-histories/lib/MT/CMS/Entry.pm

    r2737 r2859  
    730730 
    731731sub preview { 
    732     my $app         = shift; 
     732    my $app = shift; 
     733    my $entry = _create_temp_entry($app); 
     734     
     735    return _build_entry_preview($app, $entry); 
     736
     737 
     738sub _create_temp_entry { 
     739    my $app = shift; 
     740    my $type        = $app->param('_type') || 'entry'; 
     741    my $entry_class = $app->model($type); 
     742    my $blog_id     = $app->param('blog_id'); 
     743    my $blog        = $app->blog; 
     744    my $id          = $app->param('id'); 
     745    my $entry; 
     746    my $user_id = $app->user->id; 
     747 
     748    if ($id) { 
     749        $entry = $entry_class->load( { id => $id, blog_id => $blog_id } ) 
     750            or return $app->errtrans( "Invalid request." ); 
     751        $user_id = $entry->author_id; 
     752    } 
     753    else { 
     754        $entry = $entry_class->new; 
     755        $entry->author_id($user_id); 
     756        $entry->id(-1); # fake out things like MT::Taggable::__load_tags 
     757        $entry->blog_id($blog_id); 
     758    } 
     759 
     760    my $names = $entry->column_names; 
     761    my %values = map { $_ => scalar $app->param($_) } @$names; 
     762    delete $values{'id'} unless $app->param('id'); 
     763    ## Strip linefeed characters. 
     764    for my $col (qw( text excerpt text_more keywords )) { 
     765        $values{$col} =~ tr/\r//d if $values{$col}; 
     766    } 
     767    $values{allow_comments} = 0 
     768      if !defined( $values{allow_comments} ) 
     769      || $app->param('allow_comments') eq ''; 
     770    $values{allow_pings} = 0 
     771      if !defined( $values{allow_pings} ) 
     772      || $app->param('allow_pings') eq ''; 
     773    $entry->set_values( \%values ); 
     774     
     775    return $entry; 
     776
     777 
     778sub _build_entry_preview { 
     779    my $app = shift; 
     780    my ($entry, %param) = @_; 
    733781    my $q           = $app->param; 
    734782    my $type        = $q->param('_type') || 'entry'; 
     
    737785    my $blog        = $app->blog; 
    738786    my $id          = $q->param('id'); 
    739     my $entry; 
    740787    my $user_id = $app->user->id; 
    741  
    742     if ($id) { 
    743         $entry = $entry_class->load( { id => $id, blog_id => $blog_id } ) 
    744             or return $app->errtrans( "Invalid request." ); 
    745         $user_id = $entry->author_id; 
    746     } 
    747     else { 
    748         $entry = $entry_class->new; 
    749         $entry->author_id($user_id); 
    750         $entry->id(-1); # fake out things like MT::Taggable::__load_tags 
    751         $entry->blog_id($blog_id); 
    752     } 
    753788    my $cat; 
    754     my $names = $entry->column_names; 
    755  
    756     my %values = map { $_ => scalar $app->param($_) } @$names; 
    757     delete $values{'id'} unless $q->param('id'); 
    758     ## Strip linefeed characters. 
    759     for my $col (qw( text excerpt text_more keywords )) { 
    760         $values{$col} =~ tr/\r//d if $values{$col}; 
    761     } 
    762     $values{allow_comments} = 0 
    763       if !defined( $values{allow_comments} ) 
    764       || $q->param('allow_comments') eq ''; 
    765     $values{allow_pings} = 0 
    766       if !defined( $values{allow_pings} ) 
    767       || $q->param('allow_pings') eq ''; 
    768     $entry->set_values( \%values ); 
    769  
    770789    my $cat_ids = $q->param('category_ids'); 
    771790    if ($cat_ids) { 
     
    861880    $ctx->var('preview_template',  1); 
    862881    my $html = $tmpl->output; 
    863     my %param; 
    864882    unless ( defined($html) ) { 
    865883        my $preview_error = $app->translate( "Publish error: [_1]", 
     
    964982          || $col eq 'meta' 
    965983          || $col eq 'comment_count' 
    966           || $col eq 'ping_count'; 
     984          || $col eq 'ping_count' 
     985          || $col eq 'current_revision'; 
    967986        if ( $col eq 'basename' ) { 
    968987            if (   ( !defined $q->param('basename') ) 
     
    10201039    $param{object_type}  = $type; 
    10211040    $param{object_label} = $entry_class->class_label; 
     1041     
     1042    $param{diff_view} = $app->param('rev_numbers') || $app->param('collision'); 
     1043    $param{collision} = 1; 
     1044    if(my @rev_numbers = split /,/, $app->param('rev_numbers')) { 
     1045        $param{comparing_revisions} = 1; 
     1046        $param{rev_a} = $rev_numbers[0]; 
     1047        $param{rev_b} = $rev_numbers[1]; 
     1048    } 
     1049     
    10221050    if ($fullscreen) { 
    10231051        return $app->load_tmpl( 'preview_entry.tmpl', \%param ); 
     
    12641292    $app->_translate_naughty_words($obj); 
    12651293 
    1266     $obj->modified_by( $author->id ) unless $is_new; 
    1267  
    12681294    $app->run_callbacks( 'cms_pre_save.' . $type, $app, $obj, $orig_obj ) 
    12691295      || return $app->error( 
     
    12731299        ) 
    12741300      ); 
     1301       
     1302    # Setting modified_by updates modified_on which we want to do before 
     1303    # a save but after pre_save callbacks fire.   
     1304    $obj->modified_by( $author->id ) unless $is_new; 
    12751305 
    12761306    $obj->save