type == 'project_project') { $output = _pivots_block_display_on_project($node_id); } else if ($node->type == 'forum') { $output = _pivots_block_display_on_forum($node_id); } else { // $output = null; } // decide whether to add GA tracking code. global $_pivots_block_track; if (!empty($output)) { $_pivots_block_track = 'pageTracker._trackPageview("/pivotslog/pageview/'.$_pivots_block_pid_double.'/'.$node_id.'");'; } else { $_pivots_block_track = ''; } return $output; } // implementation of hook_footer(). add Google Analytics tracking to pivots_block pageview. function pivots_block_footer($main = 0) { global $_pivots_block_track; if (!empty($_pivots_block_track)) { drupal_add_js($_pivots_block_track, 'inline', 'footer'); } } function _pivots_block_display_on_project($node_id) { global $_pivots_block_pid_conversation, $_pivots_block_pid_double; $output = ''; $output .= _pivots_block_content($node_id, $_pivots_block_pid_conversation, t("Related discussions"), LIMIT_CONVERSATION); $output .= _pivots_block_content($node_id, $_pivots_block_pid_double, t("Related projects"), LIMIT_DOUBLE); return $output; } function _pivots_block_display_on_forum($node_id) { global $_pivots_block_pid_conversation; $output = ''; $output = _pivots_block_content($node_id, $_pivots_block_pid_conversation, t("Projects mentioned in the discussion"), LIMIT_CONVERSATION); return $output; } function _pivots_block_content($node_id, $pivot_id, $title, $limit) { $output = ''; $items = _pivots_block_generate_items($node_id, $pivot_id, $limit); if (!empty($items)) { foreach ($items as $position => $item) { $items[$position] = l($item['title'], "node/{$item['nid']}", array( "onClick" => "javascript: pageTracker._trackPageview('/pivotslog/$pivot_id/$node_id/{$item['nid']}/$position');" )); } $output = theme('item_list', $items, $title); } return $output; } function _pivots_block_generate_items($node_id, $pivot_id, $limit) { if ($limit <= 0) { $limit = LIMIT_MAX; } db_set_active('pivots'); // NOTE: here we activate the pivots database. // if there's database failure, we just pretend nothing happens whatsoever. pivots_block returns nothing in this case. $matches = @db_query("SELECT DISTINCT dest_id FROM {pivots_match} WHERE pivot_id=%d AND src_id=%d AND dest_id<>%d ORDER BY score DESC", $pivot_id, $node_id, $node_id); db_set_active(); // NOTE: change back to use the default database $count = 0; $items = array(); while (($match = @db_fetch_array($matches)) && $count < $limit) { $dest_id = $match['dest_id']; $result = db_query("SELECT title FROM {node} WHERE nid=%d AND status=1", $dest_id); // there might be cases that the node was deleted, or set to unpublished between pivots database refresh // so here we only count the valid node. if (db_num_rows($result) > 0) { $title = db_result($result); $items[] = array('nid' => $dest_id, 'title' => $title, 'pid' => $pivot_id); $count++; } } return $items; } /** * Implementation of hook_block() */ function pivots_block_block($op = 'list', $delta = 0, $edit = array()) { switch ($op) { case 'list': $blocks[0]['info'] = t('pivots_block: Recommendations'); return $blocks; case 'view': $block['subject'] = t('Recommendations'); $block['content'] = pivots_block_output(); return $block; } }