diff --git a/apps/gallery/ajax/galleryOp.php b/apps/gallery/ajax/galleryOp.php index 0224977e63..a62f0fe3f5 100644 --- a/apps/gallery/ajax/galleryOp.php +++ b/apps/gallery/ajax/galleryOp.php @@ -29,6 +29,7 @@ OC_JSON::checkAppEnabled('gallery'); function handleRename($oldname, $newname) { OC_JSON::checkLoggedIn(); OC_Gallery_Album::rename($oldname, $newname, OC_User::getUser()); + OC_Gallery_Album::changeThumbnailPath($oldname, $newname); } function handleRemove($name) { diff --git a/apps/gallery/js/album_cover.js b/apps/gallery/js/album_cover.js index 84a89c5a91..0009eb04e4 100644 --- a/apps/gallery/js/album_cover.js +++ b/apps/gallery/js/album_cover.js @@ -58,7 +58,7 @@ function galleryRemove(albumName) { } function galleryRename(name) { - var result = window.prompt("Input new gallery name", ""); + var result = window.prompt("Input new gallery name", name); if (result) { if (Albums.find(result)) { alert("Album named '" + result + "' already exists"); diff --git a/apps/gallery/lib/album.php b/apps/gallery/lib/album.php index a94eff3acd..a708ba83ea 100644 --- a/apps/gallery/lib/album.php +++ b/apps/gallery/lib/album.php @@ -58,6 +58,17 @@ class OC_Gallery_Album { return $stmt->execute($args); } + public static function changePath($oldname, $newname, $owner) { + $stmt = OC_DB::prepare('UPDATE OR IGNORE *PREFIX*gallery_albums SET album_path=? WHERE uid_owner=? AND album_path=?'); + $stmt->execute(array($newname, $owner, $oldname)); + } + + public static function changeThumbnailPath($oldname, $newname) { + require_once('../../../lib/base.php'); + $thumbpath = OC::$CONFIG_DATADIRECTORY.'/../gallery/'; + rename($thumbpath.$oldname.'.png', $thumbpath.$newname.'.png'); + } + } ?> diff --git a/apps/gallery/lib/hooks_handlers.php b/apps/gallery/lib/hooks_handlers.php index 65f3faaeea..ee17c0df7d 100644 --- a/apps/gallery/lib/hooks_handlers.php +++ b/apps/gallery/lib/hooks_handlers.php @@ -34,8 +34,19 @@ class OC_Gallery_Hooks_Handlers { private static function isPhoto($filename) { OC_Log::write(self::$APP_TAG, "Checking file ".$filename." with mimetype ".OC_Filesystem::getMimeType($filename), OC_Log::DEBUG); if (substr(OC_Filesystem::getMimeType($filename), 0, 6) == "image/") - return 1; - return 0; + return true; + return false; + } + + private static function directoryContainsPhotos($dirpath) { + $dirhandle = opendir(OC::$CONFIG_DATADIRECTORY.$dirpath); + if ($dirhandle != FALSE) { + while (($filename = readdir($dirhandle)) != FALSE) { + if ($filename[0] == '.') continue; + if (self::isPhoto($dirpath.'/'.$filename)) return true; + } + } + return false; } private static function createAlbum($path) { @@ -77,41 +88,45 @@ class OC_Gallery_Hooks_Handlers { } public static function renamePhoto($params) { - $olddir = substr($params['oldpath'], 0, strrpos($params['oldpath'], '/')); - $newdir = substr($params['newpath'], 0, strrpos($params['newpath'], '/')); - if ($olddir == '') $olddir = '/'; - if ($newdir == '') $newdir = '/'; - if (!self::isPhoto($params['newpath'])) return; - OC_Log::write(self::$APP_TAG, 'Moving photo from '.$params['oldpath'].' to '.$params['newpath'], OC_Log::DEBUG); - $album; - $newAlbumId; - $oldAlbumId; - if ($olddir == $newdir) { - // album changing is not needed - $album = OC_Gallery_Album::find(OC_User::getUser(), null, $olddir); - if ($album->numRows() == 0) { - $album = self::createAlbum($newdir); - } - $album = $album->fetchRow(); - $newAlbumId = $oldAlbumId = $album['album_id']; - } else { - $newalbum = OC_Gallery_Album::find(OC_User::getUser(), null, $newdir); - $oldalbum = OC_Gallery_Album::find(OC_User::getUser(), null, $olddir); + if (OC_Filesystem::is_dir($params['newpath']) && self::directoryContainsPhotos($params['newpath'])) { + OC_Gallery_Album::changePath($params['oldpath'], $params['newpath'], OC_User::getUser()); + } elseif (!self::isPhoto($params['newpath'])) { + $olddir = substr($params['oldpath'], 0, strrpos($params['oldpath'], '/')); + $newdir = substr($params['newpath'], 0, strrpos($params['newpath'], '/')); + if ($olddir == '') $olddir = '/'; + if ($newdir == '') $newdir = '/'; + if (!self::isPhoto($params['newpath'])) return; + OC_Log::write(self::$APP_TAG, 'Moving photo from '.$params['oldpath'].' to '.$params['newpath'], OC_Log::DEBUG); + $album; + $newAlbumId; + $oldAlbumId; + if ($olddir == $newdir) { + // album changing is not needed + $album = OC_Gallery_Album::find(OC_User::getUser(), null, $olddir); + if ($album->numRows() == 0) { + $album = self::createAlbum($newdir); + } + $album = $album->fetchRow(); + $newAlbumId = $oldAlbumId = $album['album_id']; + } else { + $newalbum = OC_Gallery_Album::find(OC_User::getUser(), null, $newdir); + $oldalbum = OC_Gallery_Album::find(OC_User::getUser(), null, $olddir); - if ($newalbum->numRows() == 0) { - $newalbum = self::createAlbum($newdir); - } - $newalbum = $newalbum->fetchRow(); - if ($oldalbum->numRows() == 0) { - OC_Gallery_Photo::create($newalbum['album_id'], $params['newpath']); - return; - } - $oldalbum = $oldalbum->fetchRow(); - $newAlbumId = $newalbum['album_id']; - $oldAlbumId = $oldalbum['album_id']; + if ($newalbum->numRows() == 0) { + $newalbum = self::createAlbum($newdir); + } + $newalbum = $newalbum->fetchRow(); + if ($oldalbum->numRows() == 0) { + OC_Gallery_Photo::create($newalbum['album_id'], $params['newpath']); + return; + } + $oldalbum = $oldalbum->fetchRow(); + $newAlbumId = $newalbum['album_id']; + $oldAlbumId = $oldalbum['album_id']; + } + OC_Gallery_Photo::changePath($oldAlbumId, $newAlbumId, $params['oldpath'], $params['newpath']); } - OC_Gallery_Photo::changePath($oldAlbumId, $newAlbumId, $params['oldpath'], $params['newpath']); } }