Subversion Repositories cheapmusic

Rev

Rev 141 | Rev 150 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10 - 1
<?php
20 - 2
error_reporting(E_ALL);
10 - 3
 
4
// BUGBUG {"message": "You are making requests too quickly."}
138 - 5
function findDiscogsMaster($random = false) {
10 - 6
    $vendors = Vendors::getInstance();
7
    $config = $vendors->getVendor(Vendors::DISCOGS);
45 - 8
    $maxMasterCount = $config['maxMasters'];
13 - 9
 
10 - 10
    $_SESSION["discogs"] = "";
137 - 11
    $stxt = $_SESSION["searchTerm"];
10 - 12
 
81 - 13
    $params = [];
14
    $params["token"] = $config['token'];
137 - 15
    $params["type"] = "All";
46 - 16
 
137 - 17
    if (!empty($_SESSION["advSearch"])) {
18
        $stxt = '';
19
        if (!empty($_SESSION["advSearch"]["Title"])) {
20
            $params["release_title"] = $_SESSION["advSearch"]["Title"];
21
            $stxt .= $_SESSION["advSearch"]["Title"];
65 - 22
        }
137 - 23
        if (!empty($_SESSION["advSearch"]["Artist"])) {
24
            $params["artist"] = $_SESSION["advSearch"]["Artist"];
25
            $stxt .= $_SESSION["advSearch"]["Artist"];
46 - 26
        }
137 - 27
        if (!empty($_SESSION["advSearch"]["Track"])) {
28
            $params["track"] = $_SESSION["advSearch"]["Track"];
29
            $stxt .= $_SESSION["advSearch"]["Track"];
30
        }
31
        if (!empty($_SESSION["advSearch"]["Barcode"])) {
32
            $params["barcode"] = $_SESSION["advSearch"]["Barcode"];
33
            $stxt .= $_SESSION["advSearch"]["Barcode"];
34
        }
35
        if (!empty($_SESSION["advSearch"]["Format"])) {
36
            $params["format"] = $_SESSION["advSearch"]["Format"];
37
            $stxt .= $_SESSION["advSearch"]["Format"];
38
        }
39
        if (!empty($_SESSION["advSearch"]["Catno"])) {
40
            $params["catno"] = $_SESSION["advSearch"]["Catno"];
41
            $stxt .= $_SESSION["advSearch"]["Catno"];
42
        }
43
        if (!empty($_SESSION["advSearch"]["Label"])) {
44
            $params["label"] = $_SESSION["advSearch"]["Label"];
45
            $stxt .= $_SESSION["advSearch"]["Label"];
46
        }
47
        if (!empty($_SESSION["advSearch"]["Country"])) {
48
            $params["country"] = $_SESSION["advSearch"]["Country"];
49
            $stxt .= $_SESSION["advSearch"]["Country"];
50
        }
51
        if (!empty($_SESSION["advSearch"]["Year"])) {
52
            $params["year"] = $_SESSION["advSearch"]["Year"];
53
            $stxt .= $_SESSION["advSearch"]["Year"];
54
        }
55
    } else {
138 - 56
        $params["query"] = ($random ? mt_rand($config['minRelease'], $config['maxRelease']) : $stxt);
65 - 57
    }
10 - 58
 
81 - 59
    $pairs = array();
60
    foreach ($params as $key => $value) {
61
        array_push($pairs, rawurlencode($key)."=".rawurlencode($value));
62
    }
63
    $canonical_query_string = join("&", $pairs);
64
 
65
    $url = "https://api.discogs.com/database/search?" . $canonical_query_string;
138 - 66
    $searchResp = ($random ? false : getSearchCache("Discogs", $stxt, ""));
99 - 67
    if ($searchResp === false) {
68
        $searchResp = getUrl($url, $config['userAgent']);
138 - 69
        if (!$random) {
127 - 70
            saveSearchCache("Discogs", $stxt, "", $searchResp);
104 - 71
        }
99 - 72
    }
65 - 73
    $searchResp = utf8_encode($searchResp);
74
    $searchResp = json_decode($searchResp);
10 - 75
 
43 - 76
    if (empty($searchResp)) {
77
        return;
78
    }
79
 
10 - 80
    if (!empty($searchResp->{'message'})) {
81
        if ($searchResp->{'message'} == "You are making requests too quickly.") {
96 - 82
            my_error_log("Discogs: You are making requests too quickly.");
10 - 83
            return;
84
        }
14 - 85
        return;
10 - 86
    }
87
 
88
    if ($searchResp->{'pagination'}->{'items'} < 1) {
138 - 89
        if ($random) { // must find something
90
            findDiscogsMaster(true);
38 - 91
        }
92
 
10 - 93
        return;
94
    }
95
 
138 - 96
    $noResults = $searchResp->{'pagination'}->{'items'};
97
 
127 - 98
    $xh = new Html;
99
    $xh->init($_SESSION["htmlIndent"]);
13 - 100
 
127 - 101
    $xh->add_attribute("class", "container-fluid bg-light");
141 - 102
    $xh->add_attribute("id", "discogsTable");
127 - 103
    $xh->tag('div');
104
    $xh->add_attribute("class", "text-center py-2");
138 - 105
    $xh->tag('h2', ($random ? "Random" : "Matching") . " Albums");
134 - 106
    $xh->add_attribute("id", "discogsDeckForm");
127 - 107
    $xh->add_attribute("method", "post");
108
    $xh->add_attribute("action", "/index.php");
109
    $xh->tag('form');
110
    $xh->insert_code(inputSessionTab());
111
    $xh->insert_code(inputNonce());
112
    $xh->add_attribute("id", "discogsTitle");
113
    $xh->add_attribute("type", "hidden");
114
    $xh->add_attribute("name", "discogsTitle");
115
    $xh->add_attribute("value", "");
116
    $xh->single_tag('input');
117
    $xh->add_attribute("id", "discogsArtist");
118
    $xh->add_attribute("type", "hidden");
119
    $xh->add_attribute("name", "discogsArtist");
120
    $xh->add_attribute("value", "");
121
    $xh->single_tag('input');
122
    $xh->add_attribute("id", "discogsBarcode");
123
    $xh->add_attribute("type", "hidden");
124
    $xh->add_attribute("name", "discogsBarcode");
125
    $xh->add_attribute("value", "");
126
    $xh->single_tag('input');
134 - 127
    $xh->add_attribute("type", "hidden");
128
    $xh->add_attribute("name", "submitBtn");
129
    $xh->add_attribute("value", "discogsSearch");
130
    $xh->single_tag('input');
127 - 131
    $xh->add_attribute("id", "discogsDeck");
132
    $xh->add_attribute("class", "card-deck");
133
    $xh->tag('div');
134
 
10 - 135
    $masterCount = 0;
136
    $processedMasters = [];
149 - 137
    $extraMasters = [];
129 - 138
    $searchUrls = [];
139
    $masterResps_cache = [];
10 - 140
    foreach ($searchResp->{'results'} as $searchey => $searchValue) {
78 - 141
// bugbug foreach ($searchResp->{'releases'} as $searchey => $searchValue) {
137 - 142
        if (!in_array($searchValue->{'type'}, array("master", "release")) && (empty($_SESSION["advSearch"]["Barcode"]))) {
10 - 143
            continue;
144
        }
145
 
137 - 146
        if (empty($_SESSION["advSearch"]["Barcode"]) && $searchValue->{'type'} == "release" && $searchValue->{'master_id'} > 0) {
10 - 147
            continue;
148
        }
57 - 149
 
137 - 150
        if (skipDuplicateMaster($searchValue, $processedMasters)) {
47 - 151
            continue;
152
        }
10 - 153
 
50 - 154
        if (++$masterCount > $maxMasterCount) {
149 - 155
            $pos = strpos($searchValue->{'title'}, " - ");
156
            if ($pos > 0) {
157
                $artist = substr($searchValue->{'title'}, 0, $pos);
158
                $title = substr($searchValue->{'title'}, $pos+3);
159
            } else {
160
                $artist = "";
161
                $title = $searchValue->{'title'};
162
            }
163
            $extraMasters[] = array( "id" => $searchValue->{'master_id'},
164
                                     "type" => $searchValue->{'type'},
165
                                     "title" => $title,
166
                                     "artists" => array(array("name" => $artist)),
167
                                     "styles" => $searchValue->{'style'} ? $searchValue->{'style'} : [],
168
                                     "genres" => $searchValue->{'genre'} ? $searchValue->{'genre'} : [],
169
                                     "thumb" => $searchValue->{'thumb'} ?? "",
170
                                     "coverImage" => $searchValue->{'cover_image'} ?? "",
171
                                     "uri" => "https://www.discogs.com" . $searchValue->{'uri'} ?? "",
172
                                     "year" => $searchValue->{'year'} ?? 0 );
173
            continue;
50 - 174
        }
175
 
137 - 176
        $processedMasters[] = array( 'id' => $searchValue->{'master_id'}, "title" => $searchValue->{'title'}, "year" => $searchValue->{'year'} ?? 0 );
177
 
99 - 178
        $searchUrl = $searchValue->{'master_id'} != 0 ? $searchValue->{'master_url'} : $searchValue->{'resource_url'};
132 - 179
 
129 - 180
        $searchType[$searchUrl] = $searchValue->{'type'};
181
        $searchThumb[$searchUrl] = $searchValue->{'thumb'};
182
        $searchCoverImage[$searchUrl] = $searchValue->{'cover_image'};
132 - 183
 
129 - 184
        $masterResps_cache[$searchUrl] = getSearchCache("Discogs", $searchUrl, "");
185
        if ($masterResps_cache[$searchUrl] === false) {
186
            $searchUrls[] = $searchUrl;
187
            unset($masterResps_cache[$searchUrl]);
99 - 188
        }
129 - 189
    }
10 - 190
 
129 - 191
    $masterResps = [];
192
    if (count($searchUrls) > 0) {
193
        $masterResps = getMultiUrl($searchUrls, $config['userAgent']);
194
    }
17 - 195
 
129 - 196
    foreach($masterResps as $key => $html) {
197
        saveSearchCache("Discogs", $key, "", $html);
198
    }
10 - 199
 
129 - 200
    foreach($masterResps_cache as $key => $html) {
201
        $masterResps[$key] = $html;
202
    }
203
    unset($masterResps_cache);
204
 
205
    $masterCount = 0;
149 - 206
    foreach ($masterResps as $key => $html) {
129 - 207
        $html = utf8_encode($html);
208
        $html = json_decode($html);
209
 
210
        if (!empty($html->{'message'})) {
211
            my_error_log("Discogs: " . $html->{'message'});
212
        } else if (!empty($html)) {
130 - 213
            $xh->insert_code(processMaster($html, $searchType[$key], $searchThumb[$key], $searchCoverImage[$key], ++$masterCount));
17 - 214
        }
10 - 215
    }
132 - 216
 
149 - 217
    foreach ($extraMasters as $html) {
218
        $object = json_decode(json_encode($html), false);
219
        $xh->insert_code(processMaster($object, $html["type"], $html["thumb"], $html["coverImage"], ++$masterCount));
220
    }
221
 
81 - 222
    if ($masterCount == 0) {
136 - 223
        $xh->reset();
81 - 224
    } else {
127 - 225
        $xh->close(); // div
226
        $xh->close(); // form
227
        $xh->close(); // div
132 - 228
 
130 - 229
        $xh->insert_code(discogsEvents());
120 - 230
 
138 - 231
        if ($random) {
127 - 232
            $xh->add_attribute("class", "container-fluid text-center");
233
            $xh->tag('div');
234
              $xh->add_attribute("method", "post");
235
              $xh->add_attribute("action", "/index.php");
236
              $xh->tag('form');
237
                $xh->insert_code(inputSessionTab());
238
                $xh->insert_code(inputSearchTerm());
239
                $xh->insert_code(inputNonce());
240
                $xh->add_attribute("id", "randomBtn");
241
                $xh->add_attribute("type", "submit");
242
                $xh->add_attribute("class", "btn btn-success m-2 rounded");
134 - 243
                $xh->add_attribute("name", "submitBtn");
127 - 244
                $xh->add_attribute("value", "random");
245
                $xh->tag('button', "More Random Album Suggestions");
246
              $xh->close(); // form
247
            $xh->close(); // div
130 - 248
            $xh->add_attribute("nonce", "xxxNONCExxx");
249
            $xh->tag('script');
137 - 250
                $str  = my_trim('document.addEventListener("DOMContentLoaded", function() {');
251
                $str .= my_trim('    document.getElementById("randomBtn").addEventListener("click", function(event) {');
252
                $str .= my_trim('        window.dataLayer.push({ "event" : "trackEvent", "eventCategory" : "Random Album", "eventAction" : "Click", "eventLabel" : "More"});');
253
                $str .= my_trim('    });');
254
                $str .= my_trim('});');
130 - 255
            $xh->insert_code($str);
256
            $xh->close(); // script
81 - 257
        }
38 - 258
    }
10 - 259
 
127 - 260
    $_SESSION["discogs"] = $xh->flush();
261
    //error_log(print_r($_SESSION["discogs"], 1));
262
 
10 - 263
    return;
264
}
265
 
130 - 266
function discogsEvents() {
127 - 267
    $xh = new Html;
268
    $xh->init($_SESSION["htmlIndent"]);
130 - 269
 
270
    $xh->add_attribute("nonce", "xxxNONCExxx");
271
    $xh->tag('script');
137 - 272
        $str  = my_trim('document.addEventListener("DOMContentLoaded", function() {');
273
        $str .= my_trim('  document.getElementById("discogsDeckForm").addEventListener("submit", function(event) {');
274
        $str .= my_trim('      if (window.google_tag_manager && window.ga && ga.create) {');
275
        $str .= my_trim('          event.preventDefault();');
276
        $str .= my_trim('          var title = document.getElementById("discogsTitle").value;');
277
        $str .= my_trim('          var artist = document.getElementById("discogsArtist").value;');
278
        $str .= my_trim('          window.dataLayer.push({ "event" : "search", "search_term" : title + " by " + (artist.length == 0 ? "Various Artists" : artist), "eventCallback": function () {event.target.submit();}});');
279
        $str .= my_trim('      }');
280
        $str .= my_trim('  });');
281
        $str .= my_trim('  document.getElementById("discogsDeck").addEventListener("click", function(event) {');
282
        $str .= my_trim('       var e = event.target.closest("button") || event.target.closest("a");');
283
        $str .= my_trim('       if (e && e.id.startsWith("discogsSearch")) {');
284
        $str .= my_trim('           document.getElementById("discogsTitle").value = e.getAttribute("data-title");');
285
        $str .= my_trim('           document.getElementById("discogsArtist").value = e.getAttribute("data-artist");');
286
        $str .= my_trim('           document.getElementById("discogsBarcode").value = e.getAttribute("data-barcode");');
287
        $str .= my_trim('           progressBar(e.getAttribute("data-search-title"));');
288
        $str .= my_trim('       } else if (e && e.id.startsWith("wl")) {');
289
        $str .= my_trim('           var user = e.getAttribute("data-user");');
290
        $str .= my_trim('           var cnt = e.getAttribute("data-cnt");');
291
        $str .= my_trim('           var wl = e.getAttribute("data-wl");');
292
        $str .= my_trim('           window.dataLayer.push({ "event" : "trackEvent", "eventCategory" : "Bookmark", "eventAction" : "Add", "eventLabel" : ""});');
293
        $str .= my_trim('           addWishlist(user, e, cnt, wl);');
294
        $str .= my_trim('       } else if (e && e.id.startsWith("discogsInfo")) {');
295
        $str .= my_trim('           window.dataLayer.push({ "event" : "trackEvent", "eventCategory" : "Album Info", "eventAction" : "Click", "eventLabel" : ""});');
296
        $str .= my_trim('       } else if (e && e.id.startsWith("discogsVideo")) {');
297
        $str .= my_trim('           window.dataLayer.push({ "event" : "trackEvent", "eventCategory" : "Video Info", "eventAction" : "Click", "eventLabel" : ""});');
298
        $str .= my_trim('       } else if (e && e.id.startsWith("discogsRedirect")) {');
299
        $str .= my_trim('           window.dataLayer.push({ "event" : "trackEvent", "eventCategory" : "Discogs", "eventAction" : "Click", "eventLabel" : "Detailed Data"});');
300
        $str .= my_trim('       } else if (e && e.id.startsWith("discogsYTRedirect")) {');
301
        $str .= my_trim('           window.dataLayer.push({ "event" : "trackEvent", "eventCategory" : "Video", "eventAction" : "Play", "eventLabel" : "Youtube"});');
302
        $str .= my_trim('       }');
303
        $str .= my_trim('  });');
304
        $str .= my_trim('});');
130 - 305
    $xh->insert_code($str);
306
    $xh->close(); // script
132 - 307
 
130 - 308
    $html = $xh->flush();
309
    //error_log(print_r($html, 1));
310
 
311
    return $html;
312
}
313
 
314
function processMaster($master, $type, $thumbnail, $coverImage, $cnt) {
315
    $xh = new Html;
316
    $xh->init($_SESSION["htmlIndent"]);
127 - 317
    $xhmod = new Html;
318
    $xhmod->init($_SESSION["htmlIndent"]);
319
 
10 - 320
    $artists = [];
149 - 321
    if (isset($master->{'artists'})) {
322
        foreach ($master->{'artists'} as $key => $value) {
323
            $artists[] = trim(preg_replace('/\([0-9]+\)$/', "", (string)$value->{'name'}));
324
        }
10 - 325
    }
326
 
327
    $genres = [];
149 - 328
    if (isset($master->{'genres'})) {
329
        foreach ($master->{'genres'} as $key => $value) {
330
            $genres[] = (string)$value;
331
        }
10 - 332
    }
333
 
20 - 334
    $labels = [];
335
    if (isset($master->{'labels'})) {
336
        foreach ($master->{'labels'} as $key => $value) {
337
            $labels[] = trim(preg_replace('/\([0-9]+\)$/', "", (string)$value->{'name'}));
338
        }
339
    }
340
 
341
    $formats = [];
342
    if (isset($master->{'formats'})) {
343
        foreach ($master->{'formats'} as $key => $value) {
344
            $formats[] = trim(preg_replace('/\([0-9]+\)$/', "", (string)$value->{'name'}));
345
        }
346
    }
38 - 347
 
20 - 348
    $country = '';
349
    if (isset($master->{'country'})) {
350
        $country = (string)$master->{'country'};
351
    }
352
 
149 - 353
    $xh->add_attribute("class", "card mx-auto discogs-card d-none");
127 - 354
    $xh->tag('div');
355
 
356
    $xh->add_attribute("class", "card-header bg-secondary d-flex");
357
    $xh->tag('div');
17 - 358
    $searchArtists = "";
20 - 359
    if (count($artists) < 5) {
47 - 360
        $wlArtists = join(", ", $artists);
149 - 361
        if (isset($artists[0]) && $artists[0] != 'Various') {
65 - 362
            $searchArtists = join(" ", $artists);
363
        }
364
    }
365
    else {
47 - 366
        $wlArtists = "Various Artists";
14 - 367
    }
127 - 368
    $xh->add_attribute("class", "card-title font-weight-bold small flex-grow-1");
149 - 369
    $xh->tag('p', htmlentities((string)$master->{'title'}) . (!empty($wlArtists) ? " by " . htmlentities($wlArtists) : ""));
127 - 370
    $xh->close(); // div
132 - 371
 
72 - 372
    if (empty($thumbnail) || preg_match("/spacer.gif$/", $thumbnail)) {
14 - 373
        $thumbnail = "images/no-image-available.jpg";
374
    }
127 - 375
    $xh->add_attribute("class", "card-body d-flex justify-content-center align-items-center p-1 m-0");
376
    $xh->tag('div');
137 - 377
    $xh->add_attribute("class", "btn p-0 m-0 lazyload lazypreload");
378
    $xh->add_attribute("src", $thumbnail);
127 - 379
    $xh->add_attribute("data-toggle", "modal");
380
    $xh->add_attribute("data-target", "#masterModal" . $cnt);
381
    $xh->add_attribute("title", "Album Information");
382
    $xh->add_attribute("data-toggle2", "tooltip");
383
    $xh->add_attribute("alt", "Discogs Cover Thumbnail");
384
    $xh->single_tag('img');
385
    $xh->close(); // div
141 - 386
    $xh->add_attribute("class", "card-footer form-row btn-group-sm bg-secondary p-0 m-0");
127 - 387
    $xh->tag('div');
141 - 388
 
389
    $xh->add_attribute("class", "col-3 btn-group p-0 m-0");
127 - 390
    $xh->tag('div');
45 - 391
 
141 - 392
    $barcode = "";
393
    if (empty($_SESSION["advSearch"]["Barcode"]) && !empty($master->{'identifiers'})) {
394
        foreach ($master->{'identifiers'} as $identifier) {
395
            if ($identifier->{'type'} == 'Barcode') {
396
                $tmpBarcode = preg_replace("/[^0-9]/", "", $identifier->{'value'});
397
                $barcodeType = clsLibGTIN::GTINCheck($tmpBarcode, false, 1);
398
                if (!empty($barcodeType)) {
399
                    $barcode = $tmpBarcode;
400
                }
401
            }
402
        }
403
    }
404
    else if (!empty($_SESSION["advSearch"]["Barcode"])) {
405
        $barcode = $_SESSION["advSearch"]["Barcode"];
406
    }
407
 
408
    if (isLoggedIn()) {
409
        $checkWlFlag = checkWishlist($type, $master->{'id'});
410
        $wlArr = array(
411
            ($type == "master" ? 'mid' : 'rid') => $master->{'id'},
412
            'title' => (string)$master->{'title'},
413
            'artist' => $wlArtists,
414
            'barcode' => $barcode,
415
            'thumbnail' => $thumbnail,
416
            'url' => (string)$master->{'uri'}
417
        );
418
        $wl = base64_encode(json_encode($wlArr));
419
 
420
        if (!$checkWlFlag) {
421
            $xh->add_attribute("id", "wl" . $cnt . "Btn");
422
            $xh->add_attribute("data-user", $_SESSION['sessData']['userID']);
423
            $xh->add_attribute("data-cnt", $cnt);
424
            $xh->add_attribute("data-wl", $wl);
425
        }
426
        $xh->add_attribute("type", "button");
427
        $xh->add_attribute("class", "btn btn-info btn-sm");
428
        $xh->add_attribute("title", $checkWlFlag ? "Already on Wishlist" : "Add to Wishlist");
429
        $xh->add_attribute("aria-label", $checkWlFlag ? "Already on Wishlist" : "Add to Wishlist");
430
        $xh->add_attribute("data-toggle", "tooltip");
431
        $xh->tag('button');
432
        $xh->add_attribute("class", "material-icons");
433
        $xh->tag('i', $checkWlFlag ? "library_add_check" : "library_add");
434
        $xh->close(); // button
435
    }
436
 
437
    $xh->close(); // div
438
 
439
    $xh->add_attribute("class", "col-3 btn-group p-0 m-0");
440
    $xh->tag('div');
441
 
45 - 442
    if (isset($master->{'videos'})) {
130 - 443
        $xh->add_attribute("id", "discogsVideo" . $cnt);
127 - 444
        $xh->add_attribute("type", "button");
445
        $xh->add_attribute("class", "btn btn-info btn-sm");
446
        $xh->add_attribute("data-toggle", "modal");
447
        $xh->add_attribute("data-target", "#videoModal" . $cnt);
448
        $xh->add_attribute("title", "Videos");
449
        $xh->add_attribute("aria-label", "Music videos");
450
        $xh->add_attribute("data-toggle2", "tooltip");
451
        $xh->tag('button');
452
        $xh->add_attribute("class", "material-icons");
453
        $xh->tag('i', "videocam");
454
        $xh->close(); // button
455
 
456
        $xhmod->add_attribute("id", "videoModal" . $cnt);
457
        $xhmod->add_attribute("class", "modal");
458
        $xhmod->tag('div');
459
        $xhmod->add_attribute("class", "modal-dialog");
460
        $xhmod->tag('div');
461
        $xhmod->add_attribute("class", "modal-content");
462
        $xhmod->tag('div');
463
        $xhmod->add_attribute("class", "modal-header bg-secondary");
464
        $xhmod->tag('div');
57 - 465
        if (count($artists) < 5) {
127 - 466
            $str = htmlentities(join(", ", $artists));
65 - 467
        }
468
        else {
127 - 469
            $str = "Various Artists";
57 - 470
        }
127 - 471
        $xhmod->add_attribute("class", "modal-title mx-auto display-6");
149 - 472
        $xhmod->tag('p', htmlentities((string)$master->{'title'}) . !empty($str) ? " by " . $str : "");
57 - 473
 
127 - 474
        $xhmod->add_attribute("type", "button");
475
        $xhmod->add_attribute("class", "close");
476
        $xhmod->add_attribute("data-dismiss", "modal");
477
        $xhmod->tag('button');
478
        $xhmod->add_attribute("class", "material-icons btn-dismiss");
479
        $xhmod->tag('i', "cancel_presentation");
480
        $xhmod->close(); // button
481
        $xhmod->close(); // div
57 - 482
 
127 - 483
        $xhmod->add_attribute("class", "modal-body mx-auto");
484
        $xhmod->tag('div');
485
        $xhmod->add_attribute("class", "display-6");
486
        $xhmod->tag('p', "Videos");
487
        $xhmod->add_attribute("class", "list-group");
488
        $xhmod->tag('ul');
489
 
45 - 490
        foreach ($master->{'videos'} as $video) {
127 - 491
            $xhmod->add_attribute("class", "list-group-item");
492
            $xhmod->tag('li');
493
            $xhmod->add_attribute("class", "row");
494
            $xhmod->tag('div');
495
            $xhmod->add_attribute("class", "col-1");
496
            $xhmod->tag('div');
497
            $xhmod->add_attribute("class", "svg-yt");
498
            $xhmod->add_attribute("viewBox", "0 0 24 24");
499
            $xhmod->tag('svg');
500
            $xhmod->add_attribute("fill", "currentColor");
501
            $xhmod->add_attribute("d", "M10,15L15.19,12L10,9V15M21.56,7.17C21.69,7.64 21.78,8.27 21.84,9.07C21.91,9.87 21.94,10.56 21.94,11.16L22,12C22,14.19 21.84,15.8 21.56,16.83C21.31,17.73 20.73,18.31 19.83,18.56C19.36,18.69 18.5,18.78 17.18,18.84C15.88,18.91 14.69,18.94 13.59,18.94L12,19C7.81,19 5.2,18.84 4.17,18.56C3.27,18.31 2.69,17.73 2.44,16.83C2.31,16.36 2.22,15.73 2.16,14.93C2.09,14.13 2.06,13.44 2.06,12.84L2,12C2,9.81 2.16,8.2 2.44,7.17C2.69,6.27 3.27,5.69 4.17,5.44C4.64,5.31 5.5,5.22 6.82,5.16C8.12,5.09 9.31,5.06 10.41,5.06L12,5C16.19,5 18.8,5.16 19.83,5.44C20.73,5.69 21.31,6.27 21.56,7.17Z");
502
            $xhmod->single_tag('path');
503
            $xhmod->close(); // svg
504
            $xhmod->close(); // div
505
            $xhmod->add_attribute("class", "col");
506
            $xhmod->tag('div');
507
            $xhmod->add_attribute("href", $video->uri);
134 - 508
            $xhmod->add_attribute("id", "discogsYTRedirect" . $cnt);
127 - 509
            $xhmod->add_attribute("target", "_blank");
510
            $xhmod->add_attribute("rel", "noreferrer noopener");
511
            $xhmod->add_attribute("class", "btn btn-light btn-link text-left");
512
            $xhmod->add_attribute("role", "button");
513
            $xhmod->tag('a', htmlentities($video->title) . " [" . gmdate('H:i:s', $video->duration) . "]");
514
            $xhmod->close(); // div
515
            $xhmod->close(); // div
516
            $xhmod->close(); // li
57 - 517
            // bugbug too many videos don't run embedded
518
            // $videoParam = basename($video->uri);
519
            // $videoIdx = strpos($videoParam, "=") + 1;
56 - 520
            // $str .= "<iframe width=\"280\" height=\"158\" src=\"https://www.youtube-nocookie.com/embed/" . substr($videoParam, $videoIdx) . "?rel=0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>";
132 - 521
 
45 - 522
        }
57 - 523
 
127 - 524
        $xhmod->close(); // ul
525
        $xhmod->close(); // div
526
        $xhmod->add_attribute("class", "modal-footer bg-secondary justify-content-between");
527
        $xhmod->tag('div');
528
        $xhmod->add_attribute("class", "font-weight-lighter small");
529
        $xhmod->tag('span');
530
        $xhmod->tag('span', "Data provided by ");
531
 
134 - 532
        $xhmod->add_attribute("id", "discogsRedirect2" . $cnt);
127 - 533
        $xhmod->add_attribute("href", (string)$master->{'uri'});
534
        $xhmod->add_attribute("target", "_blank");
535
        $xhmod->add_attribute("rel", "noreferrer noopener");
536
        $xhmod->tag('a', "Discogs");
537
        $xhmod->close(); // span
538
 
539
        $xhmod->add_attribute("class", "float-right");
540
        $xhmod->tag('span');
541
        $xhmod->add_attribute("type", "button");
542
        $xhmod->add_attribute("class", "btn btn-danger");
543
        $xhmod->add_attribute("data-dismiss", "modal");
544
        $xhmod->tag('button', "Close");
545
        $xhmod->close(); // span
546
        $xhmod->close(); // div
547
        $xhmod->close(); // div
548
        $xhmod->close(); // div
549
        $xhmod->close(); // div
45 - 550
    }
551
 
141 - 552
    $xh->close(); // div
62 - 553
 
125 - 554
    $searchTitle = 'Searching for:<br><br><strong>' . htmlentities((string)$master->{'title'}) . ' by ' . (empty($searchArtists) ? 'Various Artists' : htmlentities($searchArtists)) . '</strong>';
50 - 555
    if (!empty($barcode)) {
556
        $searchTitle .= " (" . displayBarcode($barcode) . ")";
557
    }
558
 
141 - 559
    $xh->add_attribute("class", "col-6 btn-group p-0 m-0");
560
    $xh->tag('div');
127 - 561
    $xh->add_attribute("id", "discogsSearch" . $cnt . "Btn");
562
    $xh->add_attribute("type", "submit");
134 - 563
    $xh->add_attribute("name", "submitBtn");
127 - 564
    $xh->add_attribute("value", "discogsSearch");
565
    $xh->add_attribute("class", "btn btn-success btn-sm");
566
    $xh->add_attribute("title", "Search for Store Offers");
567
    $xh->add_attribute("aria-label", "Search for Store Offers");
568
    $xh->add_attribute("data-toggle", "tooltip");
130 - 569
    $xh->add_attribute("data-title", htmlentities((string)$master->{"title"}));
570
    $xh->add_attribute("data-artist", htmlentities($searchArtists));
571
    $xh->add_attribute("data-barcode", $barcode);
572
    $xh->add_attribute("data-search-title", $searchTitle);
127 - 573
    $xh->tag('button');
141 - 574
    $xh->add_attribute("class", "material-icons material-text");
127 - 575
    $xh->tag('i', "search");
141 - 576
    $xh->tag('span', "Offer");
127 - 577
    $xh->close(); // button
578
    $xh->close(); // div
132 - 579
 
127 - 580
    $xh->add_attribute("id", "wishlistAdd" . $cnt);
581
    $xh->tag('span', "");
582
    $xh->close(); // div
14 - 583
 
127 - 584
    $xhmod->add_attribute("id", "masterModal" . $cnt);
585
    $xhmod->add_attribute("class", "modal");
586
    $xhmod->tag('div');
587
    $xhmod->add_attribute("class", "modal-dialog");
588
    $xhmod->tag('div');
589
    $xhmod->add_attribute("class", "modal-content");
590
    $xhmod->tag('div');
591
    $xhmod->add_attribute("class", "modal-header bg-secondary");
592
    $xhmod->tag('div');
20 - 593
    if (count($artists) < 5) {
127 - 594
        $str = htmlentities(join(", ", $artists));
65 - 595
    }
596
    else {
127 - 597
        $str = "Various Artists";
14 - 598
    }
127 - 599
    $xhmod->add_attribute("class", "modal-title mx-auto display-6");
149 - 600
    $xhmod->tag('p', htmlentities((string)$master->{'title'}) . (!empty($str) ? " by " . $str : ""));
127 - 601
    $xhmod->add_attribute("type", "button");
602
    $xhmod->add_attribute("class", "close");
603
    $xhmod->add_attribute("data-dismiss", "modal");
604
    $xhmod->tag('button');
605
    $xhmod->add_attribute("class", "material-icons btn-dismiss");
606
    $xhmod->tag('i', "cancel_presentation");
607
    $xhmod->close(); // button
608
    $xhmod->close(); // div
14 - 609
 
127 - 610
    $xhmod->add_attribute("class", "modal-body mx-auto");
611
    $xhmod->tag('div');
14 - 612
 
72 - 613
    if (!empty($coverImage) && !preg_match("/spacer.gif$/", $coverImage)) {
141 - 614
        $xhmod->add_attribute("class", "img-fluid mx-auto mb-4 lazyload");
127 - 615
        $xhmod->add_attribute("src", $coverImage);
616
        $xhmod->add_attribute("alt", "Discogs Cover");
617
        $xhmod->single_tag('img');
43 - 618
    }
619
 
127 - 620
    $xhmod->add_attribute("class", "table-borderless table-condensed small mx-auto");
621
    $xhmod->tag('table');
14 - 622
 
127 - 623
    $xhmod->tag('tr');
624
    $xhmod->add_attribute("class", "px-1");
625
    $xhmod->tag('td', "Title:");
626
    $xhmod->add_attribute("class", "px-1");
627
    $xhmod->tag('td', htmlentities((string)$master->{'title'}));
628
    $xhmod->close(); // tr
14 - 629
 
127 - 630
    $xhmod->tag('tr');
631
    $xhmod->add_attribute("class", "px-1");
632
    $xhmod->tag('td', "Artist:");
633
    $xhmod->add_attribute("class", "px-1");
634
    $xhmod->tag('td', htmlentities(join(", ", $artists)));
635
    $xhmod->close(); // tr
636
 
50 - 637
    if (!empty($barcode)) {
127 - 638
        $xhmod->tag('tr');
639
        $xhmod->add_attribute("class", "px-1");
640
        $xhmod->tag('td', "Barcode:");
641
        $xhmod->add_attribute("class", "px-1");
642
        $xhmod->tag('td', displayBarcode($barcode));
643
        $xhmod->close(); // tr
50 - 644
    }
645
 
20 - 646
    if (!empty($labels)) {
127 - 647
        $xhmod->tag('tr');
648
        $xhmod->add_attribute("class", "px-1");
649
        $xhmod->tag('td', "Label:");
650
        $xhmod->add_attribute("class", "px-1");
651
        $xhmod->tag('td', htmlentities($labels[0]));
652
        $xhmod->close(); // tr
20 - 653
    }
654
 
655
    if (!empty($formats)) {
127 - 656
        $xhmod->tag('tr');
657
        $xhmod->add_attribute("class", "px-1");
658
        $xhmod->tag('td', "Format:");
659
        $xhmod->add_attribute("class", "px-1");
660
        $xhmod->tag('td', htmlentities(join(", ", $formats)));
661
        $xhmod->close(); // tr
20 - 662
    }
663
 
664
    if (!empty($country)) {
127 - 665
        $xhmod->tag('tr');
666
        $xhmod->add_attribute("class", "px-1");
667
        $xhmod->tag('td', "Country:");
668
        $xhmod->add_attribute("class", "px-1");
669
        $xhmod->tag('td', htmlentities($country));
670
        $xhmod->close(); // tr
20 - 671
    }
672
 
14 - 673
    if ($master->{'year'} > 0) {
127 - 674
        $xhmod->tag('tr');
675
        $xhmod->add_attribute("class", "px-1");
676
        $xhmod->tag('td', "Year:");
677
        $xhmod->add_attribute("class", "px-1");
678
        $xhmod->tag('td', htmlentities((string)$master->{'year'}));
679
        $xhmod->close(); // tr
14 - 680
    }
681
 
127 - 682
    $xhmod->tag('tr');
683
    $xhmod->add_attribute("class", "px-1");
684
    $xhmod->tag('td', "Genre:");
685
    $xhmod->add_attribute("class", "px-1");
686
    $xhmod->tag('td', htmlentities(join(", ", $genres)));
687
    $xhmod->close(); // tr
14 - 688
 
10 - 689
    if (isset($master->{'styles'})) {
690
        $styles = [];
691
        foreach ($master->{'styles'} as $key => $value) {
692
            $styles[] = $value;
693
        }
127 - 694
        $xhmod->tag('tr');
695
        $xhmod->add_attribute("class", "px-1");
696
        $xhmod->tag('td', "Style:");
697
        $xhmod->add_attribute("class", "px-1");
698
        $xhmod->tag('td', htmlentities(join(", ", $styles)));
699
        $xhmod->close(); // tr
10 - 700
    }
14 - 701
 
149 - 702
    if (isset($master->{'tracklist'}) && is_array($master->{'tracklist'})) {
127 - 703
        $xhmod->tag('tr');
704
        $xhmod->add_attribute("class", "px-1");
705
        $xhmod->add_attribute("colspan", "2");
706
        $xhmod->tag('td', "Tracks:");
707
        $xhmod->close(); // tr
708
 
709
        $xhmod->tag('tr');
710
        $xhmod->add_attribute("class", "px-1");
711
        $xhmod->add_attribute("colspan", "2");
712
        $xhmod->tag('td');
713
        $xhmod->add_attribute("class", "pl-3 pt-0 small list-unstyled");
714
        $xhmod->tag('ul');
14 - 715
        foreach ($master->{'tracklist'} as $key => $value) {
20 - 716
            if ((string)$value->{'type_'} == "heading") {
127 - 717
                $xhmod->add_attribute("class", "font-weight-bold");
718
                $xhmod->tag('li', htmlentities((string)$value->{'title'}));
65 - 719
            }
720
            else if ((string)$value->{'type_'} == "index") {
127 - 721
                $xhmod->add_attribute("class", "font-italic");
722
                $xhmod->tag('li');
723
                $xhmod->add_attribute("class", "font-italic");
724
                $xhmod->tag('span', htmlentities((string)$value->{'title'}));
14 - 725
                foreach ($value->{'sub_tracks'} as $subkey => $subvalue) {
127 - 726
                    $xhmod->add_attribute("class", "pl-3 pt-0 list-unstyled");
727
                    $xhmod->tag('ul');
728
                    $xhmod->insert_code(processTrack($subvalue, false));
729
                    $xhmod->close(); // ul
17 - 730
                }
127 - 731
                $xhmod->close(); // li
65 - 732
            }
733
            else if ((string)$value->{'type_'} == "track") {
127 - 734
                $xhmod->insert_code(processTrack($value, true));
14 - 735
            }
736
        }
127 - 737
        $xhmod->close(); // ul
738
        $xhmod->close(); // td
739
        $xhmod->close(); // tr
17 - 740
    }
14 - 741
 
127 - 742
    $xhmod->close(); // table
743
    $xhmod->close(); // div
744
    $xhmod->add_attribute("class", "modal-footer bg-secondary justify-content-between");
745
    $xhmod->tag('div');
746
    $xhmod->tag('span');
747
    $xhmod->tag('span', "Data provided by ");
134 - 748
    $xhmod->add_attribute("id", "discogsRedirect" . $cnt);
127 - 749
    $xhmod->add_attribute("href", (string)$master->{'uri'});
750
    $xhmod->add_attribute("target", "_blank");
751
    $xhmod->add_attribute("rel", "noreferrer noopener");
752
    $xhmod->tag('a', "Discogs");
753
    $xhmod->close(); // span
754
    $xhmod->add_attribute("class", "float-right");
755
    $xhmod->tag('span');
756
    $xhmod->add_attribute("type", "button");
757
    $xhmod->add_attribute("class", "btn btn-danger");
758
    $xhmod->add_attribute("data-dismiss", "modal");
759
    $xhmod->tag('button', "Close");
760
    $xhmod->close(); // span
761
    $xhmod->close(); // div
762
    $xhmod->close(); // div
763
    $xhmod->close(); // div
764
    $xhmod->close(); // div
10 - 765
 
127 - 766
    $html = $xhmod->flush();
767
    //error_log(print_r($html, 1));
768
 
769
    $xh->insert_code($html);
770
    $xh->close(); // div
771
 
772
    $html = $xh->flush();
773
    //error_log(print_r($html, 1));
774
 
775
    return $html;
14 - 776
}
10 - 777
 
14 - 778
function processTrack($value, $posFlag) {
127 - 779
    $xh = new Html;
780
    $xh->init($_SESSION["htmlIndent"]);
781
 
782
    $str = "";
783
 
14 - 784
    if ($posFlag && !empty($value->{'position'})) {
20 - 785
        if (!preg_match("/^[a-zA-Z][0-9]/", (string)$value->{'position'}) && !preg_match("/^[a-zA-Z]$/", (string)$value->{'position'})) {
786
            $str .= (string)$value->{'position'} . '. ';
10 - 787
        }
14 - 788
    }
17 - 789
 
20 - 790
    $str .= (string)$value->{'title'};
10 - 791
 
14 - 792
    $trackArtists = [];
793
    if (isset($value->{'artists'})) {
794
        foreach ($value->{'artists'} as $taKey => $taValue) {
20 - 795
            $trackArtists[] = trim(preg_replace('/\([0-9]+\)$/', "", (string)$taValue->{'name'}));
10 - 796
        }
14 - 797
        if (count($trackArtists)) {
798
            $str .= " - " . join(", ", $trackArtists);
799
        }
800
    }
10 - 801
 
14 - 802
    if (!empty($value->{'duration'})) {
20 - 803
        $str .= " [" . (string)$value->{'duration'} . "]";
10 - 804
    }
13 - 805
 
127 - 806
    $xh->tag('li', $str);
14 - 807
 
127 - 808
    $html = $xh->flush();
809
    //error_log(print_r($html, 1));
810
 
811
    return $html;
14 - 812
}
137 - 813
 
814
function skipDuplicateMaster($master, $processedMasters) {
815
    if ($master->{'master_id'} > 0 && in_array($master->{'master_id'}, array_column($processedMasters, "id"))) {
816
        return true;
817
    }
818
 
138 - 819
    $year = $master->{'year'} ?? 0;
820
 
137 - 821
    foreach ($processedMasters as $v) {
822
        $s1 = strtolower($master->{'title'});
823
        $s2 = strtolower($v['title']);
824
        $sim = similar_text($s1, $s2, $perc);
138 - 825
        if ($perc > 85.00 && $year == $v["year"]) {
137 - 826
//error_log($master->{'title'} . " === " . $v['title']);
827
//error_log("similarity: $sim (" . number_format($perc, 2) . "%)");
828
            return true;
829
        }
830
    }
831
 
832
    return false;
833
}