| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* $Id:$
|
| 4 |
* Implementation of hook_install().
|
| 5 |
*
|
| 6 |
* For country codes, use http://www.iso.org/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html
|
| 7 |
* If you are adding states or provinces for other countries, just add them in below. We should be able to grab what we need by Country Code.
|
| 8 |
* For now, while there is only support for the U.S., my code in the module won't use the country code.
|
| 9 |
*
|
| 10 |
* Also, once two or more countries are supported, another table will need to be created which cross references Country Codes with
|
| 11 |
* country names.
|
| 12 |
*
|
| 13 |
* @package CCK_Address
|
| 14 |
* @category NeighborForge
|
| 15 |
*/
|
| 16 |
function cck_address_install() {
|
| 17 |
$query_ok = TRUE;
|
| 18 |
switch ($GLOBALS['db_type']) {
|
| 19 |
case 'mysql':
|
| 20 |
case 'mysqli':
|
| 21 |
if (!db_table_exists('cck_address_states')) {
|
| 22 |
$query1 = db_query("CREATE TABLE {cck_address_states} (
|
| 23 |
state_id int unsigned NOT NULL auto_increment,
|
| 24 |
state_name varchar(64) NOT NULL default '',
|
| 25 |
state_abbrv varchar(5) NOT NULL default '',
|
| 26 |
country_code varchar(2) NOT NULL default '',
|
| 27 |
PRIMARY KEY (state_id),
|
| 28 |
UNIQUE KEY (state_name)
|
| 29 |
) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
|
| 30 |
}
|
| 31 |
if (!db_table_exists('cck_address_countries')) {
|
| 32 |
$query3 = db_query("CREATE TABLE {cck_address_countries} (
|
| 33 |
country_code varchar(2) NOT NULL default '',
|
| 34 |
country_name varchar(64) NOT NULL default '',
|
| 35 |
PRIMARY KEY name_code (country_name, country_code)
|
| 36 |
) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
|
| 37 |
}
|
| 38 |
break;
|
| 39 |
case 'pgsql':
|
| 40 |
//I don't really know what I'm doing here, so if I screwed up the pgsql, let me know how to fix it.
|
| 41 |
if (!db_table_exists('cck_address_states')) {
|
| 42 |
$query1 = db_query("CREATE TABLE {cck_address_states} (
|
| 43 |
state_id serial,
|
| 44 |
state_name varchar(64) NOT NULL default '',
|
| 45 |
state_abbrv varchar(5) NOT NULL default '',
|
| 46 |
country_code varchar(2) NOT NULL default '',
|
| 47 |
PRIMARY KEY (state_id),
|
| 48 |
UNIQUE (state_name)
|
| 49 |
)");
|
| 50 |
}
|
| 51 |
if (!db_table_exists('cck_address_countries')) {
|
| 52 |
$query3 = db_query("CREATE TABLE {cck_address_countries} (
|
| 53 |
country_code varchar(2) NOT NULL default '',
|
| 54 |
country_name varchar(64) NOT NULL default '',
|
| 55 |
PRIMARY KEY (country_name, country_code)
|
| 56 |
)");
|
| 57 |
}
|
| 58 |
break;
|
| 59 |
}
|
| 60 |
if (!$query1 || !$query3) $query_ok = FALSE;
|
| 61 |
|
| 62 |
include_once('./'. drupal_get_path('module', 'cck_address') .'/cck_address_state.inc');
|
| 63 |
|
| 64 |
if ($query_ok) {
|
| 65 |
drupal_set_message('The CCK_Address module was installed successfully. Tables were added to the database.');
|
| 66 |
}
|
| 67 |
else {
|
| 68 |
drupal_set_message('There was an error installing the CCK_Address database tables.', 'error');
|
| 69 |
}
|
| 70 |
}
|
| 71 |
|
| 72 |
/**
|
| 73 |
* Implementation of hook_update_N().
|
| 74 |
*/
|
| 75 |
function cck_address_update_1() {
|
| 76 |
$ret = array();
|
| 77 |
|
| 78 |
//first, handle existing table issues
|
| 79 |
include_once('./'. drupal_get_path('module', 'content') .'/content.module');
|
| 80 |
include_once('./'. drupal_get_path('module', 'content') .'/content_admin.inc');
|
| 81 |
|
| 82 |
content_clear_type_cache();
|
| 83 |
$fields = content_fields();
|
| 84 |
|
| 85 |
foreach ($fields as $field) {
|
| 86 |
switch ($field['type']) {
|
| 87 |
case 'cck_address':
|
| 88 |
$db_info = content_database_info($field);
|
| 89 |
$columns_old = $db_info['columns'];
|
| 90 |
$columns = $columns_old;
|
| 91 |
$columns['state']['length'] = 100;
|
| 92 |
$columns['other']['type'] = 'longtext';
|
| 93 |
$columns['other']['not_null'] = TRUE;
|
| 94 |
$columns['other']['default'] = "''";
|
| 95 |
$columns['other']['sortable'] = FALSE;
|
| 96 |
// force the old values : if the db info was rebuilt before the update is run,
|
| 97 |
// it will already contain the new values, and nothing gets changed in content_alter_db_field
|
| 98 |
$columns_old['state']['length'] = 2;
|
| 99 |
unset($columns_old['other']);
|
| 100 |
content_alter_db_field($field, $columns_old, $field, $columns);
|
| 101 |
$ret[] = array(
|
| 102 |
'query' => strtr('The field %field_name has been updated with an \'other\' column. Also, the length of the state column has been increased from 2 to 100 characters.', array('%field_name' => $field['field_name'])),
|
| 103 |
'success' => TRUE
|
| 104 |
);
|
| 105 |
break;
|
| 106 |
}
|
| 107 |
}
|
| 108 |
//now add the new table
|
| 109 |
$query_ok = TRUE;
|
| 110 |
switch ($GLOBALS['db_type']) {
|
| 111 |
case 'mysql':
|
| 112 |
case 'mysqli':
|
| 113 |
if (!db_table_exists('cck_address_countries')) {
|
| 114 |
$ret[] = $query3 = update_sql("CREATE TABLE {cck_address_countries} (
|
| 115 |
country_code varchar(2) NOT NULL default '',
|
| 116 |
country_name varchar(64) NOT NULL default '',
|
| 117 |
PRIMARY KEY name_code (country_name, country_code)
|
| 118 |
) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
|
| 119 |
}
|
| 120 |
break;
|
| 121 |
case 'pgsql':
|
| 122 |
//I don't really know what I'm doing here, so if I screwed up the pgsql, let me know how to fix it.
|
| 123 |
if (!db_table_exists('cck_address_countries')) {
|
| 124 |
$ret[] = $query3 = update_sql("CREATE TABLE {cck_address_countries} (
|
| 125 |
country_code varchar(2) NOT NULL default '',
|
| 126 |
country_name varchar(64) NOT NULL default '',
|
| 127 |
PRIMARY KEY (country_name, country_code)
|
| 128 |
)");
|
| 129 |
}
|
| 130 |
break;
|
| 131 |
}
|
| 132 |
if (!$query3) $query_ok = FALSE;
|
| 133 |
|
| 134 |
$query2 = db_query("INSERT INTO {cck_address_countries} (country_name, country_code) VALUES ('United States', 'US')");
|
| 135 |
if (!$query2) $query_ok = FALSE;
|
| 136 |
|
| 137 |
if ($query_ok) {
|
| 138 |
drupal_set_message('The CCK_Address module was updated successfully. Tables were added to the database.');
|
| 139 |
}
|
| 140 |
else {
|
| 141 |
drupal_set_message('There was an error installing the CCK_Address database table update.', 'error');
|
| 142 |
}
|
| 143 |
return $ret;
|
| 144 |
}
|
| 145 |
|
| 146 |
/**
|
| 147 |
* Implementation of hook_update_N().
|
| 148 |
*
|
| 149 |
* This should change any relevent tables that use this module. It should roughly look like:
|
| 150 |
* +-----------------------+------------------+------+-----+---------+-------+
|
| 151 |
* | Field | Type | Null | Key | Default | Extra |
|
| 152 |
* +-----------------------+------------------+------+-----+---------+-------+
|
| 153 |
* | vid | int(10) unsigned | NO | PRI | 0 | |
|
| 154 |
* | nid | int(10) unsigned | NO | | 0 | |
|
| 155 |
* | field_address_street1 | longtext | NO | | | |
|
| 156 |
* | field_address_street2 | longtext | YES | | NULL | |
|
| 157 |
* | field_address_apt | varchar(15) | YES | | NULL | |
|
| 158 |
* | field_address_city | longtext | NO | | | |
|
| 159 |
* | field_address_state | varchar(100) | NO | | | |
|
| 160 |
* | field_address_zip | varchar(15) | NO | | | |
|
| 161 |
* | field_address_country | varchar(2) | NO | | | |
|
| 162 |
* | field_address_other | longtext | YES | | NULL | |
|
| 163 |
* +-----------------------+------------------+------+-----+---------+-------+
|
| 164 |
* Changes are made to type, null and default as noted below. If your table doesn't change
|
| 165 |
* for one reason or another, I've included the print-out of my table above so you can change
|
| 166 |
* it by hand. Most of the types will not change during this operation, so don't change them
|
| 167 |
* by hand either. The important columns are the Null and Default ones.
|
| 168 |
*/
|
| 169 |
function cck_address_update_2() {
|
| 170 |
$ret = array();
|
| 171 |
|
| 172 |
//first, handle existing table issues
|
| 173 |
include_once('./'. drupal_get_path('module', 'content') .'/content.module');
|
| 174 |
include_once('./'. drupal_get_path('module', 'content') .'/content_admin.inc');
|
| 175 |
|
| 176 |
content_clear_type_cache();
|
| 177 |
$fields = content_fields();
|
| 178 |
|
| 179 |
foreach ($fields as $field) {
|
| 180 |
switch ($field['type']) {
|
| 181 |
case 'cck_address':
|
| 182 |
$db_info = content_database_info($field);
|
| 183 |
$columns_old = $db_info['columns'];
|
| 184 |
$columns = $columns_old;
|
| 185 |
$columns['street1']['not null'] = TRUE;
|
| 186 |
$columns['street1']['default'] = "''";
|
| 187 |
$columns['street2']['not null'] = FALSE;
|
| 188 |
$columns['street2']['default'] = NULL;
|
| 189 |
$columns['apt']['type'] = 'varchar';
|
| 190 |
$columns['apt']['length'] = 15;
|
| 191 |
$columns['apt']['not null'] = FALSE;
|
| 192 |
$columns['apt']['default'] = NULL;
|
| 193 |
$columns['city']['not null'] = TRUE;
|
| 194 |
$columns['city']['default'] = "''";
|
| 195 |
$columns['state']['not null'] = TRUE;
|
| 196 |
$columns['state']['default'] = "''";
|
| 197 |
$columns['zip']['type'] = 'varchar';
|
| 198 |
$columns['zip']['length'] = 15;
|
| 199 |
$columns['zip']['not null'] = TRUE;
|
| 200 |
$columns['zip']['default'] = "''";
|
| 201 |
$columns['country']['not null'] = TRUE;
|
| 202 |
$columns['country']['default'] = "''";
|
| 203 |
$columns['other']['not null'] = FALSE;
|
| 204 |
$columns['other']['default'] = NULL;
|
| 205 |
// force the old values : if the db info was rebuilt before the update is run,
|
| 206 |
// it will already contain the new values, and nothing gets changed in content_alter_db_field
|
| 207 |
$columns_old['street1']['not null'] = TRUE;
|
| 208 |
$columns_old['street1']['default'] = "''";
|
| 209 |
$columns_old['street2']['not null'] = TRUE;
|
| 210 |
$columns_old['street2']['default'] = "''";
|
| 211 |
$columns_old['apt']['type'] = 'int';
|
| 212 |
$columns_old['apt']['not null'] = FALSE;
|
| 213 |
$columns_old['apt']['default'] = NULL;
|
| 214 |
$columns_old['city']['not null'] = FALSE;
|
| 215 |
$columns_old['city']['not null'] = TRUE;
|
| 216 |
$columns_old['city']['default'] = "''";
|
| 217 |
$columns_old['state']['not null'] = TRUE;
|
| 218 |
$columns_old['state']['default'] = "''";
|
| 219 |
$columns_old['zip']['type'] = 'int';
|
| 220 |
$columns_old['zip']['not null'] = TRUE;
|
| 221 |
$columns_old['zip']['default'] = 0;
|
| 222 |
$columns_old['country']['not null'] = TRUE;
|
| 223 |
$columns_old['country']['default'] = "''";
|
| 224 |
$columns_old['other']['not null'] = TRUE;
|
| 225 |
$columns_old['other']['default'] = "''";
|
| 226 |
content_alter_db_field($field, $columns_old, $field, $columns);
|
| 227 |
$ret[] = array(
|
| 228 |
'query' => strtr('The field %field_name has been updated in several respects.', array('%field_name' => $field['field_name'])),
|
| 229 |
'success' => TRUE
|
| 230 |
);
|
| 231 |
break;
|
| 232 |
}
|
| 233 |
}
|
| 234 |
$query2 = db_query("INSERT INTO {cck_address_states} (state_name, state_abbrv, country_code) VALUES ('Washington, D.C.', 'DC', 'US')");
|
| 235 |
if (!$query2) $query_ok = FALSE;
|
| 236 |
|
| 237 |
if ($query_ok) {
|
| 238 |
drupal_set_message('The CCK_Address module was updated successfully. Tables were updated.');
|
| 239 |
}
|
| 240 |
else {
|
| 241 |
drupal_set_message('There was an error updating the CCK_Address database table.', 'error');
|
| 242 |
}
|
| 243 |
return $ret;
|
| 244 |
}
|
| 245 |
|
| 246 |
/**
|
| 247 |
* Implementation of hook_uninstall().
|
| 248 |
*/
|
| 249 |
function cck_address_uninstall() {
|
| 250 |
$query1 = db_query('DROP TABLE {cck_address_states}');
|
| 251 |
$query2 = db_query('DROP TABLE {cck_address_countries}');
|
| 252 |
|
| 253 |
if ($query1 && $query2) {
|
| 254 |
drupal_set_message('The CCK_Address module was uninstalled successfully.');
|
| 255 |
}
|
| 256 |
else {
|
| 257 |
drupal_set_message('There was an error removing the CCK_Address database tables.', 'error');
|
| 258 |
}
|
| 259 |
|
| 260 |
}
|