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