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);
8 else if (isset($_POST['add_contact'])) {
9 set_last_selected("area_id", $_POST['area_id']);
10 $id = add_contact($displayname);
12 echo "<p>Added contact.</p>\n";
13 $parameters = array($displayname, $id);
16 else if (isset($_POST['update_contact'])) {
17 list($name, $id, $args) = parse_parameters($parameters);
18 $q = new ContactQuery;
19 $contact = $q->findOneById($id);
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());
29 echo "<p>No such contact!</p>\n";
32 else if ($_POST['search_contact']) {
33 header(sprintf("Location: http%s://%s/%s/search/%s%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, $_POST['search_by_phone'] ? "phone/" : "", urlencode($_POST['search_contact'])));
36 else if ($_POST['show_in_area']) {
37 set_last_selected("area_id", $_POST['area_id']);
39 $area = $q->findOneById($_POST['area_id']);
40 header(sprintf("Location: http%s://%s/%s/in/area/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($area->getName()), $_POST['area_id']));
43 else if ($_POST['show_in_city']) {
44 set_last_selected("city_id", $_POST['city_id']);
46 $city = $q->findOneById($_POST['city_id']);
47 header(sprintf("Location: http%s://%s/%s/in/city/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($city->getName()), $_POST['city_id']));
51 function show_contact_summary(&$contact, $editing = false) {
52 if ($editing) echo "<p>Contact: <span class=\"strong\">" . htmlspecialchars($contact->getDisplayname()) . "</span>";
53 else echo "<br>\nContact " . $contact->getStrongLink();
54 $role = $contact->getRole();
55 $role_string = get_contact_role_string($contact);
56 if ($role_string) echo " $role_string";
57 $d = urlencode($contact->getDisplayname());
58 $i = $contact->getId();
59 if ($role & $GLOBALS['ROLE_DONOR']) echo " " . get_small_link("Donations", "/donation/from/contact/%s/%d", $d, $i);
60 if ($role & $GLOBALS['ROLE_REQUESTER']) echo " " . get_small_link("Referred", "/order/from/referrer/%s/%d", $d, $i);
61 if ($role & $GLOBALS['ROLE_BENEFICIARY']) {
62 echo " " . get_small_link("Orders", "/order/to/beneficiary/%s/%d", $d, $contact->getId());
63 if (get_contact_area($contact)) echo " " . get_small_link("Place", "/order/place/for/beneficiary/%s/%d", $d, $i);
66 echo " " . $contact->getDeleteLink();
68 $area = get_contact_area($contact);
69 echo " in " . $area->getLink();
70 $city = get_contact_city($contact);
71 echo ", " . $city->getLink(get_city_displayname($city));
74 function show_contacts($address_ids) {
75 list($first_page, $per_page) = pagination();
76 $q = new ContactQuery;
77 $contacts = $q->filterByAddressId($address_ids)->orderByForename()->orderBySurname()->orderById('desc')->paginate($first_page, $per_page);
78 if (count($contacts)) {
79 foreach ($contacts as $contact) show_contact_summary($contact);
80 show_pagination($contacts);
85 function search_contacts($search, $phone = false) {
86 list($first_page, $per_page) = pagination();
87 $q = new ContactQuery;
88 if ($phone) $q->filterByTelephone1("%$search%")->_or()->filterByTelephone2("%$search%");
89 else $q->filterByDisplayname("%$search%");
90 $contacts = $q->orderByForename()->orderBySurname()->orderById('desc')->paginate($first_page, $per_page);
91 echo "<p>Contacts matching " . ($phone ? "telephone " : "") . "'" . htmlspecialchars($search) . "':";
92 if (count($contacts)) {
93 foreach ($contacts as $contact) show_contact_summary($contact);
94 show_pagination($contacts);
99 $address_ids = array();
101 $q = new AddressQuery;
102 $addresses = $q->filterByLine("%$search%")->find();
103 foreach ($addresses as $address) $address_ids[] = $address->getId();
104 $q = new AddressQuery;
105 $addresses = $q->filterByPostcode("%$search%")->find();
106 foreach ($addresses as $address) $address_ids[] = $address->getId();
108 $q = new ContactQuery;
109 $contacts = $q->filterByAddressId($address_ids)->orderByForename()->orderBySurname()->orderById('desc')->paginate($first_page, $per_page);
110 echo "<p>Contacts in address '" . htmlspecialchars($search) . "':";
111 if (count($contacts)) {
112 foreach ($contacts as $contact) show_contact_summary($contact);
113 show_pagination($contacts);
120 function show_city_contacts($city_name, $city_id = null) {
121 if (isset($city_id)) $city = get_city_by_id($city_id);
122 else if ($city_name) $city = get_city_by_name($city_name);
125 $areas = $q->filterByCityId($city->getId())->find();
127 foreach ($areas as $area) $area_ids[] = $area->getId();
129 $q = new AddressQuery;
130 $addresses = $q->filterByAreaId($area_ids)->find();
131 $address_ids = array();
132 foreach ($addresses as $address) $address_ids[] = $address->getId();
134 echo "<p>Contacts in city " . $city->getLink(get_city_displayname($city)) . ":";
135 return show_contacts($address_ids);
137 else echo "<p>No such city!</p>\n";
140 function show_area_contacts($area_name, $area_id = null) {
141 if (isset($area_id)) $area = get_area_by_id($area_id);
142 else if ($area_name) $area = get_area_by_name($area_name);
144 $q = new AddressQuery;
145 $addresses = $q->filterByAreaId($area->getId())->find();
146 $address_ids = array();
147 foreach ($addresses as $address) $address_ids[] = $address->getId();
149 echo "<p>Contacts in area " . $area->getLink() . ":";
150 return show_contacts($address_ids);
152 else echo "<p>No such area!</p>\n";
155 function show_contact_areas_form($city_id = null) {
156 $areas = get_city_areas($city_id);
157 if (! count($areas)) {
158 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
162 $candidates = array();
163 foreach ($areas as $area) {
164 if (! count(get_area_contacts($area->getId()))) continue;
165 $candidates[] = $area;
167 if (! count($candidates)) return;
169 echo "<p>Show contacts in area\n";
170 echo "<select name=\"area_id\">\n";
171 foreach ($candidates as $area) {
172 option("area_id", $area->getId(), get_area_displayname($area));
175 submit("show_in_area", "Show");
178 function show_contact_cities_form($city_id = null) {
180 $cities = $q->orderByName()->find();
182 if (! count($cities)) {
183 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
187 $candidates = array();
188 foreach ($cities as $city) {
189 if (! count(get_city_contacts($city->getId()))) continue;
190 $candidates[] = $city;
192 if (! count($candidates)) return;
194 echo "<p>Show contacts in city\n";
195 echo "<select name=\"city_id\">\n";
196 foreach ($candidates as $city) {
197 option("city_id", $city->getId(), get_city_displayname($city), $city_id);
200 submit("show_in_city", "Show");
203 function show_contact_search_form() {
204 echo "<p>Search for contacts:";
205 input("search_contact");
206 echo "<input type=\"checkbox\" id=\"search_by_phone\" name=\"search_by_phone\">";
207 echo "<label for=\"search_by_phone\">by telephone</label>\n";
208 echo "<input type=\"submit\" value=\"Search\">\n";
212 function show_contact_forms($city_id) {
213 form("noprint standout");
214 show_contact_areas_form($city_id);
215 show_contact_cities_form($city_id);
216 show_contact_search_form();
220 function show_contact_role_form($role) {
221 return show_role_form($role, $GLOBALS['contact_roles']);
224 function show_contact_form($contact = null, $new = false) {
225 global $contact_roles, $parcel_sizes, $parcel_contents;
227 if (! $contact) $contact = new Contact;
228 else if ($contact->getRole() & ($GLOBALS['ROLE_BENEFICIARY'] | $GLOBALS['ROLE_REQUESTER'])) {
229 $state_mask = $GLOBALS['STATE_ANY'];
230 $state_mask &= ~$GLOBALS['STATE_DELIVERED'];
231 $state_mask &= ~$GLOBALS['STATE_CANCELLED'];
233 $orders = get_contact_orders($contact, $state_mask);
235 if (count($orders)) {
237 echo " <td colspan=2><strong>Outstanding orders:</strong></td>\n";
241 echo " <td colspan=2 class=\"history\">\n";
242 foreach ($orders as $order) {
243 echo " " . get_order_summary($order) . "<br>\n";
252 echo " <td>Role</td>\n";
253 echo " <td>"; show_contact_role_form($contact->getRole()); echo "</td>\n";
259 echo " <td>Registered</td>\n";
260 echo " <td>" . $contact->getAdded() . "</td>\n";
266 echo " <td>Forename</td>\n";
267 echo " <td>"; input("forename", $contact->getForename()); echo "</td>\n";
272 echo " <td>Middle name(s)</td>\n";
273 echo " <td>"; input("middle", $contact->getMiddle()); echo "</td>\n";
278 echo " <td>Surname</td>\n";
279 echo " <td>"; input("surname", $contact->getSurname()); echo "</td>\n";
284 echo " <td>Display name (if not concatenation of above)</td>\n";
285 echo " <td>"; input("displayname", $contact->getDisplayname()); echo "</td>\n";
289 $address = get_contact_address($contact);
290 if (! $address) $address = new Address;
292 echo " <td>Address</td>\n";
293 echo " <td>"; textarea("address", $address->getLine()); echo "</td>\n";
298 echo " <td>Postcode</td>\n";
299 $postcode = $address->getPostcode();
300 if (validate_postcode($postcode)) {
301 echo " <td>"; input("postcode", $postcode); echo get_address_map_link($address); echo "</td>\n";
304 echo " <td>"; input("postcode", $address->getPostcode()); echo " (invalid)</td>\n";
310 echo " <td>Telephone</td>\n";
311 echo " <td>"; input("telephone1", $contact->getTelephone1()); echo "</td>\n";
314 echo " <td>Alternative telephone</td>\n";
315 echo " <td>"; input("telephone2", $contact->getTelephone2()); echo "</td>\n";
320 echo " <td>Email</td>\n";
321 echo " <td>"; input("email", $contact->getEmail()); echo "</td>\n";
325 $area = get_contact_area($contact);
326 if ($area) $area_id = $area->getId();
328 echo " <td>Area</td>\n";
329 echo " <td><select name=\"area_id\">\n";
330 $areas = get_city_areas();
331 foreach ($areas as $area) {
332 option("area_id", $area->getId(), get_area_displayname($area), $area_id);
334 echo " </select></td>\n";
339 echo " <td>Family unit</td>\n";
340 echo " <td><select name=\"parcel_size\">\n";
341 $mask = 1 << count($parcel_sizes);
342 for ($i = 0; $i < count($parcel_sizes); $i++) {
343 option("parcel_size", 1 << $i, $parcel_sizes[$i], $contact->getParcel() % $mask);
345 echo "</select></td>\n";
348 /* Parcel contents. */
350 echo " <td>Dietary requirements</td>\n";
352 for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
353 if (1 << $i == $GLOBALS['PARCEL_TOILETRY']) continue;
354 echo " <input type=\"checkbox\" id=\"parcel_$i\" name=\"parcel_$i\"";
355 if ($contact->getParcel() & (1 << $i)) echo " checked";
356 echo "><label for=\"parcel_$i\">$parcel_contents[$i]</label>\n";
363 echo " <td>Notes</td>\n";
364 echo " <td>"; textarea("notes", $contact->getNotes()); echo "</td>\n";
368 function show_new_contact_form($city_id = null) {
369 if (! check_admin(1)) return;
371 $areas = get_city_areas($city_id);
372 if (! count($areas)) {
373 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
378 echo "<p>Add a new contact:</p>\n";
381 show_contact_form($contact, true);
384 echo " <td colspan=2>"; submit("add_contact", "Add"); echo "</td></tr>\n";
390 function show_add_new_contact_form() {
391 if (! check_admin(1)) return;
394 $cities = $q->find();
395 if (! count($cities)) {
396 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
400 form("noprint standout");
401 echo "<p>Add a new contact in <select name=\"city_id\">\n";
402 foreach ($cities as $city) {
403 option("city_id", $city->getId(), get_city_displayname($city));
406 submit("show_add_contact", "Proceed");
411 function update_contact(&$contact, $area_id, $new = false) {
412 global $contact_roles, $parcel_sizes, $parcel_contents;
415 for ($i = 0; $i < count($contact_roles); $i++) {
416 if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
419 /* Staff can place orders. */
420 if ($role & (1 << 0)) $role |= (1 << 2);
422 $forename = $_POST['forename'];
423 $middle = $_POST['middle'];
424 $surname = $_POST['surname'];
425 $displayname = $_POST['displayname'];
427 if (! $forename && ! $surname) {
428 echo "<p>Must have either a forename or surname!</p>\n";
431 if ($middle && ! ($forename && $surname)) {
432 echo "<p>Must have both a forename or surname for middle name(s) to make sense!</p>\n";
436 if (! $displayname) {
437 $displayname = $forename;
438 if ($middle) $displayname .= " $middle";
439 if ($forename) $displayname .= " ";
440 $displayname .= $surname;
441 echo "<p>Display name will be $displayname.</p>\n";
445 $area_id = $_POST['area_id'];
446 $line = $_POST['address'];
447 $postcode = trim($_POST['postcode']);
449 $postcode = format_postcode($_POST['postcode'], true);
450 if (! $postcode) return false;
452 $q = new AddressQuery;
453 /* XXX: Finding by area properly? */
454 $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
455 if ($address->isNew()) {
456 /* Changing address. */
459 XXX: Check for other contacts at the old address.
460 Make this a new address if there are others, but
461 provide a link to update other contacts.
466 catch (Exception $e) {
467 echo "<p>Error adding $line.</p>\n";
472 $telephone1 = $_POST['telephone1'];
473 $telephone2 = $_POST['telephone2'];
474 $email = $_POST['email'];
475 $parcel = $_POST['parcel_size'];
476 for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
477 if ($_POST['parcel_' . $i] == "on") $parcel |= (1 << $i);
479 $notes = $_POST['notes'];
481 $contact->setRole($role);
482 $contact->setForename($forename);
483 $contact->setMiddle($middle);
484 $contact->setSurname($surname);
485 $contact->setDisplayname($displayname);
486 $contact->setTelephone1($telephone1);
487 $contact->setTelephone2($telephone2);
488 $contact->setEmail($email);
489 $contact->setParcel($parcel);
490 $contact->setNotes($notes);
491 $contact->setAddressId($address->getId());
496 catch (Exception $e) {
497 if ($new) echo "<p>Error adding $displayname.</p>\n";
498 else echo "<p>Error updating $displayname.</p>\n";
505 function add_contact(&$name) {
506 if (! check_admin(1, "add a contact")) return;
508 $area_id = $_POST['area_id'];
509 if (! is_numeric($area_id)) {
510 echo "<p>Invalid area!</p>\n";
514 $area = get_area_by_id($area_id);
516 echo "<p>No such area!</p>\n";
520 $contact = new Contact;
521 if (! update_contact($contact, $area_id, true)) return false;
522 $name = $contact->getDisplayname();
523 return $contact->getId();
526 function confirm_delete_contact($name, $id = null, &$city_id = null) {
527 if (! check_admin(1, "delete a contact")) return;
529 if (isset($id)) $contact = get_contact_by_id($id);
530 else $contact = get_contact_by_name($name);
531 if (! $contact) return false;
533 echo "<h3>Confirm deletion</h3>\n";
534 echo "<p>You must confirm deletion of contact " . $contact->getDisplayname() . ": " . $contact->getDeleteLink(true) . "</p>\n";
537 function delete_contact($name, $id = null, &$city_id = null) {
538 if (! check_admin(1, "delete a contact")) return;
540 if (isset($id)) $contact = get_contact_by_id($id);
541 else $contact = get_contact_by_name($name);
542 if (! $contact) return false;
544 ///* Remember city ID for dropdown. */
545 //$city_id = $area->getCityId();
549 echo "<p>Deleted contact.</p>\n";
551 catch (Exception $e) {
552 echo "<p>Error deleting $name!</p>\n";
559 function show_contact($name, &$id = null) {
560 if (isset($id)) $contact = get_contact_by_id($id);
561 else $contact = get_contact_by_name($name);
562 if (! $contact) return;
565 show_contact_summary($contact, true);
570 show_contact_form($contact);
572 if (check_admin(1)) {
574 echo " <td colspan=2>";
575 submit("update_contact", "Update");
584 /* /contact/in/area/Cambridge/1 */
585 if (count($parameters)) {
586 if ($parameters[0] == "in") {
587 switch ($parameters[1]) {
589 $area_id = $parameters[3];
590 $_POST['area_id'] = $area_id;
592 $area = $q->findOneById($area_id);
593 $city = get_area_city($area);
594 if ($city) $city_id = $city->getId();
595 show_area_contacts($parameters[2], $area_id);
599 $city_id = $parameters[3];
600 $_POST['city_id'] = $city_id;
602 $city = $q->findOneById($city_id);
603 show_city_contacts($parameters[2], $city_id);
607 else if ($parameters[0] == "search") {
608 if ($parameters[1] == "phone") search_contacts(urldecode($parameters[2]), true);
609 else search_contacts(urldecode($parameters[1]));
612 list($name, $id, $args) = parse_parameters($parameters);
613 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
617 confirm_delete_contact($name, $id);
620 case "confirmdelete":
621 delete_contact($name, $id);
625 else if (isset($name)) show_contact($name, $id);
627 show_contact_forms($city_id);
628 show_add_new_contact_form($city_id);