You are here

function cpn_codemirror_version in Code per Node 7

Identify the version of CodeMirror that is currently available.

Return value

string If CodeMirror is not installed, returns FALSE. If version cannot be identified, returns TRUE, otherwise returns a string representation of the version of CodeMirror currently installed, e.g. '3.20.1'.

1 call to cpn_codemirror_version()
cpn_settings in ./cpn.admin.inc
Settings form.
1 string reference to 'cpn_codemirror_version'
cpn_uninstall in ./cpn.install
Implements hook_uninstall().

File

./cpn.module, line 814
Primary hook implementations.

Code

function cpn_codemirror_version() {
  static $version;

  // Only process this once per page load.
  if (is_null($version)) {
    $path = cpn_codemirror();
    $version = FALSE;

    // Ensure the actual codemirror JS file exists.
    $js_exists = file_exists($path . '/lib/codemirror.js');
    if ($js_exists) {
      $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) {

              // Record that CodeMirror is installed.
              $version = $package['version'];

              // Check the version that was installed the last time this page
              // was loaded.
              $old_ver = variable_get('cpn_codemirror_version', NULL);
              if ($old_ver != $version) {
                variable_set('cpn_codemirror_version', $version);

                // Notify the user that CodeMirror was updated.
                if (!empty($old_ver)) {
                  drupal_set_message(t('Updated the site configuration to note that CodeMirror v!ver is installed.', array(
                    '!ver' => $package['version'],
                  )));
                }
              }
            }
            else {
              $version = $package['version'];
              variable_set('cpn_codemirror_version', $version);
            }
          }
        }
      }

      // The version of CodeMirror installed could not be identified.
      if (!$identified) {

        // This version of CodeMirror may not be compatible, but we'll try to
        // load it anyway.
        $version = TRUE;
        variable_set('cpn_codemirror_version', TRUE);
      }
    }
  }
  return $version;
}