Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @file
- */
- /**
- * Implements hook_menu().
- */
- function myModule_menu() {
- $items['admin/reports/myModule'] = array(
- 'title' => 'List of publishers and contributors',
- 'description' => 'List of pub and contrib',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('myModule_form'),
- 'access arguments' => array('access content'),
- 'type' => MENU_NORMAL_ITEM,
- );
- return $items;
- }
- /**
- * Page callback: my module settings
- *
- */
- function myModule_form($form, &$form_state) {
- $header = array('domain' => t('Sites to gather users from (click to select all)'));
- $options = myModule_get_domains();
- $index = 1;
- $sites_selection = variable_get('myModule_sites_selection','');
- foreach ($options as $key => $domain) {
- if ($sites_selection[$index] == $domain['domain']) {
- $default_values[$index] = TRUE;
- }
- $index = $index+1;
- }
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => 'Submit',
- );
- $form['myModule_sites_selection'] = array(
- '#type' => 'tableselect',
- '#header' => $header,
- '#options' => $options,
- '#default_value' => $default_values,
- '#attributes' => array('id' => 'tableselect-id', 'class' => array('tableselect-class')),
- '#empty' => t('No content available.'),
- );
- $form['submit2'] = array(
- '#type' => 'submit',
- '#value' => 'Submit',
- );
- $form['some_text'] = array(
- '#markup' => myModule_admin_get()
- );
- return $form;
- }
- function myModule_form_submit($form, &$form_state) {
- $options = myModule_get_domains();
- $sites_array = array();
- $index = 1;
- foreach($form_state['values']['myModule_sites_selection'] as $value) {
- if ($form_state['values']['myModule_sites_selection'][$index] == $index) {
- $sites_array[$index] = $options[$index]['domain'];
- }
- $index++;
- }
- // store domain names in variable, index don't work for default_value setting (0)
- variable_set('myModule_sites_selection', $sites_array);
- drupal_set_message(t('Review users below.'));
- }
- /**
- * Implements hook_permisson().
- */
- function myModule_permission() {
- return array('configure utilities administration' => array(
- 'title' => t('Configure utilities admin'),
- 'description' => t('Allow user to configure utilities admin.'),
- ));
- }
- function myModule_admin_get() {
- $check_domains = variable_get('myModule_sites_selection');
- $output = '<table border="1">';
- $output .= '<th>Domain</th><th>Name</th><th>Email</th><th>NetID</th><th>Role</th><th>Status</th><th>Last access</th>';
- foreach($check_domains as $index => $domain) {
- // call domain to get its users
- $output .= myModule_process_site_users($domain);
- }
- $output .= '</table>';
- return $output;
- }
- function myModule_process_site_users($domain) {
- $data = 'user=life&password=science';
- $options = array(
- 'method' => 'POST',
- 'data' => $data,
- 'timeout' => 15,
- 'headers' => array('Content-Type' => 'application/x-www-form-urlencoded'),
- );
- $users = drupal_http_request('http://'.$domain.'/api/v1/userslist', $options);
- global $user;
- $role = $user->roles;
- $is_admin = in_array("administrator", $role);
- if (!$is_admin){
- return '<tr><td colspan="6" align="center"><strong>You are not an Administrator</td></tr>';
- }
- $users_array = json_decode($users->data);
- $list = array();
- foreach ($users_array as $key => $value) {
- $output .= '<tr>';
- $output .= '<td>' . $domain .'</td>';
- $output .= '<td>' . $value->name .'</td>';
- $output .= '<td>'. $value->email .'</td>';
- $output .= '<td>'. $value->netID .'</td>';
- $output .= '<td>'. $value->role .'</td>';
- $output .= '<td>'. $value->status .'</td>';
- $output .= '<td>'. $value->last_access .'</td>';
- $output .= '</tr>';
- $list[] = array($domain, $value->name, $value->email, $value->netID, $value->role, $value->status, $value->last_access);
- }
- myModule_export_to_csv($list);
- return $output;
- }
- /**
- * Get all sites from a JSON file. If this is a local environment, use hardcoded sites.
- */
- function get_all_sites() {
- $sites = '';
- if (isset($_ENV['AH_SITE_ENVIRONMENT'])) {
- $all_sites = json_decode(file_get_contents('/var/www/html/site.'.$_ENV['AH_SITE_ENVIRONMENT'].'/docroot/sites/all/drush/sites.json'));
- switch ($_ENV['AH_SITE_ENVIRONMENT']) {
- case 'dev':
- $sites = $all_sites->dev;
- break;
- case 'test':
- $sites = $all_sites->test;
- break;
- case 'prod':
- $sites = $all_sites->prod;
- break;
- }
- } else {
- $sites = json_decode('{"me": "site.me","you": "site.you","thee": "site.thee"}');
- }
- return $sites;
- }
- /**
- * Get all domain after a JSON file was found.
- */
- function myModule_get_domains($sites) {
- $sites = get_all_sites();
- $domains = array ();
- $index = 1;
- foreach($sites as $site => $domain) {
- // $domains[$domain] = $domain; // $options can't have string offset
- // start options array at 1 rather than zero to get around bug with 0 index being always false
- $domains[$index] = array('domain' => $domain);
- // $domains[$index] = $domain;
- $index++;
- }
- return $domains;
- }
- function myModule_export_to_csv($list){
- drupal_add_http_header('Content-Type', 'text/csv');
- drupal_add_http_header('Content-Disposition', 'attachment;filename=csvfile.csv');
- $header = array('Domain','Name','Email','NetID','Role','Status','Last access');
- fputcsv($fp, $header);
- $fp = fopen('php://output', 'w');
- foreach($list as $line){
- fputcsv($fp, $line);
- }
- fclose($fp);
- drupal_exit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement