Projet

Général

Profil

Paste
Télécharger (19,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / twitter_block / twitter_block.module @ 7fe061e8

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5
 * A module to provide simple Twitter blocks using the Twitter Search API.
6
 */
7
8 66b5cbf6 Assos Assos
/**
9
 * Implements hook_cron().
10
 */
11
function twitter_block_cron() {
12
  // Regenerate the JavaScript file every day.
13 a6e869e4 Assos Assos
  if (REQUEST_TIME - variable_get('twitter_block_last_cache', 0) >= 86400 && variable_get('twitter_block_cache', 0)) {
14 66b5cbf6 Assos Assos
    // 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 85ad3d82 Assos Assos
/**
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 a6e869e4 Assos Assos
    case 'admin/config/system/twitter-block':
30
      return '<p>' . t('Configure global settings for Twitter blocks.') . '</p>';
31 85ad3d82 Assos Assos
  }
32
}
33
34 a6e869e4 Assos Assos
/**
35
 * Implements hook_permission().
36
 */
37
function twitter_block_permission() {
38
  return array(
39
    'administer twitter block' => array(
40
      'title' => t('Administer Twitter Block'),
41
      'description' => t('Perform maintenance tasks for Twitter Block.'),
42
    ),
43
  );
44
}
45
46 85ad3d82 Assos Assos
/**
47
 * Implements hook_menu().
48
 */
49
function twitter_block_menu() {
50 66b5cbf6 Assos Assos
  // Create an array of block settings.
51 85ad3d82 Assos Assos
  $settings = array(
52
    'title' => 'Add Twitter block',
53
    'description' => 'Add a new Twitter block.',
54
    'page callback' => 'drupal_get_form',
55
    'page arguments' => array('twitter_block_add_block_form'),
56
    'type' => MENU_LOCAL_ACTION,
57
    'file' => 'twitter_block.admin.inc',
58
  );
59
60 66b5cbf6 Assos Assos
  // Add a local action to the block configuration page.
61 85ad3d82 Assos Assos
  $items['admin/structure/block/add-twitter-block'] = array(
62
    'access arguments' => array('administer blocks'),
63
  ) + $settings;
64
65 66b5cbf6 Assos Assos
  // Get the default site theme.
66 85ad3d82 Assos Assos
  $default_theme = variable_get('theme_default', 'bartik');
67
68 66b5cbf6 Assos Assos
  // Add a local action to the per-theme block configuration pages.
69 85ad3d82 Assos Assos
  foreach (list_themes() as $key => $theme) {
70
    if ($key != $default_theme) {
71
      $items['admin/structure/block/list/' . $key . '/add-twitter-block'] = array(
72
        'access callback' => '_twitter_block_themes_access',
73
        'access arguments' => array($theme),
74
      ) + $settings;
75
    }
76
  }
77
78
  $items['admin/structure/block/administer/twitter_block/%/delete'] = array(
79
    'title' => 'Delete Twitter block',
80
    'page callback' => 'drupal_get_form',
81
    'page arguments' => array('twitter_block_delete', 5),
82
    'access arguments' => array('administer blocks'),
83
    'type' => MENU_CALLBACK,
84
    'file' => 'twitter_block.admin.inc',
85
  );
86 a6e869e4 Assos Assos
87
  $items['admin/config/system/twitter-block'] = array(
88
    'title' => 'Twitter Block',
89
    'description' => 'Configure cache settings for Twitter blocks.',
90
    'page callback' => 'drupal_get_form',
91
    'page arguments' => array('twitter_block_admin_settings_form'),
92
    'access arguments' => array('administer twitter block'),
93
    'type' => MENU_NORMAL_ITEM,
94
    'file' => 'twitter_block.admin.inc',
95
  );
96
97 85ad3d82 Assos Assos
  return $items;
98
}
99
100
/**
101
 * Menu item access callback - only admin or enabled themes can be accessed.
102
 */
103
function _twitter_block_themes_access($theme) {
104
  return user_access('administer blocks') && drupal_theme_access($theme);
105
}
106
107
/**
108
 * Implements hook_form_FORM_ID_alter();
109
 */
110
function twitter_block_form_block_admin_display_form_alter(&$form, &$form_state, $form_id) {
111
  $result = db_query('SELECT bid FROM {twitter_block}');
112
113 66b5cbf6 Assos Assos
  // Add delete links to Twitter Block blocks.
114 85ad3d82 Assos Assos
  foreach ($result as $block) {
115
    $form['blocks']['twitter_block_' . $block->bid]['delete'] = array(
116
      '#type' => 'link',
117
      '#title' => t('delete'),
118
      '#href' => 'admin/structure/block/administer/twitter_block/' . $block->bid . '/delete',
119
    );
120
  }
121
}
122
123
/**
124
 * Returns information from database about a user-created (Twitter) block.
125
 *
126
 * @param $bid
127
 *   ID of the block to get information for.
128
 *
129
 * @return
130
 *   Associative array of information stored in the database for this block.
131
 *   Array keys:
132
 *   - bid: Block ID.
133
 *   - info: Block description.
134
 *   - widget_id: Widget ID.
135 66b5cbf6 Assos Assos
 *   - username: Account username.
136 85ad3d82 Assos Assos
 *   - theme: Theme.
137
 *   - link_color: Link color.
138
 *   - width: Width.
139
 *   - height: Height.
140
 *   - chrome: Chrome.
141
 *   - border_color: Border color.
142
 *   - language: Language.
143
 *   - tweet_limit: Tweet limit.
144
 *   - related: Related users.
145
 *   - polite: ARIA politeness.
146
 */
147
function twitter_block_block_get($bid) {
148
  return db_query("SELECT * FROM {twitter_block} WHERE bid = :bid", array(':bid' => $bid))->fetchAssoc();
149
}
150
151
/**
152
 * Implements hook_block_info().
153
 */
154
function twitter_block_block_info() {
155
  $blocks = array();
156
157
  $result = db_query('SELECT bid, info FROM {twitter_block} ORDER BY info');
158
  foreach ($result as $block) {
159
    $blocks[$block->bid]['info'] = $block->info;
160
  }
161
  return $blocks;
162
}
163
164
/**
165
 * Implements hook_block_configure().
166
 */
167
function twitter_block_block_configure($delta = 0) {
168
  if ($delta) {
169
    $config = twitter_block_block_get($delta);
170
171
    // Unserialize the timeline settings.
172
    $data = unserialize($config['data']);
173
174
    // Remove the serialized timeline settings.
175
    unset($config['data']);
176
177
    // Add the timeline settings to the block settings.
178
    $twitter_block = $config + $data;
179
  }
180
  else {
181
    $twitter_block = array();
182
  }
183
  return twitter_block_custom_block_form($twitter_block);
184
}
185
186
/**
187
 * Form constructor for the Twitter block form.
188
 *
189
 * @param $edit
190
 *   (optional) An associative array of information retrieved by
191
 *   twitter_block_block_get() if an existing block is being edited, or an
192
 *   empty array otherwise. Defaults to array().
193
 *
194
 * @ingroup forms
195
 */
196
function twitter_block_custom_block_form($edit = array()) {
197
  $edit += array(
198
    'info' => '',
199
    'widget_id' => '',
200 66b5cbf6 Assos Assos
    'username' => '',
201 85ad3d82 Assos Assos
    'theme' => '',
202
    'link_color' => '',
203
    'width' => '',
204
    'height' => '',
205
    'chrome' => array(),
206
    'border_color' => '',
207
    'language' => '',
208
    'tweet_limit' => '',
209
    'related' => '',
210
    'polite' => array(),
211
  );
212
213
  $form['info'] = array(
214
    '#type' => 'textfield',
215
    '#title' => t('Block description'),
216
    '#default_value' => $edit['info'],
217
    '#maxlength' => 64,
218
    '#description' => t('A brief description of your block. Used on the <a href="@overview">Blocks administration page</a>.', array('@overview' => url('admin/structure/block'))),
219
    '#required' => TRUE,
220
  );
221
  $form['widget_id'] = array(
222
    '#type' => 'textfield',
223
    '#title' => t('Widget ID'),
224
    '#default_value' => $edit['widget_id'],
225
    '#required' => TRUE,
226
    '#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')),
227
  );
228 66b5cbf6 Assos Assos
  $form['username'] = array(
229
    '#type' => 'textfield',
230
    '#title' => t('Username'),
231
    '#default_value' => $edit['username'],
232
    '#required' => TRUE,
233
    '#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')),
234
  );
235 85ad3d82 Assos Assos
  $form['appearance'] = array(
236
    '#type' => 'fieldset',
237
    '#title' => t('Appearance'),
238
  );
239
  $form['appearance']['theme'] = array(
240
    '#type' => 'select',
241
    '#title' => t('Theme'),
242
    '#default_value' => $edit['theme'],
243
    '#options' => array(
244
      '' => t('Default'),
245
      'dark' => t('Dark'),
246
    ),
247
    '#description' => t('Select a theme for the widget.'),
248
  );
249
  $form['appearance']['link_color'] = array(
250
    '#type' => 'textfield',
251
    '#title' => t('Link color'),
252
    '#default_value' => $edit['link_color'],
253
    '#maxlength' => 6,
254
    '#size' => 6,
255
    '#field_prefix' => '#',
256
    '#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')),
257
  );
258
  $form['appearance']['border_color'] = array(
259
    '#type' => 'textfield',
260
    '#title' => t('Border color'),
261
    '#default_value' => $edit['border_color'],
262
    '#maxlength' => 6,
263
    '#size' => 6,
264
    '#field_prefix' => '#',
265
    '#description' => t('Change the border color used by the widget. Takes an %format hex format color.', array('%format' => 'abc123')),
266
  );
267
  $form['appearance']['chrome'] = array(
268
    '#type' => 'checkboxes',
269
    '#title' => t('Chrome'),
270
    '#default_value' => $edit['chrome'],
271
    '#options' => array(
272
      'noheader' => t('No header'),
273
      'nofooter' => t('No footer'),
274
      'noborders' => t('No borders'),
275
      'noscrollbar' => t('No scrollbar'),
276
      'transparent' => t('Transparent'),
277
    ),
278
    '#description' => t('Control the widget layout and chrome.'),
279
  );
280
  $form['functionality'] = array(
281
    '#type' => 'fieldset',
282
    '#title' => t('Functionality'),
283
  );
284
  $form['functionality']['related'] = array(
285
    '#type' => 'textfield',
286
    '#title' => t('Related users'),
287
    '#default_value' => $edit['related'],
288
    '#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.'),
289
  );
290
  $form['functionality']['tweet_limit'] = array(
291
    '#type' => 'select',
292
    '#title' => t('Tweet limit'),
293
    '#default_value' => $edit['tweet_limit'],
294
    '#options' => array('' => t('Auto')) + drupal_map_assoc(range(1, 20)),
295
    '#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.'),
296
  );
297
  $form['size'] = array(
298
    '#type' => 'fieldset',
299
    '#title' => t('Size'),
300
    '#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.'),
301
  );
302
  $form['size']['width'] = array(
303
    '#type' => 'textfield',
304
    '#title' => t('Width'),
305
    '#default_value' => $edit['width'],
306
    '#size' => 6,
307
    '#field_suffix' => 'px',
308
    '#description' => t('Change the width of the widget.'),
309
  );
310
  $form['size']['height'] = array(
311
    '#type' => 'textfield',
312
    '#title' => t('Height'),
313
    '#default_value' => $edit['height'],
314
    '#size' => 6,
315
    '#field_suffix' => 'px',
316
    '#description' => t('Change the height of the widget.'),
317
  );
318
  $form['size']['note'] = array(
319
    '#type' => 'markup',
320
    '#markup' => '<p>' . t('The minimum width of a timeline is 180px and the maximum is 520px. The minimum height is 200px.') . '</p>',
321
  );
322
  $form['accessibility'] = array(
323
    '#type' => 'fieldset',
324
    '#title' => t('Accessibility'),
325
  );
326
  $form['accessibility']['language'] = array(
327
    '#type' => 'textfield',
328
    '#title' => t('Language'),
329
    '#default_value' => $edit['language'],
330
    '#maxlength' => 5,
331
    '#size' => 5,
332
    '#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')),
333
  );
334
  $form['accessibility']['polite'] = array(
335
    '#type' => 'select',
336
    '#title' => t('ARIA politeness'),
337
    '#options' => array(
338
      'polite' => t('Polite'),
339
      'assertive' => t('Assertive'),
340
    ),
341
    '#default_value' => $edit['polite'],
342
    '#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')),
343
  );
344
345
  return $form;
346
}
347
348
/**
349
 * Implements hook_block_save().
350
 */
351
function twitter_block_block_save($delta = 0, $edit = array()) {
352
  twitter_block_custom_block_save($edit, $delta);
353
}
354
355
/**
356
 * Saves a user-created Twitter block in the database.
357
 *
358
 * @param $edit
359
 *   Associative array of fields to save. Array keys:
360
 *   - info: Block description.
361
 *   - widget_id: Widget ID.
362 66b5cbf6 Assos Assos
 *   - username: Account username.
363 85ad3d82 Assos Assos
 *   - theme: Theme.
364
 *   - link_color: Link color.
365
 *   - width: Width.
366
 *   - height: Height.
367
 *   - chrome: Chrome.
368
 *   - border_color: Border color.
369
 *   - language: Language.
370
 *   - tweet_limit: Tweet limit.
371
 *   - related: Related users.
372
 *   - polite: ARIA politeness.
373
 * @param $delta
374
 *   Block ID of the block to save.
375
 *
376
 * @return
377
 *   Always returns TRUE.
378
 */
379
function twitter_block_custom_block_save($edit, $delta) {
380
  // The serialized 'data' column contains the timeline settings.
381
  $data = array(
382
    'theme' => $edit['theme'],
383
    'link_color' => $edit['link_color'],
384
    'width' => $edit['width'],
385
    'height' => $edit['height'],
386
    'chrome' => $edit['chrome'],
387
    'border_color' => $edit['border_color'],
388
    'language' => $edit['language'],
389
    'tweet_limit' => $edit['tweet_limit'],
390
    'related' => $edit['related'],
391
    'polite' => $edit['polite'],
392
  );
393
394 66b5cbf6 Assos Assos
  // Save the block configuration.
395 85ad3d82 Assos Assos
  $delta = db_update('twitter_block')
396
    ->fields(array(
397
      'info' => $edit['info'],
398
      'widget_id' => $edit['widget_id'],
399 66b5cbf6 Assos Assos
      'username' => $edit['username'],
400 85ad3d82 Assos Assos
      'data' => serialize($data),
401
    ))
402
    ->condition('bid', $delta)
403
    ->execute();
404
405
  return TRUE;
406
}
407
408
/**
409
 * Implements hook_block_view().
410
 */
411
function twitter_block_block_view($delta) {
412
  // Load the configuration.
413
  $config = twitter_block_block_get($delta);
414
415 66b5cbf6 Assos Assos
  // Unserialize the timeline.
416 85ad3d82 Assos Assos
  $data = unserialize($config['data']);
417
418
  $block = array();
419
  $block['subject'] = check_plain($config['info']);
420
  $block['content'] = array(
421 66b5cbf6 Assos Assos
    '#type' => 'link',
422
    '#title' => t('Tweets by @username', array('@username' => $config['username'])),
423
    '#href' => 'https://twitter.com/' . $config['username'],
424 85ad3d82 Assos Assos
    '#options' => array(
425
      'attributes' => array(
426
        'class' => array('twitter-timeline'),
427
        'data-widget-id' => $config['widget_id'],
428
      ),
429
      'html' => FALSE,
430
    ),
431
  );
432
433 66b5cbf6 Assos Assos
  // Use a locally cached copy of widgets.js by default.
434 a6e869e4 Assos Assos
  if (variable_get('twitter_block_cache', 0) && $url = twitter_block_cache()) {
435 66b5cbf6 Assos Assos
    $block['content']['#attached']['js'] = array(
436
      array(
437
        'data' => $url,
438
        'type' => 'file',
439
      ),
440
    );
441
  }
442
  else {
443
    $block['content']['#attached']['js'] = array(
444
      '//platform.twitter.com/widgets.js' => array(
445
        'type' => 'external',
446
        'scope' => 'footer',
447
      ),
448
    );
449
  }
450
451 85ad3d82 Assos Assos
  if (!empty($data['theme'])) {
452
    $block['content']['#options']['attributes']['data-theme'] = $data['theme'];
453
  }
454
455
  if (!empty($data['link_color'])) {
456
    $block['content']['#options']['attributes']['data-link-color'] = '#' . $data['link_color'];
457
  }
458
459
  if (!empty($data['width'])) {
460
    $block['content']['#options']['attributes']['width'] = $data['width'];
461
  }
462
463
  if (!empty($data['height'])) {
464
    $block['content']['#options']['attributes']['height'] = $data['height'];
465
  }
466
467
  if (!empty($data['chrome'])) {
468
    $options = array();
469
470
    foreach ($data['chrome'] as $option => $status) {
471
      if ($status) {
472
        $options[] = $option;
473
      }
474
    }
475
476
    if (count($options)) {
477
      $block['content']['#options']['attributes']['data-chrome'] = implode(' ', $options);
478
    }
479
  }
480
481
  if (!empty($data['border_color'])) {
482
    $block['content']['#options']['attributes']['data-border-color'] = '#' . $data['border_color'];
483
  }
484
485
  if (!empty($data['language'])) {
486
    $block['content']['#options']['attributes']['lang'] = $data['language'];
487
  }
488
489
  if (!empty($data['tweet_limit'])) {
490
    $block['content']['#options']['attributes']['data-tweet-limit'] = $data['tweet_limit'];
491
  }
492
493
  if (!empty($data['related'])) {
494
    $block['content']['#options']['attributes']['data-related'] = $data['related'];
495
  }
496
497
  if (!empty($data['polite'])) {
498 66b5cbf6 Assos Assos
    $block['content']['#options']['attributes']['data-aria-polite'] = $data['polite'];
499 85ad3d82 Assos Assos
  }
500
501
  return $block;
502
}
503 66b5cbf6 Assos Assos
504
/**
505
 * Download/Synchronize/Cache widget code file locally.
506
 *
507
 * @param $sync_cached_file
508
 *   Synchronize widget code and update if remote file have changed.
509
 * @return mixed
510
 *   The path to the local javascript file on success, boolean FALSE on failure.
511
 */
512
function twitter_block_cache($sync_cached_file = FALSE) {
513
  $location = 'http://platform.twitter.com/widgets.js';
514
  $path = 'public://twitter_block';
515
  $file_destination = $path . '/' . basename($location);
516
517
  if (!file_exists($file_destination) || $sync_cached_file) {
518
    // Download the latest widget code.
519
    $result = drupal_http_request($location);
520
521
    if ($result->code == 200) {
522
      if (file_exists($file_destination)) {
523
        // Synchronize widget code and and replace local file if outdated.
524
        $data_hash_local = drupal_hash_base64(file_get_contents($file_destination));
525
        $data_hash_remote = drupal_hash_base64($result->data);
526
        // Check that the files directory is writable.
527
        if ($data_hash_local != $data_hash_remote && file_prepare_directory($path)) {
528
          // Save updated widget code file to disk.
529
          file_unmanaged_save_data($result->data, $file_destination, FILE_EXISTS_REPLACE);
530
          watchdog('twitter_block', 'Locally cached widget code file has been updated.', array(), WATCHDOG_INFO);
531
532
          // Change query-strings on css/js files to enforce reload for all users.
533
          _drupal_flush_css_js();
534
        }
535
      }
536
      else {
537
        // Check that the files directory is writable.
538
        if (file_prepare_directory($path, FILE_CREATE_DIRECTORY)) {
539
          // There is no need to flush JS here as core refreshes JS caches
540
          // automatically, if new files are added.
541
          file_unmanaged_save_data($result->data, $file_destination, FILE_EXISTS_REPLACE);
542
          watchdog('twitter_block', 'Locally cached widget code file has been saved.', array(), WATCHDOG_INFO);
543
544
          // Return the local JS file path.
545
          return file_create_url($file_destination);
546
        }
547
      }
548
    }
549
  }
550
  else {
551
    // Return the local JS file path.
552
    return file_create_url($file_destination);
553
  }
554
}
555
556
/**
557
 * Delete cached files and directory.
558
 */
559
function twitter_block_clear_js_cache() {
560
  $path = 'public://twitter_block';
561
  if (file_prepare_directory($path)) {
562
    file_scan_directory($path, '/.*/', array('callback' => 'file_unmanaged_delete'));
563
    drupal_rmdir($path);
564
565
    // Change query-strings on css/js files to enforce reload for all users.
566
    _drupal_flush_css_js();
567
568
    watchdog('twitter_block', 'Local cache has been purged.', array(), WATCHDOG_INFO);
569
  }
570
}