<?php
    require_once('include/functions/create_quilt_image_cache.php');

    $_id = isset($_GET['i']) ? intval($_GET['i']) : 0;
    $_name = isset($_POST['name']) ? trim(strval($_POST['name'])) : '';
    $_description = isset($_POST['description']) ? trim(strval($_POST['description'])) : '';
    $_timelimit = isset($_POST['timelimit']) ? intval($_POST['timelimit']) : 0;
    $_side_pixels = isset($_POST['side_pixels']) ? intval($_POST['side_pixels']) : 0;
    $_level = isset($_POST['level']) ? intval($_POST['level']) : 0;
    $_show_all = isset($_POST['show_all']) ? intval($_POST['show_all']) : 0;
    $_work_on_all = isset($_POST['work_on_all']) ? intval($_POST['work_on_all']) : 0;
    $_start_anywhere = isset($_POST['start_anywhere']) ? intval($_POST['start_anywhere']) : 0;
    $_multiple = isset($_POST['multiple']) ? intval($_POST['multiple']) : 0;
    $_moderated = isset($_POST['moderated']) ? intval($_POST['moderated']) : 0;

    if ($_level > 2 || $_level < 0)
    {
        $_level = 0;
    }
    
    $errors = '';
    
    if (strlen($_name) < 4)
    {
        $errors .= 'The name of the quilt must be 4 or more characters.<br />';
    }
    
    if ($_timelimit < 1)
    {
        $errors .= 'The minimum amount of time to work on a tile must be greater or equal to 1 hour.<br />';
    }
    elseif ($_timelimit > 72)
    {
        $errors .= 'The maximum amount of time to work on a tile must be less than or equal to 72 hours.<br />';
    }
    
    if ($_multiple < 1)
    {
        $errors .= 'The minimum amount of tiles one can work on must be greater or equal to 1.<br />';
    }
    elseif ($_multiple > 10)
    {
        $errors .= 'The maximum amount of tiles one can work on must be less than or equal to 10.<br />';
    }
    
    if ($_side_pixels != 15 && $_side_pixels != 16 && $_side_pixels != 20 && $_side_pixels != 24 && $_side_pixels != 25 && $_side_pixels != 30 && $_side_pixels != 32 && $_side_pixels != 35 && $_side_pixels != 40 && $_side_pixels != 45 && $_side_pixels != 50)
    {
        $errors .= 'The side pixels people can work with must be either 15, 16, 20, 24, 25, 30, 32, 35, 40, 45 or 50.<br />';
    }
    
    if ($errors)
    {
        include('include/show/quilt_edit.php');
        die;
    }
    else
    {
        $quilts_permissions = mysqli_query_logged("SELECT * FROM quilts_permissions WHERE user_id = '" . $GLOBALS['auth']['id'] . "' AND quilt_id = " . sq($_id) . " AND permission = 'root'");
        if ($quilts_permissions_row = mysqli_fetch_assoc($quilts_permissions))
        {
            $timelimit = $_timelimit * 60 * 60;
            mysqli_query_logged("UPDATE quilts SET name = " . sq($_name) . ", description = " . sq($_description) . ", timelimit = '" . $timelimit . "', side_pixels = " . sq($_side_pixels) . ", level = " . sq($_level) . ", show_all = " . sq($_show_all) . ", work_on_all = " . sq($_work_on_all) . ", multiple = " . sq($_multiple) . ", start_anywhere = " . sq($_start_anywhere) . ", moderated = " . sq($_moderated) . " WHERE id = " . sq($_id));
            create_quilt_image_cache($_id);            
        }
    }

    header('Location: ./?s=quilts_moderate');
    die;
?>