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_formatter';
14 const DEFAULT_NUMBER_OF_PREFERRED_SERVICES
= 4;
15 const FIELD_TYPE
= 'addthis';
16 const MODULE_NAME
= 'addthis';
17 const PERMISSION_ADMINISTER_ADDTHIS
= 'administer addthis';
18 const PERMISSION_ADMINISTER_ADVANCED_ADDTHIS
= 'administer advanced addthis';
19 const STYLE_KEY
= 'addthis_style';
20 const WIDGET_TYPE
= 'addthis_button_widget';
22 // AddThis attribute and parameter names (as defined in AddThis APIs)
23 const PROFILE_ID_QUERY_PARAMETER
= 'pubid';
24 const TITLE_ATTRIBUTE
= 'addthis:title';
26 // Persistent variable keys
27 const BLOCK_WIDGET_TYPE_KEY
= 'addthis_block_widget_type';
28 const BOOKMARK_URL_KEY
= 'addthis_bookmark_url';
29 const CUSTOM_CONFIGURATION_CODE_ENABLED_KEY
= 'addthis_custom_configuration_code_enabled';
30 const CUSTOM_CONFIGURATION_CODE_KEY
= 'addthis_custom_configuration_code';
31 const ENABLED_SERVICES_KEY
= 'addthis_enabled_services';
32 const LARGE_ICONS_ENABLED_KEY
= 'addthis_large_icons_enabled';
33 const NUMBER_OF_PREFERRED_SERVICES_KEY
= 'addthis_number_of_preferred_services';
34 const PROFILE_ID_KEY
= 'addthis_profile_id';
35 const SERVICES_CSS_URL_KEY
= 'addthis_services_css_url';
36 const SERVICES_JSON_URL_KEY
= 'addthis_services_json_url';
37 const UI_HEADER_BACKGROUND_COLOR_KEY
= 'addthis_ui_header_background_color';
38 const UI_HEADER_COLOR_KEY
= 'addthis_ui_header_color';
39 const WIDGET_JS_URL_KEY
= 'addthis_widget_js_url';
42 const DEFAULT_BOOKMARK_URL
= 'http://www.addthis.com/bookmark.php?v=250';
43 const DEFAULT_SERVICES_CSS_URL
= 'http://cache.addthiscdn.com/icons/v1/sprites/services.css';
44 const DEFAULT_SERVICES_JSON_URL
= 'http://cache.addthiscdn.com/services/v1/sharing.en.json';
45 const DEFAULT_WIDGET_JS_URL
= 'http://s7.addthis.com/js/250/addthis_widget.js';
48 const ADMIN_CSS_FILE
= 'addthis.admin.css';
49 const ADMIN_INCLUDE_FILE
= 'includes/addthis.admin.inc';
52 const WIDGET_TYPE_COMPACT_BUTTON
= 'compact_button';
53 const WIDGET_TYPE_DISABLED
= 'disabled';
54 const WIDGET_TYPE_LARGE_BUTTON
= 'large_button';
55 const WIDGET_TYPE_SHARECOUNT
= 'sharecount';
56 const WIDGET_TYPE_TOOLBOX
= 'toolbox';
58 private static
$instance;
63 /* @var MarkupGenerator */
64 private
$markupGenerator;
69 public static
function getInstance() {
70 if (!isset(self
::$instance)) {
71 $addThis = new
AddThis();
72 $addThis->setJson(new
Json());
73 $addThis->setMarkupGenerator(new
MarkupGenerator());
74 self
::$instance = $addThis;
76 return self
::$instance;
79 public
function setJson(Json
$json) {
83 public
function setMarkupGenerator(MarkupGenerator
$markupGenerator) {
84 $this->markupGenerator
= $markupGenerator;
87 public
function getWidgetTypes() {
89 self
::WIDGET_TYPE_DISABLED
=> t('Disabled'),
90 self
::WIDGET_TYPE_COMPACT_BUTTON
=> t('Compact button'),
91 self
::WIDGET_TYPE_LARGE_BUTTON
=> t('Large button'),
92 self
::WIDGET_TYPE_TOOLBOX
=> t('Toolbox'),
93 self
::WIDGET_TYPE_SHARECOUNT
=> t('Sharecount'),
97 public
function getBlockWidgetType() {
98 return variable_get(self
::BLOCK_WIDGET_TYPE_KEY
, self
::WIDGET_TYPE_COMPACT_BUTTON
);
101 public
function getWidgetMarkup($widgetType = '', $entity = NULL
) {
103 if (self
::WIDGET_TYPE_LARGE_BUTTON
== $widgetType) {
104 $markup = $this->getLargeButtonWidgetMarkup($entity);
105 } elseif (self
::WIDGET_TYPE_COMPACT_BUTTON
== $widgetType) {
106 $markup = $this->getCompactButtonWidgetMarkup($entity);
107 } elseif (self
::WIDGET_TYPE_TOOLBOX
== $widgetType) {
108 $markup = $this->getToolboxWidgetMarkup($entity);
109 } elseif (self
::WIDGET_TYPE_SHARECOUNT
== $widgetType) {
110 $markup = $this->getSharecountWidgetMarkup($entity);
115 public
function getProfileId() {
116 return check_plain(variable_get(AddThis
::PROFILE_ID_KEY
));
119 public
function getServicesCssUrl() {
120 return check_url(variable_get(AddThis
::SERVICES_CSS_URL_KEY
, self
::DEFAULT_SERVICES_CSS_URL
));
123 public
function getServicesJsonUrl() {
124 return check_url(variable_get(AddThis
::SERVICES_JSON_URL_KEY
, self
::DEFAULT_SERVICES_JSON_URL
));
127 public
function getServices() {
129 $services = $this->json
->decode($this->getServicesJsonUrl());
130 if ($services != NULL
) {
131 foreach ($services['data'] AS
$service) {
132 $serviceCode = check_plain($service['code']);
133 $serviceName = check_plain($service['name']);
134 $rows[$serviceCode] = '<span class="addthis_service_icon icon_' .
$serviceCode .
'"></span> ' .
$serviceName;
140 public
function getEnabledServices() {
141 return variable_get(self
::ENABLED_SERVICES_KEY
, array());
144 public
function addStylesheets() {
145 drupal_add_css($this->getServicesCssUrl(), 'external');
146 drupal_add_css($this->getAdminCssFilePath(), 'file');
149 public
function addConfigurationOptionsJs() {
150 if ($this->isCustomConfigurationCodeEnabled()) {
151 $javascript = $this->getCustomConfigurationCode();
153 $enabledServices = $this->getServiceNamesAsCommaSeparatedString();
154 $javascript = "var addthis_config = {services_compact: '" .
$enabledServices .
"more'"
155 .
$this->getUiHeaderColorConfigurationOptions()
158 drupal_add_js($javascript, 'inline');
161 public
function areLargeIconsEnabled() {
162 return variable_get(self
::LARGE_ICONS_ENABLED_KEY
, TRUE
);
165 public
function getUiHeaderColor() {
166 return check_plain(variable_get(self
::UI_HEADER_COLOR_KEY
));
169 public
function getUiHeaderBackgroundColor() {
170 return check_plain(variable_get(self
::UI_HEADER_BACKGROUND_COLOR_KEY
));
173 public
function getCustomConfigurationCode() {
174 return variable_get(self
::CUSTOM_CONFIGURATION_CODE_KEY
, self
::DEFAULT_CUSTOM_CONFIGURATION_CODE
);
177 public
function isCustomConfigurationCodeEnabled() {
178 return variable_get(self
::CUSTOM_CONFIGURATION_CODE_ENABLED_KEY
, FALSE
);
181 public
function getBaseWidgetJsUrl() {
182 return check_url(variable_get(self
::WIDGET_JS_URL_KEY
, self
::DEFAULT_WIDGET_JS_URL
));
185 public
function getBaseBookmarkUrl() {
186 return check_url(variable_get(self
::BOOKMARK_URL_KEY
, self
::DEFAULT_BOOKMARK_URL
));
189 public
function getNumberOfPreferredServices() {
190 return variable_get(self
::NUMBER_OF_PREFERRED_SERVICES_KEY
, self
::DEFAULT_NUMBER_OF_PREFERRED_SERVICES
);
193 private
function getLargeButtonWidgetMarkup($entity) {
194 return '<a class="addthis_button" '
195 .
$this->getAddThisAttributesMarkup($entity)
196 .
$this->markupGenerator
->generateAttribute('href', self
::getFullBookmarkUrl())
197 .
'><img src="http://s7.addthis.com/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="'
198 .
t('Bookmark and Share')
199 .
'" style="border:0"/></a>'
200 . self
::getWidgetScriptElement();
203 private
function getCompactButtonWidgetMarkup($entity) {
204 return '<a class="addthis_button" '
205 . self
::getAddThisAttributesMarkup($entity)
206 .
$this->markupGenerator
->generateAttribute('href', $this->getFullBookmarkUrl())
207 .
'><img src="http://s7.addthis.com/static/btn/sm-share-en.gif" width="83" height="16" alt="'
208 .
t('Bookmark and Share')
209 .
'" style="border:0"/></a>'
210 .
$this->getWidgetScriptElement();
213 private
function getToolboxWidgetMarkup($entity) {
214 $markup = '<div class="addthis_toolbox addthis_default_style '
215 .
$this->getLargeButtonsClass()
217 .
$this->getAddThisAttributesMarkup($entity)
219 .
$this->markupGenerator
->generateAttribute('href', $this->getFullBookmarkUrl())
220 .
' class="addthis_button_compact"></a>';
222 $numberOfPreferredServices = self
::getNumberOfPreferredServices();
224 for ($i = 1; $i <= $numberOfPreferredServices; $i++) {
225 $markup .
= "<a class=\"addthis_button_preferred_$i\"></a>";
228 $markup .
= '</div>' .
$this->getWidgetScriptElement();
233 private
function getSharecountWidgetMarkup($entity) {
234 return '<div class="addthis_toolbox addthis_default_style"><a class="addthis_counter" '
235 .
$this->getAddThisAttributesMarkup($entity)
237 .
$this->getWidgetScriptElement();
240 private
function getAddThisAttributesMarkup($entity) {
241 if (is_object($entity)) {
242 return $this->getAddThisTitleAttributeMarkup($entity) .
' ';
247 private
function getAddThisTitleAttributeMarkup($entity) {
248 return $this->markupGenerator
->generateAttribute(
249 self
::TITLE_ATTRIBUTE
, drupal_get_title() .
' - ' .
check_plain($entity->title
)
253 private
function getWidgetScriptElement() {
254 return '<script type="text/javascript" src="' .
$this->getWidgetUrl() .
'"></script>';
257 private
function getUiHeaderColorConfigurationOptions() {
258 $configurationOptions = ',';
259 $uiHeaderColor = $this->getUiHeaderColor();
260 $uiHeaderBackgroundColor = $this->getUiHeaderBackgroundColor();
261 if ($uiHeaderColor != NULL
) {
262 $configurationOptions .
= "ui_header_color: '$uiHeaderColor'";
264 if ($uiHeaderBackgroundColor != NULL
) {
265 $configurationOptions .
= ", ui_header_background: '$uiHeaderBackgroundColor'";
267 return $configurationOptions;
270 private
function getLargeButtonsClass() {
271 return $this->areLargeIconsEnabled() ?
' addthis_32x32_style ' : '';
274 private
function getServiceNamesAsCommaSeparatedString() {
275 $enabledServiceNames = array_values($this->getEnabledServices());
276 $enabledServicesAsCommaSeparatedString = '';
277 foreach ($enabledServiceNames as
$enabledServiceName) {
278 if ($enabledServiceName != '0') {
279 $enabledServicesAsCommaSeparatedString .
= $enabledServiceName .
',';
282 return $enabledServicesAsCommaSeparatedString;
285 private
function getAdminCssFilePath() {
286 return drupal_get_path('module', self
::MODULE_NAME
) .
'/' . self
::ADMIN_CSS_FILE
;
289 private
function getFullBookmarkUrl() {
290 return check_url($this->getBaseBookmarkUrl() .
$this->getProfileIdQueryParameterPrefixedWithAmp());
293 private
function getProfileIdQueryParameter($prefix) {
294 $profileId = $this->getProfileId();
295 return $profileId != NULL ?
$prefix . self
::PROFILE_ID_QUERY_PARAMETER .
'=' .
$profileId : '';
298 private
function getProfileIdQueryParameterPrefixedWithAmp() {
299 return $this->getProfileIdQueryParameter('&');
302 private
function getProfileIdQueryParameterPrefixedWithHash() {
303 return $this->getProfileIdQueryParameter('#');
306 private
function getWidgetUrl() {
307 return check_url($this->getBaseWidgetJsUrl() .
$this->getProfileIdQueryParameterPrefixedWithHash());