sub
_init {
my
$self
=
shift
;
$self
->set_tag_to_items( {} );
$self
->set_item_to_tags( {} );
}
sub
_items_for_tag {
my
(
$self
,
$tag
,
$paginate_start
,
$paginate_length
) =
@_
;
my
$xpath
=
$self
->_path_to_root() .
'/tag_to_items'
;
return
Yote::ObjProvider::paginate_xpath(
$xpath
,
$paginate_start
,
$paginate_length
);
}
sub
_items_for_tags {
my
(
$self
,
$args
) =
@_
;
my
$tags
=
$args
->{tags};
my
$exclude
=
$args
->{exclude_tags};
my
(
%res
,
%scores
);
for
my
$tag
(
@$tags
) {
my
$items
=
$self
->_items_for_tag(
$tag
);
for
my
$item
(
@$items
) {
if
(
$exclude
) {
next
if
grep
{
$self
->_has_tag(
$item
,
$_
) }
@$exclude
;
}
$res
{
$item
} =
$item
;
$scores
{
$item
}++;
}
}
return
[
sort
{
$scores
{
$b
} <=>
$scores
{
$a
} }
values
%res
];
}
sub
_has_tag {
my
(
$self
,
$item
,
$tag
) =
@_
;
my
$xpath
=
$self
->_path_to_root();
return
Yote::ObjProvider::xpath_count(
"$xpath/item_to_tags/$tag"
) > 0;
}
sub
_add_tag {
my
(
$self
,
$tag
,
@items
) =
@_
;
my
$xpath
=
$self
->_path_to_root();
for
my
$item
(
@items
) {
Yote::ObjProvider::xpath_insert(
"$xpath/tag_to_items/$tag"
,
$item
);
Yote::ObjProvider::xpath_insert(
"$xpath/item_to_tags/$item->{ID}"
,
$tag
);
}
}
sub
_remove_tag {
my
(
$self
,
$tag
,
@items
) =
@_
;
for
my
$item
(
@items
) {
Yote::ObjProvider::xpath_insert(
"$xpath/item_to_tags/$item->{ID}/$tag"
);
}
}
1;