summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2009-07-16 11:19:34 -0700
committerBharat Mediratta <bharat@menalto.com>2009-07-16 11:19:34 -0700
commit5b3b675b6d8a1cd9a5f2b9455c551791e18d88ff (patch)
tree239f6042596a5168b9023706c33a0f89ebaf6d1d
parent2864aceb8117d0644b264ceca4d0f84fd028538f (diff)
Non-trivial changes to the event handling code:
1) The item_updated event no longer takes the old and new items. Instead we overload ORM to track the original data and make that available via the item. This will allow us to move event publishing down into the API methods which in turn will give us more stability since we won't require each controller to remember to do it. 2) ORM class now tracks the original values. It doesn't track the original relationships (no need for that, yet) 3) Added new events: item_deleted group_deleted user_deleted
-rw-r--r--modules/akismet/helpers/akismet_event.php6
-rw-r--r--modules/comment/controllers/admin_comments.php4
-rw-r--r--modules/comment/helpers/comment_event.php2
-rw-r--r--modules/exif/helpers/exif_event.php2
-rw-r--r--modules/g2_import/helpers/g2_import_event.php2
-rw-r--r--modules/gallery/controllers/albums.php2
-rw-r--r--modules/gallery/controllers/movies.php2
-rw-r--r--modules/gallery/controllers/photos.php2
-rw-r--r--modules/gallery/helpers/gallery_event.php4
-rw-r--r--modules/gallery/libraries/MY_ORM.php27
-rw-r--r--modules/gallery/models/item.php3
-rw-r--r--modules/gallery/tests/Item_Model_Test.php16
-rw-r--r--modules/notification/helpers/notification.php19
-rw-r--r--modules/notification/helpers/notification_event.php10
-rw-r--r--modules/notification/helpers/notification_menu.php2
-rw-r--r--modules/notification/views/item_updated.html.php16
-rw-r--r--modules/organize/controllers/organize.php4
-rw-r--r--modules/search/helpers/search_event.php6
-rw-r--r--modules/tag/helpers/tag_event.php2
-rw-r--r--modules/user/models/group.php2
-rw-r--r--modules/user/models/user.php2
21 files changed, 91 insertions, 44 deletions
diff --git a/modules/akismet/helpers/akismet_event.php b/modules/akismet/helpers/akismet_event.php
index 80fe0127..bffc0fd7 100644
--- a/modules/akismet/helpers/akismet_event.php
+++ b/modules/akismet/helpers/akismet_event.php
@@ -40,14 +40,14 @@ class akismet_event_Core {
$comment->save();
}
- static function comment_updated($old, $new) {
+ static function comment_updated($comment) {
if (!module::get_var("akismet", "api_key")) {
return;
}
- if ($old->state != "spam" && $new->state == "spam") {
+ if ($comment->original("state") != "spam" && $comment->state == "spam") {
akismet::submit_spam($new);
- } else if ($old->state == "spam" && $new->state != "spam") {
+ } else if ($comment->original("state") == "spam" && $comment->state != "spam") {
akismet::submit_ham($new);
}
}
diff --git a/modules/comment/controllers/admin_comments.php b/modules/comment/controllers/admin_comments.php
index 3e8d3c46..ea76b188 100644
--- a/modules/comment/controllers/admin_comments.php
+++ b/modules/comment/controllers/admin_comments.php
@@ -113,8 +113,8 @@ class Admin_Comments_Controller extends Admin_Controller {
if ($comment->loaded) {
$comment->state = $state;
$comment->save();
- module::event("comment_updated", $orig, $comment);
- if ($orig->state == "published" || $comment->state == "published") {
+ module::event("comment_updated", $comment);
+ if ($comment->original("state") == "published" || $comment->state == "published") {
module::event("item_related_update", $comment->item());
}
}
diff --git a/modules/comment/helpers/comment_event.php b/modules/comment/helpers/comment_event.php
index a3beb27a..3850a001 100644
--- a/modules/comment/helpers/comment_event.php
+++ b/modules/comment/helpers/comment_event.php
@@ -18,7 +18,7 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class comment_event_Core {
- static function item_before_delete($item) {
+ static function item_deleted($item) {
Database::instance()->delete("comments", array("item_id" => $item->id));
}
}
diff --git a/modules/exif/helpers/exif_event.php b/modules/exif/helpers/exif_event.php
index f5677653..24243f4d 100644
--- a/modules/exif/helpers/exif_event.php
+++ b/modules/exif/helpers/exif_event.php
@@ -22,7 +22,7 @@ class exif_event_Core {
exif::extract($item);
}
- static function item_before_delete($item) {
+ static function item_deleted($item) {
Database::instance()->delete("exif_records", array("item_id" => $item->id));
}
}
diff --git a/modules/g2_import/helpers/g2_import_event.php b/modules/g2_import/helpers/g2_import_event.php
index 13f5b1a0..77b489a7 100644
--- a/modules/g2_import/helpers/g2_import_event.php
+++ b/modules/g2_import/helpers/g2_import_event.php
@@ -18,7 +18,7 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class g2_import_event_Core {
- static function item_before_delete($item) {
+ static function item_deleted($item) {
Database::instance()->delete("g2_maps", array("g3_id" => $item->id));
}
diff --git a/modules/gallery/controllers/albums.php b/modules/gallery/controllers/albums.php
index e6d01b90..c378e3ce 100644
--- a/modules/gallery/controllers/albums.php
+++ b/modules/gallery/controllers/albums.php
@@ -192,7 +192,7 @@ class Albums_Controller extends Items_Controller {
}
$album->save();
- module::event("item_updated", $orig, $album);
+ module::event("item_updated", $album);
log::success("content", "Updated album", "<a href=\"albums/$album->id\">view</a>");
message::success(
diff --git a/modules/gallery/controllers/movies.php b/modules/gallery/controllers/movies.php
index 30a5d78c..fc511082 100644
--- a/modules/gallery/controllers/movies.php
+++ b/modules/gallery/controllers/movies.php
@@ -91,7 +91,7 @@ class Movies_Controller extends Items_Controller {
$photo->rename($form->edit_photo->filename->value);
$photo->save();
- module::event("item_updated", $orig, $photo);
+ module::event("item_updated", $photo);
log::success("content", "Updated photo", "<a href=\"photos/$photo->id\">view</a>");
message::success(
diff --git a/modules/gallery/controllers/photos.php b/modules/gallery/controllers/photos.php
index 6a62e859..77627009 100644
--- a/modules/gallery/controllers/photos.php
+++ b/modules/gallery/controllers/photos.php
@@ -84,7 +84,7 @@ class Photos_Controller extends Items_Controller {
$photo->rename($form->edit_photo->filename->value);
$photo->save();
- module::event("item_updated", $orig, $photo);
+ module::event("item_updated", $photo);
log::success("content", "Updated photo", "<a href=\"photos/$photo->id\">view</a>");
message::success(
diff --git a/modules/gallery/helpers/gallery_event.php b/modules/gallery/helpers/gallery_event.php
index aa11b7c0..2f3a64d3 100644
--- a/modules/gallery/helpers/gallery_event.php
+++ b/modules/gallery/helpers/gallery_event.php
@@ -23,7 +23,7 @@ class gallery_event_Core {
access::add_group($group);
}
- static function group_before_delete($group) {
+ static function group_deleted($group) {
access::delete_group($group);
}
@@ -31,7 +31,7 @@ class gallery_event_Core {
access::add_item($item);
}
- static function item_before_delete($item) {
+ static function item_deleted($item) {
access::delete_item($item);
}
diff --git a/modules/gallery/libraries/MY_ORM.php b/modules/gallery/libraries/MY_ORM.php
index 2bd9b4eb..319cbe09 100644
--- a/modules/gallery/libraries/MY_ORM.php
+++ b/modules/gallery/libraries/MY_ORM.php
@@ -18,6 +18,9 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class ORM extends ORM_Core {
+ // Track the original value of this ORM instance so that we can look it up in ORM::original()
+ protected $original = null;
+
public function open_paren() {
$this->db->open_paren();
return $this;
@@ -30,7 +33,29 @@ class ORM extends ORM_Core {
public function save() {
model_cache::clear($this->object_name, $this->{$this->primary_key}, $this->primary_key);
- return parent::save();
+ $result = parent::save();
+ $this->original = $this->object;
+ return $result;
+ }
+
+ public function __set($column, $value) {
+ if (!isset($this->original)) {
+ $this->original = $this->object;
+ }
+
+ return parent::__set($column, $value);
+ }
+
+ public function __unset($column) {
+ if (!isset($this->original)) {
+ $this->original = $this->object;
+ }
+
+ return parent::__unset($column);
+ }
+
+ public function original($column) {
+ return $this->original[$column];
}
}
diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php
index 51037073..80f19d26 100644
--- a/modules/gallery/models/item.php
+++ b/modules/gallery/models/item.php
@@ -93,6 +93,7 @@ class Item_Model extends ORM_MPTT {
}
public function delete() {
+ $old = clone $this;
module::event("item_before_delete", $this);
$parent = $this->parent();
@@ -114,6 +115,8 @@ class Item_Model extends ORM_MPTT {
@unlink($resize_path);
@unlink($thumb_path);
}
+
+ module::event("item_deleted", $old);
}
/**
diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php
index 615b8997..a21cdc13 100644
--- a/modules/gallery/tests/Item_Model_Test.php
+++ b/modules/gallery/tests/Item_Model_Test.php
@@ -140,4 +140,20 @@ class Item_Model_Test extends Unit_Test_Case {
}
$this->assert_false(true, "Item_Model::rename should not accept / characters");
}
+
+ public function save_original_values_test() {
+ print "START\n";
+ $item = $this->create_random_item();
+ $item->title = "ORIGINAL_VALUE";
+ $item->save();
+
+ print "CHANGE\n";
+ $item->title = "NEW_VALUE";
+
+ //printf("<pre>%s</pre>",print_r($item,1));flush();
+
+ print "COMPARE\n";
+ $this->assert_same("ORIGINAL_VALUE", $item->original("title"));
+ $this->assert_same("NEW_VALUE", $item->title);
+ }
}
diff --git a/modules/notification/helpers/notification.php b/modules/notification/helpers/notification.php
index 8ee0c6ba..e246af2c 100644
--- a/modules/notification/helpers/notification.php
+++ b/modules/notification/helpers/notification.php
@@ -82,17 +82,16 @@ class notification {
return $subscribers;
}
- static function send_item_updated($old, $new) {
+ static function send_item_updated($item) {
$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) {
diff --git a/modules/notification/helpers/notification_event.php b/modules/notification/helpers/notification_event.php
index 1cf9ff58..536557c6 100644
--- a/modules/notification/helpers/notification_event.php
+++ b/modules/notification/helpers/notification_event.php
@@ -18,15 +18,15 @@
* 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);
+ static function item_updated($item) {
+ notification::send_item_updated($item);
}
static function item_created($item) {
notification::send_item_add($item);
}
- static function item_before_delete($item) {
+ static function item_deleted($item) {
notification::send_item_deleted($item);
if (notification::is_watching($item)) {
@@ -40,8 +40,8 @@ class notification_event_Core {
}
}
- static function comment_updated($old, $new) {
- if ($new->state == "published" && $old->state != "published") {
+ static function comment_updated($item) {
+ if ($item->state == "published" && $item->original("state") != "published") {
notification::send_comment_published($new);
}
}
diff --git a/modules/notification/helpers/notification_menu.php b/modules/notification/helpers/notification_menu.php
index 696aad62..87478b8a 100644
--- a/modules/notification/helpers/notification_menu.php
+++ b/modules/notification/helpers/notification_menu.php
@@ -21,7 +21,7 @@ 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);
diff --git a/modules/notification/views/item_updated.html.php b/modules/notification/views/item_updated.html.php
index 0620c50c..39f9113b 100644
--- a/modules/notification/views/item_updated.html.php
+++ b/modules/notification/views/item_updated.html.php
@@ -7,27 +7,27 @@
<h2> <?= p::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><?= p::clean($item->title) ?></td>
<? else: ?>
<td><?= t("Title:") ?></td>
- <td><?= p::clean($new->title) ?></td>
+ <td><?= p::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->url(array(), true) ?>"><?= $item->url(array(), true) ?></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><?= p::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><?= p::clean($item->description) ?></td>
</tr>
<? endif ?>
</table>
diff --git a/modules/organize/controllers/organize.php b/modules/organize/controllers/organize.php
index 6792573d..54e04071 100644
--- a/modules/organize/controllers/organize.php
+++ b/modules/organize/controllers/organize.php
@@ -279,7 +279,7 @@ class Organize_Controller extends Controller {
$item->rename($form->dirname->value);
$item->save();
- module::event("item_updated", $orig, $item);
+ module::event("item_updated", $item);
if ($item->is_album()) {
log::success("content", "Updated album", "<a href=\"albums/$item->id\">view</a>");
@@ -322,7 +322,7 @@ class Organize_Controller extends Controller {
$item->sort_order = $form->direction->value;
$item->save();
- module::event("item_updated", $orig, $item);
+ module::event("item_updated", $item);
log::success("content", "Updated album", "<a href=\"albums/$item->id\">view</a>");
$message = t("Saved album %album_title", array("album_title" => p::purify($item->title)));
diff --git a/modules/search/helpers/search_event.php b/modules/search/helpers/search_event.php
index b9657395..764fdd18 100644
--- a/modules/search/helpers/search_event.php
+++ b/modules/search/helpers/search_event.php
@@ -22,11 +22,11 @@ class search_event_Core {
search::update($item);
}
- static function item_updated($old_item, $new_item) {
- search::update($new_item);
+ static function item_updated($item) {
+ search::update($item);
}
- static function item_before_delete($item) {
+ static function item_deleted($item) {
ORM::factory("search_record")
->where("item_id", $item->id)
->delete_all();
diff --git a/modules/tag/helpers/tag_event.php b/modules/tag/helpers/tag_event.php
index 7a170bf8..0164f556 100644
--- a/modules/tag/helpers/tag_event.php
+++ b/modules/tag/helpers/tag_event.php
@@ -59,7 +59,7 @@ class tag_event_Core {
return;
}
- static function item_before_delete($item) {
+ static function item_deleted($item) {
$db = Database::instance();
$db->query("UPDATE {tags} SET `count` = `count` - 1 WHERE `count` > 0 " .
"AND `id` IN (SELECT `tag_id` from {items_tags} WHERE `item_id` = $item->id)");
diff --git a/modules/user/models/group.php b/modules/user/models/group.php
index 45948887..e0724e30 100644
--- a/modules/user/models/group.php
+++ b/modules/user/models/group.php
@@ -27,7 +27,9 @@ class Group_Model extends ORM {
* @see ORM::delete()
*/
public function delete($id=null) {
+ $old = clone $this;
module::event("group_before_delete", $this);
parent::delete($id);
+ module::event("group_deleted", $old);
}
} \ No newline at end of file
diff --git a/modules/user/models/user.php b/modules/user/models/user.php
index b447892e..e3260270 100644
--- a/modules/user/models/user.php
+++ b/modules/user/models/user.php
@@ -44,8 +44,10 @@ class User_Model extends ORM {
* @see ORM::delete()
*/
public function delete($id=null) {
+ $old = clone $this;
module::event("user_before_delete", $this);
parent::delete($id);
+ module::event("user_deleted", $old);
}
/**