Some cleanup, commenting and refactoring.
[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';
9268db58
JP
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
65e6db6f 21 const BLOCK_WIDGET_TYPE_KEY = 'addthis_block_widget_type';
5fc529ad 22 const BOOKMARK_URL_KEY = 'addthis_bookmark_url';
65e6db6f 23 const ENABLED_SERVICES_KEY = 'addthis_enabled_services';
cfab0d33 24 const PROFILE_ID_KEY = 'addthis_profile_id';
fc3ad5d3 25 const SERVICES_JSON_URL_KEY = 'addthis_services_json_url';
9268db58
JP
26
27 // External resources
28 const DEFAULT_BOOKMARK_URL = 'http://www.addthis.com/bookmark.php?v=250';
29 const DEFAULT_SERVICES_JSON_URL = 'http://cache.addthiscdn.com/services/v1/sharing.en.json';
30 const SERVICES_CSS_URL = 'http://cache.addthiscdn.com/icons/v1/sprites/services.css';
65e6db6f 31 const WIDGET_JS_URL = 'http://s7.addthis.com/js/250/addthis_widget.js';
9268db58
JP
32
33 // Internal resources
34 const ADMIN_CSS_FILE = 'addthis.admin.css';
35 const ADMIN_INCLUDE_FILE = 'addthis.admin.inc';
36
37 // Widget types
63315932 38 const WIDGET_TYPE_COMPACT_BUTTON = 'compact_button';
9268db58 39 const WIDGET_TYPE_DISABLED = 'disabled';
63315932
JP
40 const WIDGET_TYPE_LARGE_BUTTON = 'large_button';
41 const WIDGET_TYPE_TOOLBOX = 'toolbox';
42 const WIDGET_TYPE_SHARECOUNT = 'sharecount';
43
9268db58
JP
44 // Markup constants
45 const AMP_ENTITY = '&amp;';
46 const HASH = '#';
47 const HREF = 'href';
48
63315932
JP
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
65e6db6f
JP
59 public static function getBlockWidgetType() {
60 return variable_get(self::BLOCK_WIDGET_TYPE_KEY, self::WIDGET_TYPE_COMPACT_BUTTON);
63315932
JP
61 }
62
f8df9461 63 public static function getWidgetMarkup($widgetType = '', $entity = NULL) {
63315932
JP
64 switch ($widgetType) {
65 case self::WIDGET_TYPE_LARGE_BUTTON:
22526524 66 $markup =
f8df9461
JP
67 '<a class="addthis_button" '
68 . self::getAddThisAttributesMarkup($entity)
5fc529ad 69 . MarkupGenerator::generateAttribute(self::HREF, self::getFullBookmarkUrl())
8317fff4 70 . '><img src="http://s7.addthis.com/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="'
3b960df8
JP
71 . t('Bookmark and Share')
72 . '" style="border:0"/></a>'
9cbc2da3 73 . self::getWidgetScriptElement();
63315932
JP
74 break;
75 case self::WIDGET_TYPE_COMPACT_BUTTON:
22526524 76 $markup =
f8df9461
JP
77 '<a class="addthis_button" '
78 . self::getAddThisAttributesMarkup($entity)
5fc529ad 79 . MarkupGenerator::generateAttribute(self::HREF, self::getFullBookmarkUrl())
8317fff4 80 . '><img src="http://s7.addthis.com/static/btn/sm-share-en.gif" width="83" height="16" alt="'
3b960df8
JP
81 . t('Bookmark and Share')
82 . '" style="border:0"/></a>'
9cbc2da3 83 . self::getWidgetScriptElement();
63315932
JP
84 break;
85 case self::WIDGET_TYPE_TOOLBOX:
22526524 86 $markup =
8317fff4 87 '<div class="addthis_toolbox addthis_default_style"><a '
5fc529ad 88 . MarkupGenerator::generateAttribute(self::HREF, self::getFullBookmarkUrl())
8317fff4 89 . ' class="addthis_button_compact" '
f8df9461
JP
90 . self::getAddThisAttributesMarkup($entity)
91 . '>'
3b960df8 92 . t('Share')
db615978
JP
93 . '</a><span class="addthis_separator">|</span>'
94 . '<a class="addthis_button_preferred_1" '
95 . self::getAddThisAttributesMarkup($entity)
96 . '></a>'
97 . '<a class="addthis_button_preferred_2" '
98 . self::getAddThisAttributesMarkup($entity)
99 . '></a>'
100 . '<a class="addthis_button_preferred_3" '
101 . self::getAddThisAttributesMarkup($entity)
102 . '></a>'
103 . '<a class="addthis_button_preferred_4" '
104 . self::getAddThisAttributesMarkup($entity)
105 . '></a></div>'
9cbc2da3 106 . self::getWidgetScriptElement();
63315932
JP
107 break;
108 case self::WIDGET_TYPE_SHARECOUNT:
22526524 109 $markup =
f8df9461
JP
110 '<div class="addthis_toolbox addthis_default_style"><a class="addthis_counter" '
111 . self::getAddThisAttributesMarkup($entity)
112 . '></a></div>'
9cbc2da3 113 . self::getWidgetScriptElement();
63315932
JP
114 break;
115 default:
116 $markup = '';
117 break;
118 }
119 return $markup;
120 }
121
18feba68
JP
122 public static function getProfileId() {
123 return variable_get(AddThis::PROFILE_ID_KEY);
124 }
125
fc3ad5d3
JP
126 public static function getServicesJsonUrl() {
127 return variable_get(AddThis::SERVICES_JSON_URL_KEY, self::DEFAULT_SERVICES_JSON_URL);
128 }
129
65e6db6f
JP
130 public static function getServiceOptions() {
131 return self::getServices();
132 }
133
134 public static function getEnabledServiceOptions() {
135 return self::getEnabledServices();
136 }
137
2e4987e2
JP
138 public static function addStylesheets() {
139 drupal_add_css(self::SERVICES_CSS_URL, 'external');
140 drupal_add_css(self::getAdminCssFilePath(), 'file');
141 }
142
87bc3b42
JP
143 public static function addConfigurationOptionsJs() {
144 $enabledServices = self::getServiceNamesAsCommaSeparatedString();
2a62f2e8 145 drupal_add_js("var addthis_config = {services_compact: '" . $enabledServices . "more'}", 'inline');
87bc3b42
JP
146 }
147
148 private static function getServiceNamesAsCommaSeparatedString() {
149 $enabledServiceNames = array_values(self::getEnabledServices());
150 $enabledServicesAsCommaSeparatedString = '';
151 foreach ($enabledServiceNames as $enabledServiceName) {
152 if ($enabledServiceName != '0') {
153 $enabledServicesAsCommaSeparatedString .= $enabledServiceName . ',';
154 }
155 }
156 return $enabledServicesAsCommaSeparatedString;
157 }
158
200fb312 159 private static function getAdminCssFilePath() {
65e6db6f
JP
160 return drupal_get_path('module', self::MODULE_NAME) . '/' . self::ADMIN_CSS_FILE;
161 }
162
163 private static function getServices() {
65e6db6f 164 $rows = array();
d523e7b4 165 $json = new Json();
fc3ad5d3 166 $services = $json->decode(self::getServicesJsonUrl());
d523e7b4
JP
167 if ($services != NULL) {
168 foreach ($services['data'] AS $service) {
87bc3b42
JP
169 $serviceCode = check_plain($service['code']);
170 $serviceName = check_plain($service['name']);
65e6db6f
JP
171 $rows[$serviceCode] = '<span class="addthis_service_icon icon_' . $serviceCode . '"></span> ' . $serviceName;
172 }
173 }
174 return $rows;
175 }
176
177 private static function getEnabledServices() {
178 return variable_get(self::ENABLED_SERVICES_KEY, array());
179 }
180
5fc529ad
JP
181 public static function getBaseBookmarkUrl() {
182 return variable_get(self::BOOKMARK_URL_KEY, self::DEFAULT_BOOKMARK_URL);
183 }
184
185 private static function getFullBookmarkUrl() {
186 return self::getBaseBookmarkUrl() . self::getProfileIdQueryParameterPrefixedWithAmp();
9cbc2da3
JP
187 }
188
22526524
JP
189 private static function getProfileIdQueryParameter($prefix) {
190 $profileId = self::getProfileId();
191 return $profileId != NULL ? $prefix . self::PROFILE_ID_QUERY_PARAMETER . '=' . $profileId : '';
192 }
193
194 private static function getProfileIdQueryParameterPrefixedWithAmp() {
195 return self::getProfileIdQueryParameter(self::AMP_ENTITY);
196 }
197
198 private static function getProfileIdQueryParameterPrefixedWithHash() {
199 return self::getProfileIdQueryParameter(self::HASH);
200 }
201
f8df9461 202 private static function getAddThisAttributesMarkup($entity) {
18b01289
JP
203 if (is_object($entity)) {
204 return self::getAddThisTitleAttributeMarkup($entity) . ' ';
205 }
206 return '';
f8df9461
JP
207 }
208
209 private static function getAddThisTitleAttributeMarkup($entity) {
21f1b045 210 return MarkupGenerator::generateAttribute(self::TITLE_ATTRIBUTE, drupal_get_title() . ' - ' . check_plain($entity->title));
f8df9461
JP
211 }
212
9cbc2da3 213 private static function getWidgetScriptElement() {
18feba68 214 return '<script type="text/javascript" src="' . self::getWidgetUrl() . '"></script>';
9cbc2da3
JP
215 }
216
18feba68 217 private static function getWidgetUrl() {
65e6db6f 218 return self::WIDGET_JS_URL . self::getProfileIdQueryParameterPrefixedWithHash();
63315932
JP
219 }
220}