tripal_core_misc.api.inc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * This function implements a pager, similar to a pager for a database query
  4. * using a non query. Rather than pass a query as in pager_query function, a
  5. * callback function should recieve as it's first argument an array of the
  6. * argumetns passed to this function. The second argument should be the
  7. * number of results to retreive, and the third argument should be the the
  8. * element index (same as with pager_query) function. The callback function
  9. * should always return an array. If no values are available then an empty
  10. * array should be returned.
  11. *
  12. * @param $func
  13. * The name of the callback function used to retrieve the results. The
  14. * callback function should have three arguments: first, an array of values
  15. * passed into the tripal_pager_callback; second, the start index of
  16. * the paged values; third, the number of records to include.
  17. * @param cfunc
  18. * The name of the callback function used to specify the total results. The
  19. * callback f unction should receive one argument: an array of values that
  20. * are passed into the tripal_pager_callbck function.
  21. * @param limit
  22. * The number of records to include in the paged results
  23. * @param element
  24. * A unique integer ID if more than one pager exists on a page. if multiple
  25. * types of pagers (e.g. pager_query, chado_pager_query) are on the same
  26. * page then this parameter should be unique amongst them all.
  27. */
  28. function tripal_pager_callback($func, $cfunc, $limit = 9, $element = 0) {
  29. global $pager_page_array, $pager_total, $pager_total_items;
  30. $page = isset($_GET['page']) ? $_GET['page'] : '';
  31. // Substitute in query arguments.
  32. $args = func_get_args();
  33. $args = array_slice($args, 3);
  34. // Alternative syntax for '...'
  35. if (isset($args[0]) && is_array($args[0])) {
  36. $args = $args[0];
  37. }
  38. $count = $cfunc($args);
  39. // Convert comma-separated $page to an array, used by other functions.
  40. $pager_page_array = explode(',', $page);
  41. // We calculate the total of pages as ceil(items / limit).
  42. $pager_total_items[$element] = $count;
  43. $pager_total[$element] = ceil($pager_total_items[$element] / $limit);
  44. $pager_page_array[$element] = max(0, min((int)$pager_page_array[$element], ((int)$pager_total[$element]) - 1));
  45. // retrieve the results using the specified callback.
  46. return $func($args, $pager_page_array[$element] * $limit, $limit);
  47. }