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;
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>Notes</td>\n";
268 echo " <td><textarea name=\"notes\">" . $contact->getNotes() . "</textarea></td>\n";
272 function show_new_contact_form($city_id = null) {
273 if (! check_admin(1)) return;
275 $areas = get_city_areas($city_id);
276 if (! count($areas)) {
277 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
281 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
282 echo "<p>Add a new contact:</p>\n";
285 show_contact_form($contact, true);
288 echo " <td colspan=2>"; submit("add_contact", "Add"); echo "</td></tr>\n";
294 function show_add_new_contact_form() {
295 if (! check_admin(1)) return;
298 $cities = $q->find();
299 if (! count($cities)) {
300 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
304 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
305 echo "<p>Add a new contact in <select name=\"city_id\">\n";
306 foreach ($cities as $city) {
307 option("city_id", $city->getId(), get_city_displayname($city));
310 submit("show_add_contact", "Proceed");
315 function update_contact(&$contact, $area_id, $new = false) {
316 global $contact_roles;
319 for ($i = 0; $i < count($contact_roles); $i++) {
320 if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
323 /* Staff can place orders. */
324 if ($role & (1 << 0)) $role |= (1 << 2);
326 $forename = $_POST['forename'];
327 $middle = $_POST['middle'];
328 $surname = $_POST['surname'];
329 $displayname = $_POST['displayname'];
331 if (! $forename && ! $surname) {
332 echo "<p>Must have either a forename or surname!</p>\n";
335 if ($middle && ! ($forename && $surname)) {
336 echo "<p>Must have both a forename or surname for middle name(s) to make sense!</p>\n";
340 if (! $displayname) {
341 $displayname = $forename;
342 if ($middle) $displayname .= " $middle";
343 if ($forename) $displayname .= " ";
344 $displayname .= $surname;
345 echo "<p>Display name will be $displayname.</p>\n";
349 $line = $_POST['address'];
350 $postcode = $_POST['postcode'];
351 $q = new AddressQuery;
352 /* XXX: Finding by area properly? */
353 $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
354 if ($address->isNew()) {
355 /* Changing address. */
358 XXX: Check for other contacts at the old address.
359 Make this a new address if there are others, but
360 provide a link to update other contacts.
365 catch (Exception $e) {
366 echo "<p>Error adding $line.</p>\n";
371 $telephone1 = $_POST['telephone1'];
372 $telephone2 = $_POST['telephone2'];
373 $email = $_POST['email'];
374 $notes = $_POST['notes'];
376 $contact->setRole($role);
377 $contact->setForename($forename);
378 $contact->setMiddle($middle);
379 $contact->setSurname($surname);
380 $contact->setDisplayname($displayname);
381 $contact->setTelephone1($telephone1);
382 $contact->setTelephone2($telephone2);
383 $contact->setEmail($email);
384 $contact->setNotes($notes);
385 $contact->setAddressId($address->getId());
390 catch (Exception $e) {
391 if ($new) echo "<p>Error adding $displayname.</p>\n";
392 else echo "<p>Error updating $displayname.</p>\n";
399 function add_contact(&$name) {
400 if (! check_admin(1, "add a contact")) return;
402 $area_id = $_POST['area_id'];
403 if (! is_numeric($area_id)) {
404 echo "<p>Invalid area!</p>\n";
408 $area = get_area_by_id($area_id);
410 echo "<p>No such area!</p>\n";
414 $contact = new Contact;
415 if (! update_contact($contact, $area_id, true)) return false;
416 return $contact->getId();
419 function delete_contact($name, $id = null, &$city_id = null) {
420 if (! check_admin(1, "delete a contact")) return;
422 if (isset($id)) $contact = get_contact_by_id($id);
423 else $contact = get_contact_by_name($name);
424 if (! $contact) return false;
426 ///* Remember city ID for dropdown. */
427 //$city_id = $area->getCityId();
431 echo "<p>Deleted contact.</p>\n";
433 catch (Exception $e) {
434 echo "<p>Error deleting $name!</p>\n";
441 function show_contact($name, &$id = null) {
442 if (isset($id)) $contact = get_contact_by_id($id);
443 else $contact = get_contact_by_name($name);
444 if (! $contact) return;
446 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
447 show_contact_summary($contact, true);
452 show_contact_form($contact);
454 if (check_admin(1)) {
456 echo " <td colspan=2>";
457 submit("update_contact", "Update");
466 /* /contact/in/area/Cambridge/1 */
467 if (count($parameters)) {
468 if ($parameters[0] == "in") {
469 switch ($parameters[1]) {
471 $area_id = $parameters[3];
472 $_POST['area_id'] = $area_id;
474 $area = $q->findOneById($area_id);
475 $city = get_area_city($area);
476 if ($city) $city_id = $city->getId();
477 show_area_contacts(0, 10, $parameters[2], $area_id);
481 $city_id = $parameters[3];
482 $_POST['city_id'] = $city_id;
484 $city = $q->findOneById($city_id);
485 show_city_contacts(0, 10, $parameters[2], $city_id);
489 show_add_new_contact_form($city_id);
491 else if ($parameters[0] == "search") {
492 search_contacts(0, 10, $parameters[1]);
495 list($name, $id, $args) = parse_parameters($parameters);
496 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
500 delete_contact($name, $id);
504 else if (isset($name)) show_contact($name, $id);
506 /* XXX: Shown after adding. */
507 show_contact_forms($city_id);
508 show_add_new_contact_form($city_id);
511 if (count($parameters)) {
512 show_contact_forms($city_id);