mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 07:24:13 +01:00
N°8796 - Add PHP code style validation in iTop and extensions - format whole code base
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* @copyright Copyright (C) 2010-2024 Combodo SAS
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
@@ -41,26 +42,26 @@ function ShowExamples($oP, $sExpression)
|
||||
{
|
||||
$bUsingExample = false;
|
||||
|
||||
$aExamples = array(
|
||||
'Pedagogic examples' => array(
|
||||
$aExamples = [
|
||||
'Pedagogic examples' => [
|
||||
"Person" => "SELECT Person",
|
||||
"Person having an 'A' in their name" => "SELECT Person AS B WHERE B.name LIKE '%A%'",
|
||||
"Organization having a name beginning by 'My'" => "SELECT Organization WHERE name REGEXP '^My .*$'",
|
||||
"Changes planned on new year's day" => "SELECT Change AS ch WHERE ch.start_date >= '2009-12-31' AND ch.end_date <= '2010-01-01'",
|
||||
"IPs in a range" => "SELECT DatacenterDevice AS dev WHERE INET_ATON(dev.managementip) > INET_ATON('10.22.32.224') AND INET_ATON(dev.managementip) < INET_ATON('10.22.32.255')",
|
||||
"Persons below a given root organization" => "SELECT Person AS P JOIN Organization AS Node ON P.org_id = Node.id JOIN Organization AS Root ON Node.parent_id BELOW Root.id WHERE Root.id=1",
|
||||
),
|
||||
'Usefull examples' => array(
|
||||
],
|
||||
'Usefull examples' => [
|
||||
"NW interfaces of equipment in production for customer 'Demo'" => "SELECT PhysicalInterface AS if JOIN DatacenterDevice AS dev ON if.connectableci_id = dev.id WHERE dev.status = 'production' AND dev.organization_name = 'Demo'",
|
||||
"My tickets" => "SELECT Ticket AS t WHERE t.agent_id = :current_contact_id",
|
||||
"People being owner of an active ticket" => "SELECT Person AS p JOIN UserRequest AS u ON u.agent_id = p.id WHERE u.status != 'closed'",
|
||||
"Contracts terminating in the next thirty days" => "SELECT Contract AS c WHERE c.end_date > NOW() AND c.end_date < DATE_ADD(NOW(), INTERVAL 30 DAY)",
|
||||
"Orphan tickets (opened one hour ago, still not assigned)" => "SELECT UserRequest AS u WHERE u.start_date < DATE_SUB(NOW(), INTERVAL 60 MINUTE) AND u.status = 'new'",
|
||||
"Long lasting incidents (duration > 8 hours)" => "SELECT UserRequest AS u WHERE u.close_date > DATE_ADD(u.start_date, INTERVAL 8 HOUR)",
|
||||
),
|
||||
);
|
||||
],
|
||||
];
|
||||
|
||||
$aDisplayData = array();
|
||||
$aDisplayData = [];
|
||||
$oAppContext = new ApplicationContext();
|
||||
$sContext = $oAppContext->GetForForm();
|
||||
foreach ($aExamples as $sTopic => $aQueries) {
|
||||
@@ -81,23 +82,23 @@ function ShowExamples($oP, $sExpression)
|
||||
$oFormButton->AddSubBlock($oButton);
|
||||
$oFormButton->AddSubBlock(new Html($sContext));
|
||||
//$aDisplayData[$sTopic][] = array(
|
||||
$aDisplayData[Dict::S('UI:RunQuery:QueryExamples')][] = array(
|
||||
$aDisplayData[Dict::S('UI:RunQuery:QueryExamples')][] = [
|
||||
'desc' => "<div class=\"$sHighlight\">".utils::EscapeHtml($sDescription)."</div>",
|
||||
'oql' => "<div class=\"$sHighlight\">".utils::EscapeHtml($sOql)."</div>",
|
||||
'go' => BlockRenderer::RenderBlockTemplates($oFormButton),
|
||||
);
|
||||
];
|
||||
}
|
||||
}
|
||||
$aDisplayConfig = array();
|
||||
$aDisplayConfig['desc'] = array(
|
||||
$aDisplayConfig = [];
|
||||
$aDisplayConfig['desc'] = [
|
||||
'label' => Dict::S('UI:RunQuery:HeaderPurpose'),
|
||||
'description' => Dict::S('UI:RunQuery:HeaderPurpose+'),
|
||||
);
|
||||
$aDisplayConfig['oql'] = array(
|
||||
];
|
||||
$aDisplayConfig['oql'] = [
|
||||
'label' => Dict::S('UI:RunQuery:HeaderOQLExpression'),
|
||||
'description' => Dict::S('UI:RunQuery:HeaderOQLExpression+'),
|
||||
);
|
||||
$aDisplayConfig['go'] = array('label' => '', 'description' => '');
|
||||
];
|
||||
$aDisplayConfig['go'] = ['label' => '', 'description' => ''];
|
||||
|
||||
foreach ($aDisplayData as $sTopic => $aQueriesDisplayData) {
|
||||
$bShowOpened = $bUsingExample;
|
||||
@@ -120,8 +121,7 @@ $sEncoding = utils::ReadParam('encoding', 'oql');
|
||||
|
||||
ShowExamples($oP, $sExpression);
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
if ($sEncoding == 'crypted') {
|
||||
// Translate $sExpression into a oql expression
|
||||
$oFilter = DBObjectSearch::unserialize($sExpression);
|
||||
@@ -129,32 +129,23 @@ try
|
||||
}
|
||||
|
||||
$oFilter = null;
|
||||
$aArgs = array();
|
||||
$aArgs = [];
|
||||
$sSyntaxError = null;
|
||||
|
||||
if (!empty($sExpression))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!empty($sExpression)) {
|
||||
try {
|
||||
$oFilter = DBObjectSearch::FromOQL($sExpression);
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
if ($e instanceof OqlException)
|
||||
{
|
||||
} catch (Exception $e) {
|
||||
if ($e instanceof OqlException) {
|
||||
$sSyntaxError = $e->getHtmlDesc();
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$sSyntaxError = $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
if ($oFilter)
|
||||
{
|
||||
$aArgs = array();
|
||||
foreach($oFilter->GetQueryParams() as $sParam => $foo)
|
||||
{
|
||||
if ($oFilter) {
|
||||
$aArgs = [];
|
||||
foreach ($oFilter->GetQueryParams() as $sParam => $foo) {
|
||||
$value = utils::ReadParam('arg_'.$sParam, null, true, 'raw_data');
|
||||
if (!is_null($value)) {
|
||||
$aArgs[$sParam] = $value;
|
||||
@@ -180,7 +171,8 @@ try
|
||||
$oQueryTextArea->AddCSSClasses(['ibo-input-text', 'ibo-query-oql', 'ibo-is-code']);
|
||||
$oQueryForm->AddSubBlock($oQueryTextArea);
|
||||
|
||||
$oP->add_ready_script(<<<JS
|
||||
$oP->add_ready_script(
|
||||
<<<JS
|
||||
$("#expression").trigger('select');
|
||||
$("#expression").on('keyup', function (oEvent) {
|
||||
if ((oEvent.ctrlKey || oEvent.metaKey) && oEvent.key === 'Enter') {
|
||||
@@ -193,11 +185,11 @@ JS
|
||||
if (count($aArgs) > 0) {
|
||||
//--- Query arguments
|
||||
$oQueryForm->AddSubBlock(TitleUIBlockFactory::MakeNeutral(Dict::S('UI:RunQuery:QueryArguments'), 3)->AddCSSClass("ibo-collapsible-section--title"));
|
||||
$oQueryArgsContainer = UIContentBlockUIBlockFactory::MakeStandard(null,['wizContainer']);
|
||||
$oQueryArgsContainer = UIContentBlockUIBlockFactory::MakeStandard(null, ['wizContainer']);
|
||||
$oQueryForm->AddSubBlock($oQueryArgsContainer);
|
||||
foreach ($aArgs as $sParam => $sValue) {
|
||||
$oInput = InputUIBlockFactory::MakeStandard("text",'arg_'.$sParam, $sValue);
|
||||
$oArgInput = \Combodo\iTop\Application\UI\Base\Component\Field\FieldUIBlockFactory::MakeFromObject($sParam,$oInput,Field::ENUM_FIELD_LAYOUT_SMALL);
|
||||
$oInput = InputUIBlockFactory::MakeStandard("text", 'arg_'.$sParam, $sValue);
|
||||
$oArgInput = \Combodo\iTop\Application\UI\Base\Component\Field\FieldUIBlockFactory::MakeFromObject($sParam, $oInput, Field::ENUM_FIELD_LAYOUT_SMALL);
|
||||
$oArgInput->AddCSSClass("ibo-field--label-small");
|
||||
//$oArgInput = InputUIBlockFactory::MakeForInputWithLabel( $sParam, 'arg_'.$sParam, $sValue );
|
||||
$oQueryArgsContainer->AddSubBlock($oArgInput);
|
||||
@@ -216,10 +208,9 @@ JS
|
||||
$oQueryForm->AddSubBlock($oToolbarButtons);
|
||||
$oToolbarButtons->AddSubBlock($oQuerySubmit);
|
||||
|
||||
|
||||
if ($oFilter) {
|
||||
//--- Query filter
|
||||
$oPanelResult= PanelUIBlockFactory::MakeWithBrandingSecondaryColor(Dict::S('UI:RunQuery:QueryResults'));
|
||||
$oPanelResult = PanelUIBlockFactory::MakeWithBrandingSecondaryColor(Dict::S('UI:RunQuery:QueryResults'));
|
||||
$oP->AddSubBlock($oPanelResult);
|
||||
$oResultBlock = new DisplayBlock($oFilter, 'list', false);
|
||||
$oPanelResult->AddSubBlock($oResultBlock->GetDisplay($oP, 'runquery'));
|
||||
@@ -228,10 +219,10 @@ JS
|
||||
//$iCount = $oResultBlock->GetDisplayedCount();
|
||||
$sPageId = "ui-search-".$oFilter->GetClass();
|
||||
$sLabel = MetaModel::GetName($oFilter->GetClass());
|
||||
$aArgs = array();
|
||||
$aArgs = [];
|
||||
foreach (array_merge($_POST, $_GET) as $sKey => $value) {
|
||||
if (is_array($value)) {
|
||||
$aItems = array();
|
||||
$aItems = [];
|
||||
foreach ($value as $sItemKey => $sItemValue) {
|
||||
$aArgs[] = $sKey.'['.$sItemKey.']='.urlencode($sItemValue);
|
||||
}
|
||||
@@ -242,7 +233,6 @@ JS
|
||||
$sUrl = utils::GetAbsoluteUrlAppRoot().'pages/run_query.php?'.implode('&', $aArgs);
|
||||
$oP->SetBreadCrumbEntry($sPageId, $sLabel, $oFilter->ToOQL(true), $sUrl, 'fas fa-terminal', iTopWebPage::ENUM_BREADCRUMB_ENTRY_ICON_TYPE_CSS_CLASSES);
|
||||
|
||||
|
||||
//--- More info
|
||||
$aMoreInfoBlocks = [];
|
||||
|
||||
@@ -254,14 +244,13 @@ JS
|
||||
$oSerializedQuerySet->AddSubBlock(UIContentBlockUIBlockFactory::MakeForCode($oFilter->serialize()));
|
||||
$aMoreInfoBlocks[] = $oSerializedQuerySet;
|
||||
|
||||
|
||||
$aModifierProperties = MetaModel::MakeModifierProperties($oFilter);
|
||||
|
||||
// Avoid adding all the fields for counts or "group by" requests
|
||||
$aCountAttToLoad = array();
|
||||
$aCountAttToLoad = [];
|
||||
$sMainClass = null;
|
||||
foreach ($oFilter->GetSelectedClasses() as $sClassAlias => $sClass) {
|
||||
$aCountAttToLoad[$sClassAlias] = array();
|
||||
$aCountAttToLoad[$sClassAlias] = [];
|
||||
if (empty($sMainClass)) {
|
||||
$sMainClass = $sClass;
|
||||
}
|
||||
@@ -279,7 +268,7 @@ JS
|
||||
}
|
||||
|
||||
// SQL Count
|
||||
$sSQL = $oFilter->MakeSelectQuery(array(), $aRealArgs, $aCountAttToLoad, null, 0, 0, true);
|
||||
$sSQL = $oFilter->MakeSelectQuery([], $aRealArgs, $aCountAttToLoad, null, 0, 0, true);
|
||||
$oCountResultQuerySet = new FieldSet(Dict::S('UI:RunQuery:ResultSQLCount'));
|
||||
$oCountResultQuerySet->AddSubBlock(UIContentBlockUIBlockFactory::MakeForCode($sSQL));
|
||||
$aMoreInfoBlocks[] = $oCountResultQuerySet;
|
||||
@@ -324,13 +313,13 @@ JS
|
||||
$sEscapedExpression = json_encode(utils::EscapeHtml($sFixedExpression));
|
||||
$oUseSuggestedQueryButton = ButtonUIBlockFactory::MakeForDestructiveAction('Use this query');
|
||||
$oUseSuggestedQueryButton->SetOnClickJsCode(
|
||||
<<<JS
|
||||
<<<JS
|
||||
let \$oQueryTextarea = $('textarea[name=expression]');
|
||||
\$oQueryTextarea.val($sEscapedExpression).trigger('focus');
|
||||
\$oQueryTextarea.closest('form').trigger('submit');
|
||||
JS
|
||||
);
|
||||
$oSyntaxErrorPanel->AddSubBlock($oUseSuggestedQueryButton);
|
||||
$oSyntaxErrorPanel->AddSubBlock($oUseSuggestedQueryButton);
|
||||
} else {
|
||||
$oSyntaxErrorPanel->AddSubBlock(HtmlFactory::MakeParagraph($e->getHtmlDesc()));
|
||||
}
|
||||
@@ -341,8 +330,7 @@ JS
|
||||
$oSyntaxErrorPanel->AddSubBlock(HtmlFactory::MakeParagraph($e->getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception $e) {
|
||||
} catch (Exception $e) {
|
||||
$oErrorAlert = AlertUIBlockFactory::MakeForFailure(
|
||||
Dict::Format('UI:RunQuery:Error', $e->getMessage()),
|
||||
'<pre>'.$e->getTraceAsString().'</pre>'
|
||||
@@ -352,4 +340,3 @@ catch (Exception $e) {
|
||||
}
|
||||
|
||||
$oP->output();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user