-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_gallery_monitors.php
280 lines (237 loc) · 9.92 KB
/
setup_gallery_monitors.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
/**
* Project: Gallery Displays
* Purpose: File Overview
* select * from monitors
* do directory scan of images directory
* sync db with directory scan of images folder: add or remove image directories in database to synchronize both
* set a default image_filename for each monitor
* enable the setup person (ie security guard) to identify each monitor from a list of available options (HTML links on page)
* selecting a monitor will set that browser to pull images intended only for that monitor
* Author: David Keiser-Clark, Williams College
*/
// ***************************
// required files
// ***************************
require_once(dirname(__FILE__) . '/app_setup.php');
# TODO - Consider putting this directory/database synchronization into another file, to enable it to periodically run silently
// fetch galleries
$galleries = Gallery::getAllFromDb([], $DB);
// read directory contents to create dynamic list of exhibition and monitor options
$array_directories = [];
if ($handle = opendir(dirname(__FILE__) . APP_IMAGES_DIR)) {
//echo "Directory handle: $handle\n<br/><br/>";
while (FALSE !== ($directory_name = readdir($handle))) {
if ($directory_name != "." && $directory_name != "..") {
// store value in array
array_push($array_directories, $directory_name);
}
}
closedir($handle);
}
// fetch monitors
$all_monitors = Monitor::getAllFromDb([], $DB);
if (DEBUG_APP) {
util_prePrintR($galleries);
util_prePrintR($array_directories);
util_prePrintR($all_monitors);
}
// do new directories exist?
foreach ($array_directories as $directory) {
$flag_directory = FALSE;
// check if directory exists in db
foreach ($all_monitors as $monitor) {
if ($monitor->monitor_name == $directory) {
// found directory match in db
$flag_directory = TRUE;
}
}
// directory does not exist in db
if (!$flag_directory) {
// check if this record was deleted (flag_delete = TRUE)
$deactivated_monitor = Monitor::getOneFromDb(['monitor_name' => $directory, 'flag_delete' => TRUE], $DB);
// update or create record
if ($deactivated_monitor->matchesDb) {
// update record
$deactivated_monitor->flag_delete = 0;
$deactivated_monitor->image_filename = set_default_image_for_monitor($directory);
$deactivated_monitor->updated_at = util_currentDateTimeString_asMySQL();
$deactivated_monitor->updateDb();
if (!$deactivated_monitor->matchesDb) {
// update failed
$evt_action = "error_db_update";
$evt_note = "database error: could not reactivate pre-existing monitor record";
util_createEventLog($monitor->gallery_id, FALSE, $evt_action, $evt_note, print_r(json_encode($directory), TRUE), $DB);
display_error($evt_note);
exit;
}
// log event
$evt_action = "reactivate_monitor";
$evt_note = "Successfully reactivated monitor record " . $directory;
util_createEventLog($deactivated_monitor->gallery_id, TRUE, $evt_action, $evt_note, print_r(json_encode($_REQUEST), TRUE), $DB);
if (DEBUG_APP) {
echo $evt_note;
}
}
else {
// create record
// check if we need to create a gallery (parent group) for this new monitor
$directory_prefix = strchr($directory, "_", -1); // fetch prefix of directory (example: "demo" is prefix of "demo_monitor_1")
$new_gallery = Gallery::getOneFromDb(['gallery_name' => $directory_prefix], $DB);
if (!$new_gallery->matchesDb) {
// check if this record was deleted (flag_delete = TRUE) because the gallery is not a live record
$deactivated_gallery = Gallery::getOneFromDb(['gallery_name' => $directory_prefix, 'flag_delete' => TRUE], $DB);
// update or create record
if ($deactivated_gallery->matchesDb) {
// update record
$deactivated_gallery->flag_delete = 0;
$deactivated_gallery->updated_at = util_currentDateTimeString_asMySQL();
$deactivated_gallery->updateDb();
if (!$deactivated_gallery->matchesDb) {
// update failed
$evt_action = "error_db_update";
$evt_note = "database error: could not reactivate pre-existing gallery record";
util_createEventLog($monitor->gallery_id, FALSE, $evt_action, $evt_note, print_r(json_encode($directory), TRUE), $DB);
display_error($evt_note);
exit;
}
// log event
$evt_action = "reactivate_gallery";
$evt_note = "Successfully reactivated gallery record " . $directory_prefix;
util_createEventLog($deactivated_gallery->gallery_id, TRUE, $evt_action, $evt_note, print_r(json_encode($_REQUEST), TRUE), $DB);
if (DEBUG_APP) {
echo $evt_note;
}
}
else {
// create record
$new_gallery = Gallery::createNewGallery($DB);
$new_gallery->gallery_name = $directory_prefix;
$new_gallery->updateDb();
if (!$new_gallery->matchesDb) {
// update failed
$evt_action = "error_db_create";
$evt_note = "database error: could not create gallery record";
util_createEventLog($new_gallery->gallery_id, FALSE, $evt_action, $evt_note, print_r(json_encode($directory), TRUE), $DB);
display_error($evt_note);
exit;
}
// log event
$evt_action = "create_gallery";
$evt_note = "Successfully created gallery record: " . $directory_prefix;
util_createEventLog($new_gallery->gallery_id, TRUE, $evt_action, $evt_note, print_r(json_encode($_REQUEST), TRUE), $DB);
if (DEBUG_APP) {
echo $evt_note;
}
}
}
// create record
$new_monitor = Monitor::createNewMonitor($DB);
$new_monitor->gallery_id = $new_gallery->gallery_id;
$new_monitor->monitor_name = $directory;
$new_monitor->image_filename = set_default_image_for_monitor($directory);
$new_monitor->updateDb();
if (!$new_monitor->matchesDb) {
// update failed
$evt_action = "error_db_create";
$evt_note = "database error: could not create monitor record";
util_createEventLog($new_monitor->gallery_id, FALSE, $evt_action, $evt_note, print_r(json_encode($directory), TRUE), $DB);
display_error($evt_note);
exit;
}
// log event
$evt_action = "create_monitor";
$evt_note = "Successfully created monitor record: " . $directory;
util_createEventLog($new_gallery->gallery_id, TRUE, $evt_action, $evt_note, print_r(json_encode($_REQUEST), TRUE), $DB);
if (DEBUG_APP) {
echo $evt_note;
}
}
}
}
// NOW, DO THE REVERSE PROCESS
# fetch galleries
$galleries = Gallery::getAllFromDb([], $DB);
# fetch monitors
$all_monitors = Monitor::getAllFromDb([], $DB);
// have any directories been removed?
foreach ($all_monitors as $monitor) {
$flag_db_monitor_name = FALSE;
// check if db_monitor_name exists in directory structure
foreach ($array_directories as $directory) {
if ($monitor->monitor_name == $directory) {
// found monitor_name match with actual directory
$flag_db_monitor_name = TRUE;
}
}
// db_monitor_name does not exist in directory structure
if (!$flag_db_monitor_name) {
// soft-delete this record as it no longer matches, or represents, the actual directory structure
// update record
$monitor->flag_delete = 1;
$monitor->updated_at = util_currentDateTimeString_asMySQL();
$monitor->updateDb();
if (!$monitor->matchesDb) {
// update failed
$evt_action = "error_delete_monitor";
$evt_note = "database error: could not delete monitor record";
util_createEventLog($monitor->gallery_id, FALSE, $evt_action, $evt_note, print_r(json_encode($monitor->monitor_name), TRUE), $DB);
display_error($evt_note);
exit;
}
// log event
$evt_action = "delete_monitor";
$evt_note = "Successfully deleted monitor record: " . $monitor->monitor_name;
util_createEventLog($monitor->gallery_id, TRUE, $evt_action, $evt_note, print_r(json_encode($_REQUEST), TRUE), $DB);
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title><?php echo "Setup - " . LANG_APP_NAME; ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="<?php echo LANG_APP_NAME; ?>">
<meta name="author" content="<?php echo LANG_AUTHOR_NAME; ?>">
<meta charset="utf-8">
<!-- CSS: Framework -->
<!-- CSS: Plugins -->
<link rel="stylesheet" href="<?php echo APP_ROOT_PATH; ?>/css/custom.css" type="text/css" media="all">
<!-- jQuery: Framework -->
<script src="<?php echo PATH_JQUERY_JS; ?>"></script>
<!-- jQuery: Plugins -->
<!-- local JS -->
</head>
<body class="setup_theme">
<?php
/* echo "<div class=\"setup_step\">";
echo "<span class=\"text_large\">1. Select Mode: </span>";
echo "<p class=\"indent\"><a href=\"" . APP_FOLDER . "/index.php?id=" . $monitor->monitor_id . "\" title=\"" . $monitor->monitor_name . "\"/>Gallery</a> (user interactive)";
echo "<p class=\"indent\"><a href=\"" . APP_FOLDER . "/index.php?id=" . $monitor->monitor_id . "\" title=\"" . $monitor->monitor_name . "\"/>Slideshow</a> (random, NOT user interactive)";
echo "<br /><br />";
echo "</div>";*/
echo "<div class=\"setup_step\">";
echo "<span class=\"text_large\">Identify this monitor by clicking the corresponding link below:</span>";
echo "</div>";
// fetch galleries
$galleries = Gallery::getAllFromDb([], $DB);
foreach ($galleries as $gallery) {
// load monitors for this gallery
$gallery->loadMonitors();
// only display a gallery group if it contains active monitors
if (count($gallery->monitors) > 0){
// display gallery group
echo "<div class=\"monitor_row\">";
echo "<h3>Gallery: <span class=\"uppercase\">"" . $gallery->gallery_name . ""</span></h3>";
// display monitors within each gallery
foreach ($gallery->monitors as $monitor) {
echo "<div class=\"monitor_item\">";
echo "<p class=\"monitor_text\"><a href=\"" . APP_FOLDER . "/index.php?id=" . $monitor->monitor_id . "\" title=\"" . $monitor->monitor_name . "\"/>" . $monitor->monitor_name . "</a></p>";
echo "</div>";
}
echo '</div>';
}
}
?>
</body>
</html>