featured_content.module in Featured Content 6
Same filename and directory in other branches
Featured Content module for created related & featured content blocks.
File
featured_content.moduleView source
<?php
/**
* @file
* Featured Content module for created related & featured content blocks.
*/
/**
* Implementation of hook_menu().
*/
function featured_content_menu() {
$items['admin/build/block/add-featured-content-block'] = array(
'title' => 'Add Featured Content Block',
'description' => 'Add a new Featured Content Block.',
'access arguments' => array(
'administer blocks',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'featured_content_add_block_form',
),
'type' => MENU_LOCAL_TASK,
'file' => 'featured_content.admin.inc',
);
$items['admin/build/block/delete-featured-content-block'] = array(
'title' => 'Delete Featured Content block',
'access arguments' => array(
'administer blocks',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'featured_content_delete_block',
),
'type' => MENU_CALLBACK,
'file' => 'featured_content.admin.inc',
);
$items['admin/settings/featured_content'] = array(
'title' => 'Featured Content',
'description' => 'Featured Content Block administration settings',
'access arguments' => array(
'administer site configuration',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'featured_content_settings_form',
),
'file' => 'featured_content.admin.inc',
);
$items['featured-content/feed'] = array(
'title' => 'Featured Content RSS Feed',
'description' => 'Featured Content Block RSS Feed',
'access arguments' => array(
'access content',
),
'page callback' => 'featured_content_feed',
'type' => MENU_CALLBACK,
'file' => 'featured_content.pages.inc',
);
$items['featured-content/more'] = array(
'title' => 'Featured Content Read More',
'description' => 'Featured Content Block Read More Page',
'access arguments' => array(
'access content',
),
'page callback' => 'featured_content_more_page',
'type' => MENU_CALLBACK,
'file' => 'featured_content.pages.inc',
);
return $items;
}
/**
* Implementation hook_help().
*/
function featured_content_help($path, $arg) {
switch ($path) {
case 'admin/build/block/configure':
if ($arg[4] != 'featured_content') {
break;
}
case 'admin/help#featured_content':
case 'admin/build/block':
case 'admin/build/block/add-featured-content-block':
module_load_include('inc', 'featured_content', 'featured_content.pages');
return featured_content_help_text($path, $arg);
}
}
/**
* Implementation hook_theme().
*/
function featured_content_theme() {
return array(
'featured_content_block' => array(
'template' => 'featured_content-block',
'arguments' => array(
'block_settings' => NULL,
),
),
'featured_content_more' => array(
'template' => 'featured_content-more',
'arguments' => array(
'block_settings' => NULL,
),
),
'featured_content_empty' => array(
'template' => 'featured_content-empty',
'arguments' => array(
'block_settings' => NULL,
),
),
);
}
/**
* Process variables for featured_content-block.tpl.php.
*
* @see featured_content-block.tpl.php
*/
function template_preprocess_featured_content_block(&$variables) {
$variables['block_classes_array'][] = 'featured-content-block-' . $variables['block_settings']['delta'];
$variables['block_classes_array'][] = 'featured-content-block-' . $variables['block_settings']['type'];
$variables['block_classes'] = check_plain(implode(' ', $variables['block_classes_array']));
$variables['template_files'][] = 'featured_content-block-' . $variables['id'];
$variables['template_files'][] = 'featured_content-block-' . $variables['block_settings']['type'];
}
/**
* Process variables for featured_content-more.tpl.php.
*
* @see featured_content-more.tpl.php
*/
function template_preprocess_featured_content_more(&$variables) {
$variables['more_classes_array'][] = 'featured-content-more-' . $variables['block_settings']['delta'];
$variables['more_classes_array'][] = 'featured-content-more-' . $variables['block_settings']['type'];
$variables['more_classes'] = check_plain(implode(' ', $variables['more_classes_array']));
$variables['template_files'][] = 'featured_content-more-' . $variables['id'];
$variables['template_files'][] = 'featured_content-more-' . $variables['block_settings']['type'];
}
/**
* Process variables for featured_content-empty.tpl.php.
*
* @see featured_content-empty.tpl.php
*/
function template_preprocess_featured_content_empty(&$variables) {
$variables['block_classes_array'][] = 'featured-content-block-' . $variables['block_settings']['delta'];
$variables['block_classes_array'][] = 'featured-content-block-' . $variables['block_settings']['type'];
$variables['block_classes'] = check_plain(implode(' ', $variables['block_classes_array']));
$variables['template_files'][] = 'featured_content-empty-' . $variables['id'];
$variables['template_files'][] = 'featured_content-empty-' . $variables['block_settings']['type'];
}
/**
* Alters the block admin form to add delete links next to featured_content blocks.
*/
function featured_content_form_block_admin_display_form_alter(&$form, $form_state) {
foreach (variable_get('featured_content_block_ids', array()) as $delta) {
$form['featured_content_' . $delta]['delete'] = array(
'#value' => l(t('delete'), 'admin/build/block/delete-featured-content-block/' . $delta),
);
}
}
/**
* Implementation of hook_block().
*/
function featured_content_block($op = 'list', $delta = NULL, $edit = NULL) {
$function = 'featured_content_' . $op;
if (function_exists($function)) {
return $function($delta, $edit);
}
else {
// less used operation functions are stored in include
module_load_include('inc', 'featured_content', 'featured_content.admin');
if (function_exists($function)) {
return $function($delta, $edit);
}
}
}
/**
* Provides 'view' info for hook_block().
*
* @param $delta string The name of the block to render.
* @param $type string If set, should be 'rss' or 'more'.
*/
function featured_content_view($delta, $type = NULL) {
$featured_content = featured_content_get_block_data($delta);
$vocabularies = taxonomy_get_vocabularies();
// check visibility settings to see if block should be shown
$show = TRUE;
// if there are some visibility settings, default to FALSE
if (!empty($featured_content['visibility']['content_types']['selected']) || !empty($featured_content['visibility']['users']['selected'])) {
$show = FALSE;
}
else {
foreach ($vocabularies as $vocabulary) {
$vid = $vocabulary->vid;
if (!empty($featured_content['visibility']['vocab'][$vid]['selected'])) {
$show = FALSE;
break;
}
}
}
if ($type == NULL && arg(0) == 'node' && is_numeric(arg(1)) && !arg(2)) {
$show = TRUE;
// only check on node pages for now
$this_node = node_load(arg(1));
if ($this_node->nid) {
if (!empty($featured_content['visibility']['content_types']['selected']) && !in_array($this_node->type, $featured_content['visibility']['content_types']['selected'])) {
$show = FALSE;
}
if (!empty($featured_content['visibility']['users']['selected']) && !in_array($this_node->uid, $featured_content['visibility']['users']['selected'])) {
$show = FALSE;
}
foreach ($vocabularies as $vocabulary) {
$vid = $vocabulary->vid;
if (!empty($featured_content['visibility']['vocab'][$vid]['selected'])) {
$tid_intersect = array_intersect($featured_content['visibility']['vocab'][$vid]['selected'], array_keys($this_node->taxonomy));
if (empty($this_node->taxonomy) || empty($tid_intersect)) {
$show = FALSE;
}
}
}
}
}
if (!$show || !$type && arg(0) == 'featured-content' && arg(1) == 'more' && arg(2) == $delta) {
// don't show featured block on more page for that block
return;
}
// get nodes to show
switch ($featured_content['type']) {
case 'manual':
$nids = $featured_content['manual']['nids'];
break;
case 'cck':
$nids = featured_content_get_cck_nids($featured_content['cck']);
break;
case 'filter':
$nids = featured_content_get_filtered_nids($featured_content['filter']);
break;
case 'search':
$nids = featured_content_get_search_nids($featured_content['search']);
break;
}
// if using modr8, only show nodes with moderate = 0
if (module_exists('modr8')) {
if (db_column_exists('node', 'moderate')) {
$results = db_query('SELECT n.nid FROM {node} AS n WHERE n.nid in (' . db_placeholders($nids, 'int') . ') AND n.moderate = 0', $nids);
$tmp = array();
while ($row = db_fetch_object($results)) {
$tmp[$row->nid] = $row->nid;
}
$nids = $tmp;
}
}
$nodes = featured_content_get_nodes($nids, $featured_content['sort']);
// create settings for theming
// all block settings
$settings = $featured_content[$featured_content['type']];
// keep some general settings
$settings['delta'] = $delta;
$settings['type'] = $featured_content['type'];
$settings['header'] = check_markup($featured_content['header']);
$settings['footer'] = check_markup($featured_content['footer']);
$settings['empty'] = check_markup($featured_content['empty']);
$settings['style'] = $featured_content['style'];
// keep track of nid in URL in for context purposes
$query_string = array();
if (isset($this_node) && $this_node->nid) {
$query_string = array(
'query' => 'nid=' . $this_node->nid,
);
}
else {
$query_string = array(
'query' => 'path=' . $_GET['q'],
);
}
// add rss link
if ($featured_content['rss']['display']) {
$settings['rss-link'] = theme('feed_icon', url('featured-content/feed/' . $delta, $query_string), 'RSS Feed');
}
// sort nodes
if (empty($nodes)) {
if (trim($featured_content['empty'])) {
$data = array();
$data['content'] = theme('featured_content_empty', $settings);
return $data;
}
}
else {
switch ($featured_content['sort']) {
case 'random':
if (is_array($nodes)) {
shuffle($nodes);
}
break;
case 'popular_asc':
$nodes = featured_content_sort_nodes($nodes, 'totalcount', TRUE, 'asc');
break;
case 'popular_desc':
$nodes = featured_content_sort_nodes($nodes, 'totalcount');
break;
case 'date_asc':
$nodes = featured_content_sort_nodes($nodes, 'created', TRUE, 'asc');
break;
case 'date_desc':
$nodes = featured_content_sort_nodes($nodes, 'created');
break;
case 'alpha_asc':
$nodes = featured_content_sort_nodes($nodes, 'title', FALSE, 'asc');
break;
case 'alpha_desc':
$nodes = featured_content_sort_nodes($nodes, 'title', FALSE);
break;
case 'terms_asc':
$nodes = featured_content_sort_nodes_by_terms($nodes, 'asc');
break;
case 'terms_desc':
$nodes = featured_content_sort_nodes_by_terms($nodes);
break;
case 'vocab_asc':
$nodes = featured_content_sort_nodes_by_vocab($nodes, 'asc');
break;
case 'vocab_desc':
$nodes = featured_content_sort_nodes_by_vocab($nodes);
break;
case 'none':
if ($nids) {
foreach ($nids as $nid) {
$tmp[] = $nodes[$nid];
}
$nodes = $tmp;
}
break;
}
}
if ($nodes) {
// limit to num_show and create links
$show_num = $featured_content['num_show'];
if ($type == 'more' && !in_array($featured_content['more']['display'], array(
'none',
'custom',
))) {
$show_num = $featured_content['more']['num'];
}
$nodes = array_splice($nodes, 0, $show_num);
$links = array();
$teasers = array();
$full_nodes = array();
$rss_nids = array();
foreach ($nodes as $node) {
if ($node->title && is_numeric($node->nid)) {
$links[] = l($node->title, 'node/' . $node->nid);
if ($type == 'rss') {
$rss_nids[] = $node->nid;
}
elseif ($featured_content['display'] == 'teasers' || $type == 'more' && $featured_content['more']['display'] == 'teasers') {
$teasers[] = node_view(node_load($node->nid), TRUE);
}
elseif ($featured_content['display'] == 'full_nodes' || $type == 'more' && $featured_content['more']['display'] == 'full_nodes') {
$full_nodes[] = node_view(node_load($node->nid));
}
}
}
if ($type == 'rss') {
featured_content_set_page_title($delta, $featured_content['rss']['title']);
return $rss_nids;
}
// add content and more settings
// content to show
$settings['links'] = $links;
$settings['teasers'] = $teasers;
$settings['full_nodes'] = $full_nodes;
if ($type == 'more') {
// return more page
$settings['style'] = $featured_content['more']['style'];
$settings['title'] = $featured_content['more']['title'];
$settings['header'] = $featured_content['more']['header'];
$settings['footer'] = $featured_content['more']['footer'];
featured_content_set_page_title($delta, $featured_content['more']['title']);
return theme('featured_content_more', $settings);
}
elseif ($featured_content['more']['display'] != 'none' && trim($featured_content['more']['text'])) {
// add more link
if ($featured_content['more']['display'] == 'custom' && trim($featured_content['more']['url'])) {
$settings['more-link'] = l($featured_content['more']['text'], $featured_content['more']['url']);
}
else {
$settings['more-link'] = l($featured_content['more']['text'], 'featured-content/more/' . $delta, $query_string);
}
}
// return block content
$data = array();
$data['content'] = theme('featured_content_block', $settings);
return $data;
}
}
/**
* Get CCK node nids.
*/
function featured_content_get_cck_nids($data) {
$nids = array();
$node = _featured_content_load_node();
if ($node->nid) {
$cck_fields = content_fields();
if (!empty($cck_fields)) {
foreach ($cck_fields as $field_name => $field_data) {
if ($data['fields'][$field_name]) {
$cck_field = $node->{$field_name};
if ($field_data['type'] == 'text') {
// parse the text field
$cck_text = trim($cck_field[0]['value']);
if ($cck_text) {
$tmp = array();
if (preg_match('/,/', $cck_text, $matches)) {
$tmp = explode(',', $cck_text);
}
else {
$tmp = explode("\r\n", $cck_text);
}
if (!empty($tmp)) {
foreach ($tmp as $nid) {
$nid = trim($nid);
if (!is_numeric($nid)) {
$path = drupal_lookup_path('source', $nid);
if (!empty($path)) {
$nid = $path;
}
$matches = array();
if (preg_match('/node\\/([0-9]+)/', $nid, $matches)) {
$nid = $matches[1];
}
}
if (is_numeric($nid) && $nid > 0) {
$nids[$nid] = $nid;
}
}
}
}
}
elseif ($field_data['type'] == 'nodereference') {
if (is_array($cck_field)) {
foreach ($cck_field as $node_refs) {
if ($node_refs['nid']) {
$nids[$node_refs['nid']] = $node_refs['nid'];
}
}
}
}
elseif ($field_data['type'] == 'userreference') {
$uids = array();
if (is_array($cck_field)) {
foreach ($cck_field as $user_refs) {
if ($user_refs['uid']) {
$uids[] = $user_refs['uid'];
}
}
}
if (!empty($uids)) {
$results = db_query('SELECT nid FROM {node} WHERE uid in (' . db_placeholders($uids, 'int') . ') AND status <> 0', $uids);
while ($row = db_fetch_object($results)) {
if (is_numeric($row->nid) && $row->nid > 0) {
$nids[$row->nid] = $row->nid;
}
}
}
}
}
}
}
}
return $nids;
}
/**
* Gets the enabled site languages.
*/
function featured_content_get_languages() {
static $languages = array();
if (empty($languages) && module_exists('locale')) {
foreach (locale_language_list() as $code => $language_name) {
$languages[$code] = check_plain($language_name);
}
natcasesort($languages);
$extra_languages = array(
'INTERFACE' => t('Use the language of the user interface.'),
'CURRENT' => t('Use the language of the page being shown.'),
);
$languages = $extra_languages + $languages;
}
return $languages;
}
/**
* Gets the enabled content types.
*/
function featured_content_get_content_types() {
$content_types = array();
foreach (node_get_types() as $content_type) {
$content_types[check_plain($content_type->type)] = check_plain($content_type->name);
}
natcasesort($content_types);
$content_types = array(
'CURRENT' => t('Use the content type of the page being shown.'),
) + $content_types;
return $content_types;
}
/**
* Gets the oldest users limited by the $limit parameter.
*/
function featured_content_get_users($limit = 200) {
$users = array();
$results = db_query('SELECT uid,name,mail FROM {users} WHERE uid <> 0 LIMIT ' . $limit);
while ($user = db_fetch_object($results)) {
$users[$user->uid] = check_plain($user->name) . ' - ' . check_plain($user->mail);
}
natcasesort($users);
$users = array(
'CURRENT' => t('Use the user (author) of the page being shown.'),
) + $users;
return $users;
}
/**
* Gets the enabled vocabulary terms.
*/
function featured_content_get_vocabulary_terms($vocabulary) {
if ($vocabulary->vid) {
$terms = array();
foreach (taxonomy_get_tree($vocabulary->vid) as $term) {
$terms[$term->tid] = check_plain($term->name);
}
$current_key = 'CURRENT-' . $vocabulary->vid;
$terms = array(
$current_key => t('Use the @vocab_name terms of the page being shown.', array(
'@vocab_name' => $vocabulary->name,
)),
) + $terms;
return $terms;
}
}
/**
* Get filtered node nids. Filter base on content types, users (authors) and
* taxonomy terms.
*/
function featured_content_get_filtered_nids($data) {
$node = _featured_content_load_node();
$where_clause = '';
$where_data = array();
// unless configured, exclude current node page
if (!$data['include_node']) {
if ($node && $node->nid) {
$where_clause .= ' AND n.nid != %d ';
$where_data = array_merge($where_data, array(
$node->nid,
));
}
}
// filter based on language type
if (module_exists('locale')) {
$languages = array();
$all_languages = featured_content_get_languages();
if (count($all_languages) > 1) {
foreach ($all_languages as $language_code => $language_label) {
if ($data['languages'][$language_code]) {
if ($language_code == 'CURRENT' && $node->nid) {
$languages[] = $node->language;
}
elseif ($language_code == 'INTERFACE') {
global $language;
$languages[] = $language->language;
}
else {
$languages[] = $language_code;
}
}
}
if (!empty($languages)) {
$where_clause .= ' AND n.language in (' . db_placeholders($languages, 'text') . ') ';
$where_data = array_merge($where_data, $languages);
}
}
}
// filter based on content type
$content_types = array();
$all_content_types = featured_content_get_content_types();
foreach ($all_content_types as $content_type_name => $content_type_label) {
if ($data['content_types']['type'][$content_type_name]) {
if ($content_type_name == 'CURRENT' && $node->nid) {
$content_types[] = $node->type;
}
else {
$content_types[] = $content_type_name;
}
}
}
if (!empty($content_types)) {
$where_clause .= ' AND n.type in (' . db_placeholders($content_types, 'text') . ') ';
$where_data = array_merge($where_data, $content_types);
}
// filter based on user
$uids = array();
$all_users = featured_content_get_users();
foreach ($all_users as $user_uid => $user_label) {
if ($data['users']['user'][$user_uid]) {
if ($user_uid == 'CURRENT' && $node->uid) {
$uids[] = $node->uid;
}
else {
$uids[] = $user_uid;
}
}
}
if (!empty($uids)) {
$where_clause .= ' AND n.uid in (' . db_placeholders($uids, 'int') . ') ';
$where_data = array_merge($where_data, $uids);
}
// filter based on taxonomy terms
$tids = array();
$vocabularies = taxonomy_get_vocabularies();
foreach ($vocabularies as $vocabulary) {
$vid = $vocabulary->vid;
$all_terms = featured_content_get_vocabulary_terms($vocabulary);
foreach ($all_terms as $term_tid => $term_name) {
if (isset($data['vocab'][$vid]['term'][$term_tid]) && $data['vocab'][$vid]['term'][$term_tid] == 1) {
if ($term_tid == 'CURRENT-' . $vid) {
if (isset($node->nid)) {
$node_terms = taxonomy_node_get_terms_by_vocabulary($node, $vid);
if (!empty($node_terms)) {
foreach ($node_terms as $node_term) {
$tids[$node_term->tid] = $node_term->tid;
}
}
}
elseif (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
// check if taxonomy page
$tids[arg(2)] = arg(2);
}
}
else {
$tids[$term_tid] = $term_tid;
}
}
}
}
$tid_inner_join = '';
if (!empty($tids)) {
$tid_inner_join = ' INNER JOIN {term_node} AS tn ON n.nid=tn.nid ';
$where_clause .= ' AND tn.tid in (' . db_placeholders($tids, 'int') . ') ';
$where_data = array_merge($where_data, $tids);
}
// filter by views
if (module_exists('views')) {
if (function_exists('views_get_all_views') && function_exists('views_get_view_result')) {
$views_nids = array();
$views = views_get_all_views();
if (!empty($views)) {
foreach ($views as $view_name => $view_data) {
if (!empty($view_data->display)) {
foreach ($view_data->display as $display_name => $display_data) {
if ($data['views']['view'][$view_name][$display_name]) {
// If the block is configured to include node ID or term ID as an argument,
// include that information here. Otherwise, pass in no arguments.
$views_results = !empty($data['view_include_arg']) && isset($node->nid) ? views_get_view_result($view_name, $display_name, $node->nid) : views_get_view_result($view_name, $display_name);
if (!empty($views_results)) {
foreach ($views_results as $views_result) {
// Look to see if there is a nid returned other than the
// main nid (to allow for more complex view results). If
// so, use that value. Otherwise, use the main returned
// nid if it exists
$found_nid = FALSE;
foreach ($views_result as $key => $val) {
if (!$found_nid) {
if (substr($key, -4) === '_nid') {
$views_nids[] = $val;
$found_nid = TRUE;
}
}
}
if (!$found_nid && $views_result->nid) {
$views_nids[] = $views_result->nid;
}
}
}
}
}
}
}
}
if (!empty($views_nids)) {
$where_clause .= ' AND n.nid in (' . db_placeholders($views_nids, 'int') . ') ';
$where_data = array_merge($where_data, $views_nids);
}
}
}
// filter by node title keyword search
if (module_exists('search')) {
// check to make sure this option has been enabled
if ($data['keyword']['filter_by_keyword'] === 1) {
// If we haven't already loaded the node from earlier on, grab it now.
// If we're not able to, don't do any keyword searching.
$node = empty($node) ? _featured_content_load_node() : $node;
if ($node) {
$num_words = (int) $data['keyword']['num_words_in_title_keyword'];
$keyword_search_string = featured_content_get_search_string($node, $num_words);
// use drupal search for actual search
$search_results = do_search($keyword_search_string, 'node');
$keyword_match_nids = array();
foreach ($search_results as $match) {
$keyword_match_nids[] = $match->sid;
}
if (!empty($keyword_match_nids)) {
$where_clause .= ' AND n.nid IN (' . db_placeholders($keyword_match_nids, 'int') . ') ';
}
else {
// no matches
$where_clause .= ' AND n.nid IN (0) ';
}
$where_data = array_merge($where_data, $keyword_match_nids);
}
}
}
// filter by primary term
$pt_inner_join = '';
if ($data['primary_term']['node'] && is_numeric($node->primaryterm) && $node->primaryterm > 0) {
$pt_inner_join = ' INNER JOIN {primary_term} AS pt ON n.vid=pt.vid ';
$where_clause .= ' AND pt.tid in (' . db_placeholders(array(
$node->primaryterm,
), 'int') . ') ';
$where_data = array_merge($where_data, array(
$node->primaryterm,
));
}
// filter based on path
$match = $data['paths']['match'];
if ($match[0] || $match[1] || $match[2] || $match[3]) {
if ($_GET['nid']) {
$path_alias = drupal_get_path_alias('node/' . $_GET['nid']);
}
elseif ($_GET['path']) {
$path_alias = drupal_get_path_alias($_GET['path']);
}
else {
$path_alias = drupal_get_path_alias($_GET['q']);
}
if ($path_alias) {
$tmp = array();
$path = explode('/', $path_alias);
if (!empty($path)) {
$filter_path = featured_content_get_path_pattern($match, $path, 0);
$filter_path .= featured_content_get_path_pattern($match, $path, 1);
$filter_path .= featured_content_get_path_pattern($match, $path, 2);
$filter_path .= featured_content_get_path_pattern($match, $path, 3);
}
}
}
$results = db_query('SELECT n.nid FROM {node} AS n ' . $tid_inner_join . ' ' . $pt_inner_join . ' WHERE n.status <> 0 ' . $where_clause, $where_data);
while ($row = db_fetch_object($results)) {
$add = TRUE;
if (isset($filter_path)) {
$node_alias = drupal_get_path_alias('node/' . $row->nid) . '/';
if (!preg_match('/^' . $filter_path . '/', $node_alias, $matches)) {
$add = FALSE;
}
}
if ($add) {
$nids[] = $row->nid;
}
}
return $nids;
}
/**
* Get search string based on give node title.
*/
function featured_content_get_search_string($node, $num_words) {
$keyword_search_string = '';
if (!empty($node) && !empty($node->title)) {
// If title trimming is enabled, pull out the first x significantly
// long words in the title.
if ($num_words > 0) {
$search_terms = array();
$found_terms = 0;
$min_length_for_search_term = variable_get('minimum_word_size', 3);
foreach (explode(' ', $node->title) as $word_position => $word) {
if (count($search_terms) < $num_words) {
$processed_word = search_simplify($word);
if (mb_strlen($processed_word) > $min_length_for_search_term) {
$search_terms[] = $word;
++$found_terms;
}
}
}
$keyword_search_string = implode(' ', $search_terms);
}
else {
$keyword_search_string = $node->title;
}
}
return $keyword_search_string;
}
/**
* Get search result node nids. Uses the title of the current node page to
* get the search results.
*/
function featured_content_get_search_nids($data) {
// only works if on a node page
$nids = array();
$node = _featured_content_load_node();
if (!empty($node)) {
// get nids associated with node title using search
$num_words = (int) $data['num_words_in_title'];
$keyword_search_string = featured_content_get_search_string($node, $num_words);
$results = do_search($keyword_search_string, 'node');
foreach ($results as $result) {
$nids[$result->sid] = $result->sid;
}
// unless configured, exclude current node page
if (!$data['include_node']) {
unset($nids[arg(1)]);
}
}
return $nids;
}
/**
* Get the pattern to use for the path.
*/
function featured_content_get_path_pattern($match, $path, $index) {
$max = 3;
for ($i = $index; $i <= $max; $i++) {
if ($match[$i] && $path[$i]) {
break;
// okay to return something
}
elseif ($i == $max) {
return '';
// nothing more to match on
}
}
if ($match[$index]) {
$pattern = $path[$index];
}
else {
$pattern = '.*?';
}
return '(' . $pattern . ')\\/';
}
/**
* Sort nodes based on terms.
*/
function featured_content_sort_nodes_by_terms($nodes, $order = 'desc') {
// only works if on a node page
if (arg(0) == 'node' && is_numeric(arg(1))) {
$tmp = array();
$nids = array_keys($nodes);
$results = db_query('
SELECT count(tn1.tid) AS count, tn1.nid
FROM {term_node} AS tn1
WHERE tn1.nid IN (' . db_placeholders($nids, 'int') . ') AND
tn1.tid IN (
SELECT tn2.tid
FROM {term_node} AS tn2
WHERE tn2.nid=%d
)
GROUP BY tn1.nid
ORDER BY count ' . $order, array_merge($nids, array(
arg(1),
)));
while ($row = db_fetch_object($results)) {
$tmp[$row->nid] = $nodes[$row->nid];
}
foreach ($nodes as $nid => $node) {
if (!in_array($nid, $tmp)) {
// tack on the rest of the nodes
$tmp[$nid] = $node;
}
}
$nodes = $tmp;
}
return $nodes;
}
/**
* Sort nodes based on vocabularies. The order is based on the weight
* of the vocabularies as well as the term of terms the node has
* within that vocabulary.
*/
function featured_content_sort_nodes_by_vocab($nodes, $order = 'desc') {
// only works if on a node page
$node = _featured_content_load_node();
if (!empty($node)) {
$nids = array_keys($nodes);
$num_terms = array();
$weights = array();
$results = db_query('
SELECT tn.nid, tn.tid, v.vid, v.weight, COUNT(*) AS count
FROM {term_node} AS tn
INNER JOIN {node} AS n on tn.vid = n.vid
INNER JOIN {term_data} AS td on tn.tid = td.tid
INNER JOIN {vocabulary} AS v on td.vid = v.vid
WHERE tn.nid IN (' . db_placeholders($nids, 'int') . ') AND
tn.tid IN (
SELECT tn2.tid
FROM {term_node} AS tn2
INNER JOIN {node} AS n2 ON tn2.vid = n2.vid
WHERE n2.nid = %d)
GROUP BY tn.nid, v.weight
ORDER BY v.weight ' . $order, array_merge($nids, array(
$node->nid,
)));
while ($row = db_fetch_object($results)) {
$num_terms[$row->vid][$row->nid] = $row->count;
$weights[$row->vid] = $row->weight;
}
asort($weights);
$totals = array();
foreach ($nids as $nid) {
$nums = array();
foreach ($weights as $vid => $weight) {
$nums[] = isset($num_terms[$vid][$nid]) ? $num_terms[$vid][$nid] : 0;
}
$totals[$nid] = implode('-', $nums);
}
natsort($totals);
$totals = array_reverse($totals, TRUE);
foreach ($totals as $nid => $total) {
$tmp[$nid] = $nodes[$nid];
}
foreach ($nodes as $nid => $node) {
if (!in_array($nid, array_keys($tmp))) {
// tack on the rest of the nodes
$tmp[$nid] = $node;
}
}
$nodes = $tmp;
}
return $nodes;
}
/**
* Sort nodes based on sort key.
*/
function featured_content_sort_nodes($nodes, $sort_key, $numeric = TRUE, $order = 'desc') {
if ($nodes) {
foreach ($nodes as $node) {
$key = $node->{$sort_key} . '' . $node->nid;
$tmp[$key] = $node;
}
$tmp_keys = array_keys($tmp);
$sort_by = $numeric ? SORT_NUMERIC : SORT_REGULAR;
if ($order == 'desc') {
rsort($tmp_keys, $sort_by);
}
else {
sort($tmp_keys, $sort_by);
}
foreach ($tmp_keys as $tmp_key) {
$sorted_nodes[] = $tmp[$tmp_key];
}
return $sorted_nodes;
}
}
/**
* Check if node statistics is enabled.
*/
function featured_content_node_statistics_enabled() {
return module_exists('statistics') && variable_get('statistics_count_content_views', 0);
}
/**
* Gets nodes from nids.
*/
function featured_content_get_nodes($nids, $sort) {
if (!empty($nids)) {
$nodes = array();
// use node_counter if sorting by popularity and counts are available
$totalcount_select = $totalcount_inner_join = '';
if (($sort == 'popular_desc' || $sort == 'popular_asc') && featured_content_node_statistics_enabled()) {
$totalcount_select = ', nc.totalcount ';
$totalcount_inner_join = ' INNER JOIN {node_counter} AS nc ON n.nid=nc.nid ';
}
$results = db_query(db_rewrite_sql('SELECT n.nid, n.title, n.created, n.type ' . $totalcount_select . ' FROM {node} AS n ' . $totalcount_inner_join . ' WHERE n.nid IN (' . db_placeholders($nids, 'int') . ') AND n.status <> 0'), $nids);
while ($node = db_fetch_object($results)) {
if ($totalcount_select == '') {
$node->totalcount = 1;
// treat all nodes equally for sort purposes
}
$nodes[$node->nid] = $node;
}
return $nodes;
}
}
/**
* Get the block data for the given $delta.
*
* @param $delta
* number The number of the block.
*/
function featured_content_get_block_data($delta) {
$featured_blocks = variable_get('featured_content_blocks', array());
return featured_content_get_value($featured_blocks[$delta], array());
}
/**
* Get the value if set, otherwise get default.
*/
function featured_content_get_value($value, $default) {
return isset($value) ? $value : $default;
}
/**
* Set page title for more page or rss page.
*/
function featured_content_set_page_title($delta, $title = NULL) {
if (!trim($title)) {
$title = featured_content_get_block_title($delta);
}
drupal_set_title(check_plain($title));
}
/**
* Get block title based on given title or internal title.
*/
function featured_content_get_block_title($delta, $add_site_name = FALSE) {
$results = db_query("SELECT title FROM {blocks} WHERE module='featured_content' AND delta=%d", $delta);
if (is_numeric($delta) && $results) {
$row = db_fetch_object($results);
$block_title = trim($row->title);
if ($block_title == '' || $block_title == '<none>') {
$info = module_invoke('featured_content', 'block', 'list');
if (isset($info[$delta])) {
$block_title = $info[$delta]['info'];
}
}
if ($add_site_name && trim($block_title)) {
$site_name = check_plain(variable_get('site_name', ''));
if ($site_name) {
$block_title = $site_name . ': ' . $block_title;
}
}
}
return check_plain($block_title);
}
/**
* Try and build the node from either the system url or the query string.
* Return FALSE if unable to build the node.
*/
function _featured_content_load_node() {
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
}
elseif (isset($_GET['nid']) && is_numeric($_GET['nid'])) {
// for RSS feed
$node = node_load($_GET['nid']);
}
return empty($node) ? FALSE : $node;
}
Functions
Name![]() |
Description |
---|---|
featured_content_block | Implementation of hook_block(). |
featured_content_form_block_admin_display_form_alter | Alters the block admin form to add delete links next to featured_content blocks. |
featured_content_get_block_data | Get the block data for the given $delta. |
featured_content_get_block_title | Get block title based on given title or internal title. |
featured_content_get_cck_nids | Get CCK node nids. |
featured_content_get_content_types | Gets the enabled content types. |
featured_content_get_filtered_nids | Get filtered node nids. Filter base on content types, users (authors) and taxonomy terms. |
featured_content_get_languages | Gets the enabled site languages. |
featured_content_get_nodes | Gets nodes from nids. |
featured_content_get_path_pattern | Get the pattern to use for the path. |
featured_content_get_search_nids | Get search result node nids. Uses the title of the current node page to get the search results. |
featured_content_get_search_string | Get search string based on give node title. |
featured_content_get_users | Gets the oldest users limited by the $limit parameter. |
featured_content_get_value | Get the value if set, otherwise get default. |
featured_content_get_vocabulary_terms | Gets the enabled vocabulary terms. |
featured_content_help | Implementation hook_help(). |
featured_content_menu | Implementation of hook_menu(). |
featured_content_node_statistics_enabled | Check if node statistics is enabled. |
featured_content_set_page_title | Set page title for more page or rss page. |
featured_content_sort_nodes | Sort nodes based on sort key. |
featured_content_sort_nodes_by_terms | Sort nodes based on terms. |
featured_content_sort_nodes_by_vocab | Sort nodes based on vocabularies. The order is based on the weight of the vocabularies as well as the term of terms the node has within that vocabulary. |
featured_content_theme | Implementation hook_theme(). |
featured_content_view | Provides 'view' info for hook_block(). |
template_preprocess_featured_content_block | Process variables for featured_content-block.tpl.php. |
template_preprocess_featured_content_empty | Process variables for featured_content-empty.tpl.php. |
template_preprocess_featured_content_more | Process variables for featured_content-more.tpl.php. |
_featured_content_load_node | Try and build the node from either the system url or the query string. Return FALSE if unable to build the node. |