Extend contact search to address.
[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     echo "  <td>"; input("postcode", $address->getPostcode()); echo get_address_map_link($address); echo "</td>\n";
284     echo "</tr>\n";
285
286     /* Telephone. */
287     echo "<tr>\n";
288     echo "  <td>Telephone</td>\n";
289     echo "  <td>"; input("telephone1", $contact->getTelephone1()); echo "</td>\n";
290     echo "</tr>\n";
291     echo "<tr>\n";
292     echo "  <td>Alternative telephone</td>\n";
293     echo "  <td>"; input("telephone2", $contact->getTelephone2()); echo "</td>\n";
294     echo "</tr>\n";
295
296     /* Email. */
297     echo "<tr>\n";
298     echo "  <td>Email</td>\n";
299     echo "  <td>"; input("email", $contact->getEmail()); echo "</td>\n";
300     echo "</tr>\n";
301
302     /* Area. */
303     $area = get_contact_area($contact);
304     if ($area) $area_id = $area->getId();
305     echo "<tr>\n";
306     echo "  <td>Area</td>\n";
307     echo "  <td><select name=\"area_id\">\n";
308     $areas = get_city_areas();
309     foreach ($areas as $area) {
310       option("area_id", $area->getId(), get_area_displayname($area), $area_id);
311     }
312     echo "  </select></td>\n";
313     echo "</tr>\n";
314
315     /* Parcel type. */
316     echo "<tr>\n";
317     echo "  <td>Family unit</td>\n";
318     echo "  <td><select name=\"parcel_size\">\n";
319     $mask = 1 << count($parcel_sizes);
320     for ($i = 0; $i < count($parcel_sizes); $i++) {
321       option("parcel_size", 1 << $i, $parcel_sizes[$i], $contact->getParcel() % $mask);
322     }
323     echo "</select></td>\n";
324     echo "</tr>\n";
325
326     /* Parcel contents. */
327     echo "<tr>\n";
328     echo "  <td>Dietary requirements</td>\n";
329     echo "  <td>";
330     for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
331       if (1 << $i == $GLOBALS['PARCEL_TOILETRY']) continue;
332       echo "  <input type=\"checkbox\" name=\"parcel_$i\"";
333       if ($contact->getParcel() & (1 << $i)) echo " checked";
334       echo ">$parcel_contents[$i]\n";
335     }
336     echo "</td>\n";
337     echo "</tr>\n";
338
339     /* Notes. */
340     echo "<tr>\n";
341     echo "  <td>Notes</td>\n";
342     echo "  <td>"; textarea("notes", $contact->getNotes()); echo "</td>\n";
343     echo "</tr>\n";
344   }
345
346   function show_new_contact_form($city_id = null) {
347     if (! check_admin(1)) return;
348
349     $areas = get_city_areas($city_id);
350     if (! count($areas)) {
351       echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
352       return;
353     }
354
355     form("noprint");
356     echo "<p>Add a new contact:</p>\n";
357
358     echo "<table>\n";
359     show_contact_form($contact, true);
360
361     echo "<tr>\n";
362     echo "  <td colspan=2>"; submit("add_contact", "Add"); echo "</td></tr>\n";
363     echo "</tr>\n";
364     echo "</table>\n";
365     end_form();
366   }
367
368   function show_add_new_contact_form() {
369     if (! check_admin(1)) return;
370
371     $q = new CityQuery;
372     $cities = $q->find();
373     if (! count($cities)) {
374       echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
375       return;
376     }
377
378     form("noprint standout");
379     echo "<p>Add a new contact in <select name=\"city_id\">\n";
380     foreach ($cities as $city) {
381       option("city_id", $city->getId(), get_city_displayname($city));
382     }
383     echo "</select>";
384     submit("show_add_contact", "Proceed");
385     echo "</p>\n";
386     end_form();
387   }
388
389   function update_contact(&$contact, $area_id, $new = false) {
390     global $contact_roles, $parcel_sizes, $parcel_contents;
391
392     $role = 0;
393     for ($i = 0; $i < count($contact_roles); $i++) {
394       if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
395     }
396
397     /* Staff can place orders. */
398     if ($role & (1 << 0)) $role |= (1 << 2);
399
400     $forename = $_POST['forename'];
401     $middle = $_POST['middle'];
402     $surname = $_POST['surname'];
403     $displayname = $_POST['displayname'];
404
405     if (! $forename && ! $surname) {
406       echo "<p>Must have either a forename or surname!</p>\n";
407       return false;
408     }
409     if ($middle && ! ($forename && $surname)) {
410       echo "<p>Must have both a forename or surname for middle name(s) to make sense!</p>\n";
411       return false;
412     }
413
414     if (! $displayname) {
415       $displayname = $forename;
416       if ($middle) $displayname .= " $middle";
417       if ($forename) $displayname .= " ";
418       $displayname .= $surname;
419       echo "<p>Display name will be $displayname.</p>\n";
420     }
421
422     /* Get address. */
423     $area_id = $_POST['area_id'];
424     $line = $_POST['address'];
425     $postcode = $_POST['postcode'];
426     $q = new AddressQuery;
427     /* XXX: Finding by area properly? */
428     $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
429     if ($address->isNew()) {
430       /* Changing address. */
431       //if (! $new)
432       /*
433         XXX: Check for other contacts at the old address.
434         Make this a new address if there are others, but
435         provide a link to update other contacts.
436       */
437       try {
438         $address->save();
439       }
440       catch (Exception $e) {
441         echo "<p>Error adding $line.</p>\n";
442         return false;
443       }
444     }
445
446     $telephone1 = $_POST['telephone1'];
447     $telephone2 = $_POST['telephone2'];
448     $email = $_POST['email'];
449     $parcel = $_POST['parcel_size'];
450     for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
451       if ($_POST['parcel_' . $i] == "on") $parcel |= (1 << $i);
452     }
453     $notes = $_POST['notes'];
454
455     $contact->setRole($role);
456     $contact->setForename($forename);
457     $contact->setMiddle($middle);
458     $contact->setSurname($surname);
459     $contact->setDisplayname($displayname);
460     $contact->setTelephone1($telephone1);
461     $contact->setTelephone2($telephone2);
462     $contact->setEmail($email);
463     $contact->setParcel($parcel);
464     $contact->setNotes($notes);
465     $contact->setAddressId($address->getId());
466
467     try {
468       $contact->save();
469     }
470     catch (Exception $e) {
471       if ($new) echo "<p>Error adding $displayname.</p>\n";
472       else echo "<p>Error updating $displayname.</p>\n";
473       return false;
474     }
475
476     return true;
477   }
478
479   function add_contact(&$name) {
480     if (! check_admin(1, "add a contact")) return;
481
482     $area_id = $_POST['area_id'];
483     if (! is_numeric($area_id)) {
484       echo "<p>Invalid area!</p>\n";
485       return false;
486     }
487
488     $area = get_area_by_id($area_id);
489     if (! $area) {
490       echo "<p>No such area!</p>\n";
491       return false;
492     }
493
494     $contact = new Contact;
495     if (! update_contact($contact, $area_id, true)) return false;
496     return $contact->getId();
497   }
498
499   function confirm_delete_contact($name, $id = null, &$city_id = null) {
500     if (! check_admin(1, "delete a contact")) return;
501
502     if (isset($id)) $contact = get_contact_by_id($id);
503     else $contact = get_contact_by_name($name);
504     if (! $contact) return false;
505
506     echo "<h3>Confirm deletion</h3>\n";
507     echo "<p>You must confirm deletion of contact " . $contact->getDisplayname() . ": " . $contact->getDeleteLink(true) . "</p>\n";
508   }
509
510   function 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     ///* Remember city ID for dropdown. */
518     //$city_id = $area->getCityId();
519
520     try {
521       $contact->delete();
522       echo "<p>Deleted contact.</p>\n";
523     }
524     catch (Exception $e) {
525       echo "<p>Error deleting $name!</p>\n";
526       return false;
527     }
528
529     return true;
530   }
531
532   function show_contact($name, &$id = null) {
533     if (isset($id)) $contact = get_contact_by_id($id);
534     else $contact = get_contact_by_name($name);
535     if (! $contact) return;
536
537     form();
538     show_contact_summary($contact, true);
539     echo ": ";
540     echo "\n</p>";
541
542     echo "<table>\n";
543     show_contact_form($contact);
544
545     if (check_admin(1)) {
546       echo "<tr>\n";
547       echo "  <td colspan=2>";
548       submit("update_contact", "Update");
549       echo "</td>\n";
550       echo "</tr>\n";
551     }
552
553     echo "</table>\n";
554     end_form();
555   }
556
557   /* /contact/in/area/Cambridge/1 */
558   if (count($parameters)) {
559     if ($parameters[0] == "in") {
560       switch ($parameters[1]) {
561         case "area":
562           $area_id = $parameters[3];
563           $_POST['area_id'] = $area_id;
564           $q = new AreaQuery;
565           $area = $q->findOneById($area_id);
566           $city = get_area_city($area);
567           if ($city) $city_id = $city->getId();
568           show_area_contacts(0, 10, $parameters[2], $area_id);
569         break;
570
571         case "city":
572           $city_id = $parameters[3];
573           $_POST['city_id'] = $city_id;
574           $q = new CityQuery;
575           $city = $q->findOneById($city_id);
576           show_city_contacts(0, 10, $parameters[2], $city_id);
577         break;
578       }
579     }
580     else if ($parameters[0] == "search") {
581       search_contacts(0, 10, $parameters[1]);
582     }
583   }
584   list($name, $id, $args) = parse_parameters($parameters);
585   //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
586   if (count($args)) {
587     switch ($args[0]) {
588       case "delete":
589         confirm_delete_contact($name, $id);
590       break;
591
592       case "confirmdelete":
593         delete_contact($name, $id);
594       break;
595     }
596   }
597   else if (isset($name)) show_contact($name, $id);
598
599   show_contact_forms($city_id);
600   show_add_new_contact_form($city_id);
601
602 ?>