function book_copy_copy_book in Book Copy 6
Same name and namespace in other branches
- 7 book_copy.module \book_copy_copy_book()
This function is intended to be called via drupals batch processing system and will clone an entire or partial book.
Parameters
object $book The book from which we are cloning content:
int $subtree The nid to start cloning from if 0 all book pages will be copied: if non-zero then only those nodes under the given nid in the tree hierarchy will be copied
array $context used to maintain state via batch code:
1 string reference to 'book_copy_copy_book'
File
- ./
book_copy.module, line 108
Code
function book_copy_copy_book($book, $subtree = 0, &$context) {
if (isset($book->book)) {
$bid = $book->book['bid'];
$book = node_load(array(
'nid' => $bid,
));
module_load_include('inc', 'clone', 'clone.pages');
if (!isset($context['sandbox']['progress'])) {
$nodes = book_copy_find_subtree($bid, $subtree);
$context['sandbox']['newbid'] = 0;
$context['sandbox']['max'] = count($nodes);
$context['sandbox']['nodes'] = $nodes;
$context['sandbox']['progress'] = 0;
$context['sandbox']['finished'] = 0;
$context['sandbox']['mlinks'] = array();
$context['sandbox']['nidmap'] = array();
$context['sandbox']['mlidmap'] = array();
}
if (!empty($context['sandbox']['nodes'])) {
while ($context['sandbox']['progress'] < $context['sandbox']['max']) {
$nid = $nodes[$context['sandbox']['progress']];
$sql = 'SELECT ml.* FROM {menu_links} ml LEFT JOIN {book} b ON b.mlid = ml.mlid WHERE b.bid = %d AND b.nid = %d';
$result = db_query($sql, $bid, $nid);
$row = db_fetch_array($result);
$plid = $row['plid'];
$mlid = $row['mlid'];
$context['sandbox']['progress']++;
// copy each node keeping reference to new nid via old nid (this copies the old book structure)
$context['sandbox']['nidmap'][$nid] = clone_node_save($nid);
// clone_node_save() returns the nid of the new node.
$node = node_load(array(
'nid' => $context['sandbox']['nidmap'][$nid],
));
$context['sandbox']['mlidmap'][$mlid] = $node->book['mlid'];
if ($context['sandbox']['newbid'] == 0) {
$context['sandbox']['newbid'] = $context['sandbox']['nidmap'][$nid];
$message = $row['link_title'];
// The function signature is: hook_book_copy_alter(&$node, $oldbid, $newbid);
drupal_alter("book_copy", $node, $bid, $context['sandbox']['newbid']);
// fix batch redirect, we didn't have the nid when batch_set() was called
$batch =& batch_get();
$data = book_copy_copy_redirection($context['sandbox']['newbid']);
$batch['redirect'] = $data['url'];
}
if ($subtree != 0 || $node->nid != $context['sandbox']['newbid']) {
// we need to set the bid for this node;
$node->book['bid'] = $context['sandbox']['newbid'];
node_save($node);
}
// fix lower level nested links
if (isset($context['sandbox']['mlidmap'][$plid])) {
$node->book['plid'] = $context['sandbox']['mlidmap'][$plid];
menu_link_save($node->book);
}
}
}
}
if ($context['sandbox']['progress'] < $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
else {
if ($context['sandbox']['progress'] != 0) {
$context['finished'] = 1;
}
}
if ($context['finished'] == 1) {
foreach ($context['sandbox']['nidmap'] as $snid => $nid) {
db_query('INSERT INTO {book_copy_history} (nid, bid, sbid, snid, copied) VALUES (%d, %d, %d, %d, %d)', $nid, $context['sandbox']['newbid'], $bid, $snid, time());
}
}
}