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