summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorTim Almdal <tnalmdal@shaw.ca>2008-11-24 23:18:32 +0000
committerTim Almdal <tnalmdal@shaw.ca>2008-11-24 23:18:32 +0000
commitf81a9879be6fd50fd26d9dd2bc1c90e87763f7a8 (patch)
tree9cebf5ba21539d11dc74ff07d9299031cf64509b /modules
parent75d9a829d794c727ff526deb07ad272faac4380d (diff)
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__)
Diffstat (limited to 'modules')
-rw-r--r--modules/tag/helpers/tag.php63
-rw-r--r--modules/tag/helpers/tag_installer.php1
-rw-r--r--modules/tag/tests/Tag_Test.php22
3 files changed, 86 insertions, 0 deletions
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 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2008 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+class tag_Core {
+
+ /**
+ * Adds the specified tags to the item
+ *
+ * @todo Write test.
+ *
+ * @param mixed $items a single item or an array of items
+ * @param mixed $tag_names a single tag name or an array of tag names
+ * @throws Exception("@todo {tag_name} WAS_NOT_ADDED_TO {$item_id}")
+ */
+ public static function add_tag($items, $tag_names) {
+ $items = is_array($items) ? $items : array($items);
+ $tag_names = is_array($tag_names) ? $tag_names : array($tag_names);
+
+ foreach($items as $item) {
+ foreach ($tag_names as $tag_name) {
+ $tag = ORM::factory("tag")->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 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2008 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Tag_Test extends Unit_Test_Case {
+ // @todo put some tests here
+} \ No newline at end of file