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 $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'])));
41 else if ($_POST['show_in_area']) {
42 set_last_selected("area_id", $_POST['area_id']);
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']));
48 else if ($_POST['show_in_city']) {
49 set_last_selected("city_id", $_POST['city_id']);
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']));
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);
71 echo " " . $contact->getDeleteLink();
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));
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);
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);
105 $address_ids = array();
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();
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);
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);
131 $areas = $q->filterByCityId($city->getId())->find();
133 foreach ($areas as $area) $area_ids[] = $area->getId();
135 $q = new AddressQuery;
136 $addresses = $q->filterByAreaId($area_ids)->find();
137 $address_ids = array();
138 foreach ($addresses as $address) $address_ids[] = $address->getId();
140 echo "<p>Contacts in city " . $city->getLink(get_city_displayname($city)) . ":";
141 return show_contacts($address_ids);
143 else echo "<p>No such city!</p>\n";
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);
150 $q = new AddressQuery;
151 $addresses = $q->filterByAreaId($area->getId())->find();
152 $address_ids = array();
153 foreach ($addresses as $address) $address_ids[] = $address->getId();
155 echo "<p>Contacts in area " . $area->getLink() . ":";
156 return show_contacts($address_ids);
158 else echo "<p>No such area!</p>\n";
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";
168 $candidates = array();
169 foreach ($areas as $area) {
170 if (! count(get_area_contacts($area->getId()))) continue;
171 $candidates[] = $area;
173 if (! count($candidates)) return;
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));
181 submit("show_in_area", "Show");
184 function show_contact_cities_form($city_id = null) {
186 $cities = $q->orderByName()->find();
188 if (! count($cities)) {
189 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
193 $candidates = array();
194 foreach ($cities as $city) {
195 if (! count(get_city_contacts($city->getId()))) continue;
196 $candidates[] = $city;
198 if (! count($candidates)) return;
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);
206 submit("show_in_city", "Show");
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";
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();
228 function show_contact_role_form($role) {
229 return show_role_form($role, $GLOBALS['contact_roles']);
232 function show_contact_form($contact = null, $new = false) {
233 global $contact_roles, $parcel_sizes, $parcel_contents;
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'];
241 $orders = get_contact_orders($contact, $state_mask);
243 if (count($orders)) {
245 echo " <td colspan=2><strong>Outstanding orders:</strong></td>\n";
249 echo " <td colspan=2 class=\"history\">\n";
250 foreach ($orders as $order) {
251 echo " " . get_order_summary($order) . "<br>\n";
260 echo " <td>Role</td>\n";
261 echo " <td>"; show_contact_role_form($contact->getRole()); echo "</td>\n";
267 echo " <td>Registered</td>\n";
268 echo " <td>" . $contact->getAdded() . "</td>\n";
274 echo " <td>Forename</td>\n";
275 echo " <td>"; input("forename", $contact->getForename()); echo "</td>\n";
280 echo " <td>Middle name(s)</td>\n";
281 echo " <td>"; input("middle", $contact->getMiddle()); echo "</td>\n";
286 echo " <td>Surname</td>\n";
287 echo " <td>"; input("surname", $contact->getSurname()); echo "</td>\n";
292 echo " <td>Display name (if not concatenation of above)</td>\n";
293 echo " <td>"; input("displayname", $contact->getDisplayname()); echo "</td>\n";
297 $address = get_contact_address($contact);
298 if (! $address) $address = new Address;
300 echo " <td>Address</td>\n";
301 echo " <td>"; textarea("address", $address->getLine()); echo "</td>\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";
312 echo " <td>"; input("postcode", $address->getPostcode()); echo " (invalid)</td>\n";
318 echo " <td>Telephone</td>\n";
319 echo " <td>"; input("telephone1", $contact->getTelephone1()); echo "</td>\n";
322 echo " <td>Alternative telephone</td>\n";
323 echo " <td>"; input("telephone2", $contact->getTelephone2()); echo "</td>\n";
328 echo " <td>Email</td>\n";
329 echo " <td>"; input("email", $contact->getEmail()); echo "</td>\n";
333 $area = get_contact_area($contact);
334 if ($area) $area_id = $area->getId();
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);
342 echo " </select></td>\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);
353 echo "</select></td>\n";
356 /* Parcel contents. */
358 echo " <td>Dietary requirements</td>\n";
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";
371 echo " <td>Notes</td>\n";
372 echo " <td>"; textarea("notes", $contact->getNotes()); echo "</td>\n";
376 function show_new_contact_form($city_id = null) {
377 if (! check_admin(1)) return;
379 $areas = get_city_areas($city_id);
380 if (! count($areas)) {
381 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
386 echo "<p>Add a new contact:</p>\n";
389 show_contact_form($contact, true);
392 echo " <td colspan=2>"; submit("add_contact", "Add"); echo "</td></tr>\n";
398 function show_add_new_contact_form() {
399 if (! check_admin(1)) return;
402 $cities = $q->find();
403 if (! count($cities)) {
404 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
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));
414 submit("show_add_contact", "Proceed");
419 function update_contact(&$contact, $area_id, $new = false) {
420 global $contact_roles, $parcel_sizes, $parcel_contents;
423 for ($i = 0; $i < count($contact_roles); $i++) {
424 if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
427 /* Staff can place orders. */
428 if ($role & (1 << 0)) $role |= (1 << 2);
430 $forename = $_POST['forename'];
431 $middle = $_POST['middle'];
432 $surname = $_POST['surname'];
433 $displayname = $_POST['displayname'];
435 if (! $forename && ! $surname) {
436 echo "<p>Must have either a forename or surname!</p>\n";
439 if ($middle && ! ($forename && $surname)) {
440 echo "<p>Must have both a forename or surname for middle name(s) to make sense!</p>\n";
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";
453 $area_id = $_POST['area_id'];
454 $line = $_POST['address'];
455 $postcode = trim($_POST['postcode']);
457 $postcode = format_postcode($_POST['postcode'], true);
458 if (! $postcode) return false;
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. */
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.
474 catch (Exception $e) {
475 echo "<p>Error adding $line.</p>\n";
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);
487 $notes = $_POST['notes'];
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());
504 catch (Exception $e) {
505 if ($new) echo "<p>Error adding $displayname.</p>\n";
506 else echo "<p>Error updating $displayname.</p>\n";
513 function add_contact(&$name) {
514 if (! check_admin(1, "add a contact")) return;
516 $area_id = $_POST['area_id'];
517 if (! is_numeric($area_id)) {
518 echo "<p>Invalid area!</p>\n";
522 $area = get_area_by_id($area_id);
524 echo "<p>No such area!</p>\n";
528 $contact = new Contact;
529 if (! update_contact($contact, $area_id, true)) return false;
530 $name = $contact->getDisplayname();
531 return $contact->getId();
534 function confirm_delete_contact($name, $id = null, &$city_id = null) {
535 if (! check_admin(1, "delete a contact")) return;
537 if (isset($id)) $contact = get_contact_by_id($id);
538 else $contact = get_contact_by_name($name);
539 if (! $contact) return false;
541 echo "<h3>Confirm deletion</h3>\n";
542 echo "<p>You must confirm deletion of contact " . $contact->getDisplayname() . ": " . $contact->getDeleteLink(true) . "</p>\n";
545 function delete_contact($name, $id = null, &$city_id = null) {
546 if (! check_admin(1, "delete a contact")) return;
548 if (isset($id)) $contact = get_contact_by_id($id);
549 else $contact = get_contact_by_name($name);
550 if (! $contact) return false;
552 ///* Remember city ID for dropdown. */
553 //$city_id = $area->getCityId();
557 echo "<p>Deleted contact.</p>\n";
559 catch (Exception $e) {
560 echo "<p>Error deleting $name!</p>\n";
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;
573 show_contact_summary($contact, true);
578 show_contact_form($contact);
580 if (check_admin(1)) {
582 echo " <td colspan=2>";
583 submit("update_contact", "Update");
592 /* /contact/in/area/Cambridge/1 */
593 if (count($parameters)) {
594 if ($parameters[0] == "in") {
595 switch ($parameters[1]) {
597 $area_id = $parameters[3];
598 $_POST['area_id'] = $area_id;
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);
607 $city_id = $parameters[3];
608 $_POST['city_id'] = $city_id;
610 $city = $q->findOneById($city_id);
611 show_city_contacts($parameters[2], $city_id);
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);
623 else search_contacts(urldecode($parameters[1]));
626 list($name, $id, $args) = parse_parameters($parameters);
627 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
631 confirm_delete_contact($name, $id);
634 case "confirmdelete":
635 delete_contact($name, $id);
639 else if (isset($name)) show_contact($name, $id);
641 show_contact_forms($city_id);
642 show_add_new_contact_form($city_id);