RSS
 
20. Dec. 2010

Flattr CakePHP Helper

20 Dec

Get "Flattred"

As you can see wordpress (and my blog) already has a Flattr plugin (for posts) and a widget (for the sidebar).

With a little helper you can easily integrate Flattr to your site – the "cake way".
I stumbled upon on this post during the process of writing it. But instead of the deprecated js elements we can now use the rel tag (in the future the HTML5 tags will take over, but for now the rel tag is fine).
My helper can be used out of the box.

Usage

Especially if you don’t want to submit your flattr uid to the helper all the time, you can store it in the configs:

Configure::write('Flattr.uid', 'yourFlattrUid');

Now add your helper to the controller (or action) and call it anywhere in the view:

echo $this->Flattr->button('http://www.your-domain.com');

Or: More specific to an article/post/profile etc – with some additional options:

$options = array(
	'button' => 'compact',
	'language' => 'de_DE',
	'tags' => array('Some', 'Tags'),
	'category' => 'software'
);
$attr = array(
	'title' => 'Some title of the article that gets flattred',
);
echo $this->Flattr->button('http://www.your-domain.com/some/unique/url', $options, $attr);

You need to make sure that the urls are unique.

Some tips

You do have to open an account on flattr.com and you have to submit at least 2 € each month. But hopefully you get even more from other flattr users 🙂
The content doesn’t have to be submitted prior to including the button. If the url (and its content) hasn’t been flattred yet, it will automatically be created on their website as soon as the first person clicks your button.

If you want to include a "global" button for your website you should submit it to flattr, though. Simply because you can create a specific title and text for it then. Just use your main domain "http://www.your-domain.com".

And here is the helper

Put it in /views/helpers/ and call it flattr.php

/**
 * Flattr Donate Button
 * @link http://flattr.com/support/integrate/js
 * 2010-12-11 ms
 */
class FlattrHelper extends AppHelper {
	
	public $helpers = array('Html');
	
	const API_URL = 'http://api.flattr.com/';
	
	
	/** 
	 * display the FlattrButton
	 * @param mixed $url (unique! necessary)
	 * @param array $options
	 * 2010-12-19 ms
	 */
	function button($url, $options = array(), $attr = array()) {
		if (empty($options['uid'])) {
			$options['uid'] = Configure::read('Flattr.uid');
		}
		$categories = array();
		
		$defaults = array(
			'mode' => 'auto',
			'language' => 'en_US',
			'category' => 'text',
			'button' => 'default', # none or compact
			'tags' => array(),
			//'hidden' => '',
			//'description' => '',
		);
		$options = array_merge($defaults, $options);
		
		$mode = $options['mode'];
		unset($options['mode']);
		if (is_array($options['tags'])) {
			$options['tags'] = implode(',', $options['tags']);
		}
		
		$rev = array();
		foreach ($options as $key => $option) {
			$rev[] = $key.':'.$option;
		}
		$linkOptions = array(
			'title' => $_SERVER['HTTP_HOST'],
			'class' => 'FlattrButton',
			'style' => 'display:none;',
			'rel' => 'flattr;'.implode(';', $rev)
		);
		$linkOptions = array_merge($linkOptions, $attr);
		
		$js = "(function() {
    var s = document.createElement('script'), t = document.getElementsByTagName('script')[0];
    s.type = 'text/javascript';
    s.async = true;
    s.src = ".self::API_URL."'js/0.6/load.js?mode=".$mode."';
    t.parentNode.insertBefore(s, t);
})();";
		$code = $this->Html->link('', $this->Html->url($url, true), $linkOptions);
		$code .= $this->Html->scriptBlock($js, array('inline' => true));
		return $code;
	}
}
Flattr CakePHP Helper
0 votes, 0.00 avg. rating (0% score)
 
3 Comments

Posted by Mark in CakePHP

 

Tags: , ,

Leave a Reply

Tip:
If you need to post a piece of code use {code type=php}...{/code}.
Allowed types are "php", "mysql", "html", "js", "css".

Please do not escape your post (leave all ", <, > and & as they are!). If you have encoded characters and need to reverse ("decode") it, you can do that here!
 

 
  1. Ramblings of a Developer » Flattr Cakephp helper

    March 29, 2011 at 14:18

    […] EDIT: DerEuroMark wrote an excellent replacement for this helper. […]

     
  2. Kristoffer Darj

    May 29, 2011 at 16:32

    Exellent… I wrote my own from the same obsolete post you are referring to.

    … then tried google. Your is a bit more tidy so I’ll throw my own in the bin. Thanks. 🙂

     
  3. Mark

    May 29, 2011 at 16:39

    i might still have missed sth.
    so feel free to enhance it even further 🙂