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;
186 echo " <td>Role</td>\n";
187 echo " <td>"; show_contact_role_form($contact->getRole()); echo "</td>\n";
193 echo " <td>Registered</td>\n";
194 echo " <td>" . $contact->getAdded() . "</td>\n";
200 echo " <td>Forename</td>\n";
201 echo " <td>"; input("forename", $contact->getForename()); echo "</td>\n";
206 echo " <td>Middle name(s)</td>\n";
207 echo " <td>"; input("middle", $contact->getMiddle()); echo "</td>\n";
212 echo " <td>Surname</td>\n";
213 echo " <td>"; input("surname", $contact->getSurname()); echo "</td>\n";
218 echo " <td>Display name (if not concatenation of above)</td>\n";
219 echo " <td>"; input("displayname", $contact->getDisplayname()); echo "</td>\n";
223 $address = get_contact_address($contact);
224 if (! $address) $address = new Address;
226 echo " <td>Address</td>\n";
227 echo " <td>"; textarea("address", $address->getLine()); echo "</td>\n";
232 echo " <td>Postcode</td>\n";
233 echo " <td>"; input("postcode", $address->getPostcode()); echo "</td>\n";
238 echo " <td>Telephone</td>\n";
239 echo " <td>"; input("telephone1", $contact->getTelephone1()); echo "</td>\n";
242 echo " <td>Alternative telephone</td>\n";
243 echo " <td>"; input("telephone2", $contact->getTelephone2()); echo "</td>\n";
248 echo " <td>Email</td>\n";
249 echo " <td>"; input("email", $contact->getEmail()); echo "</td>\n";
253 $area = get_contact_area($contact);
254 if ($area) $area_id = $area->getId();
256 echo " <td>Area</td>\n";
257 echo " <td><select name=\"area_id\">\n";
258 $areas = get_city_areas();
259 foreach ($areas as $area) {
260 option("area_id", $area->getId(), get_area_displayname($area), $area_id);
262 echo " </select></td>\n";
267 echo " <td>Family unit</td>\n";
268 echo " <td><select name=\"parcel_size\">\n";
269 $mask = 1 << (count($parcel_sizes) + 1);
270 for ($i = 0; $i < count($parcel_sizes); $i++) {
271 option("parcel_size", $i << 1, $parcel_sizes[$i], $contact->getParcel() % $mask);
273 echo "</select></td>\n";
276 /* Parcel contents. */
278 echo " <td>Dietary requirements</td>\n";
280 for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
281 if (1 << $i == $GLOBALS['PARCEL_TOILETRY']) continue;
282 echo " <input type=\"checkbox\" name=\"parcel_$i\"";
283 if ($contact->getParcel() & (1 << $i)) echo " checked";
284 echo ">$parcel_contents[$i]\n";
291 echo " <td>Notes</td>\n";
292 echo " <td><textarea name=\"notes\">" . $contact->getNotes() . "</textarea></td>\n";
296 function show_new_contact_form($city_id = null) {
297 if (! check_admin(1)) return;
299 $areas = get_city_areas($city_id);
300 if (! count($areas)) {
301 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
305 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
306 echo "<p>Add a new contact:</p>\n";
309 show_contact_form($contact, true);
312 echo " <td colspan=2>"; submit("add_contact", "Add"); echo "</td></tr>\n";
318 function show_add_new_contact_form() {
319 if (! check_admin(1)) return;
322 $cities = $q->find();
323 if (! count($cities)) {
324 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
328 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
329 echo "<p>Add a new contact in <select name=\"city_id\">\n";
330 foreach ($cities as $city) {
331 option("city_id", $city->getId(), get_city_displayname($city));
334 submit("show_add_contact", "Proceed");
339 function update_contact(&$contact, $area_id, $new = false) {
340 global $contact_roles, $parcel_sizes, $parcel_contents;
343 for ($i = 0; $i < count($contact_roles); $i++) {
344 if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
347 /* Staff can place orders. */
348 if ($role & (1 << 0)) $role |= (1 << 2);
350 $forename = $_POST['forename'];
351 $middle = $_POST['middle'];
352 $surname = $_POST['surname'];
353 $displayname = $_POST['displayname'];
355 if (! $forename && ! $surname) {
356 echo "<p>Must have either a forename or surname!</p>\n";
359 if ($middle && ! ($forename && $surname)) {
360 echo "<p>Must have both a forename or surname for middle name(s) to make sense!</p>\n";
364 if (! $displayname) {
365 $displayname = $forename;
366 if ($middle) $displayname .= " $middle";
367 if ($forename) $displayname .= " ";
368 $displayname .= $surname;
369 echo "<p>Display name will be $displayname.</p>\n";
373 $line = $_POST['address'];
374 $postcode = $_POST['postcode'];
375 $q = new AddressQuery;
376 /* XXX: Finding by area properly? */
377 $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
378 if ($address->isNew()) {
379 /* Changing address. */
382 XXX: Check for other contacts at the old address.
383 Make this a new address if there are others, but
384 provide a link to update other contacts.
389 catch (Exception $e) {
390 echo "<p>Error adding $line.</p>\n";
395 $telephone1 = $_POST['telephone1'];
396 $telephone2 = $_POST['telephone2'];
397 $email = $_POST['email'];
398 $parcel = $_POST['parcel_size'];
399 for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
400 if ($_POST['parcel_' . $i] == "on") $parcel |= (1 << $i);
402 $notes = $_POST['notes'];
404 $contact->setRole($role);
405 $contact->setForename($forename);
406 $contact->setMiddle($middle);
407 $contact->setSurname($surname);
408 $contact->setDisplayname($displayname);
409 $contact->setTelephone1($telephone1);
410 $contact->setTelephone2($telephone2);
411 $contact->setEmail($email);
412 $contact->setParcel($parcel);
413 $contact->setNotes($notes);
414 $contact->setAddressId($address->getId());
419 catch (Exception $e) {
420 if ($new) echo "<p>Error adding $displayname.</p>\n";
421 else echo "<p>Error updating $displayname.</p>\n";
428 function add_contact(&$name) {
429 if (! check_admin(1, "add a contact")) return;
431 $area_id = $_POST['area_id'];
432 if (! is_numeric($area_id)) {
433 echo "<p>Invalid area!</p>\n";
437 $area = get_area_by_id($area_id);
439 echo "<p>No such area!</p>\n";
443 $contact = new Contact;
444 if (! update_contact($contact, $area_id, true)) return false;
445 return $contact->getId();
448 function delete_contact($name, $id = null, &$city_id = null) {
449 if (! check_admin(1, "delete a contact")) return;
451 if (isset($id)) $contact = get_contact_by_id($id);
452 else $contact = get_contact_by_name($name);
453 if (! $contact) return false;
455 ///* Remember city ID for dropdown. */
456 //$city_id = $area->getCityId();
460 echo "<p>Deleted contact.</p>\n";
462 catch (Exception $e) {
463 echo "<p>Error deleting $name!</p>\n";
470 function show_contact($name, &$id = null) {
471 if (isset($id)) $contact = get_contact_by_id($id);
472 else $contact = get_contact_by_name($name);
473 if (! $contact) return;
475 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
476 show_contact_summary($contact, true);
481 show_contact_form($contact);
483 if (check_admin(1)) {
485 echo " <td colspan=2>";
486 submit("update_contact", "Update");
495 /* /contact/in/area/Cambridge/1 */
496 if (count($parameters)) {
497 if ($parameters[0] == "in") {
498 switch ($parameters[1]) {
500 $area_id = $parameters[3];
501 $_POST['area_id'] = $area_id;
503 $area = $q->findOneById($area_id);
504 $city = get_area_city($area);
505 if ($city) $city_id = $city->getId();
506 show_area_contacts(0, 10, $parameters[2], $area_id);
510 $city_id = $parameters[3];
511 $_POST['city_id'] = $city_id;
513 $city = $q->findOneById($city_id);
514 show_city_contacts(0, 10, $parameters[2], $city_id);
518 show_add_new_contact_form($city_id);
520 else if ($parameters[0] == "search") {
521 search_contacts(0, 10, $parameters[1]);
524 list($name, $id, $args) = parse_parameters($parameters);
525 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
529 delete_contact($name, $id);
533 else if (isset($name)) show_contact($name, $id);
535 /* XXX: Shown after adding. */
536 show_contact_forms($city_id);
537 show_add_new_contact_form($city_id);
540 if (count($parameters)) {
541 show_contact_forms($city_id);