| 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::DefaultTemplates; |
|---|
| 8 | |
|---|
| 9 | use strict; |
|---|
| 10 | |
|---|
| 11 | =start |
|---|
| 12 | Registry storage format for default templates: |
|---|
| 13 | |
|---|
| 14 | default_templates: |
|---|
| 15 | index: |
|---|
| 16 | # The identifier used here never changes for this |
|---|
| 17 | # template. It is also unique. |
|---|
| 18 | main_index: |
|---|
| 19 | filename: (optional; defaults to <identifier>.mtml) |
|---|
| 20 | label: Main Index (auto-translated) |
|---|
| 21 | outfile: (applicable for index templates only) |
|---|
| 22 | rebuild_me: (applicable for index templates only) |
|---|
| 23 | |
|---|
| 24 | =cut |
|---|
| 25 | |
|---|
| 26 | my $loaded = 0; |
|---|
| 27 | my $templates; |
|---|
| 28 | BEGIN { |
|---|
| 29 | $templates = { |
|---|
| 30 | 'index' => { |
|---|
| 31 | 'main_index' => { |
|---|
| 32 | label => 'Main Index', |
|---|
| 33 | outfile => 'index.html', |
|---|
| 34 | rebuild_me => 1, |
|---|
| 35 | }, |
|---|
| 36 | 'archive_index' => { |
|---|
| 37 | label => 'Archive Index', |
|---|
| 38 | outfile => 'archives.html', |
|---|
| 39 | rebuild_me => 1, |
|---|
| 40 | }, |
|---|
| 41 | 'styles' => { |
|---|
| 42 | label => 'Stylesheet', |
|---|
| 43 | outfile => 'styles.css', |
|---|
| 44 | rebuild_me => 1, |
|---|
| 45 | }, |
|---|
| 46 | 'javascript' => { |
|---|
| 47 | label => 'JavaScript', |
|---|
| 48 | outfile => 'mt.js', |
|---|
| 49 | rebuild_me => 1, |
|---|
| 50 | }, |
|---|
| 51 | 'feed_recent' => { |
|---|
| 52 | label => 'Feed - Recent Entries', |
|---|
| 53 | outfile => 'atom.xml', |
|---|
| 54 | rebuild_me => 1, |
|---|
| 55 | }, |
|---|
| 56 | 'rsd' => { |
|---|
| 57 | label => 'RSD', |
|---|
| 58 | outfile => 'rsd.xml', |
|---|
| 59 | rebuild_me => 1, |
|---|
| 60 | }, |
|---|
| 61 | }, |
|---|
| 62 | 'individual' => { |
|---|
| 63 | 'entry' => { |
|---|
| 64 | label => 'Entry', |
|---|
| 65 | mappings => { |
|---|
| 66 | entry_archive => { |
|---|
| 67 | archive_type => 'Individual', |
|---|
| 68 | }, |
|---|
| 69 | }, |
|---|
| 70 | }, |
|---|
| 71 | }, |
|---|
| 72 | 'page' => { |
|---|
| 73 | 'page' => { |
|---|
| 74 | label => 'Page', |
|---|
| 75 | mappings => { |
|---|
| 76 | page_archive => { |
|---|
| 77 | archive_type => 'Page', |
|---|
| 78 | }, |
|---|
| 79 | }, |
|---|
| 80 | }, |
|---|
| 81 | }, |
|---|
| 82 | 'archive' => { |
|---|
| 83 | 'monthly_entry_listing' => { |
|---|
| 84 | label => 'Monthly Entry Listing', |
|---|
| 85 | mappings => { |
|---|
| 86 | monthly => { |
|---|
| 87 | archive_type => 'Monthly', |
|---|
| 88 | }, |
|---|
| 89 | }, |
|---|
| 90 | }, |
|---|
| 91 | 'category_entry_listing' => { |
|---|
| 92 | label => 'Category Entry Listing', |
|---|
| 93 | mappings => { |
|---|
| 94 | category => { |
|---|
| 95 | archive_type => 'Category', |
|---|
| 96 | }, |
|---|
| 97 | }, |
|---|
| 98 | }, |
|---|
| 99 | }, |
|---|
| 100 | 'system' => { |
|---|
| 101 | 'comment_response' => { |
|---|
| 102 | label => 'Comment Response', |
|---|
| 103 | description_label => 'Displays error, pending or confirmation message for comments.' |
|---|
| 104 | }, |
|---|
| 105 | 'comment_preview' => { |
|---|
| 106 | label => 'Comment Preview', |
|---|
| 107 | description_label => 'Displays preview of comment.', |
|---|
| 108 | }, |
|---|
| 109 | 'dynamic_error' => { |
|---|
| 110 | label => 'Dynamic Error', |
|---|
| 111 | description_label => 'Displays errors for dynamically published templates.', |
|---|
| 112 | }, |
|---|
| 113 | 'popup_image' => { |
|---|
| 114 | label => 'Popup Image', |
|---|
| 115 | description_label => 'Displays image when user clicks a popup-linked image.', |
|---|
| 116 | }, |
|---|
| 117 | 'search_results' => { |
|---|
| 118 | label => 'Search Results', |
|---|
| 119 | description_label => 'Displays results of a search.', |
|---|
| 120 | }, |
|---|
| 121 | }, |
|---|
| 122 | 'module' => { |
|---|
| 123 | 'banner_header' => { |
|---|
| 124 | label => 'Banner Header', |
|---|
| 125 | }, |
|---|
| 126 | 'banner_footer' => { |
|---|
| 127 | label => 'Banner Footer', |
|---|
| 128 | }, |
|---|
| 129 | 'entry_summary' => { |
|---|
| 130 | label => 'Entry Summary', |
|---|
| 131 | }, |
|---|
| 132 | 'html_head' => { |
|---|
| 133 | label => 'HTML Head', |
|---|
| 134 | }, |
|---|
| 135 | 'sidebar' => { |
|---|
| 136 | label => 'Sidebar', |
|---|
| 137 | }, |
|---|
| 138 | 'comments' => { |
|---|
| 139 | label => 'Comments', |
|---|
| 140 | }, |
|---|
| 141 | 'trackbacks' => { |
|---|
| 142 | label => 'Trackbacks', |
|---|
| 143 | }, |
|---|
| 144 | }, |
|---|
| 145 | 'widget' => { |
|---|
| 146 | 'about_this_page' => { |
|---|
| 147 | label => 'About This Page', |
|---|
| 148 | }, |
|---|
| 149 | 'archive_widgets_group' => { |
|---|
| 150 | label => 'Archive Widgets Group', |
|---|
| 151 | }, |
|---|
| 152 | 'author_archive_list' => { |
|---|
| 153 | label => 'Author Archives', |
|---|
| 154 | }, |
|---|
| 155 | 'current_author_monthly_archive_list' => { |
|---|
| 156 | label => 'Current Author Monthly Archives', |
|---|
| 157 | }, |
|---|
| 158 | 'calendar' => { |
|---|
| 159 | label => 'Calendar', |
|---|
| 160 | }, |
|---|
| 161 | 'category_archive_list' => { |
|---|
| 162 | label => 'Category Archives', |
|---|
| 163 | }, |
|---|
| 164 | 'current_category_monthly_archive_list' => { |
|---|
| 165 | label => 'Current Category Monthly Archives', |
|---|
| 166 | }, |
|---|
| 167 | 'creative_commons' => { |
|---|
| 168 | label => 'Creative Commons', |
|---|
| 169 | }, |
|---|
| 170 | 'main_index_widgets_group' => { |
|---|
| 171 | label => 'Home Page Widgets Group', |
|---|
| 172 | }, |
|---|
| 173 | 'monthly_archive_dropdown' => { |
|---|
| 174 | label => 'Monthly Archives Dropdown', |
|---|
| 175 | }, |
|---|
| 176 | 'monthly_archive_list' => { |
|---|
| 177 | label => 'Monthly Archives', |
|---|
| 178 | }, |
|---|
| 179 | 'pages_list' => { |
|---|
| 180 | label => 'Page Listing', |
|---|
| 181 | }, |
|---|
| 182 | 'recent_assets' => { |
|---|
| 183 | label => 'Recent Assets', |
|---|
| 184 | }, |
|---|
| 185 | 'powered_by' => { |
|---|
| 186 | label => 'Powered By', |
|---|
| 187 | }, |
|---|
| 188 | 'recent_comments' => { |
|---|
| 189 | label => 'Recent Comments', |
|---|
| 190 | }, |
|---|
| 191 | 'recent_entries' => { |
|---|
| 192 | label => 'Recent Entries', |
|---|
| 193 | }, |
|---|
| 194 | 'search' => { |
|---|
| 195 | label => 'Search', |
|---|
| 196 | }, |
|---|
| 197 | 'signin' => { |
|---|
| 198 | label => 'Sign In', |
|---|
| 199 | }, |
|---|
| 200 | 'syndication' => { |
|---|
| 201 | label => 'Syndication', |
|---|
| 202 | }, |
|---|
| 203 | 'tag_cloud' => { |
|---|
| 204 | label => 'Tag Cloud', |
|---|
| 205 | }, |
|---|
| 206 | 'technorati_search' => { |
|---|
| 207 | label => 'Technorati Search', |
|---|
| 208 | }, |
|---|
| 209 | 'date_based_author_archives' => { |
|---|
| 210 | label => 'Date-Based Author Archives', |
|---|
| 211 | }, |
|---|
| 212 | 'date_based_category_archives' => { |
|---|
| 213 | label => 'Date-Based Category Archives', |
|---|
| 214 | } |
|---|
| 215 | }, |
|---|
| 216 | 'widgetset' => { |
|---|
| 217 | '2column_layout_sidebar' => { |
|---|
| 218 | order => 1000, |
|---|
| 219 | label => '2-column layout - Sidebar', |
|---|
| 220 | widgets => [ |
|---|
| 221 | 'Search', |
|---|
| 222 | 'About This Page', |
|---|
| 223 | 'Home Page Widgets Group', |
|---|
| 224 | 'Archive Widgets Group', |
|---|
| 225 | 'Page Listing', |
|---|
| 226 | 'Syndication', |
|---|
| 227 | 'Powered By', |
|---|
| 228 | ], |
|---|
| 229 | }, |
|---|
| 230 | '3column_layout_primary_sidebar' => { |
|---|
| 231 | order => 1000, |
|---|
| 232 | label => '3-column layout - Primary Sidebar', |
|---|
| 233 | widgets => [ |
|---|
| 234 | 'Archive Widgets Group', |
|---|
| 235 | 'Page Listing', |
|---|
| 236 | 'Syndication', |
|---|
| 237 | 'Powered By', |
|---|
| 238 | ], |
|---|
| 239 | }, |
|---|
| 240 | '3column_layout_secondary_sidebar' => { |
|---|
| 241 | order => 1000, |
|---|
| 242 | label => '3-column layout - Secondary Sidebar', |
|---|
| 243 | widgets => [ |
|---|
| 244 | 'Search', |
|---|
| 245 | 'Home Page Widgets Group', |
|---|
| 246 | 'About This Page', |
|---|
| 247 | ], |
|---|
| 248 | }, |
|---|
| 249 | }, |
|---|
| 250 | 'global:module' => { |
|---|
| 251 | 'footer-email' => { |
|---|
| 252 | label => 'Mail Footer', |
|---|
| 253 | }, |
|---|
| 254 | }, |
|---|
| 255 | 'global:email' => { |
|---|
| 256 | 'comment_throttle' => { |
|---|
| 257 | label => 'Comment throttle', |
|---|
| 258 | }, |
|---|
| 259 | 'commenter_confirm' => { |
|---|
| 260 | label => 'Commenter Confirm', |
|---|
| 261 | }, |
|---|
| 262 | 'commenter_notify' => { |
|---|
| 263 | label => 'Commenter Notify', |
|---|
| 264 | }, |
|---|
| 265 | 'new-comment' => { |
|---|
| 266 | label => 'New Comment', |
|---|
| 267 | }, |
|---|
| 268 | 'new-ping' => { |
|---|
| 269 | label => 'New Ping', |
|---|
| 270 | }, |
|---|
| 271 | 'notify-entry' => { |
|---|
| 272 | label => 'Entry Notify', |
|---|
| 273 | }, |
|---|
| 274 | 'recover-password' => { |
|---|
| 275 | label => 'Password Recovery', |
|---|
| 276 | }, |
|---|
| 277 | 'verify-subscribe' => { |
|---|
| 278 | label => 'Subscribe Verify', |
|---|
| 279 | }, |
|---|
| 280 | }, |
|---|
| 281 | }; |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | sub core_default_templates { |
|---|
| 285 | return $templates; |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | sub load { |
|---|
| 289 | my $class = shift; |
|---|
| 290 | my ($terms) = @_; |
|---|
| 291 | my $tmpls = $class->templates || []; |
|---|
| 292 | if ($terms) { |
|---|
| 293 | foreach my $key (keys %$terms) { |
|---|
| 294 | @$tmpls = grep { $_->{$key} eq $terms->{$key} } @$tmpls; |
|---|
| 295 | } |
|---|
| 296 | } |
|---|
| 297 | return wantarray ? @$tmpls : (@$tmpls ? $tmpls->[0] : undef); |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | sub templates { |
|---|
| 301 | my $pkg = shift; |
|---|
| 302 | my ($set) = @_; |
|---|
| 303 | require File::Spec; |
|---|
| 304 | |
|---|
| 305 | # A set of default templates as returned by MT::Component->registry |
|---|
| 306 | # yields an array of hashes. |
|---|
| 307 | |
|---|
| 308 | my @tmpl_path = $set ? ("template_sets", $set) : ("default_templates"); |
|---|
| 309 | my $all_tmpls = MT::Component->registry(@tmpl_path) || []; |
|---|
| 310 | my $weblog_templates_path = MT->config('WeblogTemplatesPath'); |
|---|
| 311 | |
|---|
| 312 | my (%tmpls, %global_tmpls); |
|---|
| 313 | foreach my $def_tmpl (@$all_tmpls) { |
|---|
| 314 | # copy structure, then run filter |
|---|
| 315 | |
|---|
| 316 | my $tmpl_hash; |
|---|
| 317 | if ($def_tmpl->{templates} && ($def_tmpl->{templates} eq '*')) { |
|---|
| 318 | $tmpl_hash = MT->registry("default_templates"); |
|---|
| 319 | } |
|---|
| 320 | else { |
|---|
| 321 | $tmpl_hash = $set ? $def_tmpl->{templates} : $def_tmpl; |
|---|
| 322 | } |
|---|
| 323 | my $plugin = $tmpl_hash->{plugin}; |
|---|
| 324 | |
|---|
| 325 | foreach my $tmpl_set (keys %$tmpl_hash) { |
|---|
| 326 | next unless ref($tmpl_hash->{$tmpl_set}) eq 'HASH'; |
|---|
| 327 | foreach my $tmpl_id (keys %{ $tmpl_hash->{$tmpl_set} }) { |
|---|
| 328 | next if $tmpl_id eq 'plugin'; |
|---|
| 329 | |
|---|
| 330 | my $p = $tmpl_hash->{plugin} || $tmpl_hash->{$tmpl_set}{plugin}; |
|---|
| 331 | my $base_path = $def_tmpl->{base_path} || $tmpl_hash->{$tmpl_set}{base_path}; |
|---|
| 332 | if ($p && $base_path) { |
|---|
| 333 | $base_path = File::Spec->catdir($p->path, $base_path); |
|---|
| 334 | } |
|---|
| 335 | else { |
|---|
| 336 | $base_path = $weblog_templates_path; |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | my $tmpl = { %{ $tmpl_hash->{$tmpl_set}{$tmpl_id} } }; |
|---|
| 340 | my $type = $tmpl_set; |
|---|
| 341 | if ($tmpl_set =~ m/^global:/) { |
|---|
| 342 | $type =~ s/^global://; |
|---|
| 343 | $tmpl->{global} = 1; |
|---|
| 344 | } |
|---|
| 345 | $tmpl->{set} = $type; # system, index, archive, etc. |
|---|
| 346 | $tmpl->{order} = 0 unless exists $tmpl->{order}; |
|---|
| 347 | |
|---|
| 348 | $type = 'custom' if $type eq 'module'; |
|---|
| 349 | $type = $tmpl_id if $type eq 'system'; |
|---|
| 350 | my $name = $tmpl->{label}; |
|---|
| 351 | $name = $name->() if ref($name) eq 'CODE'; |
|---|
| 352 | $tmpl->{name} = $name; |
|---|
| 353 | $tmpl->{type} = $type; |
|---|
| 354 | $tmpl->{key} = $tmpl_id; |
|---|
| 355 | $tmpl->{identifier} = $tmpl_id; |
|---|
| 356 | |
|---|
| 357 | # load template if it hasn't been loaded already |
|---|
| 358 | if (!exists $tmpl->{text}) { |
|---|
| 359 | local (*FIN, $/); |
|---|
| 360 | my $filename = $tmpl->{filename} || ($tmpl_id . '.mtml'); |
|---|
| 361 | my $file = File::Spec->catfile($base_path, $filename); |
|---|
| 362 | if ((-e $file) && (-r $file)) { |
|---|
| 363 | open FIN, "<$file"; my $data = <FIN>; close FIN; |
|---|
| 364 | $tmpl->{text} = $data; |
|---|
| 365 | } else { |
|---|
| 366 | $tmpl->{text} = ''; |
|---|
| 367 | } |
|---|
| 368 | } |
|---|
| 369 | |
|---|
| 370 | if ( exists $tmpl->{widgets} ) { |
|---|
| 371 | my $widgets = $tmpl->{widgets}; |
|---|
| 372 | my @widgets; |
|---|
| 373 | foreach my $widget ( @$widgets ) { |
|---|
| 374 | if ( $plugin ) { |
|---|
| 375 | push @widgets, $plugin->translate( $widget ); |
|---|
| 376 | } |
|---|
| 377 | else { |
|---|
| 378 | push @widgets, MT->translate( $widget ); |
|---|
| 379 | } |
|---|
| 380 | } |
|---|
| 381 | $tmpl->{widgets} = \@widgets if @widgets; |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | my $local_global_tmpls = $tmpl->{global} ? \%global_tmpls : \%tmpls; |
|---|
| 385 | if (exists $local_global_tmpls->{$tmpl_id}) { |
|---|
| 386 | # allow components/plugins to override core |
|---|
| 387 | # templates |
|---|
| 388 | $local_global_tmpls->{$tmpl_id} = $tmpl if $p && ($p->id ne 'core'); |
|---|
| 389 | } |
|---|
| 390 | else { |
|---|
| 391 | $local_global_tmpls->{$tmpl_id} = $tmpl; |
|---|
| 392 | } |
|---|
| 393 | } |
|---|
| 394 | } |
|---|
| 395 | } |
|---|
| 396 | my @tmpls = (values(%tmpls), values(%global_tmpls)); |
|---|
| 397 | @tmpls = sort { $a->{order} <=> $b->{order} } @tmpls; |
|---|
| 398 | MT->run_callbacks('DefaultTemplateFilter' . ($set ? '.' . $set : ''), \@tmpls); |
|---|
| 399 | return \@tmpls; |
|---|
| 400 | } |
|---|
| 401 | |
|---|
| 402 | 1; |
|---|
| 403 | __END__ |
|---|
| 404 | |
|---|
| 405 | =head1 NAME |
|---|
| 406 | |
|---|
| 407 | MT::DefaultTemplates |
|---|
| 408 | |
|---|
| 409 | =head1 METHODS |
|---|
| 410 | |
|---|
| 411 | =head2 templates() |
|---|
| 412 | |
|---|
| 413 | Return the list of the templates in the WeblogTemplatesPath. |
|---|
| 414 | |
|---|
| 415 | =head1 AUTHOR & COPYRIGHT |
|---|
| 416 | |
|---|
| 417 | Please see L<MT/AUTHOR & COPYRIGHT>. |
|---|
| 418 | |
|---|
| 419 | =cut |
|---|