You are here

views_calc.install in Views Calc 5

Same filename and directory in other branches
  1. 6.3 views_calc.install
  2. 6 views_calc.install
  3. 7 views_calc.install

File

views_calc.install
View source
<?php

function views_calc_install() {
  global $db_type;
  switch ($db_type) {
    case 'mysql':
    case 'mysqli':
      $ret[] = db_query("CREATE TABLE if not exists {views_calc_fields} (\n        cid int(10) unsigned NOT NULL default '0',\n        label varchar(255),\n        format varchar(255),\n        custom varchar(255),\n        tablelist text,\n        fieldlist text,\n        calc text,\n        KEY (cid)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */");
      break;
    case 'postgres':
      $ret[] = db_query("CREATE TABLE if not exists {views_calc_fields} (\n        cid int(10) unsigned NOT NULL default '0',\n        label varchar(255),\n        format varchar(255),\n        custom varchar(255),\n        tablelist text,\n        fieldlist text,\n        calc text,\n        KEY (cid)\n      ) ");
      break;
  }
}
function views_calc_uninstall() {
  $view_calcs = (array) variable_get('views_calc_vid', '');
  foreach ($view_calcs as $view_id) {
    variable_del('views_calc_' . $view_id . '_col');
    variable_del('views_calc_' . $view_id . '_col_calc');
    variable_del('views_calc_' . $view_id . '_row');
    variable_del('views_calc_' . $view_id . '_row_calc');
  }
  variable_del('views_calc_vid');
  variable_del('views_calc_operators');
  db_query("DROP TABLE {views_calc_fields}");
}

/**
 * Convert the queryname stored in the variable to the fullname
 * so we can access both the fieldname and the tablename from
 * the selections.
 *
 * Change the calc 'AVERAGE' to 'AVG' to match the db function.
 */
function views_calc_update_1() {
  $ret = array();
  include_once drupal_get_path('module', 'views') . '/views.module';
  include_once drupal_get_path('module', 'views_calc') . '/views_calc.module';
  $view_calcs = (array) variable_get('views_calc_vid', '');
  foreach ($view_calcs as $view_id) {
    if ($view = views_get_view($view_id)) {
      $cols = (array) variable_get('views_calc_' . $view->vid . '_col', '');
      $new_cols = array();
      foreach ($cols as $col) {
        foreach ($view->field as $field) {
          if ($field['queryname'] == $col) {
            $new_cols[] = $field['fullname'];
          }
        }
      }
      variable_set('views_calc_' . $view->vid . '_col', $new_cols);
      $rows = (array) variable_get('views_calc_' . $view->vid . '_row', '');
      $new_rows = array();
      foreach ($rows as $row) {
        foreach ($view->field as $field) {
          if ($field['queryname'] == $col) {
            $new_rows[] = $field['fullname'];
          }
        }
      }
      variable_set('views_calc_' . $view->vid . '_row', $new_rows);
      $col_calc = (array) variable_get('views_calc_' . $view->vid . '_col_calc', '');
      foreach ($col_calc as $calc) {
        if ($calc == 'AVERAGE') {
          $new_calcs[] = 'AVG';
        }
        else {
          $new_calcs[] = $calc;
        }
      }
      variable_set('views_calc_' . $view->vid . '_col_calc', $new_calcs);
      $row_calc = (array) variable_get('views_calc_' . $view->vid . '_row_calc', '');
      foreach ($row_calc as $calc) {
        if ($calc == 'AVERAGE') {
          $new_calcs[] = 'AVG';
        }
        else {
          $new_calcs[] = $calc;
        }
      }
      variable_set('views_calc_' . $view->vid . '_row_calc', $new_calcs);
    }
  }
  return $ret;
}

/**
 * Create a table to store custom views calculation fields.
 */
function views_calc_update_2() {
  $ret = array();
  global $db_type;
  switch ($db_type) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("CREATE TABLE if not exists {views_calc_fields} (\n        cid int(10) unsigned NOT NULL default '0',\n        label varchar(255),\n        format varchar(255),\n        custom varchar(255),\n        tablelist text,\n        fieldlist text,\n        calc text,\n        KEY (cid)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */");
      break;
    case 'postgres':
      $ret[] = update_sql("CREATE TABLE if not exists {views_calc_fields} (\n        cid int(10) unsigned NOT NULL default '0',\n        label varchar(255),\n        format varchar(255),\n        custom varchar(255),\n        tablelist text,\n        fieldlist text,\n        calc text,\n        KEY (cid)\n      ) ");
      break;
  }
  return $ret;
}

Functions

Namesort descending Description
views_calc_install
views_calc_uninstall
views_calc_update_1 Convert the queryname stored in the variable to the fullname so we can access both the fieldname and the tablename from the selections.
views_calc_update_2 Create a table to store custom views calculation fields.