| 1 | # Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd. |
|---|
| 2 | # This program is distributed under the terms of the |
|---|
| 3 | # GNU General Public License, version 2. |
|---|
| 4 | # |
|---|
| 5 | # $Id$ |
|---|
| 6 | |
|---|
| 7 | package MT::CMS::Template; |
|---|
| 8 | |
|---|
| 9 | use strict; |
|---|
| 10 | |
|---|
| 11 | sub edit { |
|---|
| 12 |   my $cb = shift; |
|---|
| 13 |   my ($app, $id, $obj, $param) = @_; |
|---|
| 14 | |
|---|
| 15 |   my $q = $app->param; |
|---|
| 16 |   my $blog_id = $q->param('blog_id'); |
|---|
| 17 | |
|---|
| 18 | Â Â # FIXME: enumeration of types |
|---|
| 19 |   unless ( $blog_id ) { |
|---|
| 20 |     my $type = $q->param('type') || ( $obj ? $obj->type : undef ); |
|---|
| 21 |     return $app->return_to_dashboard( redirect => 1 ) |
|---|
| 22 |       if $type eq 'archive' |
|---|
| 23 |       || $type eq 'individual' |
|---|
| 24 |       || $type eq 'category' |
|---|
| 25 |       || $type eq 'page' |
|---|
| 26 |       || $type eq 'index'; |
|---|
| 27 | Â Â } |
|---|
| 28 | |
|---|
| 29 |   my $type = $q->param('_type'); |
|---|
| 30 |   my $blog = $app->blog; |
|---|
| 31 |   my $cfg = $app->config; |
|---|
| 32 |   my $perms = $app->permissions; |
|---|
| 33 |   my $can_preview = 0; |
|---|
| 34 | |
|---|
| 35 |   if ($blog) { |
|---|
| 36 | Â Â Â Â # include_system/include_cache are only applicable |
|---|
| 37 | Â Â Â Â # to blog-level templates |
|---|
| 38 | Â Â Â Â $param->{include_system}Â =Â $blog->include_system; |
|---|
| 39 | Â Â Â Â $param->{include_cache}Â =Â $blog->include_cache; |
|---|
| 40 | Â Â } |
|---|
| 41 | |
|---|
| 42 |   if ($id) { |
|---|
| 43 | Â Â Â Â # FIXME: Template types should not be enumerated here |
|---|
| 44 | Â Â Â Â $param->{nav_templates}Â =Â 1; |
|---|
| 45 |     my $tab; |
|---|
| 46 |     if ( $obj->type eq 'index' ) { |
|---|
| 47 |       $tab = 'index'; |
|---|
| 48 | Â Â Â Â Â Â $param->{template_group_trans}Â =Â $app->translate('index'); |
|---|
| 49 | Â Â Â Â } |
|---|
| 50 |     elsif ($obj->type eq 'archive' |
|---|
| 51 |       || $obj->type eq 'individual' |
|---|
| 52 |       || $obj->type eq 'category' |
|---|
| 53 |       || $obj->type eq 'page' ) |
|---|
| 54 | Â Â Â Â { |
|---|
| 55 | |
|---|
| 56 | Â Â Â Â Â Â # FIXME: enumeration of types |
|---|
| 57 |       $tab = 'archive'; |
|---|
| 58 | Â Â Â Â Â Â $param->{template_group_trans}Â =Â $app->translate('archive'); |
|---|
| 59 | Â Â Â Â } |
|---|
| 60 |     elsif ( $obj->type eq 'custom' ) { |
|---|
| 61 |       $tab = 'module'; |
|---|
| 62 | Â Â Â Â Â Â $param->{template_group_trans}Â =Â $app->translate('module'); |
|---|
| 63 | Â Â Â Â } |
|---|
| 64 |     elsif ( $obj->type eq 'widget' ) { |
|---|
| 65 |       $tab = 'widget'; |
|---|
| 66 | Â Â Â Â Â Â $param->{template_group_trans}Â =Â $app->translate('widget'); |
|---|
| 67 | Â Â Â Â } |
|---|
| 68 |     elsif ( $obj->type eq 'email' ) { |
|---|
| 69 |       $tab = 'email'; |
|---|
| 70 | Â Â Â Â Â Â $param->{template_group_trans}Â =Â $app->translate('email'); |
|---|
| 71 | Â Â Â Â } |
|---|
| 72 |     else { |
|---|
| 73 |       $tab = 'system'; |
|---|
| 74 | Â Â Â Â Â Â $param->{template_group_trans}Â =Â $app->translate('system'); |
|---|
| 75 | Â Â Â Â } |
|---|
| 76 | Â Â Â Â $param->{template_group}Â =Â $tab; |
|---|
| 77 |     $blog_id = $obj->blog_id; |
|---|
| 78 | |
|---|
| 79 | Â Â Â Â # FIXME: enumeration of types |
|---|
| 80 |        $param->{has_name} = $obj->type eq 'index' |
|---|
| 81 |      || $obj->type eq 'custom' |
|---|
| 82 |      || $obj->type eq 'widget' |
|---|
| 83 |      || $obj->type eq 'archive' |
|---|
| 84 |      || $obj->type eq 'category' |
|---|
| 85 |      || $obj->type eq 'page' |
|---|
| 86 |      || $obj->type eq 'individual'; |
|---|
| 87 |     if ( !$param->{has_name} ) { |
|---|
| 88 |       $param->{ 'type_' . $obj->type } = 1; |
|---|
| 89 | Â Â Â Â Â Â $param->{name}Â =Â $obj->name; |
|---|
| 90 | Â Â Â Â } |
|---|
| 91 | Â Â Â Â $app->add_breadcrumb(Â $param->{name}Â ); |
|---|
| 92 |     $param->{has_outfile} = $obj->type eq 'index'; |
|---|
| 93 | Â Â Â Â $param->{has_rebuild}Â = |
|---|
| 94 |      (   ( $obj->type eq 'index' ) |
|---|
| 95 |        && ( ( $blog->custom_dynamic_templates || "" ) ne 'all' ) ); |
|---|
| 96 | |
|---|
| 97 | Â Â Â Â # FIXME: enumeration of types |
|---|
| 98 |        $param->{is_special} = $param->{type} ne 'index' |
|---|
| 99 |      && $param->{type} ne 'archive' |
|---|
| 100 |      && $param->{type} ne 'category' |
|---|
| 101 |      && $param->{type} ne 'page' |
|---|
| 102 |      && $param->{type} ne 'individual'; |
|---|
| 103 | Â Â Â Â Â Â Â $param->{has_build_options}Â =Â $param->{has_build_options} |
|---|
| 104 |      && $param->{type} ne 'custom' |
|---|
| 105 |      && $param->{type} ne 'widget' |
|---|
| 106 | Â Â Â Â Â &&Â !$param->{is_special}; |
|---|
| 107 | Â Â Â Â $param->{search_label}Â =Â $app->translate('Templates'); |
|---|
| 108 | Â Â Â Â $param->{object_type}Â =Â 'template'; |
|---|
| 109 |     my $published_url = $obj->published_url; |
|---|
| 110 |     $param->{published_url} = $published_url if $published_url; |
|---|
| 111 |     $param->{saved_rebuild} = 1 if $q->param('saved_rebuild'); |
|---|
| 112 | |
|---|
| 113 |     my $filter = $app->param('filter_key'); |
|---|
| 114 |     if ($param->{template_group} eq 'email') { |
|---|
| 115 |       $app->param( 'filter_key', 'email_templates' ); |
|---|
| 116 |     }elsif ($param->{template_group} eq 'system') { |
|---|
| 117 |       $app->param( 'filter_key', 'system_templates' ); |
|---|
| 118 | Â Â Â Â } |
|---|
| 119 |     $app->load_list_actions( 'template', $param ); |
|---|
| 120 |     $app->param( 'filter_key', $filter ); |
|---|
| 121 | |
|---|
| 122 | Â Â Â Â $obj->compile; |
|---|
| 123 |     if ( $obj->{errors} && @{ $obj->{errors} } ) { |
|---|
| 124 | Â Â Â Â Â Â $param->{error}Â =Â $app->translate( |
|---|
| 125 | Â Â Â Â Â Â Â Â "One or more errors were found in this template."); |
|---|
| 126 | Â Â Â Â Â Â $param->{error}Â .=Â "<ul>\n"; |
|---|
| 127 |       foreach my $err ( @{ $obj->{errors} } ) { |
|---|
| 128 | Â Â Â Â Â Â Â Â $param->{error}Â .=Â "<li>" |
|---|
| 129 |          . MT::Util::encode_html( $err->{message} ) |
|---|
| 130 |          . "</li>\n"; |
|---|
| 131 | Â Â Â Â Â Â } |
|---|
| 132 | Â Â Â Â Â Â $param->{error}Â .=Â "</ul>\n"; |
|---|
| 133 | Â Â Â Â } |
|---|
| 134 | |
|---|
| 135 | Â Â Â Â # Populate list of included templates |
|---|
| 136 |     if ( my $includes = $obj->getElementsByTagName('Include') ) { |
|---|
| 137 |       my @includes; |
|---|
| 138 |       my @widgets; |
|---|
| 139 |       my %seen; |
|---|
| 140 |       foreach my $tag (@$includes) { |
|---|
| 141 |         my $include = {}; |
|---|
| 142 |         my $mod = $include->{include_module} = $tag->[1]->{module} || $tag->[1]->{widget}; |
|---|
| 143 |         next unless $mod; |
|---|
| 144 |         my $type = $tag->[1]->{widget} ? 'widget' : 'custom'; |
|---|
| 145 |         next if exists $seen{$type}{$mod}; |
|---|
| 146 | Â Â Â Â Â Â Â Â $seen{$type}{$mod}Â =Â 1; |
|---|
| 147 |         my $other = MT::Template->load( |
|---|
| 148 | Â Â Â Â Â Â Â Â Â Â { |
|---|
| 149 |             blog_id => [ $obj->blog_id, 0 ], |
|---|
| 150 |             name  => $mod, |
|---|
| 151 |             type  => $type, |
|---|
| 152 |           }, { |
|---|
| 153 |             sort   => 'blog_id', |
|---|
| 154 | Â Â Â Â Â Â Â Â Â Â Â Â direction =>Â 'descend', |
|---|
| 155 | Â Â Â Â Â Â Â Â Â Â } |
|---|
| 156 | Â Â Â Â Â Â Â Â ); |
|---|
| 157 |         if ($other) { |
|---|
| 158 | Â Â Â Â Â Â Â Â Â Â $include->{include_link}Â =Â $app->mt_uri( |
|---|
| 159 | Â Â Â Â Â Â Â Â Â Â Â Â mode =>Â 'view', |
|---|
| 160 | Â Â Â Â Â Â Â Â Â Â Â Â args =>Â { |
|---|
| 161 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â blog_id =>Â $other->blog_id ||Â 0, |
|---|
| 162 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â '_type'Â =>Â 'template', |
|---|
| 163 |               id   => $other->id |
|---|
| 164 | Â Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 165 | Â Â Â Â Â Â Â Â Â Â ); |
|---|
| 166 | Â Â Â Â Â Â Â Â Â Â # Try to compile template module if using MTInclude in this template. |
|---|
| 167 | Â Â Â Â Â Â Â Â Â Â $other->compile; |
|---|
| 168 |           if ( $other->{errors} && @{ $other->{errors} } ) { |
|---|
| 169 | Â Â Â Â Â Â Â Â Â Â Â Â $param->{error}Â =Â $app->translate( |
|---|
| 170 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â "One or more errors were found in included template module (".$other->name.")."); |
|---|
| 171 | Â Â Â Â Â Â Â Â Â Â Â Â $param->{error}Â .=Â "<ul>\n"; |
|---|
| 172 |             foreach my $err ( @{ $other->{errors} } ) { |
|---|
| 173 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â $param->{error}Â .=Â "<li>" |
|---|
| 174 |                . MT::Util::encode_html( $err->{message} ) |
|---|
| 175 |                . "</li>\n"; |
|---|
| 176 | Â Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 177 | Â Â Â Â Â Â Â Â Â Â Â Â $param->{error}Â .=Â "</ul>\n"; |
|---|
| 178 | Â Â Â Â Â Â Â Â Â Â } |
|---|
| 179 | Â Â Â Â Â Â Â Â } |
|---|
| 180 |         else { |
|---|
| 181 | Â Â Â Â Â Â Â Â Â Â $include->{create_link}Â =Â $app->mt_uri( |
|---|
| 182 | Â Â Â Â Â Â Â Â Â Â Â Â mode =>Â 'view', |
|---|
| 183 | Â Â Â Â Â Â Â Â Â Â Â Â args =>Â { |
|---|
| 184 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â blog_id =>Â $obj->blog_id, |
|---|
| 185 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â '_type'Â =>Â 'template', |
|---|
| 186 |               type  => $type, |
|---|
| 187 |               name  => $mod, |
|---|
| 188 | Â Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 189 | Â Â Â Â Â Â Â Â Â Â ); |
|---|
| 190 | Â Â Â Â Â Â Â Â } |
|---|
| 191 |         if ($type eq 'widget') { |
|---|
| 192 |           push @widgets, $include; |
|---|
| 193 |         } else { |
|---|
| 194 |           push @includes, $include; |
|---|
| 195 | Â Â Â Â Â Â Â Â } |
|---|
| 196 | Â Â Â Â Â Â } |
|---|
| 197 |       $param->{include_loop} = \@includes if @includes; |
|---|
| 198 |       $param->{widget_loop} = \@widgets if @widgets; |
|---|
| 199 | Â Â Â Â } |
|---|
| 200 |     my @sets = ( @{ $obj->getElementsByTagName('WidgetSet') || [] }, @{ $obj->getElementsByTagName('WidgetManager') || [] } ); |
|---|
| 201 |     if ( @sets ) { |
|---|
| 202 |       my @widget_sets; |
|---|
| 203 |       my %seen; |
|---|
| 204 |       foreach my $set (@sets) { |
|---|
| 205 |         my $name = $set->[1]->{name}; |
|---|
| 206 |         next unless $name; |
|---|
| 207 |         next if $seen{$name}; |
|---|
| 208 | Â Â Â Â Â Â Â Â $seen{$name}Â =Â 1; |
|---|
| 209 |         my $wset = MT::Template->load( |
|---|
| 210 | Â Â Â Â Â Â Â Â Â Â { |
|---|
| 211 |             blog_id => [ $obj->blog_id, 0 ], |
|---|
| 212 |             name  => $name, |
|---|
| 213 |             type  => 'widgetset', |
|---|
| 214 |           }, { |
|---|
| 215 |             sort   => 'blog_id', |
|---|
| 216 | Â Â Â Â Â Â Â Â Â Â Â Â direction =>Â 'descend', |
|---|
| 217 | Â Â Â Â Â Â Â Â Â Â } |
|---|
| 218 | Â Â Â Â Â Â Â Â ); |
|---|
| 219 |         push @widget_sets, { |
|---|
| 220 | Â Â Â Â Â Â Â Â Â Â include_link =>Â $app->mt_uri( |
|---|
| 221 | Â Â Â Â Â Â Â Â Â Â Â Â mode =>Â 'edit_widget', |
|---|
| 222 | Â Â Â Â Â Â Â Â Â Â Â Â args =>Â { |
|---|
| 223 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â blog_id =>Â $wset->blog_id, |
|---|
| 224 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â id =>Â $wset->id, |
|---|
| 225 | Â Â Â Â Â Â Â Â Â Â Â Â }, |
|---|
| 226 | Â Â Â Â Â Â Â Â Â Â ), |
|---|
| 227 | Â Â Â Â Â Â Â Â Â Â include_module =>Â $name, |
|---|
| 228 | Â Â Â Â Â Â Â Â }; |
|---|
| 229 | Â Â Â Â Â Â } |
|---|
| 230 |       $param->{widget_set_loop} = \@widget_sets if @widget_sets; |
|---|
| 231 | Â Â Â Â } |
|---|
| 232 |     $param->{have_includes} = 1 if $param->{widget_set_loop} || $param->{include_loop} || $param->{widget_loop}; |
|---|
| 233 | Â Â Â Â # Populate archive types for creating new map |
|---|
| 234 |     my $obj_type = $obj->type; |
|---|
| 235 |     if (  $obj_type eq 'individual' |
|---|
| 236 |       || $obj_type eq 'page' |
|---|
| 237 |       || $obj_type eq 'author' |
|---|
| 238 |       || $obj_type eq 'category' |
|---|
| 239 |       || $obj_type eq 'archive' ) |
|---|
| 240 | Â Â Â Â { |
|---|
| 241 |       my @at = $app->publisher->archive_types; |
|---|
| 242 |       my @archive_types; |
|---|
| 243 |       for my $at (@at) { |
|---|
| 244 |         my $archiver   = $app->publisher->archiver($at); |
|---|
| 245 |         my $archive_label = $archiver->archive_label; |
|---|
| 246 |         $archive_label = $at unless $archive_label; |
|---|
| 247 |         $archive_label = $archive_label->() |
|---|
| 248 |          if ( ref $archive_label ) eq 'CODE'; |
|---|
| 249 |         if (  ( $obj_type eq 'archive' ) |
|---|
| 250 |           || ( $obj_type eq 'author' ) |
|---|
| 251 |           || ( $obj_type eq 'category' ) ) |
|---|
| 252 | Â Â Â Â Â Â Â Â { |
|---|
| 253 | |
|---|
| 254 | Â Â Â Â Â Â Â Â Â Â # only include if it is NOT an entry-based archive type |
|---|
| 255 |           next if $archiver->entry_based; |
|---|
| 256 | Â Â Â Â Â Â Â Â } |
|---|
| 257 |         elsif ( $obj_type eq 'page' ) { |
|---|
| 258 | Â Â Â Â Â Â Â Â Â Â # only include if it is a entry-based archive type and page |
|---|
| 259 |           next unless $archiver->entry_based; |
|---|
| 260 |           next if $archiver->entry_class ne 'page'; |
|---|
| 261 | Â Â Â Â Â Â Â Â } |
|---|
| 262 |         elsif ( $obj_type eq 'individual' ) { |
|---|
| 263 | Â Â Â Â Â Â Â Â Â Â # only include if it is a entry-based archive type and entry |
|---|
| 264 |           next unless $archiver->entry_based; |
|---|
| 265 |           next if $archiver->entry_class eq 'page'; |
|---|
| 266 | Â Â Â Â Â Â Â Â } |
|---|
| 267 |         push @archive_types, |
|---|
| 268 | Â Â Â Â Â Â Â Â Â { |
|---|
| 269 | Â Â Â Â Â Â Â Â Â Â archive_type_translated =>Â $archive_label, |
|---|
| 270 |           archive_type      => $at, |
|---|
| 271 | Â Â Â Â Â Â Â Â Â }; |
|---|
| 272 |         @archive_types = |
|---|
| 273 |          sort { MT::App::CMS::archive_type_sorter( $a, $b ) } @archive_types; |
|---|
| 274 | Â Â Â Â Â Â } |
|---|
| 275 | Â Â Â Â Â Â $param->{archive_types}Â =Â \@archive_types; |
|---|
| 276 | |
|---|
| 277 | Â Â Â Â Â Â # Populate template maps for this template |
|---|
| 278 |       my $maps = _populate_archive_loop( $app, $blog, $obj ); |
|---|
| 279 |       if (@$maps) { |
|---|
| 280 | Â Â Â Â Â Â Â Â $param->{object_loop}Â =Â $param->{template_map_loop}Â =Â $maps |
|---|
| 281 |          if @$maps; |
|---|
| 282 |         my %archive_types = map { $_->{archive_label} => 1 } @$maps; |
|---|
| 283 |         $param->{enabled_archive_types} = join(", ", sort keys %archive_types); |
|---|
| 284 | Â Â Â Â Â Â } |
|---|
| 285 | Â Â Â Â } |
|---|
| 286 | Â Â Â Â # publish options |
|---|
| 287 | Â Â Â Â $param->{build_type}Â =Â $obj->build_type; |
|---|
| 288 |     $param->{ 'build_type_' . ( $obj->build_type || 0 ) } = 1; |
|---|
| 289 | Â Â Â Â #my ( $period, $interval ) = _get_schedule( $obj->build_interval ); |
|---|
| 290 | Â Â Â Â #$param->{ 'schedule_period_' . $period } = 1; |
|---|
| 291 | Â Â Â Â #$param->{schedule_interval} = $interval; |
|---|
| 292 |     $param->{type} = 'custom' if $param->{type} eq 'module'; |
|---|
| 293 |   } else { |
|---|
| 294 |     my $new_tmpl = $q->param('create_new_template'); |
|---|
| 295 |     my $template_type; |
|---|
| 296 |     if ($new_tmpl) { |
|---|
| 297 |       if ( $new_tmpl =~ m/^blank:(.+)/ ) { |
|---|
| 298 |         $template_type = $1; |
|---|
| 299 | Â Â Â Â Â Â Â Â $param->{type}Â =Â $1; |
|---|
| 300 | Â Â Â Â Â Â } |
|---|
| 301 |       elsif ( $new_tmpl =~ m/^default:([^:]+):(.+)/ ) { |
|---|
| 302 |         $template_type = $1; |
|---|
| 303 |         $template_type = 'custom' if $template_type eq 'module'; |
|---|
| 304 |         my $template_id = $2; |
|---|
| 305 |         my $set = $blog ? $blog->template_set : undef; |
|---|
| 306 |         require MT::DefaultTemplates; |
|---|
| 307 |         my $def_tmpl = MT::DefaultTemplates->templates($set) || []; |
|---|
| 308 |         my ($tmpl) = |
|---|
| 309 |          grep { $_->{identifier} eq $template_id } @$def_tmpl; |
|---|
| 310 | Â Â Â Â Â Â Â Â $param->{text}Â =Â $app->translate_templatized(Â $tmpl->{text}Â ) |
|---|
| 311 |          if $tmpl; |
|---|
| 312 | Â Â Â Â Â Â Â Â $param->{type}Â =Â $template_type; |
|---|
| 313 | Â Â Â Â Â Â } |
|---|
| 314 | Â Â Â Â } |
|---|
| 315 |     else { |
|---|
| 316 |       $template_type = $q->param('type'); |
|---|
| 317 |       $template_type = 'custom' if 'module' eq $template_type; |
|---|
| 318 | Â Â Â Â Â Â $param->{type}Â Â =Â $template_type; |
|---|
| 319 | Â Â Â Â } |
|---|
| 320 |     return $app->errtrans("Create template requires type") |
|---|
| 321 |      unless $template_type; |
|---|
| 322 | Â Â Â Â $param->{nav_templates}Â =Â 1; |
|---|
| 323 |     my $tab; |
|---|
| 324 | |
|---|
| 325 | Â Â Â Â # FIXME: enumeration of types |
|---|
| 326 |     if ( $template_type eq 'index' ) { |
|---|
| 327 |       $tab = 'index'; |
|---|
| 328 | Â Â Â Â Â Â $param->{template_group_trans}Â =Â $app->translate('index'); |
|---|
| 329 | Â Â Â Â } |
|---|
| 330 |     elsif ($template_type eq 'archive' |
|---|
| 331 |       || $template_type eq 'individual' |
|---|
| 332 |       || $template_type eq 'category' |
|---|
| 333 |       || $template_type eq 'page' ) |
|---|
| 334 | Â Â Â Â { |
|---|
| 335 |       $tab             = 'archive'; |
|---|
| 336 | Â Â Â Â Â Â $param->{template_group_trans}Â =Â $app->translate('archive'); |
|---|
| 337 | Â Â Â Â Â Â $param->{type_archive}Â Â Â Â Â =Â 1; |
|---|
| 338 |       my @types = ( |
|---|
| 339 | Â Â Â Â Â Â Â Â { |
|---|
| 340 |           key  => 'archive', |
|---|
| 341 | Â Â Â Â Â Â Â Â Â Â label =>Â $app->translate('Archive') |
|---|
| 342 | Â Â Â Â Â Â Â Â }, |
|---|
| 343 | Â Â Â Â Â Â Â Â { |
|---|
| 344 |           key  => 'individual', |
|---|
| 345 | Â Â Â Â Â Â Â Â Â Â label =>Â $app->translate('Entry or Page') |
|---|
| 346 | Â Â Â Â Â Â Â Â }, |
|---|
| 347 | Â Â Â Â Â Â ); |
|---|
| 348 | Â Â Â Â Â Â $param->{new_archive_types}Â =Â \@types; |
|---|
| 349 | Â Â Â Â } |
|---|
| 350 |     elsif ( $template_type eq 'custom' ) { |
|---|
| 351 |       $tab = 'module'; |
|---|
| 352 | Â Â Â Â Â Â $param->{template_group_trans}Â =Â $app->translate('module'); |
|---|
| 353 | Â Â Â Â } |
|---|
| 354 |     elsif ( $template_type eq 'widget' ) { |
|---|
| 355 |       $tab = 'widget'; |
|---|
| 356 | Â Â Â Â Â Â $param->{template_group_trans}Â =Â $app->translate('widget'); |
|---|
| 357 | Â Â Â Â } |
|---|
| 358 |     else { |
|---|
| 359 |       $tab = 'system'; |
|---|
| 360 | Â Â Â Â Â Â $param->{template_group_trans}Â =Â $app->translate('system'); |
|---|
| 361 | Â Â Â Â } |
|---|
| 362 | Â Â Â Â $param->{template_group}Â =Â $tab; |
|---|
| 363 | Â Â Â Â $app->translate($tab); |
|---|
| 364 | Â Â Â Â $app->add_breadcrumb(Â $app->translate('New Template')Â ); |
|---|
| 365 | |
|---|
| 366 | Â Â Â Â # FIXME: enumeration of types |
|---|
| 367 |        $param->{has_name} = $template_type eq 'index' |
|---|
| 368 |      || $template_type eq 'custom' |
|---|
| 369 |      || $template_type eq 'widget' |
|---|
| 370 |      || $template_type eq 'archive' |
|---|
| 371 |      || $template_type eq 'category' |
|---|
| 372 |      || $template_type eq 'page' |
|---|
| 373 |      || $template_type eq 'individual'; |
|---|
| 374 |     $param->{has_outfile} = $template_type eq 'index'; |
|---|
| 375 | Â Â Â Â $param->{has_rebuild}Â = |
|---|
| 376 |      (   ( $template_type eq 'index' ) |
|---|
| 377 |        && ( ( $blog->custom_dynamic_templates || "" ) ne 'all' ) ); |
|---|
| 378 | Â Â Â Â $param->{custom_dynamic}Â = |
|---|
| 379 |      $blog && $blog->custom_dynamic_templates eq 'custom'; |
|---|
| 380 | Â Â Â Â $param->{has_build_options}Â = |
|---|
| 381 |        $blog && ($blog->custom_dynamic_templates eq 'custom' |
|---|
| 382 | Â Â Â Â Â ||Â $param->{has_rebuild}); |
|---|
| 383 | |
|---|
| 384 | Â Â Â Â # FIXME: enumeration of types |
|---|
| 385 |        $param->{is_special} = $param->{type} ne 'index' |
|---|
| 386 |      && $param->{type} ne 'archive' |
|---|
| 387 |      && $param->{type} ne 'category' |
|---|
| 388 |      && $param->{type} ne 'page' |
|---|
| 389 |      && $param->{type} ne 'individual'; |
|---|
| 390 | Â Â Â Â Â Â Â $param->{has_build_options}Â =Â $param->{has_build_options} |
|---|
| 391 |      && $param->{type} ne 'custom' |
|---|
| 392 |      && $param->{type} ne 'widget' |
|---|
| 393 | Â Â Â Â Â &&Â !$param->{is_special}; |
|---|
| 394 | |
|---|
| 395 | Â Â Â Â $param->{name}Â Â Â Â =Â MT::Util::decode_url(Â $app->param('name')Â ) |
|---|
| 396 |      if $app->param('name'); |
|---|
| 397 | Â Â } |
|---|
| 398 |   $param->{publish_queue_available} = eval 'require List::Util; require Scalar::Util; 1;'; |
|---|
| 399 | |
|---|
| 400 |   my $set = $blog ? $blog->template_set : undef; |
|---|
| 401 |   require MT::DefaultTemplates; |
|---|
| 402 |   my $tmpls = MT::DefaultTemplates->templates($set); |
|---|
| 403 |   my @tmpl_ids; |
|---|
| 404 |   foreach my $dtmpl (@$tmpls) { |
|---|
| 405 |     if ( !$param->{has_name} ) { |
|---|
| 406 |       if ($obj->type eq 'email') { |
|---|
| 407 |         if ($dtmpl->{identifier} eq $obj->identifier) { |
|---|
| 408 | Â Â Â Â Â Â Â Â Â Â $param->{template_name_label}Â =Â $dtmpl->{label}; |
|---|
| 409 | Â Â Â Â Â Â Â Â Â Â $param->{template_name}Â Â Â Â =Â $dtmpl->{name}; |
|---|
| 410 | Â Â Â Â Â Â Â Â } |
|---|
| 411 | Â Â Â Â Â Â } |
|---|
| 412 |       else { |
|---|
| 413 |         if ( $dtmpl->{type} eq $obj->type ) { |
|---|
| 414 | Â Â Â Â Â Â Â Â Â Â $param->{template_name_label}Â =Â $dtmpl->{label}; |
|---|
| 415 | Â Â Â Â Â Â Â Â Â Â $param->{template_name}Â Â Â Â =Â $dtmpl->{name}; |
|---|
| 416 | Â Â Â Â Â Â Â Â } |
|---|
| 417 | Â Â Â Â Â Â } |
|---|
| 418 | Â Â Â Â } |
|---|
| 419 |     if ( $dtmpl->{type} eq 'index' ) { |
|---|
| 420 |       push @tmpl_ids, |
|---|
| 421 | Â Â Â Â Â Â Â { |
|---|
| 422 |         label  => $dtmpl->{label}, |
|---|
| 423 |         key   => $dtmpl->{key}, |
|---|
| 424 | Â Â Â Â Â Â Â Â selected =>Â $dtmpl->{key}Â eq |
|---|
| 425 |          ( ( $obj ? $obj->identifier : undef ) || '' ), |
|---|
| 426 | Â Â Â Â Â Â Â }; |
|---|
| 427 | Â Â Â Â } |
|---|
| 428 | Â Â } |
|---|
| 429 | Â Â $param->{index_identifiers}Â =Â \@tmpl_ids; |
|---|
| 430 | |
|---|
| 431 | Â Â $param->{"type_$param->{type}"}Â =Â 1; |
|---|
| 432 |   if ($perms) { |
|---|
| 433 |     my $pref_param = |
|---|
| 434 | Â Â Â Â Â $app->load_template_prefs(Â $perms->template_prefs ); |
|---|
| 435 |     %$param = ( %$param, %$pref_param ); |
|---|
| 436 | Â Â } |
|---|
| 437 | |
|---|
| 438 | Â Â # Populate structure for template snippets |
|---|
| 439 |   if ( my $snippets = $app->registry('template_snippets') || {} ) { |
|---|
| 440 |     my @snippets; |
|---|
| 441 |     for my $snip_id ( keys %$snippets ) { |
|---|
| 442 |       my $label = $snippets->{$snip_id}{label}; |
|---|
| 443 |       $label = $label->() if ref($label) eq 'CODE'; |
|---|
| 444 |       push @snippets, |
|---|
| 445 | Â Â Â Â Â Â Â { |
|---|
| 446 |         id   => $snip_id, |
|---|
| 447 | Â Â Â Â Â Â Â Â trigger =>Â $snippets->{$snip_id}{trigger}, |
|---|
| 448 |         label  => $label, |
|---|
| 449 | Â Â Â Â Â Â Â Â content =>Â $snippets->{$snip_id}{content}, |
|---|
| 450 | Â Â Â Â Â Â Â }; |
|---|
| 451 | Â Â Â Â } |
|---|
| 452 |     @snippets = sort { $a->{label} cmp $b->{label} } @snippets; |
|---|
| 453 | Â Â Â Â $param->{template_snippets}Â =Â \@snippets; |
|---|
| 454 | Â Â } |
|---|
| 455 | |
|---|
| 456 | Â Â # Populate structure for tag documentation |
|---|
| 457 |   my $all_tags = MT::Component->registry("tags"); |
|---|
| 458 |   my $tag_docs = {}; |
|---|
| 459 |   foreach my $tag_set (@$all_tags) { |
|---|
| 460 |     my $url = $tag_set->{help_url}; |
|---|
| 461 |     $url = $url->() if ref($url) eq 'CODE'; |
|---|
| 462 | Â Â Â Â # hey, at least give them a google search |
|---|
| 463 |     $url ||= 'http://www.google.com/search?q=mt%t'; |
|---|
| 464 |     my $tag_list = ''; |
|---|
| 465 |     foreach my $type (qw( block function )) { |
|---|
| 466 |       my $tags = $tag_set->{$type} or next; |
|---|
| 467 |       $tag_list .= ($tag_list eq '' ? '' : ',') . join(",", keys(%$tags)); |
|---|
| 468 | Â Â Â Â } |
|---|
| 469 |     $tag_list =~ s/(^|,)plugin(,|$)/,/; |
|---|
| 470 |     if (exists $tag_docs->{$url}) { |
|---|
| 471 |       $tag_docs->{$url} .= ',' . $tag_list; |
|---|
| 472 | Â Â Â Â } |
|---|
| 473 |     else { |
|---|
| 474 | Â Â Â Â Â Â $tag_docs->{$url}Â =Â $tag_list; |
|---|
| 475 | Â Â Â Â } |
|---|
| 476 | Â Â } |
|---|
| 477 | Â Â $param->{tag_docs}Â =Â $tag_docs; |
|---|
| 478 | Â Â $param->{link_doc}Â =Â $app->help_url('appendices/tags/'); |
|---|
| 479 | |
|---|
| 480 |   $param->{screen_id} = "edit-template-" . $param->{type}; |
|---|
| 481 | |
|---|
| 482 | Â Â # template language |
|---|
| 483 | Â Â $param->{template_lang}Â =Â 'html'; |
|---|
| 484 |   if ( $obj && $obj->outfile ) { |
|---|
| 485 |     if ( $obj->outfile =~ m/\.(css|js|html|php|pl|asp)$/ ) { |
|---|
| 486 | Â Â Â Â Â Â $param->{template_lang}Â =Â { |
|---|
| 487 | Â Â Â Â Â Â Â Â css =>Â 'css', |
|---|
| 488 | Â Â Â Â Â Â Â Â js =>Â 'javascript', |
|---|
| 489 | Â Â Â Â Â Â Â Â html =>Â 'html', |
|---|
| 490 | Â Â Â Â Â Â Â Â php =>Â 'php', |
|---|
| 491 | Â Â Â Â Â Â Â Â pl =>Â 'perl', |
|---|
| 492 | Â Â Â Â Â Â Â Â asp =>Â 'asp', |
|---|
| 493 | Â Â Â Â Â Â }->{$1}; |
|---|
| 494 | Â Â Â Â } |
|---|
| 495 | Â Â } |
|---|
| 496 | |
|---|
| 497 |   if (($param->{type} eq 'custom') || ($param->{type} eq 'widget')) { |
|---|
| 498 |     if ($blog) { |
|---|
| 499 | Â Â Â Â Â Â $param->{include_with_ssi}Â Â Â =Â 0; |
|---|
| 500 | Â Â Â Â Â Â $param->{cache_path}Â Â Â Â Â Â =Â ''; |
|---|
| 501 | Â Â Â Â Â Â $param->{cache_expire_type}Â Â Â =Â 0; |
|---|
| 502 | Â Â Â Â Â Â $param->{cache_expire_period}Â Â =Â ''; |
|---|
| 503 | Â Â Â Â Â Â $param->{cache_expire_interval}Â =Â 0; |
|---|
| 504 |       $param->{ssi_type} = uc $blog->include_system; |
|---|
| 505 | Â Â Â Â } |
|---|
| 506 |     if ($obj) { |
|---|
| 507 | Â Â Â Â Â Â $param->{include_with_ssi}Â =Â $obj->include_with_ssi |
|---|
| 508 |        if defined $obj->include_with_ssi; |
|---|
| 509 | Â Â Â Â Â Â $param->{cache_path}Â Â Â Â =Â $obj->cache_path |
|---|
| 510 |        if defined $obj->cache_path; |
|---|
| 511 | Â Â Â Â Â Â $param->{cache_expire_type}Â =Â $obj->cache_expire_type |
|---|
| 512 |        if defined $obj->cache_expire_type; |
|---|
| 513 |       my ( $period, $interval ) = |
|---|
| 514 | Â Â Â Â Â Â Â _get_schedule(Â $obj->cache_expire_interval ); |
|---|
| 515 |       $param->{cache_expire_period}  = $period  if defined $period; |
|---|
| 516 |       $param->{cache_expire_interval} = $interval if defined $interval; |
|---|
| 517 |       my @events = split ',', ($obj->cache_expire_event || ''); |
|---|
| 518 |       foreach my $name (@events) { |
|---|
| 519 |         $param->{ 'cache_expire_event_' . $name } = 1; |
|---|
| 520 | Â Â Â Â Â Â } |
|---|
| 521 | Â Â Â Â } |
|---|
| 522 | Â Â } |
|---|
| 523 | |
|---|
| 524 | Â Â # if unset, default to 30 so if they choose to enable caching, |
|---|
| 525 | Â Â # it will be preset to something sane. |
|---|
| 526 | Â Â $param->{cache_expire_interval}Â ||=Â 30; |
|---|
| 527 | |
|---|
| 528 | Â Â $param->{dirty}Â =Â 1 |
|---|
| 529 |     if $app->param('dirty'); |
|---|
| 530 | |
|---|
| 531 | Â Â $param->{can_preview}Â =Â 1 |
|---|
| 532 |     if (!$param->{is_special}) && (!$obj || ($obj && ($obj->outfile || '') !~ m/\.(css|xml|rss|js)$/)); |
|---|
| 533 | |
|---|
| 534 | Â Â 1; |
|---|
| 535 | } |
|---|
| 536 | |
|---|
| 537 | sub list { |
|---|
| 538 |   my $app = shift; |
|---|
| 539 | |
|---|
| 540 |   my $perms = $app->blog ? $app->permissions : $app->user->permissions; |
|---|
| 541 |   return $app->return_to_dashboard( redirect => 1 ) |
|---|
| 542 |    unless $perms || $app->user->is_superuser; |
|---|
| 543 |   if ( $perms && !$perms->can_edit_templates ) { |
|---|
| 544 |     return $app->return_to_dashboard( permission => 1 ); |
|---|
| 545 | Â Â } |
|---|
| 546 |   my $blog = $app->blog; |
|---|
| 547 | |
|---|
| 548 |   require MT::Template; |
|---|
| 549 |   my $blog_id = $app->param('blog_id') || 0; |
|---|
| 550 |   my $terms = { blog_id => $blog_id }; |
|---|
| 551 |   my $args = { sort  => 'name' }; |
|---|
| 552 | |
|---|
| 553 |   my $hasher = sub { |
|---|
| 554 |     my ( $obj, $row ) = @_; |
|---|
| 555 |     my $template_type; |
|---|
| 556 |     my $type = $row->{type} || ''; |
|---|
| 557 |     if ( $type =~ m/^(individual|page|category|archive)$/ ) { |
|---|
| 558 |       $template_type = 'archive'; |
|---|
| 559 | Â Â Â Â Â Â # populate context with templatemap loop |
|---|
| 560 |       my $tblog = $obj->blog_id == $blog->id ? $blog : MT::Blog->load( $obj->blog_id ); |
|---|
| 561 |       if ($tblog) { |
|---|
| 562 |         $row->{archive_types} = _populate_archive_loop( $app, $tblog, $obj ); |
|---|
| 563 | Â Â Â Â Â Â } |
|---|
| 564 | Â Â Â Â } |
|---|
| 565 |     elsif ( $type eq 'widget' ) { |
|---|
| 566 |       $template_type = 'widget'; |
|---|
| 567 | Â Â Â Â } |
|---|
| 568 |     elsif ( $type eq 'index' ) { |
|---|
| 569 |       $template_type = 'index'; |
|---|
| 570 | Â Â Â Â } |
|---|
| 571 |     elsif ( $type eq 'custom' ) { |
|---|
| 572 |       $template_type = 'module'; |
|---|
| 573 | Â Â Â Â } |
|---|
| 574 |     elsif ( $type eq 'email' ) { |
|---|
| 575 |       $template_type = 'email'; |
|---|
| 576 | Â Â Â Â } |
|---|
| 577 |     elsif ( $type eq 'backup' ) { |
|---|
| 578 |       $template_type = 'backup'; |
|---|
| 579 | Â Â Â Â } |
|---|
| 580 |     else { |
|---|
| 581 |       $template_type = 'system'; |
|---|
| 582 | Â Â Â Â } |
|---|
| 583 |     $row->{use_cache} = ( ($obj->cache_expire_type || 0) != 0 ) ? 1 : 0; |
|---|
| 584 | Â Â Â Â $row->{template_type}Â =Â $template_type; |
|---|
| 585 |     $row->{type} = 'entry' if $type eq 'individual'; |
|---|
| 586 |     my $published_url = $obj->published_url; |
|---|
| 587 |     $row->{published_url} = $published_url if $published_url; |
|---|
| 588 | Â Â }; |
|---|
| 589 | |
|---|
| 590 |   my $params    = {}; |
|---|
| 591 |   my $filter = $app->param('filter_key'); |
|---|
| 592 |   my $template_type = $filter || ''; |
|---|
| 593 |   $template_type =~ s/_templates//; |
|---|
| 594 | |
|---|
| 595 | Â Â $params->{screen_class}Â =Â "list-template"; |
|---|
| 596 | Â Â $params->{listing_screen}Â =Â 1; |
|---|
| 597 | |
|---|
| 598 |   $app->load_list_actions( 'template', $params ); |
|---|
| 599 | Â Â $params->{page_actions}Â =Â $app->page_actions('list_templates'); |
|---|
| 600 | Â Â $params->{search_label}Â =Â $app->translate("Templates"); |
|---|
| 601 | Â Â $params->{object_type}Â =Â 'template'; |
|---|
| 602 | Â Â $params->{blog_view}Â =Â 1; |
|---|
| 603 | Â Â $params->{refreshed}Â =Â $app->param('refreshed'); |
|---|
| 604 | Â Â $params->{published}Â =Â $app->param('published'); |
|---|
| 605 | Â Â $params->{saved_copied}Â =Â $app->param('saved_copied'); |
|---|
| 606 | Â Â $params->{saved_deleted}Â =Â $app->param('saved_deleted'); |
|---|
| 607 | Â Â $params->{saved}Â =Â $app->param('saved'); |
|---|
| 608 | |
|---|
| 609 | Â Â # determine list of system template types: |
|---|
| 610 |   my $scope; |
|---|
| 611 |   my $set; |
|---|
| 612 |   if ( $blog ) { |
|---|
| 613 |     $set  = $blog->template_set; |
|---|
| 614 |     $scope = 'system'; |
|---|
| 615 | Â Â } |
|---|
| 616 |   else { |
|---|
| 617 |     $scope = 'global:system'; |
|---|
| 618 | Â Â } |
|---|
| 619 |   my @tmpl_path = ( $set && ($set ne 'mt_blog')) ? ("template_sets", $set, 'templates', $scope) : ("default_templates", $scope); |
|---|
| 620 |   my $sys_tmpl = MT->registry(@tmpl_path) || {}; |
|---|
| 621 | |
|---|
| 622 |   my @tmpl_loop; |
|---|
| 623 |   my %types; |
|---|
| 624 |   if ($template_type ne 'backup') { |
|---|
| 625 |     if ($blog) { |
|---|
| 626 | Â Â Â Â Â Â # blog template listings |
|---|
| 627 |       %types = ( |
|---|
| 628 | Â Â Â Â Â Â Â Â 'index'Â =>Â { |
|---|
| 629 | Â Â Â Â Â Â Â Â Â Â label =>Â $app->translate("Index Templates"), |
|---|
| 630 | Â Â Â Â Â Â Â Â Â Â type =>Â 'index', |
|---|
| 631 | Â Â Â Â Â Â Â Â Â Â order =>Â 100, |
|---|
| 632 | Â Â Â Â Â Â Â Â }, |
|---|
| 633 | Â Â Â Â Â Â Â Â 'archive'Â =>Â { |
|---|
| 634 | Â Â Â Â Â Â Â Â Â Â label =>Â $app->translate("Archive Templates"), |
|---|
| 635 |           type => ['archive', 'individual', 'page', 'category'], |
|---|
| 636 | Â Â Â Â Â Â Â Â Â Â order =>Â 200, |
|---|
| 637 | Â Â Â Â Â Â Â Â }, |
|---|
| 638 | Â Â Â Â Â Â Â Â 'module'Â =>Â { |
|---|
| 639 | Â Â Â Â Â Â Â Â Â Â label =>Â $app->translate("Template Modules"), |
|---|
| 640 | Â Â Â Â Â Â Â Â Â Â type =>Â 'custom', |
|---|
| 641 | Â Â Â Â Â Â Â Â Â Â order =>Â 300, |
|---|
| 642 | Â Â Â Â Â Â Â Â }, |
|---|
| 643 | Â Â Â Â Â Â Â Â 'system'Â =>Â { |
|---|
| 644 | Â Â Â Â Â Â Â Â Â Â label =>Â $app->translate("System Templates"), |
|---|
| 645 |           type => [ keys %$sys_tmpl ], |
|---|
| 646 | Â Â Â Â Â Â Â Â Â Â order =>Â 400, |
|---|
| 647 | Â Â Â Â Â Â Â Â }, |
|---|
| 648 | Â Â Â Â Â Â ); |
|---|
| 649 |     } else { |
|---|
| 650 | Â Â Â Â Â Â # global template listings |
|---|
| 651 |       %types = ( |
|---|
| 652 | Â Â Â Â Â Â Â Â 'module'Â =>Â { |
|---|
| 653 | Â Â Â Â Â Â Â Â Â Â label =>Â $app->translate("Template Modules"), |
|---|
| 654 | Â Â Â Â Â Â Â Â Â Â type =>Â 'custom', |
|---|
| 655 | Â Â Â Â Â Â Â Â Â Â order =>Â 100, |
|---|
| 656 | Â Â Â Â Â Â Â Â }, |
|---|
| 657 | Â Â Â Â Â Â Â Â 'email'Â =>Â { |
|---|
| 658 | Â Â Â Â Â Â Â Â Â Â label =>Â $app->translate("Email Templates"), |
|---|
| 659 | Â Â Â Â Â Â Â Â Â Â type =>Â 'email', |
|---|
| 660 | Â Â Â Â Â Â Â Â Â Â order =>Â 200, |
|---|
| 661 | Â Â Â Â Â Â Â Â }, |
|---|
| 662 | Â Â Â Â Â Â Â Â 'system'Â =>Â { |
|---|
| 663 | Â Â Â Â Â Â Â Â Â Â label =>Â $app->translate("System Templates"), |
|---|
| 664 |           type => [ keys %$sys_tmpl ], |
|---|
| 665 | Â Â Â Â Â Â Â Â Â Â order =>Â 300, |
|---|
| 666 | Â Â Â Â Â Â Â Â }, |
|---|
| 667 | Â Â Â Â Â Â ); |
|---|
| 668 | Â Â Â Â } |
|---|
| 669 |   } else { |
|---|
| 670 | Â Â Â Â # global template listings |
|---|
| 671 |     %types = ( |
|---|
| 672 | Â Â Â Â Â Â 'backup'Â =>Â { |
|---|
| 673 | Â Â Â Â Â Â Â Â label =>Â $app->translate("Template Backups"), |
|---|
| 674 | Â Â Â Â Â Â Â Â type =>Â 'backup', |
|---|
| 675 | Â Â Â Â Â Â Â Â order =>Â 100, |
|---|
| 676 | Â Â Â Â Â Â }, |
|---|
| 677 | Â Â Â Â ); |
|---|
| 678 | Â Â } |
|---|
| 679 |   my @types = sort { $types{$a}->{order} <=> $types{$b}->{order} } keys %types; |
|---|
| 680 |   if ($template_type) { |
|---|
| 681 |     @types = ( $template_type ); |
|---|
| 682 | Â Â } |
|---|
| 683 |   $app->delete_param('filter_key') if $filter; |
|---|
| 684 |   foreach my $tmpl_type (@types) { |
|---|
| 685 |     if ( $tmpl_type eq 'index' ) { |
|---|
| 686 |       $app->param( 'filter_key', 'index_templates' ); |
|---|
| 687 | Â Â Â Â } |
|---|
| 688 |     elsif ( $tmpl_type eq 'archive' ) { |
|---|
| 689 |       $app->param( 'filter_key', 'archive_templates' ); |
|---|
| 690 | Â Â Â Â } |
|---|
| 691 |     elsif ( $tmpl_type eq 'system' ) { |
|---|
| 692 |       $app->param( 'filter_key', 'system_templates' ); |
|---|
| 693 | Â Â Â Â } |
|---|
| 694 |     elsif ( $tmpl_type eq 'email' ) { |
|---|
| 695 |       $app->param( 'filter_key', 'email_templates' ); |
|---|
| 696 | Â Â Â Â } |
|---|
| 697 |     elsif ( $tmpl_type eq 'module' ) { |
|---|
| 698 |       $app->param( 'filter_key', 'module_templates' ); |
|---|
| 699 | Â Â Â Â } |
|---|
| 700 | Â Â Â Â $terms->{type}Â =Â $types{$tmpl_type}->{type}; |
|---|
| 701 |     my $tmpl_param = $app->listing( |
|---|
| 702 | Â Â Â Â Â Â { |
|---|
| 703 |         type   => 'template', |
|---|
| 704 |         terms  => $terms, |
|---|
| 705 |         args   => $args, |
|---|
| 706 | Â Â Â Â Â Â Â Â no_limit =>Â 1, |
|---|
| 707 |         no_html => 1, |
|---|
| 708 |         code   => $hasher, |
|---|
| 709 | Â Â Â Â Â Â } |
|---|
| 710 | Â Â Â Â ); |
|---|
| 711 | |
|---|
| 712 |     my $template_type_label = $types{$tmpl_type}->{label}; |
|---|
| 713 | Â Â Â Â $tmpl_param->{template_type}Â =Â $tmpl_type; |
|---|
| 714 | Â Â Â Â $tmpl_param->{template_type_label}Â =Â $template_type_label; |
|---|
| 715 |     push @tmpl_loop, $tmpl_param; |
|---|
| 716 | Â Â } |
|---|
| 717 |   if ($filter) { |
|---|
| 718 | Â Â Â Â $params->{filter_key}Â =Â $filter; |
|---|
| 719 | Â Â Â Â $params->{filter_label}Â =Â $types{$template_type}{label} |
|---|
| 720 |       if exists $types{$template_type}; |
|---|
| 721 |     $app->param('filter_key', $filter); |
|---|
| 722 |   } else { |
|---|
| 723 | Â Â Â Â # restore filter_key param (we modified it for the |
|---|
| 724 | Â Â Â Â # sake of the individual table listings) |
|---|
| 725 | Â Â Â Â $app->delete_param('filter_key'); |
|---|
| 726 | Â Â } |
|---|
| 727 | |
|---|
| 728 | Â Â $params->{template_type_loop}Â =Â \@tmpl_loop; |
|---|
| 729 | Â Â $params->{screen_id}Â =Â "list-template"; |
|---|
| 730 | |
|---|
| 731 |   return $app->load_tmpl('list_template.tmpl', $params); |
|---|
| 732 | } |
|---|
| 733 | |
|---|
| 734 | sub preview { |
|---|
| 735 |   my $app     = shift; |
|---|
| 736 |   my $q      = $app->param; |
|---|
| 737 |   my $blog_id   = $q->param('blog_id'); |
|---|
| 738 |   my $blog    = $app->blog; |
|---|
| 739 |   my $id     = $q->param('id'); |
|---|
| 740 |   my $tmpl; |
|---|
| 741 |   my $user_id = $app->user->id; |
|---|
| 742 | |
|---|
| 743 | Â Â # We can only do previews on blog templates. Have to publish |
|---|
| 744 | Â Â # the preview file somewhere! |
|---|
| 745 |   return $app->errtrans("Invalid request.") unless $blog; |
|---|
| 746 | |
|---|
| 747 |   require MT::Template; |
|---|
| 748 |   if ($id) { |
|---|
| 749 |     $tmpl = MT::Template->load( { id => $id, blog_id => $blog_id } ) |
|---|
| 750 |       or return $app->errtrans( "Invalid request." ); |
|---|
| 751 | Â Â } |
|---|
| 752 |   else { |
|---|
| 753 |     $tmpl = MT::Template->new; |
|---|
| 754 | Â Â Â Â $tmpl->id(-1); |
|---|
| 755 | Â Â Â Â $tmpl->blog_id($blog_id); |
|---|
| 756 | Â Â } |
|---|
| 757 | |
|---|
| 758 |   my $names = $tmpl->column_names; |
|---|
| 759 |   my %values = map { $_ => scalar $app->param($_) } @$names; |
|---|
| 760 |   delete $values{'id'} unless $q->param('id'); |
|---|
| 761 | |
|---|
| 762 | Â Â ## Strip linefeed characters. |
|---|
| 763 |   for my $col (qw( text )) { |
|---|
| 764 |     $values{$col} =~ tr/\r//d if $values{$col}; |
|---|
| 765 | Â Â } |
|---|
| 766 |   $tmpl->set_values( \%values ); |
|---|
| 767 | |
|---|
| 768 |   my $preview_basename = $app->preview_object_basename; |
|---|
| 769 | |
|---|
| 770 |   my $type = $tmpl->type; |
|---|
| 771 |   my $preview_tmpl = $tmpl; |
|---|
| 772 |   my $archive_file; |
|---|
| 773 |   my $archive_url; |
|---|
| 774 |   my %param; |
|---|
| 775 |   my $blog_path = $blog->site_path; |
|---|
| 776 |   my $blog_url = $blog->site_url; |
|---|
| 777 | |
|---|
| 778 |   if (($type eq 'custom') || ($type eq 'widget')) { |
|---|
| 779 | Â Â Â Â # determine 'host' template |
|---|
| 780 |     $preview_tmpl = MT::Template->load({ blog_id => $blog_id, identifier => 'main_index' }); |
|---|
| 781 |     if (!$preview_tmpl) { |
|---|
| 782 |       return $app->errtrans("Can't locate host template to preview module/widget."); |
|---|
| 783 | Â Â Â Â } |
|---|
| 784 |     my $req = $app->request; |
|---|
| 785 | Â Â Â Â # stash this module so that it is selected through a |
|---|
| 786 | Â Â Â Â # MTInclude tag instead of the one in the database: |
|---|
| 787 |     my $tmpl_name = $tmpl->name; |
|---|
| 788 |     $tmpl_name =~ s/^Widget: // if $type eq 'widget'; |
|---|
| 789 |     my $stash_id = 'template_' . $type . '::' . $blog_id . '::' . $tmpl_name; |
|---|
| 790 |     $req->stash($stash_id, [ $tmpl, $tmpl->tokens ]); |
|---|
| 791 |   } elsif (($type eq 'individual') || ($type eq 'page')) { |
|---|
| 792 |     my $ctx = $preview_tmpl->context; |
|---|
| 793 |     my $entry_type = $type eq 'individual' ? 'entry' : 'page'; |
|---|
| 794 |     my ($obj) = create_preview_content($app, $blog, $entry_type, 1); |
|---|
| 795 |     $obj->basename( $preview_basename ); |
|---|
| 796 |     $ctx->stash('entry', $obj); |
|---|
| 797 |     $ctx->{current_archive_type} = $type eq 'individual' ? 'Individual' : 'Page'; |
|---|
| 798 |     if (($type eq 'individual') && $blog->archive_path) { |
|---|
| 799 |       $blog_path = $blog->archive_path; |
|---|
| 800 |       $blog_url = $blog->archive_url; |
|---|
| 801 | Â Â Â Â } |
|---|
| 802 |     $archive_file = File::Spec->catfile( $blog_path, $obj->archive_file ); |
|---|
| 803 |     $archive_url = $obj->archive_url; |
|---|
| 804 |   } elsif ($type eq 'archive') { |
|---|
| 805 | Â Â Â Â # some variety of archive template |
|---|
| 806 |     my $ctx = $preview_tmpl->context; |
|---|
| 807 |     require MT::TemplateMap; |
|---|
| 808 |     my $map = MT::TemplateMap->load( { template_id => $id, is_preferred => 1 }); |
|---|
| 809 |     if (! $map) { |
|---|
| 810 |       return $app->error("Cannot preview without a template map!"); |
|---|
| 811 | Â Â Â Â } |
|---|
| 812 | Â Â Â Â $ctx->{current_archive_type}Â =Â $map->archive_type; |
|---|
| 813 |     my $archiver = MT->publisher->archiver( $map->archive_type ); |
|---|
| 814 |     my @entries = create_preview_content($app, $blog, $archiver->entry_class, 10); |
|---|
| 815 |     if ($archiver->date_based) { |
|---|
| 816 | Â Â Â Â Â Â $ctx->{current_timestamp}Â =Â $entries[0]->authored_on; |
|---|
| 817 | Â Â Â Â Â Â $ctx->{current_timestamp_end}Â =Â $entries[$#entries]->authored_on; |
|---|
| 818 | Â Â Â Â } |
|---|
| 819 |     if ($archiver->author_based) { |
|---|
| 820 |       $ctx->stash('author', $app->user); |
|---|
| 821 | Â Â Â Â } |
|---|
| 822 |     my $cat; |
|---|
| 823 |     if ($archiver->category_based) { |
|---|
| 824 |       $cat = new MT::Category; |
|---|
| 825 | Â Â Â Â Â Â $cat->label($app->translate("Preview")); |
|---|
| 826 | Â Â Â Â Â Â $cat->basename("preview"); |
|---|
| 827 | Â Â Â Â Â Â $cat->parent(0); |
|---|
| 828 |       $ctx->stash('archive_category', $cat); |
|---|
| 829 | Â Â Â Â } |
|---|
| 830 |     $ctx->stash('entries', \@entries); |
|---|
| 831 | |
|---|
| 832 |     my $file = MT->publisher->archive_file_for( $entries[0], $blog, $map->archive_type, $cat, $map, $ctx->{current_timestamp}, $app->user); |
|---|
| 833 |     $archive_file = File::Spec->catfile( $blog_path, $file ); |
|---|
| 834 |     $archive_url = MT::Util::caturl( $blog_url, $file ); |
|---|
| 835 |   } elsif ($type eq 'index') { |
|---|
| 836 |   } else { |
|---|
| 837 | Â Â Â Â # for now, only index templates can be previewed |
|---|
| 838 |     return $app->errtrans("Invalid request."); |
|---|
| 839 | Â Â } |
|---|
| 840 | |
|---|
| 841 |   my $orig_file; |
|---|
| 842 |   my $path; |
|---|
| 843 | |
|---|
| 844 | Â Â # Default case; works for index templates (other template types should |
|---|
| 845 | Â Â # have defined $archive_file by now). |
|---|
| 846 |   $archive_file = File::Spec->catfile( $blog_path, $preview_tmpl->outfile ) |
|---|
| 847 |     unless defined $archive_file; |
|---|
| 848 | |
|---|
| 849 |   ( $orig_file, $path ) = File::Basename::fileparse( $archive_file ); |
|---|
| 850 | |
|---|
| 851 |   $archive_url = MT::Util::caturl( $blog_url, $orig_file ) |
|---|
| 852 |     unless defined $archive_url; |
|---|
| 853 | |
|---|
| 854 |   my $file_ext; |
|---|
| 855 |   require File::Basename; |
|---|
| 856 |   $file_ext = $archive_file; |
|---|
| 857 |   if ($file_ext =~ m/\.[a-z]+$/) { |
|---|
| 858 |     $file_ext =~ s!.+\.!.!; |
|---|
| 859 |   } else { |
|---|
| 860 |     $file_ext = ''; |
|---|
| 861 | Â Â } |
|---|
| 862 |   $archive_file = File::Spec->catfile( $path, $preview_basename . $file_ext ); |
|---|
| 863 | |
|---|
| 864 |   my @data; |
|---|
| 865 |   $app->run_callbacks( 'cms_pre_preview.template', $app, $preview_tmpl, \@data ); |
|---|
| 866 | |
|---|
| 867 |   my $has_hires = eval 'require Time::HiRes; 1' ? 1 : 0; |
|---|
| 868 |   my $start_time = $has_hires ? Time::HiRes::time() : time; |
|---|
| 869 | |
|---|
| 870 |   my $ctx = $preview_tmpl->context; |
|---|
| 871 |   $ctx->var('preview_template', 1); |
|---|
| 872 |   my $html = $preview_tmpl->output; |
|---|
| 873 | |
|---|
| 874 |   $param{build_time} = $has_hires ? sprintf("%.3f", Time::HiRes::time() - $start_time ) : "~" . ( time - $start_time ); |
|---|
| 875 | |
|---|
| 876 |   unless ( defined($html) ) { |
|---|
| 877 |     return $app->error( $app->translate( "Publish error: [_1]", |
|---|
| 878 | Â Â Â Â Â Â MT::Util::encode_html(Â $preview_tmpl->errstr )Â )Â ); |
|---|
| 879 | Â Â } |
|---|
| 880 | |
|---|
| 881 | Â Â # If MT is configured to do 'local' previews, convert all |
|---|
| 882 | Â Â # the normal blog URLs into the domain used by MT itself (ie, |
|---|
| 883 | Â Â # blog is published to www.example.com, which is a different |
|---|
| 884 | Â Â # server from where MT runs, mt.example.com; previews therefore |
|---|
| 885 | Â Â # should occur locally, so replace all http://www.example.com/ |
|---|
| 886 | Â Â # with http://mt.example.com/). |
|---|
| 887 |   my ($old_url, $new_url); |
|---|
| 888 |   if ($app->config('LocalPreviews')) { |
|---|
| 889 |     $old_url = $blog_url; |
|---|
| 890 |     $old_url =~ s!^(https?://[^/]+?/)(.*)?!$1!; |
|---|
| 891 |     $new_url = $app->base . '/'; |
|---|
| 892 |     $html =~ s!\Q$old_url\E!$new_url!g; |
|---|
| 893 | Â Â } |
|---|
| 894 | |
|---|
| 895 |   my $fmgr = $blog->file_mgr; |
|---|
| 896 | |
|---|
| 897 | Â Â ## Determine if we need to build directory structure, |
|---|
| 898 | Â Â ## and build it if we do. DirUmask determines |
|---|
| 899 | Â Â ## directory permissions. |
|---|
| 900 |   require File::Basename; |
|---|
| 901 |   $path =~ s!/$!! |
|---|
| 902 |    unless $path eq '/';  ## OS X doesn't like / at the end in mkdir(). |
|---|
| 903 |   unless ( $fmgr->exists($path) ) { |
|---|
| 904 | Â Â Â Â $fmgr->mkpath($path); |
|---|
| 905 | Â Â } |
|---|
| 906 | |
|---|
| 907 |   if ( $fmgr->exists($path) && $fmgr->can_write($path) ) { |
|---|
| 908 | Â Â Â Â $param{preview_file}Â =Â $preview_basename; |
|---|
| 909 |     my $preview_url = $archive_url; |
|---|
| 910 |     $preview_url =~ s! / \Q$orig_file\E ( /? ) $!/$preview_basename$file_ext$1!x; |
|---|
| 911 | |
|---|
| 912 | Â Â Â Â # We also have to translate the URL used for the |
|---|
| 913 | Â Â Â Â # published file to be on the MT app domain. |
|---|
| 914 |     if (defined $new_url) { |
|---|
| 915 |       $preview_url =~ s!^\Q$old_url\E!$new_url!; |
|---|
| 916 | Â Â Â Â } |
|---|
| 917 | |
|---|
| 918 | Â Â Â Â $param{preview_url}Â =Â $preview_url; |
|---|
| 919 | |
|---|
| 920 |     $fmgr->put_data( $html, $archive_file ); |
|---|
| 921 | |
|---|
| 922 | Â Â Â Â # we have to make a record of this preview just in case it |
|---|
| 923 | Â Â Â Â # isn't cleaned up by re-editing, saving or cancelling on |
|---|
| 924 | Â Â Â Â # by the user. |
|---|
| 925 |     require MT::Session; |
|---|
| 926 |     my $sess_obj = MT::Session->get_by_key( |
|---|
| 927 | Â Â Â Â Â Â { |
|---|
| 928 |         id  => $preview_basename, |
|---|
| 929 |         kind => 'TF',        # TF = Temporary File |
|---|
| 930 | Â Â Â Â Â Â Â Â name =>Â $archive_file, |
|---|
| 931 | Â Â Â Â Â Â } |
|---|
| 932 | Â Â Â Â ); |
|---|
| 933 | Â Â Â Â $sess_obj->start(time); |
|---|
| 934 | Â Â Â Â $sess_obj->save; |
|---|
| 935 | Â Â } |
|---|
| 936 |   else { |
|---|
| 937 |     return $app->error( $app->translate( |
|---|
| 938 |       "Unable to create preview file in this location: [_1]", $path ) ); |
|---|
| 939 | Â Â } |
|---|
| 940 | |
|---|
| 941 |   $param{id} = $id if $id; |
|---|
| 942 |   $param{new_object} = $param{id} ? 0 : 1; |
|---|
| 943 | Â Â $param{name}Â =Â $tmpl->name; |
|---|
| 944 |   my $cols = $tmpl->column_names; |
|---|
| 945 |   for my $col (@$cols) { |
|---|
| 946 |     push @data, |
|---|
| 947 | Â Â Â Â Â { |
|---|
| 948 |       data_name => $col, |
|---|
| 949 |       data_value => scalar $q->param($col) |
|---|
| 950 | Â Â Â Â Â }; |
|---|
| 951 | Â Â } |
|---|
| 952 | Â Â $param{template_loop}Â =Â \@data; |
|---|
| 953 | Â Â $param{object_type}Â =Â $type; |
|---|
| 954 |   return $app->load_tmpl( 'preview_template_strip.tmpl', \%param ); |
|---|
| 955 | } |
|---|
| 956 | |
|---|
| 957 | sub create_preview_content { |
|---|
| 958 |   my ($app, $blog, $type, $number) = @_; |
|---|
| 959 | |
|---|
| 960 |   my $blog_id = $blog->id; |
|---|
| 961 |   my $entry_class = $app->model($type); |
|---|
| 962 |   my @obj = $entry_class->load({ |
|---|
| 963 | Â Â Â Â blog_id =>Â $blog_id, |
|---|
| 964 | Â Â Â Â status =>Â MT::Entry::RELEASE() |
|---|
| 965 |   }, { |
|---|
| 966 |     limit => $number || 1, |
|---|
| 967 | Â Â Â Â direction =>Â 'descend', |
|---|
| 968 | Â Â Â Â 'sort'Â =>Â 'authored_on' |
|---|
| 969 | Â Â }); |
|---|
| 970 |   unless ( @obj ) { |
|---|
| 971 | Â Â Â Â # create a dummy object |
|---|
| 972 |     my $obj = $entry_class->new; |
|---|
| 973 | Â Â Â Â $obj->blog_id($blog_id); |
|---|
| 974 | Â Â Â Â $obj->id(-1); |
|---|
| 975 | Â Â Â Â $obj->author_id(Â $app->user->id ); |
|---|
| 976 | Â Â Â Â $obj->authored_on(Â $blog->current_timestamp ); |
|---|
| 977 | Â Â Â Â $obj->status(Â MT::Entry::RELEASE()Â ); |
|---|
| 978 | Â Â Â Â $obj->title($app->translate("Lorem ipsum")); |
|---|
| 979 |     my $preview_text = $app->translate('LOREM_IPSUM_TEXT'); |
|---|
| 980 |     if ($preview_text eq 'LOREM_IPSUM_TEXT') { |
|---|
| 981 |       $preview_text = q{Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut diam quam, accumsan eu, aliquam vel, ultrices a, augue. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Fusce hendrerit, lacus eget bibendum sollicitudin, mi tellus interdum neque, sit amet pretium tortor tellus id erat. Duis placerat justo ac erat. Duis posuere, risus eu elementum viverra, nisl lacus sagittis lorem, ac fermentum neque pede vitae arcu. Phasellus arcu elit, placerat eu, luctus posuere, tristique non, augue. In hac habitasse platea dictumst. Nunc non dolor et ipsum mattis malesuada. Praesent porta orci eu ligula. Ut dui augue, dapibus vitae, sodales in, lobortis non, felis. Aliquam feugiat mollis ipsum.}; |
|---|
| 982 | Â Â Â Â } |
|---|
| 983 |     my $preview_more = $app->translate('LORE_IPSUM_TEXT_MORE'); |
|---|
| 984 |     if ($preview_text eq 'LOREM_IPSUM_TEXT_MORE') { |
|---|
| 985 |       $preview_more = q{Integer nunc nulla, vulputate sit amet, varius ac, faucibus ac, lectus. Nulla semper bibendum justo. In hac habitasse platea dictumst. Aliquam auctor pretium ante. Etiam porta consectetuer erat. Phasellus consequat, nisi eu suscipit elementum, metus leo malesuada pede, vel scelerisque lorem ligula in augue. Sed aliquet. Donec malesuada metus sit amet sapien. Integer non libero. Morbi egestas, mauris posuere consequat sodales, augue lectus suscipit velit, eu commodo lacus dolor congue justo. Suspendisse justo. Curabitur sagittis, lorem tincidunt elementum rhoncus, odio dolor mattis odio, quis ultrices ligula ipsum ac lacus. Nam et sapien ac lacus ultrices sollicitudin. Vestibulum ut dolor nec dui malesuada imperdiet. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; |
|---|
| 986 | |
|---|
| 987 | Â Â Â Â Â Â Quisque pharetra libero quis nibh. Cras lacus orci, commodo et, fringilla non, lobortis non, mauris. Curabitur dui sapien, tristique imperdiet, ultrices vitae, gravida varius, ante. Maecenas ac arcu nec nibh euismod feugiat. Pellentesque sed orci eget enim egestas faucibus. Aenean laoreet leo ornare velit. Nunc fermentum dolor eget massa. Fusce fringilla, tellus in pellentesque sodales, urna mi hendrerit leo, vel adipiscing ligula odio sit amet risus. Cras rhoncus, mi et posuere gravida, purus sem porttitor nisl, auctor laoreet nisl turpis quis ligula. Aliquam in nisi tristique augue egestas lacinia. Aenean ante magna, facilisis a, faucibus at, aliquam laoreet, dui. Ut tellus leo, tristique a, pellentesque ac, bibendum non, ipsum. Curabitur eu neque pretium arcu accumsan tincidunt. Ut ipsum. Quisque congue accumsan elit. Nulla ligula felis, aliquam ultricies, vestibulum vestibulum, semper vel, sapien. Aenean sodales ligula venenatis tellus. Vestibulum leo. Morbi viverra convallis eros. |
|---|
| 988 | |
|---|
| 989 | Â Â Â Â Â Â Phasellus rhoncus pulvinar enim. Ut gravida ante nec lectus. Nam luctus gravida odio. Morbi vitae lorem vitae justo fermentum porttitor. Suspendisse vestibulum magna at purus. Cras nec sem. Duis id felis. Mauris hendrerit dapibus est. Donec semper. Praesent vehicula interdum velit. Ut sed tellus et diam venenatis pulvinar.}; |
|---|
| 990 | Â Â Â Â } |
|---|
| 991 | Â Â Â Â $obj->text($preview_text); |
|---|
| 992 | Â Â Â Â $obj->text_more($preview_more); |
|---|
| 993 | Â Â Â Â $obj->keywords(MT->translate("sample, entry, preview")); |
|---|
| 994 | Â Â Â Â $obj->tags(qw( lorem ipsum sample preview )); |
|---|
| 995 |     @obj = ($obj); |
|---|
| 996 | Â Â } |
|---|
| 997 |   return @obj; |
|---|
| 998 | } |
|---|
| 999 | |
|---|
| 1000 | sub reset_blog_templates { |
|---|
| 1001 |   my $app  = shift; |
|---|
| 1002 |   my $q   = $app->param; |
|---|
| 1003 |   my $perms = $app->permissions |
|---|
| 1004 |    or return $app->error( $app->translate("No permissions") ); |
|---|
| 1005 |   return $app->error( $app->translate("Permission denied.") ) |
|---|
| 1006 |    unless $perms->can_edit_templates; |
|---|
| 1007 |   $app->validate_magic() or return; |
|---|
| 1008 |   my $blog = MT::Blog->load( $perms->blog_id ) |
|---|
| 1009 |     or return $app->error($app->translate('Can\'t load blog #[_1].', $perms->blog_id)); |
|---|
| 1010 |   require MT::Template; |
|---|
| 1011 |   my @tmpl = MT::Template->load( { blog_id => $blog->id } ); |
|---|
| 1012 | |
|---|
| 1013 |   for my $tmpl (@tmpl) { |
|---|
| 1014 |     $tmpl->remove or return $app->error( $tmpl->errstr ); |
|---|
| 1015 | Â Â } |
|---|
| 1016 |   my $set = $blog ? $blog->template_set : undef; |
|---|
| 1017 |   require MT::DefaultTemplates; |
|---|
| 1018 |   my $tmpl_list = MT::DefaultTemplates->templates($set) || []; |
|---|
| 1019 |   my @arch_tmpl; |
|---|
| 1020 |   for my $val (@$tmpl_list) { |
|---|
| 1021 | Â Â Â Â $val->{name}Â =Â $app->translate(Â $val->{name}Â ); |
|---|
| 1022 | Â Â Â Â $val->{text}Â =Â $app->translate_templatized(Â $val->{text}Â ); |
|---|
| 1023 |     my $tmpl = MT::Template->new; |
|---|
| 1024 |     if ( ( 'widgetset' eq $val->{type} ) |
|---|
| 1025 |      && ( exists $val->{modulesets} ) ) { |
|---|
| 1026 |       my $modulesets = delete $val->{modulesets}; |
|---|
| 1027 |       $tmpl->modulesets( join ',', @$modulesets ); |
|---|
| 1028 | Â Â Â Â } |
|---|
| 1029 | Â Â Â Â $tmpl->set_values($val); |
|---|
| 1030 | Â Â Â Â $tmpl->build_dynamic(0); |
|---|
| 1031 | Â Â Â Â $tmpl->blog_id(Â $blog->id ); |
|---|
| 1032 | Â Â Â Â $tmpl->save |
|---|
| 1033 |      or return $app->error( |
|---|
| 1034 | Â Â Â Â Â Â $app->translate( |
|---|
| 1035 | Â Â Â Â Â Â Â Â "Populating blog with default templates failed: [_1]", |
|---|
| 1036 | Â Â Â Â Â Â Â Â $tmpl->errstr |
|---|
| 1037 | Â Â Â Â Â Â ) |
|---|
| 1038 | Â Â Â Â Â ); |
|---|
| 1039 | |
|---|
| 1040 | Â Â Â Â # FIXME: enumeration of types |
|---|
| 1041 |     if (  $val->{type} eq 'archive' |
|---|
| 1042 |       || $val->{type} eq 'category' |
|---|
| 1043 |       || $val->{type} eq 'page' |
|---|
| 1044 |       || $val->{type} eq 'individual' ) |
|---|
| 1045 | Â Â Â Â { |
|---|
| 1046 |       push @arch_tmpl, $tmpl; |
|---|
| 1047 | Â Â Â Â } |
|---|
| 1048 | Â Â } |
|---|
| 1049 | |
|---|
| 1050 | Â Â ## Set up mappings from new templates to archive types. |
|---|
| 1051 |   for my $tmpl (@arch_tmpl) { |
|---|
| 1052 |     my (@at); |
|---|
| 1053 | |
|---|
| 1054 | Â Â Â Â # FIXME: enumeration of types |
|---|
| 1055 |     if ( $tmpl->type eq 'archive' ) { |
|---|
| 1056 |       @at = qw( Daily Weekly Monthly Category ); |
|---|
| 1057 | Â Â Â Â } |
|---|
| 1058 |     elsif ( $tmpl->type eq 'page' ) { |
|---|
| 1059 |       @at = qw( Page ); |
|---|
| 1060 | Â Â Â Â } |
|---|
| 1061 |     elsif ( $tmpl->type eq 'individual' ) { |
|---|
| 1062 |       @at = qw( Individual ); |
|---|
| 1063 | Â Â Â Â } |
|---|
| 1064 |     require MT::TemplateMap; |
|---|
| 1065 |     for my $at (@at) { |
|---|
| 1066 |       my $map = MT::TemplateMap->new; |
|---|
| 1067 | Â Â Â Â Â Â $map->archive_type($at); |
|---|
| 1068 | Â Â Â Â Â Â $map->is_preferred(1); |
|---|
| 1069 | Â Â Â Â Â Â $map->template_id(Â $tmpl->id ); |
|---|
| 1070 | Â Â Â Â Â Â $map->blog_id(Â $tmpl->blog_id ); |
|---|
| 1071 | Â Â Â Â Â Â $map->save |
|---|
| 1072 |        or return $app->error( |
|---|
| 1073 | Â Â Â Â Â Â Â Â $app->translate( |
|---|
| 1074 | Â Â Â Â Â Â Â Â Â Â "Setting up mappings failed: [_1]", |
|---|
| 1075 | Â Â Â Â Â Â Â Â Â Â $map->errstr |
|---|
| 1076 | Â Â Â Â Â Â Â Â ) |
|---|
| 1077 | Â Â Â Â Â Â Â ); |
|---|
| 1078 | Â Â Â Â } |
|---|
| 1079 | Â Â } |
|---|
| 1080 | Â Â $app->redirect( |
|---|
| 1081 | Â Â Â Â $app->uri( |
|---|
| 1082 | Â Â Â Â Â Â 'mode'Â =>Â 'list', |
|---|
| 1083 | Â Â Â Â Â Â args => |
|---|
| 1084 |        { '_type' => 'template', blog_id => $blog->id, 'reset' => 1 } |
|---|
| 1085 | Â Â Â Â ) |
|---|
| 1086 | Â Â ); |
|---|
| 1087 | } |
|---|
| 1088 | |
|---|
| 1089 | sub _generate_map_table { |
|---|
| 1090 |   my $app = shift; |
|---|
| 1091 |   my ( $blog_id, $template_id ) = @_; |
|---|
| 1092 | |
|---|
| 1093 |   require MT::Template; |
|---|
| 1094 |   require MT::Blog; |
|---|
| 1095 |   my $blog   = MT::Blog->load($blog_id); |
|---|
| 1096 |   my $template = MT::Template->load($template_id); |
|---|
| 1097 |   my $tmpl   = $app->load_tmpl('include/archive_maps.tmpl'); |
|---|
| 1098 |   my $maps   = _populate_archive_loop( $app, $blog, $template ); |
|---|
| 1099 | Â Â $tmpl->param(Â object_type =>Â 'templatemap'Â ); |
|---|
| 1100 |   $tmpl->param( publish_queue_available => eval 'require List::Util; require Scalar::Util; 1;' ); |
|---|
| 1101 |   $tmpl->param( object_loop => $maps ) if @$maps; |
|---|
| 1102 |   my $html = $tmpl->output(); |
|---|
| 1103 | |
|---|
| 1104 |   if ( $html =~ m/<__trans / ) { |
|---|
| 1105 |     $html = $app->translate_templatized($html); |
|---|
| 1106 | Â Â } |
|---|
| 1107 | Â Â $html; |
|---|
| 1108 | } |
|---|
| 1109 | |
|---|
| 1110 | sub _populate_archive_loop { |
|---|
| 1111 |   my $app = shift; |
|---|
| 1112 |   my ( $blog, $obj ) = @_; |
|---|
| 1113 | |
|---|
| 1114 |   my $index = $app->config('IndexBasename'); |
|---|
| 1115 |   my $ext = $blog->file_extension || ''; |
|---|
| 1116 |   $ext = '.' . $ext if $ext ne ''; |
|---|
| 1117 | |
|---|
| 1118 |   require MT::TemplateMap; |
|---|
| 1119 |   my @tmpl_maps = MT::TemplateMap->load( { template_id => $obj->id } ); |
|---|
| 1120 |   my @maps; |
|---|
| 1121 |   my %types; |
|---|
| 1122 |   foreach my $map_obj (@tmpl_maps) { |
|---|
| 1123 |     my $map = {}; |
|---|
| 1124 | Â Â Â Â $map->{map_id}Â Â Â Â Â Â =Â $map_obj->id; |
|---|
| 1125 | Â Â Â Â $map->{map_is_preferred}Â =Â $map_obj->is_preferred; |
|---|
| 1126 | Â Â Â Â # publish options |
|---|
| 1127 | Â Â Â Â $map->{map_build_type}Â =Â $map_obj->build_type; |
|---|
| 1128 |     $map->{ 'map_build_type_' . ( $map_obj->build_type || 0 ) } = 1; |
|---|
| 1129 |     my ( $period, $interval ) = _get_schedule( $map_obj->build_interval ); |
|---|
| 1130 |     $map->{ 'map_schedule_period_' . $period } = 1 |
|---|
| 1131 |       if defined $period; |
|---|
| 1132 | Â Â Â Â $map->{map_schedule_interval}Â =Â $interval |
|---|
| 1133 |       if defined $interval; |
|---|
| 1134 | |
|---|
| 1135 |     my $at = $map->{archive_type} = $map_obj->archive_type; |
|---|
| 1136 | Â Â Â Â $types{$at}++; |
|---|
| 1137 |     $map->{ 'archive_type_preferred_' . $blog->archive_type_preferred } = 1 |
|---|
| 1138 |      if $blog->archive_type_preferred; |
|---|
| 1139 | Â Â Â Â $map->{file_template}Â =Â $map_obj->file_template |
|---|
| 1140 |      if $map_obj->file_template; |
|---|
| 1141 | |
|---|
| 1142 |     my $archiver = $app->publisher->archiver($at); |
|---|
| 1143 |     next unless $archiver; |
|---|
| 1144 | Â Â Â Â $map->{archive_label}Â =Â $archiver->archive_label; |
|---|
| 1145 |     my $tmpls   = $archiver->default_archive_templates; |
|---|
| 1146 |     my $tmpl_loop = []; |
|---|
| 1147 |     foreach (@$tmpls) { |
|---|
| 1148 |       my $name = $_->{label}; |
|---|
| 1149 |       $name =~ s/\.html$/$ext/; |
|---|
| 1150 |       $name =~ s/index$ext$/$index$ext/; |
|---|
| 1151 |       push @$tmpl_loop, |
|---|
| 1152 | Â Â Â Â Â Â Â { |
|---|
| 1153 |         name  => $name, |
|---|
| 1154 |         value  => $_->{template}, |
|---|
| 1155 | Â Â Â Â Â Â Â Â default =>Â (Â $_->{default}Â ||Â 0Â ), |
|---|
| 1156 | Â Â Â Â Â Â Â }; |
|---|
| 1157 | Â Â Â Â } |
|---|
| 1158 | |
|---|
| 1159 |     my $custom = 1; |
|---|
| 1160 | |
|---|
| 1161 |     foreach (@$tmpl_loop) { |
|---|
| 1162 |       if (  ( !$map->{file_template} && $_->{default} ) |
|---|
| 1163 |         || ( $map->{file_template} eq $_->{value} ) ) |
|---|
| 1164 | Â Â Â Â Â Â { |
|---|
| 1165 | Â Â Â Â Â Â Â Â $_->{selected}Â Â Â Â =Â 1; |
|---|
| 1166 |         $custom        = 0; |
|---|
| 1167 | Â Â Â Â Â Â Â Â $map->{file_template}Â =Â $_->{value} |
|---|
| 1168 |          if !$map->{file_template}; |
|---|
| 1169 | Â Â Â Â Â Â } |
|---|
| 1170 | Â Â Â Â } |
|---|
| 1171 |     if ($custom) { |
|---|
| 1172 |       unshift @$tmpl_loop, |
|---|
| 1173 | Â Â Â Â Â Â Â { |
|---|
| 1174 |         name   => $map->{file_template}, |
|---|
| 1175 |         value  => $map->{file_template}, |
|---|
| 1176 | Â Â Â Â Â Â Â Â selected =>Â 1, |
|---|
| 1177 | Â Â Â Â Â Â Â }; |
|---|
| 1178 | Â Â Â Â } |
|---|
| 1179 | |
|---|
| 1180 | Â Â Â Â $map->{archive_tmpl_loop}Â =Â $tmpl_loop; |
|---|
| 1181 |     if ( |
|---|
| 1182 | Â Â Â Â Â Â 1Â <Â MT::TemplateMap->count( |
|---|
| 1183 |         { archive_type => $at, blog_id => $obj->blog_id } |
|---|
| 1184 | Â Â Â Â Â Â ) |
|---|
| 1185 | Â Â Â Â Â ) |
|---|
| 1186 | Â Â Â Â { |
|---|
| 1187 | Â Â Â Â Â Â $map->{has_multiple_archives}Â =Â 1; |
|---|
| 1188 | Â Â Â Â } |
|---|
| 1189 | |
|---|
| 1190 |     push @maps, $map; |
|---|
| 1191 | Â Â } |
|---|
| 1192 |   @maps = sort { MT::App::CMS::archive_type_sorter( $a, $b ) } @maps; |
|---|
| 1193 |   return \@maps; |
|---|
| 1194 | } |
|---|
| 1195 | |
|---|
| 1196 | sub delete_map { |
|---|
| 1197 |   my $app = shift; |
|---|
| 1198 |   $app->validate_magic() or return; |
|---|
| 1199 |   my $perms = $app->{perms} |
|---|
| 1200 |    or return $app->error( $app->translate("No permissions") ); |
|---|
| 1201 |   my $q = $app->param; |
|---|
| 1202 |   my $id = $q->param('id'); |
|---|
| 1203 | |
|---|
| 1204 |   require MT::TemplateMap; |
|---|
| 1205 |   MT::TemplateMap->remove( { id => $id } ); |
|---|
| 1206 |   my $html = |
|---|
| 1207 |    _generate_map_table( $app, $q->param('blog_id'), |
|---|
| 1208 | Â Â Â Â $q->param('template_id')Â ); |
|---|
| 1209 | Â Â $app->{no_print_body}Â =Â 1; |
|---|
| 1210 | Â Â $app->send_http_header("text/plain"); |
|---|
| 1211 | Â Â $app->print($html); |
|---|
| 1212 | } |
|---|
| 1213 | |
|---|
| 1214 | sub add_map { |
|---|
| 1215 |   my $app = shift; |
|---|
| 1216 |   $app->validate_magic() or return; |
|---|
| 1217 |   my $perms = $app->{perms} |
|---|
| 1218 |    or return $app->error( $app->translate("No permissions") ); |
|---|
| 1219 | |
|---|
| 1220 |   my $q = $app->param; |
|---|
| 1221 | |
|---|
| 1222 |   require MT::TemplateMap; |
|---|
| 1223 |   my $blog_id = $q->param('blog_id'); |
|---|
| 1224 |   my $at   = $q->param('new_archive_type'); |
|---|
| 1225 |   my $exist  = MT::TemplateMap->exist( |
|---|
| 1226 | Â Â Â Â { |
|---|
| 1227 |       blog_id   => $blog_id, |
|---|
| 1228 | Â Â Â Â Â Â archive_type =>Â $at |
|---|
| 1229 | Â Â Â Â } |
|---|
| 1230 | Â Â ); |
|---|
| 1231 |   my $map = MT::TemplateMap->new; |
|---|
| 1232 |   $map->is_preferred( $exist ? 0 : 1 ); |
|---|
| 1233 |   $map->template_id( scalar $q->param('template_id') ); |
|---|
| 1234 | Â Â $map->blog_id($blog_id); |
|---|
| 1235 | Â Â $map->archive_type($at); |
|---|
| 1236 | Â Â $map->save |
|---|
| 1237 |    or return $app->error( |
|---|
| 1238 |     $app->translate( "Saving map failed: [_1]", $map->errstr ) ); |
|---|
| 1239 |   my $html = |
|---|
| 1240 |    _generate_map_table( $app, $blog_id, scalar $q->param('template_id') ); |
|---|
| 1241 | Â Â $app->{no_print_body}Â =Â 1; |
|---|
| 1242 | Â Â $app->send_http_header("text/plain"); |
|---|
| 1243 | Â Â $app->print($html); |
|---|
| 1244 | } |
|---|
| 1245 | |
|---|
| 1246 | sub can_view { |
|---|
| 1247 |   my ( $eh, $app, $id ) = @_; |
|---|
| 1248 |   my $perms = $app->permissions; |
|---|
| 1249 |   return !$id || ($perms && $perms->can_edit_templates) || (!$app->blog && $app->user->can_edit_templates); |
|---|
| 1250 | } |
|---|
| 1251 | |
|---|
| 1252 | sub can_save { |
|---|
| 1253 |   my ( $eh, $app, $id ) = @_; |
|---|
| 1254 |   my $perms = $app->permissions; |
|---|
| 1255 |   return ($perms && $perms->can_edit_templates) || (!$perms && $app->user->can_edit_templates); |
|---|
| 1256 | } |
|---|
| 1257 | |
|---|
| 1258 | sub can_delete { |
|---|
| 1259 |   my ( $eh, $app, $obj ) = @_; |
|---|
| 1260 |   return 1 if $app->user->is_superuser(); |
|---|
| 1261 |   my $perms = $app->permissions; |
|---|
| 1262 |   return ($perms && $perms->can_edit_templates) || (!$perms && $app->user->can_edit_templates); |
|---|
| 1263 | } |
|---|
| 1264 | |
|---|
| 1265 | sub pre_save { |
|---|
| 1266 |   my $eh = shift; |
|---|
| 1267 |   my ( $app, $obj ) = @_; |
|---|
| 1268 | |
|---|
| 1269 | Â Â ## Strip linefeed characters. |
|---|
| 1270 |   ( my $text = $obj->text ) =~ tr/\r//d; |
|---|
| 1271 | |
|---|
| 1272 |   if ($text =~ m/<(MT|_)_trans/i) { |
|---|
| 1273 |     $text = $app->translate_templatized($text); |
|---|
| 1274 | Â Â } |
|---|
| 1275 | |
|---|
| 1276 | Â Â $obj->text($text); |
|---|
| 1277 | |
|---|
| 1278 | Â Â # update text heights if necessary |
|---|
| 1279 |   if ( my $perms = $app->permissions ) { |
|---|
| 1280 |     my $prefs = $perms->template_prefs || ''; |
|---|
| 1281 |     my $text_height = $app->param('text_height'); |
|---|
| 1282 |     if ( defined $text_height ) { |
|---|
| 1283 |       my ($pref_text_height) = $prefs =~ m/\btext:(\d+)\b/; |
|---|
| 1284 |       $pref_text_height ||= 0; |
|---|
| 1285 |       if ( $text_height != $pref_text_height ) { |
|---|
| 1286 |         if ( $prefs =~ m/\btext\b/ ) { |
|---|
| 1287 |           $prefs =~ s/\btext(:\d+)\b/text:$text_height/; |
|---|
| 1288 | Â Â Â Â Â Â Â Â } |
|---|
| 1289 |         else { |
|---|
| 1290 |           $prefs = 'text:' . $text_height . ',' . $prefs; |
|---|
| 1291 | Â Â Â Â Â Â Â Â } |
|---|
| 1292 | Â Â Â Â Â Â } |
|---|
| 1293 | Â Â Â Â } |
|---|
| 1294 | |
|---|
| 1295 |     if ( $prefs ne ( $perms->template_prefs || '' ) ) { |
|---|
| 1296 | Â Â Â Â Â Â $perms->template_prefs($prefs); |
|---|
| 1297 | Â Â Â Â Â Â $perms->save; |
|---|
| 1298 | Â Â Â Â } |
|---|
| 1299 | Â Â } |
|---|
| 1300 | |
|---|
| 1301 | Â Â # module caching |
|---|
| 1302 |   $obj->include_with_ssi( $app->param('include_with_ssi') ? 1 : 0 ); |
|---|
| 1303 | Â Â $obj->cache_path(Â $app->param('cache_path')); |
|---|
| 1304 |   my $cache_expire_type = $app->param('cache_expire_type') || ''; |
|---|
| 1305 | Â Â $obj->cache_expire_type($cache_expire_type); |
|---|
| 1306 |   my $period  = $app->param('cache_expire_period'); |
|---|
| 1307 |   my $interval = $app->param('cache_expire_interval'); |
|---|
| 1308 |   my $sec   = _get_interval( $period, $interval ); |
|---|
| 1309 |   $obj->cache_expire_interval($sec) if defined $sec; |
|---|
| 1310 |   my $q = $app->param; |
|---|
| 1311 |   my @events; |
|---|
| 1312 | |
|---|
| 1313 |   foreach my $name ( $q->param('cache_expire_event') ) { |
|---|
| 1314 |     push @events, $name; |
|---|
| 1315 | Â Â } |
|---|
| 1316 |   $obj->cache_expire_event( join ',', @events ) if $#events >= 0; |
|---|
| 1317 |   if ( $cache_expire_type == 1 ) { |
|---|
| 1318 |     return $eh->error( |
|---|
| 1319 | Â Â Â Â Â Â $app->translate("You should not be able to enter 0 as the time.")Â ) |
|---|
| 1320 |      if $interval == 0; |
|---|
| 1321 | Â Â } |
|---|
| 1322 |   elsif ( $cache_expire_type == 2 ) { |
|---|
| 1323 |     return $eh->error( |
|---|
| 1324 | Â Â Â Â Â Â $app->translate("You must select at least one event checkbox.")Â ) |
|---|
| 1325 |      if !@events; |
|---|
| 1326 | Â Â } |
|---|
| 1327 | |
|---|
| 1328 |   require MT::PublishOption; |
|---|
| 1329 |   my $build_type = $app->param('build_type'); |
|---|
| 1330 | |
|---|
| 1331 |   if ( $build_type == MT::PublishOption::SCHEDULED() ) { |
|---|
| 1332 |     my $period  = $app->param('schedule_period'); |
|---|
| 1333 |     my $interval = $app->param('schedule_interval'); |
|---|
| 1334 |     my $sec   = _get_interval( $period, $interval ); |
|---|
| 1335 | Â Â Â Â $obj->build_interval($sec); |
|---|
| 1336 | Â Â } |
|---|
| 1337 |   my $rebuild_me = 1; |
|---|
| 1338 |   if (  $build_type == MT::PublishOption::DISABLED() |
|---|
| 1339 |     || $build_type == MT::PublishOption::MANUALLY() ) |
|---|
| 1340 | Â Â { |
|---|
| 1341 |     $rebuild_me = 0; |
|---|
| 1342 | Â Â } |
|---|
| 1343 | Â Â $obj->rebuild_me($rebuild_me); |
|---|
| 1344 | Â Â 1; |
|---|
| 1345 | } |
|---|
| 1346 | |
|---|
| 1347 | sub post_save { |
|---|
| 1348 |   my $eh = shift; |
|---|
| 1349 |   my ( $app, $obj, $original ) = @_; |
|---|
| 1350 | |
|---|
| 1351 |   my $sess_obj = $app->autosave_session_obj; |
|---|
| 1352 |   $sess_obj->remove if $sess_obj; |
|---|
| 1353 | |
|---|
| 1354 |   my $dynamic = 0; |
|---|
| 1355 |   my $q = $app->param; |
|---|
| 1356 |   my $type = $q->param('type'); |
|---|
| 1357 | Â Â # FIXME: enumeration of types |
|---|
| 1358 |   if ( $type eq 'custom' |
|---|
| 1359 |    || $type eq 'index' |
|---|
| 1360 |    || $type eq 'widget' |
|---|
| 1361 |    || $type eq 'widgetset' ) |
|---|
| 1362 | Â Â { |
|---|
| 1363 |     $dynamic = $obj->build_dynamic; |
|---|
| 1364 | Â Â } |
|---|
| 1365 | Â Â else |
|---|
| 1366 | Â Â { |
|---|
| 1367 | Â Â Â Â # archive template specific post_save tasks |
|---|
| 1368 |     require MT::TemplateMap; |
|---|
| 1369 |     my @p = $q->param; |
|---|
| 1370 |     for my $p (@p) { |
|---|
| 1371 |       my $map; |
|---|
| 1372 |       if ( $p =~ /^archive_tmpl_preferred_(\w+)_(\d+)$/ ) { |
|---|
| 1373 |         my $at   = $1; |
|---|
| 1374 |         my $map_id = $2; |
|---|
| 1375 |         $map  = MT::TemplateMap->load($map_id) |
|---|
| 1376 |           or next; |
|---|
| 1377 | Â Â Â Â Â Â Â Â $map->prefer(Â $q->param($p)Â );Â Â # prefer method saves in itself |
|---|
| 1378 | Â Â Â Â Â Â } |
|---|
| 1379 |       elsif ( $p =~ /^archive_file_tmpl_(\d+)$/ ) { |
|---|
| 1380 |         my $map_id = $1; |
|---|
| 1381 |         $map  = MT::TemplateMap->load($map_id) |
|---|
| 1382 |           or next; |
|---|
| 1383 | Â Â Â Â Â Â Â Â $map->file_template(Â $q->param($p)Â ); |
|---|
| 1384 | Â Â Â Â Â Â Â Â $map->save; |
|---|
| 1385 | Â Â Â Â Â Â } |
|---|
| 1386 |       elsif ( $p =~ /^map_build_type_(\d+)$/ ) { |
|---|
| 1387 |         my $map_id   = $1; |
|---|
| 1388 |         $map    = MT::TemplateMap->load($map_id) |
|---|
| 1389 |           or next; |
|---|
| 1390 |         my $build_type = $q->param($p); |
|---|
| 1391 |         require MT::PublishOption; |
|---|
| 1392 | Â Â Â Â Â Â Â Â $map->build_type($build_type); |
|---|
| 1393 |         if ( $build_type == MT::PublishOption::SCHEDULED() ) { |
|---|
| 1394 |           my $period  = $q->param( 'map_schedule_period_' . $map_id ); |
|---|
| 1395 |           my $interval = $q->param( 'map_schedule_interval_' . $map_id ); |
|---|
| 1396 |           my $sec   = _get_interval( $period, $interval ); |
|---|
| 1397 | Â Â Â Â Â Â Â Â Â Â $map->build_interval($sec); |
|---|
| 1398 | Â Â Â Â Â Â Â Â } |
|---|
| 1399 | Â Â Â Â Â Â Â Â $map->save; |
|---|
| 1400 | Â Â Â Â Â Â } |
|---|
| 1401 |       if ( !$dynamic |
|---|
| 1402 |        && $map && $map->build_type == MT::PublishOption::DYNAMIC() ) |
|---|
| 1403 | Â Â Â Â Â Â { |
|---|
| 1404 |         $dynamic = 1; |
|---|
| 1405 | Â Â Â Â Â Â } |
|---|
| 1406 | Â Â Â Â } |
|---|
| 1407 | Â Â } |
|---|
| 1408 | |
|---|
| 1409 |   if ( !$original->id ) { |
|---|
| 1410 | Â Â Â Â $app->log( |
|---|
| 1411 | Â Â Â Â Â Â { |
|---|
| 1412 | Â Â Â Â Â Â Â Â message =>Â $app->translate( |
|---|
| 1413 | Â Â Â Â Â Â Â Â Â Â "Template '[_1]' (ID:[_2]) created by '[_3]'", |
|---|
| 1414 |           $obj->name, $obj->id, $app->user->name |
|---|
| 1415 | Â Â Â Â Â Â Â Â ), |
|---|
| 1416 |         level  => MT::Log::INFO(), |
|---|
| 1417 |         class  => 'template', |
|---|
| 1418 | Â Â Â Â Â Â Â Â category =>Â 'new', |
|---|
| 1419 | Â Â Â Â Â Â } |
|---|
| 1420 | Â Â Â Â ); |
|---|
| 1421 | Â Â } |
|---|
| 1422 | |
|---|
| 1423 |   if ( $dynamic ) { |
|---|
| 1424 |     if ( $obj->type eq 'index' ) { |
|---|
| 1425 | Â Â Â Â Â Â $app->rebuild_indexes( |
|---|
| 1426 | Â Â Â Â Â Â Â Â BlogIDÂ Â =>Â $obj->blog_id, |
|---|
| 1427 | Â Â Â Â Â Â Â Â Template =>Â $obj, |
|---|
| 1428 | Â Â Â Â Â Â Â Â NoStatic =>Â 1, |
|---|
| 1429 |       ) or return $app->publish_error();  # XXXX |
|---|
| 1430 | Â Â Â Â } |
|---|
| 1431 |     if ( my $blog = $app->blog ) { |
|---|
| 1432 |       require MT::CMS::Blog; |
|---|
| 1433 |       my ( $path, $url ); |
|---|
| 1434 |       if ( $obj->type eq 'index' ) { |
|---|
| 1435 |         $path = $blog->site_path; |
|---|
| 1436 |         $url = $blog->site_url; |
|---|
| 1437 | Â Â Â Â Â Â } |
|---|
| 1438 |       else { |
|---|
| 1439 | Â Â Â Â Â Â Â Â # must be archive since other types can't be dynamic |
|---|
| 1440 |         if ( $path = $blog->archive_path ) { |
|---|
| 1441 |           $url = $blog->archive_url; |
|---|
| 1442 | Â Â Â Â Â Â Â Â } |
|---|
| 1443 |         else { |
|---|
| 1444 |           $path = $blog->site_path; |
|---|
| 1445 |           $url = $blog->site_url; |
|---|
| 1446 | Â Â Â Â Â Â Â Â } |
|---|
| 1447 | Â Â Â Â Â Â } |
|---|
| 1448 | Â Â Â Â Â Â # specific arguments so not to overwrite mtview and htaccess |
|---|
| 1449 | Â Â Â Â Â Â MT::CMS::Blog::prepare_dynamic_publishing( |
|---|
| 1450 |         $eh, |
|---|
| 1451 | Â Â Â Â Â Â Â Â $blog, |
|---|
| 1452 | Â Â Â Â Â Â Â Â undef, |
|---|
| 1453 | Â Â Â Â Â Â Â Â undef, |
|---|
| 1454 | Â Â Â Â Â Â Â Â $path, |
|---|
| 1455 | Â Â Â Â Â Â Â Â $url |
|---|
| 1456 | Â Â Â Â Â Â ); |
|---|
| 1457 | Â Â Â Â } |
|---|
| 1458 | Â Â } |
|---|
| 1459 | Â Â 1; |
|---|
| 1460 | } |
|---|
| 1461 | |
|---|
| 1462 | sub post_delete { |
|---|
| 1463 |   my ( $eh, $app, $obj ) = @_; |
|---|
| 1464 | |
|---|
| 1465 | Â Â $app->log( |
|---|
| 1466 | Â Â Â Â { |
|---|
| 1467 | Â Â Â Â Â Â message =>Â $app->translate( |
|---|
| 1468 | Â Â Â Â Â Â Â Â "Template '[_1]' (ID:[_2]) deleted by '[_3]'", |
|---|
| 1469 |         $obj->name, $obj->id, $app->user->name |
|---|
| 1470 | Â Â Â Â Â Â ), |
|---|
| 1471 |       level  => MT::Log::INFO(), |
|---|
| 1472 |       class  => 'system', |
|---|
| 1473 | Â Â Â Â Â Â category =>Â 'delete' |
|---|
| 1474 | Â Â Â Â } |
|---|
| 1475 | Â Â ); |
|---|
| 1476 | } |
|---|
| 1477 | |
|---|
| 1478 | sub build_template_table { |
|---|
| 1479 |   my $app = shift; |
|---|
| 1480 |   my (%args) = @_; |
|---|
| 1481 | |
|---|
| 1482 |   my $perms   = $app->permissions; |
|---|
| 1483 |   my $list_pref = $app->list_pref('template'); |
|---|
| 1484 |   my $limit   = $args{limit}; |
|---|
| 1485 |   my $param   = $args{param} || {}; |
|---|
| 1486 |   my $iter; |
|---|
| 1487 |   if ( $args{load_args} ) { |
|---|
| 1488 |     my $class = $app->model('template'); |
|---|
| 1489 |     $iter = $class->load_iter( @{ $args{load_args} } ); |
|---|
| 1490 | Â Â } |
|---|
| 1491 |   elsif ( $args{iter} ) { |
|---|
| 1492 |     $iter = $args{iter}; |
|---|
| 1493 | Â Â } |
|---|
| 1494 |   elsif ( $args{items} ) { |
|---|
| 1495 |     $iter = sub { pop @{ $args{items} } }; |
|---|
| 1496 |     $limit = scalar @{ $args{items} }; |
|---|
| 1497 | Â Â } |
|---|
| 1498 |   return [] unless $iter; |
|---|
| 1499 | |
|---|
| 1500 |   my @data; |
|---|
| 1501 |   my $i; |
|---|
| 1502 |   my %blogs; |
|---|
| 1503 |   while ( my $tmpl = $iter->() ) { |
|---|
| 1504 |     my $blog = $blogs{ $tmpl->blog_id } ||= |
|---|
| 1505 |      MT::Blog->load( $tmpl->blog_id ) if $tmpl->blog_id; |
|---|
| 1506 | |
|---|
| 1507 |     my $row = $tmpl->column_values; |
|---|
| 1508 |     $row->{name} = '' if !defined $row->{name}; |
|---|
| 1509 | Â Â Â Â $row->{name}Â =~Â s/^\s+|\s+$//g; |
|---|
| 1510 |     $row->{name} = "(" . $app->translate("No Name") . ")" |
|---|
| 1511 |      if $row->{name} eq ''; |
|---|
| 1512 |     my $published_url = $tmpl->published_url; |
|---|
| 1513 |     $row->{published_url} = $published_url if $published_url; |
|---|
| 1514 |     $row->{use_cache} = ( ($tmpl->cache_expire_type || 0) != 0 ) ? 1 : 0; |
|---|
| 1515 | |
|---|
| 1516 | Â Â Â Â # FIXME: enumeration of types |
|---|
| 1517 | Â Â Â Â $row->{can_delete}Â =Â 1 |
|---|
| 1518 |      if $tmpl->type =~ m/(custom|index|archive|page|individual|category|widget)/; |
|---|
| 1519 |     if ($blog) { |
|---|
| 1520 | Â Â Â Â Â Â $row->{weblog_name}Â =Â $blog->name; |
|---|
| 1521 | Â Â Â Â } |
|---|
| 1522 |     elsif ($tmpl->blog_id) { |
|---|
| 1523 |       $row->{weblog_name} = '* ' . $app->translate('Orphaned') . ' *'; |
|---|
| 1524 | Â Â Â Â } |
|---|
| 1525 |     else { |
|---|
| 1526 |       $row->{weblog_name} = '* ' . $app->translate('Global Templates') . ' *'; |
|---|
| 1527 | Â Â Â Â } |
|---|
| 1528 | Â Â Â Â $row->{object}Â =Â $tmpl; |
|---|
| 1529 |     push @data, $row; |
|---|
| 1530 |     last if defined($limit) && (@data > $limit); |
|---|
| 1531 | Â Â } |
|---|
| 1532 |   return [] unless @data; |
|---|
| 1533 | |
|---|
| 1534 | Â Â $param->{template_table}[0]Â Â Â Â Â Â Â =Â {%$list_pref}; |
|---|
| 1535 | Â Â $param->{template_table}[0]{object_loop}Â =Â \@data; |
|---|
| 1536 | Â Â $param->{template_table}[0]{object_type}Â =Â 'template'; |
|---|
| 1537 |   $app->load_list_actions( 'template', $param ); |
|---|
| 1538 | Â Â $param->{object_loop}Â =Â \@data; |
|---|
| 1539 | Â Â \@data; |
|---|
| 1540 | } |
|---|
| 1541 | |
|---|
| 1542 | sub dialog_publishing_profile { |
|---|
| 1543 |   my $app = shift; |
|---|
| 1544 |   $app->validate_magic or return; |
|---|
| 1545 | |
|---|
| 1546 |   my $blog = $app->blog; |
|---|
| 1547 |   $app->assert( $blog ) or return; |
|---|
| 1548 | |
|---|
| 1549 | Â Â # permission check |
|---|
| 1550 |   my $perms = $app->permissions; |
|---|
| 1551 |   return $app->errtrans("Permission denied.") |
|---|
| 1552 |     unless $app->user->is_superuser || |
|---|
| 1553 | Â Â Â Â Â Â $perms->can_administer_blog || |
|---|
| 1554 | Â Â Â Â Â Â $perms->can_edit_templates; |
|---|
| 1555 | |
|---|
| 1556 |   my $param = {}; |
|---|
| 1557 | Â Â $param->{dynamicity}Â =Â $blog->custom_dynamic_templates ||Â 'none'; |
|---|
| 1558 | Â Â $param->{screen_id}Â =Â "publishing-profile-dialog"; |
|---|
| 1559 | Â Â $param->{return_args}Â =Â $app->param('return_args'); |
|---|
| 1560 | |
|---|
| 1561 | Â Â $app->build_page('dialog/publishing_profile.tmpl', |
|---|
| 1562 | Â Â Â Â $param); |
|---|
| 1563 | } |
|---|
| 1564 | |
|---|
| 1565 | sub dialog_refresh_templates { |
|---|
| 1566 |   my $app = shift; |
|---|
| 1567 |   $app->validate_magic or return; |
|---|
| 1568 | |
|---|
| 1569 | Â Â # permission check |
|---|
| 1570 |   my $perms = $app->permissions; |
|---|
| 1571 |   return $app->errtrans("Permission denied.") |
|---|
| 1572 |     unless $app->user->is_superuser || |
|---|
| 1573 | Â Â Â Â Â Â $perms->can_administer_blog || |
|---|
| 1574 | Â Â Â Â Â Â $perms->can_edit_templates; |
|---|
| 1575 | |
|---|
| 1576 |   my $param = {}; |
|---|
| 1577 |   my $blog = $app->blog; |
|---|
| 1578 | Â Â $param->{return_args}Â =Â $app->param('return_args'); |
|---|
| 1579 | |
|---|
| 1580 |   if ($blog) { |
|---|
| 1581 | Â Â Â Â $param->{blog_id}Â =Â $blog->id; |
|---|
| 1582 | |
|---|
| 1583 |     my $sets = $app->registry("template_sets"); |
|---|
| 1584 |     $sets->{$_}{key} = $_ for keys %$sets; |
|---|
| 1585 |     $sets = $app->filter_conditional_list([ values %$sets ]); |
|---|
| 1586 | |
|---|
| 1587 |     no warnings; # some sets may not define an order |
|---|
| 1588 |     @$sets = sort { $a->{order} <=> $b->{order} } @$sets; |
|---|
| 1589 | Â Â Â Â $param->{'template_set_loop'}Â =Â $sets; |
|---|
| 1590 | |
|---|
| 1591 |     my $existing_set = $blog->template_set || 'mt_blog'; |
|---|
| 1592 |     foreach (@$sets) { |
|---|
| 1593 |       if ($_->{key} eq $existing_set) { |
|---|
| 1594 | Â Â Â Â Â Â Â Â $_->{selected}Â =Â 1; |
|---|
| 1595 | Â Â Â Â Â Â } |
|---|
| 1596 | Â Â Â Â } |
|---|
| 1597 | Â Â Â Â $param->{'template_set_index'}Â =Â $#$sets; |
|---|
| 1598 |     $param->{'template_set_count'} = scalar @$sets; |
|---|
| 1599 | |
|---|
| 1600 | Â Â Â Â $param->{template_sets}Â =Â $sets; |
|---|
| 1601 | Â Â Â Â $param->{screen_id}Â =Â "refresh-templates-dialog"; |
|---|
| 1602 | Â Â } |
|---|
| 1603 | |
|---|
| 1604 | Â Â # load template sets |
|---|
| 1605 | Â Â $app->build_page('dialog/refresh_templates.tmpl', |
|---|
| 1606 | Â Â Â Â $param); |
|---|
| 1607 | } |
|---|
| 1608 | |
|---|
| 1609 | sub refresh_all_templates { |
|---|
| 1610 |   my ($app) = @_; |
|---|
| 1611 | |
|---|
| 1612 |   my $backup = 0; |
|---|
| 1613 |   if ($app->param('backup')) { |
|---|
| 1614 | Â Â Â Â # refresh templates dialog uses a 'backup' field |
|---|
| 1615 |     $backup = 1; |
|---|
| 1616 | Â Â } |
|---|
| 1617 | |
|---|
| 1618 |   my $template_set = $app->param('template_set'); |
|---|
| 1619 |   my $refresh_type = $app->param('refresh_type') || 'refresh'; |
|---|
| 1620 | |
|---|
| 1621 |   my $t = time; |
|---|
| 1622 | |
|---|
| 1623 |   my @id; |
|---|
| 1624 |   if ($app->param('blog_id')) { |
|---|
| 1625 |     @id = ( scalar $app->param('blog_id') ); |
|---|
| 1626 | Â Â } |
|---|
| 1627 |   else { |
|---|
| 1628 |     @id = $app->param('id'); |
|---|
| 1629 |     if (! @id) { |
|---|
| 1630 | Â Â Â Â Â Â # refresh global templates |
|---|
| 1631 |       @id = ( 0 ); |
|---|
| 1632 | Â Â Â Â } |
|---|
| 1633 | Â Â } |
|---|
| 1634 | |
|---|
| 1635 |   require MT::Template; |
|---|
| 1636 |   require MT::DefaultTemplates; |
|---|
| 1637 |   require MT::Blog; |
|---|
| 1638 |   require MT::Permission; |
|---|
| 1639 |   require MT::Util; |
|---|
| 1640 | |
|---|
| 1641 |   foreach my $blog_id (@id) { |
|---|
| 1642 |     my $blog; |
|---|
| 1643 |     if ($blog_id) { |
|---|
| 1644 |       $blog = MT::Blog->load($blog_id); |
|---|
| 1645 |       next unless $blog; |
|---|
| 1646 | Â Â Â Â } |
|---|
| 1647 |     if ( !$app->user->is_superuser() ) { |
|---|
| 1648 |       my $perms = MT::Permission->load( |
|---|
| 1649 |         { blog_id => $blog_id, author_id => $app->user->id } ); |
|---|
| 1650 |       if ( |
|---|
| 1651 | Â Â Â Â Â Â Â Â !$perms |
|---|
| 1652 | Â Â Â Â Â Â Â Â ||Â (Â Â !$perms->can_edit_templates() |
|---|
| 1653 | Â Â Â Â Â Â Â Â Â Â &&Â !$perms->can_administer_blog()Â ) |
|---|
| 1654 | Â Â Â Â Â Â Â ) |
|---|
| 1655 | Â Â Â Â Â Â { |
|---|
| 1656 | Â Â Â Â Â Â Â Â next; |
|---|
| 1657 | Â Â Â Â Â Â } |
|---|
| 1658 | Â Â Â Â } |
|---|
| 1659 | |
|---|
| 1660 |     my $tmpl_list; |
|---|
| 1661 |     if ($blog_id) { |
|---|
| 1662 | |
|---|
| 1663 |       if ($refresh_type eq 'clean') { |
|---|
| 1664 | Â Â Â Â Â Â Â Â # the user wants to back up all templates and |
|---|
| 1665 | Â Â Â Â Â Â Â Â # install the new ones |
|---|
| 1666 | |
|---|
| 1667 |         my @ts = MT::Util::offset_time_list( $t, $blog_id ); |
|---|
| 1668 |         my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d", |
|---|
| 1669 |           $ts[5] + 1900, $ts[4] + 1, @ts[ 3, 2, 1, 0 ]; |
|---|
| 1670 | |
|---|
| 1671 |         my $tmpl_iter = MT::Template->load_iter({ |
|---|
| 1672 | Â Â Â Â Â Â Â Â Â Â blog_id =>Â $blog_id, |
|---|
| 1673 |           type => { not => 'backup' }, |
|---|
| 1674 | Â Â Â Â Â Â Â Â }); |
|---|
| 1675 | |
|---|
| 1676 |         while (my $tmpl = $tmpl_iter->()) { |
|---|
| 1677 |           if ($backup) { |
|---|
| 1678 | Â Â Â Â Â Â Â Â Â Â Â Â # zap all template maps |
|---|
| 1679 |             require MT::TemplateMap; |
|---|
| 1680 | Â Â Â Â Â Â Â Â Â Â Â Â MT::TemplateMap->remove({ |
|---|
| 1681 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â template_id =>Â $tmpl->id, |
|---|
| 1682 | Â Â Â Â Â Â Â Â Â Â Â Â }); |
|---|
| 1683 | Â Â Â Â Â Â Â Â Â Â Â Â $tmpl->type('backup'); |
|---|
| 1684 | Â Â Â Â Â Â Â Â Â Â Â Â $tmpl->name( |
|---|
| 1685 |               $tmpl->name . ' (Backup from ' . $ts . ')' ); |
|---|
| 1686 | Â Â Â Â Â Â Â Â Â Â Â Â $tmpl->identifier(undef); |
|---|
| 1687 | Â Â Â Â Â Â Â Â Â Â Â Â $tmpl->rebuild_me(0); |
|---|
| 1688 | Â Â Â Â Â Â Â Â Â Â Â Â $tmpl->linked_file(undef); |
|---|
| 1689 | Â Â Â Â Â Â Â Â Â Â Â Â $tmpl->outfile(''); |
|---|
| 1690 | Â Â Â Â Â Â Â Â Â Â Â Â $tmpl->save; |
|---|
| 1691 |           } else { |
|---|
| 1692 | Â Â Â Â Â Â Â Â Â Â Â Â $tmpl->remove; |
|---|
| 1693 | Â Â Â Â Â Â Â Â Â Â } |
|---|
| 1694 | Â Â Â Â Â Â Â Â } |
|---|
| 1695 | |
|---|
| 1696 | Â Â Â Â Â Â Â Â # This also creates our template mappings |
|---|
| 1697 |         $blog->create_default_templates( $template_set || |
|---|
| 1698 | Â Â Â Â Â Â Â Â Â Â $blog->template_set ||Â 'mt_blog'Â ); |
|---|
| 1699 | |
|---|
| 1700 |         if ($template_set) { |
|---|
| 1701 |           $blog->template_set( $template_set ); |
|---|
| 1702 | Â Â Â Â Â Â Â Â Â Â $blog->save; |
|---|
| 1703 |           $app->run_callbacks( 'blog_template_set_change', { blog => $blog } ); |
|---|
| 1704 | Â Â Â Â Â Â Â Â } |
|---|
| 1705 | |
|---|
| 1706 | Â Â Â Â Â Â Â Â next; |
|---|
| 1707 | Â Â Â Â Â Â } |
|---|
| 1708 | |
|---|
| 1709 |       $tmpl_list = MT::DefaultTemplates->templates($template_set || $blog->template_set) || MT::DefaultTemplates->templates(); |
|---|
| 1710 | Â Â Â Â } |
|---|
| 1711 |     else { |
|---|
| 1712 |       $tmpl_list = MT::DefaultTemplates->templates(); |
|---|
| 1713 | Â Â Â Â } |
|---|
| 1714 | |
|---|
| 1715 |     foreach my $val (@$tmpl_list) { |
|---|
| 1716 |       if ($blog_id) { |
|---|
| 1717 | Â Â Â Â Â Â Â Â # when refreshing blog templates, |
|---|
| 1718 | Â Â Â Â Â Â Â Â # skip over global templates which |
|---|
| 1719 | Â Â Â Â Â Â Â Â # specify a blog_id of 0... |
|---|
| 1720 |         next if $val->{global}; |
|---|
| 1721 | Â Â Â Â Â Â } |
|---|
| 1722 |       else { |
|---|
| 1723 |         next unless exists $val->{global}; |
|---|
| 1724 | Â Â Â Â Â Â } |
|---|
| 1725 | |
|---|
| 1726 |       if ( !$val->{orig_name} ) { |
|---|
| 1727 | Â Â Â Â Â Â Â Â $val->{orig_name}Â =Â $val->{name}; |
|---|
| 1728 | Â Â Â Â Â Â Â Â $val->{name}Â Â Â =Â $app->translate(Â $val->{name}Â ); |
|---|
| 1729 | Â Â Â Â Â Â Â Â $val->{text}Â Â Â =Â $app->translate_templatized(Â $val->{text}Â ); |
|---|
| 1730 | Â Â Â Â Â Â } |
|---|
| 1731 | |
|---|
| 1732 |       my $orig_name = $val->{orig_name}; |
|---|
| 1733 | |
|---|
| 1734 |       my @ts = MT::Util::offset_time_list( $t, ( $blog_id ? $blog_id : undef ) ); |
|---|
| 1735 |       my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d", $ts[5] + 1900, |
|---|
| 1736 |        $ts[4] + 1, @ts[ 3, 2, 1, 0 ]; |
|---|
| 1737 | |
|---|
| 1738 |       my $terms = {}; |
|---|
| 1739 | Â Â Â Â Â Â $terms->{blog_id}Â =Â $blog_id; |
|---|
| 1740 | Â Â Â Â Â Â $terms->{type}Â =Â $val->{type}; |
|---|
| 1741 |       if ( $val->{type} =~ |
|---|
| 1742 | Â Â Â Â Â Â Â Â m/^(archive|individual|page|category|index|custom|widget)$/Â ) |
|---|
| 1743 | Â Â Â Â Â Â { |
|---|
| 1744 | Â Â Â Â Â Â Â Â $terms->{name}Â =Â $val->{name}; |
|---|
| 1745 | Â Â Â Â Â Â } |
|---|
| 1746 |       else { |
|---|
| 1747 | Â Â Â Â Â Â Â Â $terms->{identifier}Â =Â $val->{identifier}; |
|---|
| 1748 | Â Â Â Â Â Â } |
|---|
| 1749 | |
|---|
| 1750 | Â Â Â Â Â Â # this should only return 1 template; we're searching |
|---|
| 1751 | Â Â Â Â Â Â # within a given blog for a specific type of template (for |
|---|
| 1752 | Â Â Â Â Â Â # "system" templates; or for a type + name, which should be |
|---|
| 1753 | Â Â Â Â Â Â # unique for that blog. |
|---|
| 1754 |       my $tmpl = MT::Template->load($terms); |
|---|
| 1755 |       if ($tmpl && $backup) { |
|---|
| 1756 | |
|---|
| 1757 | Â Â Â Â Â Â Â Â # check for default template text... |
|---|
| 1758 | Â Â Â Â Â Â Â Â # if it is a default template, then outright replace it |
|---|
| 1759 |         my $text = $tmpl->text; |
|---|
| 1760 |         $text =~ s/\s+//g; |
|---|
| 1761 | |
|---|
| 1762 |         my $def_text = $val->{text}; |
|---|
| 1763 |         $def_text =~ s/\s+//g; |
|---|
| 1764 | |
|---|
| 1765 | Â Â Â Â Â Â Â Â # if it has been customized, back it up to a new tmpl record |
|---|
| 1766 |         if ($def_text ne $text) { |
|---|
| 1767 |           my $backup = $tmpl->clone; |
|---|
| 1768 |           delete $backup->{column_values} |
|---|
| 1769 | Â Â Â Â Â Â Â Â Â Â Â ->{id};Â Â # make sure we don't overwrite original |
|---|
| 1770 |           delete $backup->{changed_cols}->{id}; |
|---|
| 1771 | Â Â Â Â Â Â Â Â Â Â $backup->name( |
|---|
| 1772 |             $backup->name . $app->translate( ' (Backup from [_1])', $ts ) ); |
|---|
| 1773 | Â Â Â Â Â Â Â Â Â Â $backup->type('backup'); |
|---|
| 1774 | Â Â Â Â Â Â Â Â Â Â # if ( $backup->type !~ |
|---|
| 1775 | Â Â Â Â Â Â Â Â Â Â #Â Â Â Â Â m/^(archive|individual|page|category|index|custom|widget)$/ ) |
|---|
| 1776 | Â Â Â Â Â Â Â Â Â Â # { |
|---|
| 1777 | Â Â Â Â Â Â Â Â Â Â #Â Â Â $backup->type('custom') |
|---|
| 1778 | Â Â Â Â Â Â Â Â Â Â #Â Â Â Â ;Â Â Â # system templates can't be created |
|---|
| 1779 | Â Â Â Â Â Â Â Â Â Â # } |
|---|
| 1780 | Â Â Â Â Â Â Â Â Â Â $backup->outfile(''); |
|---|
| 1781 | Â Â Â Â Â Â Â Â Â Â $backup->linked_file(Â $tmpl->linked_file ); |
|---|
| 1782 | Â Â Â Â Â Â Â Â Â Â $backup->identifier(undef); |
|---|
| 1783 | Â Â Â Â Â Â Â Â Â Â $backup->rebuild_me(0); |
|---|
| 1784 | Â Â Â Â Â Â Â Â Â Â $backup->build_dynamic(0); |
|---|
| 1785 | Â Â Â Â Â Â Â Â Â Â $backup->save; |
|---|
| 1786 | Â Â Â Â Â Â Â Â } |
|---|
| 1787 | Â Â Â Â Â Â } |
|---|
| 1788 |       if ($tmpl) { |
|---|
| 1789 | Â Â Â Â Â Â Â Â # we found that the previous template had not been |
|---|
| 1790 | Â Â Â Â Â Â Â Â # altered, so replace it with new default template... |
|---|
| 1791 |         if ( ( 'widgetset' eq $val->{type} ) |
|---|
| 1792 |          && ( exists $val->{widgets} ) ) { |
|---|
| 1793 |           my $modulesets = delete $val->{widgets}; |
|---|
| 1794 |           $tmpl->modulesets( MT::Template->widgets_to_modulesets($modulesets, $blog_id) ); |
|---|
| 1795 | Â Â Â Â Â Â Â Â } |
|---|
| 1796 | Â Â Â Â Â Â Â Â $tmpl->text(Â $val->{text}Â ); |
|---|
| 1797 | Â Â Â Â Â Â Â Â $tmpl->identifier(Â $val->{identifier}Â ); |
|---|
| 1798 | Â Â Â Â Â Â Â Â $tmpl->type(Â $val->{type}Â ) |
|---|
| 1799 | Â Â Â Â Â Â Â Â Â ;Â # fixes mismatch of types for cases like "archive" => "individual" |
|---|
| 1800 | Â Â Â Â Â Â Â Â $tmpl->linked_file(''); |
|---|
| 1801 | Â Â Â Â Â Â Â Â $tmpl->save; |
|---|
| 1802 | Â Â Â Â Â Â } |
|---|
| 1803 |       else { |
|---|
| 1804 | Â Â Â Â Â Â Â Â # create this one... |
|---|
| 1805 |         my $tmpl = new MT::Template; |
|---|
| 1806 |         if ( ( 'widgetset' eq $val->{type} ) |
|---|
| 1807 |          && ( exists $val->{widgets} ) ) { |
|---|
| 1808 |           my $modulesets = delete $val->{widgets}; |
|---|
| 1809 |           $tmpl->modulesets( MT::Template->widgets_to_modulesets($modulesets, $blog_id) ); |
|---|
| 1810 | Â Â Â Â Â Â Â Â } |
|---|
| 1811 | Â Â Â Â Â Â Â Â $tmpl->build_dynamic(0); |
|---|
| 1812 | Â Â Â Â Â Â Â Â $tmpl->set_values( |
|---|
| 1813 | Â Â Â Â Â Â Â Â Â Â { |
|---|
| 1814 |             text    => $val->{text}, |
|---|
| 1815 |             name    => $val->{name}, |
|---|
| 1816 |             type    => $val->{type}, |
|---|
| 1817 | Â Â Â Â Â Â Â Â Â Â Â Â identifier =>Â $val->{identifier}, |
|---|
| 1818 |             outfile  => $val->{outfile}, |
|---|
| 1819 | Â Â Â Â Â Â Â Â Â Â Â Â rebuild_me =>Â $val->{rebuild_me}, |
|---|
| 1820 | Â Â Â Â Â Â Â Â Â Â } |
|---|
| 1821 | Â Â Â Â Â Â Â Â ); |
|---|
| 1822 | Â Â Â Â Â Â Â Â $tmpl->blog_id($blog_id); |
|---|
| 1823 | Â Â Â Â Â Â Â Â $tmpl->save |
|---|
| 1824 |          or return $app->error( |
|---|
| 1825 | Â Â Â Â Â Â Â Â Â Â Â Â $app->translate("Error creating new template: ") |
|---|
| 1826 |            . $tmpl->errstr ); |
|---|
| 1827 | Â Â Â Â Â Â } |
|---|
| 1828 | Â Â Â Â } |
|---|
| 1829 | Â Â } |
|---|
| 1830 | |
|---|
| 1831 | Â Â $app->add_return_arg(Â 'refreshed'Â =>Â 1Â ); |
|---|
| 1832 | Â Â $app->call_return; |
|---|
| 1833 | } |
|---|
| 1834 | |
|---|
| 1835 | sub refresh_individual_templates { |
|---|
| 1836 |   my ($app) = @_; |
|---|
| 1837 | |
|---|
| 1838 |   require MT::Util; |
|---|
| 1839 | |
|---|
| 1840 |   my $user = $app->user; |
|---|
| 1841 |   my $perms = $app->permissions; |
|---|
| 1842 |   return $app->error( |
|---|
| 1843 | Â Â Â Â $app->translate( |
|---|
| 1844 | Â Â Â Â Â Â "Permission denied.") |
|---|
| 1845 | Â Â Â ) |
|---|
| 1846 | Â Â Â #TODO: system level-designer permission |
|---|
| 1847 |    unless $user->is_superuser() || $user->can_edit_templates() |
|---|
| 1848 | Â Â Â ||Â (Â $perms |
|---|
| 1849 | Â Â Â Â &&Â (Â $perms->can_edit_templates() |
|---|
| 1850 | Â Â Â Â Â ||Â $perms->can_administer_blog )Â ); |
|---|
| 1851 | |
|---|
| 1852 |   my $set; |
|---|
| 1853 |   if ( my $blog_id = $app->param('blog_id') ) { |
|---|
| 1854 |     my $blog = $app->model('blog')->load($blog_id) |
|---|
| 1855 |       or return $app->error($app->translate('Can\'t load blog #[_1].', $blog_id)); |
|---|
| 1856 |     $set = $blog->template_set() |
|---|
| 1857 |       if $blog; |
|---|
| 1858 | Â Â } |
|---|
| 1859 | |
|---|
| 1860 |   require MT::DefaultTemplates; |
|---|
| 1861 |   my $tmpl_list = MT::DefaultTemplates->templates($set) or return; |
|---|
| 1862 | |
|---|
| 1863 |   my $tmpl_types = {}; |
|---|
| 1864 |   my $tmpl_ids  = {}; |
|---|
| 1865 |   my $tmpls   = {}; |
|---|
| 1866 |   foreach my $tmpl (@$tmpl_list) { |
|---|
| 1867 | Â Â Â Â $tmpl->{text}Â =Â $app->translate_templatized(Â $tmpl->{text}Â ); |
|---|
| 1868 | Â Â Â Â $tmpl_ids->{Â $tmpl->{identifier}Â }Â =Â $tmpl |
|---|
| 1869 |       if $tmpl->{identifier}; |
|---|
| 1870 |     if ( $tmpl->{type} !~ m/^(archive|individual|page|category|index|custom|widget)$/ ) |
|---|
| 1871 | Â Â Â Â { |
|---|
| 1872 | Â Â Â Â Â Â $tmpl_types->{Â $tmpl->{type}Â }Â =Â $tmpl; |
|---|
| 1873 | Â Â Â Â } |
|---|
| 1874 |     else { |
|---|
| 1875 | Â Â Â Â Â Â $tmpls->{Â $tmpl->{type}Â }{Â $tmpl->{name}Â }Â =Â $tmpl; |
|---|
| 1876 | Â Â Â Â } |
|---|
| 1877 | Â Â } |
|---|
| 1878 | |
|---|
| 1879 |   my $t = time; |
|---|
| 1880 | |
|---|
| 1881 |   my @msg; |
|---|
| 1882 |   my @id = $app->param('id'); |
|---|
| 1883 |   require MT::Template; |
|---|
| 1884 |   foreach my $tmpl_id (@id) { |
|---|
| 1885 |     my $tmpl = MT::Template->load($tmpl_id); |
|---|
| 1886 |     next unless $tmpl; |
|---|
| 1887 |     my $blog_id = $tmpl->blog_id; |
|---|
| 1888 | |
|---|
| 1889 | Â Â Â Â # FIXME: permission check -- for this blog_id |
|---|
| 1890 | |
|---|
| 1891 |     my @ts = MT::Util::offset_time_list( $t, $blog_id ); |
|---|
| 1892 |     my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d", $ts[5] + 1900, |
|---|
| 1893 |      $ts[4] + 1, @ts[ 3, 2, 1, 0 ]; |
|---|
| 1894 | |
|---|
| 1895 |     my $val = ( $tmpl->identifier ? $tmpl_ids->{ $tmpl->identifier() } : undef ) |
|---|
| 1896 | Â Â Â Â Â ||Â $tmpl_types->{Â $tmpl->type()Â } |
|---|
| 1897 | Â Â Â Â Â ||Â $tmpls->{Â $tmpl->type()Â }{Â $tmpl->name }; |
|---|
| 1898 |     if ( !$val ) { |
|---|
| 1899 |       push @msg, |
|---|
| 1900 | Â Â Â Â Â Â Â $app->translate( |
|---|
| 1901 | "Skipping template '[_1]' since it appears to be a custom template.", |
|---|
| 1902 | Â Â Â Â Â Â Â Â $tmpl->name |
|---|
| 1903 | Â Â Â Â Â Â Â ); |
|---|
| 1904 | Â Â Â Â Â Â next; |
|---|
| 1905 | Â Â Â Â } |
|---|
| 1906 | |
|---|
| 1907 |     my $text = $tmpl->text; |
|---|
| 1908 |     $text =~ s/\s+//g; |
|---|
| 1909 | |
|---|
| 1910 |     my $def_text = $val->{text}; |
|---|
| 1911 |     $def_text =~ s/\s+//g; |
|---|
| 1912 | |
|---|
| 1913 |     if ($text ne $def_text) { |
|---|
| 1914 | Â Â Â Â Â Â # if it has been customized, back it up to a new tmpl record |
|---|
| 1915 |       my $backup = $tmpl->clone; |
|---|
| 1916 |       delete $backup->{column_values} |
|---|
| 1917 | Â Â Â Â Â Â Â ->{id};Â Â # make sure we don't overwrite original |
|---|
| 1918 |       delete $backup->{changed_cols}->{id}; |
|---|
| 1919 |       $backup->name( $backup->name . ' (Backup from ' . $ts . ')' ); |
|---|
| 1920 | Â Â Â Â Â Â $backup->type('backup'); |
|---|
| 1921 | Â Â Â Â Â Â $backup->outfile(''); |
|---|
| 1922 | Â Â Â Â Â Â $backup->linked_file(Â $tmpl->linked_file ); |
|---|
| 1923 | Â Â Â Â Â Â $backup->rebuild_me(0); |
|---|
| 1924 | Â Â Â Â Â Â $backup->build_dynamic(0); |
|---|
| 1925 | Â Â Â Â Â Â $backup->identifier(undef); |
|---|
| 1926 | Â Â Â Â Â Â $backup->save; |
|---|
| 1927 |       push @msg, |
|---|
| 1928 | Â Â Â Â Â Â Â $app->translate( |
|---|
| 1929 | Â Â 'Refreshing template <strong>[_3]</strong> with <a href="?__mode=view&blog_id=[_1]&_type=template&id=[_2]">backup</a>', |
|---|
| 1930 |          $blog_id, $backup->id, $tmpl->name ); |
|---|
| 1931 | |
|---|
| 1932 | Â Â Â Â Â Â # we found that the previous template had not been |
|---|
| 1933 | Â Â Â Â Â Â # altered, so replace it with new default template... |
|---|
| 1934 | Â Â Â Â Â Â $tmpl->text(Â $val->{text}Â ); |
|---|
| 1935 | Â Â Â Â Â Â $tmpl->identifier(Â $val->{identifier}Â ); |
|---|
| 1936 | Â Â Â Â Â Â $tmpl->linked_file(''); |
|---|
| 1937 | Â Â Â Â Â Â $tmpl->save; |
|---|
| 1938 |     } else { |
|---|
| 1939 |       push @msg, $app->translate("Skipping template '[_1]' since it has not been changed.", $tmpl->name); |
|---|
| 1940 | Â Â Â Â } |
|---|
| 1941 | Â Â } |
|---|
| 1942 |   my @msg_loop; |
|---|
| 1943 |   push @msg_loop, { message => $_ } foreach @msg; |
|---|
| 1944 | |
|---|
| 1945 | Â Â $app->build_page(Â 'refresh_results.tmpl', |
|---|
| 1946 |     { message_loop => \@msg_loop, return_url => $app->return_uri } ); |
|---|
| 1947 | } |
|---|
| 1948 | |
|---|
| 1949 | sub clone_templates { |
|---|
| 1950 |   my ($app) = @_; |
|---|
| 1951 | |
|---|
| 1952 |   my $user = $app->user; |
|---|
| 1953 |   my $perms = $app->permissions; |
|---|
| 1954 |   return $app->error( |
|---|
| 1955 | Â Â Â Â $app->translate( |
|---|
| 1956 | Â Â Â Â Â Â "Permission denied.") |
|---|
| 1957 | Â Â Â ) |
|---|
| 1958 | Â Â Â #TODO: system level-designer permission |
|---|
| 1959 |    unless $user->is_superuser() || $user->can_edit_templates() |
|---|
| 1960 | Â Â Â ||Â (Â $perms |
|---|
| 1961 | Â Â Â Â &&Â (Â $perms->can_edit_templates() |
|---|
| 1962 | Â Â Â Â Â ||Â $perms->can_administer_blog )Â ); |
|---|
| 1963 | |
|---|
| 1964 |   my @id = $app->param('id'); |
|---|
| 1965 |   require MT::Template; |
|---|
| 1966 |   foreach my $tmpl_id (@id) { |
|---|
| 1967 |     my $tmpl = MT::Template->load($tmpl_id); |
|---|
| 1968 |     next unless $tmpl; |
|---|
| 1969 | |
|---|
| 1970 |     my $new_tmpl = $tmpl->clone({ |
|---|
| 1971 | Â Â Â Â Â Â Except =>Â { |
|---|
| 1972 | Â Â Â Â Â Â Â Â id =>Â 1, |
|---|
| 1973 | Â Â Â Â Â Â Â Â name =>Â 1, |
|---|
| 1974 | Â Â Â Â Â Â Â Â identifier =>Â 1, |
|---|
| 1975 | Â Â Â Â Â Â }, |
|---|
| 1976 | Â Â Â Â }); |
|---|
| 1977 | |
|---|
| 1978 |     my $new_basename = $app->translate("Copy of [_1]", $tmpl->name); |
|---|
| 1979 |     my $new_name = $new_basename; |
|---|
| 1980 |     my $i = 0; |
|---|
| 1981 |     while (MT::Template->exist({ name => $new_name, blog_id => $tmpl->blog_id })) { |
|---|
| 1982 |       $new_name = $new_basename . ' (' . ++$i . ')'; |
|---|
| 1983 | Â Â Â Â } |
|---|
| 1984 | |
|---|
| 1985 | Â Â Â Â $new_tmpl->name($new_name); |
|---|
| 1986 | Â Â Â Â $new_tmpl->save; |
|---|
| 1987 | Â Â } |
|---|
| 1988 | |
|---|
| 1989 | Â Â $app->add_return_arg(Â 'saved_copied'Â =>Â 1Â ); |
|---|
| 1990 | Â Â $app->call_return; |
|---|
| 1991 | } |
|---|
| 1992 | |
|---|
| 1993 | sub publish_index_templates { |
|---|
| 1994 |   my $app = shift; |
|---|
| 1995 |   $app->validate_magic or return; |
|---|
| 1996 | |
|---|
| 1997 | Â Â # permission check |
|---|
| 1998 |   my $perms = $app->permissions; |
|---|
| 1999 |   return $app->errtrans("Permission denied.") |
|---|
| 2000 |     unless $app->user->is_superuser || |
|---|
| 2001 | Â Â Â Â Â Â $perms->can_administer_blog || |
|---|
| 2002 | Â Â Â Â Â Â $perms->can_rebuild; |
|---|
| 2003 | |
|---|
| 2004 |   my $blog = $app->blog; |
|---|
| 2005 |   my $templates = MT->model('template')->lookup_multi([ $app->param('id') ]); |
|---|
| 2006 |   TEMPLATE: for my $tmpl (@$templates) { |
|---|
| 2007 |     next TEMPLATE if !defined $tmpl; |
|---|
| 2008 |     next TEMPLATE if $tmpl->blog_id != $blog->id; |
|---|
| 2009 |     next TEMPLATE unless $tmpl->build_type; |
|---|
| 2010 | |
|---|
| 2011 | Â Â Â Â $app->rebuild_indexes( |
|---|
| 2012 |       Blog   => $blog, |
|---|
| 2013 | Â Â Â Â Â Â Template =>Â $tmpl, |
|---|
| 2014 |       Force  => 1, |
|---|
| 2015 | Â Â Â Â ); |
|---|
| 2016 | Â Â } |
|---|
| 2017 | |
|---|
| 2018 | Â Â $app->call_return(Â published =>Â 1Â ); |
|---|
| 2019 | } |
|---|
| 2020 | |
|---|
| 2021 | sub publish_archive_templates { |
|---|
| 2022 |   my $app = shift; |
|---|
| 2023 |   $app->validate_magic or return; |
|---|
| 2024 | |
|---|
| 2025 | Â Â # permission check |
|---|
| 2026 |   my $perms = $app->permissions; |
|---|
| 2027 |   return $app->errtrans("Permission denied.") |
|---|
| 2028 |    unless $app->user->is_superuser |
|---|
| 2029 | Â Â Â ||Â $perms->can_administer_blog |
|---|
| 2030 | Â Â Â ||Â $perms->can_rebuild; |
|---|
| 2031 | |
|---|
| 2032 |   my @ids = $app->param('id'); |
|---|
| 2033 |   if (scalar @ids == 1) { |
|---|
| 2034 | Â Â Â Â # we also support a list of comma-delimited ids like this |
|---|
| 2035 |     @ids = split /,/, $ids[0]; |
|---|
| 2036 | Â Â } |
|---|
| 2037 |   return $app->error($app->translate("Invalid request.")) |
|---|
| 2038 |     unless @ids; |
|---|
| 2039 | |
|---|
| 2040 |   my $tmpl_id; |
|---|
| 2041 |   my %ats; |
|---|
| 2042 |   require MT::TemplateMap; |
|---|
| 2043 |   while (!$tmpl_id && @ids) { |
|---|
| 2044 |     $tmpl_id = shift @ids; |
|---|
| 2045 |     my @tmpl_maps = MT::TemplateMap->load( { template_id => $tmpl_id } ); |
|---|
| 2046 |     foreach my $map (@tmpl_maps) { |
|---|
| 2047 |       next unless $map->build_type; |
|---|
| 2048 | Â Â Â Â Â Â $ats{Â $map->archive_type }Â =Â 1; |
|---|
| 2049 | Â Â Â Â } |
|---|
| 2050 |     undef $tmpl_id unless keys %ats; |
|---|
| 2051 | Â Â } |
|---|
| 2052 | |
|---|
| 2053 | Â Â # we have a template and archive types to publish! |
|---|
| 2054 | |
|---|
| 2055 |   require MT::CMS::Blog; |
|---|
| 2056 |   my $return_args; |
|---|
| 2057 |   if (@ids) { |
|---|
| 2058 | Â Â Â Â # we have more to do after this, so save the list |
|---|
| 2059 | Â Â Â Â # of remaining archive templates... |
|---|
| 2060 |     $return_args = $app->uri_params( |
|---|
| 2061 | Â Â Â Â Â Â mode =>Â 'publish_archive_templates', |
|---|
| 2062 | Â Â Â Â Â Â args =>Â { |
|---|
| 2063 | Â Â Â Â Â Â Â Â magic_token =>Â $app->current_magic, |
|---|
| 2064 |         blog_id => scalar $app->param('blog_id'), |
|---|
| 2065 |         id => join(",", @ids), |
|---|
| 2066 | Â Â Â Â Â Â } |
|---|
| 2067 | Â Â Â Â ); |
|---|
| 2068 |   } else { |
|---|
| 2069 | Â Â Â Â # nothing left after this publish operation; just return |
|---|
| 2070 | Â Â Â Â # to the edit screen. |
|---|
| 2071 |     $return_args = $app->uri_params( |
|---|
| 2072 | Â Â Â Â Â Â mode =>Â 'view', |
|---|
| 2073 | Â Â Â Â Â Â args =>Â { |
|---|
| 2074 |         _type   => 'template', |
|---|
| 2075 |         blog_id  => scalar $app->param('blog_id'), |
|---|
| 2076 | Â Â Â Â Â Â Â Â published =>Â 1, |
|---|
| 2077 |         id    => $tmpl_id, |
|---|
| 2078 |         saved   => 1, |
|---|
| 2079 | Â Â Â Â Â Â } |
|---|
| 2080 | Â Â Â Â ); |
|---|
| 2081 | Â Â } |
|---|
| 2082 |   $return_args =~ s/^\?//; |
|---|
| 2083 | |
|---|
| 2084 |   $app->return_args( $return_args ); |
|---|
| 2085 |   $app->param( 'template_id', $tmpl_id ); |
|---|
| 2086 |   $app->param( 'single_template', 1 ); # forces fullscreen mode |
|---|
| 2087 |   $app->param( 'type', join(",", keys %ats) ); |
|---|
| 2088 |   return MT::CMS::Blog::start_rebuild_pages($app); |
|---|
| 2089 | } |
|---|
| 2090 | |
|---|
| 2091 | sub save_widget { |
|---|
| 2092 |   my $app = shift; |
|---|
| 2093 |   my $q  = $app->param; |
|---|
| 2094 | |
|---|
| 2095 |   $app->validate_magic() or return; |
|---|
| 2096 |   my $author = $app->user; |
|---|
| 2097 | |
|---|
| 2098 |   my $id = $q->param('id'); |
|---|
| 2099 | |
|---|
| 2100 |   if ( !$author->is_superuser ) { |
|---|
| 2101 |     $app->run_callbacks( 'cms_save_permission_filter.template', $app, $id ) |
|---|
| 2102 |      || return $app->error( |
|---|
| 2103 |       $app->translate( "Permission denied: [_1]", $app->errstr() ) ); |
|---|
| 2104 | Â Â } |
|---|
| 2105 | |
|---|
| 2106 |   my $filter_result = $app->run_callbacks( 'cms_save_filter.widgetset', $app ); |
|---|
| 2107 | |
|---|
| 2108 |   if ( !$filter_result ) { |
|---|
| 2109 |     return edit_widget( $app, { error => $app->translate( "Save failed: [_1]", $app->errstr ) } ); |
|---|
| 2110 | Â Â } |
|---|
| 2111 | |
|---|
| 2112 |   my $class = $app->model('template'); |
|---|
| 2113 |   my $obj; |
|---|
| 2114 |   if ( $id ) { |
|---|
| 2115 |     $obj = $class->load($id) |
|---|
| 2116 |       or return $app->error($app->translate("Invalid ID [_1]", $id)); |
|---|
| 2117 | Â Â } |
|---|
| 2118 |   else { |
|---|
| 2119 |     $obj = $class->new; |
|---|
| 2120 | Â Â } |
|---|
| 2121 | |
|---|
| 2122 |   my $original = $obj->clone(); |
|---|
| 2123 | Â Â $obj->name($q->param('name')); |
|---|
| 2124 | Â Â $obj->type('widgetset'); |
|---|
| 2125 | Â Â $obj->blog_id(Â $q->param('blog_id')Â ||Â 0Â ); |
|---|
| 2126 | Â Â $obj->modulesets($q->param('modules')); |
|---|
| 2127 | |
|---|
| 2128 |   unless ( |
|---|
| 2129 |     $app->run_callbacks( 'cms_pre_save.template', $app, $obj, $original ) ) |
|---|
| 2130 | Â Â { |
|---|
| 2131 |     return edit_widget( $app, { error => $app->translate( "Save failed: [_1]", $app->errstr ) } ); |
|---|
| 2132 | Â Â } |
|---|
| 2133 | |
|---|
| 2134 | Â Â $obj->save |
|---|
| 2135 |    or return $app->error( |
|---|
| 2136 |     $app->translate( "Saving object failed: [_1]", $obj->errstr ) ); |
|---|
| 2137 | |
|---|
| 2138 |   $app->run_callbacks( 'cms_post_save.template', $app, $obj, $original ) |
|---|
| 2139 |    or return $app->error( $app->errstr() ); |
|---|
| 2140 | |
|---|
| 2141 | Â Â $app->redirect( |
|---|
| 2142 | Â Â Â Â $app->uri( |
|---|
| 2143 | Â Â Â Â Â Â 'mode'Â =>Â 'edit_widget', |
|---|
| 2144 | Â Â Â Â Â Â args => |
|---|
| 2145 |        { blog_id => $obj->blog_id, 'saved' => 1, rebuild => 1, id => $obj->id } |
|---|
| 2146 | Â Â Â Â ) |
|---|
| 2147 | Â Â ); |
|---|
| 2148 | } |
|---|
| 2149 | |
|---|
| 2150 | sub edit_widget { |
|---|
| 2151 |   my $app = shift; |
|---|
| 2152 |   my (%opt) = @_; |
|---|
| 2153 | |
|---|
| 2154 |   my $q    = $app->param(); |
|---|
| 2155 |   my $id   = scalar($q->param('id')) || $opt{id}; |
|---|
| 2156 |   my $blog_id = scalar $q->param('blog_id') || 0; |
|---|
| 2157 | |
|---|
| 2158 |   my $tmpl_class = $app->model('template'); |
|---|
| 2159 |   require MT::Promise; |
|---|
| 2160 |   my $obj_promise = MT::Promise::delay( |
|---|
| 2161 | Â Â Â Â sub { |
|---|
| 2162 |       return $tmpl_class->load($id) || undef; |
|---|
| 2163 | Â Â Â Â } |
|---|
| 2164 | Â Â ); |
|---|
| 2165 | |
|---|
| 2166 |   if ( !$app->user->is_superuser ) { |
|---|
| 2167 | Â Â Â Â $app->run_callbacks(Â 'cms_view_permission_filter.template', |
|---|
| 2168 |       $app, $id, $obj_promise ) |
|---|
| 2169 |      || return $app->error( |
|---|
| 2170 |       $app->translate( "Permission denied: [_1]", $app->errstr() ) ); |
|---|
| 2171 | Â Â } |
|---|
| 2172 | |
|---|
| 2173 |   my $param = { |
|---|
| 2174 |     blog_id   => $blog_id, |
|---|
| 2175 |     search_type => "template", |
|---|
| 2176 | Â Â Â Â search_label =>Â MT::Template->class_label_plural, |
|---|
| 2177 |     $id ? ( id => $id ) : (), |
|---|
| 2178 |     exists($opt{rebuild}) ? ( rebuild => $opt{rebuild} ) : (), |
|---|
| 2179 |     exists($opt{error}) ? ( error => $opt{error} ) : (), |
|---|
| 2180 |     exists($opt{saved}) ? ( saved => $opt{saved} ) : () |
|---|
| 2181 | Â Â }; |
|---|
| 2182 |   if ($blog_id) { |
|---|
| 2183 |     my $blog = $app->blog; |
|---|
| 2184 | Â Â Â Â # include_system/include_cache are only applicable |
|---|
| 2185 | Â Â Â Â # to blog-level templates |
|---|
| 2186 | Â Â Â Â $param->{include_system}Â =Â $blog->include_system; |
|---|
| 2187 | Â Â Â Â $param->{include_cache}Â =Â $blog->include_cache; |
|---|
| 2188 | Â Â Â Â $param->{include_with_ssi}Â Â Â =Â 0; |
|---|
| 2189 | Â Â Â Â $param->{cache_path}Â Â Â Â Â Â =Â ''; |
|---|
| 2190 | Â Â Â Â $param->{cache_enabled}Â Â Â Â Â =Â 0; |
|---|
| 2191 | Â Â Â Â $param->{cache_expire_type}Â Â Â =Â 0; |
|---|
| 2192 | Â Â Â Â $param->{cache_expire_period}Â Â =Â ''; |
|---|
| 2193 | Â Â Â Â $param->{cache_expire_interval}Â =Â 0; |
|---|
| 2194 |     $param->{ssi_type} = uc $blog->include_system; |
|---|
| 2195 | Â Â } |
|---|
| 2196 | Â Â |
|---|
| 2197 |   my $iter = $tmpl_class->load_iter( |
|---|
| 2198 |     { type => 'widget', blog_id => $blog_id ? [ $blog_id, 0 ] : 0 }, |
|---|
| 2199 |     { sort => 'name', direction => 'ascend' } |
|---|
| 2200 | Â Â ); |
|---|
| 2201 | |
|---|
| 2202 |   my %all_widgets; |
|---|
| 2203 |   while (my $m = $iter->()) { |
|---|
| 2204 |     next unless $m; |
|---|
| 2205 | Â Â Â Â $all_widgets{Â $m->id }{name}Â =Â $m->name; |
|---|
| 2206 | Â Â Â Â $all_widgets{Â $m->id }{blog_id}Â =Â $m->blog_id; |
|---|
| 2207 | Â Â } |
|---|
| 2208 | |
|---|
| 2209 |   my @inst_modules; |
|---|
| 2210 |   my $wtmpl; |
|---|
| 2211 |   if ( $id ) { |
|---|
| 2212 |     $wtmpl = $obj_promise->force() |
|---|
| 2213 |      or return $app->error( |
|---|
| 2214 | Â Â Â Â Â Â $app->translate( |
|---|
| 2215 | Â Â Â Â Â Â Â Â "Load failed: [_1]", |
|---|
| 2216 | Â Â Â Â Â Â Â Â $tmpl_class->errstr ||Â $app->translate("(no reason given)") |
|---|
| 2217 | Â Â Â Â Â Â ) |
|---|
| 2218 | Â Â Â Â Â ); |
|---|
| 2219 | Â Â Â Â $param->{name}Â =Â $wtmpl->name; |
|---|
| 2220 | Â Â Â Â $param->{include_with_ssi}Â =Â $wtmpl->include_with_ssi |
|---|
| 2221 |      if defined $wtmpl->include_with_ssi; |
|---|
| 2222 | Â Â Â Â $param->{cache_path}Â Â Â Â =Â $wtmpl->cache_path |
|---|
| 2223 |      if defined $wtmpl->cache_path; |
|---|
| 2224 | Â Â Â Â $param->{cache_expire_type}Â =Â $wtmpl->cache_expire_type |
|---|
| 2225 |      if defined $wtmpl->cache_expire_type; |
|---|
| 2226 |     my ( $period, $interval ) = |
|---|
| 2227 | Â Â Â Â Â _get_schedule(Â $wtmpl->cache_expire_interval ); |
|---|
| 2228 |     $param->{cache_expire_period}  = $period  if defined $period; |
|---|
| 2229 |     $param->{cache_expire_interval} = $interval if defined $interval; |
|---|
| 2230 |     my @events = split ',', $wtmpl->cache_expire_event; |
|---|
| 2231 |     foreach my $name (@events) { |
|---|
| 2232 |       $param->{ 'cache_expire_event_' . $name } = 1; |
|---|
| 2233 | Â Â Â Â } |
|---|
| 2234 |     my $modulesets = $wtmpl->modulesets; |
|---|
| 2235 |     if ( $modulesets ) { |
|---|
| 2236 |       my @modules = split ',', $modulesets; |
|---|
| 2237 |       foreach my $mid ( @modules ) { |
|---|
| 2238 |         push @inst_modules, { |
|---|
| 2239 | Â Â Â Â Â Â Â Â Â Â id =>Â $mid, |
|---|
| 2240 | Â Â Â Â Â Â Â Â Â Â name =>Â $all_widgets{$mid}{name}, |
|---|
| 2241 | Â Â Â Â Â Â Â Â Â Â blog_id =>Â $all_widgets{$mid}{blog_id}, |
|---|
| 2242 | Â Â Â Â Â Â Â Â }; |
|---|
| 2243 |         delete $all_widgets{$mid}; |
|---|
| 2244 | Â Â Â Â Â Â } |
|---|
| 2245 | Â Â Â Â } |
|---|
| 2246 | Â Â } |
|---|
| 2247 |   $param->{installed} = \@inst_modules if @inst_modules; |
|---|
| 2248 |   my @avail_modules = map { { |
|---|
| 2249 |     id => $_, name => $all_widgets{$_}{name}, blog_id => $all_widgets{$_}{blog_id} |
|---|
| 2250 |   } } keys %all_widgets; |
|---|
| 2251 | Â Â $param->{available}Â =Â \@avail_modules; |
|---|
| 2252 | |
|---|
| 2253 |   my $res = $app->run_callbacks('cms_edit.widgetset', $app, $id, $wtmpl, $param); |
|---|
| 2254 |   if (!$res) { |
|---|
| 2255 |     return $app->error($app->callback_errstr()); |
|---|
| 2256 | Â Â } |
|---|
| 2257 | |
|---|
| 2258 |   $app->load_tmpl('edit_widget.tmpl', $param); |
|---|
| 2259 | } |
|---|
| 2260 | |
|---|
| 2261 | sub list_widget { |
|---|
| 2262 |   my $app = shift; |
|---|
| 2263 |   my (%opt) = @_; |
|---|
| 2264 |   my $q = $app->param; |
|---|
| 2265 | |
|---|
| 2266 |   my $perms = $app->blog ? $app->permissions : $app->user->permissions; |
|---|
| 2267 |   return $app->return_to_dashboard( redirect => 1 ) |
|---|
| 2268 |    unless $perms || $app->user->is_superuser; |
|---|
| 2269 |   if ( $perms && !$perms->can_edit_templates ) { |
|---|
| 2270 |     return $app->return_to_dashboard( permission => 1 ); |
|---|
| 2271 | Â Â } |
|---|
| 2272 |   my $blog_id = $q->param('blog_id') || 0; |
|---|
| 2273 | |
|---|
| 2274 |   my $widget_loop = &build_template_table( $app, |
|---|
| 2275 | Â Â Â Â load_args =>Â [Â |
|---|
| 2276 |       { type => 'widget', blog_id => $blog_id ? [ $blog_id, 0 ] : 0 }, |
|---|
| 2277 |       { sort => 'name', direction => 'ascend' } |
|---|
| 2278 | Â Â Â Â ], |
|---|
| 2279 | Â Â ); |
|---|
| 2280 | |
|---|
| 2281 |   my $iter = $app->model('template')->load_iter( |
|---|
| 2282 |     { type => 'widgetset', blog_id => $blog_id ? $blog_id : 0 }, |
|---|
| 2283 |     { sort => 'name', direction => 'ascend' } |
|---|
| 2284 | Â Â ); |
|---|
| 2285 |   my @widgetmanagers; |
|---|
| 2286 |   while ( my $widgetset = $iter->() ) { |
|---|
| 2287 |     next unless $widgetset; |
|---|
| 2288 |     my $ws = { |
|---|
| 2289 | Â Â Â Â Â Â id =>Â $widgetset->id, |
|---|
| 2290 | Â Â Â Â Â Â widgetmanager =>Â $widgetset->name, |
|---|
| 2291 | Â Â Â Â }; |
|---|
| 2292 |     if ( my $modulesets = $widgetset->modulesets ) { |
|---|
| 2293 | Â Â Â Â Â Â $ws->{widgets}Â =Â $modulesets; |
|---|
| 2294 |       my @names; |
|---|
| 2295 |       foreach my $module ( split ',', $modulesets ) { |
|---|
| 2296 |         my ( $widget ) = grep { $_->{id} eq $module } @$widget_loop; |
|---|
| 2297 |         push @names, $widget->{name} if $widget; |
|---|
| 2298 | Â Â Â Â Â Â } |
|---|
| 2299 |       $ws->{names} = join(', ', @names) if @names; |
|---|
| 2300 | Â Â Â Â } |
|---|
| 2301 |     push @widgetmanagers, $ws; |
|---|
| 2302 | Â Â } |
|---|
| 2303 | |
|---|
| 2304 |   my @widget_loop; |
|---|
| 2305 |   if ( $blog_id ) { |
|---|
| 2306 | Â Â Â Â # Remove system level widgets from the listing |
|---|
| 2307 |     @widget_loop = grep { $_->{blog_id} == $blog_id } @$widget_loop; |
|---|
| 2308 | Â Â } |
|---|
| 2309 |   else { |
|---|
| 2310 |     @widget_loop = @$widget_loop; |
|---|
| 2311 | Â Â } |
|---|
| 2312 | |
|---|
| 2313 |   my $param = { |
|---|
| 2314 |     @widgetmanagers ? ( object_loop => \@widgetmanagers ) : (), |
|---|
| 2315 |     @widget_loop  ? ( widget_table => \@widget_loop ) : (), |
|---|
| 2316 |     object_type  => "template", |
|---|
| 2317 |     search_type  => "template", |
|---|
| 2318 |     search_label  => MT::Template->class_label_plural, |
|---|
| 2319 | Â Â Â Â listing_screen =>Â 1, |
|---|
| 2320 |     screen_id   => "list-widget-set", |
|---|
| 2321 |     $blog_id ? ( blog_view => 1, blog_id => $blog_id ) : (), |
|---|
| 2322 |     exists($opt{rebuild}) ? ( rebuild => $opt{rebuild} ) : (), |
|---|
| 2323 |     exists($opt{error}) ? ( error => $opt{error} ) : (), |
|---|
| 2324 |     exists($opt{deleted}) ? ( saved => $opt{deleted} ) : () |
|---|
| 2325 | Â Â }; |
|---|
| 2326 | |
|---|
| 2327 |   $app->load_tmpl('list_widget.tmpl', $param); |
|---|
| 2328 | } |
|---|
| 2329 | |
|---|
| 2330 | sub delete_widget { |
|---|
| 2331 |   my $app = shift; |
|---|
| 2332 |   my $q  = $app->param; |
|---|
| 2333 |   my $type = $q->param('_type'); |
|---|
| 2334 | |
|---|
| 2335 |   return $app->errtrans("Invalid request.") |
|---|
| 2336 |    unless $type; |
|---|
| 2337 | |
|---|
| 2338 |   return $app->error( $app->translate("Invalid request.") ) |
|---|
| 2339 |    if $app->request_method() ne 'POST'; |
|---|
| 2340 | |
|---|
| 2341 |   $app->validate_magic() or return; |
|---|
| 2342 | |
|---|
| 2343 |   my $tmpl_class = $app->model('template'); |
|---|
| 2344 | |
|---|
| 2345 |   for my $id ( $q->param('id') ) { |
|---|
| 2346 |     next unless $id;  # avoid 'empty' ids |
|---|
| 2347 | |
|---|
| 2348 |     my $obj = $tmpl_class->load($id); |
|---|
| 2349 |     next unless $obj; |
|---|
| 2350 | Â Â Â Â $app->run_callbacks(Â 'cms_delete_permission_filter.template', |
|---|
| 2351 |       $app, $obj ) |
|---|
| 2352 |      || return $app->error( |
|---|
| 2353 |       $app->translate( "Permission denied: [_1]", $app->errstr() ) ); |
|---|
| 2354 | |
|---|
| 2355 | Â Â Â Â $obj->remove |
|---|
| 2356 |      or return $app->errtrans( |
|---|
| 2357 | Â Â Â Â Â Â 'Removing [_1] failed: [_2]', |
|---|
| 2358 | Â Â Â Â Â Â $app->translate('template'), |
|---|
| 2359 | Â Â Â Â Â Â $obj->errstr |
|---|
| 2360 | Â Â Â Â Â ); |
|---|
| 2361 |     $app->run_callbacks( 'cms_post_delete.template', $app, $obj ); |
|---|
| 2362 | Â Â } |
|---|
| 2363 | Â Â $app->call_return; |
|---|
| 2364 | } |
|---|
| 2365 | |
|---|
| 2366 | sub restore_widgetmanagers { |
|---|
| 2367 |   my ($cb, $objects, $deferred, $errors, $callback) = @_; |
|---|
| 2368 |   my @keys = grep { $_ =~ /^MT::Template#/ } keys( %$objects ); |
|---|
| 2369 |   foreach my $key ( @keys ) { |
|---|
| 2370 |     my $tmpl = $objects->{$key}; |
|---|
| 2371 |     next unless 'widgetset' eq $tmpl->type; |
|---|
| 2372 |     my $modulesets = $tmpl->modulesets; |
|---|
| 2373 |     next unless $modulesets; |
|---|
| 2374 |     $callback->( MT->translate( 'Restoring widget set [_1]... ', $tmpl->name ) ); |
|---|
| 2375 | |
|---|
| 2376 |     my @tmpl_ids = split ',', $modulesets; |
|---|
| 2377 |     my @new_ids; |
|---|
| 2378 |     foreach my $id ( @tmpl_ids ) { |
|---|
| 2379 |       my $new_tmpl = $objects->{"MT::Template#$id"}; |
|---|
| 2380 |       next unless $new_tmpl; |
|---|
| 2381 |       push @new_ids, $new_tmpl->id; |
|---|
| 2382 | Â Â Â Â } |
|---|
| 2383 |     if ( @new_ids ) { |
|---|
| 2384 |       $tmpl->modulesets( join(',', @new_ids) ); |
|---|
| 2385 | Â Â Â Â Â Â $tmpl->save; |
|---|
| 2386 |       $callback->( MT->translate("Done.") . "\n" ); |
|---|
| 2387 | Â Â Â Â } |
|---|
| 2388 |     else { |
|---|
| 2389 |       $callback->( MT->translate("Failed.") . "\n" |
|---|