root/trunk/CommunityEdit/lib/MT/App/CommunityEdit.pm @ 814

Revision 814, 9.3 kB (checked in by arvind, 17 months ago)

Support to choose multiple categories

Line 
1package MT::App::CommunityEdit;
2
3use strict;
4use base qw( MT::App::Community );
5use MT::Util qw( encode_url );
6use CustomFields::Util qw( field_loop );
7
8sub edit_entry {
9        my $app      = shift;
10    my $entry_id = $app->param('id');
11    my $blog_id  = $app->param('blog_id') || 0;
12    my $user     = $app->_login_user_commenter();
13   
14    if(!$user) { # Redirect them to log in so that we don't have to use the JS
15        my $cfg = $app->config;
16        my $signin_link = $cfg->CGIPath;
17        $signin_link .= '/' unless $signin_link =~ m!/$!;
18        $signin_link .= $cfg->CommunityScript . '?__mode=login&blog_id='.$blog_id;
19        $signin_link .= '&return_to=' . encode_url($app->uri( mode => 'edit_entry', 
20                                                        args => { id => $entry_id, blog_id => $blog_id}));
21                                                       
22        return $app->redirect($signin_link);
23    }
24   
25    my $perms = $user->permissions($blog_id) if $blog_id && $user;
26   
27    require MT::Entry;
28    my $entry;
29   
30    ## Can Create Entry?
31    if ( !$entry_id ) {
32        return $app->errtrans("Permission denied.")
33          unless ( $perms && $perms->can_create_post );
34         
35        $entry = MT::Entry->new();
36    } else {
37        $entry = MT::Entry->load($entry_id); 
38    }
39       
40        ## Can Edit Entry?
41        if($entry_id && $entry) {
42            return $app->error( $app->translate("Permission denied.") )
43          unless $perms && $perms->can_edit_entry( $entry, $user );
44        }
45
46        my $tmpl = $app->load_global_tmpl('edit_entry')
47      or return $app->error("No edit entry template defined");
48       
49    my %param = ();
50    $param{blog_id}  = $blog_id;
51    $param{field_loop} =
52      field_loop( object_type => 'entry', ($entry) ? (object_id => $entry_id) : () );
53        $param{entry_id} = $entry_id;
54        $param{entry_status} = lc $entry->status_text;
55        $param{magic_token} = $app->current_magic;
56
57    my $ctx = $tmpl->context;
58    $ctx->stash('author', $user);
59    $ctx->stash('blog_id', $blog_id) if $blog_id;
60    $ctx->stash('entry', $entry);
61    $tmpl->param( \%param );
62    $tmpl;
63}
64
65sub save_entry {
66    my $app = shift;
67    return
68        unless $app->request_method eq 'POST';
69
70    my $q   = $app->param;
71    my $blog_id = $q->param('blog_id');
72    return $app->errtrans("Invalid request") unless $blog_id;
73
74    my $blog = $app->model('blog.community')->load($blog_id);
75    return $app->errtrans("Invalid request") unless $blog;
76    $app->blog($blog);
77
78    my $user = $app->_login_user_commenter()
79      or return $app->errtrans("Login required");
80    $app->validate_magic;
81   
82    my $perms = $user->permissions($blog_id);
83    my $entry_id = $q->param('id');
84   
85    require MT::Entry;
86    my $entry;
87   
88    if ( !$entry_id ) {
89        return $app->errtrans("Permission denied.")
90          unless ( $perms && $perms->can_create_post );
91    } else {
92        $entry = MT::Entry->load($entry_id);
93    }
94   
95    if($entry) {
96        return $app->error( $app->translate("Permission denied.") )
97              unless $perms->can_edit_entry( $entry, $user );
98    } else {
99        $entry = MT::Entry->new;
100    }
101
102    require MT::Sanitize;
103    my $sanitize_spec = $blog->sanitize_spec
104      || $app->config->GlobalSanitizeSpec;
105    my $title = $q->param('title');
106    $title = $title ? MT::Sanitize->sanitize( $title, $sanitize_spec ) : $title;
107    my $text = $q->param('text');
108    $text = $text ? MT::Sanitize->sanitize( $text, $sanitize_spec ) : $text;
109    if ( $title && $text ) {
110        # for custom fields
111        $q->param('customfield_beacon', 1);
112        $q->param('_type', 'entry');
113
114        {
115            require CustomFields::App::CMS;
116            local $app->{component} = 'Commercial';
117            CustomFields::App::CMS::CMSSaveFilter_customfield_objs( $app, $app )
118                or return $app->error( $app->errstr() );
119        }
120
121        my $orig  = $entry->clone();
122        $entry->title($title);
123        $entry->text($text);
124        $entry->blog_id($blog_id);
125        $entry->author_id( $user->id );
126        my $cb = $user->text_format || $blog->convert_paras || '__default__';
127
128        # richtext is specifically disabled since we don't support
129        # rich text editing blog-side yet.
130        $cb = '__default__' if $cb eq 'richtext';
131
132        $entry->convert_breaks($cb);
133        $entry->allow_comments($blog->allow_comments_default);
134        $entry->allow_pings($blog->allow_pings_default);
135       
136        # If it's published, updates remain published = trust!
137        $entry->status(($entry->status == MT::Entry::RELEASE()) ? 
138                                                    MT::Entry::RELEASE() : MT::Entry::REVIEW());
139
140        # FIXME: This should be in MT::Util, not CMS.
141        require MT::App::CMS;
142        MT::App::CMS::_translate_naughty_words($app, $entry);
143
144        $app->run_callbacks( 'api_pre_save.entry', $app, $entry, $orig )
145          || return $app->error(
146            $app->translate(
147                "Saving [_1] failed: [_2]",
148                $entry->class_label, $app->errstr
149            )
150          );
151
152        $entry->save
153          or return $app->error(
154            $app->translate(
155                "Saving [_1] failed: [_2]", $entry->class_label,
156                $entry->errstr
157            )
158          );
159         
160        my $tags = $app->param('tags');
161        if (defined $tags) {
162            require MT::Tag;
163            my $tag_delim = chr( $app->user->entry_prefs->{tag_delim} );
164            my @tags = MT::Tag->split( $tag_delim, $tags );
165            $entry->set_tags(@tags);
166            $entry->save_tags;
167        } 
168
169        $app->run_callbacks( 'api_post_save.entry', $app, $entry, $orig );
170
171        $app->log(
172            {
173                message => $app->translate(
174                    "[_1] '[_2]' (ID:[_3]) added by user '[_4]'",
175                    $entry->class_label, $entry->title,
176                    $entry->id,          $user->name
177                ),
178                level    => MT::Log::INFO(),
179                class    => 'entry',
180                category => 'new',
181                metadata => $entry->id
182            }
183        );
184
185        # for custom field asset
186        foreach my $p ( $q->param() ) {
187            next if !$q->param($p);
188            if ( $p =~ /^file_customfield_(.*?)$/ ) {
189                if ( my $file_type = $q->param("type_customfield_$1") ) {
190                    my $asset =
191                      $app->_handle_upload( $p, require_type => $file_type );
192                    if ( !defined($asset) && $app->errstr ) {
193                        return $app->error( $app->errstr );
194                    }
195                    if ($asset) {
196                        $q->param( "customfield_$1",
197                            $asset->as_html( { include => 1 } ) );
198                    }
199                }
200            }
201        }
202
203        my $cid = $q->param('category') || $q->param('category_id');
204        my @add_cat;
205            my @param = $q->param();
206            foreach (@param) {
207                if (m/^add_category_id_(\d+)$/) {
208                    push @add_cat, $1;
209                }
210            }       
211       
212        require MT::Placement;
213            my $place = MT::Placement->load({ entry_id => $entry->id, is_primary => 1 });
214            if ($cid) {
215                unless ($place) {
216                    $place = MT::Placement->new;
217                    $place->entry_id($entry->id);
218                    $place->blog_id($entry->blog_id);
219                    $place->is_primary(1);
220                }
221                $place->category_id($cid);
222                $place->save;
223            } else {
224                if ((defined $cid) && ($place)) {
225                    $place->remove;
226                }
227            }
228            my @place = MT::Placement->load({ entry_id => $entry->id, is_primary => 0 });
229            for my $place (@place) {
230                $place->remove;
231            }       
232           
233            for my $cid (@add_cat) {
234                my $place = MT::Placement->new;
235                $place->entry_id($entry->id);
236                $place->blog_id($entry->blog_id);
237                $place->is_primary(0);
238                $place->category_id($cid);
239                $place->save
240                    or return $app->error($app->translate(
241                        "Saving placement failed: [_1]", $place->errstr));
242            }
243
244        {
245            local $app->{component} = 'Commercial';
246            CustomFields::App::CMS::CMSPostSave_customfield_objs( $app, $app, $entry )
247              or return $app->error( $app->errstr() );
248        }
249
250        if ( MT::Entry::RELEASE() == $entry->status ) {
251            $app->rebuild_entry( Entry => $entry->id, BuildDependencies => 1, BuildIndexes => 0 )
252              or return $app->error(
253                $app->translate( "Publish failed: [_1]", $app->errstr ) );
254
255            MT::Util::start_background_task(
256                sub {
257                    $app->rebuild_indexes( Blog => $blog )
258                      or return $app->errtrans( "Publish failed: [_1]", $app->errstr );
259                }
260            );
261        }
262
263        ## Send notification email in the background.
264        if ( my $registration = $app->config->CommenterRegistration ) {
265            if ( my $ids = $registration->{Notify} ) {
266                MT::Util::start_background_task(
267                    sub {
268                        $app->_send_entry_notification( $entry, $blog, $ids );
269                    }
270                );
271            }
272        }
273
274
275        return $app->redirect($app->uri( mode => 'edit_entry', 
276                                                        args => { id => $entry->id, blog_id => $blog_id}));
277    }
278
279    return $app->errtrans('Posting a new entry failed.');
280}
281
2821;
Note: See TracBrowser for help on using the browser.