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_add_new_donation_form() {
244 if (! check_admin(1)) return;
246 /* We intentionally hide areas with no hubs. */
247 $areas = get_city_areas_with_hubs();
248 if (! count($areas)) {
249 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";
253 form("noprint standout");
254 echo "<p>Record a donation in <select name=\"area_id\">\n";
255 foreach ($areas as $area) {
256 option("area_id", $area->getId(), get_area_displayname($area));
259 submit("show_add_donation", "Proceed");
264 function update_donation(&$donation, $new = false) {
265 $date = $_POST['date'];
266 $contact_id = $_POST['contact_id'];
267 $hub_id = $_POST['hub_id'];
268 $quantity = $_POST['quantity'];
270 if (! $date) $date = time();
271 /* XXX: check date */
273 $contact = get_contact_by_id($contact_id);
275 echo "<p>Invalid contact!</p>\n";
279 $hub = get_hub_by_id($hub_id);
281 echo "<p>Invalid hub!</p>\n";
285 if (! is_numeric($quantity)) {
286 echo "<p>Invalid quantity!</p>\n";
290 $donation->setDate($date);
291 $donation->setContactId($contact_id);
292 $donation->setHubId($hub_id);
293 $donation->setQuantity($quantity * 1000);
298 catch (Exception $e) {
299 if ($new) echo "<p>Error recording donation.</p>\n";
300 else echo "<p>Error updating donation.</p>\n";
307 function add_donation() {
308 if (! check_admin(1, "record a donation")) return;
310 $donation = new Donation;
311 if (! update_donation($donation, true)) return false;
312 return $donation->getId();
315 function confirm_delete_donation($id = null) {
316 if (! check_admin(1, "delete a donation")) return;
318 if (isset($id)) $donation = get_donation_by_id($id);
319 if (! $donation) return false;
321 echo "<h3>Confirm deletion</h3>\n";
322 echo "<p>You must confirm deletion of donation $id: " . $donation->getDeleteLink(true) . "</p>\n";
325 function delete_donation($id = null) {
326 if (! check_admin(1, "delete a donation")) return;
328 if (isset($id)) $donation = get_donation_by_id($id);
329 if (! $donation) return false;
333 echo "<p>Deleted donation.</p>\n";
335 catch (Exception $e) {
336 echo "<p>Error deleting donation $id!</p>\n";
343 function show_donation(&$id = null) {
344 if (isset($id)) $donation = get_donation_by_id($id);
345 if (! $donation) return;
348 echo "<p>Donation <span class=\"strong\">" . $donation->getId() . "</span>";
349 if (check_admin(1)) {
350 echo " " . $donation->getDeleteLink();
356 show_donation_form($donation);
358 if (check_admin(1)) {
360 echo " <td colspan=2>";
361 submit("update_donation", "Update");
370 if (count($parameters)) {
371 if ($parameters[0] == "in") {
372 /* /donation/in/area/Romsey+Town/1 */
373 switch ($parameters[1]) {
375 $area_id = $parameters[3];
376 $_POST['area_id'] = $area_id;
378 $area = $q->findOneById($area_id);
379 $city = get_area_city($area);
380 if ($city) $city_id = $city->getId();
381 show_area_donations(0, 10, $parameters[2], $area_id);
385 $city_id = $parameters[3];
386 $_POST['city_id'] = $city_id;
388 $city = $q->findOneById($city_id);
389 show_city_donations(0, 10, $parameters[2], $city_id);
393 else if ($parameters[0] == "from") {
394 /* /donation/from/contact/Iain+Patterson/4 */
395 switch ($parameters[1]) {
397 $contact_id = $parameters[3];
398 $q = new ContactQuery;
399 $contact = $q->findOneById($contact_id);
400 show_contact_donations(0, 10, $parameters[2], $contact_id);
404 else if ($parameters[0] == "to") {
405 /* /donation/to/hub/Cambridge+Community+Church/1 */
406 switch ($parameters[1]) {
408 $hub_id = $parameters[3];
410 $hub = $q->findOneById($hub_id);
411 show_hub_donations(0, 10, $parameters[2], $hub_id);
416 list($ignored, $id, $args) = parse_parameters($parameters);
417 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
421 confirm_delete_donation($id);
424 case "confirmdelete":
425 delete_donation($id);
429 else if (isset($id)) show_donation($id);
431 show_donation_forms($city_id);
432 show_add_new_donation_form($city_id);