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_hub_donation_form($hub) {
244     if (! check_admin(1)) return;
245
246     $area = get_hub_area($hub);
247     if (! $area) {
248       echo "<p>No such <a href=\"/area\">area</a>!</p>\n";
249       return;
250     }
251
252     $donation = new Donation;
253     $donation->setHubId($hub->getId());
254
255     form();
256     echo "<p>Record a donation:</p>\n";
257
258     echo "<table>\n";
259     show_donation_form($donation, $area->getId());
260
261     echo "<tr>\n";
262     echo "  <td colspan=2>"; submit("add_donation", "Record"); echo "</td></tr>\n";
263     echo "</tr>\n";
264     echo "</table>\n";
265     end_form();
266   }
267
268   function show_add_new_donation_form() {
269     if (! check_admin(1)) return;
270
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";
275       return;
276     }
277
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));
282     }
283     echo "</select>";
284     submit("show_add_donation", "Proceed");
285     echo "</p>\n";
286     end_form();
287   }
288
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'];
294
295     if (! $date) $date = time();
296     /* XXX: check date */
297
298     $contact = get_contact_by_id($contact_id);
299     if (! $contact) {
300       echo "<p>Invalid contact!</p>\n";
301       return false;
302     }
303
304     $hub = get_hub_by_id($hub_id);
305     if (! $hub) {
306       echo "<p>Invalid hub!</p>\n";
307       return false;
308     }
309
310     if (! is_numeric($quantity)) {
311       echo "<p>Invalid quantity!</p>\n";
312       return false;
313     }
314
315     $donation->setDate($date);
316     $donation->setContactId($contact_id);
317     $donation->setHubId($hub_id);
318     $donation->setQuantity($quantity * 1000);
319
320     try {
321       $donation->save();
322     }
323     catch (Exception $e) {
324       if ($new) echo "<p>Error recording donation.</p>\n";
325       else echo "<p>Error updating donation.</p>\n";
326       return false;
327     }
328
329     return true;
330   }
331
332   function add_donation() {
333     if (! check_admin(1, "record a donation")) return;
334
335     $donation = new Donation;
336     if (! update_donation($donation, true)) return false;
337     return $donation->getId();
338   }
339
340   function confirm_delete_donation($id = null) {
341     if (! check_admin(1, "delete a donation")) return;
342
343     if (isset($id)) $donation = get_donation_by_id($id);
344     if (! $donation) return false;
345
346     echo "<h3>Confirm deletion</h3>\n";
347     echo "<p>You must confirm deletion of donation $id: " . $donation->getDeleteLink(true) . "</p>\n";
348   }
349
350   function delete_donation($id = null) {
351     if (! check_admin(1, "delete a donation")) return;
352
353     if (isset($id)) $donation = get_donation_by_id($id);
354     if (! $donation) return false;
355
356     try {
357       $donation->delete();
358       echo "<p>Deleted donation.</p>\n";
359     }
360     catch (Exception $e) {
361       echo "<p>Error deleting donation $id!</p>\n";
362       return false;
363     }
364
365     return true;
366   }
367
368   function show_donation(&$id = null) {
369     if (isset($id)) $donation = get_donation_by_id($id);
370     if (! $donation) return;
371
372     form();
373     echo "<p>Donation <span class=\"strong\">" . $donation->getId() . "</span>";
374     if (check_admin(1)) {
375       echo " " . $donation->getDeleteLink();
376     }
377     echo ": ";
378     echo "\n</p>";
379
380     echo "<table>\n";
381     show_donation_form($donation);
382
383     if (check_admin(1)) {
384       echo "<tr>\n";
385       echo "  <td colspan=2>";
386       submit("update_donation", "Update");
387       echo "</td>\n";
388       echo "</tr>\n";
389     }
390
391     echo "</table>\n";
392     end_form();
393   }
394
395   if (count($parameters)) {
396     if ($parameters[0] == "in") {
397       /* /donation/in/area/Romsey+Town/1 */
398       switch ($parameters[1]) {
399         case "area":
400           $area_id = $parameters[3];
401           $_POST['area_id'] = $area_id;
402           $q = new AreaQuery;
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);
407         break;
408
409         case "city":
410           $city_id = $parameters[3];
411           $_POST['city_id'] = $city_id;
412           $q = new CityQuery;
413           $city = $q->findOneById($city_id);
414           show_city_donations(0, 10, $parameters[2], $city_id);
415         break;
416       }
417     }
418     else if ($parameters[0] == "from") {
419       /* /donation/from/contact/Iain+Patterson/4 */
420       switch ($parameters[1]) {
421         case "contact":
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);
426         break;
427       }
428     }
429     else if ($parameters[0] == "to") {
430       /* /donation/to/hub/Cambridge+Community+Church/1 */
431       switch ($parameters[1]) {
432         case "hub":
433           $hub_id = $parameters[3];
434           $q = new HubQuery;
435           $hub = $q->findOneById($hub_id);
436           show_hub_donations(0, 10, $parameters[2], $hub_id);
437         break;
438       }
439     }
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);
446         }
447       }
448     }
449   }
450   list($ignored, $id, $args) = parse_parameters($parameters);
451   //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
452   if (count($args)) {
453     switch ($args[0]) {
454       case "delete":
455         confirm_delete_donation($id);
456       break;
457
458       case "confirmdelete":
459         delete_donation($id);
460       break;
461     }
462   }
463   else if (isset($id)) show_donation($id);
464
465   show_donation_forms($city_id);
466   show_add_new_donation_form($city_id);
467
468 ?>