You are here

function cpn_requirements in Code per Node 7

Implement hook_requirements().

Check if CodeMirror is installed, if it isn't inform the user.

File

./cpn.install, line 12
Installation, schema and update hook implementations.

Code

function cpn_requirements($phase) {
  $requirements = array();

  // Look for CodeMirror.
  if (module_exists('libraries')) {
    $path = libraries_get_path('codemirror');
  }
  else {
    $path = 'sites/all/libraries/codemirror';
  }
  $dir_exists = file_exists($path);
  $js_exists = file_exists($path . '/lib/codemirror.js');

  // Install time.
  if ($phase == 'install') {

    // Ensure translations don't break at install time
    $t = get_t();

    // The directory doesn't exist.
    if (!$dir_exists) {
      $requirements[] = array(
        'title' => 'Code per Node',
        'value' => $t('<a href="!url" title="CodeMirror">CodeMirror</a> is not available.', array(
          '!url' => 'http://codemirror.net/',
        )),
        'description' => $t('It is recommended to install <a href="!url" title="CodeMirror">CodeMirror</a> (v3.20 or newer), it will add syntax highlighting and many other great benefits to the CSS and JS fields.', array(
          '!url' => 'http://codemirror.net/',
        )),
        'severity' => REQUIREMENT_INFO,
      );
    }
    elseif (!$js_exists) {
      $requirements[] = array(
        'title' => 'Code per Node',
        'value' => $t('<a href="!url" title="CodeMirror">CodeMirror</a> is not installed correctly.', array(
          '!url' => 'http://codemirror.net/',
        )),
        'description' => $t('The "!dir" directory exists but could not find "!path", please verify CodeMirror was installed correctly.', array(
          '!dir' => $path,
          '!path' => $path . '/lib/codemirror.js',
        )),
        'severity' => REQUIREMENT_INFO,
      );
    }
  }
  elseif ($phase == 'runtime') {

    // CodeMirror is not available.
    if (!$dir_exists) {
      $requirements[] = array(
        'title' => 'Code per Node',
        'value' => t('<a href="!url" title="CodeMirror">CodeMirror</a> is not available.', array(
          '!url' => 'http://codemirror.net/',
        )),
        'description' => t('Installing <a href="!url" title="CodeMirror">CodeMirror</a> (v3.20 or newer) will add syntax highlighting and many other great benefits to the CSS and JS fields.', array(
          '!url' => 'http://codemirror.net/',
        )),
        'severity' => REQUIREMENT_INFO,
      );
    }
    else {
      $identified = TRUE;
      $package_file = $path . '/package.json';

      // Verify that the package.json file is available.
      if (!file_exists($package_file) || !is_readable($package_file)) {
        $identified = FALSE;
      }
      else {

        // Load the package file.
        $package = file_get_contents($package_file);

        // Could not load the package file.
        if (empty($package)) {
          $identified = FALSE;
        }
        else {
          $package = json_decode($package, TRUE);

          // Cannot parse the package file or there is no version string
          // present.
          if (empty($package) || empty($package['version'])) {
            $identified = FALSE;
          }
          else {

            // Identify what version is installed.
            $version_parts = explode('.', $package['version']);

            // The version string should contain at least two parts, e.g.
            // "3,20.0", if it doesn't then something is wrong.
            if (empty($version_parts) || count($version_parts) < 2) {
              $identified = FALSE;
            }
            elseif ($version_parts[0] == 3 && $version_parts[1] >= 20 || $version_parts[0] >= 4) {
              $requirements[] = array(
                'title' => 'Code per Node',
                'value' => t('CodeMirror v!ver is installed.', array(
                  '!ver' => $package['version'],
                )),
                'description' => t('CodeMirror is installed and it is a compatible version. Excellent!'),
                'severity' => REQUIREMENT_OK,
              );
            }
            else {
              $requirements[] = array(
                'title' => 'Code per Node',
                'value' => t('Please update CodeMirror to v3.20 or newer, v!ver is installed.', array(
                  '!ver' => $package['version'],
                )),
                'description' => t('Code per Node has been built against <a href="!url" title="CodeMirror">CodeMirror v3.20</a>, anything older may be incompatible.', array(
                  '!url' => 'http://codemirror.net/',
                )),
                'severity' => REQUIREMENT_WARNING,
              );
            }
          }
        }
      }

      // The version of CodeMirror installed could not be identified.
      if (!$identified) {
        $requirements[] = array(
          'title' => 'Code per Node',
          'value' => t('Could not identify the version of CodeMirror available, replace it with v3.20 or newer.'),
          'description' => t('Code per Node has been built against <a href="!url" title="CodeMirror">CodeMirror v3.20</a>, anything older is most likely incompatible.'),
          'severity' => REQUIREMENT_WARNING,
        );
      }
    }
  }
  return $requirements;
}