summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorBartek Przybylski <bart.p.pl@gmail.com>2012-01-08 01:32:11 +0100
committerBartek Przybylski <bart.p.pl@gmail.com>2012-01-08 01:32:11 +0100
commit2de3b4bd1738cfce48d7c003316e13e988819898 (patch)
tree3df1580b58ba552913770b3a63820051ac655476 /apps
parente51957b4ded8f538180e55581f4c757d2688aaa5 (diff)
downloadnextcloud-server-2de3b4bd1738cfce48d7c003316e13e988819898.tar.gz
nextcloud-server-2de3b4bd1738cfce48d7c003316e13e988819898.zip
rename and delete hooks
Diffstat (limited to 'apps')
-rw-r--r--apps/gallery/lib/hooks_handlers.php65
-rw-r--r--apps/gallery/lib/photo.php15
2 files changed, 74 insertions, 6 deletions
diff --git a/apps/gallery/lib/hooks_handlers.php b/apps/gallery/lib/hooks_handlers.php
index 5d1e7502aa3..1bddaf363e3 100644
--- a/apps/gallery/lib/hooks_handlers.php
+++ b/apps/gallery/lib/hooks_handlers.php
@@ -1,6 +1,8 @@
<?php
OC_Hook::connect("OC_Filesystem", "post_write", "OC_Gallery_Hooks_Handlers", "addPhotoFromPath");
+OC_Hook::connect("OC_Filesystem", "delete", "OC_Gallery_Hooks_Handlers", "removePhoto");
+OC_Hook::connect("OC_Filesystem", "post_rename", "OC_Gallery_Hooks_Handlers", "renamePhoto");
require_once(OC::$CLASSPATH['OC_Gallery_Album']);
require_once(OC::$CLASSPATH['OC_Gallery_Photo']);
@@ -14,23 +16,76 @@ class OC_Gallery_Hooks_Handlers {
return 0;
}
+ private static function createAlbum($path) {
+ $new_album_name = trim(str_replace('/', '.', $path), '.');
+ if ($new_album_name == '') $new_album_name = 'main';
+
+ OC_Log::write(self::$APP_TAG, 'Creating new album '.$new_album_name, OC_Log::DEBUG);
+ OC_Gallery_Album::create(OC_User::getUser(), $new_album_name, $path);
+
+ return OC_Gallery_Album::find(OC_User::getUser(), null, $path);
+ }
+
public static function addPhotoFromPath($params) {
if (!self::isPhoto($params['path'])) return;
$fullpath = $params['path'];
OC_Log::write(self::$APP_TAG, 'Adding file with path '. $fullpath, OC_Log::DEBUG);
$path = substr($fullpath, 0, strrpos($fullpath, '/'));
$album = OC_Gallery_Album::find(OC_User::getUser(), null, $path);
+
if ($album->numRows() == 0) {
- $new_album_name = trim(str_replace('/', '.', $fullpath));
- if ($new_album_name == '.') $new_album_name = 'main';
- OC_Gallery_Album::create(OC_User::getUser(), $new_album_name, $path);
- $album = OC_Gallery_Album::find(OC_User::getUser(), null, $path);
+ $album = self::createAlbum($path);
}
$album = $album->fetchRow();
$albumId = $album['album_id'];
- OC_Gallery_Photo::create($albumId, $fullpath);
+ $photo = OC_Gallery_Photo::find($albumId, $fullpath);
+ if ($photo->numRows() == 0) { // don't duplicate photo entries
+ OC_Log::write(self::$APP_TAG, 'Adding new photo to album', OC_Log::DEBUG);
+ OC_Gallery_Photo::create($albumId, $fullpath);
+ }
}
+
+ public static function removePhoto($params) {
+ $path = $params['path'];
+ if (!self::isPhoto($path)) return;
+ OC_Gallery_Photo::removeByPath($path);
+ }
+
+ public static function renamePhoto($params) {
+ $olddir = substr($params['oldpath'], 0, strrpos($params['oldpath'], '/'));
+ $newdir = substr($params['newpath'], 0, strrpos($params['newpath'], '/'));
+ if (!self::isPhoto($params['newpath'])) return;
+ $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']);
+ }
}
?>
diff --git a/apps/gallery/lib/photo.php b/apps/gallery/lib/photo.php
index a89a56981f5..f8a640819a1 100644
--- a/apps/gallery/lib/photo.php
+++ b/apps/gallery/lib/photo.php
@@ -8,7 +8,6 @@ class OC_Gallery_Photo{
public static function find($albumId, $img=null){
$sql = 'SELECT * FROM *PREFIX*gallery_photos WHERE album_id = ?';
$args = array($albumId);
- $args = array($albumId);
if (!is_null($img)){
$sql .= ' AND file_path = ?';
$args[] = $img;
@@ -26,5 +25,19 @@ class OC_Gallery_Photo{
return $stmt->execute(array($owner, $album_name));
}
+ public static function removeByPath($path) {
+ $stmt = OC_DB::prepare('DELETE FROM *PREFIX*gallery_photos WHERE file_path = ?');
+ $stmt->execute(array($path));
+ }
+
+ public static function removeById($id) {
+ $stmt = OC_DB::prepare('DELETE FROM *PREFIX*gallery_photos WHERE photo_id = ?');
+ $stmt->execute(array($id));
+ }
+
+ public static function changePath($oldAlbumId, $newAlbumId, $oldpath, $newpath) {
+ $stmt = OC_DB::prepare("UPDATE *PREFIX*gallery_photos SET file_path = ?, album_id = ? WHERE album_id = ? and file_path = ?");
+ $stmt->execute(array($newpath, $newAlbumId, $oldAlbumId, $oldpath));
+ }
}