root/branches/release-27/plugins/MultiBlog/multiblog.pl @ 1185

Revision 1185, 12.5 kB (checked in by auno, 23 months ago)

Fixed to handle MTPages and MTFolders context for MultiBlog. BugzID:64928

  • Property svn:keywords set to Id
Line 
1# Movable Type (r) Open Source (C) 2006-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# Original Copyright (c) 2004-2006 David Raynes
8
9package MT::Plugin::MultiBlog;
10
11use strict;
12use warnings;
13
14use base qw( MT::Plugin );
15
16our $VERSION = '2.0';
17my $plugin  = MT::Plugin::MultiBlog->new({
18    id          =>  'multiblog',
19    name        =>  'MultiBlog',
20    description =>  '<MT_TRANS phrase="MultiBlog allows you to publish content from other blogs and define publishing rules and access controls between them.">',
21    version                => $VERSION,
22    author_name            => 'Six Apart, Ltd.',
23    author_link            => 'http://www.sixapart.com/',
24    system_config_template => 'system_config.tmpl',
25    blog_config_template   => 'blog_config.tmpl',
26    settings               => new MT::PluginSettings([
27        [ 'default_access_allowed',     { Default =>  1, Scope => 'system' }],
28        [ 'rebuild_triggers',           { Default => '', Scope => 'blog' } ],
29        [ 'blog_content_accessible',    { Default => '', Scope => 'blog' } ],
30        [ 'other_triggers',             { Scope   => 'blog' } ],
31        [ 'all_triggers',               { Scope   => 'system' } ],
32        [ 'default_mtmultiblog_action', { Default =>  1, Scope => 'blog' } ],
33        [ 'default_mtmulitblog_blogs',  { Default => '', Scope => 'blog' } ],
34    ]),
35    l10n_class => 'MultiBlog::L10N',
36    registry => {
37        applications => {
38            'cms' => {
39                methods => {
40                    multiblog_add_trigger => '$multiblog::MT::Plugin::MultiBlog::add_trigger',
41                },
42            },
43        },
44        tags => {
45            help_url => sub { MT->translate('http://www.movabletype.org/documentation/appendices/tags/%t.html') },
46            block => {
47                Entries => 'MultiBlog::preprocess_native_tags',
48                Categories => 'MultiBlog::preprocess_native_tags',
49                Comments => 'MultiBlog::preprocess_native_tags',
50                Pages => 'MultiBlog::preprocess_native_tags',
51                Folders => 'MultiBlog::preprocess_native_tags',
52                Blogs => 'MultiBlog::preprocess_native_tags',
53                Assets => 'MultiBlog::preprocess_native_tags',
54                Comments => 'MultiBlog::preprocess_native_tags',
55                Pings => 'MultiBlog::preprocess_native_tags',
56                Authors => 'MultiBlog::preprocess_native_tags',
57                Tags => 'MultiBlog::preprocess_native_tags',
58                MultiBlog => 'MultiBlog::Tags::MultiBlog',
59                OtherBlog => 'MultiBlog::Tags::MultiBlog',
60                MultiBlogLocalBlog => 'MultiBlog::Tags::MultiBlogLocalBlog',
61                'MultiBlogIfLocalBlog?' => 'MultiBlog::Tags::MultiBlogIfLocalBlog',
62            },
63            function => {
64                'Include' => 'MultiBlog::preprocess_native_tags',
65                'BlogCategoryCount' => 'MultiBlog::preprocess_native_tags',
66                'BlogEntryCount' => 'MultiBlog::preprocess_native_tags',
67                'BlogPingCount' => 'MultiBlog::preprocess_native_tags',
68                'TagSearchLink' => 'MultiBlog::preprocess_native_tags',
69            },
70        },
71    },
72});
73MT->add_plugin($plugin);
74
75# Register entry post-save callback for rebuild triggers
76MT->add_callback( 'cms_post_save.entry', 10, $plugin,
77    sub { $plugin->runner( 'post_entry_save', @_ ) } );
78
79# Register Comment/TB post-save callbacks for rebuild triggers
80MT->add_callback( 'MT::Comment::post_save', 10, $plugin,
81    sub { $plugin->runner( 'post_feedback_save', 'comment_pub', @_ ) } );
82MT->add_callback( 'MT::TBPing::post_save', 10, $plugin,
83    sub { $plugin->runner( 'post_feedback_save', 'tb_pub', @_ ) } );
84
85sub instance { $plugin }
86
87sub add_trigger {
88    my $app = shift;
89
90    return $plugin->translate("Permission denied.")
91        unless $app->user->is_superuser() ||
92               ($app->blog && $app->user->permissions($app->blog->id)->can_administer_blog());
93
94    my $blog_id = $app->blog->id;
95
96    my $dialog_tmpl = $plugin->load_tmpl('dialog_create_trigger.tmpl');
97    my $tmpl = $app->listing({
98        template => $dialog_tmpl,
99        type => 'blog',
100        code => sub {
101            my ($obj, $row) = @_;
102            if ($obj) {
103                $row->{label} = $obj->name;
104                $row->{link} = $obj->site_url;
105            }
106        },
107        terms => {
108            id => [ $blog_id ],
109        },
110        args => {
111            not => { id => 1 },
112        },
113        params => {
114            panel_type => 'blog',
115            dialog_title => $plugin->translate('MultiBlog'),
116            panel_title => $plugin->translate('Create Trigger'),
117            panel_label => $plugin->translate("Weblog Name"),
118            search_prompt => $plugin->translate("Search Weblogs") . ':',
119            panel_description => $plugin->translate("Description"),
120            panel_multi => 0,
121            panel_first => 1,
122            panel_last => 1,
123            panel_searchable => 1,
124            multiblog_trigger_loop => trigger_loop(),
125            multiblog_action_loop => action_loop(),
126            list_noncron => 1,
127            trigger_caption => $plugin->translate('When this'),
128        },
129    });
130    if (!$app->param('search')) {
131        if (my $loop = $tmpl->param('object_loop')) {
132            unshift @$loop, {
133                id => '_all',
134                label => $plugin->translate('* All Weblogs'),
135                description => $plugin->translate('Select to apply this trigger to all weblogs'),
136            };
137        }
138    }
139    return $app->build_page($tmpl);
140}
141
142sub trigger_loop {
143    [
144        {
145            trigger_key  => 'entry_save',
146            trigger_name => $plugin->translate('saves an entry'),
147        },
148        {
149            trigger_key  => 'entry_pub',
150            trigger_name => $plugin->translate('publishes an entry'),
151        },
152        {
153            trigger_key  => 'comment_pub',
154            trigger_name => $plugin->translate('publishes a comment'),
155        },
156        {
157            trigger_key  => 'tb_pub',
158            trigger_name => $plugin->translate('publishes a TrackBack'),
159        },
160    ];
161}
162
163sub action_loop {
164    [
165        {
166            action_id   => 'ri',
167            action_name => $plugin->translate('rebuild indexes.'),
168        },
169        {
170            action_id   => 'rip',
171            action_name => $plugin->translate('rebuild indexes and send pings.'),
172        },
173    ];
174}
175
176sub load_config {
177    my $plugin = shift;
178    my ($args, $scope) = @_;
179
180    $plugin->SUPER::load_config(@_);
181
182    if ( $scope =~ /blog:(\d+)/ ) {
183        my $blog_id = $1;
184
185        require MT::Blog;
186
187        $args->{multiblog_trigger_loop} = trigger_loop();
188        my %triggers =
189            map { $_->{trigger_key} => $_->{trigger_name} }
190                @{ $args->{multiblog_trigger_loop}};
191
192        $args->{multiblog_action_loop} = action_loop();
193        my %actions =
194            map { $_->{action_id} => $_->{action_name} }
195                @{ $args->{multiblog_action_loop} };
196
197        my $rebuild_triggers = $args->{rebuild_triggers};
198        my @rebuilds = map {
199            my ( $action, $id, $trigger ) = split ( /:/, $_ );
200            if ($id eq '_all') {
201                {
202                    action_name   => $actions{$action},
203                    action_value  => $action,
204                    blog_name     => $plugin->translate('* All Weblogs'),
205                    blog_id       => $id,
206                    trigger_name  => $triggers{$trigger},
207                    trigger_value => $trigger,
208                };
209            } elsif (my $blog = MT::Blog->load($id, { cached_ok => 1 })) {
210                {
211                    action_name   => $actions{$action},
212                    action_value  => $action,
213                    blog_name     => $blog->name,
214                    blog_id       => $id,
215                    trigger_name  => $triggers{$trigger},
216                    trigger_value => $trigger,
217                };
218            } else {
219                ();
220            }
221        } split ( /\|/, $rebuild_triggers );
222        $args->{rebuilds_loop} = \@rebuilds;
223    }
224    my $app = MT->instance;
225    if ($app->isa('MT::App')) {
226        $args->{blog_id} = $app->blog->id if $app->blog;
227    }
228}
229
230sub save_config {
231    my $plugin = shift;
232    my ($args, $scope) = @_;
233
234    $plugin->SUPER::save_config(@_);
235
236    my ($blog_id);
237    if ( $scope =~ /blog:(\d+)/ ) {
238        $blog_id = $1;
239
240        # Save blog-level content aggregation policy to single
241        # system config hash for easy lookup
242        my ($cfg_old, $cfg_new) = 0;
243        my $override = 
244            $plugin->get_config_value( 'access_overrides', "system" ) || {};
245        $cfg_new = $args->{blog_content_accessible};
246        if ( exists $override->{$blog_id} ) {
247            $cfg_old = $override->{$blog_id};
248        }
249        if ( $cfg_old != $cfg_new ) {
250            $override->{$blog_id} = $cfg_new 
251                or delete $override->{$blog_id};
252            $plugin->set_config_value( 'access_overrides'
253                                     , $override
254                                     , 'system' );
255        }
256
257        # Fiddle with rebuild triggers...
258        my $rebuild_triggers = $args->{rebuild_triggers};
259        my $old_triggers     = $args->{old_rebuild_triggers};
260
261        # Check to see if the triggers changed
262        if ( $old_triggers ne $rebuild_triggers ) {
263            # If so, remove all references to the current blog from the triggers cached in other blogs
264            foreach ( split ( /\|/, $old_triggers ) ) {
265                my ( $action, $id, $trigger ) = split ( /:/, $_ );
266                my $name = $id eq '_all' ? "all_triggers" : "other_triggers";
267                my $scope = $id eq '_all' ? "system" : "blog:$id";
268                my $d = $plugin->get_config_value($name, $scope);
269                next unless exists $d->{$trigger}{$blog_id};
270                delete $d->{$trigger}{$blog_id};
271                $plugin->set_config_value($name, $d, $scope);
272            }
273        }
274        foreach ( split ( /\|/, $rebuild_triggers ) ) {
275            my ($action, $id, $trigger) = split ( /:/, $_ );
276            my $name = $id eq '_all' ? "all_triggers" : "other_triggers";
277            my $scope = $id eq '_all' ? "system" : "blog:$id";
278            my $d = $plugin->get_config_value($name, $scope) || {};
279            $d->{$trigger}{$blog_id}{$action} = 1;
280            $plugin->set_config_value($name, $d, $scope);
281        }
282    }
283}
284
285sub reset_config {
286    my $plugin = shift;
287    my ($args, $scope) = @_;
288
289    if ( $scope =~ /blog:(\d+)/ ) {
290        my $blog_id = $1;
291
292        # Get the blogs this one triggers from and update them
293        # And then save the triggers this blog runs
294        my $other_triggers =
295            $plugin->get_config_value( 'other_triggers', $scope );
296        my $rebuild_triggers =
297            $plugin->get_config_value( 'rebuild_triggers', $scope );
298        my $all_triggers =
299            $plugin->get_config_value( 'all_triggers', 'system' );
300
301        foreach ( split ( /\|/, $rebuild_triggers ) ) {
302            my ( $action, $id, $trigger ) = split ( /:/, $_ );
303            next if $id eq '_all';
304            my $d = $plugin->get_config_value( 'other_triggers', "blog:$id" );
305            delete $d->{$trigger}{$blog_id}
306                if exists $d->{$trigger}{$blog_id};
307            $plugin->set_config_value( 'other_triggers', $d, "blog:$id" );
308        }
309        # remove this blog from the 'all_triggers'
310        if ($all_triggers) {
311            my $changed = 0;
312            foreach my $trigger (keys %$all_triggers) {
313                if (exists $all_triggers->{$trigger}{$blog_id}) {
314                    delete $all_triggers->{$trigger}{$blog_id};
315                    $changed = 1;
316                }
317            }
318            if ($changed) {
319                $plugin->set_config_value('all_triggers', $all_triggers, 'system');
320            }
321        }
322        $plugin->SUPER::reset_config(@_);
323        $plugin->set_config_value( 'other_triggers', $other_triggers,
324            "blog:$blog_id" );
325    }
326    else {
327        # reset should not alter the 'all_triggers' element which is
328        # configured through the blog-level settings
329        my $all_triggers = $plugin->get_config_value('all_triggers');
330        $plugin->SUPER::reset_config(@_);
331        $plugin->set_config_value('all_triggers', $all_triggers, 'system');
332    }
333}
334
335# Run-time loading for MultiBlog core methods
336sub runner {
337    my $plugin = shift;
338    my $method = shift;
339    require MultiBlog;
340    no strict 'refs';
341    return $_->( $plugin, @_ ) if $_ = \&{"MultiBlog::$method"};
342    die "Failed to find MultiBlog::$method";
343}
344
3451;
Note: See TracBrowser for help on using the browser.