diff options
author | Tim Almdal <tnalmdal@shaw.ca> | 2008-11-25 18:14:52 +0000 |
---|---|---|
committer | Tim Almdal <tnalmdal@shaw.ca> | 2008-11-25 18:14:52 +0000 |
commit | d554adc484faa8bd605d230f49b8e3db2bc595b9 (patch) | |
tree | f9bdba45b90e20ffd9bd090f72e8b4363be267a3 /modules/tag/helpers | |
parent | 53f45b3079d08813b427305ed12ac91530d7e06e (diff) |
1) Changed how the test tags are generated to be able to create a better distribution
2) Added a new helper function "load_buckets" assign the class suffix to each tag
3) Created a unit test to test the load_buckets function
Diffstat (limited to 'modules/tag/helpers')
-rw-r--r-- | modules/tag/helpers/tag.php | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/modules/tag/helpers/tag.php b/modules/tag/helpers/tag.php index 1090aa42..6dfbcf38 100644 --- a/modules/tag/helpers/tag.php +++ b/modules/tag/helpers/tag.php @@ -18,6 +18,8 @@ * 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. * @@ -47,4 +49,56 @@ class tag_Core { $tag->save(); } } + + /** + * Assign a css class to the tags based on frequency of use. Optionally, allow a filter value + * which allows for adjusting the granularity of the cloud by ignoring any frequencies below + * the specified value. This code is based on the code from: http://www.hawkee.com/snippet/1485/ + * + * @param int $filter Minimum frequency to be included in the tag cloud + * @return array List of tags each entry has the following format: + * array("name" => "tag_name", "count" => "frequency", "class" => "bucket") + */ + public static function load_buckets($filter=1) { + $tag_list = array(); + $tags = ORM::factory("tag") + ->where("count >=", $filter) + ->orderby("count", "ASC") + ->find_all() + ->as_array(); + if (count($tags) > 0) { + $min_tags = count($tags) / self::$NUMBER_OF_BUCKETS; + $bucket_count = 0; + $bucket_items = 0; + $tags_set = 0; + + foreach($tags as $key => $tag) { + if (($bucket_items >= $min_tags) && $last_count != $tag->count && + $bucket_count < self::$NUMBER_OF_BUCKETS) { + $bucket_count++; + $bucket_items = 0; + + // Calculate a new minimum number if tags for the remaining classes. + $remaining_tags = count($tags) - $tags_set; + $min_tags = $remaining_tags / self::$NUMBER_OF_BUCKETS; + } + + // Set the tag to the current class + $tag_list[$key] = array("name" => $tag->name, "count" => $tag->count, + "class" => "$bucket_count"); + $bucket_items++; + $tags_set++; + $last_count = $tag->count; + } + usort($tag_list, array("tag", "alphasort")); + } + return $tag_list; + } + + public function alphasort($tag1, $tag2) { + if ($tag1["name"] == $tag2["name"]) { + return 0; + } + return $tag1["name"] < $tag2["name"] ? -1 : 1; + } } |