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, $new = false) {
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";
190 echo " <td>Registered</td>\n";
191 echo " <td>" . $contact->getAdded() . "</td>\n";
197 echo " <td>Forename</td>\n";
198 echo " <td>"; input("forename", $contact->getForename()); echo "</td>\n";
203 echo " <td>Middle name(s)</td>\n";
204 echo " <td>"; input("middle", $contact->getMiddle()); echo "</td>\n";
209 echo " <td>Surname</td>\n";
210 echo " <td>"; input("surname", $contact->getSurname()); echo "</td>\n";
215 echo " <td>Display name (if not concatenation of above)</td>\n";
216 echo " <td>"; input("displayname", $contact->getDisplayname()); echo "</td>\n";
220 $address = get_contact_address($contact);
221 if (! $address) $address = new Address;
223 echo " <td>Address</td>\n";
224 echo " <td>"; textarea("address", $address->getLine()); echo "</td>\n";
229 echo " <td>Postcode</td>\n";
230 echo " <td>"; input("postcode", $address->getPostcode()); echo "</td>\n";
235 echo " <td>Telephone</td>\n";
236 echo " <td>"; input("telephone1", $contact->getTelephone1()); echo "</td>\n";
239 echo " <td>Alternative telephone</td>\n";
240 echo " <td>"; input("telephone2", $contact->getTelephone2()); echo "</td>\n";
245 echo " <td>Email</td>\n";
246 echo " <td>"; input("email", $contact->getEmail()); echo "</td>\n";
250 $area = get_contact_area($contact);
251 if ($area) $area_id = $area->getId();
253 echo " <td>Area</td>\n";
254 echo " <td><select name=\"area_id\">\n";
255 $areas = get_city_areas();
256 foreach ($areas as $area) {
257 option("area_id", $area->getId(), get_area_displayname($area), $area_id);
259 echo " </select></td>\n";
264 echo " <td>Notes</td>\n";
265 echo " <td><textarea name=\"notes\">" . $contact->getNotes() . "</textarea></td>\n";
269 function show_new_contact_form($city_id = null) {
270 if (! check_admin(1)) return;
272 $areas = get_city_areas($city_id);
273 if (! count($areas)) {
274 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
278 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
279 echo "<p>Add a new contact:</p>\n";
282 show_contact_form($contact, true);
285 echo " <td colspan=2>"; submit("add_contact", "Add"); echo "</td></tr>\n";
291 function show_add_new_contact_form() {
292 if (! check_admin(1)) return;
295 $cities = $q->find();
296 if (! count($cities)) {
297 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
301 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
302 echo "<p>Add a new contact in <select name=\"city_id\">\n";
303 foreach ($cities as $city) {
304 option("city_id", $city->getId(), get_city_displayname($city));
307 submit("show_add_contact", "Proceed");
312 function update_contact(&$contact, $area_id, $new = false) {
313 global $contact_roles;
316 for ($i = 0; $i < count($contact_roles); $i++) {
317 if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
320 /* Staff can place orders. */
321 if ($role & (1 << 0)) $role |= (1 << 2);
323 $forename = $_POST['forename'];
324 $middle = $_POST['middle'];
325 $surname = $_POST['surname'];
326 $displayname = $_POST['displayname'];
328 if (! $forename && ! $surname) {
329 echo "<p>Must have either a forename or surname!</p>\n";
332 if ($middle && ! ($forename && $surname)) {
333 echo "<p>Must have both a forename or surname for middle name(s) to make sense!</p>\n";
337 if (! $displayname) {
338 $displayname = $forename;
339 if ($middle) $displayname .= " $middle";
340 if ($forename) $displayname .= " ";
341 $displayname .= $surname;
342 echo "<p>Display name will be $displayname.</p>\n";
346 $line = $_POST['address'];
347 $postcode = $_POST['postcode'];
348 $q = new AddressQuery;
349 /* XXX: Finding by area properly? */
350 $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
351 if ($address->isNew()) {
352 /* Changing address. */
355 XXX: Check for other contacts at the old address.
356 Make this a new address if there are others, but
357 provide a link to update other contacts.
362 catch (Exception $e) {
363 echo "<p>Error adding $line.</p>\n";
368 $telephone1 = $_POST['telephone1'];
369 $telephone2 = $_POST['telephone2'];
370 $email = $_POST['email'];
371 $notes = $_POST['notes'];
373 $contact->setRole($role);
374 $contact->setForename($forename);
375 $contact->setMiddle($middle);
376 $contact->setSurname($surname);
377 $contact->setDisplayname($displayname);
378 $contact->setTelephone1($telephone1);
379 $contact->setTelephone2($telephone2);
380 $contact->setEmail($email);
381 $contact->setNotes($notes);
382 $contact->setAddressId($address->getId());
387 catch (Exception $e) {
388 if ($new) echo "<p>Error adding $displayname.</p>\n";
389 else echo "<p>Error updating $displayname.</p>\n";
396 function add_contact(&$name) {
397 if (! check_admin(1, "add a contact")) return;
399 $area_id = $_POST['area_id'];
400 if (! is_numeric($area_id)) {
401 echo "<p>Invalid area!</p>\n";
405 $area = get_area_by_id($area_id);
407 echo "<p>No such area!</p>\n";
411 $contact = new Contact;
412 if (! update_contact($contact, $area_id, true)) return false;
413 return $contact->getId();
416 function delete_contact($name, $id = null, &$city_id = null) {
417 if (! check_admin(1, "delete a contact")) return;
419 if (isset($id)) $contact = get_contact_by_id($id);
420 else $contact = get_contact_by_name($name);
421 if (! $contact) return false;
423 ///* Remember city ID for dropdown. */
424 //$city_id = $area->getCityId();
428 echo "<p>Deleted contact.</p>\n";
430 catch (Exception $e) {
431 echo "<p>Error deleting $name!</p>\n";
438 function show_contact($name, &$id = null) {
439 if (isset($id)) $contact = get_contact_by_id($id);
440 else $contact = get_contact_by_name($name);
441 if (! $contact) return;
443 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
444 show_contact_summary($contact, true);
449 show_contact_form($contact);
451 if (check_admin(1)) {
453 echo " <td colspan=2>";
454 submit("update_contact", "Update");
463 /* /contact/in/area/Cambridge/1 */
464 if (count($parameters)) {
465 if ($parameters[0] == "in") {
466 switch ($parameters[1]) {
468 $area_id = $parameters[3];
469 $_POST['area_id'] = $area_id;
471 $area = $q->findOneById($area_id);
472 $city = get_area_city($area);
473 if ($city) $city_id = $city->getId();
474 show_area_contacts(0, 10, $parameters[2], $area_id);
478 $city_id = $parameters[3];
479 $_POST['city_id'] = $city_id;
481 $city = $q->findOneById($city_id);
482 show_city_contacts(0, 10, $parameters[2], $city_id);
486 show_add_new_contact_form($city_id);
488 else if ($parameters[0] == "search") {
489 search_contacts(0, 10, $parameters[1]);
492 list($name, $id, $args) = parse_parameters($parameters);
493 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
497 delete_contact($name, $id);
501 else if (isset($name)) show_contact($name, $id);
503 /* XXX: Shown after adding. */
504 show_contact_forms($city_id);
505 show_add_new_contact_form($city_id);
508 if (count($parameters)) {
509 show_contact_forms($city_id);