Changeset 997
- Timestamp:
- 08/28/08 10:40:30 (3 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/RichEditorSelector/plugins/RichEditorSelector/richeditorselector.pl
r970 r997 8 8 9 9 use base 'MT::Plugin'; 10 our $VERSION = '1. 00';10 our $VERSION = '1.1'; 11 11 12 12 my $plugin = __PACKAGE__->new( … … 23 23 ['editor_name', { Default => 'archetype'}] 24 24 ]), 25 blog_config_template => 'config.tmpl',25 system_config_template => 'config.tmpl', 26 26 } 27 27 ); … … 29 29 MT->add_plugin($plugin); 30 30 MT->add_callback( 'pre_run', 9, $plugin, \&_hdlr_pre_run ); 31 MT->add_callback( 'cms_post_save.author', 9, $plugin, \&_post_save_author ); 32 MT->add_callback( 'MT::App::CMS::template_param.edit_author', 9, $plugin, \&_add_field ); 33 34 sub _post_save_author { 35 my $eh = shift; 36 my ( $app, $obj, $original ) = @_; 37 38 my $q = $app->param; 39 my $value = $q->param('editor_name'); 40 return unless $value; 41 42 my $param; 43 $param->{editor_name} = $value; 44 $plugin->save_config($param, 'system'); 45 } 46 47 sub _add_field { 48 my ( $eh, $app, $param, $tmpl ) = @_; 49 return unless UNIVERSAL::isa( $tmpl, 'MT::Template' ); 50 51 # Load from plugin config 52 my $editor = _get_config_value($app); 53 54 # Load registered editors. 55 my $editors = $app->registry('richtext_editors'); 56 return unless $editor; 57 58 # Make options field 59 my $options; 60 foreach my $key ( keys %$editors ) { 61 my $selected = $editor eq $key ? 'selected="selected"' : ''; 62 $options 63 .= '<option value="' 64 . $key . '" ' 65 . $selected . '>' 66 . $editors->{$key}->{label}->() 67 . '</option>'; 68 } 69 70 # Make innerHTML 71 my $innerHTML 72 = '<select name="editor_name" id="editor_name" class="se">' 73 . $options 74 . '</select>'; 75 76 # Transform 77 my $host_node = $tmpl->getElementById('tag_delim') 78 or return $app->error('cannot get the tag_delim block'); 79 my $block_node = $tmpl->createElement( 80 'app:setting', 81 { id => 'editor_name', 82 label => 'Rich text editor', 83 hint => 'Which kind of rich text editor do you like?', 84 } 85 ) or return $app->error('cannot create the element'); 86 $block_node->innerHTML($innerHTML); 87 $tmpl->insertAfter( $block_node, $host_node ) 88 or return $app->error('failed to insertAfter.'); 89 } 90 31 91 32 92 sub _hdlr_pre_run { 33 93 my ( $cb, $app ) = @_; 34 94 35 my $q = $app->param; 36 my $blog_id = $q->param('blog_id'); 37 return unless $blog_id; 38 39 my $user = $app->user; 40 return unless $user; 41 my $user_id = $user->id; 42 43 my $editor = $plugin->get_config_value( 'editor_name', 44 'blog:' . $blog_id . ':user:' . $user_id ); 45 return unless $editor; 95 # Load 96 my $editor = _get_config_value($app); 46 97 47 98 # Switch editor … … 50 101 51 102 sub load_config { 52 my $plugin = shift;103 my $plugin = shift; 53 104 my ( $param, $scope ) = @_; 54 105 55 my $app = MT->instance; 56 return unless $app->can('user'); 57 $scope .= ':user:' . $app->user->id if $scope =~ m/^blog:/; 58 59 my $editor = $plugin->get_config_value('editor_name', $scope); 60 unless ($editor) { 61 my $default_config = $plugin->settings->defaults; 62 $editor = $default_config->{editor_name}; 63 } 64 106 my $app = MT->instance; 107 my $editor = _get_config_value($app); 65 108 my $editors = $app->registry('richtext_editors'); 66 109 return unless $editor; … … 76 119 my $plugin = shift; 77 120 my $app = MT->instance; 78 return unless $app->can('user');79 121 80 122 my ( $param, $scope ) = @_; 81 $scope .= ':user:' . $app->user->id if $scope =~ m/^blog:/;123 $scope = _get_scope($app); 82 124 $plugin->SUPER::save_config( $param, $scope ); 83 125 } 84 126 85 127 sub reset_config { 86 my $plugin = shift; 87 my $app = MT->instance; 88 return unless $app->can('user'); 128 my $plugin = shift; 129 my $app = MT->instance; 89 130 my ($scope) = @_; 90 $scope .= ':user:' . $app->user->id if $scope =~ m/^blog:/; 131 132 $scope = _get_scope($app); 91 133 $plugin->SUPER::reset_config($scope); 92 134 } 93 135 136 sub _get_scope { 137 my ($app) = @_; 138 139 my $user = $app->user; 140 return unless $user; 141 142 my $user_id = $user->id; 143 return 'configuration:system'.':user:' . $user_id 144 } 145 146 sub _get_config_value { 147 my ($app, $scope) = @_; 148 $scope = _get_scope($app) unless $scope; 149 my $editor = $plugin->get_config_value( 'editor_name', $scope); 150 unless ($editor) { 151 my $default_config = $plugin->settings->defaults; 152 $editor = $default_config->{editor_name}; 153 } 154 return $editor; 155 } 156 94 157 1;
