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";
255 function show_new_contact_form($city_id = null) {
256 if (! check_admin(1)) return;
258 $areas = get_city_areas($city_id);
259 if (! count($areas)) {
260 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
264 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
265 echo "<p>Add a new contact:</p>\n";
268 show_contact_form($contact);
271 echo " <td colspan=2>"; submit("add_contact", "Add"); echo "</td></tr>\n";
277 function show_add_new_contact_form() {
278 if (! check_admin(1)) return;
281 $cities = $q->find();
282 if (! count($cities)) {
283 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
287 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
288 echo "<p>Add a new contact in <select name=\"city_id\">\n";
289 foreach ($cities as $city) {
290 option("city_id", $city->getId(), get_city_displayname($city));
293 submit("show_add_contact", "Proceed");
298 function update_contact(&$contact, $area_id, $new = false) {
299 global $contact_roles;
302 for ($i = 0; $i < count($contact_roles); $i++) {
303 if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
306 /* Staff can place orders. */
307 if ($role & (1 << 0)) $role |= (1 << 2);
309 $forename = $_POST['forename'];
310 $middle = $_POST['middle'];
311 $surname = $_POST['surname'];
312 $displayname = $_POST['displayname'];
314 if (! $forename && ! $surname) {
315 echo "<p>Must have either a forename or surname!</p>\n";
318 if ($middle && ! ($forename && $surname)) {
319 echo "<p>Must have both a forename or surname for middle name(s) to make sense!</p>\n";
323 if (! $displayname) {
324 $displayname = $forename;
325 if ($middle) $displayname .= " $middle";
326 if ($forename) $displayname .= " ";
327 $displayname .= $surname;
328 echo "<p>Display name will be $displayname.</p>\n";
332 $line = $_POST['address'];
333 $postcode = $_POST['postcode'];
334 $q = new AddressQuery;
335 /* XXX: Finding by area properly? */
336 $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
337 if ($address->isNew()) {
338 /* Changing address. */
341 XXX: Check for other contacts at the old address.
342 Make this a new address if there are others, but
343 provide a link to update other contacts.
348 catch (Exception $e) {
349 echo "<p>Error adding $line.</p>\n";
354 $telephone1 = $_POST['telephone1'];
355 $telephone2 = $_POST['telephone2'];
356 $email = $_POST['email'];
358 $contact->setRole($role);
359 $contact->setForename($forename);
360 $contact->setMiddle($middle);
361 $contact->setSurname($surname);
362 $contact->setDisplayname($displayname);
363 $contact->setTelephone1($telephone1);
364 $contact->setTelephone2($telephone2);
365 $contact->setEmail($email);
366 $contact->setAddressId($address->getId());
371 catch (Exception $e) {
372 if ($new) echo "<p>Error adding $displayname.</p>\n";
373 else echo "<p>Error updating $displayname.</p>\n";
380 function add_contact(&$name) {
381 if (! check_admin(1, "add a contact")) return;
383 $area_id = $_POST['area_id'];
384 if (! is_numeric($area_id)) {
385 echo "<p>Invalid area!</p>\n";
389 $area = get_area_by_id($area_id);
391 echo "<p>No such area!</p>\n";
395 $contact = new Contact;
396 if (! update_contact($contact, $area_id, true)) return false;
397 return $contact->getId();
400 function delete_contact($name, $id = null, &$city_id = null) {
401 if (! check_admin(1, "delete a contact")) return;
403 if (isset($id)) $contact = get_contact_by_id($id);
404 else $contact = get_contact_by_name($name);
405 if (! $contact) return false;
407 ///* Remember city ID for dropdown. */
408 //$city_id = $area->getCityId();
412 echo "<p>Deleted contact.</p>\n";
414 catch (Exception $e) {
415 echo "<p>Error deleting $name!</p>\n";
422 function show_contact($name, &$id = null) {
423 if (isset($id)) $contact = get_contact_by_id($id);
424 else $contact = get_contact_by_name($name);
425 if (! $contact) return;
427 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
428 show_contact_summary($contact, true);
433 show_contact_form($contact);
435 if (check_admin(1)) {
437 echo " <td colspan=2>";
438 submit("update_contact", "Update");
447 /* /contact/in/area/Cambridge/1 */
448 if (count($parameters)) {
449 if ($parameters[0] == "in") {
450 switch ($parameters[1]) {
452 $area_id = $parameters[3];
453 $_POST['area_id'] = $area_id;
455 $area = $q->findOneById($area_id);
456 $city = get_area_city($area);
457 if ($city) $city_id = $city->getId();
458 show_area_contacts(0, 10, $parameters[2], $area_id);
462 $city_id = $parameters[3];
463 $_POST['city_id'] = $city_id;
465 $city = $q->findOneById($city_id);
466 show_city_contacts(0, 10, $parameters[2], $city_id);
470 show_add_new_contact_form($city_id);
472 else if ($parameters[0] == "search") {
473 search_contacts(0, 10, $parameters[1]);
476 list($name, $id, $args) = parse_parameters($parameters);
477 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
481 delete_contact($name, $id);
485 else if (isset($name)) show_contact($name, $id);
487 /* XXX: Shown after adding. */
488 show_contact_forms($city_id);
489 show_add_new_contact_form($city_id);
492 if (count($parameters)) {
493 show_contact_forms($city_id);