Changeset 2829
- Timestamp:
- 07/22/08 21:22:43 (4 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/feature-revision-histories/lib/MT/Revisable.pm
r2826 r2829 15 15 my ($class) = @_; 16 16 my $props = $class->properties; 17 my $datasource = $class->datasource; 17 18 18 19 $props->{column_defs}{current_revision} = { … … 24 25 $props->{defaults}{current_revision} = 0; 25 26 26 MT->add_callback( 'api_pre_save.entry', 1, undef,27 \&mt_remove_unchanged_cols );28 MT->add_callback( ' cms_pre_save.entry', 1, undef,29 \&mt_remove_unchanged_cols);27 # Callbacks: clean list of changed columns to only 28 # include versioned columns 29 MT->add_callback( 'api_pre_save.' . $datasource, 1, undef, \&mt_presave_obj ); 30 MT->add_callback( 'cms_pre_save.' . $datasource, 1, undef, \&mt_presave_obj ); 30 31 31 $class->add_trigger( pre_save => \&increment_revision ); 32 $class->add_trigger( post_save => \&save_revision ); 32 # Callbacks: object-level callbacks could not be 33 # prioritized and thus caused problems with plugins 34 # registering a post_save and saving 35 MT->add_callback( 'api_post_save.' . $datasource, 9, undef, \&mt_postsave_obj); 36 MT->add_callback( 'cms_post_save.' . $datasource, 9, undef, \&mt_postsave_obj); 33 37 } 34 38 … … 132 136 } 133 137 134 sub mt_ remove_unchanged_cols{138 sub mt_presave_obj { 135 139 my ($cb, $app, $obj, $orig) = @_; 136 remove_unchanged_cols($obj, $orig); 137 } 138 139 sub remove_unchanged_cols { 140 my ($obj, $orig) = @_; 141 142 return 1 unless defined $orig; 143 return 1 unless $obj->id; 144 140 141 $obj->gather_changed_cols($orig); 142 143 $obj->increment_revision($orig); # Added here for consistency 144 } 145 146 sub mt_postsave_obj { 147 my ($cb, $app, $obj, $orig) = @_; 148 149 $obj->save_revision(); 150 } 151 152 sub gather_changed_cols { 153 my $obj = shift; 154 my ($orig) = @_; 155 156 my @changed_cols; 157 my $revisioned_cols = $obj->revisioned_columns; 158 145 159 my %date_cols = map { $_ => 1 } 146 160 @{$obj->columns_of_type('datetime', 'timestamp')}; 147 148 if ( my @changed_cols = $obj->changed_cols ) { 149 for my $col ( @changed_cols ) { 150 unless($obj->is_revisioned_column($col)) { 151 delete $obj->{changed_cols}->{$col}; 152 } 153 if ( $obj->$col eq $orig->$col ) { 154 delete $obj->{changed_cols}->{$col}; 155 } 156 elsif ( exists $date_cols{$col} ) { 157 delete $obj->{changed_cols}->{$col} 158 if $orig->$col eq MT::Object::_db2ts($obj->$col); 159 } 160 } 161 } 161 162 foreach my $col (@$revisioned_cols) { 163 next if $obj->$col eq $orig->$col; 164 next if exists $date_cols{$col} 165 && $orig->$col eq MT::Object::_db2ts($obj->$col); 166 167 push @changed_cols, $col; 168 } 169 170 $obj->{changed_revisioned_cols} = \@changed_cols; 162 171 1; 163 172 } … … 173 182 174 183 my $meta_values = $obj->meta; 175 foreach my $key ( %$meta_values) {184 foreach my $key (keys %$meta_values) { 176 185 $values->{$key} = $meta_values->{$key}; 177 186 } … … 190 199 my $obj = shift; 191 200 my ($orig) = @_; 201 202 my $changed_cols = $obj->{changed_revisioned_cols}; 203 return 1 unless scalar @$changed_cols > 0; 192 204 193 205 # We default current_revision to 0 so we can always increment … … 199 211 sub save_revision { 200 212 my $obj = shift; 201 my ($orig) = @_; 202 return 1 unless $orig->id; 213 return 1 unless $obj->id; 214 215 my $changed_cols = $obj->{changed_revisioned_cols}; 216 return 1 unless scalar @$changed_cols > 0; 203 217 204 218 my $datasource = $obj->datasource; 205 219 my $obj_id = $datasource . '_id'; 206 my $packed_obj = $o rig->pack_revision();220 my $packed_obj = $obj->pack_revision(); 207 221 208 222 require MT::Serialize; … … 210 224 my $revision = $rev_class->new; 211 225 $revision->set_values({ 212 $obj_id => $o rig->id,226 $obj_id => $obj->id, 213 227 $datasource => MT::Serialize->serialize(\$packed_obj), 214 changed => join ',', $obj->changed_cols228 changed => join ',', @$changed_cols 215 229 }); 216 230 $revision->rev_number($obj->current_revision); … … 313 327 my $diff_method = $diff_args->{method} || 'html_word_diff'; 314 328 my $limit_unchanged = $diff_args->{limit_unchanged}; 315 329 316 330 require HTML::Diff; 331 Carp::croak(MT->translate("Unknown method [_1]", 'HTML::Diff::' . $diff_method)) 332 unless HTML::Diff->can($diff_method); 333 317 334 my $diff_result = eval "HTML::Diff::$diff_method(\$str_a, \$str_b)"; 318 335 my @result;
