You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
6.8 KiB
PHP

<?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->setType($VARS["type"]);
$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 "deletemachine":
$user = new User($_SESSION['uid']);
if (!$user->hasPermission("MACHINEMANAGER_DELETE")) {
returnToSender("no_permission");
die();
}
$machine = new Machine($VARS['id']);
$machine->setDeleted(true);
$machine->save();
returnToSender("machine_deleted");
case "editcomponent":
$user = new User($_SESSION['uid']);
if (!$user->hasPermission("MACHINEMANAGER_EDIT")) {
returnToSender("no_permission");
die();
}
$component = new Component($VARS['id']);
if (!empty($VARS["machine"])) {
if (!Machine::exists($VARS['machine'])) {
returnToSender("invalid_parameters");
}
$component->setMachineID($VARS['machine']);
}
$component->setSerial($VARS['serial']);
$component->setTypeID($VARS['type']);
if (!empty($VARS['date'])) {
$component->setTestedDate(date(
"Y-m-d H:i:s",
strtotime(trim($VARS['date'] . " " . $VARS['time']))
)
);
} else {
$component->clearTestedDate();
}
$component->setCapacity($VARS['capacity']);
$component->setModel($VARS['model']);
$component->setManufacturer($VARS['manufacturer']);
$component->setPrice($VARS['price'] * 1.0);
$component->setPrivateNotes($VARS['privatenotes']);
$component->setPublicNotes($VARS['publicnotes']);
$component->save();
if (empty($VARS["machine"])) {
returnToSender("component_saved");
}
returnToSender("component_saved", $component->getMachineID());
case "unlinkcomponent":
$user = new User($_SESSION['uid']);
if (!$user->hasPermission("MACHINEMANAGER_EDIT")) {
returnToSender("no_permission");
die();
}
$component = new Component($VARS['id']);
$component->setMachineID(null);
$component->save();
if (!empty($VARS["machine"])) {
if (Machine::exists($VARS['machine'])) {
returnToSender("component_unlinked", $VARS["machine"]);
}
}
returnToSender("component_unlinked");
case "deletecomponent":
$user = new User($_SESSION['uid']);
if (!$user->hasPermission("MACHINEMANAGER_EDIT")) {
returnToSender("no_permission");
die();
}
$component = new Component($VARS['id']);
$component->delete();
returnToSender("component_deleted");
case "addevent":
$user = new User($_SESSION['uid']);
if (!$user->hasPermission("MACHINEMANAGER_EDIT")) {
returnToSender("no_permission");
die();
}
if (!Machine::exists($VARS['machine'])) {
returnToSender("invalid_parameters");
}
$evt = Event::create(
$VARS['machine'],
date(
"Y-m-d H:i:s",
strtotime($VARS['date'] . " " . $VARS['time'])
),
$VARS['event'],
$user->getUID(),
$VARS['publicnotes'],
$VARS['privatenotes']
);
returnToSender("event_added", $evt->getMachineID());
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 "editjob":
$user = new User($_SESSION['uid']);
if (!$user->hasPermission("MACHINEMANAGER_EDIT")) {
returnToSender("no_permission");
die();
}
if (!empty($VARS["jobid"]) && Job::exists($VARS["jobid"])) {
$job = new Job($VARS['jobid']);
} else {
$job = Job::create();
}
$job->setMachineID($VARS["machineid"]);
$job->setName($VARS["jobname"]);
$job->setInfo($VARS["jobinfo"]);
$job->setNotes($VARS["jobnotes"]);
$job->save();
returnToSender("job_saved", $job->getID());
case "signout":
session_destroy();
header('Location: index.php?logout=1');
die("Logged out.");
}