-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.php
129 lines (123 loc) · 3.42 KB
/
todo.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
<?php
$items = array();
//for $file use 'list.txt'
function import($File){
$filename = $File;
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
$file_array = explode("\n", $contents);
return $file_array;
fclose($handle);
}
function save_file($filename, $data_to_save)
{
$input = 'Y';
if (file_exists($filename))
{
echo "This will overwrite the file. Are you sure? Y or N? ";
$input = get_input(TRUE);
if ($input == 'Y')
{
$handle = fopen($filename, 'w');
$contents = implode("\n", $data_to_save);
fwrite($handle, $contents);
fclose($handle);
}
}
else
{
echo "No changes were made.\n";
}
}
//////////
function list_items($list)
{
$result = '';
foreach ($list as $key => $values) {
$result .= "[" . ($key + 1) . "] {$values}\n";
}
return $result;
}
////////////
function get_input($upper = false)
{
$result = trim(fgets(STDIN));
return $upper ? strtoupper($result) : $result; // if ? true : false (layout for if then statement)
}
///////////
function sort_menu($items)
{
echo "How would you like to sort?\n";
echo "(A)-Z, (Z)-A, (O)rder entered, (R)everse order entered : \n";
$input = get_input(true);
switch ($input){
case 'A':
asort($items, SORT_NATURAL | SORT_FLAG_CASE);
break;
case 'Z';
arsort($items, SORT_NATURAL | SORT_FLAG_CASE);
break;
case 'O';
ksort($items, SORT_NATURAL | SORT_FLAG_CASE);
break;
case 'R';
krsort($items, SORT_NATURAL | SORT_FLAG_CASE);
break;
}
return $items;
}
///////////
// When sort menu is opened, show the following options
// "(A)-Z{sort($items)}, (Z)-A{rsort($items), (O)rder entered {else}, (R)everse order entered {krsort}".
do {
echo list_items($items);
echo '(N)ew item, (R)emove item, (S)ort, (O)pen File, s(A)ve, (Q)uit : ';
$input = get_input(true);
if ($input == 'N') {
//Would you like your item to be at the (B)eginning or (E)nd of your list?
echo 'Enter item: ';
$item = get_input();
echo 'Would you like your item to be at the (B)eginning or (E)nd of your list? ';
$b_or_e = get_input(true);
if ($b_or_e == 'B')
{
array_unshift($items, $item);
} elseif ($b_or_e == 'E')
{
array_push($items, $item);
}
}
elseif ($input == 'R') {
echo 'Enter item number to remove: ';
$key = get_input();
--$key;
unset($items[$key]);
$items = array_values($items);
}
elseif ($input == 'F') {
echo 'Power User Function.\n';
array_shift($items);
}
elseif ($input == 'L') {
echo "Power User function.\n";
array_pop($items);
}
elseif ($input == 'S') {
$items = sort_menu($items);
}
elseif ($input == 'O') {
echo "Enter the name of your file: ";
$File = get_input();
$extfile_array = import($File);
$items = array_merge($items, $extfile_array);
}
elseif ($input == 'A') {
echo "Enter file name: ";
$filename = get_input(true);
save_file($filename, $items);
echo "*** File was saved ***";
}
} while ($input != 'Q');
echo "Goodbye!\n";
exit(0);
?>