Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package MAUS::Callbacks;
use strict;
sub _data_api_pre_save_entry {
my ( $cb, $app, $obj, $original ) = @_;
my $entry = $app->param( 'entry' );
$entry = MT::DataAPI::Format::JSON::unserialize( $entry );
# if ( $entry->{ 'convert_breaks' } ) {
# $obj->convert_breaks( lc( $entry->{ 'convert_breaks' } ) );
# }
my $tags = $entry->{ tags };
if (ref $tags ne 'ARRAY' ) {
$obj->remove_tags;
}
return 1;
}
sub _setCategoryId {
my ( $entry, $obj, $field, $stash ) = @_;
if ( defined( $entry->{ 'categoryId' } ) ) {
my $categoryId = $entry->{ 'categoryId' };
if ( $categoryId == 0 ) {
if ( $obj->id ) {
my $categories = $obj->categories;
if ( $categories ) {
my @placement = MT->model( 'placement' )->load( { entry_id => $obj->id } );
for my $place ( @placement ) {
$place->remove or die $place->errstr;
}
$obj->clear_cache;
}
return;
}
}
my $is_in_category;
if (! $obj->id ) {
$obj->save;
} else {
my $categories = $obj->categories;
if ( $categories ) {
for my $cat ( @$categories ) {
my $placement = MT->model( 'placement' )->load( { category_id => $cat->id,
entry_id => $obj->id } );
if ( $cat->id == $categoryId ) {
if (! $placement->is_primary ) {
$placement->is_primary( 1 );
$placement->save or die $placement->errstr;
}
$is_in_category = 1;
} else{
$placement->remove or die $placement->errstr;
# if ( $placement->is_primary ) {
# $placement->is_primary( 0 );
# $placement->save or die $placement->errstr;
# }
}
}
}
}
if (! $is_in_category ) {
my $placement = MT->model( 'placement' )->new;
$placement->blog_id( $obj->blog_id );
$placement->entry_id( $obj->id );
$placement->category_id( $categoryId );
$placement->is_primary( 1 );
$placement->save or die $placement->errstr;
}
$obj->clear_cache;
}
return;
}
sub _categoryId {
my ( $entry, $hash, $field, $stash ) = @_;
my $placement = MT->model( 'placement' )->load( { entry_id => $entry->id,
is_primary => 1 } );
if (! $placement ) {
return 0;
}
# if ( $entry->category ) {
# return $entry->category->id;
# }
return $placement->category_id;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment