uc_roles.views.inc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * @file
  4. * Views 2 hooks and callback registries.
  5. */
  6. /**
  7. * Implementation of hook_views_data().
  8. */
  9. function uc_roles_views_data() {
  10. $data['uc_roles_expirations']['table']['group'] = t('User');
  11. $data['uc_roles_expirations']['table']['join'] = array(
  12. 'users' => array(
  13. 'left_field' => 'uid',
  14. 'field' => 'uid',
  15. ),
  16. );
  17. // Expose the role expiration date
  18. $data['uc_roles_expirations']['expiration'] = array(
  19. 'title' => t('Ubercart Role expiration date/time'),
  20. 'help' => t('Date and time the role will expire. (See also Role expiration role.)'),
  21. 'field' => array(
  22. 'handler' => 'views_handler_field_date',
  23. 'click sortable' => TRUE,
  24. ),
  25. 'sort' => array(
  26. 'handler' => 'views_handler_sort_date',
  27. ),
  28. 'filter' => array(
  29. 'handler' => 'views_handler_filter_date',
  30. ),
  31. );
  32. // Expose the role id from uc_roles_expirations
  33. $data['uc_roles_expirations']['rid'] = array(
  34. 'title' => t('Ubercart Role expiration role'),
  35. 'help' => t('The Role that corresponds with the Role expiration date/time'),
  36. // Information for displaying the rid
  37. 'field' => array(
  38. 'handler' => 'uc_roles_handler_field_rid',
  39. 'click sortable' => TRUE,
  40. ),
  41. // Information for accepting a rid as an argument
  42. 'argument' => array(
  43. 'handler' => 'views_handler_argument_users_roles_rid',
  44. 'name field' => 'title', // the field to display in the summary.
  45. 'numeric' => TRUE,
  46. 'validate type' => 'rid',
  47. ),
  48. // Information for accepting a uid as a filter
  49. 'filter' => array(
  50. 'handler' => 'views_handler_filter_user_roles',
  51. ),
  52. // Information for sorting on a uid.
  53. 'sort' => array(
  54. 'handler' => 'views_handler_sort',
  55. ),
  56. );
  57. return $data;
  58. }
  59. class uc_roles_handler_field_rid extends views_handler_field {
  60. // Derived from views_handler_field_user_roles
  61. // Purpose: get the *names* that correspond to the role_expire_rids.
  62. function pre_render($values) {
  63. $roles = array();
  64. $this->items = array();
  65. // Get all the unique role ids into the keys of $roles. Initializing into
  66. // array_keys helps prevent us from having a list where the same rid appears
  67. // over and over and over.
  68. foreach ($values as $result) {
  69. $roles[$this->get_value($result, NULL, TRUE)] = FALSE;
  70. }
  71. if ($roles) {
  72. $result = db_query("SELECT r.rid, r.name FROM {role} r WHERE r.rid IN (:rids) ORDER BY r.name",
  73. array(':rids' => array_keys($roles)));
  74. foreach ($result as $role) {
  75. $this->items[$role->rid]['role'] = check_plain($role->name);
  76. $this->items[$role->rid]['rid'] = $role->rid;
  77. }
  78. }
  79. }
  80. // Render the rid as the role name.
  81. function render($values) {
  82. // Return the role name corresponding to the role ID.
  83. // TODO: Should I be using this->get_value() here?
  84. $rid = $values->uc_roles_expirations_rid;
  85. if ($rid) {
  86. $role = $this->items[$rid]['role'];
  87. if (!empty($role)) {
  88. return $role;
  89. }
  90. }
  91. }
  92. }