<?php
/**********************************************************************
***********************************************************************/

include_once($path_to_root . "/includes/date_functions.inc");


function upload_assets_import_file(&$Mode, $import_value)
{
    if ($Mode == 'ADD_ITEM' && (!isset($_FILES['filename']) || $_FILES['filename']['size'] == 0)){
		display_error(_("Select attachment file."));
        return;
    }
    
    switch ($import_value){
        case 'IMPORT_ASSETS':
            $unique_name = "Import_Assets";
            break;

        default:
            break;
    }

  
    $tmpname = $_FILES['filename']['tmp_name'];

    $dir =  company_path()."/import_files";
    if (!file_exists($dir))
    {
        mkdir ($dir,0777);
        $index_file = "<?php\nheader(\"Location: ../index.php\");\n?>";
        $fp = fopen($dir."/index.php", "w");
        fwrite($fp, $index_file);
        fclose($fp);
    }

    $dir =  company_path()."/import_files/asset_details";
    if (!file_exists($dir))
    {
        mkdir ($dir,0777);
        $index_file = "<?php\nheader(\"Location: ../index.php\");\n?>";
        $fp = fopen($dir."/index.php", "w");
        fwrite($fp, $index_file);
        fclose($fp);
    }

    $filename = basename($_FILES['filename']['name']);
    $filesize = $_FILES['filename']['size'];
    $filetype = $_FILES['filename']['type'];

		// file name compatible with POSIX
		// protect against directory traversal
        

    if ($filename && file_exists($dir."/".$unique_name)){
        unlink($dir."/".$unique_name);
    }

    move_uploaded_file($tmpname, $dir."/".$unique_name);

    $Mode = 'RESET';
}


function display_assets_import_file_data($project_id, $import_value, $import = false)
{
    if(list_updated('import_values')){
        clear_session_variable('Import_Assets');
    }
  
    $file_data = get_session_variable('Import_Assets');
    if($file_data ==''){
        $file_data = read_assets_import_file($project_id, $import_value);
    }
    
    switch ($import_value){
        case 'IMPORT_ASSETS':
//            display_stock_item_file_data($project_id, $file_data, $import);
            break;

        default:
            break;
    }
    
    
    end_table();

    set_session_variable('Import_Assets', $file_data);
    
}
    

function read_assets_import_file($project_id, $import_value)
{
        $file_data = array();
        
        switch ($import_value){
            case 'IMPORT_ASSETS':
                $file_name = "Import_Assets";
                break;

            default:
                break;
        }
        
        $file_path_name = company_path()."/import_files/asset_details/".$file_name;
        
        if(file_exists($file_path_name)){
            $file_handle = fopen($file_path_name, "r");
        }
        else{
            return array();
        }
        
        $header_line = true;
        if ($file_handle){            
            while (!feof($file_handle)) 
            {
                $line = fgetcsv($file_handle, 1024);
                if($header_line){
                    $header_line =  false;
                }
                else{
                    switch ($import_value){
                        
                        case 'IMPORT_ASSETS':
                            read_asset_details_import_file_data($file_data, $line);
                            break;

                        default:
                            break;
                    }
                    
                }
            } 
            
            fclose($file_handle);
            
            return $file_data;
        } 
        else {

        }
}



?>
