You are here

function user_relationship_node_access_form_alter in User Relationships 6

Same name and namespace in other branches
  1. 5.3 user_relationship_node_access/user_relationship_node_access.module \user_relationship_node_access_form_alter()
  2. 5.2 plugins/user_relationship_node_access/user_relationship_node_access.module \user_relationship_node_access_form_alter()

hook_form_alter()

File

user_relationship_node_access/user_relationship_node_access.module, line 92
User Relationships Node Access module Allows content posted to be shared with users in one's social network

Code

function user_relationship_node_access_form_alter(&$form, $form_state, $form_id) {
  global $user;
  if (isset($form['#node']) && $form['#node'] && $form['#node']->uid != $user->uid) {
    return;
  }
  if (!isset($form['type']) || is_null($form['type']) || !isset($form['type']['#value']) || $form['type']['#value'] . '_node_form' != $form_id || !count($types = user_relationships_types_load())) {
    return;
  }

  // verify we can set permissions on this node
  if (!_user_relationship_node_access_node_eligible($form['#node'])) {
    return;
  }
  foreach ($types as $rtid => $type) {
    unset($types[$rtid]);
    if ($type->is_oneway) {
      $types["{$rtid}_yt"] = t('Post to @type (you to them)', array(
        '@type' => $type->plural_name,
      ));
      $types["{$rtid}_ty"] = t('Post to @type (them to you)', array(
        '@type' => $type->plural_name,
      ));
    }
    else {
      $types[$rtid] = t('Post to @type', array(
        '@type' => $type->plural_name,
      ));
    }
  }
  asort($types);

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

  // get permissions that can be set - from node author not current user
  $author_allowed_perms = _user_relationship_node_access_get_allowed_grants(user_load($form['#node']->uid));
  if (!count($author_allowed_perms)) {
    return;
  }

  // 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') && og_is_group_post_type($form['type']['#value']);
  $form['user_relationship_node_access'] = array(
    '#type' => 'fieldset',
    '#title' => $use_advanced_form ? t('User Relationships Node Access') : t('Post to Social Network'),
    '#description' => $is_group_post ? t('Node access based on your relationships to other users, even if they are not in selected groups') : t('Node access based on your relationships to other users'),
    '#collapsible' => TRUE,
    '#collapsed' => $use_advanced_form,
    '#tree' => TRUE,
  );

  //place fieldset where content type admin has placed it on cck field management page
  if (module_exists('content')) {
    $fieldset_weight = content_extra_field_weight($form['type']['#value'], 'user_relationship_node_access');
    if (isset($fieldset_weight)) {
      $form['user_relationship_node_access']['#weight'] = $fieldset_weight;
    }
  }

  //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),
      ));
    }
  }
}