]> source.dussan.org Git - nextcloud-server.git/commitdiff
Gallery: splitout model code
authorBart Visscher <bartv@thisnet.nl>
Thu, 8 Dec 2011 19:04:56 +0000 (20:04 +0100)
committerBart Visscher <bartv@thisnet.nl>
Thu, 8 Dec 2011 19:05:12 +0000 (20:05 +0100)
14 files changed:
apps/gallery/ajax/createAlbum.php
apps/gallery/ajax/getAlbums.php
apps/gallery/ajax/getCovers.php
apps/gallery/ajax/scanForAlbums.php
apps/gallery/ajax/thumbnail.php
apps/gallery/appinfo/app.php
apps/gallery/css/styles.css
apps/gallery/index.php
apps/gallery/lib/album.php [new file with mode: 0644]
apps/gallery/lib/photo.php [new file with mode: 0644]
apps/gallery/lib/scanner.php [new file with mode: 0644]
apps/gallery/lib_scanner.php [deleted file]
apps/gallery/templates/view_album.php
lib/db.php

index 610f761b72ac0d8f967fcf19ba1f6f4f353be1b3..9413b54718abf46a9393ce957e6740182bb0b067 100644 (file)
@@ -3,8 +3,7 @@ require_once('../../../lib/base.php');
 OC_JSON::checkLoggedIn();
 OC_JSON::checkAppEnabled('gallery');
 
-$stmt = OC_DB::prepare('INSERT INTO *PREFIX*gallery_albums ("uid_owner", "album_name") VALUES ("'.OC_User::getUser().'", "'.$_GET['album_name'].'")');
-$stmt->execute(array());
+OC_Gallery_Album::create(OC_User::getUser(), $_GET['album_name']);
 
 OC_JSON::success(array('name' => $_GET['album_name']));
 
index 38bea74636fb0e0604e2ac624fbf61324d579ffa..856f29344d71e07c2bd11791bffbdab6a42df7cc 100644 (file)
@@ -4,13 +4,11 @@ OC_JSON::checkLoggedIn();
 OC_JSON::checkAppEnabled('gallery');
 
 $a = array();
-$stmt = OC_DB::prepare('SELECT * FROM *PREFIX*gallery_albums WHERE `uid_owner` = ?');
-$result = $stmt->execute(array(OC_User::getUser()));
+$result = OC_Gallery_Album::find(OC_User::getUser());
 
 while ($r = $result->fetchRow()) {
   $album_name = $r['album_name'];
-  $stmt = OC_DB::prepare('SELECT * FROM *PREFIX*gallery_photos WHERE `album_id` = ?');
-  $tmp_res = $stmt->execute(array($r['album_id']));
+  $tmp_res = OC_Gallery_Photo::find($r['album_id']);
   $a[] = array('name' => $album_name, 'numOfItems' => min($tmp_res->numRows(), 10));
 }
 
index b9c7558a53ce8966261d9821fd6eb09265f33966..db7c8e9fcdec977aff344fe630741e46a0dc7f8d 100644 (file)
@@ -18,7 +18,7 @@ function CroppedThumbnail($imgSrc,$thumbnail_width,$thumbnail_height, $tgtImg, $
       default:
         exit();
     }
-               if(!$myImage) exit();
+    if(!$myImage) exit();
     $ratio_orig = $width_orig/$height_orig;
     
     if ($thumbnail_width/$thumbnail_height > $ratio_orig) {
@@ -44,15 +44,19 @@ function CroppedThumbnail($imgSrc,$thumbnail_width,$thumbnail_height, $tgtImg, $
 $box_size = 200;
 $album_name= $_GET['album_name'];
 
-$stmt = OC_DB::prepare('SELECT `file_path` FROM *PREFIX*gallery_photos,*PREFIX*gallery_albums WHERE *PREFIX*gallery_albums.`uid_owner` = ? AND `album_name` = ? AND *PREFIX*gallery_photos.`album_id` = *PREFIX*gallery_albums.`album_id`');
-$result = $stmt->execute(array(OC_User::getUser(), $album_name));
+$result = OC_Gallery_Photo::findForAlbum(OC_User::getUser(), $album_name);
 
 $numOfItems = min($result->numRows(),10);
 
-$targetImg = imagecreatetruecolor($numOfItems*$box_size, $box_size);
+if ($numOfItems){
+       $targetImg = imagecreatetruecolor($numOfItems*$box_size, $box_size);
+}
+else{
+       $targetImg = imagecreatetruecolor($box_size, $box_size);
+}
 $counter = 0;
 while (($i = $result->fetchRow()) && $counter < $numOfItems) {
-  $imagePath = OC::$CONFIG_DATADIRECTORY . $i['file_path'];
+       $imagePath = OC_Filesystem::getLocalFile($i['file_path']);
        if(file_exists($imagePath))
        {
                CroppedThumbnail($imagePath, $box_size, $box_size, $targetImg, $counter*$box_size);
@@ -65,7 +69,7 @@ header('Content-Type: image/png');
 $offset = 3600 * 24;
 // calc the string in GMT not localtime and add the offset
 header("Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT");
-header('Cache-Control: max-age=3600, must-revalidate');
+header('Cache-Control: max-age='.$offset.', must-revalidate');
 header('Pragma: public');
 
 imagepng($targetImg);
index de0b141a36709c32acc27c9f04e95dcc023c6f96..ff696804b00630a0a4744228fc1fa7effddea979 100644 (file)
@@ -3,9 +3,8 @@
 require_once('../../../lib/base.php');
 OC_JSON::checkLoggedIn();
 OC_JSON::checkAppEnabled('gallery');
-require_once('../lib_scanner.php');
 
-OC_JSON::success(array('albums' => OC_GALLERY_SCANNER::scan('')));
+OC_JSON::success(array('albums' => OC_Gallery_Scanner::scan('')));
 //OC_JSON::success(array('albums' => array(array('name' => 'test', 'imagesCount' => 1, 'images' => array('dupa')))));
 
 ?>
index a1416452932d1bd95aec1e4cf41707241dfc8865..d937691fa036607b94b8b4a80d2213a5964d872b 100644 (file)
@@ -49,12 +49,19 @@ function CroppedThumbnail($imgSrc,$thumbnail_width,$thumbnail_height) { //$imgSr
 $box_size = 200;
 $img = $_GET['img'];
 
-$tmp = OC::$CONFIG_DATADIRECTORY . $img;
+$imagePath = OC_Filesystem::getLocalFile($img);
 
-if(file_exists($tmp))
+if(file_exists($imagePath))
 {
-  header('Content-Type: image/png');
-       $image = CroppedThumbnail($tmp, $box_size, $box_size);
+       $image = CroppedThumbnail($imagePath, $box_size, $box_size);
+
+       header('Content-Type: image/png');
+       $offset = 3600 * 24;
+       // calc the string in GMT not localtime and add the offset
+       header("Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT");
+       header('Cache-Control: max-age='.$offset.', must-revalidate');
+       header('Pragma: public');
+
        imagepng($image);
        imagedestroy($image);
-}
\ No newline at end of file
+}
index 8f855c470e579a9a763c3e0053e3fa1e3c6b115f..2b1ab857afcdacbe24dc0ef5f8d75582c0966160 100644 (file)
@@ -1,4 +1,8 @@
 <?php
+OC::$CLASSPATH['OC_Gallery_Album'] = 'apps/gallery/lib/album.php';
+OC::$CLASSPATH['OC_Gallery_Photo'] = 'apps/gallery/lib/photo.php';
+OC::$CLASSPATH['OC_Gallery_Scanner'] = 'apps/gallery/lib/scanner.php';
+
 OC_App::register(array(
   'order' => 20,
   'id' => 'gallery',
index 03b179138e655ffc14a10811fc86d533915a52b3..070effe3a9259ad2307e6cb22985826c435672e0 100644 (file)
@@ -1,14 +1,22 @@
 div#gallery_list {
   margin: 90pt 20pt;
 }
+div#gallery_list.leftcontent {
+  padding-top: 15px;
+  margin: 0;
+  text-align: center;
+}
 
 div#gallery_album_box {
   width: 200px;
   text-align: center;
   border: 0;
-  float: left;
+  display: inline-block;
   margin: 5pt;
 }
+.leftcontent div#gallery_album_box {
+  margin: 5px;
+}
 
 div#gallery_album_box h1 {
   font-size: 12pt;
@@ -21,3 +29,6 @@ div#gallery_album_cover {
   border: solid 1px black;
 }
 
+#gallery_images {
+padding:10px 5px;
+}
index 87fdafcf13c0a2f0341e3d2f11679e6186b2bfce..2c409089ebe5b1511a9cd8cb19320e79a6f1136b 100644 (file)
@@ -7,8 +7,7 @@ OC_App::setActiveNavigationEntry( 'gallery_index' );
 
 
 if (!isset($_GET['view'])) {
-  $stmt = OC_DB::prepare('SELECT * FROM *PREFIX*gallery_albums WHERE uid_owner = ?');
-  $result = $stmt->execute(array(OC_User::getUser()));
+  $result = OC_Gallery_Album::find(OC_User::getUser());
 
   $r = array();
   while ($row = $result->fetchRow())
@@ -18,9 +17,7 @@ if (!isset($_GET['view'])) {
   $tmpl->assign('r', $r);
   $tmpl->printPage();
 } else {
-  $stmt = OC_DB::prepare('SELECT * FROM *PREFIX*gallery_photos, *PREFIX*gallery_albums WHERE uid_owner = ? AND album_name = ? AND *PREFIX*gallery_albums.album_id = *PREFIX*gallery_photos.album_id');
-  
-  $result = $stmt->execute(array(OC_User::getUser(), $_GET['view']));
+  $result = OC_Gallery_Photo::findForAlbum(OC_User::getUser(), $_GET['view']);
 
   $photos = array();
   while ($p = $result->fetchRow())
diff --git a/apps/gallery/lib/album.php b/apps/gallery/lib/album.php
new file mode 100644 (file)
index 0000000..6ddfe46
--- /dev/null
@@ -0,0 +1,18 @@
+<?php
+
+class OC_Gallery_Album{
+       public static function create($owner, $name){
+               $stmt = OC_DB::prepare('INSERT INTO *PREFIX*gallery_albums (uid_owner, album_name) VALUES (?, ?)');
+               $stmt->execute(array($owner, $name));
+       }
+       public static function find($owner, $name=null){
+               $sql = 'SELECT * FROM *PREFIX*gallery_albums WHERE uid_owner = ?';
+               $args = array($owner);
+               if (!is_null($name)){
+                       $sql .= ' AND album_name = ?';
+                       $args[] = $name;
+               }
+               $stmt = OC_DB::prepare($sql);
+               return $stmt->execute($args);
+       }
+}
diff --git a/apps/gallery/lib/photo.php b/apps/gallery/lib/photo.php
new file mode 100644 (file)
index 0000000..97d1599
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+
+class OC_Gallery_Photo{
+       public static function create($albumId, $img){
+               $stmt = OC_DB::prepare('INSERT INTO *PREFIX*gallery_photos (album_id, file_path) VALUES (?, ?)');
+               $stmt->execute(array($albumId, $img));
+       }
+       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;
+               }
+               $stmt = OC_DB::prepare($sql);
+               return $stmt->execute($args);
+       }
+       public static function findForAlbum($owner, $album_name){
+               $stmt = OC_DB::prepare('SELECT *'
+                       .' FROM *PREFIX*gallery_photos photos,'
+                               .' *PREFIX*gallery_albums albums'
+                       .' WHERE albums.uid_owner = ?'
+                               .' AND albums.album_name = ?'
+                               .' AND photos.album_id = albums.album_id');
+               return $stmt->execute(array($owner, $album_name));
+       }
+}
diff --git a/apps/gallery/lib/scanner.php b/apps/gallery/lib/scanner.php
new file mode 100644 (file)
index 0000000..1590051
--- /dev/null
@@ -0,0 +1,49 @@
+<?php
+
+class OC_Gallery_Scanner {
+  public static function scan($root) {
+    $albums = array();
+    self::scanDir($root, $albums);
+    return $albums;
+  }
+
+  public static function scanDir($path, &$albums) {
+    $current_album = array('name'=> $path, 'imagesCount' => 0, 'images' => array());
+    $current_album['name'] = str_replace('/', '.', str_replace(OC::$CONFIG_DATADIRECTORY, '', $current_album['name']));
+    $current_album['name'] = ($current_album['name']==='')?'main':$current_album['name'];
+
+    if ($dh = OC_Filesystem::opendir($path)) {
+      while (($filename = readdir($dh)) !== false) {
+        $filepath = $path.'/'.$filename;
+        if (substr($filename, 0, 1) == '.') continue;
+        if (OC_Filesystem::is_dir($filepath)) {
+          self::scanDir($filepath, $albums);
+        } elseif (self::isPhoto($path.'/'.$filename)) {
+          $current_album['images'][] = $filepath;
+        }
+      }
+    }
+    $current_album['imagesCount'] = count($current_album['images']);
+    $albums[] = $current_album;
+    $result = OC_Gallery_Album::find(OC_User::getUser(), $current_album['name']);
+    if ($result->numRows() == 0 && count($current_album['images'])) {
+           OC_Gallery_Album::create(OC_User::getUser(), $current_album['name']);
+           $result = OC_Gallery_Album::find(OC_User::getUser(), $current_album['name']);
+    }
+    $albumId = $result->fetchRow();
+    $albumId = $albumId['album_id'];
+    foreach ($current_album['images'] as $img) {
+      $result = OC_Gallery_Photo::find($albumId, $img);
+      if ($result->numRows() == 0) {
+             OC_Gallery_Photo::create($albumId, $img);
+      }
+    }
+  }
+
+  public static function isPhoto($filename) {
+    if (substr(OC_Filesystem::getMimeType($filename), 0, 6) == "image/")
+      return 1;
+    return 0;
+  }
+}
+?>
diff --git a/apps/gallery/lib_scanner.php b/apps/gallery/lib_scanner.php
deleted file mode 100644 (file)
index 1231de3..0000000
+++ /dev/null
@@ -1,57 +0,0 @@
-<?php
-
-require_once('base.php'); // base lib
-
-class OC_GALLERY_SCANNER {
-
-  public static function scan($root) {
-    $albums = array();
-    self::scanDir($root, $albums);
-    return $albums;
-  }
-
-  public static function scanDir($path, &$albums) {
-    $current_album = array('name'=> $path, 'imagesCount' => 0, 'images' => array());
-    $current_album['name'] = str_replace('/', '.', str_replace(OC::$CONFIG_DATADIRECTORY, '', $current_album['name']));
-    $current_album['name'] = ($current_album['name']==='')?'main':$current_album['name'];
-
-    if ($dh = OC_Filesystem::opendir($path)) {
-      while (($filename = readdir($dh)) !== false) {
-        $filepath = $path.'/'.$filename;
-        if (substr($filename, 0, 1) == '.') continue;
-        if (OC_Filesystem::is_dir($filepath)) {
-          self::scanDir($filepath, $albums);
-        } elseif (self::isPhoto($path.'/'.$filename)) {
-          $current_album['images'][] = $filepath;
-        }
-      } 
-    }
-    $current_album['imagesCount'] = count($current_album['images']);
-    $albums[] = $current_album;
-    $stmt = OC_DB::prepare('SELECT * FROM *PREFIX*gallery_albums WHERE `uid_owner` = ? AND `album_name` = ?');
-    $result = $stmt->execute(array(OC_User::getUser(), $current_album['name']));
-    if ($result->numRows() == 0 && count($current_album['images'])) {
-      $stmt = OC_DB::prepare('INSERT INTO *PREFIX*gallery_albums (`uid_owner`, `album_name`) VALUES (?, ?)');
-      $stmt->execute(array(OC_User::getUser(), $current_album['name']));
-    }
-    $stmt = OC_DB::prepare('SELECT * FROM *PREFIX*gallery_albums WHERE `uid_owner` = ? AND `album_name` = ?');
-    $result = $stmt->execute(array(OC_User::getUser(), $current_album['name']));
-    $albumId = $result->fetchRow();
-    $albumId = $albumId['album_id'];
-    foreach ($current_album['images'] as $img) {
-      $stmt = OC_DB::prepare('SELECT * FROM *PREFIX*gallery_photos WHERE `album_id` = ? AND `file_path` = ?');
-      $result = $stmt->execute(array($albumId, $img));
-      if ($result->numRows() == 0) {
-        $stmt = OC_DB::prepare('INSERT INTO *PREFIX*gallery_photos (`album_id`, `file_path`) VALUES (?, ?)');
-        $stmt->execute(array($albumId, $img));
-      }
-    }
-  }
-
-  public static function isPhoto($filename) {
-    if (substr(OC_Filesystem::getMimeType($filename), 0, 6) == "image/")
-      return 1;
-    return 0;
-  }
-}
-?>
index 230e2a5c21ddccc48f52c4f0ecc165fb1b17c87d..ae43e2fc557573e16c7e2365a88da1ea9ed2c578 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 OC_Util::addStyle('gallery', 'styles');
+OC_Util::addScript('gallery', 'albums');
 OC_Util::addScript('gallery', 'album_cover');
 OC_Util::addScript('files_imageviewer', 'jquery.mousewheel-3.0.4.pack');
 OC_Util::addScript('files_imageviewer', 'jquery.fancybox-1.3.4.pack');
@@ -16,13 +17,16 @@ OC_Util::addStyle( 'files_imageviewer', 'jquery.fancybox-1.3.4' );
 <div id="controls">
   <a href="?"><input type="button" value="Back" /></a><br/>
 </div>
-<div id="gallery_list">
+
+<div id="gallery_list" class="leftcontent">
+</div>
+
+<div id="gallery_images" class="rightcontent">
 <?php
 foreach ($_['photos'] as $a) {
 ?>
-<a rel="images" href="../../files/ajax/download.php?files=<?php echo $a; ?>"><img src="ajax/thumbnail.php?img=<?php echo $a ?>"></a>
+<a rel="images" href="../../files/download.php?file=<?php echo urlencode($a); ?>"><img src="ajax/thumbnail.php?img=<?php echo urlencode($a) ?>"></a>
 <?php
   }
 ?>
-
 </div>
index c059f5ab3366e696b0b7103909984ba7f694a3c3..bcfe320665f51a433e2daecf1c971b0d4b68c0ad 100644 (file)
@@ -492,7 +492,7 @@ class PDOStatementWrapper{
        }
        
        /**
-        * make exucute return the result instead of a bool
+        * make execute return the result instead of a bool
         */
        public function execute($input=array()){
                $this->lastArguments=$input;