You are here

function oa_files_upload_create_documents_form in Open Atrium Files 7.2

Form callback; Create multiple documents.

1 string reference to 'oa_files_upload_create_documents_form'
oa_files_upload_create_documents_form_page in ./oa_files.module
Page callback to return the create documents.

File

./oa_files.module, line 173

Code

function oa_files_upload_create_documents_form($form, &$form_state, $files) {
  $form = array();
  $form['documents'] = array(
    '#tree' => TRUE,
    '#theme' => 'table',
    '#header' => array(
      t('File Name'),
      t('Update'),
      t('Title'),
    ),
    '#rows' => array(),
    '#sticky' => FALSE,
  );
  $form['#files'] = $files;

  // Get the group/section to look for the file in.
  $gid = $sid = NULL;
  if (($context = og_context()) && og_user_access($context['group_type'], $context['gid'], "create oa_wiki_page content")) {
    $gid = $context['gid'];
    $section_id = !empty($_GET['oa_section_ref']) ? $_GET['oa_section_ref'] : oa_section_get_section_context();
    if ($section_id && node_access('view', node_load($section_id))) {
      $sid = $section_id;
    }
  }
  $form['#gid'] = $gid;
  $form['#sid'] = $sid;
  if (empty($sid) && module_exists('oa_sections')) {

    // Figure out best Section to create document page.
    // And prompt user to select section.
    $sid = oa_sections_get_best_section('oa_wiki_page', NULL, $gid);
    $form['oa_files_section'] = array(
      '#type' => 'oa_section_ref',
      '#title' => 'Section',
      '#default_value' => $sid,
      '#og_group_ref' => '',
    );
  }
  $has_update = FALSE;
  foreach ($files as $fid => $file) {

    // Just be sure they have access in case URL manipulation.
    if (!file_entity_access('view', $file)) {
      continue;
    }
    $form['documents'][$file->fid]['title'] = array(
      '#type' => 'textfield',
      '#title' => t('Title'),
      '#default_value' => $file->filename,
      '#weight' => $fid,
    );
    $form['documents'][$file->fid]['title'] = array(
      '#type' => 'textfield',
      '#title' => t('Title'),
      '#default_value' => $file->filename,
      '#weight' => $fid,
      '#attributes' => array(
        'style' => 'width:auto;',
      ),
    );
    $form['documents'][$file->fid]['update'] = array(
      '#access' => FALSE,
    );
    if ($node = oa_files_find_document_for_existing_document($file, $gid, $sid)) {
      $has_update = TRUE;
      $form['documents'][$file->fid]['update'] = array(
        '#type' => 'checkbox',
        '#title' => t('Document <a href="@url">@title</a> already contains @filename, make a new copy?', array(
          '@url' => url('node/' . $node->nid),
          '@title' => $node->title,
          '@filename' => $file->filename,
        )),
        '#default_value' => 0,
        '#weight' => $fid - 0.5,
      );
      $form['documents'][$file->fid]['title']['#states'] = array(
        'invisible' => array(
          ':input[name="documents[' . $file->fid . '][update]"]' => array(
            'checked' => FALSE,
          ),
        ),
      );
      $form['#files'][$file->fid]->node = $node;
      $form['#files'][$file->fid]->existing_document = TRUE;
    }
    elseif ($node = oa_files_find_document($file, $gid)) {
      $has_update = TRUE;
      $form['#files'][$file->fid]->node = $node;
      $form['documents'][$file->fid]['update'] = array(
        '#type' => 'checkbox',
        '#title' => t('Update Document <a href="@url">@title</a> to file @filename?', array(
          '@url' => url('node/' . $node->nid),
          '@title' => $node->title,
          '@filename' => $file->filename,
        )),
        '#default_value' => 1,
        '#weight' => $fid - 0.5,
      );
      $form['documents'][$file->fid]['title']['#states'] = array(
        'invisible' => array(
          ':input[name="documents[' . $file->fid . '][update]"]' => array(
            'checked' => TRUE,
          ),
        ),
      );
    }
    $form['documents']['#rows'][$file->fid] = array(
      array(
        'data' => check_plain($file->filename),
      ),
      array(
        'data' => &$form['documents'][$file->fid]['update'],
      ),
      array(
        'data' => &$form['documents'][$file->fid]['title'],
      ),
    );
  }

  // Remove update row if none are filled in.
  if (!$has_update) {
    foreach ($form['documents']['#rows'] as $fid => $row) {
      unset($form['documents']['#rows'][$fid][1]);
    }
    unset($form['documents']['#header'][1]);
  }
  $form['create'] = array(
    '#value' => t('Process'),
    '#type' => 'submit',
  );
  return $form;
}