uid); if ($owned_resources = _wsauth_get_owned_resources($owner)) { // Sort resources according to title uasort( $owned_resources, function ($a, $b) { if (strtolower($a->title) == strtolower($b->title)) { return 0; } else { return (strtolower($a->title) < strtolower($b->title)) ? -1 : 1; } } ); // Fetch users that are permitted to access our resources $permitted_user_uids = array(); foreach ($owned_resources as $resource) { if ($field_permitted_users = field_get_items('node', $resource, 'field_permitted_users')) { foreach ($field_permitted_users as $uid) { $permitted_user_uids[$uid['target_id']] = $uid['target_id']; } } } $permitted_users = entity_load('user', $permitted_user_uids); // FIXA: Sorteringarna bör göras i databasen i stället uasort( $permitted_users, function ($a, $b) { if (strtolower($a->name) == strtolower($b->name)) { return 0; } else { return (strtolower($a->name) < strtolower($b->name)) ? -1 : 1; } } ); $form['permissions'] = array( '#theme' => 'wsauth_permissions_table', ); // Get current users and resources $options = array_fill_keys(array_keys($owned_resources), ''); $form['permissions']['users'] = array( '#tree' => TRUE, ); foreach ($permitted_users as $id => $permitted_user) { $permitted_resources = _wsauth_get_permitted_resources($permitted_user); $form['permissions']['users'][$id] = array( 'userinfo' => array( '#type' => 'item', '#markup' => theme('wsauth_permissions_form_userinfo', array('user' => $permitted_user)), ), 'resources' => array( '#type' => 'checkboxes', '#options' => $options, '#default_value' => array_combine(array_keys($permitted_resources), array_keys($permitted_resources)), ), ); } // Fields for adding users if (!isset($form_state['num_new_users'])) { $form_state['num_new_users'] = 0; } if (isset($form_state['triggering_element']) && $form_state['triggering_element']['#name'] == 'add_another_user') { $form_state['num_new_users']++; } $languages = array(); foreach (language_list() as $language) { $languages[$language->language] = t($language->name); } $form['permissions']['new_users'] = array( '#tree' => TRUE, ); for ($i = 0; $i < $form_state['num_new_users']; $i++) { $form['permissions']['new_users'][$i] = array( 'userinfo' => array( 'name' => array( '#title' => t('Name'), '#type' => 'textfield', ), 'mail' => array( '#title' => t('E-mail address'), '#type' => 'textfield', ), 'language' => array( '#title' => t('Language'), '#type' => 'select', '#options' => $languages, '#default_value' => language_default('language'), ), ), 'resources' => array( '#type' => 'checkboxes', '#options' => $options, ), ); } // Misc foreach ($owned_resources as $id => $owned_resource) { $form['permissions']['owned_resources'][$id] = array( '#markup' => check_plain($owned_resource->title), '#tree' => TRUE, ); } $form['actions'] = array( '#type' => 'actions', 'submit' => array( '#type' => 'submit', '#name' => 'submit', '#value' => t('Save permissions'), ), 'add_another_user' => array( '#type' => 'button', '#name' => 'add_another_user', '#value' => t('Add user'), '#ajax' => array( 'callback' => 'wsauth_permissions_form_add_another_user_callback', 'wrapper' => 'permissions', 'method' => 'replace', ), ), ); $form['#validate'] = array('wsauth_permissions_form_validate'); $form['#submit'] = array('wsauth_permissions_form_submit'); } else { $form['noresources'] = array( '#markup' => t('No resources found'), ); } return $form; } function wsauth_permissions_form_add_another_user_callback($form, $form_state) { return $form['permissions']; } function theme_wsauth_permissions_table($variables) { // Inspired by user.admin.inc:theme_user_admin_permissions $form = $variables['table']; $rows = array(); if (isset($form['users'])) { foreach (element_children($form['users']) as $uid) { $row = array(); $row[] = array( 'data' => drupal_render($form['users'][$uid]['userinfo']), 'class' => array('permission'), ); foreach (element_children($form['users'][$uid]['resources']) as $resid) { $row[] = array( 'data' => drupal_render($form['users'][$uid]['resources'][$resid]), 'class' => array('checkbox'), ); } $rows[] = $row; } } if (isset($form['new_users'])) { foreach (element_children($form['new_users']) as $id) { $row = array(); $row[] = array( 'data' => drupal_render($form['new_users'][$id]['userinfo']), 'class' => array('permission', 'permission-new-user'), ); foreach (element_children($form['new_users'][$id]['resources']) as $resid) { $row[] = array( 'data' => drupal_render($form['new_users'][$id]['resources'][$resid]), 'class' => array('checkbox', 'checkbox-new-user'), ); } $rows[] = $row; } } $header = array(); if (isset($form['owned_resources'])) { $header[] = t('User'); foreach (element_children($form['owned_resources']) as $res) { $header[] = array( 'data' => drupal_render($form['owned_resources'][$res]), ); } } $output = theme( 'table', array( 'header' => $header, 'rows' => $rows, 'attributes' => array( 'id' => 'permissions', ), ) ); return $output; } function theme_wsauth_permissions_form_userinfo($variables) { $user = $variables['user']; $output = '