<?php
/* $Id: service_links.module,v 1.2.2.1 2006/01/31 14:07:38 frjo Exp $ */

/**
* @file
* Author: Fredrik Jonsson <fredrik at combonet dot se>
* A module that adds Digg, del.icio.us, reddit and Technorati links to nodes.
*/

/**
* Implementation of hook_help().
*/
function service_links_help($section = 'admin/help#service_links') {
switch ($section) {
case 'admin/modules#description':
$output = t('Add Digg, del.icio.us, reddit, bloglines, Shadows Tag!, My Yahoo!, My Web 2.0, Google Reader, RawSugar, Spurl, Simpy, Newsgator, Fark, Blink List, Furl and Technorati links to nodes.');
break;
case 'admin/settings/service_links':
$output = t('Here you can configure the service links.');
break;
}
return $output;
}

/**
* Implementation of hook_settings().
*/
function service_links_settings() {

foreach(node_list() as $type) {
$name = node_invoke($type, 'node_name');
$options[$type] = $name;
}

$show_set = form_checkbox(t('Show del.icio.us links'), 'service_links_show_delicious', 1, variable_get('service_links_show_delicious', 1));
$show_set .= form_checkbox(t('Show Digg links'), 'service_links_show_digg', 1, variable_get('service_links_show_digg', 1));
$show_set .= form_checkbox(t('Show Reddit links'), 'service_links_show_reddit', 1, variable_get('service_links_show_reddit', 1));
$show_set .= form_checkbox(t('Show Technorati links'), 'service_links_show_technorati', 1, variable_get('service_links_show_technorati', 1));
$show_set .= form_checkbox(t('Show Bloglines links'), 'service_links_show_bloglines', 1, variable_get('service_links_show_bloglines', 1));
$show_set .= form_checkbox(t('Show My Yahoo links'), 'service_links_show_yahoo', 1, variable_get('service_links_show_yahoo', 1));
$show_set .= form_checkbox(t('Show Google Reader links'), 'service_links_show_google', 1, variable_get('service_links_show_google', 1));
$show_set .= form_checkbox(t('Show Newsgator links'), 'service_links_show_newsgator', 1, variable_get('service_links_show_newsgator', 1));
$show_set .= form_checkbox(t('Show Furl links'), 'service_links_show_furl', 1, variable_get('service_links_show_furl', 1));
$show_set .= form_checkbox(t('Show Simpy links'), 'service_links_show_simpy', 1, variable_get('service_links_show_simpy', 1));
$show_set .= form_checkbox(t('Show Spurl links'), 'service_links_show_spurl', 1, variable_get('service_links_show_spurl', 1));
$show_set .= form_checkbox(t('Show Blink List links'), 'service_links_show_blink', 1, variable_get('service_links_show_blink', 1));
$show_set .= form_checkbox(t('Show Web 2.0 List links'), 'service_links_show_web2', 1, variable_get('service_links_show_web2', 1));
$show_set .= form_checkbox(t('Show Tag!RawSugar List links'), 'service_links_show_rawsugar', 1, variable_get('service_links_show_rawsugar', 1));
$show_set .= form_checkbox(t('Show Shadows Tag List links'), 'service_links_show_shadows', 1, variable_get('service_links_show_shadows', 1));
$show_set .= form_checkbox(t('Show Fark it'), 'service_links_show_fark', 1, variable_get('service_links_show_fark', 1));
$output = form_group(t('What links to show'), $show_set);

$how_set = form_radios(t('Link style'), 'service_links_style', variable_get('service_links_style', 1), array('1' => t('Text links'),'2' => t('Image links'),'3' => t('Image and text links')));
$how_set .= form_select(t('Node types to display links for'), 'service_links_node_types', variable_get('service_links_node_types', array()), $options, NULL, 0, TRUE);
$how_set .= form_checkbox(t('Show links also on nodes on front page'), 'service_links_main', 1, variable_get('service_links_main', 0));
$output .= form_group(t('Where and how to show the links'), $how_set);

return $output;
}

/**
* Implementation of hook_link().
*/
function service_links_link($type, $node = 0, $main) {
$links = array();

$node_type = in_array($node->type, variable_get('service_links_node_types', array()), TRUE);
$links_main = variable_get('service_links_main', 0) == '1' || $main == 0 ? '1' : '0';

if ($type == 'node' && $node_type && $links_main) {
$url = url("node/$node->nid", NULL, NULL, TRUE);
$title = urlencode($node->title);

if (variable_get('service_links_show_delicious', 1)) {
$links[] = _service_link_build_link(t('Del.icio.us'), "http://del.icio.us/post?url=$url&title=$title", t('Bookmark this post on del.icio.us.'), 'delicious.png');
}
if (variable_get('service_links_show_digg', 1)) {
$links[] = _service_link_build_link(t('Digg it'), "http://digg.com/submit?phase=2&url=$url", t('Digg this post on digg.com.'), 'digg.png');
}
if (variable_get('service_links_show_fark', 1)) {
$links[] = _service_link_build_link(t('Fark it!'), "http://cgi.fark.com/cgi/fark/submit.pl", t('Fark this post on Fark.com.'), 'fark.gif');
}
if (variable_get('service_links_show_reddit', 1)) {
$links[] = _service_link_build_link(t('Reddit'), "http://reddit.com/submit?url=$url&title=$title", t('Submit this post on reddit.com.'), 'reddit.png');
}
if (variable_get('service_links_show_technorati', 1)) {
$links[] = _service_link_build_link(t('Technorati'), "http://technorati.com/cosmos/search.html?url=$url", t('Search Technorati for links to this post.'), 'technorati.png');
}
if (variable_get('service_links_show_furl', 1)) {
$links[] = _service_link_build_link(t('Furl'), "http://www.furl.net/storeIt.jsp?u=$url", t('Store this link in furl.'), 'furl.gif');
}
if (variable_get('service_links_show_simpy', 1)) {
$links[] = _service_link_build_link(t('Simpy'), "http://simpy.com/simpy/LinkAdd.do?note=Hellacrazy&href=$url&title=$title&tags=$terms", t('Submit link to Simpy.'), 'simpy.bmp');
}
if (variable_get('service_links_show_blink', 1)) {
$links[] = _service_link_build_link(t('Blink List'), "http://www.blinklist.com/index.php?Action=Blink/addblink.php&Url=$url&title=$title", t('Submit link to Blink List.'), 'blinklist.gif');
}
if (variable_get('service_links_show_spurl', 1)) {
$links[] = _service_link_build_link(t('Spurl'), "http://www.spurl.net/spurl.php?url=$url&title=$title", t('Submit link to Spurl.'), 'spurl.jpg');
}
if (variable_get('service_links_show_web2', 1)) {
$links[] = _service_link_build_link(t('Web 2.0'), "http://myweb2.search.yahoo.com/myresults/bookmarklet?t=$title&u=$url&d=HellaCrazy&tag=$terms", t('Submit link to Web 2.0.'), 'yahoomyweb.gif');
}
if (variable_get('service_links_show_rawsugar', 1)) {
$links[] = _service_link_build_link(t('Tag! RawSugar'), "http://www.rawsugar.com/pages/tagger.faces?turl=$url&tttl=$title", t('Submit link to Tag!RawSugar Account.'), 'rawsugar.png');
}
if (variable_get('service_links_show_shadows', 1)) {
$links[] = _service_link_build_link(t('Shadow Tag'), "http://www.shadows.com/features/tcr.htm?url=$url&title=$title", t('Submit link to Shadows Tag Account.'), 'shadows.ico');
}

if (variable_get('service_links_show_bloglines', 1)) {
$links[] = _service_link_build_link(t('Bloglines'), "http://www.bloglines.com/sub/$url/feed", t('Submit this Article to your Bloglines account.'), 'bloglines.jpg');
}
if (variable_get('service_links_show_yahoo', 1)) {
$links[] = _service_link_build_link(t('My Yahoo!'), "http://add.my.yahoo.com/rss?url=$url/feed", t('Submit this Article to your My Yahoo account.'), 'web2.ico');
}
if (variable_get('service_links_show_google', 1)) {
$links[] = _service_link_build_link(t('Google Reader'), "http://fusion.google.com/add?feedurl=$url/feed", t('Submit this Article to your Google Reader account.'), 'google.gif');
}
if (variable_get('service_links_show_newsgator', 1)) {
$links[] = _service_link_build_link(t('Newsgator'), "http://www.newsgator.com/ngs/subscriber/subext.aspx?url=$url/feed", t('Submit this Article to your Newsgator Reader account.'), 'newsgator.jpg');
}


}

return $links;
}

function _service_link_build_link($text, $url, $title, $image) {
if (variable_get('service_links_style', 1) == '3') {
$links = '<a href="'. check_url($url) .'" title="'. $title .'">'. $text .'<img src="'. drupal_get_path('module', 'service_links') .'/'. $image .'" alt="'. $text .'" /></a>';
}
elseif (variable_get('service_links_style', 1) == '2') {
$links = '<a href="'. check_url($url) .'" title="'. $title .'"><img src="'. drupal_get_path('module', 'service_links') .'/'. $image .'" alt="'. $text .'" /></a>';
}
else {
$links = '<a href="'. check_url($url) .'" title="'. $title .'">'. $text .'</a>';
}

return $links;
}
?>