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 if ($role & $GLOBALS['ROLE_DONOR']) printf(" <a class=\"small\" href=\"/donation/from/contact/%s/%d\">Donations</a>", urlencode($contact->getDisplayname()), $contact->getId());
54 if ($role & $GLOBALS['ROLE_REQUESTER']) printf(" <a class=\"small\" href=\"/order/from/referrer/%s/%d\">Referred</a>", urlencode($contact->getDisplayname()), $contact->getId());
55 if ($role & $GLOBALS['ROLE_BENEFICIARY']) {
56 printf(" <a class=\"small\" href=\"/order/to/beneficiary/%s/%d\">Orders</a>", urlencode($contact->getDisplayname()), $contact->getId());
57 if (get_contact_area($contact)) printf(" <a class=\"small\" href=\"/order/place/for/beneficiary/%s/%d\">Place</a>", urlencode($contact->getDisplayname()), $contact->getId());
60 echo " " . $contact->getDeleteLink();
62 $area = get_contact_area($contact);
63 echo " in " . $area->getLink();
64 $city = get_contact_city($contact);
65 echo ", " . $city->getLink(get_city_displayname($city));
68 function show_contacts($offset, $per_page, $address_ids) {
69 $q = new ContactQuery;
70 $contacts = $q->filterByAddressId($address_ids)->orderByForename()->orderBySurname()->find();
71 if (count($contacts)) {
72 foreach ($contacts as $contact) show_contact_summary($contact);
77 function search_contacts($offset, $per_page, $search) {
78 $q = new ContactQuery;
79 $contacts = $q->filterByDisplayname("%$search%")->find();
80 echo "<p>Contacts matching '" . htmlspecialchars($search) . "':";
81 if (count($contacts)) {
82 foreach ($contacts as $contact) show_contact_summary($contact);
88 function show_city_contacts($offset, $per_page, $city_name, $city_id = null) {
89 if (isset($city_id)) $city = get_city_by_id($city_id);
90 else if ($city_name) $city = get_city_by_name($city_name);
93 $areas = $q->filterByCityId($city->getId())->find();
95 foreach ($areas as $area) $area_ids[] = $area->getId();
97 $q = new AddressQuery;
98 $addresses = $q->filterByAreaId($area_ids)->find();
99 $address_ids = array();
100 foreach ($addresses as $address) $address_ids[] = $address->getId();
102 echo "<p>Contacts in city " . $city->getLink(get_city_displayname($city)) . ":";
103 return show_contacts($offset, $per_page, $address_ids);
105 else echo "<p>No such city!</p>\n";
108 function show_area_contacts($offset, $per_page, $area_name, $area_id = null) {
109 if (isset($area_id)) $area = get_area_by_id($area_id);
110 else if ($area_name) $area = get_area_by_name($area_name);
112 $q = new AddressQuery;
113 $addresses = $q->filterByAreaId($area->getId())->find();
114 $address_ids = array();
115 foreach ($addresses as $address) $address_ids[] = $address->getId();
117 echo "<p>Contacts in area " . $area->getLink() . ":";
118 return show_contacts($offset, $per_page, $address_ids);
120 else echo "<p>No such area!</p>\n";
123 function show_contact_areas_form($city_id = null) {
124 $areas = get_city_areas($city_id);
125 if (! count($areas)) {
126 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
130 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
131 echo "<p>Show contacts in area\n";
132 echo "<select name=\"area_id\">\n";
133 foreach ($areas as $area) {
134 option("area_id", $area->getId(), get_area_displayname($area));
137 echo "<input type=\"submit\" value=\"Show\">\n";
141 function show_contact_cities_form($city_id = null) {
143 $cities = $q->orderByName()->find();
145 if (! count($cities)) {
146 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
150 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
151 echo "<p>Show contacts in city\n";
152 echo "<select name=\"city_id\">\n";
153 foreach ($cities as $city) {
154 option("city_id", $city->getId(), get_city_displayname($city), $city_id);
157 echo "<input type=\"submit\" value=\"Show\">\n";
161 function show_contact_search_form() {
162 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
163 echo "<p>Search for contacts:";
164 input("search_contact");
165 echo "<input type=\"submit\" value=\"Search\">\n";
169 function show_contact_forms($city_id) {
170 show_contact_areas_form($city_id);
171 show_contact_cities_form($city_id);
172 show_contact_search_form();
175 function show_contact_role_form($role) {
176 return show_role_form($role, $GLOBALS['contact_roles']);
179 function show_contact_form($contact = null, $new = false) {
180 global $contact_roles, $parcel_sizes, $parcel_contents;
182 if (! $contact) $contact = new Contact;
183 else if ($contact->getRole() & $GLOBALS['ROLE_BENEFICIARY']) {
184 $state_mask = $GLOBALS['STATE_ANY'];
185 $state_mask &= ~$GLOBALS['STATE_DELIVERED'];
186 $state_mask &= ~$GLOBALS['STATE_CANCELLED'];
188 $orders = get_contact_orders($contact, $state_mask);
190 if (count($orders)) {
192 echo " <td colspan=2><strong>Outstanding orders:</strong></td>\n";
196 echo " <td colspan=2>\n";
197 foreach ($orders as $order) {
198 echo " Order " . $order->getStrongLink($order->getId()) . ": " . get_order_displayname($order) . "<br>\n";
207 echo " <td>Role</td>\n";
208 echo " <td>"; show_contact_role_form($contact->getRole()); echo "</td>\n";
214 echo " <td>Registered</td>\n";
215 echo " <td>" . $contact->getAdded() . "</td>\n";
221 echo " <td>Forename</td>\n";
222 echo " <td>"; input("forename", $contact->getForename()); echo "</td>\n";
227 echo " <td>Middle name(s)</td>\n";
228 echo " <td>"; input("middle", $contact->getMiddle()); echo "</td>\n";
233 echo " <td>Surname</td>\n";
234 echo " <td>"; input("surname", $contact->getSurname()); echo "</td>\n";
239 echo " <td>Display name (if not concatenation of above)</td>\n";
240 echo " <td>"; input("displayname", $contact->getDisplayname()); echo "</td>\n";
244 $address = get_contact_address($contact);
245 if (! $address) $address = new Address;
247 echo " <td>Address</td>\n";
248 echo " <td>"; textarea("address", $address->getLine()); echo "</td>\n";
253 echo " <td>Postcode</td>\n";
254 echo " <td>"; input("postcode", $address->getPostcode()); echo "</td>\n";
259 echo " <td>Telephone</td>\n";
260 echo " <td>"; input("telephone1", $contact->getTelephone1()); echo "</td>\n";
263 echo " <td>Alternative telephone</td>\n";
264 echo " <td>"; input("telephone2", $contact->getTelephone2()); echo "</td>\n";
269 echo " <td>Email</td>\n";
270 echo " <td>"; input("email", $contact->getEmail()); echo "</td>\n";
274 $area = get_contact_area($contact);
275 if ($area) $area_id = $area->getId();
277 echo " <td>Area</td>\n";
278 echo " <td><select name=\"area_id\">\n";
279 $areas = get_city_areas();
280 foreach ($areas as $area) {
281 option("area_id", $area->getId(), get_area_displayname($area), $area_id);
283 echo " </select></td>\n";
288 echo " <td>Family unit</td>\n";
289 echo " <td><select name=\"parcel_size\">\n";
290 $mask = 1 << count($parcel_sizes);
291 for ($i = 0; $i < count($parcel_sizes); $i++) {
292 option("parcel_size", 1 << $i, $parcel_sizes[$i], $contact->getParcel() % $mask);
294 echo "</select></td>\n";
297 /* Parcel contents. */
299 echo " <td>Dietary requirements</td>\n";
301 for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
302 if (1 << $i == $GLOBALS['PARCEL_TOILETRY']) continue;
303 echo " <input type=\"checkbox\" name=\"parcel_$i\"";
304 if ($contact->getParcel() & (1 << $i)) echo " checked";
305 echo ">$parcel_contents[$i]\n";
312 echo " <td>Notes</td>\n";
313 echo " <td><textarea name=\"notes\">" . $contact->getNotes() . "</textarea></td>\n";
317 function show_new_contact_form($city_id = null) {
318 if (! check_admin(1)) return;
320 $areas = get_city_areas($city_id);
321 if (! count($areas)) {
322 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
326 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
327 echo "<p>Add a new contact:</p>\n";
330 show_contact_form($contact, true);
333 echo " <td colspan=2>"; submit("add_contact", "Add"); echo "</td></tr>\n";
339 function show_add_new_contact_form() {
340 if (! check_admin(1)) return;
343 $cities = $q->find();
344 if (! count($cities)) {
345 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
349 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
350 echo "<p>Add a new contact in <select name=\"city_id\">\n";
351 foreach ($cities as $city) {
352 option("city_id", $city->getId(), get_city_displayname($city));
355 submit("show_add_contact", "Proceed");
360 function update_contact(&$contact, $area_id, $new = false) {
361 global $contact_roles, $parcel_sizes, $parcel_contents;
364 for ($i = 0; $i < count($contact_roles); $i++) {
365 if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
368 /* Staff can place orders. */
369 if ($role & (1 << 0)) $role |= (1 << 2);
371 $forename = $_POST['forename'];
372 $middle = $_POST['middle'];
373 $surname = $_POST['surname'];
374 $displayname = $_POST['displayname'];
376 if (! $forename && ! $surname) {
377 echo "<p>Must have either a forename or surname!</p>\n";
380 if ($middle && ! ($forename && $surname)) {
381 echo "<p>Must have both a forename or surname for middle name(s) to make sense!</p>\n";
385 if (! $displayname) {
386 $displayname = $forename;
387 if ($middle) $displayname .= " $middle";
388 if ($forename) $displayname .= " ";
389 $displayname .= $surname;
390 echo "<p>Display name will be $displayname.</p>\n";
394 $area_id = $_POST['area_id'];
395 $line = $_POST['address'];
396 $postcode = $_POST['postcode'];
397 $q = new AddressQuery;
398 /* XXX: Finding by area properly? */
399 $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
400 if ($address->isNew()) {
401 /* Changing address. */
404 XXX: Check for other contacts at the old address.
405 Make this a new address if there are others, but
406 provide a link to update other contacts.
411 catch (Exception $e) {
412 echo "<p>Error adding $line.</p>\n";
417 $telephone1 = $_POST['telephone1'];
418 $telephone2 = $_POST['telephone2'];
419 $email = $_POST['email'];
420 $parcel = $_POST['parcel_size'];
421 for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
422 if ($_POST['parcel_' . $i] == "on") $parcel |= (1 << $i);
424 $notes = $_POST['notes'];
426 $contact->setRole($role);
427 $contact->setForename($forename);
428 $contact->setMiddle($middle);
429 $contact->setSurname($surname);
430 $contact->setDisplayname($displayname);
431 $contact->setTelephone1($telephone1);
432 $contact->setTelephone2($telephone2);
433 $contact->setEmail($email);
434 $contact->setParcel($parcel);
435 $contact->setNotes($notes);
436 $contact->setAddressId($address->getId());
441 catch (Exception $e) {
442 if ($new) echo "<p>Error adding $displayname.</p>\n";
443 else echo "<p>Error updating $displayname.</p>\n";
450 function add_contact(&$name) {
451 if (! check_admin(1, "add a contact")) return;
453 $area_id = $_POST['area_id'];
454 if (! is_numeric($area_id)) {
455 echo "<p>Invalid area!</p>\n";
459 $area = get_area_by_id($area_id);
461 echo "<p>No such area!</p>\n";
465 $contact = new Contact;
466 if (! update_contact($contact, $area_id, true)) return false;
467 return $contact->getId();
470 function delete_contact($name, $id = null, &$city_id = null) {
471 if (! check_admin(1, "delete a contact")) return;
473 if (isset($id)) $contact = get_contact_by_id($id);
474 else $contact = get_contact_by_name($name);
475 if (! $contact) return false;
477 ///* Remember city ID for dropdown. */
478 //$city_id = $area->getCityId();
482 echo "<p>Deleted contact.</p>\n";
484 catch (Exception $e) {
485 echo "<p>Error deleting $name!</p>\n";
492 function show_contact($name, &$id = null) {
493 if (isset($id)) $contact = get_contact_by_id($id);
494 else $contact = get_contact_by_name($name);
495 if (! $contact) return;
497 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
498 show_contact_summary($contact, true);
503 show_contact_form($contact);
505 if (check_admin(1)) {
507 echo " <td colspan=2>";
508 submit("update_contact", "Update");
517 /* /contact/in/area/Cambridge/1 */
518 if (count($parameters)) {
519 if ($parameters[0] == "in") {
520 switch ($parameters[1]) {
522 $area_id = $parameters[3];
523 $_POST['area_id'] = $area_id;
525 $area = $q->findOneById($area_id);
526 $city = get_area_city($area);
527 if ($city) $city_id = $city->getId();
528 show_area_contacts(0, 10, $parameters[2], $area_id);
532 $city_id = $parameters[3];
533 $_POST['city_id'] = $city_id;
535 $city = $q->findOneById($city_id);
536 show_city_contacts(0, 10, $parameters[2], $city_id);
540 show_add_new_contact_form($city_id);
542 else if ($parameters[0] == "search") {
543 search_contacts(0, 10, $parameters[1]);
546 list($name, $id, $args) = parse_parameters($parameters);
547 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
551 delete_contact($name, $id);
555 else if (isset($name)) show_contact($name, $id);
557 /* XXX: Shown after adding. */
558 show_contact_forms($city_id);
559 show_add_new_contact_form($city_id);
562 if (count($parameters)) {
563 show_contact_forms($city_id);