summaryrefslogtreecommitdiffstats
path: root/classes/AddThis.php
blob: c54fc4cfe209a626108142765afdab1eab1b1a4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
/**
 * @file
 * An AddThis-class.
 *
 * @author Jani Palsamäki
 */

class AddThis {

  const BLOCK_NAME = 'addthis_block';
  const DEFAULT_FORMATTER = 'addthis_default';
  const MODULE_NAME = 'addthis';
  const STYLE_KEY = 'addthis_style';

  // AddThis attribute and parameter names (as defined in AddThis APIs)
  const PROFILE_ID_QUERY_PARAMETER = 'pubid';
  const TITLE_ATTRIBUTE = 'addthis:title';

  // Persistent variable keys
  const BLOCK_WIDGET_TYPE_KEY = 'addthis_block_widget_type';
  const BOOKMARK_URL_KEY = 'addthis_bookmark_url';
  const ENABLED_SERVICES_KEY = 'addthis_enabled_services';
  const LARGE_ICONS_KEY = 'addthis_large_icons';
  const PROFILE_ID_KEY = 'addthis_profile_id';
  const SERVICES_CSS_URL_KEY = 'addthis_services_css_url';
  const SERVICES_JSON_URL_KEY = 'addthis_services_json_url';
  const UI_HEADER_BACKGROUND_COLOR_KEY = 'addthis_ui_header_background_color';
  const UI_HEADER_COLOR_KEY = 'addthis_ui_header_color';
  const WIDGET_JS_URL_KEY = 'addthis_widget_js_url';

  // External resources
  const DEFAULT_BOOKMARK_URL = 'http://www.addthis.com/bookmark.php?v=250';
  const DEFAULT_SERVICES_JSON_URL = 'http://cache.addthiscdn.com/services/v1/sharing.en.json';
  const DEFAULT_SERVICES_CSS_URL = 'http://cache.addthiscdn.com/icons/v1/sprites/services.css';
  const DEFAULT_WIDGET_JS_URL = 'http://s7.addthis.com/js/250/addthis_widget.js';

  // Internal resources
  const ADMIN_CSS_FILE = 'addthis.admin.css';
  const ADMIN_INCLUDE_FILE = 'addthis.admin.inc';

  // Widget types
  const WIDGET_TYPE_COMPACT_BUTTON = 'compact_button';
  const WIDGET_TYPE_DISABLED = 'disabled';
  const WIDGET_TYPE_LARGE_BUTTON = 'large_button';
  const WIDGET_TYPE_TOOLBOX = 'toolbox';
  const WIDGET_TYPE_SHARECOUNT = 'sharecount';

  // Markup constants
  const AMP_ENTITY = '&amp;';
  const HASH = '#';
  const HREF = 'href';

  public static function getWidgetTypes() {
    return array(
      self::WIDGET_TYPE_DISABLED => t('Disabled'),
      self::WIDGET_TYPE_COMPACT_BUTTON => t('Compact button'),
      self::WIDGET_TYPE_LARGE_BUTTON => t('Large button'),
      self::WIDGET_TYPE_TOOLBOX => t('Toolbox'),
      self::WIDGET_TYPE_SHARECOUNT => t('Sharecount'),
    );
  }

  public static function getBlockWidgetType() {
    return variable_get(self::BLOCK_WIDGET_TYPE_KEY, self::WIDGET_TYPE_COMPACT_BUTTON);
  }

  public static function getWidgetMarkup($widgetType = '', $entity = NULL) {
    switch ($widgetType) {
      case self::WIDGET_TYPE_LARGE_BUTTON:
        $markup =
          '<a class="addthis_button" '
          . self::getAddThisAttributesMarkup($entity)
          . MarkupGenerator::generateAttribute(self::HREF, self::getFullBookmarkUrl())
          . '><img src="http://s7.addthis.com/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="'
          . t('Bookmark and Share')
          . '" style="border:0"/></a>'
          . self::getWidgetScriptElement();
        break;
      case self::WIDGET_TYPE_COMPACT_BUTTON:
        $markup =
          '<a class="addthis_button" '
          . self::getAddThisAttributesMarkup($entity)
          . MarkupGenerator::generateAttribute(self::HREF, self::getFullBookmarkUrl())
          . '><img src="http://s7.addthis.com/static/btn/sm-share-en.gif" width="83" height="16" alt="'
          . t('Bookmark and Share')
          . '" style="border:0"/></a>'
          . self::getWidgetScriptElement();
        break;
      case self::WIDGET_TYPE_TOOLBOX:
        $markup =
          '<div class="addthis_toolbox addthis_default_style'
          . self::getLargeButtonsClass()
          . '"><a '
          . MarkupGenerator::generateAttribute(self::HREF, self::getFullBookmarkUrl())
          . ' class="addthis_button_compact" '
          . self::getAddThisAttributesMarkup($entity)
          . '>'
          . t('Share')
          . '</a><span class="addthis_separator">|</span>' 
          . '<a class="addthis_button_preferred_1" '
          . self::getAddThisAttributesMarkup($entity)
          . '></a>'
          . '<a class="addthis_button_preferred_2" '
          . self::getAddThisAttributesMarkup($entity)
          . '></a>'
          . '<a class="addthis_button_preferred_3" '
          . self::getAddThisAttributesMarkup($entity)
          . '></a>'
          . '<a class="addthis_button_preferred_4" '
          . self::getAddThisAttributesMarkup($entity)
          . '></a></div>'
          . self::getWidgetScriptElement();
        break;
      case self::WIDGET_TYPE_SHARECOUNT:
        $markup =
          '<div class="addthis_toolbox addthis_default_style"><a class="addthis_counter" '
          . self::getAddThisAttributesMarkup($entity)
          . '></a></div>'
          . self::getWidgetScriptElement();
        break;
      default:
        $markup = '';
        break;
    }
    return $markup;
  }

  public static function getProfileId() {
    return variable_get(AddThis::PROFILE_ID_KEY);
  }

  public static function getServicesCssUrl() {
    return variable_get(AddThis::SERVICES_CSS_URL_KEY, self::DEFAULT_SERVICES_CSS_URL);
  }

  public static function getServicesJsonUrl() {
    return variable_get(AddThis::SERVICES_JSON_URL_KEY, self::DEFAULT_SERVICES_JSON_URL);
  }

  public static function getServiceOptions() {
    return self::getServices();
  }

  public static function getEnabledServiceOptions() {
    return self::getEnabledServices();
  }

  public static function addStylesheets() {
    drupal_add_css(self::getServicesCssUrl(), 'external');
    drupal_add_css(self::getAdminCssFilePath(), 'file');
  }

  public static function addConfigurationOptionsJs() {
    $enabledServices = self::getServiceNamesAsCommaSeparatedString();
    drupal_add_js(
      "var addthis_config = {services_compact: '" . $enabledServices . "more'"
      . self::getUiHeaderColorConfigurationOptions()
      . '}', 'inline'
    );
  }

  public static function areLargeIconsEnabled() {
    return variable_get(self::LARGE_ICONS_KEY, FALSE);
  }

  public static function getUiHeaderColor() {
    return variable_get(self::UI_HEADER_COLOR_KEY);
  }

  public static function getUiHeaderBackgroundColor() {
    return variable_get(self::UI_HEADER_BACKGROUND_COLOR_KEY);
  }

  private static function getUiHeaderColorConfigurationOptions() {
    $configurationOptions = ',';
    $uiHeaderColor = self::getUiHeaderColor();
    $uiHeaderBackgroundColor = self::getUiHeaderBackgroundColor();
    if ($uiHeaderColor != NULL) {
      $configurationOptions .= "ui_header_color: '$uiHeaderColor'";
    }
    if ($uiHeaderBackgroundColor != NULL) {
      $configurationOptions .= ", ui_header_background: '$uiHeaderBackgroundColor'";
    }
    return $configurationOptions;
  }

  private static function getLargeButtonsClass() {
    return self::areLargeIconsEnabled() ? ' addthis_32x32_style ' : '';
  }

  private static function getServiceNamesAsCommaSeparatedString() {
    $enabledServiceNames = array_values(self::getEnabledServices());
    $enabledServicesAsCommaSeparatedString = '';
    foreach ($enabledServiceNames as $enabledServiceName) {
      if ($enabledServiceName != '0') {
        $enabledServicesAsCommaSeparatedString .= $enabledServiceName . ',';
      }
    }
    return $enabledServicesAsCommaSeparatedString;
  }

  private static function getAdminCssFilePath() {
    return drupal_get_path('module', self::MODULE_NAME) . '/' . self::ADMIN_CSS_FILE;
  }

  private static function getServices() {
    $rows = array();
    $json = new Json();
    $services = $json->decode(self::getServicesJsonUrl());
    if ($services != NULL) {
      foreach ($services['data'] AS $service) {
        $serviceCode = check_plain($service['code']);
        $serviceName = check_plain($service['name']);
        $rows[$serviceCode] = '<span class="addthis_service_icon icon_' . $serviceCode . '"></span> ' . $serviceName;
      }
    }
    return $rows;
  }

  private static function getEnabledServices() {
    return variable_get(self::ENABLED_SERVICES_KEY, array());
  }

  public static function getBaseBookmarkUrl() {
    return variable_get(self::BOOKMARK_URL_KEY, self::DEFAULT_BOOKMARK_URL);
  }

  private static function getFullBookmarkUrl() {
    return self::getBaseBookmarkUrl() . self::getProfileIdQueryParameterPrefixedWithAmp();
  }

  public static function getBaseWidgetJsUrl() {
    return variable_get(self::WIDGET_JS_URL_KEY, self::DEFAULT_WIDGET_JS_URL);
  }

  private static function getProfileIdQueryParameter($prefix) {
    $profileId = self::getProfileId();
    return $profileId != NULL ? $prefix . self::PROFILE_ID_QUERY_PARAMETER . '=' . $profileId : '';
  }

  private static function getProfileIdQueryParameterPrefixedWithAmp() {
    return self::getProfileIdQueryParameter(self::AMP_ENTITY);
  }

  private static function getProfileIdQueryParameterPrefixedWithHash() {
    return self::getProfileIdQueryParameter(self::HASH);
  }

  private static function getAddThisAttributesMarkup($entity) {
    if (is_object($entity)) {
      return self::getAddThisTitleAttributeMarkup($entity) . ' ';
    }
    return '';
  }

  private static function getAddThisTitleAttributeMarkup($entity) {
    return MarkupGenerator::generateAttribute(self::TITLE_ATTRIBUTE, drupal_get_title() . ' - ' . check_plain($entity->title));
  }

  private static function getWidgetScriptElement() {
    return '<script type="text/javascript" src="' . self::getWidgetUrl() . '"></script>';
  }

  private static function getWidgetUrl() {
    return self::getBaseWidgetJsUrl() . self::getProfileIdQueryParameterPrefixedWithHash();
  }
}