| 1 |
<?php
|
| 2 |
// $Id: carbon.install,v 1.21 2008/07/02 13:38:14 johnackers Exp $
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
/**
|
| 7 |
* hook_install
|
| 8 |
*
|
| 9 |
* Install the current version of the database schema.
|
| 10 |
*/
|
| 11 |
|
| 12 |
function carbon_install() {
|
| 13 |
global $user ;
|
| 14 |
|
| 15 |
switch ($GLOBALS['db_type']) {
|
| 16 |
case "mysql":
|
| 17 |
case "mysqli":
|
| 18 |
|
| 19 |
$sql = "CREATE TABLE {carbon_account} (" .
|
| 20 |
"nid int(10) unsigned NOT NULL default 0," .
|
| 21 |
"postcode varchar(10) NOT NULL default ''," .
|
| 22 |
"organization varchar(40) NOT NULL default ''," .
|
| 23 |
"model varchar(20) NOT NULL default '', " .
|
| 24 |
"firstdate int(11) NOT NULL, ".
|
| 25 |
"period int(2) NOT NULL default 12, ".
|
| 26 |
"enablekwh0 tinyint(1) NOT NULL default 0, ".
|
| 27 |
"kwh0 decimal(10,2) NOT NULL default 0, ".
|
| 28 |
"enablekwh1 tinyint(1) NOT NULL default 0, ".
|
| 29 |
"kwh1 decimal(10,2) NOT NULL default 0, ".
|
| 30 |
"enableco2 tinyint(1) NOT NULL default 1, ".
|
| 31 |
"co2 decimal(10,2) NOT NULL default 0," .
|
| 32 |
"filing_status int (2) NOT NULL default '0'," .
|
| 33 |
"protection int(2) NOT NULL default '0'," .
|
| 34 |
"PRIMARY KEY (nid))" ;
|
| 35 |
db_query($sql);
|
| 36 |
|
| 37 |
$sql = "CREATE TABLE {carbon_stamp} (" .
|
| 38 |
"nid int(10) unsigned NOT NULL," .
|
| 39 |
"model varchar(20) NOT NULL default ''," .
|
| 40 |
"code varchar(20) NOT NULL," .
|
| 41 |
"reading decimal(10,2) NOT NULL, " .
|
| 42 |
"adjustment varchar(30) NOT NULL default 1," .
|
| 43 |
"startdate int(11) NOT NULL default 0," .
|
| 44 |
"enddate int(11) NOT NULL," .
|
| 45 |
"scope int(2) unsigned NOT NULL default 0," .
|
| 46 |
"protection int(2) unsigned NOT NULL default 0," .
|
| 47 |
"exclude int(2) unsigned NOT NULL default 0," .
|
| 48 |
"PRIMARY KEY (nid))" ;
|
| 49 |
db_query($sql);
|
| 50 |
|
| 51 |
$sql = _get_transfer_sql();
|
| 52 |
db_query($sql);
|
| 53 |
|
| 54 |
$sql = _get_reading_sql();
|
| 55 |
db_query($sql);
|
| 56 |
|
| 57 |
$sql = "CREATE TABLE {carbon_source} (" .
|
| 58 |
"nid int(10) unsigned NOT NULL," .
|
| 59 |
"model varchar(20) NOT NULL," .
|
| 60 |
"sector varchar(20) NOT NULL," .
|
| 61 |
"code varchar(20) NOT NULL," .
|
| 62 |
"units varchar(20) NOT NULL," .
|
| 63 |
"classname varchar(20) NOT NULL, " .
|
| 64 |
"tokwh0 double NOT NULL default 0, ".
|
| 65 |
"tokwh1 double NOT NULL default 0, ".
|
| 66 |
"toco2 double NOT NULL default 0, " .
|
| 67 |
"params varchar(255) default NULL, " .
|
| 68 |
"PRIMARY KEY (nid)," .
|
| 69 |
"UNIQUE KEY (model,code))" ;
|
| 70 |
db_query($sql);
|
| 71 |
|
| 72 |
$sql = "CREATE TABLE {carbon_stamp_account} (" .
|
| 73 |
"sid int(10) unsigned NOT NULL," .
|
| 74 |
"fid int(10) unsigned NOT NULL)" ;
|
| 75 |
db_query($sql);
|
| 76 |
|
| 77 |
$sql = "CREATE TABLE {carbon_account_share} (" .
|
| 78 |
"nid int(10) unsigned NOT NULL," .
|
| 79 |
"uid int(10) unsigned NOT NULL)" ;
|
| 80 |
db_query($sql);
|
| 81 |
|
| 82 |
drupal_set_message("Carbon module: created all tables", "info");
|
| 83 |
break;
|
| 84 |
|
| 85 |
case "pgsql":
|
| 86 |
drupal_set_message("No support for postgres sql yet", "info");
|
| 87 |
return;
|
| 88 |
}
|
| 89 |
drupal_set_message(t("Carbon module successfully installed."));
|
| 90 |
}
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
function _get_transfer_sql()
|
| 95 |
{
|
| 96 |
return "CREATE TABLE {carbon_transfer} (" .
|
| 97 |
"nid int(10) unsigned NOT NULL," .
|
| 98 |
"seller int(10) unsigned NOT NULL,".
|
| 99 |
"buyer int(10) unsigned NOT NULL,".
|
| 100 |
"carbon decimal(10,2) NOT NULL default 0,". // transferred from seller to buyer
|
| 101 |
"cost decimal(10,2) NOT NULL default 0,". // transferred from buyer to seller
|
| 102 |
"valuedate int(11) NOT NULL default 0,".
|
| 103 |
"PRIMARY KEY (nid)".
|
| 104 |
")" ;
|
| 105 |
}
|
| 106 |
|
| 107 |
|
| 108 |
function _get_reading_sql()
|
| 109 |
{
|
| 110 |
return "CREATE TABLE {carbon_reading} (" .
|
| 111 |
"nid int(10) unsigned NOT NULL," .
|
| 112 |
"mid int(10) unsigned NOT NULL,". // meter id
|
| 113 |
"date int(11) NOT NULL default 0,".
|
| 114 |
"reading decimal(10,2) NOT NULL default 0,".
|
| 115 |
"PRIMARY KEY (nid)".
|
| 116 |
")" ;
|
| 117 |
}
|
| 118 |
|
| 119 |
|
| 120 |
/*
|
| 121 |
function carbon_update_1() {
|
| 122 |
return _system_update_utf8(array('carbon_footprint', 'carbon_stamp', 'carbon_source', 'carbon_stamp_account'));
|
| 123 |
}
|
| 124 |
*/
|
| 125 |
function carbon_update_3()
|
| 126 |
{
|
| 127 |
$ret = array();
|
| 128 |
$ret[] = update_sql("ALTER TABLE {carbon_source} ADD sector varchar(20) NOT NULL default ''");
|
| 129 |
$ret[] = update_sql("ALTER TABLE {carbon_stamp} MODIFY COLUMN reading decimal(10,2) NOT NULL");
|
| 130 |
return $ret ;
|
| 131 |
}
|
| 132 |
|
| 133 |
|
| 134 |
/**
|
| 135 |
* fix the organization column which appears to have a missing character
|
| 136 |
*/
|
| 137 |
|
| 138 |
function carbon_update_4()
|
| 139 |
{
|
| 140 |
$ret = array();
|
| 141 |
$ret[] = update_sql("ALTER TABLE {carbon_footprint} CHANGE COLUMN organiation organization varchar(40) NOT NULL default ''");
|
| 142 |
return $ret ;
|
| 143 |
}
|
| 144 |
|
| 145 |
|
| 146 |
/**
|
| 147 |
* add the user share table
|
| 148 |
*/
|
| 149 |
|
| 150 |
function carbon_update_5()
|
| 151 |
{
|
| 152 |
$ret = array();
|
| 153 |
$ret[] = update_sql("CREATE TABLE {carbon_footprint_share} (" .
|
| 154 |
"nid int(10) unsigned NOT NULL," .
|
| 155 |
"uid int(10) unsigned NOT NULL)");
|
| 156 |
return $ret ;
|
| 157 |
}
|
| 158 |
|
| 159 |
|
| 160 |
/**
|
| 161 |
* fix the air-miles entry so that it converts the distance
|
| 162 |
* into Km and then run through the choose-climate algorithm
|
| 163 |
*/
|
| 164 |
|
| 165 |
function carbon_update_6()
|
| 166 |
{
|
| 167 |
$ret = array();
|
| 168 |
$ret[] = update_sql("UPDATE {carbon_source} SET multiplier=1.609 WHERE code = 'air-miles' AND model='choose climate'");
|
| 169 |
$ret[] = update_sql("UPDATE {carbon_source} SET multiplier=1.609 WHERE code = 'air-747-miles' AND model='choose climate'");
|
| 170 |
return $ret;
|
| 171 |
}
|
| 172 |
|
| 173 |
/**
|
| 174 |
* CVS version 0.6 onwards, 24-jan-07
|
| 175 |
* add kwh and CO2 to footprint
|
| 176 |
* rename 'multiplier'
|
| 177 |
*/
|
| 178 |
|
| 179 |
function carbon_update_7()
|
| 180 |
{
|
| 181 |
$ret = array();
|
| 182 |
$ret[] = update_sql("ALTER TABLE {carbon_footprint} ADD enablekwh tinyint(1) NOT NULL default 0 AFTER firstdate");
|
| 183 |
$ret[] = update_sql("ALTER TABLE {carbon_footprint} ADD kwh decimal(10,2) NOT NULL default 0 AFTER enablekwh");
|
| 184 |
$ret[] = update_sql("ALTER TABLE {carbon_footprint} ADD enableco2 tinyint(1) NOT NULL default 1 AFTER kwh");
|
| 185 |
$ret[] = update_sql("ALTER TABLE {carbon_footprint} ADD co2 decimal(10,2) NOT NULL default 0 AFTER enableco2");
|
| 186 |
|
| 187 |
|
| 188 |
$ret[] = update_sql("ALTER TABLE {carbon_source} ADD tokwh double AFTER classname");
|
| 189 |
$ret[] = update_sql("ALTER TABLE {carbon_source} CHANGE COLUMN multiplier toco2 double");
|
| 190 |
|
| 191 |
return $ret;
|
| 192 |
}
|
| 193 |
|
| 194 |
/*
|
| 195 |
* for groups that calculate footprints more often than 12 months
|
| 196 |
*/
|
| 197 |
|
| 198 |
function carbon_update_8()
|
| 199 |
{
|
| 200 |
$ret = array();
|
| 201 |
$ret[] = update_sql("ALTER TABLE {carbon_footprint} ADD period int(2) NOT NULL DEFAULT 12 AFTER firstdate");
|
| 202 |
return $ret;
|
| 203 |
}
|
| 204 |
|
| 205 |
/**
|
| 206 |
* put two optional energy columns into carbon footprint, one
|
| 207 |
* for renewable energy and one for now reneable energy
|
| 208 |
* see issue http://drupal.org/node/122830
|
| 209 |
*/
|
| 210 |
|
| 211 |
|
| 212 |
function carbon_update_9()
|
| 213 |
{
|
| 214 |
$ret = array();
|
| 215 |
|
| 216 |
$ret[] = update_sql("ALTER TABLE {carbon_footprint} ADD enablekwh1 tinyint(1) NOT NULL default 0 AFTER kwh");
|
| 217 |
$ret[] = update_sql("ALTER TABLE {carbon_footprint} ADD kwh1 decimal(10,2) NOT NULL default 0 AFTER enablekwh1");
|
| 218 |
$ret[] = update_sql("ALTER TABLE {carbon_source} ADD tokwh1 double NOT NULL default 0 AFTER tokwh");
|
| 219 |
|
| 220 |
$ret[] = update_sql("ALTER TABLE {carbon_source} CHANGE COLUMN tokwh tokwh0 double NOT NULL default 0");
|
| 221 |
$ret[] = update_sql("ALTER TABLE {carbon_footprint} CHANGE COLUMN enablekwh enablekwh0 tinyint(1) NOT NULL default 0");
|
| 222 |
$ret[] = update_sql("ALTER TABLE {carbon_footprint} CHANGE COLUMN kwh kwh0 decimal(10,2) NOT NULL default 0");
|
| 223 |
|
| 224 |
return $ret;
|
| 225 |
}
|
| 226 |
|
| 227 |
/**
|
| 228 |
* This is a major change, as part of the change from carbon_footprint to
|
| 229 |
* carbon_account, change the database table name as well.
|
| 230 |
*/
|
| 231 |
|
| 232 |
function carbon_update_10()
|
| 233 |
{
|
| 234 |
$ret = array();
|
| 235 |
$ret[] = update_sql("RENAME TABLE {carbon_footprint} TO {carbon_account}");
|
| 236 |
$ret[] = update_sql("RENAME TABLE {carbon_stamp_footprint} TO {carbon_stamp_account}");
|
| 237 |
$ret[] = update_sql("RENAME TABLE {carbon_footprint_share} TO {carbon_account_share}");
|
| 238 |
$ret[] = update_sql("UPDATE {node} SET TYPE='carbon_account' WHERE TYPE='carbon_footprint'");
|
| 239 |
return $ret;
|
| 240 |
}
|
| 241 |
|
| 242 |
|
| 243 |
/**
|
| 244 |
* Support carbon transfers between accounts
|
| 245 |
*/
|
| 246 |
|
| 247 |
function carbon_update_11()
|
| 248 |
{
|
| 249 |
$ret = array();
|
| 250 |
$ret[] = update_sql(_get_transfer_sql());
|
| 251 |
return $ret;
|
| 252 |
}
|
| 253 |
|
| 254 |
/*
|
| 255 |
function carbon_update_12()
|
| 256 |
{
|
| 257 |
$ret = array();
|
| 258 |
$ret[] = update_sql(_get_reading_sql());
|
| 259 |
return $ret;
|
| 260 |
}
|
| 261 |
*/
|
| 262 |
|
| 263 |
|
| 264 |
/**
|
| 265 |
* hook_uninstall
|
| 266 |
*
|
| 267 |
* Remove any tables or variables that the module sets.
|
| 268 |
* The uninstall hook will fire when the module gets uninstalled.
|
| 269 |
*/
|
| 270 |
|
| 271 |
|
| 272 |
function carbon_uninstall() {
|
| 273 |
global $user ;
|
| 274 |
|
| 275 |
_drop_tables();
|
| 276 |
_remove_carbon_nodes();
|
| 277 |
drupal_set_message("All carbon records deleted from node table, all carbon tables dropped.", "info");
|
| 278 |
}
|
| 279 |
|
| 280 |
|
| 281 |
function _drop_tables()
|
| 282 |
{
|
| 283 |
$tables = array("carbon_account_share","carbon_stamp_account",
|
| 284 |
"carbon_account","carbon_stamp","carbon_source","carbon_transfer");
|
| 285 |
|
| 286 |
foreach ($tables as $table)
|
| 287 |
{
|
| 288 |
$result = db_query("drop table ".$table);
|
| 289 |
if ($result == 0) break ;
|
| 290 |
}
|
| 291 |
return $result ;
|
| 292 |
}
|
| 293 |
|
| 294 |
|
| 295 |
function _remove_carbon_nodes()
|
| 296 |
{
|
| 297 |
$db_query1 = db_query(
|
| 298 |
"delete from node where node.type = 'carbon_account'"
|
| 299 |
);
|
| 300 |
$db_query2 = db_query(
|
| 301 |
"delete from node where node.type = 'carbon_stamp'"
|
| 302 |
);
|
| 303 |
$db_query3 = db_query(
|
| 304 |
"delete from node where node.type = 'carbon_source'"
|
| 305 |
);
|
| 306 |
$db_query4 = db_query(
|
| 307 |
"delete from node where node.type = 'carbon_transfer'"
|
| 308 |
);
|
| 309 |
return t("done") ;
|
| 310 |
}
|