Subversion Repositories cheapmusic

Rev

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