Subversion Repositories cheapmusic

Rev

Rev 150 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
error_reporting(E_ALL);

// BUGBUG {"message": "You are making requests too quickly."}
function findDiscogsMaster($random = false) {
    $vendors = Vendors::getInstance();
    $config = $vendors->getVendor(Vendors::DISCOGS);
    $maxMasterCount = $config['maxMasters'];

    $_SESSION["discogs"] = "";
    $stxt = $_SESSION["searchTerm"];

    $params = [];
    $params["token"] = $config['token'];
    $params["type"] = "master";

    if (!empty($_SESSION["advSearch"])) {
        $stxt = '';
        if (!empty($_SESSION["advSearch"]["Title"])) {
            $params["release_title"] = $_SESSION["advSearch"]["Title"];
            $stxt .= $_SESSION["advSearch"]["Title"];
        }
        if (!empty($_SESSION["advSearch"]["Artist"])) {
            $params["artist"] = $_SESSION["advSearch"]["Artist"];
            $stxt .= $_SESSION["advSearch"]["Artist"];
        }
        if (!empty($_SESSION["advSearch"]["Track"])) {
            $params["track"] = $_SESSION["advSearch"]["Track"];
            $stxt .= $_SESSION["advSearch"]["Track"];
            $params["type"] = "release";
        }
        if (!empty($_SESSION["advSearch"]["Barcode"])) {
            $params["barcode"] = $_SESSION["advSearch"]["Barcode"];
            $stxt .= $_SESSION["advSearch"]["Barcode"];
            $params["type"] = "release";
        }
        if (!empty($_SESSION["advSearch"]["Format"])) {
            $params["format"] = $_SESSION["advSearch"]["Format"];
            $stxt .= $_SESSION["advSearch"]["Format"];
            $params["type"] = "release";
        }
        if (!empty($_SESSION["advSearch"]["Catno"])) {
            $params["catno"] = $_SESSION["advSearch"]["Catno"];
            $stxt .= $_SESSION["advSearch"]["Catno"];
            $params["type"] = "release";
        }
        if (!empty($_SESSION["advSearch"]["Label"])) {
            $params["label"] = $_SESSION["advSearch"]["Label"];
            $stxt .= $_SESSION["advSearch"]["Label"];
        }
        if (!empty($_SESSION["advSearch"]["Country"])) {
            $params["country"] = $_SESSION["advSearch"]["Country"];
            $stxt .= $_SESSION["advSearch"]["Country"];
            $params["type"] = "release";
        }
        if (!empty($_SESSION["advSearch"]["Year"])) {
            $params["year"] = $_SESSION["advSearch"]["Year"];
            $stxt .= $_SESSION["advSearch"]["Year"];
        }
    } else {
        $params["query"] = ($random ? mt_rand($config['minRelease'], $config['maxRelease']) : $stxt);
    }

    $pairs = array();
    foreach ($params as $key => $value) {
        array_push($pairs, rawurlencode($key)."=".rawurlencode($value));
    }
    $canonical_query_string = join("&", $pairs);

    $url = "https://api.discogs.com/database/search?" . $canonical_query_string;
    $searchResp = ($random ? false : getSearchCache("Discogs", $stxt, ""));
    if ($searchResp === false) {
        $searchResp = getUrl($url, $config['userAgent']);
        if (!$random) {
            saveSearchCache("Discogs", $stxt, "", $searchResp);
        }
    }
    $searchResp = utf8_encode($searchResp);
    $searchResp = json_decode($searchResp);

    if (empty($searchResp)) {
        return;
    }

    if (!empty($searchResp->{'message'})) {
        if ($searchResp->{'message'} == "You are making requests too quickly.") {
            my_error_log("Discogs: You are making requests too quickly.");
            return;
        }
        return;
    }

    if ($searchResp->{'pagination'}->{'items'} < 1) {
        if ($random) { // must find something
            findDiscogsMaster(true);
        }

        return;
    }

    $noResults = $searchResp->{'pagination'}->{'items'};

    $xh = new Html;
    $xh->init($_SESSION["htmlIndent"]);

    $xh->add_attribute("class", "container-fluid bg-light");
    $xh->add_attribute("id", "discogsTable");
    $xh->tag('div');
    $xh->add_attribute("class", "text-center py-2");
    $xh->tag('h2', ($random ? "Random" : "Matching") . " Albums");
    $xh->add_attribute("id", "discogsDeckForm");
    $xh->add_attribute("method", "post");
    $xh->add_attribute("action", "/index.php");
    $xh->tag('form');
    $xh->insert_code(inputSessionTab());
    $xh->insert_code(inputNonce());
    $xh->add_attribute("id", "discogsTitle");
    $xh->add_attribute("type", "hidden");
    $xh->add_attribute("name", "discogsTitle");
    $xh->add_attribute("value", "");
    $xh->single_tag('input');
    $xh->add_attribute("id", "discogsArtist");
    $xh->add_attribute("type", "hidden");
    $xh->add_attribute("name", "discogsArtist");
    $xh->add_attribute("value", "");
    $xh->single_tag('input');
    $xh->add_attribute("id", "discogsBarcode");
    $xh->add_attribute("type", "hidden");
    $xh->add_attribute("name", "discogsBarcode");
    $xh->add_attribute("value", "");
    $xh->single_tag('input');
    $xh->add_attribute("type", "hidden");
    $xh->add_attribute("name", "submitBtn");
    $xh->add_attribute("value", "discogsSearch");
    $xh->single_tag('input');
    $xh->add_attribute("id", "discogsDeck");
    $xh->add_attribute("class", "card-deck");
    $xh->tag('div');

    $masterCount = 0;
    $processedMasters = [];
    $extraMasters = [];
    $searchUrls = [];
    $masterResps_cache = [];
    foreach ($searchResp->{'results'} as $searchey => $searchValue) {
        if (!in_array($searchValue->{'type'}, array("master", "release")) && (empty($_SESSION["advSearch"]["Barcode"]))) {
            continue;
        }

        if (empty($_SESSION["advSearch"]["Barcode"]) && $searchValue->{'type'} == "release" && $searchValue->{'master_id'} > 0) {
            continue;
        }

        if (skipDuplicateMaster($searchValue, $processedMasters)) {
            continue;
        }

        if (++$masterCount > $maxMasterCount) {
            $pos = strpos($searchValue->{'title'}, " - ");
            if ($pos > 0) {
                $artist = substr($searchValue->{'title'}, 0, $pos);
                $title = substr($searchValue->{'title'}, $pos+3);
            } else {
                $artist = "";
                $title = $searchValue->{'title'};
            }
            $extraMasters[] = array( "id" => $searchValue->{'master_id'},
                                     "type" => $searchValue->{'type'},
                                     "title" => $title,
                                     "artists" => array(array("name" => $artist)),
                                     "styles" => $searchValue->{'style'} ? $searchValue->{'style'} : [],
                                     "genres" => $searchValue->{'genre'} ? $searchValue->{'genre'} : [],
                                     "thumb" => $searchValue->{'thumb'} ?? "",
                                     "coverImage" => $searchValue->{'cover_image'} ?? "",
                                     "uri" => "https://www.discogs.com" . $searchValue->{'uri'} ?? "",
                                     "year" => $searchValue->{'year'} ?? 0 );
            continue;
        }

        $processedMasters[] = array( 'id' => $searchValue->{'master_id'}, "title" => $searchValue->{'title'}, "year" => $searchValue->{'year'} ?? 0 );

        $searchUrl = $searchValue->{'master_id'} != 0 ? $searchValue->{'master_url'} : $searchValue->{'resource_url'};

        $searchType[$searchUrl] = $searchValue->{'type'};
        $searchThumb[$searchUrl] = $searchValue->{'thumb'};
        $searchCoverImage[$searchUrl] = $searchValue->{'cover_image'};

        $masterResps_cache[$searchUrl] = getSearchCache("Discogs", $searchUrl, "");
        if ($masterResps_cache[$searchUrl] === false) {
            $searchUrls[] = $searchUrl;
            unset($masterResps_cache[$searchUrl]);
        }
    }

    $masterResps = [];
    if (count($searchUrls) > 0) {
        $masterResps = getMultiUrl($searchUrls, $config['userAgent']);
    }

    foreach($masterResps as $key => $html) {
        saveSearchCache("Discogs", $key, "", $html);
    }

    foreach($masterResps_cache as $key => $html) {
        $masterResps[$key] = $html;
    }
    unset($masterResps_cache);

    $masterCount = 0;
    foreach ($masterResps as $key => $html) {
        $html = utf8_encode($html);
        $html = json_decode($html);

        if (!empty($html->{'message'})) {
            my_error_log("Discogs: " . $html->{'message'});
        } else if (!empty($html)) {
            $xh->insert_code(processMaster($html, $searchType[$key], $searchThumb[$key], $searchCoverImage[$key], ++$masterCount));
        }
    }

    foreach ($extraMasters as $html) {
        $object = json_decode(json_encode($html), false);
        $xh->insert_code(processMaster($object, $html["type"], $html["thumb"], $html["coverImage"], ++$masterCount));
    }

    if ($masterCount == 0) {
        $xh->reset();
    } else {
        $xh->close(); // div
        $xh->close(); // form
        $xh->close(); // div

        $xh->insert_code(discogsEvents());

        if ($random) {
            $xh->add_attribute("class", "container-fluid text-center");
            $xh->tag('div');
              $xh->add_attribute("method", "post");
              $xh->add_attribute("action", "/index.php");
              $xh->tag('form');
                $xh->insert_code(inputSessionTab());
                $xh->insert_code(inputSearchTerm());
                $xh->insert_code(inputNonce());
                $xh->add_attribute("id", "randomBtn");
                $xh->add_attribute("type", "submit");
                $xh->add_attribute("class", "btn btn-success m-2 rounded");
                $xh->add_attribute("name", "submitBtn");
                $xh->add_attribute("value", "random");
                $xh->tag('button', "More Random Album Suggestions");
              $xh->close(); // form
            $xh->close(); // div
            $xh->add_attribute("nonce", "xxxNONCExxx");
            $xh->tag('script');
                $str  = my_trim('document.addEventListener("DOMContentLoaded", function() {');
                $str .= my_trim('    document.getElementById("randomBtn").addEventListener("click", function(event) {');
                $str .= my_trim('        window.dataLayer.push({ "event" : "trackEvent", "eventCategory" : "Random Album", "eventAction" : "Click", "eventLabel" : "More"});');
                $str .= my_trim('    });');
                $str .= my_trim('});');
            $xh->insert_code($str);
            $xh->close(); // script
        }
    }

    $_SESSION["discogs"] = $xh->flush();
    //error_log(print_r($_SESSION["discogs"], 1));

    return;
}

function discogsEvents() {
    $xh = new Html;
    $xh->init($_SESSION["htmlIndent"]);

    $xh->add_attribute("nonce", "xxxNONCExxx");
    $xh->tag('script');
        $str  = my_trim('document.addEventListener("DOMContentLoaded", function() {');
        $str .= my_trim('  document.getElementById("discogsDeckForm").addEventListener("submit", function(event) {');
        $str .= my_trim('      if (window.google_tag_manager && window.ga && ga.create) {');
        $str .= my_trim('          event.preventDefault();');
        $str .= my_trim('          var title = document.getElementById("discogsTitle").value;');
        $str .= my_trim('          var artist = document.getElementById("discogsArtist").value;');
        $str .= my_trim('          window.dataLayer.push({ "event" : "search", "search_term" : title + " by " + (artist.length == 0 ? "Various Artists" : artist), "eventCallback": function () {event.target.submit();}});');
        $str .= my_trim('      }');
        $str .= my_trim('  });');
        $str .= my_trim('  document.getElementById("discogsDeck").addEventListener("click", function(event) {');
        $str .= my_trim('       var e = event.target.closest("button") || event.target.closest("a");');
        $str .= my_trim('       if (e && e.id.startsWith("discogsSearch")) {');
        $str .= my_trim('           document.getElementById("discogsTitle").value = e.getAttribute("data-title");');
        $str .= my_trim('           document.getElementById("discogsArtist").value = e.getAttribute("data-artist");');
        $str .= my_trim('           document.getElementById("discogsBarcode").value = e.getAttribute("data-barcode");');
        $str .= my_trim('           progressBar(e.getAttribute("data-search-title"));');
        $str .= my_trim('       } else if (e && e.id.startsWith("wl")) {');
        $str .= my_trim('           var user = e.getAttribute("data-user");');
        $str .= my_trim('           var cnt = e.getAttribute("data-cnt");');
        $str .= my_trim('           var wl = e.getAttribute("data-wl");');
        $str .= my_trim('           window.dataLayer.push({ "event" : "trackEvent", "eventCategory" : "Bookmark", "eventAction" : "Add", "eventLabel" : ""});');
        $str .= my_trim('           addWishlist(user, e, cnt, wl);');
        $str .= my_trim('       } else if (e && e.id.startsWith("discogsInfo")) {');
        $str .= my_trim('           window.dataLayer.push({ "event" : "trackEvent", "eventCategory" : "Album Info", "eventAction" : "Click", "eventLabel" : ""});');
        $str .= my_trim('       } else if (e && e.id.startsWith("discogsVideo")) {');
        $str .= my_trim('           window.dataLayer.push({ "event" : "trackEvent", "eventCategory" : "Video Info", "eventAction" : "Click", "eventLabel" : ""});');
        $str .= my_trim('       } else if (e && e.id.startsWith("discogsRedirect")) {');
        $str .= my_trim('           window.dataLayer.push({ "event" : "trackEvent", "eventCategory" : "Discogs", "eventAction" : "Click", "eventLabel" : "Detailed Data"});');
        $str .= my_trim('       } else if (e && e.id.startsWith("discogsYTRedirect")) {');
        $str .= my_trim('           window.dataLayer.push({ "event" : "trackEvent", "eventCategory" : "Video", "eventAction" : "Play", "eventLabel" : "Youtube"});');
        $str .= my_trim('       }');
        $str .= my_trim('  });');
        $str .= my_trim('});');
    $xh->insert_code($str);
    $xh->close(); // script

    $html = $xh->flush();
    //error_log(print_r($html, 1));

    return $html;
}

function processMaster($master, $type, $thumbnail, $coverImage, $cnt) {
    $xh = new Html;
    $xh->init($_SESSION["htmlIndent"]);
    $xhmod = new Html;
    $xhmod->init($_SESSION["htmlIndent"]);

    $artists = [];
    if (isset($master->{'artists'})) {
        foreach ($master->{'artists'} as $key => $value) {
            $artists[] = trim(preg_replace('/\([0-9]+\)$/', "", (string)$value->{'name'}));
        }
    }

    $genres = [];
    if (isset($master->{'genres'})) {
        foreach ($master->{'genres'} as $key => $value) {
            $genres[] = (string)$value;
        }
    }

    $labels = [];
    if (isset($master->{'labels'})) {
        foreach ($master->{'labels'} as $key => $value) {
            $labels[] = trim(preg_replace('/\([0-9]+\)$/', "", (string)$value->{'name'}));
        }
    }

    $formats = [];
    if (isset($master->{'formats'})) {
        foreach ($master->{'formats'} as $key => $value) {
            $formats[] = trim(preg_replace('/\([0-9]+\)$/', "", (string)$value->{'name'}));
        }
    }

    $country = '';
    if (isset($master->{'country'})) {
        $country = (string)$master->{'country'};
    }

    $xh->add_attribute("class", "card mx-auto discogs-card d-none");
    $xh->tag('div');

    $xh->add_attribute("class", "card-header bg-secondary d-flex");
    $xh->tag('div');
    $searchArtists = "";
    if (count($artists) < 5) {
        $wlArtists = join(", ", $artists);
        if (isset($artists[0]) && $artists[0] != 'Various') {
            $searchArtists = join(" ", $artists);
        }
    }
    else {
        $wlArtists = "Various Artists";
    }
    $xh->add_attribute("class", "card-title font-weight-bold small flex-grow-1");
    $xh->tag('p', htmlentities((string)$master->{'title'}) . (!empty($wlArtists) ? " by " . htmlentities($wlArtists) : ""));
    $xh->close(); // div

    if (empty($thumbnail) || preg_match("/spacer.gif$/", $thumbnail)) {
        $thumbnail = "images/no-image-available.jpg";
    }
    $xh->add_attribute("class", "card-body d-flex justify-content-center align-items-center p-1 m-0");
    $xh->tag('div');
    $xh->add_attribute("class", "btn p-0 m-0 lazyload lazypreload");
    $xh->add_attribute("src", $thumbnail);
    $xh->add_attribute("data-toggle", "modal");
    $xh->add_attribute("data-target", "#masterModal" . $cnt);
    $xh->add_attribute("title", "Album Information");
    $xh->add_attribute("data-toggle2", "tooltip");
    $xh->add_attribute("alt", "Discogs Cover Thumbnail");
    $xh->single_tag('img');
    $xh->close(); // div
    $xh->add_attribute("class", "card-footer form-row btn-group-sm bg-secondary p-0 m-0");
    $xh->tag('div');

    $xh->add_attribute("class", "col-3 btn-group p-0 m-0");
    $xh->tag('div');

    $barcode = "";
    if (empty($_SESSION["advSearch"]["Barcode"]) && !empty($master->{'identifiers'})) {
        foreach ($master->{'identifiers'} as $identifier) {
            if ($identifier->{'type'} == 'Barcode') {
                $tmpBarcode = preg_replace("/[^0-9]/", "", $identifier->{'value'});
                $barcodeType = clsLibGTIN::GTINCheck($tmpBarcode, false, 1);
                if (!empty($barcodeType)) {
                    $barcode = $tmpBarcode;
                }
            }
        }
    }
    else if (!empty($_SESSION["advSearch"]["Barcode"])) {
        $barcode = $_SESSION["advSearch"]["Barcode"];
    }

    if (isLoggedIn()) {
        $checkWlFlag = checkWishlist($type, $master->{'id'});
        $wlArr = array(
            ($type == "master" ? 'mid' : 'rid') => $master->{'id'},
            'title' => (string)$master->{'title'},
            'artist' => $wlArtists,
            'barcode' => $barcode,
            'thumbnail' => $thumbnail,
            'url' => (string)$master->{'uri'}
        );
        $wl = base64_encode(json_encode($wlArr));

        if (!$checkWlFlag) {
            $xh->add_attribute("id", "wl" . $cnt . "Btn");
            $xh->add_attribute("data-user", $_SESSION['sessData']['userID']);
            $xh->add_attribute("data-cnt", $cnt);
            $xh->add_attribute("data-wl", $wl);
        }
        $xh->add_attribute("type", "button");
        $xh->add_attribute("class", "btn btn-info btn-sm");
        $xh->add_attribute("title", $checkWlFlag ? "Already on Wishlist" : "Add to Wishlist");
        $xh->add_attribute("aria-label", $checkWlFlag ? "Already on Wishlist" : "Add to Wishlist");
        $xh->add_attribute("data-toggle", "tooltip");
        $xh->tag('button');
        $xh->add_attribute("class", "material-icons");
        $xh->tag('i', $checkWlFlag ? "library_add_check" : "library_add");
        $xh->close(); // button
    }

    $xh->close(); // div

    $xh->add_attribute("class", "col-3 btn-group p-0 m-0");
    $xh->tag('div');

    if (isset($master->{'videos'})) {
        $xh->add_attribute("id", "discogsVideo" . $cnt);
        $xh->add_attribute("type", "button");
        $xh->add_attribute("class", "btn btn-info btn-sm");
        $xh->add_attribute("data-toggle", "modal");
        $xh->add_attribute("data-target", "#videoModal" . $cnt);
        $xh->add_attribute("title", "Videos");
        $xh->add_attribute("aria-label", "Music videos");
        $xh->add_attribute("data-toggle2", "tooltip");
        $xh->tag('button');
        $xh->add_attribute("class", "material-icons");
        $xh->tag('i', "videocam");
        $xh->close(); // button

        $xhmod->add_attribute("id", "videoModal" . $cnt);
        $xhmod->add_attribute("class", "modal");
        $xhmod->tag('div');
        $xhmod->add_attribute("class", "modal-dialog");
        $xhmod->tag('div');
        $xhmod->add_attribute("class", "modal-content");
        $xhmod->tag('div');
        $xhmod->add_attribute("class", "modal-header bg-secondary");
        $xhmod->tag('div');
        if (count($artists) < 5) {
            $str = htmlentities(join(", ", $artists));
        }
        else {
            $str = "Various Artists";
        }
        $xhmod->add_attribute("class", "modal-title mx-auto display-6");
        $xhmod->tag('p', htmlentities((string)$master->{'title'}) . !empty($str) ? " by " . $str : "");

        $xhmod->add_attribute("type", "button");
        $xhmod->add_attribute("class", "close");
        $xhmod->add_attribute("data-dismiss", "modal");
        $xhmod->tag('button');
        $xhmod->add_attribute("class", "material-icons btn-dismiss");
        $xhmod->tag('i', "cancel_presentation");
        $xhmod->close(); // button
        $xhmod->close(); // div

        $xhmod->add_attribute("class", "modal-body mx-auto");
        $xhmod->tag('div');
        $xhmod->add_attribute("class", "display-6");
        $xhmod->tag('p', "Videos");
        $xhmod->add_attribute("class", "list-group");
        $xhmod->tag('ul');

        $cnt2 = 0;
        foreach ($master->{'videos'} as $video) {
            $xhmod->add_attribute("class", "list-group-item");
            $xhmod->tag('li');
            $xhmod->add_attribute("class", "row");
            $xhmod->tag('div');
            $xhmod->add_attribute("class", "col-1");
            $xhmod->tag('div');
            $xhmod->add_attribute("class", "svg-yt");
            $xhmod->add_attribute("viewBox", "0 0 24 24");
            $xhmod->tag('svg');
            $xhmod->add_attribute("fill", "currentColor");
            $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");
            $xhmod->single_tag('path');
            $xhmod->close(); // svg
            $xhmod->close(); // div
            $xhmod->add_attribute("class", "col");
            $xhmod->tag('div');
            $xhmod->add_attribute("href", $video->uri);
            $xhmod->add_attribute("id", "discogsYTRedirect" . $cnt . ++$cnt2);
            $xhmod->add_attribute("target", "_blank");
            $xhmod->add_attribute("rel", "noreferrer noopener");
            $xhmod->add_attribute("class", "btn btn-light btn-link text-left");
            $xhmod->add_attribute("role", "button");
            $xhmod->tag('a', htmlentities($video->title) . " [" . gmdate('H:i:s', $video->duration) . "]");
            $xhmod->close(); // div
            $xhmod->close(); // div
            $xhmod->close(); // li
            // bugbug too many videos don't run embedded
            // $videoParam = basename($video->uri);
            // $videoIdx = strpos($videoParam, "=") + 1;
            // $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>";

        }

        $xhmod->close(); // ul
        $xhmod->close(); // div
        $xhmod->add_attribute("class", "modal-footer bg-secondary justify-content-between");
        $xhmod->tag('div');
        $xhmod->add_attribute("class", "font-weight-lighter small");
        $xhmod->tag('span');
        $xhmod->tag('span', "Data provided by ");

        $xhmod->add_attribute("id", "discogsRedirectTwo" . $cnt);
        $xhmod->add_attribute("href", (string)$master->{'uri'});
        $xhmod->add_attribute("target", "_blank");
        $xhmod->add_attribute("rel", "noreferrer noopener");
        $xhmod->tag('a', "Discogs");
        $xhmod->close(); // span

        $xhmod->add_attribute("class", "float-right");
        $xhmod->tag('span');
        $xhmod->add_attribute("type", "button");
        $xhmod->add_attribute("class", "btn btn-danger");
        $xhmod->add_attribute("data-dismiss", "modal");
        $xhmod->tag('button', "Close");
        $xhmod->close(); // span
        $xhmod->close(); // div
        $xhmod->close(); // div
        $xhmod->close(); // div
        $xhmod->close(); // div
    }

    $xh->close(); // div

    $searchTitle = 'Searching for:<br><br><strong>' . htmlentities((string)$master->{'title'}) . ' by ' . (empty($searchArtists) ? 'Various Artists' : htmlentities($searchArtists)) . '</strong>';
    if (!empty($barcode)) {
        $searchTitle .= " (" . displayBarcode($barcode) . ")";
    }

    $xh->add_attribute("class", "col-6 btn-group p-0 m-0");
    $xh->tag('div');
    $xh->add_attribute("id", "discogsSearch" . $cnt . "Btn");
    $xh->add_attribute("type", "submit");
    $xh->add_attribute("name", "submitBtn");
    $xh->add_attribute("value", "discogsSearch");
    $xh->add_attribute("class", "btn btn-success btn-sm");
    $xh->add_attribute("title", "Search for Store Offers");
    $xh->add_attribute("aria-label", "Search for Store Offers");
    $xh->add_attribute("data-toggle", "tooltip");
    $xh->add_attribute("data-title", htmlentities((string)$master->{"title"}));
    $xh->add_attribute("data-artist", htmlentities($searchArtists));
    $xh->add_attribute("data-barcode", $barcode);
    $xh->add_attribute("data-search-title", $searchTitle);
    $xh->tag('button');
    $xh->add_attribute("class", "material-icons material-text");
    $xh->tag('i', "search");
    $xh->tag('span', "Offer");
    $xh->close(); // button
    $xh->close(); // div

    $xh->add_attribute("id", "wishlistAdd" . $cnt);
    $xh->tag('span', "");
    $xh->close(); // div

    $xhmod->add_attribute("id", "masterModal" . $cnt);
    $xhmod->add_attribute("class", "modal");
    $xhmod->tag('div');
    $xhmod->add_attribute("class", "modal-dialog");
    $xhmod->tag('div');
    $xhmod->add_attribute("class", "modal-content");
    $xhmod->tag('div');
    $xhmod->add_attribute("class", "modal-header bg-secondary");
    $xhmod->tag('div');
    if (count($artists) < 5) {
        $str = htmlentities(join(", ", $artists));
    }
    else {
        $str = "Various Artists";
    }
    $xhmod->add_attribute("class", "modal-title mx-auto display-6");
    $xhmod->tag('p', htmlentities((string)$master->{'title'}) . (!empty($str) ? " by " . $str : ""));
    $xhmod->add_attribute("type", "button");
    $xhmod->add_attribute("class", "close");
    $xhmod->add_attribute("data-dismiss", "modal");
    $xhmod->tag('button');
    $xhmod->add_attribute("class", "material-icons btn-dismiss");
    $xhmod->tag('i', "cancel_presentation");
    $xhmod->close(); // button
    $xhmod->close(); // div

    $xhmod->add_attribute("class", "modal-body mx-auto");
    $xhmod->tag('div');

    if (!empty($coverImage) && !preg_match("/spacer.gif$/", $coverImage)) {
        $xhmod->add_attribute("class", "img-fluid mx-auto mb-4 lazyload");
        $xhmod->add_attribute("src", $coverImage);
        $xhmod->add_attribute("alt", "Discogs Cover");
        $xhmod->single_tag('img');
    }

    $xhmod->add_attribute("class", "table-borderless table-condensed small mx-auto");
    $xhmod->tag('table');

    $xhmod->tag('tr');
    $xhmod->add_attribute("class", "px-1");
    $xhmod->tag('td', "Title:");
    $xhmod->add_attribute("class", "px-1");
    $xhmod->tag('td', htmlentities((string)$master->{'title'}));
    $xhmod->close(); // tr

    $xhmod->tag('tr');
    $xhmod->add_attribute("class", "px-1");
    $xhmod->tag('td', "Artist:");
    $xhmod->add_attribute("class", "px-1");
    $xhmod->tag('td', htmlentities(join(", ", $artists)));
    $xhmod->close(); // tr

    if (!empty($barcode)) {
        $xhmod->tag('tr');
        $xhmod->add_attribute("class", "px-1");
        $xhmod->tag('td', "Barcode:");
        $xhmod->add_attribute("class", "px-1");
        $xhmod->tag('td', displayBarcode($barcode));
        $xhmod->close(); // tr
    }

    if (!empty($labels)) {
        $xhmod->tag('tr');
        $xhmod->add_attribute("class", "px-1");
        $xhmod->tag('td', "Label:");
        $xhmod->add_attribute("class", "px-1");
        $xhmod->tag('td', htmlentities($labels[0]));
        $xhmod->close(); // tr
    }

    if (!empty($formats)) {
        $xhmod->tag('tr');
        $xhmod->add_attribute("class", "px-1");
        $xhmod->tag('td', "Format:");
        $xhmod->add_attribute("class", "px-1");
        $xhmod->tag('td', htmlentities(join(", ", $formats)));
        $xhmod->close(); // tr
    }

    if (!empty($country)) {
        $xhmod->tag('tr');
        $xhmod->add_attribute("class", "px-1");
        $xhmod->tag('td', "Country:");
        $xhmod->add_attribute("class", "px-1");
        $xhmod->tag('td', htmlentities($country));
        $xhmod->close(); // tr
    }

    if ($master->{'year'} > 0) {
        $xhmod->tag('tr');
        $xhmod->add_attribute("class", "px-1");
        $xhmod->tag('td', "Year:");
        $xhmod->add_attribute("class", "px-1");
        $xhmod->tag('td', htmlentities((string)$master->{'year'}));
        $xhmod->close(); // tr
    }

    $xhmod->tag('tr');
    $xhmod->add_attribute("class", "px-1");
    $xhmod->tag('td', "Genre:");
    $xhmod->add_attribute("class", "px-1");
    $xhmod->tag('td', htmlentities(join(", ", $genres)));
    $xhmod->close(); // tr

    if (isset($master->{'styles'})) {
        $styles = [];
        foreach ($master->{'styles'} as $key => $value) {
            $styles[] = $value;
        }
        $xhmod->tag('tr');
        $xhmod->add_attribute("class", "px-1");
        $xhmod->tag('td', "Style:");
        $xhmod->add_attribute("class", "px-1");
        $xhmod->tag('td', htmlentities(join(", ", $styles)));
        $xhmod->close(); // tr
    }

    if (isset($master->{'tracklist'}) && is_array($master->{'tracklist'})) {
        $xhmod->tag('tr');
        $xhmod->add_attribute("class", "px-1");
        $xhmod->add_attribute("colspan", "2");
        $xhmod->tag('td', "Tracks:");
        $xhmod->close(); // tr

        $xhmod->tag('tr');
        $xhmod->add_attribute("class", "px-1");
        $xhmod->add_attribute("colspan", "2");
        $xhmod->tag('td');
        $xhmod->add_attribute("class", "pl-3 pt-0 small list-unstyled");
        $xhmod->tag('ul');
        foreach ($master->{'tracklist'} as $key => $value) {
            if ((string)$value->{'type_'} == "heading") {
                $xhmod->add_attribute("class", "font-weight-bold");
                $xhmod->tag('li', htmlentities((string)$value->{'title'}));
            }
            else if ((string)$value->{'type_'} == "index") {
                $xhmod->add_attribute("class", "font-italic");
                $xhmod->tag('li');
                $xhmod->add_attribute("class", "font-italic");
                $xhmod->tag('span', htmlentities((string)$value->{'title'}));
                foreach ($value->{'sub_tracks'} as $subkey => $subvalue) {
                    $xhmod->add_attribute("class", "pl-3 pt-0 list-unstyled");
                    $xhmod->tag('ul');
                    $xhmod->insert_code(processTrack($subvalue, false));
                    $xhmod->close(); // ul
                }
                $xhmod->close(); // li
            }
            else if ((string)$value->{'type_'} == "track") {
                $xhmod->insert_code(processTrack($value, true));
            }
        }
        $xhmod->close(); // ul
        $xhmod->close(); // td
        $xhmod->close(); // tr
    }

    $xhmod->close(); // table
    $xhmod->close(); // div
    $xhmod->add_attribute("class", "modal-footer bg-secondary justify-content-between");
    $xhmod->tag('div');
    $xhmod->tag('span');
    $xhmod->tag('span', "Data provided by ");
    $xhmod->add_attribute("id", "discogsRedirect" . $cnt);
    $xhmod->add_attribute("href", (string)$master->{'uri'});
    $xhmod->add_attribute("target", "_blank");
    $xhmod->add_attribute("rel", "noreferrer noopener");
    $xhmod->tag('a', "Discogs");
    $xhmod->close(); // span
    $xhmod->add_attribute("class", "float-right");
    $xhmod->tag('span');
    $xhmod->add_attribute("type", "button");
    $xhmod->add_attribute("class", "btn btn-danger");
    $xhmod->add_attribute("data-dismiss", "modal");
    $xhmod->tag('button', "Close");
    $xhmod->close(); // span
    $xhmod->close(); // div
    $xhmod->close(); // div
    $xhmod->close(); // div
    $xhmod->close(); // div

    $html = $xhmod->flush();
    //error_log(print_r($html, 1));

    $xh->insert_code($html);
    $xh->close(); // div

    $html = $xh->flush();
    //error_log(print_r($html, 1));

    return $html;
}

function processTrack($value, $posFlag) {
    $xh = new Html;
    $xh->init($_SESSION["htmlIndent"]);

    $str = "";

    if ($posFlag && !empty($value->{'position'})) {
        if (!preg_match("/^[a-zA-Z][0-9]/", (string)$value->{'position'}) && !preg_match("/^[a-zA-Z]$/", (string)$value->{'position'})) {
            $str .= (string)$value->{'position'} . '. ';
        }
    }

    $str .= (string)$value->{'title'};

    $trackArtists = [];
    if (isset($value->{'artists'})) {
        foreach ($value->{'artists'} as $taKey => $taValue) {
            $trackArtists[] = trim(preg_replace('/\([0-9]+\)$/', "", (string)$taValue->{'name'}));
        }
        if (count($trackArtists)) {
            $str .= " - " . join(", ", $trackArtists);
        }
    }

    if (!empty($value->{'duration'})) {
        $str .= " [" . (string)$value->{'duration'} . "]";
    }

    $xh->tag('li', $str);

    $html = $xh->flush();
    //error_log(print_r($html, 1));

    return $html;
}

function skipDuplicateMaster($master, $processedMasters) {
    if ($master->{'master_id'} > 0 && in_array($master->{'master_id'}, array_column($processedMasters, "id"))) {
        return true;
    }

    $year = $master->{'year'} ?? 0;

    foreach ($processedMasters as $v) {
        $s1 = strtolower($master->{'title'});
        $s2 = strtolower($v['title']);
        $sim = similar_text($s1, $s2, $perc);
        if ($perc > 85.00 && $year == $v["year"]) {
//error_log($master->{'title'} . " === " . $v['title']); 
//error_log("similarity: $sim (" . number_format($perc, 2) . "%)");
            return true;
        }
    }

    return false;
}