You are here

function _user_relationship_node_access_permission_form in User Relationships 7

Helper function to generate the permissions grid for a node edit form or for a user edit form.

Parameters

$object_type: The type of form being viewed. Either 'node' or 'user'.

$object: The object associated with the form being viewed. If $object_type is 'node', pass the node object. If $object_type is 'user', pass the user object.

Return value

A Form API array suitable for including in another form.

2 calls to _user_relationship_node_access_permission_form()
user_relationship_node_access_form_node_form_alter in user_relationship_node_access/user_relationship_node_access.module
Implements hook_form_BASE_FORM_ID_alter() for node_form.
user_relationship_node_access_form_user_profile_form_alter in user_relationship_node_access/user_relationship_node_access.module
Implements hook_form_BASE_FORM_ID_alter() for user_profile_form.

File

user_relationship_node_access/user_relationship_node_access.module, line 163
Allows content posted to be shared with users in one's social network

Code

function _user_relationship_node_access_permission_form($object_type, $object) {
  $form = array();
  $types = user_relationships_types_load();
  if ($object_type == 'node') {
    $node = $object;
    $account = user_load($node->uid);
  }
  else {
    $account = $object;
  }
  foreach ($types as $rtid => $type) {
    unset($types[$rtid]);
    if ($type->is_oneway) {
      $types["{$rtid}_yt"] = t('Post to @rel_name', user_relationships_type_translations($type));
      $types["{$rtid}_ty"] = t('Post to @rel_name_reverse', user_relationships_type_translations($type));
    }
    else {
      $types[$rtid] = t('Post to @rel_name', user_relationships_type_translations($type));
    }
  }
  asort($types);

  // reverse the optimization made after saving
  $permissions = array();
  if ($object_type == 'node' && isset($node->user_relationship_node_access) && is_array($node->user_relationship_node_access)) {
    foreach ($node->user_relationship_node_access as $rtid => $permission) {
      foreach ($permission as $action => $trash) {
        $permissions[$action][$rtid] = $rtid;
      }
    }
  }
  else {

    // There are no permissions set on this node. Pull in the defaults from
    // the author of the node.
    if (isset($account->data['user_relationship_node_access_defaults'])) {
      $permissions = $account->data['user_relationship_node_access_defaults'];
    }
  }

  // Get permissions that can be set - from node author or the user defaults.
  $author_allowed_perms = _user_relationship_node_access_get_allowed_grants(user_load($object->uid));
  if (!count($author_allowed_perms)) {
    return array();
  }

  // use advanced form with a table if more than one permission, otherwise just a simple 'post to related users' checkbox
  $use_advanced_form = count($author_allowed_perms) > 1;

  // different labels for group posts (that have the group audience fieldset)
  $is_group_post = module_exists('og') && $object_type == 'node' && og_is_group_content_type('node', $node->type);
  $form['user_relationship_node_access'] = array(
    '#type' => 'fieldset',
    '#title' => t('Share content'),
    '#description' => $is_group_post ? t('Content access based on your relationships to other users, even if they are not in selected groups') : t('Content access based on your relationships to other users'),
    '#collapsible' => TRUE,
    '#collapsed' => $use_advanced_form,
    '#group' => 'additional_settings',
    '#tree' => TRUE,
  );

  //theme as a table if more than one permission is given
  if ($use_advanced_form) {
    $form['user_relationship_node_access']['#theme'] = 'user_relationship_node_access_form';
  }
  foreach ($author_allowed_perms as $action) {
    $defaults = isset($permissions[$action]) ? $permissions[$action] : array();
    $form['user_relationship_node_access'][$action] = array(
      '#type' => 'checkboxes',
      '#multiple' => TRUE,
      '#options' => $types,
      '#default_value' => $defaults,
      '#description' => $is_group_post ? t('@group_post_ur_node_access_description', array(
        '@group_post_ur_node_access_description' => '',
      )) : t('If no box is ticked, then anyone can @action.', array(
        '@action' => t($action),
      )),
    );
    if ($use_advanced_form) {
      $form['user_relationship_node_access'][$action]['#title'] = t('@action', array(
        '@action' => ucfirst($action),
      ));
    }
  }
  return $form;
}