aboutsummaryrefslogtreecommitdiff
path: root/fblikebutton.module
blob: cb191b62f48631694e3fde613a558fa88602bfde (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
<?php
// $Id$

/**
 * @file
 * Adds Facebook's "Like" button to each selected node type.
 * Adds a block with a global static value where users can "Like" the URL set by admins.
 */

/**
 * Implements of hook_menu().
 */
function fblikebutton_menu() {
  $items['admin/config/fblikebutton'] = array(
    'title' => 'FB Like Button',
    'description' => 'Configure the Facebook Like button',
    'page callback' => 'system_admin_menu_block_page',
    'access arguments' => array('administer fblikebutton'),
    'position' => 'right',
    'weight' => -20,
    'type' => MENU_NORMAL_ITEM,
    'file' => 'system.admin.inc',
    'file path' => drupal_get_path('module', 'system'),
  );
  $items['admin/config/fblikebutton/dynamic'] = array(
    'title' => 'Dynamic Like button settings',
    'description' => 'Configure the settings for the Like button as it appears on individual nodes (liking that node).',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('fblikebutton_dynamic_settings'),
    'access arguments' => array('administer fblikebutton'),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'fblikebutton.admin.inc',
    'weight' => 0,
  );
  $items['admin/config/fblikebutton/static'] = array(
    'title' => 'Static Like button settings',
    'description' => 'Configure the settings for the static Like button as it appears in the block (liking a given url). Use this to like for example your Facebook fanpage.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('fblikebutton_static_settings'),
    'access arguments' => array('administer fblikebutton block'),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'fblikebutton.admin.inc',
    'weight' => 1,
  );
  return $items;
}

/**
 * Implements of hook_node_view().
 */
function fblikebutton_node_view($node, $view_mode) {
  global $user, $base_url;
  $types = variable_get('fblikebutton_node_types', array());
  $fullnodedisplay = variable_get('fblikebutton_full_node_display', 0);
  $teaserdisplay = variable_get('fblikebutton_teaser_display', 0);
  $full = ($view_mode == 'full') ? TRUE : FALSE;
  $show = ( ! empty($types[$node->type]) && user_access('access fblikebutton'));
  // Thanks to corbacho for supplying the code for the $webpage_to_like variable...
  // (It was apparently throwing errors/notices the way I had it set up.)
  $webpage_to_like = url("node/$node->nid", array('absolute' => TRUE));
  $likebutton_weight = variable_get('fblikebutton_weight', '0');
  $conf = array(
    'layout' => variable_get('fblikebutton_layout', 'standard'),
    'action' => variable_get('fblikebutton_action', 'like'),
    'color_scheme' => variable_get('fblikebutton_color_scheme', 'light'),
    'show_faces' => variable_get('fblikebutton_show_faces', 'true'),
    'font' => variable_get('fblikebutton_font', 'arial'),
    'height' => variable_get('fblikebutton_iframe_height', '80'),
    'width' => variable_get('fblikebutton_iframe_width', '450'),
    'send' => variable_get('fblikebutton_send', 'true'),
    'other_css' => variable_get('fblikebutton_iframe_css', ''),
    'language' => variable_get('fblikebutton_language', 'en_US'),
  );
  if ($show) {
      
  // Content area
    if (($view_mode == 'teaser' && $teaserdisplay == 1) || ($view_mode == 'full' && $fullnodedisplay == 0)) {
      $node->content['fblikebutton_field'] = array(
        '#markup' => _fblikebutton_field($webpage_to_like, $conf),
        '#weight' => $likebutton_weight,
      );
    }
  // Link area
    if (($view_mode == 'teaser' && $teaserdisplay == 2) || ($view_mode == 'full' && $fullnodedisplay == 2)) {
      $node->content['links']['#links']['fblikebutton_field'] = array(
        'title' => _fblikebutton_field($webpage_to_like, $conf),
        'html' => TRUE,
      );
    }
  }
}


/**
 * Implements of hook_permission().
 */
function fblikebutton_permission() {
  return array(
    'administer fblikebutton' => array(
      'title' => t('Administer FB Like button'),
      'description' => t('Perform administration tasks for FB Like button')
    ),
    'administer fblikebutton block' => array(
      'title' => t('Administer FB Like button block'),
      'description' => t('Perform administration tasks for FB Like button block')
    ),
    'access fblikebutton' => array(
      'title' => t('Access FB Like button'),
    ),
  );
}


/**
 * Implementation of hook_block_info()
 */
function fblikebutton_block_info() {
  $fullnodedisplay = variable_get('fblikebutton_full_node_display', 0);
  $blocks['fblikebutton_static_block'] = array(
    'info' => t('Static FB Like button'),
  );
  if ($fullnodedisplay == 1) {
    $blocks['fblikebutton_dynamic_block'] = array(
      'info' => t('Dynamic FB Like button'),
    );
  }
  return $blocks;
}

/**
 * Implementation of hook_block_configure()
 */
function fblikebutton_block_configure($delta = '') {
  global $base_url;
  $form = array();
  if ($delta == 'fblikebutton_static_block') {
    $form['fblikebutton_static_block'] = array(
      '#type' => 'fieldset',
      '#title' => t('Static FB Like button block'),
      '#collapsible' => FALSE,
    );  
    $form['fblikebutton_static_block']['fblikebutton_static_config'] = array(
      '#markup' => '<p>' . t('To configure the URL and the appearance of the button go to the ' . l(t('static Like button settings'), 'admin/config/fblikebutton/static') . '. Make sure you set the right permissions on the ' . l(t('permissions page'), 'admin/people/permissions') . '.</p>'),
    );
  }
  if ($delta == 'fblikebutton_dynamic_block') {
    $form['fblikebutton_dynamic_block'] = array(
      '#type' => 'fieldset',
      '#title' => t('Dynamic FB Like button block'),
      '#collapsible' => FALSE,
    );  
    $form['fblikebutton_dynamic_block']['fblikebutton_dynamic_config'] = array(
      '#markup' => '<p>' . t('To configure the visibility and the appearance of the button go to the ' . l(t('dynamic Like button settings'), 'admin/config/fblikebutton/dynamic') . '. You can enhance the visibility settings by using the settings on this page. Make sure you set the right permissions on the ' . l(t('permissions page'), 'admin/people/permissions') . '.</p>'),
    );
  }
  return $form;
}

/**
 * Implementation of hook_block_view()
 */
function fblikebutton_block_view($delta = '') {
  global $base_url;
  $node = node_load(arg(1));
  $types = variable_get('fblikebutton_node_types', array());
  if ($node) {
    $show = ( ! empty($types[$node->type]) && user_access('access fblikebutton'));
  }
  else {
    $show = NULL;
  }
  $fullnodedisplay = variable_get('fblikebutton_full_node_display', 0);
  $block = array();
  switch ($delta) {
    case 'fblikebutton_dynamic_block':
      if ($show && $fullnodedisplay == 1) {
        $webpage_to_like = url("node/$node->nid", array('absolute' => TRUE));
        $conf = array(
          'layout' => variable_get('fblikebutton_layout', 'standard'),
          'action' => variable_get('fblikebutton_action', 'like'),
          'color_scheme' => variable_get('fblikebutton_color_scheme', 'light'),
          'show_faces' => variable_get('fblikebutton_show_faces', 'true'),
          'font' => variable_get('fblikebutton_font', 'arial'),
          'height' => variable_get('fblikebutton_iframe_height', '80'),
          'width' => variable_get('fblikebutton_iframe_width', '450'),
          'send' => variable_get('fblikebutton_send', 'true'),
          'other_css' => variable_get('fblikebutton_iframe_css', ''),
          'language' => variable_get('fblikebutton_bl_language', 'en_US'),
        );
        $block['content'] = _fblikebutton_field($webpage_to_like, $conf);
      }
      break;
    case 'fblikebutton_static_block':
      $addr = variable_get('fblikebutton_block_url', $base_url);
      $conf = array(
        'layout' => variable_get('fblikebutton_bl_layout', "standard"),
        'action' => variable_get('fblikebutton_bl_action', "like"),
        'color_scheme' => variable_get('fblikebutton_bl_color_scheme', "light"),
        'show_faces' => variable_get('fblikebutton_bl_show_faces', "false"),
        'font' => variable_get('fblikebutton_bl_font', "arial"),
        'height' => variable_get('fblikebutton_bl_iframe_height', '80'),
        'width' => variable_get('fblikebutton_bl_iframe_width', '450'),
        'other_css' => variable_get('fblikebutton_bl_iframe_css', ''),
        'language' => variable_get('fblikebutton_bl_language', 'en_US'),
      );
      $block['content'] = _fblikebutton_field($addr, $conf);
      break;
  }
  return $block;
}

function _fblikebutton_field($webpage_to_like, $conf) {
  $webpage_to_like = urlencode($webpage_to_like);
  $width = $conf['width'];
  $height = $conf['height'];
  $layout = $conf['layout'];
  $action = $conf['action'];
  $colorscheme = $conf['color_scheme'];
  $show_faces = $conf['show_faces'];
  $font = $conf['font'];
//  $send = $conf['send'];
  $other_css = $conf['other_css'];
  $language = $conf['language'];
  $params = "href={$webpage_to_like}&layout={$layout}&show_faces={$show_faces}&width={$width}px&font={$font}&height={$height}px&action={$action}&colorscheme={$colorscheme}&locale={$language}";
  $src = htmlentities($params);
  $output = '<iframe src="https://www.facebook.com/plugins/like.php?' . $src . '" scrolling="no" frameborder="0" style="border: none; overflow: hidden; width: ' . $width . 'px; height: ' . $height . 'px;' . $other_css . '" allowTransparency="true"></iframe>';
  return $output;
}