<?php

require_once dirname(__FILE__) . '/library_attach.field.inc';

/**
 * Implements hook_hook_info_alter().
 */
function library_attach_hook_info_alter(array &$info) {
  // Support the Views hooks being placed in library_attach.views.inc.
  $info += array(
    'views_api' => array('group' => 'views'),
    'views_plugins' => array('group' => 'views'),
    'views_pre_render' => array('group' => 'views'),
  );
}

/**
 * Get an associative array of library options for use in checkboxes.
 */
function library_attach_get_library_options() {
  $libraries = &drupal_static(__FUNCTION__);
  if (!isset($libraries)) {
    $libraries = array();
    foreach (module_list() as $module) {
      if ($module_libraries = drupal_get_library($module)) {
        foreach ($module_libraries as $name => $library) {
          $key = $module . ':' . $name;
          $libraries[$key] = check_plain($library['title']);
        }
      }
    }
    asort($libraries);
    // Allow modules to alter the list of library options.
    drupal_alter('library_attach_options', $libraries);
  }
  return $libraries;
}

/**
 * Implements hook_library_attach_options_alter() on behalf of system.module.
 */
function system_library_attach_options_alter(array &$libraries) {
  // Remove deprecated library namespaces that will cause duplicate options.
  unset($libraries['system:once']);
  unset($libraries['system:form']);
  unset($libraries['system:jquery-bbq']);
  unset($libraries['system:vertical-tabs']);
  unset($libraries['system:cookie']);

  // These libraries will be on every page if any JavaScript is included.
  unset($libraries['system:jquery']);
  unset($libraries['system:jquery.once']);
}

function _library_attach_get_actual_view_mode_used($entity_type, $bundle, $view_mode) {
  $view_mode_settings = field_view_mode_settings($entity_type, $bundle);
  return (!empty($view_mode_settings[$view_mode]['custom_settings']) ? $view_mode : 'default');
}

function library_attach_get_view_mode_libraries($entity_type, $bundle, $view_mode) {
  $settings = field_bundle_settings($entity_type, $bundle);
  return isset($settings['libraries'][$view_mode]) ? $settings['libraries'][$view_mode] : array();
}

function library_attach_set_view_mode_libraries($entity_type, $bundle, $view_mode, array $libraries) {
  $settings = field_bundle_settings($entity_type, $bundle);
  $settings['libraries'][$view_mode] = $libraries;
  field_bundle_settings($entity_type, $bundle, $settings);
}

function library_attach_add_libraries($libraries, &$render_element = NULL) {
  if (isset($render_element) && is_array($render_element)) {
    foreach ($libraries as $key) {
      list($module, $name) = explode(':', $key, 2);
      $render_element['#attached']['library'][] = array($module, $name);
    }
  }
  else {
    foreach ($libraries as $key) {
      list($module, $name) = explode(':', $key, 2);
      drupal_add_library($module, $name);
    }
  }
}

/**
 * Implements hook_registry_files_alter().
 */
function library_attach_registry_files_alter(array &$files, array $modules) {
  // Add the optional integration with Views by adding the necessary files.
  if (module_exists('views')) {
    $dir = drupal_get_path('module', 'library_attach') . '/views';
    foreach (file_scan_directory($dir, '/\.(php|inc)$/') as $filename => $file) {
      $files[$file->uri] = array(
        'module' => 'library_attach',
        'weight' => 0,
      );
    }
  }
}
