You are here

function oa_files_create_document in Open Atrium Files 7.2

Creates a document from a file.

Parameters

$file: A file object.

$title: The title of document. Returns the created document node

1 call to oa_files_create_document()
oa_files_upload_create_documents_form_submit in ./oa_files.module
Submit callback that creates or updates documents.

File

./oa_files.module, line 317

Code

function oa_files_create_document($file, $title, $gid = NULL, $sid = NULL) {
  global $user;
  $node = (object) array(
    // Create oa_files.
    'type' => 'oa_wiki_page',
    'uid' => $user->uid,
    'name' => $user->name,
    // Add the file to the oa_media attachment field.
    'field_oa_media' => array(
      LANGUAGE_NONE => array(
        0 => array(
          'fid' => $file->fid,
          'display' => 1,
          'description' => $title,
        ),
      ),
    ),
    'language' => LANGUAGE_NONE,
    // Set the Title of the Document node to the same title.
    'title' => $title,
    'is_new' => TRUE,
  );

  // Set the Space (og_group_ref) to the current Space Context.
  // Set the Section (oa_section_ref) to the current Section Context.
  if ($gid) {
    $node->og_group_ref = array(
      LANGUAGE_NONE => array(
        0 => array(
          'target_id' => $gid,
        ),
      ),
    );
    if ($sid) {
      $node->oa_section_ref = array(
        LANGUAGE_NONE => array(
          0 => array(
            'target_id' => $sid,
          ),
        ),
      );
    }
    if (isset($_GET['menu_parent']) || $sid) {
      $pid = !empty($_GET['menu_parent']) ? $_GET['menu_parent'] : $sid;
      if (($parent = node_load($pid)) && node_access('view', $parent) && ($mlid = og_menu_single_get_link_mlid('node', $pid))) {
        $is_in_group = FALSE;
        foreach (field_get_items('node', $parent, 'og_group_ref') as $value) {
          if ($value['target_id'] == $gid) {
            $is_in_group = TRUE;
            break;
          }
        }
        if ($is_in_group) {
          $node->menu = array(
            'plid' => $mlid,
            'link_title' => $title,
            'enabled' => TRUE,
            'description' => '',
          );
          if ($parent->type == 'oa_section') {
            $node->oa_section_ref = array(
              LANGUAGE_NONE => array(
                0 => array(
                  'target_id' => $parent->nid,
                ),
              ),
            );
          }
        }
      }
    }
    if (!empty($_GET['term_parent']) && ($term = taxonomy_term_load($_GET['term_parent']))) {

      // if using og_vocab, grab the valid vocab ids.
      $fieldname = '';
      $propname = '';
      if (module_exists('og_vocab')) {
        $vids = og_vocab_get_accessible_vocabs('node', $node->type, OG_VOCAB_FIELD);
        if (in_array($term->vid, $vids)) {
          $fieldname = 'og_vocabulary';
          $propname = 'target_id';
        }
      }
      if (empty($fieldname)) {

        // did not find og vocab, so check for global taxonomy reference fields
        $vocab = taxonomy_vocabulary_load($term->vid);
        $field_info = field_info_fields();
        $fields = field_info_instances('node', $node->type);
        foreach ($fields as $key => $field) {
          $info = $field_info[$key];
          if ($info['module'] == 'taxonomy') {
            foreach ($info['settings']['allowed_values'] as $index => $item) {
              if ($item['vocabulary'] == $vocab->machine_name) {
                $fieldname = $info['field_name'];
                $propname = 'tid';
                break;
              }
            }
          }
        }
      }
      if (!empty($fieldname)) {
        $node->{$fieldname}[LANGUAGE_NONE][] = array(
          $propname => $term->tid,
        );
      }
    }
    if (module_exists('workbench_moderation')) {
      $node->workbench_moderation_state_new = variable_get('workbench_moderation_default_state_' . $node->type, workbench_moderation_state_none());
    }
    node_save($node);
    drupal_set_message(t('Document <a href="@url">@title</a> created.', array(
      '@title' => $node->title,
      '@url' => url('node/' . $node->nid),
    )));
  }
  return $node;
}