extract methods for apply context on oql

This commit is contained in:
acognet
2021-04-30 22:41:44 +02:00
parent d0d254ad59
commit 2cc4448a15
6 changed files with 68 additions and 87 deletions

View File

@@ -0,0 +1,60 @@
<?php
/*
* @copyright Copyright (C) 2010-2021 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
/**
* Class DBSearchHelper
*
* @since 3.0.0
*/
class DBSearchHelper
{
/**
* Add context filter to DBUnionSearch
*
* @param \DBSearch|null $oSearch
*
* @throws \Exception
* @since 3.0.0
*/
public static function AddContextFilter(?DBSearch $oSearch): void
{
$oAppContext = new ApplicationContext();
$sClass = $oSearch->GetClass();
foreach ($oAppContext->GetNames() as $key) {
// Find the value of the object corresponding to each 'context' parameter
$aCallSpec = [$sClass, 'MapContextParam'];
$sAttCode = '';
if (is_callable($aCallSpec)) {
$sAttCode = call_user_func($aCallSpec, $key); // Returns null when there is no mapping for this parameter
}
if (MetaModel::IsValidAttCode($sClass, $sAttCode)) {
// Add Hierarchical condition if hierarchical key
$oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
if (isset($oAttDef) && ($oAttDef->IsExternalKey())) {
$iDefaultValue = intval($oAppContext->GetCurrentValue($key));
if ($iDefaultValue != 0) {
try {
/** @var AttributeExternalKey $oAttDef */
$sTargetClass = $oAttDef->GetTargetClass();
$sHierarchicalKeyCode = MetaModel::IsHierarchicalClass($sTargetClass);
if ($sHierarchicalKeyCode !== false) {
$oFilter = new DBObjectSearch($sTargetClass);
$oFilter->AddCondition('id', $iDefaultValue);
$oHKFilter = new DBObjectSearch($sTargetClass);
$oHKFilter->AddCondition_PointingTo($oFilter, $sHierarchicalKeyCode, TREE_OPERATOR_BELOW);
$oSearch->AddCondition_PointingTo($oHKFilter, $sAttCode);
}
}
catch (Exception $e) {
// If filtering fails just ignore it
}
}
}
}
}
}
}

View File

@@ -1,20 +1,7 @@
<?php
/**
* Copyright (C) 2013-2021 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
/*
* @copyright Copyright (C) 2010-2021 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
use Combodo\iTop\Application\UI\Base\Component\Title\TitleUIBlockFactory;
@@ -729,41 +716,7 @@ abstract class MenuNode
// Count the entries up to 99
$oSearch = DBSearch::FromOQL($sOQL);
$oAppContext = new ApplicationContext();
$sClass = $oSearch->GetClass();
foreach ($oAppContext->GetNames() as $key) {
// Find the value of the object corresponding to each 'context' parameter
$aCallSpec = [$sClass, 'MapContextParam'];
$sAttCode = '';
if (is_callable($aCallSpec)) {
$sAttCode = call_user_func($aCallSpec, $key); // Returns null when there is no mapping for this parameter
}
if (MetaModel::IsValidAttCode($sClass, $sAttCode)) {
// Add Hierarchical condition if hierarchical key
$oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
if (isset($oAttDef) && ($oAttDef->IsExternalKey())) {
$iDefaultValue = intval($oAppContext->GetCurrentValue($key));
if ($iDefaultValue != 0) {
try {
/** @var AttributeExternalKey $oAttDef */
$sTargetClass = $oAttDef->GetTargetClass();
$sHierarchicalKeyCode = MetaModel::IsHierarchicalClass($sTargetClass);
if ($sHierarchicalKeyCode !== false) {
$oFilter = new DBObjectSearch($sTargetClass);
$oFilter->AddCondition('id', $iDefaultValue);
$oHKFilter = new DBObjectSearch($sTargetClass);
$oHKFilter->AddCondition_PointingTo($oFilter, $sHierarchicalKeyCode, TREE_OPERATOR_BELOW);
$oSearch->AddCondition_PointingTo($oHKFilter, $sAttCode);
}
}
catch (Exception $e) {
// If filtering fails just ignore it
}
}
}
}
}
DBSearchHelper::AddContextFilter($oSearch);
$oSet = new DBObjectSet($oSearch);
$iCount = $oSet->CountWithLimit(99);

View File

@@ -42,8 +42,6 @@ namespace Composer\Autoload;
*/
class ClassLoader
{
private $vendorDir;
// PSR-4
private $prefixLengthsPsr4 = array();
private $prefixDirsPsr4 = array();
@@ -59,13 +57,6 @@ class ClassLoader
private $missingClasses = array();
private $apcuPrefix;
private static $registeredLoaders = array();
public function __construct($vendorDir = null)
{
$this->vendorDir = $vendorDir;
}
public function getPrefixes()
{
if (!empty($this->prefixesPsr0)) {
@@ -309,17 +300,6 @@ class ClassLoader
public function register($prepend = false)
{
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
if (null === $this->vendorDir) {
return;
}
if ($prepend) {
self::$registeredLoaders = array($this->vendorDir => $this) + self::$registeredLoaders;
} else {
unset(self::$registeredLoaders[$this->vendorDir]);
self::$registeredLoaders[$this->vendorDir] = $this;
}
}
/**
@@ -328,10 +308,6 @@ class ClassLoader
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
if (null !== $this->vendorDir) {
unset(self::$registeredLoaders[$this->vendorDir]);
}
}
/**
@@ -391,16 +367,6 @@ class ClassLoader
return $file;
}
/**
* Returns the currently registered loaders indexed by their corresponding vendor directories.
*
* @return self[]
*/
public static function getRegisteredLoaders()
{
return self::$registeredLoaders;
}
private function findFileWithExtension($class, $ext)
{
// PSR-4 lookup

View File

@@ -371,6 +371,7 @@ return array(
'DBObjectSetComparator' => $baseDir . '/core/dbobjectset.class.php',
'DBProperty' => $baseDir . '/core/dbproperty.class.inc.php',
'DBSearch' => $baseDir . '/core/dbsearch.class.php',
'DBSearchHelper' => $baseDir . '/application/DBSearchHelper.php',
'DBUnionSearch' => $baseDir . '/core/dbunionsearch.class.php',
'DailyRotatingLogFileNameBuilder' => $baseDir . '/core/log.class.inc.php',
'Dashboard' => $baseDir . '/application/dashboard.class.inc.php',
@@ -1917,7 +1918,7 @@ return array(
'TemplateString' => $baseDir . '/core/templatestring.class.inc.php',
'TemplateStringPlaceholder' => $baseDir . '/core/templatestring.class.inc.php',
'ThemeHandler' => $baseDir . '/application/themehandler.class.inc.php',
'ThemeHandlerService' => $baseDir . '/application/themehandlerservice.class.inc.php',
'ThemeHandlerService' => $baseDir . '/application/themehandlerservice.class.inc.php',
'ToolsLog' => $baseDir . '/core/log.class.inc.php',
'Trigger' => $baseDir . '/core/trigger.class.inc.php',
'TriggerOnObject' => $baseDir . '/core/trigger.class.inc.php',

View File

@@ -25,7 +25,7 @@ class ComposerAutoloaderInit0018331147de7601e7552f7da8e3bb8b
require __DIR__ . '/platform_check.php';
spl_autoload_register(array('ComposerAutoloaderInit0018331147de7601e7552f7da8e3bb8b', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
spl_autoload_unregister(array('ComposerAutoloaderInit0018331147de7601e7552f7da8e3bb8b', 'loadClassLoader'));
$includePaths = require __DIR__ . '/include_paths.php';

View File

@@ -601,6 +601,7 @@ class ComposerStaticInit0018331147de7601e7552f7da8e3bb8b
'DBObjectSetComparator' => __DIR__ . '/../..' . '/core/dbobjectset.class.php',
'DBProperty' => __DIR__ . '/../..' . '/core/dbproperty.class.inc.php',
'DBSearch' => __DIR__ . '/../..' . '/core/dbsearch.class.php',
'DBSearchHelper' => __DIR__ . '/../..' . '/application/DBSearchHelper.php',
'DBUnionSearch' => __DIR__ . '/../..' . '/core/dbunionsearch.class.php',
'DailyRotatingLogFileNameBuilder' => __DIR__ . '/../..' . '/core/log.class.inc.php',
'Dashboard' => __DIR__ . '/../..' . '/application/dashboard.class.inc.php',