Fixed erroneous description text (custom configuration code description).
[project/addthis.git] / classes / AddThis.php
CommitLineData
63315932
JP
1<?php
2/**
3 * @file
4 * An AddThis-class.
5 *
6 * @author Jani Palsamäki
7 */
8
9class AddThis {
10
591b038e 11 const BLOCK_NAME = 'addthis_block';
460f1e3c 12 const DEFAULT_CUSTOM_CONFIGURATION_CODE = 'var addthis_config = {}';
06a60498 13 const DEFAULT_FORMATTER = 'addthis_default_formatter';
e3447863 14 const DEFAULT_NUMBER_OF_PREFERRED_SERVICES = 4;
43a336b9 15 const FIELD_TYPE = 'addthis';
9268db58 16 const MODULE_NAME = 'addthis';
460f1e3c
JP
17 const PERMISSION_ADMINISTER_ADDTHIS = 'administer addthis';
18 const PERMISSION_ADMINISTER_ADVANCED_ADDTHIS = 'administer advanced addthis';
9268db58 19 const STYLE_KEY = 'addthis_style';
43a336b9 20 const WIDGET_TYPE = 'addthis_button_widget';
9268db58
JP
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
65e6db6f 27 const BLOCK_WIDGET_TYPE_KEY = 'addthis_block_widget_type';
5fc529ad 28 const BOOKMARK_URL_KEY = 'addthis_bookmark_url';
460f1e3c
JP
29 const CUSTOM_CONFIGURATION_CODE_ENABLED_KEY = 'addthis_custom_configuration_code_enabled';
30 const CUSTOM_CONFIGURATION_CODE_KEY = 'addthis_custom_configuration_code';
65e6db6f 31 const ENABLED_SERVICES_KEY = 'addthis_enabled_services';
460f1e3c 32 const LARGE_ICONS_ENABLED_KEY = 'addthis_large_icons_enabled';
e3447863 33 const NUMBER_OF_PREFERRED_SERVICES_KEY = 'addthis_number_of_preferred_services';
cfab0d33 34 const PROFILE_ID_KEY = 'addthis_profile_id';
8109364c 35 const SERVICES_CSS_URL_KEY = 'addthis_services_css_url';
fc3ad5d3 36 const SERVICES_JSON_URL_KEY = 'addthis_services_json_url';
57765925
JP
37 const UI_HEADER_BACKGROUND_COLOR_KEY = 'addthis_ui_header_background_color';
38 const UI_HEADER_COLOR_KEY = 'addthis_ui_header_color';
8109364c 39 const WIDGET_JS_URL_KEY = 'addthis_widget_js_url';
9268db58
JP
40
41 // External resources
42 const DEFAULT_BOOKMARK_URL = 'http://www.addthis.com/bookmark.php?v=250';
8109364c 43 const DEFAULT_SERVICES_CSS_URL = 'http://cache.addthiscdn.com/icons/v1/sprites/services.css';
be7a8d4b 44 const DEFAULT_SERVICES_JSON_URL = 'http://cache.addthiscdn.com/services/v1/sharing.en.json';
8109364c 45 const DEFAULT_WIDGET_JS_URL = 'http://s7.addthis.com/js/250/addthis_widget.js';
9268db58
JP
46
47 // Internal resources
48 const ADMIN_CSS_FILE = 'addthis.admin.css';
211164bd 49 const ADMIN_INCLUDE_FILE = 'includes/addthis.admin.inc';
9268db58
JP
50
51 // Widget types
63315932 52 const WIDGET_TYPE_COMPACT_BUTTON = 'compact_button';
9268db58 53 const WIDGET_TYPE_DISABLED = 'disabled';
63315932 54 const WIDGET_TYPE_LARGE_BUTTON = 'large_button';
63315932 55 const WIDGET_TYPE_SHARECOUNT = 'sharecount';
be7a8d4b 56 const WIDGET_TYPE_TOOLBOX = 'toolbox';
9268db58 57
2ee37e69
JP
58 private static $instance;
59
ab677dcc 60 /* @var Json */
45d4a8eb 61 private $json;
45d4a8eb 62
ab677dcc
JP
63 /* @var MarkupGenerator */
64 private $markupGenerator;
2ee37e69 65
ab677dcc
JP
66 /**
67 * @return AddThis
68 */
2ee37e69
JP
69 public static function getInstance() {
70 if (!isset(self::$instance)) {
ab677dcc
JP
71 $addThis = new AddThis();
72 $addThis->setJson(new Json());
73 $addThis->setMarkupGenerator(new MarkupGenerator());
74 self::$instance = $addThis;
2ee37e69
JP
75 }
76 return self::$instance;
77 }
78
ab677dcc
JP
79 public function setJson(Json $json) {
80 $this->json = $json;
81 }
82
83 public function setMarkupGenerator(MarkupGenerator $markupGenerator) {
84 $this->markupGenerator = $markupGenerator;
85 }
86
2ee37e69 87 public function getWidgetTypes() {
63315932
JP
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
2ee37e69 97 public function getBlockWidgetType() {
65e6db6f 98 return variable_get(self::BLOCK_WIDGET_TYPE_KEY, self::WIDGET_TYPE_COMPACT_BUTTON);
63315932
JP
99 }
100
2ee37e69 101 public function getWidgetMarkup($widgetType = '', $entity = NULL) {
ab677dcc
JP
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);
63315932
JP
111 }
112 return $markup;
113 }
114
2ee37e69 115 public function getProfileId() {
e960315c 116 return check_plain(variable_get(AddThis::PROFILE_ID_KEY));
18feba68
JP
117 }
118
2ee37e69 119 public function getServicesCssUrl() {
e960315c 120 return check_url(variable_get(AddThis::SERVICES_CSS_URL_KEY, self::DEFAULT_SERVICES_CSS_URL));
8109364c
JP
121 }
122
2ee37e69 123 public function getServicesJsonUrl() {
e960315c 124 return check_url(variable_get(AddThis::SERVICES_JSON_URL_KEY, self::DEFAULT_SERVICES_JSON_URL));
fc3ad5d3
JP
125 }
126
ab677dcc
JP
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;
65e6db6f
JP
138 }
139
ab677dcc
JP
140 public function getEnabledServices() {
141 return variable_get(self::ENABLED_SERVICES_KEY, array());
65e6db6f
JP
142 }
143
2ee37e69 144 public function addStylesheets() {
45d4a8eb
JP
145 drupal_add_css($this->getServicesCssUrl(), 'external');
146 drupal_add_css($this->getAdminCssFilePath(), 'file');
2e4987e2
JP
147 }
148
4c82025d
JP
149 public function addWidgetJs() {
150 drupal_add_js(self::getWidgetUrl(), array('type' => 'external', 'scope' => 'footer'));
151 }
152
2ee37e69 153 public function addConfigurationOptionsJs() {
45d4a8eb
JP
154 if ($this->isCustomConfigurationCodeEnabled()) {
155 $javascript = $this->getCustomConfigurationCode();
460f1e3c 156 } else {
45d4a8eb 157 $enabledServices = $this->getServiceNamesAsCommaSeparatedString();
ab677dcc
JP
158 $javascript = "var addthis_config = {services_compact: '" . $enabledServices . "more'"
159 . $this->getUiHeaderColorConfigurationOptions()
160 . '}';
460f1e3c 161 }
4c82025d 162 drupal_add_js($javascript, array('type' => 'inline'));
87bc3b42
JP
163 }
164
2ee37e69 165 public function areLargeIconsEnabled() {
ab677dcc 166 return variable_get(self::LARGE_ICONS_ENABLED_KEY, TRUE);
8abc0f61
JP
167 }
168
2ee37e69 169 public function getUiHeaderColor() {
e960315c 170 return check_plain(variable_get(self::UI_HEADER_COLOR_KEY));
57765925
JP
171 }
172
2ee37e69 173 public function getUiHeaderBackgroundColor() {
e960315c 174 return check_plain(variable_get(self::UI_HEADER_BACKGROUND_COLOR_KEY));
57765925
JP
175 }
176
2ee37e69 177 public function getCustomConfigurationCode() {
460f1e3c
JP
178 return variable_get(self::CUSTOM_CONFIGURATION_CODE_KEY, self::DEFAULT_CUSTOM_CONFIGURATION_CODE);
179 }
180
2ee37e69 181 public function isCustomConfigurationCodeEnabled() {
460f1e3c
JP
182 return variable_get(self::CUSTOM_CONFIGURATION_CODE_ENABLED_KEY, FALSE);
183 }
184
2ee37e69
JP
185 public function getBaseWidgetJsUrl() {
186 return check_url(variable_get(self::WIDGET_JS_URL_KEY, self::DEFAULT_WIDGET_JS_URL));
187 }
188
189 public function getBaseBookmarkUrl() {
190 return check_url(variable_get(self::BOOKMARK_URL_KEY, self::DEFAULT_BOOKMARK_URL));
191 }
192
e3447863
JP
193 public function getNumberOfPreferredServices() {
194 return variable_get(self::NUMBER_OF_PREFERRED_SERVICES_KEY, self::DEFAULT_NUMBER_OF_PREFERRED_SERVICES);
195 }
196
ab677dcc
JP
197 private function getLargeButtonWidgetMarkup($entity) {
198 return '<a class="addthis_button" '
199 . $this->getAddThisAttributesMarkup($entity)
200 . $this->markupGenerator->generateAttribute('href', self::getFullBookmarkUrl())
201 . '><img src="http://s7.addthis.com/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="'
202 . t('Bookmark and Share')
4c82025d 203 . '" style="border:0"/></a>';
ab677dcc
JP
204 }
205
206 private function getCompactButtonWidgetMarkup($entity) {
207 return '<a class="addthis_button" '
208 . self::getAddThisAttributesMarkup($entity)
209 . $this->markupGenerator->generateAttribute('href', $this->getFullBookmarkUrl())
210 . '><img src="http://s7.addthis.com/static/btn/sm-share-en.gif" width="83" height="16" alt="'
211 . t('Bookmark and Share')
4c82025d 212 . '" style="border:0"/></a>';
ab677dcc
JP
213 }
214
215 private function getToolboxWidgetMarkup($entity) {
e3447863 216 $markup = '<div class="addthis_toolbox addthis_default_style '
ab677dcc 217 . $this->getLargeButtonsClass()
64ccf4c4 218 . '" '
ab677dcc 219 . $this->getAddThisAttributesMarkup($entity)
64ccf4c4
JP
220 . '><a '
221 . $this->markupGenerator->generateAttribute('href', $this->getFullBookmarkUrl())
e3447863
JP
222 . ' class="addthis_button_compact"></a>';
223
224 $numberOfPreferredServices = self::getNumberOfPreferredServices();
225
226 for ($i = 1; $i <= $numberOfPreferredServices; $i++) {
227 $markup .= "<a class=\"addthis_button_preferred_$i\"></a>";
228 }
229
4c82025d 230 $markup .= '</div>';
e3447863
JP
231
232 return $markup;
ab677dcc
JP
233 }
234
235 private function getSharecountWidgetMarkup($entity) {
236 return '<div class="addthis_toolbox addthis_default_style"><a class="addthis_counter" '
237 . $this->getAddThisAttributesMarkup($entity)
4c82025d 238 . '></a></div>';
ab677dcc
JP
239 }
240
241 private function getAddThisAttributesMarkup($entity) {
242 if (is_object($entity)) {
243 return $this->getAddThisTitleAttributeMarkup($entity) . ' ';
244 }
245 return '';
246 }
247
248 private function getAddThisTitleAttributeMarkup($entity) {
249 return $this->markupGenerator->generateAttribute(
250 self::TITLE_ATTRIBUTE, drupal_get_title() . ' - ' . check_plain($entity->title)
251 );
252 }
253
2ee37e69 254 private function getUiHeaderColorConfigurationOptions() {
57765925 255 $configurationOptions = ',';
45d4a8eb
JP
256 $uiHeaderColor = $this->getUiHeaderColor();
257 $uiHeaderBackgroundColor = $this->getUiHeaderBackgroundColor();
57765925
JP
258 if ($uiHeaderColor != NULL) {
259 $configurationOptions .= "ui_header_color: '$uiHeaderColor'";
260 }
261 if ($uiHeaderBackgroundColor != NULL) {
262 $configurationOptions .= ", ui_header_background: '$uiHeaderBackgroundColor'";
263 }
264 return $configurationOptions;
265 }
266
2ee37e69 267 private function getLargeButtonsClass() {
45d4a8eb 268 return $this->areLargeIconsEnabled() ? ' addthis_32x32_style ' : '';
8abc0f61
JP
269 }
270
2ee37e69 271 private function getServiceNamesAsCommaSeparatedString() {
45d4a8eb 272 $enabledServiceNames = array_values($this->getEnabledServices());
87bc3b42
JP
273 $enabledServicesAsCommaSeparatedString = '';
274 foreach ($enabledServiceNames as $enabledServiceName) {
275 if ($enabledServiceName != '0') {
276 $enabledServicesAsCommaSeparatedString .= $enabledServiceName . ',';
277 }
278 }
279 return $enabledServicesAsCommaSeparatedString;
280 }
281
2ee37e69 282 private function getAdminCssFilePath() {
65e6db6f
JP
283 return drupal_get_path('module', self::MODULE_NAME) . '/' . self::ADMIN_CSS_FILE;
284 }
285
2ee37e69 286 private function getFullBookmarkUrl() {
45d4a8eb 287 return check_url($this->getBaseBookmarkUrl() . $this->getProfileIdQueryParameterPrefixedWithAmp());
9cbc2da3
JP
288 }
289
2ee37e69 290 private function getProfileIdQueryParameter($prefix) {
45d4a8eb 291 $profileId = $this->getProfileId();
ab677dcc 292 return $profileId != NULL ? $prefix . self::PROFILE_ID_QUERY_PARAMETER . '=' . $profileId : '';
22526524
JP
293 }
294
2ee37e69 295 private function getProfileIdQueryParameterPrefixedWithAmp() {
45d4a8eb 296 return $this->getProfileIdQueryParameter('&');
22526524
JP
297 }
298
2ee37e69 299 private function getProfileIdQueryParameterPrefixedWithHash() {
45d4a8eb 300 return $this->getProfileIdQueryParameter('#');
22526524
JP
301 }
302
2ee37e69 303 private function getWidgetUrl() {
45d4a8eb 304 return check_url($this->getBaseWidgetJsUrl() . $this->getProfileIdQueryParameterPrefixedWithHash());
63315932
JP
305 }
306}