Added contact offers.
[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($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);
55
56         /* XXX: Should pull from query. */
57         $q = new ContactQuery;
58         $contact = $q->findOneById($donation->getContactId());
59         if ($contact) echo " from " . $contact->getLink();
60
61         $q = new HubQuery;
62         $hub = $q->findOneById($donation->getHubId());
63         if ($hub) {
64           echo " to " . $hub->getLink();
65           $area = get_hub_area($hub);
66           if ($area) {
67             echo " in " . $area->getLink();
68             $city = get_area_city($area);
69             if ($city) echo ", " . $city->getLink(get_city_displayname($city));
70           }
71         }
72         if (check_admin(1)) {
73           echo " " . $donation->getDeleteLink();
74         }
75       }
76       show_pagination($donations);
77     }
78     else echo " none";
79   }
80
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);
84     if ($city) {
85       $hubs = get_city_hubs($city->getId());
86       $hub_ids = array();
87       foreach ($hubs as $hub) $hub_ids[] = $hub->getId();
88
89       echo "<p>Donations in city " . $city->getLink(get_city_displayname($city)) . ":";
90       return show_donations(null, $hub_ids);
91     }
92     else echo "<p>No such city!</p>\n";
93   }
94
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);
98     if ($contact) {
99       echo "<p>Donations from contact " . $contact->getLink() . ":";
100       return show_donations($contact->getId());
101     }
102     else echo "<p>No such contact!</p>\n";
103   }
104
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);
108     if ($hub) {
109       echo "<p>Donations to hub " . $hub->getLink() . ":";
110       return show_donations(null, $hub->getId());
111     }
112     else echo "<p>No such hub!</p>\n";
113   }
114
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);
118     if ($area) {
119       $hubs = get_area_hubs($area->getId());
120       $hub_ids = array();
121       foreach ($hubs as $hub) $hub_ids[] = $hub->getId();
122
123       echo "<p>Donations in area " . $area->getLink() . ":";
124       return show_donations(null, $hub_ids);
125     }
126     else echo "<p>No such area!</p>\n";
127   }
128
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";
133       return;
134     }
135
136     form("noprint standout");
137     $candidates = array();
138     foreach ($areas as $area) {
139       if (! count(get_area_hubs($area->getId()))) continue;
140       $candidates[] = $area;
141     }
142     if (! count($candidates)) return;
143
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));
148     }
149     echo "</select>\n";
150     submit("show_in_area", "Show");
151   }
152
153   function show_donation_cities_form($city_id = null) {
154     $q = new CityQuery;
155     $cities = $q->orderByName()->find();
156
157     if (! count($cities)) {
158       echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
159       return;
160     }
161
162     $candidates = array();
163     foreach ($cities as $city) {
164       if (! count(get_city_hubs($city->getId()))) continue;
165       $candidates[] = $city;
166     }
167     if (! count($candidates)) return;
168
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);
173     }
174     echo "</select>\n";
175     submit("show_in_city", "Show");
176   }
177
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);
182     end_form();
183   }
184
185   function show_donation_form($donation = null, $area_id = null) {
186     if (! $donation) $donation = new Donation;
187
188     /* Date. */
189     echo "<tr>\n";
190     echo "  <td>Date</td>\n";
191     echo "  <td>"; show_date_form("date", $donation->getDate()); echo "</td>\n";
192     echo "</tr>\n";
193
194     /* Contact. */
195     echo "<tr>\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());
201     }
202     echo "</select></td>\n";
203     echo "</tr>\n";
204
205     /* Hub. */
206     echo "<tr>\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());
212     }
213     echo "</select></td>\n";
214     echo "</tr>\n";
215
216     /* Quantity. */
217     echo "<tr>\n";
218     echo "  <td>Quantity (kg)</td>\n";
219     echo "  <td>"; input("quantity", sprintf("%0.2f", $donation->getQuantity() / 1000)); echo "</td>\n";
220     echo "</tr>\n";
221   }
222
223   function show_new_donation_form($area_id = null) {
224     if (! check_admin(1)) return;
225
226     $area = get_area_by_id($area_id);
227     if (! count($area)) {
228       echo "<p>No such <a href=\"/area\">area</a>!</p>\n";
229       return;
230     }
231
232     form();
233     echo "<p>Record a donation:</p>\n";
234
235     echo "<table>\n";
236     show_donation_form(null, $area_id);
237
238     echo "<tr>\n";
239     echo "  <td colspan=2>"; submit("add_donation", "Record"); echo "</td></tr>\n";
240     echo "</tr>\n";
241     echo "</table>\n";
242     end_form();
243   }
244
245   function show_hub_donation_form($hub) {
246     if (! check_admin(1)) return;
247
248     $area = get_hub_area($hub);
249     if (! $area) {
250       echo "<p>No such <a href=\"/area\">area</a>!</p>\n";
251       return;
252     }
253
254     $donation = new Donation;
255     $donation->setHubId($hub->getId());
256
257     form();
258     echo "<p>Record a donation:</p>\n";
259
260     echo "<table>\n";
261     show_donation_form($donation, $area->getId());
262
263     echo "<tr>\n";
264     echo "  <td colspan=2>"; submit("add_donation", "Record"); echo "</td></tr>\n";
265     echo "</tr>\n";
266     echo "</table>\n";
267     end_form();
268   }
269
270   function show_add_new_donation_form() {
271     if (! check_admin(1)) return;
272
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";
277       return;
278     }
279
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));
284     }
285     echo "</select>";
286     submit("show_add_donation", "Proceed");
287     echo "</p>\n";
288     end_form();
289   }
290
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'];
296
297     if (! $date) $date = time();
298     /* XXX: check date */
299
300     $contact = get_contact_by_id($contact_id);
301     if (! $contact) {
302       echo "<p>Invalid contact!</p>\n";
303       return false;
304     }
305
306     $hub = get_hub_by_id($hub_id);
307     if (! $hub) {
308       echo "<p>Invalid hub!</p>\n";
309       return false;
310     }
311
312     if (! is_numeric($quantity)) {
313       echo "<p>Invalid quantity!</p>\n";
314       return false;
315     }
316
317     $donation->setDate($date);
318     $donation->setContactId($contact_id);
319     $donation->setHubId($hub_id);
320     $donation->setQuantity($quantity * 1000);
321
322     try {
323       $donation->save();
324     }
325     catch (Exception $e) {
326       if ($new) echo "<p>Error recording donation.</p>\n";
327       else echo "<p>Error updating donation.</p>\n";
328       return false;
329     }
330
331     return true;
332   }
333
334   function add_donation() {
335     if (! check_admin(1, "record a donation")) return;
336
337     $donation = new Donation;
338     if (! update_donation($donation, true)) return false;
339     return $donation->getId();
340   }
341
342   function confirm_delete_donation($id = null) {
343     if (! check_admin(1, "delete a donation")) return;
344
345     if (isset($id)) $donation = get_donation_by_id($id);
346     if (! $donation) return false;
347
348     echo "<h3>Confirm deletion</h3>\n";
349     echo "<p>You must confirm deletion of donation $id: " . $donation->getDeleteLink(true) . "</p>\n";
350   }
351
352   function delete_donation($id = null) {
353     if (! check_admin(1, "delete a donation")) return;
354
355     if (isset($id)) $donation = get_donation_by_id($id);
356     if (! $donation) return false;
357
358     try {
359       $donation->delete();
360       echo "<p>Deleted donation.</p>\n";
361     }
362     catch (Exception $e) {
363       echo "<p>Error deleting donation $id!</p>\n";
364       return false;
365     }
366
367     return true;
368   }
369
370   function show_donation(&$id = null) {
371     if (isset($id)) $donation = get_donation_by_id($id);
372     if (! $donation) return;
373
374     form();
375     echo "<p>Donation <span class=\"strong\">" . $donation->getId() . "</span>";
376     if (check_admin(1)) {
377       echo " " . $donation->getDeleteLink();
378     }
379     echo ": ";
380     echo "\n</p>";
381
382     echo "<table>\n";
383     show_donation_form($donation);
384
385     if (check_admin(1)) {
386       echo "<tr>\n";
387       echo "  <td colspan=2>";
388       submit("update_donation", "Update");
389       echo "</td>\n";
390       echo "</tr>\n";
391     }
392
393     echo "</table>\n";
394     end_form();
395   }
396
397   if (count($parameters)) {
398     if ($parameters[0] == "in") {
399       /* /donation/in/area/Romsey+Town/1 */
400       switch ($parameters[1]) {
401         case "area":
402           $area_id = $parameters[3];
403           $_POST['area_id'] = $area_id;
404           $q = new AreaQuery;
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);
409         break;
410
411         case "city":
412           $city_id = $parameters[3];
413           $_POST['city_id'] = $city_id;
414           $q = new CityQuery;
415           $city = $q->findOneById($city_id);
416           show_city_donations($parameters[2], $city_id);
417         break;
418       }
419     }
420     else if ($parameters[0] == "from") {
421       /* /donation/from/contact/Iain+Patterson/4 */
422       switch ($parameters[1]) {
423         case "contact":
424           $contact_id = $parameters[3];
425           $q = new ContactQuery;
426           $contact = $q->findOneById($contact_id);
427           show_contact_donations($parameters[2], $contact_id);
428         break;
429       }
430     }
431     else if ($parameters[0] == "to") {
432       /* /donation/to/hub/Cambridge+Community+Church/1 */
433       switch ($parameters[1]) {
434         case "hub":
435           $hub_id = $parameters[3];
436           $q = new HubQuery;
437           $hub = $q->findOneById($hub_id);
438           show_hub_donations($parameters[2], $hub_id);
439         break;
440       }
441     }
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);
448         }
449       }
450     }
451   }
452   list($ignored, $id, $args) = parse_parameters($parameters);
453   //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
454   if (count($args)) {
455     switch ($args[0]) {
456       case "delete":
457         confirm_delete_donation($id);
458       break;
459
460       case "confirmdelete":
461         delete_donation($id);
462       break;
463     }
464   }
465   else if (isset($id)) show_donation($id);
466
467   show_donation_forms($city_id);
468   show_add_new_donation_form($city_id);
469
470 ?>