Projet

Général

Profil

Paperstream.php

Henri QUENEAU, 01/06/2018 16:10

 
1
<?php
2

    
3
class Paperstream 
4
{
5
    private $Batch;
6

    
7
    function __construct()
8
    {
9
        $this->Batch = $_SESSION['capture']->Batch;
10
    }
11
    
12
    function separateSubjectPDF($ScanSource,$ResultDirectory = false)
13
    {
14

    
15
        echo "Init process ...\n";
16
        $_SESSION['capture']->logEvent(
17
            "Init process ... "
18
        );
19

    
20
        if (!is_readable($ScanSource)) {
21
            echo "Source directory is not valid !\n";
22
            $_SESSION['capture']->logEvent(
23
                "Source directory is not valid !"
24
            );
25
            exit();
26
        }
27

    
28
        if (!is_readable($ScanSource)) {
29
            echo "Result directory is not valid ! \n";
30
            $_SESSION['capture']->logEvent(
31
                "Result directory is not valid !"
32
            );
33
            exit();
34
        }
35

    
36
        $files = array_diff(scandir($ScanSource), array('..', '.','FAILED'));
37
        //print_r($files);
38

    
39
        if (empty($files)) {
40
            echo "No files to process ! End of process ...\n";
41
            $_SESSION['capture']->logEvent(
42
                "No files to process ! End of process ..."
43
            );
44
            exit();
45
        }
46

    
47
        echo "There is ".count($files)." file(s) to process\n";
48
        $_SESSION['capture']->logEvent(
49
            "There is ".count($files)." to process"
50
        );
51
        $num_file = 1;
52
        foreach ($files as $key => $value) {
53
            echo "\n\n * File n°".$num_file.": ".$files[$key]." *\n";
54
            $_SESSION['capture']->logEvent(
55
                "* File n°".$num_file.": ".$files[$key]." *"
56
            );
57
            $array_files = explode('.', $files[$key]);
58
            
59

    
60
            //Ignore all files except pdf
61
            if (strtolower($array_files[1]) == 'pdf') {
62

    
63
                echo "\n\n * Séparation du service et du code barre * \n";
64
                $data = explode('_', $array_files[0]);
65

    
66
                $destination = $data[0];
67
                $barcode = $data[1];
68

    
69
                echo "\n\n * Le service est ".$destination." *\n";
70
                echo "\n\n * Le codebarre est ".$barcode." *\n";
71
                //add document in folder 
72
                $Document = $this->Batch->addDocument($ScanSource.$files[$key]);
73
                //add destination data
74
                $Document->setMetadata(
75
                    "destination", 
76
                    $destination
77
                );
78
                $Document->setMetadata(
79
                    "barcode", 
80
                    $barcode
81
                );
82
                //delete scan in source
83
                unlink ($ScanSource.$files[$key]); 
84
            } else {
85
                echo "No pdf format ! skipping ...\n";
86
                $_SESSION['capture']->logEvent(
87
                    "No pdf format ! skipping ...\n"
88
                );
89
            }
90
            $num_file++;
91
        }
92

    
93
        $files = glob(realpath('tmp').'/*'); // get all file names
94
        foreach($files as $file){ // iterate files
95
          if(is_file($file))
96
            unlink($file); // delete file
97
        }
98

    
99
        echo "End of process ...\n";
100
        $_SESSION['capture']->logEvent(
101
            "End of process ..."
102
        );
103

    
104
    }
105

    
106
}