<?php
$this->htmlElement('body')->appendAttribute('class', 'sites browse');
$escape = $this->plugin('escapeHtml');
$sortHeadings = [
    [
        'label' => $this->translate('Title'),
        'value' => 'title'
    ],
    [
        'label' => $this->translate('URL slug'),
        'value' => 'slug'
    ],
    [
        'label' => $this->translate('Owner'),
        'value' => 'owner_name'
    ]
];
?>

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

<div id="page-actions">
    <?php if ($this->userIsAllowed('Omeka\Entity\Site', 'create')): ?>
    <?php echo $this->hyperlink($this->translate('Add new site'), $this->url('admin/site/add'), ['class' => 'button']); ?>
    <?php endif; ?>
</div>

<?php if (count($sites) > 0): ?>

<div class="browse-controls">
    <?php echo $this->pagination(); ?>
    <?php echo $this->sortSelector($sortHeadings); ?>
</div>
<?php $this->trigger('view.browse.before'); ?>
<table class="tablesaw" data-tablesaw-mode="stack">
    <thead>
        <tr>
            <th><?php echo $this->translate('Title'); ?></th>
            <th><?php echo $this->translate('URL slug'); ?></th>
            <th><?php echo $this->translate('Owner'); ?></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($sites as $site): ?>
        <?php
        if ($owner = $site->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>
                <span class="resource-name">
                    <?php echo $site->link($site->title(), 'show'); ?>
                </span>
                <ul class="actions">
                    <li>
                        <a href="<?php echo $site->siteUrl(); ?>" target="_blank" class="o-icon-external public" aria-label="<?php echo $this->translate('View public site'); ?>" title="<?php echo $this->translate('View public site'); ?>"></a>
                    </li>
                    <?php if ($site->userIsAllowed('update')): ?>
                    <li><?php echo $site->link('', null, [
                        'class' => 'o-icon-edit',
                        'aria-label' => $this->translate('Edit'),
                        'title' => $this->translate('Edit'),
                    ]); ?></li>
                    <?php endif; ?>
                    <?php if ($site->userIsAllowed('delete')): ?>
                    <li><a href="#"
                        data-sidebar-content-url="<?php echo $escape($site->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>
                    <?php endif; ?>
                </ul>
            </td>
            <td><?php echo $escape($site->slug()); ?></td>
            <td><?php echo $ownerText; ?></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 sites.'); ?></p>
</div>

<?php endif; ?>
