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