Changeset 1385

Show
Ignore:
Timestamp:
02/21/08 19:46:48 (8 months ago)
Author:
bchoate
Message:

Restored missing 'refresh all templates' code.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/release-30/lib/MT/CMS/Template.pm

    r1374 r1385  
    928928} 
    929929 
     930sub refresh_all_templates { 
     931    my ($app) = @_; 
     932 
     933    my $backup = 0; 
     934    if ($app->param('backup')) { 
     935        # refresh templates dialog uses a 'backup' field 
     936        $backup = 1; 
     937    } 
     938 
     939    my $template_set = $app->param('template_set'); 
     940    my $refresh_type = $app->param('refresh_type') || 'refresh'; 
     941 
     942    my $t = time; 
     943 
     944    my @id; 
     945    if ($app->param('blog_id')) { 
     946        @id = ( scalar $app->param('blog_id') ); 
     947    } 
     948    else { 
     949        @id = $app->param('id'); 
     950        if (! @id) { 
     951            # refresh global templates 
     952            @id = ( 0 ); 
     953        } 
     954    } 
     955 
     956    require MT::Template; 
     957    require MT::DefaultTemplates; 
     958    require MT::Blog; 
     959    require MT::Permission; 
     960    require MT::Util; 
     961 
     962    foreach my $blog_id (@id) { 
     963        my $blog; 
     964        if ($blog_id) { 
     965            $blog = MT::Blog->load($blog_id); 
     966            next unless $blog; 
     967        } 
     968        if ( !$app->user->is_superuser() ) { 
     969            my $perms = MT::Permission->load( 
     970                { blog_id => $blog_id, author_id => $app->user->id } ); 
     971            if ( 
     972                !$perms 
     973                || (   !$perms->can_edit_templates() 
     974                    && !$perms->can_administer_blog() ) 
     975              ) 
     976            { 
     977                next; 
     978            } 
     979        } 
     980 
     981        my $tmpl_list; 
     982        if ($blog_id) { 
     983 
     984            if ($refresh_type eq 'clean') { 
     985                # the user wants to back up all templates and 
     986                # install the new ones 
     987 
     988                my @ts = MT::Util::offset_time_list( $t, $blog_id ); 
     989                my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d", 
     990                    $ts[5] + 1900, $ts[4] + 1, @ts[ 3, 2, 1, 0 ]; 
     991 
     992                my $tmpl_iter = MT::Template->load_iter({ 
     993                    blog_id => $blog_id, 
     994                    type => { not => 'backup' }, 
     995                }); 
     996 
     997                while (my $tmpl = $tmpl_iter->()) { 
     998                    if ($backup) { 
     999                        # zap all template maps 
     1000                        require MT::TemplateMap; 
     1001                        MT::TemplateMap->remove({ 
     1002                            template_id => $tmpl->id, 
     1003                        }); 
     1004                        $tmpl->type('backup'); 
     1005                        $tmpl->name( 
     1006                            $tmpl->name . ' (Backup from ' . $ts . ')' ); 
     1007                        $tmpl->identifier(undef); 
     1008                        $tmpl->rebuild_me(0); 
     1009                        $tmpl->linked_file(undef); 
     1010                        $tmpl->outfile(''); 
     1011                        $tmpl->save; 
     1012                    } else { 
     1013                        $tmpl->remove; 
     1014                    } 
     1015                } 
     1016 
     1017                # This also creates our template mappings 
     1018                $blog->create_default_templates( $template_set || 
     1019                    $blog->template_set || 'mt_blog' ); 
     1020 
     1021                if ($template_set) { 
     1022                    $blog->template_set( $template_set ); 
     1023                    $blog->save; 
     1024                    $app->run_callbacks( 'blog_template_set_change', { blog => $blog } ); 
     1025                } 
     1026 
     1027                next; 
     1028            } 
     1029 
     1030            $tmpl_list = MT::DefaultTemplates->templates($template_set || $blog->template_set) || MT::DefaultTemplates->templates(); 
     1031        } 
     1032        else { 
     1033            $tmpl_list = MT::DefaultTemplates->templates(); 
     1034        } 
     1035 
     1036        foreach my $val (@$tmpl_list) { 
     1037            if ($blog_id) { 
     1038                # when refreshing blog templates, 
     1039                # skip over global templates which 
     1040                # specify a blog_id of 0... 
     1041                next if $val->{global}; 
     1042            } 
     1043            else { 
     1044                next unless exists $val->{global}; 
     1045            } 
     1046 
     1047            if ( !$val->{orig_name} ) { 
     1048                $val->{orig_name} = $val->{name}; 
     1049                $val->{name}      = $app->translate( $val->{name} ); 
     1050                $val->{text}      = $app->translate_templatized( $val->{text} ); 
     1051            } 
     1052 
     1053            my $orig_name = $val->{orig_name}; 
     1054 
     1055            my @ts = MT::Util::offset_time_list( $t, ( $blog_id ? $blog_id : undef ) ); 
     1056            my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d", $ts[5] + 1900, 
     1057              $ts[4] + 1, @ts[ 3, 2, 1, 0 ]; 
     1058 
     1059            my $terms = {}; 
     1060            $terms->{blog_id} = $blog_id; 
     1061            $terms->{type} = $val->{type}; 
     1062            if ( $val->{type} =~ 
     1063                m/^(archive|individual|page|category|index|custom|widget)$/ ) 
     1064            { 
     1065                $terms->{name} = $val->{name}; 
     1066            } 
     1067            else { 
     1068                $terms->{identifier} = $val->{identifier}; 
     1069            } 
     1070 
     1071            # this should only return 1 template; we're searching 
     1072            # within a given blog for a specific type of template (for 
     1073            # "system" templates; or for a type + name, which should be 
     1074            # unique for that blog. 
     1075            my $tmpl = MT::Template->load($terms); 
     1076            if ($tmpl && $backup) { 
     1077 
     1078                # check for default template text... 
     1079                # if it is a default template, then outright replace it 
     1080                my $text = $tmpl->text; 
     1081                $text =~ s/\s+//g; 
     1082 
     1083                my $def_text = $val->{text}; 
     1084                $def_text =~ s/\s+//g; 
     1085 
     1086                # if it has been customized, back it up to a new tmpl record 
     1087                if ($def_text ne $text) { 
     1088                    my $backup = $tmpl->clone; 
     1089                    delete $backup->{column_values} 
     1090                      ->{id};    # make sure we don't overwrite original 
     1091                    delete $backup->{changed_cols}->{id}; 
     1092                    $backup->name( 
     1093                        $backup->name . $app->translate( ' (Backup from [_1])', $ts ) ); 
     1094                    $backup->type('backup'); 
     1095                    # if ( $backup->type !~ 
     1096                    #         m/^(archive|individual|page|category|index|custom|widget)$/ ) 
     1097                    # { 
     1098                    #     $backup->type('custom') 
     1099                    #       ;      # system templates can't be created 
     1100                    # } 
     1101                    $backup->outfile(''); 
     1102                    $backup->linked_file( $tmpl->linked_file ); 
     1103                    $backup->identifier(undef); 
     1104                    $backup->rebuild_me(0); 
     1105                    $backup->build_dynamic(0); 
     1106                    $backup->save; 
     1107                } 
     1108            } 
     1109            if ($tmpl) { 
     1110                # we found that the previous template had not been 
     1111                # altered, so replace it with new default template... 
     1112                $tmpl->text( $val->{text} ); 
     1113                $tmpl->identifier( $val->{identifier} ); 
     1114                $tmpl->type( $val->{type} ) 
     1115                  ; # fixes mismatch of types for cases like "archive" => "individual" 
     1116                $tmpl->linked_file(''); 
     1117                $tmpl->save; 
     1118            } 
     1119            else { 
     1120                # create this one... 
     1121                my $tmpl = new MT::Template; 
     1122                $tmpl->build_dynamic(0); 
     1123                $tmpl->set_values( 
     1124                    { 
     1125                        text       => $val->{text}, 
     1126                        name       => $val->{name}, 
     1127                        type       => $val->{type}, 
     1128                        identifier => $val->{identifier}, 
     1129                        outfile    => $val->{outfile}, 
     1130                        rebuild_me => $val->{rebuild_me} 
     1131                    } 
     1132                ); 
     1133                $tmpl->blog_id($blog_id); 
     1134                $tmpl->save 
     1135                  or return $app->error( 
     1136                        $app->translate("Error creating new template: ") 
     1137                      . $tmpl->errstr ); 
     1138            } 
     1139        } 
     1140    } 
     1141 
     1142    $app->add_return_arg( 'refreshed' => 1 ); 
     1143    $app->call_return; 
     1144} 
     1145 
    9301146sub refresh_individual_templates { 
    9311147    my ($app) = @_;