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