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);
8 else if (isset($_POST['add_order'])) {
9 set_last_selected("area_id", $_POST['area_id']);
12 echo "<p>Order placed.</p>\n";
13 $parameters = array("id", $id);
16 else if (isset($_POST['update_order'])) {
17 list($ignored, $id, $args) = parse_parameters($parameters);
19 $order = $q->findOneById($id);
21 if (update_order($order) !== false) {
22 echo "<p>Updated order.</p>\n";
23 $parameters = array("id", $order->getId());
27 echo "<p>No such contact!</p>\n";
30 else if ($_POST['show_in_area']) {
31 set_last_selected("area_id", $_POST['area_id']);
33 $area = $q->findOneById($_POST['area_id']);
34 header(sprintf("Location: http%s://%s/%s/in/area/%s/%d%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($area->getName()), $_POST['area_id'], get_order_state_query_uri(get_order_state_mask())));
37 else if ($_POST['show_in_city']) {
38 set_last_selected("city_id", $_POST['city_id']);
40 $city = $q->findOneById($_POST['city_id']);
41 header(sprintf("Location: http%s://%s/%s/in/city/%s/%d%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($city->getName()), $_POST['city_id'], get_order_state_query_uri(get_order_state_mask())));
45 function show_orders($city_id = null, $area_id = null, $requester_ids = null, $beneficiary_ids = null, $state_mask = 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);
50 if (isset($requester_ids)) $q->filterByRequesterId($requester_ids);
51 if (isset($beneficiary_ids)) $q->filterByBeneficiaryId($beneficiary_ids);
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);
63 if (isset($state_mask)) $q->filterById($order_ids);
64 $orders = $q->orderByDate('desc')->orderById('desc')->paginate($first_page, $per_page);
66 foreach ($orders as $order) {
67 echo "<br>\n" . get_order_summary($order) . "<br>\n";
69 show_pagination($orders);
74 function show_city_orders($city_name, $city_id = null, $state_mask = null) {
75 if (isset($city_id)) $city = get_city_by_id($city_id);
76 else if ($city_name) $city = get_city_by_name($city_name);
78 echo "<p>Orders in city " . $city->getLink(get_city_displayname($city)) . ":";
79 return show_orders($city->getId(), null, null, null, $state_mask);
81 else echo "<p>No such city!</p>\n";
84 function show_requester_orders($contact_name, $contact_id = null, $state_mask = null) {
85 if (isset($contact_id)) $contact = get_contact_by_id($contact_id);
86 else if ($contact_name) $contact = get_contact_by_name($contact_name);
88 echo "<p>Orders from referrer " . $contact->getLink() . ":";
89 return show_orders(null, null, $contact->getId(), null, $state_mask);
91 else echo "<p>No such contact!</p>\n";
94 function show_beneficiary_orders($contact_name, $contact_id = null, $state_mask = null) {
95 if (isset($contact_id)) $contact = get_contact_by_id($contact_id);
96 else if ($contact_name) $contact = get_contact_by_name($contact_name);
98 echo "<p>Orders to beneficiary " . $contact->getLink() . ":";
99 return show_orders(null, null, null, $contact->getId(), $state_mask);
101 else echo "<p>No such contact!</p>\n";
104 function show_area_orders($area_name, $area_id = null, $state_mask = null) {
105 if (isset($area_id)) $area = get_area_by_id($area_id);
106 else if ($area_name) $area = get_area_by_name($area_name);
108 echo "<p>Orders in area " . $area->getLink() . ":";
109 return show_orders(null, $area->getId(), null, null, $state_mask);
111 else echo "<p>No such area!</p>\n";
114 function show_order_state_form($state_mask = null) {
115 global $states, $all_states;
117 if (is_null($state_mask)) $state_mask = $all_states;
119 echo "<p>Restrict to order states:\n";
120 for ($i = 0; $i < count($states); $i++) {
121 echo " <input type=\"checkbox\" id=\"state_$i\" name=\"state_$i\"";
122 if ($state_mask & (1 << $i)) echo " checked";
123 echo "><label for=\"state_$i\">$states[$i]</label>\n";
128 function get_order_state_mask($string = null) {
129 global $states, $all_states;
133 if (isset($string)) {
134 $selected = explode("+", $string);
135 for ($i = 0; $i < count($states); $i++) {
136 if (in_array($states[$i], $selected)) $mask |= (1 << $i);
140 for ($i = 0; $i < count($states); $i++) {
141 if ($_POST['state_' . $i] == "on") $mask |= (1 << $i);
145 if (! $mask) $mask = $all_states;
149 function get_order_state_query_string($mask) {
154 for ($i = 0; $i < count($states); $i++) {
155 if ($mask & (1 << $i)) $selected[] = $states[$i];
158 return implode("+", $selected);
161 function get_order_state_query_uri($mask) {
164 if (is_null($mask)) return "";
165 if ($mask == $all_states) return "";
167 return "/state/" . get_order_state_query_string($mask);
170 function show_order_areas_form($city_id = null) {
171 $areas = get_city_areas($city_id);
172 if (! count($areas)) {
173 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
177 $candidates = array();
178 foreach ($areas as $area) {
179 if (! count(get_area_contacts($area->getId()))) continue;
180 $candidates[] = $area;
182 if (! count($candidates)) return;
184 echo "<p>Show orders in area\n";
185 echo "<select name=\"area_id\">\n";
186 foreach ($candidates as $area) {
187 option("area_id", $area->getId(), get_area_displayname($area));
190 echo "<input type=\"submit\" name=\"show_in_area\" value=\"Show\">\n";
193 function show_order_cities_form($city_id = null) {
195 $cities = $q->orderByName()->find();
197 if (! count($cities)) {
198 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
202 $candidates = array();
203 foreach ($cities as $city) {
204 if (! count(get_city_contacts($city->getId()))) continue;
205 $candidates[] = $city;
207 if (! count($candidates)) return;
209 echo "<p>Show orders in city\n";
210 echo "<select name=\"city_id\">\n";
211 foreach ($candidates as $city) {
212 option("city_id", $city->getId(), get_city_displayname($city), $city_id);
215 echo "<input type=\"submit\" name=\"show_in_city\" value=\"Show\">\n";
218 function show_order_forms($city_id, $state_mask) {
219 form("noprint standout");
220 show_order_state_form($state_mask);
221 show_order_areas_form($city_id);
222 show_order_cities_form($city_id);
226 function show_order_form($order = null, $area_id = null) {
227 global $states, $parcel_sizes, $parcel_contents;
230 $order_state = get_order_state($order);
232 $state = $order_state->getState();
233 $driver_id = $order_state->getDriverId();
236 else $order = new Order;
240 echo " <td>Delivery</td>\n";
241 /* XXX: Find suitable dates from area. */
243 show_date_form("date", $order->getDate());
244 if (! $order->getDate()) {
245 echo " and recur for <select name=\"recurrence\">\n";
246 for ($i = 0; $i < 4; $i++) option("recurrence", $i, $i);
247 echo "</select> weeks";
254 echo " <td>Referrer</td>\n";
255 echo " <td><select name=\"requester_id\">\n";
256 option("requester_id", null, "");
257 $contacts = get_area_requesters();
258 foreach ($contacts as $contact) {
259 option("requester_id", $contact->getId(), $contact->getDisplayname(), $order->getRequesterId());
262 $contact = get_contact_by_id($order->getRequesterId(), false);
263 if ($contact) echo " " . get_small_link($contact->getDisplayname(), $contact->getURL());
269 echo " <td>Beneficiary</td>\n";
270 echo " <td><select name=\"beneficiary_id\">\n";
271 option("beneficiary_id", null, "");
272 if (! $order->getId() && $order->getBeneficiaryId()) {
273 $contact = get_contact_by_id($order->getBeneficiaryId());
274 if ($contact) option("beneficiary_id", $order->getBeneficiaryId(), $contact->getDisplayname(), $order->getBeneficiaryId());
277 $contacts = get_area_beneficiaries($area_id);
278 foreach ($contacts as $contact) {
279 option("beneficiary_id", $contact->getId(), $contact->getDisplayname(), $order->getBeneficiaryId());
283 $contact = get_contact_by_id($order->getBeneficiaryId(), false);
284 if ($contact) echo " " . get_small_link($contact->getDisplayname(), $contact->getURL());
290 echo " <td>Hub</td>\n";
291 echo " <td><select name=\"hub_id\">\n";
292 option("hub_id", null, "");
293 $hubs = get_area_hubs();
294 foreach ($hubs as $hub) {
295 option("hub_id", $hub->getId(), $hub->getDisplayname(), $order->getHubId());
298 $hub = get_hub_by_id($order->getHubId(), false);
299 if ($hub) echo " " . get_small_link($hub->getDisplayname(), $hub->getURL());
305 echo " <td>Parcel size</td>\n";
306 echo " <td><select name=\"parcel_size\">\n";
307 $mask = 1 << count($parcel_sizes);
308 for ($i = 0; $i < count($parcel_sizes); $i++) {
309 option("parcel_size", 1 << $i, $parcel_sizes[$i], $order->getParcel() % $mask);
311 echo "</select></td>\n";
314 /* Parcel contents. */
316 echo " <td>Parcel contents</td>\n";
318 for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
319 echo " <input type=\"checkbox\" id=\"parcel_$i\" name=\"parcel_$i\"";
320 if ($order->getParcel() & (1 << $i)) echo " checked";
321 echo "><label for=\"parcel_$i\">$parcel_contents[$i]</label>\n";
328 echo " <td>Notes</td>\n";
329 echo " <td>"; textarea("notes", $order->getNotes()); echo "</td>\n";
334 echo " <td>Driver</td>\n";
335 $contacts = get_city_drivers();
336 if (count($contacts)) {
337 echo " <td><select name=\"driver_id\">\n";
338 option("driver_id", null, "");
339 foreach ($contacts as $contact) {
340 option("driver_id", $contact->getId(), $contact->getDisplayname(), $driver_id);
343 $contact = get_contact_by_id($driver_id, false);
344 if ($contact) echo " " . get_small_link($contact->getDisplayname(), $contact->getURL());
347 else echo " <td>No drivers!</td>\n";
351 if ($order->getId()) {
353 echo " <td>State</td>\n";
354 echo " <td><select name=\"state\">\n";
355 for ($i = 0; $i < count($states); $i++) {
356 option("state", 1 << $i, ucfirst($states[$i]), $state);
358 echo "</select></td>\n";
363 function show_new_order_form($area_id = null) {
364 if (! check_admin(1)) return;
366 $area = get_area_by_id($area_id);
367 if (! count($area)) {
368 echo "<p>No such <a href=\"/area\">area</a>!</p>\n";
373 echo "<p>Place an order:</p>\n";
376 show_order_form(null, $area_id);
379 echo " <td colspan=2>"; submit("add_order", "Order"); echo "</td></tr>\n";
385 function show_contact_order_form($contact) {
386 if (! check_admin(1)) return;
388 $area = get_contact_area($contact);
390 echo "<p>No valid <a href=\"/area\">area</a> for contact!</p>\n";
395 $order->setBeneficiaryId($contact->getId());
398 echo "<p>Placing order for " . $contact->getStrongLink() . ".";
399 $parcel = $contact->getParcel();
401 echo " Suggested parcel type is <span class=\"strong\">" . get_contact_parcel_string($contact) . "</span>";
402 $order->setParcel($parcel);
407 show_order_form($order, $area_id);
410 echo " <td colspan=2>"; submit("add_order", "Order"); echo "</td></tr>\n";
416 function show_add_new_order_form() {
417 if (! check_admin(1)) return;
419 /* We intentionally hide areas with no contacts. */
420 $areas = get_city_areas_with_contacts(null, $GLOBALS['ROLE_BENEFICIARY']);
421 if (! count($areas)) {
422 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";
426 form("noprint standout");
427 echo "<p>Place an order in <select name=\"area_id\">\n";
428 foreach ($areas as $area) {
429 option("area_id", $area->getId(), get_area_displayname($area));
432 submit("show_add_order", "Proceed");
437 function update_order(&$order, $new = false) {
438 global $user_id, $parcel_sizes, $parcel_contents;
440 #$date = ymd_to_iso8601("date");
441 $date = $_POST['date'];
442 $requester_id = $_POST['requester_id'];
443 $beneficiary_id = $_POST['beneficiary_id'];
444 $hub_id = $_POST['hub_id'];
445 $driver_id = $_POST['driver_id'];
446 if (! $driver_id) $driver_id = null;
447 $state = $_POST['state'];
448 if (! $state) $state = $GLOBALS['STATE_PLACED'];
449 $parcel = $_POST['parcel_size'];
450 for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
451 if ($_POST['parcel_' . $i] == "on") $parcel |= (1 << $i);
453 $notes = $_POST['notes'];
456 list($y, $m, $d) = explode('-', $date);
457 $then = mktime(0, 0, 0, $m, $d, $y);
460 /* XXX: check date */
462 $requester = get_contact_by_id($requester_id);
464 echo "<p>Invalid referrer!</p>\n";
468 $beneficiary = get_contact_by_id($beneficiary_id);
469 if (! $beneficiary) {
470 echo "<p>Invalid beneficiary!</p>\n";
475 $hub = get_hub_by_id($hub_id);
477 echo "<p>Invalid hub!</p>\n";
483 if ($new && isset($_POST['recurrence'])) $recurrence = $_POST['recurrence'];
484 if (! $recurrence) $recurrence = 0;
487 for ($i = 0; $i <= $recurrence; $i++) {
489 echo "<p>Creating recurrence $i.</p>\n";
493 $order->setDate($then + 7 * 86400 * $i);
494 $order->setRequesterId($requester_id);
495 $order->setBeneficiaryId($beneficiary_id);
496 $order->setHubId($hub_id);
497 $order->setParcel($parcel);
498 $order->setNotes($notes);
500 /* XXX: begin/commit */
504 $order_state = new OrderState;
505 $order_state->setUpdated($now);
506 $order_state->setOrderId($order->getId());
507 $order_state->setUserId($user_id);
508 $order_state->setDriverId($driver_id);
509 $order_state->setState($state);
511 $order_state->save();
513 catch (Exception $e) {
514 if ($new) echo "<p>Error placing order.</p>\n";
515 else echo "<p>Error updating order.</p>\n";
516 echo "<p>" . $e->getMessage() . "</p>\n";
524 function add_order() {
525 if (! check_admin(1, "place an order")) return;
528 if (! update_order($order, true)) return false;
529 return $order->getId();
532 function confirm_delete_order($id = null) {
533 if (! check_admin(1, "delete an order")) return;
535 if (isset($id)) $order = get_order_by_id($id);
536 if (! $order) return false;
538 echo "<h3>Confirm deletion</h3>\n";
539 echo "<p>You must confirm deletion of order $id: " . $order->getDeleteLink(true) . "</p>\n";
542 function delete_order($id = null) {
543 if (! check_admin(1, "delete an order")) return;
545 if (isset($id)) $order = get_order_by_id($id);
546 if (! $order) return false;
549 $q = new OrderStateQuery;
550 $order_states = $q->filterByOrderId($id)->find();
551 foreach ($order_states as $order_state) $order_state->delete();
553 echo "<p>Deleted order.</p>\n";
555 catch (Exception $e) {
556 echo "<p>Error deleting order $id!</p>\n";
563 function show_order_history($id) {
566 $q = new OrderStateQuery();
567 $order_states = $q->filterByOrderId($id)->orderById()->find();
569 if (! count($order_states)) return;
571 echo "<h3>Order history</h3>\n";
572 echo "<p class=\"history\">\n";
573 foreach ($order_states as $order_state) {
574 $date = $order_state->getUpdated();
576 $user = get_contact_by_id($order_state->getUserId());
577 if ($user) $username = $user->getDisplayname();
578 else $username = "unknown user";
580 $driver_id = $order_state->getDriverId();
581 if ($driver_id) $driver = get_contact_by_id($driver_id);
584 echo "<strong>$username</strong> changed order to state <strong>" . get_order_state_string($order_state) . "</strong>";
585 if ($driver) echo " for driver " . $driver->getDisplayname();
586 echo " on $date.<br>\n";
591 function show_order(&$id = null) {
592 if (isset($id)) $order = get_order_by_id($id);
593 if (! $order) return;
596 echo "<p>Order: <span class=\"strong\">" . $order->getId() . "</span>";
597 if (check_admin(1)) {
598 echo " " . $order->getDeleteLink();
604 show_order_form($order);
606 if (check_admin(1)) {
608 echo " <td colspan=2>";
609 submit("update_order", "Update");
617 show_order_history($order->getId());
621 if (count($parameters)) {
622 for ($i = 1; $i < count($parameters); $i++) {
623 if ($parameters[$i] == "state") {
624 /* /order/state/placed+picked */
625 $state_mask = get_order_state_mask($parameters[$i + 1]);
629 if ($parameters[0] == "in") {
630 /* /order/in/area/Romsey+Town/1 */
631 switch ($parameters[1]) {
634 $area_id = $parameters[3];
635 $_POST['area_id'] = $area_id;
637 $area = $q->findOneById($area_id);
638 $city = get_area_city($area);
639 if ($city) $city_id = $city->getId();
640 show_area_orders($parameters[2], $area_id, $state_mask);
644 $city_id = $parameters[3];
645 $_POST['city_id'] = $city_id;
647 $city = $q->findOneById($city_id);
648 show_city_orders($parameters[2], $city_id, $state_mask);
652 else if ($parameters[0] == "from") {
653 /* /order/from/referrer/Iain+Patterson/4 */
654 switch ($parameters[1]) {
656 $contact_id = $parameters[3];
657 $q = new ContactQuery;
658 $contact = $q->findOneById($contact_id);
659 show_requester_orders($parameters[2], $contact_id, $state_mask);
663 else if ($parameters[0] == "to") {
664 /* /order/to/beneficiary/Cambridge+Community+Church/1 */
665 switch ($parameters[1]) {
667 $contact_id = $parameters[3];
668 $q = new ContactQuery;
669 $hub = $q->findOneById($contact_id);
670 show_beneficiary_orders($parameters[2], $contact_id, $state_mask);
674 else if ($parameters[0] == "place") {
675 if ($parameters[1] == "for") {
676 if ($parameters[2] == "beneficiary") {
677 if ($parameters[4]) $contact = get_contact_by_id($parameters[4]);
678 if (! $contact) $contact = get_contact_by_name(urldecode($parameters[3]));
679 if ($contact) show_contact_order_form($contact);
684 list($ignored, $id, $args) = parse_parameters($parameters);
685 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
689 confirm_delete_order($id);
692 case "confirmdelete":
697 else if (isset($id)) show_order($id);
698 else if ($state_mask) show_orders(null, null, null, null, $state_mask);
700 show_order_forms($city_id, $state_mask);
701 show_add_new_order_form($city_id);