| 1 |
<?php
|
| 2 |
// $Id: views_calc.install,v 1.3 2008/09/16 18:03:08 karens Exp $
|
| 3 |
/**
|
| 4 |
* Implementation of hook_schema().
|
| 5 |
*/
|
| 6 |
function views_calc_schema() {
|
| 7 |
$schema['views_calc_fields'] = array(
|
| 8 |
'fields' => array(
|
| 9 |
'cid' => array('type' => 'serial', 'not null' => TRUE, 'disp-width' => '10'),
|
| 10 |
'label' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''),
|
| 11 |
'format' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''),
|
| 12 |
'custom' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''),
|
| 13 |
'base' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''),
|
| 14 |
'tablelist' => array('type' => 'text', 'not null' => TRUE),
|
| 15 |
'fieldlist' => array('type' => 'text', 'not null' => TRUE),
|
| 16 |
'calc' => array('type' => 'text', 'not null' => TRUE),
|
| 17 |
),
|
| 18 |
'primary key' => array('cid'),
|
| 19 |
'indexes' => array(
|
| 20 |
'cid' => array('cid'))
|
| 21 |
);
|
| 22 |
return $schema;
|
| 23 |
}
|
| 24 |
|
| 25 |
function views_calc_install() {
|
| 26 |
drupal_install_schema('views_calc');
|
| 27 |
}
|
| 28 |
|
| 29 |
function views_calc_uninstall() {
|
| 30 |
db_query("DROP TABLE {views_calc_fields}");
|
| 31 |
}
|
| 32 |
|
| 33 |
/**
|
| 34 |
* Convert the queryname stored in the variable to the fullname
|
| 35 |
* so we can access both the fieldname and the tablename from
|
| 36 |
* the selections.
|
| 37 |
*
|
| 38 |
* Change the calc 'AVERAGE' to 'AVG' to match the db function.
|
| 39 |
*/
|
| 40 |
function views_calc_update_1() {
|
| 41 |
$ret = array();
|
| 42 |
include_once(drupal_get_path('module', 'views') .'/views.module');
|
| 43 |
include_once(drupal_get_path('module', 'views_calc') .'/views_calc.module');
|
| 44 |
$view_calcs = (array) variable_get('views_calc_vid','');
|
| 45 |
foreach ($view_calcs as $view_id) {
|
| 46 |
if ($view = views_get_view($view_id)) {
|
| 47 |
$cols = (array) variable_get('views_calc_'. $view->vid .'_col','');
|
| 48 |
$new_cols = array();
|
| 49 |
foreach ($cols as $col) {
|
| 50 |
foreach ($view->field as $field) {
|
| 51 |
if ($field['queryname'] == $col) {
|
| 52 |
$new_cols[] = $field['fullname'];
|
| 53 |
}
|
| 54 |
}
|
| 55 |
}
|
| 56 |
variable_set('views_calc_'. $view->vid .'_col', $new_cols);
|
| 57 |
$rows = (array) variable_get('views_calc_'. $view->vid. '_row','');
|
| 58 |
$new_rows = array();
|
| 59 |
foreach ($rows as $row) {
|
| 60 |
foreach ($view->field as $field) {
|
| 61 |
if ($field['queryname'] == $col) {
|
| 62 |
$new_rows[] = $field['fullname'];
|
| 63 |
}
|
| 64 |
}
|
| 65 |
}
|
| 66 |
variable_set('views_calc_'. $view->vid .'_row', $new_rows);
|
| 67 |
$col_calc = (array) variable_get('views_calc_'.$view->vid.'_col_calc','');
|
| 68 |
foreach ($col_calc as $calc) {
|
| 69 |
if ($calc == 'AVERAGE') {
|
| 70 |
$new_calcs[] = 'AVG';
|
| 71 |
}
|
| 72 |
else {
|
| 73 |
$new_calcs[] = $calc;
|
| 74 |
}
|
| 75 |
}
|
| 76 |
variable_set('views_calc_'. $view->vid .'_col_calc', $new_calcs);
|
| 77 |
$row_calc = (array) variable_get('views_calc_'.$view->vid.'_row_calc','');
|
| 78 |
foreach ($row_calc as $calc) {
|
| 79 |
if ($calc == 'AVERAGE') {
|
| 80 |
$new_calcs[] = 'AVG';
|
| 81 |
}
|
| 82 |
else {
|
| 83 |
$new_calcs[] = $calc;
|
| 84 |
}
|
| 85 |
}
|
| 86 |
variable_set('views_calc_'. $view->vid .'_row_calc', $new_calcs);
|
| 87 |
}
|
| 88 |
}
|
| 89 |
return $ret;
|
| 90 |
}
|
| 91 |
|
| 92 |
/**
|
| 93 |
* Create a table to store custom views calculation fields.
|
| 94 |
*/
|
| 95 |
function views_calc_update_2() {
|
| 96 |
$ret = array();
|
| 97 |
global $db_type;
|
| 98 |
|
| 99 |
switch ($db_type) {
|
| 100 |
case 'mysql':
|
| 101 |
case 'mysqli':
|
| 102 |
$ret[] = update_sql("CREATE TABLE if not exists {views_calc_fields} (
|
| 103 |
cid int(10) unsigned NOT NULL default '0',
|
| 104 |
label varchar(255),
|
| 105 |
format varchar(255),
|
| 106 |
custom varchar(255),
|
| 107 |
tablelist text,
|
| 108 |
fieldlist text,
|
| 109 |
calc text,
|
| 110 |
KEY (cid)
|
| 111 |
) /*!40100 DEFAULT CHARACTER SET utf8 */");
|
| 112 |
break;
|
| 113 |
case 'postgres':
|
| 114 |
$ret[] = update_sql("CREATE TABLE if not exists {views_calc_fields} (
|
| 115 |
cid int(10) unsigned NOT NULL default '0',
|
| 116 |
label varchar(255),
|
| 117 |
format varchar(255),
|
| 118 |
custom varchar(255),
|
| 119 |
tablelist text,
|
| 120 |
fieldlist text,
|
| 121 |
calc text,
|
| 122 |
KEY (cid)
|
| 123 |
) ");
|
| 124 |
break;
|
| 125 |
}
|
| 126 |
return $ret;
|
| 127 |
}
|
| 128 |
|
| 129 |
function views_calc_update_6000() {
|
| 130 |
$ret = array();
|
| 131 |
db_add_field($ret, 'views_calc_fields', 'base', array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => 'node'));
|
| 132 |
return $ret;
|
| 133 |
}
|