Split hub roles.
[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/referrer/%s/%d\">Referred</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     return show_role_form($role, $GLOBALS['contact_roles']);
171   }
172
173   function show_contact_form($contact = null) {
174     global $contact_roles;
175
176     if (! $contact) $contact = new Contact;
177
178     /* Role. */
179     echo "<tr>\n";
180     echo "  <td>Role</td>\n";
181     echo "  <td>"; show_contact_role_form($contact->getRole()); echo "</td>\n";
182     echo "</tr>\n";
183
184     /* Forename. */
185     echo "<tr>\n";
186     echo "  <td>Forename</td>\n";
187     echo "  <td>"; input("forename", $contact->getForename()); echo "</td>\n";
188     echo "</tr>\n";
189
190     /* Middle names. */
191     echo "<tr>\n";
192     echo "  <td>Middle name(s)</td>\n";
193     echo "  <td>"; input("middle", $contact->getMiddle()); echo "</td>\n";
194     echo "</tr>\n";
195
196     /* Surname. */
197     echo "<tr>\n";
198     echo "  <td>Surname</td>\n";
199     echo "  <td>"; input("surname", $contact->getSurname()); echo "</td>\n";
200     echo "</tr>\n";
201
202     /* Display name. */
203     echo "<tr>\n";
204     echo "  <td>Display name (if not concatenation of above)</td>\n";
205     echo "  <td>"; input("displayname", $contact->getDisplayname()); echo "</td>\n";
206     echo "</tr>\n";
207
208     /* Address. */
209     $address = get_contact_address($contact);
210     if (! $address) $address = new Address;
211     echo "<tr>\n";
212     echo "  <td>Address</td>\n";
213     echo "  <td>"; textarea("address", $address->getLine()); echo "</td>\n";
214     echo "</tr>\n";
215
216     /* Postcode. */
217     echo "<tr>\n";
218     echo "  <td>Postcode</td>\n";
219     echo "  <td>"; input("postcode", $address->getPostcode()); echo "</td>\n";
220     echo "</tr>\n";
221
222     /* Telephone. */
223     echo "<tr>\n";
224     echo "  <td>Telephone</td>\n";
225     echo "  <td>"; input("telephone1", $contact->getTelephone1()); echo "</td>\n";
226     echo "</tr>\n";
227     echo "<tr>\n";
228     echo "  <td>Alternative telephone</td>\n";
229     echo "  <td>"; input("telephone2", $contact->getTelephone2()); echo "</td>\n";
230     echo "</tr>\n";
231
232     /* Email. */
233     echo "<tr>\n";
234     echo "  <td>Email</td>\n";
235     echo "  <td>"; input("email", $contact->getEmail()); echo "</td>\n";
236     echo "</tr>\n";
237
238     /* Area. */
239     $area = get_contact_area($contact);
240     if ($area) $area_id = $area->getId();
241     echo "<tr>\n";
242     echo "  <td>Area</td>\n";
243     echo "  <td><select name=\"area_id\">\n";
244     $areas = get_city_areas();
245     foreach ($areas as $area) {
246       option("area_id", $area->getId(), get_area_displayname($area), $area_id);
247     }
248     echo "  </select></td>\n";
249     echo "</tr>\n";
250   }
251
252   function show_new_contact_form($city_id = null) {
253     if (! check_admin(1)) return;
254
255     $areas = get_city_areas($city_id);
256     if (! count($areas)) {
257       echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
258       return;
259     }
260
261     echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
262     echo "<p>Add a new contact:</p>\n";
263
264     echo "<table>\n";
265     show_contact_form($contact);
266
267     echo "<tr>\n";
268     echo "  <td colspan=2>"; submit("add_contact", "Add"); echo "</td></tr>\n";
269     echo "</tr>\n";
270     echo "</table>\n";
271     echo "</form>\n";
272   }
273
274   function show_add_new_contact_form() {
275     if (! check_admin(1)) return;
276
277     $q = new CityQuery;
278     $cities = $q->find();
279     if (! count($cities)) {
280       echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
281       return;
282     }
283
284     echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
285     echo "<p>Add a new contact in <select name=\"city_id\">\n";
286     foreach ($cities as $city) {
287       option("city_id", $city->getId(), get_city_displayname($city));
288     }
289     echo "</select>";
290     submit("show_add_contact", "Proceed");
291     echo "</p>\n";
292     echo "</form>\n";
293   }
294
295   function update_contact(&$contact, $area_id, $new = false) {
296     global $contact_roles;
297
298     $role = 0;
299     for ($i = 0; $i < count($contact_roles); $i++) {
300       if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
301     }
302
303     /* Staff can place orders. */
304     if ($role & (1 << 0)) $role |= (1 << 2);
305
306     $forename = $_POST['forename'];
307     $middle = $_POST['middle'];
308     $surname = $_POST['surname'];
309     $displayname = $_POST['displayname'];
310
311     if (! $forename && ! $surname) {
312       echo "<p>Must have either a forename or surname!</p>\n";
313       return false;
314     }
315     if ($middle && ! ($forename && $surname)) {
316       echo "<p>Must have both a forename or surname for middle name(s) to make sense!</p>\n";
317       return false;
318     }
319
320     if (! $displayname) {
321       $displayname = $forename;
322       if ($middle) $displayname .= " $middle";
323       if ($forename) $displayname .= " ";
324       $displayname .= $surname;
325       echo "<p>Display name will be $displayname.</p>\n";
326     }
327
328     /* Get address. */
329     $line = $_POST['address'];
330     $postcode = $_POST['postcode'];
331     $q = new AddressQuery;
332     /* XXX: Finding by area properly? */
333     $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
334     if ($address->isNew()) {
335       /* Changing address. */
336       //if (! $new)
337       /*
338         XXX: Check for other contacts at the old address.
339         Make this a new address if there are others, but
340         provide a link to update other contacts.
341       */
342       try {
343         $address->save();
344       }
345       catch (Exception $e) {
346         echo "<p>Error adding $line.</p>\n";
347         return false;
348       }
349     }
350
351     $telephone1 = $_POST['telephone1'];
352     $telephone2 = $_POST['telephone2'];
353     $email = $_POST['email'];
354
355     $contact->setRole($role);
356     $contact->setForename($forename);
357     $contact->setMiddle($middle);
358     $contact->setSurname($surname);
359     $contact->setDisplayname($displayname);
360     $contact->setTelephone1($telephone1);
361     $contact->setTelephone2($telephone2);
362     $contact->setEmail($email);
363     $contact->setAddressId($address->getId());
364
365     try {
366       $contact->save();
367     }
368     catch (Exception $e) {
369       if ($new) echo "<p>Error adding $displayname.</p>\n";
370       else echo "<p>Error updating $displayname.</p>\n";
371       return false;
372     }
373
374     return true;
375   }
376
377   function add_contact(&$name) {
378     if (! check_admin(1, "add a contact")) return;
379
380     $area_id = $_POST['area_id'];
381     if (! is_numeric($area_id)) {
382       echo "<p>Invalid area!</p>\n";
383       return false;
384     }
385
386     $area = get_area_by_id($area_id);
387     if (! $area) {
388       echo "<p>No such area!</p>\n";
389       return false;
390     }
391
392     $contact = new Contact;
393     if (! update_contact($contact, $area_id, true)) return false;
394     return $contact->getId();
395   }
396
397   function delete_contact($name, $id = null, &$city_id = null) {
398     if (! check_admin(1, "delete a contact")) return;
399
400     if (isset($id)) $contact = get_contact_by_id($id);
401     else $contact = get_contact_by_name($name);
402     if (! $contact) return false;
403
404     ///* Remember city ID for dropdown. */
405     //$city_id = $area->getCityId();
406
407     try {
408       $contact->delete();
409       echo "<p>Deleted contact.</p>\n";
410     }
411     catch (Exception $e) {
412       echo "<p>Error deleting $name!</p>\n";
413       return false;
414     }
415
416     return true;
417   }
418
419   function show_contact($name, &$id = null) {
420     if (isset($id)) $contact = get_contact_by_id($id);
421     else $contact = get_contact_by_name($name);
422     if (! $contact) return;
423
424     echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
425     echo "<p>Contact: <span class=\"strong\">" . $contact->getDisplayname() . "</span>";
426     $role = $contact->getRole();
427     $role_string = get_contact_role_string($contact);
428     if ($role_string) echo " $role_string";
429     if ($role & $GLOBALS['ROLE_DONOR']) printf(" <a class=\"small\" href=\"/donation/from/contact/%s/%d\">Donations</a>", urlencode($contact->getDisplayname()), $contact->getId());
430     if ($role & $GLOBALS['ROLE_REQUESTER']) printf(" <a class=\"small\" href=\"/order/from/referrer/%s/%d\">Referred</a>", urlencode($contact->getDisplayname()), $contact->getId());
431     if ($role & $GLOBALS['ROLE_BENEFICIARY']) printf(" <a class=\"small\" href=\"/order/to/beneficiary/%s/%d\">Orders</a>", urlencode($contact->getDisplayname()), $contact->getId());
432     if (check_admin(1)) {
433       echo " " . $contact->getDeleteLink();
434     }
435     $city = get_contact_city($contact);
436     if ($city) echo " in " . $city->getLink(get_city_displayname($city));
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 ?>