You are here

function bulk_photo_nodes_save_node in Bulk File Nodes 7

Batch operation: Saves an individual bulk photo node.

Parameters

string $node_type: The content type of the node.

array $node_fields: The fields of the given node.

array $node_overrides: Fields that will override $node_fields if they are empty.

Return value

bool TRUE if the node was saved successfully, FALSE otherwise.

1 string reference to 'bulk_photo_nodes_save_node'
bulk_photo_nodes_add_form_submit in ./bulk_photo_nodes.module
Form submission handler for bulk_photo_nodes_add_form().

File

./bulk_photo_nodes.module, line 611
hooks and helper functions for bulk photo node.

Code

function bulk_photo_nodes_save_node($node_type, $node_fields, $node_overrides, &$context) {

  // Move title and body back.
  $node_fields['right']['node']['body'] = $node_fields['right']['body'];
  $node_fields['right']['node']['title'] = $node_fields['right']['title'];

  // Update batch operation progressbar.
  $context['message'] = t('Saving node "@title"', array(
    '@title' => $node_fields['right']['node']['title'],
  ));
  $context['results'][] = $node_fields['right']['node']['title'];

  // Prepare a new node for saving.
  $node = (object) $node_fields['right']['node'];
  $node->type = $node_type;
  $node->language = LANGUAGE_NONE;
  node_object_prepare($node);

  // Move the file from public to field-defined destination.
  $options = bulk_photo_nodes_get_file_info($node_type);
  $image_field_name = $options['field_name'];
  $image_field_ref =& $node->{$image_field_name}[LANGUAGE_NONE][0];
  $image_field = (object) $node->{$image_field_name}[LANGUAGE_NONE][0];
  $extension = pathinfo($image_field->filename, PATHINFO_EXTENSION);
  $query_string = "SELECT COUNT(nid) FROM {node} WHERE type = :node_type AND uid = :uid";
  $images_count = db_query($query_string, array(
    ':node_type' => $node_type,
    ':uid' => $image_field->uid,
  ))
    ->fetchField();
  $images_count++;
  drupal_alter('bulk_photo_nodes_filename', $images_count);
  if ($options['directory'] == '/') {
    $options['directory'] = '';
  }
  $destination = $options['scheme'] . $image_field->uid . '-' . $images_count . '.' . $extension;
  $image_field->filename = $image_field->uid . '-' . $images_count . '.' . $extension;
  $image_field_ref = (array) file_move($image_field, $destination, FILE_EXISTS_REPLACE);

  // Correctly set all fields for the node.
  $node = bulk_photo_nodes_prepare_fields($node_fields, $node, $node_overrides, $image_field_name);
  drupal_alter('bulk_photo_nodes_node', $node, $node_fields, $node_overrides);
  if (!isset($node->path)) {
    $node->path = array();
  }
  $node->path['pathauto'] = TRUE;
  node_save($node);
  return !empty($node->nid) ? TRUE : FALSE;
}