From cc399bf4f07fd7060536f442373a1e9c0ea4a5aa Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 6 Sep 2010 16:20:37 -0700 Subject: Track the language of the user receiving the notification and send the email in that user's language. Incidentally, send one email per recipient, else we're leaking email addresses which is bad for community sites. Bump notification module to v2 in the process. Fixes ticket #1287. --- modules/notification/helpers/notification.php | 156 ++++++++++----------- .../helpers/notification_installer.php | 8 ++ 2 files changed, 80 insertions(+), 84 deletions(-) (limited to 'modules/notification/helpers') diff --git a/modules/notification/helpers/notification.php b/modules/notification/helpers/notification.php index e4212203..0564d336 100644 --- a/modules/notification/helpers/notification.php +++ b/modules/notification/helpers/notification.php @@ -67,7 +67,7 @@ class notification { } static function get_subscribers($item) { - $subscriber_ids = array(); + $subscriber_ids = array(); foreach (ORM::factory("subscription") ->select("user_id") ->join("items", "subscriptions.item_id", "items.id") @@ -86,88 +86,76 @@ class notification { $subscribers = array(); foreach ($users as $user) { if (access::user_can($user, "view", $item) && !empty($user->email)) { - $subscribers[$user->email] = 1; + $subscribers[$user->email] = $user->locale; } } - return array_keys($subscribers); + return $subscribers; } static function send_item_updated($original, $item) { - $subscribers = self::get_subscribers($item); - if (!$subscribers) { - return; + foreach (self::get_subscribers($item) as $email => $locale) { + $v = new View("item_updated.html"); + $v->original = $original; + $v->item = $item; + $v->subject = $item->is_album() ? + t("Album \"%title\" updated", array("title" => $original->title, "locale" => $locale)) : + ($item->is_photo() ? + t("Photo \"%title\" updated", array("title" => $original->title, "locale" => $locale)) + : t("Movie \"%title\" updated", array("title" => $original->title, "locale" => $locale))); + self::_notify($email, $locale, $item, $v->render(), $v->subject); } - - $v = new View("item_updated.html"); - $v->original = $original; - $v->item = $item; - $v->subject = $item->is_album() ? - t("Album \"%title\" updated", array("title" => $original->title)) : - ($item->is_photo() ? - t("Photo \"%title\" updated", array("title" => $original->title)) - : t("Movie \"%title\" updated", array("title" => $original->title))); - - self::_notify($subscribers, $item, $v->render(), $v->subject); } static function send_item_add($item) { - $subscribers = self::get_subscribers($item); - if (!$subscribers) { - return; - } - $parent = $item->parent(); - $v = new View("item_added.html"); - $v->item = $item; - $v->subject = $item->is_album() ? - t("Album \"%title\" added to \"%parent_title\"", - array("title" => $item->title, "parent_title" => $parent->title)) : - ($item->is_photo() ? - t("Photo \"%title\" added to \"%parent_title\"", - array("title" => $item->title, "parent_title" => $parent->title)) : - t("Movie \"%title\" added to \"%parent_title\"", - array("title" => $item->title, "parent_title" => $parent->title))); - - self::_notify($subscribers, $item, $v->render(), $v->subject); + foreach (self::get_subscribers($item) as $email => $locale) { + $v = new View("item_added.html"); + $v->item = $item; + $v->subject = $item->is_album() ? + t("Album \"%title\" added to \"%parent_title\"", + array("title" => $item->title, "parent_title" => $parent->title, "locale" => $locale)) : + ($item->is_photo() ? + t("Photo \"%title\" added to \"%parent_title\"", + array("title" => $item->title, "parent_title" => $parent->title, "locale" => $locale)) : + t("Movie \"%title\" added to \"%parent_title\"", + array("title" => $item->title, "parent_title" => $parent->title, "locale" => $locale))); + self::_notify($email, $locale, $item, $v->render(), $v->subject); + } } static function send_item_deleted($item) { - $subscribers = self::get_subscribers($item); - if (!$subscribers) { - return; - } - $parent = $item->parent(); - $v = new View("item_deleted.html"); - $v->item = $item; - $v->subject = $item->is_album() ? - t("Album \"%title\" removed from \"%parent_title\"", - array("title" => $item->title, "parent_title" => $parent->title)) : - ($item->is_photo() ? - t("Photo \"%title\" removed from \"%parent_title\"", - array("title" => $item->title, "parent_title" => $parent->title)) - : t("Movie \"%title\" removed from \"%parent_title\"", - array("title" => $item->title, "parent_title" => $parent->title))); - - self::_notify($subscribers, $item, $v->render(), $v->subject); + foreach (self::get_subscribers($item) as $email => $locale) { + $v = new View("item_deleted.html"); + $v->item = $item; + $v->subject = $item->is_album() ? + t("Album \"%title\" removed from \"%parent_title\"", + array("title" => $item->title, "parent_title" => $parent->title, "locale" => $locale)) : + ($item->is_photo() ? + t("Photo \"%title\" removed from \"%parent_title\"", + array("title" => $item->title, "parent_title" => $parent->title, "locale" => $locale)) + : t("Movie \"%title\" removed from \"%parent_title\"", + array("title" => $item->title, "parent_title" => $parent->title, + "locale" => $locale))); + self::_notify($email, $locale, $item, $v->render(), $v->subject); + } } static function send_comment_published($comment) { $item = $comment->item(); - $subscribers = self::get_subscribers($item); - if (!$subscribers) { - return; - } - - $v = new View("comment_published.html"); - $v->comment = $comment; - $v->subject = $item->is_album() ? - t("A new comment was published for album \"%title\"", array("title" => $item->title)) : + foreach (self::get_subscribers($item) as $email => $locale) { + $v = new View("comment_published.html"); + $v->comment = $comment; + $v->subject = $item->is_album() ? + t("A new comment was published for album \"%title\"", + array("title" => $item->title, "locale" => $locale)) : ($item->is_photo() ? - t("A new comment was published for photo \"%title\"", array("title" => $item->title)) - : t("A new comment was published for movie \"%title\"", array("title" => $item->title))); - - self::_notify($subscribers, $item, $v->render(), $v->subject); + t("A new comment was published for photo \"%title\"", + array("title" => $item->title, "locale" => $locale)) + : t("A new comment was published for movie \"%title\"", + array("title" => $item->title, "locale" => $locale))); + self::_notify($email, $locale, $item, $v->render(), $v->subject); + } } static function send_pending_notifications() { @@ -191,13 +179,16 @@ class notification { $pending->delete(); } else { $text = ""; + $locale = null; foreach ($result as $pending) { $text .= $pending->text; + $locale = $pending->locale; $pending->delete(); } Sendmail::factory() ->to($email) - ->subject(t("Multiple events have occurred")) // @todo fix this terrible subject line + ->subject(t("New activity for %site_name", + array("site_name" => item::root()->title, "locale" => $locale))) ->header("Mime-Version", "1.0") ->header("Content-Type", "text/html; charset=UTF-8") ->message($text) @@ -206,25 +197,22 @@ class notification { } } - private static function _notify($subscribers, $item, $text, $subject) { - if (!empty($subscribers)) { - if (!batch::in_progress()) { - Sendmail::factory() - ->to($subscribers) - ->subject($subject) - ->header("Mime-Version", "1.0") - ->header("Content-Type", "text/html; charset=UTF-8") - ->message($text) - ->send(); - } else { - foreach ($subscribers as $subscriber) { - $pending = ORM::factory("pending_notification"); - $pending->subject = $subject; - $pending->text = $text; - $pending->email = $subscriber; - $pending->save(); - } - } + private static function _notify($email, $locale, $item, $text, $subject) { + if (!batch::in_progress()) { + Sendmail::factory() + ->to($email) + ->subject($subject) + ->header("Mime-Version", "1.0") + ->header("Content-Type", "text/html; charset=UTF-8") + ->message($text) + ->send(); + } else { + $pending = ORM::factory("pending_notification"); + $pending->subject = $subject; + $pending->text = $text; + $pending->email = $email; + $pending->locale = $locale; + $pending->save(); } } } diff --git a/modules/notification/helpers/notification_installer.php b/modules/notification/helpers/notification_installer.php index d082d80f..78f72194 100644 --- a/modules/notification/helpers/notification_installer.php +++ b/modules/notification/helpers/notification_installer.php @@ -39,6 +39,14 @@ class notification_installer { module::set_version("notification", 1); } + static function upgrade($version) { + $db = Database::instance(); + if ($version == 1) { + $db->query("ALTER TABLE {pending_notifications} ADD COLUMN `locale` char(10) default NULL"); + module::set_version("notification", $version = 2); + } + } + static function uninstall() { $db = Database::instance(); $db->query("DROP TABLE IF EXISTS {subscriptions};"); -- cgit v1.2.3 From d7edbc211f75acf0214ee554722a4f470d92157b Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Wed, 8 Sep 2010 19:35:10 -0700 Subject: Oops, forgot to update install() with the locale change that I made in cc399bf4f07fd7060536f442373a1e9c0ea4a5aa. No permanent harm done, though. --- modules/notification/helpers/notification_installer.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'modules/notification/helpers') diff --git a/modules/notification/helpers/notification_installer.php b/modules/notification/helpers/notification_installer.php index 78f72194..2ba25298 100644 --- a/modules/notification/helpers/notification_installer.php +++ b/modules/notification/helpers/notification_installer.php @@ -30,13 +30,14 @@ class notification_installer { DEFAULT CHARSET=utf8;"); $db->query("CREATE TABLE IF NOT EXISTS {pending_notifications} ( `id` int(9) NOT NULL auto_increment, + `locale` char(10) default NULL, `email` varchar(128) NOT NULL, `subject` varchar(255) NOT NULL, `text` text, PRIMARY KEY (`id`)) DEFAULT CHARSET=utf8;"); - module::set_version("notification", 1); + module::set_version("notification", 2); } static function upgrade($version) { -- cgit v1.2.3