From: Iain Patterson Insufficient privileges to $complaint.
Areas in " . $city->getLink(get_city_displayname($city)) . ":";
+ $q = new AreaQuery;
+ $areas = $q->filterByCityId($city_id)->find();
+
+ if (count($areas)) {
+ foreach ($areas as $area) {
+ echo "
\nArea: " . $area->getStrongLink();
+ printf(" Contacts", urlencode($area->getName()), $area->getId());
+ printf(" Donations", urlencode($area->getName()), $area->getId());
+ printf(" Orders", urlencode($area->getName()), $area->getId());
+ if (check_admin(1)) {
+ echo " " . $area->getDeleteLink();
+ }
+ }
+ }
+ else echo " none";
+ echo "
No such city!
\n"; + } + } + + function show_area_cities_form($city_id = null) { + $q = new CityQuery; + $cities = $q->find(); + if (! count($cities)) { + echo "No cities!
\n"; + return; + } + + echo "\n"; + } + + function show_new_area_form($city_id = null) { + if (! check_admin(1)) return; + + $q = new CityQuery; + $cities = $q->find(); + if (! count($cities)) { + echo "No cities!
\n"; + return; + } + + echo "\n"; + } + + function add_area($name, $city_id) { + if (! check_admin(1, "add an area")) return; + + $name = urldecode($name); + $area = get_area_by_name($name, false); + if ($area) { + echo "$name already exists!
\n"; + show_area($name); + return false; + } + + $city = get_city_by_id($city_id); + if (! $city) { + echo "Not a valid city!
\n"; + return false; + } + + $area = new Area; + $area->setName($name); + $area->setCityId($city_id); + + try { + $area->save(); + } + catch (Exception $e) { + echo "Error adding $name!
\n"; + /* XXX: Why? */ + return false; + } + + return $area->getId(); + } + + function delete_city($name) { + if (! check_admin(1, "delete a city")) return; + + $city = get_city_by_name($name); + if (! $city) return false; + + try { + $city->delete(); + } + catch (Exception $e) { + echo "Error deleting $name!
\n"; + /* XXX: Why? Check for addresses in use... */ + return false; + } + + return true; + } + + function show_area_delivery_days_form($days) { + global $week; + echo "Delivery days:"; + if (check_admin(1)) { + for ($i = 0; $i < count($week); $i++) { + echo " $week[$i]\n"; + } + } + else { + if (! $days) echo " none"; + else { + for ($i = 0; $i < count($week); $i++) { + if ($days & (1 << $i)) echo " $week[$i]"; + } + } + } + } + + function update_area_delivery_days($name) { + global $week; + + if (! check_admin(1, "edit an area")) return false; + + $days = 0; + for ($i = 0; $i < count($week); $i++) { + if ($_POST['day_' . $i] == "on") $days |= (1 << $i); + } + + $area = get_area_by_name($name); + if (! $area) return false; + + $area->setDays($days); + + try { + $area->save(); + } + catch (Exception $e) { + echo "Error updating area!
\n"; + return false; + } + + echo "Updated area.
\n"; + return true; + } + + function delete_area($name, $id = null, &$city_id = null) { + if (! check_admin(1, "delete a area")) return; + + if (isset($id)) $area = get_area_by_id($id); + else $area = get_area_by_name($name); + if (! $area) return false; + + /* Remember city ID for dropdown. */ + $city_id = $area->getCityId(); + + try { + $area->delete(); + echo "Deleted area.
\n"; + } + catch (Exception $e) { + echo "Error deleting $name!
\n"; + /* XXX: Why? Check for addresses in use... */ + return false; + } + + return true; + } + + function show_area($name, &$city_id = null) { + $area = get_area_by_name($name); + if (! $area) return; + + echo "\n"; + } + + /* /area/in/Cambridge/1 */ + if (count($parameters)) { + if ($parameters[0] == "in") { + $city_id = $parameters[2]; + show_areas(0, 10, $parameters[1], $city_id); + show_new_area_form($city_id); + } + } + list($name, $id, $args) = parse_parameters($parameters); + //echo "$name($id) " . print_r($args, true) . "
\n"; + if (count($args)) { + switch ($args[0]) { + case "delete": + delete_area($name, $id, $city_id); + break; + } + } + else if (isset($name)) show_area($name, $city_id); + else { + show_area_cities_form($city_id); + show_new_area_form($city_id); + } + + if (count($parameters)) + show_area_cities_form($city_id); +?> diff --git a/lib/city.php b/lib/city.php new file mode 100644 index 0000000..29f2c75 --- /dev/null +++ b/lib/city.php @@ -0,0 +1,145 @@ +Added city.\n"; + $parameters = array($_POST['city_name'], $id); + } + } + + function show_cities($offset, $per_page, $name = null) { + echo "Cities:";
+ $q = new CityQuery;
+ if (isset($name)) $q->filterByName($name);
+ $p = $q->paginate($offset, $per_page);
+ if (count($p)) {
+ foreach ($p as $city) {
+ echo "
\nCity: " . $city->getStrongLink(get_city_displayname($city));
+ printf(" Areas", urlencode($city->getName()), $city->getID());
+ printf(" Contacts", urlencode($city->getName()), $city->getId());
+ printf(" Donations", urlencode($city->getName()), $city->getId());
+ printf(" Orders", urlencode($city->getName()), $city->getId());
+ if (check_admin(1)) {
+ echo " " . $city->getDeleteLink();
+ }
+ }
+ }
+ else echo " none";
+ echo "
No cities!
\n"; + return; + } + + echo "\n"; + } + + function show_contact_search_form() { + echo "\n"; + } + + function show_contact_forms($city_id) { + show_contact_areas_form($city_id); + show_contact_cities_form($city_id); + show_contact_search_form(); + } + + function show_contact_role_form($role) { + global $roles; + + for ($i = 0; $i < count($roles); $i++) { + echo " $roles[$i]\n"; + } + } + + function show_contact_form($contact = null) { + global $roles; + + if (! $contact) $contact = new Contact; + + /* Role. */ + echo "No areas!
\n"; + return; + } + + echo "\n"; + } + + function show_add_new_contact_form() { + if (! check_admin(1)) return; + + $q = new CityQuery; + $cities = $q->find(); + if (! count($cities)) { + echo "No cities!
\n"; + return; + } + + echo "\n"; + } + + function update_contact(&$contact, $area_id, $new = false) { + global $roles; + + $role = 0; + for ($i = 0; $i < count($roles); $i++) { + if ($_POST['role_' . $i] == "on") $role |= (1 << $i); + } + + /* Staff can place orders. */ + if ($role & (1 << 0)) $role |= (1 << 2); + + $forename = $_POST['forename']; + $middle = $_POST['middle']; + $surname = $_POST['surname']; + $displayname = $_POST['displayname']; + + if (! $forename && ! $surname) { + echo "Must have either a forename or surname!
\n"; + return false; + } + if ($middle && ! ($forename && $surname)) { + echo "Must have both a forename or surname for middle name(s) to make sense!
\n"; + return false; + } + + if (! $displayname) { + $displayname = $forename; + if ($middle) $displayname .= " $middle"; + if ($forename) $displayname .= " "; + $displayname .= $surname; + echo "Display name will be $displayname.
\n"; + } + + /* Get address. */ + $line = $_POST['address']; + $postcode = $_POST['postcode']; + $q = new AddressQuery; + /* XXX: Finding by area properly? */ + $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate(); + if ($address->isNew()) { + /* Changing address. */ + //if (! $new) + /* + XXX: Check for other contacts at the old address. + Make this a new address if there are others, but + provide a link to update other contacts. + */ + try { + $address->save(); + } + catch (Exception $e) { + echo "Error adding $line.
\n"; + return false; + } + } + + $telephone1 = $_POST['telephone1']; + $telephone2 = $_POST['telephone2']; + $email = $_POST['email']; + + $contact->setRole($role); + $contact->setForename($forename); + $contact->setMiddle($middle); + $contact->setSurname($surname); + $contact->setDisplayname($displayname); + $contact->setTelephone1($telephone1); + $contact->setTelephone2($telephone2); + $contact->setEmail($email); + $contact->setAddressId($address->getId()); + + try { + $contact->save(); + } + catch (Exception $e) { + if ($new) echo "Error adding $displayname.
\n"; + else echo "Error updating $displayname.
\n"; + return false; + } + + return true; + } + + function add_contact(&$name) { + if (! check_admin(1, "add a contact")) return; + + $area_id = $_POST['area_id']; + if (! is_numeric($area_id)) { + echo "Invalid area!
\n"; + return false; + } + + $area = get_area_by_id($area_id); + if (! $area) { + echo "No such area!
\n"; + return false; + } + + $contact = new Contact; + if (! update_contact($contact, $area_id, true)) return false; + return $contact->getId(); + } + + function delete_contact($name, $id = null, &$city_id = null) { + if (! check_admin(1, "delete a contact")) return; + + if (isset($id)) $contact = get_contact_by_id($id); + else $contact = get_contact_by_name($name); + if (! $contact) return false; + + ///* Remember city ID for dropdown. */ + //$city_id = $area->getCityId(); + + try { + $contact->delete(); + echo "Deleted contact.
\n"; + } + catch (Exception $e) { + echo "Error deleting $name!
\n"; + return false; + } + + return true; + } + + function show_contact($name, &$id = null) { + if (isset($id)) $contact = get_contact_by_id($id); + else $contact = get_contact_by_name($name); + if (! $contact) return; + + echo "\n"; + } + + /* /contact/in/area/Cambridge/1 */ + if (count($parameters)) { + if ($parameters[0] == "in") { + switch ($parameters[1]) { + 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_contacts(0, 10, $parameters[2], $area_id); + break; + + case "city": + $city_id = $parameters[3]; + $_POST['city_id'] = $city_id; + $q = new CityQuery; + $city = $q->findOneById($city_id); + show_city_contacts(0, 10, $parameters[2], $city_id); + break; + } + + show_add_new_contact_form($city_id); + } + else if ($parameters[0] == "search") { + search_contacts(0, 10, $parameters[1]); + } + } + list($name, $id, $args) = parse_parameters($parameters); + //echo "$name($id) " . print_r($args, true) . "
\n"; + if (count($args)) { + switch ($args[0]) { + case "delete": + delete_contact($name, $id); + break; + } + } + else if (isset($name)) show_contact($name, $id); + else { + /* XXX: Shown after adding. */ + show_contact_forms($city_id); + show_add_new_contact_form($city_id); + } + + if (count($parameters)) { + show_contact_forms($city_id); + } + +?> diff --git a/lib/delivery.php b/lib/delivery.php new file mode 100644 index 0000000..03495df --- /dev/null +++ b/lib/delivery.php @@ -0,0 +1,122 @@ +filterByDate(time('Y-m-d'))->find(); + if (count($orders)) { + foreach ($orders as $order) $order_ids[] = $order->getId(); + } + else echo "No deliveries today.
\n"; + + return $order_ids; + } + + /* Find drivers with deliveries today. */ + function get_drivers_by_order_id($order_ids) { + $driver_ids = array(); + + if (count($order_ids)) { + $dbh = Propel::getConnection(); + $sth = $dbh->prepare("select * from OrderState o where updated=(select max(updated) from OrderState where order_id=o.order_id) and order_id in (" . implode(",", $order_ids) . ") and driver_id is not null"); + $sth->execute(); + $order_states = OrderStatePeer::populateObjects($sth); + if (count($order_states)) { + foreach ($order_states as $order_state) $driver_ids[] = $order_state->getDriverId(); + } + else echo "No drivers assigned for deliveries.
\n"; + } + + return $driver_ids; + } + + /* Find schedule for a driver today. */ + function get_driver_schedule_by_order_id($driver_id, $all_order_ids) { + $order_ids = array(); + + if (! count($all_order_ids)) { + echo "No orders for today.
\n"; + return null; + } + + $dbh = Propel::getConnection(); + $sth = $dbh->prepare("select * from OrderState o where updated=(select max(updated) from OrderState where order_id=o.order_id) and order_id in (" . implode(",", $all_order_ids) . ") and driver_id=$driver_id"); + $sth->execute(); + $order_states = OrderStatePeer::populateObjects($sth); + if (count($order_states)) { + foreach ($order_states as $order_state) $order_ids[] = $order_state->getOrderId(); + } + else echo "No deliveries for this driver.
\n"; + + return $order_ids; + } + + function show_driver_forms($driver_ids) { + global $module; + + if (! count($driver_ids)) return; + + $q = new ContactQuery(); + $contacts = $q->filterById($driver_ids)->find(); + if (! count($contacts)) { + echo "Can't find drivers!
\n"; + return; + } + + echo "Drivers with deliveries scheduled:";
+ foreach ($contacts as $contact) {
+ printf("
\n%s", urlencode($contact->getDisplayname()), $contact->getId(), htmlspecialchars($contact->getDisplayname()));
+ }
+ }
+
+ function show_driver_schedule($driver_name = null, $driver_id = null) {
+ if (isset($driver_id)) $contact = get_contact_by_id($driver_id);
+ else if (isset($driver_name)) $contact = get_contact_by_name($driver_name);
+ if (! $contact) {
+ echo "
No such driver!
\n"; + return; + } + + echo "Order of " . $order->getQuantity() . "kg for " . htmlspecialchars($contact->getDisplayname()) . " in " . htmlspecialchars(get_area_displayname($area)) . ".
\n"; + $hub = get_hub_by_id($order->getHubId(), false); + if ($hub) { + echo "Deliver to hub " . htmlspecialchars($hub->getName()) . ""; + $address = get_hub_address($hub); + } + else { + echo "
Deliver direct to beneficiary";
+ $address = get_contact_address($contact);
+ }
+ $area = get_address_area($address);
+
+ echo " in " . htmlspecialchars($area->getName()) . " at:
";
+ $city = get_area_city($area);
+ echo "\n
" . htmlspecialchars($address->getLine());
+ echo "\n
" . htmlspecialchars($city->getName());
+ echo "\n
" . htmlspecialchars($address->getPostcode());
+ echo "
Updated donation.
\n"; + $parameters = array("id", $donation->getId()); + } + } + else { + echo "No such contact!
\n"; + } + } + else if ($_POST['area_id']) { + $q = new AreaQuery; + $area = $q->findOneById($_POST['area_id']); + /* XXX: Function to build URL because we need to set a class in links. */ + header(sprintf("Location: http%s://%s/%s/in/area/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($area->getName()), $_POST['area_id'])); + exit; + } + else if ($_POST['city_id']) { + $q = new CityQuery; + $city = $q->findOneById($_POST['city_id']); + header(sprintf("Location: http%s://%s/%s/in/city/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($city->getName()), $_POST['city_id'])); + exit; + } + + function show_donations($offset, $per_page, $contact_ids = null, $hub_ids = null) { + $q = new DonationQuery; + if (isset($contact_ids)) $q->filterByContactId($contact_ids); + if (isset($hub_ids)) $q->filterByHubId($hub_ids); + $donations = $q->find(); + if (count($donations)) { + foreach ($donations as $donation) { + echo "Donations in city " . $city->getLink(get_city_displayname($city)) . ":"; + return show_donations($offset, $per_page, null, $hub_ids); + } + else echo "
No such city!
\n"; + } + + function show_contact_donations($offset, $per_page, $contact_name, $contact_id = 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 "Donations from contact " . $contact->getLink() . ":"; + return show_donations($offset, $per_page, $contact->getId()); + } + else echo "
No such contact!
\n"; + } + + function show_hub_donations($offset, $per_page, $hub_name, $hub_id = null) { + if (isset($hub_id)) $hub = get_hub_by_id($hub_id); + else if ($hub_name) $hub = get_hub_by_name($hub_name); + if ($hub) { + echo "Donations to hub " . $hub->getLink() . ":"; + return show_donations($offset, $per_page, null, $hub->getId()); + } + else echo "
No such hub!
\n"; + } + + function show_area_donations($offset, $per_page, $area_name, $area_id = 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) { + $hubs = get_area_hubs($area->getId()); + $hub_ids = array(); + foreach ($hubs as $hub) $hub_ids[] = $hub->getId(); + + echo "Donations in area " . $area->getLink() . ":"; + return show_donations($offset, $per_page, null, $hub_ids); + } + else echo "
No such area!
\n"; + } + + function show_donation_areas_form($city_id = null) { + $areas = get_city_areas($city_id); + if (! count($areas)) { + echo "No areas!
\n"; + return; + } + + echo "\n"; + } + + function show_donation_cities_form($city_id = null) { + $q = new CityQuery; + $cities = $q->orderByName()->find(); + + if (! count($cities)) { + echo "No cities!
\n"; + return; + } + + echo "\n"; + } + + function show_donation_forms($city_id) { + show_donation_areas_form($city_id); + show_donation_cities_form($city_id); + } + + function show_donation_form($donation = null, $area_id = null) { + if (! $donation) $donation = new Donation; + + /* Date. */ + echo "No such area!
\n"; + return; + } + + echo "\n"; + } + + function show_add_new_donation_form() { + if (! check_admin(1)) return; + + /* We intentionally hide areas with no hubs. */ + $areas = get_city_areas_with_hubs(); + if (! count($areas)) { + echo "Can't record any donations until at least one area has a hub!
\n"; + return; + } + + echo "\n"; + } + + function update_donation(&$donation, $new = false) { + #$date = ymd_to_iso8601("date"); + $date = $_POST['date']; + $contact_id = $_POST['contact_id']; + $hub_id = $_POST['hub_id']; + $quantity = $_POST['quantity']; + + if (! $date) $date = time(); + /* XXX: check date */ + + $contact = get_contact_by_id($contact_id); + if (! $contact) { + echo "Invalid contact!
\n"; + return false; + } + + $hub = get_hub_by_id($hub_id); + if (! $hub) { + echo "Invalid hub!
\n"; + return false; + } + + if (! is_numeric($quantity)) { + echo "Invalid quantity!
\n"; + return false; + } + + $donation->setDate($date); + $donation->setContactId($contact_id); + $donation->setHubId($hub_id); + $donation->setQuantity($quantity); + + try { + $donation->save(); + } + catch (Exception $e) { + if ($new) echo "Error recording donation.
\n"; + else echo "Error updating donation.
\n"; + return false; + } + + return true; + } + + function add_donation() { + if (! check_admin(1, "record a donation")) return; + + $donation = new Donation; + if (! update_donation($donation, true)) return false; + return $donation->getId(); + } + + function delete_donation($id = null) { + if (! check_admin(1, "delete a donation")) return; + + if (isset($id)) $donation = get_donation_by_id($id); + if (! $donation) return false; + + try { + $donation->delete(); + echo "Deleted donation.
\n"; + } + catch (Exception $e) { + echo "Error deleting donation $id!
\n"; + return false; + } + + return true; + } + + function show_donation(&$id = null) { + if (isset($id)) $donation = get_donation_by_id($id); + if (! $donation) return; + + echo "\n"; + } + + if (count($parameters)) { + if ($parameters[0] == "in") { + /* /donation/in/area/Romsey+Town/1 */ + switch ($parameters[1]) { + 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_donations(0, 10, $parameters[2], $area_id); + break; + + case "city": + $city_id = $parameters[3]; + $_POST['city_id'] = $city_id; + $q = new CityQuery; + $city = $q->findOneById($city_id); + show_city_donations(0, 10, $parameters[2], $city_id); + break; + } + } + else if ($parameters[0] == "from") { + /* /donation/from/contact/Iain+Patterson/4 */ + switch ($parameters[1]) { + case "contact": + $contact_id = $parameters[3]; + $q = new ContactQuery; + $contact = $q->findOneById($contact_id); + show_contact_donations(0, 10, $parameters[2], $contact_id); + break; + } + } + else if ($parameters[0] == "to") { + /* /donation/to/hub/Cambridge+Community+Church/1 */ + switch ($parameters[1]) { + case "hub": + $hub_id = $parameters[3]; + $q = new HubQuery; + $hub = $q->findOneById($hub_id); + show_hub_donations(0, 10, $parameters[2], $hub_id); + break; + } + } + } + list($ignored, $id, $args) = parse_parameters($parameters); + //echo "$name($id) " . print_r($args, true) . "
\n"; + if (count($args)) { + switch ($args[0]) { + case "delete": + delete_donation($id); + break; + } + } + else if (isset($id)) show_donation($id); + else { + /* XXX: Shown after adding. */ + show_donation_forms($city_id); + show_add_new_donation_form($city_id); + } + + if (count($parameters)) { + show_donation_forms($city_id); + } + + +?> diff --git a/lib/footer.php b/lib/footer.php new file mode 100644 index 0000000..9943ff0 --- /dev/null +++ b/lib/footer.php @@ -0,0 +1,3 @@ + +