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']) { $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_uri(get_order_state_mask()))); exit; } else if ($_POST['show_in_city']) { $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_uri(get_order_state_mask()))); exit; } function show_orders($offset, $per_page, $requester_ids = null, $beneficiary_ids = null, $state_mask = null) { /* XXX: Use Propel methods. */ if (isset($state_mask)) { $order_ids = array(); $dbh = Propel::getConnection(); $sth = $dbh->prepare("select * from OrderState o where updated=(select max(updated) from OrderState where order_id=o.order_id) and state & $state_mask"); $sth->execute(); $order_states = OrderStatePeer::populateObjects($sth); foreach ($order_states as $order_state) $order_ids[] = $order_state->getOrderId(); } $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()->find(); if (count($orders)) { foreach ($orders as $order) { 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 " $states[$i]\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_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_uri($mask) { global $all_states; if (is_null($mask)) return ""; if ($mask == $all_states) return ""; return "/state/" . get_order_state_string($mask); } function show_order_areas_form($city_id = null) { $areas = get_city_areas($city_id); if (! count($areas)) { echo "No areas!
\n"; 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; } echo "Show orders in city\n"; echo "\n"; echo "\n"; } function show_order_forms($city_id, $state_mask) { echo "
\n"; } function show_order_form($order = null, $area_id = null) { global $states, $parcel_sizes, $parcel_contents; if ($order) { $q = new OrderStateQuery; $order_state = $q->filterByOrderId($order->getId())->orderByUpdated('desc')->limit(1)->findOne(); if ($order_state) { $state = $order_state->getState(); $driver_id = $order_state->getDriverId(); } } else $order = new Order; /* Date. */ echo "No such area!
\n"; return; } echo "\n"; } 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()); echo "\n"; } 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; } echo "\n"; } 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); } 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); /* 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 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 "$username changed order to state $state"; if ($driver) echo " for driver " . $driver->getDisplayname(); echo " on $date.
\n"; } } function show_order(&$id = null) { if (isset($id)) $order = get_order_by_id($id); if (! $order) return; echo "\n"; 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": delete_order($id); break; } } else if (isset($id)) show_order($id); else if ($state_mask) show_orders(0, 10, null, null, $state_mask); else { /* XXX: Shown after adding. */ show_order_forms($city_id, $state_mask); show_add_new_order_form($city_id); } if (count($parameters)) { show_order_forms($city_id, $state_mask); } ?>