Donation recorded.
\n"; $parameters = array("id", $id); } } else if (isset($_POST['update_donation'])) { list($ignored, $id, $args) = parse_parameters($parameters); $q = new DonationQuery; $donation = $q->findOneById($id); if ($donation) { if (update_donation($donation) !== false) { echo "Updated donation.
\n"; $parameters = array("id", $donation->getId()); } } else { echo "No such contact!
\n"; } } else if ($_POST['show_in_area']) { set_last_selected("area_id", $_POST['area_id']); $q = new AreaQuery; $area = $q->findOneById($_POST['area_id']); /* XXX: Function to build URL because we need to set a class in links. */ header(sprintf("Location: http%s://%s/%s/in/area/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($area->getName()), $_POST['area_id'])); exit; } else if ($_POST['show_in_city']) { set_last_selected("city_id", $_POST['city_id']); $q = new CityQuery; $city = $q->findOneById($_POST['city_id']); header(sprintf("Location: http%s://%s/%s/in/city/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($city->getName()), $_POST['city_id'])); exit; } function show_donations($contact_ids = null, $hub_ids = null) { list($first_page, $per_page) = pagination(); $q = new DonationQuery; if (isset($contact_ids)) $q->filterByContactId($contact_ids); if (isset($hub_ids)) $q->filterByHubId($hub_ids); $donations = $q->orderByDate('desc')->orderById('desc')->paginate($first_page, $per_page); if (count($donations)) { foreach ($donations as $donation) { echo "Donations in city " . $city->getLink(get_city_displayname($city)) . ":"; return show_donations(null, $hub_ids); } else echo "
No such city!
\n"; } function show_contact_donations($contact_name, $contact_id = null) { if (isset($contact_id)) $contact = get_contact_by_id($contact_id); else if ($contact_name) $contact = get_contact_by_name($contact_name); if ($contact) { echo "Donations from contact " . $contact->getLink() . ":"; return show_donations($contact->getId()); } else echo "
No such contact!
\n"; } function show_hub_donations($hub_name, $hub_id = null) { if (isset($hub_id)) $hub = get_hub_by_id($hub_id); else if ($hub_name) $hub = get_hub_by_name($hub_name); if ($hub) { echo "Donations to hub " . $hub->getLink() . ":"; return show_donations(null, $hub->getId()); } else echo "
No such hub!
\n"; } function show_area_donations($area_name, $area_id = null) { if (isset($area_id)) $area = get_area_by_id($area_id); else if ($area_name) $area = get_area_by_name($area_name); if ($area) { $hubs = get_area_hubs($area->getId()); $hub_ids = array(); foreach ($hubs as $hub) $hub_ids[] = $hub->getId(); echo "Donations in area " . $area->getLink() . ":"; return show_donations(null, $hub_ids); } else echo "
No such area!
\n"; } function show_donation_areas_form($city_id = null) { $areas = get_city_areas($city_id); if (! count($areas)) { echo "No areas!
\n"; return; } form("noprint standout"); $candidates = array(); foreach ($areas as $area) { if (! count(get_area_hubs($area->getId()))) continue; $candidates[] = $area; } if (! count($candidates)) return; echo "Show donations in area\n"; echo "\n"; submit("show_in_area", "Show"); } function show_donation_cities_form($city_id = null) { $q = new CityQuery; $cities = $q->orderByName()->find(); if (! count($cities)) { echo "
No cities!
\n"; return; } $candidates = array(); foreach ($cities as $city) { if (! count(get_city_hubs($city->getId()))) continue; $candidates[] = $city; } if (! count($candidates)) return; echo "Show donations in city\n"; echo "\n"; submit("show_in_city", "Show"); } function show_donation_forms($city_id) { form("noprint standout"); show_donation_areas_form($city_id); show_donation_cities_form($city_id); end_form(); } function show_donation_form($donation = null, $area_id = null) { if (! $donation) $donation = new Donation; /* Date. */ echo "
No such area!
\n"; return; } form(); echo "Record a donation:
\n"; echo ""; submit("add_donation", "Record"); echo " |
No such area!
\n"; return; } $donation = new Donation; $donation->setHubId($hub->getId()); form(); echo "Record a donation:
\n"; echo ""; submit("add_donation", "Record"); echo " |
Can't record any donations until at least one area has a hub!
\n"; return; } form("noprint standout"); echo "Record a donation in "; submit("show_add_donation", "Proceed"); echo "
\n"; end_form(); } function update_donation(&$donation, $new = false) { $date = $_POST['date']; $contact_id = $_POST['contact_id']; $hub_id = $_POST['hub_id']; $quantity = $_POST['quantity']; if (! $date) $date = time(); /* XXX: check date */ $contact = get_contact_by_id($contact_id); if (! $contact) { echo "Invalid contact!
\n"; return false; } $hub = get_hub_by_id($hub_id); if (! $hub) { echo "Invalid hub!
\n"; return false; } if (! is_numeric($quantity)) { echo "Invalid quantity!
\n"; return false; } $donation->setDate($date); $donation->setContactId($contact_id); $donation->setHubId($hub_id); $donation->setQuantity($quantity * 1000); try { $donation->save(); } catch (Exception $e) { if ($new) echo "Error recording donation.
\n"; else echo "Error updating donation.
\n"; return false; } return true; } function add_donation() { if (! check_admin(1, "record a donation")) return; $donation = new Donation; if (! update_donation($donation, true)) return false; return $donation->getId(); } function confirm_delete_donation($id = null) { if (! check_admin(1, "delete a donation")) return; if (isset($id)) $donation = get_donation_by_id($id); if (! $donation) return false; echo "You must confirm deletion of donation $id: " . $donation->getDeleteLink(true) . "
\n"; } function delete_donation($id = null) { if (! check_admin(1, "delete a donation")) return; if (isset($id)) $donation = get_donation_by_id($id); if (! $donation) return false; try { $donation->delete(); echo "Deleted donation.
\n"; } catch (Exception $e) { echo "Error deleting donation $id!
\n"; return false; } return true; } function show_donation(&$id = null) { if (isset($id)) $donation = get_donation_by_id($id); if (! $donation) return; form(); echo "Donation " . $donation->getId() . ""; if (check_admin(1)) { echo " " . $donation->getDeleteLink(); } echo ": "; echo "\n
"; echo ""; submit("update_donation", "Update"); echo " | \n"; echo "
$name($id) " . print_r($args, true) . "
\n"; if (count($args)) { switch ($args[0]) { case "delete": confirm_delete_donation($id); break; case "confirmdelete": delete_donation($id); break; } } else if (isset($id)) show_donation($id); show_donation_forms($city_id); show_add_new_donation_form($city_id); ?>