From 59db5456d42d3a300cce661a9b5f7da069c26893 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sat, 16 May 2009 03:29:14 +0000 Subject: Implement Item_Model::rename(), with unit tests. --- core/models/item.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'core/models') diff --git a/core/models/item.php b/core/models/item.php index 5b989b0c..2d53ca0b 100644 --- a/core/models/item.php +++ b/core/models/item.php @@ -154,6 +154,34 @@ class Item_Model extends ORM_MPTT { return $this; } + /** + * Rename the underlying file for this item to a new name. Move all the files. This requires a + * save. + * + * @chainable + */ + public function rename($new_name) { + if ($new_name == $this->name) { + return; + } + + $old_relative_path = $this->relative_path(); + $new_relative_path = dirname($old_relative_path) . "/" . $new_name; + @rename(VARPATH . "albums/$old_relative_path", VARPATH . "albums/$new_relative_path"); + @rename(VARPATH . "resizes/$old_relative_path", VARPATH . "resizes/$new_relative_path"); + @rename(VARPATH . "thumbs/$old_relative_path", VARPATH . "thumbs/$new_relative_path"); + $this->name = $new_name; + + if ($this->is_album()) { + Database::instance() + ->update("items", + array("relative_path_cache" => null), + array("left >" => $this->left, "right <" => $this->right)); + } + + return $this; + } + /** * album: url::site("albums/2") * photo: url::site("photos/3") -- cgit v1.2.3