You are here

function private_files_download_permission_get_directory_form in Private files download permission 7.2

(Form callback.) Displays a form to add/edit a directory.

2 string references to 'private_files_download_permission_get_directory_form'
private_files_download_permission_add_directory in ./private_files_download_permission.module
(Page callback.) Adds a directory to the control list.
private_files_download_permission_edit_directory in ./private_files_download_permission.module
(Page callback.) Edits a directory in the control list.

File

./private_files_download_permission.module, line 226
Handles both module settings and its behaviour.

Code

function private_files_download_permission_get_directory_form($form, &$form_state, $did) {
  $directory_list = private_files_download_permission_get_directory_list();
  $form = array();

  // Check that $did is actually a valid directory id, if not blank.
  if (NULL !== $did) {
    if (!in_array($did, array_keys($directory_list))) {
      drupal_set_message(t('You need to provide a valid directory id.'), 'error');
      return;
    }
  }

  // Prepare default values.
  $default_path = NULL;
  $default_bypass = FALSE;
  $default_grant_file_owners = FALSE;
  if (variable_get('private_files_download_permission_by_user_checks')) {
    $default_users = array();
  }
  $default_roles = array();
  if (NULL !== $did) {
    $default_path = $directory_list[$did]->path;
    $default_bypass = $directory_list[$did]->bypass;
    $default_grant_file_owners = $directory_list[$did]->grant_file_owners;
    if (variable_get('private_files_download_permission_by_user_checks')) {
      $default_users = array_keys($directory_list[$did]->uid);
    }
    $default_roles = array_keys($directory_list[$did]->rid);
  }

  // Prepare the directory id value to be eventually submitted.
  $form['did'] = array(
    '#type' => 'value',
    '#value' => $did,
  );

  // Prepare the path text field.
  $form['path'] = array(
    '#type' => 'textfield',
    '#title' => t('Path'),
    '#field_prefix' => variable_get('file_private_path'),
    '#size' => 60,
    '#maxlength' => 255,
    '#required' => TRUE,
    '#default_value' => $default_path,
  );

  // Prepare the bypass checkbox.
  $form['bypass'] = array(
    '#type' => 'checkbox',
    '#title' => t('Bypass'),
    '#default_value' => $default_bypass,
    '#description' => t('Enable to make this module ignore the above path.'),
  );

  // Prepare the grant file owners checkbox.
  $form['grant_file_owners'] = array(
    '#type' => 'checkbox',
    '#title' => t('Grant file owners'),
    '#default_value' => $default_grant_file_owners,
    '#description' => t('Grant access to users who uploaded the files (i.e.: the file owners).'),
  );

  // Prepare the user checkbox fieldset.
  if (variable_get('private_files_download_permission_by_user_checks')) {
    $form['users'] = array(
      '#type' => 'fieldset',
      '#title' => t('Enabled users'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
  }

  // Prepare user checkboxes.
  if (variable_get('private_files_download_permission_by_user_checks')) {
    $users = array_flip(private_files_download_permission_get_users());
    ksort($users, SORT_NATURAL | SORT_FLAG_CASE);
    $users = array_flip($users);
    foreach ($users as $uid => $user) {
      $form['users']['user_' . $uid] = array(
        '#type' => 'checkbox',
        '#title' => check_plain($user),
        '#default_value' => NULL === $did && 1 === $uid ? TRUE : in_array($uid, $default_users),
      );
    }
  }

  // Prepare the role checkbox fieldset.
  $form['roles'] = array(
    '#type' => 'fieldset',
    '#title' => t('Enabled roles'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );

  // Prepare role checkboxes.
  $roles = array_flip(user_roles());
  ksort($roles, SORT_NATURAL | SORT_FLAG_CASE);
  $roles = array_flip($roles);
  foreach ($roles as $rid => $role) {
    $form['roles']['role_' . $rid] = array(
      '#type' => 'checkbox',
      '#title' => check_plain($role),
      '#default_value' => in_array($rid, $default_roles),
    );
  }

  // Prepare the submit button.
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save directory to the control list'),
  );

  // Return form.
  return $form;
}