Created
February 20, 2014 09:33
-
-
Save alfasado/9110035 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' } ) ); | |
} | |
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; | |
} | |
} | |
return 1; | |
} | |
} | |
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{ | |
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; | |
} | |
} | |
return 1; | |
} | |
sub _categoryId { | |
my ( $entry, $hash, $field, $stash ) = @_; | |
my $category = $entry->category; | |
if (! $category ) { | |
return 0; | |
} | |
return $category->id; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment