sharer_toolbar.module 4.08 KB
Newer Older
1
2
3
4
5
6
<?php

/**
 * @file sharer_toolbar.module
 * Hooks implementations and code logic related to Sharer Toolbar module
 */
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


/**
 * Implements hook_permission().
 */
function sharer_toolbar_permission() {
  return array(
    'admin sharer toolbar' => array(
      'title' => t('Administer sharer toolbar'),
      'description' => t('Choose which social sharer to be displayed and other stuffs.'),
    ),
  );
}


/**
 * Implements hook_menu().
 */
function sharer_toolbar_menu() {
  return array(
    'admin/config/services/social-sharer-toolbar' => array(
      'title' => 'Sharer toolbar configuration',
      'description' => 'Configure sharer toolbar.',
      'page callback' => 'drupal_get_form',
      'page arguments' => array('sharer_toolbar_config_form'),
      'access arguments' => array('admin sharer toolbar'),
      'file' => 'sharer_toolbar.forms.inc',
    )
  );
}


/**
 * Implements hook_theme().
 */
function sharer_toolbar_theme($existing, $type, $theme, $path) {
  return array(
44
45

    // Sharer toolbar config form
46
47
48
49
    'sharer_toolbar_config_form' => array(
      'render element' => 'form',
      'file' => 'sharer_toolbar.theme.inc',
    ),
50
51
52
53
54
55
56
57
58

    // Sharer toolbar template
    'sharer_toolbar' => array(
      'variables' => array(
        'sharers' => array(),
      ),
      'template' => 'sharer-toolbar',
      // 'path' => 'templates',
    ),
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  );
}


/**
 * Implements hook_sharer_toolbar_available_sharer().
 */
function sharer_toolbar_sharer_toolbar_available_sharer() {

  // Define default sharers
  return array(

    // Facebook sharer
    'facebook' => array(
      'sid' => 'facebook',
      'name' => 'Facebook',
      'icon_class' => 'icon-facebook',
76
      'url' => 'https://www.facebook.com/sharer/sharer.php?u=[:shared_url:]',
77
      'label' => 'Share on Facebook',
78
79
80
81
82
83
84
    ),

    // Twitter sharer
    'twitter' => array(
      'sid' => 'twitter',
      'name' => 'Twitter',
      'icon_class' => 'icon-twitter',
85
86
      'url' => 'https://twitter.com/home?status=[:shared_summary:] [:shared_url:]',
      'label' => 'Share on Twitter',
87
88
89
90
91
92
93
94
    ),

    // Google Plus sharer
    'googleplus' => array(
      'sid' => 'googleplus',
      'name' => 'Google +',
      'icon_class' => 'icon-googleplus',
      'url' => 'https://plus.google.com/share?url=[:shared_url:]',
95
      'label' => 'Share on Google +',
96
97
98
99
100
101
102
    ),

    // Twitter sharer
    'linkedin' => array(
      'sid' => 'linkedin',
      'name' => 'LinkedIn',
      'icon_class' => 'icon-linkedin',
103
      'url' => 'https://www.linkedin.com/shareArticle?mini=true&url=[:shared_url:]&title=[:shared_title:]&summary=[:shared_summary:]&source=[:shared_source:]',
104
      'label' => 'Share on LinkedIn',
105
106
    ),

107
108
109
110
111
112
113
114
    // Email sharer
    'email' => array(
      'sid' => 'email',
      'name' => 'Email',
      'icon_class' => 'icon-email',
      'url' => 'mailto:?subject=[:shared_title:]&body=[:shared_url:]',
      'label' => 'Share by email',
    ),
115
116
  );
}
117
118
119


/**
120
 * Implements hook_process_node().
121
 */
122
function sharer_toolbar_process_node(&$variables) {
123
124
125
126
127
128
129
130
131
132
133
  module_load_include('inc', 'sharer_toolbar');
  $sharing_buttons = array();

  // Get all active and available sharers
  $active_sharers = variable_get('sharer_toolbar_sharers_configuration', array());
  $available_sharers = _sharer_toolbar_get_available_sharers();

  // Generate button if sharer is active
  foreach ($active_sharers as $sid => &$sharer) {
    if ($sharer['status']) {

134
      // Merge file configuration to complete database configuration
135
136
137
138
139
      $sharer = array_merge($available_sharers[$sid], $sharer);

      // Transform URL
      _sharer_toolbar_generate_sharer_url($sharer['url'], $variables);

140
141
142
143
144
    }

    // or unset before sending array to template
    else {
      unset($active_sharers[$sid]);
145
146
147
    }
  }

148
149
150
  // Add javascript feature
  drupal_add_js(drupal_get_path('module', 'sharer_toolbar') . '/js/sharer-opener.js');

151
  // Render HTML for sharer toolbar
152
153
  if (!empty($active_sharers)) {
    $variables['sharer_toolbar'] = theme('sharer_toolbar', array(
154
      'title' => variable_get('sharer_toolbar_title', 'Share'),
155
156
157
158
159
160
      'sharers' => $active_sharers,
    ));
  }
  else {
    $variables['sharer_toolbar'] = NULL;
  }
161
}