function book_copy_copy_book in Book Copy 7
Same name and namespace in other branches
- 6 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'
- book_copy_menu in ./
book_copy.module - Implements hook_menu().
File
- ./
book_copy.module, line 119
Code
function book_copy_copy_book($book, $subtree = 0) {
$newbid = 0;
// make sure this is part of a book
if (isset($book->book)) {
$bid = $book->book['bid'];
$book = node_load($bid);
$args = array(
'bid' => $bid,
);
if (is_object($subtree)) {
$mlid_res = db_query("SELECT ml.mlid FROM {menu_links} ml LEFT JOIN {book} b ON b.mlid = ml.mlid WHERE b.nid = :subtree", array(
'subtree' => $subtree->nid,
));
$mlid = $mlid_res
->fetchAll();
$where = 'AND (ml.plid = :mlid OR ml.p1 = :mlid OR ml.p2 = :mlid OR ml.p3 = :mlid OR ml.p4 = :mlid OR ml.p5 = :mlid OR ml.p6 = :mlid OR ml.p7 = :mlid OR ml.p8 = :mlid OR ml.p9 = :mlid)';
$args['mlid'] = $mlid[0]->mlid;
}
else {
$where = '';
}
module_load_include('inc', 'clone', 'clone.pages');
$result = db_query("SELECT ml.menu_name, ml.mlid, ml.plid, ml.link_path, ml.router_path, ml.link_title, ml.module, b.nid FROM {menu_links} ml LEFT JOIN {book} b ON b.mlid = ml.mlid WHERE b.bid = :bid " . $where . " ORDER BY ml.depth", $args);
if ($result) {
$mlinks = array();
$nidmap = array();
$mlidmap = array();
$result = $result
->fetchAll();
// loop through results
foreach ($result as $row) {
$plid = $row->plid;
$mlid = $row->mlid;
// clone item and save it into the map
$nidmap[$row->nid] = clone_node_save($row->nid);
// load the new node from the map
$node = node_load($nidmap[$row->nid]);
$mlidmap[$mlid] = $node->book['mlid'];
// make sure this isn't the book root
if ($newbid == 0) {
$newbid = $nidmap[$row->nid];
$message = $row->link_title;
// book_copy alter to allow for modification during operation
drupal_alter("book_copy", $node, $bid, $newbid);
}
if (is_object($subtree) || $node->nid != $newbid) {
$node->book['bid'] = $newbid;
node_save($node);
}
// save the menu link if the map is set
if (isset($mlidmap[$plid])) {
$node->book['plid'] = $mlidmap[$plid];
menu_link_save($node->book);
}
}
foreach ($nidmap as $snid => $nid) {
// TODO use drupal_write_record function
db_query('INSERT INTO {book_copy_history} (nid, bid, sbid, snid, copied) VALUES (:nid, :bid, :sbid, :snid, :copied)', array(
'nid' => $nid,
'bid' => $newbid,
'sbid' => $bid,
'snid' => $snid,
'copied' => time(),
));
}
$book = node_load($newbid);
$book->bookcopydata = array();
$message = $book->title;
$book->bookcopydata['message'] = t('Successfully cloned "%message", now viewing copy.', array(
'%message' => $message,
));
// ensure access to this outline
if (_book_outline_access($book)) {
$book->bookcopydata['url'] = 'node/' . $newbid . '/outline';
}
else {
$book->bookcopydata['url'] = 'node/' . $newbid;
}
// allow for modification of the goto after duplication
drupal_alter("book_copy_goto", $book);
drupal_set_message($book->bookcopydata['message']);
// go to the book copy address or the node itself based on permission
drupal_goto($book->bookcopydata['url']);
}
}
}