service_links.module in Service links 5
Same filename and directory in other branches
Mantainer: Fabio Mucciante aka TheCrow Original Author: Fredrik Jonsson fredrik at combonet dot se A module that adds Digg, del.icio.us, reddit, Technorati etc. links to nodes.
File
service_links.moduleView source
<?php
/**
* @file
* Mantainer: Fabio Mucciante aka TheCrow
* Original Author: Fredrik Jonsson fredrik at combonet dot se
* A module that adds Digg, del.icio.us, reddit, Technorati etc. links to nodes.
*/
/**
* Implementation of hook_help().
*/
function service_links_help($section) {
switch ($section) {
case 'admin/settings/service_links':
return '<p>' . t('Here you can configure the service links.') . '</p>';
break;
}
}
/**
* Implementation of hook_perm().
*/
function service_links_perm() {
return array(
'use service links',
);
}
/**
* Menu callback; administration settings for Service Links.
*/
function service_links_admin_settings() {
$form['where_to_show_the_links'] = array(
'#type' => 'fieldset',
'#title' => t('Where to show the service links'),
'#description' => t('Set the node types and categories you want to display links for.'),
);
$form['where_to_show_the_links']['service_links_node_types'] = array(
'#type' => 'checkboxes',
'#title' => t('Node types'),
'#default_value' => variable_get('service_links_node_types', array()),
'#options' => array_map('check_plain', node_get_types('names')),
);
if (module_exists('taxonomy')) {
$form['where_to_show_the_links']['service_links_category_types'] = array(
'#type' => 'select',
'#multiple' => TRUE,
'#title' => t('Categories'),
'#default_value' => variable_get('service_links_category_types', array()),
'#options' => _service_links_get_terms(),
);
}
$form['what_links_to_show'] = array(
'#type' => 'fieldset',
'#title' => t('What bookmark links to show'),
);
$form['what_links_to_show']['service_links_show_delicious'] = array(
'#type' => 'checkbox',
'#title' => t('Show del.icio.us link'),
'#return_value' => 1,
'#default_value' => variable_get('service_links_show_delicious', 1),
);
$form['what_links_to_show']['service_links_show_digg'] = array(
'#type' => 'checkbox',
'#title' => t('Show Digg link'),
'#return_value' => 1,
'#default_value' => variable_get('service_links_show_digg', 1),
);
$form['what_links_to_show']['service_links_show_stumbleupon'] = array(
'#type' => 'checkbox',
'#title' => t('Show StumbleUpon link'),
'#return_value' => 1,
'#default_value' => variable_get('service_links_show_stumbleupon', 0),
);
$form['what_links_to_show']['service_links_show_propeller'] = array(
'#type' => 'checkbox',
'#title' => t('Show Propeller link'),
'#return_value' => 1,
'#default_value' => variable_get('service_links_show_propeller', 0),
);
$form['what_links_to_show']['service_links_show_reddit'] = array(
'#type' => 'checkbox',
'#title' => t('Show Reddit link'),
'#return_value' => 1,
'#default_value' => variable_get('service_links_show_reddit', 0),
);
$form['what_links_to_show']['service_links_show_magnoliacom'] = array(
'#type' => 'checkbox',
'#title' => t('Show ma.gnolia.com link'),
'#return_value' => 1,
'#default_value' => variable_get('service_links_show_magnoliacom', 0),
);
$form['what_links_to_show']['service_links_show_newsvine'] = array(
'#type' => 'checkbox',
'#title' => t('Show Newsvine link'),
'#return_value' => 1,
'#default_value' => variable_get('service_links_show_newsvine', 0),
);
$form['what_links_to_show']['service_links_show_furl'] = array(
'#type' => 'checkbox',
'#title' => t('Show Furl link'),
'#return_value' => 1,
'#default_value' => variable_get('service_links_show_furl', 0),
);
$form['what_links_to_show']['service_links_show_facebook'] = array(
'#type' => 'checkbox',
'#title' => t('Show Facebook link'),
'#return_value' => 1,
'#default_value' => variable_get('service_links_show_facebook', 0),
);
$form['what_links_to_show']['service_links_show_myspace'] = array(
'#type' => 'checkbox',
'#title' => t('Show MySpace link'),
'#return_value' => 1,
'#default_value' => variable_get('service_links_show_myspace', 0),
);
$form['what_links_to_show']['service_links_show_twitter'] = array(
'#type' => 'checkbox',
//'#title' => t('Show Twitter link without TinyURL support'),
'#title' => t('Show Twitter link'),
'#return_value' => 1,
'#default_value' => variable_get('service_links_show_twitter', 0),
);
/* $form['what_links_to_show']['service_links_show_twitter_with_tinyurl'] = array(
'#type' => 'checkbox',
'#title' => t('Show Twitter link with TinyURL support'),
'#return_value' => 1,
'#default_value' => variable_get('service_links_show_twitter_with_tinyurl', 0),
);*/
$form['what_links_to_show']['service_links_show_google'] = array(
'#type' => 'checkbox',
'#title' => t('Show Google link'),
'#return_value' => 1,
'#default_value' => variable_get('service_links_show_google', 0),
);
$form['what_links_to_show']['service_links_show_yahoo'] = array(
'#type' => 'checkbox',
'#title' => t('Show Yahoo link'),
'#return_value' => 1,
'#default_value' => variable_get('service_links_show_yahoo', 0),
);
$form['what_links_to_show']['service_links_show_buzz'] = array(
'#type' => 'checkbox',
'#title' => t('Show Buzz link'),
'#return_value' => 1,
'#default_value' => variable_get('service_links_show_buzz', 0),
);
$form['what_links_to_show']['service_links_show_linkedin'] = array(
'#type' => 'checkbox',
'#title' => t('Show LinkedIn link'),
'#return_value' => 1,
'#default_value' => variable_get('service_links_show_linkedin', 0),
);
$form['what_links2_to_show'] = array(
'#type' => 'fieldset',
'#title' => t('What search links to show'),
);
$form['what_links2_to_show']['service_links_show_technorati'] = array(
'#type' => 'checkbox',
'#title' => t('Show Technorati link'),
'#return_value' => 1,
'#default_value' => variable_get('service_links_show_technorati', 1),
);
$form['what_links2_to_show']['service_links_show_icerocket'] = array(
'#type' => 'checkbox',
'#title' => t('Show IceRocket link'),
'#return_value' => 1,
'#default_value' => variable_get('service_links_show_icerocket', 0),
);
$form['how_to_show_the_links'] = array(
'#type' => 'fieldset',
'#title' => t('When and how to show the links'),
);
$form['how_to_show_the_links']['service_links_in_links'] = array(
'#type' => 'select',
'#title' => t('Service links in links'),
'#default_value' => variable_get('service_links_in_links', 0),
'#options' => array(
0 => t('Disabled'),
1 => t('Teaser view'),
2 => t('Full-page view'),
3 => t('Teasers and full-page view'),
),
'#description' => t('When to display the services in the links section.'),
);
$form['how_to_show_the_links']['service_links_in_node'] = array(
'#type' => 'select',
'#title' => t('Service links in nodes'),
'#default_value' => variable_get('service_links_in_node', 2),
'#options' => array(
0 => t('Disabled'),
1 => t('Teaser view'),
2 => t('Full-page view'),
3 => t('Teasers and full-page view'),
),
'#description' => t('When to display the services after the node text.'),
);
$form['how_to_show_the_links']['service_links_style'] = array(
'#type' => 'select',
'#title' => t('Service links style'),
'#default_value' => variable_get('service_links_style', 1),
'#options' => array(
1 => t('Text links'),
2 => t('Image links'),
3 => t('Image and text links'),
),
);
if (module_exists('aggregator2')) {
$form['aggregator2_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Aggregator2 settings'),
);
$form['aggregator2_settings']['service_links_agg2_link'] = array(
'#type' => 'checkbox',
'#title' => t('Use link to original article aggregated by aggregator2 module'),
'#return_value' => 1,
'#default_value' => variable_get('service_links_agg2_link', 0),
);
}
return system_settings_form($form);
}
/**
* Implementation of hook_menu().
*/
function service_links_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/settings/servicelinks',
'title' => t('Service links'),
'description' => t('Control which and where service links should be active.'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'service_links_admin_settings',
),
'access' => user_access('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
}
return $items;
}
/**
* Implementation of hook_nodeapi().
*/
function service_links_nodeapi(&$node, $op, $teaser, $page) {
switch ($op) {
case 'view':
$links_show = _service_links_show($node->type, $node->nid);
if ($links_show && user_access('use service links')) {
switch (variable_get('service_links_in_node', 0)) {
case 1:
if ($teaser) {
$node->content['service_links'] = array(
'#value' => theme('service_links_node_format', service_links_render($node, TRUE)),
'#weight' => 10,
);
}
break;
case 2:
if ($page) {
$node->content['service_links'] = array(
'#value' => theme('service_links_node_format', service_links_render($node, TRUE)),
'#weight' => 10,
);
}
break;
case 3:
if ($teaser) {
$node->content['service_links'] = array(
'#value' => theme('service_links_node_format', service_links_render($node, TRUE)),
'#weight' => 10,
);
}
else {
if ($page) {
$node->content['service_links'] = array(
'#value' => theme('service_links_node_format', service_links_render($node, TRUE)),
'#weight' => 10,
);
}
}
break;
}
}
break;
}
}
/**
* Implementation of hook_link().
*/
function service_links_link($type, $node = NULL, $teaser = FALSE) {
$links = array();
if ($type == 'node') {
switch (variable_get('service_links_in_links', 0)) {
case 0:
$show_links = FALSE;
break;
case 1:
$show_links = $teaser ? TRUE : FALSE;
break;
case 2:
$show_links = $teaser ? FALSE : TRUE;
break;
case 3:
$show_links = TRUE;
break;
default:
$show_links = FALSE;
}
if (_service_links_show($node->type, $node->nid) && $show_links && user_access('use service links')) {
$links = service_links_render($node, TRUE);
}
}
return $links;
}
/**
* Implementation of hook_block().
*/
function service_links_block($op = 'list', $delta = 0, $edit = array()) {
if ($op == 'list') {
$blocks[0]['info'] = t('Service links');
return $blocks;
}
else {
if ($op == 'view') {
if (user_access('use service links') && arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
$links_show = _service_links_show($node->type, $node->nid);
if ($links_show) {
$block['subject'] = t('Bookmark/Search this post');
$block['content'] = theme('service_links_block_format', service_links_render($node));
}
return $block;
}
}
}
}
/**
* Function that renders the service links.
* This is the function themers want to call to insert the service links.
*/
function service_links_render($node, $nodelink = FALSE) {
$links = array();
if (variable_get('service_links_agg2_link', 0) && $node->link) {
$url = $node->source_link ? $node->source_link : $node->link;
$title = isset($node->title) ? $node->title : drupal_get_title();
}
elseif (!empty($node)) {
$url = url("node/{$node->nid}", NULL, NULL, TRUE);
$title = isset($node->title) ? $node->title : '';
}
else {
$url = url($_GET['q'], NULL, NULL, TRUE);
$title = drupal_get_title();
}
$url = urlencode($url);
$title = isset($title) ? rawurlencode($title) : '';
$summary = urlencode(strip_tags($node->teaser));
if (variable_get('service_links_show_delicious', 0)) {
$links['service_links_delicious'] = theme('service_links_build_link', t('Delicious'), "http://del.icio.us/post?url={$url}&title={$title}", t('Bookmark this post on del.icio.us.'), 'delicious.png', $nodelink);
}
if (variable_get('service_links_show_digg', 0)) {
$links['service_links_digg'] = theme('service_links_build_link', t('Digg'), "http://digg.com/submit?phase=2&url={$url}&title={$title}", t('Digg this post on digg.com.'), 'digg.png', $nodelink);
}
if (variable_get('service_links_show_stumbleupon', 0)) {
$links['service_links_stumbleupon'] = theme('service_links_build_link', t('StumbleUpon'), "http://www.stumbleupon.com/submit?url={$url}&title={$title}", t('Thumb this up at StumbleUpon.'), 'stumbleit.png', $nodelink);
}
if (variable_get('service_links_show_propeller', 0)) {
$links['service_links_propeller'] = theme('service_links_build_link', t('Propeller'), "http://www.propeller.com/submit/?U={$url}&T={$title}", t('Submit to Propeller.'), 'propeller.png', $nodelink);
}
if (variable_get('service_links_show_reddit', 0)) {
$links['service_links_reddit'] = theme('service_links_build_link', t('Reddit'), "http://reddit.com/submit?url={$url}&title={$title}", t('Submit this post on reddit.com.'), 'reddit.png', $nodelink);
}
if (variable_get('service_links_show_magnoliacom', 0)) {
$links['service_links_magnoliacom'] = theme('service_links_build_link', t('Magnoliacom'), "http://ma.gnolia.com/bookmarklet/add?url={$url}&title={$title}", t('Submit this post on ma.gnolia.com.'), 'magnoliacom.png', $nodelink);
}
if (variable_get('service_links_show_newsvine', 0)) {
$links['service_links_newsvine'] = theme('service_links_build_link', t('Newsvine'), "http://www.newsvine.com/_tools/seed&save?u={$url}&h={$title}", t('Submit this post on newsvine.com.'), 'newsvine.png', $nodelink);
}
if (variable_get('service_links_show_furl', 0)) {
$links['service_links_furl'] = theme('service_links_build_link', t('Furl'), "http://www.furl.net/storeIt.jsp?u={$url}&t={$title}", t('Submit this post on furl.net.'), 'furl.png', $nodelink);
}
if (variable_get('service_links_show_facebook', 0)) {
$links['service_links_facebook'] = theme('service_links_build_link', t('Facebook'), "http://www.facebook.com/sharer.php?u={$url}&t={$title}", t('Share on Facebook.'), 'facebook.png', $nodelink);
}
if (variable_get('service_links_show_myspace', 0)) {
$links['service_links_myspace'] = theme('service_links_build_link', t('MySpace'), "http://www.myspace.com/index.cfm?fuseaction=postto&t={$title}&u={$url}", t('Share on MySpace.'), 'myspace.png', $nodelink);
}
if (variable_get('service_links_show_twitter', 0)) {
$surl = isset($node->nid) ? url('node/' . $node->nid, NULL, NULL, TRUE) : $url;
$links['service_links_twitter'] = theme('service_links_build_link', t('Twitter'), "http://twitter.com/?status={$surl}" . "%20--%20" . $title, t('Share on Twitter.'), 'twitter.png', $nodelink);
}
/*if (variable_get('service_links_show_twitter_with_tinyurl', 0)) {
$turl = drupal_http_request('http://tinyurl.com/api-create.php?url='. $url);
$turl = (isset($turl->data) && ($turl->code == 200)) ? $turl->data : $url;
$links['service_links_twitter_with_tinyurl'] = theme('service_links_build_link', t('Twitter'), "http://twitter.com/home/?status=$turl". "+". $title, t('Share on Twitter.'), 'twitter.png', $nodelink);
}*/
if (variable_get('service_links_show_google', 0)) {
$links['service_links_google'] = theme('service_links_build_link', t('Google'), "http://www.google.com/bookmarks/mark?op=add&bkmk={$url}&title={$title}", t('Bookmark this post on Google.'), 'google.png', $nodelink);
}
if (variable_get('service_links_show_yahoo', 0)) {
$links['service_links_yahoo'] = theme('service_links_build_link', t('Yahoo'), "http://bookmarks.yahoo.com/myresults/bookmarklet?u={$url}&t={$title}", t('Bookmark this post on Yahoo.'), 'yahoo.png', $nodelink);
}
if (variable_get('service_links_show_buzz', 0)) {
$links['service_links_buzz'] = theme('service_links_build_link', t('Buzz Up!'), "http://buzz.yahoo.com/buzz?targetUrl={$url}&headline={$title}&summary={$summary}", t('Buzz up this post on Yahoo! Buzz.'), 'buzz.png', $nodelink);
}
if (variable_get('service_links_show_linkedin', 0)) {
$source = urlencode(variable_get('site_name', 'Drupal'));
$links['service_links_linkedin'] = theme('service_links_build_link', t('LinkedIn'), "http://www.linkedin.com/shareArticle?mini=true&url={$url}&title={$title}&summary={$summary}&source={$source}", t('Publish this to LinkedIn'), 'linkedin.png', $nodelink);
}
if (variable_get('service_links_show_technorati', 0)) {
$links['service_links_technorati'] = theme('service_links_build_link', t('Technorati'), "http://technorati.com/search/{$url}", t('Search Technorati for links to this post.'), 'technorati.png', $nodelink);
}
if (variable_get('service_links_show_icerocket', 0)) {
$links['service_links_icerocket'] = theme('service_links_build_link', t('Icerocket'), "http://blogs.icerocket.com/search?q={$url}", t('Search IceRocket for links to this post.'), 'icerocket.png', $nodelink);
}
// Add your own link by modifing the link below and uncomment it.
//$links['service_links_delicious'] = theme('service_links_build_link', t('Delicious'), "http://del.icio.us/post?url=$url&title=$title", t('Bookmark this post on del.icio.us.'), 'delicious.png', $nodelink);
return $links;
}
function theme_service_links_build_link($text, $url, $title, $image, $nodelink) {
global $base_path;
if ($nodelink) {
switch (variable_get('service_links_style', 1)) {
case 1:
$link = array(
'title' => $text,
'href' => $url,
'attributes' => array(
'title' => $title,
'rel' => 'nofollow',
'target' => '_blank',
),
);
break;
case 2:
$link = array(
'title' => '<img src="' . $base_path . drupal_get_path('module', 'service_links') . '/images/' . $image . '" alt="' . $text . '" />',
'href' => $url,
'attributes' => array(
'title' => $title,
'rel' => 'nofollow',
'target' => '_blank',
),
'html' => TRUE,
);
break;
case 3:
$link = array(
'title' => '<img src="' . $base_path . drupal_get_path('module', 'service_links') . '/images/' . $image . '" alt="' . $text . '" /> ' . $text,
'href' => $url,
'attributes' => array(
'title' => $title,
'rel' => 'nofollow',
'target' => '_blank',
),
'html' => TRUE,
);
break;
}
}
else {
switch (variable_get('service_links_style', 1)) {
case 1:
$link = '<a href="' . check_url($url) . '" title="' . $title . '" rel="nofollow" target="_blank">' . $text . '</a>';
break;
case 2:
$link = '<a href="' . check_url($url) . '" title="' . $title . '" rel="nofollow" target="_blank"><img src="' . $base_path . drupal_get_path('module', 'service_links') . '/images/' . $image . '" alt="' . $text . '" /></a>';
break;
case 3:
$link = '<a href="' . check_url($url) . '" title="' . $title . '" rel="nofollow" target="_blank"><img src="' . $base_path . drupal_get_path('module', 'service_links') . '/images/' . $image . '" alt="' . $text . '" /> ' . $text . '</a>';
break;
}
}
return $link;
}
function theme_service_links_node_format($links) {
return '<div class="service-links"><div class="service-label">' . t('Bookmark/Search this post with: ') . '</div>' . theme('links', $links) . '</div>';
}
function theme_service_links_block_format($items) {
return '<div class="service-links">' . theme('item_list', array_values($items)) . '</div>';
}
/**
* Build a array of all taxonomy terms.
*/
function _service_links_get_terms() {
$types = array();
$vocabularies = taxonomy_get_vocabularies();
foreach ($vocabularies as $vocabulary) {
$tree = taxonomy_get_tree($vocabulary->vid);
foreach ($tree as $term) {
$types[$term->tid] = $term->name;
}
}
return $types;
}
/**
* Check if the service links should be displayed for the node type/category.
*/
function _service_links_show($type, $nid) {
$links_show = FALSE;
$node_type = in_array($type, variable_get('service_links_node_types', array()), TRUE);
if (module_exists('taxonomy')) {
$terms = taxonomy_node_get_terms($nid);
foreach ($terms as $term) {
$category_type = in_array($term->tid, variable_get('service_links_category_types', array()), FALSE);
}
}
if ($node_type || $category_type) {
$links_show = TRUE;
}
return $links_show;
}
Functions
Name | Description |
---|---|
service_links_admin_settings | Menu callback; administration settings for Service Links. |
service_links_block | Implementation of hook_block(). |
service_links_help | Implementation of hook_help(). |
service_links_link | Implementation of hook_link(). |
service_links_menu | Implementation of hook_menu(). |
service_links_nodeapi | Implementation of hook_nodeapi(). |
service_links_perm | Implementation of hook_perm(). |
service_links_render | Function that renders the service links. This is the function themers want to call to insert the service links. |
theme_service_links_block_format | |
theme_service_links_build_link | |
theme_service_links_node_format | |
_service_links_get_terms | Build a array of all taxonomy terms. |
_service_links_show | Check if the service links should be displayed for the node type/category. |