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:
parent
cf75213423
commit
e77473bd51
@ -29,12 +29,26 @@ class tagsinalbum_block_Core {
|
|||||||
case "tagsinalbum":
|
case "tagsinalbum":
|
||||||
if (($theme->item) && ($theme->item->is_album())) {
|
if (($theme->item) && ($theme->item->is_album())) {
|
||||||
$item = $theme->item;
|
$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_tags", "items_tags.tag_id", "tags.id")
|
||||||
->join("items", "items.id", "items_tags.item_id", "LEFT")
|
->join("items", "items.id", "items_tags.item_id", "LEFT")
|
||||||
->where("items.parent_id", "=", $item->id)
|
->where("items.parent_id", "=", $item->id)
|
||||||
->order_by("tags.id", "ASC")
|
->order_by("tags.name", "ASC")
|
||||||
->find_all();
|
->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) {
|
if (count($all_tags) > 0) {
|
||||||
$block = new Block();
|
$block = new Block();
|
||||||
$block->css_id = "g-tags-in-album-block";
|
$block->css_id = "g-tags-in-album-block";
|
||||||
|
@ -1,33 +1,11 @@
|
|||||||
<?php defined("SYSPATH") or die("No direct script access.") ?>
|
<?php defined("SYSPATH") or die("No direct script access.") ?>
|
||||||
<?
|
<?
|
||||||
// Create an array to store the tag names and urls in.
|
// Loop through each tag in $all_tags, and display it as a link.
|
||||||
$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.
|
|
||||||
$not_first = 0;
|
$not_first = 0;
|
||||||
foreach ($display_tags as $one_tag) {
|
foreach ($all_tags as $one_tag) {
|
||||||
if ($not_first++ > 0) {
|
if ($not_first++ > 0) {
|
||||||
print ", ";
|
print ", ";
|
||||||
}
|
}
|
||||||
print "<a href=\"" . $one_tag[1] . "\">" . $one_tag[0] . "</a>";
|
print "<a href=\"" . $one_tag->url() . "\">" . html::clean($one_tag->name) . "</a>";
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
@ -29,12 +29,26 @@ class tagsinalbum_block_Core {
|
|||||||
case "tagsinalbum":
|
case "tagsinalbum":
|
||||||
if (($theme->item) && ($theme->item->is_album())) {
|
if (($theme->item) && ($theme->item->is_album())) {
|
||||||
$item = $theme->item;
|
$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_tags", "items_tags.tag_id", "tags.id")
|
||||||
->join("items", "items.id", "items_tags.item_id", "LEFT")
|
->join("items", "items.id", "items_tags.item_id", "LEFT")
|
||||||
->where("items.parent_id", "=", $item->id)
|
->where("items.parent_id", "=", $item->id)
|
||||||
->order_by("tags.id", "ASC")
|
->order_by("tags.name", "ASC")
|
||||||
->find_all();
|
->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) {
|
if (count($all_tags) > 0) {
|
||||||
$block = new Block();
|
$block = new Block();
|
||||||
$block->css_id = "g-tags-in-album-block";
|
$block->css_id = "g-tags-in-album-block";
|
||||||
|
@ -1,33 +1,11 @@
|
|||||||
<?php defined("SYSPATH") or die("No direct script access.") ?>
|
<?php defined("SYSPATH") or die("No direct script access.") ?>
|
||||||
<?
|
<?
|
||||||
// Create an array to store the tag names and urls in.
|
// Loop through each tag in $all_tags, and display it as a link.
|
||||||
$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.
|
|
||||||
$not_first = 0;
|
$not_first = 0;
|
||||||
foreach ($display_tags as $one_tag) {
|
foreach ($all_tags as $one_tag) {
|
||||||
if ($not_first++ > 0) {
|
if ($not_first++ > 0) {
|
||||||
print ", ";
|
print ", ";
|
||||||
}
|
}
|
||||||
print "<a href=\"" . $one_tag[1] . "\">" . $one_tag[0] . "</a>";
|
print "<a href=\"" . $one_tag->url() . "\">" . html::clean($one_tag->name) . "</a>";
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
Reference in New Issue
Block a user