Correctly filter city or area.
[readifood.git] / lib / contact.php
1 <?php
2
3   if (isset($_POST['show_add_contact'])) {
4     $city_id = $_POST['city_id'];
5     show_new_contact_form($city_id);
6   }
7   else if (isset($_POST['add_contact'])) {
8     $id = add_contact($displayname);
9     if ($id !== false) {
10       echo "<p>Added contact.</p>\n";
11       $parameters = array($displayname, $id);
12     }
13   }
14   else if (isset($_POST['update_contact'])) {
15     list($name, $id, $args) = parse_parameters($parameters);
16     $q = new ContactQuery;
17     $contact = $q->findOneById($id);
18     if ($contact) {
19       $area = get_contact_area($contact);
20       if ($area) $area_id = $area->getId();
21       if (update_contact($contact, $area_id) !== false) {
22         echo "<p>Updated contact.</p>\n";
23         $parameters = array($contact->getDisplayname(), $contact->getId());
24       }
25     }
26     else {
27       echo "<p>No such contact!</p>\n";
28     }
29   }
30   else if ($_POST['search_contact']) {
31     header(sprintf("Location: http%s://%s/%s/search/%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($_POST['search_contact'])));
32     exit;
33   }
34   else if ($_POST['show_in_area']) {
35     $q = new AreaQuery;
36     $area = $q->findOneById($_POST['area_id']);
37     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     exit;
39   }
40   else if ($_POST['show_in_city']) {
41     $q = new CityQuery;
42     $city = $q->findOneById($_POST['city_id']);
43     header(sprintf("Location: http%s://%s/%s/in/city/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($city->getName()), $_POST['city_id']));
44     exit;
45   }
46
47   function show_contact_summary(&$contact, $editing = false) {
48     if ($editing) echo "<p>Contact: <span class=\"strong\">" . htmlspecialchars($contact->getDisplayname()) . "</span>";
49     else echo "<br>\nContact " . $contact->getStrongLink();
50     $role = $contact->getRole();
51     $role_string = get_contact_role_string($contact);
52     if ($role_string) echo " $role_string";
53     $d = urlencode($contact->getDisplayname());
54     $i = $contact->getId();
55     if ($role & $GLOBALS['ROLE_DONOR']) echo " " . get_small_link("Donations", "/donation/from/contact/%s/%d", $d, $i);
56     if ($role & $GLOBALS['ROLE_REQUESTER']) echo " " . get_small_link("Referred", "/order/from/referrer/%s/%d", $d, $i);
57     if ($role & $GLOBALS['ROLE_BENEFICIARY']) {
58       echo " " . get_small_link("Orders", "/order/to/beneficiary/%s/%d", $d, $contact->getId());
59       if (get_contact_area($contact)) echo " " . get_small_link("Place", "/order/place/for/beneficiary/%s/%d", $d, $i);
60     }
61     if (check_admin(1)) {
62       echo " " . $contact->getDeleteLink();
63     }
64     $area = get_contact_area($contact);
65     echo " in " . $area->getLink();
66     $city = get_contact_city($contact);
67     echo ", " . $city->getLink(get_city_displayname($city));
68   }
69
70   function show_contacts($offset, $per_page, $address_ids) {
71     $q = new ContactQuery;
72     $contacts = $q->filterByAddressId($address_ids)->orderByForename()->orderBySurname()->find();
73     if (count($contacts)) {
74       foreach ($contacts as $contact) show_contact_summary($contact);
75     }
76     else echo " none";
77   }
78
79   function search_contacts($offset, $per_page, $search) {
80     $q = new ContactQuery;
81     $contacts = $q->filterByDisplayname("%$search%")->find();
82     echo "<p>Contacts matching '" . htmlspecialchars($search) . "':";
83     if (count($contacts)) {
84       foreach ($contacts as $contact) show_contact_summary($contact);
85     }
86     else echo "none";
87     echo "</p>\n";
88   }
89
90   function show_city_contacts($offset, $per_page, $city_name, $city_id = null) {
91     if (isset($city_id)) $city = get_city_by_id($city_id);
92     else if ($city_name) $city = get_city_by_name($city_name);
93     if ($city) {
94       $q = new AreaQuery;
95       $areas = $q->filterByCityId($city->getId())->find();
96       $area_ids = array();
97       foreach ($areas as $area) $area_ids[] = $area->getId();
98
99       $q = new AddressQuery;
100       $addresses = $q->filterByAreaId($area_ids)->find();
101       $address_ids = array();
102       foreach ($addresses as $address) $address_ids[] = $address->getId();
103
104       echo "<p>Contacts in city " . $city->getLink(get_city_displayname($city)) . ":";
105       return show_contacts($offset, $per_page, $address_ids);
106     }
107     else echo "<p>No such city!</p>\n";
108   }
109
110   function show_area_contacts($offset, $per_page, $area_name, $area_id = null) {
111     if (isset($area_id)) $area = get_area_by_id($area_id);
112     else if ($area_name) $area = get_area_by_name($area_name);
113     if ($area) {
114       $q = new AddressQuery;
115       $addresses = $q->filterByAreaId($area->getId())->find();
116       $address_ids = array();
117       foreach ($addresses as $address) $address_ids[] = $address->getId();
118
119       echo "<p>Contacts in area " . $area->getLink() . ":";
120       return show_contacts($offset, $per_page, $address_ids);
121     }
122     else echo "<p>No such area!</p>\n";
123   }
124
125   function show_contact_areas_form($city_id = null) {
126     $areas = get_city_areas($city_id);
127     if (! count($areas)) {
128       echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
129       return;
130     }
131
132     $candidates = array();
133     foreach ($areas as $area) {
134       if (! count(get_area_contacts($area->getId()))) continue;
135       $candidates[] = $area;
136     }
137     if (! count($candidates)) return;
138
139     echo "<p>Show contacts in area\n";
140     echo "<select name=\"area_id\">\n";
141     foreach ($candidates as $area) {
142       option("area_id", $area->getId(), get_area_displayname($area));
143     }
144     echo "</select>\n";
145     submit("show_in_area", "Show");
146   }
147
148   function show_contact_cities_form($city_id = null) {
149     $q = new CityQuery;
150     $cities = $q->orderByName()->find();
151
152     if (! count($cities)) {
153       echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
154       return;
155     }
156
157     $candidates = array();
158     foreach ($cities as $city) {
159       if (! count(get_city_contacts($city->getId()))) continue;
160       $candidates[] = $city;
161     }
162     if (! count($candidates)) return;
163
164     echo "<p>Show contacts in city\n";
165     echo "<select name=\"city_id\">\n";
166     foreach ($candidates as $city) {
167       option("city_id", $city->getId(), get_city_displayname($city), $city_id);
168     }
169     echo "</select>\n";
170     submit("show_in_city", "Show");
171   }
172
173   function show_contact_search_form() {
174     echo "<p>Search for contacts:";
175     input("search_contact");
176     echo "<input type=\"submit\" value=\"Search\">\n";
177   }
178
179   function show_contact_forms($city_id) {
180     form("noprint standout");
181     show_contact_areas_form($city_id);
182     show_contact_cities_form($city_id);
183     show_contact_search_form();
184     end_form();
185   }
186
187   function show_contact_role_form($role) {
188     return show_role_form($role, $GLOBALS['contact_roles']);
189   }
190
191   function show_contact_form($contact = null, $new = false) {
192     global $contact_roles, $parcel_sizes, $parcel_contents;
193
194     if (! $contact) $contact = new Contact;
195     else if ($contact->getRole() & ($GLOBALS['ROLE_BENEFICIARY'] | $GLOBALS['ROLE_REQUESTER'])) {
196       $state_mask = $GLOBALS['STATE_ANY'];
197       $state_mask &= ~$GLOBALS['STATE_DELIVERED'];
198       $state_mask &= ~$GLOBALS['STATE_CANCELLED'];
199
200       $orders = get_contact_orders($contact, $state_mask);
201
202       if (count($orders)) {
203         echo "<tr>\n";
204         echo "  <td colspan=2><strong>Outstanding orders:</strong></td>\n";
205         echo "</tr>\n";
206
207         echo "<tr>\n";
208         echo "  <td colspan=2 class=\"history\">\n";
209         foreach ($orders as $order) {
210           echo "    " . get_order_summary($order) . "<br>\n";
211         }
212         echo "  </td>\n";
213         echo "</tr>\n";
214       }
215     }
216
217     /* Role. */
218     echo "<tr>\n";
219     echo "  <td>Role</td>\n";
220     echo "  <td>"; show_contact_role_form($contact->getRole()); echo "</td>\n";
221     echo "</tr>\n";
222
223     /* Date added. */
224     if (! $new) {
225       echo "<tr>\n";
226       echo "  <td>Registered</td>\n";
227       echo "  <td>" . $contact->getAdded() . "</td>\n";
228       echo "</tr>\n";
229     }
230
231     /* Forename. */
232     echo "<tr>\n";
233     echo "  <td>Forename</td>\n";
234     echo "  <td>"; input("forename", $contact->getForename()); echo "</td>\n";
235     echo "</tr>\n";
236
237     /* Middle names. */
238     echo "<tr>\n";
239     echo "  <td>Middle name(s)</td>\n";
240     echo "  <td>"; input("middle", $contact->getMiddle()); echo "</td>\n";
241     echo "</tr>\n";
242
243     /* Surname. */
244     echo "<tr>\n";
245     echo "  <td>Surname</td>\n";
246     echo "  <td>"; input("surname", $contact->getSurname()); echo "</td>\n";
247     echo "</tr>\n";
248
249     /* Display name. */
250     echo "<tr>\n";
251     echo "  <td>Display name (if not concatenation of above)</td>\n";
252     echo "  <td>"; input("displayname", $contact->getDisplayname()); echo "</td>\n";
253     echo "</tr>\n";
254
255     /* Address. */
256     $address = get_contact_address($contact);
257     if (! $address) $address = new Address;
258     echo "<tr>\n";
259     echo "  <td>Address</td>\n";
260     echo "  <td>"; textarea("address", $address->getLine()); echo "</td>\n";
261     echo "</tr>\n";
262
263     /* Postcode. */
264     echo "<tr>\n";
265     echo "  <td>Postcode</td>\n";
266     echo "  <td>"; input("postcode", $address->getPostcode()); echo get_address_map_link($address); echo "</td>\n";
267     echo "</tr>\n";
268
269     /* Telephone. */
270     echo "<tr>\n";
271     echo "  <td>Telephone</td>\n";
272     echo "  <td>"; input("telephone1", $contact->getTelephone1()); echo "</td>\n";
273     echo "</tr>\n";
274     echo "<tr>\n";
275     echo "  <td>Alternative telephone</td>\n";
276     echo "  <td>"; input("telephone2", $contact->getTelephone2()); echo "</td>\n";
277     echo "</tr>\n";
278
279     /* Email. */
280     echo "<tr>\n";
281     echo "  <td>Email</td>\n";
282     echo "  <td>"; input("email", $contact->getEmail()); echo "</td>\n";
283     echo "</tr>\n";
284
285     /* Area. */
286     $area = get_contact_area($contact);
287     if ($area) $area_id = $area->getId();
288     echo "<tr>\n";
289     echo "  <td>Area</td>\n";
290     echo "  <td><select name=\"area_id\">\n";
291     $areas = get_city_areas();
292     foreach ($areas as $area) {
293       option("area_id", $area->getId(), get_area_displayname($area), $area_id);
294     }
295     echo "  </select></td>\n";
296     echo "</tr>\n";
297
298     /* Parcel type. */
299     echo "<tr>\n";
300     echo "  <td>Family unit</td>\n";
301     echo "  <td><select name=\"parcel_size\">\n";
302     $mask = 1 << count($parcel_sizes);
303     for ($i = 0; $i < count($parcel_sizes); $i++) {
304       option("parcel_size", 1 << $i, $parcel_sizes[$i], $contact->getParcel() % $mask);
305     }
306     echo "</select></td>\n";
307     echo "</tr>\n";
308
309     /* Parcel contents. */
310     echo "<tr>\n";
311     echo "  <td>Dietary requirements</td>\n";
312     echo "  <td>";
313     for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
314       if (1 << $i == $GLOBALS['PARCEL_TOILETRY']) continue;
315       echo "  <input type=\"checkbox\" name=\"parcel_$i\"";
316       if ($contact->getParcel() & (1 << $i)) echo " checked";
317       echo ">$parcel_contents[$i]\n";
318     }
319     echo "</td>\n";
320     echo "</tr>\n";
321
322     /* Notes. */
323     echo "<tr>\n";
324     echo "  <td>Notes</td>\n";
325     echo "  <td>"; textarea("notes", $contact->getNotes()); echo "</td>\n";
326     echo "</tr>\n";
327   }
328
329   function show_new_contact_form($city_id = null) {
330     if (! check_admin(1)) return;
331
332     $areas = get_city_areas($city_id);
333     if (! count($areas)) {
334       echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
335       return;
336     }
337
338     form("noprint");
339     echo "<p>Add a new contact:</p>\n";
340
341     echo "<table>\n";
342     show_contact_form($contact, true);
343
344     echo "<tr>\n";
345     echo "  <td colspan=2>"; submit("add_contact", "Add"); echo "</td></tr>\n";
346     echo "</tr>\n";
347     echo "</table>\n";
348     end_form();
349   }
350
351   function show_add_new_contact_form() {
352     if (! check_admin(1)) return;
353
354     $q = new CityQuery;
355     $cities = $q->find();
356     if (! count($cities)) {
357       echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
358       return;
359     }
360
361     form("noprint standout");
362     echo "<p>Add a new contact in <select name=\"city_id\">\n";
363     foreach ($cities as $city) {
364       option("city_id", $city->getId(), get_city_displayname($city));
365     }
366     echo "</select>";
367     submit("show_add_contact", "Proceed");
368     echo "</p>\n";
369     end_form();
370   }
371
372   function update_contact(&$contact, $area_id, $new = false) {
373     global $contact_roles, $parcel_sizes, $parcel_contents;
374
375     $role = 0;
376     for ($i = 0; $i < count($contact_roles); $i++) {
377       if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
378     }
379
380     /* Staff can place orders. */
381     if ($role & (1 << 0)) $role |= (1 << 2);
382
383     $forename = $_POST['forename'];
384     $middle = $_POST['middle'];
385     $surname = $_POST['surname'];
386     $displayname = $_POST['displayname'];
387
388     if (! $forename && ! $surname) {
389       echo "<p>Must have either a forename or surname!</p>\n";
390       return false;
391     }
392     if ($middle && ! ($forename && $surname)) {
393       echo "<p>Must have both a forename or surname for middle name(s) to make sense!</p>\n";
394       return false;
395     }
396
397     if (! $displayname) {
398       $displayname = $forename;
399       if ($middle) $displayname .= " $middle";
400       if ($forename) $displayname .= " ";
401       $displayname .= $surname;
402       echo "<p>Display name will be $displayname.</p>\n";
403     }
404
405     /* Get address. */
406     $area_id = $_POST['area_id'];
407     $line = $_POST['address'];
408     $postcode = $_POST['postcode'];
409     $q = new AddressQuery;
410     /* XXX: Finding by area properly? */
411     $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
412     if ($address->isNew()) {
413       /* Changing address. */
414       //if (! $new)
415       /*
416         XXX: Check for other contacts at the old address.
417         Make this a new address if there are others, but
418         provide a link to update other contacts.
419       */
420       try {
421         $address->save();
422       }
423       catch (Exception $e) {
424         echo "<p>Error adding $line.</p>\n";
425         return false;
426       }
427     }
428
429     $telephone1 = $_POST['telephone1'];
430     $telephone2 = $_POST['telephone2'];
431     $email = $_POST['email'];
432     $parcel = $_POST['parcel_size'];
433     for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
434       if ($_POST['parcel_' . $i] == "on") $parcel |= (1 << $i);
435     }
436     $notes = $_POST['notes'];
437
438     $contact->setRole($role);
439     $contact->setForename($forename);
440     $contact->setMiddle($middle);
441     $contact->setSurname($surname);
442     $contact->setDisplayname($displayname);
443     $contact->setTelephone1($telephone1);
444     $contact->setTelephone2($telephone2);
445     $contact->setEmail($email);
446     $contact->setParcel($parcel);
447     $contact->setNotes($notes);
448     $contact->setAddressId($address->getId());
449
450     try {
451       $contact->save();
452     }
453     catch (Exception $e) {
454       if ($new) echo "<p>Error adding $displayname.</p>\n";
455       else echo "<p>Error updating $displayname.</p>\n";
456       return false;
457     }
458
459     return true;
460   }
461
462   function add_contact(&$name) {
463     if (! check_admin(1, "add a contact")) return;
464
465     $area_id = $_POST['area_id'];
466     if (! is_numeric($area_id)) {
467       echo "<p>Invalid area!</p>\n";
468       return false;
469     }
470
471     $area = get_area_by_id($area_id);
472     if (! $area) {
473       echo "<p>No such area!</p>\n";
474       return false;
475     }
476
477     $contact = new Contact;
478     if (! update_contact($contact, $area_id, true)) return false;
479     return $contact->getId();
480   }
481
482   function confirm_delete_contact($name, $id = null, &$city_id = null) {
483     if (! check_admin(1, "delete a contact")) return;
484
485     if (isset($id)) $contact = get_contact_by_id($id);
486     else $contact = get_contact_by_name($name);
487     if (! $contact) return false;
488
489     echo "<h3>Confirm deletion</h3>\n";
490     echo "<p>You must confirm deletion of contact " . $contact->getDisplayname() . ": " . $contact->getDeleteLink(true) . "</p>\n";
491   }
492
493   function delete_contact($name, $id = null, &$city_id = null) {
494     if (! check_admin(1, "delete a contact")) return;
495
496     if (isset($id)) $contact = get_contact_by_id($id);
497     else $contact = get_contact_by_name($name);
498     if (! $contact) return false;
499
500     ///* Remember city ID for dropdown. */
501     //$city_id = $area->getCityId();
502
503     try {
504       $contact->delete();
505       echo "<p>Deleted contact.</p>\n";
506     }
507     catch (Exception $e) {
508       echo "<p>Error deleting $name!</p>\n";
509       return false;
510     }
511
512     return true;
513   }
514
515   function show_contact($name, &$id = null) {
516     if (isset($id)) $contact = get_contact_by_id($id);
517     else $contact = get_contact_by_name($name);
518     if (! $contact) return;
519
520     form();
521     show_contact_summary($contact, true);
522     echo ": ";
523     echo "\n</p>";
524
525     echo "<table>\n";
526     show_contact_form($contact);
527
528     if (check_admin(1)) {
529       echo "<tr>\n";
530       echo "  <td colspan=2>";
531       submit("update_contact", "Update");
532       echo "</td>\n";
533       echo "</tr>\n";
534     }
535
536     echo "</table>\n";
537     end_form();
538   }
539
540   /* /contact/in/area/Cambridge/1 */
541   if (count($parameters)) {
542     if ($parameters[0] == "in") {
543       switch ($parameters[1]) {
544         case "area":
545           $area_id = $parameters[3];
546           $_POST['area_id'] = $area_id;
547           $q = new AreaQuery;
548           $area = $q->findOneById($area_id);
549           $city = get_area_city($area);
550           if ($city) $city_id = $city->getId();
551           show_area_contacts(0, 10, $parameters[2], $area_id);
552         break;
553
554         case "city":
555           $city_id = $parameters[3];
556           $_POST['city_id'] = $city_id;
557           $q = new CityQuery;
558           $city = $q->findOneById($city_id);
559           show_city_contacts(0, 10, $parameters[2], $city_id);
560         break;
561       }
562     }
563     else if ($parameters[0] == "search") {
564       search_contacts(0, 10, $parameters[1]);
565     }
566   }
567   list($name, $id, $args) = parse_parameters($parameters);
568   //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
569   if (count($args)) {
570     switch ($args[0]) {
571       case "delete":
572         confirm_delete_contact($name, $id);
573       break;
574
575       case "confirmdelete":
576         delete_contact($name, $id);
577       break;
578     }
579   }
580   else if (isset($name)) show_contact($name, $id);
581
582   show_contact_forms($city_id);
583   show_add_new_contact_form($city_id);
584
585 ?>