Projet

Général

Profil

Paste
Télécharger (21,9 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / libraries / libraries.admin.inc @ ed912c77

1
<?php
2

    
3
/**
4
 * @file
5
 * Provides administrative page and form callbacks for Libraries module.
6
 */
7

    
8
/**
9
 * Form generation callback for the libraries overview table.
10
 *
11
 * This is a form instead of a page to allow easier extending in contributed
12
 * modules.
13
 *
14
 * @param array $form
15
 *   An associative array containing the structure of the form.
16
 * @param array $form_state
17
 *   A keyed array containing the current state of the form.
18
 *
19
 * @return array
20
 *   The form array for the overview form.
21
 */
22
function libraries_admin_overview(array $form, array &$form_state) {
23
  // Only show variants for installed libraries.
24
  $header_installed = array(t('Name'), t('Version'), t('Variants'), t('Dependencies'), t('Provider'), t('Links'));
25
  // Only show status for libraries with an error.
26
  $header_error = array(t('Name'), t('Status'), t('Version'), t('Dependencies'), t('Provider'), t('Links'));
27
  // For unregistered libraries the only information we can show is the path.
28
  $header_unregistered = array(t('Name'), t('Path'));
29

    
30
  $rows_installed = array();
31
  $rows_error = array();
32
  $rows_unregistered = array();
33

    
34
  // Registered libraries: we prefer to use libraries_detect() since it provides
35
  // library metadata.
36
  $libraries_registered = libraries_detect();
37
  uasort($libraries_registered, 'libraries_admin_sort_title');
38

    
39
  // Unregistered libraries: modules can depend on Libraries API without sharing
40
  // metadata by using libraries_get_path(). Libraries can also be placed in the
41
  // filesystem that are incorrectly installed, a wrong version, or a standalone
42
  // not connected to any module. In these cases, libraries_get_libraries()
43
  // provides a full library list. Libraries found by libraries_get_libraries(),
44
  // but not identified by libraries_detect, are displayed in a separate table.
45
  $libraries_unregistered = libraries_get_libraries();
46
  natcasesort($libraries_unregistered);
47

    
48
  foreach ($libraries_registered as $machine_name => $library) {
49
    $actions = array();
50
    $row = array();
51

    
52
    if ($library['vendor url']) {
53
      $actions[] = l(t('Homepage'), $library['vendor url']);
54
    }
55
    if ($library['download url']) {
56
      $actions[] = l(t('Download'), $library['download url']);
57
    }
58

    
59
    $row['data'][] = l($library['name'], 'admin/reports/libraries/' . $machine_name);
60
    // Only show status for libraries with an error. See above.
61
    if (!$library['installed']) {
62
      $row['data'][] = drupal_ucfirst($library['error']);
63
    }
64
    $row['data'][] = isset($library['version']) ? $library['version'] : '';
65
    if ($library['installed']) {
66
      $row['data'][] = implode(', ', array_keys($library['variants']));
67
    }
68
    $row['data'][] = libraries_admin_get_dependencies($library);
69
    $row['data'][] = libraries_admin_get_provider_with_type($library);
70
    $row['data'][] = implode(' | ', $actions);
71
    $row['class'] = $library['installed'] ? array('ok') : array('warning');
72

    
73
    if ($library['installed']) {
74
      $rows_installed[] = $row;
75
    }
76
    else {
77
      $rows_error[] = $row;
78
    }
79

    
80
    // Filter registered libraries from unregistered libraries.
81
    unset($libraries_unregistered[$library['machine name']]);
82
  }
83

    
84
  // Build table of registered libraries with installed status.
85
  $form['libraries']['installed'] = array(
86
    '#theme' => 'libraries_table_with_title',
87
    '#title' => t('Installed'),
88
    '#header' => $header_installed,
89
    '#rows' => $rows_installed,
90
    '#description' => t('These libraries are registered and installed correctly.'),
91
    '#empty' => t('There are currently no libraries that are registered and installed.'),
92
  );
93

    
94
  // Build table of registered libraries with error status.
95
  $form['libraries']['error'] = array(
96
    '#theme' => 'libraries_table_with_title',
97
    '#title' => t('Uninstalled'),
98
    '#header' => $header_error,
99
    '#rows' => $rows_error,
100
    '#description' => t('These libraries are registered but not installed. They may not need to be installed in case a module or theme provides optional integration with a library.'),
101
    '#empty' => t('There are currently no libraries that are registered but not installed.'),
102
  );
103

    
104
  // Build table of unregistered libraries.
105
  foreach ($libraries_unregistered as $name => $path) {
106
    $rows_unregistered[] = array(
107
      'data' => array(
108
        $name,
109
        $path,
110
      ),
111
    );
112
  }
113
  $form['libraries']['unregistered'] = array(
114
    '#theme' => 'libraries_table_with_title',
115
    '#title' => t('Unregistered'),
116
    '#header' => $header_unregistered,
117
    '#rows' => $rows_unregistered,
118
    '#description' => t('These libraries were found in the filesystem but there is no metadata about them.'),
119
    // Do not show the table at all, if there are no unregistered libraries.
120
    '#access' => (bool) $libraries_unregistered,
121
  );
122

    
123
  // Clear the cached library information so that the library can be loaded if
124
  // it was just downloaded. Because these instructions use libraries_detect()
125
  // directly, they will never use the cached information, but this avoids the
126
  // overview showing a library as installed but it not being loadable.
127
  libraries_cache_clear();
128

    
129
  return $form;
130
}
131

    
132
/**
133
 * Form generation callback for the status overview for a single library.
134
 *
135
 * This is a form instead of a page to allow easier extending in contributed
136
 * modules.
137
 *
138
 * @param array $form
139
 *   An associative array containing the structure of the form.
140
 * @param array $form_state
141
 *   A keyed array containing the current state of the form.
142
 * @param array $library
143
 *   A library information array.
144
 *
145
 * @return array|null
146
 *   The form array for the status form or NULL if the library was not found.
147
 *
148
 * @todo Add some var_export($library)-style output
149
 */
150
function libraries_admin_library_status_form(array $form, array &$form_state, $library) {
151
  drupal_set_title(t('Status report for library %library', array('%library' => $library['name'])), PASS_THROUGH);
152

    
153
  if ($library['installed']) {
154
    drupal_set_message(t('The %name library is installed correctly.', array('%name' => $library['name'])));
155
    $form['status'] = libraries_admin_status_table($library);
156
  }
157
  else {
158
    drupal_set_message($library['error message'], 'error');
159
    switch ($library['error']) {
160
      case 'not found':
161
        $form['instructions'] = libraries_admin_instructions_missing($library);
162
        break;
163

    
164
      case 'not detected':
165
        $form['instructions'] = libraries_admin_instructions_undetected($library);;
166
        break;
167

    
168
      case 'not supported':
169
        $form['instructions'] = libraries_admin_instructions_unsupported($library);
170
        break;
171

    
172
      case 'missing dependency':
173
        $form['instructions']['instruction']['#markup'] = t('There is a missing dependency in your configuration that prevents this library from working properly.') . '<br>';
174
        break;
175

    
176
      case 'incompatible dependency':
177
        $form['instructions']['instruction']['#markup'] = t('There is an incompatible dependency in your configuration that prevents this library from working properly.') . '<br>';
178
        break;
179
    }
180
  }
181

    
182
  return $form;
183
}
184

    
185

    
186
/**
187
 * Displays a table of status information about a library.
188
 *
189
 * @param array $library
190
 *   A library information array.
191
 *
192
 * @return array
193
 *   A renderable array containing a table with status information.
194
 */
195
function libraries_admin_status_table(array $library) {
196
  $header = array(array(
197
    // @todo The title implies that other type of information is displayed, as
198
    //   well, but this is currently not the case.
199
    // @todo Use CSS instead of a <strong> element.
200
    'data' => '<strong>' . t('General information') . '</strong>',
201
    'colspan' => 2,
202
    'class' => 'table-heading',
203
    'no_striping' => TRUE,
204
  ));
205

    
206
  $rows = array();
207
  // @todo Use CSS instead of <strong> elements.
208
  $rows['name'] = array('<strong>' . t('Name') . '</strong>', check_plain($library['name']));
209
  $rows['machine_name'] = array('<strong>' . t('Machine name') . '</strong>', check_plain($library['machine name']));
210
  if ($library['vendor url']) {
211
    $rows['vendor_url'] = array('<strong>' . t('Vendor URL') . '</strong>', l($library['vendor url'], $library['vendor url']));
212
  }
213
  if ($library['download url']) {
214
    $rows['download_url'] = array('<strong>' . t('Download URL') . '</strong>', l($library['download url'], $library['download url']));
215
  }
216
  $rows['provider'] = array('<strong>' . t('Provider') . '</strong>', libraries_admin_get_provider_with_type($library));
217
  $rows['library_path'] = array('<strong>' . t('Library path') . '</strong>', $library['library path']);
218
  $rows['version'] = array('<strong>' . t('Version') . '</strong>', $library['version']);
219
  if (!empty($library['variants'])) {
220
    $rows['variants'] = array('<strong>' . t('Variants') . '</strong>', implode(', ', array_keys($library['variants'])));
221
  }
222

    
223
  return array(
224
    '#theme' => 'table',
225
    '#header' => $header,
226
    '#rows' => $rows,
227
  );
228
}
229

    
230
/**
231
 * Returns instructions for dealing with a missing library.
232
 *
233
 * @param array $library
234
 *   A library information array.
235
 *
236
 * @return array
237
 *   A renderable array containing the instructions.
238
 */
239
function libraries_admin_instructions_missing(array $library) {
240
  $build = array();
241

    
242
  $build['instruction']['#markup'] = t('Follow these steps to install the library:');
243

    
244
  $items = array();
245
  // 1. Download the library.
246
  // If no supported versions are specified, the latest version is
247
  // recommended.
248
  if (empty($library['versions'])) {
249
    $items[] = t('Download the latest version of the library <a href="@download-url">here</a>.', array(
250
      '@download-url' => $library['download url'],
251
    ));
252
  }
253
  // Otherwise, the latest supported version is recommended.
254
  else {
255
    $versions = array_keys($library['versions']);
256
    usort($versions, 'version_compare');
257
    $versions = array_reverse($versions);
258
    $version = $versions[0];
259
    $items[] = t('Download version %version of the library <a href="@download-url">here</a>.', array(
260
      '%version' => $version,
261
      '@download-url' => $library['download url'],
262
    ));
263
  }
264
  // 2. Unpack it.
265
  $items[] = t('If the library is an archive, i.e. if the file ending is for example <em>.tar.gz</em> or <em>.zip</em>, unpack it.');
266
  // 3. Create the libraries folder.
267
  $items[] = t('In the %library-directory directory of your Drupal installation create a %library directory.', array(
268
    '%library-directory' => 'sites/all/libraries',
269
    '%library' => $library['machine name'],
270
  ));
271
  // 4. Upload it.
272
  // If the library has variant-independent files, give the user the
273
  // location of an example file to check his filesystem against.
274
  if ($directory_layout = libraries_admin_directory_layout($library)) {
275
    $items[] = t('Upload the whole library (which can consist of multiple directories) into the newly created %library-path directory. The following files and directories should be contained in that directory: !directory-layout', array(
276
      '%library-path' => 'sites/all/libraries/' . $library['machine name'],
277
      '!directory-layout' => drupal_render($directory_layout),
278
    ));
279
  }
280
  else {
281
    $items[] = t('Upload the whole library (which can consist of multiple directories) into the newly created %library-path directory.', array(
282
      '%library-path' => 'sites/all/libraries/' . $library['machine name'],
283
    ));
284
  }
285
  // 5. Reload.
286
  $items[] = t('<a href="">Reload</a> the page. If successful, you should see status information about this library.');
287

    
288
  $build['steps'] = array(
289
    '#theme' => 'item_list',
290
    '#items' => $items,
291
    '#type' => 'ol'
292
  );
293

    
294
  return $build;
295
}
296

    
297

    
298
/**
299
 * Returns instructions for dealing with an undetected library.
300
 *
301
 * @param array $library
302
 *   A library information array.
303
 *
304
 * @return array
305
 *   A renderable array containing the instructions.
306
 */
307
function libraries_admin_instructions_undetected($library) {
308
  $build = array();
309
  // Re-check location.
310
  // @todo Avoid usage of <br> elements.
311
  $build['instruction']['#markup'] = t('Check that the whole library is located at %library-path.', array(
312
      '%library-path' => $library['library path'],
313
    )) . '<br>';
314
  // If the library has variant-independent files, give the user the
315
  // exact location of the files to check against.
316
  // @todo It should be possible to display even variant-specific files
317
  //   in case the variant is installed, but libraries_detect() does not
318
  //   detect variants if the library version cannot be detected.
319
  if ($directory_layout = libraries_admin_directory_layout($library)) {
320
    $build['directory_layout'] = $directory_layout;
321
    $build['directory_layout']['#prefix'] = t('The following files and directories should be contained in that directory:');
322
  }
323

    
324
  // If the library is placed correctly the library information is
325
  // incorrect.
326
  // This switch could be avoided by using $library['info type'], but that would
327
  // hinder properly translating these strings.
328
  $build['reload']['#markup'] = t('If you have moved any files, <a href="">reload</a> the page. If successful, you should see status information about this library.') . '<br>';
329
  $build['notice']['#markup'] = t('If the files are placed correctly and the version can still not be detected, the library information is incorrect.') . '<br>';
330

    
331
  $provider = libraries_admin_get_provider($library);
332
  switch ($library['info type']) {
333
    case 'module':
334
      $build['contact']['#markup'] = t('Contact the maintainer of the %module module to correct this.', array(
335
          '%module' => $provider,
336
        )) . '<br>';
337
      break;
338

    
339
    case 'theme':
340
      $build['contact']['#markup'] = t('Contact the maintainer of the %theme theme to correct this.', array(
341
          '%theme' => $provider,
342
        )) . '<br>';
343
      break;
344

    
345
    case 'info file':
346
      $build['contact']['#markup'] = t('Contact the maintainer of the %info-file info file to correct this.', array(
347
          '%info-file' => $provider,
348
        )) . '<br>';
349
      break;
350
  }
351
  return $build;
352
}
353

    
354

    
355
/**
356
 * Returns instructions for dealing with an unsupported library.
357
 *
358
 * @param array $library
359
 *   A library information array.
360
 *
361
 * @return array
362
 *   A renderable array containing the instructions.
363
 */
364
function libraries_admin_instructions_unsupported($library) {
365
  $build = array();
366
  $items = array();
367

    
368
  // Either download a different version of the library...
369
  $versions = array_keys($library['versions']);
370
  usort($versions, 'version_compare');
371
  $versions = array_reverse($versions);
372
  $version = $versions[0];
373
  $build['instruction']['#markup'] = t('Please install version %version of the library by following the following steps:',
374
    array(
375
      '%version' => $version,
376
    ));
377
  // 1. Delete the old library.
378
  $items[] = t('Delete the entire contents of the %library-path directory.',
379
    array(
380
      '%library-path' => $library['library path'],
381
    ));
382
  // 2. Download the new library.
383
  $items[] = t('Download version %version of the library <a href="@download-url">here</a>.',
384
    array(
385
      '%version' => $version,
386
      '@download-url' => $library['download url'],
387
    ));
388
  // 3. Unpack it.
389
  $items[] = t('If the library is an archive, i.e. if the file ending is for example <em>.tar.gz</em> or <em>.zip</em>, unpack it.');
390
  // 4. Upload the new library.
391
  // If the library has variant-independent files, give the user the
392
  // location of an example file to check his filesystem against.
393
  if ($directory_layout = libraries_admin_directory_layout($library)) {
394
    $items[] = t('Upload the new files into the %library-path directory. The following files and directories should be contained in that directory: !directory-layout',
395
      array(
396
        '%library-path' => $library['library path'],
397
        '!directory-layout' => drupal_render($directory_layout),
398
      ));
399
  }
400
  else {
401
    $items[] = t('Upload the new files into the %library-path directory.',
402
      array(
403
        '%library-path' => $library['library path'],
404
      ));
405
  }
406
  // 5. Reload.
407
  $items[] = t('<a href="">Reload</a> the page. If successful, you should see status information about this library.');
408
  $build['steps'] = array(
409
    '#theme' => 'item_list',
410
    '#items' => $items,
411
    '#type' => 'ol',
412
  );
413
  // ...or contact the maintainer of the library information.
414
  $provider = libraries_admin_get_provider($library);
415
  switch ($library['info type']) {
416
    case 'module':
417
      $build['contact']['#markup'] = t('If you are bound to version @version of the library, ask the maintainer of the %module module to provide support for it.', array(
418
          '@version' => $library['version'],
419
          '%module' => $provider,
420
        )) . '<br>';
421
      break;
422

    
423
    case 'theme':
424
      $build['contact']['#markup'] = t('If you are bound to version @version of the library, ask the maintainer of the %theme theme to provide support for it.', array(
425
          '@version' => $library['version'],
426
          '%theme' => $provider,
427
        )) . '<br>';
428
      break;
429

    
430
    case 'info file':
431
      $build['contact']['#markup'] = t('If you are bound to version @version of the library, ask the maintainer of the %info-file info file to provide support for it.', array(
432
          '@version' => $library['version'],
433
          '%info-file' => $provider,
434
        )) . '<br>';
435
      break;
436
  }
437
  return $build;
438
}
439

    
440
/**
441
 * Returns the directory layout of the library, if possible.
442
 *
443
 * The result of this function can help users to verify that they have uploaded
444
 * the library to the correct location.
445
 *
446
 * @param array $library
447
 *   A library information array.
448
 *
449
 * @return array|false
450
 *   A renderable array containing the directory layout of the library or FALSE
451
 *   if a directory layout could not be generated.
452
 */
453
function libraries_admin_directory_layout(array $library) {
454
  $build = array(
455
    '#theme' => 'item_list',
456
    '#type' => 'ul',
457
    '#items' => array(),
458
  );
459

    
460
  $items = &$build['#items'];
461
  if ($library['path']) {
462
    $items = &libraries_admin_path_to_tree($items, $library['path']);
463
  }
464
  foreach (array('js', 'css', 'php') as $type) {
465
    if (!empty($library['files'][$type])) {
466
      $files = array_keys($library['files'][$type]);
467
      foreach ($files as $file) {
468
        // Skip JavaScript settings.
469
        if (is_int($file)) {
470
          continue;
471
        }
472

    
473
        $children = &$items;
474
        libraries_admin_path_to_tree($children, $file);
475
      }
476
    }
477
  }
478
  return $build['#items'] ? $build : FALSE;
479
}
480

    
481
/**
482
 * Converts a file path into a tree structure for use in an item list.
483
 *
484
 * For example, the path 'foo/bar/baz' will be converted into the tree structure
485
 * represented by the following list:
486
 * - foo
487
 *   - bar
488
 *     - baz
489
 *
490
 * The $items array that is modified by reference or returned (see below) can
491
 * be used as the 'items' variable for theme_item_list().
492
 *
493
 * This function modifies passed-in $items array, so that multiple paths can
494
 * be placed into the same tree structure easily.
495
 *
496
 * @code
497
 *   $items = array();
498
 *   foreach ($paths as $path) {
499
 *     libraries_admin_path_to_tree($items, $path);
500
 *   }
501
 * @endcode
502
 *
503
 * It also returns the last item by reference, so that it can also be used to
504
 * traverse into a sub-structure and add further children there.
505
 *
506
 * @code
507
 *   $items = array();
508
 *   $children = &libraries_admin_path_to_tree($items, $path);
509
 *   foreach ($sub_paths as $sub_path) {
510
 *     libraries_admin_path_to_tree($children, $sub_path);
511
 *   }
512
 * @endcode
513
 *
514
 * @param array $items
515
 * @param string $path
516
 *
517
 * @return array
518
 */
519
function &libraries_admin_path_to_tree(array &$items, $path) {
520
  $part = strtok($path, '/');
521
  while ($part) {
522
    if (!isset($items[$part])) {
523
      $items[$part] = array(
524
        'data' => $part,
525
        'children' => array(),
526
      );
527
    }
528
    $items = &$items[$part]['children'];
529
    $part = strtok('/');
530
  }
531

    
532
  return $items;
533
}
534

    
535
/**
536
 * Sorts libraries by name.
537
 *
538
 * This function can be used as a callback for usort() or uasort().
539
 *
540
 * @param array $a
541
 *   The first library information array.
542
 * @param array $b
543
 *   The second library information array.
544
 *
545
 * @return int
546
 *   Returns -1 if $a is considered smaller than $b, 1 if $a considered greater
547
 *   than $b and 0 if $a and $b are considered equal.
548
 *
549
 * @see strnatcasecmp()
550
 * @see usort()
551
 * @see uasort()
552
 */
553
function libraries_admin_sort_title(array $a, array $b) {
554
  return strnatcasecmp($a['name'], $b['name']);
555
}
556

    
557
/**
558
 * Returns the library's dependencies, if any.
559
 *
560
 * @param array $library
561
 *   A library information array.
562
 *
563
 * @return string
564
 *   The dependencies.
565
 */
566
function libraries_admin_get_dependencies($library) {
567
  $dependencies = array();
568
  foreach ($library['dependencies'] as $dependency_name) {
569
    if ($dependency = libraries_info($dependency_name)) {
570
      $dependencies[] = $dependency['name'];
571
    }
572
    else {
573
      $dependencies[] = $dependency_name;
574
    }
575
  }
576
  return implode(', ', $dependencies);
577
}
578

    
579
/**
580
 * Returns the library's provider.
581
 *
582
 * The provider can be a module, a theme, or an info file.
583
 *
584
 * @param array $library
585
 *   A library information array.
586
 *
587
 * @return string
588
 *   The provider.
589
 */
590
function libraries_admin_get_provider($library) {
591
  $provider = '';
592

    
593
  switch ($library['info type']) {
594
    case 'module':
595
    case 'theme':
596
      $info = system_get_info($library['info type'], $library[$library['info type']]);
597
      $provider = $info['name'];
598
      break;
599

    
600
    case 'info file':
601
      $provider = basename($library['info file']);
602
      break;
603
  }
604

    
605
  return $provider;
606
}
607

    
608
/**
609
 * Returns the library's provider and provider type.
610
 *
611
 * The provider type is either 'module', 'theme', or 'info file'.
612
 *
613
 * @param array $library
614
 *   A library information array.
615
 *
616
 * @return string
617
 *   The provider and provider type.
618
 */
619
function libraries_admin_get_provider_with_type($library) {
620
  $provider = libraries_admin_get_provider($library);
621
  $provider_with_type = '';
622

    
623
  // This switch could be avoided by using $library['info type'], but that would
624
  // hinder properly translating these strings.
625
  switch ($library['info type']) {
626
    case 'module':
627
      $provider_with_type = t('%module module', array('%module' => $provider));
628
      break;
629

    
630
    case 'theme':
631
      $provider_with_type = t('%theme theme', array('%theme' => $provider));
632
      break;
633

    
634
    case 'info file':
635
      $provider_with_type = t('%info-file info file', array('%info-file' => $provider));
636
      break;
637
  }
638

    
639
  return $provider_with_type;
640
}