From 6fdbfe32798452ec1c0fdbda57edabb892518409 Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Tue, 9 Apr 2013 11:44:41 -0400 Subject: [PATCH] Go live. --- .gitignore | 3 + Makefile | 2 + lib/admin.php | 10 + lib/area.php | 261 +++++++++ lib/city.php | 145 +++++ lib/constants.php | 25 + lib/contact.php | 510 +++++++++++++++++ lib/delivery.php | 122 ++++ lib/donation.php | 411 ++++++++++++++ lib/footer.php | 3 + lib/forms.php | 33 ++ lib/functions.php | 378 ++++++++++++ lib/header.php | 24 + lib/hub.php | 403 +++++++++++++ lib/menu.php | 39 ++ lib/order.php | 600 ++++++++++++++++++++ propel/build.properties | 3 + propel/build/classes/ReadifoodObject.php | 30 + propel/build/classes/readifood/Contact.php | 22 + propel/build/classes/readifood/Donation.php | 22 + propel/build/classes/readifood/Hub.php | 21 + propel/build/classes/readifood/Order.php | 22 + propel/schema.xml | 165 ++++++ www/.htaccess | 4 + www/index.php | 48 ++ www/logo.png | Bin 0 -> 3868 bytes www/style.css | 61 ++ 27 files changed, 3367 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 lib/admin.php create mode 100644 lib/area.php create mode 100644 lib/city.php create mode 100644 lib/constants.php create mode 100644 lib/contact.php create mode 100644 lib/delivery.php create mode 100644 lib/donation.php create mode 100644 lib/footer.php create mode 100644 lib/forms.php create mode 100644 lib/functions.php create mode 100644 lib/header.php create mode 100644 lib/hub.php create mode 100644 lib/menu.php create mode 100644 lib/order.php create mode 100644 propel/build.properties create mode 100644 propel/build/classes/ReadifoodObject.php create mode 100644 propel/build/classes/readifood/Contact.php create mode 100644 propel/build/classes/readifood/Donation.php create mode 100644 propel/build/classes/readifood/Hub.php create mode 100644 propel/build/classes/readifood/Order.php create mode 100644 propel/schema.xml create mode 100644 www/.htaccess create mode 100644 www/index.php create mode 100644 www/logo.png create mode 100644 www/style.css diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d20bcbf --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +propel/build/ +propel/schema-transformed.xml +propel/runtime-conf.xml diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..cfa96a4 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +all: + cd propel && propel-gen diff --git a/lib/admin.php b/lib/admin.php new file mode 100644 index 0000000..39f6db4 --- /dev/null +++ b/lib/admin.php @@ -0,0 +1,10 @@ += $level) return true; + if (isset($complaint)) echo "

Insufficient privileges to $complaint.

\n"; + return false; + } + +?> diff --git a/lib/area.php b/lib/area.php new file mode 100644 index 0000000..5785ed2 --- /dev/null +++ b/lib/area.php @@ -0,0 +1,261 @@ +Added area.

\n"; + $parameters = array($_POST['area_name'], $id); + } + } + else if ($_POST['city_id']) { + /* XXX: city_id is actually a string $city_name/$city_id */ + header(sprintf("Location: http%s://%s/%s/in/%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, $_POST['city_id'])); + exit; + } + else if ($_POST['update_area']) { + update_area_delivery_days($parameters[0]); + } + + function show_areas($offset, $per_page, $city_name = null, $city_id = null) { + if (isset($city_name) || isset($city_id)) { + if (isset($city_id)) $city = get_city_by_id($city_id); + else if ($city_name) $city = get_city_by_name($city_name); + if ($city) { + echo "

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 "

\n"; + } + else 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"; + echo "

Show areas in\n"; + echo "\n"; + echo "\n"; + 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"; + echo "

Add a new area \n"; + echo "in \n"; + echo "\n"; + 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"; + echo "

Area: " . $area->getName() . ""; + 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(); + } + $city = get_city_by_id($area->getCityId()); + if ($city) { + /* Remember city ID for dropdown. */ + $city_id = $city->getId(); + echo " in " . $city->getLink(get_city_displayname($city)); + } + echo ": "; + echo "\n
"; + show_area_delivery_days_form($area->getDays()); + if (check_admin(1)) { + echo "\n"; + } + echo "

\n"; + 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 "

\n"; + } + + function show_new_city_form() { + if (! check_admin(1)) return; + + echo "
\n"; + echo "

Add a new city \n"; + echo "with postcode prefix \n"; + echo "

\n"; + } + + function add_city($name, $postcode) { + if (! check_admin(1, "add a city")) return; + + if (preg_match('/^([A-Za-z]+)/', $postcode, $m)) { + $prefix = strtoupper($m[1]); + } + else { + echo "

Invalid postcode prefix!

\n"; + return false; + } + + $city = get_city_by_name($name, $prefix, false); + if ($city) { + echo "

$name already exists!

\n"; + show_city($name, $city->getId()); + return false; + } + + $city = new City; + $city->setName($name); + $city->setPostcodeArea($prefix); + + try { + $city->save(); + } + catch (Exception $e) { + echo "

Error adding $name!

\n"; + /* XXX: Why? */ + return false; + } + + return $city->getId(); + } + + function delete_city($name, $id = null) { + if (! check_admin(1, "delete a city")) return; + + if (isset($id)) $city = get_city_by_id($id); + else $city = get_city_by_name($name); + if (! $city) return false; + + try { + $city->delete(); + echo "

Deleted city.

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

Error deleting $name!

\n"; + /* XXX: Why? Check for addresses in use... */ + return false; + } + + return true; + } + + function show_city($name, $id = null) { + if (isset($id)) $city = get_city_by_id($id); + else $city = get_city_by_name($name); + if (! $city) return; + + echo "

City: " . 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(); + } + + $q = new AreaQuery; + $areas = $q->filterByCityId($city->getId())->find(); + if (count($areas)) { + foreach ($areas as $area) { + echo "
\nArea: " . $area->getLink(); + } + } + + echo "

\n"; + } + + list($name, $id, $args) = parse_parameters($parameters); + //echo "

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

\n"; + if (count($args)) { + switch ($args[0]) { + case "delete": + delete_city($name, $id); + break; + + default: + show_cities(0, 10, $name); + show_new_city_form(); + break; + } + } + else if (isset($name)) show_city($name, $id); + else { + show_cities(0, 10); + show_new_city_form(); + } + + # XXX: Format URL in branch case... + if (count($parameters)) + echo "

Show all cities

\n"; +?> diff --git a/lib/constants.php b/lib/constants.php new file mode 100644 index 0000000..06a7c9e --- /dev/null +++ b/lib/constants.php @@ -0,0 +1,25 @@ + diff --git a/lib/contact.php b/lib/contact.php new file mode 100644 index 0000000..170d342 --- /dev/null +++ b/lib/contact.php @@ -0,0 +1,510 @@ +Added contact.

\n"; + $parameters = array($displayname, $id); + } + } + else if (isset($_POST['update_contact'])) { + list($name, $id, $args) = parse_parameters($parameters); + $q = new ContactQuery; + $contact = $q->findOneById($id); + if ($contact) { + $area = get_contact_area($contact); + if ($area) $area_id = $area->getId(); + if (update_contact($contact, $area_id) !== false) { + echo "

Updated contact.

\n"; + $parameters = array($contact->getDisplayname(), $contact->getId()); + } + } + else { + echo "

No such contact!

\n"; + } + } + else if ($_POST['search_contact']) { + header(sprintf("Location: http%s://%s/%s/search/%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($_POST['search_contact']))); + exit; + } + else if ($_POST['area_id']) { + $q = new AreaQuery; + $area = $q->findOneById($_POST['area_id']); + 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_contact_summary(&$contact) { + echo "
\nContact " . $contact->getLink(); + $role = $contact->getRole(); + $role_string = get_contact_role_string($contact); + if ($role_string) echo " $role_string"; + if ($role & $GLOBALS['ROLE_DONOR']) printf(" Donations", urlencode($contact->getDisplayname()), $contact->getId()); + if ($role & $GLOBALS['ROLE_REQUESTER']) printf(" Requested", urlencode($contact->getDisplayname()), $contact->getId()); + if ($role & $GLOBALS['ROLE_BENEFICIARY']) printf(" Orders", urlencode($contact->getDisplayname()), $contact->getId()); + if (check_admin(1)) { + echo " " . $contact->getDeleteLink(); + } + $area = get_contact_area($contact); + echo " in " . $area->getLink(); + } + + function show_contacts($offset, $per_page, $address_ids) { + $q = new ContactQuery; + $contacts = $q->filterByAddressId($address_ids)->orderByForename()->orderBySurname()->find(); + if (count($contacts)) { + foreach ($contacts as $contact) show_contact_summary($contact); + } + else echo " none"; + } + + function search_contacts($offset, $per_page, $search) { + $q = new ContactQuery; + $contacts = $q->filterByDisplayname("%$search%")->find(); + echo "

Contacts matching '" . htmlspecialchars($search) . "':"; + if (count($contacts)) { + foreach ($contacts as $contact) show_contact_summary($contact); + } + else echo "none"; + echo "

\n"; + } + + function show_city_contacts($offset, $per_page, $city_name, $city_id = 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) { + $q = new AreaQuery; + $areas = $q->filterByCityId($city->getId())->find(); + $area_ids = array(); + foreach ($areas as $area) $area_ids[] = $area->getId(); + + $q = new AddressQuery; + $addresses = $q->filterByAreaId($area_ids)->find(); + $address_ids = array(); + foreach ($addresses as $address) $address_ids[] = $address->getId(); + + echo "

Contacts in city " . $city->getLink(get_city_displayname($city)) . ":"; + return show_contacts($offset, $per_page, $address_ids); + } + else echo "

No such city!

\n"; + } + + function show_area_contacts($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) { + $q = new AddressQuery; + $addresses = $q->filterByAreaId($area->getId())->find(); + $address_ids = array(); + foreach ($addresses as $address) $address_ids[] = $address->getId(); + + echo "

Contacts in area " . $area->getLink() . ":"; + return show_contacts($offset, $per_page, $address_ids); + } + else echo "

No such area!

\n"; + } + + function show_contact_areas_form($city_id = null) { + $areas = get_city_areas($city_id); + if (! count($areas)) { + echo "

No areas!

\n"; + return; + } + + echo "\n"; + echo "

Show contacts in area\n"; + echo "\n"; + echo "\n"; + echo "

\n"; + } + + function show_contact_cities_form($city_id = null) { + $q = new CityQuery; + $cities = $q->orderByName()->find(); + + if (! count($cities)) { + echo "

No cities!

\n"; + return; + } + + echo "
\n"; + echo "

Show contacts in city\n"; + echo "\n"; + echo "\n"; + echo "

\n"; + } + + function show_contact_search_form() { + echo "
\n"; + echo "

Search for contacts:"; + input("search_contact"); + echo "\n"; + 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 "\n"; + echo " Role\n"; + echo " "; show_contact_role_form($contact->getRole()); echo "\n"; + echo "\n"; + + /* Forename. */ + echo "\n"; + echo " Forename\n"; + echo " "; input("forename", $contact->getForename()); echo "\n"; + echo "\n"; + + /* Middle names. */ + echo "\n"; + echo " Middle name(s)\n"; + echo " "; input("middle", $contact->getMiddle()); echo "\n"; + echo "\n"; + + /* Surname. */ + echo "\n"; + echo " Surname\n"; + echo " "; input("surname", $contact->getSurname()); echo "\n"; + echo "\n"; + + /* Display name. */ + echo "\n"; + echo " Display name (if not concatenation of above)\n"; + echo " "; input("displayname", $contact->getDisplayname()); echo "\n"; + echo "\n"; + + /* Address. */ + $address = get_contact_address($contact); + if (! $address) $address = new Address; + echo "\n"; + echo " Address\n"; + echo " "; textarea("address", $address->getLine()); echo "\n"; + echo "\n"; + + /* Postcode. */ + echo "\n"; + echo " Postcode\n"; + echo " "; input("postcode", $address->getPostcode()); echo "\n"; + echo "\n"; + + /* Telephone. */ + echo "\n"; + echo " Telephone\n"; + echo " "; input("telephone1", $contact->getTelephone1()); echo "\n"; + echo "\n"; + echo "\n"; + echo " Alternative telephone\n"; + echo " "; input("telephone2", $contact->getTelephone2()); echo "\n"; + echo "\n"; + + /* Email. */ + echo "\n"; + echo " Email\n"; + echo " "; input("email", $contact->getEmail()); echo "\n"; + echo "\n"; + + /* Area. */ + $area = get_contact_area($contact); + if ($area) $area_id = $area->getId(); + echo "\n"; + echo " Area\n"; + echo " \n"; + echo "\n"; + } + + function show_new_contact_form($city_id = null) { + if (! check_admin(1)) return; + + $areas = get_city_areas($city_id); + if (! count($areas)) { + echo "

No areas!

\n"; + return; + } + + echo "
\n"; + echo "

Add a new contact:

\n"; + + echo "\n"; + show_contact_form($contact); + + echo "\n"; + echo " \n"; + echo "\n"; + echo "
"; submit("add_contact", "Add"); echo "
\n"; + 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"; + echo "

Add a new contact in "; + submit("show_add_contact", "Proceed"); + echo "

\n"; + 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"; + echo "

Contact: " . $contact->getDisplayname() . ""; + $role = $contact->getRole(); + $role_string = get_contact_role_string($contact); + if ($role_string) echo " $role_string"; + if ($role & $GLOBALS['ROLE_DONOR']) printf(" Donations", urlencode($contact->getDisplayname()), $contact->getId()); + if ($role & $GLOBALS['ROLE_REQUESTER']) printf(" Requested", urlencode($contact->getDisplayname()), $contact->getId()); + if ($role & $GLOBALS['ROLE_BENEFICIARY']) printf(" Orders", urlencode($contact->getDisplayname()), $contact->getId()); + if (check_admin(1)) { + echo " " . $contact->getDeleteLink(); + } + $city = get_contact_city($contact); + if ($city) echo " in " . $city->getLink(get_city_displayname($city)); + echo ": "; + echo "\n

"; + + echo "\n"; + show_contact_form($contact); + + if (check_admin(1)) { + echo "\n"; + echo " \n"; + echo "\n"; + } + + echo "
"; + submit("update_contact", "Update"); + echo "
\n"; + 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 "

Delivery schedule for " . htmlspecialchars($contact->getDisplayname()) . "

\n"; + $order_ids = get_driver_schedule_by_order_id($contact->getId(), get_orders_for_today()); + $q = new OrderQuery; + $orders = $q->filterById($order_ids)->find(); + foreach ($orders as $order) { + $contact = get_contact_by_id($order->getBeneficiaryId()); + if (! $contact) continue; + + $area = get_contact_area($contact); + 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 "

\n"; + + echo "
\n\n"; + } + } + + list($ignored, $id, $args) = parse_parameters($parameters); + if (count($args)) show_driver_schedule($args[0], $args[1]); + else { + $order_ids = get_orders_for_today(); + if ($order_ids) $driver_ids = get_drivers_by_order_id($order_ids); + if ($driver_ids) show_driver_forms($driver_ids); + } + +?> diff --git a/lib/donation.php b/lib/donation.php new file mode 100644 index 0000000..8a62a84 --- /dev/null +++ b/lib/donation.php @@ -0,0 +1,411 @@ +Donation recorded.

\n"; + $parameters = array("id", $id); + } + } + else if (isset($_POST['update_donation'])) { + list($ignored, $id, $args) = parse_parameters($parameters); + $q = new DonationQuery; + $donation = $q->findOneById($id); + if ($donation) { + if (update_donation($donation) !== false) { + 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 "
\nDonation " . $donation->getStrongLink($donation->getId()) . ": " . get_donation_displayname($donation); + + /* XXX: Should pull from query. */ + $q = new ContactQuery; + $contact = $q->findOneById($donation->getContactId()); + if ($contact) echo " from " . $contact->getLink(); + + $q = new HubQuery; + $hub = $q->findOneById($donation->getHubId()); + if ($hub) { + echo " to " . $hub->getLink(); + $area = get_hub_area($hub); + if ($area) { + echo " in " . $area->getLink(); + $city = get_area_city($area); + if ($city) echo ", " . $city->getLink(get_city_displayname($city)); + } + } + if (check_admin(1)) { + echo " " . $donation->getDeleteLink(); + } + } + } + else echo " none"; + } + + function show_city_donations($offset, $per_page, $city_name, $city_id = 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) { + $hubs = get_city_hubs($city->getId()); + $hub_ids = array(); + foreach ($hubs as $hub) $hub_ids[] = $hub->getId(); + + 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"; + echo "

Show donations in area\n"; + echo "\n"; + echo "\n"; + 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"; + echo "

Show donations in city\n"; + echo "\n"; + echo "\n"; + 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 "\n"; + echo " Date\n"; + echo " "; show_date_form("date", $donation->getDate()); echo "\n"; + echo "\n"; + + /* Contact. */ + echo "\n"; + echo " Donor\n"; + echo " \n"; + echo "\n"; + + /* Hub. */ + echo "\n"; + echo " Hub\n"; + echo " \n"; + echo "\n"; + + /* Quantity. */ + echo "\n"; + echo " Quantity (kg)\n"; + echo " "; input("quantity", $donation->getQuantity()); echo "\n"; + echo "\n"; + } + + function show_new_donation_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; + } + + echo "
\n"; + echo "

Record a donation:

\n"; + + echo "\n"; + show_donation_form(null, $area_id); + + echo "\n"; + echo " \n"; + echo "\n"; + echo "
"; submit("add_donation", "Record"); echo "
\n"; + 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"; + echo "

Record a donation in "; + submit("show_add_donation", "Proceed"); + echo "

\n"; + 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"; + echo "

Donation " . $donation->getId() . ""; + if (check_admin(1)) { + echo " " . $donation->getDeleteLink(); + } + echo ": "; + echo "\n

"; + + echo "\n"; + show_donation_form($donation); + + if (check_admin(1)) { + echo "\n"; + echo " \n"; + echo "\n"; + } + + echo "
"; + submit("update_donation", "Update"); + echo "
\n"; + 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 @@ + + + diff --git a/lib/forms.php b/lib/forms.php new file mode 100644 index 0000000..af51c1c --- /dev/null +++ b/lib/forms.php @@ -0,0 +1,33 @@ +"; + } + + function hidden($name, $value = null) { + return input($name, $value, "hidden"); + } + + function submit($name, $value = null) { + return input($name, $value, "submit"); + } + + function textarea($name, $value = null) { + echo ""; + } + + function option($select, $value, $text, $selected = null) { + echo "