FlattrHelper.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. App::uses('AppHelper', 'View/Helper');
  3. /*
  4. uid The Flattr User ID as found on the Flattr dashboard (in the example I used mine).
  5. tle The title for the link to be submitted.
  6. Since the helper supports the full range of options below are the other options:
  7. dsc A description for the link.
  8. cat The category for the link. This can be any of the following: text, images, video, audio, software, rest. The default if this option isn’t specified is text.
  9. lng The language of the link. Any of the languages on this list and defaults to en_GB.
  10. tags Any tags matching the link. This field must be an array!
  11. url The URL of the link.
  12. btn The badge to use. Currently the only option is compact but if not specified or set to something else it defaults to the standard larger badge
  13. */
  14. /**
  15. * Flattr Donate Button
  16. * @link http://flattr.com/support/integrate/js
  17. */
  18. class FlattrHelper extends AppHelper {
  19. public $helpers = ['Html'];
  20. const API_URL = 'http://api.flattr.com/';
  21. /**
  22. * Display the FlattrButton
  23. *
  24. * @param mixed $url (unique! necessary)
  25. * @param array $options
  26. * @return string
  27. */
  28. public function button($url, $options = [], $attr = []) {
  29. if (empty($options['uid'])) {
  30. $options['uid'] = Configure::read('Flattr.uid');
  31. }
  32. $categories = [];
  33. $defaults = [
  34. 'mode' => 'auto',
  35. 'language' => 'de_DE',
  36. 'category' => 'text',
  37. 'button' => 'default', # none or compact
  38. 'tags' => [],
  39. //'hidden' => '',
  40. //'description' => '',
  41. ];
  42. $options += $defaults;
  43. $mode = $options['mode'];
  44. unset($options['mode']);
  45. if (is_array($options['tags'])) {
  46. $options['tags'] = implode(',', $options['tags']);
  47. }
  48. $rev = [];
  49. foreach ($options as $key => $option) {
  50. $rev[] = $key . ':' . $option;
  51. }
  52. $linkOptions = [
  53. 'title' => env('HTTP_HOST'),
  54. 'class' => 'FlattrButton',
  55. 'style' => 'display:none;',
  56. 'rel' => 'flattr;' . implode(';', $rev)
  57. ];
  58. $linkOptions = $attr + $linkOptions;
  59. $js = "(function() {
  60. var s = document.createElement('script'), t = document.getElementsByTagName('script')[0];
  61. s.type = 'text/javascript';
  62. s.async = true;
  63. s.src = '" . static::API_URL . "js/0.6/load.js?mode=" . $mode . "';
  64. t.parentNode.insertBefore(s, t);
  65. })();";
  66. $code = $this->Html->link('', $this->Html->url($url, true), $linkOptions);
  67. //&uid=gargamel&language=sv_SE&category=text
  68. // compact: <a class="FlattrButton" style="display:none;" rev="flattr;button:compact;"href="X"></a>
  69. // static: <a href="http://flattr.com/thing/X" target="_blank"><img src="http://api.flattr.com/button/flattr-badge-large.png" alt="Flattr this" title="Flattr this" border="0" /></a>
  70. $code .= $this->Html->scriptBlock($js, ['inline' => true]);
  71. return $code;
  72. }
  73. //TODO: http://api.flattr.com/odapi/categories/json - parse
  74. /**
  75. * @deprecated!!!
  76. * @param mixed $uid
  77. * @param array $options:
  78. * - tle, cat, lng, dsc, url, btn, tags, hidden (optional)
  79. * @return string html with js script tag
  80. */
  81. public function badge($uid = null, $options = []) {
  82. if (!$uid) {
  83. $uid = Configure::read('Flattr.uid');
  84. }
  85. if (!isset($options['tle'])) {
  86. $options['tle'] = __d('tools', 'Donate');
  87. }
  88. $vars = '';
  89. $vars .= "var flattr_uid = '" . h($uid) . "';\r\n";
  90. $vars .= "var flattr_tle = '" . $options['tle'] . "';\r\n";
  91. if (!isset($options['dsc'])) {
  92. $options['dsc'] = '';
  93. }
  94. $vars .= "var flattr_dsc = '" . $options['dsc'] . "';\r\n";
  95. if (!isset($options['cat'])) {
  96. $options['cat'] = 'text';
  97. }
  98. $vars .= "var flattr_cat = '" . $options['cat'] . "';\r\n";
  99. if (!isset($options['lng'])) {
  100. $options['lng'] = 'en_GB';
  101. }
  102. $vars .= "var flattr_lng = '" . $options['lng'] . "';\r\n";
  103. if (isset($options['tags']) && count($options['tags']) > 0) {
  104. //array_walk($options['tags'], 'Sanitize::paranoid');
  105. $vars .= "var flattr_tag = '" . implode(', ', $options['tags']) . "';\r\n";
  106. }
  107. if (isset($options['url']) && ((version_compare(phpversion(), '5.2.0', '>=') && function_exists('filter_var')) ? filter_var($options['url'],
  108. FILTER_VALIDATE_URL) : true)) {
  109. $vars .= "var flattr_url = '" . $options['url'] . "';\r\n";
  110. }
  111. if (isset($options['btn']) && $options['btn'] === 'compact') {
  112. $vars .= "var flattr_btn = 'compact';\r\n";
  113. }
  114. $code = $this->Html->scriptBlock($vars, ['inline' => true]);
  115. $code .= $this->Html->script(static::API_URL . 'button/load.js', ['inline' => true]);
  116. return $code;
  117. }
  118. }