3 if (isset($_POST['show_add_donation'])) {
4 $area_id = $_POST['area_id'];
5 show_new_donation_form($area_id);
7 else if (isset($_POST['add_donation'])) {
10 echo "<p>Donation recorded.</p>\n";
11 $parameters = array("id", $id);
14 else if (isset($_POST['update_donation'])) {
15 list($ignored, $id, $args) = parse_parameters($parameters);
16 $q = new DonationQuery;
17 $donation = $q->findOneById($id);
19 if (update_donation($donation) !== false) {
20 echo "<p>Updated donation.</p>\n";
21 $parameters = array("id", $donation->getId());
25 echo "<p>No such contact!</p>\n";
28 else if ($_POST['area_id']) {
30 $area = $q->findOneById($_POST['area_id']);
31 /* XXX: Function to build URL because we need to set a class in links. */
32 header(sprintf("Location: http%s://%s/%s/in/area/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($area->getName()), $_POST['area_id']));
35 else if ($_POST['city_id']) {
37 $city = $q->findOneById($_POST['city_id']);
38 header(sprintf("Location: http%s://%s/%s/in/city/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($city->getName()), $_POST['city_id']));
42 function show_donations($offset, $per_page, $contact_ids = null, $hub_ids = null) {
43 $q = new DonationQuery;
44 if (isset($contact_ids)) $q->filterByContactId($contact_ids);
45 if (isset($hub_ids)) $q->filterByHubId($hub_ids);
46 $donations = $q->find();
47 if (count($donations)) {
48 foreach ($donations as $donation) {
49 echo "<br>\nDonation " . $donation->getStrongLink($donation->getId()) . ": " . get_donation_displayname($donation);
51 /* XXX: Should pull from query. */
52 $q = new ContactQuery;
53 $contact = $q->findOneById($donation->getContactId());
54 if ($contact) echo " from " . $contact->getLink();
57 $hub = $q->findOneById($donation->getHubId());
59 echo " to " . $hub->getLink();
60 $area = get_hub_area($hub);
62 echo " in " . $area->getLink();
63 $city = get_area_city($area);
64 if ($city) echo ", " . $city->getLink(get_city_displayname($city));
68 echo " " . $donation->getDeleteLink();
75 function show_city_donations($offset, $per_page, $city_name, $city_id = null) {
76 if (isset($city_id)) $city = get_city_by_id($city_id);
77 else if ($city_name) $city = get_city_by_name($city_name);
79 $hubs = get_city_hubs($city->getId());
81 foreach ($hubs as $hub) $hub_ids[] = $hub->getId();
83 echo "<p>Donations in city " . $city->getLink(get_city_displayname($city)) . ":";
84 return show_donations($offset, $per_page, null, $hub_ids);
86 else echo "<p>No such city!</p>\n";
89 function show_contact_donations($offset, $per_page, $contact_name, $contact_id = null) {
90 if (isset($contact_id)) $contact = get_contact_by_id($contact_id);
91 else if ($contact_name) $contact = get_contact_by_name($contact_name);
93 echo "<p>Donations from contact " . $contact->getLink() . ":";
94 return show_donations($offset, $per_page, $contact->getId());
96 else echo "<p>No such contact!</p>\n";
99 function show_hub_donations($offset, $per_page, $hub_name, $hub_id = null) {
100 if (isset($hub_id)) $hub = get_hub_by_id($hub_id);
101 else if ($hub_name) $hub = get_hub_by_name($hub_name);
103 echo "<p>Donations to hub " . $hub->getLink() . ":";
104 return show_donations($offset, $per_page, null, $hub->getId());
106 else echo "<p>No such hub!</p>\n";
109 function show_area_donations($offset, $per_page, $area_name, $area_id = null) {
110 if (isset($area_id)) $area = get_area_by_id($area_id);
111 else if ($area_name) $area = get_area_by_name($area_name);
113 $hubs = get_area_hubs($area->getId());
115 foreach ($hubs as $hub) $hub_ids[] = $hub->getId();
117 echo "<p>Donations in area " . $area->getLink() . ":";
118 return show_donations($offset, $per_page, null, $hub_ids);
120 else echo "<p>No such area!</p>\n";
123 function show_donation_areas_form($city_id = null) {
124 $areas = get_city_areas($city_id);
125 if (! count($areas)) {
126 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
130 form("noprint standout");
131 $candidates = array();
132 foreach ($areas as $area) {
133 if (! count(get_area_hubs($area->getId()))) continue;
134 $candidates[] = $area;
136 if (! count($candidates)) return;
138 echo "<p>Show donations in area\n";
139 echo "<select name=\"area_id\">\n";
140 foreach ($candidates as $area) {
141 option("area_id", $area->getId(), get_area_displayname($area));
144 echo "<input type=\"submit\" value=\"Show\">\n";
147 function show_donation_cities_form($city_id = null) {
149 $cities = $q->orderByName()->find();
151 if (! count($cities)) {
152 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
156 $candidates = array();
157 foreach ($cities as $city) {
158 if (! count(get_city_hubs($city->getId()))) continue;
159 $candidates[] = $city;
161 if (! count($candidates)) return;
163 echo "<p>Show donations in city\n";
164 echo "<select name=\"city_id\">\n";
165 foreach ($candidates as $city) {
166 option("city_id", $city->getId(), get_city_displayname($city), $city_id);
169 echo "<input type=\"submit\" value=\"Show\">\n";
172 function show_donation_forms($city_id) {
173 form("noprint standout");
174 show_donation_areas_form($city_id);
175 show_donation_cities_form($city_id);
179 function show_donation_form($donation = null, $area_id = null) {
180 if (! $donation) $donation = new Donation;
184 echo " <td>Date</td>\n";
185 echo " <td>"; show_date_form("date", $donation->getDate()); echo "</td>\n";
190 echo " <td>Donor</td>\n";
191 echo " <td><select name=\"contact_id\">\n";
192 $contacts = get_area_donors();
193 foreach ($contacts as $contact) {
194 option("contact_id", $contact->getId(), $contact->getDisplayname(), $donation->getContactId());
196 echo "</select></td>\n";
201 echo " <td>Hub</td>\n";
202 echo " <td><select name=\"hub_id\">\n";
203 $hubs = get_area_hubs();
204 foreach ($hubs as $hub) {
205 option("hub_id", $hub->getId(), $hub->getDisplayname(), $donation->getHubId());
207 echo "</select></td>\n";
212 echo " <td>Quantity (kg)</td>\n";
213 echo " <td>"; input("quantity", sprintf("%0.2f", $donation->getQuantity() / 1000)); echo "</td>\n";
217 function show_new_donation_form($area_id = null) {
218 if (! check_admin(1)) return;
220 $area = get_area_by_id($area_id);
221 if (! count($area)) {
222 echo "<p>No such <a href=\"/area\">area</a>!</p>\n";
227 echo "<p>Record a donation:</p>\n";
230 show_donation_form(null, $area_id);
233 echo " <td colspan=2>"; submit("add_donation", "Record"); echo "</td></tr>\n";
239 function show_add_new_donation_form() {
240 if (! check_admin(1)) return;
242 /* We intentionally hide areas with no hubs. */
243 $areas = get_city_areas_with_hubs();
244 if (! count($areas)) {
245 echo "<p>Can't record any donations until at least one <a href=\"/area\">area</a> has a <a href=\"/hub\">hub</a>!</p>\n";
249 form("noprint standout");
250 echo "<p>Record a donation in <select name=\"area_id\">\n";
251 foreach ($areas as $area) {
252 option("area_id", $area->getId(), get_area_displayname($area));
255 submit("show_add_donation", "Proceed");
260 function update_donation(&$donation, $new = false) {
261 #$date = ymd_to_iso8601("date");
262 $date = $_POST['date'];
263 $contact_id = $_POST['contact_id'];
264 $hub_id = $_POST['hub_id'];
265 $quantity = $_POST['quantity'];
267 if (! $date) $date = time();
268 /* XXX: check date */
270 $contact = get_contact_by_id($contact_id);
272 echo "<p>Invalid contact!</p>\n";
276 $hub = get_hub_by_id($hub_id);
278 echo "<p>Invalid hub!</p>\n";
282 if (! is_numeric($quantity)) {
283 echo "<p>Invalid quantity!</p>\n";
287 $donation->setDate($date);
288 $donation->setContactId($contact_id);
289 $donation->setHubId($hub_id);
290 $donation->setQuantity($quantity * 1000);
295 catch (Exception $e) {
296 if ($new) echo "<p>Error recording donation.</p>\n";
297 else echo "<p>Error updating donation.</p>\n";
304 function add_donation() {
305 if (! check_admin(1, "record a donation")) return;
307 $donation = new Donation;
308 if (! update_donation($donation, true)) return false;
309 return $donation->getId();
312 function delete_donation($id = null) {
313 if (! check_admin(1, "delete a donation")) return;
315 if (isset($id)) $donation = get_donation_by_id($id);
316 if (! $donation) return false;
320 echo "<p>Deleted donation.</p>\n";
322 catch (Exception $e) {
323 echo "<p>Error deleting donation $id!</p>\n";
330 function show_donation(&$id = null) {
331 if (isset($id)) $donation = get_donation_by_id($id);
332 if (! $donation) return;
335 echo "<p>Donation <span class=\"strong\">" . $donation->getId() . "</span>";
336 if (check_admin(1)) {
337 echo " " . $donation->getDeleteLink();
343 show_donation_form($donation);
345 if (check_admin(1)) {
347 echo " <td colspan=2>";
348 submit("update_donation", "Update");
357 if (count($parameters)) {
358 if ($parameters[0] == "in") {
359 /* /donation/in/area/Romsey+Town/1 */
360 switch ($parameters[1]) {
362 $area_id = $parameters[3];
363 $_POST['area_id'] = $area_id;
365 $area = $q->findOneById($area_id);
366 $city = get_area_city($area);
367 if ($city) $city_id = $city->getId();
368 show_area_donations(0, 10, $parameters[2], $area_id);
372 $city_id = $parameters[3];
373 $_POST['city_id'] = $city_id;
375 $city = $q->findOneById($city_id);
376 show_city_donations(0, 10, $parameters[2], $city_id);
380 else if ($parameters[0] == "from") {
381 /* /donation/from/contact/Iain+Patterson/4 */
382 switch ($parameters[1]) {
384 $contact_id = $parameters[3];
385 $q = new ContactQuery;
386 $contact = $q->findOneById($contact_id);
387 show_contact_donations(0, 10, $parameters[2], $contact_id);
391 else if ($parameters[0] == "to") {
392 /* /donation/to/hub/Cambridge+Community+Church/1 */
393 switch ($parameters[1]) {
395 $hub_id = $parameters[3];
397 $hub = $q->findOneById($hub_id);
398 show_hub_donations(0, 10, $parameters[2], $hub_id);
403 list($ignored, $id, $args) = parse_parameters($parameters);
404 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
408 delete_donation($id);
412 else if (isset($id)) show_donation($id);
414 show_donation_forms($city_id);
415 show_add_new_donation_form($city_id);