Index: branches/release-34/lib/MT/CMS/Template.pm
===================================================================
--- branches/release-34/lib/MT/CMS/Template.pm (revision 1875)
+++ branches/release-34/lib/MT/CMS/Template.pm (revision 1877)
@@ -1,2 +1,8 @@
+# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
+# This program is distributed under the terms of the
+# GNU General Public License, version 2.
+#
+# $Id$
+
 package MT::CMS::Template;
 
@@ -76,8 +82,4 @@
           (      ( $obj->type eq 'index' )
               && ( ( $blog->custom_dynamic_templates || "" ) ne 'all' ) );
-        $param->{custom_dynamic} =
-          $blog && ( $blog->custom_dynamic_templates || "" ) eq 'custom';
-        $param->{has_build_options} =
-          ( $param->{custom_dynamic} || $param->{has_rebuild} );
 
         # FIXME: enumeration of types
@@ -243,5 +245,5 @@
         }
         # publish options
-        $param->{publish_queue} = $blog->publish_queue if $blog;
+        $param->{publish_queue_available} = eval 'require List::Util; require Scalar::Util; 1;';
         $param->{build_type} = $obj->build_type;
         $param->{ 'build_type_' . ( $obj->build_type || 0 ) } = 1;
@@ -563,4 +565,6 @@
     $params->{published} = $app->param('published');
     $params->{saved_copied} = $app->param('saved_copied');
+    $params->{saved_deleted} = $app->param('saved_deleted');
+    $params->{saved} = $app->param('saved');
 
     # determine list of system template types:
@@ -652,4 +656,7 @@
             $app->param( 'filter_key', 'email_templates' );
         }
+        elsif ( $tmpl_type eq 'module' ) {
+            $app->param( 'filter_key', 'module_templates' );
+        }
         $terms->{type} = $types{$tmpl_type}->{type};
         my $tmpl_param = $app->listing(
@@ -1062,5 +1069,5 @@
                 name    => $name,
                 value   => $_->{template},
-                default => ( $_->{default} || 0 )
+                default => ( $_->{default} || 0 ),
               };
         }
@@ -1183,11 +1190,4 @@
     my ( $app, $obj ) = @_;
 
-    $obj->rebuild_me(0) 
-      if $app->param('current_rebuild_me')
-      && !$app->param('rebuild_me');
-    $obj->build_dynamic(0)
-      if $app->param('current_build_dynamic')
-      && !$app->param('build_dynamic');
-
     ## Strip linefeed characters.
     ( my $text = $obj->text ) =~ tr/\r//d;
@@ -1429,5 +1429,8 @@
 
     my $param = {};
+    my $blog = $app->blog;
+    $param->{dynamicity} = $blog->custom_dynamic_templates || 'none';
     $param->{screen_id} = "publishing-profile-dialog";
+    $param->{return_args} = $app->param('return_args');
 
     $app->build_page('dialog/publishing_profile.tmpl',
Index: branches/release-34/lib/MT/CMS/Blog.pm
===================================================================
--- branches/release-34/lib/MT/CMS/Blog.pm (revision 1866)
+++ branches/release-34/lib/MT/CMS/Blog.pm (revision 1877)
@@ -13,5 +13,4 @@
 
     if ($id) {
-        require MT::IPBanList;
         my $output = $param->{output} ||= 'cfg_prefs.tmpl';
         $param->{need_full_rebuild}  = 1 if $q->param('need_full_rebuild');
@@ -233,4 +232,6 @@
                 }
             }
+            require MT::Template;
+            $param->{dynamic_enabled} = MT::Template->exist( { blog_id => $obj->id, build_dynamic => 1 });
             eval "require List::Util; require Scalar::Util;";
             unless ($@) {
@@ -1201,5 +1202,4 @@
         }
         elsif ( $screen eq 'cfg_archives' ) {
-            @fields = qw( file_extension );
         }
         elsif ( $screen eq 'cfg_templatemaps' ) {
@@ -1385,11 +1385,13 @@
         }
         if ( $screen eq 'cfg_archives' ) {
-            if ( my $dcty = $app->param('dynamicity') ) {
-                $obj->custom_dynamic_templates($dcty);
-            }
             $obj->include_system( $app->param('include_system') || '' );
             if ( !$app->param('enable_archive_paths') ) {
                 $obj->archive_url('');
                 $obj->archive_path('');
+            }
+        }
+        if ( $screen eq 'cfg_publish_profile' ) {
+            if ( my $dcty = $app->param('dynamicity') ) {
+                $obj->custom_dynamic_templates($dcty);
             }
         }
@@ -1446,42 +1448,21 @@
 
     my $screen = $app->param('cfg_screen') || '';
-    if ( $screen eq 'cfg_archives' ) {
+    if ( $screen eq 'cfg_publish_profile' ) {
         if ( my $dcty = $app->param('dynamicity') ) {
-            my $dcty_changed = $dcty ne $original->custom_dynamic_templates ? 1 : 0;
-
-            if ($dcty_changed) {
-
-                # Apply publishing rules for templates based on
-                # publishing method selected:
-                #     none (0% publish queue, all static)
-                #     async_all (100% publish queue)
-                #     async_partial (high-priority templates publish synchronously (main index, preferred indiv. archives, feed templates))
-                #     all (100% dynamic)
-                #     archives (archives dynamic, static indexes)
-                #     custom (custom configuration)
-
-                if ( $dcty eq 'none' ) {
-                    require MT::Template;
-                    my @tmpls = MT::Template->load({
-                        blog_id       => $obj->id,
-                        build_dynamic => 1,
-                    });
-                    for my $tmpl (@tmpls) {
-                        $tmpl->build_dynamic(0);
-                        $tmpl->save;
-                    }
-                }
-                update_dynamicity(
-                    $app,
-                    $obj,
-                    $app->param('dynamic_cache')       ? 1 : 0,
-                    $app->param('dynamic_conditional') ? 1 : 0
-                );
-            }
-
-            if (!$dcty_changed || $dcty eq 'custom') {
-                # do nothing
-            }
-            elsif ($dcty eq 'none') {
+            # Apply publishing rules for templates based on
+            # publishing method selected:
+            #     none (0% publish queue, all static)
+            #     async_all (100% publish queue)
+            #     async_partial (high-priority templates publish synchronously (main index, preferred indiv. archives, feed templates))
+            #     all (100% dynamic)
+            #     archives (archives dynamic, static indexes)
+            #     custom (custom configuration)
+
+            update_publishing_profile(
+                $app,
+                $obj
+            );
+
+            if (($dcty eq 'none') || ($dcty =~ m/^async/)) {
                 _update_finfos($app, 0);
             }
@@ -1494,16 +1475,25 @@
                 _update_finfos($app, 0, { templatemap_id => \'is null' });
             }
-
-            # If either of the publishing paths changed, rebuild the fileinfos.
-            my $path_changed = 0;
-            for my $path_field (qw( site_path archive_path site_url archive_url )) {
-                $path_changed = 1 && last if $app->param($path_field)
-                    && $app->param($path_field) ne $original->$path_field();
-            }
-
-            if ($path_changed) {
-                $app->rebuild( BlogID => $obj->id, NoStatic => 1 )
-                    or return $app->publish_error();
-            }
+        }
+
+        cfg_publish_profile_save($app, $obj) or return;
+    }
+    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;
+        for my $path_field (qw( site_path archive_path site_url archive_url )) {
+            $path_changed = 1 && last if $app->param($path_field)
+                && $app->param($path_field) ne $original->$path_field();
+        }
+
+        if ($path_changed) {
+            $app->rebuild( BlogID => $obj->id, NoStatic => 1 )
+                or return $app->publish_error();
         }
 
@@ -1847,18 +1837,29 @@
 
 sub _switch_publish_options {
-    my ( $blog, $current, $new ) = @_;
+    my ( $blog, $current, $new, $dcty ) = @_;
     require MT::Template;
     require MT::TemplateMap;
     my @tmpl = MT::Template->load( { blog_id => $blog->id } );
+    my $pref_at = $blog->archive_type_preferred || '';
     for my $tmpl (@tmpl) {
+        # FIXME: enumeration of types
         next
-          if !( $tmpl->type =~ m/^(individual|page|category|archive|index)$/ );
-        if ( $tmpl->build_type == $current ) {
-            $tmpl->build_type($new);
-            $tmpl->save;
-        }
+          if !( $tmpl->type =~ m/^(individual|page|category|archive)$/ );
+        # if ( $tmpl->build_type == $current ) {
+        #     $tmpl->build_type($new);
+        #     $tmpl->save;
+        # }
         my @tmpl_maps = MT::TemplateMap->load( { template_id => $tmpl->id } );
         foreach my $tmpl_map (@tmpl_maps) {
-            if ( $tmpl_map->build_type == $current ) {
+            if ($dcty eq 'async_partial') {
+                # build the preferred archive type synchronously
+                if (($tmpl_map->archive_type eq $pref_at) &&
+                    ($tmpl_map->is_preferred)) {
+                    $tmpl_map->build_type(MT::PublishOption::ONDEMAND());
+                    $tmpl_map->save;
+                    next;
+                }
+            }
+            if ( $tmpl_map->build_type != $new ) {
                 $tmpl_map->build_type($new);
                 $tmpl_map->save;
@@ -1875,7 +1876,19 @@
     my $at = $app->param('preferred_archive_type');
     $blog->archive_type_preferred($at);
-    my $pq = $app->param('publish_queue');
-    $blog->publish_queue( $pq ? 1 : 0 );
     $blog->include_cache( $app->param('include_cache') ? 1 : 0 );
+    $blog->save
+      or return $app->error(
+        $app->translate( "Saving blog failed: [_1]", $blog->errstr ) );
+
+    1;
+}
+
+sub cfg_publish_profile_save {
+    my $app = shift;
+    my ($blog) = @_;
+
+    my $dcty = $app->param('dynamicity') || 'none';
+    my $pq = $dcty =~ m/^async/ ? 1 : 0;
+    $blog->publish_queue( $pq );
     $blog->save
       or return $app->error(
@@ -1887,5 +1900,6 @@
             $blog,
             MT::PublishOption::ONDEMAND(),
-            MT::PublishOption::ASYNC()
+            MT::PublishOption::ASYNC(),
+            $dcty,
         );
     }
@@ -1894,7 +1908,10 @@
             $blog,
             MT::PublishOption::ASYNC(),
-            MT::PublishOption::ONDEMAND()
+            MT::PublishOption::ONDEMAND(),
+            $dcty,
         );
     }
+
+    1;
 }
 
@@ -1905,26 +1922,48 @@
     my @tms = MT::TemplateMap->load(
         {
-            archive_type => $archive_type,
-            blog_id      => $blog_id
+            archive_type  => $archive_type,
+            blog_id       => $blog_id
         }
     );
-    grep { $_->text =~ /<MT:?IfRegistration/i }
+    grep { !$_->build_dynamic && ($_->text =~ /<MT:?IfRegistration/i) }
       map { MT::Template->load( $_->template_id ) } @tms;
 }
 
-sub update_dynamicity {
+sub update_publishing_profile {
     my $app = shift;
-    my ( $blog, $cache, $conditional ) = @_;
+    my ( $blog ) = @_;
+
     my $dcty = $blog->custom_dynamic_templates;
 
-    if ( ($dcty eq 'async_partial') || ($dcty eq 'async_all') ) {
-        # these behave like static publishing
-        $dcty = 'none';
-    }
-
-    if ( $dcty eq 'none' ) {
-        require MT::Template;
-        my @templates = MT::Template->load( { blog_id => $blog->id } );
+    require MT::PublishOption;
+    require MT::Template;
+
+    if ( ($dcty eq 'none') || ($dcty =~ m/^async/) ) {
+        my @templates = MT::Template->load( {
+            blog_id => $blog->id,
+            # FIXME: enumeration of types
+            type =>
+              [ 'index', 'archive', 'individual', 'page', 'category' ],
+          } );
         for my $tmpl (@templates) {
+            my $bt = $tmpl->build_type || 0;
+            # Do not make automatic modifications to templates with these
+            # manually configured build types
+            next if $bt == MT::PublishOption::DISABLED();
+            next if $bt == MT::PublishOption::MANUALLY();
+            next if $bt == MT::PublishOption::SCHEDULED();
+
+            if ($dcty eq 'async_partial') {
+                # these should be build synchronously
+                if (($tmpl->identifier || '') =~ m/^(main_index|feed_recent)$/) {
+                    $tmpl->build_type(MT::PublishOption::ONDEMAND());
+                } else {
+                    $tmpl->build_type(MT::PublishOption::ASYNC());
+                }
+            } elsif ($dcty eq 'async_all') {
+                $tmpl->build_type(MT::PublishOption::ASYNC());
+            } else {
+                $tmpl->build_type(MT::PublishOption::ONDEMAND());
+            }
             $tmpl->build_dynamic(0);
             $tmpl->save();
@@ -1932,15 +1971,17 @@
     }
     elsif ( $dcty eq 'archives' ) {
-        require MT::Template;
-        my @templates = MT::Template->load( { blog_id => $blog->id } );
+        my @templates = MT::Template->load( {
+            blog_id => $blog->id,
+            # FIXME: enumeration of types
+            type =>
+              [ 'index', 'archive', 'individual', 'page', 'category' ],
+          } );
         for my $tmpl (@templates) {
+            $tmpl->build_type( $tmpl->type ne 'index' ? MT::PublishOption::DYNAMIC() : MT::PublishOption::ONDEMAND() );
             $tmpl->build_dynamic( $tmpl->type ne 'index' ? 1 : 0 );
             $tmpl->save();
         }
     }
-    elsif ( $dcty eq 'custom' ) {
-    }
     elsif ( $dcty eq 'all' ) {
-        require MT::Template;
         my @templates = MT::Template->load(
             {
@@ -1953,13 +1994,28 @@
         );
         for my $tmpl (@templates) {
+            $tmpl->build_type( MT::PublishOption::DYNAMIC() );
             $tmpl->build_dynamic(1);
             $tmpl->save();
         }
     }
-
-    if ( $dcty ne 'none' ) {
-        prepare_dynamic_publishing($app, @_, $blog->site_path, $blog->site_url);
+    return 1;
+}
+
+sub update_dynamicity {
+    my $app = shift;
+    my ( $blog ) = @_;
+
+    my $cache       = $app->param('dynamic_cache')       ? 1 : 0;
+    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 ) {
+        prepare_dynamic_publishing($app, $blog, $cache, $conditional, $blog->site_path, $blog->site_url);
         if ( $blog->archive_path ) {
-            prepare_dynamic_publishing($app, @_, $blog->archive_path, $blog->archive_url);
+            prepare_dynamic_publishing($app, $blog, $cache, $conditional, $blog->archive_path, $blog->archive_url);
         }
         my $compiled_template_path =
@@ -1987,5 +2043,4 @@
         }
     }
-    $app->add_return_arg( dynamic_set => 1 );
 }
 
@@ -2004,6 +2059,4 @@
       && ( -f $mtview_path );
     return 1 if ( 'none' eq $blog->custom_dynamic_templates );
-
-    require File::Spec;
 
     # IIS itself does not handle .htaccess,
