You are here

function permissions_by_term_form_taxonomy_form_term_alter in Permissions by Term 7

Implements hook_form_FORM_ID_alter().

File

./permissions_by_term.module, line 65
Allows access to terms in a vocabulary to be limited by user or role.

Code

function permissions_by_term_form_taxonomy_form_term_alter(&$form, $form_state, $form_id) {
  if (user_access('show term permission form on term page')) {

    // This is the add / edit term form from the taxonomy page.
    // Normally the term is an array, but when deleting terms it becomes an
    // object. So, we cast it to an object so we can consistently reference it.
    $term = (object) $form['#term'];
    $form['access'] = array(
      '#type' => 'fieldset',
      '#title' => t('Permissions'),
      '#description' => t("To limit access of this term and it's related nodes\n        (in node view and views) by user or roles, add users or roles to the\n        following lists. Leave empty to allow selection by all users."),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#attributes' => array(
        'id' => 'fieldset_term_access',
      ),
      '#weight' => -5,
      '#tree' => TRUE,
    );

    // Pull in any stored users in the database.
    $allowed_users = array();
    if (!empty($form['tid']['#value'])) {
      $allowed_users = db_query("SELECT u.name FROM {permissions_by_term_user} p\n                        JOIN {users} u ON p.uid = u.uid WHERE p.tid = :tid", array(
        ':tid' => $term->tid,
      ))
        ->fetchCol();
    }
    $allowed_users = drupal_implode_tags($allowed_users);

    // Note that the autocomplete widget will only enable for users with the
    // 'access profiles' permission. Other users will have to specify the name
    // manually.
    $form['access']['user'] = array(
      '#type' => 'textfield',
      '#title' => t('Allowed users'),
      '#description' => t('Enter a comma-seperated list of user names to ' . 'give them permission to use this term and access related nodes in ' . 'single node view and views listings.'),
      '#default_value' => $allowed_users,
      '#size' => 60,
      '#autocomplete_path' => 'permissions-by-term/autocomplete',
      '#weight' => -10,
    );
    $allowed_roles = array();
    if (!empty($form['tid']['#value'])) {
      $query = db_select('permissions_by_term_role', 'pbt')
        ->fields('pbt', array(
        'tid',
        'rid',
      ))
        ->condition('pbt.tid', $term->tid);

      // joins cannot be chained in Drupal oop db-queries.
      $query
        ->leftJoin('role', 'r', 'r.rid = pbt.rid');
      $query
        ->fields('r', array(
        'name',
      ));
      $allowed_roles = $query
        ->execute()
        ->fetchAllAssoc('name');
    }

    // firstly fetch all translated allowed role names.
    $arr__translated_allowed_role_names = array();
    foreach ($allowed_roles as $role) {
      $arr__translated_allowed_role_names[] = t($role->name);
    }

    // get all roles for the complete form and translate them.
    $arr__all_translated_user_roles = array();
    $array_key_counter = 1;
    foreach (user_roles() as $user_role_id => $user_role_name) {
      $arr__all_translated_user_roles[$user_role_id] = t($user_role_name);
      $array_key_counter++;
    }

    // generate the default values for the form.
    $allowed_roles_positions = array();
    if (!empty($arr__translated_allowed_role_names)) {
      foreach ($arr__translated_allowed_role_names as $role_name) {
        $pos = array_search($role_name, $arr__all_translated_user_roles);
        if (is_numeric($pos)) {
          $allowed_roles_positions[] = $pos;
        }
      }
    }

    // Now, lets do the Roles table.
    $form['access']['role'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Allowed roles'),
      '#description' => t('Select a role to allow all members of this role ' . 'to use this term and access related nodes in single node view and ' . 'views listings.'),
      '#default_value' => $allowed_roles_positions,
      '#options' => $arr__all_translated_user_roles,
      //'#options' => user_roles(),
      '#multiple' => FALSE,
      '#weight' => 5,
    );
    $form['#validate'][] = 'permissions_by_term_validate';
    $form['#submit'][] = 'permissions_by_term_submit';
  }
}