root/trunk/lib/MT/CMS/Entry.pm @ 2994

Revision 2994, 74.6 kB (checked in by takayama, 15 months ago)

Fixed typo

  • Property svn:keywords set to Id Revision
Line 
1package MT::CMS::Entry;
2
3use strict;
4use MT::Util qw( format_ts relative_date remove_html encode_html encode_js
5    encode_url archive_file_for offset_time_list );
6use MT::I18N qw( substr_text const length_text wrap_text encode_text
7    break_up_text first_n_text guess_encoding );
8
9sub edit {
10    my $cb = shift;
11    my ($app, $id, $obj, $param) = @_;
12
13    my $q = $app->param;
14    my $type = $q->param('_type');
15    my $perms = $app->permissions;
16    my $blog_class = $app->model('blog');
17    my $blog = $app->blog;
18    my $blog_id = $blog->id;
19    my $author = $app->user;
20    my $class = $app->model($type);
21
22    # to trigger autosave logic in main edit routine
23    $param->{autosave_support} = 1;
24
25    if ($id) {
26        return $app->error( $app->translate("Invalid parameter") )
27          if $obj->class ne $type;
28
29        $param->{nav_entries} = 1;
30        $param->{entry_edit}  = 1;
31        if ( $type eq 'entry' ) {
32            $app->add_breadcrumb(
33                $app->translate('Entries'),
34                $app->uri(
35                    'mode' => 'list_entries',
36                    args   => { blog_id => $blog_id }
37                )
38            );
39        }
40        elsif ( $type eq 'page' ) {
41            $app->add_breadcrumb(
42                $app->translate('Pages'),
43                $app->uri(
44                    'mode' => 'list_pages',
45                    args   => { blog_id => $blog_id }
46                )
47            );
48        }
49        $app->add_breadcrumb( $obj->title
50              || $app->translate('(untitled)') );
51        ## Don't pass in author_id, because it will clash with the
52        ## author_id parameter of the author currently logged in.
53        delete $param->{'author_id'};
54        unless ( defined $q->param('category_id') ) {
55            delete $param->{'category_id'};
56            if ( my $cat = $obj->category ) {
57                $param->{category_id} = $cat->id;
58            }
59        }
60        $blog_id = $obj->blog_id;
61        my $blog = $app->model('blog')->load($blog_id);
62        my $status = $q->param('status') || $obj->status;
63        $param->{ "status_" . MT::Entry::status_text($status) } = 1;
64        $param->{ "allow_comments_"
65              . ( $q->param('allow_comments') || $obj->allow_comments || 0 )
66          } = 1;
67        $param->{'authored_on_date'} = $q->param('authored_on_date')
68          || format_ts( "%Y-%m-%d", $obj->authored_on, $blog, $app->user ? $app->user->preferred_language : undef );
69        $param->{'authored_on_time'} = $q->param('authored_on_time')
70          || format_ts( "%H:%M:%S", $obj->authored_on, $blog, $app->user ? $app->user->preferred_language : undef );
71
72        $param->{num_comments} = $id ? $obj->comment_count : 0;
73        $param->{num_pings} = $id ? $obj->ping_count : 0;
74
75        # Check permission to send notifications and if the
76        # blog has notification list subscribers
77        if (   $perms->can_send_notifications
78            && $obj->status == MT::Entry::RELEASE() )
79        {
80            my $not_class = $app->model('notification');
81            $param->{can_send_notifications} = 1;
82            $param->{has_subscribers} =
83              $not_class->exist( { blog_id => $blog_id } );
84        }
85
86        ## Load next and previous entries for next/previous links
87        if ( my $next = $obj->next ) {
88            $param->{next_entry_id} = $next->id;
89        }
90        if ( my $prev = $obj->previous ) {
91            $param->{previous_entry_id} = $prev->id;
92        }
93
94        $param->{has_any_pinged_urls} = ( $obj->pinged_urls || '' ) =~ m/\S/;
95        $param->{ping_errors}         = $q->param('ping_errors');
96        $param->{can_view_log}        = $app->user->can_view_log;
97        $param->{entry_permalink}     = $obj->permalink;
98        $param->{'mode_view_entry'}   = 1;
99        $param->{'basename_old'}      = $obj->basename;
100
101        if ( my $ts = $obj->authored_on ) {
102            $param->{authored_on_ts} = $ts;
103            $param->{authored_on_formatted} =
104              format_ts( MT::App::CMS::LISTING_DATETIME_FORMAT(), $ts, $blog, $app->user ? $app->user->preferred_language : undef );
105        }
106
107        $app->load_list_actions( $type, $param );
108    } else {
109        $param->{entry_edit} = 1;
110        if ($blog_id) {
111            if ( $type eq 'entry' ) {
112                $app->add_breadcrumb(
113                    $app->translate('Entries'),
114                    $app->uri(
115                        'mode' => 'list_entries',
116                        args   => { blog_id => $blog_id }
117                    )
118                );
119                $app->add_breadcrumb( $app->translate('New Entry') );
120                $param->{nav_new_entry} = 1;
121            }
122            elsif ( $type eq 'page' ) {
123                $app->add_breadcrumb(
124                    $app->translate('Pages'),
125                    $app->uri(
126                        'mode' => 'list_pages',
127                        args   => { blog_id => $blog_id }
128                    )
129                );
130                $app->add_breadcrumb( $app->translate('New Page') );
131                $param->{nav_new_page} = 1;
132            }
133        }
134
135        # (if there is no blog_id parameter, this is a
136        # bookmarklet post and doesn't need breadcrumbs.)
137        delete $param->{'author_id'};
138        delete $param->{'pinged_urls'};
139        my $blog_timezone = 0;
140        if ($blog_id) {
141            my $blog = $blog_class->load($blog_id)
142                or return $app->error($app->translate('Can\'t load blog #[_1].', $blog_id));
143            $blog_timezone = $blog->server_offset();
144
145            # new entry defaults used for new entries AND new pages.
146            my $def_status = $q->param('status')
147              || $blog->status_default;
148            if ($def_status) {
149                $param->{ "status_"
150                      . MT::Entry::status_text($def_status) } = 1;
151            }
152            if ( $param->{status} ) {
153                $param->{ 'allow_comments_'
154                      . $q->param('allow_comments') } = 1;
155                $param->{allow_comments} = $q->param('allow_comments');
156                $param->{allow_pings}    = $q->param('allow_pings');
157            }
158            else {
159                # new edit
160                $param->{ 'allow_comments_'
161                      . $blog->allow_comments_default } = 1;
162                $param->{allow_comments} = $blog->allow_comments_default;
163                $param->{allow_pings}    = $blog->allow_pings_default;
164            }
165        }
166
167        require POSIX;
168        my @now = offset_time_list( time, $blog );
169        $param->{authored_on_date} = $q->param('authored_on_date')
170          || POSIX::strftime( "%Y-%m-%d", @now );
171        $param->{authored_on_time} = $q->param('authored_on_time')
172          || POSIX::strftime( "%H:%M:%S", @now );
173    }
174
175    ## Load categories and process into loop for category pull-down.
176    require MT::Placement;
177    my $cat_id = $param->{category_id};
178    my $depth  = 0;
179    my %places;
180
181    # set the dirty flag in js?
182    $param->{dirty} = $q->param('dirty') ? 1 : 0;
183
184    if ($id) {
185        my @places =
186          MT::Placement->load( { entry_id => $id, is_primary => 0 } );
187        %places = map { $_->category_id => 1 } @places;
188    }
189    my $cats = $q->param('category_ids');
190    if ( defined $cats ) {
191        if ( my @cats = split /,/, $cats ) {
192            $cat_id = $cats[0];
193            %places = map { $_ => 1 } @cats;
194        }
195    }
196    if ( $q->param('reedit') ) {
197        $param->{reedit} = 1;
198        if ( !$q->param('basename_manual') ) {
199            $param->{'basename'} = '';
200        }
201    }
202    if ($blog) {
203        $param->{file_extension} = $blog->file_extension || '';
204        $param->{file_extension} = '.' . $param->{file_extension}
205          if $param->{file_extension} ne '';
206    }
207    else {
208        $param->{file_extension} = 'html';
209    }
210
211    ## Now load user's preferences and customization for new/edit
212    ## entry page.
213    if ($perms) {
214        my $pref_param = $app->load_entry_prefs( $perms->entry_prefs );
215        %$param = ( %$param, %$pref_param );
216        $param->{disp_prefs_bar_colspan} = $param->{new_object} ? 1 : 2;
217
218        # Completion for tags
219        my $auth_prefs = $author->entry_prefs;
220        if ( my $delim = chr( $auth_prefs->{tag_delim} ) ) {
221            if ( $delim eq ',' ) {
222                $param->{'auth_pref_tag_delim_comma'} = 1;
223            }
224            elsif ( $delim eq ' ' ) {
225                $param->{'auth_pref_tag_delim_space'} = 1;
226            }
227            else {
228                $param->{'auth_pref_tag_delim_other'} = 1;
229            }
230            $param->{'auth_pref_tag_delim'} = $delim;
231        }
232
233        require JSON;
234        my $json = JSON->new( autoconv => 0 ); # stringifies numbers this way
235        $param->{tags_js} =
236          $json->objToJson(
237            MT::Tag->cache( blog_id => $blog_id, class => 'MT::Entry', private => 1 )
238          );
239
240        $param->{can_edit_categories} = $perms->can_edit_categories;
241    }
242
243    my $data = $app->_build_category_list(
244        blog_id => $blog_id,
245        markers => 1,
246        type    => $class->container_type,
247    );
248    my $top_cat = $cat_id;
249    my @sel_cats;
250    my $cat_tree = [];
251    if ( $type eq 'page' ) {
252        push @$cat_tree,
253          {
254            id    => -1,
255            label => '/',
256            basename => '/',
257            path  => [],
258          };
259        $top_cat ||= -1;
260    }
261    foreach (@$data) {
262        next unless exists $_->{category_id};
263        if ( $type eq 'page' ) {
264            $_->{category_path_ids} ||= [];
265            unshift @{ $_->{category_path_ids} }, -1;
266        }
267        push @$cat_tree,
268          {
269            id => $_->{category_id},
270            label => $_->{category_label} . ( $type eq 'page' ? '/' : '' ),
271            basename => $_->{category_basename} . ( $type eq 'page' ? '/' : '' ),
272            path => $_->{category_path_ids} || [],
273          };
274        push @sel_cats, $_->{category_id}
275          if $places{ $_->{category_id} }
276          && $_->{category_id} != $cat_id;
277    }
278    $param->{category_tree} = $cat_tree;
279    unshift @sel_cats, $top_cat if defined $top_cat && $top_cat ne "";
280    $param->{selected_category_loop}   = \@sel_cats;
281    $param->{have_multiple_categories} = scalar @$data > 1;
282
283    $param->{basename_limit} = ( $blog ? $blog->basename_limit : 0 ) || 30;
284
285    if ( $q->param('tags') ) {
286        $param->{tags} = $q->param('tags');
287    }
288    else {
289        if ($obj) {
290            my $tag_delim = chr( $app->user->entry_prefs->{tag_delim} );
291            require MT::Tag;
292            my $tags = MT::Tag->join( $tag_delim, $obj->tags );
293            $param->{tags} = $tags;
294        }
295    }
296
297    ## Load text filters if user displays them
298    my %entry_filters;
299    if ( defined( my $filter = $q->param('convert_breaks') ) ) {
300        $entry_filters{$filter} = 1;
301    }
302    elsif ($obj) {
303        %entry_filters = map { $_ => 1 } @{ $obj->text_filters };
304    }
305    elsif ($blog) {
306        my $cb = $author->text_format || $blog->convert_paras;
307        $cb = '__default__' if $cb eq '1';
308        $entry_filters{$cb} = 1;
309        $param->{convert_breaks} = $cb;
310    }
311    my $filters = MT->all_text_filters;
312    $param->{text_filters} = [];
313    for my $filter ( keys %$filters ) {
314        push @{ $param->{text_filters} },
315          {
316            filter_key      => $filter,
317            filter_label    => $filters->{$filter}{label},
318            filter_selected => $entry_filters{$filter},
319            filter_docs     => $filters->{$filter}{docs},
320          };
321    }
322    $param->{text_filters} =
323      [ sort { $a->{filter_key} cmp $b->{filter_key} }
324          @{ $param->{text_filters} } ];
325    unshift @{ $param->{text_filters} },
326      {
327        filter_key      => '0',
328        filter_label    => $app->translate('None'),
329        filter_selected => ( !keys %entry_filters ),
330      };
331
332    if ($blog) {
333        if ( !defined $param->{convert_breaks} ) {
334            my $cb = $blog->convert_paras;
335            $cb = '__default__' if $cb eq '1';
336            $param->{convert_breaks} = $cb;
337        }
338        my $ext = ( $blog->file_extension || '' );
339        $ext = '.' . $ext if $ext ne '';
340        $param->{blog_file_extension} = $ext;
341    }
342
343    my $rte;
344    if ($param->{convert_breaks} eq 'richtext') {
345        ## Rich Text editor
346        $rte = lc($app->config('RichTextEditor'));
347    }
348    else {
349        $rte = 'archetype';
350    }
351    my $editors = $app->registry("richtext_editors");
352    my $edit_reg = $editors->{$rte} || $editors->{archetype};
353    my $rich_editor_tmpl;
354    if ($rich_editor_tmpl = $edit_reg->{plugin}->load_tmpl($edit_reg->{template})) {
355        $param->{rich_editor} = $rte;
356        $param->{rich_editor_tmpl} = $rich_editor_tmpl;
357    }
358
359    $param->{object_type}  = $type;
360    $param->{quickpost_js} = MT::CMS::Entry::quickpost_js($app, $type);
361    if ( 'page' eq $type ) {
362        $param->{search_label} = $app->translate('pages');
363        $param->{output}       = 'edit_entry.tmpl';
364        $param->{screen_class} = 'edit-page edit-entry';
365    }
366    $param->{sitepath_configured} = $blog && $blog->site_path ? 1 : 0;
367    1;
368}
369
370sub list {
371    my $app = shift;
372    my ($param) = @_;
373    $param ||= {};
374
375    require MT::Entry;
376    my $type = $param->{type} || MT::Entry->class_type;
377    my $pkg = $app->model($type) or return "Invalid request.";
378
379    my $q     = $app->param;
380    my $perms = $app->permissions;
381    if ( $type eq 'page' ) {
382        if ( $perms
383            && ( !$perms->can_manage_pages ) )
384        {
385            return $app->errtrans("Permission denied.");
386        }
387    }
388    else {
389        if (
390            $perms
391            && (   !$perms->can_edit_all_posts
392                && !$perms->can_create_post
393                && !$perms->can_publish_post )
394          )
395        {
396            return $app->errtrans("Permission denied.");
397        }
398    }
399
400    my $list_pref = $app->list_pref($type);
401    my %param     = %$list_pref;
402    my $blog_id   = $q->param('blog_id');
403    my %terms;
404    $terms{blog_id} = $blog_id if $blog_id;
405    $terms{class} = $type;
406    my $limit = $list_pref->{rows};
407    my $offset = $app->param('offset') || 0;
408
409    if ( !$blog_id && !$app->user->is_superuser ) {
410        require MT::Permission;
411        $terms{blog_id} = [
412            map { $_->blog_id }
413              grep { $_->can_create_post || $_->can_edit_all_posts }
414              MT::Permission->load( { author_id => $app->user->id } )
415        ];
416    }
417
418    my %arg;
419    my $filter_key = $q->param('filter_key') || '';
420    my $filter_col = $q->param('filter')     || '';
421    my $filter_val = $q->param('filter_val');
422
423    # check blog_id for deciding to apply category filter or not
424    my ( $filter_name, $filter_value );    # human-readable versions
425    if ( !exists( $terms{$filter_col} ) ) {
426        if ( $filter_col eq 'category_id' ) {
427            $filter_name = $app->translate('Category');
428            require MT::Category;
429            my $cat = MT::Category->load($filter_val);
430            return $app->errtrans( "Load failed: [_1]", MT::Category->errstr )
431              unless $cat;
432            if ( $cat->blog_id != $blog_id ) {
433                $filter_key = '';
434                $filter_col = '';
435                $filter_val = '';
436            }
437            $filter_value = $cat->label;
438        }
439        elsif ( $filter_col eq 'asset_id' ) {
440            $filter_name = $app->translate('Asset');
441            my $asset_class = MT->model('asset');
442            my $asset = $asset_class->load($filter_val);
443            return $app->errtrans( "Load failed: [_1]", $asset_class->errstr )
444              unless $asset;
445            if ($asset->blog_id != $blog_id) {
446                $filter_key = '';
447                $filter_col = '';
448                $filter_val = '';
449            }
450            $filter_value = $param{asset_label} = $asset->label;
451        }
452    }
453    if ( $filter_col && $filter_val ) {
454        if ( 'power_edit' eq $filter_col ) {
455            $filter_col = 'id';
456            unless ( 'ARRAY' eq ref($filter_val) ) {
457                my @values = $app->param('filter_val');
458                $filter_val = \@values;
459            }
460        }
461        if ( !exists( $terms{$filter_col} ) ) {
462            if ( $filter_col eq 'category_id' ) {
463                $arg{'join'} = MT::Placement->join_on(
464                    'entry_id',
465                    { category_id => $filter_val },
466                    { unique      => 1 }
467                );
468            }
469            elsif ( $filter_col eq 'asset_id' ) {
470                $arg{'join'} = MT->model('objectasset')->join_on(
471                    'object_id',
472                    { object_ds => $type,
473                      asset_id  => $filter_val },
474                    { unique    => 1 }
475                );
476            }
477            elsif (( $filter_col eq 'normalizedtag' )
478                || ( $filter_col eq 'exacttag' ) )
479            {
480                my $normalize = ( $filter_col eq 'normalizedtag' );
481                require MT::Tag;
482                require MT::ObjectTag;
483                my $tag_delim   = chr( $app->user->entry_prefs->{tag_delim} );
484                my @filter_vals = MT::Tag->split( $tag_delim, $filter_val );
485                my @filter_tags = @filter_vals;
486                if ($normalize) {
487                    push @filter_tags, MT::Tag->normalize($_)
488                      foreach @filter_vals;
489                }
490                my @tags = MT::Tag->load( { name => [@filter_tags] },
491                    { binary => { name => 1 } } );
492                my @tag_ids;
493                foreach (@tags) {
494                    push @tag_ids, $_->id;
495                    if ($normalize) {
496                        my @more = MT::Tag->load(
497                            { n8d_id => $_->n8d_id ? $_->n8d_id : $_->id } );
498                        push @tag_ids, $_->id foreach @more;
499                    }
500                }
501                @tag_ids = (0) unless @tags;
502                $arg{'join'} = MT::ObjectTag->join_on(
503                    'object_id',
504                    {
505                        tag_id            => \@tag_ids,
506                        object_datasource => $pkg->datasource
507                    },
508                    { unique => 1 }
509                );
510            }
511            else {
512                $terms{$filter_col} = $filter_val;
513            }
514            $param{filter_args} = "&filter=" . encode_url($filter_col) . "&filter_val=" . encode_url($filter_val);
515
516            if (   ( $filter_col eq 'normalizedtag' )
517                || ( $filter_col eq 'exacttag' ) )
518            {
519                $filter_name  = $app->translate('Tag');
520                $filter_value = $filter_val;
521            }
522            elsif ( $filter_col eq 'author_id' ) {
523                $filter_name = $app->translate('User');
524                my $author = MT::Author->load($filter_val);
525                return $app->errtrans( "Load failed: [_1]", MT::Author->errstr )
526                  unless $author;
527                $filter_value = $author->name;
528            }
529            elsif ( $filter_col eq 'status' ) {
530                $filter_name = $app->translate('Entry Status');
531                $filter_value =
532                  $app->translate( MT::Entry::status_text($filter_val) );
533            }
534            if ( $filter_name && $filter_value ) {
535                $param{filter}                        = $filter_col;
536                $param{ 'filter_col_' . $filter_col } = 1;
537                $param{filter_val}                    = $filter_val;
538            }
539        }
540        $param{filter_unpub} = $filter_col eq 'status';
541    }
542    elsif ($filter_key) {
543        my $filters = $app->registry("list_filters", "entry") || {};
544        if ( my $filter = $filters->{$filter_key} ) {
545            if ( my $code = $filter->{code}
546                || $app->handler_to_coderef( $filter->{handler} ) )
547            {
548                $param{filter_key}   = $filter_key;
549                $param{filter_label} = $filter->{label};
550                $code->( \%terms, \%arg );
551            }
552        }
553    }
554    require MT::Category;
555    require MT::Placement;
556
557    my $total = $pkg->count( \%terms, \%arg ) || 0;
558    $arg{'sort'} = $type eq 'page' ? 'modified_on' : 'authored_on';
559    $arg{direction} = 'descend';
560    $arg{limit}     = $limit + 1;
561    if ( $total <= $limit ) {
562        delete $arg{limit};
563        $offset = 0;
564    }
565    elsif ( $total && $offset > $total - 1 ) {
566        $arg{offset} = $offset = $total - $limit;
567    }
568    elsif ( $offset && ( ( $offset < 0 ) || ( $total - $offset < $limit ) ) ) {
569        $arg{offset} = $offset = $total - $limit;
570    }
571    else {
572        $arg{offset} = $offset if $offset;
573    }
574
575    my $iter = $pkg->load_iter( \%terms, \%arg );
576
577    my $is_power_edit = $q->param('is_power_edit');
578    if ($is_power_edit) {
579        $param{has_expanded_mode} = 0;
580        delete $param{view_expanded};
581    }
582    else {
583        $param{has_expanded_mode} = 1;
584    }
585    my $data = build_entry_table( $app,
586        iter          => $iter,
587        is_power_edit => $is_power_edit,
588        param         => \%param,
589        type          => $type
590    );
591    delete $_->{object} foreach @$data;
592    delete $param{entry_table} unless @$data;
593
594    ## We tried to load $limit + 1 entries above; if we actually got
595    ## $limit + 1 back, we know we have another page of entries.
596    my $have_next_entry = @$data > $limit;
597    pop @$data while @$data > $limit;
598    if ($offset) {
599        $param{prev_offset}     = 1;
600        $param{prev_offset_val} = $offset - $limit;
601        $param{prev_offset_val} = 0 if $param{prev_offset_val} < 0;
602    }
603    if ($have_next_entry) {
604        $param{next_offset}     = 1;
605        $param{next_offset_val} = $offset + $limit;
606    }
607
608    $iter = MT::Author->load_iter(
609        { type => MT::Author::AUTHOR() },
610        {
611            'join' => $pkg->join_on(
612                'author_id',
613                { $blog_id ? ( blog_id => $blog_id ) : () },
614                {
615                    'unique'  => 1,
616                    'sort'    => 'authored_on',
617                    direction => 'descend'
618                }
619            ),
620            limit => 51,
621        }
622    );
623    my %seen;
624    my @authors;
625    while ( my $au = $iter->() ) {
626        next if $seen{ $au->id };
627        $seen{ $au->id } = 1;
628        my $row = {
629            author_name => $au->name,
630            author_id   => $au->id
631        };
632        push @authors, $row;
633        if ( @authors == 50 ) {
634            $iter->end;
635            last;
636        }
637    }
638    $param{entry_author_loop} = \@authors;
639
640    # $iter = $app->model('asset')->load_iter(
641    #     { class => '*', blog_id => $blog_id },
642    #     {
643    #         'join' => MT->model('objectasset')->join_on(
644    #             'asset_id',
645    #             {},
646    #             { unique => 1 }
647    #         ),
648    #         'sort'    => 'created_on',
649    #         direction => 'descend',
650    #     }
651    # );
652    # %seen = ();
653    # my @assets;
654    # while ( my $asset = $iter->() ) {
655    #     next if $seen{ $asset->id };
656    #     $seen{ $asset->id } = 1;
657    #     my $row = {
658    #         asset_label => $asset->label,
659    #         asset_id    => $asset->id,
660    #     };
661    #     push @assets, $row;
662    #     if ( @assets == 50 ) {
663    #         $iter->end;
664    #         last;
665    #     }
666    # }
667    # if ($filter_col eq 'asset_id' && !$seen{$filter_val}) {
668    #     push @assets, {
669    #         asset_label => $param{asset_label},
670    #         asset_id    => $filter_val,
671    #     };
672    # }
673    # $param{entry_asset_loop} = \@assets;
674
675    $param{page_actions}        = $app->page_actions( $app->mode );
676    $param{list_filters}        = $app->list_filters('entry');
677    $param{can_power_edit}      = $blog_id && !$is_power_edit;
678    $param{can_republish}       = $blog_id ? $perms->can_rebuild : 1;
679    $param{is_power_edit}       = $is_power_edit;
680    $param{saved_deleted}       = $q->param('saved_deleted');
681    $param{no_rebuild}          = $q->param('no_rebuild');
682    $param{saved}               = $q->param('saved');
683    $param{limit}               = $limit;
684    $param{offset}              = $offset;
685    $param{object_type}         = $type;
686    $param{object_label}        = $pkg->class_label;
687    $param{object_label_plural} = $param{search_label} =
688      $pkg->class_label_plural;
689    $param{list_start}  = $offset + 1;
690    $param{list_end}    = $offset + scalar @$data;
691    $param{list_total}  = $total;
692    $param{next_max}    = $param{list_total} - $limit;
693    $param{next_max}    = 0 if ( $param{next_max} || 0 ) < $offset + 1;
694    $param{nav_entries} = 1;
695    $param{feed_label}  = $app->translate( "[_1] Feed", $pkg->class_label );
696    $param{feed_url} =
697      $app->make_feed_link( $type, $blog_id ? { blog_id => $blog_id } : undef );
698    $app->add_breadcrumb( $pkg->class_label_plural );
699    $param{listing_screen} = 1;
700
701    unless ($blog_id) {
702        $param{system_overview_nav} = 1;
703    }
704    $param{container_label} = $pkg->container_label;
705    unless ( $param{screen_class} ) {
706        $param{screen_class} = "list-$type";
707        $param{screen_class} .= " list-entry"
708          if $param{object_type} eq "page";  # to piggyback on list-entry styles
709    }
710    $param{mode}            = $app->mode;
711    if ( my $blog = MT::Blog->load($blog_id) ) {
712        $param{sitepath_unconfigured} = $blog->site_path ? 0 : 1;
713    }
714
715    $param->{return_args} ||= $app->make_return_args;
716    my @return_args = grep { $_ !~ /offset=\d/ } split /&/, $param->{return_args};
717    $param{return_args} = join '&', @return_args;
718    $param{return_args} .= "&offset=$offset" if $offset;
719    $param{screen_id} = "list-entry";
720    $param{screen_id} = "list-page"
721      if $param{object_type} eq "page";
722    if ( $param{is_power_edit} ) {
723        $param{screen_id} = "batch-edit-entry";
724        $param{screen_id} = "batch-edit-page"
725          if $param{object_type} eq "page";
726        $param{screen_class} .= " batch-edit";
727    }
728    $app->load_tmpl( "list_entry.tmpl", \%param );
729}
730
731sub preview {
732    my $app         = shift;
733    my $q           = $app->param;
734    my $type        = $q->param('_type') || 'entry';
735    my $entry_class = $app->model($type);
736    my $blog_id     = $q->param('blog_id');
737    my $blog        = $app->blog;
738    my $id          = $q->param('id');
739    my $entry;
740    my $user_id = $app->user->id;
741
742    if ($id) {
743        $entry = $entry_class->load( { id => $id, blog_id => $blog_id } )
744            or return $app->errtrans( "Invalid request." );
745        $user_id = $entry->author_id;
746    }
747    else {
748        $entry = $entry_class->new;
749        $entry->author_id($user_id);
750        $entry->id(-1); # fake out things like MT::Taggable::__load_tags
751        $entry->blog_id($blog_id);
752    }
753    my $cat;
754    my $names = $entry->column_names;
755
756    my %values = map { $_ => scalar $app->param($_) } @$names;
757    delete $values{'id'} unless $q->param('id');
758    ## Strip linefeed characters.
759    for my $col (qw( text excerpt text_more keywords )) {
760        $values{$col} =~ tr/\r//d if $values{$col};
761    }
762    $values{allow_comments} = 0
763      if !defined( $values{allow_comments} )
764      || $q->param('allow_comments') eq '';
765    $values{allow_pings} = 0
766      if !defined( $values{allow_pings} )
767      || $q->param('allow_pings') eq '';
768    $entry->set_values( \%values );
769
770    my $cat_ids = $q->param('category_ids');
771    if ($cat_ids) {
772        my @cats = split /,/, $cat_ids;
773        if (@cats) {
774            my $primary_cat = $cats[0];
775            $cat =
776              MT::Category->load( { id => $primary_cat, blog_id => $blog_id } );
777            my @categories = MT::Category->load( { id => \@cats, blog_id => $blog_id });
778            $entry->cache_property('category', undef, $cat);
779            $entry->cache_property('categories', undef, \@categories);
780        }
781    } else {
782        $entry->cache_property('category', undef, undef);
783        $entry->cache_property('categories', undef, []);
784    }
785    my $tag_delim = chr( $app->user->entry_prefs->{tag_delim} );
786    my @tag_names = MT::Tag->split( $tag_delim, $q->param('tags') );
787    if (@tag_names) {
788        my @tags;
789        foreach my $tag_name (@tag_names) {
790            my $tag = MT::Tag->new;
791            $tag->name($tag_name);
792            push @tags, $tag;
793        }
794        $entry->{__tags} = \@tag_names;
795        $entry->{__tag_objects} = \@tags;
796    }
797
798    my $date = $q->param('authored_on_date');
799    my $time = $q->param('authored_on_time');
800    my $ts   = $date . $time;
801    $ts =~ s/\D//g;
802    $entry->authored_on($ts);
803
804    my $preview_basename = $app->preview_object_basename;
805    $entry->basename($preview_basename);
806
807    require MT::TemplateMap;
808    require MT::Template;
809    my $tmpl_map = MT::TemplateMap->load(
810        {
811            archive_type => ( $type eq 'page' ? 'Page' : 'Individual' ),
812            is_preferred => 1,
813            blog_id => $blog_id,
814        }
815    );
816
817    my $tmpl;
818    my $fullscreen;
819    my $archive_file;
820    my $orig_file;
821    my $file_ext;
822    if ($tmpl_map) {
823        $tmpl         = MT::Template->load( $tmpl_map->template_id );
824        $file_ext = $blog->file_extension || '';
825        $archive_file = $entry->archive_file;
826
827        my $blog_path = $type eq 'page' ?
828            $blog->site_path :
829            ($blog->archive_path || $blog->site_path);
830        $archive_file = File::Spec->catfile( $blog_path, $archive_file );
831        require File::Basename;
832        my $path;
833        ( $orig_file, $path ) = File::Basename::fileparse( $archive_file );
834        $file_ext = '.' . $file_ext if $file_ext ne '';
835        $archive_file = File::Spec->catfile( $path, $preview_basename . $file_ext );
836    }
837    else {
838        $tmpl       = $app->load_tmpl('preview_entry_content.tmpl');
839        $fullscreen = 1;
840    }
841    return $app->error($app->translate('Can\'t load template.')) unless $tmpl;
842
843    # translates naughty words when PublishCharset is NOT UTF-8
844    $app->_translate_naughty_words($entry);
845
846    $entry->convert_breaks( scalar $q->param('convert_breaks') );
847       
848    my @data = ( { data_name => 'author_id', data_value => $user_id } );
849    $app->run_callbacks( 'cms_pre_preview', $app, $entry, \@data );
850
851    my $ctx = $tmpl->context;
852    $ctx->stash( 'entry', $entry );
853    $ctx->stash( 'blog',  $blog );
854    $ctx->stash( 'category', $cat ) if $cat;
855    $ctx->{current_timestamp} = $ts;
856    $ctx->var('entry_template',    1);
857    $ctx->var('archive_template',  1);
858    $ctx->var('entry_template',    1);
859    $ctx->var('feedback_template', 1);
860    $ctx->var('archive_class',     'entry-archive');
861    $ctx->var('preview_template',  1);
862    my $html = $tmpl->output;
863    my %param;
864    unless ( defined($html) ) {
865        my $preview_error = $app->translate( "Publish error: [_1]",
866            MT::Util::encode_html( $tmpl->errstr ) );
867        $param{preview_error} = $preview_error;
868        my $tmpl_plain = $app->load_tmpl('preview_entry_content.tmpl');
869        $tmpl->text( $tmpl_plain->text );
870        $html = $tmpl->output;
871        defined($html)
872          or return $app->error(
873            $app->translate( "Publish error: [_1]", $tmpl->errstr ) );
874        $fullscreen = 1;
875    }
876
877    # If MT is configured to do 'local' previews, convert all
878    # the normal blog URLs into the domain used by MT itself (ie,
879    # blog is published to www.example.com, which is a different
880    # server from where MT runs, mt.example.com; previews therefore
881    # should occur locally, so replace all http://www.example.com/
882    # with http://mt.example.com/).
883    my ($old_url, $new_url);
884    if ($app->config('LocalPreviews')) {
885        $old_url = $blog->site_url;
886        $old_url =~ s!^(https?://[^/]+?/)(.*)?!$1!;
887        $new_url = $app->base . '/';
888        $html =~ s!\Q$old_url\E!$new_url!g;
889    }
890
891    if ( !$fullscreen ) {
892        my $fmgr = $blog->file_mgr;
893
894        ## Determine if we need to build directory structure,
895        ## and build it if we do. DirUmask determines
896        ## directory permissions.
897        require File::Basename;
898        my $path = File::Basename::dirname($archive_file);
899        $path =~ s!/$!!
900          unless $path eq '/';    ## OS X doesn't like / at the end in mkdir().
901        unless ( $fmgr->exists($path) ) {
902            $fmgr->mkpath($path);
903        }
904
905        if ( $fmgr->exists($path) && $fmgr->can_write($path) ) {
906            $fmgr->put_data( $html, $archive_file );
907            $param{preview_file} = $preview_basename;
908            my $preview_url = $entry->archive_url;
909            $preview_url =~ s! / \Q$orig_file\E ( /? ) $!/$preview_basename$file_ext$1!x;
910
911            # We also have to translate the URL used for the
912            # published file to be on the MT app domain.
913            if (defined $new_url) {
914                $preview_url =~ s!^\Q$old_url\E!$new_url!;
915            }
916
917            $param{preview_url}  = $preview_url;
918
919            # we have to make a record of this preview just in case it
920            # isn't cleaned up by re-editing, saving or cancelling on
921            # by the user.
922            require MT::Session;
923            my $sess_obj = MT::Session->get_by_key(
924                {
925                    id   => $preview_basename,
926                    kind => 'TF',                # TF = Temporary File
927                    name => $archive_file,
928                }
929            );
930            $sess_obj->start(time);
931            $sess_obj->save;
932        }
933        else {
934            $fullscreen = 1;
935            $param{preview_error} = $app->translate(
936                "Unable to create preview file in this location: [_1]", $path );
937            my $tmpl_plain = $app->load_tmpl('preview_entry_content.tmpl');
938            $tmpl->text( $tmpl_plain->text );
939            $tmpl->reset_tokens;
940            $html = $tmpl->output;
941            $param{preview_body} = $html;
942        }
943    }
944    else {
945        $param{preview_body} = $html;
946    }
947    $param{id} = $id if $id;
948    $param{new_object} = $param{id} ? 0 : 1;
949    $param{title} = $entry->title;
950    my $cols = $entry_class->column_names;
951
952    for my $col (@$cols) {
953        next
954          if $col eq 'created_on'
955          || $col eq 'created_by'
956          || $col eq 'modified_on'
957          || $col eq 'modified_by'
958          || $col eq 'authored_on'
959          || $col eq 'author_id'
960          || $col eq 'pinged_urls'
961          || $col eq 'tangent_cache'
962          || $col eq 'template_id'
963          || $col eq 'class'
964          || $col eq 'meta'
965          || $col eq 'comment_count'
966          || $col eq 'ping_count';
967        if ( $col eq 'basename' ) {
968            if (   ( !defined $q->param('basename') )
969                || ( $q->param('basename') eq '' ) )
970            {
971                $q->param( 'basename', $q->param('basename_old') );
972            }
973        }
974        push @data,
975          {
976            data_name  => $col,
977            data_value => scalar $q->param($col)
978          };
979    }
980    for my $data (
981        qw( authored_on_date authored_on_time basename_manual basename_old category_ids tags )
982      )
983    {
984        push @data,
985          {
986            data_name  => $data,
987            data_value => scalar $q->param($data)
988          };
989    }
990
991    $param{entry_loop} = \@data;
992    my $list_mode;
993    my $list_title;
994    if ( $type eq 'page' ) {
995        $list_title = 'Pages';
996        $list_mode  = 'list_pages';
997    }
998    else {
999        $list_title = 'Entries';
1000        $list_mode  = 'list_entries';
1001    }
1002    if ($id) {
1003        $app->add_breadcrumb(
1004            $app->translate($list_title),
1005            $app->uri(
1006                'mode' => $list_mode,
1007                args   => { blog_id => $blog_id }
1008            )
1009        );
1010        $app->add_breadcrumb( $entry->title || $app->translate('(untitled)') );
1011    }
1012    else {
1013        $app->add_breadcrumb( $app->translate($list_title),
1014            $app->uri( 'mode' => $list_mode, args => { blog_id => $blog_id } )
1015        );
1016        $app->add_breadcrumb(
1017            $app->translate( 'New [_1]', $entry_class->class_label ) );
1018        $param{nav_new_entry} = 1;
1019    }
1020    $param{object_type}  = $type;
1021    $param{object_label} = $entry_class->class_label;
1022    if ($fullscreen) {
1023        return $app->load_tmpl( 'preview_entry.tmpl', \%param );
1024    }
1025    else {
1026        $app->request('preview_object', $entry);
1027        return $app->load_tmpl( 'preview_strip.tmpl', \%param );
1028    }
1029}
1030
1031sub cfg_entry {
1032    my $app     = shift;
1033    my $q       = $app->param;
1034    my $blog_id = scalar $q->param('blog_id');
1035    return $app->return_to_dashboard( redirect => 1 )
1036      unless $blog_id;
1037    $q->param( '_type', 'blog' );
1038    $q->param( 'id',    scalar $q->param('blog_id') );
1039    $app->forward("view",
1040        {
1041            output       => 'cfg_entry.tmpl',
1042            screen_class => 'settings-screen entry-screen'
1043        }
1044    );
1045}
1046
1047sub save {
1048    my $app = shift;
1049    $app->validate_magic or return;
1050
1051    $app->remove_preview_file;
1052
1053    if ( $app->param('is_power_edit') ) {
1054        return $app->save_entries(@_);
1055    }
1056    my $author = $app->user;
1057    my $type = $app->param('_type') || 'entry';
1058
1059    my $class = $app->model($type)
1060      or retrun $app->errtrans("Invalid parameter");
1061
1062    my $cat_class = $app->model( $class->container_type );
1063
1064    my $perms = $app->permissions
1065      or return $app->errtrans("Permission denied.");
1066
1067    if ( $type eq 'page' ) {
1068        return $app->errtrans("Permission denied.")
1069          unless $perms->can_manage_pages;
1070    }
1071
1072    my $id = $app->param('id');
1073    if ( !$id ) {
1074        return $app->errtrans("Permission denied.")
1075          unless ( ( 'entry' eq $type ) && $perms->can_create_post )
1076          || ( ( 'page' eq $type ) && $perms->can_manage_pages );
1077    }
1078
1079    $app->validate_magic() or return;
1080
1081    # check for autosave
1082    if ( $app->param('_autosave') ) {
1083        return $app->autosave_object();
1084    }
1085
1086    require MT::Blog;
1087    my $blog_id = $app->param('blog_id');
1088    my $blog    = MT::Blog->load($blog_id)
1089        or return $app->error($app->translate('Can\'t load blog #[_1].', $blog_id));
1090   
1091    my $archive_type;
1092
1093    my ( $obj, $orig_obj, $orig_file );
1094    if ($id) {
1095        $obj = $class->load($id)
1096          || return $app->error(
1097            $app->translate( "No such [_1].", $class->class_label ) );
1098        return $app->error( $app->translate("Invalid parameter") )
1099          unless $obj->blog_id == $blog_id;
1100        if ( $type eq 'entry' ) {
1101            return $app->error( $app->translate("Permission denied.") )
1102              unless $perms->can_edit_entry( $obj, $author );
1103            return $app->error( $app->translate("Permission denied.") )
1104              if ( $obj->status ne $app->param('status') )
1105              && !( $perms->can_edit_entry( $obj, $author, 1 ) );
1106            $archive_type = 'Individual';
1107        }
1108        elsif ( $type eq 'page' ) {
1109            $archive_type = 'Page';
1110        }
1111        $orig_obj = $obj->clone;
1112        $orig_file = archive_file_for( $orig_obj, $blog, $archive_type );
1113    }
1114    else {
1115        $obj = $class->new;
1116    }
1117    my $status_old = $id ? $obj->status : 0;
1118    my $names = $obj->column_names;
1119
1120    ## Get rid of category_id param, because we don't want to just set it
1121    ## in the Entry record; save it for later when we will set the Placement.
1122    my ( $cat_id, @add_cat ) = split /\s*,\s*/,
1123      ( $app->param('category_ids') || '' );
1124    $app->delete_param('category_id');
1125    if ($id) {
1126        ## Delete the author_id param (if present), because we don't want to
1127        ## change the existing author.
1128        $app->delete_param('author_id');
1129    }
1130
1131    my %values = map { $_ => scalar $app->param($_) } @$names;
1132    delete $values{'id'} unless $app->param('id');
1133    ## Strip linefeed characters.
1134    for my $col (qw( text excerpt text_more keywords )) {
1135        $values{$col} =~ tr/\r//d if $values{$col};
1136    }
1137    $values{allow_comments} = 0
1138      if !defined( $values{allow_comments} )
1139      || $app->param('allow_comments') eq '';
1140    delete $values{week_number}
1141      if ( $app->param('week_number') || '' ) eq '';
1142    delete $values{basename}
1143      unless $perms->can_publish_post || $perms->can_edit_all_posts;
1144    $obj->set_values( \%values );
1145    $obj->allow_pings(0)
1146      if !defined $app->param('allow_pings')
1147      || $app->param('allow_pings') eq '';
1148    my $ao_d = $app->param('authored_on_date');
1149    my $ao_t = $app->param('authored_on_time');
1150
1151    if ( !$id ) {
1152
1153        #  basename check for this new entry...
1154        if (   ( my $basename = $app->param('basename') )
1155            && !$app->param('basename_manual')
1156            && $type eq 'entry' )
1157        {
1158            my $exist =
1159              $class->exist( { blog_id => $blog_id, basename => $basename } );
1160            if ($exist) {
1161                $obj->basename( MT::Util::make_unique_basename($obj) );
1162            }
1163        }
1164    }
1165
1166    if ( $type eq 'page' ) {
1167
1168        # -1 is a special id for identifying the 'root' folder
1169        $cat_id = 0 if $cat_id == -1;
1170        my $dup_it = $class->load_iter(
1171            {
1172                blog_id  => $blog_id,
1173                basename => $obj->basename,
1174                class    => 'page',
1175                ( $id ? ( id => $id ) : () )
1176            },
1177            { ( $id ? ( not => { id => 1 } ) : () ) }
1178        );
1179        while ( my $p = $dup_it->() ) {
1180            my $p_folder = $p->folder;
1181            my $dup_folder_path =
1182              defined $p_folder ? $p_folder->publish_path() : '';
1183            my $folder = MT::Folder->load($cat_id) if $cat_id;
1184            my $folder_path = defined $folder ? $folder->publish_path() : '';
1185            return $app->error(
1186                $app->translate(
1187"Same Basename has already been used. You should use an unique basename."
1188                )
1189            ) if ( $dup_folder_path eq $folder_path );
1190        }
1191
1192    }
1193
1194    if ( $type eq 'entry' ) {
1195        $obj->status( MT::Entry::HOLD() )
1196          if !$id
1197          && !$perms->can_publish_post
1198          && !$perms->can_edit_all_posts;
1199    }
1200
1201    my $filter_result = $app->run_callbacks( 'cms_save_filter.' . $type, $app );
1202
1203    if ( !$filter_result ) {
1204        my %param = ();
1205        $param{error}       = $app->errstr;
1206        $param{return_args} = $app->param('return_args');
1207        return $app->forward( "view", \%param );
1208    }
1209
1210    # check to make sure blog has site url and path defined.
1211    # otherwise, we can't publish a released entry
1212    if ( ( $obj->status || 0 ) != MT::Entry::HOLD() ) {
1213        if ( !$blog->site_path || !$blog->site_url ) {
1214            return $app->error(
1215                $app->translate(
1216"Your blog has not been configured with a site path and URL. You cannot publish entries until these are defined."
1217                )
1218            );
1219        }
1220    }
1221
1222    my ( $previous_old, $next_old );
1223    if (   ( $perms->can_publish_post || $perms->can_edit_all_posts )
1224        && ($ao_d) )
1225    {
1226        my %param = ();
1227        my $ao = $ao_d . ' ' . $ao_t;
1228        unless (
1229            $ao =~ m!^(\d{4})-(\d{1,2})-(\d{1,2})\s+(\d{1,2}):(\d{1,2})(?::(\d{1,2}))?$! )
1230        {
1231            $param{error} = $app->translate(
1232"Invalid date '[_1]'; authored on dates must be in the format YYYY-MM-DD HH:MM:SS.",
1233$ao
1234            );
1235        }
1236        my $s = $6 || 0;
1237            $param{error} = $app->translate(
1238                "Invalid date '[_1]'; authored on dates should be real dates.",
1239                $ao
1240          )
1241          if (
1242               $s > 59
1243            || $s < 0
1244            || $5 > 59
1245            || $5 < 0
1246            || $4 > 23
1247            || $4 < 0
1248            || $2 > 12
1249            || $2 < 1
1250            || $3 < 1
1251            || ( MT::Util::days_in( $2, $1 ) < $3
1252                && !MT::Util::leap_day( $0, $1, $2 ) )
1253          );
1254        $param{return_args} = $app->param('return_args');
1255        return $app->forward( "view", \%param ) if $param{error};
1256        if ($obj->authored_on) {
1257            $previous_old = $obj->previous(1);
1258            $next_old     = $obj->next(1);
1259        }
1260        my $ts = sprintf "%04d%02d%02d%02d%02d%02d", $1, $2, $3, $4, $5, $s;
1261        $obj->authored_on($ts);
1262    }
1263    my $is_new = $obj->id ? 0 : 1;
1264
1265    $app->_translate_naughty_words($obj);
1266
1267    $obj->modified_by( $author->id ) unless $is_new;
1268
1269    $app->run_callbacks( 'cms_pre_save.' . $type, $app, $obj, $orig_obj )
1270      || return $app->error(
1271        $app->translate(
1272            "Saving [_1] failed: [_2]",
1273            $class->class_label, $app->errstr
1274        )
1275      );
1276
1277    $obj->save
1278      or return $app->error(
1279        $app->translate(
1280            "Saving [_1] failed: [_2]",
1281            $class->class_label, $obj->errstr
1282        )
1283      );
1284
1285    my $message;
1286    if ($is_new) {
1287        $message =
1288          $app->translate( "[_1] '[_2]' (ID:[_3]) added by user '[_4]'",
1289            $class->class_label, $obj->title, $obj->id, $author->name );
1290    }
1291    elsif ( $orig_obj->status ne $obj->status ) {
1292        $message = $app->translate(
1293"[_1] '[_2]' (ID:[_3]) edited and its status changed from [_4] to [_5] by user '[_6]'",
1294            $class->class_label,
1295            $obj->title,
1296            $obj->id,
1297            $app->translate( MT::Entry::status_text( $orig_obj->status ) ),
1298            $app->translate( MT::Entry::status_text( $obj->status ) ),
1299            $author->name
1300        );
1301
1302    }
1303    else {
1304        $message =
1305          $app->translate( "[_1] '[_2]' (ID:[_3]) edited by user '[_4]'",
1306            $class->class_label, $obj->title, $obj->id, $author->name );
1307    }
1308    require MT::Log;
1309    $app->log(
1310        {
1311            message => $message,
1312            level   => MT::Log::INFO(),
1313            class   => $type,
1314            $is_new ? ( category => 'new' ) : ( category => 'edit' ),
1315            metadata => $obj->id
1316        }
1317    );
1318
1319    my $error_string = MT::callback_errstr();
1320
1321    ## Now that the object is saved, we can be certain that it has an
1322    ## ID. So we can now add/update/remove the primary placement.
1323    require MT::Placement;
1324    my $place =
1325      MT::Placement->load( { entry_id => $obj->id, is_primary => 1 } );
1326    if ($cat_id) {
1327        unless ($place) {
1328            $place = MT::Placement->new;
1329            $place->entry_id( $obj->id );
1330            $place->blog_id( $obj->blog_id );
1331            $place->is_primary(1);
1332        }
1333        $place->category_id($cat_id);
1334        $place->save;
1335        my $cat = $cat_class->load($cat_id);
1336        $obj->cache_property( 'category', undef, $cat );
1337    }
1338    else {
1339        if ( $place ) {
1340            $place->remove;
1341            $obj->cache_property( 'category', undef, undef );
1342        }
1343    }
1344
1345    my $placements_updated;
1346
1347    # save secondary placements...
1348    my @place = MT::Placement->load(
1349        {
1350            entry_id   => $obj->id,
1351            is_primary => 0
1352        }
1353    );
1354    for my $place (@place) {
1355        $place->remove;
1356        $placements_updated = 1;
1357    }
1358    my @add_cat_obj;
1359    for my $cat_id (@add_cat) {
1360        my $cat = $cat_class->load($cat_id);
1361
1362        # blog_id sanity check
1363        next if !$cat || $cat->blog_id != $obj->blog_id;
1364
1365        my $place = MT::Placement->new;
1366        $place->entry_id( $obj->id );
1367        $place->blog_id( $obj->blog_id );
1368        $place->is_primary(0);
1369        $place->category_id($cat_id);
1370        $place->save
1371          or return $app->error(
1372            $app->translate( "Saving placement failed: [_1]", $place->errstr )
1373          );
1374        $placements_updated = 1;
1375        push @add_cat_obj, $cat;
1376    }
1377    if ($placements_updated) {
1378        unshift @add_cat_obj, $obj->cache_property('category')
1379          if $obj->cache_property('category');
1380        $obj->cache_property( 'categories', undef, [] );
1381        $obj->cache_property( 'categories', undef, \@add_cat_obj );
1382    }
1383
1384    $app->run_callbacks( 'cms_post_save.' . $type, $app, $obj, $orig_obj );
1385
1386    ## If the saved status is RELEASE, or if the *previous* status was
1387    ## RELEASE, then rebuild entry archives, indexes, and send the
1388    ## XML-RPC ping(s). Otherwise the status was and is HOLD, and we
1389    ## don't have to do anything.
1390    if ( ( $obj->status || 0 ) == MT::Entry::RELEASE()
1391        || $status_old eq MT::Entry::RELEASE() )
1392    {
1393        if ( $app->config('DeleteFilesAtRebuild') && $orig_obj ) {
1394            my $file = archive_file_for( $obj, $blog, $archive_type );
1395            if ( $file ne $orig_file || $obj->status != MT::Entry::RELEASE() ) {
1396                $app->publisher->remove_entry_archive_file(
1397                    Entry       => $orig_obj,
1398                    ArchiveType => $archive_type
1399                );
1400            }
1401        }
1402
1403        # If there are no static pages, just rebuild indexes.
1404        if ( $blog->count_static_templates($archive_type) == 0
1405            || MT::Util->launch_background_tasks() )
1406        {
1407            my $res = MT::Util::start_background_task(
1408                sub {
1409                    $app->run_callbacks('pre_build');
1410                    $app->rebuild_entry(
1411                        Entry             => $obj,
1412                        BuildDependencies => 1,
1413                        OldEntry          => $orig_obj,
1414                        OldPrevious       => ($previous_old)
1415                        ? $previous_old->id
1416                        : undef,
1417                        OldNext => ($next_old) ? $next_old->id : undef
1418                    ) or return $app->publish_error();
1419                    $app->run_callbacks( 'rebuild', $blog );
1420                    $app->run_callbacks( 'post_build' );
1421                    1;
1422                }
1423            );
1424            return unless $res;
1425            return ping_continuation($app,
1426                $obj, $blog,
1427                OldStatus => $status_old,
1428                IsNew     => $is_new,
1429            );
1430        }
1431        else {
1432            return $app->redirect(
1433                $app->uri(
1434                    'mode' => 'start_rebuild',
1435                    args   => {
1436                        blog_id    => $obj->blog_id,
1437                        'next'     => 0,
1438                        type       => 'entry-' . $obj->id,
1439                        entry_id   => $obj->id,
1440                        is_new     => $is_new,
1441                        old_status => $status_old,
1442                        (
1443                            $previous_old
1444                            ? ( old_previous => $previous_old->id )
1445                            : ()
1446                        ),
1447                        ( $next_old ? ( old_next => $next_old->id ) : () )
1448                    }
1449                )
1450            );
1451        }
1452    }
1453    _finish_rebuild_ping( $app, $obj, !$id );
1454}
1455
1456sub save_entries {
1457    my $app   = shift;
1458    my $perms = $app->permissions;
1459    my $type  = $app->param('_type');
1460    return $app->errtrans("Permission denied.")
1461      unless $perms
1462      && (
1463        $type eq 'page'
1464        ? ( $perms->can_manage_pages )
1465        : (      $perms->can_publish_post
1466              || $perms->can_create_post
1467              || $perms->can_edit_all_posts )
1468      );
1469
1470    $app->validate_magic() or return;
1471
1472    my $q = $app->param;
1473    my @p = $q->param;
1474    require MT::Entry;
1475    require MT::Placement;
1476    require MT::Log;
1477    my $blog_id        = $q->param('blog_id');
1478    my $this_author    = $app->user;
1479    my $this_author_id = $this_author->id;
1480    for my $p (@p) {
1481        next unless $p =~ /^category_id_(\d+)/;
1482        my $id    = $1;
1483        my $entry = MT::Entry->load($id)
1484            or next;
1485        return $app->error( $app->translate("Permission denied.") )
1486            unless $perms
1487              && (
1488                $type eq 'page'
1489                ? ( $perms->can_manage_pages )
1490                : (      $perms->can_publish_post
1491                      || $perms->can_create_post
1492                      || $perms->can_edit_all_posts )
1493              );
1494        my $orig_obj = $entry->clone;
1495        if ( $perms->can_edit_entry( $entry, $this_author ) ) {
1496            my $author_id = $q->param( 'author_id_' . $id );
1497            $entry->author_id( $author_id ? $author_id : 0 );
1498            $entry->title( scalar $q->param( 'title_' . $id ) );
1499        }
1500        if ( $perms->can_edit_entry( $entry, $this_author, 1 ) )
1501        {    ## can he/she change status?
1502            $entry->status( scalar $q->param( 'status_' . $id ) );
1503            my $co = $q->param( 'created_on_' . $id );
1504            unless ( $co =~
1505                m!(\d{4})-(\d{2})-(\d{2})\s+(\d{2}):(\d{2})(?::(\d{2}))?! )
1506            {
1507                return $app->error(
1508                    $app->translate(
1509"Invalid date '[_1]'; authored on dates must be in the format YYYY-MM-DD HH:MM:SS.",
1510                        $co
1511                    )
1512                );
1513            }
1514            my $s = $6 || 0;
1515
1516            # Emit an error message if the date is bogus.
1517            return $app->error(
1518                $app->translate(
1519"Invalid date '[_1]'; authored on dates should be real dates.",
1520                    $co
1521                )
1522              )
1523              if $s > 59
1524              || $s < 0
1525              || $5 > 59
1526              || $5 < 0
1527              || $4 > 23
1528              || $4 < 0
1529              || $2 > 12
1530              || $2 < 1
1531              || $3 < 1
1532              || ( MT::Util::days_in( $2, $1 ) < $3
1533                && !MT::Util::leap_day( $0, $1, $2 ) );
1534
1535            # FIXME: Should be assigning the publish_date field here
1536            my $ts = sprintf "%04d%02d%02d%02d%02d%02d", $1, $2, $3, $4, $5, $s;
1537            if ($type eq 'page' ) {
1538                $entry->modified_on($ts);
1539            } else {
1540                $entry->authored_on($ts);
1541            }
1542        }
1543        $app->run_callbacks( 'cms_pre_save.' . $type, $app, $entry, $orig_obj )
1544          || return $app->error(
1545            $app->translate(
1546                "Saving [_1] failed: [_2]",
1547                $entry->class_label, $app->errstr
1548            )
1549          );
1550        $entry->save
1551          or return $app->error(
1552            $app->translate(
1553                "Saving entry '[_1]' failed: [_2]", $entry->title,
1554                $entry->errstr
1555            )
1556          );
1557        my $cat_id = $q->param("category_id_$id");
1558        my $place  = MT::Placement->load(
1559            {
1560                entry_id   => $id,
1561                is_primary => 1
1562            }
1563        );
1564        if ( $place && !$cat_id ) {
1565            $place->remove
1566              or return $app->error(
1567                $app->translate(
1568                    "Removing placement failed: [_1]",
1569                    $place->errstr
1570                )
1571              );
1572        }
1573        elsif ($cat_id) {
1574            unless ($place) {
1575                $place = MT::Placement->new;
1576                $place->entry_id($id);
1577                $place->blog_id($blog_id);
1578                $place->is_primary(1);
1579            }
1580            $place->category_id( scalar $q->param($p) );
1581            $place->save
1582              or return $app->error(
1583                $app->translate(
1584                    "Saving placement failed: [_1]",
1585                    $place->errstr
1586                )
1587              );
1588        }
1589        my $message;
1590        if ( $orig_obj->status ne $entry->status ) {
1591            $message = $app->translate(
1592"[_1] '[_2]' (ID:[_3]) edited and its status changed from [_4] to [_5] by user '[_6]'",
1593                $entry->class_label,
1594                $entry->title,
1595                $entry->id,
1596                $app->translate( MT::Entry::status_text( $orig_obj->status ) ),
1597                $app->translate( MT::Entry::status_text( $entry->status ) ),
1598                $this_author->name
1599            );
1600        }
1601        else {
1602            $message =
1603              $app->translate( "[_1] '[_2]' (ID:[_3]) edited by user '[_4]'",
1604                $entry->class_label, $entry->title, $entry->id,
1605                $this_author->name );
1606        }
1607        $app->log(
1608            {
1609                message  => $message,
1610                level    => MT::Log::INFO(),
1611                class    => $entry->class,
1612                category => 'edit',
1613                metadata => $entry->id
1614            }
1615        );
1616        $app->run_callbacks( 'cms_post_save.' . $type, $app, $entry, $orig_obj );
1617    }
1618    $app->add_return_arg( 'saved' => 1, is_power_edit => 1 );
1619    $app->call_return;
1620}
1621
1622sub send_pings {
1623    my $app = shift;
1624    my $q   = $app->param;
1625    $app->validate_magic() or return;
1626    require MT::Entry;
1627    require MT::Blog;
1628    my $blog  = MT::Blog->load( scalar $q->param('blog_id') );
1629    my $entry = MT::Entry->load( scalar $q->param('entry_id') );
1630    ## MT::ping_and_save pings each of the necessary URLs, then processes
1631    ## the return value from MT::ping to update the list of URLs pinged
1632    ## and not successfully pinged. It returns the return value from
1633    ## MT::ping for further processing. If a fatal error occurs, it returns
1634    ## undef.
1635    my $results = $app->ping_and_save(
1636        Blog      => $blog,
1637        Entry     => $entry,
1638        OldStatus => scalar $q->param('old_status')
1639    ) or return;
1640    my $has_errors = 0;
1641    require MT::Log;
1642    for my $res (@$results) {
1643        $has_errors++,
1644          $app->log(
1645            {
1646                message => $app->translate(
1647                    "Ping '[_1]' failed: [_2]",
1648                    $res->{url},
1649                    encode_text( $res->{error}, undef, undef )
1650                ),
1651                class => 'system',
1652                level => MT::Log::WARNING()
1653            }
1654          ) unless $res->{good};
1655    }
1656    _finish_rebuild_ping( $app, $entry, scalar $q->param('is_new'),
1657        $has_errors );
1658}
1659
1660sub pinged_urls {
1661    my $app   = shift;
1662    my $perms = $app->permissions
1663      or return $app->error( $app->translate("No permissions") );
1664    my %param;
1665    my $entry_id = $app->param('entry_id');
1666    require MT::Entry;
1667    my $entry = MT::Entry->load($entry_id)
1668        or return $app->error($app->translate('Can\'t load entry #[_1].', $entry_id));
1669    $param{url_loop} = [ map { { url => $_ } } @{ $entry->pinged_url_list } ];
1670    $param{failed_url_loop} =
1671      [ map { { url => $_ } }
1672          @{ $entry->pinged_url_list( OnlyFailures => 1 ) } ];
1673    $app->load_tmpl( 'popup/pinged_urls.tmpl', \%param );
1674}
1675
1676sub save_entry_prefs {
1677    my $app   = shift;
1678    my $perms = $app->permissions
1679      or return $app->error( $app->translate("No permissions") );
1680    $app->validate_magic() or return;
1681    my $q     = $app->param;
1682    my $prefs = $app->_entry_prefs_from_params;
1683    $perms->entry_prefs($prefs);
1684    $perms->save
1685      or return $app->error(
1686        $app->translate( "Saving permissions failed: [_1]", $perms->errstr ) );
1687    $app->send_http_header("text/json");
1688    return "true";
1689}
1690
1691sub publish_entries {
1692    my $app = shift;
1693    require MT::Entry;
1694    update_entry_status( $app, MT::Entry::RELEASE(), $app->param('id') );
1695}
1696
1697sub draft_entries {
1698    my $app = shift;
1699    require MT::Entry;
1700    update_entry_status( $app, MT::Entry::HOLD(), $app->param('id') );
1701}
1702
1703sub open_batch_editor {
1704    my $app = shift;
1705    my @ids = $app->param('id');
1706
1707    $app->param( 'is_power_edit', 1 );
1708    $app->param( 'filter',        'power_edit' );
1709    $app->param( 'filter_val',    \@ids );
1710    $app->mode(
1711        'list_' . ( 'entry' eq $app->param('_type') ? 'entries' : 'pages' ) );
1712    $app->forward( "list_entry", { type => $app->param('_type') } );
1713}
1714
1715sub build_entry_table {
1716    my $app = shift;
1717    my (%args) = @_;
1718
1719    my $app_author = $app->user;
1720    my $perms      = $app->permissions;
1721    my $type       = $args{type};
1722    my $class      = $app->model($type);
1723
1724    my $list_pref = $app->list_pref($type);
1725    if ( $args{is_power_edit} ) {
1726        delete $list_pref->{view_expanded};
1727    }
1728    my $iter;
1729    if ( $args{load_args} ) {
1730        $iter = $class->load_iter( @{ $args{load_args} } );
1731    }
1732    elsif ( $args{iter} ) {
1733        $iter = $args{iter};
1734    }
1735    elsif ( $args{items} ) {
1736        $iter = sub { shift @{ $args{items} } };
1737    }
1738    return [] unless $iter;
1739    my $limit         = $args{limit};
1740    my $is_power_edit = $args{is_power_edit} || 0;
1741    my $param         = $args{param} || {};
1742
1743    ## Load list of categories for display in filter pulldown (and selection
1744    ## pulldown on power edit page).
1745    my ( $c_data, %cats );
1746    my $blog_id = $app->param('blog_id');
1747    if ($blog_id) {
1748        $c_data = $app->_build_category_list(
1749            blog_id => $blog_id,
1750            type    => $class->container_type,
1751        );
1752        my $i = 0;
1753        for my $row (@$c_data) {
1754            $row->{category_index} = $i++;
1755            my $spacer = $row->{category_label_spacer} || '';
1756            $spacer =~ s/\&nbsp;/\\u00A0/g;
1757            $row->{category_label_js} =
1758              $spacer . encode_js( $row->{category_label} );
1759            $cats{ $row->{category_id} } = $row;
1760        }
1761        $param->{category_loop} = $c_data;
1762    }
1763
1764    my ( $date_format, $datetime_format );
1765
1766    if ($is_power_edit) {
1767        $date_format          = "%Y.%m.%d";
1768        $datetime_format      = "%Y-%m-%d %H:%M:%S";
1769    }
1770    else {
1771        $date_format     = MT::App::CMS::LISTING_DATE_FORMAT();
1772        $datetime_format = MT::App::CMS::LISTING_DATETIME_FORMAT();
1773    }
1774
1775    my @cat_list;
1776    if ($is_power_edit) {
1777        @cat_list =
1778          sort { $cats{$a}->{category_index} <=> $cats{$b}->{category_index} }
1779          keys %cats;
1780    }
1781
1782    my @data;
1783    my %blogs;
1784    require MT::Blog;
1785    my $title_max_len   = const('DISPLAY_LENGTH_EDIT_ENTRY_TITLE');
1786    my $excerpt_max_len = const('DISPLAY_LENGTH_EDIT_ENTRY_TEXT_FROM_EXCERPT');
1787    my $text_max_len    = const('DISPLAY_LENGTH_EDIT_ENTRY_TEXT_BREAK_UP');
1788    my %blog_perms;
1789    $blog_perms{ $perms->blog_id } = $perms if $perms;
1790    while ( my $obj = $iter->() ) {
1791        my $blog_perms;
1792        if ( !$app_author->is_superuser() ) {
1793            $blog_perms = $blog_perms{ $obj->blog_id }
1794              || $app_author->blog_perm( $obj->blog_id );
1795        }
1796
1797        my $row = $obj->column_values;
1798        $row->{text} ||= '';
1799        if ( my $ts =
1800            ( $type eq 'page' ) ? $obj->modified_on : $obj->authored_on )
1801        {
1802            $row->{created_on_formatted} =
1803              format_ts( $date_format, $ts, $obj->blog, $app->user ? $app->user->preferred_language : undef );
1804            $row->{created_on_time_formatted} =
1805              format_ts( $datetime_format, $ts, $obj->blog, $app->user ? $app->user->preferred_language : undef );
1806            $row->{created_on_relative} =
1807              relative_date( $ts, time, $obj->blog );
1808        }
1809        my $author = $obj->author;
1810        $row->{author_name} =
1811          $author ? $author->name : $app->translate('(user deleted)');
1812        if ( my $cat = $obj->category ) {
1813            $row->{category_label}    = $cat->label;
1814            $row->{category_basename} = $cat->basename;
1815        }
1816        else {
1817            $row->{category_label}    = '';
1818            $row->{category_basename} = '';
1819        }
1820        $row->{file_extension} = $obj->blog ? $obj->blog->file_extension : '';
1821        $row->{title_short} = $obj->title;
1822        if ( !defined( $row->{title_short} ) || $row->{title_short} eq '' ) {
1823            my $title = remove_html( $obj->text );
1824            $row->{title_short} =
1825              substr_text( defined($title) ? $title : "", 0, $title_max_len )
1826              . '...';
1827        }
1828        else {
1829            $row->{title_short} = remove_html( $row->{title_short} );
1830            $row->{title_short} =
1831              substr_text( $row->{title_short}, 0, $title_max_len + 3 ) . '...'
1832              if length_text( $row->{title_short} ) > $title_max_len;
1833        }
1834        if ( $row->{excerpt} ) {
1835            $row->{excerpt} = remove_html( $row->{excerpt} );
1836        }
1837        if ( !$row->{excerpt} ) {
1838            my $text = remove_html( $row->{text} ) || '';
1839            $row->{excerpt} = first_n_text( $text, $excerpt_max_len );
1840            if ( length($text) > length( $row->{excerpt} ) ) {
1841                $row->{excerpt} .= ' ...';
1842            }
1843        }
1844        $row->{text} = break_up_text( $row->{text}, $text_max_len )
1845          if $row->{text};
1846        $row->{title_long} = remove_html( $obj->title );
1847        $row->{status_text} =
1848          $app->translate( MT::Entry::status_text( $obj->status ) );
1849        $row->{ "status_" . MT::Entry::status_text( $obj->status ) } = 1;
1850        $row->{has_edit_access} = $app_author->is_superuser
1851          || ( ( 'entry' eq $type )
1852            && $blog_perms
1853            && $blog_perms->can_edit_entry( $obj, $app_author ) )
1854          || ( ( 'page' eq $type )
1855            && $blog_perms
1856            && $blog_perms->can_manage_pages );
1857        if ($is_power_edit) {
1858            $row->{has_publish_access} = $app_author->is_superuser
1859              || ( ( 'entry' eq $type )
1860                && $blog_perms
1861                && $blog_perms->can_edit_entry( $obj, $app_author, 1 ) )
1862              || ( ( 'page' eq $type )
1863                && $blog_perms
1864                && $blog_perms->can_manage_pages );
1865            $row->{is_editable} = $row->{has_edit_access};
1866
1867            ## This is annoying. In order to generate and pre-select the
1868            ## category, user, and status pull down menus, we need to
1869            ## have a separate *copy* of the list of categories and
1870            ## users for every entry listed, so that each row in the list
1871            ## can "know" whether it is selected for this entry or not.
1872            my @this_c_data;
1873            my $this_category_id = $obj->category ? $obj->category->id : undef;
1874            for my $c_id (@cat_list) {
1875                push @this_c_data, { %{ $cats{$c_id} } };
1876                $this_c_data[-1]{category_is_selected} = $this_category_id
1877                  && $this_category_id == $c_id ? 1 : 0;
1878            }
1879            $row->{row_category_loop} = \@this_c_data;
1880
1881            if ( $obj->author ) {
1882                $row->{row_author_name} = $obj->author->name;
1883                $row->{row_author_id}   = $obj->author->id;
1884            } else {
1885                $row->{row_author_name} = $app->translate(
1886                    '(user deleted - ID:[_1])',
1887                    $obj->author_id
1888                );
1889                $row->{row_author_id} = $obj->author_id,
1890             }
1891        }
1892        if ( my $blog = $blogs{ $obj->blog_id } ||=
1893            MT::Blog->load( $obj->blog_id ) )
1894        {
1895            $row->{weblog_id}   = $blog->id;
1896            $row->{weblog_name} = $blog->name;
1897        }
1898        if ( $obj->status == MT::Entry::RELEASE() ) {
1899            $row->{entry_permalink} = $obj->permalink;
1900        }
1901        $row->{object} = $obj;
1902        push @data, $row;
1903    }
1904    return [] unless @data;
1905
1906    $param->{entry_table}[0] = {%$list_pref};
1907    $param->{object_loop} = $param->{entry_table}[0]{object_loop} = \@data;
1908    $app->load_list_actions( $type, \%$param )
1909      unless $is_power_edit;
1910    \@data;
1911}
1912
1913sub quickpost_js {
1914    my $app     = shift;
1915    my ($type)  = @_;
1916    my $blog_id = $app->blog->id;
1917    my $blog    = $app->model('blog')->load($blog_id)
1918        or return $app->error($app->translate('Can\'t load blog #[_1].', $blog_id));
1919    my %args    = ( '_type' => $type, blog_id => $blog_id, qp => 1 );
1920    my $uri = $app->base . $app->uri( 'mode' => 'view', args => \%args );
1921    my $script = qq!javascript:d=document;w=window;t='';if(d.selection)t=d.selection.createRange().text;else{if(d.getSelection)t=d.getSelection();else{if(w.getSelection)t=w.getSelection()}}void(w.open('$uri&title='+encodeURIComponent(d.title)+'&text='+encodeURIComponent(d.location.href)+encodeURIComponent('<br/><br/>')+encodeURIComponent(t),'_blank','scrollbars=yes,status=yes,resizable=yes,location=yes'))!;
1922    # Translate the phrase here to avoid ActivePerl DLL bug.
1923    $app->translate('<a href="[_1]">QuickPost to [_2]</a> - Drag this link to your browser\'s toolbar then click it when you are on a site you want to blog about.', encode_html($script), $blog->name);
1924}
1925
1926sub can_view {
1927    my ( $eh, $app, $id, $objp ) = @_;
1928    my $perms = $app->permissions;
1929    if (   !$id
1930        && !$perms->can_create_post )
1931    {
1932        return 0;
1933    }
1934    if ($id) {
1935        my $obj = $objp->force();
1936        if ( !$perms->can_edit_entry( $obj, $app->user ) ) {
1937            return 0;
1938        }
1939    }
1940    1;
1941}
1942
1943sub can_delete {
1944    my ( $eh, $app, $obj ) = @_;
1945    my $author = $app->user;
1946    return 1 if $author->is_superuser();
1947    my $perms = $app->permissions;
1948    if ( !$perms || $perms->blog_id != $obj->blog_id ) {
1949        $perms ||= $author->permissions( $obj->blog_id );
1950    }
1951    return $perms && $perms->can_edit_entry( $obj, $author );
1952}
1953
1954sub pre_save {
1955    my $eh = shift;
1956    my ( $app, $obj ) = @_;
1957
1958    # save tags
1959    my $tags = $app->param('tags');
1960    if ( defined $tags ) {
1961        my $blog = $app->blog;
1962        my $fields = $blog->smart_replace_fields;
1963        if ( $fields =~ m/tags/ig ) {
1964            $tags = MT::App::CMS::_convert_word_chars( $app, $tags );
1965        }
1966
1967        require MT::Tag;
1968        my $tag_delim = chr( $app->user->entry_prefs->{tag_delim} );
1969        my @tags = MT::Tag->split( $tag_delim, $tags );
1970        if (@tags) {
1971            $obj->set_tags(@tags);
1972        }
1973        else {
1974            $obj->remove_tags();
1975        }
1976    }
1977
1978    # update text heights if necessary
1979    if ( my $perms = $app->permissions ) {
1980        my $prefs = $perms->entry_prefs || $app->load_default_entry_prefs;
1981        my $text_height = $app->param('text_height');
1982        if ( defined $text_height ) {
1983            my ($pref_text_height) = $prefs =~ m/\bbody:(\d+)\b/;
1984            $pref_text_height ||= 0;
1985            if ( $text_height != $pref_text_height ) {
1986                if ( $prefs =~ m/\bbody\b/ ) {
1987                    $prefs =~ s/\bbody(:\d+)\b/body:$text_height/;
1988                }
1989                else {
1990                    $prefs = 'body:' . $text_height . ',' . $prefs;
1991                }
1992            }
1993        }
1994        if ( $prefs ne ( $perms->entry_prefs || '' ) ) {
1995            $perms->entry_prefs($prefs);
1996            $perms->save;
1997        }
1998    }
1999    $obj->discover_tb_from_entry();
2000    1;
2001}
2002
2003sub post_save {
2004    my $eh = shift;
2005    my ( $app, $obj ) = @_;
2006    my $sess_obj = $app->autosave_session_obj;
2007    $sess_obj->remove if $sess_obj;
2008    1;
2009}
2010
2011sub post_delete {
2012    my ( $eh, $app, $obj ) = @_;
2013
2014    my $sess_obj = $app->autosave_session_obj;
2015    $sess_obj->remove if $sess_obj;
2016
2017    $app->log(
2018        {
2019            message => $app->translate(
2020                "Entry '[_1]' (ID:[_2]) deleted by '[_3]'",
2021                $obj->title, $obj->id, $app->user->name
2022            ),
2023            level    => MT::Log::INFO(),
2024            class    => 'system',
2025            category => 'delete'
2026        }
2027    );
2028}
2029
2030sub update_entry_status {
2031    my $app = shift;
2032    my ( $new_status, @ids ) = @_;
2033    return $app->errtrans("Need a status to update entries")
2034      unless $new_status;
2035    return $app->errtrans("Need entries to update status")
2036      unless @ids;
2037    my @bad_ids;
2038    my %rebuild_these;
2039    require MT::Entry;
2040
2041    foreach my $id (@ids) {
2042        my $entry = MT::Entry->load($id)
2043          or return $app->errtrans(
2044            "One of the entries ([_1]) did not actually exist", $id );
2045        if ( $app->config('DeleteFilesAtRebuild')
2046            && ( MT::Entry::RELEASE() eq $entry->status ) )
2047        {
2048            my $archive_type =
2049              $entry->class eq 'page'
2050              ? 'Page'
2051              : 'Individual';
2052            $app->publisher->remove_entry_archive_file(
2053                Entry       => $entry,
2054                ArchiveType => $archive_type
2055            );
2056        }
2057        my $old_status = $entry->status;
2058        $entry->status($new_status);
2059        $entry->save() and $rebuild_these{$id} = 1;
2060        my $message = $app->translate(
2061            "[_1] '[_2]' (ID:[_3]) status changed from [_4] to [_5]",
2062            $entry->class_label,
2063            $entry->title,
2064            $entry->id,
2065            $app->translate( MT::Entry::status_text($old_status) ),
2066            $app->translate( MT::Entry::status_text($new_status) )
2067        );
2068        $app->log(
2069            {
2070                message  => $message,
2071                level    => MT::Log::INFO(),
2072                class    => $entry->class,
2073                category => 'edit',
2074                metadata => $entry->id
2075            }
2076        );
2077    }
2078    $app->rebuild_these( \%rebuild_these, how => MT::App::CMS::NEW_PHASE() );
2079}
2080
2081sub _finish_rebuild_ping {
2082    my $app = shift;
2083    my ( $entry, $is_new, $ping_errors ) = @_;
2084    $app->redirect(
2085        $app->uri(
2086            'mode' => 'view',
2087            args   => {
2088                '_type' => $entry->class,
2089                blog_id => $entry->blog_id,
2090                id      => $entry->id,
2091                ( $is_new ? ( saved_added => 1 ) : ( saved_changes => 1 ) ),
2092                ( $ping_errors ? ( ping_errors => 1 ) : () )
2093            }
2094        )
2095    );
2096}
2097
2098sub ping_continuation {
2099    my $app = shift;
2100    my ( $entry, $blog, %options ) = @_;
2101    my $list = $app->needs_ping(
2102        Entry     => $entry,
2103        Blog      => $blog,
2104        OldStatus => $options{OldStatus}
2105    );
2106    require MT::Entry;
2107    if ( $entry->status == MT::Entry::RELEASE() && $list ) {
2108        my @urls = map { { url => $_ } } @$list;
2109        $app->load_tmpl(
2110            'pinging.tmpl',
2111            {
2112                blog_id    => $blog->id,
2113                entry_id   => $entry->id,
2114                old_status => $options{OldStatus},
2115                is_new     => $options{IsNew},
2116                url_list   => \@urls,
2117            }
2118        );
2119    }
2120    else {
2121        _finish_rebuild_ping( $app, $entry, $options{IsNew} );
2122    }
2123}
2124
2125sub delete {
2126    my $app = shift;
2127    $app->validate_magic() or return;
2128    require MT::Blog;
2129    my $q       = $app->param;
2130    my $blog_id = $q->param('blog_id');
2131    my $blog    = MT::Blog->load($blog_id)
2132        or return $app->error($app->translate('Can\'t load blog #[_1].', $blog_id));
2133
2134    my $can_background =
2135        ( $blog->count_static_templates('Individual') == 0
2136            || MT::Util->launch_background_tasks() ) ? 1 : 0;
2137
2138    my %rebuild_recipe;
2139    for my $id ( $q->param('id') ) {
2140        my $class = $app->model("entry");
2141        my $obj   = $class->load($id);
2142        return $app->call_return unless $obj;
2143
2144        $app->run_callbacks( 'cms_delete_permission_filter.entry', $app, $obj )
2145          || return $app->error(
2146            $app->translate( "Permission denied: [_1]", $app->errstr() ) );
2147
2148        my %recipe = $app->publisher->rebuild_deleted_entry(
2149            Entry => $obj,
2150            Blog  => $blog);
2151
2152        # Remove object from database
2153        $obj->remove()
2154            or return $app->errtrans(
2155                'Removing [_1] failed: [_2]',
2156                $app->translate('entry'),
2157                $obj->errstr
2158            );
2159        $app->run_callbacks( 'cms_post_delete.entry', $app, $obj );
2160    }
2161
2162    $app->add_return_arg( saved_deleted => 1 );
2163    if ( $q->param('is_power_edit') ) {
2164        $app->add_return_arg( is_power_edit => 1 );
2165    }
2166
2167    if ( $app->config('RebuildAtDelete') ) {
2168        if ($can_background) {
2169            my $res = MT::Util::start_background_task(
2170                sub {
2171                    my $res = $app->rebuild_archives(
2172                        Blog             => $blog,
2173                        Recipe           => \%rebuild_recipe,
2174                    ) or return $app->publish_error();
2175                    $app->rebuild_indexes( Blog => $blog )
2176                        or return $app->publish_error();
2177                    $app->run_callbacks( 'rebuild', $blog );
2178                    1;
2179                }
2180            );
2181        }
2182        else {
2183            $app->rebuild_archives(
2184                Blog             => $blog,
2185                Recipe           => \%rebuild_recipe,
2186            ) or return $app->publish_error();
2187            $app->rebuild_indexes( Blog => $blog )
2188                or return $app->publish_error();
2189
2190            $app->run_callbacks( 'rebuild', $blog );
2191        }
2192
2193        $app->add_return_arg( no_rebuild => 1 );
2194        my %params = (
2195            is_full_screen  => 1,
2196            redirect_target => $app->app_path
2197              . $app->script . '?'
2198              . $app->return_args,
2199        );
2200        return $app->load_tmpl( 'rebuilding.tmpl', \%params );
2201    }
2202
2203    return $app->call_return();
2204}
2205
22061;
Note: See TracBrowser for help on using the browser.