root/trunk/lib/MT/CMS/Common.pm @ 3082

Revision 3082, 37.8 kB (checked in by bchoate, 14 months ago)

Merging fireball branch changes to-date to trunk: svn merge -r2974:3081 http://code.sixapart.com/svn/movabletype/branches/fireball .

  • Property svn:keywords set to Id Revision
Line 
1package MT::CMS::Common;
2
3use strict;
4
5use MT::Util qw( format_ts offset_time_list relative_date );
6
7sub save {
8    my $app  = shift;
9    my $q    = $app->param;
10    my $type = $q->param('_type');
11
12    return $app->errtrans("Invalid request.")
13      unless $type;
14
15    # being a general-purpose method, lets look for a mode handler
16    # that is specifically for editing this type. if we find it,
17    # reroute to it.
18
19    my $save_mode = 'save_' . $type;
20    if ( my $hdlrs = $app->handlers_for_mode($save_mode) ) {
21        return $app->forward($save_mode);
22    }
23
24    my $id = $q->param('id');
25    $q->param( 'allow_pings', 0 )
26      if ( $type eq 'category' ) && !defined( $q->param('allow_pings') );
27
28    $app->validate_magic() or return;
29    my $author = $app->user;
30
31    # Check permissions
32    my $perms = $app->permissions;
33
34    if ( !$author->is_superuser ) {
35        if ( ($type ne 'author') && ($type ne 'template') )
36        {    # for authors, blog-ctx $perms is not relevant
37            return $app->errtrans("Permisison denied.")
38              if !$perms && $id;
39        }
40
41        $app->run_callbacks( 'cms_save_permission_filter.' . $type, $app, $id )
42          || return $app->error(
43            $app->translate( "Permission denied: [_1]", $app->errstr() ) );
44    }
45
46    my $param = {};
47    if ( $type eq 'author' ) {
48        if ( my $delim = $q->param('tag_delim') ) {
49            $param->{ 'auth_pref_tag_delim_' . $delim } = 1;
50            $param->{'auth_pref_tag_delim'} = $delim;
51        }
52        $param->{languages} =
53          MT::I18N::languages_list( $app, $q->param('preferred_language') )
54          if $q->param('preferred_language');
55        $param->{create_personal_weblog} =
56          $q->param('create_personal_weblog') ? 1 : 0;
57        require MT::Permission;
58        my $sys_perms = MT::Permission->perms('system');
59        foreach (@$sys_perms) {
60            $param->{ 'perm_can_' . $_->[0] } = 1
61              if $q->param( 'can_' . $_->[0] );
62        }
63    }
64
65    my $filter_result = $app->run_callbacks( 'cms_save_filter.' . $type, $app );
66
67    if ( !$filter_result ) {
68        my %param = (%$param);
69        $param{error}       = $app->errstr;
70        $param{return_args} = $app->param('return_args');
71
72        if ( ( $type eq 'notification' ) || ( $type eq 'banlist' ) ) {
73            return list( $app, \%param );
74        }
75        elsif ( ( $app->param('cfg_screen') || '' ) eq 'cfg_archives' ) {
76            return MT::CMS::Blog::cfg_archives( $app, \%param );
77        }
78        else {
79            if ($type) {
80                my $mode = 'view_' . $type;
81                if ( $app->handlers_for_mode($mode) ) {
82                    return $app->forward( $mode, \%param );
83                }
84            }
85            return $app->forward( 'view', \%param );
86        }
87    }
88
89    return $app->errtrans(
90        'The Template Name and Output File fields are required.')
91      if $type eq 'template' && !$q->param('name') && !$q->param('outfile');
92
93    if ( $type eq 'template' ) {
94
95        # check for autosave
96        if ( $q->param('_autosave') ) {
97            return $app->autosave_object();
98        }
99    }
100
101    my $class = $app->model($type)
102      or return $app->errtrans( "Invalid type [_1]", $type );
103    my ($obj);
104    if ($id) {
105        $obj = $class->load($id)
106            or return $app->error($app->translate("Invalid ID [_1]", $id));
107    }
108    else {
109        $obj = $class->new;
110    }
111
112    my $original = $obj->clone();
113    my $names    = $obj->column_names;
114    my %values   = map { $_ => ( scalar $q->param($_) ) } @$names;
115
116    if ( $type eq 'blog' ) {
117        unless ( $author->is_superuser
118            || ( $perms && $perms->can_administer_blog ) )
119        {
120            if ( $id && !( $perms->can_set_publish_paths ) ) {
121                delete $values{site_url};
122                delete $values{site_path};
123                delete $values{archive_url};
124                delete $values{archive_path};
125            }
126            if ( $id && !( $perms->can_edit_config ) ) {
127                delete $values{$_} foreach grep {
128                         $_ ne 'site_path'
129                      && $_ ne 'site_url'
130                      && $_ ne 'archive_path'
131                      && $_ ne 'archive_url'
132                } @$names;
133            }
134        }
135    }
136
137    if ( $type eq 'author' ) {
138
139        #FIXME: Legacy columns - remove them
140        my @cols = qw(is_superuser can_create_blog can_view_log can_edit_templates);
141        if ( !$author->is_superuser ) {
142            delete $values{$_} for @cols;
143        }
144        else {
145            if ( !$id || ( $author->id != $id ) ) {
146                # Assign the auth_type unless it was assigned
147                # through the form.
148                $obj->auth_type($app->config->AuthenticationModule)
149                    unless $obj->auth_type;
150                if ( $values{'status'} == MT::Author::ACTIVE() ) {
151                    my $sys_perms = MT::Permission->perms('system');
152                    if ( defined($q->param('is_superuser'))
153                      && $q->param('is_superuser')) {
154                        $obj->is_superuser(1);
155                    }
156                    else {
157                        foreach (@$sys_perms) {
158                            my $name = 'can_' . $_->[0];
159                            $name = 'is_superuser' if $name eq 'can_administer';
160                            if ( defined $q->param($name) ) {
161                                $obj->$name( $q->param($name) );
162                                delete $values{$name};
163                            }
164                            else {
165                                $obj->$name(0);
166                            }
167                        }
168                    }
169                }
170            }
171        }
172        delete $values{'password'};
173    }
174
175    if ( $type eq 'blog' ) {
176        # If this is a new blog, set the preferences, archive settings
177        # and template set to the defaults.
178        if ( !$obj->id ) {
179            $obj->language( $app->user->preferred_language );
180            $obj->nofollow_urls(1);
181            $obj->follow_auth_links(1);
182            $obj->page_layout('layout-wtt');
183            my @authenticators = qw( MovableType );
184            foreach my $auth (qw( Vox LiveJournal )) {
185                my $a = MT->commenter_authenticator($auth);
186                if ( !defined $a
187                    || ( exists $a->{condition} && ( !$a->{condition}->() ) ) )
188                {
189                    next;
190                }
191                push @authenticators, $auth;
192            }
193            $obj->commenter_authenticators( join ',', @authenticators );
194            my $set = $app->param('template_set') || 'mt_blog';
195            $obj->template_set( $set );
196        }
197
198        if ( $values{file_extension} ) {
199            $values{file_extension} =~ s/^\.*//
200              if ( $q->param('file_extension') || '' ) ne '';
201        }
202
203        unless ( $values{site_url} =~ m!/$! ) {
204            my $url = $values{site_url};
205            $values{site_url} = $url;
206        }
207    }
208
209    if ( $type eq 'entry' || $type eq 'page' ) {
210
211        # This has to happen prior to callbacks since callbacks may
212        # be affected by the translation...
213
214        # translates naughty words when PublishCharset is NOT UTF-8
215        $app->_translate_naughty_words($obj);
216    }
217
218    if ( $type eq 'template' ) {
219        if (   $q->param('type') eq 'archive'
220            && $q->param('archive_type') )
221        {
222            $values{type} = $q->param('archive_type');
223        }
224    }
225
226    delete $values{'id'} if exists( $values{'id'} ) && !$values{'id'};
227    $obj->set_values( \%values );
228
229    if ( $obj->properties->{audit} ) {
230        $obj->created_by( $author->id ) unless $obj->id;
231        $obj->modified_by( $author->id ) if $obj->id;
232    }
233
234    unless (
235        $app->run_callbacks( 'cms_pre_save.' . $type, $app, $obj, $original ) )
236    {
237        if ( 'blog' eq $type ) {
238            my $meth = $q->param('cfg_screen');
239            if ( $meth && $app->handlers_for_mode($meth) ) {
240                $app->error(
241                    $app->translate( "Save failed: [_1]", $app->errstr ) );
242                return $app->$meth;
243            }
244        }
245        $param->{return_args} = $app->param('return_args');
246        return edit( $app,
247            {
248                %$param,
249                error => $app->translate( "Save failed: [_1]", $app->errstr )
250            }
251        );
252    }
253
254    # Done pre-processing the record-to-be-saved; now save it.
255
256    $obj->touch() if ( $type eq 'blog' );
257
258    $obj->save
259      or return $app->error(
260        $app->translate( "Saving object failed: [_1]", $obj->errstr ) );
261
262    # Now post-process it.
263    $app->run_callbacks( 'cms_post_save.' . $type, $app, $obj, $original )
264      or return $app->error( $app->errstr() );
265
266    # Save NWC settings
267    my $screen = $q->param('cfg_screen') || '';
268    if ( $type eq 'blog' && $screen eq 'cfg_entry' ) {
269        my @fields;
270        push( @fields, 'title' )     if $q->param('nwc_title');
271        push( @fields, 'text' )      if $q->param('nwc_text');
272        push( @fields, 'text_more' ) if $q->param('nwc_text_more');
273        push( @fields, 'keywords' )  if $q->param('nwc_keywords');
274        push( @fields, 'excerpt' )   if $q->param('nwc_excerpt');
275        push( @fields, 'tags' )      if $q->param('nwc_tags');
276        my $fields = @fields ? join( ',', @fields ) : 0;
277        $obj->smart_replace_fields( $fields );
278        $obj->smart_replace( $q->param('nwc_smart_replace') );
279        $obj->save;
280    }
281
282    # Finally, decide where to go next, depending on the object type.
283    my $blog_id = $q->param('blog_id');
284    if ( $type eq 'blog' ) {
285        $blog_id = $obj->id;
286    }
287
288    # TODO: convert this to use $app->call_return();
289    # then templates can determine the page flow.
290    if ( $type eq 'notification' ) {
291        return $app->redirect(
292            $app->uri(
293                'mode' => 'list',
294                args   => {
295                    '_type' => 'notification',
296                    blog_id => $blog_id,
297                    saved   => $obj->email
298                }
299            )
300        );
301    }
302    elsif ( my $cfg_screen = $q->param('cfg_screen') ) {
303        if ( $cfg_screen eq 'cfg_publish_profile' ) {
304            my $dcty = $obj->custom_dynamic_templates || 'none';
305            if ( ( $dcty eq 'all' ) || ( $dcty eq 'archives' ) ) {
306                require MT::CMS::Blog;
307                my %param = ();
308                MT::CMS::Blog::_create_build_order( $app, $obj, \%param );
309                $q->param( 'single_template', 1 ); # to show tmpl full-screen
310                if ( $dcty eq 'all' ) {
311                    $q->param( 'type', $param{build_order} );
312                }
313                elsif ( $dcty eq 'archives' ) {
314                    my @ats = map { $_->{archive_type} } @{ $param{archive_type_loop} };
315                    $q->param( 'type', join( ',', @ats ) );
316                }
317                return MT::CMS::Blog::start_rebuild_pages($app);
318            }
319        }
320        if ( $cfg_screen eq 'cfg_templatemaps' ) {
321            $cfg_screen = 'cfg_archives';
322        }
323        my $site_path = $obj->site_path;
324        my $fmgr      = $obj->file_mgr;
325        unless ( $fmgr->exists($site_path) ) {
326            $fmgr->mkpath($site_path);
327        }
328        $app->add_return_arg( no_writedir => 1 )
329          unless $fmgr->exists($site_path) && $fmgr->can_write($site_path);
330    }
331    elsif ( $type eq 'banlist' ) {
332        return $app->redirect(
333            $app->uri(
334                'mode' => 'list',
335                args   => {
336                    '_type' => 'banlist',
337                    blog_id => $blog_id,
338                    saved   => $obj->ip
339                }
340            )
341        );
342    }
343    elsif ( $type eq 'template' && $q->param('rebuild') ) {
344        if ( !$id ) {
345            # add return argument for newly created templates
346            $app->add_return_arg( id => $obj->id );
347        }
348        if ( $obj->build_type ) {
349            if ( $obj->type eq 'index' ) {
350                $q->param( 'type',            'index-' . $obj->id );
351                $q->param( 'tmpl_id',         $obj->id );
352                $q->param( 'single_template', 1 );
353                return $app->forward( 'start_rebuild' );
354            } else {
355                # archive rebuild support
356                $q->param( 'id', $obj->id );
357                $q->param( 'reedit', $obj->id );
358                return $app->forward( 'publish_archive_templates' );
359            }
360        }
361    }
362    elsif ( $type eq 'template' ) {
363        if (   $obj->type eq 'archive'
364            || $obj->type eq 'category'
365            || $obj->type eq 'page'
366            || $obj->type eq 'individual' )
367        {
368            my $static_maps = delete $app->{static_dynamic_maps};
369            require MT::TemplateMap;
370            my $terms = {};
371            if ( $static_maps && @$static_maps ) {
372                $terms->{id} = $static_maps;
373            }
374            else {
375                # all existing maps have been dynamic
376                # do nothing
377            }
378            if ( %$terms ) {
379                my @maps = MT::TemplateMap->load($terms);
380                my @ats = map { $_->archive_type } @maps;
381                if ($#ats >= 0) {
382                    $q->param( 'type', join( ',', @ats ) );
383                    $q->param( 'with_indexes', 1 );
384                    $q->param( 'no_static', 1 );
385                    $q->param( 'template_id', $obj->id );
386                    $q->param( 'single_template', 1 );
387                    require MT::CMS::Blog;
388                    return MT::CMS::Blog::start_rebuild_pages($app);
389                }
390            }
391        }
392    }
393    elsif ( $type eq 'blog' ) {
394        return $app->redirect(
395            $app->uri(
396                'mode' => 'cfg_prefs',
397                args   => { blog_id => $blog_id, saved => 1 }
398            )
399        );
400    }
401    elsif ( $type eq 'author' ) {
402        # Delete the author's userpic thumb (if any); it'll be regenerated.
403        if ($original->userpic_asset_id != $obj->userpic_asset_id) {
404            my $thumb_file = $original->userpic_file();
405            my $fmgr = MT::FileMgr->new('Local');
406            if ($fmgr->exists($thumb_file)) {
407                $fmgr->delete($thumb_file);
408            }
409        }
410    }
411
412    $app->add_return_arg( 'id' => $obj->id ) if !$original->id;
413    $app->add_return_arg( 'saved' => 1 );
414    $app->call_return;
415}
416
417sub edit {
418    my $app  = shift;
419    my $q    = $app->param;
420    my $type = $q->param('_type');
421
422    return $app->errtrans("Invalid request.")
423      unless $type;
424
425    # being a general-purpose method, lets look for a mode handler
426    # that is specifically for editing this type. if we find it,
427    # reroute to it.
428
429    my $edit_mode = $app->mode . '_' . $type;
430    if ( my $hdlrs = $app->handlers_for_mode($edit_mode) ) {
431        return $app->forward($edit_mode, @_);
432    }
433
434    my %param = eval { $_[0] ? %{ $_[0] } : (); };
435    die Carp::longmess if $@;
436    my $class = $app->model($type) or return;
437    my $blog_id = $q->param('blog_id');
438
439    if ( defined($blog_id) && $blog_id ) {
440        return $app->error( $app->translate("Invalid parameter") )
441          unless ( $blog_id =~ m/\d+/ );
442    }
443
444    $app->remove_preview_file;
445
446    my $enc = $app->config->PublishCharset;
447    if ( $q->param('_recover') ) {
448        my $sess_obj = $app->autosave_session_obj;
449        if ($sess_obj) {
450            my $data = $sess_obj->thaw_data;
451            if ($data) {
452                $q->param( $_, $data->{$_} ) for keys %$data;
453                $param{'recovered_object'} = 1;
454            }
455            else {
456                $param{'recovered_failed'} = 1;
457            }
458        }
459        else {
460            $param{'recovered_failed'} = 1;
461        }
462    }
463    elsif ( $q->param('qp') ) {
464        foreach (qw( title text )) {
465            my $data = $q->param($_);
466            my $encoded = MT::I18N::encode_text( $data, undef, $enc )
467              if $data;
468            $q->param( $_, $encoded );
469        }
470    }
471
472    $param{autosave_frequency} = $app->config->AutoSaveFrequency;
473
474    my $id     = $q->param('id');
475    my $perms  = $app->permissions;
476    my $author = $app->user;
477    my $cfg    = $app->config;
478    $param{styles} = '';
479    if ( $type eq 'author' ) {
480        if ( $perms || $blog_id ) {
481            return $app->return_to_dashboard( redirect => 1 );
482        }
483    }
484    else {
485        if ( ( !$perms || !$blog_id )
486            && ( $type eq 'entry' || $type eq 'page'
487                 || $type eq 'category' || $type eq 'folder'
488                 || $type eq 'comment'  || $type eq 'commenter'
489                 || $type eq 'ping' ) ) {
490            return $app->return_to_dashboard( redirect => 1 );
491        }
492    }
493
494    my $cols = $class->column_names;
495    require MT::Promise;
496    my $obj_promise = MT::Promise::delay(
497        sub {
498            return $class->load($id) || undef;
499        }
500    );
501
502    if ( !$author->is_superuser ) {
503        $app->run_callbacks( 'cms_view_permission_filter.' . $type,
504            $app, $id, $obj_promise )
505          || return $app->error(
506            $app->translate( "Permission denied: [_1]", $app->errstr() ) );
507    }
508    my $obj;
509    my $blog;
510    my $blog_class = $app->model('blog');
511    if ($blog_id) {
512        $blog = $blog_class->load($blog_id);
513    }
514    else {
515        $blog_id = 0;
516    }
517
518    if ($id) {    # object exists, we're just editing it.
519          # Stash the object itself so we don't have to keep forcing the promise
520        $obj = $obj_promise->force()
521          or return $app->error(
522            $app->translate(
523                "Load failed: [_1]",
524                $class->errstr || $app->translate("(no reason given)")
525            )
526          );
527
528        # Populate the param hash with the object's own values
529        for my $col (@$cols) {
530            $param{$col} =
531              defined $q->param($col) ? $q->param($col) : $obj->$col();
532        }
533
534        # Make certain any blog-specific element matches the blog we're
535        # dealing with. If not, call shenanigans.
536        if (   ( exists $param{blog_id} )
537            && ( $blog_id != ($obj->blog_id || 0) ) )
538        {
539            return $app->return_to_dashboard( redirect => 1 );
540        }
541
542        if ( $class->properties->{audit} ) {
543            my $creator = MT::Author->load(
544                {
545                    id   => $obj->created_by(),
546                    type => MT::Author::AUTHOR()
547                }
548            );
549            if ($creator) {
550                $param{created_by} = $creator->name;
551            }
552            if ( my $mod_by = $obj->modified_by() ) {
553                my $modified = MT::Author->load(
554                    {
555                        id   => $mod_by,
556                        type => MT::Author::AUTHOR()
557                    }
558                );
559                if ($modified) {
560                    $param{modified_by} = $modified->name;
561                }
562                else {
563                    $param{modified_by} = $app->translate("(user deleted)");
564                }
565
566                # Since legacy MT installs will still have a
567                # timestamp type for their modified_on fields,
568                # we cannot reliably disaply a modified on date
569                # by default; we must only show the modification
570                # date IF there is also a modified_by value.
571                if ( my $ts = $obj->modified_on ) {
572                    $param{modified_on_ts} = $ts;
573                    $param{modified_on_formatted} =
574                      format_ts( MT::App::CMS::LISTING_DATETIME_FORMAT(), $ts, undef, $app->user ? $app->user->preferred_language : undef );
575                }
576            }
577            if ( my $ts = $obj->created_on ) {
578                $param{created_on_ts} = $ts;
579                $param{created_on_formatted} =
580                  format_ts( MT::App::CMS::LISTING_DATETIME_FORMAT(), $ts, undef, $app->user ? $app->user->preferred_language : undef );
581            }
582        }
583
584        $param{new_object} = 0;
585    }
586    else {    # object is new
587        $param{new_object} = 1;
588        for my $col (@$cols) {
589            $param{$col} = $q->param($col);
590        }
591    }
592
593    my $res = $app->run_callbacks('cms_edit.' . $type, $app, $id, $obj, \%param);
594    if (!$res) {
595        return $app->error($app->callback_errstr());
596    }
597
598    if ($param{autosave_support}) {
599        # autosave support, but don't bother if we're reediting
600        if ( !$app->param('reedit') ) {
601            my $sess_obj = $app->autosave_session_obj;
602            if ($sess_obj) {
603                $param{autosaved_object_exists} = 1;
604                $param{autosaved_object_ts} =
605                  MT::Util::epoch2ts( $blog, $sess_obj->start );
606            }
607        }
608    }
609
610    if ( ( $q->param('msg') || "" ) eq 'nosuch' ) {
611        $param{nosuch} = 1;
612    }
613    for my $p ( $q->param ) {
614        $param{$p} = $q->param($p) if $p =~ /^saved/;
615    }
616    $param{page_actions} = $app->page_actions($type, $obj);
617    if ( $class->can('class_label') ) {
618        $param{object_label} = $class->class_label;
619    }
620    if ( $class->can('class_label_plural') ) {
621        $param{object_label_plural} = $class->class_label_plural;
622    }
623
624    my $tmpl_file = $param{output} || "edit_${type}.tmpl";
625    $param{object_type} ||= $type;
626    $param{screen_id} ||= "edit-$type";
627    $param{screen_class} .= " edit-$type";
628    return $app->load_tmpl( $tmpl_file, \%param );
629}
630
631sub list {
632    my $app  = shift;
633    my $q    = $app->param;
634    my $type = $q->param('_type');
635
636    return $app->errtrans("Invalid request.")
637      unless $type;
638
639    # being a general-purpose method, lets look for a mode handler
640    # that is specifically for editing this type. if we find it,
641    # reroute to it.
642
643    my $list_mode = 'list_' . $type;
644    if ( my $hdlrs = $app->handlers_for_mode($list_mode) ) {
645        return $app->forward($list_mode);
646    }
647
648    my %param = $_[0] ? %{ $_[0] } : ();
649
650    my $perms = $app->permissions;
651    return $app->return_to_dashboard( redirect => 1 )
652      unless $perms;
653    if (
654        $perms
655        && (   ( $type eq 'blog' && !$perms->can_edit_config )
656            || ( $type eq 'template'     && !$perms->can_edit_templates )
657            || ( $type eq 'notification' && !$perms->can_edit_notifications ) )
658      )
659    {
660        return $app->return_to_dashboard( permission => 1 );
661    }
662    my $id        = $q->param('id');
663    my $class     = $app->model($type) or return;
664    my $blog_id   = $q->param('blog_id');
665    my $list_pref = $app->list_pref($type);
666    my ( %terms, %args );
667    %param = ( %param, %$list_pref );
668    my $cols   = $class->column_names;
669    my $limit  = $list_pref->{rows};
670    my $offset = $app->param('offset') || 0;
671
672    for my $name (@$cols) {
673        $terms{blog_id} = $blog_id, last
674          if $name eq 'blog_id';
675    }
676    if ( $type eq 'notification' ) {
677        $args{direction} = 'descend';
678        $args{offset}    = $offset;
679        $args{limit}     = $limit + 1;
680    }
681    elsif ( $type eq 'banlist' ) {
682        $param{use_plugins} = $app->config->UsePlugins;
683        $limit = 0;
684    }
685    my $iter = $class->load_iter( \%terms, \%args );
686
687    my (
688        @data,         @index_data,  @custom_data,
689        @archive_data, @system_data, @widget_data
690    );
691    my (%authors);
692    my $blog_class = $app->model('blog');
693    my $blog       = $blog_class->load($blog_id);
694    my $set        = $blog ? $blog->template_set : undef;
695    require MT::DefaultTemplates;
696    my $dtmpl = MT::DefaultTemplates->templates($set) || [];
697    my %dtmpl = map { $_->{type} => $_ } @$dtmpl;
698
699    while ( my $obj = $iter->() ) {
700        my $row = $obj->column_values;
701        if ( my $ts = $obj->created_on ) {
702            $row->{created_on_formatted} =
703              format_ts( MT::App::CMS::LISTING_DATE_FORMAT(), $ts, $blog, $app->user ? $app->user->preferred_language : undef );
704            $row->{created_on_time_formatted} =
705              format_ts( MT::App::CMS::LISTING_DATETIME_FORMAT(), $ts, $blog, $app->user ? $app->user->preferred_language : undef );
706            $row->{created_on_relative} = relative_date( $ts, time, $blog );
707        }
708        if ( $type eq 'template' ) {
709            $row->{name} = '' if !defined $row->{name};
710            $row->{name} =~ s/^\s+|\s+$//g;
711            $row->{name} = "(" . $app->translate("No Name") . ")"
712              if $row->{name} eq '';
713
714            if ( $obj->type eq 'index' ) {
715                push @index_data, $row;
716                $row->{rebuild_me} =
717                  defined $row->{rebuild_me} ? $row->{rebuild_me} : 1;
718                my $published_url = $obj->published_url;
719                $row->{published_url} = $published_url if $published_url;
720            }
721            elsif ( $obj->type eq 'custom' ) {
722                push @custom_data, $row;
723            }
724            elsif ( $obj->type eq 'widget' ) {
725                push @widget_data, $row;
726            }
727            elsif ($obj->type eq 'archive'
728                || $obj->type eq 'category'
729                || $obj->type eq 'page'
730                || $obj->type eq 'individual' )
731            {
732
733                # FIXME: enumeration of types
734                push @archive_data, $row;
735            }
736            else {
737                if ( my $def_tmpl = $dtmpl{ $obj->type } ) {
738                    $row->{description} = $def_tmpl->{description_label};
739                }
740                else {
741
742                    # unknown system template; skip over it
743                    # or should we change it to a custom template
744                    # right now?
745                    next;
746                }
747                push @system_data, $row;
748            }
749            $param{search_label} = $app->translate('Templates');
750        }
751        else {
752            if ( $limit && ( scalar @data == $limit ) ) {
753                $param{next_offset} = 1;
754                last;
755            }
756            push @data, $row;
757        }
758        if ( $type eq 'ping' ) {
759            return $app->list_pings();
760            require MT::Trackback;
761            require MT::Entry;
762            my $tb_center = MT::Trackback->load( $obj->tb_id );
763            my $entry     = MT::Entry->load( $tb_center->entry_id )
764                or return $app->error($app->translate('Can\'t load entry #[_1].', $tb_center->entry_id));
765            if ( my $ts = $obj->created_on ) {
766                $row->{created_on_time_formatted} =
767                  format_ts( MT::App::CMS::LISTING_DATETIME_FORMAT(), $ts, $blog, $app->user ? $app->user->preferred_language : undef );
768                $row->{has_edit_access} = $perms->can_edit_all_posts
769                  || $app->user->id == $entry->author_id;
770            }
771        }
772    }    # end loop over the set of objects;
773         # NOW transform the @data array
774    if ( $type eq 'notification' ) {
775        $app->add_breadcrumb( $app->translate('Notification List') );
776        $param{nav_notifications} = 1;
777
778        #@data = sort { $a->{email} cmp $b->{email} } @data;
779        $param{object_type}        = 'notification';
780        $param{list_noncron}       = 1;
781        $param{notification_count} = scalar @data;
782        $param{search_type} = 'entry';
783    }
784    if ( $type eq 'template' ) {
785        $app->add_breadcrumb( $app->translate('Templates') );
786        $param{nav_templates} = 1;
787        for my $ref ( \@index_data, \@custom_data, \@archive_data ) {
788            @$ref = sort { $a->{name} cmp $b->{name} } @$ref;
789        }
790        my $tab = $app->param('tab') || 'index';
791        $param{template_group}      = $tab;
792        $param{"tab_$tab"}          = 1;
793        $param{object_index_loop}   = \@index_data;
794        $param{object_custom_loop}  = \@custom_data;
795        $param{object_widget_loop}  = \@widget_data;
796        $param{object_archive_loop} = \@archive_data;
797        $param{object_system_loop}  = \@system_data;
798        $param{object_type}         = 'template';
799    }
800    else {
801        $param{object_loop} = \@data;
802    }
803
804    # add any breadcrumbs
805    if ( $type eq 'banlist' ) {
806        $app->add_breadcrumb( $app->translate('IP Banning') );
807        $param{nav_config}                       = 1;
808        $param{object_type}                      = 'banlist';
809        $param{show_ip_info}                     = 1;
810        $param{list_noncron}                     = 1;
811        $param{search_type} = 'entry';
812        $param{can_edit_config_or_publish_paths} = $perms->can_edit_config
813          || $perms->can_set_publish_paths;
814    }
815    elsif ( $type eq 'ping' ) {
816        $app->add_breadcrumb( $app->translate('TrackBacks') );
817        $param{nav_trackbacks} = 1;
818        $param{object_type}    = 'ping';
819    }
820    $param{object_count} = scalar @data;
821
822    if ( $type ne 'template' ) {
823        $param{offset}     = $offset;
824        $param{list_start} = $offset + 1;
825        delete $args{limit};
826        delete $args{offset};
827        $param{list_total} = $class->count( \%terms, \%args );
828        $param{list_end}        = $offset + ( scalar @data );
829        $param{next_offset_val} = $offset + ( scalar @data );
830
831    #$param{next_offset} = $param{next_offset_val} < $param{list_total} ? 1 : 0;
832        $param{next_max} = $param{list_total} - $limit;
833        $param{next_max} = 0 if ( $param{next_max} || 0 ) < $offset + 1;
834        if ( $offset > 0 ) {
835            $param{prev_offset}     = 1;
836            $param{prev_offset_val} = $offset - $limit;
837            $param{prev_offset_val} = 0 if $param{prev_offset_val} < 0;
838        }
839    }
840
841    $app->load_list_actions( $type, \%param );
842
843    $param{saved}         = $q->param('saved');
844    $param{saved_deleted} = $q->param('saved_deleted');
845    $param{page_actions}  = $app->page_actions( 'list_' . $type );
846    $param{screen_class} ||= "list-$type";
847    $param{screen_id} ||= "list-$type";
848    $param{listing_screen} = 1;
849    $app->load_tmpl( "list_${type}.tmpl", \%param );
850}
851
852sub delete {
853    my $app  = shift;
854    my $q    = $app->param;
855    my $type = $q->param('_type');
856
857    return $app->errtrans("Invalid request.")
858      unless $type;
859
860    return $app->error( $app->translate("Invalid request.") )
861      if $app->request_method() ne 'POST';
862
863    # being a general-purpose method, lets look for a mode handler
864    # that is specifically for editing this type. if we find it,
865    # reroute to it.
866
867    my $delete_mode = 'delete_' . $type;
868    if ( my $hdlrs = $app->handlers_for_mode($delete_mode) ) {
869        return $app->forward($delete_mode);
870    }
871
872    my $parent  = $q->param('parent');
873    my $blog_id = $q->param('blog_id');
874    my $class   = $app->model($type) or return;
875    my $perms   = $app->permissions;
876    my $author  = $app->user;
877
878    $app->validate_magic() or return;
879
880    my ( $entry_id, $cat_id, $author_id ) = ( "", "", "" );
881    my %rebuild_entries;
882    my @rebuild_cats;
883    my $required_items = 0;
884    for my $id ( $q->param('id') ) {
885        next unless $id;    # avoid 'empty' ids
886        if ( ( $type eq 'association' ) && ( $id =~ /PSEUDO-/ ) ) {
887            require MT::CMS::User;
888            MT::CMS::User::_delete_pseudo_association($app, $id);
889            next;
890        }
891
892        my $obj = $class->load($id);
893        next unless $obj;
894        $app->run_callbacks( 'cms_delete_permission_filter.' . $type,
895            $app, $obj )
896          || return $app->error(
897            $app->translate( "Permission denied: [_1]", $app->errstr() ) );
898
899        if ( $type eq 'comment' ) {
900            $entry_id = $obj->entry_id;
901            $rebuild_entries{$entry_id} = 1 if $obj->visible;
902        }
903        elsif ( $type eq 'ping' || $type eq 'ping_cat' ) {
904            require MT::Trackback;
905            my $tb = MT::Trackback->load( $obj->tb_id );
906            if ($tb) {
907                $entry_id = $tb->entry_id;
908                $cat_id   = $tb->category_id;
909                if ( $obj->visible ) {
910                    $rebuild_entries{$entry_id} = 1 if $entry_id;
911                    push @rebuild_cats, $cat_id if $cat_id;
912                }
913            }
914        }
915        elsif ( $type eq 'tag' ) {
916
917            # if we're in a blog context, remove ONLY tags from that weblog
918            if ($blog_id) {
919                my $ot_class  = $app->model('objecttag');
920                my $obj_type  = $q->param('__type') || 'entry';
921                my $obj_class = $app->model($obj_type);
922                my $iter      = $ot_class->load_iter(
923                    {
924                        blog_id           => $blog_id,
925                        object_datasource => $obj_class->datasource,
926                        tag_id            => $id
927                    },
928                    {
929                        'join' => $obj_class->join_on(
930                            undef,
931                            {
932                                id => \'= objecttag_object_id',
933                                (
934                                    $obj_class =~ m/asset/i
935                                    ? ()
936                                    : ( class => $obj_class->class_type )
937                                )
938                            }
939                        )
940                    }
941                );
942
943                if ($iter) {
944                    my @ot;
945                    while ( my $obj = $iter->() ) {
946                        push @ot, $obj->id;
947                    }
948                    foreach (@ot) {
949                        my $obj = $ot_class->load($_);
950                        next unless $obj;
951                        $obj->remove
952                          or return $app->errtrans( 'Removing tag failed: [_1]',
953                            $obj->errstr );
954                    }
955                }
956
957                $app->run_callbacks( 'cms_post_delete.' . $type, $app, $obj );
958                next;
959
960            }
961        }
962        elsif ( $type eq 'category' ) {
963            if ( $app->config('DeleteFilesAtRebuild') ) {
964                require MT::Blog;
965                require MT::Entry;
966                require MT::Placement;
967                my $blog = MT::Blog->load($blog_id)
968                    or return $app->error($app->translate('Can\'t load blog #[_1].', $blog_id));
969                my $at   = $blog->archive_type;
970                if ( $at && $at ne 'None' ) {
971                    my @at = split /,/, $at;
972                    for my $target (@at) {
973                        my $archiver = $app->publisher->archiver($target);
974                        next unless $archiver;
975                        if ( $archiver->category_based ) {
976                            if ( $archiver->date_based ) {
977                                my @entries = MT::Entry->load(
978                                    { status => MT::Entry::RELEASE() },
979                                    {
980                                        join => MT::Placement->join_on(
981                                            'entry_id',
982                                            { category_id => $id },
983                                            { unqiue      => 1 }
984                                        )
985                                    }
986                                );
987                                for (@entries) {
988                                    $app->publisher->remove_entry_archive_file(
989                                        Category    => $obj,
990                                        ArchiveType => $target,
991                                        Entry       => $_
992                                    );
993                                }
994                            }
995                            else {
996                                $app->publisher->remove_entry_archive_file(
997                                    Category    => $obj,
998                                    ArchiveType => $target
999                                );
1000                            }
1001                        }
1002                    }
1003                }
1004            }
1005        }
1006        elsif ( $type eq 'page' ) {
1007            if ( $app->config('DeleteFilesAtRebuild') ) {
1008                $app->publisher->remove_entry_archive_file(
1009                    Entry       => $obj,
1010                    ArchiveType => 'Page'
1011                );
1012            }
1013        }
1014        elsif ( $type eq 'author' ) {
1015            if ( $app->config->ExternalUserManagement ) {
1016                require MT::LDAP;
1017                my $ldap = MT::LDAP->new
1018                  or return $app->error(
1019                    MT->translate(
1020                        "Loading MT::LDAP failed: [_1].",
1021                        MT::LDAP->errstr
1022                    )
1023                  );
1024                my $dn = $ldap->get_dn( $obj->name );
1025                if ($dn) {
1026                    $app->add_return_arg( author_ldap_found => 1 );
1027                }
1028            }
1029        }
1030
1031        # FIXME: enumeration of types
1032        if (   $type eq 'template'
1033            && $obj->type !~
1034            /(custom|index|archive|page|individual|category|widget|backup)/o )
1035        {
1036            $required_items++;
1037        }
1038        else {
1039            $obj->remove
1040              or return $app->errtrans(
1041                'Removing [_1] failed: [_2]',
1042                $app->translate($type),
1043                $obj->errstr
1044              );
1045            $app->run_callbacks( 'cms_post_delete.' . $type, $app, $obj );
1046        }
1047    }
1048    require MT::Entry;
1049    for my $entry_id ( keys %rebuild_entries ) {
1050        my $entry = MT::Entry->load($entry_id);
1051        $app->rebuild_entry( Entry => $entry, BuildDependencies => 1 )
1052            or return $app->publish_error();
1053    }
1054    for my $cat_id (@rebuild_cats) {
1055
1056        # FIXME: What about other category-based archives?
1057        # What if user is not publishing category archives?
1058        my $cat = MT::Category->load($cat_id);
1059        $app->rebuild(
1060            Category    => $cat,
1061            BlogID      => $blog_id,
1062            ArchiveType => 'Category'
1063        ) or return $app->publish_error();
1064    }
1065    $app->run_callbacks( 'rebuild', MT::Blog->load($blog_id) );
1066    $app->add_return_arg(
1067        $type eq 'ping'
1068        ? ( saved_deleted_ping => 1 )
1069        : ( saved_deleted => 1 )
1070    );
1071    if ( $q->param('is_power_edit') ) {
1072        $app->add_return_arg( is_power_edit => 1 );
1073    }
1074    if ($required_items) {
1075        $app->add_return_arg(
1076            error => $app->translate("System templates can not be deleted.") );
1077    }
1078    $app->call_return;
1079}
1080
1081sub not_junk_test {
1082    my ( $eh, $app, $obj ) = @_;
1083    require MT::JunkFilter;
1084    MT::JunkFilter->filter($obj);
1085    $obj->is_junk ? 0 : 1;
1086}
1087
10881;
Note: See TracBrowser for help on using the browser.