]> source.dussan.org Git - nextcloud-server.git/commitdiff
handling directory renaming and thumbnail move on gallery name chenge
authorBartek Przybylski <bart.p.pl@gmail.com>
Sat, 14 Jan 2012 12:52:24 +0000 (13:52 +0100)
committerBartek Przybylski <bart.p.pl@gmail.com>
Sat, 14 Jan 2012 12:52:24 +0000 (13:52 +0100)
apps/gallery/ajax/galleryOp.php
apps/gallery/js/album_cover.js
apps/gallery/lib/album.php
apps/gallery/lib/hooks_handlers.php

index 0224977e633a8128e5b4b983b97f5e0095c02b47..a62f0fe3f5bae7d71fd21b03c4abf9adbb7c80a4 100644 (file)
@@ -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) {
index 84a89c5a91f69c5b8b508781751dd5ef73986acc..0009eb04e4f19bf14a4f301fe73aef0d1b7e411e 100644 (file)
@@ -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");
index a94eff3acd7864abf4907883cd6a2b94672e7616..a708ba83ea4f92aec859c2c949270242d984f93f 100644 (file)
@@ -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');
+  }
+
 }
 
 ?>
index 65f3faaeeaf3f57559289a9797c2d1e46b0b0e1c..ee17c0df7dfc81ed3e0c14e1a2f84aea4a411030 100644 (file)
@@ -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 ($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 (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'];
 
+      }
+      OC_Gallery_Photo::changePath($oldAlbumId, $newAlbumId, $params['oldpath'], $params['newpath']);
     }
-    OC_Gallery_Photo::changePath($oldAlbumId, $newAlbumId, $params['oldpath'], $params['newpath']);
   }
 }