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 "<p>Show contacts in area\n";
133 echo "<select name=\"area_id\">\n";
134 foreach ($areas as $area) {
135 option("area_id", $area->getId(), get_area_displayname($area));
138 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 "<p>Show contacts in city\n";
151 echo "<select name=\"city_id\">\n";
152 foreach ($cities as $city) {
153 option("city_id", $city->getId(), get_city_displayname($city), $city_id);
156 echo "<input type=\"submit\" value=\"Show\">\n";
159 function show_contact_search_form() {
160 echo "<p>Search for contacts:";
161 input("search_contact");
162 echo "<input type=\"submit\" value=\"Search\">\n";
165 function show_contact_forms($city_id) {
166 form("noprint standout");
167 show_contact_areas_form($city_id);
168 show_contact_cities_form($city_id);
169 show_contact_search_form();
173 function show_contact_role_form($role) {
174 return show_role_form($role, $GLOBALS['contact_roles']);
177 function show_contact_form($contact = null, $new = false) {
178 global $contact_roles, $parcel_sizes, $parcel_contents;
180 if (! $contact) $contact = new Contact;
181 else if ($contact->getRole() & $GLOBALS['ROLE_BENEFICIARY']) {
182 $state_mask = $GLOBALS['STATE_ANY'];
183 $state_mask &= ~$GLOBALS['STATE_DELIVERED'];
184 $state_mask &= ~$GLOBALS['STATE_CANCELLED'];
186 $orders = get_contact_orders($contact, $state_mask);
188 if (count($orders)) {
190 echo " <td colspan=2><strong>Outstanding orders:</strong></td>\n";
194 echo " <td colspan=2>\n";
195 foreach ($orders as $order) {
196 echo " Order " . $order->getStrongLink($order->getId()) . ": " . get_order_displayname($order) . "<br>\n";
205 echo " <td>Role</td>\n";
206 echo " <td>"; show_contact_role_form($contact->getRole()); echo "</td>\n";
212 echo " <td>Registered</td>\n";
213 echo " <td>" . $contact->getAdded() . "</td>\n";
219 echo " <td>Forename</td>\n";
220 echo " <td>"; input("forename", $contact->getForename()); echo "</td>\n";
225 echo " <td>Middle name(s)</td>\n";
226 echo " <td>"; input("middle", $contact->getMiddle()); echo "</td>\n";
231 echo " <td>Surname</td>\n";
232 echo " <td>"; input("surname", $contact->getSurname()); echo "</td>\n";
237 echo " <td>Display name (if not concatenation of above)</td>\n";
238 echo " <td>"; input("displayname", $contact->getDisplayname()); echo "</td>\n";
242 $address = get_contact_address($contact);
243 if (! $address) $address = new Address;
245 echo " <td>Address</td>\n";
246 echo " <td>"; textarea("address", $address->getLine()); echo "</td>\n";
251 echo " <td>Postcode</td>\n";
252 echo " <td>"; input("postcode", $address->getPostcode()); echo "</td>\n";
257 echo " <td>Telephone</td>\n";
258 echo " <td>"; input("telephone1", $contact->getTelephone1()); echo "</td>\n";
261 echo " <td>Alternative telephone</td>\n";
262 echo " <td>"; input("telephone2", $contact->getTelephone2()); echo "</td>\n";
267 echo " <td>Email</td>\n";
268 echo " <td>"; input("email", $contact->getEmail()); echo "</td>\n";
272 $area = get_contact_area($contact);
273 if ($area) $area_id = $area->getId();
275 echo " <td>Area</td>\n";
276 echo " <td><select name=\"area_id\">\n";
277 $areas = get_city_areas();
278 foreach ($areas as $area) {
279 option("area_id", $area->getId(), get_area_displayname($area), $area_id);
281 echo " </select></td>\n";
286 echo " <td>Family unit</td>\n";
287 echo " <td><select name=\"parcel_size\">\n";
288 $mask = 1 << count($parcel_sizes);
289 for ($i = 0; $i < count($parcel_sizes); $i++) {
290 option("parcel_size", 1 << $i, $parcel_sizes[$i], $contact->getParcel() % $mask);
292 echo "</select></td>\n";
295 /* Parcel contents. */
297 echo " <td>Dietary requirements</td>\n";
299 for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
300 if (1 << $i == $GLOBALS['PARCEL_TOILETRY']) continue;
301 echo " <input type=\"checkbox\" name=\"parcel_$i\"";
302 if ($contact->getParcel() & (1 << $i)) echo " checked";
303 echo ">$parcel_contents[$i]\n";
310 echo " <td>Notes</td>\n";
311 echo " <td><textarea name=\"notes\">" . $contact->getNotes() . "</textarea></td>\n";
315 function show_new_contact_form($city_id = null) {
316 if (! check_admin(1)) return;
318 $areas = get_city_areas($city_id);
319 if (! count($areas)) {
320 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
325 echo "<p>Add a new contact:</p>\n";
328 show_contact_form($contact, true);
331 echo " <td colspan=2>"; submit("add_contact", "Add"); echo "</td></tr>\n";
337 function show_add_new_contact_form() {
338 if (! check_admin(1)) return;
341 $cities = $q->find();
342 if (! count($cities)) {
343 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
347 form("noprint standout");
348 echo "<p>Add a new contact in <select name=\"city_id\">\n";
349 foreach ($cities as $city) {
350 option("city_id", $city->getId(), get_city_displayname($city));
353 submit("show_add_contact", "Proceed");
358 function update_contact(&$contact, $area_id, $new = false) {
359 global $contact_roles, $parcel_sizes, $parcel_contents;
362 for ($i = 0; $i < count($contact_roles); $i++) {
363 if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
366 /* Staff can place orders. */
367 if ($role & (1 << 0)) $role |= (1 << 2);
369 $forename = $_POST['forename'];
370 $middle = $_POST['middle'];
371 $surname = $_POST['surname'];
372 $displayname = $_POST['displayname'];
374 if (! $forename && ! $surname) {
375 echo "<p>Must have either a forename or surname!</p>\n";
378 if ($middle && ! ($forename && $surname)) {
379 echo "<p>Must have both a forename or surname for middle name(s) to make sense!</p>\n";
383 if (! $displayname) {
384 $displayname = $forename;
385 if ($middle) $displayname .= " $middle";
386 if ($forename) $displayname .= " ";
387 $displayname .= $surname;
388 echo "<p>Display name will be $displayname.</p>\n";
392 $area_id = $_POST['area_id'];
393 $line = $_POST['address'];
394 $postcode = $_POST['postcode'];
395 $q = new AddressQuery;
396 /* XXX: Finding by area properly? */
397 $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
398 if ($address->isNew()) {
399 /* Changing address. */
402 XXX: Check for other contacts at the old address.
403 Make this a new address if there are others, but
404 provide a link to update other contacts.
409 catch (Exception $e) {
410 echo "<p>Error adding $line.</p>\n";
415 $telephone1 = $_POST['telephone1'];
416 $telephone2 = $_POST['telephone2'];
417 $email = $_POST['email'];
418 $parcel = $_POST['parcel_size'];
419 for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
420 if ($_POST['parcel_' . $i] == "on") $parcel |= (1 << $i);
422 $notes = $_POST['notes'];
424 $contact->setRole($role);
425 $contact->setForename($forename);
426 $contact->setMiddle($middle);
427 $contact->setSurname($surname);
428 $contact->setDisplayname($displayname);
429 $contact->setTelephone1($telephone1);
430 $contact->setTelephone2($telephone2);
431 $contact->setEmail($email);
432 $contact->setParcel($parcel);
433 $contact->setNotes($notes);
434 $contact->setAddressId($address->getId());
439 catch (Exception $e) {
440 if ($new) echo "<p>Error adding $displayname.</p>\n";
441 else echo "<p>Error updating $displayname.</p>\n";
448 function add_contact(&$name) {
449 if (! check_admin(1, "add a contact")) return;
451 $area_id = $_POST['area_id'];
452 if (! is_numeric($area_id)) {
453 echo "<p>Invalid area!</p>\n";
457 $area = get_area_by_id($area_id);
459 echo "<p>No such area!</p>\n";
463 $contact = new Contact;
464 if (! update_contact($contact, $area_id, true)) return false;
465 return $contact->getId();
468 function delete_contact($name, $id = null, &$city_id = null) {
469 if (! check_admin(1, "delete a contact")) return;
471 if (isset($id)) $contact = get_contact_by_id($id);
472 else $contact = get_contact_by_name($name);
473 if (! $contact) return false;
475 ///* Remember city ID for dropdown. */
476 //$city_id = $area->getCityId();
480 echo "<p>Deleted contact.</p>\n";
482 catch (Exception $e) {
483 echo "<p>Error deleting $name!</p>\n";
490 function show_contact($name, &$id = null) {
491 if (isset($id)) $contact = get_contact_by_id($id);
492 else $contact = get_contact_by_name($name);
493 if (! $contact) return;
496 show_contact_summary($contact, true);
501 show_contact_form($contact);
503 if (check_admin(1)) {
505 echo " <td colspan=2>";
506 submit("update_contact", "Update");
515 /* /contact/in/area/Cambridge/1 */
516 if (count($parameters)) {
517 if ($parameters[0] == "in") {
518 switch ($parameters[1]) {
520 $area_id = $parameters[3];
521 $_POST['area_id'] = $area_id;
523 $area = $q->findOneById($area_id);
524 $city = get_area_city($area);
525 if ($city) $city_id = $city->getId();
526 show_area_contacts(0, 10, $parameters[2], $area_id);
530 $city_id = $parameters[3];
531 $_POST['city_id'] = $city_id;
533 $city = $q->findOneById($city_id);
534 show_city_contacts(0, 10, $parameters[2], $city_id);
538 else if ($parameters[0] == "search") {
539 search_contacts(0, 10, $parameters[1]);
542 list($name, $id, $args) = parse_parameters($parameters);
543 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
547 delete_contact($name, $id);
551 else if (isset($name)) show_contact($name, $id);
553 show_contact_forms($city_id);
554 show_add_new_contact_form($city_id);