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['area_id']) {
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['city_id']) {
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);
90 function show_city_contacts($offset, $per_page, $city_name, $city_id = null) {
91 if (isset($city_id)) $city = get_city_by_id($city_id);
92 else if ($city_name) $city = get_city_by_name($city_name);
95 $areas = $q->filterByCityId($city->getId())->find();
97 foreach ($areas as $area) $area_ids[] = $area->getId();
99 $q = new AddressQuery;
100 $addresses = $q->filterByAreaId($area_ids)->find();
101 $address_ids = array();
102 foreach ($addresses as $address) $address_ids[] = $address->getId();
104 echo "<p>Contacts in city " . $city->getLink(get_city_displayname($city)) . ":";
105 return show_contacts($offset, $per_page, $address_ids);
107 else echo "<p>No such city!</p>\n";
110 function show_area_contacts($offset, $per_page, $area_name, $area_id = null) {
111 if (isset($area_id)) $area = get_area_by_id($area_id);
112 else if ($area_name) $area = get_area_by_name($area_name);
114 $q = new AddressQuery;
115 $addresses = $q->filterByAreaId($area->getId())->find();
116 $address_ids = array();
117 foreach ($addresses as $address) $address_ids[] = $address->getId();
119 echo "<p>Contacts in area " . $area->getLink() . ":";
120 return show_contacts($offset, $per_page, $address_ids);
122 else echo "<p>No such area!</p>\n";
125 function show_contact_areas_form($city_id = null) {
126 $areas = get_city_areas($city_id);
127 if (! count($areas)) {
128 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
132 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
133 echo "<p>Show contacts in area\n";
134 echo "<select name=\"area_id\">\n";
135 foreach ($areas as $area) {
136 option("area_id", $area->getId(), get_area_displayname($area));
139 echo "<input type=\"submit\" value=\"Show\">\n";
143 function show_contact_cities_form($city_id = null) {
145 $cities = $q->orderByName()->find();
147 if (! count($cities)) {
148 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
152 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
153 echo "<p>Show contacts in city\n";
154 echo "<select name=\"city_id\">\n";
155 foreach ($cities as $city) {
156 option("city_id", $city->getId(), get_city_displayname($city), $city_id);
159 echo "<input type=\"submit\" value=\"Show\">\n";
163 function show_contact_search_form() {
164 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
165 echo "<p>Search for contacts:";
166 input("search_contact");
167 echo "<input type=\"submit\" value=\"Search\">\n";
171 function show_contact_forms($city_id) {
172 show_contact_areas_form($city_id);
173 show_contact_cities_form($city_id);
174 show_contact_search_form();
177 function show_contact_role_form($role) {
178 return show_role_form($role, $GLOBALS['contact_roles']);
181 function show_contact_form($contact = null, $new = false) {
182 global $contact_roles, $parcel_sizes, $parcel_contents;
184 if (! $contact) $contact = new Contact;
185 else if ($contact->getRole() & $GLOBALS['ROLE_BENEFICIARY']) {
186 $state_mask = $GLOBALS['STATE_ANY'];
187 $state_mask &= ~$GLOBALS['STATE_DELIVERED'];
188 $state_mask &= ~$GLOBALS['STATE_CANCELLED'];
190 $orders = get_contact_orders($contact, $state_mask);
192 if (count($orders)) {
194 echo " <td colspan=2><strong>Outstanding orders:</strong></td>\n";
198 echo " <td colspan=2>\n";
199 foreach ($orders as $order) {
200 echo " Order " . $order->getStrongLink($order->getId()) . ": " . get_order_displayname($order) . "<br>\n";
209 echo " <td>Role</td>\n";
210 echo " <td>"; show_contact_role_form($contact->getRole()); echo "</td>\n";
216 echo " <td>Registered</td>\n";
217 echo " <td>" . $contact->getAdded() . "</td>\n";
223 echo " <td>Forename</td>\n";
224 echo " <td>"; input("forename", $contact->getForename()); echo "</td>\n";
229 echo " <td>Middle name(s)</td>\n";
230 echo " <td>"; input("middle", $contact->getMiddle()); echo "</td>\n";
235 echo " <td>Surname</td>\n";
236 echo " <td>"; input("surname", $contact->getSurname()); echo "</td>\n";
241 echo " <td>Display name (if not concatenation of above)</td>\n";
242 echo " <td>"; input("displayname", $contact->getDisplayname()); echo "</td>\n";
246 $address = get_contact_address($contact);
247 if (! $address) $address = new Address;
249 echo " <td>Address</td>\n";
250 echo " <td>"; textarea("address", $address->getLine()); echo "</td>\n";
255 echo " <td>Postcode</td>\n";
256 echo " <td>"; input("postcode", $address->getPostcode()); echo "</td>\n";
261 echo " <td>Telephone</td>\n";
262 echo " <td>"; input("telephone1", $contact->getTelephone1()); echo "</td>\n";
265 echo " <td>Alternative telephone</td>\n";
266 echo " <td>"; input("telephone2", $contact->getTelephone2()); echo "</td>\n";
271 echo " <td>Email</td>\n";
272 echo " <td>"; input("email", $contact->getEmail()); echo "</td>\n";
276 $area = get_contact_area($contact);
277 if ($area) $area_id = $area->getId();
279 echo " <td>Area</td>\n";
280 echo " <td><select name=\"area_id\">\n";
281 $areas = get_city_areas();
282 foreach ($areas as $area) {
283 option("area_id", $area->getId(), get_area_displayname($area), $area_id);
285 echo " </select></td>\n";
290 echo " <td>Family unit</td>\n";
291 echo " <td><select name=\"parcel_size\">\n";
292 $mask = 1 << count($parcel_sizes);
293 for ($i = 0; $i < count($parcel_sizes); $i++) {
294 option("parcel_size", 1 << $i, $parcel_sizes[$i], $contact->getParcel() % $mask);
296 echo "</select></td>\n";
299 /* Parcel contents. */
301 echo " <td>Dietary requirements</td>\n";
303 for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
304 if (1 << $i == $GLOBALS['PARCEL_TOILETRY']) continue;
305 echo " <input type=\"checkbox\" name=\"parcel_$i\"";
306 if ($contact->getParcel() & (1 << $i)) echo " checked";
307 echo ">$parcel_contents[$i]\n";
314 echo " <td>Notes</td>\n";
315 echo " <td><textarea name=\"notes\">" . $contact->getNotes() . "</textarea></td>\n";
319 function show_new_contact_form($city_id = null) {
320 if (! check_admin(1)) return;
322 $areas = get_city_areas($city_id);
323 if (! count($areas)) {
324 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
328 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
329 echo "<p>Add a new contact:</p>\n";
332 show_contact_form($contact, true);
335 echo " <td colspan=2>"; submit("add_contact", "Add"); echo "</td></tr>\n";
341 function show_add_new_contact_form() {
342 if (! check_admin(1)) return;
345 $cities = $q->find();
346 if (! count($cities)) {
347 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
351 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
352 echo "<p>Add a new contact in <select name=\"city_id\">\n";
353 foreach ($cities as $city) {
354 option("city_id", $city->getId(), get_city_displayname($city));
357 submit("show_add_contact", "Proceed");
362 function update_contact(&$contact, $area_id, $new = false) {
363 global $contact_roles, $parcel_sizes, $parcel_contents;
366 for ($i = 0; $i < count($contact_roles); $i++) {
367 if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
370 /* Staff can place orders. */
371 if ($role & (1 << 0)) $role |= (1 << 2);
373 $forename = $_POST['forename'];
374 $middle = $_POST['middle'];
375 $surname = $_POST['surname'];
376 $displayname = $_POST['displayname'];
378 if (! $forename && ! $surname) {
379 echo "<p>Must have either a forename or surname!</p>\n";
382 if ($middle && ! ($forename && $surname)) {
383 echo "<p>Must have both a forename or surname for middle name(s) to make sense!</p>\n";
387 if (! $displayname) {
388 $displayname = $forename;
389 if ($middle) $displayname .= " $middle";
390 if ($forename) $displayname .= " ";
391 $displayname .= $surname;
392 echo "<p>Display name will be $displayname.</p>\n";
396 $area_id = $_POST['area_id'];
397 $line = $_POST['address'];
398 $postcode = $_POST['postcode'];
399 $q = new AddressQuery;
400 /* XXX: Finding by area properly? */
401 $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
402 if ($address->isNew()) {
403 /* Changing address. */
406 XXX: Check for other contacts at the old address.
407 Make this a new address if there are others, but
408 provide a link to update other contacts.
413 catch (Exception $e) {
414 echo "<p>Error adding $line.</p>\n";
419 $telephone1 = $_POST['telephone1'];
420 $telephone2 = $_POST['telephone2'];
421 $email = $_POST['email'];
422 $parcel = $_POST['parcel_size'];
423 for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
424 if ($_POST['parcel_' . $i] == "on") $parcel |= (1 << $i);
426 $notes = $_POST['notes'];
428 $contact->setRole($role);
429 $contact->setForename($forename);
430 $contact->setMiddle($middle);
431 $contact->setSurname($surname);
432 $contact->setDisplayname($displayname);
433 $contact->setTelephone1($telephone1);
434 $contact->setTelephone2($telephone2);
435 $contact->setEmail($email);
436 $contact->setParcel($parcel);
437 $contact->setNotes($notes);
438 $contact->setAddressId($address->getId());
443 catch (Exception $e) {
444 if ($new) echo "<p>Error adding $displayname.</p>\n";
445 else echo "<p>Error updating $displayname.</p>\n";
452 function add_contact(&$name) {
453 if (! check_admin(1, "add a contact")) return;
455 $area_id = $_POST['area_id'];
456 if (! is_numeric($area_id)) {
457 echo "<p>Invalid area!</p>\n";
461 $area = get_area_by_id($area_id);
463 echo "<p>No such area!</p>\n";
467 $contact = new Contact;
468 if (! update_contact($contact, $area_id, true)) return false;
469 return $contact->getId();
472 function delete_contact($name, $id = null, &$city_id = null) {
473 if (! check_admin(1, "delete a contact")) return;
475 if (isset($id)) $contact = get_contact_by_id($id);
476 else $contact = get_contact_by_name($name);
477 if (! $contact) return false;
479 ///* Remember city ID for dropdown. */
480 //$city_id = $area->getCityId();
484 echo "<p>Deleted contact.</p>\n";
486 catch (Exception $e) {
487 echo "<p>Error deleting $name!</p>\n";
494 function show_contact($name, &$id = null) {
495 if (isset($id)) $contact = get_contact_by_id($id);
496 else $contact = get_contact_by_name($name);
497 if (! $contact) return;
499 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
500 show_contact_summary($contact, true);
505 show_contact_form($contact);
507 if (check_admin(1)) {
509 echo " <td colspan=2>";
510 submit("update_contact", "Update");
519 /* /contact/in/area/Cambridge/1 */
520 if (count($parameters)) {
521 if ($parameters[0] == "in") {
522 switch ($parameters[1]) {
524 $area_id = $parameters[3];
525 $_POST['area_id'] = $area_id;
527 $area = $q->findOneById($area_id);
528 $city = get_area_city($area);
529 if ($city) $city_id = $city->getId();
530 show_area_contacts(0, 10, $parameters[2], $area_id);
534 $city_id = $parameters[3];
535 $_POST['city_id'] = $city_id;
537 $city = $q->findOneById($city_id);
538 show_city_contacts(0, 10, $parameters[2], $city_id);
542 show_add_new_contact_form($city_id);
544 else if ($parameters[0] == "search") {
545 search_contacts(0, 10, $parameters[1]);
548 list($name, $id, $args) = parse_parameters($parameters);
549 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
553 delete_contact($name, $id);
557 else if (isset($name)) show_contact($name, $id);
559 /* XXX: Shown after adding. */
560 show_contact_forms($city_id);
561 show_add_new_contact_form($city_id);
564 if (count($parameters)) {
565 show_contact_forms($city_id);