Report abuse

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
sub save_config {
    my $app        = shift;
    my $q          = $app->can('query') ? $app->query : $app->param;
    my $plugin_sig = $q->param('plugin_sig');
    my $profile    = $MT::Plugins{$plugin_sig};
    my $blog_id    = $q->param('blog_id');
    ###l4p $logger ||= MT::Log::Log4perl->new(); $logger->trace();

    # this should not break out anymore, except for theme settings
    #return unless $blog_id; # this works in blog-context, no sys. plugin cfg
    $app->blog( MT->model('blog')->load($blog_id) ) if $blog_id;

    $app->validate_magic or return;
    return $app->errtrans("Permission denied.")
      unless $app->user->can_manage_plugins
          or (     $blog_id
               and $app->user->permissions($blog_id)->can_edit_templates );

    my $param;
    my @params = $q->param;
    foreach (@params) {
        next
          if $_ =~ m/^(__mode|return_args|plugin_sig|magic_token|blog_id)$/;
        my @vals = $q->param($_);
        if ( $#vals > 0 ) {

            # TODO - should this join items together?
            $param->{$_} = \@vals;
        }
        else {
            $param->{$_} = $vals[0];
        }
    }
    if ( $profile && $profile->{object} ) {
        my $plugin = $profile->{object};
        $plugin->error(undef);
        my $scope = $blog_id ? 'blog:' . $blog_id : 'system';

        #        $plugin->save_config( \%param, $scope );

        # BEGIN - contents of MT::Plugin->save_config
        my $pdata = $plugin->get_config_obj($scope);
        $scope =~ s/:.*//;
        my $data = $pdata->data() || {};

        my $repub_queue;
        my $plugin_changed = 0;

        my @vars = $plugin->config_vars($scope);
        foreach my $var (@vars) {
            my $opt = find_option_def( $app, $var );
            ###l4p $logger->debug( '$opt and var: ',
            ###l4p                 l4mtdump( { opt => $opt, var => $var } ) );

            # TODO - this should be pluggable. Field types should register a pre_save handler
            #        or something
            if ( $opt->{type} eq 'checkbox' ) {
                if ( ref( $param->{$var} ) ne 'ARRAY' && $opt->{'values'} ) {
                    $param->{$var} = [ $param->{$var} ]
                      ;    # Could this be a leak or be weakened?
                }
            }
            if ( $opt->{type} eq 'file' ) {
                my $scope = $opt->{scope} || 'support';
                my $result = process_file_upload( $app, $var, $scope,
                                                  $opt->{destination} );
                if ( $result->{status} == ConfigAssistant::Util::ERROR() ) {
                    return $app->error(
                              "Error uploading file: " . $result->{message} );
                } elsif ( $result->{status} == ConfigAssistant::Util::NO_UPLOAD ) {
                    if ($param->{$var.'-clear'} && $data->{$var}) {
                        my $old = MT->model('asset')->load( $data->{$var} );
                        $old->remove if $old;
                        $param->{$var} = undef;
                    }
                    else {
                        
                        # The user hasn't changed the file--keep it.
                        $param->{$var} = $data->{$var};
                    }
                } else {
                    if ( $data->{$var} ) {
                        my $old = MT->model('asset')->load( $data->{$var} );
                        $old->remove if $old;
                    }
                    $param->{$var} = $result->{asset}->{id};
                }
            }
            my $old = $data->{$var};
            my $new = $param->{$var};
            my $has_changed 
              = ( defined $new and !defined $old )
              || ( defined $old && !defined $new )
              || ( defined $new and $old ne $new );
            ###l4p $logger->debug('$has_changed: '.$has_changed);

            # If the field data has changed, and if the field uses the 
            # "republish" key, we want to republish the specified templates.
            # Add the specified templates to $repub_queue so that they can
            # be republished later.
            if ( $has_changed && $opt && $opt->{'republish'} ) {
                foreach ( split( ',', $opt->{'republish'} ) ) {
                    $repub_queue->{$_} = 1;
                }
            }
            $data->{$var} = $new ? $new : undef;
            if ($has_changed) {
                $opt->{'basename'} = $var;
                #MT->log("Triggering: " . 'options_change.option.' . $var );
                $app->run_callbacks( 'options_change.option.' . $var,
                                     $app, $opt, $old, $new );
                $app->run_callbacks( 'options_change.option.*',
                                     $app, $opt, $old, $new );
                $plugin_changed = 1;
            }
        } ## end foreach my $var (@vars)
        if ($plugin_changed) {

            #MT->log("Triggering: ".'options_change.plugin.' . $plugin->id );
            $app->run_callbacks( 'options_change.plugin.' . $plugin->id,
                                 $app, $plugin );
        }

        # Set the data and save it. This must be done before trying to
        # republish because the new selections the user made are not available
        # until the data is saved.
        $pdata->data($data);
        MT->request( 'plugin_config.' . $plugin->id, undef );
        $pdata->save() or die $pdata->errstr;

        # Index templates that have been flagged should be republished.
        use MT::WeblogPublisher;
        foreach ( keys %$repub_queue ) {
            my $tmpl = MT->model('template')
              ->load( { blog_id => $blog_id, identifier => $_, } );

            if (!$tmpl) {
                MT->log( {
                       blog_id => $blog_id,
                       level   => '2', # Warning
                       message => "Config Assistant could not find a "
                                  . "template with the identifier " . $_,
                     }
                );
                next;
            }

            my $result = $app->rebuild_indexes(
                                   Blog     => $app->blog,
                                   Template => $tmpl,
                                   Force    => 1,
            );

            # Report on the success/failure of the template republishing.
            my ($message, $level);
            if ($result) {
                $message = "Config Assistant: Republishing template " 
                           . $tmpl->name;
                $level   = '1'; # Info
            }
            else {
                $message = "Config Assistant could not republish template " 
                           . $tmpl->name;
                $level   = '4'; # Error
            }
            MT->log( {
                   blog_id => $blog_id,
                   level   => $level,
                   message => $message,
                 }
            );
        }

        # END - contents of MT::Plugin->save_config

        if ( $plugin->errstr ) {
            return $app->error(
                         "Error saving plugin settings: " . $plugin->errstr );
        }
    } ## end if ( $profile && $profile...)

    $app->add_return_arg( saved => $profile->{object}->id );
    $app->call_return;
} ## end sub save_config