comparison views_calc.install @ 0:0651c02e6ed7

views_calc 1.3
author Franck Deroche <franck@defr.org>
date Wed, 05 Aug 2009 18:20:29 +0200
parents
children b0a976e17cc7
comparison
equal deleted inserted replaced
-1:000000000000 0:0651c02e6ed7
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 'tablelist' => array('type' => 'text', 'not null' => TRUE),
14 'fieldlist' => array('type' => 'text', 'not null' => TRUE),
15 'calc' => array('type' => 'text', 'not null' => TRUE),
16 ),
17 'primary key' => array('cid'),
18 'indexes' => array(
19 'cid' => array('cid'))
20 );
21 return $schema;
22 }
23
24 function views_calc_install() {
25 drupal_install_schema('views_calc');
26 }
27
28 function views_calc_uninstall() {
29 db_query("DROP TABLE {views_calc_fields}");
30 }
31
32 /**
33 * Convert the queryname stored in the variable to the fullname
34 * so we can access both the fieldname and the tablename from
35 * the selections.
36 *
37 * Change the calc 'AVERAGE' to 'AVG' to match the db function.
38 */
39 function views_calc_update_1() {
40 $ret = array();
41 include_once(drupal_get_path('module', 'views') .'/views.module');
42 include_once(drupal_get_path('module', 'views_calc') .'/views_calc.module');
43 $view_calcs = (array) variable_get('views_calc_vid','');
44 foreach ($view_calcs as $view_id) {
45 if ($view = views_get_view($view_id)) {
46 $cols = (array) variable_get('views_calc_'. $view->vid .'_col','');
47 $new_cols = array();
48 foreach ($cols as $col) {
49 foreach ($view->field as $field) {
50 if ($field['queryname'] == $col) {
51 $new_cols[] = $field['fullname'];
52 }
53 }
54 }
55 variable_set('views_calc_'. $view->vid .'_col', $new_cols);
56 $rows = (array) variable_get('views_calc_'. $view->vid. '_row','');
57 $new_rows = array();
58 foreach ($rows as $row) {
59 foreach ($view->field as $field) {
60 if ($field['queryname'] == $col) {
61 $new_rows[] = $field['fullname'];
62 }
63 }
64 }
65 variable_set('views_calc_'. $view->vid .'_row', $new_rows);
66 $col_calc = (array) variable_get('views_calc_'.$view->vid.'_col_calc','');
67 foreach ($col_calc as $calc) {
68 if ($calc == 'AVERAGE') {
69 $new_calcs[] = 'AVG';
70 }
71 else {
72 $new_calcs[] = $calc;
73 }
74 }
75 variable_set('views_calc_'. $view->vid .'_col_calc', $new_calcs);
76 $row_calc = (array) variable_get('views_calc_'.$view->vid.'_row_calc','');
77 foreach ($row_calc as $calc) {
78 if ($calc == 'AVERAGE') {
79 $new_calcs[] = 'AVG';
80 }
81 else {
82 $new_calcs[] = $calc;
83 }
84 }
85 variable_set('views_calc_'. $view->vid .'_row_calc', $new_calcs);
86 }
87 }
88 return $ret;
89 }
90
91 /**
92 * Create a table to store custom views calculation fields.
93 */
94 function views_calc_update_2() {
95 $ret = array();
96 global $db_type;
97
98 switch ($db_type) {
99 case 'mysql':
100 case 'mysqli':
101 $ret[] = update_sql("CREATE TABLE if not exists {views_calc_fields} (
102 cid int(10) unsigned NOT NULL default '0',
103 label varchar(255),
104 format varchar(255),
105 custom varchar(255),
106 tablelist text,
107 fieldlist text,
108 calc text,
109 KEY (cid)
110 ) /*!40100 DEFAULT CHARACTER SET utf8 */");
111 break;
112 case 'postgres':
113 $ret[] = update_sql("CREATE TABLE if not exists {views_calc_fields} (
114 cid int(10) unsigned NOT NULL default '0',
115 label varchar(255),
116 format varchar(255),
117 custom varchar(255),
118 tablelist text,
119 fieldlist text,
120 calc text,
121 KEY (cid)
122 ) ");
123 break;
124 }
125 return $ret;
126 }
127
128 /**
129 * TODO
130 * Use these values to create new default views, then delete them.
131 *
132 function views_calc_update_6000() {
133 $view_calcs = (array) variable_get('views_calc_vid','');
134 foreach ($view_calcs as $view_id) {
135 variable_del('views_calc_'. $view_id .'_col');
136 variable_del('views_calc_'. $view_id .'_col_calc');
137 variable_del('views_calc_'. $view_id .'_row');
138 variable_del('views_calc_'. $view_id .'_row_calc');
139 }
140 variable_del('views_calc_vid');
141 variable_del('views_calc_operators');
142
143 }
144 */