uc_roles.admin.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * @file
  4. * Roles administration menu items.
  5. */
  6. /**
  7. * Creates the header for the table/pager.
  8. */
  9. function _uc_roles_expiration_header() {
  10. return array(
  11. array('data' => t('Username'), 'field' => 'u.name'),
  12. array('data' => t('Role'), 'field' => 'e.rid'),
  13. array('data' => t('Expiration date'), 'field' => 'e.expiration', 'sort' => 'asc'),
  14. array('data' => t('Operations'), 'colspan' => 2),
  15. );
  16. }
  17. /**
  18. * Menu callback for viewing expirations.
  19. */
  20. function uc_roles_expiration($form, &$form_state) {
  21. // Create the header for the pager.
  22. $header = _uc_roles_expiration_header();
  23. // Grab all the info to build the pager.
  24. $query = db_select('uc_roles_expirations', 'e')->extend('PagerDefault')->extend('TableSort')
  25. ->fields('e')
  26. ->limit(50)
  27. ->orderByHeader($header);
  28. $query->join('users', 'u', 'e.uid = u.uid');
  29. $query->fields('u');
  30. $result = $query->execute();
  31. // Stick the expirations into the form.
  32. foreach ($result as $row) {
  33. $account = user_load($row->uid);
  34. $name = check_plain(format_username($account));
  35. $form['name'][$row->uid . ' ' . $row->rid] = array(
  36. '#theme' => 'username',
  37. '#account' => $account,
  38. '#name' => $name,
  39. );
  40. $form['role'][$row->uid . ' ' . $row->rid] = array('#markup' => check_plain(_uc_roles_get_name($row->rid)));
  41. $form['expiration'][$row->uid . ' ' . $row->rid] = array('#markup' => format_date($row->expiration, 'short'));
  42. $form['edit'][$row->uid . ' ' . $row->rid] = array('#markup' => l(t('edit'), 'user/' . $row->uid . '/edit', array('fragment' => 'role-expiration-' . $row->rid, 'query' => array('destination' => 'admin/people/expiration'))));
  43. $form['delete'][$row->uid . ' ' . $row->rid] = array('#markup' => l(t('delete'), 'admin/people/expiration/delete/' . $row->uid . '/' . $row->rid));
  44. }
  45. return $form;
  46. }
  47. /**
  48. * Themes user role expiration page.
  49. *
  50. * @param $variables
  51. * An associative array containing:
  52. * - form: A render element representing the form.
  53. *
  54. * @ingroup themeable
  55. */
  56. function theme_uc_roles_expiration($variables) {
  57. $form = $variables['form'];
  58. $header = _uc_roles_expiration_header();
  59. $rows = array();
  60. if (isset($form['name'])) {
  61. foreach (element_children($form['name']) as $key) {
  62. $rows[] = array(
  63. drupal_render($form['name'][$key]),
  64. drupal_render($form['role'][$key]),
  65. drupal_render($form['expiration'][$key]),
  66. drupal_render($form['edit'][$key]),
  67. drupal_render($form['delete'][$key]),
  68. );
  69. }
  70. }
  71. $output = theme('table', array(
  72. 'header' => $header,
  73. 'rows' => $rows,
  74. 'empty' => t('No expirations set to occur'),
  75. ));
  76. $output .= theme('pager');
  77. $output .= drupal_render_children($form);
  78. return $output;
  79. }
  80. /**
  81. * Form builder for role deletions.
  82. *
  83. * @see uc_roles_deletion_form_submit()
  84. * @ingroup forms
  85. */
  86. function uc_roles_deletion_form($form, &$form_state, $account, $rid) {
  87. $expiration = db_query("SELECT expiration FROM {uc_roles_expirations} WHERE uid = :uid AND rid = :rid", array(':uid' => $account->uid, ':rid' => $rid))->fetchField();
  88. if ($expiration) {
  89. $role_name = _uc_roles_get_name($rid);
  90. $form['user'] = array('#type' => 'value', '#value' => format_username($account));
  91. $form['uid'] = array('#type' => 'value', '#value' => $account->uid);
  92. $form['role'] = array('#type' => 'value', '#value' => $role_name);
  93. $form['rid'] = array('#type' => 'value', '#value' => $rid);
  94. $form = confirm_form(
  95. $form,
  96. t('Delete expiration of %role_name role for the user !user?', array(
  97. '!user' => theme('username', array(
  98. 'account' => $account,
  99. 'name' => check_plain(format_username($account)),
  100. 'link_path' => 'user/' . $account->uid,
  101. )),
  102. '%role_name' => $role_name,
  103. )),
  104. 'admin/people/expiration',
  105. t('Deleting the expiration will give !user privileges set by the %role_name role indefinitely unless manually removed.', array(
  106. '!user' => theme('username', array(
  107. 'account' => $account,
  108. 'name' => check_plain(format_username($account)),
  109. 'link_path' => 'user/' . $account->uid,
  110. )),
  111. '%role_name' => $role_name,
  112. )),
  113. t('Yes'),
  114. t('No')
  115. );
  116. }
  117. else {
  118. $form['error'] = array(
  119. '#markup' => t('Invalid user id or role id.'),
  120. );
  121. }
  122. return $form;
  123. }
  124. /**
  125. * Submission handler for uc_roles_deletion_form().
  126. *
  127. * @see uc_roles_deletion_form()
  128. */
  129. function uc_roles_deletion_form_submit($form, &$form_state) {
  130. uc_roles_delete(user_load($form_state['values']['uid']), $form_state['values']['rid']);
  131. drupal_goto('admin/people/expiration');
  132. }