summaryrefslogtreecommitdiff
path: root/src/files.php
blob: 2a68d9d13b3d79d30c46a49f83ffff3cf9c3a083 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
// src/files.php
//
// fonctions pour manipuler les fichiers latex et pdf

function makeFile($path, $file_name, $data)
{
    //~ if(is_writable('../' . $path))
    //~ {
    file_put_contents($path. $file_name, $data);
    chmod($path . $file_name, 0644); // droits en octal
    //~ }

    //~ protected function createFile(string $latex, string $file_name, string $latexPath)
    //~ {
    //~ // nom du fichier créé = nom.tex
    //~ // pour les devis, factures et enveloppes, le nom est le code la prestation
    //~ // pour les livre de recettes et registres des achats mensuels:
    //~ // le nom du fichier suit cet exemple: "Recettes-2022-06-Juin.tex"
    //~ // pour le livre de recette ou le registre des achats annuel, même principe: "Achats-2022.tex"
    //~ // pour le bilan comptable annuel, ça donne: "Bilan-2022.tex"
    //~ $fichier = fopen($latexPath . $file_name, "w+");
    //~ fputs($fichier, $latex);
    //~ fclose($fichier);
    //~ }
}

function makeFolder(string $path)
{
    $rights = 0755; // droits en octal

    if(!file_exists($path))
    {
	//~ if(is_writable('../' . $path))
	//~ {
	mkdir($path);
	chmod($path, $rights);
	//~ }
	//~ else
	//~ {
	//~ echo "debug: la création du dossier " . $path . " est impossible\n";
	//~ }
    }
    else
    {
	//~ if(is_writable($path))
	//~ {
	chmod($path, $rights);
	//~ }
	//~ else
	//~ {
	//~ echo "debug: la modification des droits du dossier " . $path . " est impossible\n";
	//~ }
    }
}

// commande système pdflatex
function latexToPdf(string $latex_path, string $file_name, string $pdf_path)
{
    // paramètre optionnel
    $output_dir = '';
    if($pdf_path !== '')
    {
        $output_dir = '-output-directory=' . $pdf_path;
    }
    
    // compilation
    //echo 'pdflatex ' . $output_dir . ' ' . $latex_path . $file_name . "\n";
    //die;
    exec('pdflatex ' . $output_dir . ' ' . $latex_path . $file_name);
    
    // nettoyage
    $basename = basename($file_name, '.tex');
    unlink($pdf_path . $basename . '.aux');
    unlink($pdf_path . $basename . '.log');
}

// équivalent de la commande mv
function renameFiles(Object $Object, string $old_code, string $new_code)
{
    rename($Object->getLatexPath() . $old_code . '.tex',
	$Object->getLatexPath() . $new_code . '.tex');
    rename($Object->getPdfPath() . $old_code . '.pdf',
	$Object->getPdfPath() . $new_code . '.pdf');
}

function makeTexAndPdf(Object $Object) // paramètre = enfant de Latex
{
    if(get_class($Object) !== 'EnveloppeVersoLatex')
    {
        makeFolder($Object->getLatexPath());
        makeFolder($Object->getPdfPath());
    }
    makeFile($Object->getLatexPath(), $Object->getFileName(), $Object->getLatex()); // fichier .tex
    latexToPdf($Object->getLatexPath(), $Object->getFileName(), $Object->getPdfPath()); // fichier .pdf avec pdflatex
}

//~ function removeFile(Object $Object)
//~ {}