2009-01-30 20:52:25 +00:00
|
|
|
<?php defined("SYSPATH") or die("No direct script access.");
|
|
|
|
|
/**
|
|
|
|
|
* Gallery - a web based photo album viewer and editor
|
2010-03-03 10:15:34 -08:00
|
|
|
* Copyright (C) 2000-2010 Bharat Mediratta
|
2009-01-30 20:52:25 +00:00
|
|
|
*
|
|
|
|
|
* 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 {
|
2009-02-01 03:58:31 +00:00
|
|
|
static function get_subscription($item_id, $user=null) {
|
2009-01-30 20:52:25 +00:00
|
|
|
if (empty($user)) {
|
2009-10-22 13:09:20 -07:00
|
|
|
$user = identity::active_user();
|
2009-01-30 20:52:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ORM::factory("subscription")
|
2009-11-26 11:43:20 -08:00
|
|
|
->where("item_id", "=", $item_id)
|
|
|
|
|
->where("user_id", "=", $user->id)
|
2009-02-01 03:58:31 +00:00
|
|
|
->find();
|
2009-01-30 20:52:25 +00:00
|
|
|
}
|
|
|
|
|
|
2009-02-01 03:58:31 +00:00
|
|
|
static function is_watching($item, $user=null) {
|
2009-01-30 20:52:25 +00:00
|
|
|
if (empty($user)) {
|
2009-10-22 13:09:20 -07:00
|
|
|
$user = identity::active_user();
|
2009-01-30 20:52:25 +00:00
|
|
|
}
|
|
|
|
|
|
2009-02-01 03:58:31 +00:00
|
|
|
return ORM::factory("subscription")
|
2009-11-26 11:43:20 -08:00
|
|
|
->where("item_id", "=", $item->id)
|
|
|
|
|
->where("user_id", "=", $user->id)
|
2009-02-01 03:58:31 +00:00
|
|
|
->find()
|
2009-11-25 13:22:24 -08:00
|
|
|
->loaded();
|
2009-01-30 20:52:25 +00:00
|
|
|
}
|
|
|
|
|
|
2009-02-01 03:58:31 +00:00
|
|
|
static function add_watch($item, $user=null) {
|
|
|
|
|
if ($item->is_album()) {
|
|
|
|
|
if (empty($user)) {
|
2009-10-22 13:09:20 -07:00
|
|
|
$user = identity::active_user();
|
2009-01-30 20:52:25 +00:00
|
|
|
}
|
2009-02-01 03:58:31 +00:00
|
|
|
$subscription = ORM::factory("subscription");
|
|
|
|
|
$subscription->item_id = $item->id;
|
|
|
|
|
$subscription->user_id = $user->id;
|
|
|
|
|
$subscription->save();
|
2009-01-30 20:52:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-01 03:58:31 +00:00
|
|
|
static function remove_watch($item, $user=null) {
|
|
|
|
|
if ($item->is_album()) {
|
|
|
|
|
if (empty($user)) {
|
2009-10-22 13:09:20 -07:00
|
|
|
$user = identity::active_user();
|
2009-01-30 20:52:25 +00:00
|
|
|
}
|
2009-02-01 03:58:31 +00:00
|
|
|
|
|
|
|
|
$subscription = ORM::factory("subscription")
|
2009-11-26 11:43:20 -08:00
|
|
|
->where("item_id", "=", $item->id)
|
|
|
|
|
->where("user_id", "=", $user->id)
|
2009-02-01 03:58:31 +00:00
|
|
|
->find()->delete();
|
2009-01-30 20:52:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-01 03:58:31 +00:00
|
|
|
static function get_subscribers($item) {
|
2009-10-16 07:41:33 -07:00
|
|
|
$subscriber_ids = array();
|
2009-10-09 01:12:05 -07:00
|
|
|
foreach (ORM::factory("subscription")
|
|
|
|
|
->select("user_id")
|
|
|
|
|
->join("items", "subscriptions.item_id", "items.id")
|
2009-11-26 11:43:20 -08:00
|
|
|
->where("items.left_ptr", "<=", $item->left_ptr)
|
|
|
|
|
->where("items.right_ptr", ">", $item->right_ptr)
|
2009-10-09 01:12:05 -07:00
|
|
|
->find_all()
|
|
|
|
|
->as_array() as $subscriber) {
|
|
|
|
|
$subscriber_ids[] = $subscriber->user_id;
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-21 11:50:42 -07:00
|
|
|
if (empty($subscriber_ids)) {
|
|
|
|
|
return array();
|
|
|
|
|
}
|
2009-10-22 13:09:20 -07:00
|
|
|
$users = identity::get_user_list($subscriber_ids);
|
2009-02-01 03:58:31 +00:00
|
|
|
|
|
|
|
|
$subscribers = array();
|
|
|
|
|
foreach ($users as $user) {
|
2009-10-16 07:41:33 -07:00
|
|
|
if (access::user_can($user, "view", $item) && !empty($user->email)) {
|
2009-07-22 11:11:48 -07:00
|
|
|
$subscribers[$user->email] = 1;
|
|
|
|
|
}
|
2009-02-01 03:58:31 +00:00
|
|
|
}
|
2009-07-22 11:11:48 -07:00
|
|
|
return array_keys($subscribers);
|
2009-01-30 20:52:25 +00:00
|
|
|
}
|
2009-02-02 07:32:45 +00:00
|
|
|
|
2010-01-16 00:10:55 -08:00
|
|
|
static function send_item_updated($original, $item) {
|
2009-09-08 21:19:00 -07:00
|
|
|
$subscribers = self::get_subscribers($item);
|
|
|
|
|
if (!$subscribers) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-04 08:51:49 +00:00
|
|
|
$v = new View("item_updated.html");
|
2010-01-16 00:10:55 -08:00
|
|
|
$v->original = $original;
|
2009-07-16 11:19:34 -07:00
|
|
|
$v->item = $item;
|
|
|
|
|
$v->subject = $item->is_album() ?
|
2010-01-16 00:10:55 -08:00
|
|
|
t("Album \"%title\" updated", array("title" => $original->title)) :
|
2009-07-16 11:19:34 -07:00
|
|
|
($item->is_photo() ?
|
2010-01-16 00:10:55 -08:00
|
|
|
t("Photo \"%title\" updated", array("title" => $original->title))
|
|
|
|
|
: t("Movie \"%title\" updated", array("title" => $original->title)));
|
|
|
|
|
|
2009-09-08 21:19:00 -07:00
|
|
|
self::_notify($subscribers, $item, $v->render(), $v->subject);
|
2009-01-30 20:52:25 +00:00
|
|
|
}
|
|
|
|
|
|
2009-02-01 03:58:31 +00:00
|
|
|
static function send_item_add($item) {
|
2009-09-08 21:19:00 -07:00
|
|
|
$subscribers = self::get_subscribers($item);
|
|
|
|
|
if (!$subscribers) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-04 06:25:55 +00:00
|
|
|
$parent = $item->parent();
|
2009-03-04 08:51:49 +00:00
|
|
|
$v = new View("item_added.html");
|
|
|
|
|
$v->item = $item;
|
|
|
|
|
$v->subject = $item->is_album() ?
|
2010-01-12 11:42:31 -08:00
|
|
|
t("Album \"%title\" added to \"%parent_title\"",
|
2009-03-04 06:25:55 +00:00
|
|
|
array("title" => $item->title, "parent_title" => $parent->title)) :
|
|
|
|
|
($item->is_photo() ?
|
2010-01-12 11:42:31 -08:00
|
|
|
t("Photo \"%title\" added to \"%parent_title\"",
|
2009-07-22 11:11:48 -07:00
|
|
|
array("title" => $item->title, "parent_title" => $parent->title)) :
|
2010-01-12 11:42:31 -08:00
|
|
|
t("Movie \"%title\" added to \"%parent_title\"",
|
2009-03-04 06:25:55 +00:00
|
|
|
array("title" => $item->title, "parent_title" => $parent->title)));
|
|
|
|
|
|
2009-09-08 21:19:00 -07:00
|
|
|
self::_notify($subscribers, $item, $v->render(), $v->subject);
|
2009-03-04 03:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
2009-02-02 05:00:09 +00:00
|
|
|
static function send_item_deleted($item) {
|
2009-09-08 21:19:00 -07:00
|
|
|
$subscribers = self::get_subscribers($item);
|
|
|
|
|
if (!$subscribers) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-04 06:25:55 +00:00
|
|
|
$parent = $item->parent();
|
2009-03-04 08:51:49 +00:00
|
|
|
$v = new View("item_deleted.html");
|
|
|
|
|
$v->item = $item;
|
|
|
|
|
$v->subject = $item->is_album() ?
|
2010-01-12 11:42:31 -08:00
|
|
|
t("Album \"%title\" removed from \"%parent_title\"",
|
2009-03-04 06:25:55 +00:00
|
|
|
array("title" => $item->title, "parent_title" => $parent->title)) :
|
|
|
|
|
($item->is_photo() ?
|
2010-01-12 11:42:31 -08:00
|
|
|
t("Photo \"%title\" removed from \"%parent_title\"",
|
2009-03-04 06:25:55 +00:00
|
|
|
array("title" => $item->title, "parent_title" => $parent->title))
|
2010-01-12 11:42:31 -08:00
|
|
|
: t("Movie \"%title\" removed from \"%parent_title\"",
|
2009-03-04 06:25:55 +00:00
|
|
|
array("title" => $item->title, "parent_title" => $parent->title)));
|
|
|
|
|
|
2009-09-08 21:19:00 -07:00
|
|
|
self::_notify($subscribers, $item, $v->render(), $v->subject);
|
2009-02-01 03:58:31 +00:00
|
|
|
}
|
2009-01-30 20:52:25 +00:00
|
|
|
|
2009-02-02 15:41:47 +00:00
|
|
|
static function send_comment_published($comment) {
|
2009-03-04 06:25:55 +00:00
|
|
|
$item = $comment->item();
|
2009-09-08 21:19:00 -07:00
|
|
|
$subscribers = self::get_subscribers($item);
|
|
|
|
|
if (!$subscribers) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-04 08:51:49 +00:00
|
|
|
$v = new View("comment_published.html");
|
|
|
|
|
$v->comment = $comment;
|
|
|
|
|
$v->subject = $item->is_album() ?
|
2010-01-12 11:42:31 -08:00
|
|
|
t("A new comment was published for album \"%title\"", array("title" => $item->title)) :
|
2009-03-04 06:25:55 +00:00
|
|
|
($item->is_photo() ?
|
2010-01-12 11:42:31 -08:00
|
|
|
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)));
|
2009-02-02 03:09:17 +00:00
|
|
|
|
2009-09-08 21:19:00 -07:00
|
|
|
self::_notify($subscribers, $item, $v->render(), $v->subject);
|
2009-02-02 03:09:17 +00:00
|
|
|
}
|
2009-02-05 08:00:42 +00:00
|
|
|
|
2009-03-04 08:51:49 +00:00
|
|
|
static function send_pending_notifications() {
|
2009-11-26 21:14:54 -08:00
|
|
|
foreach (db::build()
|
2009-12-02 10:43:06 -08:00
|
|
|
->select(new Database_Expression("DISTINCT `email`"))
|
2009-03-04 08:51:49 +00:00
|
|
|
->from("pending_notifications")
|
2009-11-26 21:14:54 -08:00
|
|
|
->execute() as $row) {
|
2009-03-04 08:51:49 +00:00
|
|
|
$email = $row->email;
|
|
|
|
|
$result = ORM::factory("pending_notification")
|
2009-11-26 11:43:20 -08:00
|
|
|
->where("email", "=", $email)
|
2009-03-04 08:51:49 +00:00
|
|
|
->find_all();
|
2009-06-01 22:40:22 -07:00
|
|
|
if ($result->count() == 1) {
|
2009-08-28 12:42:37 -07:00
|
|
|
$pending = $result->current();
|
2009-03-04 08:51:49 +00:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-03-04 03:31:01 +00:00
|
|
|
}
|
2009-03-04 06:25:55 +00:00
|
|
|
|
2009-09-08 21:19:00 -07:00
|
|
|
private static function _notify($subscribers, $item, $text, $subject) {
|
|
|
|
|
if (!empty($subscribers)) {
|
2009-03-04 08:51:49 +00:00
|
|
|
if (!batch::in_progress()) {
|
|
|
|
|
Sendmail::factory()
|
2009-09-08 21:19:00 -07:00
|
|
|
->to($subscribers)
|
2009-03-04 08:51:49 +00:00
|
|
|
->subject($subject)
|
|
|
|
|
->header("Mime-Version", "1.0")
|
|
|
|
|
->header("Content-type", "text/html; charset=utf-8")
|
|
|
|
|
->message($text)
|
|
|
|
|
->send();
|
|
|
|
|
} else {
|
2009-09-08 21:19:00 -07:00
|
|
|
foreach ($subscribers as $subscriber) {
|
2009-03-04 08:51:49 +00:00
|
|
|
$pending = ORM::factory("pending_notification");
|
|
|
|
|
$pending->subject = $subject;
|
|
|
|
|
$pending->text = $text;
|
2009-09-08 21:19:00 -07:00
|
|
|
$pending->email = $subscriber;
|
2009-03-04 08:51:49 +00:00
|
|
|
$pending->save();
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-02-02 07:32:45 +00:00
|
|
|
}
|
2009-02-01 03:58:31 +00:00
|
|
|
}
|
2009-01-30 20:52:25 +00:00
|
|
|
}
|