<?php
$this->headScript()->appendFile($this->assetUrl('js/browse.js'));
$translate = $this->plugin('translate');
$layoutSetting = $this->themeSetting('browse_layout');
$gridState = ($layoutSetting == 'togglegrid') ? 'disabled' : '';
$listState = ($layoutSetting == 'togglelist') ? 'disabled': '';
$currentLayout = (!isset($layoutSetting) || strpos($layoutSetting, 'grid') !== false) ? 'grid' : 'list';
$isGrid = ($currentLayout == 'grid') ? true : false;
$queryUrl = $this->url(
    'site/resource', ['controller' => $this->resourceType, 'action' => 'browse'], ['query' => $this->query], true
);
?>
<div class="preview-block">

<?php if ($this->heading): ?>
<div class="preview-header">
    <h3><?php echo $this->hyperlink($this->heading, $queryUrl); ?></h3>
    <?php
    if ($this->linkText):
        echo $this->hyperlink($this->linkText, $queryUrl);
    endif;
    ?>
</div>
<?php endif; ?>

<?php if (strpos($layoutSetting, 'toggle') !== false): ?>
<div class="browse-controls">
    <div class="layout-toggle">
        <button type="button" aria-label="<?php echo $translate('Grid'); ?>" class="grid o-icon-grid" <?php echo $gridState; ?>></button>
        <button type="button" aria-label="<?php echo $translate('List'); ?>" class="list o-icon-list" <?php echo $listState; ?>></button>        
    </div>
</div>
<?php endif; ?>


<ul class="resources <?php echo ($isGrid) ? 'resource-grid' : 'resource-list'; ?>">
<?php
$headingTerm = $this->siteSetting('browse_heading_property_term');
$bodyTerm = $this->siteSetting('browse_body_property_term');
foreach ($this->resources as $resource):
    $heading = $headingTerm ? $resource->value($headingTerm, ['default' => $translate('[Untitled]')]) : $resource->displayTitle();
    $body = $bodyTerm ? $resource->value($bodyTerm) : $resource->displayDescription();
?>
    <li class="<?php echo $this->resourceType; ?> resource <?php echo ($isGrid) ? '' : 'media-object'; ?>">
        <?php if ($thumbnail = $this->thumbnail($resource, 'medium')): ?>
        <div class="resource-image <?php echo ($isGrid) ? '' : 'media-object-section'; ?>">
            <?php echo $resource->linkRaw($thumbnail, null, ['class' => 'thumbnail']); ?>
        </div>
        <?php endif; ?>
        <div class="resource-meta <?php echo ($isGrid) ? '' : 'media-object-section'; ?>">
            <h4><?php echo $resource->link($heading); ?></h4>
            <?php if ($body): ?>
            <div class="description"><?php echo $body; ?></div>
            <?php endif; ?>
        </div>
    </li>

<?php endforeach; ?>
</ul>

<?php
if ($this->linkText && !$this->heading):
    echo $this->hyperlink($this->linkText, $queryUrl);
endif;
?>

</div>
