Projet

Général

Profil

Paste
Télécharger (14,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / twitter_block / twitter_block.module @ 74f6bef0

1
<?php
2

    
3
/**
4
 * @file
5
 * A module to provide simple Twitter blocks using the Twitter Search API.
6
 */
7

    
8
/**
9
 * Implements hook_help().
10
 */
11
function twitter_block_help($path, $arg) {
12
  switch ($path) {
13
    case 'admin/structure/block/add-twitter-block':
14
      return '<p>' . t('Use this page to create a new custom Twitter block.') . '</p>';
15
  }
16
}
17

    
18
/**
19
 * Implements hook_menu().
20
 */
21
function twitter_block_menu() {
22
  // Create an array of block settings
23
  $settings = array(
24
    'title' => 'Add Twitter block',
25
    'description' => 'Add a new Twitter block.',
26
    'page callback' => 'drupal_get_form',
27
    'page arguments' => array('twitter_block_add_block_form'),
28
    'type' => MENU_LOCAL_ACTION,
29
    'file' => 'twitter_block.admin.inc',
30
  );
31

    
32
  // Add a local action to the block configuration page
33
  $items['admin/structure/block/add-twitter-block'] = array(
34
    'access arguments' => array('administer blocks'),
35
  ) + $settings;
36

    
37
  // Get the default site theme
38
  $default_theme = variable_get('theme_default', 'bartik');
39

    
40
  // Add a local action to the per-theme block configuration pages
41
  foreach (list_themes() as $key => $theme) {
42
    if ($key != $default_theme) {
43
      $items['admin/structure/block/list/' . $key . '/add-twitter-block'] = array(
44
        'access callback' => '_twitter_block_themes_access',
45
        'access arguments' => array($theme),
46
      ) + $settings;
47
    }
48
  }
49

    
50
  $items['admin/structure/block/administer/twitter_block/%/delete'] = array(
51
    'title' => 'Delete Twitter block',
52
    'page callback' => 'drupal_get_form',
53
    'page arguments' => array('twitter_block_delete', 5),
54
    'access arguments' => array('administer blocks'),
55
    'type' => MENU_CALLBACK,
56
    'file' => 'twitter_block.admin.inc',
57
  );
58
  return $items;
59
}
60

    
61
/**
62
 * Menu item access callback - only admin or enabled themes can be accessed.
63
 */
64
function _twitter_block_themes_access($theme) {
65
  return user_access('administer blocks') && drupal_theme_access($theme);
66
}
67

    
68
/**
69
 * Implements hook_form_FORM_ID_alter();
70
 */
71
function twitter_block_form_block_admin_display_form_alter(&$form, &$form_state, $form_id) {
72
  $result = db_query('SELECT bid FROM {twitter_block}');
73

    
74
  // Add delete links to Twitter Block blocks
75
  foreach ($result as $block) {
76
    $form['blocks']['twitter_block_' . $block->bid]['delete'] = array(
77
      '#type' => 'link',
78
      '#title' => t('delete'),
79
      '#href' => 'admin/structure/block/administer/twitter_block/' . $block->bid . '/delete',
80
    );
81
  }
82
}
83

    
84
/**
85
 * Returns information from database about a user-created (Twitter) block.
86
 *
87
 * @param $bid
88
 *   ID of the block to get information for.
89
 *
90
 * @return
91
 *   Associative array of information stored in the database for this block.
92
 *   Array keys:
93
 *   - bid: Block ID.
94
 *   - info: Block description.
95
 *   - widget_id: Widget ID.
96
 *   - theme: Theme.
97
 *   - link_color: Link color.
98
 *   - width: Width.
99
 *   - height: Height.
100
 *   - chrome: Chrome.
101
 *   - border_color: Border color.
102
 *   - language: Language.
103
 *   - tweet_limit: Tweet limit.
104
 *   - related: Related users.
105
 *   - polite: ARIA politeness.
106
 */
107
function twitter_block_block_get($bid) {
108
  return db_query("SELECT * FROM {twitter_block} WHERE bid = :bid", array(':bid' => $bid))->fetchAssoc();
109
}
110

    
111
/**
112
 * Implements hook_block_info().
113
 */
114
function twitter_block_block_info() {
115
  $blocks = array();
116

    
117
  $result = db_query('SELECT bid, info FROM {twitter_block} ORDER BY info');
118
  foreach ($result as $block) {
119
    $blocks[$block->bid]['info'] = $block->info;
120
  }
121
  return $blocks;
122
}
123

    
124
/**
125
 * Implements hook_block_configure().
126
 */
127
function twitter_block_block_configure($delta = 0) {
128
  if ($delta) {
129
    $config = twitter_block_block_get($delta);
130

    
131
    // Unserialize the timeline settings.
132
    $data = unserialize($config['data']);
133

    
134
    // Remove the serialized timeline settings.
135
    unset($config['data']);
136

    
137
    // Add the timeline settings to the block settings.
138
    $twitter_block = $config + $data;
139
  }
140
  else {
141
    $twitter_block = array();
142
  }
143
  return twitter_block_custom_block_form($twitter_block);
144
}
145

    
146
/**
147
 * Form constructor for the Twitter block form.
148
 *
149
 * @param $edit
150
 *   (optional) An associative array of information retrieved by
151
 *   twitter_block_block_get() if an existing block is being edited, or an
152
 *   empty array otherwise. Defaults to array().
153
 *
154
 * @ingroup forms
155
 */
156
function twitter_block_custom_block_form($edit = array()) {
157
  $edit += array(
158
    'info' => '',
159
    'widget_id' => '',
160
    'theme' => '',
161
    'link_color' => '',
162
    'width' => '',
163
    'height' => '',
164
    'chrome' => array(),
165
    'border_color' => '',
166
    'language' => '',
167
    'tweet_limit' => '',
168
    'related' => '',
169
    'polite' => array(),
170
  );
171

    
172
  $form['info'] = array(
173
    '#type' => 'textfield',
174
    '#title' => t('Block description'),
175
    '#default_value' => $edit['info'],
176
    '#maxlength' => 64,
177
    '#description' => t('A brief description of your block. Used on the <a href="@overview">Blocks administration page</a>.', array('@overview' => url('admin/structure/block'))),
178
    '#required' => TRUE,
179
  );
180
  $form['widget_id'] = array(
181
    '#type' => 'textfield',
182
    '#title' => t('Widget ID'),
183
    '#default_value' => $edit['widget_id'],
184
    '#required' => TRUE,
185
    '#description' => t('Each Twitter Block block requires a unique widget ID which determines, among other things, the source (user timeline, favourites, list or search) of the tweets to display. You can view a list of your existing embedded timeline widgets (and their widget IDs) or create new embedded timeline widgets by visiting the <a href="@widgets_section">widgets section of your settings page</a> (make sure that you\'re logged in). You can determine a widget\'s ID by editing it and inspecting the URL (which should be in the form of twitter.com/settings/widgets/WIDGET_ID/edit) or by looking at the widget\'s embed code (look for data-widget-id="WIDGET_ID").', array('@widgets_section' => 'https://twitter.com/settings/widgets')),
186
  );
187
  $form['appearance'] = array(
188
    '#type' => 'fieldset',
189
    '#title' => t('Appearance'),
190
  );
191
  $form['appearance']['theme'] = array(
192
    '#type' => 'select',
193
    '#title' => t('Theme'),
194
    '#default_value' => $edit['theme'],
195
    '#options' => array(
196
      '' => t('Default'),
197
      'dark' => t('Dark'),
198
    ),
199
    '#description' => t('Select a theme for the widget.'),
200
  );
201
  $form['appearance']['link_color'] = array(
202
    '#type' => 'textfield',
203
    '#title' => t('Link color'),
204
    '#default_value' => $edit['link_color'],
205
    '#maxlength' => 6,
206
    '#size' => 6,
207
    '#field_prefix' => '#',
208
    '#description' => t('Change the link color used by the widget. Takes an %format hex format color. Note that some icons in the widget will also appear this color.', array('%format' => 'abc123')),
209
  );
210
  $form['appearance']['border_color'] = array(
211
    '#type' => 'textfield',
212
    '#title' => t('Border color'),
213
    '#default_value' => $edit['border_color'],
214
    '#maxlength' => 6,
215
    '#size' => 6,
216
    '#field_prefix' => '#',
217
    '#description' => t('Change the border color used by the widget. Takes an %format hex format color.', array('%format' => 'abc123')),
218
  );
219
  $form['appearance']['chrome'] = array(
220
    '#type' => 'checkboxes',
221
    '#title' => t('Chrome'),
222
    '#default_value' => $edit['chrome'],
223
    '#options' => array(
224
      'noheader' => t('No header'),
225
      'nofooter' => t('No footer'),
226
      'noborders' => t('No borders'),
227
      'noscrollbar' => t('No scrollbar'),
228
      'transparent' => t('Transparent'),
229
    ),
230
    '#description' => t('Control the widget layout and chrome.'),
231
  );
232
  $form['functionality'] = array(
233
    '#type' => 'fieldset',
234
    '#title' => t('Functionality'),
235
  );
236
  $form['functionality']['related'] = array(
237
    '#type' => 'textfield',
238
    '#title' => t('Related users'),
239
    '#default_value' => $edit['related'],
240
    '#description' => t('As per the Tweet and follow buttons, you may provide a comma-separated list of user screen names as suggested followers to a user after they reply, Retweet, or favorite a Tweet in the timeline.'),
241
  );
242
  $form['functionality']['tweet_limit'] = array(
243
    '#type' => 'select',
244
    '#title' => t('Tweet limit'),
245
    '#default_value' => $edit['tweet_limit'],
246
    '#options' => array('' => t('Auto')) + drupal_map_assoc(range(1, 20)),
247
    '#description' => t('Fix the size of a timeline to a preset number of Tweets between 1 and 20. The timeline will render the specified number of Tweets from the timeline, expanding the height of the widget to display all Tweets without scrolling. Since the widget is of a fixed size, it will not poll for updates when using this option.'),
248
  );
249
  $form['size'] = array(
250
    '#type' => 'fieldset',
251
    '#title' => t('Size'),
252
    '#description' => t('Embedded timelines are flexible and adaptive, functioning at a variety of dimensions ranging from wide to narrow, and short to tall. The default dimensions for a timeline are 520×600px, which can be overridden to fit the dimension requirements of your page. Setting a width is not required, and by default the widget will shrink to the width of its parent element in the page.'),
253
  );
254
  $form['size']['width'] = array(
255
    '#type' => 'textfield',
256
    '#title' => t('Width'),
257
    '#default_value' => $edit['width'],
258
    '#size' => 6,
259
    '#field_suffix' => 'px',
260
    '#description' => t('Change the width of the widget.'),
261
  );
262
  $form['size']['height'] = array(
263
    '#type' => 'textfield',
264
    '#title' => t('Height'),
265
    '#default_value' => $edit['height'],
266
    '#size' => 6,
267
    '#field_suffix' => 'px',
268
    '#description' => t('Change the height of the widget.'),
269
  );
270
  $form['size']['note'] = array(
271
    '#type' => 'markup',
272
    '#markup' => '<p>' . t('The minimum width of a timeline is 180px and the maximum is 520px. The minimum height is 200px.') . '</p>',
273
  );
274
  $form['accessibility'] = array(
275
    '#type' => 'fieldset',
276
    '#title' => t('Accessibility'),
277
  );
278
  $form['accessibility']['language'] = array(
279
    '#type' => 'textfield',
280
    '#title' => t('Language'),
281
    '#default_value' => $edit['language'],
282
    '#maxlength' => 5,
283
    '#size' => 5,
284
    '#description' => t('The widget language is detected from the page, based on the language of your content. Enter a <a href="@website">language code</a> to manually override the language.', array('@website' => 'http://www.w3.org/TR/html401/struct/dirlang.html#h-8.1.1')),
285
  );
286
  $form['accessibility']['polite'] = array(
287
    '#type' => 'select',
288
    '#title' => t('ARIA politeness'),
289
    '#options' => array(
290
      'polite' => t('Polite'),
291
      'assertive' => t('Assertive'),
292
    ),
293
    '#default_value' => $edit['polite'],
294
    '#description' => t('ARIA is an accessibility system that aids people using assistive technology interacting with dynamic web content. <a href="@website">Read more about ARIA on W3C\'s website</a>. By default, the embedded timeline uses the least obtrusive setting: "polite". If you\'re using an embedded timeline as a primary source of content on your page, you may wish to override this to the assertive setting, using "assertive".', array('@website' => 'http://www.w3.org/WAI/intro/aria.php')),
295
  );
296

    
297
  return $form;
298
}
299

    
300
/**
301
 * Implements hook_block_save().
302
 */
303
function twitter_block_block_save($delta = 0, $edit = array()) {
304
  twitter_block_custom_block_save($edit, $delta);
305
}
306

    
307
/**
308
 * Saves a user-created Twitter block in the database.
309
 *
310
 * @param $edit
311
 *   Associative array of fields to save. Array keys:
312
 *   - info: Block description.
313
 *   - widget_id: Widget ID.
314
 *   - theme: Theme.
315
 *   - link_color: Link color.
316
 *   - width: Width.
317
 *   - height: Height.
318
 *   - chrome: Chrome.
319
 *   - border_color: Border color.
320
 *   - language: Language.
321
 *   - tweet_limit: Tweet limit.
322
 *   - related: Related users.
323
 *   - polite: ARIA politeness.
324
 * @param $delta
325
 *   Block ID of the block to save.
326
 *
327
 * @return
328
 *   Always returns TRUE.
329
 */
330
function twitter_block_custom_block_save($edit, $delta) {
331
  // The serialized 'data' column contains the timeline settings.
332
  $data = array(
333
    'theme' => $edit['theme'],
334
    'link_color' => $edit['link_color'],
335
    'width' => $edit['width'],
336
    'height' => $edit['height'],
337
    'chrome' => $edit['chrome'],
338
    'border_color' => $edit['border_color'],
339
    'language' => $edit['language'],
340
    'tweet_limit' => $edit['tweet_limit'],
341
    'related' => $edit['related'],
342
    'polite' => $edit['polite'],
343
  );
344

    
345
  // Save the block configuration
346
  $delta = db_update('twitter_block')
347
    ->fields(array(
348
      'info' => $edit['info'],
349
      'widget_id' => $edit['widget_id'],
350
      'data' => serialize($data),
351
    ))
352
    ->condition('bid', $delta)
353
    ->execute();
354

    
355
  return TRUE;
356
}
357

    
358
/**
359
 * Implements hook_block_view().
360
 */
361
function twitter_block_block_view($delta) {
362
  // Load the configuration.
363
  $config = twitter_block_block_get($delta);
364

    
365
  // Unserialize the timeline 
366
  $data = unserialize($config['data']);
367

    
368
  $block = array();
369
  $block['subject'] = check_plain($config['info']);
370
  $block['content'] = array(
371
    '#theme' => 'link',
372
    '#text' => 'Tweets by @twitterapi',
373
    '#path' => 'https://twitter.com/twitterapi',
374
    '#options' => array(
375
      'attributes' => array(
376
        'class' => array('twitter-timeline'),
377
        'data-widget-id' => $config['widget_id'],
378
      ),
379
      'html' => FALSE,
380
    ),
381
    '#attached' => array(
382
      'js' => array(
383
        'https://platform.twitter.com/widgets.js' => array(
384
          'type' => 'external',
385
        ),
386
      ),
387
    ),
388
  );
389

    
390
  if (!empty($data['theme'])) {
391
    $block['content']['#options']['attributes']['data-theme'] = $data['theme'];
392
  }
393

    
394
  if (!empty($data['link_color'])) {
395
    $block['content']['#options']['attributes']['data-link-color'] = '#' . $data['link_color'];
396
  }
397

    
398
  if (!empty($data['width'])) {
399
    $block['content']['#options']['attributes']['width'] = $data['width'];
400
  }
401

    
402
  if (!empty($data['height'])) {
403
    $block['content']['#options']['attributes']['height'] = $data['height'];
404
  }
405

    
406
  if (!empty($data['chrome'])) {
407
    $options = array();
408

    
409
    foreach ($data['chrome'] as $option => $status) {
410
      if ($status) {
411
        $options[] = $option;
412
      }
413
    }
414

    
415
    if (count($options)) {
416
      $block['content']['#options']['attributes']['data-chrome'] = implode(' ', $options);
417
    }
418
  }
419

    
420
  if (!empty($data['border_color'])) {
421
    $block['content']['#options']['attributes']['data-border-color'] = '#' . $data['border_color'];
422
  }
423

    
424
  if (!empty($data['language'])) {
425
    $block['content']['#options']['attributes']['lang'] = $data['language'];
426
  }
427

    
428
  if (!empty($data['tweet_limit'])) {
429
    $block['content']['#options']['attributes']['data-tweet-limit'] = $data['tweet_limit'];
430
  }
431

    
432
  if (!empty($data['related'])) {
433
    $block['content']['#options']['attributes']['data-related'] = $data['related'];
434
  }
435

    
436
  if (!empty($data['polite'])) {
437
    $block['content']['#options']['attributes']['aria-polite'] = $data['polite'];
438
  }
439

    
440
  return $block;
441
}