N°3537 run_query : fix query suggestion button

Was throwing plain old HTML
Now use standard button component, and a new HtmlFactory to avoid calling \WebPage::GetP

Also change "use this query" behavior : now also submits the form directly

And : added a TODO 3.0.0 for the "query examples" buttons (will be done later)
This commit is contained in:
Pierre Goiffon
2021-01-14 17:07:55 +01:00
parent a41229b223
commit 442e9598f8
4 changed files with 54 additions and 13 deletions

View File

@@ -179,6 +179,7 @@ return array(
'Combodo\\iTop\\Application\\UI\\Base\\Component\\GlobalSearch\\GlobalSearchFactory' => $baseDir . '/sources/application/UI/Base/Component/GlobalSearch/GlobalSearchFactory.php',
'Combodo\\iTop\\Application\\UI\\Base\\Component\\GlobalSearch\\GlobalSearchHelper' => $baseDir . '/sources/application/UI/Base/Component/GlobalSearch/GlobalSearchHelper.php',
'Combodo\\iTop\\Application\\UI\\Base\\Component\\Html\\Html' => $baseDir . '/sources/application/UI/Base/Component/Html/Html.php',
'Combodo\\iTop\\Application\\UI\\Base\\Component\\Html\\HtmlFactory' => $baseDir . '/sources/application/UI/Base/Component/Html/HtmlFactory.php',
'Combodo\\iTop\\Application\\UI\\Base\\Component\\Input\\AbstractInput' => $baseDir . '/sources/application/UI/Base/Component/Input/AbstractInput.php',
'Combodo\\iTop\\Application\\UI\\Base\\Component\\Input\\Input' => $baseDir . '/sources/application/UI/Base/Component/Input/Input.php',
'Combodo\\iTop\\Application\\UI\\Base\\Component\\Input\\InputFactory' => $baseDir . '/sources/application/UI/Base/Component/Input/InputFactory.php',

View File

@@ -409,6 +409,7 @@ class ComposerStaticInit0018331147de7601e7552f7da8e3bb8b
'Combodo\\iTop\\Application\\UI\\Base\\Component\\GlobalSearch\\GlobalSearchFactory' => __DIR__ . '/../..' . '/sources/application/UI/Base/Component/GlobalSearch/GlobalSearchFactory.php',
'Combodo\\iTop\\Application\\UI\\Base\\Component\\GlobalSearch\\GlobalSearchHelper' => __DIR__ . '/../..' . '/sources/application/UI/Base/Component/GlobalSearch/GlobalSearchHelper.php',
'Combodo\\iTop\\Application\\UI\\Base\\Component\\Html\\Html' => __DIR__ . '/../..' . '/sources/application/UI/Base/Component/Html/Html.php',
'Combodo\\iTop\\Application\\UI\\Base\\Component\\Html\\HtmlFactory' => __DIR__ . '/../..' . '/sources/application/UI/Base/Component/Html/HtmlFactory.php',
'Combodo\\iTop\\Application\\UI\\Base\\Component\\Input\\AbstractInput' => __DIR__ . '/../..' . '/sources/application/UI/Base/Component/Input/AbstractInput.php',
'Combodo\\iTop\\Application\\UI\\Base\\Component\\Input\\Input' => __DIR__ . '/../..' . '/sources/application/UI/Base/Component/Input/Input.php',
'Combodo\\iTop\\Application\\UI\\Base\\Component\\Input\\InputFactory' => __DIR__ . '/../..' . '/sources/application/UI/Base/Component/Input/InputFactory.php',

View File

@@ -23,6 +23,7 @@ use Combodo\iTop\Application\UI\Base\Component\CollapsibleSection\CollapsibleSec
use Combodo\iTop\Application\UI\Base\Component\FieldSet\FieldSet;
use Combodo\iTop\Application\UI\Base\Component\Form\Form;
use Combodo\iTop\Application\UI\Base\Component\Html\Html;
use Combodo\iTop\Application\UI\Base\Component\Html\HtmlFactory;
use Combodo\iTop\Application\UI\Base\Component\Input\InputFactory;
use Combodo\iTop\Application\UI\Base\Component\Input\TextArea;
use Combodo\iTop\Application\UI\Base\Component\Panel\PanelFactory;
@@ -83,6 +84,8 @@ function ShowExamples($oP, $sExpression)
$aDisplayData[Dict::S('UI:RunQuery:QueryExamples')][] = array(
'desc' => "<div style=\"$sHighlight\">".utils::EscapeHtml($sDescription)."</div>",
'oql' => "<div style=\"$sHighlight\">".utils::EscapeHtml($sOql)."</div>",
//TODO 3.0.0 : buttons are not styled properly yet...
// This whole "query examples" may be migrated to TWIG using iTop Twig tags ?
'go' => "<form method=\"get\"><input type=\"hidden\" name=\"expression\" value=\"$sOql\"><input type=\"submit\" value=\"".Dict::S('UI:Button:Test')."\" $sDisable>$sContext</form>\n",
);
}
@@ -301,6 +304,9 @@ EOF
$oMoreInfoSection->EnableSaveCollapsibleState('run_query__more-info');
$oP->AddUiBlock($oMoreInfoSection);
} elseif ($sSyntaxError) {
$oSyntaxErrorPanel = PanelFactory::MakeForFailure(Dict::S('UI:RunQuery:Error'));
$oP->AddUiBlock($oSyntaxErrorPanel);
if ($e instanceof OqlException) {
$sWrongWord = $e->GetWrongWord();
$aSuggestedWords = $e->GetSuggestions();
@@ -313,27 +319,27 @@ EOF
$sAfter = substr($sExpression, $e->GetColumn() + strlen($sWrongWord));
$sFixedExpression = $sBefore.$sSuggestedWord.$sAfter;
$sFixedExpressionHtml = $sBefore.'<span style="background-color:yellow">'.$sSuggestedWord.'</span>'.$sAfter;
$sSyntaxErrorText .= $oP->GetP("Suggesting: $sFixedExpressionHtml");
$sSyntaxErrorText .= "<p>Suggesting: $sFixedExpressionHtml</p>";
$oSyntaxErrorPanel->AddSubBlock(new Html($sSyntaxErrorText));
$sEscapedExpression = utils::EscapeHtml(addslashes($sFixedExpression));
$sSyntaxErrorText .= $oP->GetP(<<<HTML
<button onClick="$('textarea[name=expression]')
.val('$sEscapedExpression')
.focus();">Use this query</button>
HTML
$oUseSuggestedQueryButton = ButtonFactory::MakeForDestructiveAction('Use this query');
$oUseSuggestedQueryButton->SetOnClickJsCode(<<<JS
let \$oQueryTextarea = $('textarea[name=expression]');
\$oQueryTextarea.val('$sEscapedExpression').focus();
\$oQueryTextarea.closest('form').submit();
JS
);
$oSyntaxErrorPanel->AddSubBlock($oUseSuggestedQueryButton);
} else {
$sSyntaxErrorText = $oP->GetP($e->getHtmlDesc());
$oSyntaxErrorPanel->AddSubBlock(HtmlFactory::MakeP($e->getHtmlDesc()));
}
} else {
$sSyntaxErrorText = $oP->GetP($e->getHtmlDesc());
$oSyntaxErrorPanel->AddSubBlock(HtmlFactory::MakeP($e->getHtmlDesc()));
}
} else {
$sSyntaxErrorText = $oP->GetP($e->getMessage());
$oSyntaxErrorPanel->AddSubBlock(HtmlFactory::MakeP($e->getMessage()));
}
$oSyntaxErrorPanel = AlertFactory::MakeForFailure(Dict::S('UI:RunQuery:Error'), $sSyntaxErrorText);
$oSyntaxErrorPanel->SetOpenedByDefault(true);
$oP->AddUiBlock($oSyntaxErrorPanel);
}
}
catch (Exception $e) {

View File

@@ -0,0 +1,33 @@
<?php
/**
* Copyright (C) 2013-2020 Combodo SARL
*
* This file is part of iTop.
*
* iTop is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* iTop is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
*/
namespace Combodo\iTop\Application\UI\Base\Component\Html;
/**
* @package Combodo\iTop\Application\UI\Base\Component\Html
* @since 3.0.0
*/
class HtmlFactory
{
public static function MakeP(string $sContent): Html
{
return new Html('<p>'.$sContent.'</p>');
}
}