pager_preference.module

Tracking master branch
  1. drupal
    1. 5 contributions/pager_preference/pager_preference.module

Functions & methods

NameDescription
pager_preference_menu
pager_preference_set_preference
pager_preference_set_preference_access
pager_preference_user
phptemplate_pagerOverride theme_pager()
theme_pager_preference_preference

File

View source
  1. <?php
  2. /*
  3. * @file
  4. * Pager preference allows users to set the number of results to display in a pager
  5. *
  6. */
  7. /*
  8. * Implements hook_menu()
  9. */
  10. function pager_preference_menu($may_cache) {
  11. $items = array();
  12. if (!$may_cache) {
  13. if (arg(0) == 'user-pager-preference' && is_numeric(arg(1)) && is_numeric(arg(2))) {
  14. $items[] = array(
  15. 'path' => 'user-pager-preference/'. arg(1) .'/'. arg(2),
  16. 'title' => t('Pager Preference'),
  17. 'callback' => 'pager_preference_set_preference',
  18. 'callback arguments' => array(arg(1), arg(2)),
  19. 'access' => pager_preference_set_preference_access(arg(2)),
  20. 'type' => MENU_CALLBACK,
  21. );
  22. }
  23. }
  24. return $items;
  25. }
  26. /*
  27. * Implements hook_user()
  28. */
  29. function pager_preference_user($op, &$edit, &$account, $category = NULL) {
  30. switch ($op) {
  31. case 'load':
  32. // Set pager preference for user
  33. if ($account->uid) {
  34. $result = db_query('SELECT pager_preference FROM {pager_preference} WHERE uid = %d', $account->uid);
  35. if (db_num_rows($result)) {
  36. $pager = db_fetch_object($result);
  37. $account->pager = $pager->pager_preference;
  38. }
  39. }
  40. break;
  41. }
  42. }
  43. /*
  44. * Set pager preference for user
  45. *
  46. * @param $limit
  47. * Pager preference to set
  48. * @param $uid
  49. * User id to set preference for
  50. */
  51. function pager_preference_set_preference($limit, $uid) {
  52. db_query("UPDATE {pager_preference} SET pager_preference = '%s' WHERE uid = %d", $limit, $uid);
  53. if (!db_affected_rows()) {
  54. db_query("INSERT INTO {pager_preference} (uid, pager_preference) VALUES (%d, '%s')", $uid, $limit);
  55. }
  56. // Reload the user object
  57. user_load(array('uid' => $uid));
  58. drupal_goto();
  59. }
  60. /*
  61. * Menu access callback for pager preference
  62. *
  63. * @param $uid
  64. * User id for pager preference being set
  65. *
  66. * @return boolean
  67. * TRUE: access is granted
  68. * FALSE: access is denied
  69. *
  70. */
  71. function pager_preference_set_preference_access($uid) {
  72. global $user;
  73. $access = FALSE;
  74. if ($uid == $user->uid) {
  75. $access = TRUE;
  76. }
  77. return $access;
  78. }
  79. /*
  80. * Theme pager preference
  81. *
  82. * @param $uid
  83. * User id for the preference being set
  84. */
  85. function theme_pager_preference_preference($uid) {
  86. $destination = drupal_get_destination();
  87. $output = '<div class="pager-preference">';
  88. $output .= t('Show ') . l(t('25'), 'user-pager-preference/25/'. $uid, array(), $destination) .' / '. l(t('50'), 'user-pager-preference/50/'. $uid, array(), $destination) .' / '. l(t('100'), 'user-pager-preference/100/'. $uid, array(), $destination);
  89. $output .= '</div>';
  90. return $output;
  91. }
  92. /**
  93. * Override theme_pager()
  94. *
  95. * Format a query pager.
  96. *
  97. * Menu callbacks that display paged query results should call theme('pager') to
  98. * retrieve a pager control so that users can view other results.
  99. *
  100. * @param $tags
  101. * An array of labels for the controls in the pager.
  102. * @param $limit
  103. * The number of query results to display per page.
  104. * @param $element
  105. * An optional integer to distinguish between multiple pagers on one page.
  106. * @param $parameters
  107. * An associative array of query string parameters to append to the pager links.
  108. * @return
  109. * An HTML string that generates the query pager.
  110. *
  111. * @ingroup themeable
  112. */
  113. function phptemplate_pager($tags = array(), $limit = 10, $element = 0, $parameters = array()) {
  114. global $pager_total;
  115. // Check if user has set a pagination preference
  116. global $user;
  117. if (!empty($user->pager)) {
  118. $limit = $user->pager;
  119. }
  120. $output = '';
  121. if ($pager_total[$element] > 1) {
  122. $output .= '<div class="pager">';
  123. $output .= theme('pager_first', ($tags[0] ? $tags[0] : t('« first')), $limit, $element, $parameters);
  124. $output .= theme('pager_previous', ($tags[1] ? $tags[1] : t('‹ previous')), $limit, $element, 1, $parameters);
  125. $output .= theme('pager_list', $limit, $element, ($tags[2] ? $tags[2] : 9 ), '', $parameters);
  126. $output .= theme('pager_next', ($tags[3] ? $tags[3] : t('next ›')), $limit, $element, 1, $parameters);
  127. $output .= theme('pager_last', ($tags[4] ? $tags[4] : t('last »')), $limit, $element, $parameters);
  128. $output .= '</div>';
  129. // Pagination preference
  130. if ($user->uid) {
  131. $output .= theme('pager_preference_preference', $user->uid);
  132. }
  133. return $output;
  134. }
  135. }