Made number of preferred services configurable (toolbox specific setting).
[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_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';
21
22 // AddThis attribute and parameter names (as defined in AddThis APIs)
23 const PROFILE_ID_QUERY_PARAMETER = 'pubid';
24 const TITLE_ATTRIBUTE = 'addthis:title';
25
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';
40
41 // External resources
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';
46
47 // Internal resources
48 const ADMIN_CSS_FILE = 'addthis.admin.css';
49 const ADMIN_INCLUDE_FILE = 'includes/addthis.admin.inc';
50
51 // Widget types
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';
57
58 private static $instance;
59
60 /* @var Json */
61 private $json;
62
63 /* @var MarkupGenerator */
64 private $markupGenerator;
65
66 /**
67 * @return AddThis
68 */
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;
75 }
76 return self::$instance;
77 }
78
79 public function setJson(Json $json) {
80 $this->json = $json;
81 }
82
83 public function setMarkupGenerator(MarkupGenerator $markupGenerator) {
84 $this->markupGenerator = $markupGenerator;
85 }
86
87 public function getWidgetTypes() {
88 return array(
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'),
94 );
95 }
96
97 public function getBlockWidgetType() {
98 return variable_get(self::BLOCK_WIDGET_TYPE_KEY, self::WIDGET_TYPE_COMPACT_BUTTON);
99 }
100
101 public function getWidgetMarkup($widgetType = '', $entity = NULL) {
102 $markup = '';
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);
111 }
112 return $markup;
113 }
114
115 public function getProfileId() {
116 return check_plain(variable_get(AddThis::PROFILE_ID_KEY));
117 }
118
119 public function getServicesCssUrl() {
120 return check_url(variable_get(AddThis::SERVICES_CSS_URL_KEY, self::DEFAULT_SERVICES_CSS_URL));
121 }
122
123 public function getServicesJsonUrl() {
124 return check_url(variable_get(AddThis::SERVICES_JSON_URL_KEY, self::DEFAULT_SERVICES_JSON_URL));
125 }
126
127 public function getServices() {
128 $rows = array();
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;
135 }
136 }
137 return $rows;
138 }
139
140 public function getEnabledServices() {
141 return variable_get(self::ENABLED_SERVICES_KEY, array());
142 }
143
144 public function addStylesheets() {
145 drupal_add_css($this->getServicesCssUrl(), 'external');
146 drupal_add_css($this->getAdminCssFilePath(), 'file');
147 }
148
149 public function addConfigurationOptionsJs() {
150 if ($this->isCustomConfigurationCodeEnabled()) {
151 $javascript = $this->getCustomConfigurationCode();
152 } else {
153 $enabledServices = $this->getServiceNamesAsCommaSeparatedString();
154 $javascript = "var addthis_config = {services_compact: '" . $enabledServices . "more'"
155 . $this->getUiHeaderColorConfigurationOptions()
156 . '}';
157 }
158 drupal_add_js($javascript, 'inline');
159 }
160
161 public function areLargeIconsEnabled() {
162 return variable_get(self::LARGE_ICONS_ENABLED_KEY, TRUE);
163 }
164
165 public function getUiHeaderColor() {
166 return check_plain(variable_get(self::UI_HEADER_COLOR_KEY));
167 }
168
169 public function getUiHeaderBackgroundColor() {
170 return check_plain(variable_get(self::UI_HEADER_BACKGROUND_COLOR_KEY));
171 }
172
173 public function getCustomConfigurationCode() {
174 return variable_get(self::CUSTOM_CONFIGURATION_CODE_KEY, self::DEFAULT_CUSTOM_CONFIGURATION_CODE);
175 }
176
177 public function isCustomConfigurationCodeEnabled() {
178 return variable_get(self::CUSTOM_CONFIGURATION_CODE_ENABLED_KEY, FALSE);
179 }
180
181 public function getBaseWidgetJsUrl() {
182 return check_url(variable_get(self::WIDGET_JS_URL_KEY, self::DEFAULT_WIDGET_JS_URL));
183 }
184
185 public function getBaseBookmarkUrl() {
186 return check_url(variable_get(self::BOOKMARK_URL_KEY, self::DEFAULT_BOOKMARK_URL));
187 }
188
189 public function getNumberOfPreferredServices() {
190 return variable_get(self::NUMBER_OF_PREFERRED_SERVICES_KEY, self::DEFAULT_NUMBER_OF_PREFERRED_SERVICES);
191 }
192
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();
201 }
202
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();
211 }
212
213 private function getToolboxWidgetMarkup($entity) {
214 $markup = '<div class="addthis_toolbox addthis_default_style '
215 . $this->getLargeButtonsClass()
216 . '" '
217 . $this->getAddThisAttributesMarkup($entity)
218 . '><a '
219 . $this->markupGenerator->generateAttribute('href', $this->getFullBookmarkUrl())
220 . ' class="addthis_button_compact"></a>';
221
222 $numberOfPreferredServices = self::getNumberOfPreferredServices();
223
224 for ($i = 1; $i <= $numberOfPreferredServices; $i++) {
225 $markup .= "<a class=\"addthis_button_preferred_$i\"></a>";
226 }
227
228 $markup .= '</div>' . $this->getWidgetScriptElement();
229
230 return $markup;
231 }
232
233 private function getSharecountWidgetMarkup($entity) {
234 return '<div class="addthis_toolbox addthis_default_style"><a class="addthis_counter" '
235 . $this->getAddThisAttributesMarkup($entity)
236 . '></a></div>'
237 . $this->getWidgetScriptElement();
238 }
239
240 private function getAddThisAttributesMarkup($entity) {
241 if (is_object($entity)) {
242 return $this->getAddThisTitleAttributeMarkup($entity) . ' ';
243 }
244 return '';
245 }
246
247 private function getAddThisTitleAttributeMarkup($entity) {
248 return $this->markupGenerator->generateAttribute(
249 self::TITLE_ATTRIBUTE, drupal_get_title() . ' - ' . check_plain($entity->title)
250 );
251 }
252
253 private function getWidgetScriptElement() {
254 return '<script type="text/javascript" src="' . $this->getWidgetUrl() . '"></script>';
255 }
256
257 private function getUiHeaderColorConfigurationOptions() {
258 $configurationOptions = ',';
259 $uiHeaderColor = $this->getUiHeaderColor();
260 $uiHeaderBackgroundColor = $this->getUiHeaderBackgroundColor();
261 if ($uiHeaderColor != NULL) {
262 $configurationOptions .= "ui_header_color: '$uiHeaderColor'";
263 }
264 if ($uiHeaderBackgroundColor != NULL) {
265 $configurationOptions .= ", ui_header_background: '$uiHeaderBackgroundColor'";
266 }
267 return $configurationOptions;
268 }
269
270 private function getLargeButtonsClass() {
271 return $this->areLargeIconsEnabled() ? ' addthis_32x32_style ' : '';
272 }
273
274 private function getServiceNamesAsCommaSeparatedString() {
275 $enabledServiceNames = array_values($this->getEnabledServices());
276 $enabledServicesAsCommaSeparatedString = '';
277 foreach ($enabledServiceNames as $enabledServiceName) {
278 if ($enabledServiceName != '0') {
279 $enabledServicesAsCommaSeparatedString .= $enabledServiceName . ',';
280 }
281 }
282 return $enabledServicesAsCommaSeparatedString;
283 }
284
285 private function getAdminCssFilePath() {
286 return drupal_get_path('module', self::MODULE_NAME) . '/' . self::ADMIN_CSS_FILE;
287 }
288
289 private function getFullBookmarkUrl() {
290 return check_url($this->getBaseBookmarkUrl() . $this->getProfileIdQueryParameterPrefixedWithAmp());
291 }
292
293 private function getProfileIdQueryParameter($prefix) {
294 $profileId = $this->getProfileId();
295 return $profileId != NULL ? $prefix . self::PROFILE_ID_QUERY_PARAMETER . '=' . $profileId : '';
296 }
297
298 private function getProfileIdQueryParameterPrefixedWithAmp() {
299 return $this->getProfileIdQueryParameter('&');
300 }
301
302 private function getProfileIdQueryParameterPrefixedWithHash() {
303 return $this->getProfileIdQueryParameter('#');
304 }
305
306 private function getWidgetUrl() {
307 return check_url($this->getBaseWidgetJsUrl() . $this->getProfileIdQueryParameterPrefixedWithHash());
308 }
309 }