require_once "../../../config.php";
include "../user_library.php";
include "../deletion_library.php";
include "../template_status.php";
_load_language_file("/website_code/php/templates/delete_template.inc");
$database_id = database_connect("delete template database connect success", "delete template database connect failed");
/*
 * get the folder id to delete
 */
if (is_numeric($_POST['template_id'])) {
    $safe_template_id = mysql_real_escape_string($_POST['template_id']);
    if (!is_template_syndicated($safe_template_id)) {
        if (is_user_creator($safe_template_id)) {
            $query_for_folder_id = "select * from " . $xerte_toolkits_site->database_table_prefix . "templaterights where template_id=\"" . $safe_template_id . "\"";
            $query_for_folder_id_response = mysql_query($query_for_folder_id);
            $row = mysql_fetch_array($query_for_folder_id_response);
            // delete from the database
            $query_to_delete_template = "update " . $xerte_toolkits_site->database_table_prefix . "templaterights set folder=\"" . get_recycle_bin() . "\" where template_id=\"" . $safe_template_id . "\" and user_id=\"" . $_SESSION['toolkits_logon_id'] . "\"";
            if (mysql_query($query_to_delete_template)) {
                receive_message($_SESSION['toolkits_logon_username'], "ADMIN", "CRITICAL", "Moved file to users recycle bin", "Moved file to users recycle bin");
            } else {
                receive_message($_SESSION['toolkits_logon_username'], "ADMIN", "CRITICAL", "Failed to move file to the recycle bin", "Failed to move file to the recycle bin");
            }
        } else {
            echo DELETE_TEMPLATE_NOT_CREATOR;
        }
    } else {
        echo DELETE_TEMPLATE_SYNDICATED;
    }
    mysql_close($database_id);
}
/**
 * 
 * Function move file
 * This function is used to move files and folders
 * @param array $files_to_move = an array of files and folders to move
 * @param string $destination = Name of the new folder
 * @version 1.0
 * @author Patrick Lockley
 */
function move_file($files_to_move, $destination)
{
    global $xerte_toolkits_site;
    $mysql_id = database_connect("Move file database connect success", "Move file database connect failure");
    $new_files_array = explode(",", $files_to_move);
    /*
     * Files array can be complicated, and this thread can lock the system, so limit max files to 50
     */
    if (count($new_files_array) != 0 && count($new_files_array) <= 50) {
        /*
         * check their is a destination
         */
        if ($destination != "") {
            for ($x = 0; $x != count($new_files_array); $x++) {
                // check there are files
                if ($new_files_array[$x] != "") {
                    if ($new_files_array[$x + 1] == "file") {
                        if ($new_files_array[$x + 2] == "folder_workspace") {
                            $parent = get_user_root_folder();
                        }
                        if ($destination == "folder_workspace") {
                            $destination = get_user_root_folder();
                        }
                        if ($destination == "recyclebin") {
                            $destination = get_recycle_bin();
                        }
                        /*
                         * Move files in the database
                         */
                        $query_file = "UPDATE " . $xerte_toolkits_site->database_table_prefix . "templaterights SET folder = \"" . $destination . "\" where (template_id=\"" . $new_files_array[$x] . "\" AND user_id =\"" . $_SESSION['toolkits_logon_id'] . "\")";
                        if (mysql_query($query_file)) {
                            receive_message($_SESSION['toolkits_logon_username'], "USER", "SUCCESS", "File " . $new_files_array[$x] . " moved into " . $destination . " for " . $_SESSION['toolkits_logon_username'], "File " . $new_files_array[$x] . " moved into " . $destination . " for " . $_SESSION['toolkits_logon_username']);
                        } else {
                            receive_message($_SESSION['toolkits_logon_username'], "USER", "SUCCESS", "File " . $new_files_array[$x] . " failed to move into " . $destination . " for " . $_SESSION['toolkits_logon_username'], "File " . $new_files_array[$x] . " failed to move into " . $destination . " for " . $_SESSION['toolkits_logon_username']);
                        }
                    } else {
                        /*
                         * destination is the root folder
                         */
                        if ($destination == "folder_workspace") {
                            $destination = get_user_root_folder();
                        }
                        $query_folder = "UPDATE " . $xerte_toolkits_site->database_table_prefix . "folderdetails SET folder_parent = \"" . $destination . "\" where (folder_id=\"" . $new_files_array[$x] . "\")";
                        if (mysql_query($query_folder)) {
                            receive_message($_SESSION['toolkits_logon_username'], "USER", "SUCCESS", "Folder " . $new_files_array[$x] . " moved into " . $destination . " for " . $_SESSION['toolkits_logon_username'], "File " . $new_files_array[$x] . " moved into " . $destination . " for " . $_SESSION['toolkits_logon_username']);
                        } else {
                            receive_message($_SESSION['toolkits_logon_username'], "USER", "SUCCESS", "File " . $new_files_array[$x] . " failed to move into " . $destination . " for " . $_SESSION['toolkits_logon_username'], "Folder " . $new_files_array[$x] . " failed to move into " . $destination . " for " . $_SESSION['toolkits_logon_username']);
                        }
                    }
                    $x += 2;
                }
            }
        }
    }
    mysql_close($mysql_id);
}
include "../template_status.php";
_load_language_file("/website_code/php/templates/delete_template.inc");
if (empty($_SESSION['toolkits_logon_id'])) {
    die("Please login");
}
/*
 * get the folder id to delete
 */
$prefix = $xerte_toolkits_site->database_table_prefix;
if (is_numeric($_POST['template_id'])) {
    $safe_template_id = (int) $_POST['template_id'];
    if (!is_template_syndicated($safe_template_id)) {
        if (is_user_creator($safe_template_id)) {
            $query_for_folder_id = "select * from {$prefix}templaterights where template_id= ?";
            $params = array($safe_template_id);
            $row = db_query_one($query_for_folder_id, $params);
            // delete from the database
            $query_to_delete_template = "UPDATE {$prefix}templaterights set folder= ? WHERE template_id = ? AND user_id = ?";
            $params = array(get_recycle_bin(), $safe_template_id, $_SESSION['toolkits_logon_id']);
            if (db_query($query_to_delete_template, $params)) {
                receive_message($_SESSION['toolkits_logon_username'], "ADMIN", "CRITICAL", "Moved file to users recycle bin", "Moved file to users recycle bin");
            } else {
                receive_message($_SESSION['toolkits_logon_username'], "ADMIN", "CRITICAL", "Failed to move file to the recycle bin", "Failed to move file to the recycle bin");
            }
        } else {
            echo DELETE_TEMPLATE_NOT_CREATOR;
        }
    } else {
        echo DELETE_TEMPLATE_SYNDICATED;
    }
}