root/branches/release-26/lib/MT/DefaultTemplates.pm @ 1174

Revision 1174, 9.8 kB (checked in by bchoate, 23 months ago)

Updated copyright year for source.

  • Property svn:keywords set to Id Revision
Line 
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
7package MT::DefaultTemplates;
8
9use strict;
10
11=start
12Registry storage format for default templates:
13
14default_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
26my $loaded = 0;
27my $templates;
28BEGIN {
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            },
50            'rsd' => {
51                label => 'RSD',
52                outfile => 'rsd.xml',
53                rebuild_me => 1,
54            },
55            'atom' => {
56                label => 'Atom',
57                outfile => 'atom.xml',
58                rebuild_me => 1,
59            },
60            'rss' => {
61                label => 'RSS',
62                outfile => 'rss.xml',
63                rebuild_me => 1,
64            },
65        },
66        'individual' => {
67            'entry' => {
68                label => 'Entry',
69                mappings => {
70                    entry_archive => {
71                        archive_type => 'Individual',
72                    },
73                },
74            },
75        },
76        'page' => {
77            'page' => {
78                label => 'Page',
79                mappings => {
80                    page_archive => {
81                        archive_type => 'Page',
82                    },
83                },
84            },
85        },
86        'archive' => {
87            'entry_listing' => {
88                label => 'Entry Listing',
89                mappings => {
90                    monthly => {
91                        archive_type => 'Monthly',
92                    },
93                    category_monthly => {
94                        archive_type => 'Category-Monthly',
95                    },
96                    author_monthly => {
97                        archive_type => 'Author-Monthly',
98                    },
99                    category => {
100                        archive_type => 'Category',
101                    },
102                },
103            },
104        },
105        'system' => {
106            'comment_response' => {
107                label => 'Comment Response',
108                description_label => 'Displays error, pending or confirmation message for comments.'
109            },
110            'comment_preview' => {
111                label => 'Comment Preview',
112                description_label => 'Displays preview of comment.',
113            },
114            'dynamic_error' => {
115                label => 'Dynamic Error',
116                description_label => 'Displays errors for dynamically published templates.',
117            },
118            'popup_image' => {
119                label => 'Popup Image',
120                description_label => 'Displays image when user clicks a popup-linked image.',
121            },
122            'search_results' => {
123                label => 'Search Results',
124                description_label => 'Displays results of a search.',
125            },
126        },
127        'module' => {
128            'categories' => {
129                label => 'Categories',
130            },
131            'comment_detail' => {
132                label => 'Comment Detail',
133            },
134            'comment_form' => {
135                label => 'Comment Form',
136            },
137            'comments' => {
138                label => 'Comments',
139            },
140            'entry_detail' => {
141                label => 'Entry Detail',
142            },
143            'entry_summary' => {
144                label => 'Entry Summary',
145            },
146            'entry_metadata' => {
147                label => 'Entry Metadata',
148            },
149            'tags' => {
150                label => 'Tags',
151            },
152            'footer' => {
153                label => 'Footer',
154            },
155            'header' => {
156                label => 'Header',
157            },
158            'sidebar_2col' => {
159                label => 'Sidebar - 2 Column Layout',
160            },
161            'sidebar_3col' => {
162                label => 'Sidebar - 3 Column Layout',
163            },
164            'sidebar' => {
165                label => 'Sidebar',
166            },
167            'trackbacks' => {
168                label => 'TrackBacks',
169            },
170            'page_detail' => {
171                label => 'Page Detail',
172            },
173        },
174        'global:module' => {
175            'footer-email' => {
176                label => 'Mail Footer',
177            },
178        },
179        'global:email' => {
180            'comment_throttle' => {
181                label => 'Comment throttle',
182            },
183            'commenter_confirm' => {
184                label => 'Commenter Confirm',
185            },
186            'commenter_notify' => {
187                label => 'Commenter Notify',
188            },
189            'new-comment' => {
190                label => 'New Comment',
191            },
192            'new-ping' => {
193                label => 'New Ping',
194            },
195            'notify-entry' => {
196                label => 'Entry Notify',
197            },
198            'recover-password' => {
199                label => 'Password Recovery',
200            },
201            'verify-subscribe' => {
202                label => 'Subscribe Verify',
203            },
204        },
205    };
206}
207
208sub core_default_templates {
209    return $templates;
210}
211
212sub load {
213    my $class = shift;
214    my ($terms) = @_;
215    my $tmpls = $class->templates || [];
216    if ($terms) {
217        foreach my $key (keys %$terms) {
218            @$tmpls = grep { $_->{$key} eq $terms->{$key} } @$tmpls;
219        }
220    }
221    return wantarray ? @$tmpls : (@$tmpls ? $tmpls->[0] : undef);
222}
223
224sub templates {
225    my $pkg = shift;
226    my ($set) = @_;
227    require File::Spec;
228
229    # A set of default templates as returned by MT::Component->registry
230    # yields an array of hashes.
231
232    my @tmpl_path = $set ? ("template_sets", $set) : ("default_templates");
233    my $all_tmpls = MT::Component->registry(@tmpl_path) || [];
234    my $weblog_templates_path = MT->config('WeblogTemplatesPath');
235
236    my (%tmpls, %global_tmpls);
237    foreach my $def_tmpl (@$all_tmpls) {
238        # copy structure, then run filter
239
240        my $tmpl_hash;
241        if ($def_tmpl->{templates} && ($def_tmpl->{templates} eq '*')) {
242            $tmpl_hash = MT->registry("default_templates");
243        }
244        else {
245            $tmpl_hash = $set ? $def_tmpl->{templates} : $def_tmpl;
246        }
247
248        foreach my $tmpl_set (keys %$tmpl_hash) {
249            next unless ref($tmpl_hash->{$tmpl_set}) eq 'HASH';
250            foreach my $tmpl_id (keys %{ $tmpl_hash->{$tmpl_set} }) {
251                next if $tmpl_id eq 'plugin';
252
253                my $p = $tmpl_hash->{plugin} || $tmpl_hash->{$tmpl_set}{plugin};
254                my $base_path = $def_tmpl->{base_path} || $tmpl_hash->{$tmpl_set}{base_path};
255                if ($p && $base_path) {
256                    $base_path = File::Spec->catdir($p->path, $base_path);
257                }
258                else {
259                    $base_path = $weblog_templates_path;
260                }
261
262                my $tmpl = { %{ $tmpl_hash->{$tmpl_set}{$tmpl_id} } };
263                my $type = $tmpl_set;
264                if ($tmpl_set =~ m/^global:/) {
265                    $type =~ s/^global://;
266                    $tmpl->{global} = 1;
267                }
268                $tmpl->{set} = $type; # system, index, archive, etc.
269
270                $type = 'custom' if $type eq 'module';
271                $type = $tmpl_id if $type eq 'system';
272                my $name = $tmpl->{label};
273                $name = $name->() if ref($name) eq 'CODE';
274                $tmpl->{name} = $name;
275                $tmpl->{type} = $type;
276                $tmpl->{key} = $tmpl_id;
277                $tmpl->{identifier} = $tmpl_id;
278
279                # load template if it hasn't been loaded already
280                if (!exists $tmpl->{text}) {
281                    local (*FIN, $/);
282                    my $filename = $tmpl->{filename} || ($tmpl_id . '.mtml');
283                    my $file = File::Spec->catfile($base_path, $filename);
284                    if ((-e $file) && (-r $file)) {
285                        open FIN, "<$file"; my $data = <FIN>; close FIN;
286                        $tmpl->{text} = $data;
287                    } else {
288                        $tmpl->{text} = '';
289                    }
290                }
291
292                my $local_global_tmpls = $tmpl->{global} ? \%global_tmpls : \%tmpls;
293                if (exists $local_global_tmpls->{$tmpl_id}) {
294                    # allow components/plugins to override core
295                    # templates
296                    $local_global_tmpls->{$tmpl_id} = $tmpl if $p && ($p->id ne 'core');
297                }
298                else {
299                    $local_global_tmpls->{$tmpl_id} = $tmpl;
300                }
301            }
302        }
303    }
304    my @tmpls = (values(%tmpls), values(%global_tmpls));
305    MT->run_callbacks('DefaultTemplateFilter' . ($set ? '.' . $set : ''), \@tmpls);
306    return \@tmpls;
307}
308
3091;
310__END__
311
312=head1 NAME
313
314MT::DefaultTemplates
315
316=head1 METHODS
317
318=head2 templates()
319
320Return the list of the templates in the WeblogTemplatesPath.
321
322=head1 AUTHOR & COPYRIGHT
323
324Please see L<MT/AUTHOR & COPYRIGHT>.
325
326=cut
Note: See TracBrowser for help on using the browser.