1
0

Handle sorting, removing duplicate tags, and limiting the number of tags displayed in the database query instead of using PHP code.

This commit is contained in:
rWatcher 2011-06-11 15:21:46 -04:00
parent cf75213423
commit e77473bd51
4 changed files with 40 additions and 56 deletions

View File

@ -29,12 +29,26 @@ class tagsinalbum_block_Core {
case "tagsinalbum":
if (($theme->item) && ($theme->item->is_album())) {
$item = $theme->item;
$all_tags = ORM::factory("tag")
// Create an ORM query for finding one instance of each tag
// used by children in the current album.
$tags_model = ORM::factory("tag")
->join("items_tags", "items_tags.tag_id", "tags.id")
->join("items", "items.id", "items_tags.item_id", "LEFT")
->where("items.parent_id", "=", $item->id)
->order_by("tags.id", "ASC")
->find_all();
->order_by("tags.name", "ASC")
->group_by("tags.id");
// Limit $all_tags to the first X tags if max_display_tags is set,
// else populate it with all tags used by this album's children.
$all_tags = "";
if (module::get_var("tagsinalbum", "max_display_tags") > 0) {
$all_tags = $tags_model->find_all(module::get_var("tagsinalbum", "max_display_tags"));
} else {
$all_tags = $tags_model->find_all();
}
// If this album has children that are tagged, display those tags.
if (count($all_tags) > 0) {
$block = new Block();
$block->css_id = "g-tags-in-album-block";

View File

@ -1,33 +1,11 @@
<?php defined("SYSPATH") or die("No direct script access.") ?>
<?
// Create an array to store the tag names and urls in.
$display_tags = array();
// Loop through all tags in the album, copying their
// names and urls into the array and skipping duplicates.
$last_tagid = "";
foreach ($all_tags as $one_tag) {
if ($last_tagid != $one_tag->id) {
$tag = ORM::factory("tag", $one_tag->id);
$display_tags[] = array(html::clean($tag->name), $tag->url());
$last_tagid = $one_tag->id;
}
if (module::get_var("tagsinalbum", "max_display_tags") > 0) {
if (count($display_tags) == module::get_var("tagsinalbum", "max_display_tags")) {
break;
}
}
}
// Sort the array.
asort($display_tags);
// Print out the list of tags as clickable links.
// Loop through each tag in $all_tags, and display it as a link.
$not_first = 0;
foreach ($display_tags as $one_tag) {
foreach ($all_tags as $one_tag) {
if ($not_first++ > 0) {
print ", ";
}
print "<a href=\"" . $one_tag[1] . "\">" . $one_tag[0] . "</a>";
print "<a href=\"" . $one_tag->url() . "\">" . html::clean($one_tag->name) . "</a>";
}
?>

View File

@ -29,12 +29,26 @@ class tagsinalbum_block_Core {
case "tagsinalbum":
if (($theme->item) && ($theme->item->is_album())) {
$item = $theme->item;
$all_tags = ORM::factory("tag")
// Create an ORM query for finding one instance of each tag
// used by children in the current album.
$tags_model = ORM::factory("tag")
->join("items_tags", "items_tags.tag_id", "tags.id")
->join("items", "items.id", "items_tags.item_id", "LEFT")
->where("items.parent_id", "=", $item->id)
->order_by("tags.id", "ASC")
->find_all();
->order_by("tags.name", "ASC")
->group_by("tags.id");
// Limit $all_tags to the first X tags if max_display_tags is set,
// else populate it with all tags used by this album's children.
$all_tags = "";
if (module::get_var("tagsinalbum", "max_display_tags") > 0) {
$all_tags = $tags_model->find_all(module::get_var("tagsinalbum", "max_display_tags"));
} else {
$all_tags = $tags_model->find_all();
}
// If this album has children that are tagged, display those tags.
if (count($all_tags) > 0) {
$block = new Block();
$block->css_id = "g-tags-in-album-block";

View File

@ -1,33 +1,11 @@
<?php defined("SYSPATH") or die("No direct script access.") ?>
<?
// Create an array to store the tag names and urls in.
$display_tags = array();
// Loop through all tags in the album, copying their
// names and urls into the array and skipping duplicates.
$last_tagid = "";
foreach ($all_tags as $one_tag) {
if ($last_tagid != $one_tag->id) {
$tag = ORM::factory("tag", $one_tag->id);
$display_tags[] = array(html::clean($tag->name), $tag->url());
$last_tagid = $one_tag->id;
}
if (module::get_var("tagsinalbum", "max_display_tags") > 0) {
if (count($display_tags) == module::get_var("tagsinalbum", "max_display_tags")) {
break;
}
}
}
// Sort the array.
asort($display_tags);
// Print out the list of tags as clickable links.
// Loop through each tag in $all_tags, and display it as a link.
$not_first = 0;
foreach ($display_tags as $one_tag) {
foreach ($all_tags as $one_tag) {
if ($not_first++ > 0) {
print ", ";
}
print "<a href=\"" . $one_tag[1] . "\">" . $one_tag[0] . "</a>";
print "<a href=\"" . $one_tag->url() . "\">" . html::clean($one_tag->name) . "</a>";
}
?>