From: Bartek Przybylski Date: Tue, 1 May 2012 08:06:40 +0000 (+0200) Subject: add/remove handlers X-Git-Tag: v4.0.0beta~130 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=11314c81d9a3977364058957d03a6126262b1bd9;p=nextcloud-server.git add/remove handlers --- diff --git a/apps/gallery/lib/hooks_handlers.php b/apps/gallery/lib/hooks_handlers.php index 2788337bbe8..178d8f3da15 100644 --- a/apps/gallery/lib/hooks_handlers.php +++ b/apps/gallery/lib/hooks_handlers.php @@ -21,8 +21,8 @@ * */ -//OC_Hook::connect(OC_Filesystem::CLASSNAME, OC_Filesystem::signal_post_write, "OC_Gallery_Hooks_Handlers", "addPhotoFromPath"); -//OC_Hook::connect(OC_Filesystem::CLASSNAME, OC_Filesystem::signal_delete, "OC_Gallery_Hooks_Handlers", "removePhoto"); +OC_Hook::connect(OC_Filesystem::CLASSNAME, OC_Filesystem::signal_post_write, "OC_Gallery_Hooks_Handlers", "addPhotoFromPath"); +OC_Hook::connect(OC_Filesystem::CLASSNAME, OC_Filesystem::signal_delete, "OC_Gallery_Hooks_Handlers", "removePhoto"); //OC_Hook::connect(OC_Filesystem::CLASSNAME, OC_Filesystem::signal_post_rename, "OC_Gallery_Hooks_Handlers", "renamePhoto"); require_once(OC::$CLASSPATH['OC_Gallery_Album']); @@ -65,22 +65,37 @@ class OC_Gallery_Hooks_Handlers { public static function addPhotoFromPath($params) { $fullpath = $params[OC_Filesystem::signal_param_path]; + $fullpath = rtrim(dirname($fullpath),'/').'/'.basename($fullpath); if (!self::isPhoto($fullpath)) return; - $path = dirname($fullpath); - if (!self::pathInRoot($path)) return; - OC_Gallery_Scanner::scanDir($path, $albums); - + $a = OC_Gallery_Album::find(OC_User::getUser(), null, dirname($fullpath)); + if (!($r = $a->fetchRow())) { + OC_Gallery_Album::create(OC_User::getUser(), basename(dirname($fullpath)), dirname($fullpath)); + $a = OC_Gallery_Album::find(OC_User::getUser(), null, dirname($fullpath)); + $r = $a->fetchRow(); + } + $albumId = $r['album_id']; + $p = OC_Gallery_Album::find($albumId, $fullpath); + if (!($p->fetchRow())) + OC_Gallery_Photo::create($albumId, $fullpath); } public static function removePhoto($params) { - $path = $params[OC_Filesystem::signal_param_path]; - if (OC_Filesystem::is_dir($path.'/') && self::directoryContainsPhotos($path)) { - if(!self::pathInRoot($path)) return; - OC_Gallery_Album::removeByPath($path, OC_User::getUser()); - } elseif (self::isPhoto($path)) { - OC_Gallery_Photo::removeByPath($path); + $fullpath = $params[OC_Filesystem::signal_param_path]; + $fullpath = rtrim(dirname($fullpath),'/').'/'.basename($fullpath); + + if (OC_Filesystem::is_dir($fullpath)) { + OC_Gallery_Album::remove(OC_User::getUser(), null, $fullpath); + } elseif (self::isPhoto($fullpath)) { + $a = OC_Gallery_Album::find(OC_User::getUser(), null, rtrim(dirname($fullpath),'/')); + if (($r = $a->fetchRow())) { + OC_Gallery_Photo::removeByPath($fullpath, $r['album_id']); + $p = OC_Gallery_Photo::findForAlbum(OC_User::getUser(), $r['album_name']); + if (!($p->fetchRow())) { + OC_Gallery_Album::remove(OC_User::getUser(), null, dirname($fullpath)); + } + } } } diff --git a/apps/gallery/lib/photo.php b/apps/gallery/lib/photo.php index 3bb6f9129fa..5098e1a2ed2 100644 --- a/apps/gallery/lib/photo.php +++ b/apps/gallery/lib/photo.php @@ -46,9 +46,9 @@ 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 LIKE ?'); - $stmt->execute(array($path)); + public static function removeByPath($path, $album_id) { + $stmt = OC_DB::prepare('DELETE FROM *PREFIX*gallery_photos WHERE file_path LIKE ? and album_id = ?'); + $stmt->execute(array($path, $album_id)); } public static function removeById($id) {