shop.balmet.com

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

1002.php (5320B)


      1 <?php
      2 class ModelUpgrade1002 extends Model {
      3 	public function upgrade() {
      4 		// setting
      5 		$query = $this->db->query("SELECT setting_id FROM `" . DB_PREFIX . "setting` WHERE `key` = 'config_product_limit'");
      6 
      7 		if (!$query->num_rows) {
      8 			$this->db->query("INSERT INTO `" . DB_PREFIX . "setting` SET `key` = 'config_product_limit', `value` = '20', `code` = 'config', `store_id` = 0");
      9 		}
     10 
     11 		//  setting
     12 		$query = $this->db->query("SELECT setting_id FROM `" . DB_PREFIX . "setting` WHERE `store_id` = '0' AND `key` = 'config_voucher_min'");
     13 
     14 		if (!$query->num_rows) {
     15 			$this->db->query("INSERT INTO `" . DB_PREFIX . "setting` SET `key` = 'config_voucher_min', `value` = '1', `code` = 'config', `store_id` = 0");
     16 		}
     17 
     18 		//  setting
     19 		$query = $this->db->query("SELECT setting_id FROM `" . DB_PREFIX . "setting` WHERE `store_id` = '0' AND `key` = 'config_voucher_max'");
     20 
     21 		if (!$query->num_rows) {
     22 			$this->db->query("INSERT INTO `" . DB_PREFIX . "setting` SET `key` = 'config_voucher_max', `value` = '1000', `code` = 'config', `store_id` = 0");
     23 		}
     24 
     25 		// customer
     26 		$query = $this->db->query("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '" . DB_DATABASE . "' AND TABLE_NAME = '" . DB_PREFIX . "customer' AND COLUMN_NAME = 'safe'");
     27 
     28 		if (!$query->num_rows) {
     29 			$this->db->query("ALTER TABLE `" . DB_PREFIX . "customer` ADD `safe` tinyint(1) NOT NULL AFTER `approved`");
     30 		}
     31 
     32 		// customer
     33 		$query = $this->db->query("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '" . DB_DATABASE . "' AND TABLE_NAME = '" . DB_PREFIX . "customer_group' AND COLUMN_NAME = 'name'");
     34 
     35 		if ($query->num_rows) {
     36 			$customer_group_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer_group`");
     37 
     38 			foreach ($customer_group_query->rows as $customer_group) {
     39 				$language_query = $this->db->query("SELECT `language_id` FROM `" . DB_PREFIX . "language`");
     40 
     41 				foreach ($language_query->rows as $language) {
     42 					$this->db->query("INSERT INTO `" . DB_PREFIX . "customer_group_description` SET `customer_group_id` = '" . (int)$customer_group['customer_group_id'] . "', `language_id` = '" . (int)$language['language_id'] . "', `name` = '" . $this->db->escape($customer_group['name']) . "'");
     43 				}
     44 			}
     45 
     46 			$this->db->query("ALTER TABLE `" . DB_PREFIX . "customer_group` DROP `name`");
     47 		}
     48 
     49 		// product_option
     50 		$query = $this->db->query("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '" . DB_DATABASE . "' AND TABLE_NAME = '" . DB_PREFIX . "product_option' AND COLUMN_NAME = 'option_value'");
     51 
     52 		if ($query->num_rows) {
     53 			// Drop product option value if exsits
     54 			$query = $this->db->query("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '" . DB_DATABASE . "' AND TABLE_NAME = '" . DB_PREFIX . "product_option' AND COLUMN_NAME = 'value'");
     55 
     56 			if ($query->num_rows) {
     57 				$this->db->query("ALTER TABLE `" . DB_PREFIX . "product_option` DROP `value`");
     58 			}
     59 
     60 			$this->db->query("ALTER TABLE `" . DB_PREFIX . "product_option` CHANGE `option_value` `value` TEXT NOT NULL");
     61 		}
     62 
     63 		// category
     64 		$primary_data = array();
     65 
     66 		$query = $this->db->query("SHOW KEYS FROM `" . DB_PREFIX . "category` WHERE Key_name = 'PRIMARY'");
     67 
     68 		foreach ($query->rows as $result) {
     69 			$primary_data[] = $result['Column_name'];
     70 		}
     71 
     72 		if (!in_array('category_id', $primary_data) || !in_array('parent_id', $primary_data)) {
     73 			$this->db->query("ALTER TABLE `" . DB_PREFIX . "category` DROP PRIMARY KEY, ADD PRIMARY KEY(`category_id`, `parent_id`)");
     74 		}
     75 
     76 		// category
     77 		$query = $this->db->query("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '" . DB_DATABASE . "' AND TABLE_NAME = '" . DB_PREFIX . "category_description' AND COLUMN_NAME = 'meta_title'");
     78 
     79 		if (!$query->num_rows) {
     80 			$this->db->query("ALTER TABLE `" . DB_PREFIX . "category_description` ADD `meta_title` varchar(255) NOT NULL AFTER `description`");
     81 		}
     82 
     83 		// Sort the categories to take advantage of the nested set model
     84 		$this->repairCategories(0);
     85 	}
     86 
     87 	// Function to repair any erroneous categories that are not in the category path table.
     88 	public function repairCategories($parent_id = 0) {
     89 		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "category` WHERE `parent_id` = '" . (int)$parent_id . "'");
     90 
     91 		foreach ($query->rows as $category) {
     92 			// Delete the path below the current one
     93 			$this->db->query("DELETE FROM `" . DB_PREFIX . "category_path` WHERE `category_id` = '" . (int)$category['category_id'] . "'");
     94 
     95 			// Fix for records with no paths
     96 			$level = 0;
     97 
     98 			$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "category_path` WHERE `category_id` = '" . (int)$parent_id . "' ORDER BY `level` ASC");
     99 
    100 			foreach ($query->rows as $result) {
    101 				$this->db->query("INSERT INTO `" . DB_PREFIX . "category_path` SET `category_id` = '" . (int)$category['category_id'] . "', `path_id` = '" . (int)$result['path_id'] . "', `level` = '" . (int)$level . "'");
    102 
    103 				$level++;
    104 			}
    105 
    106 			$this->db->query("REPLACE INTO `" . DB_PREFIX . "category_path` SET `category_id` = '" . (int)$category['category_id'] . "', `path_id` = '" . (int)$category['category_id'] . "', `level` = '" . (int)$level . "'");
    107 
    108 			$this->repairCategories($category['category_id']);
    109 		}
    110 	}
    111 }