Tidy up forms.
[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['area_id']) {
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['city_id']) {
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     echo "<p>Show contacts in area\n";
133     echo "<select name=\"area_id\">\n";
134     foreach ($areas as $area) {
135       option("area_id", $area->getId(), get_area_displayname($area));
136     }
137     echo "</select>\n";
138     echo "<input type=\"submit\" value=\"Show\">\n";
139   }
140
141   function show_contact_cities_form($city_id = null) {
142     $q = new CityQuery;
143     $cities = $q->orderByName()->find();
144
145     if (! count($cities)) {
146       echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
147       return;
148     }
149
150     echo "<p>Show contacts in city\n";
151     echo "<select name=\"city_id\">\n";
152     foreach ($cities as $city) {
153       option("city_id", $city->getId(), get_city_displayname($city), $city_id);
154     }
155     echo "</select>\n";
156     echo "<input type=\"submit\" value=\"Show\">\n";
157   }
158
159   function show_contact_search_form() {
160     echo "<p>Search for contacts:";
161     input("search_contact");
162     echo "<input type=\"submit\" value=\"Search\">\n";
163   }
164
165   function show_contact_forms($city_id) {
166     form("noprint standout");
167     show_contact_areas_form($city_id);
168     show_contact_cities_form($city_id);
169     show_contact_search_form();
170     end_form();
171   }
172
173   function show_contact_role_form($role) {
174     return show_role_form($role, $GLOBALS['contact_roles']);
175   }
176
177   function show_contact_form($contact = null, $new = false) {
178     global $contact_roles, $parcel_sizes, $parcel_contents;
179
180     if (! $contact) $contact = new Contact;
181     else if ($contact->getRole() & $GLOBALS['ROLE_BENEFICIARY']) {
182       $state_mask = $GLOBALS['STATE_ANY'];
183       $state_mask &= ~$GLOBALS['STATE_DELIVERED'];
184       $state_mask &= ~$GLOBALS['STATE_CANCELLED'];
185
186       $orders = get_contact_orders($contact, $state_mask);
187
188       if (count($orders)) {
189         echo "<tr>\n";
190         echo "  <td colspan=2><strong>Outstanding orders:</strong></td>\n";
191         echo "</tr>\n";
192
193         echo "<tr>\n";
194         echo "  <td colspan=2>\n";
195         foreach ($orders as $order) {
196           echo "    Order " . $order->getStrongLink($order->getId()) . ": " . get_order_displayname($order) . "<br>\n";
197         }
198         echo "  </td>\n";
199         echo "</tr>\n";
200       }
201     }
202
203     /* Role. */
204     echo "<tr>\n";
205     echo "  <td>Role</td>\n";
206     echo "  <td>"; show_contact_role_form($contact->getRole()); echo "</td>\n";
207     echo "</tr>\n";
208
209     /* Date added. */
210     if (! $new) {
211       echo "<tr>\n";
212       echo "  <td>Registered</td>\n";
213       echo "  <td>" . $contact->getAdded() . "</td>\n";
214       echo "</tr>\n";
215     }
216
217     /* Forename. */
218     echo "<tr>\n";
219     echo "  <td>Forename</td>\n";
220     echo "  <td>"; input("forename", $contact->getForename()); echo "</td>\n";
221     echo "</tr>\n";
222
223     /* Middle names. */
224     echo "<tr>\n";
225     echo "  <td>Middle name(s)</td>\n";
226     echo "  <td>"; input("middle", $contact->getMiddle()); echo "</td>\n";
227     echo "</tr>\n";
228
229     /* Surname. */
230     echo "<tr>\n";
231     echo "  <td>Surname</td>\n";
232     echo "  <td>"; input("surname", $contact->getSurname()); echo "</td>\n";
233     echo "</tr>\n";
234
235     /* Display name. */
236     echo "<tr>\n";
237     echo "  <td>Display name (if not concatenation of above)</td>\n";
238     echo "  <td>"; input("displayname", $contact->getDisplayname()); echo "</td>\n";
239     echo "</tr>\n";
240
241     /* Address. */
242     $address = get_contact_address($contact);
243     if (! $address) $address = new Address;
244     echo "<tr>\n";
245     echo "  <td>Address</td>\n";
246     echo "  <td>"; textarea("address", $address->getLine()); echo "</td>\n";
247     echo "</tr>\n";
248
249     /* Postcode. */
250     echo "<tr>\n";
251     echo "  <td>Postcode</td>\n";
252     echo "  <td>"; input("postcode", $address->getPostcode()); echo "</td>\n";
253     echo "</tr>\n";
254
255     /* Telephone. */
256     echo "<tr>\n";
257     echo "  <td>Telephone</td>\n";
258     echo "  <td>"; input("telephone1", $contact->getTelephone1()); echo "</td>\n";
259     echo "</tr>\n";
260     echo "<tr>\n";
261     echo "  <td>Alternative telephone</td>\n";
262     echo "  <td>"; input("telephone2", $contact->getTelephone2()); echo "</td>\n";
263     echo "</tr>\n";
264
265     /* Email. */
266     echo "<tr>\n";
267     echo "  <td>Email</td>\n";
268     echo "  <td>"; input("email", $contact->getEmail()); echo "</td>\n";
269     echo "</tr>\n";
270
271     /* Area. */
272     $area = get_contact_area($contact);
273     if ($area) $area_id = $area->getId();
274     echo "<tr>\n";
275     echo "  <td>Area</td>\n";
276     echo "  <td><select name=\"area_id\">\n";
277     $areas = get_city_areas();
278     foreach ($areas as $area) {
279       option("area_id", $area->getId(), get_area_displayname($area), $area_id);
280     }
281     echo "  </select></td>\n";
282     echo "</tr>\n";
283
284     /* Parcel type. */
285     echo "<tr>\n";
286     echo "  <td>Family unit</td>\n";
287     echo "  <td><select name=\"parcel_size\">\n";
288     $mask = 1 << count($parcel_sizes);
289     for ($i = 0; $i < count($parcel_sizes); $i++) {
290       option("parcel_size", 1 << $i, $parcel_sizes[$i], $contact->getParcel() % $mask);
291     }
292     echo "</select></td>\n";
293     echo "</tr>\n";
294
295     /* Parcel contents. */
296     echo "<tr>\n";
297     echo "  <td>Dietary requirements</td>\n";
298     echo "  <td>";
299     for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
300       if (1 << $i == $GLOBALS['PARCEL_TOILETRY']) continue;
301       echo "  <input type=\"checkbox\" name=\"parcel_$i\"";
302       if ($contact->getParcel() & (1 << $i)) echo " checked";
303       echo ">$parcel_contents[$i]\n";
304     }
305     echo "</td>\n";
306     echo "</tr>\n";
307
308     /* Notes. */
309     echo "<tr>\n";
310     echo "  <td>Notes</td>\n";
311     echo "  <td><textarea name=\"notes\">" . $contact->getNotes() . "</textarea></td>\n";
312     echo "</tr>\n";
313   }
314
315   function show_new_contact_form($city_id = null) {
316     if (! check_admin(1)) return;
317
318     $areas = get_city_areas($city_id);
319     if (! count($areas)) {
320       echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
321       return;
322     }
323
324     form("noprint");
325     echo "<p>Add a new contact:</p>\n";
326
327     echo "<table>\n";
328     show_contact_form($contact, true);
329
330     echo "<tr>\n";
331     echo "  <td colspan=2>"; submit("add_contact", "Add"); echo "</td></tr>\n";
332     echo "</tr>\n";
333     echo "</table>\n";
334     end_form();
335   }
336
337   function show_add_new_contact_form() {
338     if (! check_admin(1)) return;
339
340     $q = new CityQuery;
341     $cities = $q->find();
342     if (! count($cities)) {
343       echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
344       return;
345     }
346
347     form("noprint standout");
348     echo "<p>Add a new contact in <select name=\"city_id\">\n";
349     foreach ($cities as $city) {
350       option("city_id", $city->getId(), get_city_displayname($city));
351     }
352     echo "</select>";
353     submit("show_add_contact", "Proceed");
354     echo "</p>\n";
355     end_form();
356   }
357
358   function update_contact(&$contact, $area_id, $new = false) {
359     global $contact_roles, $parcel_sizes, $parcel_contents;
360
361     $role = 0;
362     for ($i = 0; $i < count($contact_roles); $i++) {
363       if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
364     }
365
366     /* Staff can place orders. */
367     if ($role & (1 << 0)) $role |= (1 << 2);
368
369     $forename = $_POST['forename'];
370     $middle = $_POST['middle'];
371     $surname = $_POST['surname'];
372     $displayname = $_POST['displayname'];
373
374     if (! $forename && ! $surname) {
375       echo "<p>Must have either a forename or surname!</p>\n";
376       return false;
377     }
378     if ($middle && ! ($forename && $surname)) {
379       echo "<p>Must have both a forename or surname for middle name(s) to make sense!</p>\n";
380       return false;
381     }
382
383     if (! $displayname) {
384       $displayname = $forename;
385       if ($middle) $displayname .= " $middle";
386       if ($forename) $displayname .= " ";
387       $displayname .= $surname;
388       echo "<p>Display name will be $displayname.</p>\n";
389     }
390
391     /* Get address. */
392     $area_id = $_POST['area_id'];
393     $line = $_POST['address'];
394     $postcode = $_POST['postcode'];
395     $q = new AddressQuery;
396     /* XXX: Finding by area properly? */
397     $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
398     if ($address->isNew()) {
399       /* Changing address. */
400       //if (! $new)
401       /*
402         XXX: Check for other contacts at the old address.
403         Make this a new address if there are others, but
404         provide a link to update other contacts.
405       */
406       try {
407         $address->save();
408       }
409       catch (Exception $e) {
410         echo "<p>Error adding $line.</p>\n";
411         return false;
412       }
413     }
414
415     $telephone1 = $_POST['telephone1'];
416     $telephone2 = $_POST['telephone2'];
417     $email = $_POST['email'];
418     $parcel = $_POST['parcel_size'];
419     for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
420       if ($_POST['parcel_' . $i] == "on") $parcel |= (1 << $i);
421     }
422     $notes = $_POST['notes'];
423
424     $contact->setRole($role);
425     $contact->setForename($forename);
426     $contact->setMiddle($middle);
427     $contact->setSurname($surname);
428     $contact->setDisplayname($displayname);
429     $contact->setTelephone1($telephone1);
430     $contact->setTelephone2($telephone2);
431     $contact->setEmail($email);
432     $contact->setParcel($parcel);
433     $contact->setNotes($notes);
434     $contact->setAddressId($address->getId());
435
436     try {
437       $contact->save();
438     }
439     catch (Exception $e) {
440       if ($new) echo "<p>Error adding $displayname.</p>\n";
441       else echo "<p>Error updating $displayname.</p>\n";
442       return false;
443     }
444
445     return true;
446   }
447
448   function add_contact(&$name) {
449     if (! check_admin(1, "add a contact")) return;
450
451     $area_id = $_POST['area_id'];
452     if (! is_numeric($area_id)) {
453       echo "<p>Invalid area!</p>\n";
454       return false;
455     }
456
457     $area = get_area_by_id($area_id);
458     if (! $area) {
459       echo "<p>No such area!</p>\n";
460       return false;
461     }
462
463     $contact = new Contact;
464     if (! update_contact($contact, $area_id, true)) return false;
465     return $contact->getId();
466   }
467
468   function delete_contact($name, $id = null, &$city_id = null) {
469     if (! check_admin(1, "delete a contact")) return;
470
471     if (isset($id)) $contact = get_contact_by_id($id);
472     else $contact = get_contact_by_name($name);
473     if (! $contact) return false;
474
475     ///* Remember city ID for dropdown. */
476     //$city_id = $area->getCityId();
477
478     try {
479       $contact->delete();
480       echo "<p>Deleted contact.</p>\n";
481     }
482     catch (Exception $e) {
483       echo "<p>Error deleting $name!</p>\n";
484       return false;
485     }
486
487     return true;
488   }
489
490   function show_contact($name, &$id = null) {
491     if (isset($id)) $contact = get_contact_by_id($id);
492     else $contact = get_contact_by_name($name);
493     if (! $contact) return;
494
495     form();
496     show_contact_summary($contact, true);
497     echo ": ";
498     echo "\n</p>";
499
500     echo "<table>\n";
501     show_contact_form($contact);
502
503     if (check_admin(1)) {
504       echo "<tr>\n";
505       echo "  <td colspan=2>";
506       submit("update_contact", "Update");
507       echo "</td>\n";
508       echo "</tr>\n";
509     }
510
511     echo "</table>\n";
512     end_form();
513   }
514
515   /* /contact/in/area/Cambridge/1 */
516   if (count($parameters)) {
517     if ($parameters[0] == "in") {
518       switch ($parameters[1]) {
519         case "area":
520           $area_id = $parameters[3];
521           $_POST['area_id'] = $area_id;
522           $q = new AreaQuery;
523           $area = $q->findOneById($area_id);
524           $city = get_area_city($area);
525           if ($city) $city_id = $city->getId();
526           show_area_contacts(0, 10, $parameters[2], $area_id);
527         break;
528
529         case "city":
530           $city_id = $parameters[3];
531           $_POST['city_id'] = $city_id;
532           $q = new CityQuery;
533           $city = $q->findOneById($city_id);
534           show_city_contacts(0, 10, $parameters[2], $city_id);
535         break;
536       }
537     }
538     else if ($parameters[0] == "search") {
539       search_contacts(0, 10, $parameters[1]);
540     }
541   }
542   list($name, $id, $args) = parse_parameters($parameters);
543   //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
544   if (count($args)) {
545     switch ($args[0]) {
546       case "delete":
547         delete_contact($name, $id);
548       break;
549     }
550   }
551   else if (isset($name)) show_contact($name, $id);
552
553   show_contact_forms($city_id);
554   show_add_new_contact_form($city_id);
555
556 ?>