A user may input custom AddThis configuration code in format described at www.addthis...
[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_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';
18
19 // AddThis attribute and parameter names (as defined in AddThis APIs)
20 const PROFILE_ID_QUERY_PARAMETER = 'pubid';
21 const TITLE_ATTRIBUTE = 'addthis:title';
22
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';
36
37 // External resources
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';
42
43 // Internal resources
44 const ADMIN_CSS_FILE = 'addthis.admin.css';
45 const ADMIN_INCLUDE_FILE = 'addthis.admin.inc';
46
47 // Widget types
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';
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 $href = 'href';
70 switch ($widgetType) {
71 case self::WIDGET_TYPE_LARGE_BUTTON:
72 $markup =
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();
80 break;
81 case self::WIDGET_TYPE_COMPACT_BUTTON:
82 $markup =
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();
90 break;
91 case self::WIDGET_TYPE_TOOLBOX:
92 $markup =
93 '<div class="addthis_toolbox addthis_default_style'
94 . self::getLargeButtonsClass()
95 . '"><a '
96 . MarkupGenerator::generateAttribute($href, self::getFullBookmarkUrl())
97 . ' class="addthis_button_compact" '
98 . self::getAddThisAttributesMarkup($entity)
99 . '>'
100 . t('Share')
101 . '</a><span class="addthis_separator">|</span>'
102 . '<a class="addthis_button_preferred_1" '
103 . self::getAddThisAttributesMarkup($entity)
104 . '></a>'
105 . '<a class="addthis_button_preferred_2" '
106 . self::getAddThisAttributesMarkup($entity)
107 . '></a>'
108 . '<a class="addthis_button_preferred_3" '
109 . self::getAddThisAttributesMarkup($entity)
110 . '></a>'
111 . '<a class="addthis_button_preferred_4" '
112 . self::getAddThisAttributesMarkup($entity)
113 . '></a></div>'
114 . self::getWidgetScriptElement();
115 break;
116 case self::WIDGET_TYPE_SHARECOUNT:
117 $markup =
118 '<div class="addthis_toolbox addthis_default_style"><a class="addthis_counter" '
119 . self::getAddThisAttributesMarkup($entity)
120 . '></a></div>'
121 . self::getWidgetScriptElement();
122 break;
123 default:
124 $markup = '';
125 break;
126 }
127 return $markup;
128 }
129
130 public static function getProfileId() {
131 return check_plain(variable_get(AddThis::PROFILE_ID_KEY));
132 }
133
134 public static function getServicesCssUrl() {
135 return check_url(variable_get(AddThis::SERVICES_CSS_URL_KEY, self::DEFAULT_SERVICES_CSS_URL));
136 }
137
138 public static function getServicesJsonUrl() {
139 return check_url(variable_get(AddThis::SERVICES_JSON_URL_KEY, self::DEFAULT_SERVICES_JSON_URL));
140 }
141
142 public static function getServiceOptions() {
143 return self::getServices();
144 }
145
146 public static function getEnabledServiceOptions() {
147 return self::getEnabledServices();
148 }
149
150 public static function addStylesheets() {
151 drupal_add_css(self::getServicesCssUrl(), 'external');
152 drupal_add_css(self::getAdminCssFilePath(), 'file');
153 }
154
155 public static function addConfigurationOptionsJs() {
156 if (self::isCustomConfigurationCodeEnabled()) {
157 $javascript = self::getCustomConfigurationCode();
158 } else {
159 $enabledServices = self::getServiceNamesAsCommaSeparatedString();
160 $javascript =
161 "var addthis_config = {services_compact: '" . $enabledServices . "more'"
162 . self::getUiHeaderColorConfigurationOptions()
163 . '}';
164 }
165 drupal_add_js($javascript, 'inline');
166 }
167
168 public static function areLargeIconsEnabled() {
169 return variable_get(self::LARGE_ICONS_ENABLED_KEY, FALSE);
170 }
171
172 public static function getUiHeaderColor() {
173 return check_plain(variable_get(self::UI_HEADER_COLOR_KEY));
174 }
175
176 public static function getUiHeaderBackgroundColor() {
177 return check_plain(variable_get(self::UI_HEADER_BACKGROUND_COLOR_KEY));
178 }
179
180 public static function getCustomConfigurationCode() {
181 return variable_get(self::CUSTOM_CONFIGURATION_CODE_KEY, self::DEFAULT_CUSTOM_CONFIGURATION_CODE);
182 }
183
184 public static function isCustomConfigurationCodeEnabled() {
185 return variable_get(self::CUSTOM_CONFIGURATION_CODE_ENABLED_KEY, FALSE);
186 }
187
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'";
194 }
195 if ($uiHeaderBackgroundColor != NULL) {
196 $configurationOptions .= ", ui_header_background: '$uiHeaderBackgroundColor'";
197 }
198 return $configurationOptions;
199 }
200
201 private static function getLargeButtonsClass() {
202 return self::areLargeIconsEnabled() ? ' addthis_32x32_style ' : '';
203 }
204
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 . ',';
211 }
212 }
213 return $enabledServicesAsCommaSeparatedString;
214 }
215
216 private static function getAdminCssFilePath() {
217 return drupal_get_path('module', self::MODULE_NAME) . '/' . self::ADMIN_CSS_FILE;
218 }
219
220 private static function getServices() {
221 $rows = array();
222 $json = new Json();
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;
229 }
230 }
231 return $rows;
232 }
233
234 private static function getEnabledServices() {
235 return variable_get(self::ENABLED_SERVICES_KEY, array());
236 }
237
238 public static function getBaseBookmarkUrl() {
239 return check_url(variable_get(self::BOOKMARK_URL_KEY, self::DEFAULT_BOOKMARK_URL));
240 }
241
242 private static function getFullBookmarkUrl() {
243 return check_url(self::getBaseBookmarkUrl() . self::getProfileIdQueryParameterPrefixedWithAmp());
244 }
245
246 public static function getBaseWidgetJsUrl() {
247 return check_url(variable_get(self::WIDGET_JS_URL_KEY, self::DEFAULT_WIDGET_JS_URL));
248 }
249
250 private static function getProfileIdQueryParameter($prefix) {
251 $profileId = self::getProfileId();
252 return $profileId != NULL ? $prefix . self::PROFILE_ID_QUERY_PARAMETER . '=' . $profileId : '';
253 }
254
255 private static function getProfileIdQueryParameterPrefixedWithAmp() {
256 return self::getProfileIdQueryParameter('&');
257 }
258
259 private static function getProfileIdQueryParameterPrefixedWithHash() {
260 return self::getProfileIdQueryParameter('#');
261 }
262
263 private static function getAddThisAttributesMarkup($entity) {
264 if (is_object($entity)) {
265 return self::getAddThisTitleAttributeMarkup($entity) . ' ';
266 }
267 return '';
268 }
269
270 private static function getAddThisTitleAttributeMarkup($entity) {
271 return MarkupGenerator::generateAttribute(self::TITLE_ATTRIBUTE, drupal_get_title() . ' - ' . check_plain($entity->title));
272 }
273
274 private static function getWidgetScriptElement() {
275 return '<script type="text/javascript" src="' . self::getWidgetUrl() . '"></script>';
276 }
277
278 private static function getWidgetUrl() {
279 return check_url(self::getBaseWidgetJsUrl() . self::getProfileIdQueryParameterPrefixedWithHash());
280 }
281 }