<?php if (!defined('PmWiki')) exit();
/* Copyright 2006-2008 Jon Haupt (jhaupt@gmail.com)
    with additions by Adam Overton (10-2009)
    This file is delicious.php; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published
    by the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.  

    This script enables you to create interaction between PmWiki and delicious.com. 

    To use this script, copy it into the cookbook/ directory
    and add the following line to config.php (or a per-page/per-group
    customization file). 
    include_once("$FarmD/cookbook/delicious.php");

    The script will generate a linkroll or tagroll.
    The simplest linkroll can be created by using the markup (:linkroll user=foo:)
    where foo is the desired delicious username, creating a roll of 10 recent links.
    Similarly (:tagroll user=foo:) creates a complete tag cloud of user foo.

    The script can also add a delicious.com tagometer, by using the markup (:deliciousbadge:).

    The recipe is derived from the delicious.com help pages, specifically:
    http://delicious.com/help/tagrolls
    http://delicious.com/help/linkrolls
    http://delicious.com/help/tagometer

    The following is a list of arguments for tagrolls and linkrolls:

    For (:linkroll:)
	* user: delicious.com username (required)
	* number: the number of links you want to display (default: 10)
	* sort: recent or alpha (default: recent)
	* tags: Limit the linkroll by tag(s). Multiple tags use the syntax 'tag+tag'
	* icon: large, small, rss, or none (default: large)
	* title: text to display as a header (default: My Delicious Bookmarks) - due to a bug, please use title='my title' rather than title="my title"
	* showtags: true or false (default: false) - include the tags associated with each link
	* shownotes: true or false (default: false) - include the notes associated with each link 
	* showname: true or false (default: false) - displays username at the bottom, with a link to user's delicious page
	* showadd: true or false (default: false) - displays link to add user to your delicious network 

    For (:tagroll:)
	* user: delicious.com username (required)
	* number: number of tags to display (default: empty / '')
	* sizerange: in the syntax '8-25', the lowest and highest text size used for display (default: 12-35)
	* title: text to display as a header (default: 'My Delicious Bookmarks')
	* sort: options are freq (frequency) or alpha (default: alpha)
	* flow: cloud or list (default: cloud)
	* color1/color2: color1 is the least frequent tag, color2 is the most frequent (colors 
      are used on a gradient between color1 and color2).  must use html colors, 
      no # (aka like this: '000099')
	* counts: true or false (defaut: false) - include the frequency of each tag
	* showicon: true or false (default: false)
	* showname: true or false (default: false) - displays username at the bottom, with a link to user's delicious page
	* showadd: true or false (default: false) - displays link to add user to your delicious network 


	For (:tagometer:)
	Users can declare their own tagometer badge using the copy&paste method from http://delicious.com/help/tagometer via $DeliciousTagometerFmt = 'yourjavascript';
	* size: wide or tall (default: wide)
	* icon: true or false (default: true) - shows or hides delicious icon
	* timesSaved: true or false (default: true) - shows or hides # of times saved
	* graph: true or false (default: true) - shows or hides graph
	* tags: true or false (default: true) - shows or hides tag

	Declare your own set of default values via $DeliciousTagometerDefaults in config.php:
	$DeliciousTagometerDefaults = array(
		'size' => 'wide',
		'icon' => 'true',
		'timesSaved' => 'true',
		'graph' => 'true',
		'tags' => 'true'
	);


	For (:deliciousbadge:)
	Users can declare their own network badge using the copy&paste method from http://delicious.com/help/networkbadges via:
	$DeliciousBadgeFmt = 'yourjavascripthere';
	Otherwise, (:deliciousbadge:) can be called with the following arguments:
	* user: delicious.com username (required)
	* icon: medium, small, or none (default: medium)
	* bookmarkcount: true or false (default: true) - number of bookmarks you've collected
	* networkcount: true or false (default: true) - number of networks you belong to
	* fancount: true or false (default: true)- number of fans you have
	* showicon: true or false  (default: true)
	* showname: true or false (default: true) - displays username at the bottom, with a link to user's delicious page

	Declare your own set of default values via $DeliciousBadgeDefaults in config.php:
	$DeliciousBadgeDefaults = array(
		'user' => 'myAccount',
		'icon' => 'medium',
		'bookmarkcount' => 'true',
		'networkcount' => 'true',
		'fancount' => 'true',
		'showicon' => 'true',
		'showname' => 'true'
	);
	
	
    Version Log:

    * March 2006 1.0 - Initial Release
    * January 2007 1.1 - Added tagometer badge
    * November 2008 - Updated for change from delicious.com to delicious.com
    * April 2009 - Bug fixes. changed "label" to "title" to match delicious.com
    * 2009-10-03 - re-uploaded supposed version of April 2009 (just guessing), as it appears November 2008 was what was still being offered above; fixed issue with 'title' not displaying; plus, added $DeliciousBookmarkDefaults and $DeliciousTagDefaults as SDVA's, for easy config.php declaration
    * 2009-10-04 - can now display notes via shownotes=true, plus added showname & showadd; revised (:deliciousbadge:) to permit arguments
    * 2009-10-05 - added more arguments (showadd, showicon, showname) to (:tagroll:). changed (:deliciousbadge:) to incorporate the Delicious Network Badge. The Tagometer is now called via (:tagometer:) and has various arguments. renamed various variable names, and cleaned up the code a bit...
    * 2009-10-14 - added style=none for tagrolls - removes default delicious formatting, allows tagrolls to be formatted via align=right, etc...

*/

$RecipeInfo['delicious']['Version']='2009-10-14';

Markup("linkroll", ">block", '/\\(:linkroll\\s*(.*?):\\)/ei', "DeliciousLinks('$1')");
Markup("tagroll", ">block", '/\\(:tagroll\\s*(.*?):\\)/ei', "DeliciousTags('$1')");


SDVA($DeliciousBookmarkDefaults, array (
    'number' => '10',
    'sort' => 'recent',
    'tags' => '',
    'user' => '',
    'icon' => 'large',
    'title' => 'My Delicious Bookmarks',
    'showtags' => 'false',
    'shownotes' => 'false',
    'showadd' => 'false',
    'showname' => 'false'
));

function DeliciousLinks($p) {
  global $DeliciousBookmarkDefaults;
  // Defaults & Inputs
  $opt = array_merge($DeliciousBookmarkDefaults, ParseArgs($p));
  
  // Return nothing if no username listed
  if ($opt['user'] == '') { return ''; }

  // Begin output
  $output = "<span class='linkroll'><script type='text/javascript' src='http://feeds.delicious.com/v2/js/".$opt['user'];
  if ($opt['tags'] != '') $output .= "/".$opt['tags'];
  $output .= "?";
  $output .= "count=".$opt['number'];
  if ($opt['title'] != '') {
    $output .= "&title=".$opt['title']; }    
/*
  if ($opt['bullet'] != '' && $opt['bullet'] != 'none') {
    #$output .= "&bullet=%E2%80%A2";  # ¥
    $output .= "&bullet=%C2%BB";  # >>
}
*/
  if ($opt['icon'] != 'none') {
    $output .= "&icon";
    if ($opt['icon'] == 'small') $output .= "=s";
      else if ($opt['icon'] == 'rss') $output .= "=rss";
  }
  $output .= "&sort=".$opt['sort'];
  if ($opt['showtags'] == 'true') $output .= "&tags";
  if ($opt['shownotes'] == 'true') $output .= "&extended";
  if ($opt['showname'] == 'true') $output .= "&name";
  if ($opt['showadd'] == 'true') $output .= "&showadd";
  $output .= "'></script></span>";
  // Finish output and return it
  return $output;
};


SDVA($DeliciousTagDefaults, array(
	'number' => '',
	'sort' => 'alpha',
	'sizerange' => '12-35',
	'user' => '',
	'flow' => 'cloud',
	'title' => 'My Delicious Tags',
	'color1' => '87ceeb',
	'color2' => '0000ff',
	'counts' => 'false',
	'showicon' => 'false',
	'showname' => 'false',
	'showadd' => 'false'
));

function DeliciousTags($p) {
	global $DeliciousTagDefaults;
	// Defaults & Inputs
	$opt = array_merge($DeliciousTagDefaults, ParseArgs($p));
	
	// Return nothing if no username listed
	if ($opt['user'] == '') { return ''; }
	
	// Begin output
	$output = "<span class='tagroll'><script type='text/javascript' src='http://feeds.delicious.com/v2/js/tags/".$opt['user']."?title=".$opt['title']."&sort=".$opt['sort'];
	if ($opt['number'] != '') $output .= "&count=".$opt['number'];
	if ($opt['counts'] == 'true') $output .= "&totals";
	if ($opt['flow'] == 'list') $output .= "&flow=list";
	  else $output .= "&flow=cloud";
	if ($opt['showicon'] == 'true') $output .= "&icon";
	if ($opt['showname'] == 'true') $output .= "&name";
	if ($opt['showadd'] == 'true') $output .= "&showadd";
	if ($opt['style'] == 'none') $output .= "&style=".$opt['style'];  # prevents delicious styling
	$output .= "&size=".$opt['sizerange']."&color=".$opt['color1']."-".$opt['color2']."'></script></span>";

	// Finish output and return it
	return $output;
}


# DELICIOUS TAGOMETER BADGE
SDV($DeliciousTagometerFmt, '');
SDVA($DeliciousTagometerDefaults, array(
	'size' => 'wide',
	'icon' => 'true',
	'timesSaved' => 'true',
	'graph' => 'true',
	'tags' => 'true'
));
Markup("delicioustagometer", ">block", '/\\(:tagometer\\s*(.*?):\\)/ei', "DeliciousTagometerBadge('$1')");

function DeliciousTagometerBadge($p) {
	global $DeliciousTagometerDefaults, $DeliciousTagometerFmt;

	# allow user to declare their own badge, using the copy&paste method from http://delicious.com/help/tagometer
	if ($DeliciousTagometerFmt) {
		return Keep($DeliciousTagometerFmt);
	}
	$opt = array_merge($DeliciousTagometerDefaults, ParseArgs($p));
	
	$output = "<script type=\"text/javascript\">
	    if (typeof window.Delicious == \"undefined\") window.Delicious = {};";

	if($opt['size']=='tall') $output .= "\nDelicious.BLOGBADGE_DEFAULT_CLASS = 'delicious-blogbadge-tall'";
	if($opt['icon']=='false') $output .= "\nDelicious.BLOGBADGE_ICON_SHOW = false";
	if($opt['timesSaved']=='false') $output .= "\nDelicious.BLOGBADGE_SAVECOUNT_SHOW = false";
	if($opt['graph']=='false') $output .= "\nDelicious.BLOGBADGE_GRAPH_SHOW = false";
	if($opt['tags']=='false') $output .= "\nDelicious.BLOGBADGE_TAGS_SHOW = false";

	$output .= "\n</script>
	<script src=\"http://static.delicious.com/js/d2-blogbadge.js\"></script>";

	return Keep($output);
}	


# DELICIOUS NETWORK BADGE
SDV($DeliciousBadgeFmt, '');
SDVA($DeliciousBadgeDefaults, array(
	'user' => '',
	'icon' => 'medium',
	'bookmarkcount' => 'true',
	'networkcount' => 'true',
	'fancount' => 'true',
	'showicon' => 'true',
	'showname' => 'true'
));
Markup("deliciousnetworkbadge", ">block", '/\\(:deliciousbadge\\s*(.*?):\\)/ei', "DeliciousNetworkBadge('$1')");

function DeliciousNetworkBadge($p) {
	global $DeliciousBadgeDefaults, $DeliciousBadgeFmt;

	# allow user to declare their own badge, using the copy&paste method from http://delicious.com/help/tagometer
	if ($DeliciousBadgeFmt) {
		return Keep($DeliciousBadgeFmt);
	}
	$opt = array_merge($DeliciousBadgeDefaults, ParseArgs($p));
	
	// Return nothing if no username listed
	if ($opt['user'] == '') { return ''; }

	$output = '<script type="text/javascript" src="http://feeds.delicious.com/v2/js/networkbadge/'.$opt['user'].'?';
	if($opt['icon']=='medium') $output .= "&icon=m";
		elseif ($opt['icon']=='small') $output .= "&icon=s";
	if($opt['bookmarkcount']=='true') $output .= "&itemcount";
	if($opt['networkcount']=='true') $output .= "&nwcount";
	if($opt['fancount']=='true') $output .= "&fancount";
	if ($opt['showicon'] == 'true') $output .= "&icon";
	if ($opt['showname'] == 'true') $output .= "&name";
	$output .= '"></script>';

	return Keep($output);
}