<?php
$this->placeholder('itemDiv')->set('
<div class="msg-qs-app">
    <h2>Quick Search Results</h2>

    <a id="whatsnew">What\'s new?</a>

    <div id="quickSearchNotes">
        <ul>
            <li>Results are now sorted by relevance like a google search, instead of sorted alphabetically.</li>
            <li>Words no longer need to be consecutive, or in order: "house welcome" will find "Welcome To The Monkey House"</li>
            <li>The Search: "talk" will find "talking", "talker", and "talks", but not "stalker".</li>
            <li>You can click the "Filter By" links to narrow down your search, then click the X to remove the filter</li>
            <li>
                <span class="msg-alert">
                    Please note that recent changes to releases may not show up in search results for up to five minutes.
                </span>
            </li>
        </ul>
    </div>

</div>');

if ($this->totalHits > 0) {
    echo($this->paginationpanel(
        '/quicksearch/release/',
        $this->currentPage,
        $this->hitsPerPage,
        ceil($this->totalHits / $this->hitsPerPage),
        $theQueryString = http_build_query($this->currentSafeQueryVars),
        $this->totalHits
    ));
}

?>

<div class="panelHeader">
    <h3>Release Results</h3>
</div>

<div class="panelContent">

    <!-- DISPLAY RESULTS -->
    <div class="container-fluid">
        <div class="row">

            <?php if ($this->totalHits == 0) { ?>
            <!-- DISPLAY NO RESULTS -->
            <div class="no-results-msg">
                <div class="no-results">
                    <p>
                        You searched for <strong>"<?=htmlspecialchars($this->currentSafeQueryVars['varSearchString'], ENT_QUOTES, 'UTF-8');?>"</strong> and there
                        were no results.  Searches are only looking in the following fields:
                    </p>
                    <ul>
                        <li>Release Name</li>
                        <li>Artist Name</li>
                        <li>Release ID</li>
                        <li>UPC</li>
                        <li>ISRC</li>
                    </ul>
                    <p>
                        Note that the following words are ignored by releases quicksearch:
                        <em>a, an, and, are, as, at, be, but, by, for, if, in, into, is, it, no, not, of, on, or, such,
                        that, the, their, then, there, these, they, this, to, was, will, with</em>
                    </p>
                </div>

                <div id="quickSearchModify">
                    <h4>Modify your search: </h4>
                    <form action="/quicksearch/release" method="get">
                        <input type="hidden" name="srchSubmit" value="1"/>
                        <input type="text" name="varSearchString" class="quickSearchField" id="smartSearch"
                            value="<?=htmlspecialchars($this->currentSafeQueryVars['varSearchString'], ENT_QUOTES, 'UTF-8');?>" />
                        <input type="submit" name="srchSubmit" value="search"/>
                    </form>
                </div>

            </div>

            <?php
} else { /* if there are results: */ ?>

            <div class="results-facets col-sm-2">
                <h3>Filter by:</h3>

                <?php
                // Print Selected Facets above Unselected facets
                foreach ($this->facetNamesSelectedGroup as $facetName => $facetDisplay) {
                    echo $this->partial(
                        'quicksearch/partials/facetlist.phtml',
                        array(
                            'facetName' => $facetName,
                            'facetDisplay' => $facetDisplay,
                            'facets' => $this->facets,
                        )
                    );
                }
                foreach ($this->facetNamesUnselectedGroup as $facetName => $facetDisplay) {
                    echo $this->partial(
                        'quicksearch/partials/facetlist.phtml',
                        array(
                            'facetName' => $facetName,
                            'facetDisplay' => $facetDisplay,
                            'facets' => $this->facets,
                        )
                    );
                }
                ?>

            </div>
            <div id="releaseResults" class="col-sm-10">
                <br>
                <div>You searched For: "<strong><?=htmlspecialchars($this->currentSafeQueryVars['varSearchString'], ENT_QUOTES, 'UTF-8');?></strong>"</div>
                <br>
                <div class="releases-by-search row">
<?php
foreach ($this->searchResults as $result) {

    if ($result['not_for_distribution'] != 'SMEAnalyticsDummy') {
        // release search type
        $releaseSearchParam = "release_id=" . $result["release_id"];
        $downloadArtworkParam = "upc=" . $result["display_upc"] . "&release_id=" . $result["release_id"];
        $result['track_type'] = !empty($result['track_type']) ? $result['track_type'] : array();
        $saleStartDate = '';
        if (!empty($result['sale_start_date'])) {
            $saleStartDateObj = new DateTime($result['sale_start_date']);
            $saleStartDate = $saleStartDateObj->format('Y-m-d');
        }

?>
                    <div class="releaseResult">
                        <div class="row">
                            <div class="col-sm-12">
                                <a class="releaseImageLink" href="/cont_mgmt/view_release.php?<?=$releaseSearchParam?>" title="<?=htmlspecialchars($result["release_name"]);?>">
                                    <?php
                                        if (!in_array('video', $result['track_type'])) {
                                            $imagePath = $this->coverImagePaths[$result['release_id']]; ?>
                                        <img class="img-responsive" src="<?=$imagePath?>" title="<?=htmlspecialchars($result["release_name"]);?>" />
                                        <?php
                                        } else { ?>
                                        <img class="img-responsive" src="<?=getImageCoverArt(intval($result["display_upc"]))?>" title="<?=htmlspecialchars($result["release_name"]);?>" />
                                    <?php }
                                     ?>
                                </a>
                            </div>
                            <div class="col-sm-12">
                                <ul class="release-meta">
                                    <li>
                                        <?php
                                        if (in_array('video', $result["track_type"])) {
                                            $iconUrl = '/images/videoicon.gif';
                                        } else {
                                            $iconUrl = '/images/musicicon.gif';
                                        }
                                        ?>
                                        <?php if ($this->viewVendorPermission) { ?>
                                            <a class="release-name" style="background-image: url(<?=$iconUrl ?>);" href="/cont_mgmt/view_release.php?<?=$releaseSearchParam?>" title="<?=htmlspecialchars($result["release_name"]);?>">
                                        <?php } else { ?>
                                            <strong class="release-name" style="background-image: url(<?=$iconUrl ?>);" title="<?=htmlspecialchars($result["release_name"]);?>"></strong>
                                        <?php } ?>
                                        <?=($result["deletions"] == 'Y' ? '<del>' : '')?>
                                        <?= !empty($result['highlight_fields']['release_name']) ? $result['highlight_fields']['release_name'] : ''; ?>
                                        <?=($result["deletions"] == 'Y' ? '</del>' : '')?>
                                        </a>
                                    </li>
                                    <li class="release-upc">
                                        <strong>UPC:</strong>
                                        <?=$result["display_upc"]?>
                                    </li>
                                    <li class="release-product-version">
                                        <strong>Product Version:</strong>
                                        <?=isset($result['delivered_version'])?$result['delivered_version']:'-';?>
                                    </li>
                                    <li class="release-label">
                                        <strong>Label:</strong>
                                        <?php $label = !empty($result['label']) ? $result['label'] : ''; ?>
                                        <a href='/cont_mgmt/view_vendor.php?vendor_id=<?=$result["vendor_id"]; ?>' title='<?=htmlspecialchars($label)?>'><?=$label?></a>
                                    </li>
                                    <li class="release-artist">
                                        <strong>Artist:</strong>
                                        <?php if ($this->viewVendorPermission) { ?>
                                            <a href="/cont_mgmt/view_artist.php?artist_id=<?=$result["artist_id"]; ?>" title="<?=htmlspecialchars($result["artist_name"], ENT_QUOTES, 'UTF-8')?>"><?=$result["highlight_fields"]["artist_name"]?></a>
                                        <?php } else { ?>
                                            <span title="<?=htmlspecialchars($result["artist_name"], ENT_QUOTES, 'UTF-8')?>"><?=$result["highlight_fields"]["artist_name"]?></span>
                                        <?php } ?>
                                    </li>
                                    <li class="release-imprint">
                                        <strong>Imprint:</strong>
                                        <?=isset($result["imprint"])?$result["imprint"]:'';?>
                                    </li>
                                    <li class="release-genre">
                                        <strong>Genre:</strong>
                                        <?= isset($result['genre']) ? $result['genre'] : ''; ?>
                                    </li>
                                    <li class="release-sales-start-date">
                                        <strong>Sales Start Date:</strong>
                                        <?=$saleStartDate;?>
                                    </li>



                                    <?php
                                        // Display Track-ISRC Results
                                    if (isset($result["highlight_fields"]["track_isrc"])) { ?>
                                    <li class="release-track-name">
                                        <strong>Track:</strong>
                                        <?=$result["highlight_fields"]["track_name"];?>
                                    </li>
                                    <li class="release-track-isrc">
                                        <strong>ISRC:</strong>
                                        <?=$result["highlight_fields"]["track_isrc"];?>
                                    </li>
                                    <?php
                                    }?>

                                    <?php
                                        // Display Track-Name Results
                                    if (isset($result["highlight_fields"]["track_list"])) { ?>
                                        <li class="release-track-name">
                                            <strong>Track:</strong>
                                            <?=array_shift($result["highlight_fields"]["track_list"]);?>
                                        </li>
                                    <?php
                                    }
                                    if (isset($result["configuration"])) { ?>
                                        <li class="release-track-name">
                                            <strong>Configuration:</strong>
                                            <?=$result["configuration"]?>
                                        </li>
                                    <?php
                                    }
                                    if (isset($result['track_list'])) {
                                    ?>
                                        <li class="release-track-name">
                                            <strong>Track Count:</strong>
                                            <?=count($result['track_list'])?>
                                        </li>
                                    <?php
                                    }
                                    if (isset($result['format'])) { ?>
                                        <li class="release-track-name">
                                            <strong>Format:</strong>
                                            <?=$result['format']?>
                                        </li>
                                    <?php } ?>
                                        <li>
                                            <strong>Listen to All Tracks:</strong>
                                                <a href="/streaming/product/<?= $result["release_id"] ?>/stream"
                                                    onClick="window.open('/streaming/product/<?= $result["release_id"] ?>/stream', '_blank', 'width=800,height=60,resizable'); return false;"
                                                    target="_blank"
                                                    class="player-link"
                                                >
                                                <img class="release-track-player-img" src="/images/play.svg" height="16" alt="listen" border=0 align="absmiddle">
                                                </a>
                                        </li>
                                        <li>
                                            <a href="/cont_mgmt/download_artwork.php?<?=$downloadArtworkParam?>" target="_top">Download Album Artwork</a>
                                        </li>
                                </ul>
                            </div>
                        </div>
                    </div>

<?php } } ?>

                <?php } /* end check for results */ ?>

                </div><!-- /.row -->
            </div><!-- releaseResults -->
        </div>
    </div>
</div><!-- panelContent -->

<?php
/* Pagination Panel for Below Content */
if ($this->totalHits > 0) {
    echo($this->paginationpanel(
        '/quicksearch/release/',
        $this->currentPage,
        $this->hitsPerPage,
        ceil($this->totalHits / $this->hitsPerPage),
        $theQueryString = http_build_query($this->currentSafeQueryVars),
        $this->totalHits
    ));
}
?>

<?php
function getImageCoverArt($upc)
{
    $config = Zend_Registry::get('configuration');
    $upc = intval($upc);
    $imagePath = substr($upc, 0, 6) . '/' . substr($upc, 6, 3) . '/' . $upc . '.jpg';
    return trim($config->images->url, '/') . '/release/large_cover/' . $imagePath;
} ?>
