5 * Provides administrative page and form callbacks for Libraries module.
9 * Form generation callback for the libraries overview table.
11 * This is a form instead of a page to allow easier extending in contributed
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.
20 * The form array for the overview form.
22 function libraries_admin_overview(array $form, array &$form_state) {
23 $header = array(t('Name'), t('Status'), t('Installed version'), t('Provider'), t('Links'));
26 $libraries = libraries_detect();
27 uasort($libraries, 'libraries_admin_sort_title');
29 foreach ($libraries as $machine_name => $library) {
32 if ($library['vendor url']) {
33 $actions[] = l('Homepage', $library['vendor url']);
35 if ($library['download url']) {
36 $actions[] = l('Download', $library['download url']);
41 l($library['name'], 'admin/reports/libraries/' . $machine_name),
42 ($library['installed'] ? t('OK') : drupal_ucfirst($library['error'])),
43 (isset($library['version']) ? $library['version'] : ''),
44 libraries_admin_get_provider_with_type($library),
45 implode(' | ', $actions),
47 'class' => ($library['installed'] ? array('ok') : array('error')),
51 $form['libraries']['list'] = array(
55 '#empty' => t('There are currently no libraries installed'),
62 * Form generation callback for the status overview for a single library.
64 * This is a form instead of a page to allow easier extending in contributed
68 * An associative array containing the structure of the form.
69 * @param array $form_state
70 * A keyed array containing the current state of the form.
71 * @param array $library
72 * A library information array.
75 * The form array for the status form or NULL if the library was not found.
77 * @todo Add some var_export($library)-style output
79 function libraries_admin_library_status_form(array $form, array &$form_state, $library) {
80 drupal_set_title(t('Status report for library %library', array('%library' => $library['name'])), PASS_THROUGH);
82 if ($library['installed']) {
83 drupal_set_message(t('The %name library is installed correctly.', array('%name' => $library['name'])));
84 $form['status'] = libraries_admin_status_table($library);
87 drupal_set_message($library['error message'], 'error');
88 switch ($library['error']) {
90 $form['instructions'] = libraries_admin_instructions_missing($library);
94 $form['instructions'] = libraries_admin_instructions_undetected($library);;
98 $form['instructions'] = libraries_admin_instructions_unsupported($library);
101 case 'missing dependency':
102 $form['instructions']['instruction']['#markup'] = t('There a missing dependency in your configuration that prevent this library to work properly.') . '<br>';
105 case 'incompatible dependency':
106 $form['instructions']['instruction']['#markup'] = t('There an incompatible dependency in your configuration that prevent this library to work properly.') . '<br>';
116 * Displays a table of status information about a library.
118 * @param array $library
119 * A library information array.
122 * A renderable array containing a table with status information.
124 function libraries_admin_status_table(array $library) {
125 $header = array(array(
126 // @todo The title implies that other type of information is displayed, as
127 // well, but this is currently not the case.
128 // @todo Use CSS instead of a <strong> element.
129 'data' => '<strong>' . t('General information') . '</strong>',
131 'class' => 'table-heading',
132 'no_striping' => TRUE,
136 // @todo Use CSS instead of <strong> elements.
137 $rows['name'] = array('<strong>' . t('Name') . '</strong>', check_plain($library['name']));
138 $rows['machine_name'] = array('<strong>' . t('Machine name') . '</strong>', check_plain($library['machine name']));
139 if ($library['vendor url']) {
140 $rows['vendor_url'] = array('<strong>' . t('Vendor URL') . '</strong>', l($library['vendor url'], $library['vendor url']));
142 if ($library['download url']) {
143 $rows['download_url'] = array('<strong>' . t('Download URL') . '</strong>', l($library['download url'], $library['download url']));
145 $rows['provider'] = array('<strong>' . t('Provider') . '</strong>', libraries_admin_get_provider_with_type($library));
146 $rows['library_path'] = array('<strong>' . t('Library path') . '</strong>', $library['library path']);
147 $rows['version'] = array('<strong>' . t('Version') . '</strong>', $library['version']);
148 if (!empty($library['variants'])) {
149 $rows['variants'] = array('<strong>' . t('Variants') . '</strong>', implode(', ', array_keys($library['variants'])));
154 '#header' => $header,
160 * Returns instructions for dealing with a missing library.
162 * @param array $library
163 * A library information array.
166 * A renderable array containing the instructions.
168 function libraries_admin_instructions_missing(array $library) {
171 $build['instruction']['#markup'] = t('Follow these steps to install the library:');
174 // 1. Download the library.
175 // If no supported versions are specified, the latest version is
177 if (empty($library['versions'])) {
178 $items[] = t('Download the latest version of the library <a href="@download-url">here</a>.', array(
179 '@download-url' => $library['download url'],
182 // Otherwise, the latest supported version is recommended.
184 $versions = array_keys($library['versions']);
185 usort($versions, 'version_compare');
186 $versions = array_reverse($versions);
187 $version = $versions[0];
188 $items[] = t('Download version %version of the library <a href="@download-url">here</a>.', array(
189 '%version' => $version,
190 '@download-url' => $library['download url'],
194 $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.');
195 // 3. Create the libraries folder.
196 $items[] = t('In the %library-directory directory of your Drupal installation create a %library directory.', array(
197 '%library-directory' => 'sites/all/libraries',
198 '%library' => $library['machine name'],
201 // If the library has variant-independent files, give the user the
202 // location of an example file to check his filesystem against.
203 if ($directory_layout = libraries_admin_directory_layout($library)) {
204 $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(
205 '%library-path' => 'sites/all/libraries/' . $library['machine name'],
206 '!directory-layout' => drupal_render($directory_layout),
210 $items[] = t('Upload the whole library (which can consist of multiple directories) into the newly created %library-path directory.', array(
211 '%library-path' => 'sites/all/libraries/' . $library['machine name'],
215 $items[] = t('<a href="">Reload</a> the page. If successful, you should see status information about this library.');
217 $build['steps'] = array(
218 '#theme' => 'item_list',
228 * Returns instructions for dealing with an undetected library.
230 * @param array $library
231 * A library information array.
234 * A renderable array containing the instructions.
236 function libraries_admin_instructions_undetected($library) {
238 // Re-check location.
239 // @todo Avoid usage of <br> elements.
240 $build['instruction']['#markup'] = t('Check that the whole library is located at %library-path.', array(
241 '%library-path' => $library['library path'],
243 // If the library has variant-independent files, give the user the
244 // exact location of the files to check against.
245 // @todo It should be possible to display even variant-specific files
246 // in case the variant is installed, but libraries_detect() does not
247 // detect variants if the library version cannot be detected.
248 if ($directory_layout = libraries_admin_directory_layout($library)) {
249 $build['directory_layout'] = $directory_layout;
250 $build['directory_layout']['#prefix'] = t('The following files and directories should be contained in that directory:');
253 // If the library is placed correctly the library information is
255 // This switch could be avoided by using $library['info type'], but that would
256 // hinder properly translating these strings.
257 $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>';
258 $build['notice']['#markup'] = t('If the files are placed correctly and the version can still not be detected, the library information is incorrect.') . '<br>';
260 $provider = libraries_admin_get_provider($library);
261 switch ($library['info type']) {
263 $build['contact']['#markup'] = t('Contact the maintainer of the %module module to correct this.', array(
264 '%module' => $provider,
269 $build['contact']['#markup'] = t('Contact the maintainer of the %theme theme to correct this.', array(
270 '%theme' => $provider,
275 $build['contact']['#markup'] = t('Contact the maintainer of the %info-file info file to correct this.', array(
276 '%info-file' => $provider,
285 * Returns instructions for dealing with an unsupported library.
287 * @param array $library
288 * A library information array.
291 * A renderable array containing the instructions.
293 function libraries_admin_instructions_unsupported($library) {
297 // Either download a different version of the library...
298 $versions = array_keys($library['versions']);
299 usort($versions, 'version_compare');
300 $versions = array_reverse($versions);
301 $version = $versions[0];
302 $build['instruction']['#markup'] = t('Please install version %version of the library by following the following steps:',
304 '%version' => $version,
306 // 1. Delete the old library.
307 $items[] = t('Delete the entire contents of the %library-path directory.',
309 '%library-path' => $library['library path'],
311 // 2. Download the new library.
312 $items[] = t('Download version %version of the library <a href="@download-url">here</a>.',
314 '%version' => $version,
315 '@download-url' => $library['download url'],
318 $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.');
319 // 4. Upload the new library.
320 // If the library has variant-independent files, give the user the
321 // location of an example file to check his filesystem against.
322 if ($directory_layout = libraries_admin_directory_layout($library)) {
323 $items[] = t('Upload the new files into the %library-path directory. The following files and directories should be contained in that directory: !directory-layout',
325 '%library-path' => $library['library path'],
326 '!directory-layout' => drupal_render($directory_layout),
330 $items[] = t('Upload the new files into the %library-path directory.',
332 '%library-path' => $library['library path'],
336 $items[] = t('<a href="">Reload</a> the page. If successful, you should see status information about this library.');
337 $build['steps'] = array(
338 '#theme' => 'item_list',
342 // ...or contact the maintainer of the library information.
343 $provider = libraries_admin_get_provider($library);
344 switch ($library['info type']) {
346 $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(
347 '@version' => $library['version'],
348 '%module' => $provider,
353 $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(
354 '@version' => $library['version'],
355 '%theme' => $provider,
360 $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(
361 '@version' => $library['version'],
362 '%info-file' => $provider,
370 * Returns the directory layout of the library, if possible.
372 * The result of this function can help users to verify that they have uploaded
373 * the library to the correct location.
375 * @param array $library
376 * A library information array.
378 * @return array|false
379 * A renderable array containing the directory layout of the library or FALSE
380 * if a directory layout could not be generated.
382 function libraries_admin_directory_layout(array $library) {
384 '#theme' => 'item_list',
389 $items = &$build['#items'];
390 if ($library['path']) {
391 $items = &libraries_admin_path_to_tree($items, $library['path']);
393 foreach (array('js', 'css', 'php') as $type) {
394 if (!empty($library['files'][$type])) {
395 $files = array_keys($library['files'][$type]);
396 foreach ($files as $file) {
397 // Skip JavaScript settings.
403 libraries_admin_path_to_tree($children, $file);
407 return $build['#items'] ? $build : FALSE;
411 * Converts a file path into a tree structure for use in an item list.
413 * For example, the path 'foo/bar/baz' will be converted into the tree structure
414 * represented by the following list:
419 * The $items array that is modified by reference or returned (see below) can
420 * be used as the 'items' variable for theme_item_list().
422 * This function modifies passed-in $items array, so that multiple paths can
423 * be placed into the same tree structure easily.
427 * foreach ($paths as $path) {
428 * libraries_admin_path_to_tree($items, $path);
432 * It also returns the last item by reference, so that it can also be used to
433 * traverse into a sub-structure and add further children there.
437 * $children = &libraries_admin_path_to_tree($items, $path);
438 * foreach ($sub_paths as $sub_path) {
439 * libraries_admin_path_to_tree($children, $sub_path);
443 * @param array $items
444 * @param string $path
448 function &libraries_admin_path_to_tree(array &$items, $path) {
449 $part = strtok($path, '/');
451 if (!isset($items[$part])) {
452 $items[$part] = array(
454 'children' => array(),
457 $items = &$items[$part]['children'];
465 * Sorts libraries by name.
467 * This function can be used as a callback for usort() or uasort().
470 * The first library information array.
472 * The second library information array.
475 * Returns -1 if $a is considered smaller than $b, 1 if $a considered greater
476 * than $b and 0 if $a and $b are considered equal.
478 * @see strnatcasecmp()
482 function libraries_admin_sort_title(array $a, array $b) {
483 return strnatcasecmp($a['name'], $b['name']);
487 * Returns the library's provider.
489 * The provider can be a module, a theme, or an info file.
491 * @param array $library
492 * A library information array.
497 function libraries_admin_get_provider($library) {
500 switch ($library['info type']) {
503 $info = system_get_info($library['info type'], $library[$library['info type']]);
504 $provider = $info['name'];
508 $provider = basename($library['info file']);
516 * Returns the library's provider and provider type.
518 * The provider type is either 'module', 'theme', or 'info file'.
520 * @param array $library
521 * A library information array.
524 * The provider and provider type.
526 function libraries_admin_get_provider_with_type($library) {
527 $provider = libraries_admin_get_provider($library);
528 $provider_with_type = '';
530 // This switch could be avoided by using $library['info type'], but that would
531 // hinder properly translating these strings.
532 switch ($library['info type']) {
534 $provider_with_type = t('%module module', array('%module' => $provider));
538 $provider_with_type = t('%theme theme', array('%theme' => $provider));
542 $provider_with_type = t('%info-file info file', array('%info-file' => $provider));
546 return $provider_with_type;