<?php
$this->headLink()->prependStylesheet($this->assetUrl('css/advanced-search.css', 'Omeka'));

// Prepare the resource class query.
if (isset($query['resource_class_id'])) {
    $resourceClassId = $query['resource_class_id'] ;
} else {
    $resourceClassId = null;
}

// Prepare the value queries.
$values = [[]];
if (isset($query['value'])) {
    $values = [];
    $types = ['eq', 'neq', 'in', 'nin'];
    foreach ($types as $type) {
        if (isset($query['value'][$type])) {
            foreach ($query['value'][$type] as $text) {
                $values[] = ['type' => $type, 'text' => $text];
            }
        }
    }
}

// Prepare the property queries.
$properties = [[]];
if (isset($query['property'])) {
    $properties = [];
    $types = ['eq', 'neq', 'in', 'nin'];
    foreach ($query['property'] as $id => $null) {
        foreach ($types as $type) {
            if (isset($query['property'][$id][$type])) {
                foreach ($query['property'][$id][$type] as $text) {
                    $properties[] = ['type' => $type, 'text' => $text, 'id' => $id];
                }
            }
        }
    }
}

// Prepare the has property queries.
$hasProperties = [[]];
if (isset($query['has_property'])) {
    $hasProperties = [];
    foreach ($query['has_property'] as $id => $type) {
        $hasProperties[] = ['id' => $id, 'type' => $type];
    }
}

// Convenience functions to minimize inline code.
$queryOption = function($type, array $search, $text) {
    $selected = null;
    if (isset($search['type']) && $type === $search['type']) {
        $selected = ' selected';
    }
    return sprintf('<option value="%s"%s>%s</option>', $type, $selected, $text);
};
$queryText = function(array $search) {
    $text = null;
    if (isset($search['text'])) {
        $text = $search['text'];
    }
    return sprintf('<input type="text" class="query-text" value="%s">',  $this->escapeHtml($text));
}
?>

<div class="field">
    <div class="field-meta">
        <label for="resource_class_id"><?php echo $this->translate('Search by class'); ?></label>
    </div>
    <div class="inputs">
        <?php echo $this->resourceClassSelect([
            'name' => 'resource_class_id',
            'attributes' => ['id' => 'resource_class_id', 'value' => $resourceClassId]
        ]); ?>
    </div>
</div>

<div id="property-queries" class="field removable" role="group" aria-labelledby="by-value-label">
    <div class="field-meta">
        <span id="by-value-label" class="label"><?php echo $this->translate('Search by value'); ?></span>
    </div>
    <div class="inputs">
        <?php foreach ($properties as $property): ?>
        <div class="value">
            <?php echo $this->propertySelect([
                'name' => 'property',
                'attributes' => [
                    'class' => 'query-property',
                    'value' => isset($property['id']) ? $property['id'] : null,
                ],
                'options' => [
                    'empty_option' => '[Any Property]', // @translate
                ]
            ]); ?>
            <select class="query-type">
                <?php echo $queryOption('eq', $property, $this->translate('has exact value')); ?>
                <?php echo $queryOption('neq', $property, $this->translate('does not have exact value')); ?>
                <?php echo $queryOption('in', $property, $this->translate('contains value')); ?>
                <?php echo $queryOption('nin', $property, $this->translate('does not contain value')); ?>
            </select>
            <?php echo $queryText($property); ?>
            <button type="button" class="o-icon-delete remove-value button"><?php echo $this->translate('Remove value'); ?></button>
        </div>
        <?php endforeach; ?>
        <a href="#" class="add-value button"><?php echo $this->translate('Add new value'); ?></a>
    </div>
</div>

<div id="has-property-queries" class="field removable" role="group" aria-labelledby="by-property-label">
    <div class="field-meta">
        <span id="by-property-label" class="label"><?php echo $this->translate('Search by property'); ?></span>
    </div>
    <div class="inputs">
        <?php foreach ($hasProperties as $hasProperty): ?>
        <div class="value">
            <select class="query-type">
                <?php echo $queryOption(1, $hasProperty, $this->translate('has property')); ?>
                <?php echo $queryOption(0, $hasProperty, $this->translate('does not have property')); ?>
            </select>
            <?php echo $this->propertySelect([
                'name' => 'property',
                'attributes' => [
                    'class' => 'query-property',
                    'value' => isset($hasProperty['id']) ? $hasProperty['id'] : null,
                ]
            ]); ?>
            <button type="button" class="o-icon-delete remove-value button"><?php echo $this->translate('Remove value'); ?></button>
        </div>
        <?php endforeach; ?>
        <a href="#" class="add-value button"><?php echo $this->translate('Add new value'); ?></a>
    </div>
</div>

<?php $this->trigger(
    'view.advanced_search',
    [
        'query' => isset($query) ? $query : null,
        'resourceType' => isset($resourceType) ? $resourceType : null
    ]
); ?>
