Social Bookmarklet helper
This helper provides an easy way to add social bookmarklet to your CakePHP based website.
hi,
This is a very simple helper that will create social bookmarklets. All you need to do is enable helper and just tell the title. Below are the steps on how to use this helper:
Enjoy
This is a very simple helper that will create social bookmarklets. All you need to do is enable helper and just tell the title. Below are the steps on how to use this helper:
- Download bookmark icons from http://www.personal.psu.edu/rja171/widgets/bookmarks.zip and unzip them in your webroot/img/ folder. Thus, for instance, google bookmark icon will be placed at /webroot/img/bookmarks/google.gif
- Create an empty file, "bookmark.php" and copy and paste the below helper code in it. The file should be located at /app/views/helpers/
- Add the Bookmark Helper in your app_controller.php file
Controller Class:
Download code<?php var $helpers = array('Bookmark'); ?>
Helper Class:
Download code
<?php
<?php
/**
* @author Ritesh Agrawal
* @version 1.0
* returns social bookmarklets
* Note: You will need to change the domain name to match your domain name
* Thanks to http://kevin.vanzonneveld.net/ for the social bookmark list
*/
class BookmarkHelper extends Helper{
var $helpers = array('Html');
/**
* Folder where all the bookmark images are located.
*/
var $imgFolder = "img/bookmarks/";
/**
* change this defaults if you want to use a different set of bookmarklets
* The array elements should correspond to $bookmarks keys
*/
var $defaults = array('google', 'stumble', 'digg');
/**
* @param $pagetitle - (required) Title of the Page
* @param $url - (optional) URL of the page
* @param $sites - (optional)social bookmarks. If not provided the helper uses the defaults set above. The values should match to the keys of the "bookmarks" variable defined below
* returns a div with the specified social bookmarklets
*/
function getBookMarks($pagetitle, $url = null, $sites = array()){
if(empty($url)){
/* Note: As an alternative you can try Router::url("", true). This should return the absolute url of the current page, but wasn't working for me. So I used this hack. Hopefully someone can tell me a better way to find absolute path */
$url = FULL_BASE_URL . $this->Html->url(null, true);
}
if(empty($sites)){
$sites = $this->defaults;
}
$output = "";
foreach($sites as $site){
if(!array_key_exists($site, $this->bookmarks))
continue;
//build url
$link = $this->bookmarks[$site]['link'];
$link = str_replace('{url}', $url, $link);
if(substr_count($link, '{title}') > 0)
$link = str_replace('{title}',urlencode($pagetitle), $link );
$name = $this->bookmarks[$site]['name'];
$iconLoc = Router::url('/', true) . $this->imgFolder . $this->bookmarks[$site]['icon'];
$image = $this->Html->image($iconLoc, array('title'=> "{$name}", 'alt'=>"{$name}", 'border'=> "0"));
$output .= $this->Html->link( $image, $link, array('escape'=> false)). ' ';
}
return '<div id="bookmarklets">' . $output . '</div>';
}
/**
* list of social bookmarks.
* if you want to use any other social bookmark, replace the actual URL with "{url}" and title with "{title}".
* See below bookmarks for more details
*/
var $bookmarks = array(
'yahoo' =>
array(
'name' => 'Yahoo! My Web',
'link' => 'http://myweb2.search.yahoo.com/myresults/bookmarklet?u={url}&t={title}',
'icon'=> 'yahoo.gif'
),
'google' => array(
'name' => 'Google Bookmarks',
'link' => 'http://www.google.com/bookmarks/mark?op=edit&bkmk={url}&title={title}',
'icon'=> 'google.gif'
),
'windows' => array(
'name' => 'Windows Live',
'link' => 'https://favorites.live.com/quickadd.aspx?url={url}&title={title}',
'icon' => 'windows.gif'
),
'facebook' => array(
'name' => 'Facebook',
'link' => 'http://www.facebook.com/sharer.php?u={url}&t={title}',
'icon' => 'facebook.gif'
),
'digg' => array(
'name' => 'Digg',
'link' => 'http://digg.com/submit?phase=2&url={url}&title={title}',
'icon' => 'digg.gif'
),
'ask' => array(
'name' => 'Ask',
'link' => 'http://myjeeves.ask.com/mysearch/BookmarkIt?v=1.2&t=webpages&url={url}&title={title}',
'icon' => 'ask.gif',
),
'technorati' => array(
'name' => 'Technorati',
'link' => 'http://www.technorati.com/faves?add={url}',
'icon' => 'bookmars/technorati.gif'
),
'delicious' => array(
'name' => 'del.icio.us',
'link' => 'http://del.icio.us/post?url={url}&title={title}',
'icon' => 'delicious.gif'
),
'stumble' => array(
'name' => 'StumbleUpon',
'link' => 'http://www.stumbleupon.com/submit?url={url}&title={title}',
'icon' => 'stumble.gif'
),
'squidoo' => array(
'name' => 'Squidoo',
'link' => 'http://www.squidoo.com/lensmaster/bookmark?{url}'
),
'netscape' => array(
'name' => 'Netscape',
'link' => 'http://www.netscape.com/submit/?U={url}&T={title}',
'icon' => 'netscape.gif'
),
'slashdot' => array(
'name' => 'Slashdot',
'link' => 'http://slashdot.org/bookmark.pl?url={url}&title={title}',
'icon' => 'slashdot.gif'
),
'reddit' => array(
'name' => 'reddit',
'link' => 'http://reddit.com/submit?url={url}&title={title}',
'icon' => 'reddit.gif'
),
'furl' => array(
'name' => 'Furl',
'link' => 'http://furl.net/storeIt.jsp?u={url}&t={title}',
'icon' => 'furl.gif'
),
'blinklist' => array(
'name' => 'BlinkList',
'link' => 'http://blinklist.com/index.php?Action=Blink/addblink.php&Url={url}&Title={title}',
'icon' => 'blinklist.gif'
),
'dzone' => array(
'name' => 'dzone',
'link' => 'http://www.dzone.com/links/add.html?url={url}&title={title}',
'icon' => 'dzone.gif'
),
'swik' => array(
'name' => 'SWiK',
'link' => 'http://stories.swik.net/?submitUrl&url={url}'
),
'shoutwire' => array(
'name' => 'Shoutwrie',
'link' => 'http://www.shoutwire.com/?p=submit&&link={url}',
'icon' => 'shoutwire.gif'
),
'blinkbits' => array(
'name' => 'Blinkbits',
'link' => 'http://www.blinkbits.com/bookmarklets/save.php?v=1&source_url={url}',
'icon' => 'blinkbits.gif'
),
'spurl' => array(
'name' => 'Spurl',
'link' => 'http://www.spurl.net/spurl.php?url={url}&title={title}',
'icon' => 'spurl.gif'
),
'diigo' => array(
'name' => 'Diigo',
'link' => 'http://www.diigo.com/post?url={url}&title={title}',
'icon' => 'diigo.gif'
),
'tailrank' => array(
'name' => 'Tailrank',
'link' => 'http://tailrank.com/share/?link_href={url}&title={title}',
'icon' => 'tailrank.gif'
),
'rawsugar' => array(
'name' => 'Rawsugar',
'link' => 'http://www.rawsugar.com/tagger/?turl={url}&tttl={title}&editorInitialized=1',
'icon' => 'rawsugar.gif'
)
);
}
?>
?>
Enjoy
Comments
Comment
1 A couple of notes
2. The preferred method of using another helper (eg HtmlHelper) is:
var $helpers = array('Html');
...
$this->Html->link();
3. Rather than requiring the developer provide a $domain variable, you could just have
Although, do you even really need an absolute path for the images?$domain = Router::url('/', true);
Comment
2 Thanks
Thanks for suggesting those changes. I updated the Bookmark helper. I didn't knew about Router::url before. This will make the helper more user-friendly as now they can just save the helper and start using it.
Thanks,
Ritesh
Bug
3 Icon Issues
A couple issues with the icons:
1) Swik doesn't have an icon setup, but there is one in the zip.
2) Technorati has the path as part of the icon.
3) Squidoo, Tailrank and RawSugar don't have icons in the zip.
Thanks for this. Figuring out all the URLs must have been fun :)