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) {
172 for ($i = 0; $i < count($roles); $i++) {
173 echo " <input type=\"checkbox\" name=\"role_$i\"";
174 if ($role & (1 << $i)) echo " checked";
175 echo ">$roles[$i]\n";
179 function show_contact_form($contact = null) {
182 if (! $contact) $contact = new Contact;
186 echo " <td>Role</td>\n";
187 echo " <td>"; show_contact_role_form($contact->getRole()); echo "</td>\n";
192 echo " <td>Forename</td>\n";
193 echo " <td>"; input("forename", $contact->getForename()); echo "</td>\n";
198 echo " <td>Middle name(s)</td>\n";
199 echo " <td>"; input("middle", $contact->getMiddle()); echo "</td>\n";
204 echo " <td>Surname</td>\n";
205 echo " <td>"; input("surname", $contact->getSurname()); echo "</td>\n";
210 echo " <td>Display name (if not concatenation of above)</td>\n";
211 echo " <td>"; input("displayname", $contact->getDisplayname()); echo "</td>\n";
215 $address = get_contact_address($contact);
216 if (! $address) $address = new Address;
218 echo " <td>Address</td>\n";
219 echo " <td>"; textarea("address", $address->getLine()); echo "</td>\n";
224 echo " <td>Postcode</td>\n";
225 echo " <td>"; input("postcode", $address->getPostcode()); echo "</td>\n";
230 echo " <td>Telephone</td>\n";
231 echo " <td>"; input("telephone1", $contact->getTelephone1()); echo "</td>\n";
234 echo " <td>Alternative telephone</td>\n";
235 echo " <td>"; input("telephone2", $contact->getTelephone2()); echo "</td>\n";
240 echo " <td>Email</td>\n";
241 echo " <td>"; input("email", $contact->getEmail()); echo "</td>\n";
245 $area = get_contact_area($contact);
246 if ($area) $area_id = $area->getId();
248 echo " <td>Area</td>\n";
249 echo " <td><select name=\"area_id\">\n";
250 $areas = get_city_areas();
251 foreach ($areas as $area) {
252 option("area_id", $area->getId(), get_area_displayname($area), $area_id);
254 echo " </select></td>\n";
258 function show_new_contact_form($city_id = null) {
259 if (! check_admin(1)) return;
261 $areas = get_city_areas($city_id);
262 if (! count($areas)) {
263 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
267 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
268 echo "<p>Add a new contact:</p>\n";
271 show_contact_form($contact);
274 echo " <td colspan=2>"; submit("add_contact", "Add"); echo "</td></tr>\n";
280 function show_add_new_contact_form() {
281 if (! check_admin(1)) return;
284 $cities = $q->find();
285 if (! count($cities)) {
286 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
290 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
291 echo "<p>Add a new contact in <select name=\"city_id\">\n";
292 foreach ($cities as $city) {
293 option("city_id", $city->getId(), get_city_displayname($city));
296 submit("show_add_contact", "Proceed");
301 function update_contact(&$contact, $area_id, $new = false) {
305 for ($i = 0; $i < count($roles); $i++) {
306 if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
309 /* Staff can place orders. */
310 if ($role & (1 << 0)) $role |= (1 << 2);
312 $forename = $_POST['forename'];
313 $middle = $_POST['middle'];
314 $surname = $_POST['surname'];
315 $displayname = $_POST['displayname'];
317 if (! $forename && ! $surname) {
318 echo "<p>Must have either a forename or surname!</p>\n";
321 if ($middle && ! ($forename && $surname)) {
322 echo "<p>Must have both a forename or surname for middle name(s) to make sense!</p>\n";
326 if (! $displayname) {
327 $displayname = $forename;
328 if ($middle) $displayname .= " $middle";
329 if ($forename) $displayname .= " ";
330 $displayname .= $surname;
331 echo "<p>Display name will be $displayname.</p>\n";
335 $line = $_POST['address'];
336 $postcode = $_POST['postcode'];
337 $q = new AddressQuery;
338 /* XXX: Finding by area properly? */
339 $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
340 if ($address->isNew()) {
341 /* Changing address. */
344 XXX: Check for other contacts at the old address.
345 Make this a new address if there are others, but
346 provide a link to update other contacts.
351 catch (Exception $e) {
352 echo "<p>Error adding $line.</p>\n";
357 $telephone1 = $_POST['telephone1'];
358 $telephone2 = $_POST['telephone2'];
359 $email = $_POST['email'];
361 $contact->setRole($role);
362 $contact->setForename($forename);
363 $contact->setMiddle($middle);
364 $contact->setSurname($surname);
365 $contact->setDisplayname($displayname);
366 $contact->setTelephone1($telephone1);
367 $contact->setTelephone2($telephone2);
368 $contact->setEmail($email);
369 $contact->setAddressId($address->getId());
374 catch (Exception $e) {
375 if ($new) echo "<p>Error adding $displayname.</p>\n";
376 else echo "<p>Error updating $displayname.</p>\n";
383 function add_contact(&$name) {
384 if (! check_admin(1, "add a contact")) return;
386 $area_id = $_POST['area_id'];
387 if (! is_numeric($area_id)) {
388 echo "<p>Invalid area!</p>\n";
392 $area = get_area_by_id($area_id);
394 echo "<p>No such area!</p>\n";
398 $contact = new Contact;
399 if (! update_contact($contact, $area_id, true)) return false;
400 return $contact->getId();
403 function delete_contact($name, $id = null, &$city_id = null) {
404 if (! check_admin(1, "delete a contact")) return;
406 if (isset($id)) $contact = get_contact_by_id($id);
407 else $contact = get_contact_by_name($name);
408 if (! $contact) return false;
410 ///* Remember city ID for dropdown. */
411 //$city_id = $area->getCityId();
415 echo "<p>Deleted contact.</p>\n";
417 catch (Exception $e) {
418 echo "<p>Error deleting $name!</p>\n";
425 function show_contact($name, &$id = null) {
426 if (isset($id)) $contact = get_contact_by_id($id);
427 else $contact = get_contact_by_name($name);
428 if (! $contact) return;
430 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
431 echo "<p>Contact: <span class=\"strong\">" . $contact->getDisplayname() . "</span>";
432 $role = $contact->getRole();
433 $role_string = get_contact_role_string($contact);
434 if ($role_string) echo " $role_string";
435 if ($role & $GLOBALS['ROLE_DONOR']) printf(" <a class=\"small\" href=\"/donation/from/contact/%s/%d\">Donations</a>", urlencode($contact->getDisplayname()), $contact->getId());
436 if ($role & $GLOBALS['ROLE_REQUESTER']) printf(" <a class=\"small\" href=\"/order/from/referrer/%s/%d\">Referred</a>", urlencode($contact->getDisplayname()), $contact->getId());
437 if ($role & $GLOBALS['ROLE_BENEFICIARY']) printf(" <a class=\"small\" href=\"/order/to/beneficiary/%s/%d\">Orders</a>", urlencode($contact->getDisplayname()), $contact->getId());
438 if (check_admin(1)) {
439 echo " " . $contact->getDeleteLink();
441 $city = get_contact_city($contact);
442 if ($city) echo " in " . $city->getLink(get_city_displayname($city));
447 show_contact_form($contact);
449 if (check_admin(1)) {
451 echo " <td colspan=2>";
452 submit("update_contact", "Update");
461 /* /contact/in/area/Cambridge/1 */
462 if (count($parameters)) {
463 if ($parameters[0] == "in") {
464 switch ($parameters[1]) {
466 $area_id = $parameters[3];
467 $_POST['area_id'] = $area_id;
469 $area = $q->findOneById($area_id);
470 $city = get_area_city($area);
471 if ($city) $city_id = $city->getId();
472 show_area_contacts(0, 10, $parameters[2], $area_id);
476 $city_id = $parameters[3];
477 $_POST['city_id'] = $city_id;
479 $city = $q->findOneById($city_id);
480 show_city_contacts(0, 10, $parameters[2], $city_id);
484 show_add_new_contact_form($city_id);
486 else if ($parameters[0] == "search") {
487 search_contacts(0, 10, $parameters[1]);
490 list($name, $id, $args) = parse_parameters($parameters);
491 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
495 delete_contact($name, $id);
499 else if (isset($name)) show_contact($name, $id);
501 /* XXX: Shown after adding. */
502 show_contact_forms($city_id);
503 show_add_new_contact_form($city_id);
506 if (count($parameters)) {
507 show_contact_forms($city_id);