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 $d = urlencode($contact->getDisplayname());
54 $i = $contact->getId();
55 if ($role & $GLOBALS['ROLE_DONOR']) echo " " . get_small_link("Donations", "/donation/from/contact/%s/%d", $d, $i);
56 if ($role & $GLOBALS['ROLE_REQUESTER']) echo " " . get_small_link("Referred", "/order/from/referrer/%s/%d", $d, $i);
57 if ($role & $GLOBALS['ROLE_BENEFICIARY']) {
58 echo " " . get_small_link("Orders", "/order/to/beneficiary/%s/%d", $d, $contact->getId());
59 if (get_contact_area($contact)) echo " " . get_small_link("Place", "/order/place/for/beneficiary/%s/%d", $d, $i);
62 echo " " . $contact->getDeleteLink();
64 $area = get_contact_area($contact);
65 echo " in " . $area->getLink();
66 $city = get_contact_city($contact);
67 echo ", " . $city->getLink(get_city_displayname($city));
70 function show_contacts($offset, $per_page, $address_ids) {
71 $q = new ContactQuery;
72 $contacts = $q->filterByAddressId($address_ids)->orderByForename()->orderBySurname()->find();
73 if (count($contacts)) {
74 foreach ($contacts as $contact) show_contact_summary($contact);
79 function search_contacts($offset, $per_page, $search) {
80 $q = new ContactQuery;
81 $contacts = $q->filterByDisplayname("%$search%")->find();
82 echo "<p>Contacts matching '" . htmlspecialchars($search) . "':";
83 if (count($contacts)) {
84 foreach ($contacts as $contact) show_contact_summary($contact);
90 function show_city_contacts($offset, $per_page, $city_name, $city_id = null) {
91 if (isset($city_id)) $city = get_city_by_id($city_id);
92 else if ($city_name) $city = get_city_by_name($city_name);
95 $areas = $q->filterByCityId($city->getId())->find();
97 foreach ($areas as $area) $area_ids[] = $area->getId();
99 $q = new AddressQuery;
100 $addresses = $q->filterByAreaId($area_ids)->find();
101 $address_ids = array();
102 foreach ($addresses as $address) $address_ids[] = $address->getId();
104 echo "<p>Contacts in city " . $city->getLink(get_city_displayname($city)) . ":";
105 return show_contacts($offset, $per_page, $address_ids);
107 else echo "<p>No such city!</p>\n";
110 function show_area_contacts($offset, $per_page, $area_name, $area_id = null) {
111 if (isset($area_id)) $area = get_area_by_id($area_id);
112 else if ($area_name) $area = get_area_by_name($area_name);
114 $q = new AddressQuery;
115 $addresses = $q->filterByAreaId($area->getId())->find();
116 $address_ids = array();
117 foreach ($addresses as $address) $address_ids[] = $address->getId();
119 echo "<p>Contacts in area " . $area->getLink() . ":";
120 return show_contacts($offset, $per_page, $address_ids);
122 else echo "<p>No such area!</p>\n";
125 function show_contact_areas_form($city_id = null) {
126 $areas = get_city_areas($city_id);
127 if (! count($areas)) {
128 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
132 $candidates = array();
133 foreach ($areas as $area) {
134 if (! count(get_area_contacts($area->getId()))) continue;
135 $candidates[] = $area;
137 if (! count($candidates)) return;
139 echo "<p>Show contacts in area\n";
140 echo "<select name=\"area_id\">\n";
141 foreach ($candidates as $area) {
142 option("area_id", $area->getId(), get_area_displayname($area));
145 echo "<input type=\"submit\" value=\"Show\">\n";
148 function show_contact_cities_form($city_id = null) {
150 $cities = $q->orderByName()->find();
152 if (! count($cities)) {
153 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
157 $candidates = array();
158 foreach ($cities as $city) {
159 if (! count(get_city_contacts($city->getId()))) continue;
160 $candidates[] = $city;
162 if (! count($candidates)) return;
164 echo "<p>Show contacts in city\n";
165 echo "<select name=\"city_id\">\n";
166 foreach ($candidates as $city) {
167 option("city_id", $city->getId(), get_city_displayname($city), $city_id);
170 echo "<input type=\"submit\" value=\"Show\">\n";
173 function show_contact_search_form() {
174 echo "<p>Search for contacts:";
175 input("search_contact");
176 echo "<input type=\"submit\" value=\"Search\">\n";
179 function show_contact_forms($city_id) {
180 form("noprint standout");
181 show_contact_areas_form($city_id);
182 show_contact_cities_form($city_id);
183 show_contact_search_form();
187 function show_contact_role_form($role) {
188 return show_role_form($role, $GLOBALS['contact_roles']);
191 function show_contact_form($contact = null, $new = false) {
192 global $contact_roles, $parcel_sizes, $parcel_contents;
194 if (! $contact) $contact = new Contact;
195 else if ($contact->getRole() & ($GLOBALS['ROLE_BENEFICIARY'] | $GLOBALS['ROLE_REQUESTER'])) {
196 $state_mask = $GLOBALS['STATE_ANY'];
197 $state_mask &= ~$GLOBALS['STATE_DELIVERED'];
198 $state_mask &= ~$GLOBALS['STATE_CANCELLED'];
200 $orders = get_contact_orders($contact, $state_mask);
202 if (count($orders)) {
204 echo " <td colspan=2><strong>Outstanding orders:</strong></td>\n";
208 echo " <td colspan=2 class=\"history\">\n";
209 foreach ($orders as $order) {
210 echo " " . get_order_summary($order) . "<br>\n";
219 echo " <td>Role</td>\n";
220 echo " <td>"; show_contact_role_form($contact->getRole()); echo "</td>\n";
226 echo " <td>Registered</td>\n";
227 echo " <td>" . $contact->getAdded() . "</td>\n";
233 echo " <td>Forename</td>\n";
234 echo " <td>"; input("forename", $contact->getForename()); echo "</td>\n";
239 echo " <td>Middle name(s)</td>\n";
240 echo " <td>"; input("middle", $contact->getMiddle()); echo "</td>\n";
245 echo " <td>Surname</td>\n";
246 echo " <td>"; input("surname", $contact->getSurname()); echo "</td>\n";
251 echo " <td>Display name (if not concatenation of above)</td>\n";
252 echo " <td>"; input("displayname", $contact->getDisplayname()); echo "</td>\n";
256 $address = get_contact_address($contact);
257 if (! $address) $address = new Address;
259 echo " <td>Address</td>\n";
260 echo " <td>"; textarea("address", $address->getLine()); echo "</td>\n";
265 echo " <td>Postcode</td>\n";
266 echo " <td>"; input("postcode", $address->getPostcode()); echo "</td>\n";
271 echo " <td>Telephone</td>\n";
272 echo " <td>"; input("telephone1", $contact->getTelephone1()); echo "</td>\n";
275 echo " <td>Alternative telephone</td>\n";
276 echo " <td>"; input("telephone2", $contact->getTelephone2()); echo "</td>\n";
281 echo " <td>Email</td>\n";
282 echo " <td>"; input("email", $contact->getEmail()); echo "</td>\n";
286 $area = get_contact_area($contact);
287 if ($area) $area_id = $area->getId();
289 echo " <td>Area</td>\n";
290 echo " <td><select name=\"area_id\">\n";
291 $areas = get_city_areas();
292 foreach ($areas as $area) {
293 option("area_id", $area->getId(), get_area_displayname($area), $area_id);
295 echo " </select></td>\n";
300 echo " <td>Family unit</td>\n";
301 echo " <td><select name=\"parcel_size\">\n";
302 $mask = 1 << count($parcel_sizes);
303 for ($i = 0; $i < count($parcel_sizes); $i++) {
304 option("parcel_size", 1 << $i, $parcel_sizes[$i], $contact->getParcel() % $mask);
306 echo "</select></td>\n";
309 /* Parcel contents. */
311 echo " <td>Dietary requirements</td>\n";
313 for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
314 if (1 << $i == $GLOBALS['PARCEL_TOILETRY']) continue;
315 echo " <input type=\"checkbox\" name=\"parcel_$i\"";
316 if ($contact->getParcel() & (1 << $i)) echo " checked";
317 echo ">$parcel_contents[$i]\n";
324 echo " <td>Notes</td>\n";
325 echo " <td>"; textarea("notes", $contact->getNotes()); echo "</td>\n";
329 function show_new_contact_form($city_id = null) {
330 if (! check_admin(1)) return;
332 $areas = get_city_areas($city_id);
333 if (! count($areas)) {
334 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
339 echo "<p>Add a new contact:</p>\n";
342 show_contact_form($contact, true);
345 echo " <td colspan=2>"; submit("add_contact", "Add"); echo "</td></tr>\n";
351 function show_add_new_contact_form() {
352 if (! check_admin(1)) return;
355 $cities = $q->find();
356 if (! count($cities)) {
357 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
361 form("noprint standout");
362 echo "<p>Add a new contact in <select name=\"city_id\">\n";
363 foreach ($cities as $city) {
364 option("city_id", $city->getId(), get_city_displayname($city));
367 submit("show_add_contact", "Proceed");
372 function update_contact(&$contact, $area_id, $new = false) {
373 global $contact_roles, $parcel_sizes, $parcel_contents;
376 for ($i = 0; $i < count($contact_roles); $i++) {
377 if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
380 /* Staff can place orders. */
381 if ($role & (1 << 0)) $role |= (1 << 2);
383 $forename = $_POST['forename'];
384 $middle = $_POST['middle'];
385 $surname = $_POST['surname'];
386 $displayname = $_POST['displayname'];
388 if (! $forename && ! $surname) {
389 echo "<p>Must have either a forename or surname!</p>\n";
392 if ($middle && ! ($forename && $surname)) {
393 echo "<p>Must have both a forename or surname for middle name(s) to make sense!</p>\n";
397 if (! $displayname) {
398 $displayname = $forename;
399 if ($middle) $displayname .= " $middle";
400 if ($forename) $displayname .= " ";
401 $displayname .= $surname;
402 echo "<p>Display name will be $displayname.</p>\n";
406 $area_id = $_POST['area_id'];
407 $line = $_POST['address'];
408 $postcode = $_POST['postcode'];
409 $q = new AddressQuery;
410 /* XXX: Finding by area properly? */
411 $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
412 if ($address->isNew()) {
413 /* Changing address. */
416 XXX: Check for other contacts at the old address.
417 Make this a new address if there are others, but
418 provide a link to update other contacts.
423 catch (Exception $e) {
424 echo "<p>Error adding $line.</p>\n";
429 $telephone1 = $_POST['telephone1'];
430 $telephone2 = $_POST['telephone2'];
431 $email = $_POST['email'];
432 $parcel = $_POST['parcel_size'];
433 for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
434 if ($_POST['parcel_' . $i] == "on") $parcel |= (1 << $i);
436 $notes = $_POST['notes'];
438 $contact->setRole($role);
439 $contact->setForename($forename);
440 $contact->setMiddle($middle);
441 $contact->setSurname($surname);
442 $contact->setDisplayname($displayname);
443 $contact->setTelephone1($telephone1);
444 $contact->setTelephone2($telephone2);
445 $contact->setEmail($email);
446 $contact->setParcel($parcel);
447 $contact->setNotes($notes);
448 $contact->setAddressId($address->getId());
453 catch (Exception $e) {
454 if ($new) echo "<p>Error adding $displayname.</p>\n";
455 else echo "<p>Error updating $displayname.</p>\n";
462 function add_contact(&$name) {
463 if (! check_admin(1, "add a contact")) return;
465 $area_id = $_POST['area_id'];
466 if (! is_numeric($area_id)) {
467 echo "<p>Invalid area!</p>\n";
471 $area = get_area_by_id($area_id);
473 echo "<p>No such area!</p>\n";
477 $contact = new Contact;
478 if (! update_contact($contact, $area_id, true)) return false;
479 return $contact->getId();
482 function delete_contact($name, $id = null, &$city_id = null) {
483 if (! check_admin(1, "delete a contact")) return;
485 if (isset($id)) $contact = get_contact_by_id($id);
486 else $contact = get_contact_by_name($name);
487 if (! $contact) return false;
489 ///* Remember city ID for dropdown. */
490 //$city_id = $area->getCityId();
494 echo "<p>Deleted contact.</p>\n";
496 catch (Exception $e) {
497 echo "<p>Error deleting $name!</p>\n";
504 function show_contact($name, &$id = null) {
505 if (isset($id)) $contact = get_contact_by_id($id);
506 else $contact = get_contact_by_name($name);
507 if (! $contact) return;
510 show_contact_summary($contact, true);
515 show_contact_form($contact);
517 if (check_admin(1)) {
519 echo " <td colspan=2>";
520 submit("update_contact", "Update");
529 /* /contact/in/area/Cambridge/1 */
530 if (count($parameters)) {
531 if ($parameters[0] == "in") {
532 switch ($parameters[1]) {
534 $area_id = $parameters[3];
535 $_POST['area_id'] = $area_id;
537 $area = $q->findOneById($area_id);
538 $city = get_area_city($area);
539 if ($city) $city_id = $city->getId();
540 show_area_contacts(0, 10, $parameters[2], $area_id);
544 $city_id = $parameters[3];
545 $_POST['city_id'] = $city_id;
547 $city = $q->findOneById($city_id);
548 show_city_contacts(0, 10, $parameters[2], $city_id);
552 else if ($parameters[0] == "search") {
553 search_contacts(0, 10, $parameters[1]);
556 list($name, $id, $args) = parse_parameters($parameters);
557 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
561 delete_contact($name, $id);
565 else if (isset($name)) show_contact($name, $id);
567 show_contact_forms($city_id);
568 show_add_new_contact_form($city_id);