From f81a9879be6fd50fd26d9dd2bc1c90e87763f7a8 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Mon, 24 Nov 2008 23:18:32 +0000 Subject: Moved the creation of tags into the tag helper library. Added a count field to the tags table. Bharat, I know you said not to worry about caching, but I want to explore what are some of the issues with keeping track of the counts as we go. (i.e. is it a pain in the a__) --- modules/tag/helpers/tag.php | 63 +++++++++++++++++++++++++++++++++++ modules/tag/helpers/tag_installer.php | 1 + modules/tag/tests/Tag_Test.php | 22 ++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 modules/tag/helpers/tag.php create mode 100644 modules/tag/tests/Tag_Test.php (limited to 'modules') diff --git a/modules/tag/helpers/tag.php b/modules/tag/helpers/tag.php new file mode 100644 index 00000000..101293e4 --- /dev/null +++ b/modules/tag/helpers/tag.php @@ -0,0 +1,63 @@ +where("name", $tag_name)->find(); + if (!$tag->loaded) { + $tag->name = $tag_name; + $tag->count = 0; + // Need to save it now to get an id assigned. + $tag->save(); + } + + $tag_has_item = false; + foreach($tag->items as $tagged_item) { + if ($tagged_item->id == $item->id) { + $tag_has_item = true; + break; + } + } + if (!$tag_has_item) { + $tag->count++; + $tag->save(); + if (!$tag->add($item, $tag)) { + throw new Exception("@todo {$tag->name} WAS_NOT_ADDED_TO {$item->id}"); + } + } + } + } + } +} diff --git a/modules/tag/helpers/tag_installer.php b/modules/tag/helpers/tag_installer.php index 6673ffcc..379749ed 100644 --- a/modules/tag/helpers/tag_installer.php +++ b/modules/tag/helpers/tag_installer.php @@ -27,6 +27,7 @@ class tag_installer { $db->query("CREATE TABLE IF NOT EXISTS `tags` ( `id` int(9) NOT NULL auto_increment, `name` varchar(255) NOT NULL, + `count` int(10) unsigned NOT NULL DEFAULT 0, PRIMARY KEY (`id`), UNIQUE KEY(`name`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;"); diff --git a/modules/tag/tests/Tag_Test.php b/modules/tag/tests/Tag_Test.php new file mode 100644 index 00000000..5286a2fa --- /dev/null +++ b/modules/tag/tests/Tag_Test.php @@ -0,0 +1,22 @@ +