TidypicsAlbum.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. /**
  3. * Tidypics Album class
  4. *
  5. * @package TidypicsAlbum
  6. * @author Cash Costello
  7. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2
  8. */
  9. class TidypicsAlbum extends ElggObject {
  10. /**
  11. * Sets the internal attributes
  12. */
  13. protected function initializeAttributes() {
  14. parent::initializeAttributes();
  15. $this->attributes['subtype'] = "album";
  16. }
  17. /**
  18. * Constructor
  19. * @param mixed $guid
  20. */
  21. public function __construct($guid = null) {
  22. parent::__construct($guid);
  23. }
  24. /**
  25. * Save an album
  26. *
  27. * @return bool
  28. */
  29. public function save() {
  30. if (!isset($this->new_album)) {
  31. $this->new_album = true;
  32. }
  33. if (!isset($this->last_notified)) {
  34. $this->last_notified = 0;
  35. }
  36. if (!parent::save()) {
  37. return false;
  38. }
  39. mkdir(tp_get_img_dir($this->guid), 0755, true);
  40. return true;
  41. }
  42. /**
  43. * Delete album
  44. *
  45. * @return bool
  46. */
  47. public function delete() {
  48. $this->deleteImages();
  49. $this->deleteAlbumDir();
  50. return parent::delete();
  51. }
  52. /**
  53. * Get the title of the photo album
  54. *
  55. * @return string
  56. */
  57. public function getTitle() {
  58. return $this->title;
  59. }
  60. /**
  61. * Get the URL for this album
  62. *
  63. * @return string
  64. */
  65. public function getURL() {
  66. $title = elgg_get_friendly_title($this->getTitle());
  67. $url = "photos/album/$this->guid/$title";
  68. return elgg_normalize_url($url);
  69. }
  70. /**
  71. * Get an array of image objects
  72. *
  73. * @param int $limit
  74. * @param int $offset
  75. * @return array
  76. */
  77. public function getImages($limit, $offset = 0) {
  78. $imageList = $this->getImageList();
  79. if ($offset > count($imageList)) {
  80. return array();
  81. }
  82. $imageList = array_slice($imageList, $offset, $limit);
  83. $images = array();
  84. foreach ($imageList as $guid) {
  85. $images[] = get_entity($guid);
  86. }
  87. return $images;
  88. }
  89. /**
  90. * View a list of images
  91. *
  92. * @param array $options Options to pass to elgg_view_entity_list()
  93. * @return string
  94. */
  95. public function viewImages(array $options = array()) {
  96. $count = $this->getSize();
  97. if ($count == 0) {
  98. return '';
  99. }
  100. $defaults = array(
  101. 'count' => $count,
  102. 'limit' => (int)get_input('limit', 16),
  103. 'offset' => (int)get_input('offset', 0),
  104. 'full_view' => false,
  105. 'list_type' => 'gallery',
  106. 'list_type_toggle' => false,
  107. 'pagination' => true,
  108. 'gallery_class' => 'tidypics-gallery',
  109. );
  110. $options = array_merge($defaults, (array) $options);
  111. $images = $this->getImages($options['limit'], $options['offset']);
  112. if (count($images) == 0) {
  113. return '';
  114. }
  115. return elgg_view_entity_list($images, $options);
  116. }
  117. /**
  118. * Returns the cover image entity
  119. * @return TidypicsImage
  120. */
  121. public function getCoverImage() {
  122. return get_entity($this->getCoverImageGuid());
  123. }
  124. /**
  125. * Get the GUID of the album cover
  126. *
  127. * @return int
  128. */
  129. public function getCoverImageGuid() {
  130. if ($this->getSize() == 0) {
  131. return 0;
  132. }
  133. $guid = $this->cover;
  134. $imageList = $this->getImageList();
  135. if (!in_array($guid, $imageList)) {
  136. // select random photo to be cover
  137. $index = array_rand($imageList, 1);
  138. $guid = $imageList[$index];
  139. $this->cover = $guid;
  140. }
  141. return $guid;
  142. }
  143. /**
  144. * Set the GUID for the album cover
  145. *
  146. * @param int $guid
  147. * @return bool
  148. */
  149. public function setCoverImageGuid($guid) {
  150. $imageList = $this->getImageList();
  151. if (!in_array($guid, $imageList)) {
  152. return false;
  153. }
  154. $this->cover = $guid;
  155. return true;
  156. }
  157. /**
  158. * Get the number of photos in the album
  159. *
  160. * @return int
  161. */
  162. public function getSize() {
  163. return count($this->getImageList());
  164. }
  165. /**
  166. * Returns an order list of image guids
  167. *
  168. * @return array
  169. */
  170. public function getImageList() {
  171. $listString = $this->orderedImages;
  172. if (!$listString) {
  173. return array();
  174. }
  175. $list = unserialize($listString);
  176. // if empty don't need to check the permissions.
  177. if (!$list) {
  178. return array();
  179. }
  180. // check access levels
  181. $guidsString = implode(',', $list);
  182. $options = array(
  183. 'wheres' => array("e.guid IN ($guidsString)"),
  184. 'order_by' => "FIELD(e.guid, $guidsString)",
  185. 'callback' => 'tp_guid_callback',
  186. 'limit' => ELGG_ENTITIES_NO_VALUE
  187. );
  188. $list = elgg_get_entities($options);
  189. return $list;
  190. }
  191. /**
  192. * Sets the album image order
  193. *
  194. * @param array $list An indexed array of image guids
  195. * @return bool
  196. */
  197. public function setImageList($list) {
  198. // validate data
  199. foreach ($list as $guid) {
  200. if (!filter_var($guid, FILTER_VALIDATE_INT)) {
  201. return false;
  202. }
  203. }
  204. $listString = serialize($list);
  205. $this->orderedImages = $listString;
  206. return true;
  207. }
  208. /**
  209. * Add new images to the front of the image list
  210. *
  211. * @param array $list An indexed array of image guids
  212. * @return bool
  213. */
  214. public function prependImageList($list) {
  215. $currentList = $this->getImageList();
  216. $list = array_merge($list, $currentList);
  217. return $this->setImageList($list);
  218. }
  219. /**
  220. * Get the previous image in the album. Wraps around to the last image if given the first.
  221. *
  222. * @param int $guid GUID of the current image
  223. * @return TidypicsImage
  224. */
  225. public function getPreviousImage($guid) {
  226. $imageList = $this->getImageList();
  227. $key = array_search($guid, $imageList);
  228. if ($key === FALSE) {
  229. return null;
  230. }
  231. $key--;
  232. if ($key < 0) {
  233. return get_entity(end($imageList));
  234. }
  235. return get_entity($imageList[$key]);
  236. }
  237. /**
  238. * Get the next image in the album. Wraps around to the first image if given the last.
  239. *
  240. * @param int $guid GUID of the current image
  241. * @return TidypicsImage
  242. */
  243. public function getNextImage($guid) {
  244. $imageList = $this->getImageList();
  245. $key = array_search($guid, $imageList);
  246. if ($key === FALSE) {
  247. return null;
  248. }
  249. $key++;
  250. if ($key >= count($imageList)) {
  251. return get_entity($imageList[0]);
  252. }
  253. return get_entity($imageList[$key]);
  254. }
  255. /**
  256. * Get the index into the album for a particular image
  257. *
  258. * @param int $guid GUID of the image
  259. * @return int
  260. */
  261. public function getIndex($guid) {
  262. return array_search($guid, $this->getImageList()) + 1;
  263. }
  264. /**
  265. * Remove an image from the album list
  266. *
  267. * @param int $imageGuid
  268. * @return bool
  269. */
  270. public function removeImage($imageGuid) {
  271. $imageList = $this->getImageList();
  272. $key = array_search($imageGuid, $imageList);
  273. if ($key === false) {
  274. return false;
  275. }
  276. unset($imageList[$key]);
  277. $this->setImageList($imageList);
  278. return true;
  279. }
  280. /**
  281. * Has enough time elapsed between the last_notified and notify_interval setting?
  282. *
  283. * @return bool
  284. */
  285. public function shouldNotify() {
  286. return time() - $this->last_notified > elgg_get_plugin_setting('notify_interval', 'tidypics');
  287. }
  288. /**
  289. * Delete all the images in this album
  290. *
  291. * @todo ElggBatch?
  292. */
  293. protected function deleteImages() {
  294. $images = elgg_get_entities(array(
  295. "type" => "object",
  296. "subtype" => "image",
  297. "container_guid" => $this->guid,
  298. "limit" => ELGG_ENTITIES_NO_VALUE,
  299. ));
  300. if ($images) {
  301. foreach ($images as $image) {
  302. if ($image) {
  303. $image->delete();
  304. }
  305. }
  306. }
  307. }
  308. /**
  309. * Delete the album directory on disk
  310. */
  311. protected function deleteAlbumDir() {
  312. $tmpfile = new ElggFile();
  313. $tmpfile->setFilename('image/' . $this->guid . '/._tmp_del_tidypics_album_');
  314. $tmpfile->subtype = 'image';
  315. $tmpfile->owner_guid = $this->owner_guid;
  316. $tmpfile->container_guid = $this->guid;
  317. $tmpfile->open("write");
  318. $tmpfile->write('');
  319. $tmpfile->close();
  320. $tmpfile->save();
  321. $albumdir = eregi_replace('/._tmp_del_tidypics_album_', '', $tmpfile->getFilenameOnFilestore());
  322. $tmpfile->delete();
  323. // sanity check: must be a directory
  324. if (!$handle = opendir($albumdir)) {
  325. return false;
  326. }
  327. // loop through all files that might still remain undeleted in this directory and delete them
  328. // note: this does not delete the corresponding image entities from the database
  329. while (($file = readdir($handle)) !== false) {
  330. if (in_array($file, array('.', '..'))) {
  331. continue;
  332. }
  333. $path = "$albumdir/$file";
  334. unlink($path);
  335. }
  336. // remove empty directory
  337. closedir($handle);
  338. return rmdir($albumdir);
  339. }
  340. }