Made AddThis widget header text and background colors configurable.
[project/addthis.git] / classes / AddThis.php
1 <?php
2 /**
3 * @file
4 * An AddThis-class.
5 *
6 * @author Jani Palsamäki
7 */
8
9 class AddThis {
10
11 const BLOCK_NAME = 'addthis_block';
12 const DEFAULT_FORMATTER = 'addthis_default';
13 const MODULE_NAME = 'addthis';
14 const STYLE_KEY = 'addthis_style';
15
16 // AddThis attribute and parameter names (as defined in AddThis APIs)
17 const PROFILE_ID_QUERY_PARAMETER = 'pubid';
18 const TITLE_ATTRIBUTE = 'addthis:title';
19
20 // Persistent variable keys
21 const BLOCK_WIDGET_TYPE_KEY = 'addthis_block_widget_type';
22 const BOOKMARK_URL_KEY = 'addthis_bookmark_url';
23 const ENABLED_SERVICES_KEY = 'addthis_enabled_services';
24 const LARGE_ICONS_KEY = 'addthis_large_icons';
25 const PROFILE_ID_KEY = 'addthis_profile_id';
26 const SERVICES_CSS_URL_KEY = 'addthis_services_css_url';
27 const SERVICES_JSON_URL_KEY = 'addthis_services_json_url';
28 const UI_HEADER_BACKGROUND_COLOR_KEY = 'addthis_ui_header_background_color';
29 const UI_HEADER_COLOR_KEY = 'addthis_ui_header_color';
30 const WIDGET_JS_URL_KEY = 'addthis_widget_js_url';
31
32 // External resources
33 const DEFAULT_BOOKMARK_URL = 'http://www.addthis.com/bookmark.php?v=250';
34 const DEFAULT_SERVICES_JSON_URL = 'http://cache.addthiscdn.com/services/v1/sharing.en.json';
35 const DEFAULT_SERVICES_CSS_URL = 'http://cache.addthiscdn.com/icons/v1/sprites/services.css';
36 const DEFAULT_WIDGET_JS_URL = 'http://s7.addthis.com/js/250/addthis_widget.js';
37
38 // Internal resources
39 const ADMIN_CSS_FILE = 'addthis.admin.css';
40 const ADMIN_INCLUDE_FILE = 'addthis.admin.inc';
41
42 // Widget types
43 const WIDGET_TYPE_COMPACT_BUTTON = 'compact_button';
44 const WIDGET_TYPE_DISABLED = 'disabled';
45 const WIDGET_TYPE_LARGE_BUTTON = 'large_button';
46 const WIDGET_TYPE_TOOLBOX = 'toolbox';
47 const WIDGET_TYPE_SHARECOUNT = 'sharecount';
48
49 // Markup constants
50 const AMP_ENTITY = '&amp;';
51 const HASH = '#';
52 const HREF = 'href';
53
54 public static function getWidgetTypes() {
55 return array(
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'),
61 );
62 }
63
64 public static function getBlockWidgetType() {
65 return variable_get(self::BLOCK_WIDGET_TYPE_KEY, self::WIDGET_TYPE_COMPACT_BUTTON);
66 }
67
68 public static function getWidgetMarkup($widgetType = '', $entity = NULL) {
69 switch ($widgetType) {
70 case self::WIDGET_TYPE_LARGE_BUTTON:
71 $markup =
72 '<a class="addthis_button" '
73 . self::getAddThisAttributesMarkup($entity)
74 . MarkupGenerator::generateAttribute(self::HREF, self::getFullBookmarkUrl())
75 . '><img src="http://s7.addthis.com/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="'
76 . t('Bookmark and Share')
77 . '" style="border:0"/></a>'
78 . self::getWidgetScriptElement();
79 break;
80 case self::WIDGET_TYPE_COMPACT_BUTTON:
81 $markup =
82 '<a class="addthis_button" '
83 . self::getAddThisAttributesMarkup($entity)
84 . MarkupGenerator::generateAttribute(self::HREF, self::getFullBookmarkUrl())
85 . '><img src="http://s7.addthis.com/static/btn/sm-share-en.gif" width="83" height="16" alt="'
86 . t('Bookmark and Share')
87 . '" style="border:0"/></a>'
88 . self::getWidgetScriptElement();
89 break;
90 case self::WIDGET_TYPE_TOOLBOX:
91 $markup =
92 '<div class="addthis_toolbox addthis_default_style'
93 . self::getLargeButtonsClass()
94 . '"><a '
95 . MarkupGenerator::generateAttribute(self::HREF, self::getFullBookmarkUrl())
96 . ' class="addthis_button_compact" '
97 . self::getAddThisAttributesMarkup($entity)
98 . '>'
99 . t('Share')
100 . '</a><span class="addthis_separator">|</span>'
101 . '<a class="addthis_button_preferred_1" '
102 . self::getAddThisAttributesMarkup($entity)
103 . '></a>'
104 . '<a class="addthis_button_preferred_2" '
105 . self::getAddThisAttributesMarkup($entity)
106 . '></a>'
107 . '<a class="addthis_button_preferred_3" '
108 . self::getAddThisAttributesMarkup($entity)
109 . '></a>'
110 . '<a class="addthis_button_preferred_4" '
111 . self::getAddThisAttributesMarkup($entity)
112 . '></a></div>'
113 . self::getWidgetScriptElement();
114 break;
115 case self::WIDGET_TYPE_SHARECOUNT:
116 $markup =
117 '<div class="addthis_toolbox addthis_default_style"><a class="addthis_counter" '
118 . self::getAddThisAttributesMarkup($entity)
119 . '></a></div>'
120 . self::getWidgetScriptElement();
121 break;
122 default:
123 $markup = '';
124 break;
125 }
126 return $markup;
127 }
128
129 public static function getProfileId() {
130 return variable_get(AddThis::PROFILE_ID_KEY);
131 }
132
133 public static function getServicesCssUrl() {
134 return variable_get(AddThis::SERVICES_CSS_URL_KEY, self::DEFAULT_SERVICES_CSS_URL);
135 }
136
137 public static function getServicesJsonUrl() {
138 return variable_get(AddThis::SERVICES_JSON_URL_KEY, self::DEFAULT_SERVICES_JSON_URL);
139 }
140
141 public static function getServiceOptions() {
142 return self::getServices();
143 }
144
145 public static function getEnabledServiceOptions() {
146 return self::getEnabledServices();
147 }
148
149 public static function addStylesheets() {
150 drupal_add_css(self::getServicesCssUrl(), 'external');
151 drupal_add_css(self::getAdminCssFilePath(), 'file');
152 }
153
154 public static function addConfigurationOptionsJs() {
155 $enabledServices = self::getServiceNamesAsCommaSeparatedString();
156 drupal_add_js(
157 "var addthis_config = {services_compact: '" . $enabledServices . "more'"
158 . self::getUiHeaderColorConfigurationOptions()
159 . '}', 'inline'
160 );
161 }
162
163 public static function areLargeIconsEnabled() {
164 return variable_get(self::LARGE_ICONS_KEY, FALSE);
165 }
166
167 public static function getUiHeaderColor() {
168 return variable_get(self::UI_HEADER_COLOR_KEY);
169 }
170
171 public static function getUiHeaderBackgroundColor() {
172 return variable_get(self::UI_HEADER_BACKGROUND_COLOR_KEY);
173 }
174
175 private static function getUiHeaderColorConfigurationOptions() {
176 $configurationOptions = ',';
177 $uiHeaderColor = self::getUiHeaderColor();
178 $uiHeaderBackgroundColor = self::getUiHeaderBackgroundColor();
179 if ($uiHeaderColor != NULL) {
180 $configurationOptions .= "ui_header_color: '$uiHeaderColor'";
181 }
182 if ($uiHeaderBackgroundColor != NULL) {
183 $configurationOptions .= ", ui_header_background: '$uiHeaderBackgroundColor'";
184 }
185 return $configurationOptions;
186 }
187
188 private static function getLargeButtonsClass() {
189 return self::areLargeIconsEnabled() ? ' addthis_32x32_style ' : '';
190 }
191
192 private static function getServiceNamesAsCommaSeparatedString() {
193 $enabledServiceNames = array_values(self::getEnabledServices());
194 $enabledServicesAsCommaSeparatedString = '';
195 foreach ($enabledServiceNames as $enabledServiceName) {
196 if ($enabledServiceName != '0') {
197 $enabledServicesAsCommaSeparatedString .= $enabledServiceName . ',';
198 }
199 }
200 return $enabledServicesAsCommaSeparatedString;
201 }
202
203 private static function getAdminCssFilePath() {
204 return drupal_get_path('module', self::MODULE_NAME) . '/' . self::ADMIN_CSS_FILE;
205 }
206
207 private static function getServices() {
208 $rows = array();
209 $json = new Json();
210 $services = $json->decode(self::getServicesJsonUrl());
211 if ($services != NULL) {
212 foreach ($services['data'] AS $service) {
213 $serviceCode = check_plain($service['code']);
214 $serviceName = check_plain($service['name']);
215 $rows[$serviceCode] = '<span class="addthis_service_icon icon_' . $serviceCode . '"></span> ' . $serviceName;
216 }
217 }
218 return $rows;
219 }
220
221 private static function getEnabledServices() {
222 return variable_get(self::ENABLED_SERVICES_KEY, array());
223 }
224
225 public static function getBaseBookmarkUrl() {
226 return variable_get(self::BOOKMARK_URL_KEY, self::DEFAULT_BOOKMARK_URL);
227 }
228
229 private static function getFullBookmarkUrl() {
230 return self::getBaseBookmarkUrl() . self::getProfileIdQueryParameterPrefixedWithAmp();
231 }
232
233 public static function getBaseWidgetJsUrl() {
234 return variable_get(self::WIDGET_JS_URL_KEY, self::DEFAULT_WIDGET_JS_URL);
235 }
236
237 private static function getProfileIdQueryParameter($prefix) {
238 $profileId = self::getProfileId();
239 return $profileId != NULL ? $prefix . self::PROFILE_ID_QUERY_PARAMETER . '=' . $profileId : '';
240 }
241
242 private static function getProfileIdQueryParameterPrefixedWithAmp() {
243 return self::getProfileIdQueryParameter(self::AMP_ENTITY);
244 }
245
246 private static function getProfileIdQueryParameterPrefixedWithHash() {
247 return self::getProfileIdQueryParameter(self::HASH);
248 }
249
250 private static function getAddThisAttributesMarkup($entity) {
251 if (is_object($entity)) {
252 return self::getAddThisTitleAttributeMarkup($entity) . ' ';
253 }
254 return '';
255 }
256
257 private static function getAddThisTitleAttributeMarkup($entity) {
258 return MarkupGenerator::generateAttribute(self::TITLE_ATTRIBUTE, drupal_get_title() . ' - ' . check_plain($entity->title));
259 }
260
261 private static function getWidgetScriptElement() {
262 return '<script type="text/javascript" src="' . self::getWidgetUrl() . '"></script>';
263 }
264
265 private static function getWidgetUrl() {
266 return self::getBaseWidgetJsUrl() . self::getProfileIdQueryParameterPrefixedWithHash();
267 }
268 }