<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_Fix_items_issue extends CI_Migration {
    function up() {
        if (!file_exists(APPPATH.'modules/items/controllers/admin.php')) {
	    # This installation suffered from the 3.1.6 no-new-folder bug. Let's treat it.
	    
	    $this->load->model('upgrade/update_system_m', 'u');
	    if ($this->u->write) {
		mkdir(APPPATH.'modules/items/');
		mkdir(APPPATH.'modules/items/controllers');
		mkdir(APPPATH.'modules/items/models');
		mkdir(APPPATH.'modules/items/views');
		
		$this->u->set_file_contents(APPPATH.'modules/items/controllers/admin.php', base64_decode('[encoded data, 7396 characters]'));
		$this->u->set_file_contents(APPPATH.'modules/items/models/item_m.php', base64_decode('PD9waHAgZGVmaW5lZCgnQkFTRVBBVEgnKSBPUiBleGl0KCdObyBkaXJlY3Qgc2NyaXB0IGFjY2VzcyBhbGxvd2VkJyk7Ci8qKgogKiBQYW5jYWtlCiAqCiAqIEEgc2ltcGxlLCBmYXN0LCBzZWxmLWhvc3RlZCBpbnZvaWNpbmcgYXBwbGljYXRpb24KICoKICogQHBhY2thZ2UJCVBhbmNha2UKICogQGF1dGhvcgkJUGFuY2FrZSBEZXYgVGVhbQogKiBAY29weXJpZ2h0CUNvcHlyaWdodCAoYykgMjAxMCwgUGFuY2FrZSBQYXltZW50cwogKiBAbGljZW5zZQkJaHR0cDovL3BhbmNha2VhcHAuY29tL2xpY2Vuc2UKICogQGxpbmsJCWh0dHA6Ly9wYW5jYWtlYXBwLmNvbQogKiBAc2luY2UJCVZlcnNpb24gMS4wCiAqLwoKLy8gLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tCgovKioKICogVGhlIEl0ZW0gTW9kZWwKICoKICogQHN1YnBhY2thZ2UJTW9kZWxzCiAqIEBjYXRlZ29yeQlJdGVtcwogKi8KY2xhc3MgSXRlbV9tIGV4dGVuZHMgUGFuY2FrZV9Nb2Rlb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dGlvbnMiPgoJCQkJCTw/cGhwIGVjaG8gYW5jaG9yKCdhZG1pbi9pdGVtcy9lZGl0LycuJGl0ZW0tPmlkLCBfXygnZ2xvYmFsOmVkaXQnKSwgYXJyYXkoJ2NsYXNzJyA9PiAnaWNvbiBlZGl0JywgJ3RpdGxlJyA9PiBfXygnZ2xvYmFsOmVkaXQnKSkpOyA/PiAKCQkJCQk8P3BocCBlY2hvIGFuY2hvcignYWRtaW4vaXRlbXMvZGVsZXRlLycuJGl0ZW0tPmlkLCBsYW5nKCdnbG9iYWw6ZGVsZXRlJyksIGFycmF5KCdjbGFzcycgPT4gJ2ljb24gZGVsZXRlJywgJ3RpdGxlJyA9PiBfXygnZ2xvYmFsOmRlbGV0ZScpKSk7ID8+CgkJCQkJPC90ZD4KCQkJCTwvdHI+CgkJCTw/cGhwIGVuZGZvcmVhY2g7ID8+CgkJCTwvdGJvZHk+CgkJPC90YWJsZT4KCTwvZGl2Pgo8L2Rpdj4KCjxkaXYgY2xhc3M9InBhZ2luYXRpb24iPgoJPD9waHAgZWNobyAkdGhpcy0+cGFnaW5hdGlvbi0+Y3JlYXRlX2xpbmtzKCk7ID8+CjwvZGl2PgoKPD9waHAgZW5kaWY7ID8+Cg=='));
		$this->u->set_file_contents(APPPATH.'modules/items/views/view.php', base64_decode('[encoded data, 9572 characters]'));
		
	    } else {
		# FTP should have created folders as necessary in 3.1.6, as this bug doesn't affect FTP. Nonetheless, I like safeguards.
		show_error("Pancake's having an issue upgrading. Please contact support@pancakeapp.com as soon as possible. The error code is FII047. Please mention it in the email.");
	    }
	    
	}
    }
    
    function down() {
        
    }
}