}
$apps_php = file_get_contents(PerchUtil::file_path(PERCH_PATH . '/config/apps.php'));
if (!strpos($apps_php, 'perch_gallery')) {
    $message = $HTML->warning_message('You need to add the Gallery app to your %sconfig/apps.php%s file.', '<code>', '</code>');
}
$Form = $API->get('Form');
if ($Form->submitted()) {
    $bucket_name = 'default';
    $Settings = PerchSettings::fetch();
    $bucket_mode = $Settings->get('perch_gallery_bucket_mode')->val();
    if ($bucket_mode == '') {
        $bucket_mode = 'single';
    }
    switch ($bucket_mode) {
        case 'dynamic':
            $Album = $Albums->find($albumID);
            if (is_object($Album)) {
                $bucket_name = $Album->albumSlug();
            }
            break;
        default:
            $bucket_name = $Settings->get('perch_gallery_bucket')->val();
            break;
    }
    if ($bucket_name == '') {
        $bucket_name = 'default';
    }
    $Perch = Perch::fetch();
    $bucket = $Perch->get_resource_bucket($bucket_name);
    PerchUtil::initialise_resource_bucket($bucket);
    $targetDir = $bucket['file_path'];
                 $fileName_b = substr($filename, $ext);
                 $count = 1;
                 while (file_exists(PerchUtil::file_path($targetDir . '/' . $fileName_a . '_' . $count . $fileName_b))) {
                     $count++;
                 }
                 $filename = $fileName_a . '_' . $count . $fileName_b;
                 $target = PerchUtil::file_path($targetDir . '/' . $filename);
             }
         }
         PerchUtil::move_uploaded_file($_FILES['upload']['tmp_name'], $target);
         $Image->update(array('imageStatus' => 'uploading'));
         if (is_object($Image)) {
             $Image->process_versions($filename, $Template, $bucket);
         }
         $Image->update(array('imageStatus' => 'active'));
         $Album = $GalleryAlbums->find($albumID);
         if (is_object($Album)) {
             $Album->update_image_count();
         }
     }
     if ($new_image) {
         PerchUtil::redirect($API->app_path() . '/images/edit/?album_id=' . $albumID . '&id=' . $result->id() . '&created=1');
     } else {
         $message = $HTML->success_message('Your image has been successfully updated. Return to %simage listing%s', '<a href="' . $API->app_path() . '/images/?id=' . $albumID . '">', '</a>');
     }
 } else {
     $message = $HTML->failure_message('Sorry, that image could not be updated.');
 }
 if (is_object($Image)) {
     $details = $Image->to_array();
 } else {
<?php

$GalleryAlbums = new PerchGallery_Albums($API);
$HTML = $API->get('HTML');
$Form = $API->get('Form');
$Form->require_field('albumID', 'Required');
$message = false;
if (isset($_GET['id']) && $_GET['id'] != '') {
    $Album = $GalleryAlbums->find($_GET['id']);
} else {
    PerchUtil::redirect($API->app_path());
}
if ($Form->submitted()) {
    $postvars = array('albumID');
    $data = $Form->receive($postvars);
    $Album = $GalleryAlbums->find($data['albumID']);
    if (is_object($Album)) {
        $Album->delete();
        PerchUtil::redirect($API->app_path());
    } else {
        $message = $HTML->failure_message('Sorry, that album could not be deleted.');
    }
}
$details = $Album->to_array();