function remove_all_resources_from_collection($ref)
{
    // abstracts it out of save_collection()
    # Remove all resources?
    if (getval("removeall", "") != "") {
        $removed_resources = sql_array('SELECT resource AS value FROM collection_resource WHERE collection = ' . $ref . ';');
        // First log this for each resource (in case it was done by mistake)
        foreach ($removed_resources as $removed_resource_id) {
            collection_log($ref, 'r', $removed_resource_id, ' - Removed all resources from collection ID ' . $ref);
        }
        sql_query('DELETE FROM collection_resource WHERE collection = ' . $ref);
        collection_log($ref, 'R', 0);
    }
}
 if ($deleteall != "") {
     $deletecollection = $deleteall;
 }
 if (!function_exists("do_search")) {
     include "../include/search_functions.php";
 }
 if (!function_exists("delete_resource")) {
     include "../include/resource_functions.php";
 }
 # Delete all resources in collection
 if (!checkperm("D")) {
     $resources = do_search("!collection" . $deletecollection);
     for ($n = 0; $n < count($resources); $n++) {
         if (checkperm("e" . $resources[$n]["archive"])) {
             delete_resource($resources[$n]["ref"]);
             collection_log($deletecollection, "D", $resources[$n]["ref"]);
         }
     }
 }
 if ($purge != "") {
     # Delete collection
     delete_collection($purge);
     # Get count of collections
     $c = get_user_collections($userref);
     # If the user has just deleted the collection they were using, select a new collection
     if ($usercollection == $purge && count($c) > 0) {
         # Select the first collection in the dropdown box.
         $usercollection = $c[0]["ref"];
         set_user_collection($userref, $usercollection);
     }
     # User has deleted their last collection? add a new one.
    # New method
    $blocksize = 2 << 20;
    //2M chunks
    $sent = 0;
    $handle = fopen($zipfile, "r");
    // Now we need to loop through the file and echo out chunks of file data
    while ($sent < $filesize) {
        echo fread($handle, $blocksize);
        $sent += $blocksize;
    }
    # Remove archive.
    unlink($zipfile);
    if ($use_zip_extension) {
        unlink($progress_file);
        rmdir(get_temp_dir(false, $id));
        collection_log($collection, "Z", "", "-" . $size);
    }
    exit;
}
include "../include/header.php";
?>
<div class="BasicsBox">
<h1><?php 
echo $lang["downloadzip"];
?>
</h1>
<?php 
if ($use_zip_extension) {
    ?>
<script>
function ajax_download()
function remove_all_resources_from_collection($ref){
	// abstracts it out of save_collection() for use in collections_compact_style
		# Remove all resources?
	if (getval("removeall","")!="")
		{
		sql_query("delete from collection_resource where collection='$ref'");
		collection_log($ref,"R",0);
		}
	}	
function delete_resource($ref)
	{
	# Delete the resource, all related entries in tables and all files on disk
	
	if ($ref<0) {return false;} # Can't delete the template

	$resource=get_resource_data($ref);
	if (!$resource) {return false;} # Resource not found in database
	
	$current_state=$resource['archive'];
	
	global $resource_deletion_state;
	if (isset($resource_deletion_state) && $current_state!=3) # Really delete if already in the 'deleted' state.
		{
		# $resource_deletion_state is set. Do not delete this resource, instead move it to the specified state.
		sql_query("update resource set archive='" . $resource_deletion_state . "' where ref='" . $ref . "'");

        # log this so that administrator can tell who requested deletion
        resource_log($ref,'x','');
		
		# Remove the resource from any collections
		sql_query("delete from collection_resource where resource='$ref'");
			
		return true;
		}
	
	# Get info
	
	# Is transcoding
	if ($resource['is_transcoding']==1) {return false;} # Can't delete when transcoding

	# Delete files first
	$extensions = array();
	$extensions[]=$resource['file_extension']?$resource['file_extension']:"jpg";
	$extensions[]=$resource['preview_extension']?$resource['preview_extension']:"jpg";
	$extensions[]=$GLOBALS['ffmpeg_preview_extension'];
	$extensions[]='icc'; // also remove any extracted icc profiles
	$extensions=array_unique($extensions);
	
	foreach ($extensions as $extension)
		{
		$sizes=get_image_sizes($ref,true,$extension);
		foreach ($sizes as $size)
			{
			if (file_exists($size['path'])) {unlink($size['path']);}
			}
		}
	
	# Delete any alternative files
	$alternatives=get_alternative_files($ref);
	for ($n=0;$n<count($alternatives);$n++)
		{
		delete_alternative_file($ref,$alternatives[$n]['ref']);
		}

	
	// remove metadump file, and attempt to remove directory
	$dirpath = dirname(get_resource_path($ref, true, "", true));
	if (file_exists("$dirpath/metadump.xml")){
		unlink("$dirpath/metadump.xml");
	}
	@rmdir($dirpath); // try to delete directory, but if it has stuff in it fail silently for now
			  // fixme - should we try to handle if there are other random files still there?
	
	# Log the deletion of this resource for any collection it was in. 
	$in_collections=sql_query("select * from collection_resource where resource = '$ref'");
	if (count($in_collections)>0){
		if (!function_exists("collection_log")){include ("collections_functions.php");}
		for($n=0;$n<count($in_collections);$n++)
			{
			collection_log($in_collections[$n]['collection'],'d',$in_collections[$n]['resource']);
			}
		}

	hook("beforedeleteresourcefromdb");

	# Delete all database entries
	sql_query("delete from resource where ref='$ref'");
	sql_query("delete from resource_data where resource='$ref'");
	sql_query("delete from resource_dimensions where resource='$ref'");
	sql_query("delete from resource_keyword where resource='$ref'");
	sql_query("delete from resource_related where resource='$ref' or related='$ref'");
	sql_query("delete from collection_resource where resource='$ref'");
	sql_query("delete from resource_custom_access where resource='$ref'");
	sql_query("delete from external_access_keys where resource='$ref'");
	sql_query("delete from resource_alt_files where resource='$ref'");
		
	hook("afterdeleteresource");
	
	return true;
	}
function delete_resources_in_collection($collection)
{
    global $resource_deletion_state;
    // Always find all resources in deleted state and delete them permanently:
    // Note: when resource_deletion_state is null it will find all resources in collection and delete them permanently
    $query = sprintf("\n\t\t\t\tSELECT ref AS value\n\t\t\t\t  FROM resource\n\t\t\tINNER JOIN collection_resource ON collection_resource.resource = resource.ref AND collection_resource.collection = '%s'\n\t\t\t\t %s;\n\t", $collection, isset($resource_deletion_state) ? "WHERE archive = '" . $resource_deletion_state . "'" : '');
    $resources_in_deleted_state = array();
    $resources_in_deleted_state = sql_array($query);
    if (!empty($resources_in_deleted_state)) {
        foreach ($resources_in_deleted_state as $resource_in_deleted_state) {
            delete_resource($resource_in_deleted_state);
        }
        collection_log($collection, 'D', '', 'Resource ' . $resource_in_deleted_state . ' deleted permanently.');
    }
    // Create a comma separated list of all resources remaining in this collection:
    $resources = sql_array("SELECT resource AS value FROM collection_resource WHERE collection = '" . $collection . "';");
    $resources = implode(',', $resources);
    // If all resources had their state the same as resource_deletion_state, stop here:
    // Note: when resource_deletion_state is null it will always stop here
    if (empty($resources)) {
        return TRUE;
    }
    // Delete (ie. move to resource_deletion_state set in config):
    if (isset($resource_deletion_state)) {
        $query = sprintf("\n\t\t\t\t    UPDATE resource\n\t\t\t\tINNER JOIN collection_resource ON collection_resource.resource = resource.ref AND collection_resource.collection = '%s'\n\t\t\t\t       SET archive = '%s';\n\t\t", $collection, $resource_deletion_state);
        sql_query($query);
        collection_log($collection, 'D', '', 'All resources of this collection have been deleted by moving them to state ' . $resource_deletion_state);
        $query = sprintf("\n\t\t\t\tDELETE FROM collection_resource \n\t\t\t\t      WHERE resource IN (%s);\n\t\t", $resources);
        sql_query($query);
    }
    return TRUE;
}
                echo " " . str_replace("%res", $resource, $lang['error-transform-failed']) . "<br />\n";
                $failcount++;
            }
        }
        ?>
</div><?php 
        flush();
    }
    ?>
<script>CollectionDivLoad("<?php 
    echo $baseurl . '/pages/collections.php?nowarn=true&nc=' . time();
    ?>
");</script><?php 
}
if ($successcount > 0) {
    collection_log($collection, 'b', '', " ({$successcount})");
}
echo "<div class='Question'><h3>" . $lang['transform_summary'] . "</h3>\n";
$qty_total = count($resources);
switch ($qty_total) {
    case 1:
        echo $lang['resources_in_collection-1'];
        break;
    default:
        echo str_replace("%qty", $qty_total, $lang['resources_in_collection-2']);
        break;
}
echo "<br />";
switch ($successcount) {
    case 0:
        echo $lang['resources_transformed_successfully-0'];