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($offset, $per_page, $contact_ids = null, $hub_ids = null) {
47 $q = new DonationQuery;
48 if (isset($contact_ids)) $q->filterByContactId($contact_ids);
49 if (isset($hub_ids)) $q->filterByHubId($hub_ids);
50 $donations = $q->find();
51 if (count($donations)) {
52 foreach ($donations as $donation) {
53 echo "<br>\nDonation " . $donation->getStrongLink($donation->getId()) . ": " . get_donation_displayname($donation);
55 /* XXX: Should pull from query. */
56 $q = new ContactQuery;
57 $contact = $q->findOneById($donation->getContactId());
58 if ($contact) echo " from " . $contact->getLink();
61 $hub = $q->findOneById($donation->getHubId());
63 echo " to " . $hub->getLink();
64 $area = get_hub_area($hub);
66 echo " in " . $area->getLink();
67 $city = get_area_city($area);
68 if ($city) echo ", " . $city->getLink(get_city_displayname($city));
72 echo " " . $donation->getDeleteLink();
79 function show_city_donations($offset, $per_page, $city_name, $city_id = null) {
80 if (isset($city_id)) $city = get_city_by_id($city_id);
81 else if ($city_name) $city = get_city_by_name($city_name);
83 $hubs = get_city_hubs($city->getId());
85 foreach ($hubs as $hub) $hub_ids[] = $hub->getId();
87 echo "<p>Donations in city " . $city->getLink(get_city_displayname($city)) . ":";
88 return show_donations($offset, $per_page, null, $hub_ids);
90 else echo "<p>No such city!</p>\n";
93 function show_contact_donations($offset, $per_page, $contact_name, $contact_id = null) {
94 if (isset($contact_id)) $contact = get_contact_by_id($contact_id);
95 else if ($contact_name) $contact = get_contact_by_name($contact_name);
97 echo "<p>Donations from contact " . $contact->getLink() . ":";
98 return show_donations($offset, $per_page, $contact->getId());
100 else echo "<p>No such contact!</p>\n";
103 function show_hub_donations($offset, $per_page, $hub_name, $hub_id = null) {
104 if (isset($hub_id)) $hub = get_hub_by_id($hub_id);
105 else if ($hub_name) $hub = get_hub_by_name($hub_name);
107 echo "<p>Donations to hub " . $hub->getLink() . ":";
108 return show_donations($offset, $per_page, null, $hub->getId());
110 else echo "<p>No such hub!</p>\n";
113 function show_area_donations($offset, $per_page, $area_name, $area_id = null) {
114 if (isset($area_id)) $area = get_area_by_id($area_id);
115 else if ($area_name) $area = get_area_by_name($area_name);
117 $hubs = get_area_hubs($area->getId());
119 foreach ($hubs as $hub) $hub_ids[] = $hub->getId();
121 echo "<p>Donations in area " . $area->getLink() . ":";
122 return show_donations($offset, $per_page, null, $hub_ids);
124 else echo "<p>No such area!</p>\n";
127 function show_donation_areas_form($city_id = null) {
128 $areas = get_city_areas($city_id);
129 if (! count($areas)) {
130 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
134 form("noprint standout");
135 $candidates = array();
136 foreach ($areas as $area) {
137 if (! count(get_area_hubs($area->getId()))) continue;
138 $candidates[] = $area;
140 if (! count($candidates)) return;
142 echo "<p>Show donations in area\n";
143 echo "<select name=\"area_id\">\n";
144 foreach ($candidates as $area) {
145 option("area_id", $area->getId(), get_area_displayname($area));
148 submit("show_in_area", "Show");
151 function show_donation_cities_form($city_id = null) {
153 $cities = $q->orderByName()->find();
155 if (! count($cities)) {
156 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
160 $candidates = array();
161 foreach ($cities as $city) {
162 if (! count(get_city_hubs($city->getId()))) continue;
163 $candidates[] = $city;
165 if (! count($candidates)) return;
167 echo "<p>Show donations in city\n";
168 echo "<select name=\"city_id\">\n";
169 foreach ($candidates as $city) {
170 option("city_id", $city->getId(), get_city_displayname($city), $city_id);
173 submit("show_in_city", "Show");
176 function show_donation_forms($city_id) {
177 form("noprint standout");
178 show_donation_areas_form($city_id);
179 show_donation_cities_form($city_id);
183 function show_donation_form($donation = null, $area_id = null) {
184 if (! $donation) $donation = new Donation;
188 echo " <td>Date</td>\n";
189 echo " <td>"; show_date_form("date", $donation->getDate()); echo "</td>\n";
194 echo " <td>Donor</td>\n";
195 echo " <td><select name=\"contact_id\">\n";
196 $contacts = get_area_donors();
197 foreach ($contacts as $contact) {
198 option("contact_id", $contact->getId(), $contact->getDisplayname(), $donation->getContactId());
200 echo "</select></td>\n";
205 echo " <td>Hub</td>\n";
206 echo " <td><select name=\"hub_id\">\n";
207 $hubs = get_area_hubs();
208 foreach ($hubs as $hub) {
209 option("hub_id", $hub->getId(), $hub->getDisplayname(), $donation->getHubId());
211 echo "</select></td>\n";
216 echo " <td>Quantity (kg)</td>\n";
217 echo " <td>"; input("quantity", sprintf("%0.2f", $donation->getQuantity() / 1000)); echo "</td>\n";
221 function show_new_donation_form($area_id = null) {
222 if (! check_admin(1)) return;
224 $area = get_area_by_id($area_id);
225 if (! count($area)) {
226 echo "<p>No such <a href=\"/area\">area</a>!</p>\n";
231 echo "<p>Record a donation:</p>\n";
234 show_donation_form(null, $area_id);
237 echo " <td colspan=2>"; submit("add_donation", "Record"); echo "</td></tr>\n";
243 function show_hub_donation_form($hub) {
244 if (! check_admin(1)) return;
246 $area = get_hub_area($hub);
248 echo "<p>No such <a href=\"/area\">area</a>!</p>\n";
252 $donation = new Donation;
253 $donation->setHubId($hub->getId());
256 echo "<p>Record a donation:</p>\n";
259 show_donation_form($donation, $area->getId());
262 echo " <td colspan=2>"; submit("add_donation", "Record"); echo "</td></tr>\n";
268 function show_add_new_donation_form() {
269 if (! check_admin(1)) return;
271 /* We intentionally hide areas with no hubs. */
272 $areas = get_city_areas_with_hubs();
273 if (! count($areas)) {
274 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";
278 form("noprint standout");
279 echo "<p>Record a donation in <select name=\"area_id\">\n";
280 foreach ($areas as $area) {
281 option("area_id", $area->getId(), get_area_displayname($area));
284 submit("show_add_donation", "Proceed");
289 function update_donation(&$donation, $new = false) {
290 $date = $_POST['date'];
291 $contact_id = $_POST['contact_id'];
292 $hub_id = $_POST['hub_id'];
293 $quantity = $_POST['quantity'];
295 if (! $date) $date = time();
296 /* XXX: check date */
298 $contact = get_contact_by_id($contact_id);
300 echo "<p>Invalid contact!</p>\n";
304 $hub = get_hub_by_id($hub_id);
306 echo "<p>Invalid hub!</p>\n";
310 if (! is_numeric($quantity)) {
311 echo "<p>Invalid quantity!</p>\n";
315 $donation->setDate($date);
316 $donation->setContactId($contact_id);
317 $donation->setHubId($hub_id);
318 $donation->setQuantity($quantity * 1000);
323 catch (Exception $e) {
324 if ($new) echo "<p>Error recording donation.</p>\n";
325 else echo "<p>Error updating donation.</p>\n";
332 function add_donation() {
333 if (! check_admin(1, "record a donation")) return;
335 $donation = new Donation;
336 if (! update_donation($donation, true)) return false;
337 return $donation->getId();
340 function confirm_delete_donation($id = null) {
341 if (! check_admin(1, "delete a donation")) return;
343 if (isset($id)) $donation = get_donation_by_id($id);
344 if (! $donation) return false;
346 echo "<h3>Confirm deletion</h3>\n";
347 echo "<p>You must confirm deletion of donation $id: " . $donation->getDeleteLink(true) . "</p>\n";
350 function delete_donation($id = null) {
351 if (! check_admin(1, "delete a donation")) return;
353 if (isset($id)) $donation = get_donation_by_id($id);
354 if (! $donation) return false;
358 echo "<p>Deleted donation.</p>\n";
360 catch (Exception $e) {
361 echo "<p>Error deleting donation $id!</p>\n";
368 function show_donation(&$id = null) {
369 if (isset($id)) $donation = get_donation_by_id($id);
370 if (! $donation) return;
373 echo "<p>Donation <span class=\"strong\">" . $donation->getId() . "</span>";
374 if (check_admin(1)) {
375 echo " " . $donation->getDeleteLink();
381 show_donation_form($donation);
383 if (check_admin(1)) {
385 echo " <td colspan=2>";
386 submit("update_donation", "Update");
395 if (count($parameters)) {
396 if ($parameters[0] == "in") {
397 /* /donation/in/area/Romsey+Town/1 */
398 switch ($parameters[1]) {
400 $area_id = $parameters[3];
401 $_POST['area_id'] = $area_id;
403 $area = $q->findOneById($area_id);
404 $city = get_area_city($area);
405 if ($city) $city_id = $city->getId();
406 show_area_donations(0, 10, $parameters[2], $area_id);
410 $city_id = $parameters[3];
411 $_POST['city_id'] = $city_id;
413 $city = $q->findOneById($city_id);
414 show_city_donations(0, 10, $parameters[2], $city_id);
418 else if ($parameters[0] == "from") {
419 /* /donation/from/contact/Iain+Patterson/4 */
420 switch ($parameters[1]) {
422 $contact_id = $parameters[3];
423 $q = new ContactQuery;
424 $contact = $q->findOneById($contact_id);
425 show_contact_donations(0, 10, $parameters[2], $contact_id);
429 else if ($parameters[0] == "to") {
430 /* /donation/to/hub/Cambridge+Community+Church/1 */
431 switch ($parameters[1]) {
433 $hub_id = $parameters[3];
435 $hub = $q->findOneById($hub_id);
436 show_hub_donations(0, 10, $parameters[2], $hub_id);
440 else if ($parameters[0] == "record") {
441 if ($parameters[1] == "to") {
442 if ($parameters[2] == "hub") {
443 if ($parameters[4]) $hub = get_hub_by_id($parameters[4]);
444 if (! $hub) $hub = get_hub_by_name(urldecode($parameters[3]));
445 if ($hub) show_hub_donation_form($hub);
450 list($ignored, $id, $args) = parse_parameters($parameters);
451 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
455 confirm_delete_donation($id);
458 case "confirmdelete":
459 delete_donation($id);
463 else if (isset($id)) show_donation($id);
465 show_donation_forms($city_id);
466 show_add_new_donation_form($city_id);