Merge branch 'master' into uat
[readifood.git] / lib / donation.php
1 <?php
2
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);
7   }
8   else if (isset($_POST['add_donation'])) {
9     set_last_selected("area_id", $_POST['area_id']);
10     $id = add_donation();
11     if ($id !== false) {
12       echo "<p>Donation recorded.</p>\n";
13       $parameters = array("id", $id);
14     }
15   }
16   else if (isset($_POST['update_donation'])) {
17     list($ignored, $id, $args) = parse_parameters($parameters);
18     $q = new DonationQuery;
19     $donation = $q->findOneById($id);
20     if ($donation) {
21       if (update_donation($donation) !== false) {
22         echo "<p>Updated donation.</p>\n";
23         $parameters = array("id", $donation->getId());
24       }
25     }
26     else {
27       echo "<p>No such contact!</p>\n";
28     }
29   }
30   else if ($_POST['show_in_area']) {
31     set_last_selected("area_id", $_POST['area_id']);
32     $q = new AreaQuery;
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']));
36     exit;
37   }
38   else if ($_POST['show_in_city']) {
39     set_last_selected("city_id", $_POST['city_id']);
40     $q = new CityQuery;
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']));
43     exit;
44   }
45
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);
54
55         /* XXX: Should pull from query. */
56         $q = new ContactQuery;
57         $contact = $q->findOneById($donation->getContactId());
58         if ($contact) echo " from " . $contact->getLink();
59
60         $q = new HubQuery;
61         $hub = $q->findOneById($donation->getHubId());
62         if ($hub) {
63           echo " to " . $hub->getLink();
64           $area = get_hub_area($hub);
65           if ($area) {
66             echo " in " . $area->getLink();
67             $city = get_area_city($area);
68             if ($city) echo ", " . $city->getLink(get_city_displayname($city));
69           }
70         }
71         if (check_admin(1)) {
72           echo " " . $donation->getDeleteLink();
73         }
74       }
75     }
76     else echo " none";
77   }
78
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);
82     if ($city) {
83       $hubs = get_city_hubs($city->getId());
84       $hub_ids = array();
85       foreach ($hubs as $hub) $hub_ids[] = $hub->getId();
86
87       echo "<p>Donations in city " . $city->getLink(get_city_displayname($city)) . ":";
88       return show_donations($offset, $per_page, null, $hub_ids);
89     }
90     else echo "<p>No such city!</p>\n";
91   }
92
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);
96     if ($contact) {
97       echo "<p>Donations from contact " . $contact->getLink() . ":";
98       return show_donations($offset, $per_page, $contact->getId());
99     }
100     else echo "<p>No such contact!</p>\n";
101   }
102
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);
106     if ($hub) {
107       echo "<p>Donations to hub " . $hub->getLink() . ":";
108       return show_donations($offset, $per_page, null, $hub->getId());
109     }
110     else echo "<p>No such hub!</p>\n";
111   }
112
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);
116     if ($area) {
117       $hubs = get_area_hubs($area->getId());
118       $hub_ids = array();
119       foreach ($hubs as $hub) $hub_ids[] = $hub->getId();
120
121       echo "<p>Donations in area " . $area->getLink() . ":";
122       return show_donations($offset, $per_page, null, $hub_ids);
123     }
124     else echo "<p>No such area!</p>\n";
125   }
126
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";
131       return;
132     }
133
134     form("noprint standout");
135     $candidates = array();
136     foreach ($areas as $area) {
137       if (! count(get_area_hubs($area->getId()))) continue;
138       $candidates[] = $area;
139     }
140     if (! count($candidates)) return;
141
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));
146     }
147     echo "</select>\n";
148     submit("show_in_area", "Show");
149   }
150
151   function show_donation_cities_form($city_id = null) {
152     $q = new CityQuery;
153     $cities = $q->orderByName()->find();
154
155     if (! count($cities)) {
156       echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
157       return;
158     }
159
160     $candidates = array();
161     foreach ($cities as $city) {
162       if (! count(get_city_hubs($city->getId()))) continue;
163       $candidates[] = $city;
164     }
165     if (! count($candidates)) return;
166
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);
171     }
172     echo "</select>\n";
173     submit("show_in_city", "Show");
174   }
175
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);
180     end_form();
181   }
182
183   function show_donation_form($donation = null, $area_id = null) {
184     if (! $donation) $donation = new Donation;
185
186     /* Date. */
187     echo "<tr>\n";
188     echo "  <td>Date</td>\n";
189     echo "  <td>"; show_date_form("date", $donation->getDate()); echo "</td>\n";
190     echo "</tr>\n";
191
192     /* Contact. */
193     echo "<tr>\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());
199     }
200     echo "</select></td>\n";
201     echo "</tr>\n";
202
203     /* Hub. */
204     echo "<tr>\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());
210     }
211     echo "</select></td>\n";
212     echo "</tr>\n";
213
214     /* Quantity. */
215     echo "<tr>\n";
216     echo "  <td>Quantity (kg)</td>\n";
217     echo "  <td>"; input("quantity", sprintf("%0.2f", $donation->getQuantity() / 1000)); echo "</td>\n";
218     echo "</tr>\n";
219   }
220
221   function show_new_donation_form($area_id = null) {
222     if (! check_admin(1)) return;
223
224     $area = get_area_by_id($area_id);
225     if (! count($area)) {
226       echo "<p>No such <a href=\"/area\">area</a>!</p>\n";
227       return;
228     }
229
230     form();
231     echo "<p>Record a donation:</p>\n";
232
233     echo "<table>\n";
234     show_donation_form(null, $area_id);
235
236     echo "<tr>\n";
237     echo "  <td colspan=2>"; submit("add_donation", "Record"); echo "</td></tr>\n";
238     echo "</tr>\n";
239     echo "</table>\n";
240     end_form();
241   }
242
243   function show_add_new_donation_form() {
244     if (! check_admin(1)) return;
245
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";
250       return;
251     }
252
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));
257     }
258     echo "</select>";
259     submit("show_add_donation", "Proceed");
260     echo "</p>\n";
261     end_form();
262   }
263
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'];
269
270     if (! $date) $date = time();
271     /* XXX: check date */
272
273     $contact = get_contact_by_id($contact_id);
274     if (! $contact) {
275       echo "<p>Invalid contact!</p>\n";
276       return false;
277     }
278
279     $hub = get_hub_by_id($hub_id);
280     if (! $hub) {
281       echo "<p>Invalid hub!</p>\n";
282       return false;
283     }
284
285     if (! is_numeric($quantity)) {
286       echo "<p>Invalid quantity!</p>\n";
287       return false;
288     }
289
290     $donation->setDate($date);
291     $donation->setContactId($contact_id);
292     $donation->setHubId($hub_id);
293     $donation->setQuantity($quantity * 1000);
294
295     try {
296       $donation->save();
297     }
298     catch (Exception $e) {
299       if ($new) echo "<p>Error recording donation.</p>\n";
300       else echo "<p>Error updating donation.</p>\n";
301       return false;
302     }
303
304     return true;
305   }
306
307   function add_donation() {
308     if (! check_admin(1, "record a donation")) return;
309
310     $donation = new Donation;
311     if (! update_donation($donation, true)) return false;
312     return $donation->getId();
313   }
314
315   function confirm_delete_donation($id = null) {
316     if (! check_admin(1, "delete a donation")) return;
317
318     if (isset($id)) $donation = get_donation_by_id($id);
319     if (! $donation) return false;
320
321     echo "<h3>Confirm deletion</h3>\n";
322     echo "<p>You must confirm deletion of donation $id: " . $donation->getDeleteLink(true) . "</p>\n";
323   }
324
325   function delete_donation($id = null) {
326     if (! check_admin(1, "delete a donation")) return;
327
328     if (isset($id)) $donation = get_donation_by_id($id);
329     if (! $donation) return false;
330
331     try {
332       $donation->delete();
333       echo "<p>Deleted donation.</p>\n";
334     }
335     catch (Exception $e) {
336       echo "<p>Error deleting donation $id!</p>\n";
337       return false;
338     }
339
340     return true;
341   }
342
343   function show_donation(&$id = null) {
344     if (isset($id)) $donation = get_donation_by_id($id);
345     if (! $donation) return;
346
347     form();
348     echo "<p>Donation <span class=\"strong\">" . $donation->getId() . "</span>";
349     if (check_admin(1)) {
350       echo " " . $donation->getDeleteLink();
351     }
352     echo ": ";
353     echo "\n</p>";
354
355     echo "<table>\n";
356     show_donation_form($donation);
357
358     if (check_admin(1)) {
359       echo "<tr>\n";
360       echo "  <td colspan=2>";
361       submit("update_donation", "Update");
362       echo "</td>\n";
363       echo "</tr>\n";
364     }
365
366     echo "</table>\n";
367     end_form();
368   }
369
370   if (count($parameters)) {
371     if ($parameters[0] == "in") {
372       /* /donation/in/area/Romsey+Town/1 */
373       switch ($parameters[1]) {
374         case "area":
375           $area_id = $parameters[3];
376           $_POST['area_id'] = $area_id;
377           $q = new AreaQuery;
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);
382         break;
383
384         case "city":
385           $city_id = $parameters[3];
386           $_POST['city_id'] = $city_id;
387           $q = new CityQuery;
388           $city = $q->findOneById($city_id);
389           show_city_donations(0, 10, $parameters[2], $city_id);
390         break;
391       }
392     }
393     else if ($parameters[0] == "from") {
394       /* /donation/from/contact/Iain+Patterson/4 */
395       switch ($parameters[1]) {
396         case "contact":
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);
401         break;
402       }
403     }
404     else if ($parameters[0] == "to") {
405       /* /donation/to/hub/Cambridge+Community+Church/1 */
406       switch ($parameters[1]) {
407         case "hub":
408           $hub_id = $parameters[3];
409           $q = new HubQuery;
410           $hub = $q->findOneById($hub_id);
411           show_hub_donations(0, 10, $parameters[2], $hub_id);
412         break;
413       }
414     }
415   }
416   list($ignored, $id, $args) = parse_parameters($parameters);
417   //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
418   if (count($args)) {
419     switch ($args[0]) {
420       case "delete":
421         confirm_delete_donation($id);
422       break;
423
424       case "confirmdelete":
425         delete_donation($id);
426       break;
427     }
428   }
429   else if (isset($id)) show_donation($id);
430
431   show_donation_forms($city_id);
432   show_add_new_donation_form($city_id);
433
434 ?>