<?php
/*
 * Copyright (C) 2000-2025. Stephen Lawrence
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

// (C) 2002, 2003, 2004 Stephen Lawrence Jr., Khoa Nguyen
// Show rejected files

session_start();

if (!isset($_SESSION['uid'])) {
    redirect_visitor();
}

$pdo = $GLOBALS['pdo'];

$with_caption = false;

$last_message = (isset($_REQUEST['last_message']) ? $_REQUEST['last_message'] : '');

if (!isset($_POST['submit'])) {
    draw_header(msg('message_documents_rejected'), $last_message);
    ob_start();

    try {
        $user_obj = new User($_SESSION['uid'], $pdo);
        $user_perms_obj = new UserPermission($_SESSION['uid'], $pdo);
    } catch (Exception $e) {
        error_log("Rejects.php - Error creating user objects: " . $e->getMessage());
        error_log("Rejects.php - Session UID: " . (isset($_SESSION['uid']) ? $_SESSION['uid'] : 'NOT SET'));
        header('Location: error?ec=1&last_message=' . urlencode('User initialization failed'));
        exit;
    }
    if (@$_REQUEST['mode']=='root') {
        echo '<form name="author_note_form" action="rejects?mode=root" method="post">';
        echo $GLOBALS['csrf']->getTokenField();
    } else {
        echo '<form name="author_note_form" action="rejects" method="post">';
        echo $GLOBALS['csrf']->getTokenField();
    }
    ?>
<?php
$GLOBALS['smarty']->assign('state', -1);
display_smarty_template('out.tpl');
?>
    <div class="d-flex gap-2 mt-3">
        <button class="btn btn-success" type="submit" name="submit" value="resubmit"><?php echo msg('button_resubmit_for_review'); ?></button>
        <button class="btn btn-danger" type="submit" name="submit" value="delete"><?php echo msg('button_delete'); ?></button>
    </div>
</form>

<?php
           $content = ob_get_clean();
           $GLOBALS['smarty']->assign('content', $content);
           display_smarty_template('_content.tpl');
           draw_footer();
} elseif (isset($_POST['submit']) && $_POST['submit'] == 'resubmit') {
    // Validate CSRF token for resubmit operation
    if (isset($GLOBALS['csrf']) && !$GLOBALS['csrf']->validateToken($_POST)) {
        header('Location: error?ec=1&last_message=' . urlencode('CSRF token validation failed'));
        exit;
    }
    
    if (!isset($_REQUEST['checkbox'])) {
        header('Location:rejects?last_message=' . urlencode(msg('message_you_did_not_enter_value')));
        exit;
    }
    
    if (isset($_POST["checkbox"])) {
        foreach ($_POST['checkbox'] as $cbox) {
            $fileid = $cbox;
            $file_obj = new FileData($fileid, $pdo);
            $file_obj->Publishable(0);
        }
    }
    header('Location:rejects?mode=' . urlencode(@$_REQUEST['mode']) . '&last_message='. urlencode(msg('message_file_authorized')));
} elseif ($_POST['submit'] == 'delete') {
    // Validate CSRF token for delete operation
    if (isset($GLOBALS['csrf']) && !$GLOBALS['csrf']->validateToken($_POST)) {
        header('Location: error?ec=1&last_message=' . urlencode('CSRF token validation failed'));
        exit;
    }
    
    if (!isset($_REQUEST['checkbox'])) {
        header('Location: rejects?last_message=' . urlencode(msg('message_you_did_not_enter_value')));
        exit;
    }
    
    $url = 'delete?mode=tmpdel&';
    $id = 0;
    if (isset($_POST["checkbox"])) {
        $loop = 0;
        foreach ($_POST['checkbox'] as $num=>$cbox) {
            $fileid = $cbox;
            $url .= 'id'.  $num . '='.$fileid.'&';
            $id ++;
            $loop++;
        }
        $url = substr($url, 0, strlen($url)-1);
    }
    header('Location:'. urlencode($url) .'&num_checkboxes=' . urlencode($loop));
}

?>
<script type="text/javascript">
    function closeWindow(close_window_in_ms)
    {
    setTimeout(window.close, close_window_in_ms);
    }


function checkedBoxesNumber()
{
    counter=0;
    record="";
    for(j=0; j<document.forms[0].elements.length; j++)
    {
            if(document.forms[0].elements[j].type == "checkbox")
            {
                    counter++;
            }
    }
    for(i=1; i<counter; i++)
    {
            if(eval('document.forms[0].checkbox' + i + '.checked') == true)
            {
                    id=(eval('document.forms[0].checkbox' + i + '.value'));
                    document.table.fileid.value +="" + id +" ";
                    record +="" + i +" ";
            }
    }

    document.table.checkedboxes.value = record;
    document.table.checkednumber.value = counter;
    alert("boxes " + document.table.checkedboxes.value  + " are selected");

}

</script>