Added a configuration setting for email addressbook integration.
[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 ADDRESSBOOK_ENABLED_KEY = 'addthis_addressbook_enabled';
28 const BLOCK_WIDGET_TYPE_KEY = 'addthis_block_widget_type';
29 const BOOKMARK_URL_KEY = 'addthis_bookmark_url';
30 const CLICKBACK_TRACKING_ENABLED_KEY = 'addthis_clickback_tracking_enabled';
31 const CO_BRAND_KEY = 'addthis_co_brand';
32 const COMPLIANT_508_KEY = 'addthis_508_compliant';
33 const CUSTOM_CONFIGURATION_CODE_ENABLED_KEY = 'addthis_custom_configuration_code_enabled';
34 const CUSTOM_CONFIGURATION_CODE_KEY = 'addthis_custom_configuration_code';
35 const ENABLED_SERVICES_KEY = 'addthis_enabled_services';
36 const LARGE_ICONS_ENABLED_KEY = 'addthis_large_icons_enabled';
37 const NUMBER_OF_PREFERRED_SERVICES_KEY = 'addthis_number_of_preferred_services';
38 const PROFILE_ID_KEY = 'addthis_profile_id';
39 const SERVICES_CSS_URL_KEY = 'addthis_services_css_url';
40 const SERVICES_JSON_URL_KEY = 'addthis_services_json_url';
41 const UI_HEADER_BACKGROUND_COLOR_KEY = 'addthis_ui_header_background_color';
42 const UI_HEADER_COLOR_KEY = 'addthis_ui_header_color';
43 const WIDGET_JS_URL_KEY = 'addthis_widget_js_url';
44
45 // External resources
46 const DEFAULT_BOOKMARK_URL = 'http://www.addthis.com/bookmark.php?v=250';
47 const DEFAULT_SERVICES_CSS_URL = 'http://cache.addthiscdn.com/icons/v1/sprites/services.css';
48 const DEFAULT_SERVICES_JSON_URL = 'http://cache.addthiscdn.com/services/v1/sharing.en.json';
49 const DEFAULT_WIDGET_JS_URL = 'http://s7.addthis.com/js/250/addthis_widget.js';
50
51 // Internal resources
52 const ADMIN_CSS_FILE = 'addthis.admin.css';
53 const ADMIN_INCLUDE_FILE = 'includes/addthis.admin.inc';
54
55 // Widget types
56 const WIDGET_TYPE_COMPACT_BUTTON = 'compact_button';
57 const WIDGET_TYPE_DISABLED = 'disabled';
58 const WIDGET_TYPE_LARGE_BUTTON = 'large_button';
59 const WIDGET_TYPE_SHARECOUNT = 'sharecount';
60 const WIDGET_TYPE_TOOLBOX = 'toolbox';
61
62 private static $instance;
63
64 /* @var AddThisConfigurationGenerator */
65 private $addThisConfigurationGenerator;
66
67 /* @var Json */
68 private $json;
69
70 /* @var MarkupGenerator */
71 private $markupGenerator;
72
73 /**
74 * @return AddThis
75 */
76 public static function getInstance() {
77 if (!isset(self::$instance)) {
78 $addThis = new AddThis();
79 $addThis->setAddThisConfigurationGenerator(new AddThisConfigurationGenerator());
80 $addThis->setJson(new Json());
81 $addThis->setMarkupGenerator(new MarkupGenerator());
82 self::$instance = $addThis;
83 }
84 return self::$instance;
85 }
86
87 public function setAddThisConfigurationGenerator(AddThisConfigurationGenerator $addThisConfigurationGenerator) {
88 $this->addThisConfigurationGenerator = $addThisConfigurationGenerator;
89 }
90
91 public function setJson(Json $json) {
92 $this->json = $json;
93 }
94
95 public function setMarkupGenerator(MarkupGenerator $markupGenerator) {
96 $this->markupGenerator = $markupGenerator;
97 }
98
99 public function getWidgetTypes() {
100 return array(
101 self::WIDGET_TYPE_DISABLED => t('Disabled'),
102 self::WIDGET_TYPE_COMPACT_BUTTON => t('Compact button'),
103 self::WIDGET_TYPE_LARGE_BUTTON => t('Large button'),
104 self::WIDGET_TYPE_TOOLBOX => t('Toolbox'),
105 self::WIDGET_TYPE_SHARECOUNT => t('Sharecount'),
106 );
107 }
108
109 public function getBlockWidgetType() {
110 return variable_get(self::BLOCK_WIDGET_TYPE_KEY, self::WIDGET_TYPE_COMPACT_BUTTON);
111 }
112
113 public function getWidgetMarkup($widgetType = '', $entity = NULL) {
114 $markup = '';
115 if (self::WIDGET_TYPE_LARGE_BUTTON == $widgetType) {
116 $markup = $this->getLargeButtonWidgetMarkup($entity);
117 } elseif (self::WIDGET_TYPE_COMPACT_BUTTON == $widgetType) {
118 $markup = $this->getCompactButtonWidgetMarkup($entity);
119 } elseif (self::WIDGET_TYPE_TOOLBOX == $widgetType) {
120 $markup = $this->getToolboxWidgetMarkup($entity);
121 } elseif (self::WIDGET_TYPE_SHARECOUNT == $widgetType) {
122 $markup = $this->getSharecountWidgetMarkup($entity);
123 }
124 return $markup;
125 }
126
127 public function getProfileId() {
128 return check_plain(variable_get(AddThis::PROFILE_ID_KEY));
129 }
130
131 public function getServicesCssUrl() {
132 return check_url(variable_get(AddThis::SERVICES_CSS_URL_KEY, self::DEFAULT_SERVICES_CSS_URL));
133 }
134
135 public function getServicesJsonUrl() {
136 return check_url(variable_get(AddThis::SERVICES_JSON_URL_KEY, self::DEFAULT_SERVICES_JSON_URL));
137 }
138
139 public function getServices() {
140 $rows = array();
141 $services = $this->json->decode($this->getServicesJsonUrl());
142 if (!empty($services)) {
143 foreach ($services['data'] AS $service) {
144 $serviceCode = check_plain($service['code']);
145 $serviceName = check_plain($service['name']);
146 $rows[$serviceCode] = '<span class="addthis_service_icon icon_' . $serviceCode . '"></span> ' . $serviceName;
147 }
148 }
149 return $rows;
150 }
151
152 public function getEnabledServices() {
153 return variable_get(self::ENABLED_SERVICES_KEY, array());
154 }
155
156 public function addStylesheets() {
157 drupal_add_css($this->getServicesCssUrl(), 'external');
158 drupal_add_css($this->getAdminCssFilePath(), 'file');
159 }
160
161 public function addWidgetJs() {
162 drupal_add_js(self::getWidgetUrl(), array('type' => 'external', 'scope' => 'footer'));
163 }
164
165 public function addConfigurationOptionsJs() {
166 if ($this->isCustomConfigurationCodeEnabled()) {
167 $javascript = $this->getCustomConfigurationCode();
168 } else {
169 $enabledServices = $this->getServiceNamesAsCommaSeparatedString();
170 $javascript = "var addthis_config = {services_compact: '" . $enabledServices . "more'"
171 . $this->addThisConfigurationGenerator->generate('ui_header_color', $this->getUiHeaderColor())
172 . $this->addThisConfigurationGenerator->generate('ui_header_backround', $this->getUiHeaderBackgroundColor())
173 . $this->addThisConfigurationGenerator->generate('ui_cobrand', $this->getCoBrand())
174 . $this->addThisConfigurationGenerator->generate('ui_508_compliant', $this->get508Compliant())
175 . $this->addThisConfigurationGenerator->generate('data_track_clickback', $this->isClickbackTrackingEnabled())
176 . $this->addThisConfigurationGenerator->generate('ui_use_addressbook', $this->isAddressbookEnabled())
177 . '}'
178 ;
179 }
180 drupal_add_js($javascript, array('type' => 'inline'));
181 }
182
183 public function areLargeIconsEnabled() {
184 return variable_get(self::LARGE_ICONS_ENABLED_KEY, TRUE);
185 }
186
187 public function getUiHeaderColor() {
188 return check_plain(variable_get(self::UI_HEADER_COLOR_KEY));
189 }
190
191 public function getUiHeaderBackgroundColor() {
192 return check_plain(variable_get(self::UI_HEADER_BACKGROUND_COLOR_KEY));
193 }
194
195 public function getCustomConfigurationCode() {
196 return variable_get(self::CUSTOM_CONFIGURATION_CODE_KEY, self::DEFAULT_CUSTOM_CONFIGURATION_CODE);
197 }
198
199 public function isCustomConfigurationCodeEnabled() {
200 return variable_get(self::CUSTOM_CONFIGURATION_CODE_ENABLED_KEY, FALSE);
201 }
202
203 public function getBaseWidgetJsUrl() {
204 return check_url(variable_get(self::WIDGET_JS_URL_KEY, self::DEFAULT_WIDGET_JS_URL));
205 }
206
207 public function getBaseBookmarkUrl() {
208 return check_url(variable_get(self::BOOKMARK_URL_KEY, self::DEFAULT_BOOKMARK_URL));
209 }
210
211 public function getNumberOfPreferredServices() {
212 return variable_get(self::NUMBER_OF_PREFERRED_SERVICES_KEY, self::DEFAULT_NUMBER_OF_PREFERRED_SERVICES);
213 }
214
215 public function getCoBrand() {
216 return variable_get(self::CO_BRAND_KEY);
217 }
218
219 public function get508Compliant() {
220 return variable_get(self::COMPLIANT_508_KEY, FALSE);
221 }
222
223 public function isClickbackTrackingEnabled() {
224 return variable_get(self::CLICKBACK_TRACKING_ENABLED_KEY, FALSE);
225 }
226
227 public function isAddressbookEnabled() {
228 return variable_get(self::ADDRESSBOOK_ENABLED_KEY, FALSE);
229 }
230
231 private function getLargeButtonWidgetMarkup($entity) {
232 return '<a class="addthis_button" '
233 . $this->getAddThisAttributesMarkup($entity)
234 . $this->markupGenerator->generateAttribute('href', self::getFullBookmarkUrl())
235 . '><img src="http://s7.addthis.com/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="'
236 . t('Bookmark and Share')
237 . '" style="border:0"/></a>';
238 }
239
240 private function getCompactButtonWidgetMarkup($entity) {
241 return '<a class="addthis_button" '
242 . self::getAddThisAttributesMarkup($entity)
243 . $this->markupGenerator->generateAttribute('href', $this->getFullBookmarkUrl())
244 . '><img src="http://s7.addthis.com/static/btn/sm-share-en.gif" width="83" height="16" alt="'
245 . t('Bookmark and Share')
246 . '" style="border:0"/></a>';
247 }
248
249 private function getToolboxWidgetMarkup($entity) {
250 $markup = '<div class="addthis_toolbox addthis_default_style '
251 . $this->getLargeButtonsClass()
252 . '" '
253 . $this->getAddThisAttributesMarkup($entity)
254 . '><a '
255 . $this->markupGenerator->generateAttribute('href', $this->getFullBookmarkUrl())
256 . ' class="addthis_button_compact"></a>';
257
258 $numberOfPreferredServices = self::getNumberOfPreferredServices();
259
260 for ($i = 1; $i <= $numberOfPreferredServices; $i++) {
261 $markup .= "<a class=\"addthis_button_preferred_$i\"></a>";
262 }
263
264 $markup .= '</div>';
265
266 return $markup;
267 }
268
269 private function getSharecountWidgetMarkup($entity) {
270 return '<div class="addthis_toolbox addthis_default_style"><a class="addthis_counter" '
271 . $this->getAddThisAttributesMarkup($entity)
272 . '></a></div>';
273 }
274
275 private function getAddThisAttributesMarkup($entity) {
276 if (is_object($entity)) {
277 return $this->getAddThisTitleAttributeMarkup($entity) . ' ';
278 }
279 return '';
280 }
281
282 private function getAddThisTitleAttributeMarkup($entity) {
283 return $this->markupGenerator->generateAttribute(
284 self::TITLE_ATTRIBUTE, drupal_get_title() . ' - ' . check_plain($entity->title)
285 );
286 }
287
288 private function getLargeButtonsClass() {
289 return $this->areLargeIconsEnabled() ? ' addthis_32x32_style ' : '';
290 }
291
292 private function getServiceNamesAsCommaSeparatedString() {
293 $enabledServiceNames = array_values($this->getEnabledServices());
294 $enabledServicesAsCommaSeparatedString = '';
295 foreach ($enabledServiceNames as $enabledServiceName) {
296 if ($enabledServiceName != '0') {
297 $enabledServicesAsCommaSeparatedString .= $enabledServiceName . ',';
298 }
299 }
300 return $enabledServicesAsCommaSeparatedString;
301 }
302
303 private function getAdminCssFilePath() {
304 return drupal_get_path('module', self::MODULE_NAME) . '/' . self::ADMIN_CSS_FILE;
305 }
306
307 private function getFullBookmarkUrl() {
308 return check_url($this->getBaseBookmarkUrl() . $this->getProfileIdQueryParameterPrefixedWithAmp());
309 }
310
311 private function getProfileIdQueryParameter($prefix) {
312 $profileId = $this->getProfileId();
313 return !empty($profileId) ? $prefix . self::PROFILE_ID_QUERY_PARAMETER . '=' . $profileId : '';
314 }
315
316 private function getProfileIdQueryParameterPrefixedWithAmp() {
317 return $this->getProfileIdQueryParameter('&');
318 }
319
320 private function getProfileIdQueryParameterPrefixedWithHash() {
321 return $this->getProfileIdQueryParameter('#');
322 }
323
324 private function getWidgetUrl() {
325 return check_url($this->getBaseWidgetJsUrl() . $this->getProfileIdQueryParameterPrefixedWithHash());
326 }
327 }