From b1fafb57dc04c07caac3d3fa421652d3fc12ba4b Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Tue, 4 Dec 2018 20:49:46 -0700 Subject: [PATCH] Add member editing and deleting (close #5) --- action.php | 159 +++++++++++++++++- langs/en/actions.json | 3 + langs/en/labels.json | 5 +- langs/en/messages.json | 7 +- langs/en/titles.json | 3 +- langs/messages.php | 8 + lib/template_child_entry.php | 89 ++++++++++ pages.php | 9 + pages/confirmdelete.php | 59 +++++++ pages/editfamily.php | 318 +++++++++++++++++++++++++++++++++++ static/js/editfamily.js | 19 +++ 11 files changed, 674 insertions(+), 5 deletions(-) create mode 100644 lib/template_child_entry.php create mode 100644 pages/confirmdelete.php create mode 100644 pages/editfamily.php create mode 100644 static/js/editfamily.js diff --git a/action.php b/action.php index 55d0320..1e35883 100644 --- a/action.php +++ b/action.php @@ -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; } \ No newline at end of file diff --git a/langs/en/actions.json b/langs/en/actions.json index f3108f4..ebeb7b0 100644 --- a/langs/en/actions.json +++ b/langs/en/actions.json @@ -2,5 +2,8 @@ "Add Family": "Add Family", "Edit": "Edit", "View": "View", + "Save": "Save", + "Delete": "Delete", + "Cancel": "Cancel", "Edit Family": "Edit Family" } diff --git a/langs/en/labels.json b/langs/en/labels.json index c93a809..251cde4 100644 --- a/langs/en/labels.json +++ b/langs/en/labels.json @@ -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" } diff --git a/langs/en/messages.json b/langs/en/messages.json index 459a168..5902e24 100644 --- a/langs/en/messages.json +++ b/langs/en/messages.json @@ -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." } diff --git a/langs/en/titles.json b/langs/en/titles.json index 5e833c9..444d329 100644 --- a/langs/en/titles.json +++ b/langs/en/titles.json @@ -3,5 +3,6 @@ "Families": "Families", "Members": "Members", "View Family": "View Family", - "Family": "Family" + "Family": "Family", + "Delete Family": "Delete Family" } diff --git a/langs/messages.php b/langs/messages.php index cfa920c..4fec34e 100644 --- a/langs/messages.php +++ b/langs/messages.php @@ -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" ] ]); diff --git a/lib/template_child_entry.php b/lib/template_child_entry.php new file mode 100644 index 0000000..390c3d6 --- /dev/null +++ b/lib/template_child_entry.php @@ -0,0 +1,89 @@ + '', '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])); +} +?> + +
+ +
+
+
+ +
+
+ +
+ +
+
+
+ +
+
+ +
+
+ +
+ +
+
+
+ +
+
+ +
+
+ +
+ +
+
+
+ +
+
+ +
+ > + +
+
+
+
+
\ No newline at end of file diff --git a/pages.php b/pages.php index 294c364..64ca7bf 100644 --- a/pages.php +++ b/pages.php @@ -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" ] diff --git a/pages/confirmdelete.php b/pages/confirmdelete.php new file mode 100644 index 0000000..440eab8 --- /dev/null +++ b/pages/confirmdelete.php @@ -0,0 +1,59 @@ +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(); +} +?> +
+
+
+

+ get("Delete Family"); ?> +

+
+

+

get("Are you sure you want to delete this family?") ?>

+
get("This action cannot be undone! All information about this family, including payment history, will be purged forever."); ?>
+
+
+ getName(); ?> +
+
+ getFather(); ?> +
+
+ getMother(); ?> +
+ getChildren() as $child) { + ?> +
+ getName(); ?> +
+ +
+
+ +
+
+
\ No newline at end of file diff --git a/pages/editfamily.php b/pages/editfamily.php new file mode 100644 index 0000000..42c0f01 --- /dev/null +++ b/pages/editfamily.php @@ -0,0 +1,318 @@ +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; +} +?> + +
+ +
+ +

+
+ + build("Editing Family", ['family' => "" . htmlentities($family->getName()) . ""]); ?> + + get("Adding Family"); ?> + +
+
+ get("Cancel"); ?> +
+

+ +
+ +

Basic Information

+
+ + "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) { + ?> + +
"> +
+ +
+
+ +
+ + " + name="" + class="form-control" + placeholder="" + aria-label="" + maxlength="" + + value="" + required /> + + + +
+
+
+ + + +
+ + + +
+ + + +

Children

+

+ get("To remove a child, delete the contents of the Name box."); ?> + +

+ 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"; + } + ?> +
+ +
+ Add another +
+ + + +
+ + + +

Consent

+

+ get("Okay to use photos?"); ?> + + getPhotoPermission()) { + echo "checked"; + } + ?> required> + + + + getPhotoPermission()) { + echo "checked"; + } + ?> required> + + +

+ + + + + + + + +
+ +
\ No newline at end of file diff --git a/static/js/editfamily.js b/static/js/editfamily.js new file mode 100644 index 0000000..f66dfc1 --- /dev/null +++ b/static/js/editfamily.js @@ -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(); +}); \ No newline at end of file