diff options
-rw-r--r-- | core/controllers/welcome.php | 9 | ||||
-rw-r--r-- | modules/tag/helpers/tag.php | 63 | ||||
-rw-r--r-- | modules/tag/helpers/tag_installer.php | 1 | ||||
-rw-r--r-- | modules/tag/tests/Tag_Test.php | 22 |
4 files changed, 87 insertions, 8 deletions
diff --git a/core/controllers/welcome.php b/core/controllers/welcome.php index a3e46214..9f42c1c4 100644 --- a/core/controllers/welcome.php +++ b/core/controllers/welcome.php @@ -245,14 +245,7 @@ class Welcome_Controller extends Template_Controller { $tag_name = $tags[array_rand($tags)]; $item = $items[array_rand($items)]; - $tag = ORM::factory("tag")->where("name", $tag_name)->find(); - if (!$tag->loaded) { - $tag->name = $tag_name; - $tag->save(); - } - if (!$tag->add($item, $tag)) { - throw new Exception("@todo {$tag->name} WAS_NOT_ADDED_TO {$item->id}"); - } + tag::add_tag($item, $tag_name); } } 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 |