summaryrefslogtreecommitdiff
path: root/src/functions.php
blob: b8779e84e1af349e0ee2764c65c44f5b9c2f77c8 (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
<?php
// src/functions.php
//
// gros fourre-tout, à ranger plus tard


// commande pour lancer une application graphique en ouvrant un fichier
function windowAppCommand(string $app, string $path = ''): string
{
    // attention, la syntaxe utilisée est la plus simple: "app fichier"
    // ça fonctionne avec les logiciels choisis: gimp, scribus
    // mais ça pourrait ne pas convenir pour d'autres

    $command = 'nohup ' . $app; // détache l'appli du script PHP
    if($path !== '')
    {
	    $command .= ' ' . $path;
    }
    $command .= ' > /dev/null 2>&1 &';
    // stdout > /dev/null et & permettent de rendre la main à PHP
    // stderr > stdout pour cacher un message inutile
    return $command;
}

// NOTE 1: en PHP les objets sont passés aux fonctions par référence par défaut, toutefois ce n'est pas entièrement vrai
// NOTE 2: PHP n'a pas de pointeur mais des références, une référence est un alias qui ne contient pas l'objet lui-même
// NOTE 3: la variable créée lors d'un "new" est elle-même une référence contenant un identifiant (= le pointeur?)
// NOTE 4: l'objet est détruit lorsque la dernière référence est supprimée

function rechercheClient(string $input, Clients $Client): array
{
    $input_array = explode(' ', $input); // si plusieurs mot, on les recherche tous l'un après l'autre
    
    $result = $Client->findByKeywords($input_array, 'prenom_nom'); // on obtient un tableau à deux dimensions avec les entrées trouvées
    return($result);
}

function makeFolder(string $path)
{
    if(!file_exists($path))
    {
        mkdir($path);
        chmod($path, 0755);
    }
}

function makeLatexAndPdfDocument($file_name, $template, Clients $Client, Prestations $Presta = null, $Details = null) // $Details peut être de type DevisFactures ou Locations
{
    //~ $data = ; // tableau associatif avec des données des différents objets
    $latex = getLatexFromTemplate($template, $data);
    file_put_contents($latex_path . $file_name, $latex); // injection des variables & écriture du fichier
    latexToPdf($latex_path, $file_name, $pdf_path);
}

function getLatexFromTemplate(string $template, $data)
{
    // variables à injecter
    $nom_client = "M. Duchmol";
    
    // on obtient la variable $latex avec ob_get_clean()
    include('latex_templates/' . $template . '.php');
    
    // on retourne le buffer
    // normallement le code PHP inséré avec include est nettoyé en quittant la fonction
    return($latex);
}

// compilation à partir d'un fichier .tex
function latexToPdf(string $latexPath, string $fileName, string $pdfPath)
{
    $outputDir = '';
    if($pdfPath !== '')
    {
        $outputDir = '-output-directory=' . $pdfPath . ' ';
    }
    
    // compilation
    exec('pdflatex ' . $outputDir . $latexPath . $fileName);

    // nettoyage
    $basename = basename($fileName, '.tex');
    unlink($pdfPath . $basename . '.aux');
    unlink($pdfPath . $basename . '.log');
}