diff --git a/contrib/tag_history/main.php b/contrib/tag_history/main.php new file mode 100644 index 00000000..6ecd9adb --- /dev/null +++ b/contrib/tag_history/main.php @@ -0,0 +1,219 @@ +get_int("ext_tag_history_version") < 1) { + $this->install(); + } + } + + if(is_a($event, 'PageRequestEvent') && ($event->page == "tag_history")) + { + if($event->get_arg(0) == "revert") + { + // this is a request to revert to a previous version of the tags + $this->process_revert_request($_POST['image_id'], $_POST['revert']); + } + else + { + // must be an attempt to view a tag history + $image_id = int_escape($event->get_arg(0)); + global $page; + global $config; + $page_heading = "Tag History: $image_id"; + $page->set_title("Image $image_id Tag History"); + $page->set_heading($page_heading); + + $page->add_block(new NavBlock()); + $page->add_block(new Block("Tag History", $this->build_tag_history($image_id), "main", 10)); + } + + } + if(is_a($event, 'DisplayingImageEvent')) + { + // handle displaying a link on the view page + global $page; + $page->add_block(new Block(null, $this->build_link($event->image->id), "main", 5)); + } + if(is_a($event, 'ImageDeletionEvent')) + { + // handle removing of history when an image is deleted + $this->delete_all_tag_history($event->image->id); + } + if(is_a($event, 'SetupBuildingEvent')) { + $sb = new SetupBlock("Tag History"); + $sb->add_label("Limit to "); + $sb->add_int_option("history_limit"); + $sb->add_label(" entires per image"); + $event->panel->add_block($sb); + } + if(is_a($event, 'ConfigSaveEvent')) { + $event->config->set_int_from_post("history_limit"); + } + if(is_a($event, 'TagSetEvent')) { + $this->add_tag_history($event->image_id); + } + + } + + protected function install() + { + global $database; + global $config; + $database->Execute("CREATE TABLE tag_histories + ( + id integer NOT NULL auto_increment PRIMARY KEY, + image_id integer NOT NULL, + tags text NOT NULL + )"); + $config->set_int("ext_tag_history_version", 1); + } + + private function process_revert_request($image_id, $revert_id) + { + // this function is called when a revert request is received + global $page; + // check for the nothing case + if($revert_id=="nothing") + { + // tried to set it too the same thing so ignore it (might be a bot) + // go back to the index page with you + $page->set_mode("redirect"); + $page->set_redirect(make_link("index")); + return; + } + + $revert_id = int_escape($revert_id); + $image_id = int_escape($image_id); + + // lets get this revert id assuming it exists + $result = $this->get_tag_history_from_revert($revert_id); + + if($result==null) + { + // there is no history entry with that id so either the image was deleted + // while the user was viewing the history, someone is playing with form + // variables or we have messed up in code somewhere. + die("Error: No tag history with specified id was found."); + } + + // lets get the values out of the result + $stored_result_id = $result->fields['id']; + $stored_image_id = $result->fields['image_id']; + $stored_tags = $result->fields['tags']; + + if($image_id!=$stored_image_id) + { + // wth is going on there ids should be the same otherwise we are trying + // to edit another image... banhammer this user... j/k + die("Error: Mismatch in history image ids."); + } + + // all should be ok so we can revert by firing the SetUserTags event. + send_event(new TagSetEvent($image_id, $stored_tags)); + + // all should be done now so redirect the user back to the image + $page->set_mode("redirect"); + $page->set_redirect(make_link("post/view/$image_id")); + } + + + + private function build_tag_history($image_id) + { + // this function is called when user tries to view tag history of an image + // check if the image exists + global $database; + $image = $database->get_image($image_id); + if($image==null)return "No image with the specified id currently exists"; + + // get the current images tags + $current_tags = html_escape(implode(' ', $image->get_tag_array())); + //$current_tags = $image->cached_tags; + + // get any stored tag histories + $result = $this->get_tag_history_from_id($image_id); + $html = "