<?php
$translate = $this->plugin('translate');
$escape = $this->plugin('escapeHtml');
$this->headScript()->appendFile($this->assetUrl('js/browse.js'));
$layoutSetting = $this->themeSetting('browse_layout');
$gridState = ($layoutSetting == 'togglegrid') ? 'disabled' : '';
$listState = ($layoutSetting == 'togglelist') ? 'disabled': '';
$isGrid = (!isset($layoutSetting) || strpos($layoutSetting, 'grid') !== false) ? true : false;
$headingTerm = $this->siteSetting('browse_heading_property_term');
$bodyTerm = $this->siteSetting('browse_body_property_term');
$bodyTruncate = $this->themeSetting('truncate_body_property');

$this->htmlElement('body')->appendAttribute('class', 'item-set resource browse');
?>

<?php echo $this->pageTitle($translate('Item sets'), 2); ?>

<?php echo $this->searchFilters(); ?>

<?php echo $this->searchFilters(); ?>
<div class="browse-control-mobile">
<button type="button" class="browse-toggle closed">Tools</button>
</div>
<div class="browse-controls closed">
    <?php echo $this->pagination(); ?>
    <?php if (strpos($layoutSetting, 'toggle') !== false): ?>
    <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>
    <?php endif; ?>
</div>

<?php $this->trigger('view.browse.before'); ?>
<ul class="resources <?php echo ($isGrid) ? 'resource-grid' : 'resource-list'; ?>">
<?php
foreach ($itemSets as $itemSet):
    $heading = $headingTerm ? $itemSet->value($headingTerm, ['default' => $translate('[Untitled]')]) : $itemSet->displayTitle();
    $body = $bodyTerm ? $itemSet->value($bodyTerm) : $itemSet->displayDescription();
?>
    <li class="item-set resource <?php echo ($isGrid) ? '' : 'media-object'; ?>">
        <?php if ($thumbnail = $this->thumbnail($itemSet, 'medium')): ?>
        <div class="resource-image <?php echo ($isGrid) ? '' : 'media-object-section'; ?>">
            <?php echo $itemSet->linkRaw($thumbnail, null, ['class' => 'thumbnail']); ?>
        </div>
        <?php endif; ?>
        <div class="resource-meta <?php echo ($isGrid) ? '' : 'media-object-section'; ?>">
            <h4><?php echo $itemSet->link($heading); ?></h4>
            <?php if ($body): ?>
            <div class="description <?php echo ($bodyTruncate) ? $bodyTruncate : ''; ?>"><?php echo $body; ?></div>
            <?php endif; ?>
        </div>
    </li>
<?php endforeach; ?>
</ul>
<?php $this->trigger('view.browse.after'); ?>
<?php echo $this->pagination(); ?>
