3 function parse_parameters($parameters) {
8 if (count($parameters) > 0) {
9 $name = array_shift($parameters);
11 /* Recall that we shifted. */
12 if (count($parameters) > 0) {
13 if (is_numeric($parameters[0])) {
14 $id = array_shift($parameters);
21 return array($name, $id, $args);
24 function get_city_by_name($name, $postcode_area = null, $verbose = true) {
27 $m = $q->filterByName(urldecode($name));
28 if (isset($postcode_area)) {
29 $m->filterByPostcodeArea($postcode_area);
33 switch ($m->count()) {
35 if ($verbose) echo "<p>No such city!</p>\n";
42 if ($verbose) echo "<p>Can't identify city uniquely.</p>!\n";
47 function get_city_by_id($id, $verbose = true) {
49 $city = $q->findOneById($id);
52 if ($verbose) echo "<p>No such city!</p>\n";
59 function get_area_by_name($name, $verbose = true) {
61 $areas = $q->filterByName(urldecode($name))->find();
63 switch ($q->count()) {
65 if ($verbose) echo "<p>No such area!</p>\n";
72 if ($verbose) echo "<p>Can't identify area uniquely.</p>!\n";
77 function get_area_by_id($id, $verbose = true) {
79 $area = $q->findOneById($id);
82 if ($verbose) echo "<p>No such area!</p>\n";
89 function get_area_city($area) {
91 return $q->findOneById($area->getCityId());
94 function get_contact_by_name($name, $verbose = true) {
95 $q = new ContactQuery;
96 $contact = $q->filterByDisplayname(urldecode($name))->find();
98 switch ($q->count()) {
100 if ($verbose) echo "<p>No such contact!</p>\n";
107 if ($verbose) echo "<p>Can't identify contact uniquely.</p>!\n";
112 function get_contact_by_id($id, $verbose = true) {
113 $q = new ContactQuery;
114 $contact = $q->findOneById($id);
117 if ($verbose) echo "<p>No such contact!</p>\n";
124 function get_hub_by_name($name, $verbose = true) {
126 $hubs = $q->filterByDisplayname(urldecode($name))->find();
128 switch ($q->count()) {
130 if ($verbose) echo "<p>No such hub!</p>\n";
137 if ($verbose) echo "<p>Can't identify hub uniquely.</p>!\n";
142 function get_hub_by_id($id, $verbose = true) {
144 $hub = $q->findOneById($id);
147 if ($verbose) echo "<p>No such hub!</p>\n";
154 function get_donation_by_id($id, $verbose = true) {
155 $q = new DonationQuery;
156 $donation = $q->findOneById($id);
159 if ($verbose) echo "<p>No such donation!</p>\n";
166 function get_order_by_id($id, $verbose = true) {
168 $order = $q->findOneById($id);
171 if ($verbose) echo "<p>No such order!</p>\n";
178 function get_order_ids_by_state($state_mask) {
179 $order_ids = array();
180 $dbh = Propel::getConnection();
181 $sth = $dbh->prepare("select * from OrderState o where updated=(select max(updated) from OrderState where order_id=o.order_id) and state & $state_mask");
183 $order_states = OrderStatePeer::populateObjects($sth);
184 foreach ($order_states as $order_state) $order_ids[] = $order_state->getOrderId();
188 function get_contact_orders($contact, $state_mask = null) {
190 $q->filterByBeneficiaryId($contact->getId());
191 if ($state_mask) $q->filterById(get_order_ids_by_state($state_mask));
192 return $q->orderByDate()->find();
195 function get_user_by_contact_id($id, $verbose = true) {
197 $user = $q->findOneByContactId($id);
200 if ($verbose) echo "<p>No such user!</p>\n";
207 function get_city_displayname($city) {
208 return $city->getName() . ", " . $city->getPostcodeArea();
211 function get_area_displayname($area) {
212 return $area->getName() . " in " . get_city_displayname(CityQuery::create()->findOneById($area->getCityId()));
215 function get_donation_displayname($donation) {
216 return sprintf("%0.2fkg on %s", $donation->getQuantity() / 1000, $donation->getDate());
219 function get_order_parcel_string($order) {
220 global $parcel_sizes, $parcel_contents;
223 for ($i = 0 ; $i < count($parcel_sizes); $i++) {
224 if ($order->getParcel() & (1 << $i)) {
225 $parcel_size = $parcel_sizes[$i];
231 for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
232 if ($order->getParcel() & (1 << $i)) $selected[] = $parcel_contents[$i];
235 return implode(": ", array($parcel_size, implode(", ", $selected)));
238 function get_order_displayname($order) {
239 return sprintf("<span class=\"small\">%s</span> on %s", get_order_parcel_string($order), $order->getDate());
242 function get_address_area($address) {
244 return $q->findOneById($address->getAreaId());
247 function get_contact_address($contact) {
248 $q = new AddressQuery;
249 return $q->findOneById($contact->getAddressId());
252 function get_contact_area($contact) {
253 $address = get_contact_address($contact);
254 if (! $address) return null;
256 return get_address_area($address);
259 function get_contact_city($contact) {
260 $area = get_contact_area($contact);
261 if (! $area) return null;
263 return get_area_city($area);
266 /* Parcel strings are the same so this can work. */
267 function get_contact_parcel_string($contact) {
268 return get_order_parcel_string($contact);
271 /* Hub and Contact are similar enough that this can work. */
272 function get_hub_address($hub) {
273 return get_contact_address($hub);
276 /* Hub and Contact are similar enough that this can work. */
277 function get_hub_area($hub) {
278 return get_contact_area($hub);
281 /* Hub and Contact are similar enough that this can work. */
282 function get_hub_city($hub) {
283 return get_contact_city($hub);
286 function get_area_contacts($area_id = null, $role = null) {
287 $q = new ContactQuery;
288 if (isset($area_id)) $q->useAddressQuery()->filterByAreaId($area_id)->endUse();
289 if (isset($role)) $q->where("role & $role");
290 return $q->orderByDisplayname()->find();
293 function get_area_requesters($area_id = null) {
294 return get_area_contacts($area_id, $GLOBALS['ROLE_REQUESTER']);
297 function get_area_beneficiaries($area_id = null) {
298 return get_area_contacts($area_id, $GLOBALS['ROLE_BENEFICIARY']);
301 function get_area_donors($area_id = null) {
302 return get_area_contacts($area_id, $GLOBALS['ROLE_DONOR']);
305 function get_city_contacts($city_id = null, $role = null) {
308 $areas = get_city_areas($city_id);
309 foreach ($areas as $area) $area_ids[] = $area->getId();
310 return get_area_contacts($area_ids, $role);
313 function get_city_requesters($city_id = null) {
314 return get_city_contacts($city_id, $GLOBALS['ROLE_REQUESTER']);
317 function get_city_beneficiaries($city_id = null) {
318 return get_city_contacts($city_id, $GLOBALS['ROLE_BENEFICIARY']);
321 function get_city_donors($city_id = null) {
322 return get_city_contacts($city_id, $GLOBALS['ROLE_DONOR']);
325 function get_city_drivers($city_id = null) {
326 return get_city_contacts($city_id, $GLOBALS['ROLE_DRIVER']);
329 function get_role_string($object, $roles) {
330 $role = $object->getRole();
334 for ($i =0; $i < count($roles); $i++) {
335 if ($role & (1 << $i)) $selected[] = $roles[$i];
338 return implode(", ", $selected);
341 function get_contact_role_string($contact) {
342 return get_role_string($contact, $GLOBALS['contact_roles']);
345 function get_hub_role_string($hub) {
346 return get_role_string($hub, $GLOBALS['hub_roles']);
349 function show_role_form($role, $roles) {
350 for ($i = 0; $i < count($roles); $i++) {
351 echo " <input type=\"checkbox\" name=\"role_$i\"";
352 if ($role & (1 << $i)) echo " checked";
353 echo ">$roles[$i]\n";
357 function get_area_hubs($area_id = null) {
359 if (isset($area_id)) $q->useAddressQuery()->filterByAreaId($area_id)->endUse();
360 return $q->orderByDisplayname()->find();
363 function get_city_areas($city_id = null) {
365 $q->join("City")->orderBy("City.Name");
366 if (isset($city_id)) $q->filterByCityId($city_id);
367 return $q->orderByName()->find();
370 function get_city_areas_with_contacts($city_id = null, $role = null) {
372 $q->join("City")->orderBy("City.Name");
373 if (isset($city_id)) $q->filterByCityId($city_id);
375 if (isset($role)) $q->useAddressQuery()->join("Contact")->useContactQuery()->where("role & $role")->endUse()->endUse();
376 else $q->useAddressQuery()->join("Contact")->endUse();
377 return $q->orderByName()->distinct()->find();
380 function get_city_areas_with_hubs($city_id = null) {
382 $q->join("City")->orderBy("City.Name");
383 if (isset($city_id)) $q->filterByCityId($city_id);
384 $q->useAddressQuery()->join("Hub")->endUse();
385 return $q->orderByName()->distinct()->find();
388 function get_city_hubs($city_id = null) {
390 if (isset($city_id)) $q->useAddressQuery()->useAreaQuery()->filterByCityId($city_id)->endUse()->endUse();
391 return $q->orderByDisplayname()->find();
394 function iso8601_to_ymd($iso8601) {
395 return split("-", $iso8601);
398 function ymd_to_iso8601($name) {
399 $y = $_POST[$name . "_y"];
400 if (! $y) return null;
401 $m = $_POST[$name . "_m"];
403 $d = $_POST[$name . "_d"];
405 return sprintf("%04d-%02d-%02d", $y, $m, $d);
408 function show_date_form($name, $date = null) {
411 echo "<select name=\"$name\">\n";
412 $now = date('Y-m-d', time());
413 list($y, $m, $d) = explode('-', $now);
414 $today = mktime(0, 0, 0, $m, $d, $y);
416 list($y, $m, $d) = explode('-', $date);
417 $then = mktime(0, 0, 0, $m, $d, $y);
418 if ($then < $today - 86400 * $past || $then > $today + 86400 * $future) {
419 option($name, $date, date('l j F Y', $then), $date);
423 for ($i = -$past; $i < $future; $i++) {
424 $then = $today + 86400 * $i;
425 option($name, date('Y-m-d', $then), date('l j F Y', $then), $date);
429 if (! isset($date)) $date = date('Y-m-d');
430 list($y, $m, $d) = iso8601_to_ymd($date);
432 echo "Year: <input name=\"$name" . "_y\" value=\"$y\" size=4 maxlen=4> ";
433 echo "Month: <input name=\"$name" . "_m\" value=\"$m\" size=2 maxlen=2> ";
434 echo "Day: <input name=\"$name" . "_d\" value=\"$d\" size=2 maxlen=2> ";
437 include_once("$lib_root/admin.php");
438 include_once("$lib_root/forms.php");