arvind / movabletype-templateshelf

This URL has Read+Write access

movabletype-templateshelf / plugins / TemplateShelf / TemplateShelf.pl
100644 104 lines (88 sloc) 2.847 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Template Shelf, part of The Handyman - A plugin for Movable Type.
# Copyright (c) 2007 Arvind Satyanarayan.
 
package MT::Plugin::TemplateShelf;
use strict;
 
use MT 4.0;
use base 'MT::Plugin';
our $VERSION = '1.01';
 
my $plugin;
MT->add_plugin($plugin = __PACKAGE__->new({
name => "Template Shelf",
version => $VERSION,
description => "<__trans phrase=\"Adds a sidebar widget to the edit template screen that provides handy access to the other templates in the blog\">",
author_name => "Arvind Satyanarayan",
author_link => "http://www.movalog.com/",
}));
 
sub init_registry {
my $plugin = shift;
$plugin->registry({
callbacks => {
'MT::App::CMS::template_param.edit_template' => \&_edit_tmpl_param,
'MT::App::CMS::template_source.edit_template' => \&_edit_tmpl
}
});
}
 
sub _edit_tmpl_param {
my ($cb, $app, $param, $tmpl) = @_;
 
    # Somewhere along the line, template list_filters were removed from the
    # registry. Reproducing them here from MT4.0
    my $sys_tmpl = MT->registry( 'default_templates', 'system' )
      || {};
    
my $filters = {
index => {
label => 'Index Templates',
type => 'index'
},
        archive => {
            label => 'Archive Templates',
            type => [ 'individual', 'page', 'archive', 'category' ]
        },
        module => {
            label => 'Template Modules',
            type => 'custom'
        },
        system => {
            label => 'System Templates',
            type => [ keys %$sys_tmpl ]
        }
    };
    
        
    $param->{filters} = $filters;
 
my @tmpl_loop;
require MT::Template;
my $iter = MT::Template->load_iter({ blog_id => [0, $app->param('blog_id')] },
{ sort => 'name', direction => 'ascend' });
while (my $tmpl = $iter->()) {
push @tmpl_loop, {
id => $tmpl->id,
blog_id => $tmpl->blog_id,
type => $tmpl->type,
name => $tmpl->name
}
}
$param->{tmpl_loop} = \@tmpl_loop;
$param->{is_dialog} = $app->param('dialog');
}
 
sub _edit_tmpl {
my ($cb, $app, $tmpl) = @_;
my $old = qq{<mt:include name="include/header.tmpl">};
$old = quotemeta($old);
 
require File::Spec;
my $mini_tmpl = File::Spec->catdir($plugin->path,'tmpl','template_shelf.tmpl');
my $new = qq{<mt:include name="$mini_tmpl">};
$$tmpl =~ s/($old)/$new\n$1/;
 
if($app->param('dialog')) {
$new = <<HTML;
<mt:setvar name="screen_type" value="dialog-screen dialog-4">
<mt:include name="dialog/header.tmpl">
<mt:if name="content_header">
<mt:var name="content_header">
</mt:if>
HTML
$$tmpl =~ s/$old/$new/;
 
$old = qq{<mt:include name="include/footer.tmpl">};
$old = quotemeta($old);
$new = qq{<mt:include name="dialog/footer.tmpl">};
$$tmpl =~ s/$old/$new/;
 
$$tmpl =~ s/setvartemplate/setvarblock/g;
}
}