66c81580b92aaad6fa3b46c58af324233f189d00
[project/addthis.git] / addthis.module
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Stand alone module file to handle AddThis button integration
7 */
8
9 /**
10 * Implementation of hook_perm().
11 */
12 function addthis_perm() {
13 $perms[] = 'administer addthis';
14 $perms[] = 'view addthis';
15 return $perms;
16 }
17
18 /**
19 * Implementation of hook_link().
20 */
21 function addthis_link($type, $node=NULL, $teaser = FALSE) {
22 $links = array();
23
24 if ($type === 'node' && user_access('view addthis')) {
25 if (($teaser && variable_get('addthis_display_in_teasers', '0')) ||
26 (!$teaser && variable_get('addthis_display_in_links', '0'))) {
27 $links['addthis'] = array(
28 'title' => _addthis_create_button($node, $teaser),
29 'html' => TRUE,
30 );
31 }
32 }
33
34 return $links;
35 }
36 /**
37 * Implementation of hook_menu().
38 */
39 function addthis_menu() {
40 $items = array();
41
42 $items['admin/settings/addthis'] = array(
43 'title' => t('AddThis'),
44 'description' => t('Set username and customize look and feel for <a href="http://www.addthis.com/">AddThis</a> button.'),
45 'page callback' => 'drupal_get_form',
46 'page arguments' => array('addthis_admin_settings'),
47 'access arguments' => array('administer addthis'),
48 'file' => 'addthis.admin.inc',
49 );
50
51 return $items;
52 }
53
54 /**
55 * Implementation of hook_block().
56 */
57 function addthis_block($op = 'list', $delta = 0) {
58 if ($op == 'list') {
59 $blocks[0]['info'] = t('AddThis button');
60 return $blocks;
61 }
62 else if ($op == 'view' && user_access('view addthis')) {
63 $block['subject'] = t('AddThis');
64 $block['content'] = _addthis_create_button();
65 return $block;
66 }
67 }
68
69 /**
70 * Internal function to generate code for AddThis button
71 *
72 * @return
73 * String containing html code for the button
74 */
75 function _addthis_create_button($node=NULL, $teaser = FALSE) {
76 global $_addthis_counter;
77 $_addthis_counter++;
78 if ($_addthis_counter == 1) {
79 drupal_add_css((drupal_get_path('module', 'addthis') .'/addthis.css'));
80 drupal_add_js(sprintf('
81 addthis_pub = \'%s\';
82 addthis_logo = \'%s\';
83 addthis_logo_background = \'%s\';
84 addthis_logo_color = \'%s\';
85 addthis_brand = \'%s\';
86 addthis_options = \'%s\';
87 ',
88 variable_get('addthis_username', 'my-username'),
89 variable_get('addthis_logo', 'http://www.addthis.com/images/yourlogo.png'),
90 variable_get('addthis_logo_background', 'EFEFFF'),
91 variable_get('addthis_logo_color', '666699'),
92 variable_get('addthis_brand', 'Your Site'),
93 variable_get('addthis_options', 'favorites, email, digg, delicious, myspace, facebook, google, live, more')
94 ), 'inline');
95 }
96
97 if (variable_get('addthis_dropdown_disabled', '0')) {
98 return ( sprintf('
99 <div class="addthis"><a href="http://www.addthis.com/bookmark.php"
100 onclick="addthis_url = location.href; addthis_title = document.title; return addthis_click(this);">
101 <img src="%s" width="%d" height="%d" %s /></a></div>
102 ',
103 variable_get('addthis_image', 'http://s9.addthis.com/button1-share.gif'),
104 variable_get('addthis_image_width', '125'),
105 variable_get('addthis_image_height', '16'),
106 variable_get('addthis_image_attributes', 'alt=""')
107 ));
108 }
109 else {
110 return ( sprintf('
111 <div class="addthis"><a href="http://www.addthis.com/bookmark.php"
112 onmouseover="return addthis_open(this, \'\', \'%s\', \'%s\')"
113 onmouseout="addthis_close()"
114 onclick="return addthis_sendto()"><img src="%s" width="%d" height="%d" %s /></a></div>
115 <script type="text/javascript" src="http://s7.addthis.com/js/152/addthis_widget.js"></script>
116 ',
117 $teaser ? url('node/'. $node->nid, array('absolute' => 1) ) : '[URL]',
118 $teaser ? addslashes($node->title) : '[TITLE]',
119 variable_get('addthis_image', 'http://s9.addthis.com/button1-share.gif'),
120 variable_get('addthis_image_width', '125'),
121 variable_get('addthis_image_height', '16'),
122 variable_get('addthis_image_attributes', 'alt=""')
123 ));
124 }
125 }