Advertisement
Guest User

Untitled

a guest
Mar 13th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. <?php
  2. /**
  3. * @file
  4. */
  5.  
  6. /**
  7. * Implements hook_menu().
  8. */
  9.  
  10. function myModule_menu() {
  11. $items['admin/reports/myModule'] = array(
  12. 'title' => 'List of publishers and contributors',
  13. 'description' => 'List of pub and contrib',
  14. 'page callback' => 'drupal_get_form',
  15. 'page arguments' => array('myModule_form'),
  16. 'access arguments' => array('access content'),
  17. 'type' => MENU_NORMAL_ITEM,
  18. );
  19.  
  20. return $items;
  21. }
  22.  
  23. /**
  24. * Page callback: my module settings
  25. *
  26. */
  27. function myModule_form($form, &$form_state) {
  28.  
  29. $header = array('domain' => t('Sites to gather users from (click to select all)'));
  30. $options = myModule_get_domains();
  31.  
  32. $index = 1;
  33. $sites_selection = variable_get('myModule_sites_selection','');
  34.  
  35. foreach ($options as $key => $domain) {
  36.  
  37. if ($sites_selection[$index] == $domain['domain']) {
  38. $default_values[$index] = TRUE;
  39. }
  40. $index = $index+1;
  41. }
  42.  
  43. $form['submit'] = array(
  44. '#type' => 'submit',
  45. '#value' => 'Submit',
  46. );
  47.  
  48. $form['myModule_sites_selection'] = array(
  49. '#type' => 'tableselect',
  50. '#header' => $header,
  51. '#options' => $options,
  52. '#default_value' => $default_values,
  53. '#attributes' => array('id' => 'tableselect-id', 'class' => array('tableselect-class')),
  54. '#empty' => t('No content available.'),
  55. );
  56.  
  57. $form['submit2'] = array(
  58. '#type' => 'submit',
  59. '#value' => 'Submit',
  60. );
  61.  
  62. $form['some_text'] = array(
  63. '#markup' => myModule_admin_get()
  64. );
  65.  
  66. return $form;
  67. }
  68.  
  69. function myModule_form_submit($form, &$form_state) {
  70.  
  71. $options = myModule_get_domains();
  72.  
  73. $sites_array = array();
  74. $index = 1;
  75. foreach($form_state['values']['myModule_sites_selection'] as $value) {
  76. if ($form_state['values']['myModule_sites_selection'][$index] == $index) {
  77. $sites_array[$index] = $options[$index]['domain'];
  78. }
  79. $index++;
  80. }
  81.  
  82. // store domain names in variable, index don't work for default_value setting (0)
  83. variable_set('myModule_sites_selection', $sites_array);
  84. drupal_set_message(t('Review users below.'));
  85. }
  86.  
  87. /**
  88. * Implements hook_permisson().
  89. */
  90. function myModule_permission() {
  91. return array('configure utilities administration' => array(
  92. 'title' => t('Configure utilities admin'),
  93. 'description' => t('Allow user to configure utilities admin.'),
  94. ));
  95. }
  96.  
  97. function myModule_admin_get() {
  98. $check_domains = variable_get('myModule_sites_selection');
  99. $output = '<table border="1">';
  100. $output .= '<th>Domain</th><th>Name</th><th>Email</th><th>NetID</th><th>Role</th><th>Status</th><th>Last access</th>';
  101.  
  102. foreach($check_domains as $index => $domain) {
  103. // call domain to get its users
  104. $output .= myModule_process_site_users($domain);
  105. }
  106.  
  107. $output .= '</table>';
  108.  
  109. return $output;
  110. }
  111.  
  112. function myModule_process_site_users($domain) {
  113. $data = 'user=life&password=science';
  114. $options = array(
  115. 'method' => 'POST',
  116. 'data' => $data,
  117. 'timeout' => 15,
  118. 'headers' => array('Content-Type' => 'application/x-www-form-urlencoded'),
  119. );
  120. $users = drupal_http_request('http://'.$domain.'/api/v1/userslist', $options);
  121.  
  122. global $user;
  123. $role = $user->roles;
  124. $is_admin = in_array("administrator", $role);
  125. if (!$is_admin){
  126. return '<tr><td colspan="6" align="center"><strong>You are not an Administrator</td></tr>';
  127. }
  128.  
  129. $users_array = json_decode($users->data);
  130. $list = array();
  131. foreach ($users_array as $key => $value) {
  132. $output .= '<tr>';
  133. $output .= '<td>' . $domain .'</td>';
  134. $output .= '<td>' . $value->name .'</td>';
  135. $output .= '<td>'. $value->email .'</td>';
  136. $output .= '<td>'. $value->netID .'</td>';
  137. $output .= '<td>'. $value->role .'</td>';
  138. $output .= '<td>'. $value->status .'</td>';
  139. $output .= '<td>'. $value->last_access .'</td>';
  140. $output .= '</tr>';
  141. $list[] = array($domain, $value->name, $value->email, $value->netID, $value->role, $value->status, $value->last_access);
  142. }
  143.  
  144. myModule_export_to_csv($list);
  145. return $output;
  146. }
  147.  
  148. /**
  149. * Get all sites from a JSON file. If this is a local environment, use hardcoded sites.
  150. */
  151. function get_all_sites() {
  152.  
  153. $sites = '';
  154. if (isset($_ENV['AH_SITE_ENVIRONMENT'])) {
  155. $all_sites = json_decode(file_get_contents('/var/www/html/site.'.$_ENV['AH_SITE_ENVIRONMENT'].'/docroot/sites/all/drush/sites.json'));
  156. switch ($_ENV['AH_SITE_ENVIRONMENT']) {
  157. case 'dev':
  158. $sites = $all_sites->dev;
  159. break;
  160.  
  161. case 'test':
  162. $sites = $all_sites->test;
  163. break;
  164.  
  165. case 'prod':
  166. $sites = $all_sites->prod;
  167. break;
  168. }
  169. } else {
  170. $sites = json_decode('{"me": "site.me","you": "site.you","thee": "site.thee"}');
  171. }
  172.  
  173. return $sites;
  174.  
  175. }
  176.  
  177. /**
  178. * Get all domain after a JSON file was found.
  179. */
  180. function myModule_get_domains($sites) {
  181.  
  182. $sites = get_all_sites();
  183.  
  184. $domains = array ();
  185. $index = 1;
  186. foreach($sites as $site => $domain) {
  187. // $domains[$domain] = $domain; // $options can't have string offset
  188. // start options array at 1 rather than zero to get around bug with 0 index being always false
  189. $domains[$index] = array('domain' => $domain);
  190. // $domains[$index] = $domain;
  191. $index++;
  192. }
  193.  
  194. return $domains;
  195.  
  196. }
  197.  
  198. function myModule_export_to_csv($list){
  199. drupal_add_http_header('Content-Type', 'text/csv');
  200. drupal_add_http_header('Content-Disposition', 'attachment;filename=csvfile.csv');
  201.  
  202. $header = array('Domain','Name','Email','NetID','Role','Status','Last access');
  203. fputcsv($fp, $header);
  204. $fp = fopen('php://output', 'w');
  205. foreach($list as $line){
  206. fputcsv($fp, $line);
  207. }
  208. fclose($fp);
  209. drupal_exit();
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement