Added contact offers.
[readifood.git] / lib / order.php
1 <?php
2
3   if (isset($_POST['show_add_order'])) {
4     set_last_selected("area_id", $_POST['area_id']);
5     $area_id = $_POST['area_id'];
6     show_new_order_form($area_id);
7   }
8   else if (isset($_POST['add_order'])) {
9     set_last_selected("area_id", $_POST['area_id']);
10     $id = add_order();
11     if ($id !== false) {
12       echo "<p>Order placed.</p>\n";
13       $parameters = array("id", $id);
14     }
15   }
16   else if (isset($_POST['update_order'])) {
17     list($ignored, $id, $args) = parse_parameters($parameters);
18     $q = new OrderQuery;
19     $order = $q->findOneById($id);
20     if ($order) {
21       if (update_order($order) !== false) {
22         echo "<p>Updated order.</p>\n";
23         $parameters = array("id", $order->getId());
24       }
25     }
26     else {
27       echo "<p>No such contact!</p>\n";
28     }
29   }
30   else if ($_POST['show_in_area']) {
31     set_last_selected("area_id", $_POST['area_id']);
32     $q = new AreaQuery;
33     $area = $q->findOneById($_POST['area_id']);
34     header(sprintf("Location: http%s://%s/%s/in/area/%s/%d%s%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($area->getName()), $_POST['area_id'], get_order_state_query_uri(get_order_state_mask(), get_order_date_query_uri($_POST['from'], $_POST['to']))));
35     exit;
36   }
37   else if ($_POST['show_in_city']) {
38     set_last_selected("city_id", $_POST['city_id']);
39     $q = new CityQuery;
40     $city = $q->findOneById($_POST['city_id']);
41     header(sprintf("Location: http%s://%s/%s/in/city/%s/%d%s%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($city->getName()), $_POST['city_id'], get_order_state_query_uri(get_order_state_mask()), get_order_date_query_uri($_POST['from'], $_POST['to'])));
42     exit;
43   }
44
45   function show_orders($city_id = null, $area_id = null, $requester_ids = null, $beneficiary_ids = null, $state_mask = null, $from = null, $to = null) {
46     list($first_page, $per_page) = pagination();
47     /* XXX: Use Propel methods. */
48     if (isset($state_mask)) $order_ids = get_order_ids_by_state($state_mask);
49     $q = new OrderQuery;
50     if (isset($requester_ids)) $q->filterByRequesterId($requester_ids);
51     if (isset($beneficiary_ids)) $q->filterByBeneficiaryId($beneficiary_ids);
52     # XXX: Doesn't work.
53     #if (isset($state_mask)) $q->useOrderStateQuery()->addSelectQuery($latest_state, 'latestState')->where("order_id=latestState.order_id")->where("state & $state_mask")->endUse();
54     if (isset($city_id) || isset($area_id)) {
55       $q->joinWith('Order.Beneficiary');
56       $q->joinWith('Beneficiary.Address');
57       if (isset($area_id)) $q->where('Address.AreaId=?', $area_id);
58       if (isset($city_id)) {
59         $q->joinWith('Address.Area');
60         $q->where('Area.CityId=?', $city_id);
61       }
62     }
63     if (isset($state_mask)) $q->filterById($order_ids);
64     if (isset($from)) $q->where('Order.Date >= ?', $from);
65     if (isset($to)) $q->where('Order.Date <= ?', $to);
66     $orders = $q->orderByDate('desc')->orderById('desc')->paginate($first_page, $per_page);
67     if (count($orders)) {
68       foreach ($orders as $order) {
69         echo "<br>\n" . get_order_summary($order) . "<br>\n";
70       }
71       show_pagination($orders);
72     }
73     else echo " none";
74   }
75
76   function show_city_orders($city_name, $city_id = null, $state_mask = null, $from = null, $to = null) {
77     if (isset($city_id)) $city = get_city_by_id($city_id);
78     else if ($city_name) $city = get_city_by_name($city_name);
79     if ($city) {
80       echo "<p>Orders in city " . $city->getLink(get_city_displayname($city)) . ":";
81       return show_orders($city->getId(), null, null, null, $state_mask, $from, $to);
82     }
83     else echo "<p>No such city!</p>\n";
84   }
85
86   function show_requester_orders($contact_name, $contact_id = null, $state_mask = null, $from = null, $to = null) {
87     if (isset($contact_id)) $contact = get_contact_by_id($contact_id);
88     else if ($contact_name) $contact = get_contact_by_name($contact_name);
89     if ($contact) {
90       echo "<p>Orders from referrer " . $contact->getLink() . ":";
91       return show_orders(null, null, $contact->getId(), null, $state_mask, $from, $to);
92     }
93     else echo "<p>No such contact!</p>\n";
94   }
95
96   function show_beneficiary_orders($contact_name, $contact_id = null, $state_mask = null, $from = null, $to = null) {
97     if (isset($contact_id)) $contact = get_contact_by_id($contact_id);
98     else if ($contact_name) $contact = get_contact_by_name($contact_name);
99     if ($contact) {
100       echo "<p>Orders to beneficiary " . $contact->getLink() . ":";
101       return show_orders(null, null, null, $contact->getId(), $state_mask, $from, $to);
102     }
103     else echo "<p>No such contact!</p>\n";
104   }
105
106   function show_area_orders($area_name, $area_id = null, $state_mask = null, $from = null, $to = null) {
107     if (isset($area_id)) $area = get_area_by_id($area_id);
108     else if ($area_name) $area = get_area_by_name($area_name);
109     if ($area) {
110       echo "<p>Orders in area " . $area->getLink() . ":";
111       return show_orders(null, $area->getId(), null, null, $state_mask, $from, $to);
112     }
113     else echo "<p>No such area!</p>\n";
114   }
115
116   function show_order_state_form($state_mask = null) {
117     global $states, $all_states;
118
119     if (is_null($state_mask)) $state_mask = $all_states;
120
121     echo "<p>Restrict to order states:\n";
122     for ($i = 0; $i < count($states); $i++) {
123       echo " <input type=\"checkbox\" id=\"state_$i\" name=\"state_$i\"";
124       if ($state_mask & (1 << $i)) echo " checked";
125       echo "><label for=\"state_$i\">$states[$i]</label>\n";
126     }
127     echo "</p>\n";
128   }
129
130   function get_order_state_mask($string = null) {
131     global $states, $all_states;
132
133     $mask = 0;
134
135     if (isset($string)) {
136       $selected = explode("+", $string);
137       for ($i = 0; $i < count($states); $i++) {
138         if (in_array($states[$i], $selected)) $mask |= (1 << $i);
139       }
140     }
141     else {
142       for ($i = 0; $i < count($states); $i++) {
143         if ($_POST['state_' . $i] == "on") $mask |= (1 << $i);
144       }
145     }
146
147     if (! $mask) $mask = $all_states;
148     return $mask;
149   }
150
151   function get_order_state_query_string($mask) {
152     global $states;
153
154     $selected = array();
155
156     for ($i = 0; $i < count($states); $i++) {
157       if ($mask & (1 << $i)) $selected[] = $states[$i];
158     }
159
160     return implode("+", $selected);
161   }
162
163   function get_order_state_query_uri($mask) {
164     global $all_states;
165
166     if (is_null($mask)) return "";
167     if ($mask == $all_states) return "";
168
169     return "/state/" . get_order_state_query_string($mask);
170   }
171
172   function get_order_date_query_uri($from, $to) {
173     $uri = "";
174     if ($from) $uri .= "/from/$from";
175     if ($to) $uri .= "/to/$to";
176
177     if (! $uri) return "";
178
179     return "/dated$uri";
180   }
181
182   function show_order_areas_form($city_id = null) {
183     $areas = get_city_areas($city_id);
184     if (! count($areas)) {
185       echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
186       return;
187     }
188
189     $candidates = array();
190     foreach ($areas as $area) {
191       if (! count(get_area_contacts($area->getId()))) continue;
192       $candidates[] = $area;
193     }
194     if (! count($candidates)) return;
195
196     echo "<p>Show orders in area\n";
197     echo "<select name=\"area_id\">\n";
198     foreach ($candidates as $area) {
199       option("area_id", $area->getId(), get_area_displayname($area));
200     }
201     echo "</select>\n";
202     echo "<input type=\"submit\" name=\"show_in_area\" value=\"Show\">\n";
203   }
204
205   function show_order_cities_form($city_id = null) {
206     $q = new CityQuery;
207     $cities = $q->orderByName()->find();
208
209     if (! count($cities)) {
210       echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
211       return;
212     }
213
214     $candidates = array();
215     foreach ($cities as $city) {
216       if (! count(get_city_contacts($city->getId()))) continue;
217       $candidates[] = $city;
218     }
219     if (! count($candidates)) return;
220
221     echo "<p>Show orders in city\n";
222     echo "<select name=\"city_id\">\n";
223     foreach ($candidates as $city) {
224       option("city_id", $city->getId(), get_city_displayname($city), $city_id);
225     }
226     echo "</select>\n";
227     echo "<input type=\"submit\" name=\"show_in_city\" value=\"Show\">\n";
228   }
229
230   function show_order_forms($city_id, $state_mask, $from = null, $to = null) {
231     form("noprint standout");
232     show_order_state_form($state_mask);
233     echo "<p>Restrict to orders for delivery between ";
234     datepicker("from", $from, true, null, false, "to");
235     echo " and ";
236     datepicker("to", $to, true, "from", false);
237     show_order_areas_form($city_id);
238     show_order_cities_form($city_id);
239     end_form();
240   }
241
242   function show_order_form($order = null, $area_id = null) {
243     global $states, $parcel_sizes, $parcel_contents;
244
245     if ($order) {
246       $order_state = get_order_state($order);
247       if ($order_state) {
248         $state = $order_state->getState();
249         $driver_id = $order_state->getDriverId();
250       }
251     }
252     else $order = new Order;
253
254     /* Date. */
255     echo "<tr>\n";
256     echo "  <td>Delivery</td>\n";
257     /* XXX: Find suitable dates from area. */
258     echo "  <td>";
259     show_date_form("date", $order->getDate());
260     if (! $order->getDate()) {
261       echo " and recur for <select name=\"recurrence\">\n";
262       for ($i = 0; $i < 4; $i++) option("recurrence", $i, $i);
263       echo "</select> weeks";
264     }
265     echo "</td>\n";
266     echo "</tr>\n";
267
268     /* Referrer. */
269     echo "<tr>\n";
270     echo "  <td>Referrer</td>\n";
271     echo "  <td><select name=\"requester_id\">\n";
272     option("requester_id", null, "");
273     $contacts = get_area_requesters();
274     foreach ($contacts as $contact) {
275       option("requester_id", $contact->getId(), $contact->getDisplayname(), $order->getRequesterId());
276     }
277     echo "</select>";
278     $contact = get_contact_by_id($order->getRequesterId(), false);
279     if ($contact) echo " " . get_small_link($contact->getDisplayname(), $contact->getURL());
280     echo "</td>\n";
281     echo "</tr>\n";
282
283     /* Beneficiary. */
284     echo "<tr>\n";
285     echo "  <td>Beneficiary</td>\n";
286     echo "  <td><select name=\"beneficiary_id\">\n";
287     option("beneficiary_id", null, "");
288     if (! $order->getId() && $order->getBeneficiaryId()) {
289       $contact = get_contact_by_id($order->getBeneficiaryId());
290       if ($contact) option("beneficiary_id", $order->getBeneficiaryId(), $contact->getDisplayname(), $order->getBeneficiaryId());
291     }
292     else {
293       $contacts = get_area_beneficiaries($area_id);
294       foreach ($contacts as $contact) {
295         option("beneficiary_id", $contact->getId(), $contact->getDisplayname(), $order->getBeneficiaryId());
296       }
297     }
298     echo "</select>";
299     $contact = get_contact_by_id($order->getBeneficiaryId(), false);
300     if ($contact) echo " " . get_small_link($contact->getDisplayname(), $contact->getURL());
301     echo "</td>\n";
302     echo "</tr>\n";
303
304     /* Hub. */
305     echo "<tr>\n";
306     echo "  <td>Hub</td>\n";
307     echo "  <td><select name=\"hub_id\">\n";
308     option("hub_id", null, "");
309     $hubs = get_area_hubs();
310     foreach ($hubs as $hub) {
311       option("hub_id", $hub->getId(), $hub->getDisplayname(), $order->getHubId());
312     }
313     echo "</select>";
314     $hub = get_hub_by_id($order->getHubId(), false);
315     if ($hub) echo " " . get_small_link($hub->getDisplayname(), $hub->getURL());
316     echo "</td>\n";
317     echo "</tr>\n";
318
319     /* Parcel type. */
320     echo "<tr>\n";
321     echo "  <td>Parcel size</td>\n";
322     echo "  <td><select name=\"parcel_size\">\n";
323     $mask = 1 << count($parcel_sizes);
324     for ($i = 0; $i < count($parcel_sizes); $i++) {
325       option("parcel_size", 1 << $i, $parcel_sizes[$i], $order->getParcel() % $mask);
326     }
327     echo "</select></td>\n";
328     echo "</tr>\n";
329
330     /* Parcel contents. */
331     echo "<tr>\n";
332     echo "  <td>Parcel contents</td>\n";
333     echo "  <td>";
334     for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
335       echo "  <input type=\"checkbox\" id=\"parcel_$i\" name=\"parcel_$i\"";
336       if ($order->getParcel() & (1 << $i)) echo " checked";
337       echo "><label for=\"parcel_$i\">$parcel_contents[$i]</label>\n";
338     }
339     echo "</td>\n";
340     echo "</tr>\n";
341
342     /* Notes. */
343     echo "<tr>\n";
344     echo "  <td>Notes</td>\n";
345     echo "  <td>"; textarea("notes", $order->getNotes()); echo "</td>\n";
346     echo "</tr>\n";
347
348     /* Driver. */
349     echo "<tr>\n";
350     echo "  <td>Driver</td>\n";
351     $contacts = get_city_drivers();
352     if (count($contacts)) {
353       echo "  <td><select name=\"driver_id\">\n";
354       option("driver_id", null, "");
355       foreach ($contacts as $contact) {
356         option("driver_id", $contact->getId(), $contact->getDisplayname(), $driver_id);
357       }
358       echo "</select>";
359       $contact = get_contact_by_id($driver_id, false);
360       if ($contact) echo " " . get_small_link($contact->getDisplayname(), $contact->getURL());
361       echo "</td>\n";
362     }
363     else echo "  <td>No drivers!</td>\n";
364     echo "</tr>\n";
365
366     /* State. */
367     if ($order->getId()) {
368       echo "<tr>\n";
369       echo "  <td>State</td>\n";
370       echo "  <td><select name=\"state\">\n";
371       for ($i = 0; $i < count($states); $i++) {
372         option("state", 1 << $i, ucfirst($states[$i]), $state);
373       }
374       echo "</select></td>\n";
375       echo "</tr>\n";
376     }
377   }
378
379   function show_new_order_form($area_id = null) {
380     if (! check_admin(1)) return;
381
382     $area = get_area_by_id($area_id);
383     if (! count($area)) {
384       echo "<p>No such <a href=\"/area\">area</a>!</p>\n";
385       return;
386     }
387
388     form("noprint");
389     echo "<p>Place an order:</p>\n";
390
391     echo "<table>\n";
392     show_order_form(null, $area_id);
393
394     echo "<tr>\n";
395     echo "  <td colspan=2>"; submit("add_order", "Order"); echo "</td></tr>\n";
396     echo "</tr>\n";
397     echo "</table>\n";
398     end_form();
399   }
400
401   function show_contact_order_form($contact) {
402     if (! check_admin(1)) return;
403
404     $area = get_contact_area($contact);
405     if (! $area) {
406       echo "<p>No valid <a href=\"/area\">area</a> for contact!</p>\n";
407       return;
408     }
409
410     $order = new Order;
411     $order->setBeneficiaryId($contact->getId());
412
413     form("standout");
414     echo "<p>Placing order for " . $contact->getStrongLink() . ".";
415     $parcel = $contact->getParcel();
416     if ($parcel) {
417       echo "  Suggested parcel type is <span class=\"strong\">" .  get_contact_parcel_string($contact) . "</span>";
418       $order->setParcel($parcel);
419     }
420     echo "</p>\n";
421
422     echo "<table>\n";
423     show_order_form($order, $area_id);
424
425     echo "<tr>\n";
426     echo "  <td colspan=2>"; submit("add_order", "Order"); echo "</td></tr>\n";
427     echo "</tr>\n";
428     echo "</table>\n";
429     end_form();
430   }
431
432   function show_add_new_order_form() {
433     if (! check_admin(1)) return;
434
435     /* We intentionally hide areas with no contacts. */
436     $areas = get_city_areas_with_contacts(null, $GLOBALS['ROLE_BENEFICIARY']);
437     if (! count($areas)) {
438       echo "<p>Can't place any orders until at least one <a href=\"/area\">area</a> has a <a href=\"/contact\">contact</a>!</p>\n";
439       return;
440     }
441
442     form("noprint standout");
443     echo "<p>Place an order in <select name=\"area_id\">\n";
444     foreach ($areas as $area) {
445       option("area_id", $area->getId(), get_area_displayname($area));
446     }
447     echo "</select>";
448     submit("show_add_order", "Proceed");
449     echo "</p>\n";
450     end_form();
451   }
452
453   function update_order(&$order, $new = false) {
454     global $user_id, $parcel_sizes, $parcel_contents;
455
456     #$date = ymd_to_iso8601("date");
457     $date = $_POST['date'];
458     $requester_id = $_POST['requester_id'];
459     $beneficiary_id = $_POST['beneficiary_id'];
460     $hub_id = $_POST['hub_id'];
461     $driver_id = $_POST['driver_id'];
462     if (! $driver_id) $driver_id = null;
463     $state = $_POST['state'];
464     if (! $state) $state = $GLOBALS['STATE_PLACED'];
465     $parcel = $_POST['parcel_size'];
466     for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
467       if ($_POST['parcel_' . $i] == "on") $parcel |= (1 << $i);
468     }
469     $notes = $_POST['notes'];
470
471     if ($date) {
472       list($y, $m, $d) = explode('-', $date);
473       $then = mktime(0, 0, 0, $m, $d, $y);
474     }
475     else $then = time();
476     /* XXX: check date */
477
478     $requester = get_contact_by_id($requester_id);
479     if (! $requester) {
480       echo "<p>Invalid referrer!</p>\n";
481       return false;
482     }
483
484     $beneficiary = get_contact_by_id($beneficiary_id);
485     if (! $beneficiary) {
486       echo "<p>Invalid beneficiary!</p>\n";
487       return false;
488     }
489
490     if ($hub_id) {
491       $hub = get_hub_by_id($hub_id);
492       if (! $hub) {
493         echo "<p>Invalid hub!</p>\n";
494         return false;
495       }
496     }
497     else $hub_id = null;
498
499     if ($new && isset($_POST['recurrence'])) $recurrence = $_POST['recurrence'];
500     if (! $recurrence) $recurrence = 0;
501
502     $now = time();
503     for ($i = 0; $i <= $recurrence; $i++) {
504       if ($i) {
505         echo "<p>Creating recurrence $i.</p>\n";
506         $order = new Order;
507       }
508
509       $order->setDate($then + 7 * 86400 * $i);
510       $order->setRequesterId($requester_id);
511       $order->setBeneficiaryId($beneficiary_id);
512       $order->setHubId($hub_id);
513       $order->setParcel($parcel);
514       $order->setNotes($notes);
515
516       /* XXX: begin/commit */
517       try {
518         $order->save();
519
520         $order_state = new OrderState;
521         $order_state->setUpdated($now);
522         $order_state->setOrderId($order->getId());
523         $order_state->setUserId($user_id);
524         $order_state->setDriverId($driver_id);
525         $order_state->setState($state);
526
527         $order_state->save();
528       }
529       catch (Exception $e) {
530         if ($new) echo "<p>Error placing order.</p>\n";
531         else echo "<p>Error updating order.</p>\n";
532         echo "<p>" . $e->getMessage() . "</p>\n";
533         return false;
534       }
535     }
536
537     return true;
538   }
539
540   function add_order() {
541     if (! check_admin(1, "place an order")) return;
542
543     $order = new Order;
544     if (! update_order($order, true)) return false;
545     return $order->getId();
546   }
547
548   function confirm_delete_order($id = null) {
549     if (! check_admin(1, "delete an order")) return;
550
551     if (isset($id)) $order = get_order_by_id($id);
552     if (! $order) return false;
553
554     echo "<h3>Confirm deletion</h3>\n";
555     echo "<p>You must confirm deletion of order $id: " . $order->getDeleteLink(true) . "</p>\n";
556   }
557
558   function delete_order($id = null) {
559     if (! check_admin(1, "delete an order")) return;
560
561     if (isset($id)) $order = get_order_by_id($id);
562     if (! $order) return false;
563
564     try {
565       $q = new OrderStateQuery;
566       $order_states = $q->filterByOrderId($id)->find();
567       foreach ($order_states as $order_state) $order_state->delete();
568       $order->delete();
569       echo "<p>Deleted order.</p>\n";
570     }
571     catch (Exception $e) {
572       echo "<p>Error deleting order $id!</p>\n";
573       return false;
574     }
575
576     return true;
577   }
578
579   function show_order_history($id) {
580     global $states;
581
582     $q = new OrderStateQuery();
583     $order_states = $q->filterByOrderId($id)->orderById()->find();
584
585     if (! count($order_states)) return;
586
587     echo "<h3>Order history</h3>\n";
588     echo "<p class=\"history\">\n";
589     foreach ($order_states as $order_state) {
590       $date = $order_state->getUpdated();
591
592       $user = get_contact_by_id($order_state->getUserId());
593       if ($user) $username = $user->getDisplayname();
594       else $username = "unknown user";
595
596       $driver_id = $order_state->getDriverId();
597       if ($driver_id) $driver = get_contact_by_id($driver_id);
598       else $driver = null;
599
600       echo "<strong>$username</strong> changed order to state <strong>" . get_order_state_string($order_state) . "</strong>";
601       if ($driver) echo " for driver " . $driver->getDisplayname();
602       echo " on $date.<br>\n";
603     }
604     echo "</p>\n";
605   }
606
607   function show_order(&$id = null) {
608     if (isset($id)) $order = get_order_by_id($id);
609     if (! $order) return;
610
611     form();
612     echo "<p>Order: <span class=\"strong\">" . $order->getId() . "</span>";
613     if (check_admin(1)) {
614       echo " " . $order->getDeleteLink();
615     }
616     echo ": ";
617     echo "\n</p>";
618
619     echo "<table>\n";
620     show_order_form($order);
621
622     if (check_admin(1)) {
623       echo "<tr>\n";
624       echo "  <td colspan=2>";
625       submit("update_order", "Update");
626       echo "</td>\n";
627       echo "</tr>\n";
628     }
629
630     echo "</table>\n";
631     end_form();
632
633     show_order_history($order->getId());
634   }
635
636   $state_mask = null;
637   if (count($parameters)) {
638     for ($i = 1; $i < count($parameters); $i++) {
639       if ($parameters[$i] == "state") {
640         /* /order/state/placed+picked */
641         $state_mask = get_order_state_mask($parameters[$i + 1]);
642       }
643     }
644
645     for ($i = 1; $i < count($parameters); $i++) {
646       if ($parameters[$i] == "dated") {
647         for ($j = $i + 1; $j < count($parameters) - 1; $j++) {
648           if ($parameters[$j] == "from") $from = $parameters[$j + 1];
649           if ($parameters[$j] == "to") $to = $parameters[$j + 1];
650         }
651
652         if ($from || $to) check_dates('order', $from, $to, false, false);
653         break;
654       }
655     }
656
657     if ($parameters[0] == "in") {
658       /* /order/in/area/Romsey+Town/1 */
659       switch ($parameters[1]) {
660       case "area":
661         case "area":
662           $area_id = $parameters[3];
663           $_POST['area_id'] = $area_id;
664           $q = new AreaQuery;
665           $area = $q->findOneById($area_id);
666           $city = get_area_city($area);
667           if ($city) $city_id = $city->getId();
668           show_area_orders($parameters[2], $area_id, $state_mask, $from, $to);
669         break;
670
671         case "city":
672           $city_id = $parameters[3];
673           $_POST['city_id'] = $city_id;
674           $q = new CityQuery;
675           $city = $q->findOneById($city_id);
676           show_city_orders($parameters[2], $city_id, $state_mask, $from, $to);
677         break;
678       }
679     }
680     else if ($parameters[0] == "from") {
681       /* /order/from/referrer/Iain+Patterson/4 */
682       switch ($parameters[1]) {
683         case "referrer":
684           $contact_id = $parameters[3];
685           $q = new ContactQuery;
686           $contact = $q->findOneById($contact_id);
687           show_requester_orders($parameters[2], $contact_id, $state_mask, $from, $to);
688         break;
689       }
690     }
691     else if ($parameters[0] == "to") {
692       /* /order/to/beneficiary/Cambridge+Community+Church/1 */
693       switch ($parameters[1]) {
694         case "beneficiary":
695           $contact_id = $parameters[3];
696           $q = new ContactQuery;
697           $hub = $q->findOneById($contact_id);
698           show_beneficiary_orders($parameters[2], $contact_id, $state_mask, $from, $to);
699         break;
700       }
701     }
702     else if ($parameters[0] == "place") {
703       if ($parameters[1] == "for") {
704         if ($parameters[2] == "beneficiary") {
705           if ($parameters[4]) $contact = get_contact_by_id($parameters[4]);
706           if (! $contact) $contact = get_contact_by_name(urldecode($parameters[3]));
707           if ($contact) show_contact_order_form($contact);
708         }
709       }
710     }
711   }
712   list($ignored, $id, $args) = parse_parameters($parameters);
713   //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
714   if (count($args)) {
715     switch ($args[0]) {
716       case "delete":
717         confirm_delete_order($id);
718       break;
719
720       case "confirmdelete":
721         delete_order($id);
722       break;
723     }
724   }
725   else if (isset($id)) show_order($id);
726   else if ($state_mask) show_orders(null, null, null, null, $state_mask, $from, $to);
727
728   show_order_forms($city_id, $state_mask, $from, $to);
729   show_add_new_order_form($city_id);
730
731
732 ?>