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, $offers;
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 $available_offers = get_available_offers($contact);
377 if (count($available_offers)) {
379 echo " <td colspan=2><strong>Offers</strong></td>\n";
382 $offer_states = Array();
383 foreach ($available_offers as $offer) {
384 $offer_id = $offer->getId();
385 $state = get_offer_state($contact, $offer);
386 $state_id = is_null($state) ? $GLOBALS['OFFER_ELIGIBLE'] : $state->getState();
387 $offer_states[$offer_id] = $state_id;
389 echo " <td>" . $offer->getDescription() . "</td>\n";
390 echo " <td><select name=\"offer$offer_id\">\n";
391 for ($i = 0; $i < count($offers); $i++) {
392 option("offer$offer_id", 1 << $i, $offers[$i], $state_id == 1 << $i);
394 echo "</select></td>\n";
397 hidden("offer_states", urlencode(json_encode($offer_states)));
401 echo " <td colspan=2><strong>No available offers</strong></td>\n";
403 hidden("offer_states", "{}");
407 function show_new_contact_form($city_id = null) {
408 if (! check_admin(1)) return;
410 $areas = get_city_areas($city_id);
411 if (! count($areas)) {
412 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
417 echo "<p>Add a new contact:</p>\n";
420 show_contact_form($contact, true);
423 echo " <td colspan=2>"; submit("add_contact", "Add"); echo "</td></tr>\n";
429 function show_add_new_contact_form() {
430 if (! check_admin(1)) return;
433 $cities = $q->find();
434 if (! count($cities)) {
435 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
439 form("noprint standout");
440 echo "<p>Add a new contact in <select name=\"city_id\">\n";
441 foreach ($cities as $city) {
442 option("city_id", $city->getId(), get_city_displayname($city));
445 submit("show_add_contact", "Proceed");
450 function update_contact(&$contact, $area_id, $new = false) {
451 global $contact_roles, $parcel_sizes, $parcel_contents;
454 for ($i = 0; $i < count($contact_roles); $i++) {
455 if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
458 /* Staff can place orders. */
459 if ($role & (1 << 0)) $role |= (1 << 2);
461 $forename = $_POST['forename'];
462 $middle = $_POST['middle'];
463 $surname = $_POST['surname'];
464 $displayname = $_POST['displayname'];
466 if (! $forename && ! $surname) {
467 echo "<p>Must have either a forename or surname!</p>\n";
470 if ($middle && ! ($forename && $surname)) {
471 echo "<p>Must have both a forename or surname for middle name(s) to make sense!</p>\n";
475 if (! $displayname) {
476 $displayname = $forename;
477 if ($middle) $displayname .= " $middle";
478 if ($forename) $displayname .= " ";
479 $displayname .= $surname;
480 echo "<p>Display name will be $displayname.</p>\n";
484 $area_id = $_POST['area_id'];
485 $line = $_POST['address'];
486 $postcode = trim($_POST['postcode']);
488 $postcode = format_postcode($_POST['postcode'], true);
489 if (! $postcode) return false;
491 $q = new AddressQuery;
492 /* XXX: Finding by area properly? */
493 $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
494 if ($address->isNew()) {
495 /* Changing address. */
498 XXX: Check for other contacts at the old address.
499 Make this a new address if there are others, but
500 provide a link to update other contacts.
505 catch (Exception $e) {
506 echo "<p>Error adding $line.</p>\n";
511 $telephone1 = $_POST['telephone1'];
512 $telephone2 = $_POST['telephone2'];
513 $email = $_POST['email'];
514 $parcel = $_POST['parcel_size'];
515 for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
516 if ($_POST['parcel_' . $i] == "on") $parcel |= (1 << $i);
518 $notes = $_POST['notes'];
520 $contact->setRole($role);
521 $contact->setForename($forename);
522 $contact->setMiddle($middle);
523 $contact->setSurname($surname);
524 $contact->setDisplayname($displayname);
525 $contact->setTelephone1($telephone1);
526 $contact->setTelephone2($telephone2);
527 $contact->setEmail($email);
528 $contact->setParcel($parcel);
529 $contact->setNotes($notes);
530 $contact->setAddressId($address->getId());
535 catch (Exception $e) {
536 if ($new) echo "<p>Error adding $displayname.</p>\n";
537 else echo "<p>Error updating $displayname.</p>\n";
541 if ($_POST['offer_states']) {
543 $offer_states = json_decode(urldecode($_POST['offer_states']));
545 catch (Exception $e) {
546 echo "<p>Error getting offer states JSON.</p>\n";
551 foreach ($offer_states as $offer_id => $state_id) {
552 $new_state_id = strval($_POST["offer$offer_id"]);
553 if ($new_state_id == "") continue;
554 if ($new_state_id == strval($state_id)) continue;
556 $offer_state = new OfferState;
557 $offer_state->setUpdated($now);
558 $offer_state->setOfferId($offer_id);
559 $offer_state->setUserId($GLOBALS['user_id']);
560 $offer_state->setContactId($contact->getId());
561 $offer_state->setState($new_state_id);
564 $offer_state->save();
566 catch (Exception $e) {
567 echo "<p>Error updating offer.</p>\n";
568 echo "<p>" . $e->getMessage() . "</p>\n";
577 function add_contact(&$name) {
578 if (! check_admin(1, "add a contact")) return;
580 $area_id = $_POST['area_id'];
581 if (! is_numeric($area_id)) {
582 echo "<p>Invalid area!</p>\n";
586 $area = get_area_by_id($area_id);
588 echo "<p>No such area!</p>\n";
592 $contact = new Contact;
593 if (! update_contact($contact, $area_id, true)) return false;
594 $name = $contact->getDisplayname();
595 return $contact->getId();
598 function confirm_delete_contact($name, $id = null, &$city_id = null) {
599 if (! check_admin(1, "delete a contact")) return;
601 if (isset($id)) $contact = get_contact_by_id($id);
602 else $contact = get_contact_by_name($name);
603 if (! $contact) return false;
605 echo "<h3>Confirm deletion</h3>\n";
606 echo "<p>You must confirm deletion of contact " . $contact->getDisplayname() . ": " . $contact->getDeleteLink(true) . "</p>\n";
609 function delete_contact($name, $id = null, &$city_id = null) {
610 if (! check_admin(1, "delete a contact")) return;
612 if (isset($id)) $contact = get_contact_by_id($id);
613 else $contact = get_contact_by_name($name);
614 if (! $contact) return false;
616 ///* Remember city ID for dropdown. */
617 //$city_id = $area->getCityId();
621 echo "<p>Deleted contact.</p>\n";
623 catch (Exception $e) {
624 echo "<p>Error deleting $name!</p>\n";
631 function show_contact($name, &$id = null) {
632 if (isset($id)) $contact = get_contact_by_id($id);
633 else $contact = get_contact_by_name($name);
634 if (! $contact) return;
637 show_contact_summary($contact, true);
642 show_contact_form($contact);
644 if (check_admin(1)) {
646 echo " <td colspan=2>";
647 submit("update_contact", "Update");
656 /* /contact/in/area/Cambridge/1 */
657 if (count($parameters)) {
658 if ($parameters[0] == "in") {
659 switch ($parameters[1]) {
661 $area_id = $parameters[3];
662 $_POST['area_id'] = $area_id;
664 $area = $q->findOneById($area_id);
665 $city = get_area_city($area);
666 if ($city) $city_id = $city->getId();
667 show_area_contacts($parameters[2], $area_id);
671 $city_id = $parameters[3];
672 $_POST['city_id'] = $city_id;
674 $city = $q->findOneById($city_id);
675 show_city_contacts($parameters[2], $city_id);
679 else if ($parameters[0] == "search") {
680 $available_search_options = array('notes', 'phone');
681 $search_options = explode('+', $parameters[1]);
682 if (count(array_intersect($available_search_options, $search_options)) == count($search_options)) {
683 $notes = in_array("notes", $search_options);
684 $phone = in_array("phone", $search_options);
685 search_contacts(urldecode($parameters[2]), $notes, $phone);
687 else search_contacts(urldecode($parameters[1]));
690 list($name, $id, $args) = parse_parameters($parameters);
691 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
695 confirm_delete_contact($name, $id);
698 case "confirmdelete":
699 delete_contact($name, $id);
703 else if (isset($name)) show_contact($name, $id);
705 show_contact_forms($city_id);
706 show_add_new_contact_form($city_id);