Order placed.

\n"; $parameters = array("id", $id); } } else if (isset($_POST['update_order'])) { list($ignored, $id, $args) = parse_parameters($parameters); $q = new OrderQuery; $order = $q->findOneById($id); if ($order) { if (update_order($order) !== false) { echo "

Updated order.

\n"; $parameters = array("id", $order->getId()); } } else { echo "

No such contact!

\n"; } } else if ($_POST['show_in_area']) { set_last_selected("area_id", $_POST['area_id']); $q = new AreaQuery; $area = $q->findOneById($_POST['area_id']); 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()))); exit; } else if ($_POST['show_in_city']) { set_last_selected("city_id", $_POST['city_id']); $q = new CityQuery; $city = $q->findOneById($_POST['city_id']); 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()))); exit; } function show_orders($offset, $per_page, $requester_ids = null, $beneficiary_ids = null, $state_mask = null) { list($offset, $per_page) = pagination(); /* XXX: Use Propel methods. */ if (isset($state_mask)) $order_ids = get_order_ids_by_state($state_mask); $q = new OrderQuery; if (isset($requester_ids)) $q->filterByRequesterId($requester_ids); if (isset($beneficiary_ids)) $q->filterByBeneficiaryId($beneficiary_ids); # XXX: Doesn't work. #if (isset($state_mask)) $q->useOrderStateQuery()->addSelectQuery($latest_state, 'latestState')->where("order_id=latestState.order_id")->where("state & $state_mask")->endUse(); if (isset($state_mask)) $q->filterById($order_ids); $orders = $q->orderByDate('desc')->orderById('desc')->paginate($offset, $per_page); if (count($orders)) { foreach ($orders as $order) { echo "
\n" . get_order_summary($order) . "
\n"; } show_pagination($orders); } else echo " none"; } function show_city_orders($offset, $per_page, $city_name, $city_id = null, $state_mask = null) { if (isset($city_id)) $city = get_city_by_id($city_id); else if ($city_name) $city = get_city_by_name($city_name); if ($city) { $contacts = get_city_contacts($city->getId(), $GLOBALS['ROLE_BENEFICIARY']); $beneficiary_ids = array(); foreach ($contacts as $contact) $beneficiary_ids[] = $contact->getId(); echo "

Orders in city " . $city->getLink(get_city_displayname($city)) . ":"; return show_orders($offset, $per_page, null, $beneficiary_ids, $state_mask); } else echo "

No such city!

\n"; } function show_requester_orders($offset, $per_page, $contact_name, $contact_id = null, $state_mask = null) { if (isset($contact_id)) $contact = get_contact_by_id($contact_id); else if ($contact_name) $contact = get_contact_by_name($contact_name); if ($contact) { echo "

Orders from referrer " . $contact->getLink() . ":"; return show_orders($offset, $per_page, $contact->getId(), null, $state_mask); } else echo "

No such contact!

\n"; } function show_beneficiary_orders($offset, $per_page, $contact_name, $contact_id = null, $state_mask = null) { if (isset($contact_id)) $contact = get_contact_by_id($contact_id); else if ($contact_name) $contact = get_contact_by_name($contact_name); if ($contact) { echo "

Orders to beneficiary " . $contact->getLink() . ":"; return show_orders($offset, $per_page, null, $contact->getId(), $state_mask); } else echo "

No such contact!

\n"; } function show_area_orders($offset, $per_page, $area_name, $area_id = null, $state_mask = null) { if (isset($area_id)) $area = get_area_by_id($area_id); else if ($area_name) $area = get_area_by_name($area_name); if ($area) { $contacts = get_area_contacts($area->getId(), $GLOBALS['ROLE_BENEFICIARY']); $contact_ids = array(); foreach ($contacts as $contact) $contact_ids[] = $contact->getId(); echo "

Orders in area " . $area->getLink() . ":"; return show_orders($offset, $per_page, null, $contact_ids, $state_mask); } else echo "

No such area!

\n"; } function show_order_state_form($state_mask = null) { global $states, $all_states; if (is_null($state_mask)) $state_mask = $all_states; echo "

Restrict to order states:\n"; for ($i = 0; $i < count($states); $i++) { echo " \n"; } echo "

\n"; } function get_order_state_mask($string = null) { global $states, $all_states; $mask = 0; if (isset($string)) { $selected = explode("+", $string); for ($i = 0; $i < count($states); $i++) { if (in_array($states[$i], $selected)) $mask |= (1 << $i); } } else { for ($i = 0; $i < count($states); $i++) { if ($_POST['state_' . $i] == "on") $mask |= (1 << $i); } } if (! $mask) $mask = $all_states; return $mask; } function get_order_state_query_string($mask) { global $states; $selected = array(); for ($i = 0; $i < count($states); $i++) { if ($mask & (1 << $i)) $selected[] = $states[$i]; } return implode("+", $selected); } function get_order_state_query_uri($mask) { global $all_states; if (is_null($mask)) return ""; if ($mask == $all_states) return ""; return "/state/" . get_order_state_query_string($mask); } function show_order_areas_form($city_id = null) { $areas = get_city_areas($city_id); if (! count($areas)) { echo "

No areas!

\n"; return; } $candidates = array(); foreach ($areas as $area) { if (! count(get_area_contacts($area->getId()))) continue; $candidates[] = $area; } if (! count($candidates)) return; echo "

Show orders in area\n"; echo "\n"; echo "\n"; } function show_order_cities_form($city_id = null) { $q = new CityQuery; $cities = $q->orderByName()->find(); if (! count($cities)) { echo "

No cities!

\n"; return; } $candidates = array(); foreach ($cities as $city) { if (! count(get_city_contacts($city->getId()))) continue; $candidates[] = $city; } if (! count($candidates)) return; echo "

Show orders in city\n"; echo "\n"; echo "\n"; } function show_order_forms($city_id, $state_mask) { form("noprint standout"); show_order_state_form($state_mask); show_order_areas_form($city_id); show_order_cities_form($city_id); end_form(); } function show_order_form($order = null, $area_id = null) { global $states, $parcel_sizes, $parcel_contents; if ($order) { $order_state = get_order_state($order); if ($order_state) { $state = $order_state->getState(); $driver_id = $order_state->getDriverId(); } } else $order = new Order; /* Date. */ echo "\n"; echo " Delivery\n"; /* XXX: Find suitable dates from area. */ echo " "; show_date_form("date", $order->getDate()); if (! $order->getDate()) { echo " and recur for weeks"; } echo "\n"; echo "\n"; /* Referrer. */ echo "\n"; echo " Referrer\n"; echo " "; $contact = get_contact_by_id($order->getRequesterId(), false); if ($contact) echo " " . get_small_link($contact->getDisplayname(), $contact->getURL()); echo "\n"; echo "\n"; /* Beneficiary. */ echo "\n"; echo " Beneficiary\n"; echo " "; $contact = get_contact_by_id($order->getBeneficiaryId(), false); if ($contact) echo " " . get_small_link($contact->getDisplayname(), $contact->getURL()); echo "\n"; echo "\n"; /* Hub. */ echo "\n"; echo " Hub\n"; echo " "; $hub = get_hub_by_id($order->getHubId(), false); if ($hub) echo " " . get_small_link($hub->getDisplayname(), $hub->getURL()); echo "\n"; echo "\n"; /* Parcel type. */ echo "\n"; echo " Parcel size\n"; echo " \n"; echo "\n"; /* Parcel contents. */ echo "\n"; echo " Parcel contents\n"; echo " "; for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) { echo " getParcel() & (1 << $i)) echo " checked"; echo ">\n"; } echo "\n"; echo "\n"; /* Notes. */ echo "\n"; echo " Notes\n"; echo " "; textarea("notes", $order->getNotes()); echo "\n"; echo "\n"; /* Driver. */ echo "\n"; echo " Driver\n"; $contacts = get_city_drivers(); if (count($contacts)) { echo " "; $contact = get_contact_by_id($driver_id, false); if ($contact) echo " " . get_small_link($contact->getDisplayname(), $contact->getURL()); echo "\n"; } else echo " No drivers!\n"; echo "\n"; /* State. */ if ($order->getId()) { echo "\n"; echo " State\n"; echo " \n"; echo "\n"; } } function show_new_order_form($area_id = null) { if (! check_admin(1)) return; $area = get_area_by_id($area_id); if (! count($area)) { echo "

No such area!

\n"; return; } form("noprint"); echo "

Place an order:

\n"; echo "\n"; show_order_form(null, $area_id); echo "\n"; echo " \n"; echo "\n"; echo "
"; submit("add_order", "Order"); echo "
\n"; end_form(); } function show_contact_order_form($contact) { if (! check_admin(1)) return; $area = get_contact_area($contact); if (! $area) { echo "

No valid area for contact!

\n"; return; } $order = new Order; $order->setBeneficiaryId($contact->getId()); form("standout"); echo "

Placing order for " . $contact->getStrongLink() . "."; $parcel = $contact->getParcel(); if ($parcel) { echo " Suggested parcel type is " . get_contact_parcel_string($contact) . ""; $order->setParcel($parcel); } echo "

\n"; echo "\n"; show_order_form($order, $area_id); echo "\n"; echo " \n"; echo "\n"; echo "
"; submit("add_order", "Order"); echo "
\n"; end_form(); } function show_add_new_order_form() { if (! check_admin(1)) return; /* We intentionally hide areas with no contacts. */ $areas = get_city_areas_with_contacts(null, $GLOBALS['ROLE_BENEFICIARY']); if (! count($areas)) { echo "

Can't place any orders until at least one area has a contact!

\n"; return; } form("noprint standout"); echo "

Place an order in "; submit("show_add_order", "Proceed"); echo "

\n"; end_form(); } function update_order(&$order, $new = false) { global $user_id, $parcel_sizes, $parcel_contents; #$date = ymd_to_iso8601("date"); $date = $_POST['date']; $requester_id = $_POST['requester_id']; $beneficiary_id = $_POST['beneficiary_id']; $hub_id = $_POST['hub_id']; $driver_id = $_POST['driver_id']; if (! $driver_id) $driver_id = null; $state = $_POST['state']; if (! $state) $state = $GLOBALS['STATE_PLACED']; $parcel = $_POST['parcel_size']; for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) { if ($_POST['parcel_' . $i] == "on") $parcel |= (1 << $i); } $notes = $_POST['notes']; if ($date) { list($y, $m, $d) = explode('-', $date); $then = mktime(0, 0, 0, $m, $d, $y); } else $then = time(); /* XXX: check date */ $requester = get_contact_by_id($requester_id); if (! $requester) { echo "

Invalid referrer!

\n"; return false; } $beneficiary = get_contact_by_id($beneficiary_id); if (! $beneficiary) { echo "

Invalid beneficiary!

\n"; return false; } if ($hub_id) { $hub = get_hub_by_id($hub_id); if (! $hub) { echo "

Invalid hub!

\n"; return false; } } else $hub_id = null; if ($new && isset($_POST['recurrence'])) $recurrence = $_POST['recurrence']; if (! $recurrence) $recurrence = 0; $now = time(); for ($i = 0; $i <= $recurrence; $i++) { if ($i) { echo "

Creating recurrence $i.

\n"; $order = new Order; } $order->setDate($then + 7 * 86400 * $i); $order->setRequesterId($requester_id); $order->setBeneficiaryId($beneficiary_id); $order->setHubId($hub_id); $order->setParcel($parcel); $order->setNotes($notes); /* XXX: begin/commit */ try { $order->save(); $order_state = new OrderState; $order_state->setUpdated($now); $order_state->setOrderId($order->getId()); $order_state->setUserId($user_id); $order_state->setDriverId($driver_id); $order_state->setState($state); $order_state->save(); } catch (Exception $e) { if ($new) echo "

Error placing order.

\n"; else echo "

Error updating order.

\n"; echo "

" . $e->getMessage() . "

\n"; return false; } } return true; } function add_order() { if (! check_admin(1, "place an order")) return; $order = new Order; if (! update_order($order, true)) return false; return $order->getId(); } function confirm_delete_order($id = null) { if (! check_admin(1, "delete an order")) return; if (isset($id)) $order = get_order_by_id($id); if (! $order) return false; echo "

Confirm deletion

\n"; echo "

You must confirm deletion of order $id: " . $order->getDeleteLink(true) . "

\n"; } function delete_order($id = null) { if (! check_admin(1, "delete an order")) return; if (isset($id)) $order = get_order_by_id($id); if (! $order) return false; try { $q = new OrderStateQuery; $order_states = $q->filterByOrderId($id)->find(); foreach ($order_states as $order_state) $order_state->delete(); $order->delete(); echo "

Deleted order.

\n"; } catch (Exception $e) { echo "

Error deleting order $id!

\n"; return false; } return true; } function show_order_history($id) { global $states; $q = new OrderStateQuery(); $order_states = $q->filterByOrderId($id)->orderById()->find(); if (! count($order_states)) return; echo "

Order history

\n"; echo "

\n"; foreach ($order_states as $order_state) { $date = $order_state->getUpdated(); $user = get_contact_by_id($order_state->getUserId()); if ($user) $username = $user->getDisplayname(); else $username = "unknown user"; $driver_id = $order_state->getDriverId(); if ($driver_id) $driver = get_contact_by_id($driver_id); else $driver = null; echo "$username changed order to state " . get_order_state_string($order_state) . ""; if ($driver) echo " for driver " . $driver->getDisplayname(); echo " on $date.
\n"; } echo "

\n"; } function show_order(&$id = null) { if (isset($id)) $order = get_order_by_id($id); if (! $order) return; form(); echo "

Order: " . $order->getId() . ""; if (check_admin(1)) { echo " " . $order->getDeleteLink(); } echo ": "; echo "\n

"; echo "\n"; show_order_form($order); if (check_admin(1)) { echo "\n"; echo " \n"; echo "\n"; } echo "
"; submit("update_order", "Update"); echo "
\n"; end_form(); show_order_history($order->getId()); } $state_mask = null; if (count($parameters)) { for ($i = 1; $i < count($parameters); $i++) { if ($parameters[$i] == "state") { /* /order/state/placed+picked */ $state_mask = get_order_state_mask($parameters[$i + 1]); } } if ($parameters[0] == "in") { /* /order/in/area/Romsey+Town/1 */ switch ($parameters[1]) { case "area": case "area": $area_id = $parameters[3]; $_POST['area_id'] = $area_id; $q = new AreaQuery; $area = $q->findOneById($area_id); $city = get_area_city($area); if ($city) $city_id = $city->getId(); show_area_orders(0, 10, $parameters[2], $area_id, $state_mask); break; case "city": $city_id = $parameters[3]; $_POST['city_id'] = $city_id; $q = new CityQuery; $city = $q->findOneById($city_id); show_city_orders(0, 10, $parameters[2], $city_id, $state_mask); break; } } else if ($parameters[0] == "from") { /* /order/from/referrer/Iain+Patterson/4 */ switch ($parameters[1]) { case "referrer": $contact_id = $parameters[3]; $q = new ContactQuery; $contact = $q->findOneById($contact_id); show_requester_orders(0, 10, $parameters[2], $contact_id, $state_mask); break; } } else if ($parameters[0] == "to") { /* /order/to/beneficiary/Cambridge+Community+Church/1 */ switch ($parameters[1]) { case "beneficiary": $contact_id = $parameters[3]; $q = new ContactQuery; $hub = $q->findOneById($contact_id); show_beneficiary_orders(0, 10, $parameters[2], $contact_id, $state_mask); break; } } else if ($parameters[0] == "place") { if ($parameters[1] == "for") { if ($parameters[2] == "beneficiary") { if ($parameters[4]) $contact = get_contact_by_id($parameters[4]); if (! $contact) $contact = get_contact_by_name(urldecode($parameters[3])); if ($contact) show_contact_order_form($contact); } } } } list($ignored, $id, $args) = parse_parameters($parameters); //echo "

$name($id) " . print_r($args, true) . "

\n"; if (count($args)) { switch ($args[0]) { case "delete": confirm_delete_order($id); break; case "confirmdelete": delete_order($id); break; } } else if (isset($id)) show_order($id); else if ($state_mask) show_orders(0, 10, null, null, $state_mask); show_order_forms($city_id, $state_mask); show_add_new_order_form($city_id); ?>