Modified implementation of hook_link not to generate actual link and changed default...
[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') && $teaser ? variable_get('addthis_display_in_teasers', '0') : variable_get('addthis_display_in_links', '0') ) {
25 $links['addthis'] = array(
26 'title' => _addthis_create_button($node,$teaser),
27 'html' => TRUE,
28 );
29 }
30
31 return $links;
32 }
33 /**
34 * Implementation of hook_menu().
35 */
36 function addthis_menu() {
37 $items = array();
38
39 $items['admin/settings/addthis'] = array(
40 'title' => t('AddThis module settings'),
41 'description' => t('Set username and customize look and feel for <a href="http://www.addthis.com/">AddThis</a> button.'),
42 'page callback' => 'drupal_get_form',
43 'page arguments' => array('addthis_admin_settings'),
44 'access arguments' => array('administer addthis'),
45 );
46
47 return $items;
48 }
49
50 /**
51 * Administration settings form.
52 *
53 * @return
54 * The completed form definition.
55 */
56 function addthis_admin_settings() {
57 $form = array();
58
59 $form['addthis_general_settings'] = array(
60 '#type' => 'fieldset',
61 '#title' => t('General settings'),
62 );
63 $form['addthis_general_settings']['addthis_username'] = array(
64 '#type' => 'textfield',
65 '#title' => t('Username'),
66 '#default_value' => variable_get('addthis_username', 'my-username'),
67 '#description' => t('Your username for <a href="http://www.addthis.com/">addthis.com</a>. Example: my-username'),
68 );
69 $form['addthis_general_settings']['addthis_display_in_links'] = array(
70 '#type' => 'checkbox',
71 '#title' => t('Display on node pages'),
72 '#default_value' => variable_get('addthis_display_in_links', '0'),
73 '#description' => t('Display an AddThis button always on a node page\'s links section.'),
74 );
75 $form['addthis_general_settings']['addthis_display_in_teasers'] = array(
76 '#type' => 'checkbox',
77 '#title' => t('Display in node teasers'),
78 '#default_value' => variable_get('addthis_display_in_teasers', '0'),
79 '#description' => t('Display an AddThis button in the node teasers.'),
80 );
81
82 $form['addthis_button_settings'] = array(
83 '#type' => 'fieldset',
84 '#title' => t('Button image settings'),
85 '#collapsible' => TRUE,
86 '#collapsed' => TRUE,
87 );
88 $form['addthis_button_settings']['addthis_image'] = array(
89 '#type' => 'textfield',
90 '#title' => t('Button image url'),
91 '#default_value' => variable_get('addthis_image', 'http://s9.addthis.com/button1-share.gif'),
92 '#description' => t('URL for small image in the button. Example: http://s9.addthis.com/button1-share.gif'),
93 );
94 $form['addthis_button_settings']['addthis_image_width'] = array(
95 '#type' => 'textfield',
96 '#title' => t('Button image width'),
97 '#default_value' => variable_get('addthis_image_width', '125'),
98 '#description' => t('Width for button image. Example: 125'),
99 );
100 $form['addthis_button_settings']['addthis_image_height'] = array(
101 '#type' => 'textfield',
102 '#title' => t('Button image height'),
103 '#default_value' => variable_get('addthis_image_height', '16'),
104 '#description' => t('Height for button image. Example: 16'),
105 );
106 $form['addthis_button_settings']['addthis_image_attributes'] = array(
107 '#type' => 'textfield',
108 '#title' => t('Button image HTML attributes'),
109 '#default_value' => variable_get('addthis_image_attributes', 'alt=""'),
110 '#description' => t('Extra HTML attributes for img tag. Example: alt=""'),
111 );
112
113 $form['addthis_widget_settings'] = array(
114 '#type' => 'fieldset',
115 '#title' => t('Widget settings'),
116 '#collapsible' => TRUE,
117 '#collapsed' => TRUE,
118 );
119 $form['addthis_widget_settings']['addthis_logo'] = array(
120 '#type' => 'textfield',
121 '#title' => t('Logo url'),
122 '#default_value' => variable_get('addthis_logo', 'http://www.addthis.com/images/yourlogo.png'),
123 '#description' => t('The logo to display on the popup window (about 200x50 pixels). The popup window is show when the user selects the \'More\' choice. Example: http://www.your-website.com/img/mylogo.gif'),
124 );
125 $form['addthis_widget_settings']['addthis_logo_background'] = array(
126 '#type' => 'textfield',
127 '#title' => t('Logo background color'),
128 '#default_value' => variable_get('addthis_logo_background', 'EFEFFF'),
129 '#description' => t('The color to use as a background around the logo in the popup. Example: EFEFEF'),
130 );
131 $form['addthis_widget_settings']['addthis_logo_color'] = array(
132 '#type' => 'textfield',
133 '#title' => t('Logo text color'),
134 '#default_value' => variable_get('addthis_logo_color', '666699'),
135 '#description' => t('The color to use for the text next to the logo in the popup. Example: 666666'),
136 );
137 $form['addthis_widget_settings']['addthis_brand'] = array(
138 '#type' => 'textfield',
139 '#title' => t('Brand text'),
140 '#default_value' => variable_get('addthis_brand', 'Your Site'),
141 '#description' => t('The brand name to display in the drop-down (top right). Example: My Website'),
142 );
143 $form['addthis_widget_settings']['addthis_options'] = array(
144 '#type' => 'textfield',
145 '#title' => t('Options'),
146 '#default_value' => variable_get('addthis_options', 'favorites, email, digg, delicious, myspace, facebook, google, live, more'),
147 '#description' => t('A comma-separated ordered list of options to include in the drop-down. Example: favorites, email, digg, delicious, more<br/>Currently supported options:<br/>
148 delicious, digg, email, favorites, facebook, fark, furl, google, live, myweb, myspace, newsvine, reddit, slashdot, stumbleupon, technorati, twitter, more'),
149 );
150 $form['addthis_widget_settings']['addthis_offset_top'] = array(
151 '#type' => 'textfield',
152 '#title' => t('Offset top'),
153 '#default_value' => variable_get('addthis_offset_top', '2'),
154 '#description' => t('Vertical offset for the drop-down window (in pixels). Example: 2'),
155 );
156 $form['addthis_widget_settings']['addthis_offset_left'] = array(
157 '#type' => 'textfield',
158 '#title' => t('Offset top'),
159 '#default_value' => variable_get('addthis_offset_left', '2'),
160 '#description' => t('Horizontal offset for the drop-down window (in pixels). Example: 2'),
161 );
162 return system_settings_form($form);
163 }
164
165 /**
166 * Implementation of hook_block().
167 */
168 function addthis_block($op = 'list', $delta = 0) {
169 if ($op == 'list') {
170 $blocks[0]['info'] = t('AddThis button');
171 return $blocks;
172 }
173 else if ($op == 'view' && user_access('view addthis')) {
174 $block['subject'] = t('AddThis');
175 $block['content'] = _addthis_create_button();
176 return $block;
177 }
178 }
179
180 /**
181 * Internal function to generate code for AddThis button
182 *
183 * @return
184 * String containing html code for the button
185 */
186 function _addthis_create_button($node=NULL, $teaser = FALSE) {
187 drupal_add_css((drupal_get_path('module', 'addthis') .'/addthis.css'), $type = 'module', $media = 'all', $preprocess = TRUE);
188 return ( sprintf('
189 <script type="text/javascript">
190 addthis_pub = \'%s\';
191 addthis_logo = \'%s\';
192 addthis_logo_background = \'%s\';
193 addthis_logo_color = \'%s\';
194 addthis_brand = \'%s\';
195 addthis_options = \'%s\';
196 addthis_offset_top = \'%d\';
197 addthis_offset_left = \'%d\';
198 </script>
199 <div class="addthis"><a href="http://www.addthis.com/bookmark.php"
200 onmouseover="return addthis_open(this, \'\', \'%s\', \'%s\')"
201 onmouseout="addthis_close()"
202 onclick="return addthis_sendto()"><img src="%s" width="%d" height="%d" %s /></a></div>
203 <script type="text/javascript" src="http://s7.addthis.com/js/152/addthis_widget.js"></script>
204 ',
205 variable_get('addthis_user', 'my-username'),
206 variable_get('addthis_logo', 'http://www.addthis.com/images/yourlogo.png'),
207 variable_get('addthis_logo_background', 'EFEFFF'),
208 variable_get('addthis_logo_color', '666699'),
209 variable_get('addthis_brand', 'Your Site'),
210 variable_get('addthis_options', 'favorites, email, digg, delicious, myspace, facebook, google, live, more'),
211 variable_get('addthis_offset_top', '2'),
212 variable_get('addthis_offset_left', '2'),
213 $teaser ? url('node/'.$node->nid, array('absolute' => 1) ) : '[URL]',
214 $teaser ? addslashes($node->title) : '[TITLE]',
215 variable_get('addthis_image', 'http://s9.addthis.com/button1-share.gif'),
216 variable_get('addthis_image_width', '125'),
217 variable_get('addthis_image_height', '16'),
218 variable_get('addthis_image_attributes', 'alt=""')
219 ));
220 }