diff options
author | Bharat Mediratta <bharat@menalto.com> | 2009-03-04 08:51:49 +0000 |
---|---|---|
committer | Bharat Mediratta <bharat@menalto.com> | 2009-03-04 08:51:49 +0000 |
commit | 23b0abb9742d3418484fe8d01609f6849d7960ce (patch) | |
tree | 917e2816d561036055769c1723382ab0de4f6431 /modules/notification/helpers | |
parent | b493a534f2966e23eb0244654e8929320721da8e (diff) |
Redefine the batch API to be very very simple. You call
batch::start() before starting a series of events, and batch::stop()
when you're done.
In batch mode, the notification module will store up pending
notifications. When the batch job is complete, it'll send a single
digested email to each user for all of her notifications.
Updated the scaffold and local_import to use this. Haven't modified
SimpleUploader yet.
Diffstat (limited to 'modules/notification/helpers')
-rw-r--r-- | modules/notification/helpers/notification.php | 109 | ||||
-rw-r--r-- | modules/notification/helpers/notification_event.php | 22 | ||||
-rw-r--r-- | modules/notification/helpers/notification_installer.php | 10 |
3 files changed, 81 insertions, 60 deletions
diff --git a/modules/notification/helpers/notification.php b/modules/notification/helpers/notification.php index 4d095888..6d8ffd64 100644 --- a/modules/notification/helpers/notification.php +++ b/modules/notification/helpers/notification.php @@ -83,24 +83,23 @@ class notification { } static function send_item_updated($old, $new) { - $body = new View("item_updated.html"); - $body->old = $old; - $body->new = $new; - $body->subject = $old->is_album() ? + $v = new View("item_updated.html"); + $v->old = $old; + $v->new = $new; + $v->subject = $old->is_album() ? t("Album %title updated", array("title" => $old->title)) : ($old->is_photo() ? t("Photo %title updated", array("title" => $old->title)) : t("Movie %title updated", array("title" => $old->title))); - self::_notify_subscribers($old, $body, $body->subject); + self::_notify_subscribers($old, $v->render(), $v->subject); } static function send_item_add($item) { - $body = new View("item_added.html"); - $body->item = $item; - $parent = $item->parent(); - $subject = $item->is_album() ? + $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() ? @@ -109,25 +108,14 @@ class notification { : t("Movie %title added to %parent_title", array("title" => $item->title, "parent_title" => $parent->title))); - self::_notify_subscribers($item, $body, $subject); - } - - static function send_batch_add($parent_id) { - $parent = ORM::factory("item", $parent_id); - if ($parent->loaded) { - $body = new View("batch_add.html"); - $body->item = $parent; - - $subject = t("Album %title updated", array("title" => $parent->title)); - self::_notify_subscribers($parent, $body, $subject); - } + self::_notify_subscribers($item, $v->render(), $v->subject); } static function send_item_deleted($item) { - $body = new View("item_deleted.html"); - $body->item = $item; $parent = $item->parent(); - $subject = $item->is_album() ? + $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() ? @@ -136,37 +124,78 @@ class notification { : t("Movie %title removed from %parent_title", array("title" => $item->title, "parent_title" => $parent->title))); - self::_notify_subscribers($item, $body, $subject); + self::_notify_subscribers($item, $v->render(), $v->subject); } static function send_comment_published($comment) { - $body = new View("comment_published.html"); - $body->comment = $comment; - $item = $comment->item(); - $subject = $item->is_album() ? + $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)) : ($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, $body, $subject); + self::_notify_subscribers($item, $v->render(), $v->subject); } - static function process_notifications() { - Kohana::log("error", "processing notifications in shutdown"); + static function send_pending_notifications() { + foreach (Database::instance() + ->select("DISTINCT email") + ->from("pending_notifications") + ->get() as $row) { + $email = $row->email; + $result = ORM::factory("pending_notification") + ->where("email", $email) + ->find_all(); + if ($result->count == 1) { + $pending = $result->get(); + Sendmail::factory() + ->to($email) + ->subject($pending->subject) + ->header("Mime-Version", "1.0") + ->header("Content-type", "text/html; charset=utf-8") + ->message($pending->body) + ->send(); + $pending->delete(); + } else { + $text = ""; + foreach ($result as $pending) { + $text .= $pending->text; + $pending->delete(); + } + Sendmail::factory() + ->to($email) + ->subject(t("Multiple events have occurred")) // @todo fix this terrible subject line + ->header("Mime-Version", "1.0") + ->header("Content-type", "text/html; charset=utf-8") + ->message($text) + ->send(); + } + } } - private static function _notify_subscribers($item, $body, $subject) { + private static function _notify_subscribers($item, $text, $subject) { $users = self::get_subscribers($item); if (!empty($users)) { - Sendmail::factory() - ->to($users) - ->subject($subject) - ->header("Mime-Version", "1.0") - ->header("Content-type", "text/html; charset=utf-8") - ->message($body->render()) - ->send(); + if (!batch::in_progress()) { + Sendmail::factory() + ->to($users) + ->subject($subject) + ->header("Mime-Version", "1.0") + ->header("Content-type", "text/html; charset=utf-8") + ->message($text) + ->send(); + } else { + foreach ($users as $user) { + $pending = ORM::factory("pending_notification"); + $pending->subject = $subject; + $pending->text = $text; + $pending->email = $user; + $pending->save(); + } + } } } } diff --git a/modules/notification/helpers/notification_event.php b/modules/notification/helpers/notification_event.php index dde328d8..4ea0be05 100644 --- a/modules/notification/helpers/notification_event.php +++ b/modules/notification/helpers/notification_event.php @@ -52,23 +52,7 @@ class notification_event_Core { ->delete_all(); } - static function operation($name, $item) { - if ($name == "add") { - $id = Session::instance()->get("notification_batch_item_id"); - if ($id && $item->id != $id) { - notification::send_batch_add($id); - } - Session::instance()->set("notification_batch_item_id", $item->id); - } + static function batch_complete() { + notification::send_pending_notifications(); } - - static function end_operation($name) { - if ($name == "add") { - $id = Session::instance()->get_once("notification_batch_item_id"); - if ($id) { - notification::send_batch_add($id); - } - } - } - -} +}
\ No newline at end of file diff --git a/modules/notification/helpers/notification_installer.php b/modules/notification/helpers/notification_installer.php index 473ae169..01b5a1c8 100644 --- a/modules/notification/helpers/notification_installer.php +++ b/modules/notification/helpers/notification_installer.php @@ -31,6 +31,13 @@ class notification_installer { UNIQUE KEY (`item_id`, `user_id`), UNIQUE KEY (`user_id`, `item_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;"); + $db->query("CREATE TABLE IF NOT EXISTS {pending_notifications} ( + `id` int(9) NOT NULL auto_increment, + `email` varchar(128) NOT NULL, + `subject` varchar(255) NOT NULL, + `text` text, + PRIMARY KEY (`id`)) + ENGINE=InnoDB DEFAULT CHARSET=utf8;"); module::set_version("notification", 1); } @@ -39,7 +46,8 @@ class notification_installer { static function uninstall() { $db = Database::instance(); $db->query("DROP TABLE IF EXISTS {subscriptions};"); - + $db->query("DROP TABLE IF EXISTS {pending_notifications};"); + module::delete("notification"); } } |