Minor cleanup.
[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_CSS_URL = 'http://cache.addthiscdn.com/icons/v1/sprites/services.css';
35 const DEFAULT_SERVICES_JSON_URL = 'http://cache.addthiscdn.com/services/v1/sharing.en.json';
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_SHARECOUNT = 'sharecount';
47 const WIDGET_TYPE_TOOLBOX = 'toolbox';
48
49 public static function getWidgetTypes() {
50 return array(
51 self::WIDGET_TYPE_DISABLED => t('Disabled'),
52 self::WIDGET_TYPE_COMPACT_BUTTON => t('Compact button'),
53 self::WIDGET_TYPE_LARGE_BUTTON => t('Large button'),
54 self::WIDGET_TYPE_TOOLBOX => t('Toolbox'),
55 self::WIDGET_TYPE_SHARECOUNT => t('Sharecount'),
56 );
57 }
58
59 public static function getBlockWidgetType() {
60 return variable_get(self::BLOCK_WIDGET_TYPE_KEY, self::WIDGET_TYPE_COMPACT_BUTTON);
61 }
62
63 public static function getWidgetMarkup($widgetType = '', $entity = NULL) {
64 $href = 'href';
65 switch ($widgetType) {
66 case self::WIDGET_TYPE_LARGE_BUTTON:
67 $markup =
68 '<a class="addthis_button" '
69 . self::getAddThisAttributesMarkup($entity)
70 . MarkupGenerator::generateAttribute($href, self::getFullBookmarkUrl())
71 . '><img src="http://s7.addthis.com/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="'
72 . t('Bookmark and Share')
73 . '" style="border:0"/></a>'
74 . self::getWidgetScriptElement();
75 break;
76 case self::WIDGET_TYPE_COMPACT_BUTTON:
77 $markup =
78 '<a class="addthis_button" '
79 . self::getAddThisAttributesMarkup($entity)
80 . MarkupGenerator::generateAttribute($href, self::getFullBookmarkUrl())
81 . '><img src="http://s7.addthis.com/static/btn/sm-share-en.gif" width="83" height="16" alt="'
82 . t('Bookmark and Share')
83 . '" style="border:0"/></a>'
84 . self::getWidgetScriptElement();
85 break;
86 case self::WIDGET_TYPE_TOOLBOX:
87 $markup =
88 '<div class="addthis_toolbox addthis_default_style'
89 . self::getLargeButtonsClass()
90 . '"><a '
91 . MarkupGenerator::generateAttribute($href, self::getFullBookmarkUrl())
92 . ' class="addthis_button_compact" '
93 . self::getAddThisAttributesMarkup($entity)
94 . '>'
95 . t('Share')
96 . '</a><span class="addthis_separator">|</span>'
97 . '<a class="addthis_button_preferred_1" '
98 . self::getAddThisAttributesMarkup($entity)
99 . '></a>'
100 . '<a class="addthis_button_preferred_2" '
101 . self::getAddThisAttributesMarkup($entity)
102 . '></a>'
103 . '<a class="addthis_button_preferred_3" '
104 . self::getAddThisAttributesMarkup($entity)
105 . '></a>'
106 . '<a class="addthis_button_preferred_4" '
107 . self::getAddThisAttributesMarkup($entity)
108 . '></a></div>'
109 . self::getWidgetScriptElement();
110 break;
111 case self::WIDGET_TYPE_SHARECOUNT:
112 $markup =
113 '<div class="addthis_toolbox addthis_default_style"><a class="addthis_counter" '
114 . self::getAddThisAttributesMarkup($entity)
115 . '></a></div>'
116 . self::getWidgetScriptElement();
117 break;
118 default:
119 $markup = '';
120 break;
121 }
122 return $markup;
123 }
124
125 public static function getProfileId() {
126 return check_plain(variable_get(AddThis::PROFILE_ID_KEY));
127 }
128
129 public static function getServicesCssUrl() {
130 return check_url(variable_get(AddThis::SERVICES_CSS_URL_KEY, self::DEFAULT_SERVICES_CSS_URL));
131 }
132
133 public static function getServicesJsonUrl() {
134 return check_url(variable_get(AddThis::SERVICES_JSON_URL_KEY, self::DEFAULT_SERVICES_JSON_URL));
135 }
136
137 public static function getServiceOptions() {
138 return self::getServices();
139 }
140
141 public static function getEnabledServiceOptions() {
142 return self::getEnabledServices();
143 }
144
145 public static function addStylesheets() {
146 drupal_add_css(self::getServicesCssUrl(), 'external');
147 drupal_add_css(self::getAdminCssFilePath(), 'file');
148 }
149
150 public static function addConfigurationOptionsJs() {
151 $enabledServices = self::getServiceNamesAsCommaSeparatedString();
152 drupal_add_js(
153 "var addthis_config = {services_compact: '" . $enabledServices . "more'"
154 . self::getUiHeaderColorConfigurationOptions()
155 . '}', 'inline'
156 );
157 }
158
159 public static function areLargeIconsEnabled() {
160 return variable_get(self::LARGE_ICONS_KEY, FALSE);
161 }
162
163 public static function getUiHeaderColor() {
164 return check_plain(variable_get(self::UI_HEADER_COLOR_KEY));
165 }
166
167 public static function getUiHeaderBackgroundColor() {
168 return check_plain(variable_get(self::UI_HEADER_BACKGROUND_COLOR_KEY));
169 }
170
171 private static function getUiHeaderColorConfigurationOptions() {
172 $configurationOptions = ',';
173 $uiHeaderColor = self::getUiHeaderColor();
174 $uiHeaderBackgroundColor = self::getUiHeaderBackgroundColor();
175 if ($uiHeaderColor != NULL) {
176 $configurationOptions .= "ui_header_color: '$uiHeaderColor'";
177 }
178 if ($uiHeaderBackgroundColor != NULL) {
179 $configurationOptions .= ", ui_header_background: '$uiHeaderBackgroundColor'";
180 }
181 return $configurationOptions;
182 }
183
184 private static function getLargeButtonsClass() {
185 return self::areLargeIconsEnabled() ? ' addthis_32x32_style ' : '';
186 }
187
188 private static function getServiceNamesAsCommaSeparatedString() {
189 $enabledServiceNames = array_values(self::getEnabledServices());
190 $enabledServicesAsCommaSeparatedString = '';
191 foreach ($enabledServiceNames as $enabledServiceName) {
192 if ($enabledServiceName != '0') {
193 $enabledServicesAsCommaSeparatedString .= $enabledServiceName . ',';
194 }
195 }
196 return $enabledServicesAsCommaSeparatedString;
197 }
198
199 private static function getAdminCssFilePath() {
200 return drupal_get_path('module', self::MODULE_NAME) . '/' . self::ADMIN_CSS_FILE;
201 }
202
203 private static function getServices() {
204 $rows = array();
205 $json = new Json();
206 $services = $json->decode(self::getServicesJsonUrl());
207 if ($services != NULL) {
208 foreach ($services['data'] AS $service) {
209 $serviceCode = check_plain($service['code']);
210 $serviceName = check_plain($service['name']);
211 $rows[$serviceCode] = '<span class="addthis_service_icon icon_' . $serviceCode . '"></span> ' . $serviceName;
212 }
213 }
214 return $rows;
215 }
216
217 private static function getEnabledServices() {
218 return variable_get(self::ENABLED_SERVICES_KEY, array());
219 }
220
221 public static function getBaseBookmarkUrl() {
222 return check_url(variable_get(self::BOOKMARK_URL_KEY, self::DEFAULT_BOOKMARK_URL));
223 }
224
225 private static function getFullBookmarkUrl() {
226 return check_url(self::getBaseBookmarkUrl() . self::getProfileIdQueryParameterPrefixedWithAmp());
227 }
228
229 public static function getBaseWidgetJsUrl() {
230 return check_url(variable_get(self::WIDGET_JS_URL_KEY, self::DEFAULT_WIDGET_JS_URL));
231 }
232
233 private static function getProfileIdQueryParameter($prefix) {
234 $profileId = self::getProfileId();
235 return $profileId != NULL ? $prefix . self::PROFILE_ID_QUERY_PARAMETER . '=' . $profileId : '';
236 }
237
238 private static function getProfileIdQueryParameterPrefixedWithAmp() {
239 return self::getProfileIdQueryParameter('&');
240 }
241
242 private static function getProfileIdQueryParameterPrefixedWithHash() {
243 return self::getProfileIdQueryParameter('#');
244 }
245
246 private static function getAddThisAttributesMarkup($entity) {
247 if (is_object($entity)) {
248 return self::getAddThisTitleAttributeMarkup($entity) . ' ';
249 }
250 return '';
251 }
252
253 private static function getAddThisTitleAttributeMarkup($entity) {
254 return MarkupGenerator::generateAttribute(self::TITLE_ATTRIBUTE, drupal_get_title() . ' - ' . check_plain($entity->title));
255 }
256
257 private static function getWidgetScriptElement() {
258 return '<script type="text/javascript" src="' . self::getWidgetUrl() . '"></script>';
259 }
260
261 private static function getWidgetUrl() {
262 return check_url(self::getBaseWidgetJsUrl() . self::getProfileIdQueryParameterPrefixedWithHash());
263 }
264 }