-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
169 lines (127 loc) · 5.39 KB
/
index.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
<?php
require_once __DIR__ . '/library.php';
define('DEFAULT_GROUP', '__DEFAULT__');
session_start();
$directoryItems = array_slice(scandir(FILES_DIRECTORY), 2);
$directories = [];
foreach ($directoryItems as $item) {
if (!is_dir(getDirectoryPath($item))) {
continue;
}
$directories[] = $item;
}
if ($_POST) {
$_SESSION['directory'] = $_POST['directory'];
if (!$_SESSION['directory']) {
halt('Select directory from the dropdown.');
}
}
if (isset($_POST['generate'])) {
$customGroups = getCustomGroups($_SESSION['directory']);
$filesList = getDirectoryFilesList($_SESSION['directory']);
if (!$filesList) {
halt('Directory does not exist or contains no .mp3 files in it.');
}
$filesData = getFilesData($filesList, $customGroups);
$songsQuantity = count($filesData);
$groupsData = getGroupsData($filesData);
$groupsBySize = getGroupsBySize($groupsData);
$batchSize = array_key_first($groupsBySize);
$batchCombinations = getBatchCombinations($groupsBySize, $batchSize, $songsQuantity);
$batchLists = getBatchLists($batchCombinations, $groupsData, $batchSize);
$playlistMap = getPlaylistMap($batchLists, $filesData, $batchSize, $songsQuantity);
$_SESSION['playlistMap'] = $playlistMap;
$_SESSION['playlistMapDirectory'] = $_SESSION['directory'];
halt('Playlist map is successfully generated.');
}
if (isset($_POST['stripIndexes'])) {
$customGroups = getCustomGroups($_SESSION['directory']);
$filesList = getDirectoryFilesList($_SESSION['directory']);
if (!$filesList) {
halt('Directory does not exist or contains no .mp3 files in it.');
}
$filesData = getFilesData($filesList, $customGroups);
$directoryPath = getDirectoryPath($_SESSION['directory']);
foreach ($filesData as $fileName => $fileData) {
$newFileName = $fileData['show'] !== DEFAULT_GROUP ? "{$fileData['show']} - {$fileData['song']}.mp3" : "{$fileData['song']}.mp3";
if ($fileName === $newFileName) {
continue;
}
rename("$directoryPath/$fileName", "$directoryPath/$newFileName");
}
halt('File indexes are successfully stripped.');
}
if (isset($_POST['empty'])) {
$filesList = getDirectoryFilesList($_SESSION['directory']);
if (!$filesList) {
halt('Directory does not exist or contains no .mp3 files in it.');
}
$directoryPath = getDirectoryPath($_SESSION['directory']);
foreach ($filesList as $fileName) {
unlink("$directoryPath/$fileName");
}
halt('Directory is successfully emptied.');
}
if (isset($_POST['apply'])) {
if (!isset($_SESSION['playlistMap']) || !isset($_SESSION['playlistMapDirectory'])) {
halt('Playlist map is not generated or corrupted.');
}
$filesNames = array_keys($_SESSION['playlistMap']);
$directoryPath = getDirectoryPath($_SESSION['playlistMapDirectory']);
foreach ($filesNames as $fileName) {
if (is_file("$directoryPath/$fileName")) {
continue;
}
halt('Playlist map is outdated and needs to be regenerated.');
}
foreach ($_SESSION['playlistMap'] as $fileName => $newFileName) {
if ($fileName === $newFileName) {
continue;
}
rename("$directoryPath/$fileName", "$directoryPath/$newFileName");
}
halt('Files are successfully rearranged according to the generated playlist.');
}
if (isset($_POST['delete'])) {
unset($_SESSION['playlistMap']);
unset($_SESSION['playlistMapDirectory']);
halt('Last generated playlist is successfully deleted.');
}
?>
<html>
<head>
<title>Playlist Generator</title>
</head>
<body>
<form method="post">
<?php if (isset($_SESSION['message'])): ?>
<div><?= $_SESSION['message'] ?></div>
<br/>
<?php unset($_SESSION['message']) ?>
<?php endif ?>
<select name="directory">
<option value="">---Select directory---</option>
<?php foreach ($directories as $directory): ?>
<option value="<?= $directory ?>"<?= isset($_SESSION['directory']) && $_SESSION['directory'] === $directory ? ' selected' : '' ?>><?= $directory ?></option>
<?php endforeach ?>
</select>
<input type="submit" name="generate" value="Generate"/>
<input type="submit" name="stripIndexes" value="Strip Indexes"/>
<input type="submit" name="empty" value="Empty" onclick="return confirm('Are you sure you want to empty the directory?')"/>
<?php if (isset($_SESSION['playlistMap']) && isset($_SESSION['playlistMapDirectory'])): ?>
<div style="font-family: Consolas; font-size: 13px">
<h3>Last Generated Playlist</h3>
<span><b>Directory:</b> <?= $_SESSION['playlistMapDirectory'] ?></span>
<br/>
<input type="submit" name="apply" value="Apply"/>
<input type="submit" name="delete" value="Delete" onclick="return confirm('Are you sure you want to delete last generated playlist?')"/>
<br/>
<?php foreach ($_SESSION['playlistMap'] as $fileName): ?>
<br/>
<?= $fileName ?>
<?php endforeach ?>
</div>
<?php endif ?>
</form>
</body>
</html>