summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorTim Almdal <tnalmdal@shaw.ca>2009-02-01 03:58:31 +0000
committerTim Almdal <tnalmdal@shaw.ca>2009-02-01 03:58:31 +0000
commit6f358291733b5844f5e9beaa747f554a0049e27a (patch)
treeaf9347258c8423ece92053180fea759720b04aef /modules
parentd49484c9a65681f0ee41490c098a0b4d7a938c0e (diff)
Simplify the setting of a notifications. Notifications are not only
set on a album. The notifications are implicitly active for all child elements. It now sends emails if the email address of the subscribed user has been set. No email, no attempt to send the notification. Still to do, come up with better messages as the current ones are just place holders.
Diffstat (limited to 'modules')
-rw-r--r--modules/notification/controllers/notification.php20
-rw-r--r--modules/notification/helpers/notification.php140
-rw-r--r--modules/notification/helpers/notification_event.php18
-rw-r--r--modules/notification/helpers/notification_installer.php1
-rw-r--r--modules/notification/helpers/notification_menu.php15
-rw-r--r--modules/notification/models/subscription.php2
6 files changed, 98 insertions, 98 deletions
diff --git a/modules/notification/controllers/notification.php b/modules/notification/controllers/notification.php
index 804a908e..26842a89 100644
--- a/modules/notification/controllers/notification.php
+++ b/modules/notification/controllers/notification.php
@@ -22,17 +22,16 @@ class Notification_Controller extends Controller {
$item = ORM::factory("item", $id);
access::required("view", $item);
- $watching = notification::is_watching($item->id);
+ $watching = notification::is_watching($item);
$form = $this->_get_form($item, $watching);
if (request::method() == "post") {
if ($form->validate()) {
- $watch_children = $form->watch->inputs["apply_to_children"]->value;
if (!$watching) {
- notification::add_watch($item, $watch_children);
+ notification::add_watch($item);
message::success(sprintf(t("Watch Enabled on %s!"), $item->title));
$response = json_encode(array("result" => "success"));
} else {
- notification::remove_watch($item, $watch_children);
+ notification::remove_watch($item);
$response = json_encode(array("result" => "success"));
message::success(sprintf(t("Watch Removed on %s!"), $item->title));
}
@@ -48,19 +47,10 @@ class Notification_Controller extends Controller {
function _get_form($item, $watching) {
$button_text = $watching ? t("Remove Watch") : t("Add Watch");
- if ($item->is_album()) {
- $label = $watching ? t("Remove Watch from Album") : t("Add Watch to Album");
- } else {
- $label = $watching ? t("Remove Watch from Photo") : t("Add Watch to Photo");
- }
+ $label = $watching ? t("Remove Watch from Album") : t("Add Watch to Album");
+
$form = new Forge("notification/watch/$item->id", "", "post", array("id" => "gAddWatchForm"));
$group = $form->group("watch")->label($label);
- $checkbox = $group->checkbox("apply_to_children")
- ->label(t("Apply to Children"))
- ->checked($item->is_album());
- if (!$item->is_album()) {
- $checkbox->disabled("disabled");
- }
$group->submit("")->value($button_text);
return $form;
diff --git a/modules/notification/helpers/notification.php b/modules/notification/helpers/notification.php
index 79f7288e..00ea994d 100644
--- a/modules/notification/helpers/notification.php
+++ b/modules/notification/helpers/notification.php
@@ -18,7 +18,7 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class notification {
- static function get_subscriptions($item_id, $user=null) {
+ static function get_subscription($item_id, $user=null) {
if (empty($user)) {
$user = user::active();
}
@@ -26,94 +26,104 @@ class notification {
return ORM::factory("subscription")
->where("item_id", $item_id)
->where("user_id", $user->id)
- ->find_all();
+ ->find();
}
- static function is_watching($item_id, $user=null) {
+ static function is_watching($item, $user=null) {
if (empty($user)) {
$user = user::active();
}
- $count = ORM::factory("subscription")
- ->where("item_id", $item_id)
+ return ORM::factory("subscription")
+ ->where("item_id", $item->id)
->where("user_id", $user->id)
- ->count_all();
-
- return $count > 0;
+ ->find()
+ ->loaded;
}
- static function add_watch($item, $watch_children=false, $user=null) {
- if (empty($user)) {
- $user = user::active();
- }
- $subscription = ORM::factory("subscription");
- $subscription->item_id = $item->id;
- $subscription->apply_to_children = $watch_children;
- $subscription->user_id = $user->id;
- $subscription->save();
-
- if ($watch_children && $item->is_album()) {
- $children = ORM::factory("item")
- ->viewable()
- ->where("parent_id", $item->id)
- ->find_all();
- foreach ($children as $child) {
- self::add_watch($child, $watch_children, $user);
+ static function add_watch($item, $user=null) {
+ if ($item->is_album()) {
+ if (empty($user)) {
+ $user = user::active();
}
+ $subscription = ORM::factory("subscription");
+ $subscription->item_id = $item->id;
+ $subscription->user_id = $user->id;
+ $subscription->save();
}
}
- static function remove_watch($item, $watch_children=false, $user=null) {
- if (empty($user)) {
- $user = user::active();
- }
-
- $subscription = ORM::factory("subscription")
- ->where("item_id", $item->id)
- ->where("user_id", $user->id)
- ->find();
- $subscription->delete();
-
- if ($watch_children && $item->is_album()) {
- $children = ORM::factory("item")
- ->viewable()
- ->where("parent_id", $item->id)
- ->find_all();
- foreach ($children as $child) {
- self::remove_watch($child, $watch_children, $user);
+ static function remove_watch($item, $user=null) {
+ if ($item->is_album()) {
+ if (empty($user)) {
+ $user = user::active();
}
+
+ $subscription = ORM::factory("subscription")
+ ->where("item_id", $item->id)
+ ->where("user_id", $user->id)
+ ->find()->delete();
}
}
- static function get_subscribers($item_id) {
- return ORM::factory("subscription")
- ->where("item_id", $item_id)
+ static function get_subscribers($item) {
+ $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)
->find_all();
+
+ $subscribers = array();
+ foreach ($users as $user) {
+ $subscribers[] = $user->email;
+ }
+ return $subscribers;
}
- static function count_subscribers($item_id) {
- return ORM::factory("subscription")
- ->where("item_id", $item_id)
- ->count_all();
+ static function send_item_changed($old, $new=null) {
+ $users = self::get_subscribers($item);
}
-
- static function get_watched_items($user=null) {
- if (empty($user)) {
- $user = user::active();
- }
- return ORM::factory("subscription")
- ->where("user_id", $item_id)
- ->find_all();
+ static function send_item_add($item) {
+ $users = self::get_subscribers($item);
+ Sendmail::factory()
+ ->to($users)
+ ->from("from@gallery3.com")
+ ->subject(t("Item added to Gallery3"))
+ ->message($item->title)
+ ->send();
}
- static function count_watched_items($user=null) {
- if (empty($user)) {
- $user = user::active();
- }
+ static function send_item_delete($item) {
+ $users = self::get_subscribers($item);
+ Sendmail::factory()
+ ->to($users)
+ ->from("from@gallery3.com")
+ ->subject("Item deleted: $item->title")
+ ->message("It was deleted")
+ ->send();
+ }
- return ORM::factory("subscription")
- ->where("user_id", $item_id)
- ->count_all();
+ static function send_comment_added($comment) {
+ $users = self::get_subscribers($comment->item());
+ Sendmail::factory()
+ ->to($users)
+ ->from("from@gallery3.com")
+ ->subject("Comment added to $comment->$item->title")
+ ->message($comment->text)
+ ->send();
}
+
+ static function send_comment_changed($old, $new) {
+ $users = self::get_subscribers($comment->item());
+ Sendmail::factory()
+ ->to($users)
+ ->from("from@gallery3.com")
+ ->subject("Comment updated on item: $comment->$item-title")
+ ->message($new->text)
+ ->send();
+ }
+
}
diff --git a/modules/notification/helpers/notification_event.php b/modules/notification/helpers/notification_event.php
index 06b5b1f6..415093a5 100644
--- a/modules/notification/helpers/notification_event.php
+++ b/modules/notification/helpers/notification_event.php
@@ -18,9 +18,27 @@
* 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_changed($old, $new);
+ }
+
+ static function item_created($item) {
+ notification::send_item_add($item);
+ }
+
+ static function item_before_delete($item) {
+ notification::send_item_deleted($item);
+
+ if (notification::is_watching($item)) {
+ notification::remove_watch($item);
+ }
+ }
+
static function comment_created($comment) {
+ notification::send_comment_added($comment);
}
static function comment_updated($old, $new) {
+ notification::send_comment_changed($old, $new);
}
}
diff --git a/modules/notification/helpers/notification_installer.php b/modules/notification/helpers/notification_installer.php
index c2a9828f..f705fe68 100644
--- a/modules/notification/helpers/notification_installer.php
+++ b/modules/notification/helpers/notification_installer.php
@@ -26,7 +26,6 @@ class notification_installer {
$db->query("CREATE TABLE IF NOT EXISTS `subscriptions` (
`id` int(9) NOT NULL auto_increment,
`item_id` int(9) NOT NULL,
- `apply_to_children` tinyint(1) NOT NULL,
`user_id` int(9) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY (`item_id`, `user_id`),
diff --git a/modules/notification/helpers/notification_menu.php b/modules/notification/helpers/notification_menu.php
index 8c2e84d8..b36b69e8 100644
--- a/modules/notification/helpers/notification_menu.php
+++ b/modules/notification/helpers/notification_menu.php
@@ -32,19 +32,4 @@ class notification_menu_Core {
}
}
}
-
- static function photo($menu, $theme) {
- if (!user::active()->guest) {
- $item = $theme->item();
-
- if ($item) {
- $menu
- ->append(Menu::factory("dialog")
- ->id("watch")
- ->label(t("Enable notifications for this photo"))
- ->url(url::site("notification/watch/$item->id"))
- ->css_id("gWatchLink"));
- }
- }
- }
}
diff --git a/modules/notification/models/subscription.php b/modules/notification/models/subscription.php
index aa90a58c..9d728009 100644
--- a/modules/notification/models/subscription.php
+++ b/modules/notification/models/subscription.php
@@ -18,6 +18,4 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class Subscription_Model extends ORM {
- protected $has_one = array("item");
- protected $has_many = array("user");
} \ No newline at end of file