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.

105 lines
2.9 KiB
PHP

<?
include_once("../php/include-request.php");
$action = getMandatoryRequestValue("action");
if($action == "create") {
$name = getMandatoryRequestValue("name", "Name cannot be empty");
$currency = getMandatoryRequestValue("currency", "Currency cannot be empty");
$password = getMandatoryRequestValue("password", "You must specify a password");
$passwordSalt = createPasswordSalt();
$passwordHash = createPasswordHash($password, $passwordSalt);
initDatabaseConnection();
$rowCount = dbUpdate("
INSERT INTO household(
name,
currency,
password_hash,
password_salt
) VALUES (
?,
?,
?,
?
)",
$name,
$currency,
$passwordHash,
$passwordSalt
);
if($rowCount) {
dbUpdate("INSERT INTO category_group(name, expense, exclude, system, household_id) VALUES('Default ".insertExpenseText(0, false)."', 0, 0, 1, (SELECT max(id) FROM household))") or die();
dbUpdate("INSERT INTO category(name, category_group_id) VALUES('Default', (SELECT max(id) FROM category_group))") or die();
dbUpdate("INSERT INTO category_group(name, expense, exclude, system, household_id) VALUES('Default ".insertExpenseText(1, false)."', 1, 0, 1, (SELECT max(id) FROM household))") or die();
dbUpdate("INSERT INTO category(name, category_group_id) VALUES('Default', (SELECT max(id) FROM category_group))") or die();
requestOk("Created $rowCount household");
}
closeDatabaseConnection();
} else if($action == "save-household") {
verifyAuthorized();
$name = getMandatoryRequestValue("name", "Name cannot be empty");
$currency = getMandatoryRequestValue("currency", "Currency cannot be empty");
initDatabaseConnection();
$rowCount = dbUpdate("
UPDATE household
SET name = ?,
currency = ?
WHERE id = ?
",
$name,
$currency,
getHouseHoldId()
);
closeDatabaseConnection();
if($rowCount) {
updateHouseholdInfo($name, $currency);
requestOk("$rowCount household updated");
}
} else if($action == "save-account") {
verifyAuthorized();
$id = getMandatoryRequestValue("id");
$name = getMandatoryRequestValue("name", "Name cannot be empty");
initDatabaseConnection();
$rowCount = dbUpdate("
UPDATE account
SET name = ?
WHERE id = ?
AND household_id = ?
",
$name,
$id,
getHouseHoldId()
);
closeDatabaseConnection();
if($rowCount) {
requestOk("$rowCount account updated");
}
} else if($action == "add-account") {
verifyAuthorized();
$name = getMandatoryRequestValue("name", "Name cannot be empty");
initDatabaseConnection();
$rowCount = dbUpdate("
INSERT INTO account(
household_id,
name
) VALUES (
?,
?
)",
getHouseHoldId(),
$name
);
closeDatabaseConnection();
if($rowCount) {
requestOk("$rowCount account created");
}
} else {
requestFail("Unknown request action \"$action\"");
}
?>