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) {
48 echo "<br>\nContact " . $contact->getLink();
49 $role = $contact->getRole();
50 $role_string = get_contact_role_string($contact);
51 if ($role_string) echo " $role_string";
52 if ($role & $GLOBALS['ROLE_DONOR']) printf(" <a class=\"small\" href=\"/donation/from/contact/%s/%d\">Donations</a>", urlencode($contact->getDisplayname()), $contact->getId());
53 if ($role & $GLOBALS['ROLE_REQUESTER']) printf(" <a class=\"small\" href=\"/order/from/referrer/%s/%d\">Referred</a>", urlencode($contact->getDisplayname()), $contact->getId());
54 if ($role & $GLOBALS['ROLE_BENEFICIARY']) printf(" <a class=\"small\" href=\"/order/to/beneficiary/%s/%d\">Orders</a>", urlencode($contact->getDisplayname()), $contact->getId());
56 echo " " . $contact->getDeleteLink();
58 $area = get_contact_area($contact);
59 echo " in " . $area->getLink();
62 function show_contacts($offset, $per_page, $address_ids) {
63 $q = new ContactQuery;
64 $contacts = $q->filterByAddressId($address_ids)->orderByForename()->orderBySurname()->find();
65 if (count($contacts)) {
66 foreach ($contacts as $contact) show_contact_summary($contact);
71 function search_contacts($offset, $per_page, $search) {
72 $q = new ContactQuery;
73 $contacts = $q->filterByDisplayname("%$search%")->find();
74 echo "<p>Contacts matching '" . htmlspecialchars($search) . "':";
75 if (count($contacts)) {
76 foreach ($contacts as $contact) show_contact_summary($contact);
82 function show_city_contacts($offset, $per_page, $city_name, $city_id = null) {
83 if (isset($city_id)) $city = get_city_by_id($city_id);
84 else if ($city_name) $city = get_city_by_name($city_name);
87 $areas = $q->filterByCityId($city->getId())->find();
89 foreach ($areas as $area) $area_ids[] = $area->getId();
91 $q = new AddressQuery;
92 $addresses = $q->filterByAreaId($area_ids)->find();
93 $address_ids = array();
94 foreach ($addresses as $address) $address_ids[] = $address->getId();
96 echo "<p>Contacts in city " . $city->getLink(get_city_displayname($city)) . ":";
97 return show_contacts($offset, $per_page, $address_ids);
99 else echo "<p>No such city!</p>\n";
102 function show_area_contacts($offset, $per_page, $area_name, $area_id = null) {
103 if (isset($area_id)) $area = get_area_by_id($area_id);
104 else if ($area_name) $area = get_area_by_name($area_name);
106 $q = new AddressQuery;
107 $addresses = $q->filterByAreaId($area->getId())->find();
108 $address_ids = array();
109 foreach ($addresses as $address) $address_ids[] = $address->getId();
111 echo "<p>Contacts in area " . $area->getLink() . ":";
112 return show_contacts($offset, $per_page, $address_ids);
114 else echo "<p>No such area!</p>\n";
117 function show_contact_areas_form($city_id = null) {
118 $areas = get_city_areas($city_id);
119 if (! count($areas)) {
120 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
124 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
125 echo "<p>Show contacts in area\n";
126 echo "<select name=\"area_id\">\n";
127 foreach ($areas as $area) {
128 option("area_id", $area->getId(), get_area_displayname($area));
131 echo "<input type=\"submit\" value=\"Show\">\n";
135 function show_contact_cities_form($city_id = null) {
137 $cities = $q->orderByName()->find();
139 if (! count($cities)) {
140 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
144 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
145 echo "<p>Show contacts in city\n";
146 echo "<select name=\"city_id\">\n";
147 foreach ($cities as $city) {
148 option("city_id", $city->getId(), get_city_displayname($city), $city_id);
151 echo "<input type=\"submit\" value=\"Show\">\n";
155 function show_contact_search_form() {
156 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
157 echo "<p>Search for contacts:";
158 input("search_contact");
159 echo "<input type=\"submit\" value=\"Search\">\n";
163 function show_contact_forms($city_id) {
164 show_contact_areas_form($city_id);
165 show_contact_cities_form($city_id);
166 show_contact_search_form();
169 function show_contact_role_form($role) {
170 return show_role_form($role, $GLOBALS['contact_roles']);
173 function show_contact_form($contact = null) {
174 global $contact_roles;
176 if (! $contact) $contact = new Contact;
180 echo " <td>Role</td>\n";
181 echo " <td>"; show_contact_role_form($contact->getRole()); echo "</td>\n";
186 echo " <td>Forename</td>\n";
187 echo " <td>"; input("forename", $contact->getForename()); echo "</td>\n";
192 echo " <td>Middle name(s)</td>\n";
193 echo " <td>"; input("middle", $contact->getMiddle()); echo "</td>\n";
198 echo " <td>Surname</td>\n";
199 echo " <td>"; input("surname", $contact->getSurname()); echo "</td>\n";
204 echo " <td>Display name (if not concatenation of above)</td>\n";
205 echo " <td>"; input("displayname", $contact->getDisplayname()); echo "</td>\n";
209 $address = get_contact_address($contact);
210 if (! $address) $address = new Address;
212 echo " <td>Address</td>\n";
213 echo " <td>"; textarea("address", $address->getLine()); echo "</td>\n";
218 echo " <td>Postcode</td>\n";
219 echo " <td>"; input("postcode", $address->getPostcode()); echo "</td>\n";
224 echo " <td>Telephone</td>\n";
225 echo " <td>"; input("telephone1", $contact->getTelephone1()); echo "</td>\n";
228 echo " <td>Alternative telephone</td>\n";
229 echo " <td>"; input("telephone2", $contact->getTelephone2()); echo "</td>\n";
234 echo " <td>Email</td>\n";
235 echo " <td>"; input("email", $contact->getEmail()); echo "</td>\n";
239 $area = get_contact_area($contact);
240 if ($area) $area_id = $area->getId();
242 echo " <td>Area</td>\n";
243 echo " <td><select name=\"area_id\">\n";
244 $areas = get_city_areas();
245 foreach ($areas as $area) {
246 option("area_id", $area->getId(), get_area_displayname($area), $area_id);
248 echo " </select></td>\n";
252 function show_new_contact_form($city_id = null) {
253 if (! check_admin(1)) return;
255 $areas = get_city_areas($city_id);
256 if (! count($areas)) {
257 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
261 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
262 echo "<p>Add a new contact:</p>\n";
265 show_contact_form($contact);
268 echo " <td colspan=2>"; submit("add_contact", "Add"); echo "</td></tr>\n";
274 function show_add_new_contact_form() {
275 if (! check_admin(1)) return;
278 $cities = $q->find();
279 if (! count($cities)) {
280 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
284 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
285 echo "<p>Add a new contact in <select name=\"city_id\">\n";
286 foreach ($cities as $city) {
287 option("city_id", $city->getId(), get_city_displayname($city));
290 submit("show_add_contact", "Proceed");
295 function update_contact(&$contact, $area_id, $new = false) {
296 global $contact_roles;
299 for ($i = 0; $i < count($contact_roles); $i++) {
300 if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
303 /* Staff can place orders. */
304 if ($role & (1 << 0)) $role |= (1 << 2);
306 $forename = $_POST['forename'];
307 $middle = $_POST['middle'];
308 $surname = $_POST['surname'];
309 $displayname = $_POST['displayname'];
311 if (! $forename && ! $surname) {
312 echo "<p>Must have either a forename or surname!</p>\n";
315 if ($middle && ! ($forename && $surname)) {
316 echo "<p>Must have both a forename or surname for middle name(s) to make sense!</p>\n";
320 if (! $displayname) {
321 $displayname = $forename;
322 if ($middle) $displayname .= " $middle";
323 if ($forename) $displayname .= " ";
324 $displayname .= $surname;
325 echo "<p>Display name will be $displayname.</p>\n";
329 $line = $_POST['address'];
330 $postcode = $_POST['postcode'];
331 $q = new AddressQuery;
332 /* XXX: Finding by area properly? */
333 $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
334 if ($address->isNew()) {
335 /* Changing address. */
338 XXX: Check for other contacts at the old address.
339 Make this a new address if there are others, but
340 provide a link to update other contacts.
345 catch (Exception $e) {
346 echo "<p>Error adding $line.</p>\n";
351 $telephone1 = $_POST['telephone1'];
352 $telephone2 = $_POST['telephone2'];
353 $email = $_POST['email'];
355 $contact->setRole($role);
356 $contact->setForename($forename);
357 $contact->setMiddle($middle);
358 $contact->setSurname($surname);
359 $contact->setDisplayname($displayname);
360 $contact->setTelephone1($telephone1);
361 $contact->setTelephone2($telephone2);
362 $contact->setEmail($email);
363 $contact->setAddressId($address->getId());
368 catch (Exception $e) {
369 if ($new) echo "<p>Error adding $displayname.</p>\n";
370 else echo "<p>Error updating $displayname.</p>\n";
377 function add_contact(&$name) {
378 if (! check_admin(1, "add a contact")) return;
380 $area_id = $_POST['area_id'];
381 if (! is_numeric($area_id)) {
382 echo "<p>Invalid area!</p>\n";
386 $area = get_area_by_id($area_id);
388 echo "<p>No such area!</p>\n";
392 $contact = new Contact;
393 if (! update_contact($contact, $area_id, true)) return false;
394 return $contact->getId();
397 function delete_contact($name, $id = null, &$city_id = null) {
398 if (! check_admin(1, "delete a contact")) return;
400 if (isset($id)) $contact = get_contact_by_id($id);
401 else $contact = get_contact_by_name($name);
402 if (! $contact) return false;
404 ///* Remember city ID for dropdown. */
405 //$city_id = $area->getCityId();
409 echo "<p>Deleted contact.</p>\n";
411 catch (Exception $e) {
412 echo "<p>Error deleting $name!</p>\n";
419 function show_contact($name, &$id = null) {
420 if (isset($id)) $contact = get_contact_by_id($id);
421 else $contact = get_contact_by_name($name);
422 if (! $contact) return;
424 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
425 echo "<p>Contact: <span class=\"strong\">" . $contact->getDisplayname() . "</span>";
426 $role = $contact->getRole();
427 $role_string = get_contact_role_string($contact);
428 if ($role_string) echo " $role_string";
429 if ($role & $GLOBALS['ROLE_DONOR']) printf(" <a class=\"small\" href=\"/donation/from/contact/%s/%d\">Donations</a>", urlencode($contact->getDisplayname()), $contact->getId());
430 if ($role & $GLOBALS['ROLE_REQUESTER']) printf(" <a class=\"small\" href=\"/order/from/referrer/%s/%d\">Referred</a>", urlencode($contact->getDisplayname()), $contact->getId());
431 if ($role & $GLOBALS['ROLE_BENEFICIARY']) printf(" <a class=\"small\" href=\"/order/to/beneficiary/%s/%d\">Orders</a>", urlencode($contact->getDisplayname()), $contact->getId());
432 if (check_admin(1)) {
433 echo " " . $contact->getDeleteLink();
435 $city = get_contact_city($contact);
436 if ($city) echo " in " . $city->getLink(get_city_displayname($city));
441 show_contact_form($contact);
443 if (check_admin(1)) {
445 echo " <td colspan=2>";
446 submit("update_contact", "Update");
455 /* /contact/in/area/Cambridge/1 */
456 if (count($parameters)) {
457 if ($parameters[0] == "in") {
458 switch ($parameters[1]) {
460 $area_id = $parameters[3];
461 $_POST['area_id'] = $area_id;
463 $area = $q->findOneById($area_id);
464 $city = get_area_city($area);
465 if ($city) $city_id = $city->getId();
466 show_area_contacts(0, 10, $parameters[2], $area_id);
470 $city_id = $parameters[3];
471 $_POST['city_id'] = $city_id;
473 $city = $q->findOneById($city_id);
474 show_city_contacts(0, 10, $parameters[2], $city_id);
478 show_add_new_contact_form($city_id);
480 else if ($parameters[0] == "search") {
481 search_contacts(0, 10, $parameters[1]);
484 list($name, $id, $args) = parse_parameters($parameters);
485 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
489 delete_contact($name, $id);
493 else if (isset($name)) show_contact($name, $id);
495 /* XXX: Shown after adding. */
496 show_contact_forms($city_id);
497 show_add_new_contact_form($city_id);
500 if (count($parameters)) {
501 show_contact_forms($city_id);