3 if (isset($_POST['show_add_donation'])) {
4 set_last_selected("area_id", $_POST['area_id']);
5 $area_id = $_POST['area_id'];
6 show_new_donation_form($area_id);
8 else if (isset($_POST['add_donation'])) {
9 set_last_selected("area_id", $_POST['area_id']);
12 echo "<p>Donation recorded.</p>\n";
13 $parameters = array("id", $id);
16 else if (isset($_POST['update_donation'])) {
17 list($ignored, $id, $args) = parse_parameters($parameters);
18 $q = new DonationQuery;
19 $donation = $q->findOneById($id);
21 if (update_donation($donation) !== false) {
22 echo "<p>Updated donation.</p>\n";
23 $parameters = array("id", $donation->getId());
27 echo "<p>No such contact!</p>\n";
30 else if ($_POST['show_in_area']) {
31 set_last_selected("area_id", $_POST['area_id']);
33 $area = $q->findOneById($_POST['area_id']);
34 /* XXX: Function to build URL because we need to set a class in links. */
35 header(sprintf("Location: http%s://%s/%s/in/area/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($area->getName()), $_POST['area_id']));
38 else if ($_POST['show_in_city']) {
39 set_last_selected("city_id", $_POST['city_id']);
41 $city = $q->findOneById($_POST['city_id']);
42 header(sprintf("Location: http%s://%s/%s/in/city/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($city->getName()), $_POST['city_id']));
46 function show_donations($contact_ids = null, $hub_ids = null) {
47 list($first_page, $per_page) = pagination();
48 $q = new DonationQuery;
49 if (isset($contact_ids)) $q->filterByContactId($contact_ids);
50 if (isset($hub_ids)) $q->filterByHubId($hub_ids);
51 $donations = $q->orderByDate('desc')->orderById('desc')->paginate($first_page, $per_page);
52 if (count($donations)) {
53 foreach ($donations as $donation) {
54 echo "<br>\nDonation " . $donation->getStrongLink($donation->getId()) . ": " . get_donation_displayname($donation);
56 /* XXX: Should pull from query. */
57 $q = new ContactQuery;
58 $contact = $q->findOneById($donation->getContactId());
59 if ($contact) echo " from " . $contact->getLink();
62 $hub = $q->findOneById($donation->getHubId());
64 echo " to " . $hub->getLink();
65 $area = get_hub_area($hub);
67 echo " in " . $area->getLink();
68 $city = get_area_city($area);
69 if ($city) echo ", " . $city->getLink(get_city_displayname($city));
73 echo " " . $donation->getDeleteLink();
76 show_pagination($donations);
81 function show_city_donations($city_name, $city_id = null) {
82 if (isset($city_id)) $city = get_city_by_id($city_id);
83 else if ($city_name) $city = get_city_by_name($city_name);
85 $hubs = get_city_hubs($city->getId());
87 foreach ($hubs as $hub) $hub_ids[] = $hub->getId();
89 echo "<p>Donations in city " . $city->getLink(get_city_displayname($city)) . ":";
90 return show_donations(null, $hub_ids);
92 else echo "<p>No such city!</p>\n";
95 function show_contact_donations($contact_name, $contact_id = null) {
96 if (isset($contact_id)) $contact = get_contact_by_id($contact_id);
97 else if ($contact_name) $contact = get_contact_by_name($contact_name);
99 echo "<p>Donations from contact " . $contact->getLink() . ":";
100 return show_donations($contact->getId());
102 else echo "<p>No such contact!</p>\n";
105 function show_hub_donations($hub_name, $hub_id = null) {
106 if (isset($hub_id)) $hub = get_hub_by_id($hub_id);
107 else if ($hub_name) $hub = get_hub_by_name($hub_name);
109 echo "<p>Donations to hub " . $hub->getLink() . ":";
110 return show_donations(null, $hub->getId());
112 else echo "<p>No such hub!</p>\n";
115 function show_area_donations($area_name, $area_id = null) {
116 if (isset($area_id)) $area = get_area_by_id($area_id);
117 else if ($area_name) $area = get_area_by_name($area_name);
119 $hubs = get_area_hubs($area->getId());
121 foreach ($hubs as $hub) $hub_ids[] = $hub->getId();
123 echo "<p>Donations in area " . $area->getLink() . ":";
124 return show_donations(null, $hub_ids);
126 else echo "<p>No such area!</p>\n";
129 function show_donation_areas_form($city_id = null) {
130 $areas = get_city_areas($city_id);
131 if (! count($areas)) {
132 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
136 form("noprint standout");
137 $candidates = array();
138 foreach ($areas as $area) {
139 if (! count(get_area_hubs($area->getId()))) continue;
140 $candidates[] = $area;
142 if (! count($candidates)) return;
144 echo "<p>Show donations in area\n";
145 echo "<select name=\"area_id\">\n";
146 foreach ($candidates as $area) {
147 option("area_id", $area->getId(), get_area_displayname($area));
150 submit("show_in_area", "Show");
153 function show_donation_cities_form($city_id = null) {
155 $cities = $q->orderByName()->find();
157 if (! count($cities)) {
158 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
162 $candidates = array();
163 foreach ($cities as $city) {
164 if (! count(get_city_hubs($city->getId()))) continue;
165 $candidates[] = $city;
167 if (! count($candidates)) return;
169 echo "<p>Show donations in city\n";
170 echo "<select name=\"city_id\">\n";
171 foreach ($candidates as $city) {
172 option("city_id", $city->getId(), get_city_displayname($city), $city_id);
175 submit("show_in_city", "Show");
178 function show_donation_forms($city_id) {
179 form("noprint standout");
180 show_donation_areas_form($city_id);
181 show_donation_cities_form($city_id);
185 function show_donation_form($donation = null, $area_id = null) {
186 if (! $donation) $donation = new Donation;
190 echo " <td>Date</td>\n";
191 echo " <td>"; show_date_form("date", $donation->getDate()); echo "</td>\n";
196 echo " <td>Donor</td>\n";
197 echo " <td><select name=\"contact_id\">\n";
198 $contacts = get_area_donors();
199 foreach ($contacts as $contact) {
200 option("contact_id", $contact->getId(), $contact->getDisplayname(), $donation->getContactId());
202 echo "</select></td>\n";
207 echo " <td>Hub</td>\n";
208 echo " <td><select name=\"hub_id\">\n";
209 $hubs = get_area_hubs();
210 foreach ($hubs as $hub) {
211 option("hub_id", $hub->getId(), $hub->getDisplayname(), $donation->getHubId());
213 echo "</select></td>\n";
218 echo " <td>Quantity (kg)</td>\n";
219 echo " <td>"; input("quantity", sprintf("%0.2f", $donation->getQuantity() / 1000)); echo "</td>\n";
223 function show_new_donation_form($area_id = null) {
224 if (! check_admin(1)) return;
226 $area = get_area_by_id($area_id);
227 if (! count($area)) {
228 echo "<p>No such <a href=\"/area\">area</a>!</p>\n";
233 echo "<p>Record a donation:</p>\n";
236 show_donation_form(null, $area_id);
239 echo " <td colspan=2>"; submit("add_donation", "Record"); echo "</td></tr>\n";
245 function show_hub_donation_form($hub) {
246 if (! check_admin(1)) return;
248 $area = get_hub_area($hub);
250 echo "<p>No such <a href=\"/area\">area</a>!</p>\n";
254 $donation = new Donation;
255 $donation->setHubId($hub->getId());
258 echo "<p>Record a donation:</p>\n";
261 show_donation_form($donation, $area->getId());
264 echo " <td colspan=2>"; submit("add_donation", "Record"); echo "</td></tr>\n";
270 function show_add_new_donation_form() {
271 if (! check_admin(1)) return;
273 /* We intentionally hide areas with no hubs. */
274 $areas = get_city_areas_with_hubs();
275 if (! count($areas)) {
276 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";
280 form("noprint standout");
281 echo "<p>Record a donation in <select name=\"area_id\">\n";
282 foreach ($areas as $area) {
283 option("area_id", $area->getId(), get_area_displayname($area));
286 submit("show_add_donation", "Proceed");
291 function update_donation(&$donation, $new = false) {
292 $date = $_POST['date'];
293 $contact_id = $_POST['contact_id'];
294 $hub_id = $_POST['hub_id'];
295 $quantity = $_POST['quantity'];
297 if (! $date) $date = time();
298 /* XXX: check date */
300 $contact = get_contact_by_id($contact_id);
302 echo "<p>Invalid contact!</p>\n";
306 $hub = get_hub_by_id($hub_id);
308 echo "<p>Invalid hub!</p>\n";
312 if (! is_numeric($quantity)) {
313 echo "<p>Invalid quantity!</p>\n";
317 $donation->setDate($date);
318 $donation->setContactId($contact_id);
319 $donation->setHubId($hub_id);
320 $donation->setQuantity($quantity * 1000);
325 catch (Exception $e) {
326 if ($new) echo "<p>Error recording donation.</p>\n";
327 else echo "<p>Error updating donation.</p>\n";
334 function add_donation() {
335 if (! check_admin(1, "record a donation")) return;
337 $donation = new Donation;
338 if (! update_donation($donation, true)) return false;
339 return $donation->getId();
342 function confirm_delete_donation($id = null) {
343 if (! check_admin(1, "delete a donation")) return;
345 if (isset($id)) $donation = get_donation_by_id($id);
346 if (! $donation) return false;
348 echo "<h3>Confirm deletion</h3>\n";
349 echo "<p>You must confirm deletion of donation $id: " . $donation->getDeleteLink(true) . "</p>\n";
352 function delete_donation($id = null) {
353 if (! check_admin(1, "delete a donation")) return;
355 if (isset($id)) $donation = get_donation_by_id($id);
356 if (! $donation) return false;
360 echo "<p>Deleted donation.</p>\n";
362 catch (Exception $e) {
363 echo "<p>Error deleting donation $id!</p>\n";
370 function show_donation(&$id = null) {
371 if (isset($id)) $donation = get_donation_by_id($id);
372 if (! $donation) return;
375 echo "<p>Donation <span class=\"strong\">" . $donation->getId() . "</span>";
376 if (check_admin(1)) {
377 echo " " . $donation->getDeleteLink();
383 show_donation_form($donation);
385 if (check_admin(1)) {
387 echo " <td colspan=2>";
388 submit("update_donation", "Update");
397 if (count($parameters)) {
398 if ($parameters[0] == "in") {
399 /* /donation/in/area/Romsey+Town/1 */
400 switch ($parameters[1]) {
402 $area_id = $parameters[3];
403 $_POST['area_id'] = $area_id;
405 $area = $q->findOneById($area_id);
406 $city = get_area_city($area);
407 if ($city) $city_id = $city->getId();
408 show_area_donations($parameters[2], $area_id);
412 $city_id = $parameters[3];
413 $_POST['city_id'] = $city_id;
415 $city = $q->findOneById($city_id);
416 show_city_donations($parameters[2], $city_id);
420 else if ($parameters[0] == "from") {
421 /* /donation/from/contact/Iain+Patterson/4 */
422 switch ($parameters[1]) {
424 $contact_id = $parameters[3];
425 $q = new ContactQuery;
426 $contact = $q->findOneById($contact_id);
427 show_contact_donations($parameters[2], $contact_id);
431 else if ($parameters[0] == "to") {
432 /* /donation/to/hub/Cambridge+Community+Church/1 */
433 switch ($parameters[1]) {
435 $hub_id = $parameters[3];
437 $hub = $q->findOneById($hub_id);
438 show_hub_donations($parameters[2], $hub_id);
442 else if ($parameters[0] == "record") {
443 if ($parameters[1] == "to") {
444 if ($parameters[2] == "hub") {
445 if ($parameters[4]) $hub = get_hub_by_id($parameters[4]);
446 if (! $hub) $hub = get_hub_by_name(urldecode($parameters[3]));
447 if ($hub) show_hub_donation_form($hub);
452 list($ignored, $id, $args) = parse_parameters($parameters);
453 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
457 confirm_delete_donation($id);
460 case "confirmdelete":
461 delete_donation($id);
465 else if (isset($id)) show_donation($id);
467 show_donation_forms($city_id);
468 show_add_new_donation_form($city_id);