Fixed changing area in existing address.
[readifood.git] / lib / contact.php
1 <?php
2
3   if (isset($_POST['show_add_contact'])) {
4     $city_id = $_POST['city_id'];
5     show_new_contact_form($city_id);
6   }
7   else if (isset($_POST['add_contact'])) {
8     $id = add_contact($displayname);
9     if ($id !== false) {
10       echo "<p>Added contact.</p>\n";
11       $parameters = array($displayname, $id);
12     }
13   }
14   else if (isset($_POST['update_contact'])) {
15     list($name, $id, $args) = parse_parameters($parameters);
16     $q = new ContactQuery;
17     $contact = $q->findOneById($id);
18     if ($contact) {
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());
24       }
25     }
26     else {
27       echo "<p>No such contact!</p>\n";
28     }
29   }
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'])));
32     exit;
33   }
34   else if ($_POST['area_id']) {
35     $q = new AreaQuery;
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']));
38     exit;
39   }
40   else if ($_POST['city_id']) {
41     $q = new CityQuery;
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']));
44     exit;
45   }
46
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']) {
56       printf(" <a class=\"small\" href=\"/order/to/beneficiary/%s/%d\">Orders</a>", urlencode($contact->getDisplayname()), $contact->getId());
57       if (get_contact_area($contact)) printf(" <a class=\"small\" href=\"/order/place/for/beneficiary/%s/%d\">Place</a>", urlencode($contact->getDisplayname()), $contact->getId());
58     }
59     if (check_admin(1)) {
60       echo " " . $contact->getDeleteLink();
61     }
62     $area = get_contact_area($contact);
63     echo " in " . $area->getLink();
64     $city = get_contact_city($contact);
65     echo ", " . $city->getLink(get_city_displayname($city));
66   }
67
68   function show_contacts($offset, $per_page, $address_ids) {
69     $q = new ContactQuery;
70     $contacts = $q->filterByAddressId($address_ids)->orderByForename()->orderBySurname()->find();
71     if (count($contacts)) {
72       foreach ($contacts as $contact) show_contact_summary($contact);
73     }
74     else echo " none";
75   }
76
77   function search_contacts($offset, $per_page, $search) {
78     $q = new ContactQuery;
79     $contacts = $q->filterByDisplayname("%$search%")->find();
80     echo "<p>Contacts matching '" . htmlspecialchars($search) . "':";
81     if (count($contacts)) {
82       foreach ($contacts as $contact) show_contact_summary($contact);
83     }
84     else echo "none";
85     echo "</p>\n";
86   }
87
88   function show_city_contacts($offset, $per_page, $city_name, $city_id = null) {
89     if (isset($city_id)) $city = get_city_by_id($city_id);
90     else if ($city_name) $city = get_city_by_name($city_name);
91     if ($city) {
92       $q = new AreaQuery;
93       $areas = $q->filterByCityId($city->getId())->find();
94       $area_ids = array();
95       foreach ($areas as $area) $area_ids[] = $area->getId();
96
97       $q = new AddressQuery;
98       $addresses = $q->filterByAreaId($area_ids)->find();
99       $address_ids = array();
100       foreach ($addresses as $address) $address_ids[] = $address->getId();
101
102       echo "<p>Contacts in city " . $city->getLink(get_city_displayname($city)) . ":";
103       return show_contacts($offset, $per_page, $address_ids);
104     }
105     else echo "<p>No such city!</p>\n";
106   }
107
108   function show_area_contacts($offset, $per_page, $area_name, $area_id = null) {
109     if (isset($area_id)) $area = get_area_by_id($area_id);
110     else if ($area_name) $area = get_area_by_name($area_name);
111     if ($area) {
112       $q = new AddressQuery;
113       $addresses = $q->filterByAreaId($area->getId())->find();
114       $address_ids = array();
115       foreach ($addresses as $address) $address_ids[] = $address->getId();
116
117       echo "<p>Contacts in area " . $area->getLink() . ":";
118       return show_contacts($offset, $per_page, $address_ids);
119     }
120     else echo "<p>No such area!</p>\n";
121   }
122
123   function show_contact_areas_form($city_id = null) {
124     $areas = get_city_areas($city_id);
125     if (! count($areas)) {
126       echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
127       return;
128     }
129
130     echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
131     echo "<p>Show contacts in area\n";
132     echo "<select name=\"area_id\">\n";
133     foreach ($areas as $area) {
134       option("area_id", $area->getId(), get_area_displayname($area));
135     }
136     echo "</select>\n";
137     echo "<input type=\"submit\" value=\"Show\">\n";
138     echo "</form>\n";
139   }
140
141   function show_contact_cities_form($city_id = null) {
142     $q = new CityQuery;
143     $cities = $q->orderByName()->find();
144
145     if (! count($cities)) {
146       echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
147       return;
148     }
149
150     echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
151     echo "<p>Show contacts in city\n";
152     echo "<select name=\"city_id\">\n";
153     foreach ($cities as $city) {
154       option("city_id", $city->getId(), get_city_displayname($city), $city_id);
155     }
156     echo "</select>\n";
157     echo "<input type=\"submit\" value=\"Show\">\n";
158     echo "</form>\n";
159   }
160
161   function show_contact_search_form() {
162     echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
163     echo "<p>Search for contacts:";
164     input("search_contact");
165     echo "<input type=\"submit\" value=\"Search\">\n";
166     echo "</form>\n";
167   }
168
169   function show_contact_forms($city_id) {
170     show_contact_areas_form($city_id);
171     show_contact_cities_form($city_id);
172     show_contact_search_form();
173   }
174
175   function show_contact_role_form($role) {
176     return show_role_form($role, $GLOBALS['contact_roles']);
177   }
178
179   function show_contact_form($contact = null, $new = false) {
180     global $contact_roles, $parcel_sizes, $parcel_contents;
181
182     if (! $contact) $contact = new Contact;
183
184     /* Role. */
185     echo "<tr>\n";
186     echo "  <td>Role</td>\n";
187     echo "  <td>"; show_contact_role_form($contact->getRole()); echo "</td>\n";
188     echo "</tr>\n";
189
190     /* Date added. */
191     if (! $new) {
192       echo "<tr>\n";
193       echo "  <td>Registered</td>\n";
194       echo "  <td>" . $contact->getAdded() . "</td>\n";
195       echo "</tr>\n";
196     }
197
198     /* Forename. */
199     echo "<tr>\n";
200     echo "  <td>Forename</td>\n";
201     echo "  <td>"; input("forename", $contact->getForename()); echo "</td>\n";
202     echo "</tr>\n";
203
204     /* Middle names. */
205     echo "<tr>\n";
206     echo "  <td>Middle name(s)</td>\n";
207     echo "  <td>"; input("middle", $contact->getMiddle()); echo "</td>\n";
208     echo "</tr>\n";
209
210     /* Surname. */
211     echo "<tr>\n";
212     echo "  <td>Surname</td>\n";
213     echo "  <td>"; input("surname", $contact->getSurname()); echo "</td>\n";
214     echo "</tr>\n";
215
216     /* Display name. */
217     echo "<tr>\n";
218     echo "  <td>Display name (if not concatenation of above)</td>\n";
219     echo "  <td>"; input("displayname", $contact->getDisplayname()); echo "</td>\n";
220     echo "</tr>\n";
221
222     /* Address. */
223     $address = get_contact_address($contact);
224     if (! $address) $address = new Address;
225     echo "<tr>\n";
226     echo "  <td>Address</td>\n";
227     echo "  <td>"; textarea("address", $address->getLine()); echo "</td>\n";
228     echo "</tr>\n";
229
230     /* Postcode. */
231     echo "<tr>\n";
232     echo "  <td>Postcode</td>\n";
233     echo "  <td>"; input("postcode", $address->getPostcode()); echo "</td>\n";
234     echo "</tr>\n";
235
236     /* Telephone. */
237     echo "<tr>\n";
238     echo "  <td>Telephone</td>\n";
239     echo "  <td>"; input("telephone1", $contact->getTelephone1()); echo "</td>\n";
240     echo "</tr>\n";
241     echo "<tr>\n";
242     echo "  <td>Alternative telephone</td>\n";
243     echo "  <td>"; input("telephone2", $contact->getTelephone2()); echo "</td>\n";
244     echo "</tr>\n";
245
246     /* Email. */
247     echo "<tr>\n";
248     echo "  <td>Email</td>\n";
249     echo "  <td>"; input("email", $contact->getEmail()); echo "</td>\n";
250     echo "</tr>\n";
251
252     /* Area. */
253     $area = get_contact_area($contact);
254     if ($area) $area_id = $area->getId();
255     echo "<tr>\n";
256     echo "  <td>Area</td>\n";
257     echo "  <td><select name=\"area_id\">\n";
258     $areas = get_city_areas();
259     foreach ($areas as $area) {
260       option("area_id", $area->getId(), get_area_displayname($area), $area_id);
261     }
262     echo "  </select></td>\n";
263     echo "</tr>\n";
264
265     /* Parcel type. */
266     echo "<tr>\n";
267     echo "  <td>Family unit</td>\n";
268     echo "  <td><select name=\"parcel_size\">\n";
269     $mask = 1 << count($parcel_sizes);
270     for ($i = 0; $i < count($parcel_sizes); $i++) {
271       option("parcel_size", 1 << $i, $parcel_sizes[$i], $contact->getParcel() % $mask);
272     }
273     echo "</select></td>\n";
274     echo "</tr>\n";
275
276     /* Parcel contents. */
277     echo "<tr>\n";
278     echo "  <td>Dietary requirements</td>\n";
279     echo "  <td>";
280     for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
281       if (1 << $i == $GLOBALS['PARCEL_TOILETRY']) continue;
282       echo "  <input type=\"checkbox\" name=\"parcel_$i\"";
283       if ($contact->getParcel() & (1 << $i)) echo " checked";
284       echo ">$parcel_contents[$i]\n";
285     }
286     echo "</td>\n";
287     echo "</tr>\n";
288
289     /* Notes. */
290     echo "<tr>\n";
291     echo "  <td>Notes</td>\n";
292     echo "  <td><textarea name=\"notes\">" . $contact->getNotes() . "</textarea></td>\n";
293     echo "</tr>\n";
294   }
295
296   function show_new_contact_form($city_id = null) {
297     if (! check_admin(1)) return;
298
299     $areas = get_city_areas($city_id);
300     if (! count($areas)) {
301       echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
302       return;
303     }
304
305     echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
306     echo "<p>Add a new contact:</p>\n";
307
308     echo "<table>\n";
309     show_contact_form($contact, true);
310
311     echo "<tr>\n";
312     echo "  <td colspan=2>"; submit("add_contact", "Add"); echo "</td></tr>\n";
313     echo "</tr>\n";
314     echo "</table>\n";
315     echo "</form>\n";
316   }
317
318   function show_add_new_contact_form() {
319     if (! check_admin(1)) return;
320
321     $q = new CityQuery;
322     $cities = $q->find();
323     if (! count($cities)) {
324       echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
325       return;
326     }
327
328     echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
329     echo "<p>Add a new contact in <select name=\"city_id\">\n";
330     foreach ($cities as $city) {
331       option("city_id", $city->getId(), get_city_displayname($city));
332     }
333     echo "</select>";
334     submit("show_add_contact", "Proceed");
335     echo "</p>\n";
336     echo "</form>\n";
337   }
338
339   function update_contact(&$contact, $area_id, $new = false) {
340     global $contact_roles, $parcel_sizes, $parcel_contents;
341
342     $role = 0;
343     for ($i = 0; $i < count($contact_roles); $i++) {
344       if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
345     }
346
347     /* Staff can place orders. */
348     if ($role & (1 << 0)) $role |= (1 << 2);
349
350     $forename = $_POST['forename'];
351     $middle = $_POST['middle'];
352     $surname = $_POST['surname'];
353     $displayname = $_POST['displayname'];
354
355     if (! $forename && ! $surname) {
356       echo "<p>Must have either a forename or surname!</p>\n";
357       return false;
358     }
359     if ($middle && ! ($forename && $surname)) {
360       echo "<p>Must have both a forename or surname for middle name(s) to make sense!</p>\n";
361       return false;
362     }
363
364     if (! $displayname) {
365       $displayname = $forename;
366       if ($middle) $displayname .= " $middle";
367       if ($forename) $displayname .= " ";
368       $displayname .= $surname;
369       echo "<p>Display name will be $displayname.</p>\n";
370     }
371
372     /* Get address. */
373     $area_id = $_POST['area_id'];
374     $line = $_POST['address'];
375     $postcode = $_POST['postcode'];
376     $q = new AddressQuery;
377     /* XXX: Finding by area properly? */
378     $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
379     if ($address->isNew()) {
380       /* Changing address. */
381       //if (! $new)
382       /*
383         XXX: Check for other contacts at the old address.
384         Make this a new address if there are others, but
385         provide a link to update other contacts.
386       */
387       try {
388         $address->save();
389       }
390       catch (Exception $e) {
391         echo "<p>Error adding $line.</p>\n";
392         return false;
393       }
394     }
395
396     $telephone1 = $_POST['telephone1'];
397     $telephone2 = $_POST['telephone2'];
398     $email = $_POST['email'];
399     $parcel = $_POST['parcel_size'];
400     for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
401       if ($_POST['parcel_' . $i] == "on") $parcel |= (1 << $i);
402     }
403     $notes = $_POST['notes'];
404
405     $contact->setRole($role);
406     $contact->setForename($forename);
407     $contact->setMiddle($middle);
408     $contact->setSurname($surname);
409     $contact->setDisplayname($displayname);
410     $contact->setTelephone1($telephone1);
411     $contact->setTelephone2($telephone2);
412     $contact->setEmail($email);
413     $contact->setParcel($parcel);
414     $contact->setNotes($notes);
415     $contact->setAddressId($address->getId());
416
417     try {
418       $contact->save();
419     }
420     catch (Exception $e) {
421       if ($new) echo "<p>Error adding $displayname.</p>\n";
422       else echo "<p>Error updating $displayname.</p>\n";
423       return false;
424     }
425
426     return true;
427   }
428
429   function add_contact(&$name) {
430     if (! check_admin(1, "add a contact")) return;
431
432     $area_id = $_POST['area_id'];
433     if (! is_numeric($area_id)) {
434       echo "<p>Invalid area!</p>\n";
435       return false;
436     }
437
438     $area = get_area_by_id($area_id);
439     if (! $area) {
440       echo "<p>No such area!</p>\n";
441       return false;
442     }
443
444     $contact = new Contact;
445     if (! update_contact($contact, $area_id, true)) return false;
446     return $contact->getId();
447   }
448
449   function delete_contact($name, $id = null, &$city_id = null) {
450     if (! check_admin(1, "delete a contact")) return;
451
452     if (isset($id)) $contact = get_contact_by_id($id);
453     else $contact = get_contact_by_name($name);
454     if (! $contact) return false;
455
456     ///* Remember city ID for dropdown. */
457     //$city_id = $area->getCityId();
458
459     try {
460       $contact->delete();
461       echo "<p>Deleted contact.</p>\n";
462     }
463     catch (Exception $e) {
464       echo "<p>Error deleting $name!</p>\n";
465       return false;
466     }
467
468     return true;
469   }
470
471   function show_contact($name, &$id = null) {
472     if (isset($id)) $contact = get_contact_by_id($id);
473     else $contact = get_contact_by_name($name);
474     if (! $contact) return;
475
476     echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
477     show_contact_summary($contact, true);
478     echo ": ";
479     echo "\n</p>";
480
481     echo "<table>\n";
482     show_contact_form($contact);
483
484     if (check_admin(1)) {
485       echo "<tr>\n";
486       echo "  <td colspan=2>";
487       submit("update_contact", "Update");
488       echo "</td>\n";
489       echo "</tr>\n";
490     }
491
492     echo "</table>\n";
493     echo "</form>\n";
494   }
495
496   /* /contact/in/area/Cambridge/1 */
497   if (count($parameters)) {
498     if ($parameters[0] == "in") {
499       switch ($parameters[1]) {
500         case "area":
501           $area_id = $parameters[3];
502           $_POST['area_id'] = $area_id;
503           $q = new AreaQuery;
504           $area = $q->findOneById($area_id);
505           $city = get_area_city($area);
506           if ($city) $city_id = $city->getId();
507           show_area_contacts(0, 10, $parameters[2], $area_id);
508         break;
509
510         case "city":
511           $city_id = $parameters[3];
512           $_POST['city_id'] = $city_id;
513           $q = new CityQuery;
514           $city = $q->findOneById($city_id);
515           show_city_contacts(0, 10, $parameters[2], $city_id);
516         break;
517       }
518
519       show_add_new_contact_form($city_id);
520     }
521     else if ($parameters[0] == "search") {
522       search_contacts(0, 10, $parameters[1]);
523     }
524   }
525   list($name, $id, $args) = parse_parameters($parameters);
526   //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
527   if (count($args)) {
528     switch ($args[0]) {
529       case "delete":
530         delete_contact($name, $id);
531       break;
532     }
533   }
534   else if (isset($name)) show_contact($name, $id);
535   else {
536     /* XXX: Shown after adding. */
537     show_contact_forms($city_id);
538     show_add_new_contact_form($city_id);
539   }
540
541   if (count($parameters)) {
542     show_contact_forms($city_id);
543   }
544
545 ?>