Mailing List Archive

Removing categories in templates
Hi everybody,

I'm using a template to add and remove secondary categories from a
story, and I've hit a snag.

Here's how it's supposed to work:

1. User chooses secondary categories from a code select, which offers a
list based on the primary category of the story. For example, in the
primary category "/baking/", the code select shows:

Yeast (Which points to /z-secondary/baking/yeast/)
Flour (Which points to /z-secondary/baking/flour/)

The names of the secondaries all start with z to keep them out of the
way in the UI.

2. When the story is previewed or published, if any of the secondary
categories chosen in the code select are not already associated with the
story, they are added.

3. Next, I want to remove any secondary categories that are present in
the story, but not picked in the code select. This seems to kind of sort
of work, but not really.

For example, if I choose "Flour" and deselect "Yeast," Bricolage throws
this error on preview:

Category '/z-secondary/baking/yeast/' not associated with story
'Delicious Flour'

This error is from line 1272 of Story.pm, in the get_uri method.
So it looks like the category is getting deleted, but it's not
registering everywhere it needs to before get_uri gets called.

Does anybody know if there's a callback I should be calling after the
category deletion?


Here's the template code:

my @desired_cat_uris = $code_select->get_values;
my @current_cat_uris = map ($_->get_uri,
$story->get_secondary_categories);

# Add the desired categories
foreach my $cat_to_possibly_add (@desired_cat_uris) {
if (my $cat_obj = Bric::Biz::Category->lookup({'uri' =>
$cat_to_possibly_add})) {
$story->add_categories([$cat_obj]) unless grep ({$_ eq
$cat_to_possibly_add} @current_cat_uris);
$story->save;
}
}

# Remove all secondary categories
foreach my $cat_to_possibly_remove (@current_cat_uris) {
if (substr($cat_to_possibly_remove,1,1) eq 'z-secondary') {
$the_story->delete_categories([Bric::Biz::Category->lookup({'uri' =>
$cat_to_possibly_remove})]) unless grep ({$_ eq $cat_to_possibly_remove}
@desired_cat_uris);
$story->save;
}
}


Thanks so much,

Bret



--
Bret Dawson
Producer
Pectopah Productions Inc.
(416) 895-7635
bret@pectopah.com
www.pectopah.com
Re: Removing categories in templates [ In reply to ]
On Feb 15, 2011, at 2:52 PM, Bret Dawson wrote:

> 3. Next, I want to remove any secondary categories that are present in
> the story, but not picked in the code select. This seems to kind of sort
> of work, but not really.
>
> For example, if I choose "Flour" and deselect "Yeast," Bricolage throws
> this error on preview:
>
> Category '/z-secondary/baking/yeast/' not associated with story
> 'Delicious Flour'
>
> This error is from line 1272 of Story.pm, in the get_uri method.
> So it looks like the category is getting deleted, but it's not
> registering everywhere it needs to before get_uri gets called.
>
> Does anybody know if there's a callback I should be calling after the
> category deletion?

Bric::SOAP uses this pattern. Here's the code:

# remove all categories if updating
if ($update) {
if (my $cats = $story->get_categories) {
# Delete 'em and log it.
$story->delete_categories($cats);
foreach my $cat (@$cats) {
log_event('story_del_category', $story,
{ Category => $cat->get_name });
}
}
}

# assign categories
my @cids;
my $primary_cid;
foreach my $cdata (@{$sdata->{categories}{category}}) {
# get cat id
my $path = ref $cdata ? $cdata->{content} : $cdata;
(my $look = $path) =~ s/([_%\\])/\\$1/g;
my $cat = Bric::Biz::Category->lookup({
uri => $look,
site_id => $init{site_id}
});
throw_ap(error => __PACKAGE__ . "::create : no category found matching "
. "(category => \"$path\")")
unless defined $cat;

my $category_id = $cat->get_id;
push(@cids, $category_id);
$primary_cid = $category_id
if ref $cdata and $cdata->{primary};

# Log it!
log_event('story_add_category', $story,
{ Category => $cat->get_name });
}

# sanity checks
throw_ap(error => __PACKAGE__ . "::create : no categories defined!")
unless @cids;
throw_ap(error => __PACKAGE__ . "::create : no primary category defined!")
unless defined $primary_cid;

# add categories to story
$story->add_categories(\@cids);
$story->set_primary_category($primary_cid);

HTH,

David