From 4ac3be445ebb041a503bbd2999f5d08dbc9a8578 Mon Sep 17 00:00:00 2001 From: Nathan Kinkade Date: Sat, 23 Jul 2011 17:00:38 +0000 Subject: Added a new module for annotating photos. --- .../photoannotation/helpers/photoannotation.php | 275 +++++++++++++++++++++ .../helpers/photoannotation_block.php | 40 +++ .../helpers/photoannotation_event.php | 248 +++++++++++++++++++ .../helpers/photoannotation_installer.php | 119 +++++++++ .../helpers/photoannotation_theme.php | 81 ++++++ 5 files changed, 763 insertions(+) create mode 100644 modules/photoannotation/helpers/photoannotation.php create mode 100644 modules/photoannotation/helpers/photoannotation_block.php create mode 100644 modules/photoannotation/helpers/photoannotation_event.php create mode 100644 modules/photoannotation/helpers/photoannotation_installer.php create mode 100644 modules/photoannotation/helpers/photoannotation_theme.php (limited to 'modules/photoannotation/helpers') diff --git a/modules/photoannotation/helpers/photoannotation.php b/modules/photoannotation/helpers/photoannotation.php new file mode 100644 index 00000000..749f7778 --- /dev/null +++ b/modules/photoannotation/helpers/photoannotation.php @@ -0,0 +1,275 @@ +escape($q) ."*"; + if ($q == "*") { + $users = ORM::factory("user"); + $count = $users->count_all(); + $data = $users->order_by("name", "ASC")->find_all($page_size, $offset); + return array($count, $data); + } else { + $query = + "SELECT SQL_CALC_FOUND_ROWS {users}.*, " . + " MATCH({users}.`name`) AGAINST ('$q' IN BOOLEAN MODE) AS `score` " . + "FROM {users} " . + "WHERE MATCH({users}.`name`, {users}.`full_name`) AGAINST ('$q' IN BOOLEAN MODE) " . + "ORDER BY `score` DESC " . + "LIMIT $page_size OFFSET $offset"; + $data = $db->query($query); + $count = $db->query("SELECT FOUND_ROWS() as c")->current()->c; + return array($count, new ORM_Iterator(ORM::factory("user"), $data)); + } + } + + static function get_user_search_form($form_id) { + $form = new Forge("photoannotation/showuser", "", "post", array("id" => $form_id, "class" => "g-short-form")); + $label = t("Search for a person"); + + $group = $form->group("showuser")->label("Search for a person"); + $group->input("name")->label($label)->id("name"); + $group->submit("")->value(t("Search")); + return $form; + } + + public static function getuser($user_string) { + $user_parts = explode("(", $user_string); + $user_part = rtrim(ltrim(end($user_parts)), ")"); + $user = ORM::factory("user")->where("name", "=", $user_part)->find(); + $user_firstpart = trim(implode(array_slice($user_parts, 0, count($user_parts)-1))); + if (!$user->loaded() || strcasecmp($user_firstpart, $user->display_name()) <> 0) { + $result->found = false; + $result->isguest = false; + $result->user = ""; + return $result; + } + if (identity::guest()->id == $user->id) { + $result->found = true; + $result->isguest = true; + $result->user = ""; + return $result; + } + $result->found = true; + $result->isguest = false; + $result->user = $user; + return $result; + } + + public static function saveuser($user_id, $item_id, $str_x1, $str_y1, $str_x2, $str_y2, $description) { + //Since we are associating a user we will remove any old annotation of this user on this photo + $item_old_users = ORM::factory("items_user") + ->where("user_id", "=", $user_id) + ->where("item_id", "=", $item_id) + ->find_all(); + if (count($item_old_users) > 1) { + foreach ($item_old_users as $item_old_user) { + $item_old_user->delete(); + } + $item_user = ORM::factory("items_user"); + } elseif (count($item_old_users) == 1) { + $item_user = ORM::factory("items_user", $item_old_users[0]->id); + } else { + $item_user = ORM::factory("items_user"); + photoannotation::send_notifications($user_id, $item_id, "newtag"); + } + $item_user->user_id = $user_id; + $item_user->item_id = $item_id; + $item_user->x1 = $str_x1; + $item_user->y1 = $str_y1; + $item_user->x2 = $str_x2; + $item_user->y2 = $str_y2; + $item_user->description = $description; + $item_user->save(); + } + + public static function saveface($tag_id, $item_id, $str_x1, $str_y1, $str_x2, $str_y2, $description, $annotate_id = "") { + if ($annotate_id == "") { + $item_face = ORM::factory("items_face"); + } else { + $item_face = ORM::factory("items_face") + ->where("id", "=", $annotate_id) + ->find(); + } + $item_face->tag_id = $tag_id; + $item_face->item_id = $item_id; + $item_face->x1 = $str_x1; + $item_face->y1 = $str_y1; + $item_face->x2 = $str_x2; + $item_face->y2 = $str_y2; + $item_face->description = $description; + $item_face->save(); + } + + public static function savenote($item_title, $item_id, $str_x1, $str_y1, $str_x2, $str_y2, $description, $annotate_id = "") { + if ($annotate_id == "") { + $item_note = ORM::factory("items_note"); + } else { + $item_note = ORM::factory("items_note") + ->where("id", "=", $annotate_id) + ->find(); + } + $item_note->item_id = $item_id; + $item_note->x1 = $str_x1; + $item_note->y1 = $str_y1; + $item_note->x2 = $str_x2; + $item_note->y2 = $str_y2; + $item_note->title = $item_title; + $item_note->description = $description; + $item_note->save(); + } + + public static function send_notifications($recipient_id, $item_id, $mailtype) { + //Load the item + $item = ORM::factory("item")->where("id", "=", $item_id)->find(); + if (!$item->loaded()) { + return false; + } + //Load the user + $recipient = ORM::factory("user")->where("id", "=", $recipient_id)->find(); + if (!$recipient->loaded()) { + return false; + } + //Only send mail if the notifications are switched on globally + if (!module::get_var("photoannotation", "nonotifications", false)) { + //Check if the use has a valid e-mail + if (!valid::email($recipient->email)) { + return false; + } + //Get the users settings + $notification_settings = self::get_user_notification_settings($recipient); + //Check which type of mail to send + switch ($mailtype) { + case "newtag": + //Only send if user has this option enabled + if ($notification_settings->newtag) { + //Get subject and body and send the mail + $subject = module::get_var("photoannotation", "newtagsubject", "Someone tagged a photo of you"); + $body = module::get_var("photoannotation", "newtagbody", "Hello %name, please visit %url to view the photo."); + $body = str_ireplace(array("%url", "%name"), array($item->abs_url(), $recipient->display_name()), $body); + return self::_send_mail($recipient->email, $subject, $body); + } + break; + case "newcomment": + //Only send if user has this option enabled + if ($notification_settings->comment) { + //Don't send if the notification module is active and the user is watching this item + if (module::is_active("notification")) { + if (notification::is_watching($item, $recipient)) { + return false; + } + } + //Get subject and body and send the mail + $subject = module::get_var("photoannotation", "newcommentsubject", "Someone added a comment to photo of you"); + $body = module::get_var("photoannotation", "newcommentbody", "Hello %name, please visit %url to read the comment."); + $body = str_ireplace(array("%url", "%name"), array($item->abs_url(), $recipient->display_name()), $body); + return self::_send_mail($recipient->email, $subject, $body); + } + break; + case "updatecomment": + //Only send if user has this option enabled + if ($notification_settings->comment) { + //Don't send if the notification module is active and the user is watching this item + if (module::is_active("notification")) { + if (notification::is_watching($item, $recipient)) { + return false; + } + } + //Get subject and body and send the mail + $subject = module::get_var("photoannotation", "updatedcommentsubject", "Someone updated a comment to photo of you"); + $body = module::get_var("photoannotation", "updatedcommentbody", "Hello %name, please visit %url to read the comment."); + $body = str_ireplace(array("%url", "%name"), array($item->abs_url(), $recipient->display_name()), $body); + return self::_send_mail($recipient->email, $subject, $body); + } + } + } + return false; + } + + private static function _send_mail($mailto, $subject, $message) { + //Send the notification mail + $message = nl2br($message); + return Sendmail::factory() + ->to($mailto) + ->subject($subject) + ->header("Mime-Version", "1.0") + ->header("Content-type", "text/html; charset=utf-8") + ->message($message) + ->send(); + } + + public static function get_user_notification_settings($user) { + //Try loading the notification settings of user + $notification_settings = ORM::factory("photoannotation_notification")->where("user_id", "=", $user->id)->find(); + if (!$notification_settings->loaded()) { + //If the user did not save his settings use the website default + $notify = module::get_var("photoannotation", "notificationoptout", false); + $notification_settings = ORM::factory("photoannotation_notification"); + $notification_settings->user_id = $user->id; + $notification_settings->newtag = $notify; + $notification_settings->comment = $notify; + $notification_settings->save(); + } + return $notification_settings; + } + + static function cloud($count) { + $users = ORM::factory("user")->order_by("name", "ASC")->find_all(); + if ($users) { + $cloud = new View("photoannotation_cloud.html"); + $fullname = module::get_var("photoannotation", "fullname", false); + foreach ($users as $user) { + $annotations = ORM::factory("items_user")->where("user_id", "=", $user->id)->count_all(); + if ($annotations > 0) { + if ($annotations > $maxcount) { + $maxcount = $annotations; + } + if ($fullname) { + $user_array[$user->name]->name = $user->display_name(); + } else { + $user_array[$user->name]->name = $user->name; + } + $user_array[$user->name]->size = $annotations; + $user_array[$user->name]->url = user_profile::url($user->id); + } + } + if (isset($user_array)) { + $cloud->users = array_slice($user_array, 0, $count); + $cloud->max_count = $maxcount; + return $cloud; + } else { + return ""; + } + } + } + + static function comment_count($user_id) { + if (module::is_active("comment")) { + return ORM::factory("comment")->where("author_id", "=", $user_id)->count_all(); + } else { + return false; + } + } + + static function annotation_count($user_id) { + return ORM::factory("items_user")->where("user_id", "=", $user_id)->count_all(); + } +} diff --git a/modules/photoannotation/helpers/photoannotation_block.php b/modules/photoannotation/helpers/photoannotation_block.php new file mode 100644 index 00000000..d0e89e0a --- /dev/null +++ b/modules/photoannotation/helpers/photoannotation_block.php @@ -0,0 +1,40 @@ + t("Users")); + } + + static function get($block_id, $theme) { + $block = ""; + if (!identity::active_user()->guest || module::get_var("photoannotation", "allowguestsearch", false)) { + switch ($block_id) { + case "photoannotation": + $block = new Block(); + $block->css_id = "g-photoannotation"; + $block->title = t("People"); + $block->content = new View("photoannotation_block.html"); + $block->content->cloud = photoannotation::cloud(30); + $block->content->form = photoannotation::get_user_search_form("g-user-cloud-form"); + } + } + return $block; + } +} diff --git a/modules/photoannotation/helpers/photoannotation_event.php b/modules/photoannotation/helpers/photoannotation_event.php new file mode 100644 index 00000000..a7836052 --- /dev/null +++ b/modules/photoannotation/helpers/photoannotation_event.php @@ -0,0 +1,248 @@ +deactivate)) { + site_status::warning( + t("The Photo Annotation module requires the Tags module. " . + "Activate the Tags module now", + array("url" => url::site("admin/modules"))), + "photoannotation_needs_tag"); + } else { + site_status::clear("photoannotation_needs_tag"); + } + if (module::is_active("tagfaces") || in_array("tagfaces", $changes->activate)) { + site_status::warning( + t("The Photo Annotation module cannot be used together with the TagFaces module. " . + "Dectivate the TagFaces module now", + array("url" => url::site("admin/modules"))), + "photoannotation_incompatibility_tagfaces"); + } else { + site_status::clear("photoannotation_incompatibility_tagfaces"); + } + } + + static function site_menu($menu, $theme) { + // Create a menu option for adding face data. + if (!$theme->item()) { + return; + } + $item = $theme->item(); + if ($item->is_photo()) { + if ((access::can("view", $item)) && (access::can("edit", $item))) { + $menu->get("options_menu") + ->append(Menu::factory("link") + ->id("photoannotation") + ->label(t("Add annotation")) + ->css_id("g-photoannotation-link") + ->url("#")); + } + } + } + + static function item_deleted($item) { + // Check for and delete existing Faces and Notes. + $existingFaces = ORM::factory("items_face") + ->where("item_id", "=", $item->id) + ->find_all(); + if (count($existingFaces) > 0) { + db::build()->delete("items_faces")->where("item_id", "=", $item->id)->execute(); + } + + $existingNotes = ORM::factory("items_note") + ->where("item_id", "=", $item->id) + ->find_all(); + if (count($existingNotes) > 0) { + db::build()->delete("items_notes")->where("item_id", "=", $item->id)->execute(); + } + + $existingUsers = ORM::factory("items_user") + ->where("item_id", "=", $item->id) + ->find_all(); + if (count($existingUsers) > 0) { + db::build()->delete("items_users")->where("item_id", "=", $item->id)->execute(); + } + } + + static function user_deleted($old) { + // Check for and delete existing Annotations linked to that user. + $existingFaces = ORM::factory("items_user") + ->where("user_id", "=", $old->id) + ->find_all(); + if (count($existingFaces) > 0) { + $onuserdelete = module::get_var("photoannotation", "onuserdelete", "0"); + if (module::get_var("photoannotation", "fullname", false)) { + $new_tag_name = $old->display_name(); + } else { + $new_tag_name = $old->name; + } + switch ($onuserdelete) { + case "1": + //convert to tag + $tag = ORM::factory("tag")->where("name", "=", $new_tag_name)->find(); + if (!$tag->loaded()) { + $tag->name = $new_tag_name; + $tag->count = 0; + } + foreach ($existingFaces as $existingFace) { + $item = ORM::factory("item")->where("id", "=", $existingFace->item_id)->find(); + $tag->add($item); + $tag->count++; + $tag->save(); + photoannotation::saveface($tag->id, $existingFace->item_id, $existingFace->x1, $existingFace->y1, $existingFace->x2, $existingFace->y2, $existingFace->description); + } + break; + case "2": + //convert to note + foreach ($existingFaces as $existingFace) { + photoannotation::savenote($new_tag_name, $existingFace->item_id, $existingFace->x1, $existingFace->y1, $existingFace->x2, $existingFace->y2, $existingFace->description); + } + } + db::build()->delete("items_users")->where("user_id", "=", $old->id)->execute(); + } + // Delete notification settings + $notification_settings = ORM::factory("photoannotation_notification") + ->where("user_id", "=", $old->id) + ->find(); + if ($notification_settings->loaded()) { + $notification_settings->delete(); + } + } + + static function user_created($user) { + // Write notification settings + $notify = module::get_var("photoannotation", "notificationoptout", false); + $notification_settings = ORM::factory("photoannotation_notification"); + $notification_settings->user_id = $user->id; + $notification_settings->newtag = $notify; + $notification_settings->comment = $notify; + $notification_settings->save(); + } + + static function admin_menu($menu, $theme) { + $menu->get("settings_menu") + ->append(Menu::factory("link") + ->id("photoannotation_menu") + ->label(t("Photo Annotation")) + ->url(url::site("admin/photoannotation"))); + } + + static function show_user_profile($data) { + $view = new Theme_View("dynamic.html", "collection", "userprofiles"); + //load thumbs + $item_users = ORM::factory("items_user")->where("user_id", "=", $data->user->id)->find_all(); + foreach ($item_users as $item_user) { + $item_thumb = ORM::factory("item") + ->viewable() + ->where("type", "!=", "album") + ->where("id", "=", $item_user->item_id) + ->find(); + if ($item_thumb->loaded()) { + $item_thumbs[] = $item_thumb; + } + } + $children_count = count($item_thumbs); + $page_size = module::get_var("gallery", "page_size", 9); + $page = (int) Input::instance()->get("page", "1"); + $offset = ($page-1) * $page_size; + $max_pages = max(ceil($children_count / $page_size), 1); + + // Make sure that the page references a valid offset + if ($page < 1) { + url::redirect($album->abs_url()); + } else if ($page > $max_pages) { + url::redirect($album->abs_url("page=$max_pages")); + } + if ($page < $max_pages) { + $next_page_url = url::site("user_profile/show/". $data->user->id ."?page=". ($page + 1)); + $view->set_global("next_page_url", $next_page_url); + $view->set_global("first_page_url", url::site("user_profile/show/". $data->user->id ."?page=". $max_pages)); + } + + if ($page > 1) { + $view->set_global("previous_page_url", url::site("user_profile/show/". $data->user->id ."?page=". ($page - 1))); + $view->set_global("first_page_url", url::site("user_profile/show/". $data->user->id ."?page=1")); + } + $view->set_global("page", $page); + $view->set_global("max_pages", $max_pages); + $view->set_global("page_size", $page_size); + $view->set_global("children", array_slice($item_thumbs, $offset, $page_size));; + $view->set_global("children_count", $children_count); + $view->set_global("total", $max_pages); + $view->set_global("position", t("Page") ." ". $page); + if ($children_count > 0) { + $data->content[] = (object)array("title" => t("Photos"), "view" => $view); + } + } + + static function user_edit_form($user, $form) { + // Allow users to modify notification settings. + if (!module::get_var("photoannotation", "nonotifications", false)) { + $notification_settings = photoannotation::get_user_notification_settings($user); + $user_notification = $form->edit_user->group("edit_notification")->label("Tag notifications"); + $user_notification->checkbox("photoannotation_newtag")->label(t("Notify me when a tag is added to a photo with me")) + ->checked($notification_settings->newtag); + $user_notification->checkbox("photoannotation_comment")->label(t("Notify me if someone comments on a photo with me on it")) + ->checked($notification_settings->comment); + } + } + + static function user_edit_form_completed($user, $form) { + // Save notification settings. + if (!module::get_var("photoannotation", "nonotifications", false)) { + $notification_settings = ORM::factory("photoannotation_notification")->where("user_id", "=", $user->id)->find(); + if (!$notification_settings->loaded()) { + $notification_settings = ORM::factory("photoannotation_notification"); + $notification_settings->user_id = $user->id; + } + $notification_settings->newtag = $form->edit_user->edit_notification->photoannotation_newtag->value; + $notification_settings->comment = $form->edit_user->edit_notification->photoannotation_comment->value; + $notification_settings->save(); + } + } + + static function comment_created($comment) { + //Check if there are any user annotations on the photo and send notification if applicable + $item_users = ORM::factory("items_user")->where("item_id", "=", $comment->item_id)->find_all(); + if (count($item_users) > 0) { + foreach ($item_users as $item_user) { + //Don't send if the commenter is the user to be notified + if ($comment->author_id != $item_user->user_id && module::is_active("notification")) { + photoannotation::send_notifications($item_user->user_id, $comment->item_id, "newcomment"); + } + } + } + } + + static function comment_updated($comment) { + //Check if there are any user annotations on the photo and send notification if applicable + $item_users = ORM::factory("items_user")->where("item_id", "=", $comment->item_id)->find_all(); + if (count($item_users) > 0) { + foreach ($item_users as $item_user) { + //Don't send if the commenter is the user to be notified + if ($comment->author_id != $item_user->user_id && module::is_active("notification")) { + photoannotation::send_notifications($item_user->user_id, $comment->item_id, "updatedcomment"); + } + } + } + } +} diff --git a/modules/photoannotation/helpers/photoannotation_installer.php b/modules/photoannotation/helpers/photoannotation_installer.php new file mode 100644 index 00000000..a0a881a9 --- /dev/null +++ b/modules/photoannotation/helpers/photoannotation_installer.php @@ -0,0 +1,119 @@ +query("CREATE TABLE IF NOT EXISTS {items_faces} ( + `id` int(9) NOT NULL auto_increment, + `tag_id` int(9) NOT NULL, + `item_id` int(9) NOT NULL, + `x1` int(9) NOT NULL, + `y1` int(9) NOT NULL, + `x2` int(9) NOT NULL, + `y2` int(9) NOT NULL, + `description` varchar(2048) default NULL, + PRIMARY KEY (`id`)) + DEFAULT CHARSET=utf8;"); + + $db->query("CREATE TABLE IF NOT EXISTS {items_notes} ( + `id` int(9) NOT NULL auto_increment, + `item_id` int(9) NOT NULL, + `x1` int(9) NOT NULL, + `y1` int(9) NOT NULL, + `x2` int(9) NOT NULL, + `y2` int(9) NOT NULL, + `title` varchar(64) NOT NULL, + `description` varchar(2048) default NULL, + PRIMARY KEY (`id`)) + DEFAULT CHARSET=utf8;"); + + $db->query("CREATE TABLE IF NOT EXISTS {items_users} ( + `id` int(9) NOT NULL auto_increment, + `user_id` int(9) NOT NULL, + `item_id` int(9) NOT NULL, + `x1` int(9) NOT NULL, + `y1` int(9) NOT NULL, + `x2` int(9) NOT NULL, + `y2` int(9) NOT NULL, + `description` varchar(2048) default NULL, + PRIMARY KEY (`id`)) + DEFAULT CHARSET=utf8;"); + + $db->query("CREATE TABLE IF NOT EXISTS {photoannotation_notifications} ( + `id` int(9) NOT NULL auto_increment, + `user_id` int(9) NOT NULL unique, + `newtag` int(2) default NULL, + `comment` int(2) default NULL, + PRIMARY KEY (`id`)) + DEFAULT CHARSET=utf8;"); + + // Set the module's version number. + module::set_version("photoannotation", 4); + } + + static function upgrade($version) { + if ($version == 1) { + module::set_version("photoannotation", $version = 2); + } + if ($version == 2) { + $db = Database::instance(); + $db->query("CREATE TABLE IF NOT EXISTS {items_users} ( + `id` int(9) NOT NULL auto_increment, + `user_id` int(9) NOT NULL, + `item_id` int(9) NOT NULL, + `x1` int(9) NOT NULL, + `y1` int(9) NOT NULL, + `x2` int(9) NOT NULL, + `y2` int(9) NOT NULL, + `description` varchar(2048) default NULL, + PRIMARY KEY (`id`)) + DEFAULT CHARSET=utf8;"); + module::set_version("photoannotation", $version = 3); + } + if ($version == 3) { + $db = Database::instance(); + $db->query("CREATE TABLE IF NOT EXISTS {photoannotation_notifications} ( + `id` int(9) NOT NULL auto_increment, + `user_id` int(9) NOT NULL unique, + `newtag` int(2) default NULL, + `comment` int(2) default NULL, + PRIMARY KEY (`id`)) + DEFAULT CHARSET=utf8;"); + module::set_version("photoannotation", $version = 4); + } + } + + static function deactivate() { + // Clear the require tags message when photoannotation is deactivated. + site_status::clear("photoannotation_needs_tag"); + site_status::clear("photoannotation_incompatibility_tagfaces"); + } + + static function uninstall() { + // Delete the face table before uninstalling. + $db = Database::instance(); + $db->query("DROP TABLE IF EXISTS {items_faces};"); + $db->query("DROP TABLE IF EXISTS {items_notes};"); + $db->query("DROP TABLE IF EXISTS {items_users};"); + $db->query("DROP TABLE IF EXISTS {photoannotation_notifications};"); + module::delete("photoannotation"); + } +} diff --git a/modules/photoannotation/helpers/photoannotation_theme.php b/modules/photoannotation/helpers/photoannotation_theme.php new file mode 100644 index 00000000..15f584f8 --- /dev/null +++ b/modules/photoannotation/helpers/photoannotation_theme.php @@ -0,0 +1,81 @@ +css("photoannotation.css"); + if ($theme->page_subtype == "photo") { + $theme->script("jquery.annotate.min.js"); + $noborder = module::get_var("photoannotation", "noborder", false); + $noclickablehover = module::get_var("photoannotation", "noclickablehover", false); + $nohover = module::get_var("photoannotation", "nohover", false); + $bordercolor = "#". module::get_var("photoannotation", "bordercolor", "000000"); + $v = "\n"; + return $v; + } + } + + static function resize_bottom($theme) { + if ($theme->page_subtype == "photo") { + return new View("photoannotation_highlight_block.html"); + } + } + + static function admin_head($theme) { + if (strpos($theme->content->kohana_filename, "admin_photoannotation.html.php")) { + $theme->css("colorpicker.css"); + $theme->script("jquery.colorpicker.min.js"); + } + } + +} -- cgit v1.2.3