album.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * ownCloud - gallery application
  4. *
  5. * @author Bartek Przybylski
  6. * @copyright 2012 Bartek Przybylski <bartek@alefzero.eu>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. class OC_Gallery_Album {
  23. public static function create($owner, $name, $path){
  24. $stmt = OCP\DB::prepare('INSERT INTO *PREFIX*gallery_albums (uid_owner, album_name, album_path, parent_path) VALUES (?, ?, ?, ?)');
  25. $stmt->execute(array($owner, $name, $path, self::getParentPath($path)));
  26. }
  27. public static function cleanup() {
  28. $albums = self::find(OCP\USER::getUser());
  29. while ($r = $albums->fetchRow()) {
  30. OC_Gallery_Photo::removeByAlbumId($r['album_id']);
  31. self::remove(OCP\USER::getUser(), $r['album_name']);
  32. }
  33. }
  34. public static function getParentPath($path) {
  35. return $path === '/' ? '' : dirname($path);
  36. }
  37. public static function remove($owner, $name=null, $path=null, $parent=null) {
  38. $sql = 'DELETE FROM *PREFIX*gallery_albums WHERE uid_owner LIKE ?';
  39. $args = array($owner);
  40. if (!is_null($name)){
  41. $sql .= ' AND album_name LIKE ?';
  42. $args[] = $name;
  43. }
  44. if (!is_null($path)){
  45. $sql .= ' AND album_path LIKE ?';
  46. $args[] = $path;
  47. }
  48. if (!is_null($parent)){
  49. $sql .= ' AND parent_path LIKE ?';
  50. $args[] = $parent;
  51. }
  52. $stmt = OCP\DB::prepare($sql);
  53. return $stmt->execute($args);
  54. }
  55. public static function removeByName($owner, $name) { self::remove($ownmer, $name); }
  56. public static function removeByPath($owner, $path) { self::remove($owner, null, $path); }
  57. public static function removeByParentPath($owner, $parent) { self::remove($owner, null, null, $parent); }
  58. public static function find($owner, $name=null, $path=null, $parent=null){
  59. $sql = 'SELECT * FROM *PREFIX*gallery_albums WHERE uid_owner = ?';
  60. $args = array($owner);
  61. if (!is_null($name)){
  62. $sql .= ' AND album_name = ?';
  63. $args[] = $name;
  64. }
  65. if (!is_null($path)){
  66. $sql .= ' AND album_path = ?';
  67. $args[] = $path;
  68. }
  69. if (!is_null($parent)){
  70. $sql .= ' AND parent_path = ?';
  71. $args[] = $parent;
  72. }
  73. $order = OCP\Config::getUserValue($owner, 'gallery', 'order', 'ASC');
  74. $sql .= ' ORDER BY album_name ' . $order;
  75. $stmt = OCP\DB::prepare($sql);
  76. return $stmt->execute($args);
  77. }
  78. public static function changePath($oldname, $newname, $owner) {
  79. $stmt = OCP\DB::prepare('UPDATE *PREFIX*gallery_albums SET album_path=? WHERE uid_owner=? AND album_path=?');
  80. $stmt->execute(array($newname, $owner, $oldname));
  81. }
  82. public static function changeThumbnailPath($oldname, $newname) {
  83. $view = OCP\Files::getStorage('gallery');
  84. $view->rename($oldname.'.png', $newname.'.png');
  85. }
  86. public static function getAlbumSize($id){
  87. $sql = 'SELECT COUNT(*) as size FROM *PREFIX*gallery_photos WHERE album_id = ?';
  88. $stmt = OCP\DB::prepare($sql);
  89. $result=$stmt->execute(array($id))->fetchRow();
  90. return $result['size'];
  91. }
  92. public static function getIntermediateGallerySize($path) {
  93. $path .= '%';
  94. $sql = 'SELECT COUNT(*) as size FROM *PREFIX*gallery_photos photos, *PREFIX*gallery_albums albums WHERE photos.album_id = albums.album_id AND uid_owner = ? AND file_path LIKE ?';
  95. $stmt = OCP\DB::prepare($sql);
  96. $result = $stmt->execute(array(OCP\USER::getUser(), $path))->fetchRow();
  97. return $result['size'];
  98. }
  99. }