3 if (isset($_POST['show_add_contact'])) {
4 $city_id = $_POST['city_id'];
5 show_new_contact_form($city_id);
7 else if (isset($_POST['add_contact'])) {
8 $id = add_contact($displayname);
10 echo "<p>Added contact.</p>\n";
11 $parameters = array($displayname, $id);
14 else if (isset($_POST['update_contact'])) {
15 list($name, $id, $args) = parse_parameters($parameters);
16 $q = new ContactQuery;
17 $contact = $q->findOneById($id);
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());
27 echo "<p>No such contact!</p>\n";
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'])));
34 else if ($_POST['show_in_area']) {
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']));
40 else if ($_POST['show_in_city']) {
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']));
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);
62 echo " " . $contact->getDeleteLink();
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));
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);
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);
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();
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);
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);
112 $areas = $q->filterByCityId($city->getId())->find();
114 foreach ($areas as $area) $area_ids[] = $area->getId();
116 $q = new AddressQuery;
117 $addresses = $q->filterByAreaId($area_ids)->find();
118 $address_ids = array();
119 foreach ($addresses as $address) $address_ids[] = $address->getId();
121 echo "<p>Contacts in city " . $city->getLink(get_city_displayname($city)) . ":";
122 return show_contacts($offset, $per_page, $address_ids);
124 else echo "<p>No such city!</p>\n";
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);
131 $q = new AddressQuery;
132 $addresses = $q->filterByAreaId($area->getId())->find();
133 $address_ids = array();
134 foreach ($addresses as $address) $address_ids[] = $address->getId();
136 echo "<p>Contacts in area " . $area->getLink() . ":";
137 return show_contacts($offset, $per_page, $address_ids);
139 else echo "<p>No such area!</p>\n";
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";
149 $candidates = array();
150 foreach ($areas as $area) {
151 if (! count(get_area_contacts($area->getId()))) continue;
152 $candidates[] = $area;
154 if (! count($candidates)) return;
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));
162 submit("show_in_area", "Show");
165 function show_contact_cities_form($city_id = null) {
167 $cities = $q->orderByName()->find();
169 if (! count($cities)) {
170 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
174 $candidates = array();
175 foreach ($cities as $city) {
176 if (! count(get_city_contacts($city->getId()))) continue;
177 $candidates[] = $city;
179 if (! count($candidates)) return;
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);
187 submit("show_in_city", "Show");
190 function show_contact_search_form() {
191 echo "<p>Search for contacts:";
192 input("search_contact");
193 echo "<input type=\"submit\" value=\"Search\">\n";
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();
204 function show_contact_role_form($role) {
205 return show_role_form($role, $GLOBALS['contact_roles']);
208 function show_contact_form($contact = null, $new = false) {
209 global $contact_roles, $parcel_sizes, $parcel_contents;
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'];
217 $orders = get_contact_orders($contact, $state_mask);
219 if (count($orders)) {
221 echo " <td colspan=2><strong>Outstanding orders:</strong></td>\n";
225 echo " <td colspan=2 class=\"history\">\n";
226 foreach ($orders as $order) {
227 echo " " . get_order_summary($order) . "<br>\n";
236 echo " <td>Role</td>\n";
237 echo " <td>"; show_contact_role_form($contact->getRole()); echo "</td>\n";
243 echo " <td>Registered</td>\n";
244 echo " <td>" . $contact->getAdded() . "</td>\n";
250 echo " <td>Forename</td>\n";
251 echo " <td>"; input("forename", $contact->getForename()); echo "</td>\n";
256 echo " <td>Middle name(s)</td>\n";
257 echo " <td>"; input("middle", $contact->getMiddle()); echo "</td>\n";
262 echo " <td>Surname</td>\n";
263 echo " <td>"; input("surname", $contact->getSurname()); echo "</td>\n";
268 echo " <td>Display name (if not concatenation of above)</td>\n";
269 echo " <td>"; input("displayname", $contact->getDisplayname()); echo "</td>\n";
273 $address = get_contact_address($contact);
274 if (! $address) $address = new Address;
276 echo " <td>Address</td>\n";
277 echo " <td>"; textarea("address", $address->getLine()); echo "</td>\n";
282 echo " <td>Postcode</td>\n";
283 $postcode = $address->getPostcode();
284 if (validate_postcode($postcode)) {
285 echo " <td>"; input("postcode", $postcode); echo get_address_map_link($address); echo "</td>\n";
288 echo " <td>"; input("postcode", $address->getPostcode()); echo " (invalid)</td>\n";
294 echo " <td>Telephone</td>\n";
295 echo " <td>"; input("telephone1", $contact->getTelephone1()); echo "</td>\n";
298 echo " <td>Alternative telephone</td>\n";
299 echo " <td>"; input("telephone2", $contact->getTelephone2()); echo "</td>\n";
304 echo " <td>Email</td>\n";
305 echo " <td>"; input("email", $contact->getEmail()); echo "</td>\n";
309 $area = get_contact_area($contact);
310 if ($area) $area_id = $area->getId();
312 echo " <td>Area</td>\n";
313 echo " <td><select name=\"area_id\">\n";
314 $areas = get_city_areas();
315 foreach ($areas as $area) {
316 option("area_id", $area->getId(), get_area_displayname($area), $area_id);
318 echo " </select></td>\n";
323 echo " <td>Family unit</td>\n";
324 echo " <td><select name=\"parcel_size\">\n";
325 $mask = 1 << count($parcel_sizes);
326 for ($i = 0; $i < count($parcel_sizes); $i++) {
327 option("parcel_size", 1 << $i, $parcel_sizes[$i], $contact->getParcel() % $mask);
329 echo "</select></td>\n";
332 /* Parcel contents. */
334 echo " <td>Dietary requirements</td>\n";
336 for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
337 if (1 << $i == $GLOBALS['PARCEL_TOILETRY']) continue;
338 echo " <input type=\"checkbox\" id=\"parcel_$i\" name=\"parcel_$i\"";
339 if ($contact->getParcel() & (1 << $i)) echo " checked";
340 echo "><label for=\"parcel_$i\">$parcel_contents[$i]</label>\n";
347 echo " <td>Notes</td>\n";
348 echo " <td>"; textarea("notes", $contact->getNotes()); echo "</td>\n";
352 function show_new_contact_form($city_id = null) {
353 if (! check_admin(1)) return;
355 $areas = get_city_areas($city_id);
356 if (! count($areas)) {
357 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
362 echo "<p>Add a new contact:</p>\n";
365 show_contact_form($contact, true);
368 echo " <td colspan=2>"; submit("add_contact", "Add"); echo "</td></tr>\n";
374 function show_add_new_contact_form() {
375 if (! check_admin(1)) return;
378 $cities = $q->find();
379 if (! count($cities)) {
380 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
384 form("noprint standout");
385 echo "<p>Add a new contact in <select name=\"city_id\">\n";
386 foreach ($cities as $city) {
387 option("city_id", $city->getId(), get_city_displayname($city));
390 submit("show_add_contact", "Proceed");
395 function update_contact(&$contact, $area_id, $new = false) {
396 global $contact_roles, $parcel_sizes, $parcel_contents;
399 for ($i = 0; $i < count($contact_roles); $i++) {
400 if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
403 /* Staff can place orders. */
404 if ($role & (1 << 0)) $role |= (1 << 2);
406 $forename = $_POST['forename'];
407 $middle = $_POST['middle'];
408 $surname = $_POST['surname'];
409 $displayname = $_POST['displayname'];
411 if (! $forename && ! $surname) {
412 echo "<p>Must have either a forename or surname!</p>\n";
415 if ($middle && ! ($forename && $surname)) {
416 echo "<p>Must have both a forename or surname for middle name(s) to make sense!</p>\n";
420 if (! $displayname) {
421 $displayname = $forename;
422 if ($middle) $displayname .= " $middle";
423 if ($forename) $displayname .= " ";
424 $displayname .= $surname;
425 echo "<p>Display name will be $displayname.</p>\n";
429 $area_id = $_POST['area_id'];
430 $line = $_POST['address'];
431 $postcode = trim($_POST['postcode']);
433 $postcode = format_postcode($_POST['postcode'], true);
434 if (! $postcode) return false;
436 $q = new AddressQuery;
437 /* XXX: Finding by area properly? */
438 $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
439 if ($address->isNew()) {
440 /* Changing address. */
443 XXX: Check for other contacts at the old address.
444 Make this a new address if there are others, but
445 provide a link to update other contacts.
450 catch (Exception $e) {
451 echo "<p>Error adding $line.</p>\n";
456 $telephone1 = $_POST['telephone1'];
457 $telephone2 = $_POST['telephone2'];
458 $email = $_POST['email'];
459 $parcel = $_POST['parcel_size'];
460 for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
461 if ($_POST['parcel_' . $i] == "on") $parcel |= (1 << $i);
463 $notes = $_POST['notes'];
465 $contact->setRole($role);
466 $contact->setForename($forename);
467 $contact->setMiddle($middle);
468 $contact->setSurname($surname);
469 $contact->setDisplayname($displayname);
470 $contact->setTelephone1($telephone1);
471 $contact->setTelephone2($telephone2);
472 $contact->setEmail($email);
473 $contact->setParcel($parcel);
474 $contact->setNotes($notes);
475 $contact->setAddressId($address->getId());
480 catch (Exception $e) {
481 if ($new) echo "<p>Error adding $displayname.</p>\n";
482 else echo "<p>Error updating $displayname.</p>\n";
489 function add_contact(&$name) {
490 if (! check_admin(1, "add a contact")) return;
492 $area_id = $_POST['area_id'];
493 if (! is_numeric($area_id)) {
494 echo "<p>Invalid area!</p>\n";
498 $area = get_area_by_id($area_id);
500 echo "<p>No such area!</p>\n";
504 $contact = new Contact;
505 if (! update_contact($contact, $area_id, true)) return false;
506 $name = $contact->getDisplayname();
507 return $contact->getId();
510 function confirm_delete_contact($name, $id = null, &$city_id = null) {
511 if (! check_admin(1, "delete a contact")) return;
513 if (isset($id)) $contact = get_contact_by_id($id);
514 else $contact = get_contact_by_name($name);
515 if (! $contact) return false;
517 echo "<h3>Confirm deletion</h3>\n";
518 echo "<p>You must confirm deletion of contact " . $contact->getDisplayname() . ": " . $contact->getDeleteLink(true) . "</p>\n";
521 function delete_contact($name, $id = null, &$city_id = null) {
522 if (! check_admin(1, "delete a contact")) return;
524 if (isset($id)) $contact = get_contact_by_id($id);
525 else $contact = get_contact_by_name($name);
526 if (! $contact) return false;
528 ///* Remember city ID for dropdown. */
529 //$city_id = $area->getCityId();
533 echo "<p>Deleted contact.</p>\n";
535 catch (Exception $e) {
536 echo "<p>Error deleting $name!</p>\n";
543 function show_contact($name, &$id = null) {
544 if (isset($id)) $contact = get_contact_by_id($id);
545 else $contact = get_contact_by_name($name);
546 if (! $contact) return;
549 show_contact_summary($contact, true);
554 show_contact_form($contact);
556 if (check_admin(1)) {
558 echo " <td colspan=2>";
559 submit("update_contact", "Update");
568 /* /contact/in/area/Cambridge/1 */
569 if (count($parameters)) {
570 if ($parameters[0] == "in") {
571 switch ($parameters[1]) {
573 $area_id = $parameters[3];
574 $_POST['area_id'] = $area_id;
576 $area = $q->findOneById($area_id);
577 $city = get_area_city($area);
578 if ($city) $city_id = $city->getId();
579 show_area_contacts(0, 10, $parameters[2], $area_id);
583 $city_id = $parameters[3];
584 $_POST['city_id'] = $city_id;
586 $city = $q->findOneById($city_id);
587 show_city_contacts(0, 10, $parameters[2], $city_id);
591 else if ($parameters[0] == "search") {
592 search_contacts(0, 10, urldecode($parameters[1]));
595 list($name, $id, $args) = parse_parameters($parameters);
596 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
600 confirm_delete_contact($name, $id);
603 case "confirmdelete":
604 delete_contact($name, $id);
608 else if (isset($name)) show_contact($name, $id);
610 show_contact_forms($city_id);
611 show_add_new_contact_form($city_id);