<?php

$page = 'storagedetails_edit';
$title = "Storage details";
require '../header.php';
ensureMember();
$project = new Project(filter_var($_GET['id'], FILTER_SANITIZE_STRING));
$projectslogs = fRecordSet::build('ProjectsLog', array('project_id=' => $project->getId()), array('id' => 'asc'));
$states = fRecordSet::build('ProjectState', array(), array('id' => 'asc'));
$projectUser = new User($project->getUserId());
// has the project owner set the status to removed or extended?
if (isset($_POST['remove']) || isset($_POST['extend']) && $user->getId() == $project->getUserId()) {
    try {
        fRequest::validateCSRFToken($_POST['token']);
        if (isset($_POST['remove']) && $project->canTransitionStates($project->getState(), 'Removed')) {
            $project->setState('Removed');
        } else {
            if (isset($_POST['extend']) && $project->canTransitionStates($project->getState(), 'Extended')) {
                $project->setState('Extended');
            }
        }
        $project->store();
        $project->submitLog('Status changed to ' . $project->getState(), $user->getId());
        fURL::redirect("/storage/{$project->getId()}");
    } catch (fValidationException $e) {
        echo $e->printMessage();
    } catch (fSQLException $e) {
        echo '<div class="alert alert-danger">An unexpected error occurred, please try again later</div>';
    }
}
// has another member updated the status?
 }
 $project->setName(filter_var($_POST['name'], FILTER_SANITIZE_STRING));
 $project->setDescription(filter_var($_POST['description'], FILTER_SANITIZE_STRING));
 if ($_POST['contact'] && $_POST['contact'] != '') {
     $project->setContact(filter_var($_POST['contact'], FILTER_SANITIZE_EMAIL));
 } else {
     $project->setContact(null);
 }
 $project->setLocationId(filter_var($_POST['location_id'], FILTER_SANITIZE_STRING));
 $project->setLocation(filter_var($_POST['location'], FILTER_SANITIZE_STRING));
 $project->setFromDate(filter_var($_POST['from_date'], FILTER_SANITIZE_STRING));
 $project->setToDate(filter_var($_POST['to_date'], FILTER_SANITIZE_STRING));
 if (!$project->getId()) {
     $auto = true;
     $logDetails = "Request created";
     $project->setState('Pending Approval');
     $initial = true;
 } else {
     $auto = false;
     $logDetails = "Request updated";
     $initial = false;
 }
 $project->setUpdatedDate(date('Y-m-d'));
 $project->setUserId($user->getId());
 $project->store();
 $project->submitLog($logDetails, $user->getId());
 // post to Google Groups
 $projectUser = new User($project->getUserId());
 $message = '<strong>' . $project->getName() . "</strong><br>\n" . "https://london.hackspace.org.uk/storage/" . $project->getId() . "<br>\n" . "by <a href=\"https://london.hackspace.org.uk/members/member.php?id=" . $project->getUserId() . "\">" . htmlspecialchars($projectUser->getFullName()) . "</a><br>\n" . $project->outputDates() . "<br>\n" . $project->outputDuration() . ' ' . $project->outputLocation() . "<br>\n<br>\n" . nl2br(stripslashes($project->getDescription())) . "<br>\n<br>\n";
 if ($auto && !$project->isShortTerm()) {
     $message .= "<strong>***If no one replies to this topic the request will be automatically approved within " . $project->automaticApprovalDuration() . " days.***</strong>";