Projet

Général

Profil

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

root / drupal7 / sites / all / modules / twitter_block / twitter_block.module @ 66b5cbf6

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_cron().
10
 */
11
function twitter_block_cron() {
12
  // Regenerate the JavaScript file every day.
13
  if (REQUEST_TIME - variable_get('twitter_block_last_cache', 0) >= 86400 && variable_get('twitter_block_cache', TRUE)) {
14
    // Synchronize the widget code and update it if remote file have changed.
15
    twitter_block_cache(TRUE);
16

    
17
    // Record when the synchronization occurred.
18
    variable_set('twitter_block_last_cache', REQUEST_TIME);
19
  }
20
}
21

    
22
/**
23
 * Implements hook_help().
24
 */
25
function twitter_block_help($path, $arg) {
26
  switch ($path) {
27
    case 'admin/structure/block/add-twitter-block':
28
      return '<p>' . t('Use this page to create a new custom Twitter block.') . '</p>';
29
  }
30
}
31

    
32
/**
33
 * Implements hook_menu().
34
 */
35
function twitter_block_menu() {
36
  // Create an array of block settings.
37
  $settings = array(
38
    'title' => 'Add Twitter block',
39
    'description' => 'Add a new Twitter block.',
40
    'page callback' => 'drupal_get_form',
41
    'page arguments' => array('twitter_block_add_block_form'),
42
    'type' => MENU_LOCAL_ACTION,
43
    'file' => 'twitter_block.admin.inc',
44
  );
45

    
46
  // Add a local action to the block configuration page.
47
  $items['admin/structure/block/add-twitter-block'] = array(
48
    'access arguments' => array('administer blocks'),
49
  ) + $settings;
50

    
51
  // Get the default site theme.
52
  $default_theme = variable_get('theme_default', 'bartik');
53

    
54
  // Add a local action to the per-theme block configuration pages.
55
  foreach (list_themes() as $key => $theme) {
56
    if ($key != $default_theme) {
57
      $items['admin/structure/block/list/' . $key . '/add-twitter-block'] = array(
58
        'access callback' => '_twitter_block_themes_access',
59
        'access arguments' => array($theme),
60
      ) + $settings;
61
    }
62
  }
63

    
64
  $items['admin/structure/block/administer/twitter_block/%/delete'] = array(
65
    'title' => 'Delete Twitter block',
66
    'page callback' => 'drupal_get_form',
67
    'page arguments' => array('twitter_block_delete', 5),
68
    'access arguments' => array('administer blocks'),
69
    'type' => MENU_CALLBACK,
70
    'file' => 'twitter_block.admin.inc',
71
  );
72
  return $items;
73
}
74

    
75
/**
76
 * Menu item access callback - only admin or enabled themes can be accessed.
77
 */
78
function _twitter_block_themes_access($theme) {
79
  return user_access('administer blocks') && drupal_theme_access($theme);
80
}
81

    
82
/**
83
 * Implements hook_form_FORM_ID_alter();
84
 */
85
function twitter_block_form_block_admin_display_form_alter(&$form, &$form_state, $form_id) {
86
  $result = db_query('SELECT bid FROM {twitter_block}');
87

    
88
  // Add delete links to Twitter Block blocks.
89
  foreach ($result as $block) {
90
    $form['blocks']['twitter_block_' . $block->bid]['delete'] = array(
91
      '#type' => 'link',
92
      '#title' => t('delete'),
93
      '#href' => 'admin/structure/block/administer/twitter_block/' . $block->bid . '/delete',
94
    );
95
  }
96
}
97

    
98
/**
99
 * Returns information from database about a user-created (Twitter) block.
100
 *
101
 * @param $bid
102
 *   ID of the block to get information for.
103
 *
104
 * @return
105
 *   Associative array of information stored in the database for this block.
106
 *   Array keys:
107
 *   - bid: Block ID.
108
 *   - info: Block description.
109
 *   - widget_id: Widget ID.
110
 *   - username: Account username.
111
 *   - theme: Theme.
112
 *   - link_color: Link color.
113
 *   - width: Width.
114
 *   - height: Height.
115
 *   - chrome: Chrome.
116
 *   - border_color: Border color.
117
 *   - language: Language.
118
 *   - tweet_limit: Tweet limit.
119
 *   - related: Related users.
120
 *   - polite: ARIA politeness.
121
 */
122
function twitter_block_block_get($bid) {
123
  return db_query("SELECT * FROM {twitter_block} WHERE bid = :bid", array(':bid' => $bid))->fetchAssoc();
124
}
125

    
126
/**
127
 * Implements hook_block_info().
128
 */
129
function twitter_block_block_info() {
130
  $blocks = array();
131

    
132
  $result = db_query('SELECT bid, info FROM {twitter_block} ORDER BY info');
133
  foreach ($result as $block) {
134
    $blocks[$block->bid]['info'] = $block->info;
135
  }
136
  return $blocks;
137
}
138

    
139
/**
140
 * Implements hook_block_configure().
141
 */
142
function twitter_block_block_configure($delta = 0) {
143
  if ($delta) {
144
    $config = twitter_block_block_get($delta);
145

    
146
    // Unserialize the timeline settings.
147
    $data = unserialize($config['data']);
148

    
149
    // Remove the serialized timeline settings.
150
    unset($config['data']);
151

    
152
    // Add the timeline settings to the block settings.
153
    $twitter_block = $config + $data;
154
  }
155
  else {
156
    $twitter_block = array();
157
  }
158
  return twitter_block_custom_block_form($twitter_block);
159
}
160

    
161
/**
162
 * Form constructor for the Twitter block form.
163
 *
164
 * @param $edit
165
 *   (optional) An associative array of information retrieved by
166
 *   twitter_block_block_get() if an existing block is being edited, or an
167
 *   empty array otherwise. Defaults to array().
168
 *
169
 * @ingroup forms
170
 */
171
function twitter_block_custom_block_form($edit = array()) {
172
  $edit += array(
173
    'info' => '',
174
    'widget_id' => '',
175
    'username' => '',
176
    'theme' => '',
177
    'link_color' => '',
178
    'width' => '',
179
    'height' => '',
180
    'chrome' => array(),
181
    'border_color' => '',
182
    'language' => '',
183
    'tweet_limit' => '',
184
    'related' => '',
185
    'polite' => array(),
186
  );
187

    
188
  $form['info'] = array(
189
    '#type' => 'textfield',
190
    '#title' => t('Block description'),
191
    '#default_value' => $edit['info'],
192
    '#maxlength' => 64,
193
    '#description' => t('A brief description of your block. Used on the <a href="@overview">Blocks administration page</a>.', array('@overview' => url('admin/structure/block'))),
194
    '#required' => TRUE,
195
  );
196
  $form['widget_id'] = array(
197
    '#type' => 'textfield',
198
    '#title' => t('Widget ID'),
199
    '#default_value' => $edit['widget_id'],
200
    '#required' => TRUE,
201
    '#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')),
202
  );
203
  $form['username'] = array(
204
    '#type' => 'textfield',
205
    '#title' => t('Username'),
206
    '#default_value' => $edit['username'],
207
    '#required' => TRUE,
208
    '#description' => t('A Twitter account username. This is used to generate a fallback link to the Twitter account associated with the widget when JavaScript is not available. You can find your account username by visting the <a href="@account_section">account section of your settings page</a> or on your profile page in the URL or prefixed with @ under your display name.', array('@account_section' => 'https://twitter.com/settings/account')),
209
  );
210
  $form['appearance'] = array(
211
    '#type' => 'fieldset',
212
    '#title' => t('Appearance'),
213
  );
214
  $form['appearance']['theme'] = array(
215
    '#type' => 'select',
216
    '#title' => t('Theme'),
217
    '#default_value' => $edit['theme'],
218
    '#options' => array(
219
      '' => t('Default'),
220
      'dark' => t('Dark'),
221
    ),
222
    '#description' => t('Select a theme for the widget.'),
223
  );
224
  $form['appearance']['link_color'] = array(
225
    '#type' => 'textfield',
226
    '#title' => t('Link color'),
227
    '#default_value' => $edit['link_color'],
228
    '#maxlength' => 6,
229
    '#size' => 6,
230
    '#field_prefix' => '#',
231
    '#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')),
232
  );
233
  $form['appearance']['border_color'] = array(
234
    '#type' => 'textfield',
235
    '#title' => t('Border color'),
236
    '#default_value' => $edit['border_color'],
237
    '#maxlength' => 6,
238
    '#size' => 6,
239
    '#field_prefix' => '#',
240
    '#description' => t('Change the border color used by the widget. Takes an %format hex format color.', array('%format' => 'abc123')),
241
  );
242
  $form['appearance']['chrome'] = array(
243
    '#type' => 'checkboxes',
244
    '#title' => t('Chrome'),
245
    '#default_value' => $edit['chrome'],
246
    '#options' => array(
247
      'noheader' => t('No header'),
248
      'nofooter' => t('No footer'),
249
      'noborders' => t('No borders'),
250
      'noscrollbar' => t('No scrollbar'),
251
      'transparent' => t('Transparent'),
252
    ),
253
    '#description' => t('Control the widget layout and chrome.'),
254
  );
255
  $form['functionality'] = array(
256
    '#type' => 'fieldset',
257
    '#title' => t('Functionality'),
258
  );
259
  $form['functionality']['related'] = array(
260
    '#type' => 'textfield',
261
    '#title' => t('Related users'),
262
    '#default_value' => $edit['related'],
263
    '#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.'),
264
  );
265
  $form['functionality']['tweet_limit'] = array(
266
    '#type' => 'select',
267
    '#title' => t('Tweet limit'),
268
    '#default_value' => $edit['tweet_limit'],
269
    '#options' => array('' => t('Auto')) + drupal_map_assoc(range(1, 20)),
270
    '#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.'),
271
  );
272
  $form['size'] = array(
273
    '#type' => 'fieldset',
274
    '#title' => t('Size'),
275
    '#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.'),
276
  );
277
  $form['size']['width'] = array(
278
    '#type' => 'textfield',
279
    '#title' => t('Width'),
280
    '#default_value' => $edit['width'],
281
    '#size' => 6,
282
    '#field_suffix' => 'px',
283
    '#description' => t('Change the width of the widget.'),
284
  );
285
  $form['size']['height'] = array(
286
    '#type' => 'textfield',
287
    '#title' => t('Height'),
288
    '#default_value' => $edit['height'],
289
    '#size' => 6,
290
    '#field_suffix' => 'px',
291
    '#description' => t('Change the height of the widget.'),
292
  );
293
  $form['size']['note'] = array(
294
    '#type' => 'markup',
295
    '#markup' => '<p>' . t('The minimum width of a timeline is 180px and the maximum is 520px. The minimum height is 200px.') . '</p>',
296
  );
297
  $form['accessibility'] = array(
298
    '#type' => 'fieldset',
299
    '#title' => t('Accessibility'),
300
  );
301
  $form['accessibility']['language'] = array(
302
    '#type' => 'textfield',
303
    '#title' => t('Language'),
304
    '#default_value' => $edit['language'],
305
    '#maxlength' => 5,
306
    '#size' => 5,
307
    '#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')),
308
  );
309
  $form['accessibility']['polite'] = array(
310
    '#type' => 'select',
311
    '#title' => t('ARIA politeness'),
312
    '#options' => array(
313
      'polite' => t('Polite'),
314
      'assertive' => t('Assertive'),
315
    ),
316
    '#default_value' => $edit['polite'],
317
    '#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')),
318
  );
319

    
320
  return $form;
321
}
322

    
323
/**
324
 * Implements hook_block_save().
325
 */
326
function twitter_block_block_save($delta = 0, $edit = array()) {
327
  twitter_block_custom_block_save($edit, $delta);
328
}
329

    
330
/**
331
 * Saves a user-created Twitter block in the database.
332
 *
333
 * @param $edit
334
 *   Associative array of fields to save. Array keys:
335
 *   - info: Block description.
336
 *   - widget_id: Widget ID.
337
 *   - username: Account username.
338
 *   - theme: Theme.
339
 *   - link_color: Link color.
340
 *   - width: Width.
341
 *   - height: Height.
342
 *   - chrome: Chrome.
343
 *   - border_color: Border color.
344
 *   - language: Language.
345
 *   - tweet_limit: Tweet limit.
346
 *   - related: Related users.
347
 *   - polite: ARIA politeness.
348
 * @param $delta
349
 *   Block ID of the block to save.
350
 *
351
 * @return
352
 *   Always returns TRUE.
353
 */
354
function twitter_block_custom_block_save($edit, $delta) {
355
  // The serialized 'data' column contains the timeline settings.
356
  $data = array(
357
    'theme' => $edit['theme'],
358
    'link_color' => $edit['link_color'],
359
    'width' => $edit['width'],
360
    'height' => $edit['height'],
361
    'chrome' => $edit['chrome'],
362
    'border_color' => $edit['border_color'],
363
    'language' => $edit['language'],
364
    'tweet_limit' => $edit['tweet_limit'],
365
    'related' => $edit['related'],
366
    'polite' => $edit['polite'],
367
  );
368

    
369
  // Save the block configuration.
370
  $delta = db_update('twitter_block')
371
    ->fields(array(
372
      'info' => $edit['info'],
373
      'widget_id' => $edit['widget_id'],
374
      'username' => $edit['username'],
375
      'data' => serialize($data),
376
    ))
377
    ->condition('bid', $delta)
378
    ->execute();
379

    
380
  return TRUE;
381
}
382

    
383
/**
384
 * Implements hook_block_view().
385
 */
386
function twitter_block_block_view($delta) {
387
  // Load the configuration.
388
  $config = twitter_block_block_get($delta);
389

    
390
  // Unserialize the timeline.
391
  $data = unserialize($config['data']);
392

    
393
  $block = array();
394
  $block['subject'] = check_plain($config['info']);
395
  $block['content'] = array(
396
    '#type' => 'link',
397
    '#title' => t('Tweets by @username', array('@username' => $config['username'])),
398
    '#href' => 'https://twitter.com/' . $config['username'],
399
    '#options' => array(
400
      'attributes' => array(
401
        'class' => array('twitter-timeline'),
402
        'data-widget-id' => $config['widget_id'],
403
      ),
404
      'html' => FALSE,
405
    ),
406
  );
407

    
408
  // Use a locally cached copy of widgets.js by default.
409
  if (variable_get('twitter_block_cache', TRUE) && $url = twitter_block_cache()) {
410
    $block['content']['#attached']['js'] = array(
411
      array(
412
        'data' => $url,
413
        'type' => 'file',
414
      ),
415
    );
416
  }
417
  else {
418
    $block['content']['#attached']['js'] = array(
419
      '//platform.twitter.com/widgets.js' => array(
420
        'type' => 'external',
421
        'scope' => 'footer',
422
      ),
423
    );
424
  }
425

    
426
  if (!empty($data['theme'])) {
427
    $block['content']['#options']['attributes']['data-theme'] = $data['theme'];
428
  }
429

    
430
  if (!empty($data['link_color'])) {
431
    $block['content']['#options']['attributes']['data-link-color'] = '#' . $data['link_color'];
432
  }
433

    
434
  if (!empty($data['width'])) {
435
    $block['content']['#options']['attributes']['width'] = $data['width'];
436
  }
437

    
438
  if (!empty($data['height'])) {
439
    $block['content']['#options']['attributes']['height'] = $data['height'];
440
  }
441

    
442
  if (!empty($data['chrome'])) {
443
    $options = array();
444

    
445
    foreach ($data['chrome'] as $option => $status) {
446
      if ($status) {
447
        $options[] = $option;
448
      }
449
    }
450

    
451
    if (count($options)) {
452
      $block['content']['#options']['attributes']['data-chrome'] = implode(' ', $options);
453
    }
454
  }
455

    
456
  if (!empty($data['border_color'])) {
457
    $block['content']['#options']['attributes']['data-border-color'] = '#' . $data['border_color'];
458
  }
459

    
460
  if (!empty($data['language'])) {
461
    $block['content']['#options']['attributes']['lang'] = $data['language'];
462
  }
463

    
464
  if (!empty($data['tweet_limit'])) {
465
    $block['content']['#options']['attributes']['data-tweet-limit'] = $data['tweet_limit'];
466
  }
467

    
468
  if (!empty($data['related'])) {
469
    $block['content']['#options']['attributes']['data-related'] = $data['related'];
470
  }
471

    
472
  if (!empty($data['polite'])) {
473
    $block['content']['#options']['attributes']['data-aria-polite'] = $data['polite'];
474
  }
475

    
476
  return $block;
477
}
478

    
479
/**
480
 * Download/Synchronize/Cache widget code file locally.
481
 *
482
 * @param $sync_cached_file
483
 *   Synchronize widget code and update if remote file have changed.
484
 * @return mixed
485
 *   The path to the local javascript file on success, boolean FALSE on failure.
486
 */
487
function twitter_block_cache($sync_cached_file = FALSE) {
488
  $location = 'http://platform.twitter.com/widgets.js';
489
  $path = 'public://twitter_block';
490
  $file_destination = $path . '/' . basename($location);
491

    
492
  if (!file_exists($file_destination) || $sync_cached_file) {
493
    // Download the latest widget code.
494
    $result = drupal_http_request($location);
495

    
496
    if ($result->code == 200) {
497
      if (file_exists($file_destination)) {
498
        // Synchronize widget code and and replace local file if outdated.
499
        $data_hash_local = drupal_hash_base64(file_get_contents($file_destination));
500
        $data_hash_remote = drupal_hash_base64($result->data);
501
        // Check that the files directory is writable.
502
        if ($data_hash_local != $data_hash_remote && file_prepare_directory($path)) {
503
          // Save updated widget code file to disk.
504
          file_unmanaged_save_data($result->data, $file_destination, FILE_EXISTS_REPLACE);
505
          watchdog('twitter_block', 'Locally cached widget code file has been updated.', array(), WATCHDOG_INFO);
506

    
507
          // Change query-strings on css/js files to enforce reload for all users.
508
          _drupal_flush_css_js();
509
        }
510
      }
511
      else {
512
        // Check that the files directory is writable.
513
        if (file_prepare_directory($path, FILE_CREATE_DIRECTORY)) {
514
          // There is no need to flush JS here as core refreshes JS caches
515
          // automatically, if new files are added.
516
          file_unmanaged_save_data($result->data, $file_destination, FILE_EXISTS_REPLACE);
517
          watchdog('twitter_block', 'Locally cached widget code file has been saved.', array(), WATCHDOG_INFO);
518

    
519
          // Return the local JS file path.
520
          return file_create_url($file_destination);
521
        }
522
      }
523
    }
524
  }
525
  else {
526
    // Return the local JS file path.
527
    return file_create_url($file_destination);
528
  }
529
}
530

    
531
/**
532
 * Delete cached files and directory.
533
 */
534
function twitter_block_clear_js_cache() {
535
  $path = 'public://twitter_block';
536
  if (file_prepare_directory($path)) {
537
    file_scan_directory($path, '/.*/', array('callback' => 'file_unmanaged_delete'));
538
    drupal_rmdir($path);
539

    
540
    // Change query-strings on css/js files to enforce reload for all users.
541
    _drupal_flush_css_js();
542

    
543
    watchdog('twitter_block', 'Local cache has been purged.', array(), WATCHDOG_INFO);
544
  }
545
}