6 * @author Jani Palsamäki
11 const BLOCK_NAME
= 'addthis_block';
12 const DEFAULT_CUSTOM_CONFIGURATION_CODE
= 'var addthis_config = {}';
13 const DEFAULT_FORMATTER
= 'addthis_default';
14 const MODULE_NAME
= 'addthis';
15 const PERMISSION_ADMINISTER_ADDTHIS
= 'administer addthis';
16 const PERMISSION_ADMINISTER_ADVANCED_ADDTHIS
= 'administer advanced addthis';
17 const STYLE_KEY
= 'addthis_style';
19 // AddThis attribute and parameter names (as defined in AddThis APIs)
20 const PROFILE_ID_QUERY_PARAMETER
= 'pubid';
21 const TITLE_ATTRIBUTE
= 'addthis:title';
23 // Persistent variable keys
24 const BLOCK_WIDGET_TYPE_KEY
= 'addthis_block_widget_type';
25 const BOOKMARK_URL_KEY
= 'addthis_bookmark_url';
26 const CUSTOM_CONFIGURATION_CODE_ENABLED_KEY
= 'addthis_custom_configuration_code_enabled';
27 const CUSTOM_CONFIGURATION_CODE_KEY
= 'addthis_custom_configuration_code';
28 const ENABLED_SERVICES_KEY
= 'addthis_enabled_services';
29 const LARGE_ICONS_ENABLED_KEY
= 'addthis_large_icons_enabled';
30 const PROFILE_ID_KEY
= 'addthis_profile_id';
31 const SERVICES_CSS_URL_KEY
= 'addthis_services_css_url';
32 const SERVICES_JSON_URL_KEY
= 'addthis_services_json_url';
33 const UI_HEADER_BACKGROUND_COLOR_KEY
= 'addthis_ui_header_background_color';
34 const UI_HEADER_COLOR_KEY
= 'addthis_ui_header_color';
35 const WIDGET_JS_URL_KEY
= 'addthis_widget_js_url';
38 const DEFAULT_BOOKMARK_URL
= 'http://www.addthis.com/bookmark.php?v=250';
39 const DEFAULT_SERVICES_CSS_URL
= 'http://cache.addthiscdn.com/icons/v1/sprites/services.css';
40 const DEFAULT_SERVICES_JSON_URL
= 'http://cache.addthiscdn.com/services/v1/sharing.en.json';
41 const DEFAULT_WIDGET_JS_URL
= 'http://s7.addthis.com/js/250/addthis_widget.js';
44 const ADMIN_CSS_FILE
= 'addthis.admin.css';
45 const ADMIN_INCLUDE_FILE
= 'addthis.admin.inc';
48 const WIDGET_TYPE_COMPACT_BUTTON
= 'compact_button';
49 const WIDGET_TYPE_DISABLED
= 'disabled';
50 const WIDGET_TYPE_LARGE_BUTTON
= 'large_button';
51 const WIDGET_TYPE_SHARECOUNT
= 'sharecount';
52 const WIDGET_TYPE_TOOLBOX
= 'toolbox';
54 public static
function getWidgetTypes() {
56 self
::WIDGET_TYPE_DISABLED
=> t('Disabled'),
57 self
::WIDGET_TYPE_COMPACT_BUTTON
=> t('Compact button'),
58 self
::WIDGET_TYPE_LARGE_BUTTON
=> t('Large button'),
59 self
::WIDGET_TYPE_TOOLBOX
=> t('Toolbox'),
60 self
::WIDGET_TYPE_SHARECOUNT
=> t('Sharecount'),
64 public static
function getBlockWidgetType() {
65 return variable_get(self
::BLOCK_WIDGET_TYPE_KEY
, self
::WIDGET_TYPE_COMPACT_BUTTON
);
68 public static
function getWidgetMarkup($widgetType = '', $entity = NULL
) {
70 switch ($widgetType) {
71 case self
::WIDGET_TYPE_LARGE_BUTTON
:
73 '<a class="addthis_button" '
74 . self
::getAddThisAttributesMarkup($entity)
75 . MarkupGenerator
::generateAttribute($href, self
::getFullBookmarkUrl())
76 .
'><img src="http://s7.addthis.com/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="'
77 .
t('Bookmark and Share')
78 .
'" style="border:0"/></a>'
79 . self
::getWidgetScriptElement();
81 case self
::WIDGET_TYPE_COMPACT_BUTTON
:
83 '<a class="addthis_button" '
84 . self
::getAddThisAttributesMarkup($entity)
85 . MarkupGenerator
::generateAttribute($href, self
::getFullBookmarkUrl())
86 .
'><img src="http://s7.addthis.com/static/btn/sm-share-en.gif" width="83" height="16" alt="'
87 .
t('Bookmark and Share')
88 .
'" style="border:0"/></a>'
89 . self
::getWidgetScriptElement();
91 case self
::WIDGET_TYPE_TOOLBOX
:
93 '<div class="addthis_toolbox addthis_default_style'
94 . self
::getLargeButtonsClass()
96 . MarkupGenerator
::generateAttribute($href, self
::getFullBookmarkUrl())
97 .
' class="addthis_button_compact" '
98 . self
::getAddThisAttributesMarkup($entity)
101 .
'</a><span class="addthis_separator">|</span>'
102 .
'<a class="addthis_button_preferred_1" '
103 . self
::getAddThisAttributesMarkup($entity)
105 .
'<a class="addthis_button_preferred_2" '
106 . self
::getAddThisAttributesMarkup($entity)
108 .
'<a class="addthis_button_preferred_3" '
109 . self
::getAddThisAttributesMarkup($entity)
111 .
'<a class="addthis_button_preferred_4" '
112 . self
::getAddThisAttributesMarkup($entity)
114 . self
::getWidgetScriptElement();
116 case self
::WIDGET_TYPE_SHARECOUNT
:
118 '<div class="addthis_toolbox addthis_default_style"><a class="addthis_counter" '
119 . self
::getAddThisAttributesMarkup($entity)
121 . self
::getWidgetScriptElement();
130 public static
function getProfileId() {
131 return check_plain(variable_get(AddThis
::PROFILE_ID_KEY
));
134 public static
function getServicesCssUrl() {
135 return check_url(variable_get(AddThis
::SERVICES_CSS_URL_KEY
, self
::DEFAULT_SERVICES_CSS_URL
));
138 public static
function getServicesJsonUrl() {
139 return check_url(variable_get(AddThis
::SERVICES_JSON_URL_KEY
, self
::DEFAULT_SERVICES_JSON_URL
));
142 public static
function getServiceOptions() {
143 return self
::getServices();
146 public static
function getEnabledServiceOptions() {
147 return self
::getEnabledServices();
150 public static
function addStylesheets() {
151 drupal_add_css(self
::getServicesCssUrl(), 'external');
152 drupal_add_css(self
::getAdminCssFilePath(), 'file');
155 public static
function addConfigurationOptionsJs() {
156 if (self
::isCustomConfigurationCodeEnabled()) {
157 $javascript = self
::getCustomConfigurationCode();
159 $enabledServices = self
::getServiceNamesAsCommaSeparatedString();
161 "var addthis_config = {services_compact: '" .
$enabledServices .
"more'"
162 . self
::getUiHeaderColorConfigurationOptions()
165 drupal_add_js($javascript, 'inline');
168 public static
function areLargeIconsEnabled() {
169 return variable_get(self
::LARGE_ICONS_ENABLED_KEY
, FALSE
);
172 public static
function getUiHeaderColor() {
173 return check_plain(variable_get(self
::UI_HEADER_COLOR_KEY
));
176 public static
function getUiHeaderBackgroundColor() {
177 return check_plain(variable_get(self
::UI_HEADER_BACKGROUND_COLOR_KEY
));
180 public static
function getCustomConfigurationCode() {
181 return variable_get(self
::CUSTOM_CONFIGURATION_CODE_KEY
, self
::DEFAULT_CUSTOM_CONFIGURATION_CODE
);
184 public static
function isCustomConfigurationCodeEnabled() {
185 return variable_get(self
::CUSTOM_CONFIGURATION_CODE_ENABLED_KEY
, FALSE
);
188 private static
function getUiHeaderColorConfigurationOptions() {
189 $configurationOptions = ',';
190 $uiHeaderColor = self
::getUiHeaderColor();
191 $uiHeaderBackgroundColor = self
::getUiHeaderBackgroundColor();
192 if ($uiHeaderColor != NULL
) {
193 $configurationOptions .
= "ui_header_color: '$uiHeaderColor'";
195 if ($uiHeaderBackgroundColor != NULL
) {
196 $configurationOptions .
= ", ui_header_background: '$uiHeaderBackgroundColor'";
198 return $configurationOptions;
201 private static
function getLargeButtonsClass() {
202 return self
::areLargeIconsEnabled() ?
' addthis_32x32_style ' : '';
205 private static
function getServiceNamesAsCommaSeparatedString() {
206 $enabledServiceNames = array_values(self
::getEnabledServices());
207 $enabledServicesAsCommaSeparatedString = '';
208 foreach ($enabledServiceNames as
$enabledServiceName) {
209 if ($enabledServiceName != '0') {
210 $enabledServicesAsCommaSeparatedString .
= $enabledServiceName .
',';
213 return $enabledServicesAsCommaSeparatedString;
216 private static
function getAdminCssFilePath() {
217 return drupal_get_path('module', self
::MODULE_NAME
) .
'/' . self
::ADMIN_CSS_FILE
;
220 private static
function getServices() {
223 $services = $json->decode(self
::getServicesJsonUrl());
224 if ($services != NULL
) {
225 foreach ($services['data'] AS
$service) {
226 $serviceCode = check_plain($service['code']);
227 $serviceName = check_plain($service['name']);
228 $rows[$serviceCode] = '<span class="addthis_service_icon icon_' .
$serviceCode .
'"></span> ' .
$serviceName;
234 private static
function getEnabledServices() {
235 return variable_get(self
::ENABLED_SERVICES_KEY
, array());
238 public static
function getBaseBookmarkUrl() {
239 return check_url(variable_get(self
::BOOKMARK_URL_KEY
, self
::DEFAULT_BOOKMARK_URL
));
242 private static
function getFullBookmarkUrl() {
243 return check_url(self
::getBaseBookmarkUrl() . self
::getProfileIdQueryParameterPrefixedWithAmp());
246 public static
function getBaseWidgetJsUrl() {
247 return check_url(variable_get(self
::WIDGET_JS_URL_KEY
, self
::DEFAULT_WIDGET_JS_URL
));
250 private static
function getProfileIdQueryParameter($prefix) {
251 $profileId = self
::getProfileId();
252 return $profileId != NULL ?
$prefix . self
::PROFILE_ID_QUERY_PARAMETER .
'=' .
$profileId : '';
255 private static
function getProfileIdQueryParameterPrefixedWithAmp() {
256 return self
::getProfileIdQueryParameter('&');
259 private static
function getProfileIdQueryParameterPrefixedWithHash() {
260 return self
::getProfileIdQueryParameter('#');
263 private static
function getAddThisAttributesMarkup($entity) {
264 if (is_object($entity)) {
265 return self
::getAddThisTitleAttributeMarkup($entity) .
' ';
270 private static
function getAddThisTitleAttributeMarkup($entity) {
271 return MarkupGenerator
::generateAttribute(self
::TITLE_ATTRIBUTE
, drupal_get_title() .
' - ' .
check_plain($entity->title
));
274 private static
function getWidgetScriptElement() {
275 return '<script type="text/javascript" src="' . self
::getWidgetUrl() .
'"></script>';
278 private static
function getWidgetUrl() {
279 return check_url(self
::getBaseWidgetJsUrl() . self
::getProfileIdQueryParameterPrefixedWithHash());