You are here

easychart.install in Easychart 7.2

Same filename and directory in other branches
  1. 7.3 easychart.install
  2. 7 easychart.install

Easychart install file.

File

easychart.install
View source
<?php

/**
 * @file
 * Easychart install file.
 */

/**
 * Implements hook_enable().
 */
function easychart_enable() {
  drupal_set_message(t('The Easychart  module was successfully enabled.'), 'ok');
  drupal_set_message(t('*** ATTENTION: This module requires HighchartsJS and EasychartJS.'), 'warning');
  drupal_set_message(t('               Use \'drush ec-dependencies\' to download these libraries.'), 'warning');
}
function easychart_requirements($phase) {
  $requirements = array();
  $t = get_t();
  if ($phase == 'runtime') {
    $highcharts = _get_lib_info('highcharts');
    $easychart = _get_lib_info('easychart');

    // regex
    $re = "/[^0-9]/";
    $subst = "";
    $msg = '';

    // available updates
    $recommended_release = array();
    $recommended_release['highcharts'] = (int) preg_replace($re, $subst, $highcharts['current_lib_info']['recommended']['version']);
    $recommended_release['easychart'] = (int) preg_replace($re, $subst, $easychart['current_lib_info']['recommended']['version']);
    $installed = array();
    $severity = REQUIREMENT_ERROR;
    $description = t('This module requires the <a href="' . $easychart['current_lib_info']['recommended']['uri'] . '">Easychart javascript library (v' . $easychart['current_lib_info']['recommended']['version'] . ')</a> and the <a href="' . $highcharts['current_lib_info']['recommended']['uri'] . '">Highcharts javascript library (v' . $highcharts['current_lib_info']['recommended']['version'] . ')</a> to be installed in the sites/all/libraries folder of your website.');
    $description .= t('<br>Drush installation: Donwload and install or update these libraries through the \'<code>drush ec-dependencies -y</code>\' command in your terminal/command line.');
    $installed['highcharts'] = (int) preg_replace($re, $subst, array_key_exists('version', $highcharts['current_lib_info']) ? $highcharts['current_lib_info']['version'] : 0);
    $installed['easychart'] = (int) preg_replace($re, $subst, array_key_exists('version', $easychart['current_lib_info']) ? $easychart['current_lib_info']['version'] : 0);
    $update_highchartsJS = $recommended_release['highcharts'] > $installed['highcharts'] ? true : false;
    $update_easychartJS = $recommended_release['easychart'] > $installed['easychart'] ? true : false;
    if ($highcharts['current_lib_info']['installed'] && $easychart['current_lib_info']['installed']) {
      if ($update_highchartsJS && $update_easychartJS) {

        // installed but not the latest/recommended version
        $msg = t('Updates available for your Highcharts and Easychart libraries.');
      }
      elseif ($update_highchartsJS || $update_easychartJS) {

        // installed but not the latest/recommended version
        if ($update_highchartsJS) {
          $msg = t('Updates available for your Highcharts library.<br>Your Easychart library is up to date.');
        }
        if ($update_easychartJS) {
          $msg = t('Updates available for your Easychart library.<br>Your Highcharts library is up to date.');
        }
      }
      else {

        // latest/recommended version is installed
        $severity = REQUIREMENT_OK;
        $msg = t('Your Highcharts and Easychart javascript libraries are up to date.');
        $description = '';
      }
    }
    else {

      // one of both libraries need to be installed
      $severity = REQUIREMENT_ERROR;
      if ($easychart['current_lib_info']['installed'] == false && $highcharts['current_lib_info']['installed'] == false) {
        $msg = t('The required javascript libraries could not be found.');
        $description .= t('<br>ATTENTION: Highcharts is free for personal, school or non-profit projects under the Creative Commons Attribution - Non Commercial 3.0 License. For commercial and governmental websites and projects, you need to buy a license. See <a href=\'http://shop.highsoft.com/highcharts.html\'>Highcharts License and Pricing</a>.');
      }
      else {
        if (!$easychart['current_lib_info']['installed']) {
          $msg = t('The required Easychart javascript library (v' . $easychart['current_lib_info']['recommended']['version'] . ') could not be found.');
        }
        else {
          if ($update_easychartJS) {
            $msg .= t('<br>Updates available for your Easychart library.');
          }
          else {
            $msg .= t('<br>No updates available for your Easychart library.');
          }
        }
        if (!$highcharts['current_lib_info']['installed']) {
          $msg = t('The required Highcharts javascript library (v' . $highcharts['current_lib_info']['recommended']['version'] . ') could not be found.');
          $description .= t('<br>ATTENTION: Highcharts is free for personal, school or non-profit projects under the Creative Commons Attribution - Non Commercial 3.0 License. For commercial and governmental websites and projects, you need to buy a license. See <a href=\'http://shop.highsoft.com/highcharts.html\'>Highcharts License and Pricing</a>.');
        }
        else {
          if ($update_highchartsJS) {
            $msg .= t('<br>Updates available for your Highcharts library.');
          }
          else {
            $msg .= t('<br>No updates available for your Highcharts library.');
          }
        }
      }
    }
    $requirements['easychart'] = array(
      'title' => $t('Easychart'),
      'description' => $description,
      'value' => $msg,
      'severity' => $severity,
    );
  }
  return $requirements;
}

/**
 * Implements hook_uninstall().
 */
function easychart_uninstall() {
  node_type_delete('easychart');
}

/**
 * Implements hook_field_schema().
 */
function easychart_field_schema($field) {
  $columns = array(
    'csv' => array(
      'type' => 'text',
      'size' => 'big',
      'not null' => FALSE,
    ),
    'csv_url' => array(
      'type' => 'text',
      'size' => 'medium',
      'not null' => FALSE,
    ),
    'config' => array(
      'type' => 'text',
      'size' => 'big',
      'not null' => FALSE,
    ),
    'stored' => array(
      'type' => 'text',
      'size' => 'big',
      'not null' => FALSE,
    ),
  );
  return array(
    'columns' => $columns,
  );
}

/**
 * Transpose all existing data.
 */
function easychart_update_7200() {
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'easychart')
    ->fieldCondition('easychart', 'csv', '', '!=');
  $result = $query
    ->execute();
  if (isset($result['node'])) {
    $easycharts = $result['node'];
    foreach ($easycharts as $easychart) {
      $node = node_load($easychart->nid);
      $data = field_get_items('node', $node, 'easychart');

      // Get separator.
      $separator = ',';
      $seps = array(
        ';',
        ',',
        '|',
        "\t",
      );
      $max = 0;
      foreach ($seps as $sep) {
        $count = substr_count($data[0]['csv'], $sep);
        if ($count > $max) {
          $separator = $sep;
          $max = $count;
        }
      }
      $new_data = array();
      $rows = str_getcsv($data[0]['csv'], "\n");

      //parse the rows
      foreach ($rows as &$row) {
        $new_data[] = str_getcsv($row, $separator);
      }
      $new_data = _transpose($new_data);

      // Clear the first cell if there are categories
      if (!is_numeric($new_data[1][0])) {
        $new_data[0][0] = '';
      }
      $node->easychart['und'][0]['csv'] = json_encode($new_data);
      field_attach_update('node', $node);
    }
  }
}

/*
 * Helper function to transpose an array
 */
function _transpose($array) {
  $transposed_array = array();
  if ($array) {
    foreach ($array as $row_key => $row) {
      if (is_array($row) && !empty($row)) {

        //check to see if there is a second dimension
        foreach ($row as $column_key => $element) {
          $transposed_array[$column_key][$row_key] = $element;
        }
      }
      else {
        $transposed_array[0][$row_key] = $row;
      }
    }
    return $transposed_array;
  }
}

/**
 * Adds storage for an external CSV url.
 */
function easychart_update_7100() {
  $spec = array(
    'type' => 'text',
    'description' => '',
    'size' => 'medium',
    'not null' => FALSE,
  );
  $data_table_name = 'field_data_easychart';
  $revision_table_name = 'field_revision_easychart';
  $field_name = 'easychart_csv_url';
  db_add_field($data_table_name, $field_name, $spec);
  db_add_field($revision_table_name, $field_name, $spec);
}

Functions

Namesort descending Description
easychart_enable Implements hook_enable().
easychart_field_schema Implements hook_field_schema().
easychart_requirements
easychart_uninstall Implements hook_uninstall().
easychart_update_7100 Adds storage for an external CSV url.
easychart_update_7200 Transpose all existing data.
_transpose