summaryrefslogtreecommitdiffstats
path: root/fb_likebox.module
blob: 1d12cd9338cefdd8107177ed1afa645aee483dbb (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
<?php

/**
 * @file
 * Simple module that provides a configurable block with Facebook Likebox's plugin.
 */

/**
 * Implements hook_block_info().
 */
function fb_likebox_block_info() {
  $blocks['0']['info'] = t('@site_name on Facebook', array('@site_name' => variable_get('site_name', 'Default site name')));
  $blocks['0']['cache'] = DRUPAL_NO_CACHE;
  return $blocks;
}

/**
 * Implements hook_block_configure().
 */
function fb_likebox_block_configure($delta = '') {
  switch ($delta) {
    case '0':
      // Facebook Widget settings.
      $form['fb_likebox_display_settings'] = array(
        '#type' => 'fieldset',
        '#title' => t('Display options'),
        '#collapsible' => FALSE,
      );
      $form['fb_likebox_theming_settings'] = array(
        '#type' => 'fieldset',
        '#title' => t('Theming Settings'),
        '#collapsible' => FALSE,
      );
      // Display settings.
      $form['fb_likebox_display_settings']['fb_likebox_url'] = array(
        '#type' => 'textfield',
        '#title' => t('Facebook Page URL'),
        '#default_value' => variable_get('fb_likebox_url', 'http://www.facebook.com/wikisaber.es'),
        '#description' => t('Enter the Facebook Page URL. I.e.: http://www.facebook.com/wikisaber.es'),
        '#required' => TRUE,
      );
      $form['fb_likebox_display_settings']['fb_likebox_colorscheme'] = array(
        '#type' => 'select',
        '#title' => t('Color Scheme'),
        '#default_value' => variable_get('fb_likebox_colorscheme', 'light'),
        '#options' => array(
          'light' => t('Light'),
          'dark' => t('Dark'),
        ),
        '#description' => t("The color scheme for the plugin. Options: 'light', 'dark'"),
        '#required' => TRUE,
      );
      $form['fb_likebox_display_settings']['fb_likebox_header'] = array(
        '#type' => 'select',
        '#title' => t('Header'),
        '#default_value' => variable_get('fb_likebox_header', 'true'),
        '#options' => array(
          'false' => t('No'),
          'true' => t('Yes'),
        ),
        '#description' => t('Specifies whether to display the Facebook header at the top of the plugin'),
        '#required' => TRUE,
      );
      $form['fb_likebox_display_settings']['fb_likebox_stream'] = array(
        '#type' => 'select',
        '#title' => t('Stream'),
        '#default_value' => variable_get('fb_likebox_stream', 'true'),
        '#options' => array(
          'false' => t('No'),
          'true' => t('Yes'),
        ),
        '#description' => t("Specifies whether to display a stream of the latest posts from the Page's wall"),
        '#required' => TRUE,
      );
      $form['fb_likebox_display_settings']['fb_likebox_show_faces'] = array(
        '#type' => 'select',
        '#title' => t('Show Faces'),
        '#default_value' => variable_get('fb_likebox_show_faces', 'true'),
        '#options' => array(
          'false' => t('No'),
          'true' => t('Yes'),
        ),
        '#description' => t('Specifies whether or not to display profile photos in the plugin.'),
        '#required' => TRUE,
      );
      $form['fb_likebox_display_settings']['fb_likebox_scrolling'] = array(
        '#type' => 'select',
        '#title' => t('Scrolling'),
        '#default_value' => variable_get('fb_likebox_scrolling', 'no'),
        '#options' => array(
          'no' => t('Disabled'),
          'yes' => t('Enabled'),
        ),
        '#description' => t('Enables vertical scrolling'),
        '#required' => TRUE,
      );
      // Theming settings.
      $form['fb_likebox_theming_settings']['fb_likebox_width'] = array(
        '#type' => 'textfield',
        '#title' => t('Width'),
        '#default_value' => variable_get('fb_likebox_width', '292'),
        '#description' => t('The width of the Facebook likebox in pixels'),
        '#required' => TRUE,
      );
      $form['fb_likebox_theming_settings']['fb_likebox_height'] = array(
        '#type' => 'textfield',
        '#title' => t('Height'),
        '#default_value' => variable_get('fb_likebox_height', '556'),
        '#description' => t('The height of the plugin in pixels. The default height provided by Facebook API varies based on number of faces to display, and whether the stream is displayed. With the stream displayed, and 10 faces the default height is 556px. With no faces, and no stream the default height is 63px. You will need to play with these value if you haved disabled those features and you want the block to be displayed without an empty section.'),
        '#required' => TRUE,
      );
      return $form;
  }
}

/**
 * Implements hook_block_view().
 */
function fb_likebox_block_view($delta = '') {
  switch ($delta) {
    case '0':
      // Get the values given in the admin form.
      $fb_url = variable_get('fb_likebox_url', 'http://www.facebook.com/wikisaber.es');
      $fb_colorscheme = variable_get('fb_likebox_colorscheme', 'light');
      $fb_header = variable_get('fb_likebox_header', 'true');
      $fb_stream = variable_get('fb_likebox_stream', 'true');
      $fb_show_faces = variable_get('fb_likebox_show_faces', 'true');
      $fb_scrolling = variable_get('fb_likebox_scrolling', 'no');
      $fb_width = variable_get('fb_likebox_width', '292');
      $fb_height = variable_get('fb_likebox_height', '556');
      // Set the subject of the block
      $block['subject'] = t('@site_name on Facebook', array('@site_name' => variable_get('site_name', 'Default site name')));
      // Call to the theming function.
      $block['content'] = theme('fb_likebox_facebook', array('fb_url' => $fb_url, 'fb_colorscheme' => $fb_colorscheme, 'fb_header' => $fb_header, 'fb_stream' => $fb_stream,
      'fb_show_faces' => $fb_show_faces, 'fb_scrolling' => $fb_scrolling, 'fb_width' => $fb_width, 'fb_height' => $fb_height, ));
      return $block;
  }
}

/**
 * Implements hook_block_save().
 */
function fb_likebox_block_save($delta = '', $edit = array()) {
  switch ($delta) {
    case '0':
      // Set the values given in the block form
      variable_set('fb_likebox_url', check_url($edit['fb_likebox_url']));
      variable_set('fb_likebox_colorscheme', check_plain($edit['fb_likebox_colorscheme']));
      variable_set('fb_likebox_header', check_plain($edit['fb_likebox_header']));
      variable_set('fb_likebox_stream', check_plain($edit['fb_likebox_stream']));
      variable_set('fb_likebox_show_faces', check_plain($edit['fb_likebox_show_faces']));
      variable_set('fb_likebox_scrolling', check_plain($edit['fb_likebox_scrolling']));
      variable_set('fb_likebox_width', check_plain($edit['fb_likebox_width']));
      variable_set('fb_likebox_height', check_plain($edit['fb_likebox_height']));
  }
}

/**
 * Implements hook_theme().
 */
function fb_likebox_theme($existing, $type, $theme, $path) {
  return array(
    'fb_likebox_facebook' => array(
      'variables' => array(
        'fb_url' => NULL,
        'fb_colorscheme' => NULL,
        'fb_header' => NULL,
        'fb_stream' => NULL,
        'fb_show_faces' => NULL,
        'fb_scrolling' => NULL,
        'fb_width' => NULL,
        'fb_height' => NULL,
      ),
      'template' => 'fb_likebox',
      'path' => $path . '/templates',
    ),
  );
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Adds validation of block configuration custom fields.
 */
function fb_likebox_form_block_admin_configure_alter(&$form, &$form_state, $form_id) {
  if ($form['module']['#value'] == 'fb_likebox') {
    $form['#validate'][] = '_fb_likebox_validate_block_settings';
  }
}


/**
 * Perform the validation of the block settings.
 */
function _fb_likebox_validate_block_settings(&$form, $form_state) {
  // Facebook display settings validation.
  $fb_url = $form_state['values']['fb_likebox_url'];
  if (!valid_url($fb_url, TRUE)) {
    form_set_error('fb_likebox_url', t('Please enter a valid url'));
  }

  // Facebook theming settings validation.
  $fb_width = $form_state['values']['fb_likebox_width'];
  if (!is_numeric($fb_width) || intval($fb_width) <= 0) {
    form_set_error('fb_likebox_width', t('Width should be a number bigger than 0'));
  }

  $fb_height = $form_state['values']['fb_likebox_height'];
  if (!is_numeric($fb_height) || intval($fb_height) <= 0) {
    form_set_error('fb_likebox_height', t('Height should be a number bigger than 0'));
  }
}