summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2010-01-18 20:13:46 -0800
committerBharat Mediratta <bharat@menalto.com>2010-01-18 20:13:46 -0800
commit0e2f4a7a372567ba1916e2533ce24ee6e1cc7d36 (patch)
tree0132649ae5971f19ef52670f8adfd509d8a25089 /modules
parentb93000c0ce6fc2fd90df0e4a3e427d15cb262117 (diff)
Fix renaming in save() by moving the actual rename action under
parent::save(). This is consistent with other changes because all filesystem operations happen after the database change is committed. Also, inline rename() since it's fairly simple now.
Diffstat (limited to 'modules')
-rw-r--r--modules/gallery/models/item.php73
1 files changed, 38 insertions, 35 deletions
diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php
index e04b1314..b3f749eb 100644
--- a/modules/gallery/models/item.php
+++ b/modules/gallery/models/item.php
@@ -140,33 +140,6 @@ class Item_Model extends ORM_MPTT {
}
/**
- * Rename the underlying file for this item to a new name and move all related files.
- *
- * @chainable
- */
- private function rename($new_name) {
- $old_relative_path = urldecode($this->original()->relative_path());
- $new_relative_path = dirname($old_relative_path) . "/" . $new_name;
- if (file_exists(VARPATH . "albums/$new_relative_path")) {
- throw new Exception("@todo INVALID_RENAME_FILE_EXISTS: $new_relative_path");
- }
-
- @rename(VARPATH . "albums/$old_relative_path", VARPATH . "albums/$new_relative_path");
- @rename(VARPATH . "resizes/$old_relative_path", VARPATH . "resizes/$new_relative_path");
- if ($this->is_movie()) {
- // Movie thumbnails have a .jpg extension
- $old_relative_thumb_path = preg_replace("/...$/", "jpg", $old_relative_path);
- $new_relative_thumb_path = preg_replace("/...$/", "jpg", $new_relative_path);
- @rename(VARPATH . "thumbs/$old_relative_thumb_path",
- VARPATH . "thumbs/$new_relative_thumb_path");
- } else {
- @rename(VARPATH . "thumbs/$old_relative_path", VARPATH . "thumbs/$new_relative_path");
- }
-
- return $this;
- }
-
- /**
* Specify the path to the data file associated with this item. To actually associate it,
* you still have to call save().
* @chainable
@@ -482,20 +455,35 @@ class Item_Model extends ORM_MPTT {
module::event("item_created", $this);
} else {
// Update an existing item
- if ($this->original()->name != $this->name) {
- $this->rename($this->name);
+
+ // The new values have to be valid before we do anything with them. If we make any
+ // other changes before we call parent::save() below, we'll have to validate those changes
+ // again. But, we can't take any action on these values until we know they're ok so this
+ // is unavoidable.
+ if (!$this->_valid) {
+ $this->validate();
+ }
+
+ $original = clone $this->original();
+
+ if ($original->name != $this->name) {
+ // Get the old relative path for when we rename below
+ if (!isset($this->relative_path_cache)) {
+ $this->_build_relative_caches(); // but don't call save()
+ }
+ $old_relative_path = $this->relative_path_cache;
$this->relative_path_cache = null;
}
- if ($this->original()->slug != $this->slug) {
- // Clear the relative url cache for this item and all children
+ if ($original->slug != $this->slug) {
$this->relative_url_cache = null;
}
+ parent::save();
+
// Changing the name or the slug ripples downwards
if ($this->is_album() &&
- ($this->original()->name != $this->name ||
- $this->original()->slug != $this->slug)) {
+ ($original->name != $this->name || $original->slug != $this->slug)) {
db::build()
->update("items")
->set("relative_url_cache", null)
@@ -504,8 +492,23 @@ class Item_Model extends ORM_MPTT {
->where("right_ptr", "<", $this->right_ptr)
->execute();
}
- $original = clone $this->original();
- parent::save();
+
+ // If we renamed this item, move all of its associated data files.
+ if ($original->name != $this->name) {
+ $relative_path = urldecode($this->relative_path());
+ @rename(VARPATH . "albums/$old_relative_path", VARPATH . "albums/$relative_path");
+ @rename(VARPATH . "resizes/$old_relative_path", VARPATH . "resizes/$relative_path");
+ if ($this->is_movie()) {
+ // Movie thumbnails have a .jpg extension
+ $old_relative_thumb_path = preg_replace("/...$/", "jpg", $old_relative_path);
+ $relative_thumb_path = preg_replace("/...$/", "jpg", $relative_path);
+ @rename(VARPATH . "thumbs/$old_relative_thumb_path",
+ VARPATH . "thumbs/$relative_thumb_path");
+ } else {
+ @rename(VARPATH . "thumbs/$old_relative_path", VARPATH . "thumbs/$relative_path");
+ }
+ }
+
module::event("item_updated", $original, $this);
}
} else if (!empty($this->changed)) {