123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- <?php
- class TidypicsAlbum extends ElggObject {
-
- protected function initializeAttributes() {
- parent::initializeAttributes();
- $this->attributes['subtype'] = "album";
- }
-
- public function __construct($guid = null) {
- parent::__construct($guid);
- }
-
- public function save() {
- if (!isset($this->new_album)) {
- $this->new_album = true;
- }
- if (!isset($this->last_notified)) {
- $this->last_notified = 0;
- }
- if (!parent::save()) {
- return false;
- }
- mkdir(tp_get_img_dir($this->guid), 0755, true);
- return true;
- }
-
- public function delete() {
- $this->deleteImages();
- $this->deleteAlbumDir();
- return parent::delete();
- }
-
- public function getTitle() {
- return $this->title;
- }
-
- public function getURL() {
- $title = elgg_get_friendly_title($this->getTitle());
- $url = "photos/album/$this->guid/$title";
- return elgg_normalize_url($url);
- }
-
- public function getImages($limit, $offset = 0) {
- $imageList = $this->getImageList();
- if ($offset > count($imageList)) {
- return array();
- }
- $imageList = array_slice($imageList, $offset, $limit);
- $images = array();
- foreach ($imageList as $guid) {
- $images[] = get_entity($guid);
- }
- return $images;
- }
-
- public function viewImages(array $options = array()) {
- $count = $this->getSize();
- if ($count == 0) {
- return '';
- }
- $defaults = array(
- 'count' => $count,
- 'limit' => (int)get_input('limit', 16),
- 'offset' => (int)get_input('offset', 0),
- 'full_view' => false,
- 'list_type' => 'gallery',
- 'list_type_toggle' => false,
- 'pagination' => true,
- 'gallery_class' => 'tidypics-gallery',
- );
- $options = array_merge($defaults, (array) $options);
- $images = $this->getImages($options['limit'], $options['offset']);
- if (count($images) == 0) {
- return '';
- }
- return elgg_view_entity_list($images, $options);
- }
-
- public function getCoverImage() {
- return get_entity($this->getCoverImageGuid());
- }
-
- public function getCoverImageGuid() {
- if ($this->getSize() == 0) {
- return 0;
- }
- $guid = $this->cover;
- $imageList = $this->getImageList();
- if (!in_array($guid, $imageList)) {
-
- $index = array_rand($imageList, 1);
- $guid = $imageList[$index];
- $this->cover = $guid;
- }
- return $guid;
- }
-
- public function setCoverImageGuid($guid) {
- $imageList = $this->getImageList();
- if (!in_array($guid, $imageList)) {
- return false;
- }
- $this->cover = $guid;
- return true;
- }
-
- public function getSize() {
- return count($this->getImageList());
- }
-
- public function getImageList() {
- $listString = $this->orderedImages;
- if (!$listString) {
- return array();
- }
- $list = unserialize($listString);
-
- if (!$list) {
- return array();
- }
-
- $guidsString = implode(',', $list);
- $options = array(
- 'wheres' => array("e.guid IN ($guidsString)"),
- 'order_by' => "FIELD(e.guid, $guidsString)",
- 'callback' => 'tp_guid_callback',
- 'limit' => ELGG_ENTITIES_NO_VALUE
- );
- $list = elgg_get_entities($options);
- return $list;
- }
-
- public function setImageList($list) {
-
- foreach ($list as $guid) {
- if (!filter_var($guid, FILTER_VALIDATE_INT)) {
- return false;
- }
- }
- $listString = serialize($list);
- $this->orderedImages = $listString;
- return true;
- }
-
- public function prependImageList($list) {
- $currentList = $this->getImageList();
- $list = array_merge($list, $currentList);
- return $this->setImageList($list);
- }
-
- public function getPreviousImage($guid) {
- $imageList = $this->getImageList();
- $key = array_search($guid, $imageList);
- if ($key === FALSE) {
- return null;
- }
- $key--;
- if ($key < 0) {
- return get_entity(end($imageList));
- }
- return get_entity($imageList[$key]);
- }
-
- public function getNextImage($guid) {
- $imageList = $this->getImageList();
- $key = array_search($guid, $imageList);
- if ($key === FALSE) {
- return null;
- }
- $key++;
- if ($key >= count($imageList)) {
- return get_entity($imageList[0]);
- }
- return get_entity($imageList[$key]);
- }
-
- public function getIndex($guid) {
- return array_search($guid, $this->getImageList()) + 1;
- }
-
- public function removeImage($imageGuid) {
- $imageList = $this->getImageList();
- $key = array_search($imageGuid, $imageList);
- if ($key === false) {
- return false;
- }
- unset($imageList[$key]);
- $this->setImageList($imageList);
- return true;
- }
-
- public function shouldNotify() {
- return time() - $this->last_notified > elgg_get_plugin_setting('notify_interval', 'tidypics');
- }
-
- protected function deleteImages() {
- $images = elgg_get_entities(array(
- "type" => "object",
- "subtype" => "image",
- "container_guid" => $this->guid,
- "limit" => ELGG_ENTITIES_NO_VALUE,
- ));
- if ($images) {
- foreach ($images as $image) {
- if ($image) {
- $image->delete();
- }
- }
- }
- }
-
- protected function deleteAlbumDir() {
- $tmpfile = new ElggFile();
- $tmpfile->setFilename('image/' . $this->guid . '/._tmp_del_tidypics_album_');
- $tmpfile->subtype = 'image';
- $tmpfile->owner_guid = $this->owner_guid;
- $tmpfile->container_guid = $this->guid;
- $tmpfile->open("write");
- $tmpfile->write('');
- $tmpfile->close();
- $tmpfile->save();
- $albumdir = eregi_replace('/._tmp_del_tidypics_album_', '', $tmpfile->getFilenameOnFilestore());
- $tmpfile->delete();
-
- if (!$handle = opendir($albumdir)) {
- return false;
- }
-
-
- while (($file = readdir($handle)) !== false) {
- if (in_array($file, array('.', '..'))) {
- continue;
- }
- $path = "$albumdir/$file";
- unlink($path);
- }
-
- closedir($handle);
- return rmdir($albumdir);
- }
- }
|