<%perl>;
$m->comp('/widgets/listManager/listManager.mc',
         object    => 'category',
         userSort => 0,
         def_sort_field => 'uri',
         title     => 'Current Categories',
         objs      => \@categories,
         profile   => $category_current_profile,
         fields    => [qw(uri site name)],
         select =>  $category_select,
         addition  => ''
        );
</%perl>
<p></p>

<& /widgets/search/search.mc,
   object       => 'category',
   field        => 'uri',
   type         => 'singular',
   use_form_tag => 0
&>
<p></p>
<& /widgets/listManager/listManager.mc,
   object    => 'category',
   title     => 'Choose Categories',
   select    => undef,
   profile   => $category_choose_profile,
   exclude   => $exclude_cats,
   fields    => [qw(uri site name)],
   addition  => '',
   behavior => 'expand',
&>

<p></p>
<input type="image" src="/media/images/<% $lang_key %>/save_red.gif" border="0" name="<% $widget %>|save_category_cb" value="Save" />
<input type="image" src="/media/images/<% $lang_key %>/save_and_stay_lgreen.gif" border="0" name="<% $widget %>|save_and_stay_category_cb" value="Save and Stay" />
<input type="image" src="/media/images/<% $lang_key %>/return_dgreen.gif" border="0" name="<% $widget %>|leave_category_cb" />

<%args>
$widget
</%args>

<%init>
my $story = get_state_data($widget, 'story');
my @categories = $story->get_categories();
my $exclude_cats = sub {
    my $cat = shift;

    # exclude unauthorized categories
    return 1 unless chk_authz($cat, READ, 1);

    # exclude already-associated categories
    my $cid = $cat->get_id;
    foreach my $ex (@categories) {
        return 1 if $cid == $ex->get_id;
    }

    return 0;
};
</%init>

<%once>
my $category_current_profile = sub {
    my $cat = shift;
    my $widget = 'story_prof';
    my $story = get_state_data($widget, 'story');
    my $primary_cat = $story->get_primary_category;

    if ($cat->get_name eq $primary_cat->get_name) {
        return;
    } else {
        return ['Primary', $r->uri, "$widget|set_primary_category_cb=" . $cat->get_id];
    }
};
my $category_choose_profile = sub {
    my $cat = shift;
    my $widget = 'story_prof';
    return ['Associate', $r->uri, "$widget|assoc_category_cb=" . $cat->get_id];
};
my $category_select = sub {
    my $cat = shift;
    my $widget = 'story_prof';
    my $story = get_state_data($widget, 'story');
    my $primary_cat = $story->get_primary_category;

    if ($cat->get_name eq $primary_cat->get_name) {
        return;
    } else {
        return ['Delete', "$widget|delete_id"];
    }
};
</%once>
