<?php

/**
 * @file
 * Install/requirements functions for Views Slideshow: Cycle.
 */

/**
 * Implements hook_requirements().
 */
function views_slideshow_cycle_requirements($phase) {
  $requirements = array();

  // If installing via Drush skip requirement checking here.
  if (php_sapi_name() == 'cli') {
    return $requirements;
  }

  // Ensure translations do not break at install time.
  $t = get_t();
  if ($phase == 'install' || $phase == 'runtime') {
    if (!drupal_get_path('module', 'libraries')) {
      $requirements['views_slideshow_cycle'] = array(
        'title' => t('Libraries module missing'),
        'severity' => REQUIREMENT_ERROR,
        'value' => t('Libraries module required for Views Slideshow Cycle'),
        'description' => $t('Views Slideshow Cycle module requires the <a href="@url">Libraries module</a> to be installed.', array('@url' => 'http://drupal.org/project/libraries')),
      );
    }
    else {
      if ($phase == 'install') {
        $views_slideshow = drupal_get_path('module', 'views_slideshow_cycle');
        require_once $views_slideshow . '/views_slideshow_cycle.module';
      }
      // Libraries module may not be installed,
      // even if it is listed as dependency in .info file.
      // For example, during installation from an installation profile
      // Libraries will *not* be installed when hook_requirements get called.
      if (!function_exists('libraries_get_path')) {
        include_once drupal_get_path('module', 'libraries') . '/libraries.module';
      }
      if (!$cycle_path = _views_slideshow_cycle_library_path()) {
        $requirements['views_slideshow_jquery_cycle'] = array(
          'title' => t('jQuery Cycle Library'),
          'severity' => REQUIREMENT_ERROR,
          'value' => t('Library required for Views Slideshow Cycle'),
          'description' => t('You need to install the jQuery cycle plugin. Create a directory in sites/all/libraries called jquery.cycle, and then copy jquery.cycle.all.js into it. You can find the plugin at !url.', array(
            '!url' => l(t('GitHub'), 'https://raw.githubusercontent.com/malsup/cycle/3.0.3/jquery.cycle.all.js', array('attributes' => array('target' => '_blank'))),
          )),
        );
      }
      else {
        $requirements['views_slideshow_jquery_cycle'] = array(
          'title' => t('jQuery Cycle Library'),
          'value' => t('Installed'),
          'severity' => REQUIREMENT_OK,
        );
      }
    }
  }
  return $requirements;
}
