Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHRAS-3884 advanced search sort field by label #4355

Merged
merged 2 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions lib/Alchemy/Phrasea/Helper/Prod.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public function get_search_datas()
if ($fieldMeta->get_type() === \databox_field::TYPE_DATE) {
if (!array_key_exists($name, $dates)) {
$dates[$name] = array('sbas' => array());
$dates[$name]['fieldname'] = $name;
}
$dates[$name]['sbas'][] = $sbasId;

Expand Down Expand Up @@ -160,12 +161,13 @@ public function get_search_datas()
$dates['created_on']['sbas'] = $allSbasId;
$dates['created_on']['label'][] = $this->app->trans('created_on');

// sort ASC by fieldname
ksort($dates, SORT_STRING | SORT_FLAG_CASE);
ksort($fields, SORT_STRING | SORT_FLAG_CASE);
$f = $this->getLabels($fields);
$d = $this->getLabels($dates);

$searchData['fields'] = $fields;
$searchData['dates'] = $dates;
$searchData['fields'] = $f['labelWithoutName'];
$searchData['fieldsWithName'] = $f['labelWithName'];
$searchData['dates'] = $d['labelWithoutName'];
$searchData['datesWithName'] = $d['labelWithName'];
$searchData['bases'] = $bases;
$searchData['sort'] = array_map(function($v){ksort($v, SORT_NATURAL);return $v;}, $sort); // sort by name of field
$searchData['elasticSort'] = $elasticSort;
Expand All @@ -177,4 +179,28 @@ public function getRandom()
{
return md5(time() . mt_rand(100000, 999999));
}

private function getLabels($fields)
{
$labelWithName = $labelWithoutName = [];

foreach ($fields as $name => $field) {
if (empty($field['label'])) {
$labelWithoutName[$name] = $field;
$labelWithName[$name] = $field;
} else {
$labelWithoutName[implode(' | ', $field['label'])] = $field;
$labelWithName[implode(' | ', $field['label']) . ' ( '. $name . ' ) '] = $field;
}
}

// sort ASC by label merged
ksort($labelWithName, SORT_STRING | SORT_FLAG_CASE);
ksort($labelWithoutName, SORT_STRING | SORT_FLAG_CASE);

return [
'labelWithName' => $labelWithName,
'labelWithoutName' => $labelWithoutName
];
}
}
48 changes: 17 additions & 31 deletions templates/web/prod/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -592,24 +592,16 @@
<div class="term_select_wrapper_template" style="display: none;">
<select class="term_select_field" style="vertical-align:middle; width:30%;">
<option value="">{{ 'Select a field' | trans }}</option>
{% for field_id, field in search_datas['fields'] %}
{# {% if field['type'] != 'date' %}#}
<option class="field_without_real_name dbx db_{{field['sbas']|join(' db_')}}" data-fieldtype="{{ field['type'] }}-FIELD" value="{{field_id}}">
{% if field['label'] is empty %}
{{ field['fieldname'] }}
{% else %}
{{ field['label'] | join(' | ')}}
{% endif %}
</option>

<option class="field_with_real_name dbx db_{{field['sbas']|join(' db_')}}" data-fieldtype="{{ field['type'] }}-FIELD" value="{{field_id}}" style="display:none;">
{% if field['label'] is empty %}
{{ field['fieldname'] }}
{% else %}
{{ field['label'] | join(' | ') ~ " ( " ~ field['fieldname'] ~ " )"}}
{% endif %}
</option>
{#{% endif %}#}
{% for label, field in search_datas['fields'] %}
<option class="field_without_real_name dbx db_{{field['sbas']|join(' db_')}}" data-fieldtype="{{ field['type'] }}-FIELD" value="{{ field['fieldname'] }}">
{{ label }}
</option>
{% endfor %}

{% for label, field in search_datas['fieldsWithName'] %}
<option class="field_with_real_name dbx db_{{field['sbas']|join(' db_')}}" data-fieldtype="{{ field['type'] }}-FIELD" value="{{ field['fieldname'] }}" style="display:none;">
{{ label }}
</option>
{% endfor %}
</select>
<select class="term_select_op" disabled style="vertical-align:middle; width: 23%;">
Expand Down Expand Up @@ -657,21 +649,15 @@
<select name="date_field" class="date-field input-medium check-filters" data-save="true" style="width: 166px;">
<option selected="selected"
value="">{{ 'Rechercher dans un champ date' | trans }}</option>
{% for fieldname, date in search_datas['dates'] %}
<option class="field_without_real_name dbx db_{{date['sbas']|join(' db_')}}" value="{{ fieldname }}">
{% if date['label'] is empty %}
{{ fieldname }}
{% else %}
{{ date['label'] | join(' | ') }}
{% endif %}
{% for label, date in search_datas['dates'] %}
<option class="field_without_real_name dbx db_{{date['sbas']|join(' db_')}}" value="{{ date['fieldname'] }}">
{{ label }}
</option>
{% endfor %}

<option class="field_with_real_name dbx db_{{date['sbas']|join(' db_')}}" value="{{ fieldname }}" style="display:none;">
{% if date['label'] is empty %}
{{ fieldname }}
{% else %}
{{ date['label'] | join(' | ') ~ " ( " ~ fieldname ~ " )" }}
{% endif %}
{% for label, date in search_datas['datesWithName'] %}
<option class="field_with_real_name dbx db_{{date['sbas']|join(' db_')}}" value="{{ date['fieldname'] }}" style="display:none;">
{{ label }}
</option>
{% endfor %}
</select>
Expand Down