root/branches/release-36/lib/MT/DefaultTemplates.pm @ 2052

Revision 2052, 13.7 kB (checked in by fumiakiy, 19 months ago)

Integrated Widget Manager to the core. BugId:68750

Now a widgetset is another type of template. The widgets contained in a widgetset is stored in a meta field.

  • 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        'widget' => {
146            'about_this_page' => {
147                fielname => 'about_this_page.mtml',
148                label => 'About This Page',
149            },
150            'archive_widgets_group' => {
151                fielname => 'archive_widgets_group.mtml',
152                label => 'Archive Widgets Group',
153            },
154            'author_archive_list' => {
155                fielname => 'author_archive_list.mtml',
156                label => 'Author Archives',
157            },
158            'current_author_monthly_archive_list' => {
159                fielname => 'current_author_monthly_archive_list.mtml',
160                label => 'Current Author Monthly Archives',
161            },
162            'calendar' => {
163                fielname => 'calendar.mtml',
164                label => 'Calendar',
165            },
166            'category_archive_list' => {
167                fielname => 'category_archive_list.mtml',
168                label => 'Category Archives',
169            },
170            'current_category_monthly_archive_list' => {
171                fielname => 'current_category_monthly_archive_list.mtml',
172                label => 'Current Category Monthly Archives',
173            },
174            'creative_commons' => {
175                fielname => 'creative_commons.mtml',
176                label => 'Creative Commons',
177            },
178            'main_index_widgets_group' => {
179                fielname => 'main_index_widgets_group.mtml',
180                label => 'Home Page Widgets Group',
181            },
182            'monthly_archive_dropdown' => {
183                fielname => 'monthly_archive_dropdown.mtml',
184                label => 'Monthly Archives Dropdown',
185            },
186            'monthly_archive_list' => {
187                fielname => 'monthly_archive_list.mtml',
188                label => 'Monthly Archives',
189            },
190            'pages_list' => {
191                fielname => 'pages_list.mtml',
192                label => 'Page Listing',
193            },
194            'recent_assets' => {
195                fielname => 'recent_assets.mtml',
196                label => 'Recent Assets',
197            },
198            'powered_by' => {
199                fielname => 'powered_by.mtml',
200                label => 'Powered By',
201            },
202            'recent_comments' => {
203                fielname => 'recent_comments.mtml',
204                label => 'Recent Comments',
205            },
206            'recent_entries' => {
207                fielname => 'recent_entries.mtml',
208                label => 'Recent Entries',
209            },
210            'search' => {
211                fielname => 'search.mtml',
212                label => 'Search',
213            },
214            'signin' => {
215                fielname => 'signin.mtml',
216                label => 'Sign In',
217            },
218            'syndication' => {
219                fielname => 'syndication.mtml',
220                label => 'Syndication',
221            },
222            'tag_cloud' => {
223                fielname => 'tag_cloud.mtml',
224                label => 'Tag Cloud',
225            },
226            'technorati_search' => {
227                fielname => 'technorati_search.mtml',
228                label => 'Technorati Search',
229            },
230            'date_based_author_archives' => {
231                fielname => 'date_based_author_archives.mtml',
232                label => 'Date-Based Author Archives',
233            },
234            'date_based_category_archives' => {
235                fielname => 'date_based_category_archives.mtml',
236                label => 'Date-Based Category Archives',
237            }
238        },
239        'widgetset' => {
240            '2column_layout_sidebar' => {
241                order => 1000,
242                label   => '2-column layout - Sidebar',
243                widgets => [
244                    'Search',
245                    'About This Page',
246                    'Home Page Widgets Group',
247                    'Archive Widgets Group',
248                    'Page Listing',
249                    'Syndication',
250                    'Powered By',
251                ],
252            },
253            '3column_layout_primary_sidebar' => {
254                order => 1000,
255                label   => '3-column layout - Primary Sidebar',
256                widgets => [
257                    'Archive Widgets Group',
258                    'Page Listing',
259                    'Syndication',
260                    'Powered By',
261                ],
262            },
263            '3column_layout_secondary_sidebar' => {
264                order => 1000,
265                label   => '3-column layout - Secondary Sidebar',
266                widgets => [
267                    'Search',
268                    'Home Page Widgets Group',
269                    'About This Page',
270                ],
271            },
272        },
273        'global:module' => {
274            'footer-email' => {
275                label => 'Mail Footer',
276            },
277        },
278        'global:email' => {
279            'comment_throttle' => {
280                label => 'Comment throttle',
281            },
282            'commenter_confirm' => {
283                label => 'Commenter Confirm',
284            },
285            'commenter_notify' => {
286                label => 'Commenter Notify',
287            },
288            'new-comment' => {
289                label => 'New Comment',
290            },
291            'new-ping' => {
292                label => 'New Ping',
293            },
294            'notify-entry' => {
295                label => 'Entry Notify',
296            },
297            'recover-password' => {
298                label => 'Password Recovery',
299            },
300            'verify-subscribe' => {
301                label => 'Subscribe Verify',
302            },
303        },
304    };
305}
306
307sub core_default_templates {
308    return $templates;
309}
310
311sub load {
312    my $class = shift;
313    my ($terms) = @_;
314    my $tmpls = $class->templates || [];
315    if ($terms) {
316        foreach my $key (keys %$terms) {
317            @$tmpls = grep { $_->{$key} eq $terms->{$key} } @$tmpls;
318        }
319    }
320    return wantarray ? @$tmpls : (@$tmpls ? $tmpls->[0] : undef);
321}
322
323sub templates {
324    my $pkg = shift;
325    my ($set) = @_;
326    require File::Spec;
327
328    # A set of default templates as returned by MT::Component->registry
329    # yields an array of hashes.
330
331    my @tmpl_path = $set ? ("template_sets", $set) : ("default_templates");
332    my $all_tmpls = MT::Component->registry(@tmpl_path) || [];
333    my $weblog_templates_path = MT->config('WeblogTemplatesPath');
334
335    my (%tmpls, %global_tmpls);
336    foreach my $def_tmpl (@$all_tmpls) {
337        # copy structure, then run filter
338
339        my $tmpl_hash;
340        if ($def_tmpl->{templates} && ($def_tmpl->{templates} eq '*')) {
341            $tmpl_hash = MT->registry("default_templates");
342        }
343        else {
344            $tmpl_hash = $set ? $def_tmpl->{templates} : $def_tmpl;
345        }
346
347        foreach my $tmpl_set (keys %$tmpl_hash) {
348            next unless ref($tmpl_hash->{$tmpl_set}) eq 'HASH';
349            foreach my $tmpl_id (keys %{ $tmpl_hash->{$tmpl_set} }) {
350                next if $tmpl_id eq 'plugin';
351
352                my $p = $tmpl_hash->{plugin} || $tmpl_hash->{$tmpl_set}{plugin};
353                my $base_path = $def_tmpl->{base_path} || $tmpl_hash->{$tmpl_set}{base_path};
354                if ($p && $base_path) {
355                    $base_path = File::Spec->catdir($p->path, $base_path);
356                }
357                else {
358                    $base_path = $weblog_templates_path;
359                }
360
361                my $tmpl = { %{ $tmpl_hash->{$tmpl_set}{$tmpl_id} } };
362                my $type = $tmpl_set;
363                if ($tmpl_set =~ m/^global:/) {
364                    $type =~ s/^global://;
365                    $tmpl->{global} = 1;
366                }
367                $tmpl->{set} = $type; # system, index, archive, etc.
368                $tmpl->{order} = 0 unless exists $tmpl->{order};
369
370                $type = 'custom' if $type eq 'module';
371                $type = $tmpl_id if $type eq 'system';
372                my $name = $tmpl->{label};
373                $name = $name->() if ref($name) eq 'CODE';
374                $tmpl->{name} = $name;
375                $tmpl->{type} = $type;
376                $tmpl->{key} = $tmpl_id;
377                $tmpl->{identifier} = $tmpl_id;
378
379                # load template if it hasn't been loaded already
380                if (!exists $tmpl->{text}) {
381                    local (*FIN, $/);
382                    my $filename = $tmpl->{filename} || ($tmpl_id . '.mtml');
383                    my $file = File::Spec->catfile($base_path, $filename);
384                    if ((-e $file) && (-r $file)) {
385                        open FIN, "<$file"; my $data = <FIN>; close FIN;
386                        $tmpl->{text} = $data;
387                    } else {
388                        $tmpl->{text} = '';
389                    }
390                }
391
392                my $local_global_tmpls = $tmpl->{global} ? \%global_tmpls : \%tmpls;
393                if (exists $local_global_tmpls->{$tmpl_id}) {
394                    # allow components/plugins to override core
395                    # templates
396                    $local_global_tmpls->{$tmpl_id} = $tmpl if $p && ($p->id ne 'core');
397                }
398                else {
399                    $local_global_tmpls->{$tmpl_id} = $tmpl;
400                }
401            }
402        }
403    }
404    my @tmpls = (values(%tmpls), values(%global_tmpls));
405    @tmpls = sort { $a->{order} <=> $b->{order} } @tmpls;
406    MT->run_callbacks('DefaultTemplateFilter' . ($set ? '.' . $set : ''), \@tmpls);
407    return \@tmpls;
408}
409
4101;
411__END__
412
413=head1 NAME
414
415MT::DefaultTemplates
416
417=head1 METHODS
418
419=head2 templates()
420
421Return the list of the templates in the WeblogTemplatesPath.
422
423=head1 AUTHOR & COPYRIGHT
424
425Please see L<MT/AUTHOR & COPYRIGHT>.
426
427=cut
Note: See TracBrowser for help on using the browser.