shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

1009.php (9493B)


      1 <?php
      2 class ModelUpgrade1009 extends Model {
      3 	public function upgrade() {
      4 		// Affiliate customer merge code
      5 		$query = $this->db->query("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '" . DB_DATABASE . "' AND TABLE_NAME = '" . DB_PREFIX . "affiliate'");
      6 		
      7 		if ($query->num_rows) {
      8 			// Removing affiliate and moving to the customer account.
      9 			$config = new Config();
     10 			
     11 			$setting_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "setting` WHERE store_id = '0'");
     12 			
     13 			foreach ($setting_query->rows as $setting) {
     14 				$config->set($setting['key'], $setting['value']);
     15 			}
     16 			
     17 			$affiliate_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "affiliate`");
     18 			
     19 			foreach ($affiliate_query->rows as $affiliate) {
     20 				$customer_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer` WHERE `email` = '" . $this->db->escape($affiliate['email']) . "'");
     21 				
     22 				if (!$customer_query->num_rows) {
     23 					$this->db->query("INSERT INTO `" . DB_PREFIX . "customer` SET `customer_group_id` = '" . (int)$config->get('config_customer_group_id') . "', `language_id` = '" . (int)$config->get('config_customer_group_id') . "', `firstname` = '" . $this->db->escape($affiliate['firstname']) . "', `lastname` = '" . $this->db->escape($affiliate['lastname']) . "', `email` = '" . $this->db->escape($affiliate['email']) . "', `telephone` = '" . $this->db->escape($affiliate['telephone']) . "', `password` = '" . $this->db->escape($affiliate['password']) . "', `salt` = '" . $this->db->escape($affiliate['salt']) . "', `cart` = '" . $this->db->escape(json_encode(array())) . "', `wishlist` = '" . $this->db->escape(json_encode(array())) . "', `newsletter` = '0', `custom_field` = '" . $this->db->escape(json_encode(array())) . "', `ip` = '" . $this->db->escape($affiliate['ip']) . "', `status` = '" . $this->db->escape($affiliate['status']) . "', `approved` = '" . (int)$affiliatee['approved'] . "', `date_added` = '" . $this->db->escape($affiliate['date_added']) . "'");
     24 					
     25 					$customer_id = $this->db->getLastId();
     26 					
     27 					$this->db->query("INSERT INTO " . DB_PREFIX . "address SET customer_id = '" . (int)$customer_id . "', firstname = '" . $this->db->escape($affiliate['firstname']) . "', lastname = '" . $this->db->escape($affiliate['lastname']) . "', company = '" . $this->db->escape($affiliate['company']) . "', address_1 = '" . $this->db->escape($affiliate['address_1']) . "', address_2 = '" . $this->db->escape($affiliate['address_2']) . "', city = '" . $this->db->escape($affiliate['city']) . "', postcode = '" . $this->db->escape($affiliate['postcode']) . "', zone_id = '" . (int)$affiliate['zone_id'] . "', country_id = '" . (int)$affiliate['country_id'] . "', custom_field = '" . $this->db->escape(json_encode(array())) . "'");
     28 			
     29 					$address_id = $this->db->getLastId();
     30 			
     31 					$this->db->query("UPDATE " . DB_PREFIX . "customer SET address_id = '" . (int)$address_id . "' WHERE customer_id = '" . (int)$customer_id . "'");
     32 				} else {
     33 					$customer_id = $customer_query->row['customer_id'];
     34 				}
     35 				
     36 				$customer_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer_affiliate` WHERE `customer_id` = '" . (int)$customer_id . "'");
     37 				
     38 				if (!$customer_query->num_rows) {
     39 					$this->db->query("INSERT INTO `" . DB_PREFIX . "customer_affiliate` SET `customer_id` = '" . (int)$customer_id . "', `company` = '" . $this->db->escape($affiliate['company']) . "', `tracking` = '" . $this->db->escape($affiliate['code']) . "', `commission` = '" . (float)$affiliate['commission'] . "', `tax` = '" . $this->db->escape($affiliate['tax']) . "', `payment` = '" . $this->db->escape($affiliate['payment']) . "', `cheque` = '" . $this->db->escape($affiliate['cheque']) . "', `paypal` = '" . $this->db->escape($affiliate['paypal']) . "', `bank_name` = '" . $this->db->escape($affiliate['bank_name']) . "', `bank_branch_number` = '" . $this->db->escape($affiliate['bank_branch_number']) . "', `bank_account_name` = '" . $this->db->escape($affiliate['bank_account_name']) . "', `bank_account_number` = '" . $this->db->escape($affiliate['bank_account_number']) . "', `status` = '" . (int)$affiliate['status'] . "', `date_added` = '" . $this->db->escape($affiliate['date_added']) . "'");
     40 				}
     41 				
     42 				$affiliate_transaction_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "affiliate_transaction` WHERE `affiliate_id` = '" . (int)$affiliate['affiliate_id'] . "'");
     43 			
     44 				foreach ($affiliate_transaction_query->rows as $affiliate_transaction) {
     45 					$this->db->query("INSERT INTO " . DB_PREFIX . "customer_transaction SET customer_id = '" . (int)$customer_id . "', order_id = '" . (int)$affiliate_transaction['order_id'] . "', description = '" . $this->db->escape($affiliate_transaction['description']) . "', amount = '" . (float)$affiliate_transaction['amount'] . "', `date_added` = '" . $this->db->escape($affiliate_transaction['date_added']) . "'");
     46 					
     47 					$this->db->query("DELETE FROM " . DB_PREFIX . "affiliate_transaction WHERE affiliate_transaction_id = '" . (int)$affiliate_transaction['affiliate_transaction_id'] . "'");
     48 				}
     49 				
     50 				$this->db->query("UPDATE `" . DB_PREFIX . "order` SET `affiliate_id` = '" . (int)$customer_id . "' WHERE affiliate_id = '" . (int)$affiliate['affiliate_id'] . "'");
     51 			}
     52 			
     53 			$this->db->query("DROP TABLE `" . DB_PREFIX . "affiliate`");
     54 			
     55 			$affiliate_query = $this->db->query("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '" . DB_DATABASE . "' AND TABLE_NAME = '" . DB_PREFIX . "affiliate_activity'");
     56 			
     57 			if (!$affiliate_query->num_rows) {
     58 				$this->db->query("DROP TABLE `" . DB_PREFIX . "affiliate_activity`");
     59 			}
     60 			
     61 			$affiliate_query = $this->db->query("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '" . DB_DATABASE . "' AND TABLE_NAME = '" . DB_PREFIX . "affiliate_login'");
     62 			
     63 			if (!$affiliate_query->num_rows) {			
     64 				$this->db->query("DROP TABLE `" . DB_PREFIX . "affiliate_login`");
     65 			}
     66 			
     67 			$this->db->query("DROP TABLE `" . DB_PREFIX . "affiliate_transaction`");
     68 		}
     69 	
     70 		$query = $this->db->query("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '" . DB_DATABASE . "' AND TABLE_NAME = '" . DB_PREFIX . "api' AND COLUMN_NAME = 'name'");
     71 		
     72 		if ($query->num_rows) {
     73 			$this->db->query("ALTER TABLE `" . DB_PREFIX . "api` CHANGE `name` `username` VARCHAR(64) NOT NULL");
     74 		}
     75 		
     76 		// Events
     77 		$query = $this->db->query("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '" . DB_DATABASE . "' AND TABLE_NAME = '" . DB_PREFIX . "event' AND COLUMN_NAME = 'sort_order'");
     78 		
     79 		if (!$query->num_rows) {
     80 			$this->db->query("ALTER TABLE `" . DB_PREFIX . "event` ADD `sort_order` INT(3) NOT NULL AFTER `action`");
     81 		}
     82 
     83 		$query = $this->db->query("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '" . DB_DATABASE . "' AND TABLE_NAME = '" . DB_PREFIX . "event' AND COLUMN_NAME = 'date_added'");
     84 		
     85 		if ($query->num_rows) {
     86 			$this->db->query("ALTER TABLE `" . DB_PREFIX . "event` DROP COLUMN `date_added`");
     87 		}
     88 						
     89 		// OPENCART_SERVER
     90 		$upgrade = true;
     91 		
     92 		$file = DIR_OPENCART . 'admin/config.php';
     93 		
     94 		$lines = file(DIR_OPENCART . 'admin/config.php');
     95 
     96 		foreach ($lines as $line) {
     97 			if (strpos(strtoupper($line), 'OPENCART_SERVER') !== false) {
     98 				$upgrade = false;
     99 
    100 				break;
    101 			}
    102 		}
    103 
    104 		if ($upgrade) {
    105 			$output = '';
    106 
    107 			foreach ($lines as $line_id => $line) {
    108 				if (strpos($line, 'DB_PREFIX') !== false) {
    109 					$output .= $line . "\n\n";
    110 					$output .= 'define(\'OPENCART_SERVER\', \'http://www.opencart.com/\');' . "\n";
    111 				} else {
    112 					$output .= $line;
    113 				}
    114 			}
    115 
    116 			$handle = fopen($file, 'w');
    117 
    118 			fwrite($handle, $output);
    119 
    120 			fclose($handle);
    121 		}
    122 	
    123 		$files = glob(DIR_OPENCART . '{config.php,admin/config.php}', GLOB_BRACE);
    124 
    125 		foreach ($files as $file) {
    126 			$lines = file($file);
    127 	
    128 			for ($i = 0; $i < count($lines); $i++) { 
    129 				if ((strpos($lines[$i], 'DIR_IMAGE') !== false) && (strpos($lines[$i + 1], 'DIR_STORAGE') === false)) {
    130 					array_splice($lines, $i + 1, 0, array('define(\'DIR_STORAGE\', DIR_SYSTEM . \'storage/\');'));
    131 				}
    132 
    133 				if ((strpos($lines[$i], 'DIR_MODIFICATION') !== false) && (strpos($lines[$i + 1], 'DIR_SESSION') === false)) {
    134 					array_splice($lines, $i + 1, 0, array('define(\'DIR_SESSION\', DIR_STORAGE . \'session/\');'));
    135 				}
    136 
    137 				if (strpos($lines[$i], 'DIR_CACHE') !== false) {
    138 					$lines[$i] = 'define(\'DIR_CACHE\', DIR_STORAGE . \'cache/\');' . "\n";
    139 				}
    140 
    141 				if (strpos($lines[$i], 'DIR_DOWNLOAD') !== false) {
    142 					$lines[$i] = 'define(\'DIR_DOWNLOAD\', DIR_STORAGE . \'download/\');' . "\n";
    143 				}
    144 
    145 				if (strpos($lines[$i], 'DIR_LOGS') !== false) {
    146 					$lines[$i] = 'define(\'DIR_LOGS\', DIR_STORAGE . \'logs/\');' . "\n";
    147 				}
    148 
    149 				if (strpos($lines[$i], 'DIR_MODIFICATION') !== false) {
    150 					$lines[$i] = 'define(\'DIR_MODIFICATION\', DIR_STORAGE . \'modification/\');' . "\n";
    151 				}
    152 				
    153 				if (strpos($lines[$i], 'DIR_SESSION') !== false) {
    154 					$lines[$i] = 'define(\'DIR_SESSION\', DIR_STORAGE . \'session/\');' . "\n";
    155 				}				
    156 	
    157 				if (strpos($lines[$i], 'DIR_UPLOAD') !== false) {
    158 					$lines[$i] = 'define(\'DIR_UPLOAD\', DIR_STORAGE . \'upload/\');' . "\n";
    159 				}
    160 			}
    161 			
    162 			$output = implode('', $lines);
    163 			
    164 			$handle = fopen($file, 'w');
    165 
    166 			fwrite($handle, $output);
    167 
    168 			fclose($handle);
    169 		}
    170 	}
    171 }