| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Install and uninstall schema and functions for the UC Affiliate module.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_schema().
|
| 11 |
*/
|
| 12 |
function uc_affiliate_schema() {
|
| 13 |
$schema['uc_affiliate_products'] = array(
|
| 14 |
'fields' => array(
|
| 15 |
'nid' => array(
|
| 16 |
'type' => 'int',
|
| 17 |
'unsigned' => TRUE,
|
| 18 |
'not null' => TRUE,
|
| 19 |
),
|
| 20 |
'affid' => array(
|
| 21 |
'type' => 'int',
|
| 22 |
'unsigned' => TRUE,
|
| 23 |
'not null' => TRUE,
|
| 24 |
),
|
| 25 |
'commission' => array(
|
| 26 |
'type' => 'text',
|
| 27 |
'size' => 'medium',
|
| 28 |
'not null' => TRUE,
|
| 29 |
),
|
| 30 |
'allowed' => array(
|
| 31 |
'type' => 'int',
|
| 32 |
'size' => 'tiny',
|
| 33 |
'unsigned' => FALSE,
|
| 34 |
'not null' => TRUE,
|
| 35 |
'default' => -1,
|
| 36 |
),
|
| 37 |
),
|
| 38 |
'primary key' => array('nid', 'affid'),
|
| 39 |
);
|
| 40 |
|
| 41 |
$schema['uc_affiliate_visits'] = array(
|
| 42 |
'fields' => array(
|
| 43 |
'affid' => array(
|
| 44 |
'type' => 'int',
|
| 45 |
'unsigned' => TRUE,
|
| 46 |
'not null' => FALSE,
|
| 47 |
),
|
| 48 |
'time' => array(
|
| 49 |
'type' => 'int',
|
| 50 |
'unsigned' => TRUE,
|
| 51 |
'not null' => TRUE,
|
| 52 |
),
|
| 53 |
),
|
| 54 |
);
|
| 55 |
|
| 56 |
$schema['uc_affiliate_counts'] = array(
|
| 57 |
'fields' => array(
|
| 58 |
'affid' => array(
|
| 59 |
'type' => 'int',
|
| 60 |
'unsigned' => TRUE,
|
| 61 |
'not null' => TRUE,
|
| 62 |
),
|
| 63 |
'visits' => array(
|
| 64 |
'type' => 'int',
|
| 65 |
'unsigned' => TRUE,
|
| 66 |
'not null' => TRUE,
|
| 67 |
),
|
| 68 |
'users' => array(
|
| 69 |
'type' => 'int',
|
| 70 |
'unsigned' => TRUE,
|
| 71 |
'not null' => TRUE,
|
| 72 |
),
|
| 73 |
'sales' => array(
|
| 74 |
'type' => 'int',
|
| 75 |
'unsigned' => TRUE,
|
| 76 |
'not null' => TRUE,
|
| 77 |
),
|
| 78 |
'returns' => array(
|
| 79 |
'type' => 'int',
|
| 80 |
'unsigned' => TRUE,
|
| 81 |
'not null' => TRUE,
|
| 82 |
),
|
| 83 |
'commission' => array(
|
| 84 |
'type' => 'float',
|
| 85 |
'size' => 'big',
|
| 86 |
'not null' => TRUE,
|
| 87 |
),
|
| 88 |
'profit' => array(
|
| 89 |
'type' => 'float',
|
| 90 |
'size' => 'big',
|
| 91 |
'not null' => TRUE,
|
| 92 |
),
|
| 93 |
'time' => array(
|
| 94 |
'type' => 'int',
|
| 95 |
'unsigned' => TRUE,
|
| 96 |
'not null' => TRUE,
|
| 97 |
),
|
| 98 |
),
|
| 99 |
'primary key' => array('affid', 'time'),
|
| 100 |
);
|
| 101 |
|
| 102 |
$schema['uc_affiliate_commissions'] = array(
|
| 103 |
'fields' => array(
|
| 104 |
'order_id' => array(
|
| 105 |
'type' => 'int',
|
| 106 |
'unsigned' => TRUE,
|
| 107 |
'not null' => TRUE,
|
| 108 |
),
|
| 109 |
'affid' => array(
|
| 110 |
'type' => 'int',
|
| 111 |
'unsigned' => TRUE,
|
| 112 |
'not null' => TRUE,
|
| 113 |
),
|
| 114 |
'level' => array(
|
| 115 |
'type' => 'int',
|
| 116 |
'size' => 'tiny',
|
| 117 |
'unsigned' => TRUE,
|
| 118 |
'not null' => TRUE,
|
| 119 |
),
|
| 120 |
'commission' => array(
|
| 121 |
'type' => 'float',
|
| 122 |
'size' => 'big',
|
| 123 |
'not null' => FALSE,
|
| 124 |
),
|
| 125 |
'commission_notes' => array(
|
| 126 |
'type' => 'varchar',
|
| 127 |
'length' => '255',
|
| 128 |
'not null' => FALSE,
|
| 129 |
),
|
| 130 |
),
|
| 131 |
'indexes' => array(
|
| 132 |
'order_id' => array('order_id'),
|
| 133 |
'order_id_level_commission' => array('order_id', 'level', 'commission'),
|
| 134 |
),
|
| 135 |
);
|
| 136 |
|
| 137 |
$schema['uc_affiliate_referrals'] = array(
|
| 138 |
'fields' => array(
|
| 139 |
'affid' => array(
|
| 140 |
'type' => 'int',
|
| 141 |
'unsigned' => TRUE,
|
| 142 |
'not null' => TRUE,
|
| 143 |
'default' => 0,
|
| 144 |
),
|
| 145 |
'referral' => array(
|
| 146 |
'type' => 'int',
|
| 147 |
'unsigned' => TRUE,
|
| 148 |
'not null' => TRUE,
|
| 149 |
),
|
| 150 |
),
|
| 151 |
'primary key' => array('referral'),
|
| 152 |
);
|
| 153 |
|
| 154 |
return $schema;
|
| 155 |
}
|
| 156 |
|
| 157 |
/**
|
| 158 |
* Implementation of hook_install().
|
| 159 |
*/
|
| 160 |
function uc_affiliate_install() {
|
| 161 |
drupal_install_schema('uc_affiliate');
|
| 162 |
}
|
| 163 |
|
| 164 |
/**
|
| 165 |
* Implementation of hook_uninstall().
|
| 166 |
*/
|
| 167 |
function uc_affiliate_uninstall() {
|
| 168 |
drupal_uninstall_schema('uc_affiliate');
|
| 169 |
db_query("DELETE FROM {variable} WHERE name LIKE 'uc_affiliate_%%'");
|
| 170 |
}
|
| 171 |
|
| 172 |
|
| 173 |
/**
|
| 174 |
* Database update 1
|
| 175 |
*/
|
| 176 |
function uc_affiliate_update_1() {
|
| 177 |
$items = array();
|
| 178 |
$items[] = update_sql('ALTER TABLE {uc_affiliate_counts} ADD orders int unsigned NOT NULL');
|
| 179 |
$items[] = update_sql('ALTER TABLE {uc_affiliate_counts} CHANGE users users int unsigned NOT NULL');
|
| 180 |
$items[] = update_sql('CREATE TABLE {uc_affiliate_paid} (aid int unsigned NOT NULL, date int unsigned NOT NULL, amount double NOT NULL) /*!40100 DEFAULT CHARACTER SET utf8 */');
|
| 181 |
return $items;
|
| 182 |
}
|
| 183 |
|
| 184 |
|
| 185 |
/**
|
| 186 |
* Database update 2
|
| 187 |
*/
|
| 188 |
function uc_affiliate_update_2() {
|
| 189 |
$items = array();
|
| 190 |
$items[] = update_sql('ALTER TABLE {uc_affiliate_counts} ADD commission DOUBLE NOT NULL AFTER orders');
|
| 191 |
$items[] = update_sql('CREATE TABLE {uc_affiliate_products} (nid int NOT NULL, aid INT NOT NULL, commission varchar(255) NOT NULL, allowed INT DEFAULT "1" NOT NULL )');
|
| 192 |
$items[] = update_sql('ALTER TABLE {uc_affiliate_counts} ADD returns INT NOT NULL AFTER orders');
|
| 193 |
$items[] = update_sql('ALTER TABLE {uc_affiliate_paid} ADD admin_comments TEXT NOT NULL, ADD user_coments TEXT NOT NULL');
|
| 194 |
$items[] = update_sql('ALTER TABLE {uc_affiliate_paid} RENAME {uc_affiliate_payments}');
|
| 195 |
$items[] = update_sql('ALTER TABLE {uc_affiliate_commission} ADD level INT NOT NULL AFTER aid, ADD percent VARCHAR(50) NOT NULL AFTER level');
|
| 196 |
$items[] = update_sql('ALTER TABLE {uc_affiliate_clicks} RENAME {uc_affiliate_visits}');
|
| 197 |
$items[] = update_sql('ALTER TABLE {uc_affiliate_visits} CHANGE click_id visit_id INT ');
|
| 198 |
$items[] = update_sql('ALTER TABLE {uc_affiliate_counts} CHANGE clicks visits INT UNSIGNED');
|
| 199 |
$items[] = update_sql('ALTER TABLE {uc_affiliate_counts} CHANGE orders sales INT UNSIGNED');
|
| 200 |
$items[] = update_sql('ALTER TABLE {uc_affiliate_counts} ADD profit DOUBLE NOT NULL AFTER commission');
|
| 201 |
$items[] = update_sql('ALTER TABLE {uc_affiliate_payments} ADD ballance DOUBLE NOT NULL AFTER date');
|
| 202 |
$items[] = update_sql('ALTER TABLE {uc_affiliate_payments} CHANGE amount paid INT UNSIGNED');
|
| 203 |
$items[] = update_sql('ALTER TABLE {uc_affiliate_payments} ADD `pay_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST');
|
| 204 |
$items[] = update_sql('ALTER TABLE {uc_affiliate_payments} ADD method INT UNSIGNED NOT NULL AFTER paid');
|
| 205 |
$items[] = update_sql('ALTER TABLE {uc_affiliate_payments} ADD status INT UNSIGNED NOT NULL');
|
| 206 |
$items[] = update_sql('CREATE TABLE {uc_affiliate_payment_methods} (mid INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name varchar(255) NOT NULL, fee varchar(20) NOT NULL, minimum DOUBLE NOT NULL, pay_by TEXT NOT NULL, status INT DEFAULT "1" NOT NULL )');
|
| 207 |
$items[] = update_sql('CREATE TABLE {uc_affiliate_payment_data} (aid INT NOT NULL, mid INT NOT NULL, status INT NOT NULL, date INT NOT NULL, minimum DOUBLE NOT NULL, data TEXT NOT NULL)');
|
| 208 |
$items[] = update_sql('INSERT {sequences} VALUES ("{uc_affiliate_payments}_pay_id",(SELECT MAX(pay_id) FROM {uc_affiliate_payments}))');
|
| 209 |
|
| 210 |
$commissions = explode(',',variable_get('affiliate_commission_structure', '5,3,2,2,1'));
|
| 211 |
foreach ($commissions as &$commission) {
|
| 212 |
$commission .= '%';
|
| 213 |
}
|
| 214 |
$commissions = implode(',',$commission);
|
| 215 |
variable_set('affiliate_commission_structure', $commissions);
|
| 216 |
|
| 217 |
return $items;
|
| 218 |
}
|
| 219 |
|
| 220 |
/**
|
| 221 |
* Database update 6000: First update for Drupal 6.
|
| 222 |
*/
|
| 223 |
function uc_affiliate_update_6000() {
|
| 224 |
include_once drupal_get_path('module', 'uc_affiliate') .'/uc_affiliate.module';
|
| 225 |
$ret = array();
|
| 226 |
|
| 227 |
// uc_affiliate_products
|
| 228 |
db_change_field($ret, 'uc_affiliate_products', 'nid', 'nid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE));
|
| 229 |
db_change_field($ret, 'uc_affiliate_products', 'aid', 'affid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE));
|
| 230 |
db_change_field($ret, 'uc_affiliate_products', 'commission', 'commission', array('type' => 'text', 'size' => 'medium', 'not null' => TRUE));
|
| 231 |
db_change_field($ret, 'uc_affiliate_products', 'allowed', 'allowed', array('type' => 'int', 'size' => 'tiny', 'unsigned' => FALSE, 'not null' => TRUE));
|
| 232 |
db_add_primary_key($ret, 'uc_affiliate_products', array('nid', 'affid'));
|
| 233 |
|
| 234 |
db_query('UPDATE {uc_affiliate_products} SET commission = REPLACE(commission, "$", "")');
|
| 235 |
$result = db_query('SELECT nid, affid, commission FROM {uc_affiliate_products}');
|
| 236 |
while ($data = db_fetch_object($result)) {
|
| 237 |
$data->commission = uc_affiliate_parse_commission($data->commission, TRUE);
|
| 238 |
drupal_write_record('uc_affiliate_products', $data, array('nid', 'affid'));
|
| 239 |
}
|
| 240 |
|
| 241 |
// uc_affiliate_users
|
| 242 |
db_rename_table($ret, 'uc_affiliate_users', 'uc_affiliate_referrals');
|
| 243 |
db_drop_unique_key($ret, 'uc_affiliate_referrals', 'uid');
|
| 244 |
db_drop_primary_key($ret, 'uc_affiliate_referrals');
|
| 245 |
db_change_field($ret, 'uc_affiliate_referrals', 'aid', 'affid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE));
|
| 246 |
db_change_field($ret, 'uc_affiliate_referrals', 'uid', 'referral', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE));
|
| 247 |
db_add_primary_key($ret, 'uc_affiliate_referrals', array('referral'));
|
| 248 |
|
| 249 |
// uc_affiliate_counts
|
| 250 |
db_drop_primary_key($ret, 'uc_affiliate_counts');
|
| 251 |
db_change_field($ret, 'uc_affiliate_counts', 'aid', 'affid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE));
|
| 252 |
db_change_field($ret, 'uc_affiliate_counts', 'visits', 'visits', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE));
|
| 253 |
db_change_field($ret, 'uc_affiliate_counts', 'sales', 'sales', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE));
|
| 254 |
db_change_field($ret, 'uc_affiliate_counts', 'returns', 'returns', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE));
|
| 255 |
db_change_field($ret, 'uc_affiliate_counts', 'time', 'time', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE));
|
| 256 |
db_add_primary_key($ret, 'uc_affiliate_counts', array('affid', 'time'));
|
| 257 |
|
| 258 |
// uc_affiliate_commissions
|
| 259 |
db_rename_table($ret, 'uc_affiliate_commission', 'uc_affiliate_commissions');
|
| 260 |
db_drop_field($ret, 'uc_affiliate_commissions', 'commission_id');
|
| 261 |
db_drop_field($ret, 'uc_affiliate_commissions', 'percent');
|
| 262 |
db_change_field($ret, 'uc_affiliate_commissions', 'order_id', 'order_id', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE));
|
| 263 |
db_change_field($ret, 'uc_affiliate_commissions', 'aid', 'affid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE));
|
| 264 |
db_change_field($ret, 'uc_affiliate_commissions', 'level', 'level', array('type' => 'int', 'size' => 'tiny', 'unsigned' => TRUE, 'not null' => TRUE));
|
| 265 |
db_add_index($ret, 'uc_affiliate_commissions', 'order_id', array('order_id'));
|
| 266 |
db_add_index($ret, 'uc_affiliate_commissions', 'order_id_level_commission', array('order_id', 'level', 'commission'));
|
| 267 |
|
| 268 |
// uc_affiliate_visits
|
| 269 |
db_drop_table($ret, 'uc_affiliate_visits');
|
| 270 |
|
| 271 |
// uc_payments
|
| 272 |
db_drop_field($ret, 'uc_affiliate_payments', 'ballance');
|
| 273 |
db_change_field($ret, 'uc_affiliate_payments', 'aid', 'affid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE));
|
| 274 |
db_change_field($ret, 'uc_affiliate_payments', 'paid', 'paid', array('type' => 'float', 'size' => 'big', 'not null' => TRUE));
|
| 275 |
db_change_field($ret, 'uc_affiliate_payments', 'admin_comments', 'admin_comments', array('type' => 'text', 'size' => 'medium', 'not null' => FALSE));
|
| 276 |
db_change_field($ret, 'uc_affiliate_payments', 'user_coments', 'user_comments',array('type' => 'text', 'size' => 'medium', 'not null' => FALSE));
|
| 277 |
db_change_field($ret, 'uc_affiliate_payments', 'date', 'date', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE));
|
| 278 |
db_change_field($ret, 'uc_affiliate_payments', 'status', 'status', array('type' => 'int', 'size' => 'tiny', 'unsigned' => TRUE, 'not null' => TRUE));
|
| 279 |
db_change_field($ret, 'uc_affiliate_payments', 'method', 'details', array('type' => 'text', 'not null' => FALSE));
|
| 280 |
|
| 281 |
// uc_payment_data
|
| 282 |
db_rename_table($ret, 'uc_affiliate_payment_data', 'uc_affiliate_user_payment_data');
|
| 283 |
db_change_field($ret, 'uc_affiliate_user_payment_data', 'aid', 'affid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE));
|
| 284 |
db_change_field($ret, 'uc_affiliate_user_payment_data', 'mid', 'mid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE));
|
| 285 |
db_change_field($ret, 'uc_affiliate_user_payment_data', 'status', 'status', array('type' => 'int', 'size' => 'tiny', 'unsigned' => TRUE, 'not null' => TRUE));
|
| 286 |
db_change_field($ret, 'uc_affiliate_user_payment_data', 'data', 'details', array('type' => 'text', 'not null' => FALSE));
|
| 287 |
db_drop_field($ret, 'uc_affiliate_user_payment_data', 'date');
|
| 288 |
|
| 289 |
// uc_payment_methods
|
| 290 |
db_change_field($ret, 'uc_affiliate_payment_methods', 'mid', 'mid', array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE));
|
| 291 |
db_change_field($ret, 'uc_affiliate_payment_methods', 'fee', 'fee', array('type' => 'varchar', 'length' => 255, 'not null' => FALSE));
|
| 292 |
db_change_field($ret, 'uc_affiliate_payment_methods', 'status', 'status', array('type' => 'int', 'size' => 'tiny', 'unsigned' => TRUE, 'not null' => TRUE));
|
| 293 |
db_change_field($ret, 'uc_affiliate_payment_methods', 'pay_by', 'gateway', array('type' => 'varchar', 'length' => 255, 'not null' => FALSE));
|
| 294 |
|
| 295 |
$ret[] = update_sql('UPDATE {uc_affiliate_payment_methods} SET fee = REPLACE(fee, "$", "")');
|
| 296 |
$result = db_query('SELECT mid, fee FROM {uc_affiliate_payment_methods}');
|
| 297 |
while ($data = db_fetch_object($result)) {
|
| 298 |
$data->fee = uc_affiliate_parse_commission($data->fee, TRUE);
|
| 299 |
drupal_write_record('uc_affiliate_payment_methods', $data, array('mid'));
|
| 300 |
}
|
| 301 |
|
| 302 |
$ret[] = update_sql('DELETE FROM {uc_affiliate_user_payment_data} WHERE mid = 0');
|
| 303 |
|
| 304 |
// rename variables
|
| 305 |
$default = uc_affiliate_parse_commission(str_replace('$', '', variable_get('affiliate_commission_structure', '5,3,2,2,1')));
|
| 306 |
variable_set('uc_affiliate_commission', $default);
|
| 307 |
variable_del('affiliate_commission_structure');
|
| 308 |
$default = variable_get('affiliate_commission_order_status', array('completed'));
|
| 309 |
variable_set('uc_affiliate_commission_order_status', $default);
|
| 310 |
variable_del('affiliate_commission_order_status');
|
| 311 |
$default = variable_get('affiliate_commission_return_status', array('canceled'));
|
| 312 |
variable_set('uc_affiliate_commission_return_status', $default);
|
| 313 |
variable_del('affiliate_commission_return_status');
|
| 314 |
$default = variable_get('affiliate_invalid_redirect', '');
|
| 315 |
variable_set('uc_affiliate_invalid_redirect', $default);
|
| 316 |
variable_del('affiliate_invalid_redirect');
|
| 317 |
|
| 318 |
return $ret;
|
| 319 |
}
|
| 320 |
|