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 ADDRESSBOOK_ENABLED_KEY
= 'addthis_addressbook_enabled';
28 const BLOCK_WIDGET_TYPE_KEY
= 'addthis_block_widget_type';
29 const BOOKMARK_URL_KEY
= 'addthis_bookmark_url';
30 const CLICKBACK_TRACKING_ENABLED_KEY
= 'addthis_clickback_tracking_enabled';
31 const CO_BRAND_KEY
= 'addthis_co_brand';
32 const COMPLIANT_508_KEY
= 'addthis_508_compliant';
33 const CUSTOM_CONFIGURATION_CODE_ENABLED_KEY
= 'addthis_custom_configuration_code_enabled';
34 const CUSTOM_CONFIGURATION_CODE_KEY
= 'addthis_custom_configuration_code';
35 const ENABLED_SERVICES_KEY
= 'addthis_enabled_services';
36 const LARGE_ICONS_ENABLED_KEY
= 'addthis_large_icons_enabled';
37 const NUMBER_OF_PREFERRED_SERVICES_KEY
= 'addthis_number_of_preferred_services';
38 const PROFILE_ID_KEY
= 'addthis_profile_id';
39 const SERVICES_CSS_URL_KEY
= 'addthis_services_css_url';
40 const SERVICES_JSON_URL_KEY
= 'addthis_services_json_url';
41 const UI_HEADER_BACKGROUND_COLOR_KEY
= 'addthis_ui_header_background_color';
42 const UI_HEADER_COLOR_KEY
= 'addthis_ui_header_color';
43 const WIDGET_JS_URL_KEY
= 'addthis_widget_js_url';
46 const DEFAULT_BOOKMARK_URL
= 'http://www.addthis.com/bookmark.php?v=250';
47 const DEFAULT_SERVICES_CSS_URL
= 'http://cache.addthiscdn.com/icons/v1/sprites/services.css';
48 const DEFAULT_SERVICES_JSON_URL
= 'http://cache.addthiscdn.com/services/v1/sharing.en.json';
49 const DEFAULT_WIDGET_JS_URL
= 'http://s7.addthis.com/js/250/addthis_widget.js';
52 const ADMIN_CSS_FILE
= 'addthis.admin.css';
53 const ADMIN_INCLUDE_FILE
= 'includes/addthis.admin.inc';
56 const WIDGET_TYPE_COMPACT_BUTTON
= 'compact_button';
57 const WIDGET_TYPE_DISABLED
= 'disabled';
58 const WIDGET_TYPE_LARGE_BUTTON
= 'large_button';
59 const WIDGET_TYPE_SHARECOUNT
= 'sharecount';
60 const WIDGET_TYPE_TOOLBOX
= 'toolbox';
62 private static
$instance;
64 /* @var AddThisConfigurationGenerator */
65 private
$addThisConfigurationGenerator;
70 /* @var MarkupGenerator */
71 private
$markupGenerator;
76 public static
function getInstance() {
77 if (!isset(self
::$instance)) {
78 $addThis = new
AddThis();
79 $addThis->setAddThisConfigurationGenerator(new
AddThisConfigurationGenerator());
80 $addThis->setJson(new
Json());
81 $addThis->setMarkupGenerator(new
MarkupGenerator());
82 self
::$instance = $addThis;
84 return self
::$instance;
87 public
function setAddThisConfigurationGenerator(AddThisConfigurationGenerator
$addThisConfigurationGenerator) {
88 $this->addThisConfigurationGenerator
= $addThisConfigurationGenerator;
91 public
function setJson(Json
$json) {
95 public
function setMarkupGenerator(MarkupGenerator
$markupGenerator) {
96 $this->markupGenerator
= $markupGenerator;
99 public
function getWidgetTypes() {
101 self
::WIDGET_TYPE_DISABLED
=> t('Disabled'),
102 self
::WIDGET_TYPE_COMPACT_BUTTON
=> t('Compact button'),
103 self
::WIDGET_TYPE_LARGE_BUTTON
=> t('Large button'),
104 self
::WIDGET_TYPE_TOOLBOX
=> t('Toolbox'),
105 self
::WIDGET_TYPE_SHARECOUNT
=> t('Sharecount'),
109 public
function getBlockWidgetType() {
110 return variable_get(self
::BLOCK_WIDGET_TYPE_KEY
, self
::WIDGET_TYPE_COMPACT_BUTTON
);
113 public
function getWidgetMarkup($widgetType = '', $entity = NULL
) {
115 if (self
::WIDGET_TYPE_LARGE_BUTTON
== $widgetType) {
116 $markup = $this->getLargeButtonWidgetMarkup($entity);
117 } elseif (self
::WIDGET_TYPE_COMPACT_BUTTON
== $widgetType) {
118 $markup = $this->getCompactButtonWidgetMarkup($entity);
119 } elseif (self
::WIDGET_TYPE_TOOLBOX
== $widgetType) {
120 $markup = $this->getToolboxWidgetMarkup($entity);
121 } elseif (self
::WIDGET_TYPE_SHARECOUNT
== $widgetType) {
122 $markup = $this->getSharecountWidgetMarkup($entity);
127 public
function getProfileId() {
128 return check_plain(variable_get(AddThis
::PROFILE_ID_KEY
));
131 public
function getServicesCssUrl() {
132 return check_url(variable_get(AddThis
::SERVICES_CSS_URL_KEY
, self
::DEFAULT_SERVICES_CSS_URL
));
135 public
function getServicesJsonUrl() {
136 return check_url(variable_get(AddThis
::SERVICES_JSON_URL_KEY
, self
::DEFAULT_SERVICES_JSON_URL
));
139 public
function getServices() {
141 $services = $this->json
->decode($this->getServicesJsonUrl());
142 if (!empty($services)) {
143 foreach ($services['data'] AS
$service) {
144 $serviceCode = check_plain($service['code']);
145 $serviceName = check_plain($service['name']);
146 $rows[$serviceCode] = '<span class="addthis_service_icon icon_' .
$serviceCode .
'"></span> ' .
$serviceName;
152 public
function getEnabledServices() {
153 return variable_get(self
::ENABLED_SERVICES_KEY
, array());
156 public
function addStylesheets() {
157 drupal_add_css($this->getServicesCssUrl(), 'external');
158 drupal_add_css($this->getAdminCssFilePath(), 'file');
161 public
function addWidgetJs() {
162 drupal_add_js(self
::getWidgetUrl(), array('type' => 'external', 'scope' => 'footer'));
165 public
function addConfigurationOptionsJs() {
166 if ($this->isCustomConfigurationCodeEnabled()) {
167 $javascript = $this->getCustomConfigurationCode();
169 $enabledServices = $this->getServiceNamesAsCommaSeparatedString();
170 $javascript = "var addthis_config = {services_compact: '" .
$enabledServices .
"more'"
171 .
$this->addThisConfigurationGenerator
->generate('ui_header_color', $this->getUiHeaderColor())
172 .
$this->addThisConfigurationGenerator
->generate('ui_header_backround', $this->getUiHeaderBackgroundColor())
173 .
$this->addThisConfigurationGenerator
->generate('ui_cobrand', $this->getCoBrand())
174 .
$this->addThisConfigurationGenerator
->generate('ui_508_compliant', $this->get508Compliant())
175 .
$this->addThisConfigurationGenerator
->generate('data_track_clickback', $this->isClickbackTrackingEnabled())
176 .
$this->addThisConfigurationGenerator
->generate('ui_use_addressbook', $this->isAddressbookEnabled())
180 drupal_add_js($javascript, array('type' => 'inline'));
183 public
function areLargeIconsEnabled() {
184 return variable_get(self
::LARGE_ICONS_ENABLED_KEY
, TRUE
);
187 public
function getUiHeaderColor() {
188 return check_plain(variable_get(self
::UI_HEADER_COLOR_KEY
));
191 public
function getUiHeaderBackgroundColor() {
192 return check_plain(variable_get(self
::UI_HEADER_BACKGROUND_COLOR_KEY
));
195 public
function getCustomConfigurationCode() {
196 return variable_get(self
::CUSTOM_CONFIGURATION_CODE_KEY
, self
::DEFAULT_CUSTOM_CONFIGURATION_CODE
);
199 public
function isCustomConfigurationCodeEnabled() {
200 return variable_get(self
::CUSTOM_CONFIGURATION_CODE_ENABLED_KEY
, FALSE
);
203 public
function getBaseWidgetJsUrl() {
204 return check_url(variable_get(self
::WIDGET_JS_URL_KEY
, self
::DEFAULT_WIDGET_JS_URL
));
207 public
function getBaseBookmarkUrl() {
208 return check_url(variable_get(self
::BOOKMARK_URL_KEY
, self
::DEFAULT_BOOKMARK_URL
));
211 public
function getNumberOfPreferredServices() {
212 return variable_get(self
::NUMBER_OF_PREFERRED_SERVICES_KEY
, self
::DEFAULT_NUMBER_OF_PREFERRED_SERVICES
);
215 public
function getCoBrand() {
216 return variable_get(self
::CO_BRAND_KEY
);
219 public
function get508Compliant() {
220 return variable_get(self
::COMPLIANT_508_KEY
, FALSE
);
223 public
function isClickbackTrackingEnabled() {
224 return variable_get(self
::CLICKBACK_TRACKING_ENABLED_KEY
, FALSE
);
227 public
function isAddressbookEnabled() {
228 return variable_get(self
::ADDRESSBOOK_ENABLED_KEY
, FALSE
);
231 private
function getLargeButtonWidgetMarkup($entity) {
232 return '<a class="addthis_button" '
233 .
$this->getAddThisAttributesMarkup($entity)
234 .
$this->markupGenerator
->generateAttribute('href', self
::getFullBookmarkUrl())
235 .
'><img src="http://s7.addthis.com/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="'
236 .
t('Bookmark and Share')
237 .
'" style="border:0"/></a>';
240 private
function getCompactButtonWidgetMarkup($entity) {
241 return '<a class="addthis_button" '
242 . self
::getAddThisAttributesMarkup($entity)
243 .
$this->markupGenerator
->generateAttribute('href', $this->getFullBookmarkUrl())
244 .
'><img src="http://s7.addthis.com/static/btn/sm-share-en.gif" width="83" height="16" alt="'
245 .
t('Bookmark and Share')
246 .
'" style="border:0"/></a>';
249 private
function getToolboxWidgetMarkup($entity) {
250 $markup = '<div class="addthis_toolbox addthis_default_style '
251 .
$this->getLargeButtonsClass()
253 .
$this->getAddThisAttributesMarkup($entity)
255 .
$this->markupGenerator
->generateAttribute('href', $this->getFullBookmarkUrl())
256 .
' class="addthis_button_compact"></a>';
258 $numberOfPreferredServices = self
::getNumberOfPreferredServices();
260 for ($i = 1; $i <= $numberOfPreferredServices; $i++) {
261 $markup .
= "<a class=\"addthis_button_preferred_$i\"></a>";
269 private
function getSharecountWidgetMarkup($entity) {
270 return '<div class="addthis_toolbox addthis_default_style"><a class="addthis_counter" '
271 .
$this->getAddThisAttributesMarkup($entity)
275 private
function getAddThisAttributesMarkup($entity) {
276 if (is_object($entity)) {
277 return $this->getAddThisTitleAttributeMarkup($entity) .
' ';
282 private
function getAddThisTitleAttributeMarkup($entity) {
283 return $this->markupGenerator
->generateAttribute(
284 self
::TITLE_ATTRIBUTE
, drupal_get_title() .
' - ' .
check_plain($entity->title
)
288 private
function getLargeButtonsClass() {
289 return $this->areLargeIconsEnabled() ?
' addthis_32x32_style ' : '';
292 private
function getServiceNamesAsCommaSeparatedString() {
293 $enabledServiceNames = array_values($this->getEnabledServices());
294 $enabledServicesAsCommaSeparatedString = '';
295 foreach ($enabledServiceNames as
$enabledServiceName) {
296 if ($enabledServiceName != '0') {
297 $enabledServicesAsCommaSeparatedString .
= $enabledServiceName .
',';
300 return $enabledServicesAsCommaSeparatedString;
303 private
function getAdminCssFilePath() {
304 return drupal_get_path('module', self
::MODULE_NAME
) .
'/' . self
::ADMIN_CSS_FILE
;
307 private
function getFullBookmarkUrl() {
308 return check_url($this->getBaseBookmarkUrl() .
$this->getProfileIdQueryParameterPrefixedWithAmp());
311 private
function getProfileIdQueryParameter($prefix) {
312 $profileId = $this->getProfileId();
313 return !empty($profileId) ?
$prefix . self
::PROFILE_ID_QUERY_PARAMETER .
'=' .
$profileId : '';
316 private
function getProfileIdQueryParameterPrefixedWithAmp() {
317 return $this->getProfileIdQueryParameter('&');
320 private
function getProfileIdQueryParameterPrefixedWithHash() {
321 return $this->getProfileIdQueryParameter('#');
324 private
function getWidgetUrl() {
325 return check_url($this->getBaseWidgetJsUrl() .
$this->getProfileIdQueryParameterPrefixedWithHash());