<?php

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * Make things happen when buttons are pressed and forms submitted.
 */
require_once __DIR__ . "/required.php";

if ($VARS['action'] !== "signout") {
    dieifnotloggedin();
}

/**
 * Redirects back to the page ID in $_POST/$_GET['source'] with the given message ID.
 * The message will be displayed by the app.
 * @param string $msg message ID (see lang/messages.php)
 * @param string $arg If set, replaces "{arg}" in the message string when displayed to the user.
 */
function returnToSender($msg, $arg = "") {
    global $VARS;
    $header = "Location: app.php?page=" . urlencode($VARS['source']) . "&msg=$msg";
    if ($arg != "") {
        $header .= "&arg=$arg";
    }
    header($header);
    die();
}

switch ($VARS['action']) {
    case "editmachine":
        $user = new User($_SESSION['uid']);
        if (!$user->hasPermission("MACHINEMANAGER_EDIT")) {
            returnToSender("no_permission");
            die();
        }

        $machine = new Machine($VARS['id']);

        $machine->setModel($VARS['model']);
        $machine->setClientID($VARS['client']);
        $machine->setOS($VARS['os']);
        $machine->setSerial($VARS['serial']);
        $machine->setManufacturer($VARS['manufacturer']);
        $machine->setCondition($VARS['condition'] * 1.0);
        $machine->setPrice($VARS['price'] * 1.0);
        $machine->setPrivateNotes($VARS['privatenotes']);
        $machine->setPublicNotes($VARS['publicnotes']);

        $machine->save();

        returnToSender("machine_saved", $machine->getID());
    case "editcomponent":
        $user = new User($_SESSION['uid']);
        if (!$user->hasPermission("MACHINEMANAGER_EDIT")) {
            returnToSender("no_permission");
            die();
        }

        if (!Machine::exists($VARS['machine'])) {
            returnToSender("invalid_parameters");
        }

        $component = new Component($VARS['id']);

        $component->setMachineID($VARS['machine']);
        $component->setSerial($VARS['serial']);
        $component->setTypeID($VARS['type']);
        $component->setTestedDate(date(
                        "Y-m-d H:i:s",
                        strtotime($VARS['date'] . " " . $VARS['time'])
                )
        );
        $component->setCapacity($VARS['capacity']);
        $component->setModel($VARS['model']);
        $component->setPrice($VARS['price'] * 1.0);
        $component->setPrivateNotes($VARS['privatenotes']);
        $component->setPublicNotes($VARS['publicnotes']);

        $component->save();

        returnToSender("component_saved", $component->getMachineID());
    case "addevent":
        $user = new User($_SESSION['uid']);
        if (!$user->hasPermission("MACHINEMANAGER_EDIT")) {
            returnToSender("no_permission");
            die();
        }
        if (!Machine::exists($VARS['machine'])) {
            returnToSender("invalid_parameters");
        }

        $machine = new Machine($VARS['machine']);

        $machine->addEvent(
                date(
                        "Y-m-d H:i:s",
                        strtotime($VARS['date'] . " " . $VARS['time'])
                ),
                $VARS['event'],
                $user->getUID(),
                $VARS['publicnotes'],
                $VARS['privatenotes']
        );

        returnToSender("event_added", $machine->getID());
    case "editclient":
        $user = new User($_SESSION['uid']);
        if (!$user->hasPermission("MACHINEMANAGER_EDIT")) {
            returnToSender("no_permission");
            die();
        }

        if (!Clients::areLocal()) {
            returnToSender("nonlocal_client");
        }

        if (Client::exists($VARS["id"])) {
            $client = new Client($VARS["id"]);
        } else {
            $client = new Client();
        }

        $client->setName($VARS["name"]);
        $client->setPhone($VARS["phone"]);
        $client->setEmail($VARS["email"]);
        $client->setBillingAddress($VARS["billingaddress"]);
        $client->setMailingAddress($VARS["mailingaddress"]);
        $client->setPublicNotes($VARS["publicnotes"]);
        $client->setPrivateNotes($VARS["privatenotes"]);

        $client->save();

        returnToSender("client_edited", $client->getID());
    case "signout":
        session_destroy();
        header('Location: index.php?logout=1');
        die("Logged out.");
}