Skip to content

Commit 14d3ed7

Browse files
committed
fix(formanswer): page switching loose filter
when changing page of form answer results of a form the filter on the form foreign key is lost.
1 parent 3b306c3 commit 14d3ed7

File tree

2 files changed

+140
-22
lines changed

2 files changed

+140
-22
lines changed

inc/formanswer.class.php

Lines changed: 75 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* ---------------------------------------------------------------------
3030
*/
3131

32+
use Glpi\Application\View\TemplateRenderer;
3233
use Glpi\Toolbox\Sanitizer;
3334
use GlpiPlugin\Formcreator\Field\DropdownField;
3435

@@ -207,7 +208,6 @@ public function rawSearchOptions() {
207208
'table' => 'glpi_plugin_formcreator_forms',
208209
'field' => 'name',
209210
'name' => PluginFormcreatorForm::getTypeName(1),
210-
'searchtype' => 'contains',
211211
'datatype' => 'string',
212212
'massiveaction' => false
213213
];
@@ -423,32 +423,85 @@ public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {
423423
}
424424

425425
static function showForForm(PluginFormcreatorForm $form, $params = []) {
426-
// set a session var to tweak search results
427-
$_SESSION['formcreator']['form_search_answers'] = $form->getID();
428-
429-
// prepare params for search
430-
$item = PluginFormcreatorCommon::getFormAnswer();
431-
$searchOptions = $item->rawSearchOptions();
432-
$filteredOptions = [];
433-
foreach ($searchOptions as $value) {
434-
if (is_numeric($value['id']) && $value['id'] <= 7) {
435-
$filteredOptions[$value['id']] = $value;
426+
global $DB;
427+
428+
$table = self::getTable();
429+
$form_table = PluginFormcreatorForm::getTable();
430+
$form_fk = PluginFormcreatorForm::getForeignKeyField();
431+
$user_table = User::getTable();
432+
if (version_compare(GLPI_VERSION, '10.0.5') >= 0) {
433+
$userQueryExpression = User::getFriendlyNameFields('requester_name');
434+
} else {
435+
// Drop this alternative when the plugin requires GLPI 10.0.5+
436+
$alias = 'requester_name';
437+
$config = Config::getConfigurationValues('core');
438+
if ($config['names_format'] == User::FIRSTNAME_BEFORE) {
439+
$first = "firstname";
440+
$second = "realname";
441+
} else {
442+
$first = "realname";
443+
$second = "firstname";
436444
}
445+
446+
$first = DBmysql::quoteName("$user_table.$first");
447+
$second = DBmysql::quoteName("$user_table.$second");
448+
$alias = DBmysql::quoteName($alias);
449+
$name = DBmysql::quoteName($user_table . '.' . self::getNameField());
450+
451+
$userQueryExpression = new QueryExpression("IF(
452+
$first <> '' && $second <> '',
453+
CONCAT($first, ' ', $second),
454+
$name
455+
) AS $alias");
437456
}
438-
$searchOptions = $filteredOptions;
439-
$sopt_keys = array_keys($searchOptions);
440457

441-
$forcedisplay = array_combine($sopt_keys, $sopt_keys);
458+
$result = $DB->request([
459+
'SELECT' => [
460+
$table => [
461+
'id',
462+
'name',
463+
'requester_id',
464+
'users_id_validator',
465+
'request_date'
466+
],
467+
$form_table => [
468+
'name as form_name'
469+
],
470+
$userQueryExpression
471+
],
472+
'FROM' => self::getTable(),
473+
'INNER JOIN' => [
474+
$form_table => [
475+
'FKEY' => [
476+
$form_table => 'id',
477+
$table => $form_fk,
478+
],
479+
],
480+
],
481+
'LEFT JOIN' => [
482+
$user_table => [
483+
'FKEY' => [
484+
$user_table => 'id',
485+
$table => 'requester_id',
486+
],
487+
],
488+
],
489+
'WHERE' => [
490+
$table . '.' . $form_fk => $form->getID(),
491+
],
492+
'LIMIT' => 20,
493+
'ORDER' => [
494+
'request_date DESC',
495+
],
496+
]);
442497

443-
// do search
444-
$params = Search::manageParams(__CLASS__, $params, false);
445-
$data = Search::prepareDatasForSearch(__CLASS__, $params, $forcedisplay);
446-
Search::constructSQL($data);
447-
Search::constructData($data);
448-
Search::displayData($data);
498+
$total_count = count($result);
449499

450-
// remove previous session var (restore default view)
451-
unset($_SESSION['formcreator']['form_search_answers']);
500+
TemplateRenderer::getInstance()->display('@formcreator/pages/form.formanswer.html.twig', [
501+
'form' => $form,
502+
'form_answers' => $result,
503+
'total_count' => $total_count,
504+
]);
452505
}
453506

454507
/**
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{#
2+
# ---------------------------------------------------------------------
3+
# Formcreator is a plugin which allows creation of custom forms of
4+
# easy access.
5+
# ---------------------------------------------------------------------
6+
# LICENSE
7+
#
8+
# This file is part of Formcreator.
9+
#
10+
# Formcreator is free software; you can redistribute it and/or modify
11+
# it under the terms of the GNU General Public License as published by
12+
# the Free Software Foundation; either version 2 of the License, or
13+
# (at your option) any later version.
14+
#
15+
# Formcreator is distributed in the hope that it will be useful,
16+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
# GNU General Public License for more details.
19+
#
20+
# You should have received a copy of the GNU General Public License
21+
# along with Formcreator. If not, see <http://www.gnu.org/licenses/>.
22+
# ---------------------------------------------------------------------
23+
# @copyright Copyright © 2011 - 2021 Teclib'
24+
# @license http://www.gnu.org/licenses/gpl.txt GPLv3+
25+
# @link https://github.com/pluginsGLPI/formcreator/
26+
# @link https://pluginsglpi.github.io/formcreator/
27+
# @link http://plugins.glpi-project.org/#/plugin/formcreator
28+
# ---------------------------------------------------------------------
29+
#}
30+
31+
{% if total_count < 1 %}
32+
<div class="alert alert-info">
33+
{{ __('No form answer yet', 'formcreator') }}
34+
</div>
35+
{% else %}
36+
{% set criteria = '?criteria[0][field]=3&criteria[0][searchtype]=equals&criteria[0][value]=' ~ form.getID() ~ '' %}
37+
<div class="center my-4">
38+
{{ __('%s latest items', 'formcreator')|format(total_count) }}
39+
<a href="{{ 'PluginFormcreatorFormAnswer'|itemtype_search_path ~ criteria }}">{{ __('See all', 'formcreator') }}</a>
40+
</div>
41+
<div class="table-responsive">
42+
<table class="table table-hover">
43+
<thead>
44+
<tr>
45+
<th>{{ __('ID') }}</th>
46+
<th>{{ __('Name') }}</th>
47+
<th>{{ __('Form') }}</th>
48+
<th>{{ __('Requester', 'Requesters', 1) }}</th>
49+
<th>{{ __('Creation date') }}</th>
50+
</tr>
51+
</thead>
52+
<tbody>
53+
{% for row in form_answers %}
54+
<tr>
55+
<td>{{ row.id }}</td>
56+
<td>{{ get_item_link('PluginFormcreatorFormAnswer', row.id) }}</a></td>
57+
<td>{{ row.form_name }}</td>
58+
<td>{{ row.requester_name }}</td>
59+
<td>{{ row.request_date }}</td>
60+
</tr>
61+
{% endfor %}
62+
</tbody>
63+
</table>
64+
</div>
65+
{% endif %}

0 commit comments

Comments
 (0)