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])); +} +?> + +
+