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_beneficiary_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_requester_orders($contact, $state_mask = null) {
197 $q->filterByRequesterId($contact->getId());
198 if ($state_mask) $q->filterById(get_order_ids_by_state($state_mask));
199 return $q->orderByDate()->find();
202 function get_contact_orders($contact, $state_mask = null) {
204 $q->filterByBeneficiaryId($contact->getId())->_or()->filterByRequesterId($contact->getId());
205 if ($state_mask) $q->filterById(get_order_ids_by_state($state_mask));
206 return $q->orderByDate()->find();
209 function get_user_by_contact_id($id, $verbose = true) {
211 $user = $q->findOneByContactId($id);
214 if ($verbose) echo "<p>No such user!</p>\n";
221 function get_city_displayname($city) {
222 return $city->getName() . ", " . $city->getPostcodeArea();
225 function get_area_displayname($area) {
226 return $area->getName() . " in " . get_city_displayname(CityQuery::create()->findOneById($area->getCityId()));
229 function get_donation_displayname($donation) {
230 return sprintf("%0.2fkg on %s", $donation->getQuantity() / 1000, $donation->getDate());
233 function get_order_parcel_string($order) {
234 global $parcel_sizes, $parcel_contents;
237 for ($i = 0 ; $i < count($parcel_sizes); $i++) {
238 if ($order->getParcel() & (1 << $i)) {
239 $parcel_size = $parcel_sizes[$i];
245 for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
246 if ($order->getParcel() & (1 << $i)) $selected[] = $parcel_contents[$i];
249 $ret = implode(": ", array($parcel_size, implode(", ", $selected)));
250 $ret = preg_replace('/^: /', '', $ret);
251 $ret = preg_replace('/: $/', '', $ret);
256 function get_order_displayname($order) {
257 return sprintf("<span class=\"small\">%s</span> on %s", get_order_parcel_string($order), $order->getDate());
260 function get_order_state_string($order_state = null) {
263 if (is_null($order_state)) return null;
265 for ($i = 0; $i < count($states); $i++) {
266 if ($order_state->getState() & (1 << $i)) {
274 function get_order_state($order) {
275 $q = new OrderStateQuery();
276 return $q->filterByOrderId($order->getId())->orderByUpdated('desc')->findOne();
279 function get_order_summary($order) {
280 $ret = "Order " . $order->getStrongLink($order->getId()) . ": " . get_order_displayname($order);
282 if (check_admin(1)) $ret .= " " . $order->getDeleteLink();
284 /* XXX: Should pull from query. */
285 $q = new ContactQuery;
286 $contact = $q->findOneById($order->getRequesterId());
288 $ret .= " referred by " . $contact->getLink();
289 $area = get_contact_area($contact);
290 if ($area) $ret .= " in " . $area->getLink();
293 $q = new ContactQuery;
294 $contact = $q->findOneById($order->getBeneficiaryId());
296 $ret .= " for " . $contact->getLink();
297 $area = get_contact_area($contact);
298 if ($area) $ret .= " in " . $area->getLink();
301 if ($order->getHubId()) {
303 $hub = $q->findOneById($order->getHubId());
304 if ($hub) $ret .= " to hub " . $hub->getLink();
305 $area = get_hub_area($hub);
306 if ($area) $ret .= " in " . $area->getLink();
312 function get_address_area($address) {
314 return $q->findOneById($address->getAreaId());
317 function get_contact_address($contact) {
318 $q = new AddressQuery;
319 return $q->findOneById($contact->getAddressId());
322 function get_contact_area($contact) {
323 $address = get_contact_address($contact);
324 if (! $address) return null;
326 return get_address_area($address);
329 function get_contact_city($contact) {
330 $area = get_contact_area($contact);
331 if (! $area) return null;
333 return get_area_city($area);
336 /* Parcel strings are the same so this can work. */
337 function get_contact_parcel_string($contact) {
338 return get_order_parcel_string($contact);
341 /* Hub and Contact are similar enough that this can work. */
342 function get_hub_address($hub) {
343 return get_contact_address($hub);
346 /* Hub and Contact are similar enough that this can work. */
347 function get_hub_area($hub) {
348 return get_contact_area($hub);
351 /* Hub and Contact are similar enough that this can work. */
352 function get_hub_city($hub) {
353 return get_contact_city($hub);
356 function get_area_contacts($area_id = null, $role = null) {
357 $q = new ContactQuery;
358 if (isset($area_id)) $q->useAddressQuery()->filterByAreaId($area_id)->endUse();
359 if (isset($role)) $q->where("role & $role");
360 return $q->orderByDisplayname()->find();
363 function get_area_requesters($area_id = null) {
364 return get_area_contacts($area_id, $GLOBALS['ROLE_REQUESTER']);
367 function get_area_beneficiaries($area_id = null) {
368 return get_area_contacts($area_id, $GLOBALS['ROLE_BENEFICIARY']);
371 function get_area_donors($area_id = null) {
372 return get_area_contacts($area_id, $GLOBALS['ROLE_DONOR']);
375 function get_city_contacts($city_id = null, $role = null) {
378 $areas = get_city_areas($city_id);
379 foreach ($areas as $area) $area_ids[] = $area->getId();
380 return get_area_contacts($area_ids, $role);
383 function get_city_requesters($city_id = null) {
384 return get_city_contacts($city_id, $GLOBALS['ROLE_REQUESTER']);
387 function get_city_beneficiaries($city_id = null) {
388 return get_city_contacts($city_id, $GLOBALS['ROLE_BENEFICIARY']);
391 function get_city_donors($city_id = null) {
392 return get_city_contacts($city_id, $GLOBALS['ROLE_DONOR']);
395 function get_city_drivers($city_id = null) {
396 return get_city_contacts($city_id, $GLOBALS['ROLE_DRIVER']);
399 function get_role_string($object, $roles) {
400 $role = $object->getRole();
404 for ($i =0; $i < count($roles); $i++) {
405 if ($role & (1 << $i)) $selected[] = $roles[$i];
408 return implode(", ", $selected);
411 function get_contact_role_string($contact) {
412 return get_role_string($contact, $GLOBALS['contact_roles']);
415 function get_hub_role_string($hub) {
416 return get_role_string($hub, $GLOBALS['hub_roles']);
419 function show_role_form($role, $roles) {
420 for ($i = 0; $i < count($roles); $i++) {
421 echo " <input type=\"checkbox\" name=\"role_$i\"";
422 if ($role & (1 << $i)) echo " checked";
423 echo ">$roles[$i]\n";
427 function get_area_hubs($area_id = null) {
429 if (isset($area_id)) $q->useAddressQuery()->filterByAreaId($area_id)->endUse();
430 return $q->orderByDisplayname()->find();
433 function get_city_areas($city_id = null) {
435 $q->join("City")->orderBy("City.Name");
436 if (isset($city_id)) $q->filterByCityId($city_id);
437 return $q->orderByName()->find();
440 function get_city_areas_with_contacts($city_id = null, $role = null) {
442 $q->join("City")->orderBy("City.Name");
443 if (isset($city_id)) $q->filterByCityId($city_id);
445 if (isset($role)) $q->useAddressQuery()->join("Contact")->useContactQuery()->where("role & $role")->endUse()->endUse();
446 else $q->useAddressQuery()->join("Contact")->endUse();
447 return $q->orderByName()->distinct()->find();
450 function get_city_areas_with_hubs($city_id = null) {
452 $q->join("City")->orderBy("City.Name");
453 if (isset($city_id)) $q->filterByCityId($city_id);
454 $q->useAddressQuery()->join("Hub")->endUse();
455 return $q->orderByName()->distinct()->find();
458 function get_city_hubs($city_id = null) {
460 if (isset($city_id)) $q->useAddressQuery()->useAreaQuery()->filterByCityId($city_id)->endUse()->endUse();
461 return $q->orderByDisplayname()->find();
464 function iso8601_to_ymd($iso8601) {
465 return split("-", $iso8601);
468 function ymd_to_iso8601($name) {
469 $y = $_POST[$name . "_y"];
470 if (! $y) return null;
471 $m = $_POST[$name . "_m"];
473 $d = $_POST[$name . "_d"];
475 return sprintf("%04d-%02d-%02d", $y, $m, $d);
478 function show_date_form($name, $date = null) {
481 echo "<select name=\"$name\">\n";
482 $now = date('Y-m-d', time());
483 list($y, $m, $d) = explode('-', $now);
484 $today = mktime(0, 0, 0, $m, $d, $y);
486 list($y, $m, $d) = explode('-', $date);
487 $then = mktime(0, 0, 0, $m, $d, $y);
488 if ($then < $today - 86400 * $past || $then > $today + 86400 * $future) {
489 option($name, $date, date('l j F Y', $then), $date);
493 for ($i = -$past; $i < $future; $i++) {
494 $then = $today + 86400 * $i;
495 option($name, date('Y-m-d', $then), date('l j F Y', $then), $date);
499 if (! isset($date)) $date = date('Y-m-d');
500 list($y, $m, $d) = iso8601_to_ymd($date);
502 echo "Year: <input name=\"$name" . "_y\" value=\"$y\" size=4 maxlen=4> ";
503 echo "Month: <input name=\"$name" . "_m\" value=\"$m\" size=2 maxlen=2> ";
504 echo "Day: <input name=\"$name" . "_d\" value=\"$d\" size=2 maxlen=2> ";
507 function get_small_link() {
508 /* Args are <alt text>, <format>, [<stuff> ...] */
509 $args = func_get_args();
510 $html = htmlspecialchars(array_shift($args));
511 $url = array_shift($args);
512 return vsprintf("<a class=\"small noprint\" href=\"$url\">$html</a>\n", $args);
515 function small_link() {
516 echo call_user_func_array("get_small_link", func_get_args());
519 include_once("$lib_root/admin.php");
520 include_once("$lib_root/forms.php");