diff options
author | hiwilson <wilsonpscheung@gmail.com> | 2009-07-19 17:02:20 +0800 |
---|---|---|
committer | Bharat Mediratta <bharat@menalto.com> | 2009-07-20 08:02:07 -0700 |
commit | 709d6c5faf7ece54046c0e2bc431a559a6b9d735 (patch) | |
tree | 406c770bd7d549e51076fe9f5f2813a43a8260f7 /modules | |
parent | 33d0b82d024df8fd85798cc4f6b9bab377f4f783 (diff) |
(1)Add tag edit field in album/photo edit form. (2)provide edit functionality. (3)support multi-word tagging.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/gallery/controllers/albums.php | 2 | ||||
-rw-r--r-- | modules/gallery/controllers/movies.php | 2 | ||||
-rw-r--r-- | modules/gallery/controllers/photos.php | 2 | ||||
-rw-r--r-- | modules/gallery/helpers/album.php | 3 | ||||
-rw-r--r-- | modules/gallery/helpers/photo.php | 2 | ||||
-rw-r--r-- | modules/tag/helpers/tag.php | 67 | ||||
-rw-r--r-- | modules/tag/helpers/tag_event.php | 18 |
7 files changed, 96 insertions, 0 deletions
diff --git a/modules/gallery/controllers/albums.php b/modules/gallery/controllers/albums.php index 9980b676..0e1c27e5 100644 --- a/modules/gallery/controllers/albums.php +++ b/modules/gallery/controllers/albums.php @@ -191,6 +191,8 @@ class Albums_Controller extends Items_Controller { } $album->save(); + module::event("album_edit_form_completed", $album, $form); + log::success("content", "Updated album", "<a href=\"albums/$album->id\">view</a>"); message::success( t("Saved album %album_title", array("album_title" => p::clean($album->title)))); diff --git a/modules/gallery/controllers/movies.php b/modules/gallery/controllers/movies.php index d954ad8d..110ea620 100644 --- a/modules/gallery/controllers/movies.php +++ b/modules/gallery/controllers/movies.php @@ -90,6 +90,8 @@ class Movies_Controller extends Items_Controller { $photo->rename($form->edit_photo->filename->value); $photo->save(); + module::event("photo_edit_form_completed", $photo, $form); + log::success("content", "Updated photo", "<a href=\"photos/$photo->id\">view</a>"); message::success( t("Saved photo %photo_title", array("photo_title" => p::clean($photo->title)))); diff --git a/modules/gallery/controllers/photos.php b/modules/gallery/controllers/photos.php index 9ce6ed23..5d37636d 100644 --- a/modules/gallery/controllers/photos.php +++ b/modules/gallery/controllers/photos.php @@ -83,6 +83,8 @@ class Photos_Controller extends Items_Controller { $photo->rename($form->edit_photo->filename->value); $photo->save(); + module::event("photo_edit_form_completed", $photo, $form); + log::success("content", "Updated photo", "<a href=\"photos/$photo->id\">view</a>"); message::success( t("Saved photo %photo_title", array("photo_title" => p::clean($photo->title)))); diff --git a/modules/gallery/helpers/album.php b/modules/gallery/helpers/album.php index f1a6c060..63182c36 100644 --- a/modules/gallery/helpers/album.php +++ b/modules/gallery/helpers/album.php @@ -126,6 +126,9 @@ class album_Core { ->options(array("ASC" => t("Ascending"), "DESC" => t("Descending"))) ->selected($parent->sort_order); + + module::event("album_edit_form", $parent, $form); + $group->hidden("type")->value("album"); $group->submit("")->value(t("Modify")); $form->add_rules_from(ORM::factory("item")); diff --git a/modules/gallery/helpers/photo.php b/modules/gallery/helpers/photo.php index ce964c14..bf38e1ee 100644 --- a/modules/gallery/helpers/photo.php +++ b/modules/gallery/helpers/photo.php @@ -147,6 +147,8 @@ class photo_Core { ->callback("item::validate_no_trailing_period") ->error_messages("no_trailing_period", t("The photo name can't end in \".\"")); + module::event("photo_edit_form", $photo, $form); + $group->submit("")->value(t("Modify")); $form->add_rules_from(ORM::factory("item")); return $form; diff --git a/modules/tag/helpers/tag.php b/modules/tag/helpers/tag.php index ab5ee303..ba8a438e 100644 --- a/modules/tag/helpers/tag.php +++ b/modules/tag/helpers/tag.php @@ -51,6 +51,59 @@ class tag_Core { } /** + * Modify the tags associate with an item. + * + * @param Item_Model $item an item + * @param string $new_tags_string a string of new tags name seperated by ; + * @return null + * @throws Exception("@todo {$tag_name} WAS_NOT_ADDED_TO {$item->id}") + * @throws Exception("@todo {$tag_name} WAS_NOT_DELETED_TO {$item->id}") + */ + static function update($item, $new_tags_string) { + $old_tags = self::get_tags($item); + + $new_tags = preg_split("/[,;]/", $new_tags_string); + foreach ($new_tags as $i => $new_tag) { + $new_tags[$i] = trim($new_tag); + } + + $add_tags = array_diff($new_tags, $old_tags); + foreach ($add_tags as $tag_name) { + if (empty($tag_name)) continue; + $tag = ORM::factory("tag")->where("name", $tag_name)->find(); + if (!$tag->loaded) { + $tag->name = $tag_name; + $tag->count = 0; + $tag->save(); + } + if (!$tag->has($item)) { + if (!$tag->add($item, $tag)) { + throw new Exception("@todo {$tag->name} WAS_NOT_ADDED_TO {$item->id}"); + } + $tag->count++; + $tag->save(); + } + } + + $del_tags = array_diff($old_tags, $new_tags); + foreach ($del_tags as $tag_name) { + $tag = ORM::factory("tag")->where("name", $tag_name)->find(); + if ($tag->has($item)) { + if (!$tag->remove($item, $tag)) { + throw new Exception("@todo {$tag->name} WAS_NOT_DELETED_TO {$item->id}"); + } + $tag->save(); + $tag->count--; + if ($tag->count <= 0) { + $tag->delete(); + } else { + $tag->save(); + } + } + } + } + + /** * Return the N most popular tags. * * @return ORM_Iterator of Tag_Model in descending tag count order @@ -127,4 +180,18 @@ class tag_Core { $group->submit("")->value(t("Delete Tag")); return $form; } + + static function get_tags($item) { + $records = ORM::factory("item") + ->select("tags.name as tag_name") + ->join("items_tags", "items.id", "items_tags.item_id", "left") + ->join("tags", "items_tags.tag_id", "tags.id", "left") + ->where("items.id", $item->id) + ->find_all(); + $tags = array(); + foreach ($records as $record) { + $tags[] = $record->tag_name; + } + return $tags; + } }
\ No newline at end of file diff --git a/modules/tag/helpers/tag_event.php b/modules/tag/helpers/tag_event.php index 0164f556..0b9504b3 100644 --- a/modules/tag/helpers/tag_event.php +++ b/modules/tag/helpers/tag_event.php @@ -67,4 +67,22 @@ class tag_event_Core { "SELECT `tag_id` from {items_tags} WHERE `item_id` = $item->id)"); $db->delete("items_tags", array("item_id" => "$item->id")); } + + static function album_edit_form($album, $form) { + $tag_value = implode('; ', tag::get_tags($album)); + $form->edit_album->input("tags")->label(t("Tags ( seperate by , or ; )"))->value($tag_value); + } + + static function album_edit_form_completed($album, $form) { + tag::update($album, $form->edit_album->tags->value); + } + + static function photo_edit_form($photo, $form) { + $tag_value = implode('; ', tag::get_tags($photo)); + $form->edit_photo->input("tags")->label(t("Tags ( seperate by , or ; )"))->value($tag_value); + } + + static function photo_edit_form_completed($photo, $form) { + tag::update($photo, $form->edit_photo->tags->value); + } } |