summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2008-11-26 10:27:13 +0000
committerBharat Mediratta <bharat@menalto.com>2008-11-26 10:27:13 +0000
commit98ee16ca49c70d12225677999b7534744d13f168 (patch)
tree644a2449f249586382b20449fecac5389c71e860
parente1f39ac13f587c6c6fbf5c5ab522b8422f992366 (diff)
Simplify tags code some more:
1) change buckets to be 1-7 not 0-6.. zero-based indicies don't make sense in the real world and are liable to confuse themers. 2) Change tag API to popular_tags($count) which just returns the popular tags. Let the block code massage it into the right format for the view. 3) Move alphasort into the block code, simplify it and rename it to sort_by_name so that it's more descriptive 4) Do the bucketing in the view; this allows the themer to override it and create their own bucketing algorithm to go with the theme's CSS. 5) Don't create any temporary objects.
-rw-r--r--modules/tag/helpers/tag.php34
-rw-r--r--modules/tag/helpers/tag_block.php12
-rw-r--r--modules/tag/views/tag_block.html.php19
-rw-r--r--themes/default/css/screen.css15
4 files changed, 33 insertions, 47 deletions
diff --git a/modules/tag/helpers/tag.php b/modules/tag/helpers/tag.php
index 8057379b..0ca86dc8 100644
--- a/modules/tag/helpers/tag.php
+++ b/modules/tag/helpers/tag.php
@@ -18,8 +18,6 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class tag_Core {
- public static $NUMBER_OF_BUCKETS = 7;
-
/**
* Associate a tag with an item. Create the tag if it doesn't already exist.
*
@@ -51,36 +49,14 @@ class tag_Core {
}
/**
- * Assign a css class to the tags based on frequency of use. This code is based on the code
- * from: http://www.hawkee.com/snippet/1485/
+ * Return the N most popular tags.
*
- * @return array List of tags each entry has the following format:
- * array("id" => "tag_id", "name" => "tag_name", "count" => "frequency",
- * "class" => "bucket")
+ * @return ORM_Iterator of Tag_Model in descending tag count order
*/
- public static function load_buckets() {
- $tag_list = array();
- $tags = ORM::factory("tag")
+ public static function popular_tags($count) {
+ return ORM::factory("tag")
->orderby("count", "DESC")
- ->limit(30)
+ ->limit($count)
->find_all();
- if ($tags->count() > 0) {
- $max_count = $tags[0]->count;
- foreach($tags as $key => $tag) {
- // Set the tag to the current class
- $size = (int)(($tag->count / $max_count) * (self::$NUMBER_OF_BUCKETS - 1));
- $tag_list[$key] = array("id" => $tag->id, "name" => $tag->name, "count" => $tag->count,
- "class" => "$size");
- }
- usort($tag_list, array("tag", "alphasort"));
- }
- return $tag_list;
- }
-
- public static function alphasort($tag1, $tag2) {
- if ($tag1["name"] == $tag2["name"]) {
- return 0;
- }
- return $tag1["name"] < $tag2["name"] ? -1 : 1;
}
}
diff --git a/modules/tag/helpers/tag_block.php b/modules/tag/helpers/tag_block.php
index 629008b6..4b74c4d7 100644
--- a/modules/tag/helpers/tag_block.php
+++ b/modules/tag/helpers/tag_block.php
@@ -24,7 +24,17 @@ class tag_block_Core {
$block->id = "gTag";
$block->title = _("Tags");
$block->content = new View("tag_block.html");
- $block->content->tag_list = tag::load_buckets();
+
+ $tags = tag::popular_tags(30)->as_array();
+ $block->content->max_count = $tags[0]->count;
+
+ usort($tags, array("tag_block", "sort_by_name"));
+ $block->content->tags = $tags;
+
return $block;
}
+
+ public static function sort_by_name($tag1, $tag2) {
+ return strcmp($tag1->name, $tag2->name);
+ }
} \ No newline at end of file
diff --git a/modules/tag/views/tag_block.html.php b/modules/tag/views/tag_block.html.php
index a949182b..3e45bdba 100644
--- a/modules/tag/views/tag_block.html.php
+++ b/modules/tag/views/tag_block.html.php
@@ -1,17 +1,18 @@
<? defined("SYSPATH") or die("No direct script access."); ?>
<ul>
- <? foreach ($tag_list as $tag): ?>
- <li class="size<?=$tag["class"] ?>">
- <span><?= $tag["count"] ?> photos are tagged with </span>
- <a href="<?=url::site("/tag/{$tag["id"]}") ?>"><?=$tag["name"] ?></a>
- </li>
- <? endforeach; ?>
+ <? foreach ($tags as $tag): ?>
+ <li class="size<?=(int)(($tag->count / $max_count) * 7) ?>">
+ <span><?= $tag->count ?> photos are tagged with </span>
+ <a href="<?=url::site("tag/$tag->id") ?>"><?= $tag->name ?></a>
+ </li>
+ <? endforeach ?>
</ul>
<form id="gAddTag">
- <ul class="gInline">
+ <ul>
<li><input type="text" class="text" value="add new tags..." id="gNewTags" /></li>
- <li class="gNoLabels"><input type="submit" value="add" /></li>
+ <li><input type="submit" value="add" /></li>
</ul>
- <label for="gNewTags" class="gUnderState">(separated by commas)</label>
+ <label for="gNewTags" class="gUnderState"><?= _("(separated by commas)") ?></label>
</form>
+
diff --git a/themes/default/css/screen.css b/themes/default/css/screen.css
index 52ad3fd9..492e0929 100644
--- a/themes/default/css/screen.css
+++ b/themes/default/css/screen.css
@@ -357,43 +357,43 @@ table.gMetadata td.toggle {
display: none;
}
-#gTag ul li.size0 {
+#gTag ul li.size1 {
color: #9cf;
font-size: 80%;
font-weight: 100;
}
-#gTag ul li.size1 {
+#gTag ul li.size2 {
color: #69f;
font-size: 90%;
font-weight: 300;
}
-#gTag ul li.size2 {
+#gTag ul li.size3 {
color: #69c;
font-size: 100%;
font-weight: 500;
}
-#gTag ul li.size3 {
+#gTag ul li.size4 {
color: #369;
font-size: 110%;
font-weight: 700;
}
-#gTag ul li.size4 {
+#gTag ul li.size5 {
color: #0e2b52;
font-size: 120%;
font-weight: 900;
}
-#gTag ul li.size5 {
+#gTag ul li.size6 {
color: #0e2b52;
font-size: 130%;
font-weight: 900;
}
-#gTag ul li.size6 {
+#gTag ul li.size7 {
color: #0e2b52;
font-size: 140%;
font-weight: 900;
@@ -406,7 +406,6 @@ table.gMetadata td.toggle {
#gTag form {
margin-top: 10px;
-}
/* Tags ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */