Added contact offers.
[readifood.git] / lib / contact.php
1 <?php
2
3   if (isset($_POST['show_add_contact'])) {
4     set_last_selected("city_id", $_POST['city_id']);
5     $city_id = $_POST['city_id'];
6     show_new_contact_form($city_id);
7   }
8   else if (isset($_POST['add_contact'])) {
9     set_last_selected("area_id", $_POST['area_id']);
10     $id = add_contact($displayname);
11     if ($id !== false) {
12       echo "<p>Added contact.</p>\n";
13       $parameters = array($displayname, $id);
14     }
15   }
16   else if (isset($_POST['update_contact'])) {
17     list($name, $id, $args) = parse_parameters($parameters);
18     $q = new ContactQuery;
19     $contact = $q->findOneById($id);
20     if ($contact) {
21       $area = get_contact_area($contact);
22       if ($area) $area_id = $area->getId();
23       if (update_contact($contact, $area_id) !== false) {
24         echo "<p>Updated contact.</p>\n";
25         $parameters = array($contact->getDisplayname(), $contact->getId());
26       }
27     }
28     else {
29       echo "<p>No such contact!</p>\n";
30     }
31   }
32   else if ($_POST['search_contact']) {
33     $search_options = array();
34     if ($_POST['search_by_notes']) array_push($search_options, 'notes');
35     if ($_POST['search_by_phone']) array_push($search_options, 'phone');
36     if (count($search_options)) $search_param = sprintf("%s/", implode('+', $search_options));
37     else $search_param = '';
38     header(sprintf("Location: http%s://%s/%s/search/%s%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, $search_param, urlencode($_POST['search_contact'])));
39     exit;
40   }
41   else if ($_POST['show_in_area']) {
42     set_last_selected("area_id", $_POST['area_id']);
43     $q = new AreaQuery;
44     $area = $q->findOneById($_POST['area_id']);
45     header(sprintf("Location: http%s://%s/%s/in/area/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($area->getName()), $_POST['area_id']));
46     exit;
47   }
48   else if ($_POST['show_in_city']) {
49     set_last_selected("city_id", $_POST['city_id']);
50     $q = new CityQuery;
51     $city = $q->findOneById($_POST['city_id']);
52     header(sprintf("Location: http%s://%s/%s/in/city/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($city->getName()), $_POST['city_id']));
53     exit;
54   }
55
56   function show_contact_summary(&$contact, $editing = false) {
57     if ($editing) echo "<p>Contact: <span class=\"strong\">" . htmlspecialchars($contact->getDisplayname()) . "</span>";
58     else echo "<br>\nContact " . $contact->getStrongLink();
59     $role = $contact->getRole();
60     $role_string = get_contact_role_string($contact);
61     if ($role_string) echo " $role_string";
62     $d = urlencode($contact->getDisplayname());
63     $i = $contact->getId();
64     if ($role & $GLOBALS['ROLE_DONOR']) echo " " . get_small_link("Donations", "/donation/from/contact/%s/%d", $d, $i);
65     if ($role & $GLOBALS['ROLE_REQUESTER']) echo " " . get_small_link("Referred", "/order/from/referrer/%s/%d", $d, $i);
66     if ($role & $GLOBALS['ROLE_BENEFICIARY']) {
67       echo " " . get_small_link("Orders", "/order/to/beneficiary/%s/%d", $d, $contact->getId());
68       if (get_contact_area($contact)) echo " " . get_small_link("Place", "/order/place/for/beneficiary/%s/%d", $d, $i);
69     }
70     if (check_admin(1)) {
71       echo " " . $contact->getDeleteLink();
72     }
73     $area = get_contact_area($contact);
74     echo " in " . $area->getLink();
75     $city = get_contact_city($contact);
76     echo ", " . $city->getLink(get_city_displayname($city));
77   }
78
79   function show_contacts($address_ids) {
80     list($first_page, $per_page) = pagination();
81     $q = new ContactQuery;
82     $contacts = $q->filterByAddressId($address_ids)->orderByForename()->orderBySurname()->orderById('desc')->paginate($first_page, $per_page);
83     if (count($contacts)) {
84       foreach ($contacts as $contact) show_contact_summary($contact);
85       show_pagination($contacts);
86     }
87     else echo " none";
88   }
89
90   function search_contacts($search, $notes = false, $phone = false) {
91     list($first_page, $per_page) = pagination();
92     $q = new ContactQuery;
93     if ($phone) $q->filterByTelephone1("%$search%")->_or()->filterByTelephone2("%$search%");
94     else $q->filterByDisplayname("%$search%");
95     if ($notes) $q->_or()->filterByNotes("%$search%");
96     $contacts = $q->orderByForename()->orderBySurname()->orderById('desc')->paginate($first_page, $per_page);
97     echo "<p>Contacts matching " . ($notes ? "notes " : "") . ($phone ? "telephone " : "") . "'" . htmlspecialchars($search) . "':";
98     if (count($contacts)) {
99       foreach ($contacts as $contact) show_contact_summary($contact);
100       show_pagination($contacts);
101     }
102     else echo "none";
103     echo "</p>\n";
104
105     $address_ids = array();
106     if (! $phone) {
107       $q = new AddressQuery;
108       $addresses = $q->filterByLine("%$search%")->find();
109       foreach ($addresses as $address) $address_ids[] = $address->getId();
110       $q = new AddressQuery;
111       $addresses = $q->filterByPostcode("%$search%")->find();
112       foreach ($addresses as $address) $address_ids[] = $address->getId();
113
114       $q = new ContactQuery;
115       $contacts = $q->filterByAddressId($address_ids)->orderByForename()->orderBySurname()->orderById('desc')->paginate($first_page, $per_page);
116       echo "<p>Contacts in address '" . htmlspecialchars($search) . "':";
117       if (count($contacts)) {
118         foreach ($contacts as $contact) show_contact_summary($contact);
119         show_pagination($contacts);
120       }
121       else echo "none";
122     }
123     echo "</p>\n";
124   }
125
126   function show_city_contacts($city_name, $city_id = null) {
127     if (isset($city_id)) $city = get_city_by_id($city_id);
128     else if ($city_name) $city = get_city_by_name($city_name);
129     if ($city) {
130       $q = new AreaQuery;
131       $areas = $q->filterByCityId($city->getId())->find();
132       $area_ids = array();
133       foreach ($areas as $area) $area_ids[] = $area->getId();
134
135       $q = new AddressQuery;
136       $addresses = $q->filterByAreaId($area_ids)->find();
137       $address_ids = array();
138       foreach ($addresses as $address) $address_ids[] = $address->getId();
139
140       echo "<p>Contacts in city " . $city->getLink(get_city_displayname($city)) . ":";
141       return show_contacts($address_ids);
142     }
143     else echo "<p>No such city!</p>\n";
144   }
145
146   function show_area_contacts($area_name, $area_id = null) {
147     if (isset($area_id)) $area = get_area_by_id($area_id);
148     else if ($area_name) $area = get_area_by_name($area_name);
149     if ($area) {
150       $q = new AddressQuery;
151       $addresses = $q->filterByAreaId($area->getId())->find();
152       $address_ids = array();
153       foreach ($addresses as $address) $address_ids[] = $address->getId();
154
155       echo "<p>Contacts in area " . $area->getLink() . ":";
156       return show_contacts($address_ids);
157     }
158     else echo "<p>No such area!</p>\n";
159   }
160
161   function show_contact_areas_form($city_id = null) {
162     $areas = get_city_areas($city_id);
163     if (! count($areas)) {
164       echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
165       return;
166     }
167
168     $candidates = array();
169     foreach ($areas as $area) {
170       if (! count(get_area_contacts($area->getId()))) continue;
171       $candidates[] = $area;
172     }
173     if (! count($candidates)) return;
174
175     echo "<p>Show contacts in area\n";
176     echo "<select name=\"area_id\">\n";
177     foreach ($candidates as $area) {
178       option("area_id", $area->getId(), get_area_displayname($area));
179     }
180     echo "</select>\n";
181     submit("show_in_area", "Show");
182   }
183
184   function show_contact_cities_form($city_id = null) {
185     $q = new CityQuery;
186     $cities = $q->orderByName()->find();
187
188     if (! count($cities)) {
189       echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
190       return;
191     }
192
193     $candidates = array();
194     foreach ($cities as $city) {
195       if (! count(get_city_contacts($city->getId()))) continue;
196       $candidates[] = $city;
197     }
198     if (! count($candidates)) return;
199
200     echo "<p>Show contacts in city\n";
201     echo "<select name=\"city_id\">\n";
202     foreach ($candidates as $city) {
203       option("city_id", $city->getId(), get_city_displayname($city), $city_id);
204     }
205     echo "</select>\n";
206     submit("show_in_city", "Show");
207   }
208
209   function show_contact_search_form() {
210     echo "<p>Search for contacts:";
211     input("search_contact");
212     echo "<input type=\"checkbox\" id=\"search_by_phone\" name=\"search_by_phone\">";
213     echo "<label for=\"search_by_phone\">by telephone</label>\n";
214     echo "<input type=\"checkbox\" id=\"search_by_notes\" name=\"search_by_notes\">";
215     echo "<label for=\"search_by_notes\">by notes</label>\n";
216     echo "<input type=\"submit\" value=\"Search\">\n";
217     echo "</p>\n";
218   }
219
220   function show_contact_forms($city_id) {
221     form("noprint standout");
222     show_contact_areas_form($city_id);
223     show_contact_cities_form($city_id);
224     show_contact_search_form();
225     end_form();
226   }
227
228   function show_contact_role_form($role) {
229     return show_role_form($role, $GLOBALS['contact_roles']);
230   }
231
232   function show_contact_form($contact = null, $new = false) {
233     global $contact_roles, $parcel_sizes, $parcel_contents, $offers;
234
235     if (! $contact) $contact = new Contact;
236     else if ($contact->getRole() & ($GLOBALS['ROLE_BENEFICIARY'] | $GLOBALS['ROLE_REQUESTER'])) {
237       $state_mask = $GLOBALS['STATE_ANY'];
238       $state_mask &= ~$GLOBALS['STATE_DELIVERED'];
239       $state_mask &= ~$GLOBALS['STATE_CANCELLED'];
240
241       $orders = get_contact_orders($contact, $state_mask);
242
243       if (count($orders)) {
244         echo "<tr>\n";
245         echo "  <td colspan=2><strong>Outstanding orders:</strong></td>\n";
246         echo "</tr>\n";
247
248         echo "<tr>\n";
249         echo "  <td colspan=2 class=\"history\">\n";
250         foreach ($orders as $order) {
251           echo "    " . get_order_summary($order) . "<br>\n";
252         }
253         echo "  </td>\n";
254         echo "</tr>\n";
255       }
256     }
257
258     /* Role. */
259     echo "<tr>\n";
260     echo "  <td>Role</td>\n";
261     echo "  <td>"; show_contact_role_form($contact->getRole()); echo "</td>\n";
262     echo "</tr>\n";
263
264     /* Date added. */
265     if (! $new) {
266       echo "<tr>\n";
267       echo "  <td>Registered</td>\n";
268       echo "  <td>" . $contact->getAdded() . "</td>\n";
269       echo "</tr>\n";
270     }
271
272     /* Forename. */
273     echo "<tr>\n";
274     echo "  <td>Forename</td>\n";
275     echo "  <td>"; input("forename", $contact->getForename()); echo "</td>\n";
276     echo "</tr>\n";
277
278     /* Middle names. */
279     echo "<tr>\n";
280     echo "  <td>Middle name(s)</td>\n";
281     echo "  <td>"; input("middle", $contact->getMiddle()); echo "</td>\n";
282     echo "</tr>\n";
283
284     /* Surname. */
285     echo "<tr>\n";
286     echo "  <td>Surname</td>\n";
287     echo "  <td>"; input("surname", $contact->getSurname()); echo "</td>\n";
288     echo "</tr>\n";
289
290     /* Display name. */
291     echo "<tr>\n";
292     echo "  <td>Display name (if not concatenation of above)</td>\n";
293     echo "  <td>"; input("displayname", $contact->getDisplayname()); echo "</td>\n";
294     echo "</tr>\n";
295
296     /* Address. */
297     $address = get_contact_address($contact);
298     if (! $address) $address = new Address;
299     echo "<tr>\n";
300     echo "  <td>Address</td>\n";
301     echo "  <td>"; textarea("address", $address->getLine()); echo "</td>\n";
302     echo "</tr>\n";
303
304     /* Postcode. */
305     echo "<tr>\n";
306     echo "  <td>Postcode</td>\n";
307     $postcode = $address->getPostcode();
308     if (validate_postcode($postcode)) {
309       echo "  <td>"; input("postcode", $postcode); echo get_address_map_link($address); echo "</td>\n";
310     }
311     else {
312       echo "  <td>"; input("postcode", $address->getPostcode()); echo " (invalid)</td>\n";
313     }
314     echo "</tr>\n";
315
316     /* Telephone. */
317     echo "<tr>\n";
318     echo "  <td>Telephone</td>\n";
319     echo "  <td>"; input("telephone1", $contact->getTelephone1()); echo "</td>\n";
320     echo "</tr>\n";
321     echo "<tr>\n";
322     echo "  <td>Alternative telephone</td>\n";
323     echo "  <td>"; input("telephone2", $contact->getTelephone2()); echo "</td>\n";
324     echo "</tr>\n";
325
326     /* Email. */
327     echo "<tr>\n";
328     echo "  <td>Email</td>\n";
329     echo "  <td>"; input("email", $contact->getEmail()); echo "</td>\n";
330     echo "</tr>\n";
331
332     /* Area. */
333     $area = get_contact_area($contact);
334     if ($area) $area_id = $area->getId();
335     echo "<tr>\n";
336     echo "  <td>Area</td>\n";
337     echo "  <td><select name=\"area_id\">\n";
338     $areas = get_city_areas();
339     foreach ($areas as $area) {
340       option("area_id", $area->getId(), get_area_displayname($area), $area_id);
341     }
342     echo "  </select></td>\n";
343     echo "</tr>\n";
344
345     /* Parcel type. */
346     echo "<tr>\n";
347     echo "  <td>Family unit</td>\n";
348     echo "  <td><select name=\"parcel_size\">\n";
349     $mask = 1 << count($parcel_sizes);
350     for ($i = 0; $i < count($parcel_sizes); $i++) {
351       option("parcel_size", 1 << $i, $parcel_sizes[$i], $contact->getParcel() % $mask);
352     }
353     echo "</select></td>\n";
354     echo "</tr>\n";
355
356     /* Parcel contents. */
357     echo "<tr>\n";
358     echo "  <td>Dietary requirements</td>\n";
359     echo "  <td>";
360     for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
361       if (1 << $i == $GLOBALS['PARCEL_TOILETRY']) continue;
362       echo "  <input type=\"checkbox\" id=\"parcel_$i\" name=\"parcel_$i\"";
363       if ($contact->getParcel() & (1 << $i)) echo " checked";
364       echo "><label for=\"parcel_$i\">$parcel_contents[$i]</label>\n";
365     }
366     echo "</td>\n";
367     echo "</tr>\n";
368
369     /* Notes. */
370     echo "<tr>\n";
371     echo "  <td>Notes</td>\n";
372     echo "  <td>"; textarea("notes", $contact->getNotes()); echo "</td>\n";
373     echo "</tr>\n";
374
375     /* Offers. */
376     $available_offers = get_available_offers($contact);
377     if (count($available_offers)) {
378       echo "<tr>\n";
379       echo "  <td colspan=2><strong>Offers</strong></td>\n";
380       echo "</tr>\n";
381
382       $offer_states = Array();
383       foreach ($available_offers as $offer) {
384         $offer_id = $offer->getId();
385         $state = get_offer_state($contact, $offer);
386         $state_id = is_null($state) ? $GLOBALS['OFFER_ELIGIBLE'] : $state->getState();
387         $offer_states[$offer_id] = $state_id;
388         echo "<tr>\n";
389         echo "  <td>" . $offer->getDescription() . "</td>\n";
390         echo "  <td><select name=\"offer$offer_id\">\n";
391         for ($i = 0; $i < count($offers); $i++) {
392           option("offer$offer_id", 1 << $i, $offers[$i], $state_id == 1 << $i);
393         }
394         echo "</select></td>\n";
395         echo "</tr>\n";
396       }
397       hidden("offer_states", urlencode(json_encode($offer_states)));
398     }
399     else {
400       echo "<tr>\n";
401       echo "  <td colspan=2><strong>No available offers</strong></td>\n";
402       echo "</tr>\n";
403       hidden("offer_states", "{}");
404     }
405   }
406
407   function show_new_contact_form($city_id = null) {
408     if (! check_admin(1)) return;
409
410     $areas = get_city_areas($city_id);
411     if (! count($areas)) {
412       echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
413       return;
414     }
415
416     form("noprint");
417     echo "<p>Add a new contact:</p>\n";
418
419     echo "<table>\n";
420     show_contact_form($contact, true);
421
422     echo "<tr>\n";
423     echo "  <td colspan=2>"; submit("add_contact", "Add"); echo "</td></tr>\n";
424     echo "</tr>\n";
425     echo "</table>\n";
426     end_form();
427   }
428
429   function show_add_new_contact_form() {
430     if (! check_admin(1)) return;
431
432     $q = new CityQuery;
433     $cities = $q->find();
434     if (! count($cities)) {
435       echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
436       return;
437     }
438
439     form("noprint standout");
440     echo "<p>Add a new contact in <select name=\"city_id\">\n";
441     foreach ($cities as $city) {
442       option("city_id", $city->getId(), get_city_displayname($city));
443     }
444     echo "</select>";
445     submit("show_add_contact", "Proceed");
446     echo "</p>\n";
447     end_form();
448   }
449
450   function update_contact(&$contact, $area_id, $new = false) {
451     global $contact_roles, $parcel_sizes, $parcel_contents;
452
453     $role = 0;
454     for ($i = 0; $i < count($contact_roles); $i++) {
455       if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
456     }
457
458     /* Staff can place orders. */
459     if ($role & (1 << 0)) $role |= (1 << 2);
460
461     $forename = $_POST['forename'];
462     $middle = $_POST['middle'];
463     $surname = $_POST['surname'];
464     $displayname = $_POST['displayname'];
465
466     if (! $forename && ! $surname) {
467       echo "<p>Must have either a forename or surname!</p>\n";
468       return false;
469     }
470     if ($middle && ! ($forename && $surname)) {
471       echo "<p>Must have both a forename or surname for middle name(s) to make sense!</p>\n";
472       return false;
473     }
474
475     if (! $displayname) {
476       $displayname = $forename;
477       if ($middle) $displayname .= " $middle";
478       if ($forename) $displayname .= " ";
479       $displayname .= $surname;
480       echo "<p>Display name will be $displayname.</p>\n";
481     }
482
483     /* Get address. */
484     $area_id = $_POST['area_id'];
485     $line = $_POST['address'];
486     $postcode = trim($_POST['postcode']);
487     if ($postcode) {
488       $postcode = format_postcode($_POST['postcode'], true);
489       if (! $postcode) return false;
490     }
491     $q = new AddressQuery;
492     /* XXX: Finding by area properly? */
493     $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
494     if ($address->isNew()) {
495       /* Changing address. */
496       //if (! $new)
497       /*
498         XXX: Check for other contacts at the old address.
499         Make this a new address if there are others, but
500         provide a link to update other contacts.
501       */
502       try {
503         $address->save();
504       }
505       catch (Exception $e) {
506         echo "<p>Error adding $line.</p>\n";
507         return false;
508       }
509     }
510
511     $telephone1 = $_POST['telephone1'];
512     $telephone2 = $_POST['telephone2'];
513     $email = $_POST['email'];
514     $parcel = $_POST['parcel_size'];
515     for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
516       if ($_POST['parcel_' . $i] == "on") $parcel |= (1 << $i);
517     }
518     $notes = $_POST['notes'];
519
520     $contact->setRole($role);
521     $contact->setForename($forename);
522     $contact->setMiddle($middle);
523     $contact->setSurname($surname);
524     $contact->setDisplayname($displayname);
525     $contact->setTelephone1($telephone1);
526     $contact->setTelephone2($telephone2);
527     $contact->setEmail($email);
528     $contact->setParcel($parcel);
529     $contact->setNotes($notes);
530     $contact->setAddressId($address->getId());
531
532     try {
533       $contact->save();
534     }
535     catch (Exception $e) {
536       if ($new) echo "<p>Error adding $displayname.</p>\n";
537       else echo "<p>Error updating $displayname.</p>\n";
538       return false;
539     }
540
541     if ($_POST['offer_states']) {
542       try {
543         $offer_states = json_decode(urldecode($_POST['offer_states']));
544       }
545       catch (Exception $e) {
546         echo "<p>Error getting offer states JSON.</p>\n";
547         return false;
548       }
549
550       $now = time();
551       foreach ($offer_states as $offer_id => $state_id) {
552         $new_state_id = strval($_POST["offer$offer_id"]);
553         if ($new_state_id == "") continue;
554         if ($new_state_id == strval($state_id)) continue;
555
556         $offer_state = new OfferState;
557         $offer_state->setUpdated($now);
558         $offer_state->setOfferId($offer_id);
559         $offer_state->setUserId($GLOBALS['user_id']);
560         $offer_state->setContactId($contact->getId());
561         $offer_state->setState($new_state_id);
562
563         try {
564           $offer_state->save();
565         }
566         catch (Exception $e) {
567           echo "<p>Error updating offer.</p>\n";
568           echo "<p>" . $e->getMessage() . "</p>\n";
569           return false;
570         }
571       }
572     }
573
574     return true;
575   }
576
577   function add_contact(&$name) {
578     if (! check_admin(1, "add a contact")) return;
579
580     $area_id = $_POST['area_id'];
581     if (! is_numeric($area_id)) {
582       echo "<p>Invalid area!</p>\n";
583       return false;
584     }
585
586     $area = get_area_by_id($area_id);
587     if (! $area) {
588       echo "<p>No such area!</p>\n";
589       return false;
590     }
591
592     $contact = new Contact;
593     if (! update_contact($contact, $area_id, true)) return false;
594     $name = $contact->getDisplayname();
595     return $contact->getId();
596   }
597
598   function confirm_delete_contact($name, $id = null, &$city_id = null) {
599     if (! check_admin(1, "delete a contact")) return;
600
601     if (isset($id)) $contact = get_contact_by_id($id);
602     else $contact = get_contact_by_name($name);
603     if (! $contact) return false;
604
605     echo "<h3>Confirm deletion</h3>\n";
606     echo "<p>You must confirm deletion of contact " . $contact->getDisplayname() . ": " . $contact->getDeleteLink(true) . "</p>\n";
607   }
608
609   function delete_contact($name, $id = null, &$city_id = null) {
610     if (! check_admin(1, "delete a contact")) return;
611
612     if (isset($id)) $contact = get_contact_by_id($id);
613     else $contact = get_contact_by_name($name);
614     if (! $contact) return false;
615
616     ///* Remember city ID for dropdown. */
617     //$city_id = $area->getCityId();
618
619     try {
620       $contact->delete();
621       echo "<p>Deleted contact.</p>\n";
622     }
623     catch (Exception $e) {
624       echo "<p>Error deleting $name!</p>\n";
625       return false;
626     }
627
628     return true;
629   }
630
631   function show_contact($name, &$id = null) {
632     if (isset($id)) $contact = get_contact_by_id($id);
633     else $contact = get_contact_by_name($name);
634     if (! $contact) return;
635
636     form();
637     show_contact_summary($contact, true);
638     echo ": ";
639     echo "\n</p>";
640
641     echo "<table>\n";
642     show_contact_form($contact);
643
644     if (check_admin(1)) {
645       echo "<tr>\n";
646       echo "  <td colspan=2>";
647       submit("update_contact", "Update");
648       echo "</td>\n";
649       echo "</tr>\n";
650     }
651
652     echo "</table>\n";
653     end_form();
654   }
655
656   /* /contact/in/area/Cambridge/1 */
657   if (count($parameters)) {
658     if ($parameters[0] == "in") {
659       switch ($parameters[1]) {
660         case "area":
661           $area_id = $parameters[3];
662           $_POST['area_id'] = $area_id;
663           $q = new AreaQuery;
664           $area = $q->findOneById($area_id);
665           $city = get_area_city($area);
666           if ($city) $city_id = $city->getId();
667           show_area_contacts($parameters[2], $area_id);
668         break;
669
670         case "city":
671           $city_id = $parameters[3];
672           $_POST['city_id'] = $city_id;
673           $q = new CityQuery;
674           $city = $q->findOneById($city_id);
675           show_city_contacts($parameters[2], $city_id);
676         break;
677       }
678     }
679     else if ($parameters[0] == "search") {
680       $available_search_options = array('notes', 'phone');
681       $search_options = explode('+', $parameters[1]);
682       if (count(array_intersect($available_search_options, $search_options)) == count($search_options)) {
683         $notes = in_array("notes", $search_options);
684         $phone = in_array("phone", $search_options);
685         search_contacts(urldecode($parameters[2]), $notes, $phone);
686       }
687       else search_contacts(urldecode($parameters[1]));
688     }
689   }
690   list($name, $id, $args) = parse_parameters($parameters);
691   //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
692   if (count($args)) {
693     switch ($args[0]) {
694       case "delete":
695         confirm_delete_contact($name, $id);
696       break;
697
698       case "confirmdelete":
699         delete_contact($name, $id);
700       break;
701     }
702   }
703   else if (isset($name)) show_contact($name, $id);
704
705   show_contact_forms($city_id);
706   show_add_new_contact_form($city_id);
707
708 ?>