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 echo " <td>"; input("postcode", $address->getPostcode()); echo get_address_map_link($address); echo "</td>\n";
288 echo " <td>Telephone</td>\n";
289 echo " <td>"; input("telephone1", $contact->getTelephone1()); echo "</td>\n";
292 echo " <td>Alternative telephone</td>\n";
293 echo " <td>"; input("telephone2", $contact->getTelephone2()); echo "</td>\n";
298 echo " <td>Email</td>\n";
299 echo " <td>"; input("email", $contact->getEmail()); echo "</td>\n";
303 $area = get_contact_area($contact);
304 if ($area) $area_id = $area->getId();
306 echo " <td>Area</td>\n";
307 echo " <td><select name=\"area_id\">\n";
308 $areas = get_city_areas();
309 foreach ($areas as $area) {
310 option("area_id", $area->getId(), get_area_displayname($area), $area_id);
312 echo " </select></td>\n";
317 echo " <td>Family unit</td>\n";
318 echo " <td><select name=\"parcel_size\">\n";
319 $mask = 1 << count($parcel_sizes);
320 for ($i = 0; $i < count($parcel_sizes); $i++) {
321 option("parcel_size", 1 << $i, $parcel_sizes[$i], $contact->getParcel() % $mask);
323 echo "</select></td>\n";
326 /* Parcel contents. */
328 echo " <td>Dietary requirements</td>\n";
330 for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
331 if (1 << $i == $GLOBALS['PARCEL_TOILETRY']) continue;
332 echo " <input type=\"checkbox\" name=\"parcel_$i\"";
333 if ($contact->getParcel() & (1 << $i)) echo " checked";
334 echo ">$parcel_contents[$i]\n";
341 echo " <td>Notes</td>\n";
342 echo " <td>"; textarea("notes", $contact->getNotes()); echo "</td>\n";
346 function show_new_contact_form($city_id = null) {
347 if (! check_admin(1)) return;
349 $areas = get_city_areas($city_id);
350 if (! count($areas)) {
351 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
356 echo "<p>Add a new contact:</p>\n";
359 show_contact_form($contact, true);
362 echo " <td colspan=2>"; submit("add_contact", "Add"); echo "</td></tr>\n";
368 function show_add_new_contact_form() {
369 if (! check_admin(1)) return;
372 $cities = $q->find();
373 if (! count($cities)) {
374 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
378 form("noprint standout");
379 echo "<p>Add a new contact in <select name=\"city_id\">\n";
380 foreach ($cities as $city) {
381 option("city_id", $city->getId(), get_city_displayname($city));
384 submit("show_add_contact", "Proceed");
389 function update_contact(&$contact, $area_id, $new = false) {
390 global $contact_roles, $parcel_sizes, $parcel_contents;
393 for ($i = 0; $i < count($contact_roles); $i++) {
394 if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
397 /* Staff can place orders. */
398 if ($role & (1 << 0)) $role |= (1 << 2);
400 $forename = $_POST['forename'];
401 $middle = $_POST['middle'];
402 $surname = $_POST['surname'];
403 $displayname = $_POST['displayname'];
405 if (! $forename && ! $surname) {
406 echo "<p>Must have either a forename or surname!</p>\n";
409 if ($middle && ! ($forename && $surname)) {
410 echo "<p>Must have both a forename or surname for middle name(s) to make sense!</p>\n";
414 if (! $displayname) {
415 $displayname = $forename;
416 if ($middle) $displayname .= " $middle";
417 if ($forename) $displayname .= " ";
418 $displayname .= $surname;
419 echo "<p>Display name will be $displayname.</p>\n";
423 $area_id = $_POST['area_id'];
424 $line = $_POST['address'];
425 $postcode = $_POST['postcode'];
426 $q = new AddressQuery;
427 /* XXX: Finding by area properly? */
428 $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
429 if ($address->isNew()) {
430 /* Changing address. */
433 XXX: Check for other contacts at the old address.
434 Make this a new address if there are others, but
435 provide a link to update other contacts.
440 catch (Exception $e) {
441 echo "<p>Error adding $line.</p>\n";
446 $telephone1 = $_POST['telephone1'];
447 $telephone2 = $_POST['telephone2'];
448 $email = $_POST['email'];
449 $parcel = $_POST['parcel_size'];
450 for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
451 if ($_POST['parcel_' . $i] == "on") $parcel |= (1 << $i);
453 $notes = $_POST['notes'];
455 $contact->setRole($role);
456 $contact->setForename($forename);
457 $contact->setMiddle($middle);
458 $contact->setSurname($surname);
459 $contact->setDisplayname($displayname);
460 $contact->setTelephone1($telephone1);
461 $contact->setTelephone2($telephone2);
462 $contact->setEmail($email);
463 $contact->setParcel($parcel);
464 $contact->setNotes($notes);
465 $contact->setAddressId($address->getId());
470 catch (Exception $e) {
471 if ($new) echo "<p>Error adding $displayname.</p>\n";
472 else echo "<p>Error updating $displayname.</p>\n";
479 function add_contact(&$name) {
480 if (! check_admin(1, "add a contact")) return;
482 $area_id = $_POST['area_id'];
483 if (! is_numeric($area_id)) {
484 echo "<p>Invalid area!</p>\n";
488 $area = get_area_by_id($area_id);
490 echo "<p>No such area!</p>\n";
494 $contact = new Contact;
495 if (! update_contact($contact, $area_id, true)) return false;
496 return $contact->getId();
499 function confirm_delete_contact($name, $id = null, &$city_id = null) {
500 if (! check_admin(1, "delete a contact")) return;
502 if (isset($id)) $contact = get_contact_by_id($id);
503 else $contact = get_contact_by_name($name);
504 if (! $contact) return false;
506 echo "<h3>Confirm deletion</h3>\n";
507 echo "<p>You must confirm deletion of contact " . $contact->getDisplayname() . ": " . $contact->getDeleteLink(true) . "</p>\n";
510 function 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 ///* Remember city ID for dropdown. */
518 //$city_id = $area->getCityId();
522 echo "<p>Deleted contact.</p>\n";
524 catch (Exception $e) {
525 echo "<p>Error deleting $name!</p>\n";
532 function show_contact($name, &$id = null) {
533 if (isset($id)) $contact = get_contact_by_id($id);
534 else $contact = get_contact_by_name($name);
535 if (! $contact) return;
538 show_contact_summary($contact, true);
543 show_contact_form($contact);
545 if (check_admin(1)) {
547 echo " <td colspan=2>";
548 submit("update_contact", "Update");
557 /* /contact/in/area/Cambridge/1 */
558 if (count($parameters)) {
559 if ($parameters[0] == "in") {
560 switch ($parameters[1]) {
562 $area_id = $parameters[3];
563 $_POST['area_id'] = $area_id;
565 $area = $q->findOneById($area_id);
566 $city = get_area_city($area);
567 if ($city) $city_id = $city->getId();
568 show_area_contacts(0, 10, $parameters[2], $area_id);
572 $city_id = $parameters[3];
573 $_POST['city_id'] = $city_id;
575 $city = $q->findOneById($city_id);
576 show_city_contacts(0, 10, $parameters[2], $city_id);
580 else if ($parameters[0] == "search") {
581 search_contacts(0, 10, urldecode($parameters[1]));
584 list($name, $id, $args) = parse_parameters($parameters);
585 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
589 confirm_delete_contact($name, $id);
592 case "confirmdelete":
593 delete_contact($name, $id);
597 else if (isset($name)) show_contact($name, $id);
599 show_contact_forms($city_id);
600 show_add_new_contact_form($city_id);