tripal_core_misc.api.inc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 limit
  18. * The number of records to include in the paged results
  19. * @param element
  20. * A unique integer ID if more than one pager exists on a page. if multiple
  21. * types of pagers (e.g. pager_query, chado_pager_query) are on the same
  22. * page then this parameter should be unique amongst them all.
  23. * @param cfunc
  24. * The name of the callback function used to specify the total results. The
  25. * callback f unction should receive one argument: an array of values that
  26. * are passed into the tripal_pager_callbck function.
  27. */
  28. function tripal_pager_callback($func, $limit = 10, $element = 0, $cfunc) {
  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, 4);
  34. // Alternative syntax for '...'
  35. if (isset($args[0]) && is_array($args[0])) {
  36. $args = $args[0];
  37. }
  38. // count the number of records by calling the counting callbackup function
  39. $count = $cfunc($args);
  40. // Convert comma-separated $page to an array, used by other functions.
  41. $pager_page_array = explode(',', $page);
  42. // We calculate the total of pages as ceil(items / limit).
  43. $pager_total_items[$element] = $count;
  44. $pager_total[$element] = ceil($pager_total_items[$element] / $limit);
  45. $pager_page_array[$element] = max(0, min((int)$pager_page_array[$element], ((int)$pager_total[$element]) - 1));
  46. // retrieve the results using the specified callback.
  47. return $func($args, $pager_page_array[$element] * $limit, $limit);
  48. }