-
Notifications
You must be signed in to change notification settings - Fork 0
/
unc_type_day.inc.php
321 lines (282 loc) · 10.7 KB
/
unc_type_day.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
/**
* This file handles all day-type shortcodes (the default type) variables and
* output.
*/
/**
* Analyse the shortcode vars relevant for the day type.
* @global type $UNC_GALLERY
* @param type $a
* @return boolean
*/
function unc_day_var_init($a) {
global $UNC_GALLERY;
// we convert the start time and end time to unix timestamp for better
// comparison
$UNC_GALLERY['display']['range'] = array('start_time' => false, 'end_time' => false);
foreach ($UNC_GALLERY['display']['range'] as $key => $value) {
if ($a[$key]) {
//re-name the variable
$UNC_GALLERY['display']['date_range'][$key] = trim($a[$key]);
// convert to UNIX timestamp
$dtime = DateTime::createFromFormat("Y-m-d G:i:s", trim($a[$key]));
if (!$dtime) { // time format was invalid
return false;
}
$UNC_GALLERY['display']['range'][$key] = $dtime->getTimestamp();
// get the date for the same
$var_name = 'date_' . $key;
$$var_name = substr($a[$key], 0, 10);
}
}
if ($a['end_time']) {
$date_end_time = substr(trim($a['end_time']), 0, 10);
}
if ($a['start_time']) {
$date_start_time = substr(trim($a['start_time']), 0, 10);
}
$UNC_GALLERY['display']['date_description'] = false; // false by default, only true if not set explicitly (latest or random date)
if ($a['end_time'] && $a['start_time']) {
$date_arr = unc_day_date_span($date_start_time, $date_end_time);
$UNC_GALLERY['display']['dates'] = $date_arr;
} else if ($a['end_time']) {
$date_str = $date_end_time;
$UNC_GALLERY['display']['dates'] = array($date_str);
} else if ($a['start_time']) {
$date_str = $date_start_time;
$UNC_GALLERY['display']['dates'] = array($date_str);
} else if ($a['date'] && in_array($a['date'], $UNC_GALLERY['keywords']['date'])) { // we have a latest or random date
// get the latest or a random date if required
if ($a['date'] == 'random') {
$date_str = unc_day_date_random();
} else if ($a['date'] == 'latest') {
$date_str = unc_day_date_latest();
}
if (!$date_str) { // we have no images in the database
$UNC_GALLERY['display']['files'] = array();
$UNC_GALLERY['display']['file'] = false;
$UNC_GALLERY['display']['dates'] = array();
} else {
$UNC_GALLERY['display']['date_description'] = true;
$UNC_GALLERY['display']['dates'] = array($date_str);
}
} else if ($a['date'] && strstr($a['date'], ",")) { // we have several dates in the string
$dates = explode(",", $a['date']);
if (count($dates) > 2) {
echo unc_display_errormsg("You can only enter 2 dates!");
return false;
}
// validate both dates
$date_str1 = unc_day_validate_date(trim($dates[0]));
$date_str2 = unc_day_validate_date(trim($dates[1]));
if (!$date_str1 || !$date_str2) {
echo unc_display_errormsg("All dates needs to be in the format '2016-01-31'");
return false;
}
// create a list of dates between the 1st and the 2nd
$date_arr = unc_day_date_span($dates[0], $dates[1]);
$UNC_GALLERY['display']['dates'] = $date_arr;
} else if ($a['date']) {
$date_str = unc_day_validate_date($a['date']);
if (!$date_str) {
echo unc_display_errormsg("All dates needs to be in the format '2016-01-31'");
return false;
}
$UNC_GALLERY['display']['dates'] = array($date_str);
}
// get the actual images
if ($a['file'] && $a['type'] == 'day') {
$UNC_GALLERY['display']['file'] = unc_tools_filename_validate(trim($a['file']));
$UNC_GALLERY['display']['files'] = array();
} else {
$UNC_GALLERY['display']['file'] = false;
$UNC_GALLERY['display']['files'] = unc_day_images_list();
}
$UNC_GALLERY['display']['date_selector'] = false;
if (in_array('calendar', $UNC_GALLERY['display']['options'])) {
$UNC_GALLERY['display']['date_selector'] = 'calendar';
} else if (in_array('datelist', $UNC_GALLERY['display']['options'])) {
$UNC_GALLERY['display']['date_selector'] = 'datelist';
}
if (count($UNC_GALLERY['display']['files']) == 0 && !$UNC_GALLERY['display']['file']) {
if ($UNC_GALLERY['no_image_alert'] == 'error') {
$UNC_GALLERY['errors'][] = unc_display_errormsg("No images found for this date!");
} else if ($UNC_GALLERY['no_image_alert'] == 'not_found') {
$UNC_GALLERY['errors'][] = "No images available.";
}
return false;
}
return true;
}
/**
* Iterate all files in a folder and make a list of all the images with all the info
* for them
*
* @global type $UNC_GALLERY
* @param type $folder
* @return array
*/
function unc_day_images_list($D = false) {
global $UNC_GALLERY, $wpdb;
if (!$D) {
$D = $UNC_GALLERY['display'];
}
$dates = $D['dates'];
if (count($dates) == 0) {
return false;
}
$files = array();
$featured_list = array();
// SQL construction
$sql_filter = '';
// both end_time and start_time are set
if ($D['range']['end_time'] && $D['range']['start_time']) {
$start_time = $D['date_range']['start_time'];
$end_time = $D['date_range']['end_time'];
$date = $D['dates'][0];
if ($D['range']['start_time'] < $D['range']['end_time']) {
$sql_filter = " (file_time >= '$start_time' AND file_time <= '$end_time')";
} else if ($D['range']['start_time'] > $D['range']['end_time']){
$sql_filter = " ((file_time >= '$date 00:00:00' AND file_time <= '$end_time') OR (file_time >= '$start_time' AND file_time <= '$date 23:59:59'))";
} else { // both times are the same
$sql_filter = " (file_time = '$start_time')";
}
} else if ($D['range']['end_time']) { // get everything from day start until end time
$end_time = $D['date_range']['end_time'];
$date = $D['dates'][0];
$sql_filter = " (file_time >= '$date 00:00:00' AND file_time <= '$end_time')";
} else if ($D['range']['start_time']) { // get everything from start till day end
$start_time = $D['date_range']['start_time'];
$date = $D['dates'][0];
$sql_filter = " (file_time >= '$start_time' AND file_time <= '$date 23:59:59')";
} else {
$dates = $D['dates'];
$date_sql = implode("','", $dates);
$sql_filter = " (att_value IN('$date_sql'))";
}
// get all images for the selected dates
$img_table_name = $wpdb->prefix . "unc_gallery_img";
$att_table_name = $wpdb->prefix . "unc_gallery_att";
$sql = "SELECT * FROM `$img_table_name`
LEFT JOIN $att_table_name ON $img_table_name.id=$att_table_name.file_id
WHERE ($att_table_name.att_name='date_str') AND $sql_filter
ORDER BY file_time ASC;";
$file_data = $wpdb->get_results($sql, 'ARRAY_A');
if (count($file_data) == 0) {
unc_tools_debug_trace('unc_day_images_list no images for date found!', $sql);
}
// now we get the detailed info per image
foreach ($file_data as $F) {
$I = unc_image_info_read($F['file_path']);
if (in_array($F['file_name'], $D['featured_image'])) {
$I['featured'] = true;
$featured_list[] = $I;
} else {
$I['featured'] = false;
$files[] = $I;
}
}
// TODO: Move this to the SQL string
// random featured file
if (in_array('random', $D['featured_image'])) {
$new_featured_key = array_rand($files);
$new_featured_arr = $files[$new_featured_key];
$new_featured_arr['featured'] = true;
$featured_list[] = $new_featured_arr;
unset($files[$new_featured_key]);
}
// TODO: Move this to the SQL string
if (in_array('latest', $D['featured_image'])) {
reset($files);
$first_key = key($files);
$new_featured_arr = $files[$first_key];
$new_featured_arr['featured'] = true;
$featured_list[] = $new_featured_arr;
unset($files[$first_key]);
}
foreach ($featured_list as $feat) {
array_unshift($files, $feat);
}
if (count($files) == 0) {
}
return $files;
}
/**
* Validate Datestr
*
* @param type $date_str
* @return boolean
*/
function unc_day_validate_date($date_str) {
$pattern = "/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/";
if (preg_match($pattern, $date_str)) {
return $date_str;
}else{
return false;
}
}
/**
* create a list of all dates between 2 dates
*
* @param string $date1 (date_str format)
* @param string $date2
* @return array
*/
function unc_day_date_span($date1, $date2) {
global $UNC_GALLERY;
// we try to sort the dates
if ($date1 < $date2) {
$early = $date1;
$later = $date2;
} else if ($date1 == $date2) {
return array($date1);
} else {
$early = $date2;
$later = $date1;
}
if (strlen($later) == 10) {
$later .= " 23:59:59";
}
$dates_arr = new DatePeriod(
new DateTime($early),
new DateInterval('P1D'),
new DateTime($later)
);
$date_str_arr = array();
foreach($dates_arr as $date_obj) {
$date_str_arr[] = $date_obj->format("Y-m-d");
}
return $date_str_arr;
}
/**
* returns the latest date
*
* @global type $UNC_GALLERY
* @return type
*/
function unc_day_date_latest() {
global $wpdb;
$img_table_name = $wpdb->prefix . "unc_gallery_img";
$sql ="SELECT SUBSTR(file_time, 1, 10) as date_str FROM `$img_table_name` ORDER BY file_time DESC LIMIT 1;";
$file_data = $wpdb->get_results($sql, 'ARRAY_A');
if (count($file_data) == 0) {
unc_tools_debug_trace('unc_day_date_latest no images found!', $sql);
return false;
}
$date_str = $file_data[0]['date_str'];
return $date_str;
}
/**
* returns a random date
*
* @global type $UNC_GALLERY
* @return type
*/
function unc_day_date_random() {
global $UNC_GALLERY, $wpdb;
$img_table_name = $wpdb->prefix . "unc_gallery_img";
$sql = "SELECT SUBSTR(file_time, 1, 10) as date_str FROM `$img_table_name` GROUP BY SUBSTR(file_time, 1, 10) ORDER BY RAND() LIMIT 1";
$file_data = $wpdb->get_results($sql, 'ARRAY_A');
$date_str = $file_data[0]['date_str'];
return $date_str;
}