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