Files
iTop/sources/Application/UI/Links/Set/LinksSetUIBlockFactory.php
2023-03-17 18:28:47 +01:00

133 lines
5.1 KiB
PHP

<?php
/**
* Copyright (C) 2013-2023 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\Links\Set;
use AttributeLinkedSet;
use Combodo\iTop\Application\UI\Base\Component\Input\Set\Set;
use Combodo\iTop\Application\UI\Base\Component\Input\Set\SetUIBlockFactory;
use Combodo\iTop\Service\Links\LinksBulkDataPostProcessor;
use Combodo\iTop\Service\Links\LinkSetDataTransformer;
use Combodo\iTop\Service\Links\LinkSetModel;
use Combodo\iTop\Service\Links\LinkSetRepository;
use DBObject;
use iDBObjectSetIterator;
/**
* Class LinksSetUIBlockFactory
*
* @api
*
* @since 3.1.0
* @package Combodo\iTop\Application\UI\Links\Set
*/
class LinksSetUIBlockFactory extends SetUIBlockFactory
{
/**
* Make a link set block.
*
* @param string $sId Block identifier
* @param AttributeLinkedSet $oAttDef Link set attribute definition
* @param iDBObjectSetIterator $oDbObjectSet Link set value
* @param string $sWizardHelperJsVarName Wizard helper name
* @param DBObject|null $oHostDbObject Host DB object
*
* @return \Combodo\iTop\Application\UI\Base\Component\Input\Set\Set
*/
public static function MakeForLinkSet(string $sId, AttributeLinkedSet $oAttDef, iDBObjectSetIterator $oDbObjectSet, string $sWizardHelperJsVarName, DBObject $oHostDbObject = null): Set
{
$sTargetClass = LinkSetModel::GetTargetClass($oAttDef);
$sTargetField = LinkSetModel::GetTargetField($oAttDef);
// Set UI block for OQL
$oSetUIBlock = SetUIBlockFactory::MakeForOQL($sId, $sTargetClass, $oAttDef->GetValuesDef()->GetFilterExpression(), $sWizardHelperJsVarName);
$oSetUIBlock->AddJsFileRelPath('js/links/links_set.js');
// Remove add button for 3_1_lot1
// Linkset controller OperationCreateLinkedObject need the host object to exist, so if we are in creation of the host object (id=-1) the linked object creation doesn't work.
//
// // Add button behaviour
// if (in_array($oAttDef->GetEditMode(), [LINKSET_EDITMODE_ADDREMOVE, LINKSET_EDITMODE_ADDONLY, LINKSET_EDITMODE_INPLACE, LINKSET_EDITMODE_ACTIONS])
// && $oHostDbObject !== null) {
// $sHostClass = get_class($oHostDbObject);
// $oSetUIBlock->SetHasAddOptionButton(true);
// $oSetUIBlock->SetAddOptionButtonJsOnClick("CombodoLinkSet.CreateLinkedObject('{$oAttDef->GetLinkedClass()}', '{$oAttDef->GetCode()}', '{$sHostClass}', '{$oHostDbObject->GetKey()}', '{$sTargetField}', '{$sTargetClass}', oWidget{$oSetUIBlock->GetId()} );");
// }
// Current value
$aCurrentValues = LinkSetDataTransformer::Decode($oDbObjectSet, $sTargetClass, $sTargetField);
// Initial options data
$aInitialOptions = LinkSetRepository::LinksDbSetToTargetObjectArray($oDbObjectSet, $sTargetClass, $sTargetField);
if ($aInitialOptions !== null) {
$oSetUIBlock->GetDataProvider()->SetOptions($aInitialOptions);
// Set value
$oSetUIBlock->SetValue(json_encode($aCurrentValues));
} else {
$oSetUIBlock->SetHasError(true);
}
return $oSetUIBlock;
}
/**
* Make a link set block for bulk modify.
*
* @param string $sId Block identifier
* @param AttributeLinkedSet $oAttDef Link set attribute definition
* @param iDBObjectSetIterator $oDbObjectSet Link set value
* @param string $sWizardHelperJsVarName Wizard helper name
* @param array $aBulkContext
*
* @return \Combodo\iTop\Application\UI\Base\Component\Input\Set\Set
*/
public static function MakeForBulkLinkSet(string $sId, AttributeLinkedSet $oAttDef, iDBObjectSetIterator $oDbObjectSet, string $sWizardHelperJsVarName, array $aBulkContext): Set
{
$oSetUIBlock = self::MakeForLinkSet($sId, $oAttDef, $oDbObjectSet, $sWizardHelperJsVarName);
// Bulk modify specific
$oSetUIBlock->GetDataProvider()->SetGroupField('group');
$oSetUIBlock->SetIsMultiValuesSynthesis(true);
// Data post processing
$aBinderSettings = [
'bulk_oql' => $aBulkContext['oql'],
'link_class' => LinkSetModel::GetLinkedClass($oAttDef),
'target_field' => LinkSetModel::GetTargetField($oAttDef),
'origin_field' => $oAttDef->GetExtKeyToMe(),
];
// Initial options
$aOptions = $oSetUIBlock->GetDataProvider()->GetOptions();
$aOptions = LinksBulkDataPostProcessor::Execute($aOptions, $aBinderSettings);
$oSetUIBlock->GetDataProvider()->SetOptions($aOptions);
// Data provider post processor
/** @var \Combodo\iTop\Application\UI\Base\Component\Input\Set\DataProvider\AjaxDataProvider $oDataProvider */
$oDataProvider = $oSetUIBlock->GetDataProvider();
$oDataProvider->SetPostParam('data_post_processor', [
'class_name' => addslashes(LinksBulkDataPostProcessor::class),
'settings' => $aBinderSettings,
]);
return $oSetUIBlock;
}
}