You are here

function book_copy_copy in Book Copy 7.2

Callback for replicating a book outline below a passed node.

1 string reference to 'book_copy_copy'
book_copy_process_copy in ./book_copy.module
Execute a batch process.

File

./book_copy.module, line 175
Book copy, copy book outlines

Code

function book_copy_copy($initial_nid, $new_title, $queue_id, &$context) {

  // batch job is starting
  if (!isset($context['sandbox']['progress'])) {
    $node = node_load($initial_nid);
    $copy_list = _book_copy_list($node->book);
    $context['sandbox']['max'] = count($copy_list);
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['list_left'] = array_keys($copy_list);
    $context['sandbox']['map'] = array();
    $context['sandbox']['root'] = NULL;
    $context['sandbox']['copy_list'] = $copy_list;
  }
  $limit = BOOK_COPY_PROCESSING_LIMIT;
  $count = 0;
  $list_left = $context['sandbox']['list_left'];

  // Replicate limit nodes at a time.  This needs to be set fairly low for stability
  foreach ($list_left as $nid) {
    $count++;
    array_shift($context['sandbox']['list_left']);
    $entity = node_load($nid);

    // clone the item so we can modify it prior to save
    $clone = replicate_clone_entity('node', $entity);

    // reset the owner to the author committing this action
    if (!drupal_is_cli()) {
      $clone->uid = $GLOBALS['user']->uid;
    }

    // test for initial item with title update
    if ($nid == $initial_nid) {
      $clone->title = $new_title;
    }

    // test for root
    if ($clone->book['plid'] == 0) {
      unset($clone->book['nid']);
      $clone->book['mlid'] = NULL;

      // this will trigger a new book to be created
      $clone->book['bid'] = 'new';
    }
    else {

      // unset this menu id
      $clone->book['mlid'] = NULL;
      if (is_object($context['sandbox']['root'])) {
        $clone->book['bid'] = $context['sandbox']['root']->book['bid'];
        $clone->book['menu_name'] = $context['sandbox']['root']->book['menu_name'];
      }

      // perform the mapping look up if   we have it
      $parent = menu_link_load($clone->book['plid']);
      $pnid = str_replace('node/', '', $parent['link_path']);
      if (isset($context['sandbox']['map'][$pnid])) {
        $clone->book['plid'] = $context['sandbox']['map'][$pnid];
      }
    }

    // save the cloned item
    node_save($clone);

    // create a map of the odl nid and the new nid and save
    $old_node_nid = $entity->nid;
    $new_node_nid = $clone->nid;
    $nid_map = array(
      $old_node_nid => $new_node_nid,
    );

    // Add the old nid and the new nid to the Drupal Queue
    $queue = DrupalQueue::get($queue_id);
    $queue
      ->createItem($nid_map);
    $context['sandbox']['map'][$nid] = $clone->book['mlid'];

    // capture new root nid for the case that we have a whole new book
    if ($clone->book['plid'] == 0) {
      $context['sandbox']['root'] = $clone;
    }
    $context['sandbox']['progress']++;

    // exit early if we have reached execution limit
    if ($count == $limit) {
      break;
    }
  }

  // max should equal progress but just to be safe, process til out of items
  if (empty($context['sandbox']['list_left'])) {

    // Update our progress information.
    $context['sandbox']['progress'] = $context['sandbox']['max'];
  }

  // Multistep processing : report progress.
  $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}