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_user_by_contact_id($id, $verbose = true) {
180 $user = $q->findOneByContactId($id);
183 if ($verbose) echo "<p>No such user!</p>\n";
190 function get_city_displayname($city) {
191 return $city->getName() . ", " . $city->getPostcodeArea();
194 function get_area_displayname($area) {
195 return $area->getName() . " in " . get_city_displayname(CityQuery::create()->findOneById($area->getCityId()));
198 function get_donation_displayname($donation) {
199 return $donation->getQuantity() . "kg on " . $donation->getDate();
202 function get_order_displayname($order) {
203 return $order->getQuantity() . "kg on " . $order->getDate();
206 function get_address_area($address) {
208 return $q->findOneById($address->getAreaId());
211 function get_contact_address($contact) {
212 $q = new AddressQuery;
213 return $q->findOneById($contact->getAddressId());
216 function get_contact_area($contact) {
217 $address = get_contact_address($contact);
218 if (! $address) return null;
220 return get_address_area($address);
223 function get_contact_city($contact) {
224 $area = get_contact_area($contact);
225 if (! $area) return null;
227 return get_area_city($area);
230 /* Hub and Contact are similar enough that this can work. */
231 function get_hub_address($hub) {
232 return get_contact_address($hub);
235 /* Hub and Contact are similar enough that this can work. */
236 function get_hub_area($hub) {
237 return get_contact_area($hub);
240 /* Hub and Contact are similar enough that this can work. */
241 function get_hub_city($hub) {
242 return get_contact_city($hub);
245 function get_area_contacts($area_id = null, $role = null) {
246 $q = new ContactQuery;
247 if (isset($area_id)) $q->useAddressQuery()->filterByAreaId($area_id)->endUse();
248 if (isset($role)) $q->where("role & $role");
249 return $q->orderByDisplayname()->find();
252 function get_area_requesters($area_id = null) {
253 return get_area_contacts($area_id, $GLOBALS['ROLE_REQUESTER']);
256 function get_area_beneficiaries($area_id = null) {
257 return get_area_contacts($area_id, $GLOBALS['ROLE_BENEFICIARY']);
260 function get_area_donors($area_id = null) {
261 return get_area_contacts($area_id, $GLOBALS['ROLE_DONOR']);
264 function get_city_contacts($city_id = null, $role = null) {
267 $areas = get_city_areas($city_id);
268 foreach ($areas as $area) $area_ids[] = $area->getId();
269 return get_area_contacts($area_ids, $role);
272 function get_city_requesters($city_id = null) {
273 return get_city_contacts($city_id, $GLOBALS['ROLE_REQUESTER']);
276 function get_city_beneficiaries($city_id = null) {
277 return get_city_contacts($city_id, $GLOBALS['ROLE_BENEFICIARY']);
280 function get_city_donors($city_id = null) {
281 return get_city_contacts($city_id, $GLOBALS['ROLE_DONOR']);
284 function get_city_drivers($city_id = null) {
285 return get_city_contacts($city_id, $GLOBALS['ROLE_DRIVER']);
288 function get_role_string($object, $roles) {
289 $role = $object->getRole();
293 for ($i =0; $i < count($roles); $i++) {
294 if ($role & (1 << $i)) $selected[] = $roles[$i];
297 return implode(", ", $selected);
300 function get_contact_role_string($contact) {
301 return get_role_string($contact, $GLOBALS['contact_roles']);
304 function get_hub_role_string($hub) {
305 return get_role_string($hub, $GLOBALS['hub_roles']);
308 function show_role_form($role, $roles) {
309 for ($i = 0; $i < count($roles); $i++) {
310 echo " <input type=\"checkbox\" name=\"role_$i\"";
311 if ($role & (1 << $i)) echo " checked";
312 echo ">$roles[$i]\n";
316 function get_area_hubs($area_id = null) {
318 if (isset($area_id)) $q->useAddressQuery()->filterByAreaId($area_id)->endUse();
319 return $q->orderByDisplayname()->find();
322 function get_city_areas($city_id = null) {
324 $q->join("City")->orderBy("City.Name");
325 if (isset($city_id)) $q->filterByCityId($city_id);
326 return $q->orderByName()->find();
329 function get_city_areas_with_contacts($city_id = null, $role = null) {
331 $q->join("City")->orderBy("City.Name");
332 if (isset($city_id)) $q->filterByCityId($city_id);
334 if (isset($role)) $q->useAddressQuery()->join("Contact")->useContactQuery()->where("role & $role")->endUse()->endUse();
335 else $q->useAddressQuery()->join("Contact")->endUse();
336 return $q->orderByName()->distinct()->find();
339 function get_city_areas_with_hubs($city_id = null) {
341 $q->join("City")->orderBy("City.Name");
342 if (isset($city_id)) $q->filterByCityId($city_id);
343 $q->useAddressQuery()->join("Hub")->endUse();
344 return $q->orderByName()->distinct()->find();
347 function get_city_hubs($city_id = null) {
349 if (isset($city_id)) $q->useAddressQuery()->useAreaQuery()->filterByCityId($city_id)->endUse()->endUse();
350 return $q->orderByDisplayname()->find();
353 function iso8601_to_ymd($iso8601) {
354 return split("-", $iso8601);
357 function ymd_to_iso8601($name) {
358 $y = $_POST[$name . "_y"];
359 if (! $y) return null;
360 $m = $_POST[$name . "_m"];
362 $d = $_POST[$name . "_d"];
364 return sprintf("%04d-%02d-%02d", $y, $m, $d);
367 function show_date_form($name, $date = null) {
368 echo "<select name=\"$name\">\n";
371 list($y, $m, $d) = explode('-', $date);
372 $then = mktime(0, 0, 0, $m, $d, $y);
373 option($name, $date, date('l j F Y', $then), $date);
375 for ($i = 0; $i < 60; $i++) {
376 $then = $now + 86400 * $i;
377 option($name, date('Y-m-d', $then), date('l j F Y', $then), $date);
381 if (! isset($date)) $date = date('Y-m-d');
382 list($y, $m, $d) = iso8601_to_ymd($date);
384 echo "Year: <input name=\"$name" . "_y\" value=\"$y\" size=4 maxlen=4> ";
385 echo "Month: <input name=\"$name" . "_m\" value=\"$m\" size=2 maxlen=2> ";
386 echo "Day: <input name=\"$name" . "_d\" value=\"$d\" size=2 maxlen=2> ";
389 include_once("$lib_root/admin.php");
390 include_once("$lib_root/forms.php");