Index: branches/release-35/lib/MT/CMS/Blog.pm
===================================================================
--- branches/release-35/lib/MT/CMS/Blog.pm (revision 1978)
+++ branches/release-35/lib/MT/CMS/Blog.pm (revision 1980)
@@ -232,6 +232,12 @@
                 }
             }
-            require MT::Template;
-            $param->{dynamic_enabled} = MT::Template->exist( { blog_id => $obj->id, build_dynamic => 1 });
+            require MT::PublishOption;
+            if ( $app->model('template')->exist(
+                    { blog_id => $blog->id, build_type => MT::PublishOption::DYNAMIC() })
+              || $app->model('templatemap')->exist(
+                    { blog_id => $blog->id, build_type => MT::PublishOption::DYNAMIC() }) )
+            {
+                $param->{dynamic_enabled} = 1;
+            }
             eval "require List::Util; require Scalar::Util;";
             unless ($@) {
@@ -446,5 +452,4 @@
     $param{no_cachedir}         = $q->param('no_cachedir');
     $param{no_writecache}       = $q->param('no_writecache');
-    $param{dynamicity}          = $blog->custom_dynamic_templates || 'none';
     $param{include_system}      = $blog->include_system || '';
 
@@ -798,5 +803,7 @@
             dynamic         => $dynamic,
             is_new          => scalar $q->param('is_new'),
-            old_status      => scalar $q->param('old_status')
+            old_status      => scalar $q->param('old_status'),
+            is_full_screen  => scalar $q->param('fs'),
+            return_args     => scalar $q->param('return_args')
         );
         $app->load_tmpl( 'rebuilding.tmpl', \%param );
@@ -846,19 +853,24 @@
             }
             if ( $q->param('fs') ) {    # full screen--go to a useful app page
-                my $type = $q->param('type');
-                $type =~ /index-(\d+)/;
-                my $tmpl_id = $1;
-                $app->run_callbacks( 'rebuild', $blog );
-                return $app->redirect(
-                    $app->uri(
-                        'mode' => 'view',
-                        args   => {
-                            '_type'       => 'template',
-                            id            => $tmpl_id,
-                            blog_id       => $blog->id,
-                            saved_rebuild => 1
-                        }
-                    )
-                );
+                if ( my $return_args = $q->param('return_args') ) {
+                    $app->call_return;
+                }
+                else {
+                    my $type = $q->param('type');
+                    $type =~ /index-(\d+)/;
+                    my $tmpl_id = $1;
+                    $app->run_callbacks( 'rebuild', $blog );
+                    return $app->redirect(
+                        $app->uri(
+                            'mode' => 'view',
+                            args   => {
+                                '_type'       => 'template',
+                                id            => $tmpl_id,
+                                blog_id       => $blog->id,
+                                saved_rebuild => 1
+                            }
+                        )
+                    );
+                }
             }
             else {    # popup--just go to cnfrmn. page
@@ -941,5 +953,6 @@
         complete        => 0,
         incomplete      => 100,
-        build_type_name => $archive_label
+        build_type_name => $archive_label,
+        return_args     => $app->return_args
     );
 
@@ -972,10 +985,7 @@
 }
 
-sub rebuild_confirm {
-    my $app     = shift;
-    my $blog_id = $app->param('blog_id');
-    require MT::Blog;
-    my $blog = MT::Blog->load($blog_id)
-        or return $app->error($app->translate('Can\'t load blog #[_1].', $blog_id));
+sub _create_build_order {
+    my ( $app, $blog, $param ) = @_;
+
     my $at = $blog->archive_type || '';
     my ( @blog_at, @at, @data );
@@ -1004,11 +1014,21 @@
         }
     }
-    my $order     = join ',', @at, 'index';
-    my $entry_pkg = $app->model('entry');
+    $param->{archive_type_loop} = \@data;
+    $param->{build_order} = join ',', @at, 'index';
+    1;
+}
+
+sub rebuild_confirm {
+    my $app     = shift;
+    my $blog_id = $app->param('blog_id');
+    require MT::Blog;
+    my $blog = MT::Blog->load($blog_id)
+        or return $app->error($app->translate('Can\'t load blog #[_1].', $blog_id));
+
     my %param     = (
-        archive_type_loop => \@data,
-        build_order       => $order,
         build_next        => 0,
     );
+    _create_build_order( $app, $blog, \%param );
+
     $param{index_selected} = ( $app->param('prompt') || "" ) eq 'index';
 
@@ -1480,10 +1500,4 @@
     }
     if ( $screen eq 'cfg_archives' ) {
-        # update the dynamic publishing options if they changed
-        update_dynamicity(
-            $app,
-            $obj
-        );
-
         # If either of the publishing paths changed, rebuild the fileinfos.
         my $path_changed = 0;
@@ -1843,4 +1857,22 @@
     $blog->archive_type_preferred($at);
     $blog->include_cache( $app->param('include_cache') ? 1 : 0 );
+    require MT::PublishOption;
+    if ( ( $blog->custom_dynamic_templates eq 'all'
+        || $blog->custom_dynamic_templates eq 'archives' )
+      && ( $app->model('template')->exist(
+            { blog_id => $blog->id, build_type => MT::PublishOption::DYNAMIC() })
+        || $app->model('templatemap')->exist(
+            { blog_id => $blog->id, build_type => MT::PublishOption::DYNAMIC() }) ) )
+    {
+        # dynamic enabled and caching option may have changed - update mtview
+        my $cache       = $app->param('dynamic_cache')       ? 1 : 0;
+        my $conditional = $app->param('dynamic_conditional') ? 1 : 0;
+        _create_mtview( $blog, $blog->site_path, $cache, $conditional );
+        _create_dynamiccache_dir( $blog, $blog->site_path ) if $cache;
+        if ( $blog->archive_path ) {
+            _create_mtview( $blog, $blog->archive_path, $cache, $conditional );
+            _create_dynamiccache_dir( $blog, $blog->archive_path ) if $cache;
+        }
+    }
     $blog->save
       or return $app->error(
@@ -1857,4 +1889,11 @@
     my $pq = $dcty =~ m/^async/ ? 1 : 0;
     $blog->publish_queue( $pq );
+    if ( $dcty eq 'all' || $dcty eq 'archives' ) {
+        # update the dynamic publishing options if they changed
+        update_dynamicity(
+            $app,
+            $blog
+        );
+    }
     $blog->save
       or return $app->error(
@@ -1983,10 +2022,11 @@
     my $conditional = $app->param('dynamic_conditional') ? 1 : 0;
 
-    require MT::Template;
-    my $dynamic_enabled = MT::Template->exist( { blog_id => $blog->id,
-        build_dynamic => 1 });
-
-    # dynamic publishing enabled
-    if ( $dynamic_enabled ) {
+    require MT::PublishOption;
+    if ( $app->model('template')->exist(
+            { blog_id => $blog->id, build_type => MT::PublishOption::DYNAMIC() })
+      || $app->model('templatemap')->exist(
+            { blog_id => $blog->id, build_type => MT::PublishOption::DYNAMIC() }) )
+    {
+        # dynamic publishing enabled
         prepare_dynamic_publishing($app, $blog, $cache, $conditional, $blog->site_path, $blog->site_url);
         if ( $blog->archive_path ) {
@@ -2016,4 +2056,85 @@
             }
         }
+    }
+}
+
+sub _create_mtview {
+    my ( $blog, $site_path, $cache, $conditional ) = @_;
+
+    my $mtview_path = File::Spec->catfile( $site_path, "mtview.php" );
+    eval {
+        my $mv_contents = '';
+        if ( -f $mtview_path ) {
+            open( my $mv, "<$mtview_path" );
+            while ( my $line = <$mv> ) {
+                $mv_contents .= $line if ( $line !~ m!^//|<\?(?:php)?|\?>! );
+            }
+            close $mv;
+        }
+        my $cgi_path = MT->instance->server_path() || "";
+        $cgi_path =~ s!/*$!!;
+        my $mtphp_path = File::Spec->canonpath("$cgi_path/php/mt.php");
+        my $blog_id    = $blog->id;
+        my $config     = MT->instance->{cfg_file};
+        my $cache_code = $cache ? "\n    \$mt->caching = true;" : '';
+        my $conditional_code =
+          $conditional ? "\n    \$mt->conditional = true;" : '';
+        my $new_mtview = <<NEW_MTVIEW;
+
+    include('$mtphp_path');
+    \$mt = new MT($blog_id, '$config');$cache_code$conditional_code
+    \$mt->view();
+NEW_MTVIEW
+
+        if ( $new_mtview ne substr( $mv_contents, 0, length($new_mtview) ) ) {
+            $mv_contents =~ s!\n!\n//!gs;
+            my $mtview = <<MTVIEW;
+<?php
+$new_mtview
+$mv_contents
+?>
+MTVIEW
+
+            $blog->file_mgr->mkpath( $site_path );
+            open( my $mv, ">$mtview_path" )
+              || die "Couldn't open $mtview_path for appending";
+            print $mv $mtview || die "Couldn't write to $mtview_path";
+            close $mv;
+        }
+    };
+    if ($@) { print STDERR $@; }
+}
+
+sub _create_dynamiccache_dir {
+    my ( $blog, $site_path ) = @_;
+
+    # FIXME: use FileMgr
+    my $cache_path = File::Spec->catfile( $site_path, 'cache' );
+    my $fmgr = $blog->file_mgr;
+    my $saved_umask = MT->config->DirUmask;
+    MT->config->DirUmask('0000');
+    $fmgr->mkpath($cache_path);
+    MT->config->DirUmask($saved_umask);
+    my $message;
+    if ( -d $cache_path ) {
+        $message = MT->translate(
+'ErrMovable Type cannot write to the template cache directory. Please check the permissions for the directory called <code>[_1]</code> underneath your blog directory.',
+            'cache'
+        ) unless ( -w $cache_path );
+    }
+    else {
+        $message = MT->translate(
+'ErrMovable Type was not able to create a directory to cache your dynamic templates. You should create a directory called <code>[_1]</code> underneath your blog directory.',
+            'cache'
+        ) unless ( -d $cache_path );
+    }
+    if ( $message ) {
+        MT->log(
+            {
+                message => $message,
+                level   => MT::Log::ERROR(),
+                class   => 'system',
+            }
+        );
     }
 }
@@ -2032,5 +2153,7 @@
       && ( -f $htaccess_path )
       && ( -f $mtview_path );
-    return 1 if ( 'none' eq $blog->custom_dynamic_templates );
+    return 1 
+        if 'all' ne $blog->custom_dynamic_templates
+        && 'archives' ne $blog->custom_dynamic_templates;
 
     # IIS itself does not handle .htaccess,
@@ -2107,45 +2230,5 @@
     }
 
-    eval {
-        my $mv_contents = '';
-        if ( -f $mtview_path ) {
-            open( my $mv, "<$mtview_path" );
-            while ( my $line = <$mv> ) {
-                $mv_contents .= $line if ( $line !~ m!^//|<\?(?:php)?|\?>! );
-            }
-            close $mv;
-        }
-        my $cgi_path = MT->instance->server_path() || "";
-        $cgi_path =~ s!/*$!!;
-        my $mtphp_path = File::Spec->canonpath("$cgi_path/php/mt.php");
-        my $blog_id    = $blog->id;
-        my $config     = MT->instance->{cfg_file};
-        my $cache_code = $cache ? "\n    \$mt->caching = true;" : '';
-        my $conditional_code =
-          $conditional ? "\n    \$mt->conditional = true;" : '';
-        my $new_mtview = <<NEW_MTVIEW;
-
-    include('$mtphp_path');
-    \$mt = new MT($blog_id, '$config');$cache_code$conditional_code
-    \$mt->view();
-NEW_MTVIEW
-
-        if ( $new_mtview ne substr( $mv_contents, 0, length($new_mtview) ) ) {
-            $mv_contents =~ s!\n!\n//!gs;
-            my $mtview = <<MTVIEW;
-<?php
-$new_mtview
-$mv_contents
-?>
-MTVIEW
-
-            $blog->file_mgr->mkpath( $site_path );
-            open( my $mv, ">$mtview_path" )
-              || die "Couldn't open $mtview_path for appending";
-            print $mv $mtview || die "Couldn't write to $mtview_path";
-            close $mv;
-        }
-    };
-    if ($@) { print STDERR $@; }
+    _create_mtview( $blog, $site_path, $cache, $conditional );
 
     my $compiled_template_path =
@@ -2172,30 +2255,7 @@
     }
 
-    # FIXME: use FileMgr
     if ($cache) {
-        my $cache_path = File::Spec->catfile( $blog->site_path(), 'cache' );
-        $cfg->DirUmask('0000');
-        $fmgr->mkpath($cache_path);
-        $cfg->DirUmask($saved_umask);
-        if ( -d $cache_path ) {
-            $message = MT->translate(
-'Error: Movable Type cannot write to the template cache directory. Please check the permissions for the directory called <code>[_1]</code> underneath your blog directory.',
-                'cache'
-            ) unless ( -w $cache_path );
-        }
-        else {
-            $message = MT->translate(
-'Error: Movable Type was not able to create a directory to cache your dynamic templates. You should create a directory called <code>[_1]</code> underneath your blog directory.',
-                'cache'
-            ) unless ( -d $cache_path );
-        }
-    }
-    MT->log(
-        {
-            message => $message,
-            level   => MT::Log::ERROR(),
-            class   => 'system',
-        }
-    );
+        _create_dynamiccache_dir( $blog, $site_path );
+    }
 }
 
Index: branches/release-35/lib/MT/CMS/Common.pm
===================================================================
--- branches/release-35/lib/MT/CMS/Common.pm (revision 1823)
+++ branches/release-35/lib/MT/CMS/Common.pm (revision 1980)
@@ -329,4 +329,21 @@
     }
     elsif ( my $cfg_screen = $q->param('cfg_screen') ) {
+        if ( $cfg_screen eq 'cfg_publish_profile' ) {
+            my $dcty = $obj->custom_dynamic_templates || 'none';
+            if ( ( $dcty eq 'all' ) || ( $dcty eq 'archives' ) ) {
+                require MT::CMS::Blog;
+                my %param = ();
+                MT::CMS::Blog::_create_build_order( $app, $obj, \%param );
+                $q->param( 'single_template', 1 ); # to show tmpl full-screen
+                if ( $dcty eq 'all' ) {
+                    $q->param( 'type', $param{build_order} );
+                }
+                elsif ( $dcty eq 'archives' ) {
+                    my @ats = map { $_->{archive_type} } @{ $param{archive_type_loop} };
+                    $q->param( 'type', join( ',', @ats ) );
+                }
+                return MT::CMS::Blog::start_rebuild_pages($app);
+            }
+        }
         if ( $cfg_screen eq 'cfg_templatemaps' ) {
             $cfg_screen = 'cfg_archives';
