uc_role_views-419700.patch 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. diff --git a/uc_roles/uc_roles.views.inc b/uc_roles/uc_roles.views.inc
  2. new file mode 100644
  3. index 0000000..558e461
  4. --- /dev/null
  5. +++ b/uc_roles/uc_roles.views.inc
  6. @@ -0,0 +1,110 @@
  7. +<?php
  8. +
  9. +/**
  10. + * @file
  11. + * Views 2 hooks and callback registries.
  12. + */
  13. +
  14. +
  15. +/**
  16. + * Implementation of hook_views_data().
  17. + */
  18. +
  19. + function uc_roles_views_data() {
  20. + $data['uc_roles_expirations']['table']['group'] = t('User');
  21. +
  22. + $data['uc_roles_expirations']['table']['join'] = array(
  23. + 'users' => array(
  24. + 'left_field' => 'uid',
  25. + 'field' => 'uid',
  26. + ),
  27. + );
  28. +
  29. +
  30. + // Expose the role expiration date
  31. + $data['uc_roles_expirations']['expiration'] = array(
  32. + 'title' => t('Ubercart Role expiration date/time'),
  33. + 'help' => t('Date and time the role will expire. (See also Role expiration role.)'),
  34. + 'field' => array(
  35. + 'handler' => 'views_handler_field_date',
  36. + 'click sortable' => TRUE,
  37. + ),
  38. + 'sort' => array(
  39. + 'handler' => 'views_handler_sort_date',
  40. + ),
  41. + 'filter' => array(
  42. + 'handler' => 'views_handler_filter_date',
  43. + ),
  44. + );
  45. +
  46. +
  47. + // Expose the role id from uc_roles_expirations
  48. + $data['uc_roles_expirations']['rid'] = array(
  49. + 'title' => t('Ubercart Role expiration role'),
  50. + 'help' => t('The Role that corresponds with the Role expiration date/time'),
  51. + // Information for displaying the rid
  52. + 'field' => array(
  53. + 'handler' => 'uc_roles_handler_field_rid',
  54. + 'click sortable' => TRUE,
  55. + ),
  56. + // Information for accepting a rid as an argument
  57. + 'argument' => array(
  58. + 'handler' => 'views_handler_argument_users_roles_rid',
  59. + 'name field' => 'title', // the field to display in the summary.
  60. + 'numeric' => TRUE,
  61. + 'validate type' => 'rid',
  62. + ),
  63. + // Information for accepting a uid as a filter
  64. + 'filter' => array(
  65. + 'handler' => 'views_handler_filter_user_roles',
  66. + ),
  67. + // Information for sorting on a uid.
  68. + 'sort' => array(
  69. + 'handler' => 'views_handler_sort',
  70. + ),
  71. + );
  72. +
  73. +
  74. + return $data;
  75. +}
  76. +
  77. +class uc_roles_handler_field_rid extends views_handler_field {
  78. +
  79. + // Derived from views_handler_field_user_roles
  80. + // Purpose: get the *names* that correspond to the role_expire_rids.
  81. + function pre_render($values) {
  82. + $roles = array();
  83. + $this->items = array();
  84. +
  85. + // Get all the unique role ids into the keys of $roles. Initializing into
  86. + // array_keys helps prevent us from having a list where the same rid appears
  87. + // over and over and over.
  88. + foreach ($values as $result) {
  89. + $roles[$this->get_value($result, NULL, TRUE)] = FALSE;
  90. + }
  91. +
  92. + if ($roles) {
  93. + $result = db_query("SELECT r.rid, r.name FROM {role} r WHERE r.rid IN (:rids) ORDER BY r.name",
  94. + array(':rids' => array_keys($roles)));
  95. + foreach ($result as $role) {
  96. + $this->items[$role->rid]['role'] = check_plain($role->name);
  97. + $this->items[$role->rid]['rid'] = $role->rid;
  98. + }
  99. + }
  100. + }
  101. +
  102. + // Render the rid as the role name.
  103. + function render($values) {
  104. +
  105. + // Return the role name corresponding to the role ID.
  106. + // TODO: Should I be using this->get_value() here?
  107. + $rid = $values->uc_roles_expirations_rid;
  108. + if ($rid) {
  109. + $role = $this->items[$rid]['role'];
  110. + if (!empty($role)) {
  111. + return $role;
  112. + }
  113. + }
  114. + }
  115. +}
  116. +
  117. diff --git a/uc_roles/uc_roles.info b/uc_roles/uc_roles.info
  118. index f7c899e..ccd2d43 100644
  119. --- a/uc_roles/uc_roles.info
  120. +++ b/uc_roles/uc_roles.info
  121. @@ -2,8 +2,12 @@ name = Roles
  122. description = Assigns permanent or expirable roles based on product purchases.
  123. dependencies[] = uc_product
  124. dependencies[] = uc_order
  125. +dependencies[] = views
  126. package = Ubercart - core (optional)
  127. core = 7.x
  128. ; Test cases
  129. files[] = tests/uc_roles.test
  130. +
  131. +; Views handlers
  132. +files[] = uc_roles.views.inc
  133. diff --git a/uc_roles/uc_roles.module b/uc_roles/uc_roles.module
  134. index d7ee52c..65bdd74 100644
  135. --- a/uc_roles/uc_roles.module
  136. +++ b/uc_roles/uc_roles.module
  137. @@ -1278,3 +1278,9 @@ function _uc_roles_get_expiration($duration, $granularity, $start_time = NULL) {
  138. return strtotime($operator . $duration . ' ' . $granularity, $start_time);
  139. }
  140. +/**
  141. + * Implements hook_views_api().
  142. + */
  143. +function uc_roles_views_api() {
  144. + return array('api' => '3.0');
  145. +}