| 1 | # Movable Type (r) Open Source (C) 2005-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 | # WidgetManager plugin for Movable Type |
|---|
| 8 | # Author: Byrne Reese, Six Apart (http://www.sixapart.com) |
|---|
| 9 | # Released under the Artistic and GPLv2 License |
|---|
| 10 | |
|---|
| 11 | package MT::Plugin::WidgetManager; |
|---|
| 12 | |
|---|
| 13 | use strict; |
|---|
| 14 | |
|---|
| 15 | use base qw( MT::Plugin ); |
|---|
| 16 | use constant DEBUG => 0; |
|---|
| 17 | our $VERSION = '1.0'; |
|---|
| 18 | |
|---|
| 19 | my $plugin = MT::Plugin::WidgetManager->new({ |
|---|
| 20 | id => 'WidgetManager', |
|---|
| 21 | name => 'Widget Manager', |
|---|
| 22 | description => q(<MT_TRANS phrase="Maintain your blog's widget content using a handy drag and drop interface.">), |
|---|
| 23 | version => $VERSION, |
|---|
| 24 | author_name => 'Six Apart', |
|---|
| 25 | key => 'widget-manager', |
|---|
| 26 | l10n_class => 'WidgetManager::L10N', |
|---|
| 27 | }); |
|---|
| 28 | MT->add_plugin($plugin); |
|---|
| 29 | |
|---|
| 30 | sub instance { $plugin; } |
|---|
| 31 | |
|---|
| 32 | sub init_registry { |
|---|
| 33 | my $plugin = shift; |
|---|
| 34 | $plugin->registry({ |
|---|
| 35 | tags => { |
|---|
| 36 | help_url => sub { MT->translate('http://www.movabletype.org/documentation/appendices/tags/%t.html') }, |
|---|
| 37 | function => { |
|---|
| 38 | WidgetManager => '$WidgetManager::WidgetManager::Plugin::_hdlr_widget_manager', |
|---|
| 39 | WidgetSet => '$WidgetManager::WidgetManager::Plugin::_hdlr_widget_manager', |
|---|
| 40 | }, |
|---|
| 41 | }, |
|---|
| 42 | callbacks => { |
|---|
| 43 | 'clone_blog_widgets' => { |
|---|
| 44 | callback => 'MT::Blog::post_clone', |
|---|
| 45 | handler => '$WidgetManager::WidgetManager::Plugin::clone_blog_widgemanagers', |
|---|
| 46 | }, |
|---|
| 47 | 'remove_blog_widgets' => { |
|---|
| 48 | callback => 'MT::Blog::post_remove', |
|---|
| 49 | handler => '$WidgetManager::WidgetManager::Plugin::remove_blog_widgetmanager', |
|---|
| 50 | }, |
|---|
| 51 | 'DefaultTemplateFilter' => '$WidgetManager::MT::Plugin::WidgetManager::default_templates', |
|---|
| 52 | 'MT::Blog::post_create_default_templates' => '$WidgetManager::WidgetManager::Plugin::create_default_widgetsets', |
|---|
| 53 | }, |
|---|
| 54 | applications => { |
|---|
| 55 | cms => { |
|---|
| 56 | methods => { |
|---|
| 57 | list_widget => '$WidgetManager::WidgetManager::CMS::list', |
|---|
| 58 | edit_widget => '$WidgetManager::WidgetManager::CMS::edit', |
|---|
| 59 | delete_widget => '$WidgetManager::WidgetManager::CMS::delete', |
|---|
| 60 | save_widget => '$WidgetManager::WidgetManager::CMS::save', |
|---|
| 61 | }, |
|---|
| 62 | menus => { |
|---|
| 63 | 'design:widgets' => { |
|---|
| 64 | label => 'Widgets', |
|---|
| 65 | mode => 'list_widget', |
|---|
| 66 | order => 200, |
|---|
| 67 | permission => 'edit_templates', |
|---|
| 68 | view => "blog", |
|---|
| 69 | }, |
|---|
| 70 | }, |
|---|
| 71 | template_snippets => { |
|---|
| 72 | 'widget_manager' => { |
|---|
| 73 | label => 'Widget Set', |
|---|
| 74 | content => '<$mt:WidgetSet name="$0"$>', |
|---|
| 75 | trigger => 'widget', |
|---|
| 76 | }, |
|---|
| 77 | }, |
|---|
| 78 | }, |
|---|
| 79 | }, |
|---|
| 80 | }); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | sub load_selected_modules { |
|---|
| 84 | require WidgetManager::Plugin; |
|---|
| 85 | WidgetManager::Plugin::load_selected_modules(@_); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | sub default_templates { |
|---|
| 89 | my $cb = shift; |
|---|
| 90 | my ($tmpl_list) = @_; |
|---|
| 91 | |
|---|
| 92 | my $widgetmgr = MT::Plugin::WidgetManager->instance; |
|---|
| 93 | my $widget_tmpls = $widgetmgr->templates(MT->instance); |
|---|
| 94 | push @$tmpl_list, @$widget_tmpls; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | sub templates { |
|---|
| 98 | my $plugin = shift; |
|---|
| 99 | my ($app) = @_; |
|---|
| 100 | my $default_widget_templates; |
|---|
| 101 | |
|---|
| 102 | use File::Spec; |
|---|
| 103 | my $widgets_dir = File::Spec->catfile($plugin->{full_path}, 'default_widgets'); |
|---|
| 104 | my $cfg_file = File::Spec->catfile($widgets_dir, 'widgets.cfg'); |
|---|
| 105 | |
|---|
| 106 | local(*FH, $_, $/); |
|---|
| 107 | $/ = "\n"; |
|---|
| 108 | open FH, $cfg_file or |
|---|
| 109 | return $app->error(MT->translate( |
|---|
| 110 | "Error opening file '[_1]': [_2]", $cfg_file, "$!" )); |
|---|
| 111 | my $cfg = join('',<FH>); |
|---|
| 112 | eval "$cfg;"; |
|---|
| 113 | close FH; |
|---|
| 114 | |
|---|
| 115 | my @tmpls; |
|---|
| 116 | require MT::Template; |
|---|
| 117 | foreach (@$default_widget_templates) { |
|---|
| 118 | open(TMPL, File::Spec->catfile($widgets_dir, $_->{template})) or die "Error: $!\n"; |
|---|
| 119 | while (my $line = <TMPL>) { |
|---|
| 120 | $_->{text} .= $line; |
|---|
| 121 | } |
|---|
| 122 | close TMPL; |
|---|
| 123 | my $tmpl = MT::Template->new; |
|---|
| 124 | $tmpl->{type} = 'widget'; |
|---|
| 125 | $tmpl->{name} = $plugin->translate($_->{label}); |
|---|
| 126 | $tmpl->{text} = $plugin->translate_templatized($_->{text}); |
|---|
| 127 | push @tmpls, $tmpl; |
|---|
| 128 | } |
|---|
| 129 | return \@tmpls; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | 1; |
|---|
| 133 | __END__ |
|---|