diff options
Diffstat (limited to 'modules/notification')
-rw-r--r-- | modules/notification/controllers/notification.php | 6 | ||||
-rw-r--r-- | modules/notification/helpers/notification.php | 74 | ||||
-rw-r--r-- | modules/notification/helpers/notification_event.php | 89 | ||||
-rw-r--r-- | modules/notification/helpers/notification_installer.php | 4 | ||||
-rw-r--r-- | modules/notification/helpers/notification_menu.php | 39 | ||||
-rw-r--r-- | modules/notification/views/comment_published.html.php | 16 | ||||
-rw-r--r-- | modules/notification/views/item_added.html.php | 12 | ||||
-rw-r--r-- | modules/notification/views/item_deleted.html.php | 10 | ||||
-rw-r--r-- | modules/notification/views/item_updated.html.php | 20 |
9 files changed, 155 insertions, 115 deletions
diff --git a/modules/notification/controllers/notification.php b/modules/notification/controllers/notification.php index ffb4b46a..d502b9fe 100644 --- a/modules/notification/controllers/notification.php +++ b/modules/notification/controllers/notification.php @@ -26,11 +26,11 @@ class Notification_Controller extends Controller { if (notification::is_watching($item)) { notification::remove_watch($item); - message::success(sprintf(t("You are no longer watching %s"), $item->title)); + message::success(sprintf(t("You are no longer watching %s"), html::purify($item->title))); } else { notification::add_watch($item); - message::success(sprintf(t("You are now watching %s"), $item->title)); + message::success(sprintf(t("You are now watching %s"), html::purify($item->title))); } - url::redirect($item->url(array(), true)); + url::redirect($item->abs_url()); } } 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()))); - } - } - } -} diff --git a/modules/notification/views/comment_published.html.php b/modules/notification/views/comment_published.html.php index 4a56cdad..a8ca1899 100644 --- a/modules/notification/views/comment_published.html.php +++ b/modules/notification/views/comment_published.html.php @@ -1,32 +1,32 @@ <?php defined("SYSPATH") or die("No direct script access.") ?> <html> <head> - <title><?= p::clean($subject) ?> </title> + <title><?= html::clean($subject) ?> </title> </head> <body> - <h2><?= p::clean($subject) ?></h2> + <h2><?= html::clean($subject) ?></h2> <table> <tr> <td><?= t("Comment:") ?></td> - <td><?= nl2br(p::purify($comment->text)) ?></td> + <td><?= nl2br(html::purify($comment->text)) ?></td> </tr> <tr> <td><?= t("Author Name:") ?></td> - <td><?= p::clean($comment->author_name()) ?></td> + <td><?= html::clean($comment->author_name()) ?></td> </tr> <tr> <td><?= t("Author Email:") ?></td> - <td><?= p::clean($comment->author_email()) ?></td> + <td><?= html::clean($comment->author_email()) ?></td> </tr> <tr> <td><?= t("Author URL:") ?></td> - <td><?= p::clean($comment->author_url()) ?></td> + <td><?= html::clean($comment->author_url()) ?></td> </tr> <tr> <td><?= t("Url:") ?></td> <td> - <a href="<?= $comment->item()->url(array(), true) ?>#comments"> - <?= $comment->item()->url(array(), true) ?>#comments + <a href="<?= $comment->item()->abs_url() ?>#comments"> + <?= $comment->item()->abs_url() ?>#comments </a> </td> </tr> diff --git a/modules/notification/views/item_added.html.php b/modules/notification/views/item_added.html.php index 86724927..1ea3720d 100644 --- a/modules/notification/views/item_added.html.php +++ b/modules/notification/views/item_added.html.php @@ -1,27 +1,27 @@ <?php defined("SYSPATH") or die("No direct script access.") ?> <html> <head> - <title><?= p::clean($subject) ?> </title> + <title><?= html::clean($subject) ?> </title> </head> <body> - <h2><?= p::clean($subject) ?></h2> + <h2><?= html::clean($subject) ?></h2> <table> <tr> <td><?= t("Title:") ?></td> - <td><?= p::purify($item->title) ?></td> + <td><?= html::purify($item->title) ?></td> </tr> <tr> <td><?= t("Url:") ?></td> <td> - <a href="<?= $item->url(array(), true) ?>"> - <?= $item->url(array(), true) ?> + <a href="<?= $item->abs_url() ?>"> + <?= $item->abs_url() ?> </a> </td> </tr> <? if ($item->description): ?> <tr> <td><?= t("Description:") ?></td> - <td><?= nl2br(p::purify($item->description)) ?></td> + <td><?= nl2br(html::purify($item->description)) ?></td> </tr> <? endif ?> </table> diff --git a/modules/notification/views/item_deleted.html.php b/modules/notification/views/item_deleted.html.php index 92215211..a95cdd89 100644 --- a/modules/notification/views/item_deleted.html.php +++ b/modules/notification/views/item_deleted.html.php @@ -1,22 +1,22 @@ <?php defined("SYSPATH") or die("No direct script access.") ?> <html> <head> - <title><?= p::clean($subject) ?> </title> + <title><?= html::clean($subject) ?> </title> </head> <body> - <h2><?= p::clean($subject) ?></h2> + <h2><?= html::clean($subject) ?></h2> <table> <tr> <td colspan="2"> <?= t("To view the changed album %title use the link below.", - array("title" => p::purify($item->parent()->title))) ?> + array("title" => html::purify($item->parent()->title))) ?> </td> </tr> <tr> <td><?= t("Url:") ?></td> <td> - <a href="<?= $item->parent()->url(array(), true) ?>"> - <?= $item->parent()->url(array(), true) ?> + <a href="<?= $item->parent()->abs_url() ?>"> + <?= $item->parent()->abs_url() ?> </a> </td> </tr> diff --git a/modules/notification/views/item_updated.html.php b/modules/notification/views/item_updated.html.php index 0620c50c..9c200964 100644 --- a/modules/notification/views/item_updated.html.php +++ b/modules/notification/views/item_updated.html.php @@ -1,33 +1,33 @@ <?php defined("SYSPATH") or die("No direct script access.") ?> <html> <head> - <title><?= p::clean($subject) ?> </title> + <title><?= html::clean($subject) ?> </title> </head> <body> - <h2> <?= p::clean($subject) ?> </h2> + <h2> <?= html::clean($subject) ?> </h2> <table> <tr> - <? if ($old->title != $new->title): ?> + <? if ($item->original("title") != $item->title): ?> <td><?= t("New Title:") ?></td> - <td><?= p::clean($new->title) ?></td> + <td><?= html::clean($item->title) ?></td> <? else: ?> <td><?= t("Title:") ?></td> - <td><?= p::clean($new->title) ?></td> + <td><?= html::clean($item->title) ?></td> <? endif ?> </tr> <tr> <td><?= t("Url:") ?></td> - <td><a href="<?= $new->url(array(), true) ?>"><?= $new->url(array(), true) ?></a></td> + <td><a href="<?= $item->abs_url() ?>"><?= $item->abs_url() ?></a></td> </tr> - <? if ($old->description != $new->description): ?> + <? if ($item->original("description") != $item->description): ?> <tr> <td><?= t("New Description:") ?></td> - <td><?= p::clean($new->description) ?></td> + <td><?= html::clean($item->description) ?></td> </tr> - <? elseif (!empty($new->description)): ?> + <? elseif (!empty($item->description)): ?> <tr> <td><?= t("Description:") ?></td> - <td><?= p::clean($new->description) ?></td> + <td><?= html::clean($item->description) ?></td> </tr> <? endif ?> </table> |