summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2009-07-16 12:29:16 -0700
committerBharat Mediratta <bharat@menalto.com>2009-07-16 12:31:40 -0700
commit0f766b149d0cee7af664f2321fddc6f04cda70ac (patch)
tree51d8cd4e26df82955d57eefaa46349568d054126 /modules
parent43324fd12a23b35707300ff110f207552c3811f1 (diff)
Second non-trivial change to the event code. We now publish model
related events from within the model handling code. The only exception to this currently is item_created which is challenging because we have to save the item using ORM_MPTT::add_to_parent() before the object itself is fully set up. When we get that down to one call to save() we can publish that event from within the model also.
Diffstat (limited to 'modules')
-rw-r--r--modules/comment/controllers/admin_comments.php4
-rw-r--r--modules/comment/controllers/comments.php1
-rw-r--r--modules/comment/helpers/comment.php5
-rw-r--r--modules/comment/models/comment.php17
-rw-r--r--modules/exif/helpers/exif_event.php4
-rw-r--r--modules/gallery/controllers/albums.php3
-rw-r--r--modules/gallery/controllers/movies.php3
-rw-r--r--modules/gallery/controllers/photos.php3
-rw-r--r--modules/gallery/helpers/album.php2
-rw-r--r--modules/gallery/helpers/movie.php2
-rw-r--r--modules/gallery/helpers/photo.php2
-rw-r--r--modules/gallery/models/item.php7
-rw-r--r--modules/organize/controllers/organize.php4
-rw-r--r--modules/user/helpers/group.php1
-rw-r--r--modules/user/helpers/user.php1
-rw-r--r--modules/user/models/group.php13
-rw-r--r--modules/user/models/user.php13
17 files changed, 57 insertions, 28 deletions
diff --git a/modules/comment/controllers/admin_comments.php b/modules/comment/controllers/admin_comments.php
index ea76b188..a164f79f 100644
--- a/modules/comment/controllers/admin_comments.php
+++ b/modules/comment/controllers/admin_comments.php
@@ -113,10 +113,6 @@ class Admin_Comments_Controller extends Admin_Controller {
if ($comment->loaded) {
$comment->state = $state;
$comment->save();
- 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/controllers/comments.php b/modules/comment/controllers/comments.php
index 02c38491..9fb4796e 100644
--- a/modules/comment/controllers/comments.php
+++ b/modules/comment/controllers/comments.php
@@ -152,7 +152,6 @@ class Comments_Controller extends REST_Controller {
$comment->url = $form->edit_comment->url->value;
$comment->text = $form->edit_comment->text->value;
$comment->save();
- module::event("comment_updated", $comment);
print json_encode(
array("result" => "success",
diff --git a/modules/comment/helpers/comment.php b/modules/comment/helpers/comment.php
index 08cba096..3d743325 100644
--- a/modules/comment/helpers/comment.php
+++ b/modules/comment/helpers/comment.php
@@ -61,11 +61,6 @@ class comment_Core {
$comment->server_remote_port = substr($input->server("REMOTE_PORT"), 0, 16);
$comment->save();
- module::event("comment_created", $comment);
- if ($comment->state == "published") {
- module::event("item_related_update", $comment->item());
- }
-
return $comment;
}
diff --git a/modules/comment/models/comment.php b/modules/comment/models/comment.php
index 22c465df..551fb245 100644
--- a/modules/comment/models/comment.php
+++ b/modules/comment/models/comment.php
@@ -61,8 +61,23 @@ class Comment_Model extends ORM {
$this->updated = time();
if (!$this->loaded && empty($this->created)) {
$this->created = $this->updated;
+ $created = true;
}
}
- return parent::save();
+ parent::save();
+
+ if (isset($created)) {
+ module::event("comment_created", $this);
+ } else {
+ module::event("comment_updated", $this);
+ }
+
+ // We only notify on the related items if we're making a visible change, which means moving in
+ // or out of a published state
+ if ($this->original("state") == "published" || $this->state == "published") {
+ module::event("item_related_update", $this->item());
+ }
+
+ return $this;
}
}
diff --git a/modules/exif/helpers/exif_event.php b/modules/exif/helpers/exif_event.php
index 24243f4d..826ec959 100644
--- a/modules/exif/helpers/exif_event.php
+++ b/modules/exif/helpers/exif_event.php
@@ -19,7 +19,9 @@
*/
class exif_event_Core {
static function item_created($item) {
- exif::extract($item);
+ if (!$item->is_album()) {
+ exif::extract($item);
+ }
}
static function item_deleted($item) {
diff --git a/modules/gallery/controllers/albums.php b/modules/gallery/controllers/albums.php
index c378e3ce..9980b676 100644
--- a/modules/gallery/controllers/albums.php
+++ b/modules/gallery/controllers/albums.php
@@ -182,7 +182,6 @@ class Albums_Controller extends Items_Controller {
}
if ($valid) {
- $orig = clone $album;
$album->title = $form->edit_album->title->value;
$album->description = $form->edit_album->description->value;
$album->sort_column = $form->edit_album->sort_order->column->value;
@@ -192,8 +191,6 @@ class Albums_Controller extends Items_Controller {
}
$album->save();
- module::event("item_updated", $album);
-
log::success("content", "Updated album", "<a href=\"albums/$album->id\">view</a>");
message::success(
t("Saved album %album_title", array("album_title" => p::clean($album->title))));
diff --git a/modules/gallery/controllers/movies.php b/modules/gallery/controllers/movies.php
index fc511082..d954ad8d 100644
--- a/modules/gallery/controllers/movies.php
+++ b/modules/gallery/controllers/movies.php
@@ -85,14 +85,11 @@ class Movies_Controller extends Items_Controller {
}
if ($valid) {
- $orig = clone $photo;
$photo->title = $form->edit_photo->title->value;
$photo->description = $form->edit_photo->description->value;
$photo->rename($form->edit_photo->filename->value);
$photo->save();
- module::event("item_updated", $photo);
-
log::success("content", "Updated photo", "<a href=\"photos/$photo->id\">view</a>");
message::success(
t("Saved photo %photo_title", array("photo_title" => p::clean($photo->title))));
diff --git a/modules/gallery/controllers/photos.php b/modules/gallery/controllers/photos.php
index 77627009..9ce6ed23 100644
--- a/modules/gallery/controllers/photos.php
+++ b/modules/gallery/controllers/photos.php
@@ -78,14 +78,11 @@ class Photos_Controller extends Items_Controller {
}
if ($valid) {
- $orig = clone $photo;
$photo->title = $form->edit_photo->title->value;
$photo->description = $form->edit_photo->description->value;
$photo->rename($form->edit_photo->filename->value);
$photo->save();
- module::event("item_updated", $photo);
-
log::success("content", "Updated photo", "<a href=\"photos/$photo->id\">view</a>");
message::success(
t("Saved photo %photo_title", array("photo_title" => p::clean($photo->title))));
diff --git a/modules/gallery/helpers/album.php b/modules/gallery/helpers/album.php
index 1197f243..f1a6c060 100644
--- a/modules/gallery/helpers/album.php
+++ b/modules/gallery/helpers/album.php
@@ -71,6 +71,8 @@ class album_Core {
mkdir(dirname($album->thumb_path()));
mkdir(dirname($album->resize_path()));
+ // @todo: publish this from inside Item_Model::save() when we refactor to the point where
+ // there's only one save() happening here.
module::event("item_created", $album);
return $album;
diff --git a/modules/gallery/helpers/movie.php b/modules/gallery/helpers/movie.php
index d62ead76..4f4169d5 100644
--- a/modules/gallery/helpers/movie.php
+++ b/modules/gallery/helpers/movie.php
@@ -102,6 +102,8 @@ class movie_Core {
copy($filename, $movie->file_path());
+ // @todo: publish this from inside Item_Model::save() when we refactor to the point where
+ // there's only one save() happening here.
module::event("item_created", $movie);
// Build our thumbnail
diff --git a/modules/gallery/helpers/photo.php b/modules/gallery/helpers/photo.php
index e8a4f357..ce964c14 100644
--- a/modules/gallery/helpers/photo.php
+++ b/modules/gallery/helpers/photo.php
@@ -105,6 +105,8 @@ class photo_Core {
copy($filename, $photo->file_path());
+ // @todo: publish this from inside Item_Model::save() when we refactor to the point where
+ // there's only one save() happening here.
module::event("item_created", $photo);
// Build our thumbnail/resizes
diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php
index 80f19d26..94e2fcf7 100644
--- a/modules/gallery/models/item.php
+++ b/modules/gallery/models/item.php
@@ -350,9 +350,14 @@ class Item_Model extends ORM_MPTT {
$this->created = $this->updated;
$r = ORM::factory("item")->select("MAX(weight) as max_weight")->find();
$this->weight = $r->max_weight + 1;
+ $created = 1;
}
}
- return parent::save();
+ parent::save();
+ if (!isset($created)) {
+ module::event("item_updated", $this);
+ }
+ return $this;
}
/**
diff --git a/modules/organize/controllers/organize.php b/modules/organize/controllers/organize.php
index 54e04071..27852904 100644
--- a/modules/organize/controllers/organize.php
+++ b/modules/organize/controllers/organize.php
@@ -279,8 +279,6 @@ class Organize_Controller extends Controller {
$item->rename($form->dirname->value);
$item->save();
- module::event("item_updated", $item);
-
if ($item->is_album()) {
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)));
@@ -322,8 +320,6 @@ class Organize_Controller extends Controller {
$item->sort_order = $form->direction->value;
$item->save();
- 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)));
print json_encode(array("form" => $form->__toString(), "message" => $message));
diff --git a/modules/user/helpers/group.php b/modules/user/helpers/group.php
index 1dace840..04e6efd6 100644
--- a/modules/user/helpers/group.php
+++ b/modules/user/helpers/group.php
@@ -39,7 +39,6 @@ class group_Core {
$group->name = $name;
$group->save();
- module::event("group_created", $group);
return $group;
}
diff --git a/modules/user/helpers/user.php b/modules/user/helpers/user.php
index a59588f8..4105d745 100644
--- a/modules/user/helpers/user.php
+++ b/modules/user/helpers/user.php
@@ -202,7 +202,6 @@ class user_Core {
$user->add(group::registered_users());
$user->save();
- module::event("user_created", $user);
return $user;
}
diff --git a/modules/user/models/group.php b/modules/user/models/group.php
index e0724e30..bb3fb58b 100644
--- a/modules/user/models/group.php
+++ b/modules/user/models/group.php
@@ -32,4 +32,17 @@ class Group_Model extends ORM {
parent::delete($id);
module::event("group_deleted", $old);
}
+
+ public function save() {
+ if (!$this->loaded) {
+ $created = 1;
+ }
+ parent::save();
+ if (isset($created)) {
+ module::event("group_created", $this);
+ } else {
+ module::event("group_updated", $this);
+ }
+ return $this;
+ }
} \ No newline at end of file
diff --git a/modules/user/models/user.php b/modules/user/models/user.php
index e3260270..0234f186 100644
--- a/modules/user/models/user.php
+++ b/modules/user/models/user.php
@@ -59,4 +59,17 @@ class User_Model extends ORM {
return sprintf("http://www.gravatar.com/avatar/%s.jpg?s=%d&r=pg%s",
md5($this->email), $size, $default ? "&d=" . urlencode($default) : "");
}
+
+ public function save() {
+ if (!$this->loaded) {
+ $created = 1;
+ }
+ parent::save();
+ if (isset($created)) {
+ module::event("user_created", $this);
+ } else {
+ module::event("user_updated", $this);
+ }
+ return $this;
+ }
} \ No newline at end of file