diff options
author | Bharat Mediratta <bharat@menalto.com> | 2009-05-16 03:29:14 +0000 |
---|---|---|
committer | Bharat Mediratta <bharat@menalto.com> | 2009-05-16 03:29:14 +0000 |
commit | 59db5456d42d3a300cce661a9b5f7da069c26893 (patch) | |
tree | b69a1a9e2a564a89dace935eb31b1626b2f9ad7c /core/models | |
parent | 9d272f29624db8a6513c609c7edfd57853ac5304 (diff) |
Implement Item_Model::rename(), with unit tests.
Diffstat (limited to 'core/models')
-rw-r--r-- | core/models/item.php | 28 |
1 files changed, 28 insertions, 0 deletions
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 @@ -155,6 +155,34 @@ class Item_Model extends ORM_MPTT { } /** + * 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") * |