root/branches/release-39/lib/MT/CMS/Common.pm @ 2502

Revision 2502, 38.4 kB (checked in by fumiakiy, 18 months ago)

Check if build_type is changed to dynamic from something else and do not rebuild if no map is. BugId:80001

  • 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          $app->languages_list( $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 edit( $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
453                # XMLHttpRequest always send text in UTF-8... right?
454                if ( 'utf-8' eq lc($enc) ) {
455                    $q->param( $_, $data->{$_} ) for keys %$data;
456                }
457                else {
458                    foreach ( keys %$data ) {
459                        my $encoded =
460                          MT::I18N::encode_text( $data->{$_}, 'utf-8', $enc );
461                        $q->param( $_, $encoded );
462                    }
463                }
464                $param{'recovered_object'} = 1;
465            }
466            else {
467                $param{'recovered_failed'} = 1;
468            }
469        }
470        else {
471            $param{'recovered_failed'} = 1;
472        }
473    }
474    elsif ( $q->param('qp') ) {
475        foreach (qw( title text )) {
476            my $data = $q->param($_);
477            my $encoded = MT::I18N::encode_text( $data, undef, $enc )
478              if $data;
479            $q->param( $_, $encoded );
480        }
481    }
482
483    $param{autosave_frequency} = $app->config->AutoSaveFrequency;
484
485    my $id     = $q->param('id');
486    my $perms  = $app->permissions;
487    my $author = $app->user;
488    my $cfg    = $app->config;
489    $param{styles} = '';
490    if ( $type eq 'author' ) {
491        if ( $perms || $blog_id ) {
492            return $app->return_to_dashboard( redirect => 1 );
493        }
494    }
495    else {
496        if ( ( !$perms || !$blog_id )
497            && ( $type eq 'entry' || $type eq 'page'
498                 || $type eq 'category' || $type eq 'folder'
499                 || $type eq 'comment'  || $type eq 'commenter'
500                 || $type eq 'ping' ) ) {
501            return $app->return_to_dashboard( redirect => 1 );
502        }
503    }
504
505    my $cols = $class->column_names;
506    require MT::Promise;
507    my $obj_promise = MT::Promise::delay(
508        sub {
509            return $class->load($id) || undef;
510        }
511    );
512
513    if ( !$author->is_superuser ) {
514        $app->run_callbacks( 'cms_view_permission_filter.' . $type,
515            $app, $id, $obj_promise )
516          || return $app->error(
517            $app->translate( "Permission denied: [_1]", $app->errstr() ) );
518    }
519    my $obj;
520    my $blog;
521    my $blog_class = $app->model('blog');
522    if ($blog_id) {
523        $blog = $blog_class->load($blog_id);
524    }
525    else {
526        $blog_id = 0;
527    }
528
529    if ($id) {    # object exists, we're just editing it.
530          # Stash the object itself so we don't have to keep forcing the promise
531        $obj = $obj_promise->force()
532          or return $app->error(
533            $app->translate(
534                "Load failed: [_1]",
535                $class->errstr || $app->translate("(no reason given)")
536            )
537          );
538
539        # Populate the param hash with the object's own values
540        for my $col (@$cols) {
541            $param{$col} =
542              defined $q->param($col) ? $q->param($col) : $obj->$col();
543        }
544
545        # Make certain any blog-specific element matches the blog we're
546        # dealing with. If not, call shenanigans.
547        if (   ( exists $param{blog_id} )
548            && ( $blog_id != ($obj->blog_id || 0) ) )
549        {
550            return $app->return_to_dashboard( redirect => 1 );
551        }
552
553        if ( $class->properties->{audit} ) {
554            my $creator = MT::Author->load(
555                {
556                    id   => $obj->created_by(),
557                    type => MT::Author::AUTHOR()
558                }
559            );
560            if ($creator) {
561                $param{created_by} = $creator->name;
562            }
563            if ( my $mod_by = $obj->modified_by() ) {
564                my $modified = MT::Author->load(
565                    {
566                        id   => $mod_by,
567                        type => MT::Author::AUTHOR()
568                    }
569                );
570                if ($modified) {
571                    $param{modified_by} = $modified->name;
572                }
573                else {
574                    $param{modified_by} = $app->translate("(user deleted)");
575                }
576
577                # Since legacy MT installs will still have a
578                # timestamp type for their modified_on fields,
579                # we cannot reliably disaply a modified on date
580                # by default; we must only show the modification
581                # date IF there is also a modified_by value.
582                if ( my $ts = $obj->modified_on ) {
583                    $param{modified_on_ts} = $ts;
584                    $param{modified_on_formatted} =
585                      format_ts( MT::App::CMS::LISTING_DATETIME_FORMAT(), $ts, undef, $app->user ? $app->user->preferred_language : undef );
586                }
587            }
588            if ( my $ts = $obj->created_on ) {
589                $param{created_on_ts} = $ts;
590                $param{created_on_formatted} =
591                  format_ts( MT::App::CMS::LISTING_DATETIME_FORMAT(), $ts, undef, $app->user ? $app->user->preferred_language : undef );
592            }
593        }
594
595        $param{new_object} = 0;
596    }
597    else {    # object is new
598        $param{new_object} = 1;
599        for my $col (@$cols) {
600            $param{$col} = $q->param($col);
601        }
602    }
603
604    my $res = $app->run_callbacks('cms_edit.' . $type, $app, $id, $obj, \%param);
605    if (!$res) {
606        return $app->error($app->callback_errstr());
607    }
608
609    if ($param{autosave_support}) {
610        # autosave support, but don't bother if we're reediting
611        if ( !$app->param('reedit') ) {
612            my $sess_obj = $app->autosave_session_obj;
613            if ($sess_obj) {
614                $param{autosaved_object_exists} = 1;
615                $param{autosaved_object_ts} =
616                  MT::Util::epoch2ts( $blog, $sess_obj->start );
617            }
618        }
619    }
620
621    if ( ( $q->param('msg') || "" ) eq 'nosuch' ) {
622        $param{nosuch} = 1;
623    }
624    for my $p ( $q->param ) {
625        $param{$p} = $q->param($p) if $p =~ /^saved/;
626    }
627    $param{page_actions} = $app->page_actions($type, $obj);
628    if ( $class->can('class_label') ) {
629        $param{object_label} = $class->class_label;
630    }
631    if ( $class->can('class_label_plural') ) {
632        $param{object_label_plural} = $class->class_label_plural;
633    }
634
635    my $tmpl_file = $param{output} || "edit_${type}.tmpl";
636    $param{object_type} ||= $type;
637    $param{screen_id} ||= "edit-$type";
638    $param{screen_class} .= " edit-$type";
639    return $app->load_tmpl( $tmpl_file, \%param );
640}
641
642sub list {
643    my $app  = shift;
644    my $q    = $app->param;
645    my $type = $q->param('_type');
646
647    return $app->errtrans("Invalid request.")
648      unless $type;
649
650    # being a general-purpose method, lets look for a mode handler
651    # that is specifically for editing this type. if we find it,
652    # reroute to it.
653
654    my $list_mode = 'list_' . $type;
655    if ( my $hdlrs = $app->handlers_for_mode($list_mode) ) {
656        return $app->forward($list_mode);
657    }
658
659    my %param = $_[0] ? %{ $_[0] } : ();
660
661    my $perms = $app->permissions;
662    return $app->return_to_dashboard( redirect => 1 )
663      unless $perms;
664    if (
665        $perms
666        && (   ( $type eq 'blog' && !$perms->can_edit_config )
667            || ( $type eq 'template'     && !$perms->can_edit_templates )
668            || ( $type eq 'notification' && !$perms->can_edit_notifications ) )
669      )
670    {
671        return $app->return_to_dashboard( permission => 1 );
672    }
673    my $id        = $q->param('id');
674    my $class     = $app->model($type) or return;
675    my $blog_id   = $q->param('blog_id');
676    my $list_pref = $app->list_pref($type);
677    my ( %terms, %args );
678    %param = ( %param, %$list_pref );
679    my $cols   = $class->column_names;
680    my $limit  = $list_pref->{rows};
681    my $offset = $app->param('offset') || 0;
682
683    for my $name (@$cols) {
684        $terms{blog_id} = $blog_id, last
685          if $name eq 'blog_id';
686    }
687    if ( $type eq 'notification' ) {
688        $args{direction} = 'descend';
689        $args{offset}    = $offset;
690        $args{limit}     = $limit + 1;
691    }
692    elsif ( $type eq 'banlist' ) {
693        $param{use_plugins} = $app->config->UsePlugins;
694        $limit = 0;
695    }
696    my $iter = $class->load_iter( \%terms, \%args );
697
698    my (
699        @data,         @index_data,  @custom_data,
700        @archive_data, @system_data, @widget_data
701    );
702    my (%authors);
703    my $blog_class = $app->model('blog');
704    my $blog       = $blog_class->load($blog_id);
705    my $set        = $blog ? $blog->template_set : undef;
706    require MT::DefaultTemplates;
707    my $dtmpl = MT::DefaultTemplates->templates($set) || [];
708    my %dtmpl = map { $_->{type} => $_ } @$dtmpl;
709
710    while ( my $obj = $iter->() ) {
711        my $row = $obj->column_values;
712        if ( my $ts = $obj->created_on ) {
713            $row->{created_on_formatted} =
714              format_ts( MT::App::CMS::LISTING_DATE_FORMAT(), $ts, $blog, $app->user ? $app->user->preferred_language : undef );
715            $row->{created_on_time_formatted} =
716              format_ts( MT::App::CMS::LISTING_DATETIME_FORMAT(), $ts, $blog, $app->user ? $app->user->preferred_language : undef );
717            $row->{created_on_relative} = relative_date( $ts, time, $blog );
718        }
719        if ( $type eq 'template' ) {
720            $row->{name} = '' if !defined $row->{name};
721            $row->{name} =~ s/^\s+|\s+$//g;
722            $row->{name} = "(" . $app->translate("No Name") . ")"
723              if $row->{name} eq '';
724
725            if ( $obj->type eq 'index' ) {
726                push @index_data, $row;
727                $row->{rebuild_me} =
728                  defined $row->{rebuild_me} ? $row->{rebuild_me} : 1;
729                my $published_url = $obj->published_url;
730                $row->{published_url} = $published_url if $published_url;
731            }
732            elsif ( $obj->type eq 'custom' ) {
733                push @custom_data, $row;
734            }
735            elsif ( $obj->type eq 'widget' ) {
736                push @widget_data, $row;
737            }
738            elsif ($obj->type eq 'archive'
739                || $obj->type eq 'category'
740                || $obj->type eq 'page'
741                || $obj->type eq 'individual' )
742            {
743
744                # FIXME: enumeration of types
745                push @archive_data, $row;
746            }
747            else {
748                if ( my $def_tmpl = $dtmpl{ $obj->type } ) {
749                    $row->{description} = $def_tmpl->{description_label};
750                }
751                else {
752
753                    # unknown system template; skip over it
754                    # or should we change it to a custom template
755                    # right now?
756                    next;
757                }
758                push @system_data, $row;
759            }
760            $param{search_label} = $app->translate('Templates');
761        }
762        else {
763            if ( $limit && ( scalar @data == $limit ) ) {
764                $param{next_offset} = 1;
765                last;
766            }
767            push @data, $row;
768        }
769        if ( $type eq 'ping' ) {
770            return $app->list_pings();
771            require MT::Trackback;
772            require MT::Entry;
773            my $tb_center = MT::Trackback->load( $obj->tb_id );
774            my $entry     = MT::Entry->load( $tb_center->entry_id )
775                or return $app->error($app->translate('Can\'t load entry #[_1].', $tb_center->entry_id));
776            if ( my $ts = $obj->created_on ) {
777                $row->{created_on_time_formatted} =
778                  format_ts( MT::App::CMS::LISTING_DATETIME_FORMAT(), $ts, $blog, $app->user ? $app->user->preferred_language : undef );
779                $row->{has_edit_access} = $perms->can_edit_all_posts
780                  || $app->user->id == $entry->author_id;
781            }
782        }
783    }    # end loop over the set of objects;
784         # NOW transform the @data array
785    if ( $type eq 'notification' ) {
786        $app->add_breadcrumb( $app->translate('Notification List') );
787        $param{nav_notifications} = 1;
788
789        #@data = sort { $a->{email} cmp $b->{email} } @data;
790        $param{object_type}        = 'notification';
791        $param{list_noncron}       = 1;
792        $param{notification_count} = scalar @data;
793        $param{search_type} = 'entry';
794    }
795    if ( $type eq 'template' ) {
796        $app->add_breadcrumb( $app->translate('Templates') );
797        $param{nav_templates} = 1;
798        for my $ref ( \@index_data, \@custom_data, \@archive_data ) {
799            @$ref = sort { $a->{name} cmp $b->{name} } @$ref;
800        }
801        my $tab = $app->param('tab') || 'index';
802        $param{template_group}      = $tab;
803        $param{"tab_$tab"}          = 1;
804        $param{object_index_loop}   = \@index_data;
805        $param{object_custom_loop}  = \@custom_data;
806        $param{object_widget_loop}  = \@widget_data;
807        $param{object_archive_loop} = \@archive_data;
808        $param{object_system_loop}  = \@system_data;
809        $param{object_type}         = 'template';
810    }
811    else {
812        $param{object_loop} = \@data;
813    }
814
815    # add any breadcrumbs
816    if ( $type eq 'banlist' ) {
817        $app->add_breadcrumb( $app->translate('IP Banning') );
818        $param{nav_config}                       = 1;
819        $param{object_type}                      = 'banlist';
820        $param{show_ip_info}                     = 1;
821        $param{list_noncron}                     = 1;
822        $param{search_type} = 'entry';
823        $param{can_edit_config_or_publish_paths} = $perms->can_edit_config
824          || $perms->can_set_publish_paths;
825    }
826    elsif ( $type eq 'ping' ) {
827        $app->add_breadcrumb( $app->translate('TrackBacks') );
828        $param{nav_trackbacks} = 1;
829        $param{object_type}    = 'ping';
830    }
831    $param{object_count} = scalar @data;
832
833    if ( $type ne 'template' ) {
834        $param{offset}     = $offset;
835        $param{list_start} = $offset + 1;
836        delete $args{limit};
837        delete $args{offset};
838        $param{list_total} = $class->count( \%terms, \%args );
839        $param{list_end}        = $offset + ( scalar @data );
840        $param{next_offset_val} = $offset + ( scalar @data );
841
842    #$param{next_offset} = $param{next_offset_val} < $param{list_total} ? 1 : 0;
843        $param{next_max} = $param{list_total} - $limit;
844        $param{next_max} = 0 if ( $param{next_max} || 0 ) < $offset + 1;
845        if ( $offset > 0 ) {
846            $param{prev_offset}     = 1;
847            $param{prev_offset_val} = $offset - $limit;
848            $param{prev_offset_val} = 0 if $param{prev_offset_val} < 0;
849        }
850    }
851
852    $app->load_list_actions( $type, \%param );
853
854    $param{saved}         = $q->param('saved');
855    $param{saved_deleted} = $q->param('saved_deleted');
856    $param{page_actions}  = $app->page_actions( 'list_' . $type );
857    $param{screen_class} ||= "list-$type";
858    $param{screen_id} ||= "list-$type";
859    $param{listing_screen} = 1;
860    $app->load_tmpl( "list_${type}.tmpl", \%param );
861}
862
863sub delete {
864    my $app  = shift;
865    my $q    = $app->param;
866    my $type = $q->param('_type');
867
868    return $app->errtrans("Invalid request.")
869      unless $type;
870
871    return $app->error( $app->translate("Invalid request.") )
872      if $app->request_method() ne 'POST';
873
874    # being a general-purpose method, lets look for a mode handler
875    # that is specifically for editing this type. if we find it,
876    # reroute to it.
877
878    my $delete_mode = 'delete_' . $type;
879    if ( my $hdlrs = $app->handlers_for_mode($delete_mode) ) {
880        return $app->forward($delete_mode);
881    }
882
883    my $parent  = $q->param('parent');
884    my $blog_id = $q->param('blog_id');
885    my $class   = $app->model($type) or return;
886    my $perms   = $app->permissions;
887    my $author  = $app->user;
888
889    $app->validate_magic() or return;
890
891    my ( $entry_id, $cat_id, $author_id ) = ( "", "", "" );
892    my %rebuild_entries;
893    my @rebuild_cats;
894    my $required_items = 0;
895    for my $id ( $q->param('id') ) {
896        next unless $id;    # avoid 'empty' ids
897        if ( ( $type eq 'association' ) && ( $id =~ /PSEUDO-/ ) ) {
898            require MT::CMS::User;
899            MT::CMS::User::_delete_pseudo_association($app, $id);
900            next;
901        }
902
903        my $obj = $class->load($id);
904        next unless $obj;
905        $app->run_callbacks( 'cms_delete_permission_filter.' . $type,
906            $app, $obj )
907          || return $app->error(
908            $app->translate( "Permission denied: [_1]", $app->errstr() ) );
909
910        if ( $type eq 'comment' ) {
911            $entry_id = $obj->entry_id;
912            $rebuild_entries{$entry_id} = 1 if $obj->visible;
913        }
914        elsif ( $type eq 'ping' || $type eq 'ping_cat' ) {
915            require MT::Trackback;
916            my $tb = MT::Trackback->load( $obj->tb_id );
917            if ($tb) {
918                $entry_id = $tb->entry_id;
919                $cat_id   = $tb->category_id;
920                if ( $obj->visible ) {
921                    $rebuild_entries{$entry_id} = 1 if $entry_id;
922                    push @rebuild_cats, $cat_id if $cat_id;
923                }
924            }
925        }
926        elsif ( $type eq 'tag' ) {
927
928            # if we're in a blog context, remove ONLY tags from that weblog
929            if ($blog_id) {
930                my $ot_class  = $app->model('objecttag');
931                my $obj_type  = $q->param('__type') || 'entry';
932                my $obj_class = $app->model($obj_type);
933                my $iter      = $ot_class->load_iter(
934                    {
935                        blog_id           => $blog_id,
936                        object_datasource => $obj_class->datasource,
937                        tag_id            => $id
938                    },
939                    {
940                        'join' => $obj_class->join_on(
941                            undef,
942                            {
943                                id => \'= objecttag_object_id',
944                                (
945                                    $obj_class =~ m/asset/i
946                                    ? ()
947                                    : ( class => $obj_class->class_type )
948                                )
949                            }
950                        )
951                    }
952                );
953
954                if ($iter) {
955                    my @ot;
956                    while ( my $obj = $iter->() ) {
957                        push @ot, $obj->id;
958                    }
959                    foreach (@ot) {
960                        my $obj = $ot_class->load($_);
961                        next unless $obj;
962                        $obj->remove
963                          or return $app->errtrans( 'Removing tag failed: [_1]',
964                            $obj->errstr );
965                    }
966                }
967
968                $app->run_callbacks( 'cms_post_delete.' . $type, $app, $obj );
969                next;
970
971            }
972        }
973        elsif ( $type eq 'category' ) {
974            my @kids = MT::Category->load( { parent => $id } );
975            return $app->errtrans(
976"You can't delete that category because it has sub-categories. Move or delete the sub-categories first if you want to delete this one."
977            ) if @kids;
978            if ( $app->config('DeleteFilesAtRebuild') ) {
979                require MT::Blog;
980                require MT::Entry;
981                require MT::Placement;
982                my $blog = MT::Blog->load($blog_id)
983                    or return $app->error($app->translate('Can\'t load blog #[_1].', $blog_id));
984                my $at   = $blog->archive_type;
985                if ( $at && $at ne 'None' ) {
986                    my @at = split /,/, $at;
987                    for my $target (@at) {
988                        my $archiver = $app->publisher->archiver($target);
989                        next unless $archiver;
990                        if ( $archiver->category_based ) {
991                            if ( $archiver->date_based ) {
992                                my @entries = MT::Entry->load(
993                                    { status => MT::Entry::RELEASE() },
994                                    {
995                                        join => MT::Placement->join_on(
996                                            'entry_id',
997                                            { category_id => $id },
998                                            { unqiue      => 1 }
999                                        )
1000                                    }
1001                                );
1002                                for (@entries) {
1003                                    $app->publisher->remove_entry_archive_file(
1004                                        Category    => $obj,
1005                                        ArchiveType => $target,
1006                                        Entry       => $_
1007                                    );
1008                                }
1009                            }
1010                            else {
1011                                $app->publisher->remove_entry_archive_file(
1012                                    Category    => $obj,
1013                                    ArchiveType => $target
1014                                );
1015                            }
1016                        }
1017                    }
1018                }
1019            }
1020        }
1021        elsif ( $type eq 'page' ) {
1022            if ( $app->config('DeleteFilesAtRebuild') ) {
1023                $app->publisher->remove_entry_archive_file(
1024                    Entry       => $obj,
1025                    ArchiveType => 'Page'
1026                );
1027            }
1028        }
1029        elsif ( $type eq 'author' ) {
1030            if ( $app->config->ExternalUserManagement ) {
1031                require MT::LDAP;
1032                my $ldap = MT::LDAP->new
1033                  or return $app->error(
1034                    MT->translate(
1035                        "Loading MT::LDAP failed: [_1].",
1036                        MT::LDAP->errstr
1037                    )
1038                  );
1039                my $dn = $ldap->get_dn( $obj->name );
1040                if ($dn) {
1041                    $app->add_return_arg( author_ldap_found => 1 );
1042                }
1043            }
1044        }
1045
1046        # FIXME: enumeration of types
1047        if (   $type eq 'template'
1048            && $obj->type !~
1049            /(custom|index|archive|page|individual|category|widget|backup)/o )
1050        {
1051            $required_items++;
1052        }
1053        else {
1054            $obj->remove
1055              or return $app->errtrans(
1056                'Removing [_1] failed: [_2]',
1057                $app->translate($type),
1058                $obj->errstr
1059              );
1060            $app->run_callbacks( 'cms_post_delete.' . $type, $app, $obj );
1061        }
1062    }
1063    require MT::Entry;
1064    for my $entry_id ( keys %rebuild_entries ) {
1065        my $entry = MT::Entry->load($entry_id);
1066        $app->rebuild_entry( Entry => $entry, BuildDependencies => 1 )
1067            or return $app->publish_error();
1068    }
1069    for my $cat_id (@rebuild_cats) {
1070
1071        # FIXME: What about other category-based archives?
1072        # What if user is not publishing category archives?
1073        my $cat = MT::Category->load($cat_id);
1074        $app->rebuild(
1075            Category    => $cat,
1076            BlogID      => $blog_id,
1077            ArchiveType => 'Category'
1078        ) or return $app->publish_error();
1079    }
1080    $app->run_callbacks( 'rebuild', MT::Blog->load($blog_id) );
1081    $app->add_return_arg(
1082        $type eq 'ping'
1083        ? ( saved_deleted_ping => 1 )
1084        : ( saved_deleted => 1 )
1085    );
1086    if ( $q->param('is_power_edit') ) {
1087        $app->add_return_arg( is_power_edit => 1 );
1088    }
1089    if ($required_items) {
1090        $app->add_return_arg(
1091            error => $app->translate("System templates can not be deleted.") );
1092    }
1093    $app->call_return;
1094}
1095
1096sub not_junk_test {
1097    my ( $eh, $app, $obj ) = @_;
1098    require MT::JunkFilter;
1099    MT::JunkFilter->filter($obj);
1100    $obj->is_junk ? 0 : 1;
1101}
1102
11031;
Note: See TracBrowser for help on using the browser.