You are here

function theme_field_permissions_troubleshooting_form in Field Permissions 6

Render the Field permissions troubleshooting form.

File

includes/admin.inc, line 412
Administrative interface for the Field Permissions module.

Code

function theme_field_permissions_troubleshooting_form($form) {

  // Stop rendering if form has errors or no options have been supplied.
  if (form_get_errors() || empty($form['#node']) || empty($form['#field'])) {
    return drupal_render($form);
  }

  // Send javascript and stylesheets used for the troubleshooting report.
  $module_path = drupal_get_path('module', 'field_permissions');
  drupal_add_css($module_path . '/css/field_permissions.admin.css');
  drupal_add_js($module_path . '/js/field_permissions.tooltip.js');

  // Check access to the given field in the given node by the selected user.
  $base_node =& $form['#node'];
  $field =& $form['#field'];
  $base_account = !empty($form['#account']) ? $form['#account'] : drupal_anonymous_user();
  $modules = module_implements('field_access');
  $permissions_list = field_permissions_list();
  $user_roles = $base_account->uid == 1 ? array(
    -1 => t('site administrator (uid: 1)'),
  ) : $base_account->roles;
  $headers = array(
    t('User role'),
  );
  foreach ($permissions_list as $permission_type => $permission_info) {
    $headers[] = array(
      'data' => $permission_info['label'],
      'class' => 'field-permissions-header',
    );
  }
  $rows = array();
  foreach ($user_roles as $rid => $role_name) {
    $row = array(
      check_plain($role_name),
    );
    foreach ($permissions_list as $permission_type => $permission_info) {

      // Prepare the user account.
      if ($rid == DRUPAL_ANONYMOUS_RID) {
        $testing_account = drupal_anonymous_user();
        $testing_account->name = variable_get('anonymous', t('Anonymous'));
      }
      else {
        $testing_account = drupal_clone($base_account);
        $testing_account->roles = array(
          DRUPAL_AUTHENTICATED_RID => $testing_account->roles[DRUPAL_AUTHENTICATED_RID],
        );
        if ($testing_account->uid != 1 && $rid != DRUPAL_AUTHENTICATED_RID) {
          $testing_account->roles[$rid] = $role_name;
        }
      }

      // Reset the static storage in user_access().
      user_access('access content', $testing_account, TRUE);

      // Prepare the node.
      $testing_node = drupal_clone($base_node);

      // Prepare the results.
      $results = array();
      $result = TRUE;
      if ($permission_type == 'view' || $permission_type == 'view own') {
        $op = 'view';
        $result = $results['node_access(view)'] = node_access('view', $testing_node, $testing_account);
      }
      else {
        $op = 'edit';
        if ($permission_type == 'create') {
          unset($testing_node->nid);
          $result = $results['node_access(create)'] = node_access('create', $testing_node->type, $testing_account);
        }
        else {
          $result = $results['node_access(update)'] = node_access('update', $testing_node, $testing_account);
        }
      }

      // Check access to field only when node access is granted.
      if ($result !== FALSE) {
        foreach ($modules as $module) {
          $key = $module . '_field_access(' . $op . ')';
          $results[$key] = module_invoke($module, 'field_access', $op, $field, $testing_account, $testing_node);
          if ($results[$key] === FALSE) {
            $result = FALSE;
          }
        }
      }
      if ($result !== FALSE) {
        $status = 'on';
        $title = t('Access allowed');
      }
      else {
        $status = 'off';
        $title = t('Access denied');
      }
      $icon = '<span class="field-permissions-status field-permissions-status-' . $status . '" title="' . check_plain($title) . '"></span>';
      $items = array();
      foreach ($results as $key => $result) {
        $items[] = $key . ':&nbsp;' . check_plain(strtoupper(var_export($result, TRUE)));
      }
      $items = !empty($items) ? '<div class="field-permissions-tooltip">' . theme('item_list', $items, t('Detailed results for %role -vs- %operation', array(
        '%role' => $role_name,
        '%operation' => $permission_info['label'],
      ))) . '</div>' : '';
      $row[] = array(
        'data' => $icon . $items,
        'class' => 'field-permissions-cell',
      );
    }
    $rows[] = $row;
  }
  $form['report']['table'] = array(
    '#type' => 'markup',
    '#value' => theme('table', $headers, $rows),
  );
  $output = drupal_render($form);
  return $output;
}