-
Notifications
You must be signed in to change notification settings - Fork 0
/
book_tree_op.module
63 lines (60 loc) · 2.03 KB
/
book_tree_op.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
// Hook node update
function book_tree_op_node_update($node) {
$book_types = variable_get('book_allowed_types', array('book'));
if(in_array($node->type,$book_types)) {
$_book_tree_op_child_unpblish = variable_get('book_tree_op_child_unpblish', TRUE);
//All child unpublish operations goes here
if($_book_tree_op_child_unpblish && !$node->status) {
_book_tree_op_child_unpublish($node);
}
}
}
//Unpublish all the nodes
//@todo update the revision table later
function _book_tree_op_child_unpublish(&$node) {
//Get all the childs
$child_tree = _book_tree_op_get_child_nids($node);
if($child_tree && count($child_tree)) {
foreach($child_tree as $_nid) {
db_query("UPDATE {node} SET `status` = 0 WHERE `nid` =:nid ;",array(':nid'=>$_nid));
$vid = db_query("select vid from {node} WHERE `nid` =:nid ;",array(':nid'=>$_nid))->fetchField();
if(intval($vid)) {
db_query("UPDATE {node_revision} SET `status` = 0 WHERE `nid` =:nid AND `vid` =:vid;",array(':nid'=>$_nid,'vid'=> $vid));
}
}
entity_get_controller('node')->resetCache($child_tree);
}
}
/**
* Find which are the children nodes of a book node.
* @param object
* Book link ($node->book)
* @return array
* An array of node id
*/
function _book_tree_op_get_child_nids(&$node) {
$book_link = FALSE;
if($node && isset($node->book) && $node->book) {
$book_link = $node->book;
}
if(!$book_link) {
return FALSE;
}
$flat = book_get_flat_menu($book_link);
$children = array();
if ($book_link['has_children']) {
// Walk through the array until we find the current page.
do {
$link = array_shift($flat);
} while ($link && ($link['mlid'] != $book_link['mlid']));
// Continue though the array and collect the links whose parent is this page.
while (($link = array_shift($flat)) && $link['plid'] == $book_link['mlid']) {
$matches = array();
if (preg_match('/^node\/([\d]+)$/', $link['href'], $matches)) {
$children[] = $matches[1];
}
}
}
return $children;
}