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($requester_ids = null, $beneficiary_ids = null, $state_mask = null) { list($first_page, $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($first_page, $per_page); if (count($orders)) { foreach ($orders as $order) { echo "Orders in city " . $city->getLink(get_city_displayname($city)) . ":"; return show_orders(null, $beneficiary_ids, $state_mask); } else echo "
No such city!
\n"; } function show_requester_orders($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($contact->getId(), null, $state_mask); } else echo "
No such contact!
\n"; } function show_beneficiary_orders($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(null, $contact->getId(), $state_mask); } else echo "
No such contact!
\n"; } function show_area_orders($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(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 "
No such area!
\n"; return; } form("noprint"); echo "Place an order:
\n"; echo ""; submit("add_order", "Order"); 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 ""; submit("add_order", "Order"); 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 "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 "\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 "
Order: " . $order->getId() . ""; if (check_admin(1)) { echo " " . $order->getDeleteLink(); } echo ": "; echo "\n
"; echo ""; submit("update_order", "Update"); echo " | \n"; 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(null, null, $state_mask); show_order_forms($city_id, $state_mask); show_add_new_order_form($city_id); ?>