draggableviews_book.module in DraggableViews 7.2
File
draggableviews_book/draggableviews_book.module
View source
<?php
function draggableviews_book_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'draggableviews_book'),
);
}
function draggableviews_book_ctools_plugin_directory($module, $plugin) {
if ($module == 'draggableviews' && ($plugin == 'handler' || $plugin == 'hierarchy_handler')) {
return 'handlers';
}
}
function draggableviews_book_menu_alter(&$items) {
$items['node/%/book']['access callback'] = '_draggableviews_book_access';
$items['node/%/book']['access arguments'] = array(
1,
);
}
function _draggableviews_book_access($nid) {
return db_query('SELECT has_children FROM {menu_links} WHERE module = :module AND link_path = :link_path', array(
':module' => 'book',
':link_path' => 'node/' . $nid,
))
->fetchField();
}
function draggableviews_book_views_post_execute($view) {
if (!isset($view->result[0]->draggableviews_book_mlid)) {
return;
}
$keyed_result = array();
foreach ($view->result as $result_item) {
$result_item->weight = array();
$keyed_result[$result_item->draggableviews_book_mlid] = $result_item;
}
foreach ($keyed_result as &$item) {
_draggableviews_book_result_set_weight($item, $keyed_result);
}
usort($keyed_result, '_draggableviews_book_uasort');
$view->result = $keyed_result;
}
function _draggableviews_book_result_set_weight(&$item, $result) {
if (!empty($item->weight)) {
return $item->weight;
}
$parent_weight = array();
if (isset($result[$item->draggableviews_book_plid])) {
$parent_weight = _draggableviews_book_result_set_weight($result[$item->draggableviews_book_plid], $result);
}
$item->weight = $parent_weight + array(
$item->draggableviews_book_depth => $item->draggableviews_book_weight,
);
return $item->weight;
}
function _draggableviews_book_uasort($item1, $item2) {
for ($i = 0; $i < 10; $i++) {
if (isset($item1->weight[$i]) && !isset($item2->weight[$i])) {
return 1;
}
if (!isset($item1->weight[$i]) && isset($item2->weight[$i])) {
return -1;
}
if (isset($item1->weight[$i]) && isset($item2->weight[$i])) {
if ($item1->weight[$i] != $item2->weight[$i]) {
return $item1->weight[$i] < $item2->weight[$i] ? -1 : 1;
}
elseif (isset($item1->weight[$i + 1]) || isset($item2->weight[$i + 1])) {
continue;
}
else {
return strcmp($item1->node_title, $item2->node_title);
}
}
}
}