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 "
\nDonation " . $donation->getStrongLink($donation->getId()) . ": " . get_donation_displayname($donation); /* XXX: Should pull from query. */ $q = new ContactQuery; $contact = $q->findOneById($donation->getContactId()); if ($contact) echo " from " . $contact->getLink(); $q = new HubQuery; $hub = $q->findOneById($donation->getHubId()); if ($hub) { echo " to " . $hub->getLink(); $area = get_hub_area($hub); if ($area) { echo " in " . $area->getLink(); $city = get_area_city($area); if ($city) echo ", " . $city->getLink(get_city_displayname($city)); } } if (check_admin(1)) { echo " " . $donation->getDeleteLink(); } } show_pagination($donations); } else echo " none"; } function show_city_donations($city_name, $city_id = null) { if (isset($city_id)) $city = get_city_by_id($city_id); else if ($city_name) $city = get_city_by_name($city_name); if ($city) { $hubs = get_city_hubs($city->getId()); $hub_ids = array(); foreach ($hubs as $hub) $hub_ids[] = $hub->getId(); 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 "\n"; echo " Date\n"; echo " "; show_date_form("date", $donation->getDate()); echo "\n"; echo "\n"; /* Contact. */ echo "\n"; echo " Donor\n"; echo " \n"; echo "\n"; /* Hub. */ echo "\n"; echo " Hub\n"; echo " \n"; echo "\n"; /* Quantity. */ echo "\n"; echo " Quantity (kg)\n"; echo " "; input("quantity", sprintf("%0.2f", $donation->getQuantity() / 1000)); echo "\n"; echo "\n"; } function show_new_donation_form($area_id = null) { if (! check_admin(1)) return; $area = get_area_by_id($area_id); if (! count($area)) { echo "

No such area!

\n"; return; } form(); echo "

Record a donation:

\n"; echo "\n"; show_donation_form(null, $area_id); echo "\n"; echo " \n"; echo "\n"; echo "
"; submit("add_donation", "Record"); echo "
\n"; end_form(); } function show_hub_donation_form($hub) { if (! check_admin(1)) return; $area = get_hub_area($hub); if (! $area) { echo "

No such area!

\n"; return; } $donation = new Donation; $donation->setHubId($hub->getId()); form(); echo "

Record a donation:

\n"; echo "\n"; show_donation_form($donation, $area->getId()); echo "\n"; echo " \n"; echo "\n"; echo "
"; submit("add_donation", "Record"); echo "
\n"; end_form(); } function show_add_new_donation_form() { if (! check_admin(1)) return; /* We intentionally hide areas with no hubs. */ $areas = get_city_areas_with_hubs(); if (! count($areas)) { 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 "

Confirm deletion

\n"; 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 "\n"; show_donation_form($donation); if (check_admin(1)) { echo "\n"; echo " \n"; echo "\n"; } echo "
"; submit("update_donation", "Update"); echo "
\n"; end_form(); } if (count($parameters)) { if ($parameters[0] == "in") { /* /donation/in/area/Romsey+Town/1 */ switch ($parameters[1]) { case "area": $area_id = $parameters[3]; $_POST['area_id'] = $area_id; $q = new AreaQuery; $area = $q->findOneById($area_id); $city = get_area_city($area); if ($city) $city_id = $city->getId(); show_area_donations($parameters[2], $area_id); break; case "city": $city_id = $parameters[3]; $_POST['city_id'] = $city_id; $q = new CityQuery; $city = $q->findOneById($city_id); show_city_donations($parameters[2], $city_id); break; } } else if ($parameters[0] == "from") { /* /donation/from/contact/Iain+Patterson/4 */ switch ($parameters[1]) { case "contact": $contact_id = $parameters[3]; $q = new ContactQuery; $contact = $q->findOneById($contact_id); show_contact_donations($parameters[2], $contact_id); break; } } else if ($parameters[0] == "to") { /* /donation/to/hub/Cambridge+Community+Church/1 */ switch ($parameters[1]) { case "hub": $hub_id = $parameters[3]; $q = new HubQuery; $hub = $q->findOneById($hub_id); show_hub_donations($parameters[2], $hub_id); break; } } else if ($parameters[0] == "record") { if ($parameters[1] == "to") { if ($parameters[2] == "hub") { if ($parameters[4]) $hub = get_hub_by_id($parameters[4]); if (! $hub) $hub = get_hub_by_name(urldecode($parameters[3])); if ($hub) show_hub_donation_form($hub); } } } } list($ignored, $id, $args) = parse_parameters($parameters); //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); ?>