-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapicli.inc.php
216 lines (176 loc) · 4.53 KB
/
apicli.inc.php
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
class APIcli {
private $command;
protected $db;
function __construct($db)
{
$this->db = $db;
}
function get_command()
{
$this->show_get_command_text();
$command = $this->read_input();
switch ($command)
{
case "1":
return "show_table";
break;
case "2":
return "new_entry";
break;
case "3":
return "search";
break;
case "4":
return "show_entry";
break;
case "5":
return "edit_entry";
break;
case "9":
return "delete_entry";
break;
case "0":
return "exit";
break;
default:
return $command;
}
}
function read_input()
{
return trim(fgets(fopen("php://stdin","r")));
}
function show_get_command_text ()
{
echo "[1 Table][2 New][3 Search][4 Show][5 Edit][9 Delete][0 Exit]: ";
}
function print_output($text)
{
echo $text;
}
function print_error($text)
{
$this->print_output("ERROR: ".$text);
}
function search()
{
$this->print_output("Enter search string: ");
$needle = $this->read_input();
if ($needle) {
$card = new Card($this->db);
$count = 0;
foreach ($this->db->search_card($needle) as $cardIds)
{
$this->card_show($cardIds[rowid]);
$count++;
}
$this->print_output("Results found: ".$count."\n");
}
else
{
$this->show_table();
}
}
function show_table()
{
$this->print_output("\n");
$card = new Card($this->db);
foreach ($this->db->get_card_ids() as $cardIds)
{
$this->card_show($cardIds[rowid]);
}
}
function show_detail()
{
$this->print_output("Please enter ID: ");
$id = (integer) $this->read_input();
$card = new Card($this->db);
$card->load_by_id($id);
$this->print_output("\n");
$this->print_output("Firstname: ".$card->firstname."\n");
$this->print_output("Surname: ".$card->surname."\n");
$this->print_output("Firm: ".$card->firm."\n");
$this->print_output("Mobile Phone: ".$card->mobilep."\n");
$this->print_output("Work Phone: ".$card->workp."\n");
$this->print_output("Private Phone: ".$card->privatep."\n");
$this->print_output("Email: ".$card->email."\n");
$this->print_output("\n");
$this->print_output("\n");
}
function new_entry()
{
$card = new Card($this->db);
$this->print_output("Firstname: ");
$card->firstname = $this->read_input();
$this->print_output("Surname: ");
$card->surname = $this->read_input();
$this->print_output("Firm: ");
$card->firm = $this->read_input();
$this->print_output("Mobile Phone: ");
$card->mobilep = $this->read_input();
$this->print_output("Work Phone: ");
$card->workp = $this->read_input();
$this->print_output("Private Phone: ");
$card->privatep = $this->read_input();
$this->print_output("Email: ");
$card->email = $this->read_input();
$card->create();
$this->print_output("Card saved.\n");
}
function delete_entry ()
{
$this->print_output("Please enter ID: ");
$id = $this->read_input();
$card = new Card($this->db);
$card->load_by_id($id);
$card->delete();
unset ($card);
$this->print_output("Entry removed.\n");
}
function card_show($id)
{
$card = new Card($this->db);
$card->load_by_id($id);
echo $card->id.":\t".$card->surname." ".$card->firstname;
if ($card->firm)
echo ", ".$card->firm;
if ($card->email)
echo ", ".$card->email;
if (($card->mobilep) OR ($card->workp) OR ($card->privatep))
echo "\n\t";
if ($card->mobilep)
echo "M:".$card->mobilep." ";
if ($card->workp)
echo "W:".$card->workp." ";
if ($card->privatep)
echo "P:".$card->privatep." ";
echo "\n";
}
function edit_entry ()
{
$this->print_output("Please enter ID: ");
$id = (integer) $this->read_input();
$card = new Card($this->db);
$card->load_by_id($id);
$this->draw_line($id, "Firstname", "firstname", $card->firstname);
$this->draw_line($id, "Surname", "surname", $card->surname);
$this->draw_line($id, "Firm", "firm", $card->firm);
$this->draw_line($id, "Mobile Phone", "mobilep", $card->mobilep);
$this->draw_line($id, "Work Phone", "workp", $card->workp);
$this->draw_line($id, "Private Phone", "privatep", $card->privatep);
$this->draw_line($id, "Email", "email", $card->email);
$this->print_output("\nNew entry:\n");
$this->card_show($id);
}
function draw_line($id, $text, $fieldname, $content)
{
$this->print_output($text." (".$content."): ");
$ret = $this->read_input();
if ($ret == "-")
$this->db->card_update($id , $fieldname, "");
elseif (! empty($ret))
$this->db->card_update($id , $fieldname, $ret);
}
}
?>