gnusocialphoto.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * GNU Social
  4. * Copyright (C) 2010, Free Software Foundation, Inc.
  5. *
  6. * PHP version 5
  7. *
  8. * LICENCE:
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Widget
  23. * @package GNU Social
  24. * @author Ian Denhardt <ian@zenhack.net>
  25. * @copyright 2010 Free Software Foundation, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  27. */
  28. if (!defined('STATUSNET')) {
  29. exit(1);
  30. }
  31. require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
  32. class GNUsocialPhoto extends Managed_DataObject
  33. {
  34. public $__table = 'GNUsocialPhoto';
  35. public $id; // int(11)
  36. public $notice_id; // int(11)
  37. public $album_id; // int(11)
  38. public $uri; // varchar(191) not 255 because utf8mb4 takes more space
  39. public $thumb_uri; // varchar(191) not 255 because utf8mb4 takes more space
  40. public $title; // varchar(191) not 255 because utf8mb4 takes more space
  41. public $photo_description; // text
  42. public $created; // datetime() not_null
  43. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  44. /* function delete()
  45. {
  46. if(!unlink(INSTALLDIR . $this->thumb_uri)) {
  47. return false;
  48. }
  49. if(!unlink(INSTALLDIR . $this->path)) {
  50. return false;
  51. }
  52. return parent::delete();
  53. } */
  54. public static function schemaDef()
  55. {
  56. return array(
  57. 'fields' => array(
  58. 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'Unique ID for Photo'),
  59. 'notice_id' => array('type' => 'int', 'not null' => true, 'description' => 'Notice ID for the related notice'),
  60. 'album_id' => array('type' => 'int', 'not null' => true, 'description' => 'The parent album ID'),
  61. 'uri' => array('type' => 'varchar', 'not null' => true, 'length' => 191, 'description' => 'unique address for this photo'),
  62. 'thumb_uri' => array('type' => 'varchar', 'not null' => true, 'length' => 191, 'description' => 'unique address for this photo thumbnail'),
  63. 'title' => array('type' => 'varchar', 'not null' => true, 'length' => 191, 'description' => 'The Photo title'),
  64. 'photo_description' => array('type' => 'text', 'not null' => true, 'description' => 'A description for this photo'),
  65. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
  66. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  67. ),
  68. 'primary key' => array('id'),
  69. 'unique keys' => array(
  70. 'gnusocialphoto_id_key' => array('notice_id'),
  71. 'gnusocialphoto_uri_key' => array('uri'),
  72. ),
  73. 'foreign keys' => array(
  74. 'gnusocialphoto_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
  75. 'gnusocialphoto_album_id_fkey' => array('GNUsocialPhotoAlbum', array('album_id' => 'id')),
  76. ),
  77. 'indexes' => array(
  78. 'gnusocialphoto_title_idx' => array('title'),
  79. ),
  80. );
  81. }
  82. static function saveNew($profile_id, $album_id, $thumb_uri, $uri, $source, $insert_now, $title = null, $photo_description = null)
  83. {
  84. $photo = new GNUsocialPhoto();
  85. $photo->thumb_uri = $thumb_uri;
  86. $photo->uri = $uri;
  87. $photo->album_id = $album_id;
  88. if(!empty($title)) $photo->title = $title;
  89. if(!empty($photo_description)) $photo->photo_description = (string)$photo_description;
  90. if($insert_now) {
  91. $notice = Notice::saveNew($profile_id, $uri, $source);
  92. $photo->notice_id = $notice->id;
  93. $photo_id = $photo->insert();
  94. if (!$photo_id) {
  95. common_log_db_error($photo, 'INSERT', __FILE__);
  96. throw new ServerException(_m('Problem Saving Photo.'));
  97. }
  98. } else {
  99. GNUsocialPhotoTemp::$tmp = $photo;
  100. Notice::saveNew($profile_id, $uri, $source);
  101. }
  102. }
  103. function getPageLink()
  104. {
  105. return '/photo/' . $this->id;
  106. }
  107. /*
  108. * TODO: -Sanitize input
  109. * @param int page_id The desired page of the gallery to show.
  110. * @param int album_id The id of the album to get photos from.
  111. * @param int gallery_size The number of thumbnails to show per page in the gallery.
  112. * @return array Array of GNUsocialPhotos for this gallery page.
  113. */
  114. static function getGalleryPage($page_id, $album_id, $gallery_size)
  115. {
  116. $page_offset = ($page_id-1) * $gallery_size;
  117. $sql = 'SELECT * FROM GNUsocialPhoto WHERE album_id = ' . $album_id .
  118. ' ORDER BY notice_id LIMIT ' . $page_offset . ',' . $gallery_size;
  119. $photo = new GNUsocialPhoto();
  120. $photo->query($sql);
  121. $photos = array();
  122. while ($photo->fetch()) {
  123. $photos[] = clone($photo);
  124. }
  125. return $photos;
  126. }
  127. }