/
uc_vat_number.install
144 lines (124 loc) · 4.35 KB
/
uc_vat_number.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
// $Id$
/**
* @file
* Update and install functions for VAT Number.
*/
/**
* Implementation of hook_install().
*/
function uc_vat_number_install() {
drupal_install_schema('uc_vat_number');
}
/**
* Implementation of hook_schema().
*/
function uc_vat_number_schema() {
$schema['uc_vat_number'] = array(
'fields' => array(
'vat_number_id' => array('type' => 'serial', 'not null' => TRUE),
'order_id' => array('type' => 'int', 'not null' => TRUE),
'vat_number' => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE),
),
'primary key' => array('vat_number_id'),
);
$schema['uc_vat_validation'] = array(
'fields' => array(
'vat_validation_id' => array('type' => 'serial', 'not null' => TRUE),
'vat_number' => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE),
'fetched' => array('type' => 'int', 'not null' => TRUE),
'response' => array('type' => 'text', 'not null' => TRUE),
),
'primary key' => array('vat_validation_id'),
'unique keys' => array('vat_number' => array('vat_number')),
);
return $schema;
}
/**
* Implementation of hook_uninstall().
*/
function uc_vat_number_uninstall() {
db_query("DROP TABLE IF EXISTS {uc_vat_number}");
db_query("DROP TABLE IF EXISTS {uc_vat_validation}");
variable_del('uc_store_vat_number');
variable_del('uc_pane_vat_number_enabled');
variable_del('uc_pane_vat_number_weight');
variable_del('uc_order_pane_vat_number_show_view');
variable_del('uc_order_pane_vat_number_weight_view');
}
/**
* Implementation of hook_update_N().
*/
function uc_vat_number_update_2() {
$schema['uc_vat_validation'] = array(
'fields' => array(
'vat_validation_id' => array('type' => 'serial', 'not null' => TRUE),
'vat_number' => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE),
'fetched' => array('type' => 'int', 'not null' => TRUE),
'response' => array('type' => 'text', 'not null' => TRUE),
),
'primary key' => array('vat_validation_id'),
);
$ret = array();
db_create_table($ret, 'uc_vat_validation', $schema['uc_vat_validation']);
return $ret;
}
function uc_vat_number_update_5() {
$ret = array();
$ret[] = update_sql("TRUNCATE TABLE {uc_vat_validation}");
db_add_unique_key($ret, 'uc_vat_validation', 'vat_number', array('vat_number'));
return $ret;
}
function uc_vat_number_update_6200() {
$on_roles = user_roles(FALSE, 'Have to fill VAT number');
$off_roles = array_diff(user_roles(FALSE), $on_roles);
$ret = array();
if (count($on_roles)) {
drupal_set_message(t('Version 2 of Ubercart VAT Number significantly changes the way the module handles role permissions. Please refer to <a href="http://drupal.org/node/1052086">the project page</a> for further details.'), 'warning');
// Removes wrong permission from roles that have it
$permissions = array();
foreach ($on_roles as $rid => $role) {
$q = db_query("SELECT perm FROM {permission} WHERE rid = %d;", $rid);
$obj = db_fetch_object($q);
$perm = explode(", ", $obj->perm);
// If wrong permission is the only permission, permissions for this role won't be regenerated
if (count($perm) > 1) {
foreach ($perm as $key => $val) {
if ($val == 'Have to fill VAT number') {
unset($perm[$key]);
}
}
$permissions[$rid] = implode(", ", $perm);
}
}
// Restores permissions for remaining roles
foreach ($off_roles as $rid => $role) {
$q = db_query("SELECT perm FROM {permission} WHERE rid = %d;", $rid);
$obj = db_fetch_object($q);
if ($obj->perm != '') {
$permissions[$rid] = $obj->perm;
}
}
ksort($permissions);
// Empties permission table
$ret[] = update_sql("TRUNCATE {permission};");
// Assigns updated roles
if (count($permissions)) {
$values = array();
foreach ($permissions as $rid => $perm) {
$values[] = "('$rid', '$perm')";
}
$values = implode(", ", $values);
$ret[] = update_sql("INSERT INTO {permission} (rid, perm) VALUES $values;");
}
$uc_vat_number_roles = array();
foreach ($on_roles as $rid => $role) {
$uc_vat_number_roles[$rid] = $rid;
}
foreach ($off_roles as $rid => $role) {
$uc_vat_number_roles[$rid] = 0;
}
variable_set('uc_vat_number_roles', $uc_vat_number_roles);
}
return $ret;
}