<?php

// $Id: move.php,v 1.1.2.1 2006/12/05 10:05:29 janne Exp $
/// This script does the actual removing images from
/// the database and filesystem.
require_once "../../config.php";
require_once "lib.php";
$sesskey = required_param('sesskey', PARAM_ALPHANUM);
$imageids = required_param('image', PARAM_NOTAGS);
$galleryid = required_param('gallery', PARAM_INT);
$catid = optional_param('category', PARAM_INT);
$gallery = new modImagegallery();
// Instantiate imagegallery object.
if (!$gallery->user_allowed_editing()) {
    error("You're not allowed to use this page!!!", "{$CFG->wwwroot}/mod/imagegallery/view.php?id={$gallery->cm->id}");
}
if (!confirm_sesskey($sesskey)) {
    error("Session key error!!!");
}
$arrimages = explode(",", $imageids);
if (empty($arrimages) && !is_array($arrimages)) {
    error("Could not find any images to move!", "{$CFG->wwwroot}/mod/imagegallery/view.php?id={$gallery->cm->id}");
}
if ($images = get_records_select("imagegallery_images", "id IN (" . addslashes($imageids) . ")")) {
    $sql = "UPDATE {$CFG->prefix}imagegallery_images ";
    $sql .= "SET galleryid = '{$galleryid}', categoryid = ";
    $sql .= "'{$catid}' WHERE id IN (" . addslashes($imageids) . ")";
    if (execute_sql($sql, false)) {
        $count = 1;
        $strimages = '';
        foreach ($images as $image) {
        if (!empty($oldfile)) {
            @rename($newfile, $oldfile);
        }
        error("Could not update database!!!", $onerrorurl);
    }
    // Retrieve fresh record of image
    if (!($image = get_record("imagegallery_images", "id", $image->id))) {
        error("Could not fetch fresh image information!!!", $onerrorurl);
    }
}
$navigation = '';
if ($gallery->course->category) {
    $navigation = "<a href=\"../../course/view.php?id={$gallery->course->id}\">{$gallery->course->shortname}</a> ->";
}
$navigation .= " <a href=\"index.php?id={$gallery->course->id}\">{$strimagegalleries}</a> ->" . " <a href=\"view.php?id={$gallery->cm->id}&amp;catid={$image->categoryid}&amp;page={$page}" . "&amp;sort={$sort}&amp;dir={$dir}\">{$gallery->module->name}</a> -> " . s($image->name);
if ($gallery->user_allowed_editing()) {
    if ($edit == 'on') {
        $USER->editing = true;
    } else {
        if ($edit == 'off') {
            $USER->editing = false;
        }
    }
    $stredit = !empty($USER->editing) ? get_string('turneditingoff') : get_string('turneditingon');
    $editvalue = !empty($USER->editing) ? 'off' : 'on';
    $buttons = '<table><tr><td><form method="get" action="details.php" target="_self">';
    $buttons .= '<input type="hidden" name="id" value="' . s($gallery->cm->id) . '" />';
    $buttons .= '<input type="hidden" name="edit" value="' . $editvalue . '" />';
    $buttons .= '<input type="hidden" name="catid" value="' . s($image->categoryid) . '" />';
    $buttons .= '<input type="hidden" name="image" value="' . s($image->id) . '" />';
    $buttons .= '<input type="hidden" name="page" value="' . s($page) . '" />';