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

class Backup extends CI_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->dbutil();
        $this->load->model(array('Backup_model', 'Ion_auth_model'));
        $this->load->database();
        $this->load->library(array('ion_auth', 'form_validation'));
        $this->load->helper(array('url', 'language', 'file'));
        $this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth'));
        $this->lang->load('auth');
    }

    public function index()
    {

        $row = $this->Backup_model->get_style();
        $data2 = array(
            'web_meta_color' => $row->meta_color,
            'web_navbar_color' => $row->navbar_color,
            'web_box_color' => $row->box_color,
            'web_box_bottom' => $row->box_bottom,
        );

        $list_file = array();
        $dir = "public/data/backup/";

        if (is_dir($dir)) {
            if ($dh = opendir($dir)) {
                while (($file = readdir($dh)) !== false) {
                    $list_file[] = $file;
                }
                closedir($dh);
            }
        }

        $data['daftar_file'] = $list_file;
        $this->template->load('template', 'backup/backup_list', $data + $data2);
    }

    public function backup()
    {

        $config = array(
            'format'    => 'zip',
            'filename'    => 'database.sql'
        );

        $backup = $this->dbutil->backup($config);

        $save = FCPATH . 'public/data/backup/backup_' . date("Y-m-d_H-i-s") . '-db.zip';

        write_file($save, $backup);
        redirect(site_url('backup'));
    }

    public function hapus($filenya)
    {
        $dir   = 'public/data/backup/';
        if (unlink($dir . $filenya)) {
            $this->session->set_flashdata('message', '3131');
            redirect(site_url('backup'));
        }
    }
}
