root/branches/release-33/lib/MT/DefaultTemplates.pm @ 1709

Revision 1709, 9.0 kB (checked in by fumiakiy, 20 months ago)

Merged the latest checkins to release-32 branch to the current dev branch. svn merge -r1669:1708 http://code.sixapart.com/svn/movabletype/branches/release-32 .

  • 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                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        'global:module' => {
146            'footer-email' => {
147                label => 'Mail Footer',
148            },
149        },
150        'global:email' => {
151            'comment_throttle' => {
152                label => 'Comment throttle',
153            },
154            'commenter_confirm' => {
155                label => 'Commenter Confirm',
156            },
157            'commenter_notify' => {
158                label => 'Commenter Notify',
159            },
160            'new-comment' => {
161                label => 'New Comment',
162            },
163            'new-ping' => {
164                label => 'New Ping',
165            },
166            'notify-entry' => {
167                label => 'Entry Notify',
168            },
169            'recover-password' => {
170                label => 'Password Recovery',
171            },
172            'verify-subscribe' => {
173                label => 'Subscribe Verify',
174            },
175        },
176    };
177}
178
179sub core_default_templates {
180    return $templates;
181}
182
183sub load {
184    my $class = shift;
185    my ($terms) = @_;
186    my $tmpls = $class->templates || [];
187    if ($terms) {
188        foreach my $key (keys %$terms) {
189            @$tmpls = grep { $_->{$key} eq $terms->{$key} } @$tmpls;
190        }
191    }
192    return wantarray ? @$tmpls : (@$tmpls ? $tmpls->[0] : undef);
193}
194
195sub templates {
196    my $pkg = shift;
197    my ($set) = @_;
198    require File::Spec;
199
200    # A set of default templates as returned by MT::Component->registry
201    # yields an array of hashes.
202
203    my @tmpl_path = $set ? ("template_sets", $set) : ("default_templates");
204    my $all_tmpls = MT::Component->registry(@tmpl_path) || [];
205    my $weblog_templates_path = MT->config('WeblogTemplatesPath');
206
207    my (%tmpls, %global_tmpls);
208    foreach my $def_tmpl (@$all_tmpls) {
209        # copy structure, then run filter
210
211        my $tmpl_hash;
212        if ($def_tmpl->{templates} && ($def_tmpl->{templates} eq '*')) {
213            $tmpl_hash = MT->registry("default_templates");
214        }
215        else {
216            $tmpl_hash = $set ? $def_tmpl->{templates} : $def_tmpl;
217        }
218
219        foreach my $tmpl_set (keys %$tmpl_hash) {
220            next unless ref($tmpl_hash->{$tmpl_set}) eq 'HASH';
221            foreach my $tmpl_id (keys %{ $tmpl_hash->{$tmpl_set} }) {
222                next if $tmpl_id eq 'plugin';
223
224                my $p = $tmpl_hash->{plugin} || $tmpl_hash->{$tmpl_set}{plugin};
225                my $base_path = $def_tmpl->{base_path} || $tmpl_hash->{$tmpl_set}{base_path};
226                if ($p && $base_path) {
227                    $base_path = File::Spec->catdir($p->path, $base_path);
228                }
229                else {
230                    $base_path = $weblog_templates_path;
231                }
232
233                my $tmpl = { %{ $tmpl_hash->{$tmpl_set}{$tmpl_id} } };
234                my $type = $tmpl_set;
235                if ($tmpl_set =~ m/^global:/) {
236                    $type =~ s/^global://;
237                    $tmpl->{global} = 1;
238                }
239                $tmpl->{set} = $type; # system, index, archive, etc.
240
241                $type = 'custom' if $type eq 'module';
242                $type = $tmpl_id if $type eq 'system';
243                my $name = $tmpl->{label};
244                $name = $name->() if ref($name) eq 'CODE';
245                $tmpl->{name} = $name;
246                $tmpl->{type} = $type;
247                $tmpl->{key} = $tmpl_id;
248                $tmpl->{identifier} = $tmpl_id;
249
250                # load template if it hasn't been loaded already
251                if (!exists $tmpl->{text}) {
252                    local (*FIN, $/);
253                    my $filename = $tmpl->{filename} || ($tmpl_id . '.mtml');
254                    my $file = File::Spec->catfile($base_path, $filename);
255                    if ((-e $file) && (-r $file)) {
256                        open FIN, "<$file"; my $data = <FIN>; close FIN;
257                        $tmpl->{text} = $data;
258                    } else {
259                        $tmpl->{text} = '';
260                    }
261                }
262
263                my $local_global_tmpls = $tmpl->{global} ? \%global_tmpls : \%tmpls;
264                if (exists $local_global_tmpls->{$tmpl_id}) {
265                    # allow components/plugins to override core
266                    # templates
267                    $local_global_tmpls->{$tmpl_id} = $tmpl if $p && ($p->id ne 'core');
268                }
269                else {
270                    $local_global_tmpls->{$tmpl_id} = $tmpl;
271                }
272            }
273        }
274    }
275    my @tmpls = (values(%tmpls), values(%global_tmpls));
276    MT->run_callbacks('DefaultTemplateFilter' . ($set ? '.' . $set : ''), \@tmpls);
277    return \@tmpls;
278}
279
2801;
281__END__
282
283=head1 NAME
284
285MT::DefaultTemplates
286
287=head1 METHODS
288
289=head2 templates()
290
291Return the list of the templates in the WeblogTemplatesPath.
292
293=head1 AUTHOR & COPYRIGHT
294
295Please see L<MT/AUTHOR & COPYRIGHT>.
296
297=cut
Note: See TracBrowser for help on using the browser.