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