<?php
use Omeka\Module\Manager;

$translate = $this->plugin('translate');
$escape = $this->plugin('escapeHtml');
$this->htmlElement('body')->appendAttribute('class', 'modules browse');
$this->headScript()->prependFile($this->assetUrl('vendor/compare-versions/index.js', 'Omeka'));
$this->plugin('formRow')->setPartial(null);
?>

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

<form method="get" class="state-filter">
    <select name="state" aria-label="<?php echo $translate('Filter'); ?>">
        <option value=""><?php echo $translate('All modules'); ?></option>
        <?php foreach ($filterStates as $value => $option): ?>
        <option value="<?php echo $escape($value); ?>"<?php if ($value == $filterState): echo ' selected'; endif; ?>><?php echo $escape($option); ?></option>
        <?php endforeach; ?>
    </select>
</form>
<?php $this->trigger('view.browse.before'); ?>
<?php if ($modules): ?>
<div id="modules">
        <?php foreach ($modules as $id => $module): ?>
        <div class="module">
            <div class="module-meta">
                <?php if ($name = $module->getName()): ?>
                <span class="module-name">
                    <?php if ($moduleLink = $module->getIni('module_link')): ?>
                    <?php echo $this->hyperlink($name, $moduleLink); ?>
                    <?php else: ?>
                    <?php echo $name; ?>
                    <?php endif; ?>
                </span>
                <?php else: ?>
                [<?php echo $translate('name not found'); ?>]
                <?php endif; ?>
                <?php if (($versionIni = $module->getIni('version')) || ($versionDb = $module->getDb('version'))): ?>
                <span class="module-version"><?php echo sprintf($translate('version %s'), $escape($versionIni ?: $versionDb)); ?></span>
                <?php endif; ?>
                <?php if ($author = $module->getIni('author')): ?>
                <span class="module-author">
                    <?php if ($moduleLink = $module->getIni('author_link')): ?>
                    <?php echo sprintf($translate('by %s'), $this->hyperlink($author, $module->getIni('author_link'))); ?>
                    <?php else: ?>
                    <?php echo sprintf($translate('by %s'), $author); ?>
                    <?php endif; ?>
                </span>
                <?php endif; ?>
                <?php if ($description = $module->getIni('description')): ?>
                <span class="module-description"><?php echo $description; ?></span>
                <?php endif; ?>
                <?php if (Manager::STATE_ACTIVE === $module->getState()): ?>
                <div class="version-notification" style="display: none;"
                    data-addon-id="<?php echo $this->escapeHtml($module->getId()); ?>"
                    data-current-version="<?php echo $this->escapeHtml($module->getIni('version')); ?>">
                    <?php echo sprintf(
                        $translate('A new version of this module is available. %s'),
                        sprintf(
                            '<a href="%s">%s</a>',
                            'http://omeka.org/s/modules/' . $module->getId(),
                            $translate('Get the new version.')
                        )
                    ); ?>
                </div>
                <?php endif; ?>
            </div>
            <div class="module-actions">
                <?php
                // configure control
                if (Manager::STATE_ACTIVE == $module->getState()
                    && $module->isConfigurable()
                    && $this->userIsAllowed('Omeka\Module\Manager', 'configure')
                ) {
                    echo $this->hyperlink($escape($translate('Configure')),
                        $this->url(
                            null,
                            ['action' => 'configure'],
                            ['query' => ['id' => $module->getId()]
                        ], true),
                        [
                            'class' => 'o-icon-configure button'
                        ]
                    );
                }
                ?>

                <?php
                // activate control
                if (Manager::STATE_NOT_ACTIVE == $module->getState()
                    && $this->userIsAllowed('Omeka\Module\Manager', 'activate')
                ) {
                    $form = $stateChangeForm('activate', $module->getId());
                    echo $this->form($form);
                }
                ?>

                <?php
                // deactivate control
                if (Manager::STATE_ACTIVE == $module->getState()
                    && $this->userIsAllowed('Omeka\Module\Manager', 'deactivate')
                ) {
                    $form = $stateChangeForm('deactivate', $module->getId());
                    echo $this->form($form);
                }
                ?>

                <?php
                // install control
                if (Manager::STATE_NOT_INSTALLED == $module->getState()
                    && $this->userIsAllowed('Omeka\Module\Manager', 'install')
                ) {
                    $form = $stateChangeForm('install', $module->getId());
                    echo $this->form($form);
                }
                ?>

                <?php
                // uninstall control
                $canBeUninstalled = in_array(
                    $module->getState(),
                    [Manager::STATE_ACTIVE, Manager::STATE_NOT_ACTIVE]
                );
                if ($canBeUninstalled && $this->userIsAllowed('Omeka\Module\Manager', 'uninstall')):
                ?>
                <?php echo $this->hyperlink($translate('Uninstall'), '#', [
                    'data-sidebar-content-url' => $this->url(null, ['action' => 'uninstall-confirm'], ['query' => ['id' => $module->getId()]], true),
                    'class' => 'o-icon-uninstall red button sidebar-content',
                ]); ?>
                <?php endif; ?>

                <?php
                // upgrade control
                if (Manager::STATE_NEEDS_UPGRADE == $module->getState()
                    && $this->userIsAllowed('Omeka\Module\Manager', 'upgrade')
                ) {
                    $form = $stateChangeForm('upgrade', $module->getId());
                    echo $this->form($form);
                }
                ?>

                <?php if ($module->getState() === Manager::STATE_INVALID_MODULE): ?>
                <span class="error"><?php echo $translate('Error: invalid Module.php file'); ?></span>
                <?php elseif ($module->getState() === Manager::STATE_INVALID_INI): ?>
                <span class="error"><?php echo $translate('Error: invalid config/module.ini file'); ?></span>
                <?php elseif ($module->getState() === Manager::STATE_INVALID_OMEKA_VERSION): ?>
                <span class="error"><?php echo sprintf($translate('Error: %1$s requires Omeka S %2$s'), $module->getName(), $module->getIni('omeka_version_constraint')); ?></span>
                <?php elseif ($module->getState() === Manager::STATE_NOT_FOUND): ?>
                <span class="error"><?php echo $translate('Error: module in database but not in filesystem'); ?></span>
                <?php endif; ?>
            </div>
        </div>
        <?php endforeach; ?>
</div>
<div class="sidebar">
    <?php echo $this->hyperlink('', '#', [
        'class' => 'sidebar-close o-icon-close',
        'title' => $translate('Close'),
    ]); ?>
    <div class="sidebar-content"></div>
</div>

<?php else: ?>

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

<?php endif; ?>

<script>
$(".state-filter select").change(function() {
    $(this).closest('form').submit();
});
</script>

<?php if ($this->setting('version_notifications')): ?>
<script>
$.get('https://omeka.org/add-ons/json/s_module.json')
    .done(function(data) {
        $('.version-notification').each(function(index) {
            var addon = $(this);
            var addonId = addon.data('addon-id');
            if (addonId in data) {
                if (compareVersions.compareVersions(addon.data('current-version'), data[addonId]['latest_version']) === -1) {
                    addon.show();
                }
            }
        });
    });
</script>
<?php endif; ?>

<?php $this->trigger('view.browse.after'); ?>
