<?php
$this->htmlElement('body')->appendAttribute('class', 'media browse');
$escape = $this->plugin('escapeHtml');
$sortHeadings = [
    [
        'label' => $this->translate('Title'),
        'value' => 'dcterms:title'
    ],
    [
        'label' => $this->translate('Class'),
        'value' => 'resource_class_label'
    ],
    [
        'label' => $this->translate('Owner'),
        'value' => 'owner_name'
    ],
    [
        'label' => $this->translate('Date Created'),
        'value' => 'created'
    ],
];
?>

<?php echo $this->pageTitle($this->translate('Media')); ?>

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

<div class="browse-controls">
    <?php echo $this->pagination(); ?>
    <?php echo $this->hyperlink($this->translate('Advanced search'), $this->url(null, ['action' => 'search'], ['query' => $this->params()->fromQuery()], true), ['class' => 'advanced-search']); ?>
    <?php echo $this->sortSelector($sortHeadings); ?>
</div>

<?php $this->trigger('view.browse.before'); ?>
<?php if($medias): ?>
<table class="tablesaw" data-tablesaw-mode="stack">
    <thead>
        <tr>
            <th><?php echo $this->translate('Title'); ?></th>
            <th><?php echo $this->translate('Class'); ?></th>
            <th><?php echo $this->translate('Owner'); ?></th>
            <th><?php echo $this->translate('Date Created'); ?></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($medias as $media): ?>
        <?php
        if ($owner = $media->owner()) {
            $ownerText = $this->hyperlink(
                $owner->name(),
                $this->url('admin/id', [
                    'controller' => 'user',
                    'action' => 'show',
                    'id' => $owner->id()]
                )
            );
        } else {
            $ownerText = $this->translate('[no owner]');
        }
        ?>
            <tr>
                <td>
                    <img src="<?php echo $escape($media->thumbnailUrl('square')); ?>">
                    <span class="resource-name">
                        <?php echo $media->link($media->displayTitle(), 'show'); ?>
                    </span>
                    <ul class="actions">
                        <li><?php echo $media->link('', 'edit', [
                            'class' => 'o-icon-edit',
                            'aria-label' => $escape($this->translate('Edit')),
                            'title' => $escape($this->translate('Edit')),
                        ]); ?></li>
                        <li><a href="#"
                            data-sidebar-content-url="<?php echo $escape($media->url('delete-confirm')); ?>"
                            class="o-icon-delete sidebar-content"
                            aria-label="<?php echo $escape($this->translate('Delete')); ?>"
                            title="<?php echo $escape($this->translate('Delete')); ?>"></a></li>
                        <li><a href="#"
                            data-sidebar-content-url="<?php echo $escape($media->url('show-details')); ?>"
                            class="o-icon-more sidebar-content"
                            aria-label="<?php echo $escape($this->translate('Details')); ?>"
                            title="<?php echo $escape($this->translate('Details')); ?>"></a></li>
                    </ul>
                </td>
                <td><?php echo $escape($media->displayResourceClassLabel()); ?></td>
                <td><?php echo $ownerText; ?></td>
                <td><?php echo $escape($this->i18n()->dateFormat($media->created())); ?></td>
            </tr>
        <?php endforeach; ?>
    </tbody>

</table>
<?php $this->trigger('view.browse.after'); ?>
<div class="browse-controls">
    <?php echo $this->pagination(); ?>
</div>

<div class="sidebar">
    <a href="#"
        class="sidebar-close o-icon-close"
        aria-label="<?php echo $escape($this->translate('Close')); ?>"
        title="<?php echo $escape($this->translate('Close')); ?>"></a>
    <div class="sidebar-content"></div>
</div>

<?php else: ?>

<div class="no-resources">
    <p><?php echo $this->translate('Omeka could not find any media.'); ?></p>
</div>

<?php endif; ?>


