<?php
use Omeka\Api\Exception\NotFoundException;

$translate = $this->plugin('translate');
$this->headLink()->appendStylesheet($this->assetUrl('css/asset-form.css', 'Omeka'));
$this->headScript()->appendFile($this->assetUrl('js/asset-form.js', 'Omeka'));

$asset = null;
$assetId = $element->getValue();
if ($assetId) {
    try {
        $asset = $this->api()->read('assets', $assetId)->getContent();
    } catch (NotFoundException $e) {
        $element->setValue(null);
    }
}
?>
<div class="asset-form-element<?php if (!$asset) echo ' empty'; ?>">
    <?php if ($asset): ?>
    <span class="selected-asset">
        <?php
        echo sprintf('<img class="selected-asset-image" src="%s" alt="%s"><div class="selected-asset-name">%s</div>',
            $this->escapeHtml($asset->assetUrl()),
            $this->escapeHtml($asset->altText()),
            $this->escapeHtml($asset->name())
        );
        ?>
    </span>
    <?php else: ?>
    <span class="selected-asset" style="display: none;">
        <img class="selected-asset-image"><div class="selected-asset-name"></div>
    </span>
    <?php endif; ?>
    <span class="no-selected-asset">
        <?php echo $translate('[No asset selected]'); ?>
    </span>
    <button type="button" class="asset-form-select" data-sidebar-content-url="<?php echo $this->escapeHtml($this->url('admin/default', ['controller' => 'asset', 'action' => 'sidebar-select'])); ?>">
        <?php echo $translate('Select'); ?>
    </button>
    <button type="button" class="asset-form-clear red button">
        <?php echo $translate('Clear'); ?>
    </button>
    <?php echo $this->formHidden($element); ?>
</div>
