<?php
// require login
$Auth->requireUser(WEB_ROOT . '/login.' . SITE_CONFIG_PAGE_EXTENSION);

/* load file */
if (isset($_REQUEST['u']))
{
    $file = file::loadById($_REQUEST['u']);
    if (!$file)
    {
        // failed lookup of file
        coreFunctions::redirect(WEB_ROOT . '/account_home.' . SITE_CONFIG_PAGE_EXTENSION);
    }

    // check current user has permission to edit file
    if ($file->userId != $Auth->id)
    {
        coreFunctions::redirect(WEB_ROOT . '/account_home.' . SITE_CONFIG_PAGE_EXTENSION);
    }
}
else
{
    coreFunctions::redirect(WEB_ROOT . '/account_home.' . SITE_CONFIG_PAGE_EXTENSION);
}


/* setup page */
define("PAGE_NAME", t("edit_page_name", "Edit"));
define("PAGE_DESCRIPTION", t("edit_meta_description", "Edit existing item"));
define("PAGE_KEYWORDS", t("edit_meta_keywords", "edit, existing, item"));

/* handle submission */
if ((int) $_REQUEST['submitme'])
{
    // validation
    $filename = trim($_REQUEST['filename']);
    $filename = strip_tags($filename);
    $filename = str_replace(array("'", "\""), "", $filename);
    $reset_stats = (int) trim($_REQUEST['reset_stats']);
    $folder = (int) trim($_REQUEST['folder']);
    $enablePassword = false;
    if (isset($_REQUEST['enablePassword']))
    {
        $enablePassword = true;
        $password = trim($_REQUEST['password']);
    }
    if (!strlen($filename))
    {
        notification::setError(t("please_enter_the_filename", "Please enter the filename"));
    }
    elseif (_CONFIG_DEMO_MODE == true)
    {
        notification::setError(t("no_changes_in_demo_mode"));
    }
    elseif(coreFunctions::getUsersAccountLockStatus($Auth->id) == 1)
    {
        notification::setError(t('account_locked_error_message', 'This account has been locked, please unlock the account to regain full functionality.'));
    }
    else
    {
        // check for files in same folder
        $foundExistingFile = (int) $db->getValue('SELECT COUNT(id) FROM file WHERE originalFilename = ' . $db->quote($filename . '.' . $file->extension) . ' AND statusId = 1 AND folderId ' . ($folder === 0 ? 'IS NULL' : ('= ' . $folder)));
        if ($foundExistingFile)
        {
            notification::setError(t("active_file_with_same_name_found", "Active file with same name found in the same folder. Please ensure the file name is unique."));
        }
    }

    // no errors
    if (!notification::isErrors())
    {
        if ($folder == 0)
        {
            $folder = null;
        }

        // update file
        $db = Database::getDatabase(true);
        $rs = $db->query('UPDATE file SET originalFilename = :originalFilename, folderId = :folderId WHERE id = :id', array('originalFilename' => $filename . '.' . $file->extension, 'folderId' => $folder, 'id' => $file->id));
        if ($rs)
        {
            // clean stats if needed
            if ($reset_stats == 1)
            {
                $db->query('UPDATE file SET visits = 0 WHERE id = :id', array('id' => $file->id));
                $db->query("DELETE FROM stats WHERE file_id = :id", array('id' => $file->id));
            }

            // update password
            $passwordHash = '';
            if ($enablePassword == true)
            {
                if (strlen($password))
                {
                    $passwordHash = MD5($password);
                }
            }
            if ((strlen($passwordHash)) || ((strlen($passwordHash) == 0) && (strlen($file->accessPassword))))
            {
                $db->query('UPDATE file SET accessPassword = :accessPassword WHERE id = :id', array('accessPassword' => $passwordHash, 'id' => $file->id));
            }

            // redirect
            coreFunctions::redirect(WEB_ROOT . "/account_home." . SITE_CONFIG_PAGE_EXTENSION . '?s=' . urlencode(t('file_item_updated', 'File updated.')));
        }
        else
        {
            notification::setError(t("problem_updating_item", "There was a problem updating the item, please try again later."));
        }
    }
}

// load folders
$folders = fileFolder::loadAllForSelect($Auth->id);

// include header
require_once(SITE_TEMPLATES_PATH . '/partial/_header.inc.php');
?>

<script>
    $(document).ready(function() {
        togglePasswordField();
    });

    function togglePasswordField()
    {
        if ($('#enablePassword').is(':checked'))
        {
            $('#password').show();
        }
        else
        {
            $('#password').hide();
        }
    }
</script>
<div class="contentPageWrapper">
    <?php
    if (notification::isErrors())
    {
        echo notification::outputErrors();
    }
    ?>
    <!-- main section -->
    <div class="pageSectionMainFull ui-corner-all">
        <div class="pageSectionMainInternal">
            <div id="pageHeader">
                <h2><?php echo t("edit_existing_item", "Edit Existing Item"); ?> (<?php echo validation::safeOutputToScreen($file->originalFilename, null, 55); ?>)</h2>
            </div>
            <div>
                <p class="introText">
                    <?php echo t("edit_existing_item_intro_text", "Use the form below to amend the selected item."); ?>
                    <br/><br/>
                </p>
                <form class="international" method="post" action="<?php echo WEB_ROOT; ?>/account_edit_item.<?php echo SITE_CONFIG_PAGE_EXTENSION; ?>" id="form-join">
                    <ul class="responsiveEditItemRow">
                        <li class="field-container"><label for="filename"><span class="field-name"><?php echo t("filename", "filename"); ?></span><input type="text" value="<?php echo validation::safeOutputToScreen($file->getFilenameExcExtension()); ?>" id="filename" name="filename" class="uiStyle accountEditSelectField" onFocus="showHideTip(this);"></label>
                            <div id="filenameTip" class="hidden formTip" style="left: 522px;">
                                <?php echo t('the_filename_on_download', 'The filename on download'); ?>
                            </div>
                        </li>
                        <li class="field-container"><label for="folder"><span class="field-name"><?php echo t("folder", "folder"); ?></span><select id="folder" name="folder" class="uiStyle accountEditSelectField" onFocus="showHideTip(this);">
                                    <option value=""><?php echo t('_default_', '- Default -'); ?></option>
                                    <?php
                                    if (COUNT($folders))
                                    {
                                        foreach ($folders AS $id => $folderLabel)
                                        {
                                            echo '<option value="' . (int) $id . '"';
                                            if ((int) $id == $file->folderId)
                                            {
                                                echo ' SELECTED';
                                            }
                                            echo '>' . validation::safeOutputToScreen($folderLabel) . '</option>';
                                        }
                                    }
                                    ?>
                                </select></label>
                            <div id="folderTip" class="hidden formTip" style="left: 522px;">
                                <?php echo t('the_items_folder', 'The items folder'); ?>
                            </div>
                        </li>
                        <li class="field-container"><label for="password"><span class="field-name"><?php echo t("access_password", "access password"); ?></span></label><div class="accountEditItemSelect"><input type="checkbox" value="1" id="enablePassword" name="enablePassword" style="width: auto; margin-top: 8px;" onClick="togglePasswordField();" <?php echo strlen($file->accessPassword) ? 'CHECKED' : ''; ?>/></div>&nbsp;&nbsp;<input type="password" value="<?php echo strlen($file->accessPassword) ? '**********' : ''; ?>" id="password" name="password" class="uiStyle" onFocus="showHideTip(this);" style="width:100px; display: none;">
                            <div class='clear'></div>
                            <div id="passwordTip" class="hidden formTip" style="left: 522px;">
                                <?php echo t('the_password_to_access_the_file', 'The password to access the file. Leave blank for none'); ?>
                            </div>
                        </li>
                        <li class="field-container"><label for="reset_stats"><span class="field-name"><?php echo t("reset_stats", "reset stats"); ?></span><select id="reset_stats" name="reset_stats" class="uiStyle" onFocus="showHideTip(this);">
                                    <option value="0"><?php echo t('no_keep_stats', 'No, keep stats'); ?></option>
                                    <option value="1"><?php echo t('yes_remove_stats', 'Yes, remove stats'); ?></option>
                                </select></label>
                            <div id="reset_statsTip" class="hidden formTip" style="left: 522px;">
                                <?php echo t('whether_to_reset_the_statistics_or_not', 'Whether to reset the statistics or not'); ?>
                            </div>
                        </li>
                        <li class="field-container">
                            <span class="field-name"></span>
                            <input tabindex="99" type="submit" name="submit" value="<?php echo t("update_item", "update item"); ?>" class="submitInput" />
                        </li>
                    </ul>
                    <input type="hidden" value="1" name="submitme"/>
                    <input type="hidden" value="<?php echo (int) $_REQUEST['u']; ?>" name="u"/>
                </form>
            </div>
            <div class="clear"><!-- --></div>
        </div>
    </div>
</div>
<?php
// include footer
require_once(SITE_TEMPLATES_PATH . '/partial/_footer.inc.php');
?>
