Changeset 997

Show
Ignore:
Timestamp:
08/28/08 10:40:30 (3 months ago)
Author:
takayama
Message:

* Moved configuration item to the user profile screen.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/RichEditorSelector/plugins/RichEditorSelector/richeditorselector.pl

    r970 r997  
    88 
    99use base 'MT::Plugin'; 
    10 our $VERSION = '1.00'; 
     10our $VERSION = '1.1'; 
    1111 
    1212my $plugin = __PACKAGE__->new( 
     
    2323            ['editor_name', { Default => 'archetype'}] 
    2424        ]), 
    25         blog_config_template => 'config.tmpl', 
     25        system_config_template => 'config.tmpl', 
    2626    } 
    2727); 
     
    2929MT->add_plugin($plugin); 
    3030MT->add_callback( 'pre_run',  9, $plugin, \&_hdlr_pre_run ); 
     31MT->add_callback( 'cms_post_save.author',  9, $plugin, \&_post_save_author ); 
     32MT->add_callback( 'MT::App::CMS::template_param.edit_author', 9, $plugin, \&_add_field ); 
     33 
     34sub _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 
     47sub _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 
    3191 
    3292sub _hdlr_pre_run { 
    3393    my ( $cb, $app ) = @_; 
    3494 
    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); 
    4697 
    4798    # Switch editor 
     
    50101 
    51102sub load_config { 
    52     my $plugin = shift; 
     103    my $plugin            = shift; 
    53104    my ( $param, $scope ) = @_; 
    54105 
    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); 
    65108    my $editors = $app->registry('richtext_editors'); 
    66109    return unless $editor; 
     
    76119    my $plugin = shift; 
    77120    my $app    = MT->instance; 
    78     return unless $app->can('user'); 
    79121 
    80122    my ( $param, $scope ) = @_; 
    81     $scope .= ':user:' . $app->user->id if $scope =~ m/^blog:/
     123    $scope = _get_scope($app)
    82124    $plugin->SUPER::save_config( $param, $scope ); 
    83125} 
    84126 
    85127sub 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; 
    89130    my ($scope) = @_; 
    90     $scope .= ':user:' . $app->user->id if $scope =~ m/^blog:/; 
     131 
     132    $scope = _get_scope($app); 
    91133    $plugin->SUPER::reset_config($scope); 
    92134} 
    93135 
     136sub _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 
     146sub _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 
    941571;