diff options
author | root <root@sleepydogs.net> | 2009-09-13 09:01:55 -0700 |
---|---|---|
committer | root <root@sleepydogs.net> | 2009-09-13 09:01:55 -0700 |
commit | c62d1f440f077ba806b7ff0c6b90ef89c79b2fd3 (patch) | |
tree | b64f05e2a7bd8db7200e3c407904e255826b4cf2 /modules/notification/helpers | |
parent | b96ac1eb81b7ccd5bd050ffab0ca9ce1feec8f4f (diff) | |
parent | caa2002d7777e0ceb884d4c628650804620ca2b6 (diff) |
Merge branch 'master' of git://github.com/gallery/gallery3
Diffstat (limited to 'modules/notification/helpers')
4 files changed, 123 insertions, 83 deletions
diff --git a/modules/notification/helpers/notification.php b/modules/notification/helpers/notification.php index 8ee0c6ba..88d92b16 100644 --- a/modules/notification/helpers/notification.php +++ b/modules/notification/helpers/notification.php @@ -67,35 +67,48 @@ class notification { } static function get_subscribers($item) { + // @todo don't access the user table directly + // @todo only return distinct email addresses $users = ORM::factory("user") ->join("subscriptions", "users.id", "subscriptions.user_id") ->join("items", "subscriptions.item_id", "items.id") ->where("email IS NOT", null) - ->where("items.left <=", $item->left) - ->where("items.right >", $item->right) + ->where("items.left_ptr <=", $item->left_ptr) + ->where("items.right_ptr >", $item->right_ptr) ->find_all(); $subscribers = array(); foreach ($users as $user) { - $subscribers[] = $user->email; + if (access::user_can($user, "view", $item)) { + $subscribers[$user->email] = 1; + } } - return $subscribers; + return array_keys($subscribers); } - static function send_item_updated($old, $new) { + static function send_item_updated($item) { + $subscribers = self::get_subscribers($item); + if (!$subscribers) { + return; + } + $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, $v->render(), $v->subject); + $v->item = $item; + $v->subject = $item->is_album() ? + t("Album %title updated", array("title" => $item->original("title"))) : + ($item->is_photo() ? + t("Photo %title updated", array("title" => $item->original("title"))) + : t("Movie %title updated", array("title" => $item->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; @@ -104,14 +117,19 @@ class notification { 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)) : + t("Movie %title added to %parent_title", array("title" => $item->title, "parent_title" => $parent->title))); - self::_notify_subscribers($item, $v->render(), $v->subject); + self::_notify($subscribers, $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; @@ -124,11 +142,16 @@ class notification { : t("Movie %title removed from %parent_title", array("title" => $item->title, "parent_title" => $parent->title))); - self::_notify_subscribers($item, $v->render(), $v->subject); + self::_notify($subscribers, $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() ? @@ -137,7 +160,7 @@ class notification { 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); + self::_notify($subscribers, $item, $v->render(), $v->subject); } static function send_pending_notifications() { @@ -150,7 +173,7 @@ class notification { ->where("email", $email) ->find_all(); if ($result->count() == 1) { - $pending = $result->get(); + $pending = $result->current(); Sendmail::factory() ->to($email) ->subject($pending->subject) @@ -176,23 +199,22 @@ class notification { } } - private static function _notify_subscribers($item, $text, $subject) { - $users = self::get_subscribers($item); - if (!empty($users)) { + private static function _notify($subscribers, $item, $text, $subject) { + if (!empty($subscribers)) { if (!batch::in_progress()) { Sendmail::factory() - ->to($users) + ->to($subscribers) ->subject($subject) ->header("Mime-Version", "1.0") ->header("Content-type", "text/html; charset=utf-8") ->message($text) ->send(); } else { - foreach ($users as $user) { + foreach ($subscribers as $subscriber) { $pending = ORM::factory("pending_notification"); $pending->subject = $subject; $pending->text = $text; - $pending->email = $user; + $pending->email = $subscriber; $pending->save(); } } diff --git a/modules/notification/helpers/notification_event.php b/modules/notification/helpers/notification_event.php index 1cf9ff58..c50b04c4 100644 --- a/modules/notification/helpers/notification_event.php +++ b/modules/notification/helpers/notification_event.php @@ -18,41 +18,98 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class notification_event_Core { - static function item_updated($old, $new) { - notification::send_item_updated($old, $new); + // The assumption is that the exception was logged at a lower level, but we + // don't want to screw up the processing that was generating the notification + // so we don't pass the exception up the call stack + static function item_updated($original, $new) { + try { + notification::send_item_updated($new); + } catch (Exception $e) { + Kohana::log("error", "@todo notification_event::item_updated() failed"); + Kohana::Log("error", $e->getMessage() . "\n" . $e->getTraceAsString()); + } } static function item_created($item) { - notification::send_item_add($item); + try { + notification::send_item_add($item); + } catch (Exception $e) { + Kohana::log("error", "@todo notification_event::item_created() failed"); + Kohana::Log("error", $e->getMessage() . "\n" . $e->getTraceAsString()); + } } - static function item_before_delete($item) { - notification::send_item_deleted($item); + static function item_deleted($item) { + try { + notification::send_item_deleted($item); - if (notification::is_watching($item)) { - notification::remove_watch($item); + if (notification::is_watching($item)) { + notification::remove_watch($item); + } + } catch (Exception $e) { + Kohana::log("error", "@todo notification_event::item_deleted() failed"); + Kohana::Log("error", $e->getMessage() . "\n" . $e->getTraceAsString()); } } static function comment_created($comment) { - if ($comment->state == "published") { - notification::send_comment_published($comment); + try { + if ($comment->state == "published") { + notification::send_comment_published($comment); + } + } catch (Exception $e) { + Kohana::log("error", "@todo notification_event::comment_created() failed"); + Kohana::Log("error", $e->getMessage() . "\n" . $e->getTraceAsString()); } } - static function comment_updated($old, $new) { - if ($new->state == "published" && $old->state != "published") { - notification::send_comment_published($new); + static function comment_updated($original, $new) { + try { + if ($new->state == "published" && $original->state != "published") { + notification::send_comment_published($new); + } + } catch (Exception $e) { + Kohana::log("error", "@todo notification_event::comment_updated() failed"); + Kohana::Log("error", $e->getMessage() . "\n" . $e->getTraceAsString()); } } static function user_before_delete($user) { - ORM::factory("subscription") - ->where("user_id", $user->id) - ->delete_all(); + try { + ORM::factory("subscription") + ->where("user_id", $user->id) + ->delete_all(); + } catch (Exception $e) { + Kohana::log("error", "@todo notification_event::user_before_delete() failed"); + Kohana::Log("error", $e->getMessage() . "\n" . $e->getTraceAsString()); + } } static function batch_complete() { - notification::send_pending_notifications(); + try { + notification::send_pending_notifications(); + } catch (Exception $e) { + Kohana::log("error", "@todo notification_event::batch_complete() failed"); + Kohana::Log("error", $e->getMessage() . "\n" . $e->getTraceAsString()); + } + } + + static function site_menu($menu, $theme) { + if (!user::active()->guest) { + $item = $theme->item(); + + if ($item && $item->is_album() && access::can("view", $item)) { + $watching = notification::is_watching($item); + + $label = $watching ? t("Remove notifications") : t("Enable notifications"); + + $menu->get("options_menu") + ->append(Menu::factory("link") + ->id("watch") + ->label($label) + ->css_id("gNotifyLink") + ->url(url::site("notification/watch/$item->id?csrf=" . access::csrf_token()))); + } + } } }
\ No newline at end of file diff --git a/modules/notification/helpers/notification_installer.php b/modules/notification/helpers/notification_installer.php index 3d450258..aa2e09f7 100644 --- a/modules/notification/helpers/notification_installer.php +++ b/modules/notification/helpers/notification_installer.php @@ -27,14 +27,14 @@ class notification_installer { PRIMARY KEY (`id`), UNIQUE KEY (`item_id`, `user_id`), UNIQUE KEY (`user_id`, `item_id`)) - ENGINE=InnoDB DEFAULT CHARSET=utf8;"); + 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;"); + DEFAULT CHARSET=utf8;"); module::set_version("notification", 1); } diff --git a/modules/notification/helpers/notification_menu.php b/modules/notification/helpers/notification_menu.php deleted file mode 100644 index 696aad62..00000000 --- a/modules/notification/helpers/notification_menu.php +++ /dev/null @@ -1,39 +0,0 @@ -<?php defined("SYSPATH") or die("No direct script access."); -/** - * Gallery - a web based photo album viewer and editor - * Copyright (C) 2000-2009 Bharat Mediratta - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or (at - * your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. - */ -class notification_menu_Core { - static function site($menu, $theme) { - if (!user::active()->guest) { - $item = $theme->item(); - - if ($item && $item->is_album()) { - $watching = notification::is_watching($item); - - $watching ? $label = t("Remove notifications") : $label = t("Enable notifications"); - - $menu->get("options_menu") - ->append(Menu::factory("link") - ->id("watch") - ->label($label) - ->css_id("gNotifyLink") - ->url(url::site("notification/watch/$item->id?csrf=" . access::csrf_token()))); - } - } - } -} |