summaryrefslogtreecommitdiff
path: root/src/view/Zenity.php
blob: 73c8c3edc032e860166070afac7c2f6b4577ff20 (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
<?php
// src/view/Zenity.php
//
// générer les commandes qui ouvrent les fenêtres zenity, un objet = une commande
// s'utilisent comme ceci: exec($Objet->get());

abstract class ZenityCmd
{
	protected $command = 'zenity';
	protected $command_type = '';
	protected $rows = [];
	protected $text = '';
	protected $width = 300;
	protected $height = 200; // recalculée en fonction du contenu, vaut au minimum 150
	
	protected function __construct(string $text, array $rows = []) // $rows est optionnel
	{
		$this->text = $text;
		$this->rows= $rows;
		$this->command .= $this->command_type;
        $this->command .= ' --title="' . Config::$business_name . '"';
		$this->command .= ' --text="' . $this->text . '"';
	}
	
	public function get()
	{
		return($this->command);
	}
}


class ZenityList extends ZenityCmd
{
	private $columns = 1; // tableau simple ou multidimensionnel?
	
	public function __construct(string $text, array $rows = [])
	{
		$this->command_type = ' --list';
		parent::__construct($text, $rows);
		$this->columns = 1;
		$this->command .= ' --hide-header'; // ligne inutile, il y a déjà le --text
		if($this->rows !== [])
		{
			$this->command .= ' --width=' . $this->width;
			$this->height = 80 + count($this->rows) * 25;
			$this->command .= ' --height=' . $this->height;
			$this->fillZenityList();
		}
	}
	
	// cas ou $this->rows est renseignée après
	// la valeur de $columns n'est plus nécessairement celle apr défaut
	public function setListRows(array $rows, string $table)
	{
		$this->rows = $rows;
		$this->columns = count(StructTablesDB::$structureOfTables[$table]);
		$this->width = 800;
		$this->command .= ' --width=' . $this->width;
		$this->height = 80 + count($this->rows) * 25;
		$this->command .= ' --height=' . $this->height;
		$this->fillZenityList();
	}
	
	// noter que la syntaxe de zentity --list est déroutante!
	// le remplissage est horizontal et le nombre de colonne dépend du nombre d'occurence de --column=""
	public function fillZenityList()
	{
		$output = '';
		if($this->columns === 1)
		{
			$output .= ' --column=""';
			// remplissage vertical
			foreach($this->rows as $one_row)
			{
				$output .= ' "' . $one_row . '"'; // forme: ' "choix 1" "choix 2"'
			}
		}
		elseif($this->columns >= 2) // marche quelque soit le nombre de colonnes
		{
			for($i = 0; $i < $this->columns; $i++)
			{
				$output .= ' --column=""';
			}
			// remplissage horizontal, un sous-tableau = une ligne
			foreach($this->rows as $one_row)
			{
				foreach($one_row as $one_field)
				{
					$output .= ' "' . $one_field . '"';
				}
			}
		}
		$this->command .= $output;
	}
	
	public function cleanCommand()
	{
		$this->command = 'zenity';
		$this->__construct($this->text);
	}
}

class ZenityQuestion extends ZenityCmd
{
	public function __construct(string $text)
	{
		$this->command_type = ' --question';
		parent::__construct($text);
        $this->command .= ' && echo $?';
        // la sortie de "zenity --question" est le statut de sortie "$?"
		// $? vaut 0 pour oui, 1 pour non, à ceci près que pour non zenity ne renvoie rien
	}
}

// note: le formulaire renvoie une chaine avec des pipes | entre les zones de texte (qui peuvent être vides)
// si on clique sur 'Annuler', renvoie une chaine vide
class ZenityForms extends ZenityCmd
{
    public function __construct(string $text, array $rows)
	{
		$this->command_type = ' --forms';
		parent::__construct($text, $rows);
        //$this->height = 80 + count($this->rows) * 25; // à tester, mais devrait produire le rendu attendu
        self::entriesZenityForms($this->rows);
	}
    
    private function entriesZenityForms(array $input)
    {
        $output = '';
		foreach($input as $one_question)
		{
			$output .= ' --add-entry="' . $one_question . '"'; // forme: ' "choix 1" "choix 2"'
		}
		$this->command .= $output;
    }
}

class ZenityCalendar extends ZenityCmd
{
	public function __construct(string $text)
	{
		$this->command_type = ' --calendar';
		parent::__construct($text);
	}
}

class ZenityEntry extends ZenityCmd
{
	public function __construct(string $text)
	{
		$this->command_type = ' --entry';
		parent::__construct($text);
	}
}