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']) printf(" <a class=\"small\" href=\"/order/to/beneficiary/%s/%d\">Orders</a>", urlencode($contact->getDisplayname()), $contact->getId());
57 echo " " . $contact->getDeleteLink();
59 $area = get_contact_area($contact);
60 echo " in " . $area->getLink();
61 $city = get_contact_city($contact);
62 echo ", " . $city->getLink(get_city_displayname($city));
65 function show_contacts($offset, $per_page, $address_ids) {
66 $q = new ContactQuery;
67 $contacts = $q->filterByAddressId($address_ids)->orderByForename()->orderBySurname()->find();
68 if (count($contacts)) {
69 foreach ($contacts as $contact) show_contact_summary($contact);
74 function search_contacts($offset, $per_page, $search) {
75 $q = new ContactQuery;
76 $contacts = $q->filterByDisplayname("%$search%")->find();
77 echo "<p>Contacts matching '" . htmlspecialchars($search) . "':";
78 if (count($contacts)) {
79 foreach ($contacts as $contact) show_contact_summary($contact);
85 function show_city_contacts($offset, $per_page, $city_name, $city_id = null) {
86 if (isset($city_id)) $city = get_city_by_id($city_id);
87 else if ($city_name) $city = get_city_by_name($city_name);
90 $areas = $q->filterByCityId($city->getId())->find();
92 foreach ($areas as $area) $area_ids[] = $area->getId();
94 $q = new AddressQuery;
95 $addresses = $q->filterByAreaId($area_ids)->find();
96 $address_ids = array();
97 foreach ($addresses as $address) $address_ids[] = $address->getId();
99 echo "<p>Contacts in city " . $city->getLink(get_city_displayname($city)) . ":";
100 return show_contacts($offset, $per_page, $address_ids);
102 else echo "<p>No such city!</p>\n";
105 function show_area_contacts($offset, $per_page, $area_name, $area_id = null) {
106 if (isset($area_id)) $area = get_area_by_id($area_id);
107 else if ($area_name) $area = get_area_by_name($area_name);
109 $q = new AddressQuery;
110 $addresses = $q->filterByAreaId($area->getId())->find();
111 $address_ids = array();
112 foreach ($addresses as $address) $address_ids[] = $address->getId();
114 echo "<p>Contacts in area " . $area->getLink() . ":";
115 return show_contacts($offset, $per_page, $address_ids);
117 else echo "<p>No such area!</p>\n";
120 function show_contact_areas_form($city_id = null) {
121 $areas = get_city_areas($city_id);
122 if (! count($areas)) {
123 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
127 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
128 echo "<p>Show contacts in area\n";
129 echo "<select name=\"area_id\">\n";
130 foreach ($areas as $area) {
131 option("area_id", $area->getId(), get_area_displayname($area));
134 echo "<input type=\"submit\" value=\"Show\">\n";
138 function show_contact_cities_form($city_id = null) {
140 $cities = $q->orderByName()->find();
142 if (! count($cities)) {
143 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
147 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
148 echo "<p>Show contacts in city\n";
149 echo "<select name=\"city_id\">\n";
150 foreach ($cities as $city) {
151 option("city_id", $city->getId(), get_city_displayname($city), $city_id);
154 echo "<input type=\"submit\" value=\"Show\">\n";
158 function show_contact_search_form() {
159 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
160 echo "<p>Search for contacts:";
161 input("search_contact");
162 echo "<input type=\"submit\" value=\"Search\">\n";
166 function show_contact_forms($city_id) {
167 show_contact_areas_form($city_id);
168 show_contact_cities_form($city_id);
169 show_contact_search_form();
172 function show_contact_role_form($role) {
173 return show_role_form($role, $GLOBALS['contact_roles']);
176 function show_contact_form($contact = null) {
177 global $contact_roles;
179 if (! $contact) $contact = new Contact;
183 echo " <td>Role</td>\n";
184 echo " <td>"; show_contact_role_form($contact->getRole()); echo "</td>\n";
189 echo " <td>Forename</td>\n";
190 echo " <td>"; input("forename", $contact->getForename()); echo "</td>\n";
195 echo " <td>Middle name(s)</td>\n";
196 echo " <td>"; input("middle", $contact->getMiddle()); echo "</td>\n";
201 echo " <td>Surname</td>\n";
202 echo " <td>"; input("surname", $contact->getSurname()); echo "</td>\n";
207 echo " <td>Display name (if not concatenation of above)</td>\n";
208 echo " <td>"; input("displayname", $contact->getDisplayname()); echo "</td>\n";
212 $address = get_contact_address($contact);
213 if (! $address) $address = new Address;
215 echo " <td>Address</td>\n";
216 echo " <td>"; textarea("address", $address->getLine()); echo "</td>\n";
221 echo " <td>Postcode</td>\n";
222 echo " <td>"; input("postcode", $address->getPostcode()); echo "</td>\n";
227 echo " <td>Telephone</td>\n";
228 echo " <td>"; input("telephone1", $contact->getTelephone1()); echo "</td>\n";
231 echo " <td>Alternative telephone</td>\n";
232 echo " <td>"; input("telephone2", $contact->getTelephone2()); echo "</td>\n";
237 echo " <td>Email</td>\n";
238 echo " <td>"; input("email", $contact->getEmail()); echo "</td>\n";
242 $area = get_contact_area($contact);
243 if ($area) $area_id = $area->getId();
245 echo " <td>Area</td>\n";
246 echo " <td><select name=\"area_id\">\n";
247 $areas = get_city_areas();
248 foreach ($areas as $area) {
249 option("area_id", $area->getId(), get_area_displayname($area), $area_id);
251 echo " </select></td>\n";
256 echo " <td>Notes</td>\n";
257 echo " <td><textarea name=\"notes\">" . $contact->getNotes() . "</textarea></td>\n";
261 function show_new_contact_form($city_id = null) {
262 if (! check_admin(1)) return;
264 $areas = get_city_areas($city_id);
265 if (! count($areas)) {
266 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
270 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
271 echo "<p>Add a new contact:</p>\n";
274 show_contact_form($contact);
277 echo " <td colspan=2>"; submit("add_contact", "Add"); echo "</td></tr>\n";
283 function show_add_new_contact_form() {
284 if (! check_admin(1)) return;
287 $cities = $q->find();
288 if (! count($cities)) {
289 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
293 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
294 echo "<p>Add a new contact in <select name=\"city_id\">\n";
295 foreach ($cities as $city) {
296 option("city_id", $city->getId(), get_city_displayname($city));
299 submit("show_add_contact", "Proceed");
304 function update_contact(&$contact, $area_id, $new = false) {
305 global $contact_roles;
308 for ($i = 0; $i < count($contact_roles); $i++) {
309 if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
312 /* Staff can place orders. */
313 if ($role & (1 << 0)) $role |= (1 << 2);
315 $forename = $_POST['forename'];
316 $middle = $_POST['middle'];
317 $surname = $_POST['surname'];
318 $displayname = $_POST['displayname'];
320 if (! $forename && ! $surname) {
321 echo "<p>Must have either a forename or surname!</p>\n";
324 if ($middle && ! ($forename && $surname)) {
325 echo "<p>Must have both a forename or surname for middle name(s) to make sense!</p>\n";
329 if (! $displayname) {
330 $displayname = $forename;
331 if ($middle) $displayname .= " $middle";
332 if ($forename) $displayname .= " ";
333 $displayname .= $surname;
334 echo "<p>Display name will be $displayname.</p>\n";
338 $line = $_POST['address'];
339 $postcode = $_POST['postcode'];
340 $q = new AddressQuery;
341 /* XXX: Finding by area properly? */
342 $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
343 if ($address->isNew()) {
344 /* Changing address. */
347 XXX: Check for other contacts at the old address.
348 Make this a new address if there are others, but
349 provide a link to update other contacts.
354 catch (Exception $e) {
355 echo "<p>Error adding $line.</p>\n";
360 $telephone1 = $_POST['telephone1'];
361 $telephone2 = $_POST['telephone2'];
362 $email = $_POST['email'];
363 $notes = $_POST['notes'];
365 $contact->setRole($role);
366 $contact->setForename($forename);
367 $contact->setMiddle($middle);
368 $contact->setSurname($surname);
369 $contact->setDisplayname($displayname);
370 $contact->setTelephone1($telephone1);
371 $contact->setTelephone2($telephone2);
372 $contact->setEmail($email);
373 $contact->setNotes($notes);
374 $contact->setAddressId($address->getId());
379 catch (Exception $e) {
380 if ($new) echo "<p>Error adding $displayname.</p>\n";
381 else echo "<p>Error updating $displayname.</p>\n";
388 function add_contact(&$name) {
389 if (! check_admin(1, "add a contact")) return;
391 $area_id = $_POST['area_id'];
392 if (! is_numeric($area_id)) {
393 echo "<p>Invalid area!</p>\n";
397 $area = get_area_by_id($area_id);
399 echo "<p>No such area!</p>\n";
403 $contact = new Contact;
404 if (! update_contact($contact, $area_id, true)) return false;
405 return $contact->getId();
408 function delete_contact($name, $id = null, &$city_id = null) {
409 if (! check_admin(1, "delete a contact")) return;
411 if (isset($id)) $contact = get_contact_by_id($id);
412 else $contact = get_contact_by_name($name);
413 if (! $contact) return false;
415 ///* Remember city ID for dropdown. */
416 //$city_id = $area->getCityId();
420 echo "<p>Deleted contact.</p>\n";
422 catch (Exception $e) {
423 echo "<p>Error deleting $name!</p>\n";
430 function show_contact($name, &$id = null) {
431 if (isset($id)) $contact = get_contact_by_id($id);
432 else $contact = get_contact_by_name($name);
433 if (! $contact) return;
435 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
436 show_contact_summary($contact, true);
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);