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['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['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($offset, $per_page, $contact_ids = null, $hub_ids = null) { $q = new DonationQuery; if (isset($contact_ids)) $q->filterByContactId($contact_ids); if (isset($hub_ids)) $q->filterByHubId($hub_ids); $donations = $q->find(); if (count($donations)) { foreach ($donations as $donation) { echo "Donations in city " . $city->getLink(get_city_displayname($city)) . ":"; return show_donations($offset, $per_page, null, $hub_ids); } else echo "
No such city!
\n"; } function show_contact_donations($offset, $per_page, $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($offset, $per_page, $contact->getId()); } else echo "
No such contact!
\n"; } function show_hub_donations($offset, $per_page, $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($offset, $per_page, null, $hub->getId()); } else echo "
No such hub!
\n"; } function show_area_donations($offset, $per_page, $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($offset, $per_page, 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; } echo "\n"; } function show_donation_cities_form($city_id = null) { $q = new CityQuery; $cities = $q->orderByName()->find(); if (! count($cities)) { echo "No cities!
\n"; return; } echo "\n"; } function show_donation_forms($city_id) { show_donation_areas_form($city_id); show_donation_cities_form($city_id); } function show_donation_form($donation = null, $area_id = null) { if (! $donation) $donation = new Donation; /* Date. */ echo "No such area!
\n"; return; } echo "\n"; } 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; } echo "\n"; } function update_donation(&$donation, $new = false) { #$date = ymd_to_iso8601("date"); $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 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; echo "\n"; } 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(0, 10, $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(0, 10, $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(0, 10, $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(0, 10, $parameters[2], $hub_id); break; } } } list($ignored, $id, $args) = parse_parameters($parameters); //echo "$name($id) " . print_r($args, true) . "
\n"; if (count($args)) { switch ($args[0]) { case "delete": delete_donation($id); break; } } else if (isset($id)) show_donation($id); else { /* XXX: Shown after adding. */ show_donation_forms($city_id); show_add_new_donation_form($city_id); } if (count($parameters)) { show_donation_forms($city_id); } ?>