<?php

class GeneralClass {

    protected $info = array();
    private $db;

    public function __construct() {
        $this->db = DBConnectorFactory::loadDriver();
        $this->logged_id = Session::getUserID();
        $this->current_date = date('Y-m-d H:i:s');
    }

    public function setData($data) {
        $this->info = $data;

        return $this;
    }

    public static function submit_archive($data,$stop_trans = false) {
        $db = DBConnectorFactory::loadDriver();
        $logged_id = Session::getUserID();
        $current_date = date('Y-m-d H:i:s');
        if(!$stop_trans)
            $db->query('START TRANSACTION');
        $file_archive = $_FILES['archive_name'];
        $archive_title = $_POST['archive_title'];
        $archive_details = $_POST['archive_details'];
        $old_archive_name = $data['old_archive_name'];
        $file_name = !empty($_FILES['archive_name']['name']) ?  (microtime(true) . '.' . pathinfo($_FILES['archive_name']['name'], PATHINFO_EXTENSION)) : $old_archive_name;
        $main_id = !empty($data['main_id']) ? $data['main_id'] : 0;
        $main_type = !empty($data['main_type']) ? $data['main_type'] : 'gen';
        $archive_id = $data['archive_id'];
        if($archive_id == 0){
            $db->prepare_Stmt('insert into  tbl_archive 
            (archive_name,main_id,main_type,date_created,date_time,created_logged_id,Logged_ID,archive_title,archive_details) VALUES(?,?,?,?,?,?,?,?,?)', 
                              [&$file_name,&$main_id,&$main_type, &$current_date, &$current_date,
                               &$logged_id,  &$logged_id,  &$archive_title,  &$archive_details]);
        }else{
            $db->prepare_Stmt('update tbl_archive 
            set archive_name =?,main_id=?,main_type=?,date_time = ?,Logged_ID = ?,archive_title = ?,archive_details = ? where archive_id = ?',
                              [&$file_name,&$main_id,&$main_type,
                               &$current_date,&$logged_id,&$archive_title,&$archive_details,&$archive_id]);
        }

        $error = $db->error();
        if (empty($error)) {
            if (!empty($_FILES['archive_name']['name'])) {
                $archive_path = dirBase . "uploads/archive";
                $destination_archive = $archive_path . "/" . $file_name;
                move_uploaded_file($file_archive['tmp_name'], $destination_archive);
            }
            if(!empty($old_archive_name) && $archive_id != 0 && !empty($_FILES['archive_name']['name'])){
                $del_archive_path = dirBase . "uploads/archive/{$old_archive_name}";
                if (file_exists($del_archive_path))
                    unlink(dirBase . "uploads/archive/{$old_archive_name}");  
            }
            if(!$stop_trans)
                $db->query('COMMIT');
        } else {
            if(!$stop_trans)
                $db->query('ROLLBACK');
        }
        return array_unique($error);
    }
    public function  delete_archive(){
        $this->db->prepare_Stmt('Delete from tbl_archive WHERE archive_id = ?', [&$this->info['archive_id']]);
        $error = $this->db->error();
        if(empty($error)){
            $del_archive_path = dirBase . "uploads/archive/{$this->info['archive_name']}";
            if (file_exists($del_archive_path))
                unlink(dirBase . "uploads/archive/{$this->info['archive_name']}");  
        }
        return $error;
    }

}
