-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPlaylists.php
More file actions
1111 lines (987 loc) · 46.4 KB
/
Copy pathPlaylists.php
File metadata and controls
1111 lines (987 loc) · 46.4 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Zookeeper Online
*
* @author Jim Mason <jmason@ibinx.com>
* @copyright Copyright (C) 1997-2024 Jim Mason <jmason@ibinx.com>
* @link https://zookeeper.ibinx.com/
* @license GPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License,
* version 3, along with this program. If not, see
* http://www.gnu.org/licenses/
*
*/
namespace ZK\UI;
use ZK\Controllers\PushServer;
use ZK\Engine\Engine;
use ZK\Engine\IDJ;
use ZK\Engine\ILibrary;
use ZK\Engine\IPlaylist;
use ZK\Engine\IReview;
use ZK\Engine\IUser;
use ZK\Engine\PlaylistEntry;
use ZK\Engine\PlaylistObserver;
use ZK\UI\UICommon as UI;
use JSMin\JSMin;
class Playlists extends MenuItem {
private const NME_PREFIX = "nme-";
//NOTE: update ui_config.php when changing the actions.
private static $subactions = [
[ "a", "", "On Now", "emitHome" ],
[ "a", "recent", 0, "recentSpins" ],
[ "a", "times", 0, "getTimes" ],
[ "u", "editList", "My Playlists", "emitListManager" ],
[ "a", "viewList", "By Date", "emitPlaylistPicker" ],
[ "a", "viewListById", 0, "emitViewPlaylist" ],
[ "a", "viewListDaysByDate", 0, "handlePlaylistDaysByDate" ],
[ "a", "viewListsByDate", 0, "handlePlaylistsByDate" ],
[ "u", "editListGetHint", 0, "listManagerGetHint" ],
[ "u", "editListEditor", 0, "emitEditor" ],
[ "a", "viewDJ", "By DJ", "emitViewDJ" ],
[ "a", "viewTop", "Top Plays", "emitTopPlays" ],
[ "u", "import", "Import", "emitImportList" ],
];
private $action;
private $subaction;
public function getSubactions($action) {
return self::$subactions;
}
public function processLocal($action, $subaction) {
$this->action = $action;
$this->subaction = $subaction;
return $this->dispatchSubaction($action, $subaction);
}
// given a time string H:MM, HH:MM, or HHMM, return normalized to HHMM
// returns empty string if invalid
private function normalizeTime($t) {
// is time already in HHMM format?
if(preg_match('/^\d{4}$/', $t)) {
$x = $t;
} else {
// is time in H:MM or HH:MM format?
// ...otherwise, it is invalid
$d = \DateTime::createFromFormat("H:i", $t);
$x = $d ? $d->format("Hi") : '';
}
return $x;
}
// convert HHMM or (H)H:MM pairs into ZK time range, eg HHMM-HHMM
// return range string if valid else empty string.
private function composeTime($fromTime, $toTime) {
$retVal = '';
$fromTimeN = $this->normalizeTime($fromTime);
$toTimeN = $this->normalizeTime($toTime);
$start = \DateTime::createFromFormat(IPlaylist::TIME_FORMAT, "2019-01-01 " . $fromTimeN);
$end = \DateTime::createFromFormat(IPlaylist::TIME_FORMAT, "2019-01-01 " . $toTimeN);
$validRange = false;
if ($start && $end) {
// if end is less than start, assume next day
if($end < $start)
$end->modify('+1 day');
$minutes = ($end->getTimestamp() - $start->getTimestamp()) / 60;
$validRange = ($minutes > IPlaylist::MIN_SHOW_LEN) && ($minutes < IPlaylist::MAX_SHOW_LEN);
}
if ($validRange)
$retVal = $fromTimeN . "-" . $toTimeN;
else if ($fromTime || $toTime)
error_log("Error: invalid playlist time -" . $fromTime . "-, -" . $toTime ."-");
return $retVal;
}
private function isOwner($playlistId) {
$p = Engine::api(IPlaylist::class)->getPlaylist($playlistId, 0);
return $p && $p['dj'] == $this->session->getUser();
}
/**
* If the show is within the lookback period, asynchronously
* load any unknown artist and album art in the playlist.
*
* Note that only timestamped entries are loaded.
*/
protected function lazyLoadImages($playlistId, $trackId = 0) {
$playlist = Engine::api(IPlaylist::class)->getPlaylist($playlistId);
// unpublished playlist
if(!$playlist['airname'])
return;
$timeAr = explode("-", $playlist['showtime']);
if(count($timeAr) != 2)
return;
$showStart = \DateTime::createFromFormat(IPlaylist::TIME_FORMAT,
$playlist['showdate'] . " " . $timeAr[0]);
$now = new \DateTime();
// future show
if($showStart > $now)
return;
$now->modify("-7 day");
if($showStart > $now) {
// show is within the lookback period
PushServer::lazyLoadImages($playlistId, $trackId);
}
}
private static function hourToLocale($hour, $full=0) {
// account for legacy, free-format time encoding
if(!is_numeric($hour) || !UI::isUsLocale())
return $hour;
$h = (int)floor($hour/100);
$m = (int)$hour % 100;
$min = $m || $full?(":" . sprintf("%02d", $m)):"";
switch($h) {
case 0:
return $m?("12" . $min . "am"):"midnight";
case 12:
return $m?($h . $min . "pm"):"noon";
default:
if($h < 12)
return $h . $min . "am";
else
return ($h - 12) . $min . "pm";
}
}
public static function timeToLocale($time) {
if(strlen($time) == 9 && $time[4] == '-') {
list($fromtime, $totime) = explode("-", $time);
return self::hourToLocale($fromtime) . " - " . self::hourToLocale($totime);
} else
return strtolower(htmlentities($time));
}
public static function timestampToDate($time) {
if ($time == null || $time == '') {
return "";
} else {
$dateSpec = UI::isUsLocale() ? 'D M d, Y ' : 'D d M Y ';
return date($dateSpec, strtotime($time));
}
}
public static function makeShowDateAndTime($row) {
return self::timestampToDate($row['showdate']) . " " .
self::timeToLocale($row['showtime']);
}
public static function makeShowTime($row) {
return self::timeToLocale($row['showtime']);
}
public function listManagerGetHint() {
$hint = null;
$now = new \DateTime("now");
$today = $now->format("Y-m-d");
$lastWeek = $now->modify("-7 day")->format("Y-m-d");
// see if there is a PL on this day last week. if so use it.
$playlists = Engine::api(IPlaylist::class)->getPlaylists(1, 1, "", 1, $this->session->getUser(), 1, 10);
$djapi = Engine::api(IDJ::class);
while ($playlists && ($playlist = $playlists->fetch())) {
// skip duplicated lists with foreign airnames
$aid = $djapi->getAirname($playlist['airname'], $this->session->getUser());
if(!$aid)
continue;
if ($playlist['showdate'] == $lastWeek) {
$sourcePlaylist = $playlist;
$sourcePlaylist['showdate'] = $today;
$hint = [
"attributes" => [
"name" => $sourcePlaylist["description"],
"airname" => $sourcePlaylist["airname"],
"date" => $sourcePlaylist["showdate"],
"time" => $sourcePlaylist["showtime"]
]
];
break;
}
}
echo $hint ? json_encode($hint) : "{}";
}
public function emitHome() {
$this->newEntity(Home::class)->emitHome();
}
public function recentSpins() {
$this->newEntity(Home::class)->recentSpins();
}
public function getTimes() {
$this->newEntity(Home::class)->getTimes();
}
public function emitListManager() {
$this->setTemplate("list/mylists.html");
$this->addVar('airnames', $this->getDJAirNames());
$this->addVar('duplicate', isset($_POST["duplicate"]) && $_POST["duplicate"] ? $_POST["playlist"] : false);
$this->addVar('DUPLICATE_SUFFIX', IPlaylist::DUPLICATE_SUFFIX);
$this->addVar('MAX_DESCRIPTION_LENGTH', IPlaylist::MAX_DESCRIPTION_LENGTH);
$this->addVar('MAX_AIRNAME_LENGTH', IDJ::MAX_AIRNAME_LENGTH);
}
private function getDJAirNames() {
$airNames = [];
$records = Engine::api(IDJ::class)->getAirnames($this->session->getUser(), 0, 1);
while ($records && ($row = $records->fetch()))
$airNames[] = $row['airname'];
$airNames[] = 'None';
return $airNames;
}
// make header for edit & view playlist
private function makePlaylistHeader($isEditMode) {
$editCol = $isEditMode ? "<TD />" : "";
$header = "<TR class='playlistHdr'>" . $editCol . "<TH WIDTH='64px'>Time</TH><TH WIDTH='25%'>" .
"Artist</TH><TH WIDTH='25%'>Track</TH><TH></TH><TH>Album/Label</TH></TR>";
return $header;
}
private function emitPlaylistBody($playlist, $editMode) {
$header = $this->makePlaylistHeader($editMode);
$editCell = "";
echo "<TABLE class='playlistTable' CELLPADDING=1>\n";
echo "<THEAD>" . $header . "</THEAD>";
$api = Engine::api(IPlaylist::class);
$entries = $api->getTracks($playlist, $editMode)->asArray();
Engine::api(ILibrary::class)->markAlbumsReviewed($entries);
$observer = PlaylistBuilder::newInstance([
"action" => $this->subaction,
"editMode" => $editMode,
"authUser" => $this->session->isAuth("u")
]);
echo "<TBODY>\n";
if($entries != null && sizeof($entries) > 0) {
foreach($entries as $entry)
$observer->observe(new PlaylistEntry($entry));
}
echo "</TBODY></TABLE>\n";
if($editMode) {
UI::emitJS('js/playlists.track.js');
} else {
$show = $api->getPlaylist($playlist);
if($api->isNowWithinShow($show))
UI::emitJS('js/playlists.live.js');
}
}
private function emitPlaylistBanner($playlistId, $playlist, $editMode) {
$showName = $playlist['description'];
$djId = $playlist['id'];
$djName = $playlist['airname'] ?? "None";
$showDateTime = self::makeShowDateAndTime($playlist);
$this->title = "$showName with $djName " . self::timestampToDate($playlist['showdate']);
$this->extra = "<span class='sub'><b>Share Playlist:</b></span> <a class='nav share-link' data-link='".Engine::getBaseURL()."?subaction=viewListById&playlist=$playlistId'><span class='fas fa-link'></span></a></span>";
if(!$editMode && $this->session->isAuth("v"))
$showDateTime .= " <A HREF='javascript:document.duplist.submit();' TITLE='Duplicate Playlist'><span class='fas fa-clone dup-playlist'></span></A><FORM NAME='duplist' ACTION='?' METHOD='POST'><INPUT TYPE='hidden' NAME='subaction' VALUE='editList'><INPUT TYPE='hidden' NAME='duplicate' VALUE='1'><INPUT TYPE='hidden' NAME='playlist' VALUE='$playlistId'></FORM>";
$djName = htmlentities($djName, ENT_QUOTES, 'UTF-8');
$djLink = $djId ? "<a href='?subaction=viewDJ&seq=selUser&viewuser=$djId' class='nav2'>$djName</a>" : $djName;
echo "<div class='playlistBanner'><span id='banner-caption'> <span id='banner-description'>".htmlentities($showName, ENT_QUOTES, 'UTF-8')."</span> <span id='banner-dj'>with $djLink</span></span><div>{$showDateTime} </div></div>\n";
?>
<SCRIPT><!--
<?php ob_start([JSMin::class, 'minify']); ?>
// Truncate the show name (banner-description) so that the combined
// show name, DJ name, and date/time fit on one line.
$().ready(function() {
var maxWidth = $(".playlistBanner").outerWidth();
var dateWidth = $(".playlistBanner div").outerWidth();
if($("#banner-caption").outerWidth() + dateWidth > maxWidth) {
var width = maxWidth - $("#banner-dj").outerWidth() - dateWidth - 12;
$("#banner-description").outerWidth(width);
}
$(".share-link").on('click', function() {
navigator.clipboard.writeText($(this).data('link')).then(function() {
alert('Playlist URL copied to the clipboard!');
});
});
});
<?php ob_end_flush(); ?>
// -->
</SCRIPT>
<?php
}
private function editPlaylist($playlistId) {
$this->emitPlaylistBody($playlistId, true);
}
private function emitTagForm($playlistId, $message) {
$playlist = Engine::api(IPlaylist::class)->getPlaylist($playlistId, 1);
$this->emitPlaylistBanner($playlistId, $playlist, true);
$this->emitTrackAdder($playlistId, $playlist);
}
private function emitTrackAdder($playlistId, $playlist, $editTrack = false) {
$isLiveShow = !$editTrack && Engine::api(IPlaylist::class)->isNowWithinShow($playlist);
$nmeAr = Engine::param('nme');
$nmeOpts = '';
$nmePrefix = self::NME_PREFIX;
if ($nmeAr) {
foreach ($nmeAr as $nme)
$nmeOpts = $nmeOpts . "<option data-args='" . $nme['args'] . "' value='" . $nmePrefix . $nme['name'] . "'>" . $nme['name'] . "</option>";
}
?>
<div class='pl-form-entry form-entry'>
<input id='show-time' type='hidden' value="<?php echo $playlist['showtime']; ?>" >
<input id='timezone-offset' type='hidden' value="<?php echo round(date('Z')/-60, 2); /* server TZ equivalent of javascript Date.getTimezoneOffset() */ ?>" >
<input id='track-playlist' type='hidden' value='<?php echo $playlistId; ?>'>
<input id='track-action' type='hidden' value='<?php echo $this->subaction; ?>'>
<input id='const-prefix' type='hidden' value='<?php echo self::NME_PREFIX; ?>'>
<label></label><span id='error-msg' class='error'></span>
<div>
<?php if(!$editTrack) { ?>
<div class='dot-menu pull-right' tabindex='-1'>
<div class='dot-menu-dots no-text-select'>⋮</div>
<div class='dot-menu-content'>
<ul>
<li><a href='#' class='nav share-link' data-link='<?php echo Engine::getBaseURL()."?subaction=viewListById&playlist=$playlistId"; ?>' title='copy playlist URL to the clipboard'>Link to Playlist</a>
<li><a href='?target=export&playlist=<?php echo $playlistId; ?>&format=csv' class='nav' download='playlist.csv' title='export playlist as CSV'>Export CSV</a>
<li><a href='api/v1/playlist/<?php echo $playlistId; ?>' class='nav' download='playlist.json' title='export playlist as JSON'>Export JSON</a>
<li><a href='?target=export&playlist=<?php echo $playlistId; ?>&format=html' class='nav' target='_blank' title='printable playlist (opens in new window)'>Print View</a>
</ul>
</div>
</div>
<?php } ?>
<label>Type:</label>
<select id='track-type-pick'>
<option value='manual-entry'>Music</option>
<option value='comment-entry'>Comment</option>
<option value='set-separator'>Mic Break (separator)</option>
<?php echo $nmeOpts; ?>
</select>
</div>
<div id='track-entry'>
<div id='manual-entry'>
<div style='white-space: nowrap'>
<label>Artist / Tag:</label>
<input required id='track-artist' autocomplete='off' maxlength=<?php echo PlaylistEntry::MAX_FIELD_LENGTH;?> data-focus />
<span class='track-info' id='tag-status'>Artist name or tag number</span>
<datalist id='track-artists'>
</datalist>
</div>
<div>
<label>Track:</label>
<input required id='track-title' maxlength=<?php echo PlaylistEntry::MAX_FIELD_LENGTH;?> autocomplete='off'/>
<datalist id='track-titles'>
</datalist>
</div>
<div>
<label>Album:</label>
<input id='track-album' maxlength=<?php echo PlaylistEntry::MAX_FIELD_LENGTH;?> />
</div>
<div>
<label>Label:</label>
<input id='track-label' maxlength=<?php echo PlaylistEntry::MAX_FIELD_LENGTH;?> />
</div>
</div>
<div id='comment-entry' class='zk-hidden' >
<div>
<label style='vertical-align: top'>Comment:</label>
<textarea wrap=virtual id='comment-data' rows=4 maxlength=<?php echo PlaylistEntry::MAX_COMMENT_LENGTH; ?> required data-focus></textarea>
<div style='display: inline-block;'>
<span class='remaining' id='remaining'>(0/<?php echo PlaylistEntry::MAX_COMMENT_LENGTH; ?> characters)</span><br/>
<a id='markdown-help-link' href='#'>formatting help</a>
</div>
<input id='comment-max' type='hidden' value='<?php echo PlaylistEntry::MAX_COMMENT_LENGTH; ?>'>
</div>
<?php UI::markdownHelp(); ?>
</div>
<div id='nme-entry' class='zk-hidden' >
<div>
<label>Name/ID:</label>
<input id='nme-id' maxlength=<?php echo PlaylistEntry::MAX_FIELD_LENGTH;?> data-focus/>
</div>
</div>
</div> <!-- track-entry -->
<?php
$api = Engine::api(IPlaylist::class);
$window = $api->getTimestampWindow($playlistId);
$time = null;
$api->getTracksWithObserver($playlistId,
(new PlaylistObserver())->on('comment logEvent setSeparator spin', function($entry) use(&$time, $editTrack) {
$created = $entry->getCreatedTime();
if($created) $time = $created;
return $editTrack === $entry->getId();
})
);
if(!$time) {
$startTime = $api->getTimestampWindow($playlistId, false)['start'];
$time = $startTime->format('H:i:s');
}
// this is probably unnecessary, as desktop browsers *should*
// degrade 'tel' to 'text', *but* as this is a hack to
// deal with the lack of keyDown support in mobile input
// type=text, we'll include 'tel' only for mobile devices...
$ttype = preg_match('/tablet|mobile|android/i',
$_SERVER['HTTP_USER_AGENT'] ?? '') ? "tel" : "text";
// colon is included in 24hr format for symmetry with fxtime,
// which it is referencing
$timeSpec = UI::isUsLocale() ? 'g:i a' : 'H:i';
$startAMPM = $window['start']->format($timeSpec);
$endAMPM = $window['end']->format($timeSpec);
$timeMsg = "($startAMPM - $endAMPM)";
echo "<div id='time-entry'".($isLiveShow?" class='zk-hidden'":"").">
<label>Time:</label>
<input id='".($editTrack ? "edit" : "track")."-time' class='fxtime' type='$ttype' step='1' min='".$window['start']->format('H:i')."' max='".$window['end']->format('H:i')."' data-live='".($isLiveShow?1:0)."' data-last-val='$time' />
<span class='track-info'>$timeMsg</span>
</div>\n";
?>
<div>
<label></label>
<div class='action-area'>
<?php if($editTrack) { ?>
<button type='button' id='edit-save' class='edit-mode default'>Save</button>
<button type='button' id='edit-delete' class='edit-mode'>Delete</button>
<button type='button' id='edit-cancel' class='edit-mode'>Cancel</button>
<?php } else { ?>
<button type='button' disabled id='track-play' class='track-submit default'>Add <?php echo $isLiveShow?"(Playing Now)<img src='img/play.svg' />":"Item";?></button>
<button type='button' disabled id='track-add' class='track-submit<?php if(!$isLiveShow) echo " zk-hidden"; ?>'>Add (Upcoming)<img src='img/play-pause.svg' /></button>
<?php } ?>
</div>
</div>
<div class='toggle-time-entry<?php if (!$isLiveShow) echo " zk-hidden"; ?>'><div><!--🕑--></div></div>
</div> <!-- track-editor -->
<hr>
<div id="extend-show" class="zk-popup">
<div class="zk-popup-content">
<h4>You have reached the end time of your show.</h4>
<p>Extend by:
<select id="extend-time">
<option value="5">5 minutes</option>
<option value="10">10 minutes</option>
<option value="15">15 minutes</option>
<option value="30">30 minutes</option>
<option value="60">1 hour</option>
</select></p>
<div class="zk-popup-actionarea">
<button type="button">Cancel</button>
<button type="button" class="default" id="extend">Extend</button>
</div>
</div>
</div> <!-- extend-show -->
<?php
}
private function emitEditForm($playlistId, $id, $album) {
?>
<DIV class='playlistBanner'> Editing highlighted item</DIV>
<input type='hidden' id='track-id' value='<?php echo $id; ?>'>
<?php
$entry = new PlaylistEntry($album);
switch($entry->getType()) {
case PlaylistEntry::TYPE_SET_SEPARATOR:
$type = "set-separator";
break;
case PlaylistEntry::TYPE_COMMENT:
$type = "comment-entry";
echo "<input type='hidden' id='old-comment-data' value='" .
htmlentities($entry->getComment(), ENT_QUOTES, 'UTF-8') . "' />\n";
break;
case PlaylistEntry::TYPE_LOG_EVENT:
$type = self::NME_PREFIX . $entry->getLogEventType();
echo "<input type='hidden' id='old-event-code' value='" .
htmlentities($entry->getLogEventCode(), ENT_QUOTES, 'UTF-8') . "' />\n";
break;
default:
$type = "manual-entry";
foreach (['tag', 'artist', 'album', 'label', 'title'] as $field)
echo "<input type='hidden' id='old-track-$field' value='" .
htmlentities($album[$field == 'title' ? 'track' : $field], ENT_QUOTES, 'UTF-8') . "' />\n";
break;
}
echo "<input type='hidden' id='old-created' value='" . $entry->getCreatedTime() . "' />\n";
echo "<input type='hidden' id='edit-type' value='$type' />\n";
$playlist = Engine::api(IPlaylist::class)->getPlaylist($playlistId, 1);
$showName = $playlist['description'];
$djName = $playlist['airname'] ?? "None";
$this->title = "$showName with $djName " . self::timestampToDate($playlist['showdate']);
$this->emitTrackAdder($playlistId, $playlist, $id);
}
public function emitEditor() {
$playlist = $_REQUEST["playlist"] ?? null;
$seq = $_REQUEST["seq"] ?? null;
$id = $_REQUEST["id"] ?? null;
?>
<TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%">
<TR><TD>
<?php
if($seq == "editTrack") {
$albuminfo = Engine::api(IPlaylist::class)->getTrack($id);
if($albuminfo) {
// if editing a track, always get the playlist from
// the track, even if one is supplied in the request
$playlist = $albuminfo['list'];
}
}
$message = "";
if(is_null($playlist) || !$this->isOwner($playlist)) {
$seq = "error";
$message = "access error";
}
switch ($seq) {
case "editTrack":
$this->emitEditForm($playlist, $id, $albuminfo);
break;
default:
$this->emitTagForm($playlist, $message);
break;
}
?>
</TD></TR>
<TR><TD>
<?php $this->editPlaylist($playlist, $id); ?>
</TD></TR>
</TABLE>
<?php
}
private function insertTrack($playlistId, $tag, $artist, $track, $album, $label, $spinTime) {
$id = 0;
$status = '';
// Run the query
$success = Engine::api(IPlaylist::class)->insertTrack($playlistId,
$tag, $artist, $track, $album, $label, $spinTime, $id, $status);
}
public function emitImportList() {
$validate = $_POST["validate"];
$format = $_REQUEST["format"] ?? "json";
if($format == "csv") {
$description = mb_substr(trim($_REQUEST["description"]), 0, IPlaylist::MAX_DESCRIPTION_LENGTH);
$date = $_REQUEST["date"];
$time = $_REQUEST["time"];
$airname = $_REQUEST["airname"];
$djname = mb_substr(trim($_REQUEST["djname"]), 0, IDJ::MAX_AIRNAME_LENGTH);
$fromtime = $_REQUEST["fromtime"];
$totime = $_REQUEST["totime"];
}
$delimiter = $_REQUEST["delimiter"] ?? "";
$enclosure = $_REQUEST["enclosure"] ?? "\"";
$userfile = $_FILES['userfile']['tmp_name'];
$playlist = $_REQUEST["playlist"];
$button = $_REQUEST["button"];
$empty = $_POST["empty"] ?? 0;
if($empty)
$errorMessage = "<b><font class='error'>Import file contains no data. Check the format and try again.</font></b>";
if($format == "csv") {
if(!$date)
$date = date("Y-m-d");
list($year, $month, $day) = explode("-", $date);
$time = $this->composeTime($fromtime, $totime);
if($validate == "edit" && !$time) {
$errorMessage = "<b><font class='error'>Invalid time range (min " . IPlaylist::MIN_SHOW_LEN . " minutes, max " . (IPlaylist::MAX_SHOW_LEN / 60) . " hours)</font></b>";
$totime = "";
}
// lookup the airname
$aid = null;
if($validate == "edit" && $airname && strcasecmp($airname, "none")) {
$djapi = Engine::api(IDJ::class);
$aid = $djapi->getAirname($airname, $this->session->getUser());
if(!$aid) {
// airname does not exist; try to create it
$success = $djapi->insertAirname(mb_substr($airname, 0, IDJ::MAX_AIRNAME_LENGTH), $this->session->getUser());
if($success > 0) {
// success!
$aid = $djapi->lastInsertId();
} else {
$errorMessage = "<b><font class='error'>Airname '$airname' is invalid or already exists.</font></b>";
$airname = "";
$aid = false;
}
}
}
}
if($userfile && $format == "json") {
// read the JSON file
$file = file_get_contents($userfile);
// parse the file
$json = json_decode($file);
// allow type 'show' in root node (legacy)
if(!$json || $json->type != "show") {
// 'show' encapsulated within data
if($json && is_array($json->data) && $json->data[0]->type == "show")
$json = $json->data[0];
else if($json && $json->data && $json->data->type == "show")
$json = $json->data;
else
$errorMessage = "<B><FONT CLASS='error'>File is not in the expected format. Ensure file is a valid JSON playlist.</FONT></B><BR>\n";
}
if($json && $json->type == "show") {
// allow for legacy attributes at the data level
$attrs = isset($json->attributes)?$json->attributes:$json;
// validate the show's properties
$valid = false;
list($year, $month, $day) = explode("-", $attrs->date);
if($attrs->airname && $attrs->name && $attrs->time &&
checkdate($month, $day, $year))
$valid = true;
// lookup the airname
if($valid) {
$djapi = Engine::api(IDJ::class);
$airname = $djapi->getAirname($attrs->airname, $this->session->getUser());
if(!$airname) {
// airname does not exist; try to create it
$success = $djapi->insertAirname(mb_substr($attrs->airname, 0, IDJ::MAX_AIRNAME_LENGTH), $this->session->getUser());
if($success > 0) {
// success!
$airname = $djapi->lastInsertId();
} else
$valid = false;
}
}
// create the playlist
if($valid) {
$papi = Engine::api(IPlaylist::class);
$papi->insertPlaylist($this->session->getUser(), $attrs->date, $attrs->time, mb_substr($attrs->name, 0, IPlaylist::MAX_DESCRIPTION_LENGTH), $airname);
$playlist = $papi->lastInsertId();
// insert the tracks
$count = 0;
$status = '';
$window = $papi->getTimestampWindow($playlist);
$data = isset($json->attributes)?$attrs->events:$json->data;
foreach($data as $pentry) {
$entry = PlaylistEntry::fromJSON($pentry);
$created = $entry->getCreated();
if($created) {
try {
$stamp = PlaylistEntry::scrubTimestamp(
new \DateTime($created), $window);
$entry->setCreated($stamp?$stamp->format(IPlaylist::TIME_FORMAT_SQL):null);
} catch(\Exception $e) {
error_log("failed to parse timestamp: $created");
$entry->setCreated(null);
}
}
$success = $papi->insertTrackEntry($playlist, $entry, $status);
$count++;
}
if($count == 0) {
$papi->deletePlaylist($playlist);
$errorMessage = "<b><font class='error'>Import file contains no entries.</font></b><br>\n";
} else {
// success
$this->lazyLoadImages($playlist);
// display the editor
?>
<SCRIPT><!--
window.open("?subaction=editListEditor&playlist=<?php echo $playlist; ?>", "_top");
// -->
</SCRIPT>
<?php
$displayForm = false;
}
} else
$errorMessage = "<B><FONT CLASS='error'>Show details are invalid.</FONT></B><BR>\n";
}
}
if(!$userfile || $userfile == "none" || $errorMessage || $empty ||
$format == "csv" && (
$description == "" ||
$time == '' ||
$aid === false ||
!checkdate($month, $day, $year))) {
$this->setTemplate("list/import.html");
$this->addVar('errorMessage',
$validate == "edit" ?
($errorMessage ?? "<b><font class='error'>Ensure fields are not blank and date is valid.</font></b><br>\n") : false);
$this->addVar('format', $format);
$this->addVar('date', $date);
$this->addVar('fromtime', $fromtime);
$this->addVar('totime', $totime);
$this->addVar('dateformat', UI::isUsLocale() ? "mm/dd/yy" : "dd-mm-yy");
$this->addVar('airnames', $this->getDJAirNames());
$this->addVar('delimiter', $delimiter);
$this->addVar('enclosure', $enclosure);
$this->addVar('description', stripslashes($description));
$this->addVar('MAX_DESCRIPTION_LENGTH', IPlaylist::MAX_DESCRIPTION_LENGTH);
$this->addVar('airname', $airname ?? '');
$this->addVar('MAX_AIRNAME_LENGTH', IDJ::MAX_AIRNAME_LENGTH);
} else if($format == "csv"){
// Create the playlist
$api = Engine::api(IPlaylist::class);
$success = $api->insertPlaylist($this->session->getUser(), $date, $time, $description, $aid);
$playlist = $api->lastInsertId();
// empty delimiter is tab
if(strlen(trim($delimiter)) == 0)
$delimiter = "\t";
// Insert the tracks
$count = 0;
$fd = new \SplFileObject($userfile, "r");
$window = $api->getTimestampWindow($playlist);
while($fd->valid()) {
$line = $fd->fgetcsv($delimiter, $enclosure);
switch(count($line)) {
case 4:
// artist track album label
$this->insertTrack($playlist,
0, // tag
PlaylistEntry::scrubField($line[0]), // artist
PlaylistEntry::scrubField($line[1]), // track
PlaylistEntry::scrubField($line[2]), // album
PlaylistEntry::scrubField($line[3]), null); // label
$count++;
break;
case 5:
case 6:
// artist track album tag label
if($line[3]) {
// Lookup tag
$albumrec = Engine::api(ILibrary::class)->search(ILibrary::ALBUM_KEY, 0, 1, $line[3]);
if(sizeof($albumrec) == 0) {
// invalid tag
$line[3] = "";
} else {
// update artist and album from tag
if(!$albumrec[0]["iscoll"])
$line[0] = $albumrec[0]["artist"];
$line[2] = $albumrec[0]["album"];
// update label name
$line[4] = isset($albumrec[0]["name"])?
$albumrec[0]["name"]:"(Unknown)";
}
}
if(count($line) == 6 && $line[5]) {
try {
$timestamp = PlaylistEntry::scrubTimestamp(
new \DateTime($line[5]), $window);
} catch(\Exception $e) {
error_log("failed to parse timestamp: {$line[5]}");
$timestamp = null;
}
} else
$timestamp = null;
$this->insertTrack($playlist,
trim($line[3]), // tag
PlaylistEntry::scrubField($line[0]), // artist
PlaylistEntry::scrubField($line[1]), // track
PlaylistEntry::scrubField($line[2]), // album
PlaylistEntry::scrubField($line[4]), // label
$timestamp); // timestamp
$count++;
break;
}
}
// echo "<B>Imported $count tracks.</B>\n";
$fd = null; // close
if($count == 0) {
$api->deletePlaylist($playlist);
$_POST["empty"] = 1;
$this->emitImportList();
return;
}
$this->lazyLoadImages($playlist);
?>
<SCRIPT><!--
window.open("?subaction=editListEditor&playlist=<?php echo $playlist; ?>", "_top");
// -->
</SCRIPT>
<?php
}
}
public function emitViewPlayList() {
$playlistId = $_REQUEST["playlist"];
$this->viewList($playlistId);
}
private function viewList($playlistId) {
$row = Engine::api(IPlaylist::class)->getPlaylist($playlistId, 1);
if(!$row ||
!$row['airname'] && $this->session->getUser() != $row['dj']) {
echo "<B>Sorry, the playlist you have requested does not exist.</B>";
return;
}
if($this->subaction == "viewDJ" && $row['airname'])
$this->tertiary = $row['airname'];
$this->emitPlaylistBanner($playlistId, $row, false);
$this->emitPlaylistBody($playlistId, false);
}
private function emitViewDJAlbum(&$result, $class="", $count=0, $labelField="label") {
for($i=0; $i < sizeof($result); $i++) {
echo " <TR><TD VALIGN=TOP ALIGN=\"right\"$class>";
if($count)
echo (string)($i + 1).". ";
else
echo " • ";
echo "</TD><TD$class>";
// Setup artist and label
$artist = preg_match("/^(\[)?COLL(?(1)\]|$)/i", $result[$i]["artist"])?"Various Artists":$result[$i]["artist"];
$label = str_replace(" Records", "", $result[$i][$labelField]);
$label = str_replace(" Recordings", "", $label);
echo UI::smartURL($artist) . " • <I>";
// Album
if($result[$i]["tag"])
echo "<A CLASS=\"nav\" HREF=\"".
"?s=byAlbumKey&n=". UI::URLify($result[$i]["tag"]).
"&action=search\">";
echo UI::smartURL($result[$i]["album"], !$result[$i]["tag"]);
if($result[$i]["tag"])
echo "</A>";
echo "</I>";
if($label)
echo " • (".UI::smartURL($label) . ")";
echo "</TD></TR>\n";
}
}
public function emitViewDJ() {
UI::emitJS('js/zklistbox.js');
$seq = $_REQUEST["seq"] ?? '';
$viewuser = $_REQUEST["viewuser"] ?? 0;
$playlist = $_REQUEST["playlist"] ?? 0;
settype($playlist, "integer");
settype($viewuser, "integer");
if(((($seq == "selUser") && $viewuser)) ||
(($seq == "selList") && !$playlist)) {
$results = Engine::api(IDJ::class)->getAirnames(0, $viewuser);
if($results) {
$row = $results->fetch();
$this->tertiary = $row['airname'];
}
?>
<FORM ACTION="?" class="selector" METHOD=POST>
<TABLE WIDTH="100%"><TR><TD ALIGN=RIGHT VALIGN=TOP>
<?php
// Emit optional URL and/or e-mail for DJ
if($row['url']) {
echo " <A HREF=\"".$row['url']."\" CLASS=\"nav\" target='_blank'><B>Go to ".$row['airname']."'s website</B></A>\n";
if($row['email'])
echo " | \n";
}
if($row['email']) {
echo " <A HREF=\"mailto:".$row['email']."\" CLASS=\"nav\"><B>e-mail ".$row['airname']."</B></A>\n";
}
?>
</TD></TR></TABLE>
<TABLE CELLSPACING=0>
<?php
$weeks = 10;
$limit = 10;
$formatEndDate = date("l, j F Y");
$topPlays = Engine::api(IPlaylist::class)->getTopPlays($viewuser, $weeks * 7, $limit);
if(sizeof($topPlays)) {
echo "<TR><TH COLSPAN=2 ALIGN=LEFT CLASS=\"subhead\"> ".$row['airname']."'s top $limit<BR> <FONT CLASS=\"subhead2\">for the $weeks week period ending $formatEndDate</FONT></TH></TR>\n";
$this->emitViewDJAlbum($topPlays, "", 1);
}
?>
</TABLE>
<?php if (sizeof($topPlays)) echo "<BR>\n"; ?>
<TABLE WIDTH="100%" CELLPADDING=0 CELLSPACING=0 BORDER=0>
<TR><TD CLASS="recentPlays" VALIGN=TOP>
<?php
$count = 10;
$recentPlays = Engine::api(IPlaylist::class)->getRecentPlays($viewuser, $count);
$recentReviews = Engine::api(ILibrary::class)->search(ILibrary::ALBUM_AIRNAME, 0, $count - 1, $viewuser, "Date-");
$block = sizeof($recentReviews);
$blname = sizeof($topPlays)?"":$row['airname'] . "'s ";
echo " <TABLE CELLSPACING=0 BORDER=0>\n";
if(sizeof($recentPlays)) {
echo "<TR><TH COLSPAN=2 ALIGN=LEFT CLASS=\"subhead\"> ${blname}Recent airplay</TH></TR>";
$this->emitViewDJAlbum($recentPlays, $block?" CLASS=\"sub\"":"");
}
?>
</TABLE>
</TD><?php
if(sizeof($recentReviews)) {
$block = sizeof($recentPlays);
echo "<TD>".($block?" ":"")."</TD><TD CLASS=\"recentReviews\"VALIGN=TOP>\n";
$blname = (sizeof($topPlays) || sizeof($recentPlays))?"":$row['airname'] . "'s ";
echo " <TABLE BORDER=0 CELLSPACING=0>\n";
echo " <TR><TH COLSPAN=2 ALIGN=LEFT CLASS=\"subhead\"> ${blname}Recent reviews</TH></TR>\n";
$this->emitViewDJAlbum($recentReviews, $block?" CLASS=\"sub\"":"", 0, "name");
if(sizeof($recentReviews) == $count - 1)
echo " <TR><TD></TD><TD ALIGN=LEFT CLASS=\"sub\"><A HREF=\"?action=viewRecent&subaction=viewDJ&seq=selUser&viewuser=$viewuser\" CLASS=\"nav\">More reviews...</A></TD></TR>\n";
echo " </TABLE></TD>\n";
}
?>
</TR></TABLE>
<?php if (sizeof($topPlays) || sizeof($recentPlays) || sizeof($recentReviews)) echo "<BR>\n"; ?>
<TABLE>
<TR><TH ALIGN=LEFT><?php echo $row['airname'];?>'s playlists:</TH></TR>
<TR><TD>
<ul tabindex='0' class='selector listbox no-text-select' data-name='playlist'>
<?php
// Run the query
$records = Engine::api(IPlaylist::class)->getPlaylists(0, 0, 0, $viewuser);
while($row = $records->fetch())
echo " <li data-value=\"$row[0]\">$row[1] -- ".htmlentities($row[3], ENT_QUOTES, 'UTF-8')."</li>\n";
?>
</ul></TD></TR>