]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
better image deletion
authorCash Costello <cash.costello@gmail.com>
Sat, 23 Oct 2010 13:18:57 +0000 (13:18 +0000)
committerCash Costello <cash.costello@gmail.com>
Sat, 23 Oct 2010 13:18:57 +0000 (13:18 +0000)
actions/delete.php
lib/image.php

index 9b8c6ac6fd982b55941f86da8d981c25dce41400..d9a65ce5fb67e7b2e42abced26c5488ef8b1e641 100644 (file)
@@ -32,64 +32,41 @@ if ($subtype != 'image' && $subtype != 'album') { // how did we even get here?
 }
 
 $owner_guid = 0; // group or user
-if ($subtype == 'image') { //deleting an image
-       $album = get_entity($entity->container_guid);
-       $owner_guid = $album->container_guid;
-       $forward_url = $container->getURL(); //forward back to album after deleting pictures
-       $images = array($entity);
+if ($subtype == 'image') {
+       //forward back to album after deleting pictures
+       $forward_url = $container->getURL();
+
        // plugins can register to be told when a Tidypics image has been deleted
        trigger_elgg_event('delete', 'tp_image', $entity);
-} else { //deleting an album
+
+       if ($entity->delete()) {
+               system_message(elgg_echo("tidypics:deleted"));
+       } else {
+               register_error(elgg_echo("tidypics:deletefailed"));
+       }
+} else {
+       //deleting an album
        $owner_guid = $entity->container_guid;
        $forward_url = 'pg/photos/owned/' . $container->username;
        //get all the images from this album as long as less than 999 images
        $images = get_entities("object", "image", $guid, '', 999);
        // plugins can register to be told when a Tidypics album has been deleted
        trigger_elgg_event('delete', 'tp_album', $entity);
-}
+       //loop through all images and delete them
+       foreach ($images as $im) {
+               if ($im) {
 
-// make sure we decrease the repo size for the size quota
-$image_repo_size_md = get_metadata_byname($owner_guid, "image_repo_size");
-$image_repo_size = (int)$image_repo_size_md->value;
-
-//loop through all images and delete them
-foreach ($images as $im) {
-       $thumbnail = $im->thumbnail;
-       $smallthumb = $im->smallthumb;
-       $largethumb = $im->largethumb;
-
-       if ($thumbnail) { //delete standard thumbnail image
-               $delfile = new ElggFile();
-               $delfile->owner_guid = $im->getOwner();
-               $delfile->setFilename($thumbnail);
-               $delfile->delete();
-       }
-       if ($smallthumb) { //delete small thumbnail image
-               $delfile = new ElggFile();
-               $delfile->owner_guid = $im->getOwner();
-               $delfile->setFilename($smallthumb);
-               $delfile->delete();
-       }
-       if ($largethumb) { //delete large thumbnail image
-               $delfile = new ElggFile();
-               $delfile->owner_guid = $im->getOwner();
-               $delfile->setFilename($largethumb);
-               $delfile->delete();
-       }
-       if ($im) {
-               $image_repo_size -= $im->size();
 
-               if (!$im->delete()) {
-                       if ($subtype=='image') {
+                       if (!$im->delete()) {
                                register_error(elgg_echo("tidypics:deletefailed")); //unable to delete object
+                       } else {
+                               if ($subtype=='image') {
+                                       system_message(elgg_echo("tidypics:deleted")); //successfully deleted object
+                               }
                        }
-               } else {
-                       if ($subtype=='image') {
-                               system_message(elgg_echo("tidypics:deleted")); //successfully deleted object
-                       }
-               }
-       } //end delete actual image file
-} //end looping through each image to delete it
+               } //end delete actual image file
+       } //end looping through each image to delete it
+}
 
 //now that all images have been deleted, delete the album
 if ($subtype == 'album') {
@@ -116,6 +93,4 @@ if ($subtype == 'album') {
        }
 } //end of delete album
 
-create_metadata($owner_guid, "image_repo_size", $image_repo_size, 'integer', $owner_guid);
-
 forward($forward_url);
index a22e1db1ca6e2c365ff40c34646735e3cedf4de7..7c7ef2d90e20405e903ec2cb5edbb3668f463703 100644 (file)
@@ -29,6 +29,12 @@ class TidypicsImage extends ElggFile {
                        $album->removeImage($this->guid);
                }
 
+               $this->removeThumbnails();
+
+               // update quota
+               $owner = $this->getOwnerEntity();
+               $owner->image_repo_size = (int)$owner->image_repo_size - $this->size();
+
                return parent::delete();
        }
 
@@ -146,6 +152,40 @@ class TidypicsImage extends ElggFile {
                        create_annotation($this->getGUID(), "tp_view", "1", "integer", $viewer_guid, ACCESS_PUBLIC);
                }
        }
+
+       /**
+        * Remove thumbnails - usually in preparation for deletion
+        *
+        * The thumbnails are not actually ElggObjects so we create
+        * temporary objects to delete them.
+        */
+       protected function removeThumbnails() {
+               $thumbnail = $this->thumbnail;
+               $smallthumb = $this->smallthumb;
+               $largethumb = $this->largethumb;
+
+               //delete standard thumbnail image
+               if ($thumbnail) {
+                       $delfile = new ElggFile();
+                       $delfile->owner_guid = $this->getOwner();
+                       $delfile->setFilename($thumbnail);
+                       $delfile->delete();
+               }
+               //delete small thumbnail image
+               if ($smallthumb) {
+                       $delfile = new ElggFile();
+                       $delfile->owner_guid = $this->getOwner();
+                       $delfile->setFilename($smallthumb);
+                       $delfile->delete();
+               }
+               //delete large thumbnail image
+               if ($largethumb) {
+                       $delfile = new ElggFile();
+                       $delfile->owner_guid = $this->getOwner();
+                       $delfile->setFilename($largethumb);
+                       $delfile->delete();
+               }
+       }
 }
 
 /**