Add member editing and deleting (close #5)

master
Skylar Ittner 5 years ago
parent c5f8b57b40
commit b1fafb57dc

@ -7,7 +7,6 @@
/**
* Make things happen when buttons are pressed and forms submitted.
*/
require_once __DIR__ . "/required.php";
if ($VARS['action'] !== "signout") {
@ -40,5 +39,161 @@ switch ($VARS['action']) {
returnToSender("no_permission");
}
function errorBack(string $errormsg) {
returnToSender($errormsg);
}
$family = new Family();
$editing = false;
if (!empty($VARS['familyid']) && $database->has("families", ['familyid' => $VARS['familyid']])) {
$family = (new Family())->load($VARS['familyid']);
$editing = true;
}
$database->action(function($database) {
global $family, $VARS, $editing;
try {
$lastname = $VARS['familyname'];
$father = $VARS['fathername'];
$mother = $VARS['mothername'];
if (empty($lastname)) {
errorBack("Enter a last name.");
}
if (empty($father)) {
errorBack("Enter a father name.");
}
if (empty($mother)) {
errorBack("Enter a mother name.");
}
$family->setName($lastname);
$family->setFather($father);
$family->setMother($mother);
$family->setPhone($VARS['phone']);
$family->setEmail($VARS['email']);
$address = $VARS['streetaddress'];
$city = $VARS['city'];
$state = strtoupper($VARS['state']);
$zip = $VARS['zip'];
if (empty($address)) {
errorBack("Enter a street address.");
}
if (empty($city)) {
errorBack("Enter a city.");
}
$family->setAddress($address);
$family->setCity($city);
$family->setState($state);
$family->setZip($zip);
$newsletter = $VARS['newsletter_method'];
$membership_cost = 2500;
if (empty($newsletter)) {
errorBack("Select a newsletter preference.");
}
$family->setNewsletter($newsletter);
switch ($newsletter) {
case 1: // Email only
$membership_cost = 2500;
break;
case 2: // Print only
$membership_cost = 3500;
break;
case 3: // Email and print
$membership_cost = 3500;
break;
default:
errorBack("Select a valid newsletter preference.");
}
$photopermission = $VARS['photo_permission'];
if (!empty($photopermission) && $photopermission == "1") {
$photopermission = true;
} else {
$photopermission = false;
}
$family->setPhotoPermission($photopermission);
$family->save();
//
// Children
//
$children = $VARS['child'];
$childObjects = $family->getChildren();
$childrenToDelete = [];
foreach ($children['ids'] as $cid) {
if (empty($children['name'][$cid])) {
$childrenToDelete[] = $cid;
continue;
}
if (!preg_match("/^([1-9]|1[012])$/", $children['month'][$cid])) {
errorBack("Invalid birth month chosen for " . htmlentities($children['name'][$cid]) . ".");
}
if (!is_numeric($children['year'][$cid])) {
errorBack("Invalid birth year chosen for " . htmlentities($children['name'][$cid]) . ".");
}
$children['year'][$cid] = $children['year'][$cid] * 1;
if ($children['year'][$cid] < 1980 || $children['year'][$cid] > date("Y")) {
errorBack("Invalid birth year chosen for " . htmlentities($children['name'][$cid]) . ".");
}
if (Child::exists($cid, $family->getID())) {
// iterate over existing children to find the correct one
for ($i = 0; $i < count($childObjects); $i++) {
if ($childObjects[$i]->getID() == $cid) {
$childObjects[$i]->setName($children['name'][$cid]);
$childObjects[$i]->setBirthday(null, $children['year'][$cid] . "-" . $children['month'][$cid] . "-00");
$childObjects[$i]->setGraduated(empty($children['graduate'][$cid]) ? false : true);
}
}
} else {
$child = new Child();
$child->setName($children['name'][$cid]);
$child->setBirthday(null, $children['year'][$cid] . "-" . $children['month'][$cid] . "-00");
$child->setGraduated(empty($children['graduate'][$cid]) ? false : true);
$child->setFamilyID($family->getID());
$childObjects[] = $child;
}
}
foreach ($childObjects as $child) {
$child->save();
}
foreach ($childrenToDelete as $rip) {
$database->delete("people", ['personid' => $rip]);
}
} catch (Exception $ex) {
errorBack($ex->getMessage());
}
});
returnToSender("family_saved");
break;
case "deletefamily":
if (!(new User($_SESSION['uid']))->hasPermission("HACHEPORTAL_EDIT")) {
returnToSender("no_permission");
}
if (!empty($VARS['familyid']) && $database->count("families", ['familyid' => $VARS['familyid']]) === 1) {
$database->delete("people", ["familyid" => $VARS['familyid']]);
$database->delete("payments", ["familyid" => $VARS['familyid']]);
$database->delete("families", ["familyid" => $VARS['familyid']]);
returnToSender("family_deleted");
} else {
returnToSender("family_doesnt_exist");
}
break;
}

@ -2,5 +2,8 @@
"Add Family": "Add Family",
"Edit": "Edit",
"View": "View",
"Save": "Save",
"Delete": "Delete",
"Cancel": "Cancel",
"Edit Family": "Edit Family"
}

@ -18,5 +18,8 @@
"Email and Print": "Email and Print",
"Children": "Children",
"Born": "Born",
"Graduated": "Graduated"
"Graduated": "Graduated",
"Okay to use photos?": "Okay to use photos?",
"Adding Family": "Adding Family",
"Editing Family": "Editing {family} Family"
}

@ -1,4 +1,9 @@
{
"You do not have permission to do that.": "You do not have permission to do that.",
"That family ID does not exist.": "That family ID does not exist."
"That family ID does not exist.": "That family ID does not exist.",
"Family saved.": "Family saved.",
"Family deleted.": "Family deleted.",
"Are you sure you want to delete this family?": "Are you sure you want to delete this family?",
"This action cannot be undone! All information about this family, including payment history, will be purged forever.": "This action cannot be undone! All information about this family, including payment history, will be purged forever.",
"To remove a child, delete the contents of the Name box.": "To remove a child, delete the contents of the Name box."
}

@ -3,5 +3,6 @@
"Families": "Families",
"Members": "Members",
"View Family": "View Family",
"Family": "Family"
"Family": "Family",
"Delete Family": "Delete Family"
}

@ -24,5 +24,13 @@ define("MESSAGES", [
"family_doesnt_exist" => [
"string" => "That family ID does not exist.",
"type" => "warning"
],
"family_saved" => [
"string" => "Family saved.",
"type" => "success"
],
"family_deleted" => [
"string" => "Family deleted.",
"type" => "success"
]
]);

@ -0,0 +1,89 @@
<?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/.
*/
require_once __DIR__ . "/../required.php";
$childinfo = ['name' => '', 'month' => 1, 'year' => date('Y', strtotime('now - 10 years')), 'graduated' => false];
if (isset($childid) && $database->has('people', ['personid' => $childid])) {
$randomid = $childid;
$chinfo = $database->get('people', ['name', 'birthday', 'graduated'], ['personid' => $childid]);
$childinfo['name'] = $chinfo['name'];
$childinfo['graduated'] = $chinfo['graduated'] == true;
$childinfo['month'] = date('m', strtotime($chinfo['birthday']));
$childinfo['year'] = date('Y', strtotime($chinfo['birthday']));
} else {
do {
$randomid = mt_rand(0, 9999999999);
} while ($database->has('people', ['personid' => $randomid]));
}
?>
<div class="list-group-item">
<input type="hidden" name="child[ids][]" value="<?php echo $randomid; ?>" />
<div class="row">
<div class="col-12 col-sm-4">
<div class="form-group">
<label>Name:</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-user-graduate"></i></span>
</div>
<input type="text" name="child[name][<?php echo $randomid; ?>]" class="form-control" value="<?php echo htmlspecialchars($childinfo['name']); ?>" />
</div>
</div>
</div>
<div class="col-12 col-sm-3">
<div class="form-group">
<label>Birth month:</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-calendar"></i></span>
</div>
<select name="child[month][<?php echo $randomid; ?>]" class="form-control" value="<?php echo $childinfo['month']; ?>" >
<?php
for ($i = 1; $i <= 12; $i++) {
$selected = "";
if ($childinfo['month'] == $i) {
$selected = " selected";
}
echo "<option value=$i$selected>" . date("F", mktime(0, 0, 0, $i, 2)) . "</option>\n";
}
?>
</select>
</div>
</div>
</div>
<div class="col-12 col-sm-3">
<div class="form-group">
<label>Birth year:</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-calendar-alt"></i></span>
</div>
<input type="number" name="child[year][<?php echo $randomid; ?>]" class="form-control" min="1980" max="<?php echo date('Y'); ?>" value="<?php echo $childinfo['year']; ?>"/>
</div>
</div>
</div>
<div class="col-12 col-sm-2">
<div class="form-group">
<label>&nbsp;</label>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="1" name="child[graduate][<?php echo $randomid; ?>]"<?php
if ($childinfo['graduated']) {
echo " checked";
}
?>>
<label class="form-check-label mt-1">Graduated</label>
</div>
</div>
</div>
</div>
</div>

@ -27,6 +27,15 @@ define("PAGES", [
"viewfamily" => [
"title" => "View Family"
],
"editfamily" => [
"title" => "Edit Family",
"scripts" => [
"static/js/editfamily.js"
],
],
"confirmdelete" => [
"title" => "Delete Family"
],
"404" => [
"title" => "404 error"
]

@ -0,0 +1,59 @@
<?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/. */
require_once __DIR__ . "/../required.php";
redirectifnotloggedin();
if (!is_empty($VARS['id'])) {
if ($database->has('families', ['familyid' => $VARS['id']])) {
$family = (new Family())->load($VARS['id']);
} else {
header('Location: app.php?page=families&msg=family_doesnt_exists');
die();
}
} else {
header('Location: app.php?page=families&msg=family_doesnt_exists');
die();
}
?>
<div class="row justify-content-center">
<div class="col-12 col-sm-6 col-sm-offset-3">
<div class="card border-red text-center">
<h3 class="card-header text-red">
<?php $Strings->get("Delete Family"); ?>
</h3>
<div class="card-body">
<p><i class="fas fa-exclamation-triangle fa-10x"></i></p>
<h4><?php $Strings->get("Are you sure you want to delete this family?") ?></h4>
<h5><?php $Strings->get("This action cannot be undone! All information about this family, including payment history, will be purged forever."); ?></h5>
<div class="list-group">
<div class="list-group-item">
<i class="fas fa-fw fa-users"></i> <?php echo $family->getName(); ?>
</div>
<div class="list-group-item">
<i class="fas fa-fw fa-male"></i> <?php echo $family->getFather(); ?>
</div>
<div class="list-group-item">
<i class="fas fa-fw fa-female"></i> <?php echo $family->getMother(); ?>
</div>
<?php
foreach ($family->getChildren() as $child) {
?>
<div class="list-group-item">
<i class="fas fa-fw fa-user-graduate"></i> <?php echo $child->getName(); ?>
</div>
<?php
}
?>
</div>
</div>
<div class="card-footer d-flex">
<a href="app.php?page=families" class="btn btn-primary mr-auto"><i class="fas fa-arrow-left"></i> <?php $Strings->get('Cancel'); ?></a>
<a href="action.php?action=deletefamily&source=families&familyid=<?php echo htmlspecialchars($VARS['id']); ?>" class="btn btn-danger"><i class="fas fa-times"></i> <?php $Strings->get('Delete'); ?></a>
</div>
</div>
</div>
</div>

@ -0,0 +1,318 @@
<?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/.
*/
redirectIfNotLoggedIn();
$user = new User($_SESSION['uid']);
if (!$user->hasPermission("HACHEPORTAL_EDIT")) {
header("Location: ./app.php?msg=no_permission");
die();
}
$editing = false;
if (empty($VARS['id']) || !$database->has('families', ['familyid' => $VARS['id']])) {
$family = new Family();
} else {
$famid = $VARS['id'];
$family = (new Family())->load($famid);
$editing = true;
}
?>
<form action="action.php" method="post">
<div class="card">
<h3 class="card-header d-flex">
<div>
<?php
if ($editing) {
?>
<i class="fas fa-edit"></i> <?php $Strings->build("Editing Family", ['family' => "<span id=\"name_title\">" . htmlentities($family->getName()) . "</span>"]); ?>
<?php
} else {
?>
<i class="fas fa-edit"></i> <?php $Strings->get("Adding Family"); ?>
<?php
}
?>
</div>
<div class="ml-auto btn btn-outline-info btn-sm" id="cancelbtn">
<i class="fas fa-times"></i> <?php $Strings->get("Cancel"); ?>
</div>
</h3>
<div class="card-body">
<h4><i class="fas fa-info fa-fw"></i> Basic Information</h4>
<div class="row">
<?php
$textboxes = [
[
"label" => "Family Name (Last Name)",
"icon" => "fas fa-users",
"name" => "familyname",
"maxlength" => 100,
"value" => $family->getName()
],
[
"label" => "Father's Name",
"icon" => "fas fa-male",
"name" => "fathername",
"maxlength" => 255,
"value" => $family->getFather()
],
[
"label" => "Mother's Name",
"icon" => "fas fa-female",
"name" => "mothername",
"maxlength" => 255,
"value" => $family->getMother()
],
[
"label" => "Street Address",
"icon" => "fas fa-home",
"name" => "streetaddress",
"maxlength" => 500,
"value" => $family->getAddress()
],
[
"label" => "City",
"icon" => "fas fa-city",
"name" => "city",
"maxlength" => 255,
"width" => 3,
"value" => $family->getCity()
],
[
"label" => "State",
"icon" => "fas fa-flag",
"name" => "state",
"type" => "select",
"value" => $family->getState(),
"options" => [
'MT' => 'Montana',
'AL' => 'Alabama',
'AK' => 'Alaska',
'AZ' => 'Arizona',
'AR' => 'Arkansas',
'CA' => 'California',
'CO' => 'Colorado',
'CT' => 'Connecticut',
'DE' => 'Delaware',
'DC' => 'District of Columbia',
'FL' => 'Florida',
'GA' => 'Georgia',
'HI' => 'Hawaii',
'ID' => 'Idaho',
'IL' => 'Illinois',
'IN' => 'Indiana',
'IA' => 'Iowa',
'KS' => 'Kansas',
'KY' => 'Kentucky',
'LA' => 'Louisiana',
'ME' => 'Maine',
'MD' => 'Maryland',
'MA' => 'Massachusetts',
'MI' => 'Michigan',
'MN' => 'Minnesota',
'MS' => 'Mississippi',
'MO' => 'Missouri',
'MT' => 'Montana',
'NE' => 'Nebraska',
'NV' => 'Nevada',
'NH' => 'New Hampshire',
'NJ' => 'New Jersey',
'NM' => 'New Mexico',
'NY' => 'New York',
'NC' => 'North Carolina',
'ND' => 'North Dakota',
'OH' => 'Ohio',
'OK' => 'Oklahoma',
'OR' => 'Oregon',
'PA' => 'Pennsylvania',
'RI' => 'Rhode Island',
'SC' => 'South Carolina',
'SD' => 'South Dakota',
'TN' => 'Tennessee',
'TX' => 'Texas',
'UT' => 'Utah',
'VT' => 'Vermont',
'VA' => 'Virginia',
'WA' => 'Washington',
'WV' => 'West Virginia',
'WI' => 'Wisconsin',
'WY' => 'Wyoming'
],
"width" => 2
],
[
"label" => "ZIP/Postal Code",
"icon" => "fas fa-mail-bulk",
"name" => "zip",
"maxlength" => 10,
"width" => 3,
"value" => $family->getZip()
],
[
"label" => "Phone Number",
"icon" => "fas fa-phone",
"name" => "phone",
"maxlength" => 20,
"value" => $family->getPhone()
],
[
"label" => "Email",
"icon" => "fas fa-at",
"name" => "email",
"maxlength" => 255,
"type" => "email",
"value" => $family->getEmail()
],
[
"label" => "Newsletter Preference",
"icon" => "fas fa-newspaper",
"name" => "newsletter_method",
"type" => "select",
"value" => $family->getNewsletter(),
"options" => [
"1" => "Email ($25)",
"2" => "Paper ($35)",
"3" => "Email and Paper ($35)"
]
]
];
foreach ($textboxes as $item) {
?>
<div class="col-12 col-md-<?php echo (empty($item['width']) ? "4" : $item['width']); ?>">
<div class="form-group mb-3">
<label class="mb-0"><?php echo $item['label']; ?>:</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="<?php echo $item['icon']; ?>"></i></span>
</div>
<?php if (empty($item['type']) || $item['type'] != "select") { ?>
<input type="<?php echo (empty($item['type']) ? "text" : $item['type']); ?>"
name="<?php echo $item['name']; ?>"
class="form-control"
placeholder=""
aria-label="<?php echo $item['label']; ?>"
maxlength="<?php echo $item['maxlength']; ?>"
<?php
if (!empty($item['value'])) {
?>
value="<?php echo htmlspecialchars($item['value']); ?>"
<?php
}
?>required />
<?php } else if ($item['type'] == "select") { ?>
<select class="form-control"
name="<?php echo $item['name']; ?>"
aria-label="<?php echo $item['label']; ?>"
required>
<?php
foreach ($item['options'] as $value => $label) {
$selected = "";
if (!empty($item['value']) && $value == $item['value']) {
$selected = " selected";
}
echo "<option value=\"$value\"$selected>$label</option>\n";
}
?>
</select>
<?php
}
?>
</div>
</div>
</div>
<?php
}
?>
</div>
<hr />
<h4><i class="fas fa-user-graduate fa-fw"></i> Children</h4>
<p class="card-text ml-2 mb-1">
<i class="fas fa-info-circle"></i> <?php $Strings->get("To remove a child, delete the contents of the Name box."); ?>
<div class="list-group" id="child_list">
<?php
if (count($family->getChildren()) > 0) {
foreach ($family->getChildren() as $child) {
$childid = $child->getID();
include __DIR__ . "/../lib/template_child_entry.php";
}
} else {
include __DIR__ . "/../lib/template_child_entry.php";
}
?>
</div>
<div class="btn btn-sm btn-primary mt-1" id="add_child_row">
<i class="fas fa-plus"></i> Add another
</div>
<hr />
<h4><i class="fas fa-check-circle fa-fw"></i> Consent</h4>
<p class="mb-0">
<?php $Strings->get("Okay to use photos?"); ?>
<span class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="photo_permission" id="photo_permission" value="1" <?php
if ($family->getPhotoPermission()) {
echo "checked";
}
?> required>
<label class="form-check-label" for="photo_permission">Yes</label>
</span>
<span class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="photo_permission" id="photo_permission" value="0" <?php
if (!$family->getPhotoPermission()) {
echo "checked";
}
?> required>
<label class="form-check-label" for="photo_permission">No</label>
</span>
</div>
<?php
if ($editing) {
?>
<input type="hidden" name="familyid" value="<?php echo $family->getID(); ?>" />
<?php
}
?>
<input type="hidden" name="action" value="editfamily" />
<input type="hidden" name="source" value="families" />
<div class="card-footer d-flex">
<button type="submit" class="btn btn-success mr-1">
<i class="fas fa-save"></i> <?php $Strings->get("Save"); ?>
</button>
<?php if ($editing) { ?>
<a href="./app.php?page=confirmdelete&id=<?php echo $family->getID(); ?>" class="btn btn-danger ml-auto">
<i class="fas fa-times"></i> <?php $Strings->get("Delete"); ?>
</a>
<?php } ?>
</div>
</div>
</form>

@ -0,0 +1,19 @@
/*
* 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/.
*/
$("input[name=familyname]").on("input propertychange paste", function () {
$('#name_title').text($(this).val());
});
$("#add_child_row").click(function () {
$.get("lib/template_child_entry.php", {}, function (resp) {
$("#child_list").append(resp);
});
});
$("#cancelbtn").click(function () {
history.back();
});
Loading…
Cancel
Save