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