summaryrefslogtreecommitdiff
path: root/src/functions.php
blob: aaacb36e024b659f7607e37a7b05a22d497f4661 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
// src/functions.php
//
// utilisées par main.php et les fonctions de la boucle principale


// 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;
}

function makeObjectCLient()
{
    // fenêtres
    $QuestionNouveauClient = new ZenityQuestion(ZenitySetup::$question_nouveau_client_text);
    $RechercheClient = new zenityEntry(ZenitySetup::$recherche_client_text);
    $ResultatsRechercheClient = new ZenityList(ZenitySetup::$resultats_recherche_client_text, []);
    $NouveauClient = new ZenityForms(ZenitySetup::$nouveau_client_text, ZenitySetup::$nouveau_client_entrees);
    
    $Client = new Clients;
    
    // est ce que le client est déjà dans la base?
    $client_inconnu = true;
    if(exec($QuestionNouveauClient->get()) == '0') // $? = 0 signifie oui, double égal == pour le transtypage
    {
        echo "choix: recherche d'une client\n";
        $input = exec($RechercheClient->get());
        if($input == '')
        {
            echo "debug: recherche annulée ou saisie vide\n";
            return 0;
        }
        
        echo "debug: recherche effectuée\n";
        $ResultatsRechercheClient->setListRows(rechercheClient($input, $Client), $Client->getTable()); // recherche silencieuse
        
        // sélection parmi les résultats
        $input = exec($ResultatsRechercheClient->get()); // renvoie l'ID de la table 'clients'
        $ResultatsRechercheClient->cleanCommand();
        
        if($input == '')
        {
            echo "debug: client pas trouvé ou pas sélectionné\n";
            return 0;
        }
        
        echo "debug: client sélectionné\n";
        $Client->hydrate($Client->findById($input));
        $client_inconnu = false;
    }
    else
    {
        echo "choix: nouveau client\n";
    }
    
    // on n'a pas cherché OU on n'a pas trouvé
    if($client_inconnu)
    {
        $input = exec($NouveauClient->get());
        if($input == '')
        {
            echo "debug: annulation lors de l'enregistrement d'un nouveau client\n";
            return 0;
        }
        if(!$Client->hydrateFromForm($input))
        {
            // messages d'erreur dans hydrateFromForm()
            return 0;
        }
        unset($input);
        $Client->create();
        $Client->setID(); // sans paramètre, exécute un $this->db->lastInsertId()
    }
    
    return $Client;
}

// 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 makeLatexAndPdfDocuments(Clients $Client = null, Prestations $Presta = null, $PrestaDetails = null)
//~ {
    //~ $latex = '';
    //~ $year = '';
    //~ $data = [];
    //~ $latex_path = Config::$latex_path;
    //~ $pdf_path = Config::$pdf_path;
    
    //~ // verso d'une enveloppe
    //~ $latex = makeLatex('enveloppe_verso'); // pas de données transmises, elles sont dans la classe Config
    //~ $file_name = 'enveloppe_verso.tex';
    //~ makeFile($latex_path, $file_name, $latex);
    
    //~ latexToPdf($latex_path, $file_name, $pdf_path);
    
    
    //~ if($Client !== null)
    //~ {
	//~ $data = $Client->getAll();
	
	//~ // recto d'une enveloppe
	//~ $latex_recto_path = $latex_path . 'enveloppes_recto/';
	//~ $pdf_recto_path = $pdf_path . 'enveloppes_recto/';
	//~ $data['code_postal_espaces'] = implode(' \ ', str_split($data['code_postal'])); // code postal avec 2 espaces entre chaque chiffre: 2 \ 9 \ 0 \ 0 \ 0
	
	//~ $latex = makeLatex('enveloppe_recto', $data); // injection des variables
	//~ $file_name = $Client->getCodeClient() . '.tex';
	//~ makeFolder($latex_recto_path);
	//~ makeFile($latex_recto_path, $file_name, $latex);
	
	//~ makeFolder($pdf_recto_path);
	//~ latexToPdf($latex_recto_path, $file_name, $pdf_recto_path);
	
	//~ // facture, devis, location
	//~ if($Presta !== null && $PrestaDetails !== null)
	//~ {
	    //~ $type = $Presta->getTypePresta();
	    //~ $file_name = $type . '.tex';
	    //~ if($type === 'facture' || $type === 'devis' || $type === 'location')
	    //~ {
		//~ $data = array_merge($data, $Presta->getAll());
		//~ $Date = new Dates($Presta->getDate()); // entrée = timestamp (doit être un "int"!!)
		//~ $year = $Date->getYear();
		//~ $latex_year_path = $latex_path . $year . '/'; // un sous-dossier par année
		//~ $pdf_year_path = $pdf_path . $year . '/';
		//~ $data = array_merge($data, $PrestaDetails->getAll());
		
		//~ $latex = makeLatex($type, $data, $Date); // injection des variables
		//~ $file_name = $Presta->getCodePresta() . '.tex';
		//~ makeFolder($latex_year_path);
		//~ makeFile($latex_year_path, $file_name, $latex);
		
		//~ makeFolder($pdf_year_path);
		//~ latexToPdf($latex_year_path, $file_name, $pdf_year_path);
	    //~ }
	    //~ elseif($type === 'cesu' || $type === 'non_vendue')
	    //~ {} // pas de document
	    //~ else
	    //~ {
		//~ echo "debug: erreur génération latex, type de prestation \n";
		//~ return 0;
	    //~ }
	//~ }
    //~ }
//~ }

//~ function makeLatex(string $type, array $data = [], Dates $Date = null)
//~ {
    //~ $date = '';
    //~ if($Date != null)
    //~ {
	//~ $date = $Date->getDate();
	//~ $date = preg_replace('#\D#', '/', $date); // date avec des slashs / parce que j'aime bien
    //~ }
    
    //~ // on obtient la variable $latex avec ob_get_clean()
    //~ // le include() ici, c'est du génie ou c'est moche ou les deux?
    //~ // un nouveau fichier php est inséré à chaque itération
    //~ include('latex_templates/' . $type . '.php');
    
    //~ // on retourne le buffer
    //~ // normallement le code PHP inséré avec include est nettoyé en quittant la fonction
    //~ return($latex);
//~ }